Commit 3b93536821ccf3428850ce3c2bd93d3e11556ee5

Authored by Jeffrey Johnson
1 parent 07ca8cc178
Exists in master

Functions maybe working?

Showing 1 changed file with 9 additions and 11 deletions 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::Value* val; 71 71 llvm::Value* val = new llvm::AllocaInst(convertType(type), id->GetName(), irgen.GetBasicBlock());
if(type == Type::intType || type == Type::uintType) 72
val = new llvm::AllocaInst(llvm::Type::getInt32Ty(*irgen.GetContext()), id->GetName(), irgen.GetBasicBlock()); 73
if(type == Type::boolType) 74
val = new llvm::AllocaInst(llvm::Type::getInt1Ty(*irgen.GetContext()), id->GetName(), irgen.GetBasicBlock()); 75
else if(type == Type::voidType) 76
val = new llvm::AllocaInst(llvm::Type::getInt32Ty(*irgen.GetContext()), id->GetName(), irgen.GetBasicBlock()); 77
else if(type == Type::floatType) 78
val = new llvm::AllocaInst(llvm::Type::getVoidTy(*irgen.GetContext()), id->GetName(), irgen.GetBasicBlock()); 79
if(assignTo) val = new llvm::StoreInst(val, assignTo->Emit(), NULL, irgen.GetBasicBlock()); 80 72 if(assignTo) val = new llvm::StoreInst(val, assignTo->Emit(), NULL, irgen.GetBasicBlock());
return val; 81 73 return val;
} 82 74 }
83 75
llvm::Value* FnDecl::Emit() { 84 76 llvm::Value* FnDecl::Emit() {
77 llvm::BasicBlock* oldBlock = irgen.GetBasicBlock();
llvm::LLVMContext* context = irgen.GetContext(); 85 78 llvm::LLVMContext* context = irgen.GetContext();
llvm::BasicBlock::Create(*context, "funcBlock", irgen.GetBasicBlock()); 86
pushScope(); 87 79 pushScope();
vector<llvm::Type*> types; 88 80 vector<llvm::Type*> types;
for(int i = 0; i < formals->NumElements(); i++) { 89 81 for(int i = 0; i < formals->NumElements(); i++) {
formals->Nth(i)->Emit(); 90 82 formals->Nth(i)->Emit();
types.push_back(convertType(formals->Nth(i)->GetType())); 91 83 types.push_back(convertType(formals->Nth(i)->GetType()));
} 92 84 }
llvm::FunctionType* ft = llvm::FunctionType::get(convertType(returnType), types, false); 93 85 llvm::FunctionType* ft = llvm::FunctionType::get(convertType(returnType), types, false);