Commit e365a9a8cd26342113dcac2d0c5bdd961d3e4720

Authored by Austin Sun
Exists in master

Merge branch 'master' of git.ucsd.edu:acs008/cse131-pa4

Showing 1 changed file Inline Diff

/* File: ast_decl.cc 1 1 /* File: ast_decl.cc
* ----------------- 2 2 * -----------------
* Implementation of Decl node classes. 3 3 * Implementation of Decl node classes.
*/ 4 4 */
#include "ast_decl.h" 5 5 #include "ast_decl.h"
#include "ast_type.h" 6 6 #include "ast_type.h"
#include "ast_stmt.h" 7 7 #include "ast_stmt.h"
#include "symtable.h" 8 8 #include "symtable.h"
9 9
Decl::Decl(Identifier *n) : Node(*n->GetLocation()) { 10 10 Decl::Decl(Identifier *n) : Node(*n->GetLocation()) {
Assert(n != NULL); 11 11 Assert(n != NULL);
(id=n)->SetParent(this); 12 12 (id=n)->SetParent(this);
} 13 13 }
14 14
VarDecl::VarDecl(Identifier *n, Type *t, Expr *e) : Decl(n) { 15 15 VarDecl::VarDecl(Identifier *n, Type *t, Expr *e) : Decl(n) {
Assert(n != NULL && t != NULL); 16 16 Assert(n != NULL && t != NULL);
(type=t)->SetParent(this); 17 17 (type=t)->SetParent(this);
if (e) (assignTo=e)->SetParent(this); 18 18 if (e) (assignTo=e)->SetParent(this);
typeq = NULL; 19 19 typeq = NULL;
} 20 20 }
21 21
VarDecl::VarDecl(Identifier *n, TypeQualifier *tq, Expr *e) : Decl(n) { 22 22 VarDecl::VarDecl(Identifier *n, TypeQualifier *tq, Expr *e) : Decl(n) {
Assert(n != NULL && tq != NULL); 23 23 Assert(n != NULL && tq != NULL);
(typeq=tq)->SetParent(this); 24 24 (typeq=tq)->SetParent(this);
if (e) (assignTo=e)->SetParent(this); 25 25 if (e) (assignTo=e)->SetParent(this);
type = NULL; 26 26 type = NULL;
} 27 27 }
28 28
VarDecl::VarDecl(Identifier *n, Type *t, TypeQualifier *tq, Expr *e) : Decl(n) { 29 29 VarDecl::VarDecl(Identifier *n, Type *t, TypeQualifier *tq, Expr *e) : Decl(n) {
Assert(n != NULL && t != NULL && tq != NULL); 30 30 Assert(n != NULL && t != NULL && tq != NULL);
(type=t)->SetParent(this); 31 31 (type=t)->SetParent(this);
(typeq=tq)->SetParent(this); 32 32 (typeq=tq)->SetParent(this);
if (e) (assignTo=e)->SetParent(this); 33 33 if (e) (assignTo=e)->SetParent(this);
} 34 34 }
35 35
void VarDecl::PrintChildren(int indentLevel) { 36 36 void VarDecl::PrintChildren(int indentLevel) {
if (typeq) typeq->Print(indentLevel+1); 37 37 if (typeq) typeq->Print(indentLevel+1);
if (type) type->Print(indentLevel+1); 38 38 if (type) type->Print(indentLevel+1);
if (id) id->Print(indentLevel+1); 39 39 if (id) id->Print(indentLevel+1);
if (assignTo) assignTo->Print(indentLevel+1, "(initializer) "); 40 40 if (assignTo) assignTo->Print(indentLevel+1, "(initializer) ");
} 41 41 }
42 42
FnDecl::FnDecl(Identifier *n, Type *r, List<VarDecl*> *d) : Decl(n) { 43 43 FnDecl::FnDecl(Identifier *n, Type *r, List<VarDecl*> *d) : Decl(n) {
Assert(n != NULL && r!= NULL && d != NULL); 44 44 Assert(n != NULL && r!= NULL && d != NULL);
(returnType=r)->SetParent(this); 45 45 (returnType=r)->SetParent(this);
(formals=d)->SetParentAll(this); 46 46 (formals=d)->SetParentAll(this);
body = NULL; 47 47 body = NULL;
returnTypeq = NULL; 48 48 returnTypeq = NULL;
} 49 49 }
50 50
FnDecl::FnDecl(Identifier *n, Type *r, TypeQualifier *rq, List<VarDecl*> *d) : Decl(n) { 51 51 FnDecl::FnDecl(Identifier *n, Type *r, TypeQualifier *rq, List<VarDecl*> *d) : Decl(n) {
Assert(n != NULL && r != NULL && rq != NULL&& d != NULL); 52 52 Assert(n != NULL && r != NULL && rq != NULL&& d != NULL);
(returnType=r)->SetParent(this); 53 53 (returnType=r)->SetParent(this);
(returnTypeq=rq)->SetParent(this); 54 54 (returnTypeq=rq)->SetParent(this);
(formals=d)->SetParentAll(this); 55 55 (formals=d)->SetParentAll(this);
body = NULL; 56 56 body = NULL;
} 57 57 }
58 58
void FnDecl::SetFunctionBody(Stmt *b) { 59 59 void FnDecl::SetFunctionBody(Stmt *b) {
(body=b)->SetParent(this); 60 60 (body=b)->SetParent(this);
} 61 61 }
62 62
void FnDecl::PrintChildren(int indentLevel) { 63 63 void FnDecl::PrintChildren(int indentLevel) {
if (returnType) returnType->Print(indentLevel+1, "(return type) "); 64 64 if (returnType) returnType->Print(indentLevel+1, "(return type) ");
if (id) id->Print(indentLevel+1); 65 65 if (id) id->Print(indentLevel+1);
if (formals) formals->PrintAll(indentLevel+1, "(formals) "); 66 66 if (formals) formals->PrintAll(indentLevel+1, "(formals) ");
if (body) body->Print(indentLevel+1, "(body) "); 67 67 if (body) body->Print(indentLevel+1, "(body) ");
} 68 68 }
/* 69 69 /*
llvm::Value* VarDecl::Emit() { 70 70 llvm::Value* VarDecl::Emit() {
llvm::BasicBlock* b = irgen.GetBasicBlock(); 71 71 llvm::BasicBlock* b = irgen.GetBasicBlock();
llvm::Value* val; 72 72 llvm::Value* val;
if(b) val = new llvm::AllocaInst(convertType(type), id->GetName(), b); 73 73 if(b) val = new llvm::AllocaInst(convertType(type), id->GetName(), b);
else { 74 74 else {
llvm::Constant* e = assignTo ? (llvm::Constant*)assignTo->Emit() : llvm::Constant::getNullValue(convertType(type)); 75 75 llvm::Constant* e = assignTo ? (llvm::Constant*)assignTo->Emit() : llvm::Constant::getNullValue(convertType(type));
val = new llvm::GlobalVariable(*irgen.GetOrCreateModule("program"), convertType(type), false, llvm::GlobalValue::ExternalLinkage, e, ""); 76 76 val = new llvm::GlobalVariable(*irgen.GetOrCreateModule("program"), convertType(type), false, llvm::GlobalValue::ExternalLinkage, e, "");
} 77 77 }
if(assignTo) val = new llvm::StoreInst(val, assignTo->Emit(), "", b); 78 78 if(assignTo) val = new llvm::StoreInst(val, assignTo->Emit(), "", b);
addToScope(id->GetName(), this, val); 79 79 addToScope(id->GetName(), this, val);
return val; 80 80 return val;
} 81 81 }
*/ 82 82 */
llvm::Value * VarDecl::Emit(){ 83 83 llvm::Value * VarDecl::Emit(){
Type * t = type; 84 84 Type * t = type;
llvm::Type * ltyp=convertType(t); 85 85 llvm::Type * ltyp=convertType(t);
llvm::Value * val; 86 86 llvm::Value * val;
llvm::Module *mod = irgen.GetOrCreateModule("program"); 87 87 llvm::Module *mod = irgen.GetOrCreateModule("program");
if (global()){ 88 88 if (global()){
val = new llvm::GlobalVariable(*mod, ltyp, false, llvm::GlobalValue::ExternalLinkage, llvm::Constant::getNullValue(ltyp), llvm::Twine(id->GetName())); 89 89 val = new llvm::GlobalVariable(*mod, ltyp, false, llvm::GlobalValue::ExternalLinkage, llvm::Constant::getNullValue(ltyp), llvm::Twine(id->GetName()));
addToScope(id->GetName(),this, val); 90 90 addToScope(id->GetName(),this, val);
} 91 91 }
else{ 92 92 else{
val = new llvm::AllocaInst(ltyp, llvm::Twine(id->GetName()), irgen.GetBasicBlock()); 93 93 val = new llvm::AllocaInst(ltyp, llvm::Twine(id->GetName()), irgen.GetBasicBlock());
addToScope(id->GetName(),this, val); 94 94 addToScope(id->GetName(),this, val);
} 95 95 }
return val; 96 96 return val;
} 97 97 }
98 98
99 99
/* 100 100 /*
llvm::Value* FnDecl::Emit() { 101 101 llvm::Value* FnDecl::Emit() {
llvm::BasicBlock* oldBlock = irgen.GetBasicBlock(); 102 102 llvm::BasicBlock* oldBlock = irgen.GetBasicBlock();
llvm::LLVMContext* context = irgen.GetContext(); 103 103 llvm::LLVMContext* context = irgen.GetContext();
pushScope(); 104 104 pushScope();
vector<llvm::Type*> types; 105 105 vector<llvm::Type*> types;
for(int i = 0; i < formals->NumElements(); i++) { 106 106 for(int i = 0; i < formals->NumElements(); i++) {
types.push_back(convertType(formals->Nth(i)->GetType())); 107 107 types.push_back(convertType(formals->Nth(i)->GetType()));
} 108 108 }
llvm::FunctionType* ft = llvm::FunctionType::get(convertType(returnType), types, false); 109 109 llvm::FunctionType* ft = llvm::FunctionType::get(convertType(returnType), types, false);
popScope(); 110 110 popScope();
llvm::Function* func = llvm::Function::Create(ft, llvm::Function::ExternalLinkage, id->GetName(), irgen.GetOrCreateModule("program")); 111 111 llvm::Function* func = llvm::Function::Create(ft, llvm::Function::ExternalLinkage, id->GetName(), irgen.GetOrCreateModule("program"));
llvm::BasicBlock * block = llvm::BasicBlock::Create(*context, "funcBlock", func); 112 112 llvm::BasicBlock * block = llvm::BasicBlock::Create(*context, "funcBlock", func);
irgen.SetBasicBlock(block); 113 113 irgen.SetBasicBlock(block);
114 irgen.SetFunction(func);
for(int i = 0; i < formals->NumElements(); i++) { 114 115 for(int i = 0; i < formals->NumElements(); i++) {
formals->Nth(i)->Emit(); 115 116 formals->Nth(i)->Emit();
} 116 117 }
body->Emit(); 117 118 body->Emit();
if(!block->getTerminator()) { 118 119 if(!block->getTerminator()) {
llvm::Value* retVar = new llvm::AllocaInst(convertType(returnType), "", block); 119 120 llvm::Value* retVar = new llvm::AllocaInst(convertType(returnType), "", block);
llvm::Value* ret = new llvm::LoadInst(retVar, "", block); 120 121 llvm::Value* ret = new llvm::LoadInst(retVar, "", block);
llvm::ReturnInst::Create(*context, ret, block); 121 122 llvm::ReturnInst::Create(*context, ret, block);
} 122 123 }
irgen.SetBasicBlock(oldBlock); 123 124 irgen.SetBasicBlock(oldBlock);
return func; 124 125 return func;
} 125 126 }
*/ 126 127 */
llvm::Value * FnDecl::Emit(){ 127 128 llvm::Value * FnDecl::Emit(){
llvm::Module *mod = irgen.GetOrCreateModule("program"); 128 129 llvm::Module *mod = irgen.GetOrCreateModule("program");
llvm::Type *rt = convertType(this->returnType); 129 130 llvm::Type *rt = convertType(this->returnType);
130 131
std::vector<llvm::Type *> ref; 131 132 std::vector<llvm::Type *> ref;
for (int i = 0; i < formals->NumElements(); i++){ 132 133 for (int i = 0; i < formals->NumElements(); i++){
ref.push_back(convertType(formals->Nth(i)->GetType())); 133 134 ref.push_back(convertType(formals->Nth(i)->GetType()));
} 134 135 }
llvm::ArrayRef<llvm::Type *> ar(ref); 135 136 llvm::ArrayRef<llvm::Type *> ar(ref);
llvm::FunctionType * ft = llvm::FunctionType::get(rt, ar, false); 136 137 llvm::FunctionType * ft = llvm::FunctionType::get(rt, ar, false);
137 138
llvm::Value * funcval = mod->getOrInsertFunction(llvm::StringRef(this->GetIdentifier()->GetName()), ft); 138 139 llvm::Value * funcval = mod->getOrInsertFunction(llvm::StringRef(this->GetIdentifier()->GetName()), ft);
llvm::Function * fp = llvm::cast<llvm::Function>(funcval); 139 140 llvm::Function * fp = llvm::cast<llvm::Function>(funcval);
140 141
addToScope(id->GetName(),this, funcval); 141 142 addToScope(id->GetName(),this, funcval);
//TODO is this how you set it? 142 143 //TODO is this how you set it?
irgen.SetFunction(fp); 143 144 irgen.SetFunction(fp);
144 145
//SetName 145 146 //SetName
pushScope(); 146 147 pushScope();
llvm::Function::arg_iterator args = fp->arg_begin(); 147 148 llvm::Function::arg_iterator args = fp->arg_begin();
148 149
llvm::LLVMContext *context = irgen.GetContext(); 149 150 llvm::LLVMContext *context = irgen.GetContext();
llvm::BasicBlock * varbb = llvm::BasicBlock::Create(*context, "entry", fp); 150 151 llvm::BasicBlock * varbb = llvm::BasicBlock::Create(*context, "entry", fp);