Nicolas Winkler 6 gadi atpakaļ
vecāks
revīzija
f709d73228
8 mainītis faili ar 111 papildinājumiem un 24 dzēšanām
  1. 11 0
      src/AstVisitor.cpp
  2. 9 4
      src/CodeGeneration.cpp
  3. 15 14
      src/CodeGeneration.h
  4. 11 1
      src/CodegenVisitor.cpp
  5. 39 1
      src/Semantic.cpp
  6. 20 0
      src/Semantic.h
  7. 5 3
      src/makefile
  8. 1 1
      src/test.qlw

+ 11 - 0
src/AstVisitor.cpp

@@ -137,10 +137,21 @@ std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::Expression& as
 
 std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::FeatureCall& ast, sem::Scope& scope)
 {
+    auto* method = scope.getMethod(ast.name);
+    auto* var = scope.getVariable(ast.name);
+    
+    if (var) {
+        
+    }
+    else if (method) {
     auto fce = std::make_unique<sem::FeatureCallExpression>();
     //fce->target = unique_dynamic_cast<sem::Expression>(ast.target.accept(*this, classes));
     fce->callee = scope.getMethod(ast.name);
     return fce;
+    }
+    else {
+        throw sem::SemanticException(sem::SemanticException::METHOD_NOT_FOUND, ast.name, ast.pos);
+    }
 }
 
 

+ 9 - 4
src/CodeGeneration.cpp

@@ -75,7 +75,7 @@ void generateObjectFile(const std::string& filename, std::unique_ptr<llvm::Modul
 
 
     printf("verifying mod\n");
-    module->dump();
+    module->print(llvm::errs(), nullptr);
     llvm::verifyModule(*module);
     printf("mod verified\n");
 
@@ -197,14 +197,19 @@ llvm::Function* qlow::gen::FunctionGenerator::generate(void)
     }
     
     for (auto& statement : method.body->statements) {
-        printf("statement visit\n");
+#ifdef DEBUGGING
+        printf("statement visit %s\n", statement->toString().c_str());
+#endif
         statement->accept(statementVisitor, *this);
     }
     
 
+#ifdef DEBUGGING
+    printf("End of Function\n");
+#endif
     
-    Value* val = llvm::ConstantFP::get(context, llvm::APFloat(5.0));
-    builder.CreateRet(val);
+    //Value* val = llvm::ConstantFP::get(context, llvm::APFloat(5.0));
+    builder.CreateRetVoid();
 
     return func;
 }

+ 15 - 14
src/CodeGeneration.h

@@ -21,27 +21,28 @@ namespace gen
 
 class qlow::gen::FunctionGenerator
 {
-const sem::Method& method;
-llvm::Module* module;
+    const sem::Method& method;
+    llvm::Module* module;
 
-std::stack<llvm::BasicBlock*> basicBlocks;
+    std::stack<llvm::BasicBlock*> basicBlocks;
 
 public:
 
-StatementVisitor statementVisitor;
-ExpressionVisitor expressionVisitor;
+    StatementVisitor statementVisitor;
+    ExpressionVisitor expressionVisitor;
 
-inline FunctionGenerator(const sem::Method& m, llvm::Module* module) :
-    method{ m }, module{ module }
-{
-}
+    inline FunctionGenerator(const sem::Method& m, llvm::Module* module) :
+        method{ m }, module{ module }
+    {
+    }
 
-llvm::Function* generate(void);
+    llvm::Function* generate(void);
 
-inline llvm::LLVMContext& getContext(void) const { return module->getContext(); }
-inline llvm::BasicBlock* getCurrentBlock(void) const { return basicBlocks.top(); }
-inline void pushBlock(llvm::BasicBlock* bb) { basicBlocks.push(bb); }
-inline llvm::BasicBlock* popBlock(void) { auto* bb = basicBlocks.top(); basicBlocks.pop(); return bb; }
+    inline llvm::Module* getModule(void) const { return module; }
+    inline llvm::LLVMContext& getContext(void) const { return module->getContext(); }
+    inline llvm::BasicBlock* getCurrentBlock(void) const { return basicBlocks.top(); }
+    inline void pushBlock(llvm::BasicBlock* bb) { basicBlocks.push(bb); }
+    inline llvm::BasicBlock* popBlock(void) { auto* bb = basicBlocks.top(); basicBlocks.pop(); return bb; }
 };
 
 

+ 11 - 1
src/CodegenVisitor.cpp

@@ -12,6 +12,11 @@ llvm::Value* ExpressionVisitor::visit(sem::BinaryOperation& binop, llvm::IRBuild
     using llvm::Value;
     Value* left = binop.left->accept(*this, builder);
     Value* right = binop.right->accept(*this, builder);
+    if (left == nullptr) {
+        printf("WOW: %s\n", binop.left->toString().c_str());
+    }
+    
+    
     switch (binop.op) {
         case ast::Operation::Operator::PLUS:
             return builder.CreateAdd(left, right, "add");
@@ -71,8 +76,13 @@ llvm::Value* StatementVisitor::visit(sem::AssignmentStatement& assignment,
 }
 
 
-llvm::Value* StatementVisitor::visit(sem::FeatureCallStatement& assignment, gen::FunctionGenerator& fg)
+llvm::Value* StatementVisitor::visit(sem::FeatureCallStatement& fc, gen::FunctionGenerator& fg)
 {
+    llvm::Module* module = fg.getModule();
+    llvm::IRBuilder<> builder(fg.getContext());
+    builder.SetInsertPoint(fg.getCurrentBlock());
+    llvm::Constant* c = module->getOrInsertFunction(fc.expr->callee->name, {});
+    builder.CreateCall(c, {});
     return llvm::ConstantFP::get(fg.getContext(), llvm::APFloat(5.0));
 }
 

+ 39 - 1
src/Semantic.cpp

@@ -113,6 +113,12 @@ std::string Class::toString(void) const
 }
 
 
+std::string Variable::toString(void) const
+{
+    return "Variable[" + this->name + "]";
+}
+
+
 std::string Field::toString(void) const
 {
     return "Field[" + this->name + "]";
@@ -140,11 +146,43 @@ ACCEPT_DEFINITION(IntConst, ExpressionVisitor, llvm::IRBuilder<>&)
 ACCEPT_DEFINITION(AssignmentStatement, StatementVisitor, qlow::gen::FunctionGenerator&) 
 ACCEPT_DEFINITION(FeatureCallStatement, StatementVisitor, qlow::gen::FunctionGenerator&) 
 
+std::string AssignmentStatement::toString(void) const
+{
+    return "AssignmentStatement[" + this->target->toString() + " := " +
+        this->value->toString() + "]";
+}
+
+
+std::string BinaryOperation::toString(void) const
+{
+    return "BinaryOperation[" + left->toString() + ", " +
+        right->toString() + "]";
+}
+
+
+std::string UnaryOperation::toString(void) const
+{
+    return "UnaryOperation[" + arg->toString() + "]";
+}
+
+
+std::string FeatureCallExpression::toString(void) const
+{
+    return "FeatureCallExpression[" + callee->toString() + "]";
+}
+
+
+std::string FeatureCallStatement::toString(void) const
+{
+    return "FeatureCallStatement[" + expr->callee->toString() + "]";
+}
+
 
 std::string SemanticException::getMessage(void) const
 {
-    std::map<ErrorCode, std::string> error = {
+    static std::map<ErrorCode, std::string> error = {
         {UNKNOWN_TYPE, "unknown type"},
+        {FEATURE_NOT_FOUND, "method or variable not found"}
     };
     
     std::string pos = std::to_string(where.first_line) + ":" +

+ 20 - 0
src/Semantic.h

@@ -38,6 +38,8 @@ namespace qlow
 
         struct FeatureCallStatement;
         struct AssignmentStatement;
+        
+        struct LocalVariableExpression;
 
         struct Operation;
         struct UnaryOperation;
@@ -107,6 +109,8 @@ struct qlow::sem::Variable : public SemanticObject
     Variable(void) = default;
     inline Variable(Class* type, std::string& name) :
         type{ type }, name{ name }, allocaInst { nullptr } {}
+        
+    virtual std::string toString(void) const override;
 };
 
 
@@ -149,6 +153,7 @@ struct qlow::sem::AssignmentStatement : public Statement
     std::unique_ptr<Expression> target;
     std::unique_ptr<Expression> value;
 
+    virtual std::string toString(void) const override;
     virtual llvm::Value* accept(qlow::StatementVisitor&, gen::FunctionGenerator&);
 };
 
@@ -164,12 +169,20 @@ struct qlow::sem::Operation : public Expression
 };
 
 
+struct qlow::sem::LocalVariableExpression : public Expression
+{
+    
+};
+
+
 struct qlow::sem::BinaryOperation : public Operation
 {
     std::unique_ptr<Expression> left;
     std::unique_ptr<Expression> right;
     
     virtual llvm::Value* accept(ExpressionVisitor& visitor, llvm::IRBuilder<>& arg2);
+    
+    virtual std::string toString(void) const override;
 };
 
 
@@ -178,13 +191,17 @@ struct qlow::sem::UnaryOperation : public Operation
     qlow::ast::UnaryOperation::Side side;
     std::unique_ptr<Expression> arg;
     virtual llvm::Value* accept(ExpressionVisitor& visitor, llvm::IRBuilder<>& arg2);
+    virtual std::string toString(void) const override;
 };
 
+
 struct qlow::sem::FeatureCallExpression : public Expression
 {
     Method* callee;
     OwningList<Expression> arguments;
     virtual llvm::Value* accept(ExpressionVisitor& visitor, llvm::IRBuilder<>& arg2);
+    
+    virtual std::string toString(void) const override;
 };
 
 
@@ -207,6 +224,7 @@ struct qlow::sem::FeatureCallStatement : public Statement
     inline FeatureCallStatement(std::unique_ptr<FeatureCallExpression> expr) :
         expr{ std::move(expr) } {}
 
+    virtual std::string toString(void) const override;
     virtual llvm::Value* accept(qlow::StatementVisitor&, gen::FunctionGenerator&);
 };
 
@@ -222,6 +240,8 @@ public:
         DUPLICATE_CLASS_DEFINITION,
         DUPLICATE_FIELD_DECLARATION,
         DUPLICATE_METHOD_DEFINITION,
+        
+        FEATURE_NOT_FOUND,
     };
     
     

+ 5 - 3
src/makefile

@@ -3,13 +3,15 @@
 
 CXX := g++
 
-INCLUDEDIRS := -I`llvm-config --includedir`
+LLVMCONFIG := llvm-config
+
+INCLUDEDIRS := -I $(shell $(LLVMCONFIG) --includedir)
 CXXFLAGS := -std=c++17 $(INCLUDEDIRS) # -Wall -Wextra
 
 ifdef STATIC
-LDFLAGS := `llvm-config --link-static --ldflags --system-libs --libs all` -static
+LDFLAGS := $(shell $(LLVMCONFIG) --link-static --ldflags --system-libs --libs all) -static
 else
-LDFLAGS := `llvm-config --ldflags --system-libs --libs all`
+LDFLAGS := $(shell $(LLVMCONFIG) --ldflags --system-libs --libs all)
 endif
 
 YACC := bison

+ 1 - 1
src/test.qlw

@@ -13,6 +13,7 @@ class Main
         var := 5;
         var := 102 + var * var;
         var := 102 + var * var;
+
         var := 102 + var * var;
         var := 102 + var * var;
         var := 102 + var * var;
@@ -27,7 +28,6 @@ end
 class FF
     sum_func: Integer do
         variable: Integer;
-        variable := sum_func;
     end
 end