Commit f3d23e0e677eb272874af14c84bdc806af390840

Authored by Austin Sun
1 parent 98c5b35993
Exists in master

think i have if done

Showing 2 changed files with 45 additions and 4 deletions Side-by-side Diff

... ... @@ -24,7 +24,7 @@
24 24 }
25 25 //pls work
26 26 llvm::Value* Program::Emit() {
27   - llvm::Module *module = irGen->GetOrCreateModule("swag");
  27 + llvm::Module *module = irGen->GetOrCreateModule("program");
28 28 pushScope();
29 29 for (int i = 0; i < decls->NumElements(); i++){
30 30 decls->Nth(i)->Emit();
31 31  
... ... @@ -136,8 +136,9 @@
136 136 if (cases) cases->PrintAll(indentLevel+1);
137 137 if (def) def->Print(indentLevel+1);
138 138 }
139   -
  139 +//-----------------------------------------------------------------------
140 140 //rest of the emits
  141 +//-----------------------------------------------------------------------
141 142 llvm::Value * StmtBlock::Emit(){
142 143 pushScope();
143 144 for (int i = 0; i < decls->NumElements(); i++){
... ... @@ -147,8 +148,6 @@
147 148 stmts->Nth(i)->Emit();
148 149 }
149 150  
150   - //TODO
151   -
152 151 popScope();
153 152 return NULL;
154 153 }
155 154  
156 155  
... ... @@ -171,8 +170,49 @@
171 170 return NULL;
172 171 }
173 172 //for statement
  173 +
174 174 //while statement
  175 +
175 176 //if statement
  177 +llvm::Value * IfStmt::Emit(){
  178 + llvm::Function * func = irGen->GetFunction();
  179 + llvm::BasicBlock * elseBlock = NULL;
  180 + llvm::BasicBlock * thenBlock = llvm::BasicBlock::Create(*context, "thenBlock", func);
  181 + llvm::BasicBlock * footBlock = llvm::BasicBlock::Create(*context, "footBlock", func);
  182 + llvm::Value * val;
  183 + llvm::Value * cond = test->Emit();
  184 + llvm::LLVMContext * context = irGen->GetContext();
  185 + if(elseBody)
  186 + {
  187 + elseBlock = llvm::BasicBlock::Create(*context, "elseBlock", func);
  188 + }
  189 +
  190 + val = llvm::BranchInst::Create(thenBlock, elseBody ? elseBlock : footBlock, cond, irGen->GetBasicBlock());
  191 + pushScope();
  192 + irGen->SetBasicBlock(thenBlock);
  193 + body->Emit();
  194 +
  195 + if(!irGen->GetBasicBlock()->getTerminator())
  196 + {
  197 + val = llvm::BranchInst::Create(footBlock, irGen->GetBasicBlock());
  198 + }
  199 + popScope();
  200 +
  201 + if(elseBody)
  202 + {
  203 + pushScope();
  204 + irGen->SetBasicBlock(elseBlock);
  205 + elseBody->Emit();
  206 +
  207 + if(!irGen->GetBasicBlock()->getTerminator())
  208 + {
  209 + val = llvm::BranchInst::Create(footBlock, irGen->GetBasicBlock());
  210 + }
  211 + popScope();
  212 + }
  213 + irGen->SetBasicBlock(footBlock);
  214 + return val;
  215 +}
176 216  
177 217 llvm::Value * BreakStmt::Emit(){
178 218 return NULL;
... ... @@ -121,6 +121,7 @@
121 121 const char *GetPrintNameForNode() { return "IfStmt"; }
122 122 void PrintChildren(int indentLevel);
123 123  
  124 + llvm::Value *Emit();
124 125 };
125 126  
126 127 class IfStmtExprError : public IfStmt