Commit 1ff8269e7921a072fceaf40d05240f3ccba094b4
Exists in
master
Merge branch 'master' of git.ucsd.edu:acs008/cse131-pa4
Showing 4 changed files Side-by-side Diff
ast_decl.cc
View file @
1ff8269
... | ... | @@ -66,4 +66,32 @@ |
66 | 66 | if (formals) formals->PrintAll(indentLevel+1, "(formals) "); |
67 | 67 | if (body) body->Print(indentLevel+1, "(body) "); |
68 | 68 | } |
69 | + | |
70 | +llvm::Value* VarDecl::Emit() { | |
71 | + llvm::Value* val; | |
72 | + if(type == Type::intType || type == Type::uintType) | |
73 | + val = new llvm::AllocaInst(llvm::Type::getInt32Ty(*irgen.GetContext()), id->GetName(), irgen.GetBasicBlock()); | |
74 | + if(type == Type::boolType) | |
75 | + val = new llvm::AllocaInst(llvm::Type::getInt1Ty(*irgen.GetContext()), id->GetName(), irgen.GetBasicBlock()); | |
76 | + else if(type == Type::voidType) | |
77 | + val = new llvm::AllocaInst(llvm::Type::getInt32Ty(*irgen.GetContext()), id->GetName(), irgen.GetBasicBlock()); | |
78 | + else if(type == Type::floatType) | |
79 | + val = new llvm::AllocaInst(llvm::Type::getVoidTy(*irgen.GetContext()), id->GetName(), irgen.GetBasicBlock()); | |
80 | + if(assignTo) val = new llvm::StoreInst(val, assignTo->Emit(), NULL, irgen.GetBasicBlock()); | |
81 | + return val; | |
82 | +} | |
83 | + | |
84 | +llvm::Value* FnDecl::Emit() { | |
85 | + llvm::LLVMContext* context = irgen.GetContext(); | |
86 | + llvm::BasicBlock::Create(*context, "funcBlock", irgen.GetBasicBlock()); | |
87 | + pushScope(); | |
88 | + vector<llvm::Type*> types; | |
89 | + for(int i = 0; i < formals->NumElements(); i++) { | |
90 | + formals->Nth(i)->Emit(); | |
91 | + types.push_back(convertType(formals->Nth(i)->GetType())); | |
92 | + } | |
93 | + llvm::FunctionType* ft = llvm::FunctionType::get(convertType(returnType), types, false); | |
94 | + popScope(); | |
95 | + return llvm::Function::Create(ft, llvm::Function::ExternalLinkage, id->GetName(), irgen.GetOrCreateModule("program")); | |
96 | +} |
ast_decl.h
View file @
1ff8269
... | ... | @@ -53,6 +53,7 @@ |
53 | 53 | const char *GetPrintNameForNode() { return "VarDecl"; } |
54 | 54 | void PrintChildren(int indentLevel); |
55 | 55 | Type *GetType() const { return type; } |
56 | + llvm::Value* Emit(); | |
56 | 57 | }; |
57 | 58 | |
58 | 59 | class VarDeclError : public VarDecl |
... | ... | @@ -80,6 +81,7 @@ |
80 | 81 | |
81 | 82 | Type *GetType() const { return returnType; } |
82 | 83 | List<VarDecl*> *GetFormals() {return formals;} |
84 | + llvm::Value* Emit(); | |
83 | 85 | }; |
84 | 86 | |
85 | 87 | class FormalsError : public FnDecl |
ast_type.cc
View file @
1ff8269
... | ... | @@ -97,4 +97,20 @@ |
97 | 97 | void ArrayType::PrintChildren(int indentLevel) { |
98 | 98 | elemType->Print(indentLevel+1); |
99 | 99 | } |
100 | + | |
101 | +llvm::Type* convertType(Type* t) { | |
102 | + if(t == Type::intType || t == Type::uintType) return llvm::Type::getInt32Ty(*irgen.GetContext()); | |
103 | + if(t == Type::floatType) return llvm::Type::getFloatTy(*irgen.GetContext()); | |
104 | + if(t == Type::boolType) return llvm::Type::getInt1Ty(*irgen.GetContext()); | |
105 | + if(t == Type::voidType) return llvm::Type::getVoidTy(*irgen.GetContext()); | |
106 | + if(t == Type::vec2Type) return llvm::VectorType::get(llvm::Type::getVoidTy(*irgen.GetContext()), 2); | |
107 | + if(t == Type::vec3Type) return llvm::VectorType::get(llvm::Type::getVoidTy(*irgen.GetContext()), 3); | |
108 | + if(t == Type::vec4Type) return llvm::VectorType::get(llvm::Type::getVoidTy(*irgen.GetContext()), 4); | |
109 | + if(t == Type::ivec2Type || t == Type::uvec2Type) return llvm::VectorType::get(llvm::Type::getVoidTy(*irgen.GetContext()), 2); | |
110 | + if(t == Type::ivec3Type || t == Type::uvec3Type) return llvm::VectorType::get(llvm::Type::getVoidTy(*irgen.GetContext()), 3); | |
111 | + if(t == Type::ivec4Type || t == Type::uvec4Type) return llvm::VectorType::get(llvm::Type::getVoidTy(*irgen.GetContext()), 4); | |
112 | + if(t == Type::bvec2Type) return llvm::VectorType::get(llvm::Type::getVoidTy(*irgen.GetContext()), 2); | |
113 | + if(t == Type::bvec3Type) return llvm::VectorType::get(llvm::Type::getVoidTy(*irgen.GetContext()), 3); | |
114 | + if(t == Type::bvec4Type) return llvm::VectorType::get(llvm::Type::getVoidTy(*irgen.GetContext()), 4); | |
115 | +} |