[Code generation]

This commit is contained in:
Philippe Tillet
2018-12-19 11:25:29 -05:00
parent 951e9733ea
commit 9247ed3714
2 changed files with 44 additions and 19 deletions

View File

@@ -58,7 +58,7 @@ enum TYPE_T{
// AST
class node {
public:
virtual void codegen(module*) { }
virtual void codegen(module*) const { }
};
template<class T>
@@ -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<T> &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<initializer*>*)init) { }
void codegen(module* mod) const;
public:
const node *spec_;
const node *init_;
const list<initializer*> *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<node*>* decls_;

View File

@@ -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{
}
}