From ba670d12e19c2dd6b9283347e6149435a26b8e0b Mon Sep 17 00:00:00 2001 From: Jeffrey C Johnson Date: Thu, 19 May 2016 10:01:48 -0700 Subject: [PATCH] Partially working decls --- ast_decl.cc | 28 ++++++++++++++++++++++++++++ ast_decl.h | 2 ++ ast_type.cc | 16 ++++++++++++++++ ast_type.h | 2 ++ 4 files changed, 48 insertions(+) diff --git a/ast_decl.cc b/ast_decl.cc index c148669..9732a7b 100644 --- a/ast_decl.cc +++ b/ast_decl.cc @@ -67,3 +67,31 @@ void FnDecl::PrintChildren(int indentLevel) { if (body) body->Print(indentLevel+1, "(body) "); } +llvm::Value* VarDecl::Emit() { + llvm::Value* val; + if(type == Type::intType || type == Type::uintType) + val = new llvm::AllocaInst(llvm::Type::getInt32Ty(*irgen.GetContext()), id->GetName(), irgen.GetBasicBlock()); + if(type == Type::boolType) + val = new llvm::AllocaInst(llvm::Type::getInt1Ty(*irgen.GetContext()), id->GetName(), irgen.GetBasicBlock()); + else if(type == Type::voidType) + val = new llvm::AllocaInst(llvm::Type::getInt32Ty(*irgen.GetContext()), id->GetName(), irgen.GetBasicBlock()); + else if(type == Type::floatType) + val = new llvm::AllocaInst(llvm::Type::getVoidTy(*irgen.GetContext()), id->GetName(), irgen.GetBasicBlock()); + if(assignTo) val = new llvm::StoreInst(val, assignTo->Emit(), NULL, irgen.GetBasicBlock()); + return val; +} + +llvm::Value* FnDecl::Emit() { + llvm::LLVMContext* context = irgen.GetContext(); + llvm::BasicBlock::Create(*context, "funcBlock", irgen.GetBasicBlock()); + pushScope(); + vector types; + for(int i = 0; i < formals->NumElements(); i++) { + formals->Nth(i)->Emit(); + types.push_back(convertType(formals->Nth(i)->GetType())); + } + llvm::FunctionType* ft = llvm::FunctionType::get(convertType(returnType), types, false); + popScope(); + return llvm::Function::Create(ft, llvm::Function::ExternalLinkage, id->GetName(), irgen.GetOrCreateModule("program")); +} + diff --git a/ast_decl.h b/ast_decl.h index c0df332..d3070e1 100644 --- a/ast_decl.h +++ b/ast_decl.h @@ -53,6 +53,7 @@ class VarDecl : public Decl const char *GetPrintNameForNode() { return "VarDecl"; } void PrintChildren(int indentLevel); Type *GetType() const { return type; } + llvm::Value* Emit(); }; class VarDeclError : public VarDecl @@ -80,6 +81,7 @@ class FnDecl : public Decl Type *GetType() const { return returnType; } List *GetFormals() {return formals;} + llvm::Value* Emit(); }; class FormalsError : public FnDecl diff --git a/ast_type.cc b/ast_type.cc index e53ce6d..fa38542 100644 --- a/ast_type.cc +++ b/ast_type.cc @@ -98,4 +98,20 @@ void ArrayType::PrintChildren(int indentLevel) { elemType->Print(indentLevel+1); } +llvm::Type* convertType(Type* t) { + if(t == Type::intType || t == Type::uintType) return llvm::Type::getInt32Ty(*irgen.GetContext()); + if(t == Type::floatType) return llvm::Type::getFloatTy(*irgen.GetContext()); + if(t == Type::boolType) return llvm::Type::getInt1Ty(*irgen.GetContext()); + if(t == Type::voidType) return llvm::Type::getVoidTy(*irgen.GetContext()); + if(t == Type::vec2Type) return llvm::VectorType::get(llvm::Type::getVoidTy(*irgen.GetContext()), 2); + if(t == Type::vec3Type) return llvm::VectorType::get(llvm::Type::getVoidTy(*irgen.GetContext()), 3); + if(t == Type::vec4Type) return llvm::VectorType::get(llvm::Type::getVoidTy(*irgen.GetContext()), 4); + if(t == Type::ivec2Type || t == Type::uvec2Type) return llvm::VectorType::get(llvm::Type::getVoidTy(*irgen.GetContext()), 2); + if(t == Type::ivec3Type || t == Type::uvec3Type) return llvm::VectorType::get(llvm::Type::getVoidTy(*irgen.GetContext()), 3); + if(t == Type::ivec4Type || t == Type::uvec4Type) return llvm::VectorType::get(llvm::Type::getVoidTy(*irgen.GetContext()), 4); + if(t == Type::bvec2Type) return llvm::VectorType::get(llvm::Type::getVoidTy(*irgen.GetContext()), 2); + if(t == Type::bvec3Type) return llvm::VectorType::get(llvm::Type::getVoidTy(*irgen.GetContext()), 3); + if(t == Type::bvec4Type) return llvm::VectorType::get(llvm::Type::getVoidTy(*irgen.GetContext()), 4); +} + diff --git a/ast_type.h b/ast_type.h index 3124bde..39d6728 100644 --- a/ast_type.h +++ b/ast_type.h @@ -92,5 +92,7 @@ class ArrayType : public Type Type *GetElemType() {return elemType;} }; +llvm::Type* convertType(Type* t); + #endif -- 1.9.1