diff options
Diffstat (limited to 'src/llvm.cpp')
-rw-r--r-- | src/llvm.cpp | 25 |
1 files changed, 24 insertions, 1 deletions
diff --git a/src/llvm.cpp b/src/llvm.cpp index 26b4bf2..fa4e99e 100644 --- a/src/llvm.cpp +++ b/src/llvm.cpp @@ -56,7 +56,7 @@ llType(const AType* t) } else if (t->kind == AType::EXPR && t->head()->str() == "Fn") { AType::const_iterator i = t->begin(); const ATuple* protT = (*++i)->to<const ATuple*>(); - const AType* retT = (*i)->as<const AType*>(); + const AType* retT = (*++i)->as<const AType*>(); if (!llType(retT)) return NULL; @@ -173,6 +173,8 @@ struct LLVMEngine : public Engine { CVal compileLiteral(CEnv& cenv, AST* lit); CVal compilePrimitive(CEnv& cenv, APrimitive* prim); CVal compileIf(CEnv& cenv, AIf* aif); + CVal compileGlobal(CEnv& cenv, AType* type, const string& name, CVal val); + CVal getGlobal(CEnv& cenv, CVal val); void writeModule(CEnv& cenv, std::ostream& os) { AssemblyAnnotationWriter writer; @@ -194,6 +196,8 @@ struct LLVMEngine : public Engine { ss << (((bool (*)())fp)() ? "#t" : "#f"); else if (t != Type::VoidTy) ss << ((void* (*)())fp)(); + else + ((void (*)())fp)(); return ss.str(); } @@ -433,3 +437,22 @@ LLVMEngine::compilePrimitive(CEnv& cenv, APrimitive* prim) throw Error(prim->loc, "unknown primitive"); } + +CVal +LLVMEngine::compileGlobal(CEnv& cenv, AType* type, const string& name, CVal val) +{ + LLVMEngine* engine = reinterpret_cast<LLVMEngine*>(cenv.engine()); + Constant* init = Constant::getNullValue(llType(type)); + GlobalVariable* global = new GlobalVariable(llType(type), false, + GlobalValue::ExternalLinkage, Constant::getNullValue(llType(type)), name, module); + + engine->builder.CreateStore(llVal(val), global); + return global; +} + +CVal +LLVMEngine::getGlobal(CEnv& cenv, CVal val) +{ + LLVMEngine* engine = reinterpret_cast<LLVMEngine*>(cenv.engine()); + return engine->builder.CreateLoad(llVal(val), "globalPtr"); +} |