diff --git a/examples/matrix.cpp b/examples/matrix.cpp index bae0ee52b..6e6389185 100644 --- a/examples/matrix.cpp +++ b/examples/matrix.cpp @@ -7,6 +7,8 @@ #include "llvm/IR/IRPrintingPasses.h" #include "llvm/IR/Module.h" #include "llvm/IR/LLVMContext.h" +#include "llvm/IR/PassManager.h" +#include "llvm/Support/raw_ostream.h" typedef struct yy_buffer_state * YY_BUFFER_STATE; extern int yyparse(); @@ -25,7 +27,7 @@ void test(fp32 *A, fp32 *B, fp32 *C, int32 i){\ int32 u = 1;\ u = u + i;\ if(k == 0)\ - u = u + 2;\ + j = u + 2;\ }\ }\ "; @@ -41,8 +43,8 @@ int main() { llvm::LLVMContext llvm_context; llvm::Module llvm_module("test", llvm_context); tdl::codegen::lowering(module, llvm_module); -// llvm::PrintModulePass print(llvm::outs()); -// llvm::AnalysisManager analysis; -// print.run(*module.handle(), analysis); + llvm::PrintModulePass print(llvm::outs()); + llvm::AnalysisManager analysis; + print.run(llvm_module, analysis); return 0; } diff --git a/include/ast/ast.h b/include/ast/ast.h index 031d49f74..d9b24f8e4 100644 --- a/include/ast/ast.h +++ b/include/ast/ast.h @@ -264,6 +264,17 @@ public: class statement: public node{ }; +class expression_statement: public statement{ +public: + expression_statement(node *expr) + : expr_((expression*)expr){ } + + ir::value* codegen(ir::module * mod) const; + +private: + expression *expr_; +}; + class compound_statement: public statement{ typedef list* declarations_t; typedef list* statements_t; diff --git a/include/ast/parser.y b/include/ast/parser.y index bcfd7498d..2806f49ea 100644 --- a/include/ast/parser.y +++ b/include/ast/parser.y @@ -278,7 +278,7 @@ statement_list expression_statement : ';' { $$ = new no_op(); } - | expression ';' { $$ = $1; } + | expression ';' { $$ = new expression_statement($1); } ; selection_statement diff --git a/include/codegen/lowering.h b/include/codegen/lowering.h index 673bcbf09..a660e5706 100644 --- a/include/codegen/lowering.h +++ b/include/codegen/lowering.h @@ -91,8 +91,8 @@ Instruction *llvm_inst(ir::instruction *inst, LLVMContext & ctx, return PHINode::Create(ty, num_ops, ii->get_name()); } if(auto* ii = dynamic_cast(inst)){ - Value *ret_val = value(ii->get_return_value()); - return ReturnInst::Create(ctx, ret_val); + ir::value *ret_val = ii->get_return_value(); + return ReturnInst::Create(ctx, ret_val?value(ret_val):nullptr); } if(auto* ii = dynamic_cast(inst)){ Value *lhs = value(ii->get_operand(0)); @@ -139,9 +139,9 @@ Value* llvm_value(ir::value *v, LLVMContext &ctx, return vmap.at(v); // create operands if(auto *uu = dynamic_cast(v)) - for(ir::use u: uu->ops()) + for(ir::use u: uu->ops()){ vmap[u.get()] = llvm_value(u, ctx, vmap, bmap); - // constant + } if(auto *cc = dynamic_cast(v)) return llvm_constant(cc, ctx); // instruction @@ -159,12 +159,12 @@ void lowering(ir::module &src, Module &dst){ // iterate over functions for(ir::function *fn: src.get_function_list()) { // create LLVM function - Type *fn_ty = llvm_type(fn->get_type(), dst_ctx); - Function *dst_fn = (Function*)dst.getOrInsertFunction(fn->get_name(), fn_ty); + FunctionType *fn_ty = (FunctionType*)llvm_type(fn->get_fn_type(), dst_ctx); + Function *dst_fn = Function::Create(fn_ty, Function::ExternalLinkage, "kernel", &dst); +// std::cout << ((FunctionType*)fn_ty)->getNumParams() << std::endl; // map parameters - for(unsigned i = 0; i < fn->args().size(); i++) { + for(unsigned i = 0; i < fn->args().size(); i++) vmap[fn->args()[i]] = &*(dst_fn->arg_begin() + i); - } // create blocks for(ir::basic_block *block: fn->blocks()) { BasicBlock *dst_block = BasicBlock::Create(dst_ctx, block->get_name(), dst_fn); @@ -176,6 +176,7 @@ void lowering(ir::module &src, Module &dst){ for(ir::instruction *inst: block->get_inst_list()) { Instruction *dst_inst = llvm_inst(inst, dst_ctx, vmap, bmap); vmap[inst] = dst_inst; + dst_builder.Insert(dst_inst); } } // add phi operands diff --git a/include/ir/function.h b/include/ir/function.h index 4becaa606..4f0762067 100644 --- a/include/ir/function.h +++ b/include/ir/function.h @@ -46,9 +46,10 @@ private: const std::string &name = "", module *parent = nullptr); public: - // arguments + // accessors const args_t &args() { return args_; } - // Factory methods + function_type* get_fn_type() { return fn_ty_; } + // factory methods static function *create(function_type *ty, linkage_types_t linkage, const std::string &name, module *mod); // blocks diff --git a/lib/ast/lowering.cpp b/lib/ast/lowering.cpp index 9dba1e633..7d9d79235 100644 --- a/lib/ast/lowering.cpp +++ b/lib/ast/lowering.cpp @@ -117,6 +117,11 @@ ir::value* compound_statement::codegen(ir::module* mod) const{ return nullptr; } +/* expression statement */ +ir::value* expression_statement::codegen(ir::module *mod) const{ + return expr_->codegen(mod); +} + /* Iteration statement */ ir::value* iteration_statement::codegen(ir::module *mod) const{ ir::builder &builder = mod->get_builder(); diff --git a/lib/ir/instructions.cpp b/lib/ir/instructions.cpp index 03f85bec6..3dd2ccd44 100644 --- a/lib/ir/instructions.cpp +++ b/lib/ir/instructions.cpp @@ -191,7 +191,7 @@ cast_inst *cast_inst::create_integer_cast(value *arg, type *ty, bool is_signed, // return_inst return_inst::return_inst(context &ctx, value *ret_val, instruction *next) - : terminator_inst(type::get_void_ty(ctx), !!ret_val, "", next){ + : terminator_inst(type::get_void_ty(ctx), ret_val!=nullptr, "", next){ if(ret_val) set_operand(0, ret_val); }