From 98c5b35993332c80acfbb1f877deecb93afb3091 Mon Sep 17 00:00:00 2001 From: Austin Sun Date: Tue, 17 May 2016 15:17:22 -0700 Subject: [PATCH] added irgen to node and the basic emit messages in stmt, only missing for,while,if --- CMakeLists.txt | 31 +++++++++++++++ ast.cc | 8 ++-- ast.h | 17 +++++++-- ast_stmt.cc | 118 +++++++++++++++++++++++++++++++++++++++++---------------- ast_stmt.h | 13 +++++++ 5 files changed, 146 insertions(+), 41 deletions(-) create mode 100644 CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..3fe0dce --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,31 @@ +cmake_minimum_required(VERSION 3.5) +project(cse_131pa4) + +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") + +set(SOURCE_FILES + ast.cc + ast.h + ast_decl.cc + ast_decl.h + ast_expr.cc + ast_expr.h + ast_stmt.cc + ast_stmt.h + ast_type.cc + ast_type.h + errors.cc + errors.h + irgen.cc + irgen.h + list.h + location.h + main.cc + parser.h + scanner.h + symtable.cc + symtable.h + utility.cc + utility.h) + +add_executable(cse_131pa4 ${SOURCE_FILES}) \ No newline at end of file diff --git a/ast.cc b/ast.cc index d93cd01..fe82e8a 100644 --- a/ast.cc +++ b/ast.cc @@ -51,8 +51,6 @@ void Identifier::PrintChildren(int indentLevel) { /*------------------------------------------------------------------------------------ * Symbol Table Information *----------------------------------------------------------------------------------*/ -SymbolTable symbols; - pair findSymbol(string s) { for(int i = symbols.size() - 1; i >= 0; i--) { if(symbols[i].count(s) > 0) return symbols[i][s]; @@ -76,7 +74,7 @@ bool isInCurrentScope(Decl* d) { } bool isInScope(string s) { - pair tmp = findSymbol(s); + pair tmp = findSymbol(s); return tmp.first != NULL; } @@ -92,6 +90,6 @@ void popScope() { symbols.pop_back(); } -void addToScope(string s, Decl* d, Type* t) { - symbols[symbols.size()-1][s] = make_pair(d, t); +void addToScope(string s, Decl* d, llvm::Value * v) { + symbols[symbols.size()-1][s] = make_pair(d, v); } diff --git a/ast.h b/ast.h index b92d2f7..6a1fe85 100644 --- a/ast.h +++ b/ast.h @@ -34,6 +34,8 @@ */ +#pragma clang diagnostic push +#pragma ide diagnostic ignored "CannotResolve" #ifndef _H_ast #define _H_ast @@ -44,6 +46,11 @@ #include #include #include +//LLVM STUFF +#include "llvm/IR/Module.h" +#include "llvm/IR/LLVMContext.h" +#include "llvm/IR/Instructions.h" +#include "llvm/IR/Constants.h" using namespace std; @@ -60,6 +67,8 @@ class Node { public: Node(yyltype loc); Node(); + static IRGenerator * irGen; + SymbolTable symbols; virtual ~Node() {} yyltype *GetLocation() { return location; } @@ -106,16 +115,18 @@ class Error : public Node * Symbol Table Information *----------------------------------------------------------------------------------*/ typedef vector > > SymbolTable; -pair findSymbol(string s); -pair findSymbol(Decl* d); +pair findSymbol(string s); +pair findSymbol(Decl* d); bool isInCurrentScope(string s); bool isInCurrentScope(Decl* d); bool isInScope(string s); bool isInScope(Decl* d); void pushScope(); void popScope(); -void addToScope(string s, Decl* d, llvm::Value * v t); +void addToScope(string s, Decl* d, llvm::Value * v); extern SymbolTable symbols; #endif + +#pragma clang diagnostic pop \ No newline at end of file diff --git a/ast_stmt.cc b/ast_stmt.cc index f4c2f45..5f68f8b 100644 --- a/ast_stmt.cc +++ b/ast_stmt.cc @@ -22,40 +22,18 @@ void Program::PrintChildren(int indentLevel) { decls->PrintAll(indentLevel+1); printf("\n"); } - +//pls work llvm::Value* Program::Emit() { - // TODO: - // This is just a reference for you to get started - // - // You can use this as a template and create Emit() function - // for individual node to fill in the module structure and instructions. - // - //IRGenerator irgen; - llvm::Module *mod = irgen.GetOrCreateModule("Name_the_Module.bc"); - - // create a function signature - std::vector argTypes; - llvm::Type *intTy = irgen.GetIntType(); - argTypes.push_back(intTy); - llvm::ArrayRef argArray(argTypes); - llvm::FunctionType *funcTy = llvm::FunctionType::get(intTy, argArray, false); - - // llvm::Function *f = llvm::cast(mod->getOrInsertFunction("foo", intTy, intTy, (Type *)0)); - llvm::Function *f = llvm::cast(mod->getOrInsertFunction("Name_the_function", funcTy)); - llvm::Argument *arg = f->arg_begin(); - arg->setName("x"); - - // insert a block into the runction - llvm::LLVMContext *context = irgen.GetContext(); - llvm::BasicBlock *bb = llvm::BasicBlock::Create(*context, "entry", f); - - // create a return instruction - llvm::Value *val = llvm::ConstantInt::get(intTy, 1); - llvm::Value *sum = llvm::BinaryOperator::CreateAdd(arg, val, "", bb); - llvm::ReturnInst::Create(*context, sum, bb); - - // write the BC into standard output - llvm::WriteBitcodeToFile(mod, llvm::outs()); + llvm::Module *module = irGen->GetOrCreateModule("swag"); + pushScope(); + for (int i = 0; i < decls->NumElements(); i++){ + decls->Nth(i)->Emit(); + } + popScope(); + + module->dump(); + llvm::WriteBitcodeToFile(module, llvm::outs()); + return NULL; } StmtBlock::StmtBlock(List *d, List *s) { @@ -159,3 +137,77 @@ void SwitchStmt::PrintChildren(int indentLevel) { if (def) def->Print(indentLevel+1); } +//rest of the emits +llvm::Value * StmtBlock::Emit(){ + pushScope(); + for (int i = 0; i < decls->NumElements(); i++){ + decls->Nth(i)->Emit(); + } + for (int i = 0; i < stmts->NumElements(); i++){ + stmts->Nth(i)->Emit(); + } + + //TODO + + popScope(); + return NULL; +} + +llvm::Value * DeclStmt::Emit(){ + llvm::Value * val; + if (VarDecl * vd = dynamic_cast(this->decl)){ + val = vd->Emit(); + } + else if (FnDecl * fd = dynamic_cast(this->decl)){ + val = fd->Emit(); + } + else{ + val = NULL; + } + return val; +} + +llvm::Value * ConditionalStmt::Emit(){ + return NULL; +} +//for statement +//while statement +//if statement + +llvm::Value * BreakStmt::Emit(){ + return NULL; +} + +llvm::Value * ContinueStmt::Emit(){ + return NULL; +} + +//Not sure +llvm::Value * ReturnStmt::Emit(){ + llvm::Value * val; + llvm::LLVMContext * context = irGen->GetContext(); + if (expr){ + llvm::Value * retVal = expr->Emit(); + retVal = llvm::ReturnInst::Create(*context, retVal, irGen->GetBasicBlock()); + return retVal; + } + val = llvm::ReturnInst::Create(*context, irGen->GetBasicBlock()); + return val; +} + +llvm::Value * SwitchStmt::Emit(){ + return NULL; +} + +llvm::Value * SwitchLabel::Emit(){ + return NULL; +} + +llvm::Value * Case::Emit(){ + return NULL; +} + +llvm::Value * Default::Emit(){ + return NULL; +} + diff --git a/ast_stmt.h b/ast_stmt.h index ff644f1..53eac9d 100644 --- a/ast_stmt.h +++ b/ast_stmt.h @@ -52,6 +52,8 @@ class StmtBlock : public Stmt StmtBlock(List *variableDeclarations, List *statements); const char *GetPrintNameForNode() { return "StmtBlock"; } void PrintChildren(int indentLevel); + + llvm::Value *Emit(); }; class DeclStmt: public Stmt @@ -64,6 +66,7 @@ class DeclStmt: public Stmt const char *GetPrintNameForNode() { return "DeclStmt"; } void PrintChildren(int indentLevel); + llvm::Value *Emit(); }; class ConditionalStmt : public Stmt @@ -76,6 +79,7 @@ class ConditionalStmt : public Stmt ConditionalStmt() : Stmt(), test(NULL), body(NULL) {} ConditionalStmt(Expr *testExpr, Stmt *body); + llvm::Value *Emit(); }; class LoopStmt : public ConditionalStmt @@ -132,6 +136,7 @@ class BreakStmt : public Stmt BreakStmt(yyltype loc) : Stmt(loc) {} const char *GetPrintNameForNode() { return "BreakStmt"; } + llvm::Value *Emit(); }; class ContinueStmt : public Stmt @@ -140,6 +145,7 @@ class ContinueStmt : public Stmt ContinueStmt(yyltype loc) : Stmt(loc) {} const char *GetPrintNameForNode() { return "ContinueStmt"; } + llvm::Value *Emit(); }; class ReturnStmt : public Stmt @@ -152,6 +158,7 @@ class ReturnStmt : public Stmt const char *GetPrintNameForNode() { return "ReturnStmt"; } void PrintChildren(int indentLevel); + llvm::Value *Emit(); }; class SwitchLabel : public Stmt @@ -166,6 +173,7 @@ class SwitchLabel : public Stmt SwitchLabel(Stmt *stmt); void PrintChildren(int indentLevel); + llvm::Value *Emit(); }; class Case : public SwitchLabel @@ -174,6 +182,8 @@ class Case : public SwitchLabel Case() : SwitchLabel() {} Case(Expr *label, Stmt *stmt) : SwitchLabel(label, stmt) {} const char *GetPrintNameForNode() { return "Case"; } + + llvm::Value *Emit(); }; class Default : public SwitchLabel @@ -181,6 +191,8 @@ class Default : public SwitchLabel public: Default(Stmt *stmt) : SwitchLabel(stmt) {} const char *GetPrintNameForNode() { return "Default"; } + + llvm::Value *Emit(); }; class SwitchStmt : public Stmt @@ -196,6 +208,7 @@ class SwitchStmt : public Stmt virtual const char *GetPrintNameForNode() { return "SwitchStmt"; } void PrintChildren(int indentLevel); + llvm::Value *Emit(); }; class SwitchStmtError : public SwitchStmt -- 1.9.1