From 9247ed3714abe9ec88e211960ad4200f4459d570 Mon Sep 17 00:00:00 2001 From: Philippe Tillet Date: Wed, 19 Dec 2018 11:25:29 -0500 Subject: [PATCH] [Code generation] --- include/ast.h | 26 +++++++++++++++++--------- lib/codegen.cpp | 37 +++++++++++++++++++++++++++---------- 2 files changed, 44 insertions(+), 19 deletions(-) diff --git a/include/ast.h b/include/ast.h index a01c20fcc..da14081ab 100644 --- a/include/ast.h +++ b/include/ast.h @@ -58,7 +58,7 @@ enum TYPE_T{ // AST class node { public: - virtual void codegen(module*) { } + virtual void codegen(module*) const { } }; template @@ -71,7 +71,7 @@ public: return this; } - void codegen(module* mod) + void codegen(module* mod) const { for(T x: values_){ x->codegen(mod); } } const std::list &values() const @@ -155,14 +155,17 @@ class statement: public node{ }; +class initializer; class declaration: public node{ public: declaration(node *spec, node *init) - : spec_(spec), init_(init) { } + : spec_(spec), init_((list*)init) { } + + void codegen(module* mod) const; public: const node *spec_; - const node *init_; + const list *init_; }; @@ -174,6 +177,8 @@ public: compound_statement(node* decls, node* statements) : decls_((declarations_t)decls), statements_((statements_t)statements) {} + virtual void codegen(module* mod) const; + private: declarations_t decls_; statements_t statements_; @@ -307,12 +312,15 @@ private: llvm::Type* type_impl(module* mod, llvm::Type *type) const; public: - initializer(node *id, node *initializer) - : declarator(id), initializer_(initializer){ } + initializer(node *decl, node *init) + : declarator((node*)((declarator*)decl)->id()), + decl_((declarator*)decl), init_(init){ } + void codegen(module *) const; public: - const node *initializer_; + const declarator *decl_; + const node *init_; }; @@ -332,7 +340,7 @@ public: function_definition(node *spec, node *header, node *body) : spec_((declaration_specifier*)spec), header_((function *)header), body_((compound_statement*)body) { } - void codegen(module* mod); + void codegen(module* mod) const; public: const declaration_specifier *spec_; @@ -351,7 +359,7 @@ public: return this; } - void codegen(module* mod); + void codegen(module* mod) const; private: list* decls_; diff --git a/lib/codegen.cpp b/lib/codegen.cpp index e5efd686b..6fd528120 100644 --- a/lib/codegen.cpp +++ b/lib/codegen.cpp @@ -31,7 +31,7 @@ llvm::IRBuilder<>& module::builder() { namespace ast{ /* Translation unit */ -void translation_unit::codegen(module *mod){ +void translation_unit::codegen(module *mod) const{ decls_->codegen(mod); } @@ -78,8 +78,8 @@ Type* tile::type_impl(module*, Type *type) const{ } // Initializer -Type* initializer::type_impl(module *, Type *type) const{ - return type; +Type* initializer::type_impl(module *mod, Type *type) const{ + return decl_->type(mod, type); } // Pointer @@ -97,14 +97,31 @@ Type* function::type_impl(module*mod, Type *type) const{ } /* Function definition */ -void function_definition::codegen(module *mod){ - llvm::FunctionType *prototype = (llvm::FunctionType *)header_->type(mod, spec_->type(mod)); +void function_definition::codegen(module *mod) const{ + FunctionType *prototype = (FunctionType *)header_->type(mod, spec_->type(mod)); const std::string &name = header_->id()->name(); - llvm::Function *fn = llvm::Function::Create(prototype, llvm::Function::ExternalLinkage, name, mod->handle()); - llvm::BasicBlock::Create(mod->handle()->getContext(), "entry", fn); - mod->builder().SetInsertPoint(); - -} + Function *fn = Function::Create(prototype, Function::ExternalLinkage, name, mod->handle()); + BasicBlock *entry = BasicBlock::Create(mod->handle()->getContext(), "entry", fn); + mod->builder().SetInsertPoint(entry); + body_->codegen(mod); +} + +/* Statements */ +void compound_statement::codegen(module* mod) const{ + decls_->codegen(mod); + statements_->codegen(mod); +} + +/* Declaration */ +void declaration::codegen(module* mod) const{ + +} + +/* Initializat */ +void initializer::codegen(module *) const{ + +} + }