Commit f1ba7fa3b582d5668ffe65daff5b923a632929f1

Authored by Austin Sun
1 parent 46e46700e9
Exists in master

i think i fixed them

Showing 1 changed file with 59 additions and 1 deletions Side-by-side Diff

... ... @@ -66,7 +66,7 @@
66 66 if (formals) formals->PrintAll(indentLevel+1, "(formals) ");
67 67 if (body) body->Print(indentLevel+1, "(body) ");
68 68 }
69   -
  69 +/*
70 70 llvm::Value* VarDecl::Emit() {
71 71 llvm::BasicBlock* b = irgen.GetBasicBlock();
72 72 llvm::Value* val;
73 73  
... ... @@ -79,7 +79,25 @@
79 79 addToScope(id->GetName(), this, val);
80 80 return val;
81 81 }
  82 + */
  83 +llvm::Value * VarDecl::Emit(){
  84 + Type * t = type;
  85 + llvm::Type * ltyp=convertType(t);
  86 + llvm::Value * val;
  87 + llvm::Module *mod = irgen.GetOrCreateModule("program");
  88 + if (global()){
  89 + val = new llvm::GlobalVariable(*mod, ltyp, false, llvm::GlobalValue::ExternalLinkage, llvm::Constant::getNullValue(ltyp), llvm::Twine(id->GetName()));
  90 + addToScope(id->GetName(),this, val);
  91 + }
  92 + else{
  93 + val = new llvm::AllocaInst(ltyp, llvm::Twine(id->GetName()), irgen.GetBasicBlock());
  94 + addToScope(id->GetName(),this, val);
  95 + }
  96 + return val;
  97 +}
82 98  
  99 +
  100 +/*
83 101 llvm::Value* FnDecl::Emit() {
84 102 llvm::BasicBlock* oldBlock = irgen.GetBasicBlock();
85 103 llvm::LLVMContext* context = irgen.GetContext();
... ... @@ -104,5 +122,46 @@
104 122 }
105 123 irgen.SetBasicBlock(oldBlock);
106 124 return func;
  125 +}
  126 +*/
  127 +llvm::Value * FnDecl::Emit(){
  128 + llvm::Module *mod = irgen.GetOrCreateModule("program");
  129 + llvm::Type *rt = convertType(this->returnType);
  130 +
  131 + std::vector<llvm::Type *> ref;
  132 + for (int i = 0; i < formals->NumElements(); i++){
  133 + ref.push_back(convertType(formals->Nth(i)->GetType()));
  134 + }
  135 + llvm::ArrayRef<llvm::Type *> ar(ref);
  136 + llvm::FunctionType * ft = llvm::FunctionType::get(rt, ar, false);
  137 +
  138 + llvm::Value * funcval = mod->getOrInsertFunction(llvm::StringRef(this->GetIdentifier()->GetName()), ft);
  139 + llvm::Function * fp = llvm::cast<llvm::Function>(funcval);
  140 +
  141 + addToScope(id->GetName(),this, funcval);
  142 + //TODO is this how you set it?
  143 + irgen.SetFunction(fp);
  144 +
  145 + //SetName
  146 + pushScope();
  147 + llvm::Function::arg_iterator args = fp->arg_begin();
  148 +
  149 + llvm::LLVMContext *context = irgen.GetContext();
  150 + llvm::BasicBlock * varbb = llvm::BasicBlock::Create(*context, "entry", fp);
  151 + irgen.SetBasicBlock(varbb);
  152 + for (int i = 0; i < formals->NumElements(); args++, i++){
  153 + VarDecl * currvd = formals->Nth(i);
  154 + string currname = string(currvd->GetIdentifier()->GetName());
  155 + args->setName(currname);
  156 + llvm::Value * currargv = currvd->Emit();
  157 + llvm::StoreInst * si = new llvm::StoreInst(&*args, currargv, varbb);
  158 + }
  159 + //Emit StmtBlock
  160 + if ( StmtBlock * sb = dynamic_cast<StmtBlock *>(body) ){
  161 + sb->Emit();
  162 + }
  163 + popScope();
  164 + irgen.SetFunction(NULL);
  165 + return funcval;
107 166 }