[code generator] more bugfixes
This commit is contained in:
@@ -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<llvm::Module> analysis;
|
||||
// print.run(*module.handle(), analysis);
|
||||
llvm::PrintModulePass print(llvm::outs());
|
||||
llvm::AnalysisManager<llvm::Module> analysis;
|
||||
print.run(llvm_module, analysis);
|
||||
return 0;
|
||||
}
|
||||
|
@@ -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<declaration*>* declarations_t;
|
||||
typedef list<statement*>* statements_t;
|
||||
|
@@ -278,7 +278,7 @@ statement_list
|
||||
|
||||
expression_statement
|
||||
: ';' { $$ = new no_op(); }
|
||||
| expression ';' { $$ = $1; }
|
||||
| expression ';' { $$ = new expression_statement($1); }
|
||||
;
|
||||
|
||||
selection_statement
|
||||
|
@@ -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<ir::return_inst*>(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<ir::binary_operator*>(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<ir::user*>(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<ir::constant*>(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
|
||||
|
@@ -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
|
||||
|
@@ -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();
|
||||
|
@@ -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);
|
||||
}
|
||||
|
Reference in New Issue
Block a user