Commit 98c5b35993332c80acfbb1f877deecb93afb3091

Authored by Austin Sun
1 parent e3f7f8a769
Exists in master

added irgen to node and the basic emit messages in stmt, only missing for,while,if

Showing 5 changed files with 146 additions and 41 deletions Side-by-side Diff

CMakeLists.txt View file @ 98c5b35
  1 +cmake_minimum_required(VERSION 3.5)
  2 +project(cse_131pa4)
  3 +
  4 +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
  5 +
  6 +set(SOURCE_FILES
  7 + ast.cc
  8 + ast.h
  9 + ast_decl.cc
  10 + ast_decl.h
  11 + ast_expr.cc
  12 + ast_expr.h
  13 + ast_stmt.cc
  14 + ast_stmt.h
  15 + ast_type.cc
  16 + ast_type.h
  17 + errors.cc
  18 + errors.h
  19 + irgen.cc
  20 + irgen.h
  21 + list.h
  22 + location.h
  23 + main.cc
  24 + parser.h
  25 + scanner.h
  26 + symtable.cc
  27 + symtable.h
  28 + utility.cc
  29 + utility.h)
  30 +
  31 +add_executable(cse_131pa4 ${SOURCE_FILES})
... ... @@ -51,8 +51,6 @@
51 51 /*------------------------------------------------------------------------------------
52 52 * Symbol Table Information
53 53 *----------------------------------------------------------------------------------*/
54   -SymbolTable symbols;
55   -
56 54 pair<Decl*,llvm::Value *> findSymbol(string s) {
57 55 for(int i = symbols.size() - 1; i >= 0; i--) {
58 56 if(symbols[i].count(s) > 0) return symbols[i][s];
... ... @@ -76,7 +74,7 @@
76 74 }
77 75  
78 76 bool isInScope(string s) {
79   - pair<Decl*,llvm::Value *> tmp = findSymbol(s);
  77 + pair<Decl*, llvm::Value *> tmp = findSymbol(s);
80 78 return tmp.first != NULL;
81 79 }
82 80  
... ... @@ -92,7 +90,7 @@
92 90 symbols.pop_back();
93 91 }
94 92  
95   -void addToScope(string s, Decl* d, Type* t) {
96   - symbols[symbols.size()-1][s] = make_pair(d, t);
  93 +void addToScope(string s, Decl* d, llvm::Value * v) {
  94 + symbols[symbols.size()-1][s] = make_pair(d, v);
97 95 }
... ... @@ -34,6 +34,8 @@
34 34  
35 35 */
36 36  
  37 +#pragma clang diagnostic push
  38 +#pragma ide diagnostic ignored "CannotResolve"
37 39 #ifndef _H_ast
38 40 #define _H_ast
39 41  
... ... @@ -44,6 +46,11 @@
44 46 #include <vector>
45 47 #include <map>
46 48 #include <algorithm>
  49 +//LLVM STUFF
  50 +#include "llvm/IR/Module.h"
  51 +#include "llvm/IR/LLVMContext.h"
  52 +#include "llvm/IR/Instructions.h"
  53 +#include "llvm/IR/Constants.h"
47 54  
48 55 using namespace std;
49 56  
... ... @@ -60,6 +67,8 @@
60 67 public:
61 68 Node(yyltype loc);
62 69 Node();
  70 + static IRGenerator * irGen;
  71 + SymbolTable symbols;
63 72 virtual ~Node() {}
64 73  
65 74 yyltype *GetLocation() { return location; }
66 75  
67 76  
... ... @@ -106,17 +115,19 @@
106 115 * Symbol Table Information
107 116 *----------------------------------------------------------------------------------*/
108 117 typedef vector<map<string,pair<Decl*,llvm::Value *> > > SymbolTable;
109   -pair<Decl*,llvm::Value * v> findSymbol(string s);
110   -pair<Decl*,llvm::Value * v> findSymbol(Decl* d);
  118 +pair<Decl*,llvm::Value *> findSymbol(string s);
  119 +pair<Decl*,llvm::Value *> findSymbol(Decl* d);
111 120 bool isInCurrentScope(string s);
112 121 bool isInCurrentScope(Decl* d);
113 122 bool isInScope(string s);
114 123 bool isInScope(Decl* d);
115 124 void pushScope();
116 125 void popScope();
117   -void addToScope(string s, Decl* d, llvm::Value * v t);
  126 +void addToScope(string s, Decl* d, llvm::Value * v);
118 127 extern SymbolTable symbols;
119 128  
120 129  
121 130 #endif
  131 +
  132 +#pragma clang diagnostic pop
... ... @@ -22,40 +22,18 @@
22 22 decls->PrintAll(indentLevel+1);
23 23 printf("\n");
24 24 }
25   -
  25 +//pls work
26 26 llvm::Value* Program::Emit() {
27   - // TODO:
28   - // This is just a reference for you to get started
29   - //
30   - // You can use this as a template and create Emit() function
31   - // for individual node to fill in the module structure and instructions.
32   - //
33   - //IRGenerator irgen;
34   - llvm::Module *mod = irgen.GetOrCreateModule("Name_the_Module.bc");
  27 + llvm::Module *module = irGen->GetOrCreateModule("swag");
  28 + pushScope();
  29 + for (int i = 0; i < decls->NumElements(); i++){
  30 + decls->Nth(i)->Emit();
  31 + }
  32 + popScope();
35 33  
36   - // create a function signature
37   - std::vector<llvm::Type *> argTypes;
38   - llvm::Type *intTy = irgen.GetIntType();
39   - argTypes.push_back(intTy);
40   - llvm::ArrayRef<llvm::Type *> argArray(argTypes);
41   - llvm::FunctionType *funcTy = llvm::FunctionType::get(intTy, argArray, false);
42   -
43   - // llvm::Function *f = llvm::cast<llvm::Function>(mod->getOrInsertFunction("foo", intTy, intTy, (Type *)0));
44   - llvm::Function *f = llvm::cast<llvm::Function>(mod->getOrInsertFunction("Name_the_function", funcTy));
45   - llvm::Argument *arg = f->arg_begin();
46   - arg->setName("x");
47   -
48   - // insert a block into the runction
49   - llvm::LLVMContext *context = irgen.GetContext();
50   - llvm::BasicBlock *bb = llvm::BasicBlock::Create(*context, "entry", f);
51   -
52   - // create a return instruction
53   - llvm::Value *val = llvm::ConstantInt::get(intTy, 1);
54   - llvm::Value *sum = llvm::BinaryOperator::CreateAdd(arg, val, "", bb);
55   - llvm::ReturnInst::Create(*context, sum, bb);
56   -
57   - // write the BC into standard output
58   - llvm::WriteBitcodeToFile(mod, llvm::outs());
  34 + module->dump();
  35 + llvm::WriteBitcodeToFile(module, llvm::outs());
  36 + return NULL;
59 37 }
60 38  
61 39 StmtBlock::StmtBlock(List<VarDecl*> *d, List<Stmt*> *s) {
... ... @@ -157,5 +135,79 @@
157 135 if (expr) expr->Print(indentLevel+1);
158 136 if (cases) cases->PrintAll(indentLevel+1);
159 137 if (def) def->Print(indentLevel+1);
  138 +}
  139 +
  140 +//rest of the emits
  141 +llvm::Value * StmtBlock::Emit(){
  142 + pushScope();
  143 + for (int i = 0; i < decls->NumElements(); i++){
  144 + decls->Nth(i)->Emit();
  145 + }
  146 + for (int i = 0; i < stmts->NumElements(); i++){
  147 + stmts->Nth(i)->Emit();
  148 + }
  149 +
  150 + //TODO
  151 +
  152 + popScope();
  153 + return NULL;
  154 +}
  155 +
  156 +llvm::Value * DeclStmt::Emit(){
  157 + llvm::Value * val;
  158 + if (VarDecl * vd = dynamic_cast<VarDecl*>(this->decl)){
  159 + val = vd->Emit();
  160 + }
  161 + else if (FnDecl * fd = dynamic_cast<FnDecl*>(this->decl)){
  162 + val = fd->Emit();
  163 + }
  164 + else{
  165 + val = NULL;
  166 + }
  167 + return val;
  168 +}
  169 +
  170 +llvm::Value * ConditionalStmt::Emit(){
  171 + return NULL;
  172 +}
  173 +//for statement
  174 +//while statement
  175 +//if statement
  176 +
  177 +llvm::Value * BreakStmt::Emit(){
  178 + return NULL;
  179 +}
  180 +
  181 +llvm::Value * ContinueStmt::Emit(){
  182 + return NULL;
  183 +}
  184 +
  185 +//Not sure
  186 +llvm::Value * ReturnStmt::Emit(){
  187 + llvm::Value * val;
  188 + llvm::LLVMContext * context = irGen->GetContext();
  189 + if (expr){
  190 + llvm::Value * retVal = expr->Emit();
  191 + retVal = llvm::ReturnInst::Create(*context, retVal, irGen->GetBasicBlock());
  192 + return retVal;
  193 + }
  194 + val = llvm::ReturnInst::Create(*context, irGen->GetBasicBlock());
  195 + return val;
  196 +}
  197 +
  198 +llvm::Value * SwitchStmt::Emit(){
  199 + return NULL;
  200 +}
  201 +
  202 +llvm::Value * SwitchLabel::Emit(){
  203 + return NULL;
  204 +}
  205 +
  206 +llvm::Value * Case::Emit(){
  207 + return NULL;
  208 +}
  209 +
  210 +llvm::Value * Default::Emit(){
  211 + return NULL;
160 212 }
... ... @@ -52,6 +52,8 @@
52 52 StmtBlock(List<VarDecl*> *variableDeclarations, List<Stmt*> *statements);
53 53 const char *GetPrintNameForNode() { return "StmtBlock"; }
54 54 void PrintChildren(int indentLevel);
  55 +
  56 + llvm::Value *Emit();
55 57 };
56 58  
57 59 class DeclStmt: public Stmt
... ... @@ -64,6 +66,7 @@
64 66 const char *GetPrintNameForNode() { return "DeclStmt"; }
65 67 void PrintChildren(int indentLevel);
66 68  
  69 + llvm::Value *Emit();
67 70 };
68 71  
69 72 class ConditionalStmt : public Stmt
... ... @@ -76,6 +79,7 @@
76 79 ConditionalStmt() : Stmt(), test(NULL), body(NULL) {}
77 80 ConditionalStmt(Expr *testExpr, Stmt *body);
78 81  
  82 + llvm::Value *Emit();
79 83 };
80 84  
81 85 class LoopStmt : public ConditionalStmt
... ... @@ -132,6 +136,7 @@
132 136 BreakStmt(yyltype loc) : Stmt(loc) {}
133 137 const char *GetPrintNameForNode() { return "BreakStmt"; }
134 138  
  139 + llvm::Value *Emit();
135 140 };
136 141  
137 142 class ContinueStmt : public Stmt
... ... @@ -140,6 +145,7 @@
140 145 ContinueStmt(yyltype loc) : Stmt(loc) {}
141 146 const char *GetPrintNameForNode() { return "ContinueStmt"; }
142 147  
  148 + llvm::Value *Emit();
143 149 };
144 150  
145 151 class ReturnStmt : public Stmt
... ... @@ -152,6 +158,7 @@
152 158 const char *GetPrintNameForNode() { return "ReturnStmt"; }
153 159 void PrintChildren(int indentLevel);
154 160  
  161 + llvm::Value *Emit();
155 162 };
156 163  
157 164 class SwitchLabel : public Stmt
... ... @@ -166,6 +173,7 @@
166 173 SwitchLabel(Stmt *stmt);
167 174 void PrintChildren(int indentLevel);
168 175  
  176 + llvm::Value *Emit();
169 177 };
170 178  
171 179 class Case : public SwitchLabel
... ... @@ -174,6 +182,8 @@
174 182 Case() : SwitchLabel() {}
175 183 Case(Expr *label, Stmt *stmt) : SwitchLabel(label, stmt) {}
176 184 const char *GetPrintNameForNode() { return "Case"; }
  185 +
  186 + llvm::Value *Emit();
177 187 };
178 188  
179 189 class Default : public SwitchLabel
... ... @@ -181,6 +191,8 @@
181 191 public:
182 192 Default(Stmt *stmt) : SwitchLabel(stmt) {}
183 193 const char *GetPrintNameForNode() { return "Default"; }
  194 +
  195 + llvm::Value *Emit();
184 196 };
185 197  
186 198 class SwitchStmt : public Stmt
... ... @@ -196,6 +208,7 @@
196 208 virtual const char *GetPrintNameForNode() { return "SwitchStmt"; }
197 209 void PrintChildren(int indentLevel);
198 210  
  211 + llvm::Value *Emit();
199 212 };
200 213  
201 214 class SwitchStmtError : public SwitchStmt