Scoped Variables

This commit is contained in:
Markus Hauschild
2013-06-01 20:29:32 +02:00
parent 9fb441dcfd
commit 2456d999d6
2 changed files with 25 additions and 12 deletions

View File

@@ -31,7 +31,7 @@ public:
virtual void visit(VariableDefinition* e); virtual void visit(VariableDefinition* e);
virtual void visit(LoadExpression* e); virtual void visit(LoadExpression* e);
void createAnonymousFunction(); void JIT(Expression* e);
private: private:
void putNamedValue(const std::string& name, llvm::Value* value); void putNamedValue(const std::string& name, llvm::Value* value);
llvm::Value* getNamedValue(const std::string& name); llvm::Value* getNamedValue(const std::string& name);
@@ -39,7 +39,9 @@ private:
llvm::IRBuilder<>* builder_; llvm::IRBuilder<>* builder_;
llvm::FunctionPassManager* fpm_; llvm::FunctionPassManager* fpm_;
llvm::Module* module_; llvm::Module* module_;
std::map<std::string, llvm::Value*> namedValues_;
int scope_;
std::vector<std::map<std::string, llvm::Value*>> namedValues_;
llvm::Value* value_; llvm::Value* value_;
}; };

View File

@@ -3,8 +3,11 @@
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());
module_ = module;
fpm_ = fpm; fpm_ = fpm;
module_ = module;
scope_ = 0;
namedValues_.push_back(std::map<std::string, llvm::Value*>());
// create external for random_if // create external for random_if
std::vector<llvm::Type*> argt(1, typeToLLVMType(Type::INT)); std::vector<llvm::Type*> argt(1, typeToLLVMType(Type::INT));
@@ -271,9 +274,11 @@ void CodeGenVisitor::visit(ReturnStatement* e) {
} }
void CodeGenVisitor::visit(Scope* e) { void CodeGenVisitor::visit(Scope* e) {
// TODO increment scope scope_++;
namedValues_.push_back(std::map<std::string, llvm::Value*>());
e->getSl()->accept(this); e->getSl()->accept(this);
// TODO decrement scope namedValues_.pop_back();
scope_--;
} }
void CodeGenVisitor::visit(StatementList* e) { void CodeGenVisitor::visit(StatementList* e) {
@@ -299,17 +304,23 @@ void CodeGenVisitor::visit(LoadExpression *e) {
value_ = getNamedValue(e->getId()); value_ = getNamedValue(e->getId());
} }
void CodeGenVisitor::createAnonymousFunction() { void CodeGenVisitor::JIT(Expression* e) {
if (value_) { // TODO implement ...
value_->dump();
}
// TODO implement
} }
void CodeGenVisitor::putNamedValue(const std::string& name, llvm::Value* value) { void CodeGenVisitor::putNamedValue(const std::string& name, llvm::Value* value) {
namedValues_[name] = value; namedValues_[scope_][name] = value;
} }
llvm::Value* CodeGenVisitor::getNamedValue(const std::string& name) { llvm::Value* CodeGenVisitor::getNamedValue(const std::string& name) {
return namedValues_[name]; llvm::Value* v = 0;
for (int i = scope_; i >= 0; i--) {
if (namedValues_[i][name] != 0) {
v = namedValues_[i][name];
break;
}
}
return v;
} }