Skip to content

Commit

Permalink
- function args scope
Browse files Browse the repository at this point in the history
- more typing for bison parser union yyval
- cleanup ast_types_resolver
- variable declaration and assignment in one expression
  • Loading branch information
Raekye committed Nov 12, 2014
1 parent aed4b7c commit 52f5fca
Show file tree
Hide file tree
Showing 11 changed files with 118 additions and 98 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ hmmm
- gdb (debug)
- valgrind (debug)

# TODO
- function types

# Flex and Bison stuff
- http://flex.sourceforge.net/manual/
- http://flex.sourceforge.net/manual/Reentrant.html
Expand Down
4 changes: 0 additions & 4 deletions sayaka/src/ast_node_block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,18 @@ void ASTNodeBlock::push(ASTNode* node) {
}

ASTNodeBlock* ASTNodeBlock::pass_types(CodeGenContext* code_gen_context, ASTType* type) {
code_gen_context->scope.push();
for (std::vector<ASTNode*>::iterator it = this->statements.begin(); it != this->statements.end(); it++) {
(*it) = (*it)->pass_types(code_gen_context, type);
}
code_gen_context->scope.pop();
this->type = this->statements.back()->type;
return this;
}

llvm::Value* ASTNodeBlock::gen_code(CodeGenContext* code_gen_context) {
std::cout << "Generating block" << std::endl;
code_gen_context->push_scope();
llvm::Value* last = NULL;
for (std::vector<ASTNode*>::iterator it = this->statements.begin(); it != this->statements.end(); it++) {
last = (*it)->gen_code(code_gen_context);
}
code_gen_context->pop_scope();
return last;
}
22 changes: 13 additions & 9 deletions sayaka/src/ast_node_function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ ASTNodeFunction::~ASTNodeFunction() {

ASTNodeFunction* ASTNodeFunction::pass_types(CodeGenContext* code_gen_context, ASTType* ignore) {
this->type = NULL; // TODO;
code_gen_context->push_scope();
this->prototype = this->prototype->pass_types(code_gen_context, NULL);
this->body = this->body->pass_types(code_gen_context, code_gen_context->ast_types_resolver.get(this->prototype->return_type));
if (this->body != NULL) {
this->body = this->body->pass_types(code_gen_context, code_gen_context->ast_types_resolver.get(this->prototype->return_type));
}
code_gen_context->pop_scope();
return this;
}

Expand All @@ -26,19 +30,19 @@ llvm::Value* ASTNodeFunction::gen_code(CodeGenContext* code_gen_context) {
if (type == NULL) {
throw std::runtime_error("Unknown type");
}
code_gen_context->push_scope();
llvm::Function* fn = (llvm::Function*) this->prototype->gen_code(code_gen_context);

llvm::BasicBlock* basic_block = llvm::BasicBlock::Create(llvm::getGlobalContext(), "entry", fn);
code_gen_context->push_block(basic_block);

llvm::Value* ret_val = this->body->gen_code(code_gen_context);
if (ret_val != NULL) {
if (this->body != NULL) {
llvm::BasicBlock* basic_block = llvm::BasicBlock::Create(llvm::getGlobalContext(), "entry", fn);
code_gen_context->push_block(basic_block);

llvm::Value* ret_val = this->body->gen_code(code_gen_context);
code_gen_context->builder.CreateRet(ret_val);
llvm::verifyFunction(*fn);
code_gen_context->pop_block();
fn->dump();
return fn;
}
code_gen_context->pop_block();
throw std::runtime_error("Error generating function");
code_gen_context->pop_scope();
return fn;
}
90 changes: 45 additions & 45 deletions sayaka/src/ast_types_resolver.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
#include "ast_types_resolver.h"

ASTTypesResolver::ASTTypesResolver() {
this->put(this->bit_ty());
this->put(this->byte_ty());
this->put(this->short_ty());
this->put(this->int_ty());
this->put(this->long_ty());
this->put(this->ubyte_ty());
this->put(this->ushort_ty());
this->put(this->uint_ty());
this->put(this->ulong_ty());
this->put(this->float_ty());
this->put(this->double_ty());
ASTTypesResolver::ASTTypesResolver(llvm::LLVMContext& llvm_context) : llvm_context(llvm_context) {
this->put(this->bit_ty(true));
this->put(this->byte_ty(true));
this->put(this->short_ty(true));
this->put(this->int_ty(true));
this->put(this->long_ty(true));
this->put(this->ubyte_ty(true));
this->put(this->ushort_ty(true));
this->put(this->uint_ty(true));
this->put(this->ulong_ty(true));
this->put(this->float_ty(true));
this->put(this->double_ty(true));
}

ASTTypesResolver::~ASTTypesResolver() {
Expand All @@ -28,112 +28,112 @@ void ASTTypesResolver::put(ASTType* type) {
this->types_map[type->name] = type;
}

ASTType* ASTTypesResolver::bit_ty() {
if (this->get("Bit") != NULL) {
ASTType* ASTTypesResolver::bit_ty(bool create) {
if (!create) {
return this->get("Bit");
}
ASTType* instance = new ASTType("Bit");
instance->llvm_type = llvm::Type::getInt1Ty(llvm::getGlobalContext());
instance->llvm_type = llvm::Type::getInt1Ty(this->llvm_context);
instance->primitive = true;
return instance;
}

ASTType* ASTTypesResolver::byte_ty() {
if (this->get("Byte") != NULL) {
ASTType* ASTTypesResolver::byte_ty(bool create) {
if (!create) {
return this->get("Byte");
}
ASTType* instance = new ASTType("Byte");
instance->llvm_type = llvm::Type::getInt8Ty(llvm::getGlobalContext());
instance->llvm_type = llvm::Type::getInt8Ty(this->llvm_context);
instance->primitive = true;
return instance;
}

ASTType* ASTTypesResolver::ubyte_ty() {
if (this->get("UByte") != NULL) {
ASTType* ASTTypesResolver::ubyte_ty(bool create) {
if (!create) {
return this->get("UByte");
}
ASTType* instance = new ASTType("UByte");
instance->llvm_type = llvm::Type::getInt8Ty(llvm::getGlobalContext());
instance->llvm_type = llvm::Type::getInt8Ty(this->llvm_context);
instance->primitive = true;
return instance;
}

ASTType* ASTTypesResolver::short_ty() {
if (this->get("Short") != NULL) {
ASTType* ASTTypesResolver::short_ty(bool create) {
if (!create) {
return this->get("Short");
}
ASTType* instance = new ASTType("Short");
instance->llvm_type = llvm::Type::getInt16Ty(llvm::getGlobalContext());
instance->llvm_type = llvm::Type::getInt16Ty(this->llvm_context);
instance->primitive = true;
return instance;
}

ASTType* ASTTypesResolver::ushort_ty() {
if (this->get("UShort") != NULL) {
ASTType* ASTTypesResolver::ushort_ty(bool create) {
if (!create) {
return this->get("UShort");
}
ASTType* instance = new ASTType("UShort");
instance->llvm_type = llvm::Type::getInt16Ty(llvm::getGlobalContext());
instance->llvm_type = llvm::Type::getInt16Ty(this->llvm_context);
instance->primitive = true;
return instance;
}

ASTType* ASTTypesResolver::int_ty() {
if (this->get("Int") != NULL) {
ASTType* ASTTypesResolver::int_ty(bool create) {
if (!create) {
return this->get("Int");
}
ASTType* instance = new ASTType("Int");
instance->llvm_type = llvm::Type::getInt32Ty(llvm::getGlobalContext());
instance->llvm_type = llvm::Type::getInt32Ty(this->llvm_context);
instance->primitive = true;
return instance;
}

ASTType* ASTTypesResolver::uint_ty() {
if (this->get("UInt") != NULL) {
ASTType* ASTTypesResolver::uint_ty(bool create) {
if (!create) {
return this->get("UInt");
}
ASTType* instance = new ASTType("UInt");
instance->llvm_type = llvm::Type::getInt32Ty(llvm::getGlobalContext());
instance->llvm_type = llvm::Type::getInt32Ty(this->llvm_context);
instance->primitive = true;
return instance;
}

ASTType* ASTTypesResolver::long_ty() {
if (this->get("Long") != NULL) {
ASTType* ASTTypesResolver::long_ty(bool create) {
if (!create) {
return this->get("Long");
}
ASTType* instance = new ASTType("Long");
instance->llvm_type = llvm::Type::getInt64Ty(llvm::getGlobalContext());
instance->llvm_type = llvm::Type::getInt64Ty(this->llvm_context);
instance->primitive = true;
return instance;
}

ASTType* ASTTypesResolver::ulong_ty() {
if (this->get("ULong") != NULL) {
ASTType* ASTTypesResolver::ulong_ty(bool create) {
if (!create) {
return this->get("ULong");
}
ASTType* instance = new ASTType("ULong");
instance->llvm_type = llvm::Type::getInt64Ty(llvm::getGlobalContext());
instance->llvm_type = llvm::Type::getInt64Ty(this->llvm_context);
instance->primitive = true;
return instance;
}

ASTType* ASTTypesResolver::float_ty() {
if (this->get("Float") != NULL) {
ASTType* ASTTypesResolver::float_ty(bool create) {
if (!create) {
return this->get("Float");
}
ASTType* instance = new ASTType("Float");
instance->llvm_type = llvm::Type::getFloatTy(llvm::getGlobalContext());
instance->llvm_type = llvm::Type::getFloatTy(this->llvm_context);
instance->primitive = true;
return instance;
}

ASTType* ASTTypesResolver::double_ty() {
if (this->get("Double") != NULL) {
ASTType* ASTTypesResolver::double_ty(bool create) {
if (!create) {
return this->get("Double");
}
ASTType* instance = new ASTType("Double");
instance->llvm_type = llvm::Type::getDoubleTy(llvm::getGlobalContext());
instance->llvm_type = llvm::Type::getDoubleTy(this->llvm_context);
instance->primitive = true;
return instance;
}
26 changes: 14 additions & 12 deletions sayaka/src/ast_types_resolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,22 @@

class ASTTypesResolver {
public:
ASTTypesResolver();
llvm::LLVMContext& llvm_context;

ASTTypesResolver(llvm::LLVMContext& llvm_context);
ASTType* get(std::string);

ASTType* bit_ty();
ASTType* byte_ty();
ASTType* ubyte_ty();
ASTType* short_ty();
ASTType* ushort_ty();
ASTType* int_ty();
ASTType* uint_ty();
ASTType* long_ty();
ASTType* ulong_ty();
ASTType* float_ty();
ASTType* double_ty();
ASTType* bit_ty(bool = false);
ASTType* byte_ty(bool = false);
ASTType* ubyte_ty(bool = false);
ASTType* short_ty(bool = false);
ASTType* ushort_ty(bool = false);
ASTType* int_ty(bool = false);
ASTType* uint_ty(bool = false);
ASTType* long_ty(bool = false);
ASTType* ulong_ty(bool = false);
ASTType* float_ty(bool = false);
ASTType* double_ty(bool = false);

~ASTTypesResolver();
private:
Expand Down
2 changes: 1 addition & 1 deletion sayaka/src/code_gen_context.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "code_gen_context.h"
#include <iostream>

CodeGenContext::CodeGenContext(llvm::LLVMContext& llvm_context) : builder(llvm_context), llvm_context(llvm_context) {
CodeGenContext::CodeGenContext(llvm::LLVMContext& llvm_context) : builder(llvm_context), llvm_context(llvm_context), ast_types_resolver(llvm_context) {
this->module = new llvm::Module("top", this->llvm_context);
}

Expand Down
20 changes: 8 additions & 12 deletions sayaka/src/code_gen_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,18 @@

#include <stack>
#include <list>
#include <llvm/IR/Module.h>
#include <llvm/IR/Function.h>
#include <llvm/IR/Type.h>
#include <llvm/Analysis/Passes.h>
#include <llvm/Analysis/Verifier.h>
#include <llvm/ExecutionEngine/ExecutionEngine.h>
#include <llvm/ExecutionEngine/JIT.h>
#include <llvm/IR/DataLayout.h>
#include <llvm/IR/DerivedTypes.h>
#include <llvm/IR/IRBuilder.h>
#include <llvm/IR/LLVMContext.h>
#include <llvm/IR/Module.h>
#include <llvm/PassManager.h>
#include <llvm/IR/Instructions.h>
#include <llvm/IR/CallingConv.h>
#include <llvm/Bitcode/ReaderWriter.h>
#include <llvm/Analysis/Verifier.h>
#include <llvm/Assembly/PrintModulePass.h>
#include <llvm/IR/IRBuilder.h>
#include <llvm/Support/TargetSelect.h>
#include <llvm/ExecutionEngine/GenericValue.h>
#include <llvm/ExecutionEngine/JIT.h>
#include <llvm/Support/raw_ostream.h>
#include <llvm/Transforms/Scalar.h>
#include "identifier_scope.h"
#include "ast_types_resolver.h"

Expand Down
1 change: 1 addition & 0 deletions sayaka/src/lexer.l
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
%option noyywrap
%option never-interactive
%option nounistd
%option yylineno
%option bison-bridge
%option bison-locations

Expand Down
2 changes: 1 addition & 1 deletion sayaka/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace boost {

extern "C" {
int32_t test_fn(int32_t x) {
std::cout << (char) x << std::endl;
std::cout << x << std::endl;
return x * 2;
}
}
Expand Down
Loading

0 comments on commit 52f5fca

Please sign in to comment.