More CodeGen
This commit is contained in:
@@ -33,6 +33,9 @@ public:
|
|||||||
|
|
||||||
void createAnonymousFunction();
|
void createAnonymousFunction();
|
||||||
private:
|
private:
|
||||||
|
void putNamedValue(const std::string& name, llvm::Value* value);
|
||||||
|
llvm::Value* getNamedValue(const std::string& name);
|
||||||
|
|
||||||
llvm::IRBuilder<>* builder_;
|
llvm::IRBuilder<>* builder_;
|
||||||
llvm::FunctionPassManager* fpm_;
|
llvm::FunctionPassManager* fpm_;
|
||||||
llvm::Module* module_;
|
llvm::Module* module_;
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
#include "AST/CodeGenVisitor.h"
|
#include "AST/CodeGenVisitor.h"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
CodeGenVisitor::CodeGenVisitor(llvm::Module* module, llvm::FunctionPassManager *fpm) {
|
CodeGenVisitor::CodeGenVisitor(llvm::Module* module, llvm::FunctionPassManager *fpm) {
|
||||||
builder_ = new llvm::IRBuilder<>(llvm::getGlobalContext());
|
builder_ = new llvm::IRBuilder<>(llvm::getGlobalContext());
|
||||||
@@ -117,8 +118,30 @@ void CodeGenVisitor::visit(FunctionDefinition* e) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO set arg names
|
// set arg names
|
||||||
|
unsigned idx = 0;
|
||||||
|
for(auto ai = f->arg_begin(); idx != params.size(); ++ai, ++idx) {
|
||||||
|
ai->setName(params[idx].second);
|
||||||
|
//std::cout << "naming parameter " << idx << ": " << params[idx].second << std::endl;
|
||||||
|
|
||||||
|
// add to symbol table
|
||||||
|
putNamedValue(params[idx].second, ai);
|
||||||
|
}
|
||||||
|
|
||||||
|
llvm::BasicBlock* bb = llvm::BasicBlock::Create(llvm::getGlobalContext(), "entry", f);
|
||||||
|
builder_->SetInsertPoint(bb);
|
||||||
|
|
||||||
|
// put all arguments on the stack
|
||||||
|
idx = 0;
|
||||||
|
for(auto ai = f->arg_begin(); idx != params.size(); ++ai, ++idx) {
|
||||||
|
std::string name = params[idx].second;
|
||||||
|
// TODO ...
|
||||||
|
}
|
||||||
|
|
||||||
|
// build code for the statements
|
||||||
|
e->getSl()->accept(this);
|
||||||
|
|
||||||
|
// TODO implement ...
|
||||||
value_ = f;
|
value_ = f;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -135,12 +158,20 @@ void CodeGenVisitor::visit(RandomIfStatement* e) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void CodeGenVisitor::visit(ReturnStatement* e) {
|
void CodeGenVisitor::visit(ReturnStatement* e) {
|
||||||
|
e->getExpr()->accept(this);
|
||||||
|
builder_->CreateRet(value_);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CodeGenVisitor::visit(Scope* e) {
|
void CodeGenVisitor::visit(Scope* e) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void CodeGenVisitor::visit(StatementList* e) {
|
void CodeGenVisitor::visit(StatementList* e) {
|
||||||
|
auto statements = e->getStatements();
|
||||||
|
auto iter = statements.begin();
|
||||||
|
auto end = statements.end();
|
||||||
|
for (; iter != end; ++iter) {
|
||||||
|
(*iter)->accept(this);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void CodeGenVisitor::visit(ValueList* e) {
|
void CodeGenVisitor::visit(ValueList* e) {
|
||||||
@@ -158,3 +189,11 @@ void CodeGenVisitor::createAnonymousFunction() {
|
|||||||
}
|
}
|
||||||
// TODO implement
|
// TODO implement
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CodeGenVisitor::putNamedValue(const std::string& name, llvm::Value* value) {
|
||||||
|
namedValues_[name] = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
llvm::Value* CodeGenVisitor::getNamedValue(const std::string& name) {
|
||||||
|
return namedValues_[name];
|
||||||
|
}
|
||||||
|
|||||||
@@ -42,6 +42,7 @@ int main() {
|
|||||||
rules.add("void", T_VOID);
|
rules.add("void", T_VOID);
|
||||||
rules.add("rfor", T_RFOR);
|
rules.add("rfor", T_RFOR);
|
||||||
rules.add("rif", T_RIF);
|
rules.add("rif", T_RIF);
|
||||||
|
rules.add("string", T_STRING);
|
||||||
|
|
||||||
// special characters
|
// special characters
|
||||||
rules.add("\"(\"", T_LPAREN);
|
rules.add("\"(\"", T_LPAREN);
|
||||||
|
|||||||
Reference in New Issue
Block a user