IT DOES STUFF!!!!!1¹11eleven
This commit is contained in:
@@ -57,7 +57,7 @@
|
||||
|
||||
%type program {int}
|
||||
program ::= fundef(F). { PrintVisitor* pv = new PrintVisitor; F->accept(pv); F->accept(cv); delete pv; }
|
||||
program ::= expr(E). { PrintVisitor* pv = new PrintVisitor; E->accept(pv); E->accept(cv); delete pv; }
|
||||
program ::= expr(E). { PrintVisitor* pv = new PrintVisitor; E->accept(pv); cv->JIT(E); delete pv; }
|
||||
|
||||
%type fundef {FunctionDefinition*}
|
||||
fundef(A) ::= type(T) T_IDENTIFIER(ID) T_LPAREN params(P) T_RPAREN T_BEGIN statements(S) T_END.
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#define CODEGENVISITOR_H
|
||||
|
||||
#include "ASTVisitor.h"
|
||||
#include "llvm/ExecutionEngine/ExecutionEngine.h"
|
||||
#include "llvm/IRBuilder.h"
|
||||
#include "llvm/Module.h"
|
||||
#include "llvm/PassManager.h"
|
||||
@@ -10,7 +11,7 @@
|
||||
|
||||
class CodeGenVisitor : public ASTVisitor {
|
||||
public:
|
||||
CodeGenVisitor(llvm::Module* module, llvm::FunctionPassManager* fpm);
|
||||
CodeGenVisitor(llvm::Module* module, llvm::FunctionPassManager* fpm, llvm::ExecutionEngine* ee);
|
||||
virtual ~CodeGenVisitor();
|
||||
|
||||
virtual void visit(AssignmentExpression* e);
|
||||
@@ -39,6 +40,7 @@ private:
|
||||
llvm::IRBuilder<>* builder_;
|
||||
llvm::FunctionPassManager* fpm_;
|
||||
llvm::Module* module_;
|
||||
llvm::ExecutionEngine* ee_;
|
||||
|
||||
int scope_;
|
||||
std::vector<std::map<std::string, llvm::Value*>> namedValues_;
|
||||
|
||||
@@ -2,10 +2,11 @@
|
||||
#include "llvm/Analysis/Verifier.h"
|
||||
#include <iostream>
|
||||
|
||||
CodeGenVisitor::CodeGenVisitor(llvm::Module* module, llvm::FunctionPassManager *fpm) {
|
||||
CodeGenVisitor::CodeGenVisitor(llvm::Module* module, llvm::FunctionPassManager *fpm, llvm::ExecutionEngine *ee) {
|
||||
builder_ = new llvm::IRBuilder<>(llvm::getGlobalContext());
|
||||
fpm_ = fpm;
|
||||
module_ = module;
|
||||
ee_ = ee;
|
||||
|
||||
scope_ = 0;
|
||||
namedValues_.push_back(std::map<std::string, llvm::Value*>());
|
||||
@@ -78,6 +79,7 @@ void CodeGenVisitor::visit(ConstantExpression* e) {
|
||||
}
|
||||
|
||||
void CodeGenVisitor::visit(ExpressionStatement* e) {
|
||||
e->getExpr()->accept(this);
|
||||
}
|
||||
|
||||
void CodeGenVisitor::visit(ForStatement* e)
|
||||
@@ -200,12 +202,11 @@ void CodeGenVisitor::visit(FunctionDefinition* e) {
|
||||
// build code for the statements
|
||||
e->getSl()->accept(this);
|
||||
|
||||
f->dump();
|
||||
// validate generated code
|
||||
llvm::verifyFunction(*f);
|
||||
|
||||
// optimize function
|
||||
//fpm_->run(*f);
|
||||
fpm_->run(*f);
|
||||
|
||||
value_ = f;
|
||||
}
|
||||
@@ -314,7 +315,26 @@ void CodeGenVisitor::visit(LoadExpression *e) {
|
||||
}
|
||||
|
||||
void CodeGenVisitor::JIT(Expression* e) {
|
||||
// TODO implement ...
|
||||
StatementList* sl = new StatementList();
|
||||
sl->addStatement(new ReturnStatement(e));
|
||||
FunctionDefinition* fd = new FunctionDefinition(Type::INT, "", new ParameterList(), sl);
|
||||
|
||||
value_ = 0;
|
||||
fd->accept(this);
|
||||
|
||||
if (!value_) {
|
||||
delete fd;
|
||||
throw "error evaluating expression";
|
||||
}
|
||||
|
||||
llvm::Function* f = dynamic_cast<llvm::Function*>(value_);
|
||||
|
||||
void* fPtr = ee_->getPointerToFunction(f);
|
||||
|
||||
// some casting ... because we like magic
|
||||
int (*fP)() = (int (*)())(intptr_t)fPtr;
|
||||
|
||||
std::cout << "Evaluated to: " << fP() << std::endl;
|
||||
}
|
||||
|
||||
void CodeGenVisitor::putNamedValue(const std::string& name, llvm::Value* value) {
|
||||
|
||||
@@ -106,7 +106,7 @@ int main() {
|
||||
|
||||
fpm->doInitialization();
|
||||
|
||||
CodeGenVisitor* cv = new CodeGenVisitor(module, fpm);
|
||||
CodeGenVisitor* cv = new CodeGenVisitor(module, fpm, ee);
|
||||
|
||||
void* parser = scullyParserAlloc(malloc);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user