aboutsummaryrefslogtreecommitdiffstats
path: root/src/llvm.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/llvm.cpp')
-rw-r--r--src/llvm.cpp97
1 files changed, 48 insertions, 49 deletions
diff --git a/src/llvm.cpp b/src/llvm.cpp
index 939eeeb..6ef25bb 100644
--- a/src/llvm.cpp
+++ b/src/llvm.cpp
@@ -56,25 +56,25 @@ struct LLVMEngine : public Engine {
LLVMEngine();
virtual ~LLVMEngine();
- CFunc startFn(CEnv& cenv, const string& name, const ATuple* args, const AType* type);
- void pushFnArgs(CEnv& cenv, const ATuple* prot, const AType* type, CFunc f);
+ CFunc startFn(CEnv& cenv, const string& name, const ATuple* args, const ATuple* type);
+ void pushFnArgs(CEnv& cenv, const ATuple* prot, const ATuple* type, CFunc f);
void finishFn(CEnv& cenv, CFunc f, CVal ret);
void eraseFn(CEnv& cenv, CFunc f);
- CVal compileCall(CEnv& cenv, CFunc f, const AType* funcT, const vector<CVal>& args);
- CVal compileCons(CEnv& cenv, const AType* type, CVal rtti, const vector<CVal>& fields);
+ CVal compileCall(CEnv& cenv, CFunc f, const ATuple* funcT, const vector<CVal>& args);
+ CVal compileCons(CEnv& cenv, const ATuple* type, CVal rtti, const vector<CVal>& fields);
CVal compileDot(CEnv& cenv, CVal tup, int32_t index);
- CVal compileGlobalSet(CEnv& cenv, const string& s, CVal v, const AType* t);
+ CVal compileGlobalSet(CEnv& cenv, const string& s, CVal v, const AST* t);
CVal compileGlobalGet(CEnv& cenv, const string& s, CVal v);
CVal compileIf(CEnv& cenv, const AST* cond, const AST* then, const AST* aelse);
- CVal compileIsA(CEnv& cenv, CVal rtti, const ASymbol* tag);
+ CVal compileIsA(CEnv& cenv, CVal rtti, CVal tag);
CVal compileLiteral(CEnv& cenv, const AST* lit);
CVal compilePrimitive(CEnv& cenv, const ATuple* prim);
CVal compileString(CEnv& cenv, const char* str);
void writeModule(CEnv& cenv, std::ostream& os);
- const string call(CEnv& cenv, CFunc f, const AType* retT);
+ const string call(CEnv& cenv, CFunc f, const AST* retT);
private:
void appendBlock(LLVMEngine* engine, Function* function, BasicBlock* block) {
@@ -84,7 +84,7 @@ private:
inline Value* llVal(CVal v) { return static_cast<Value*>(v); }
inline Function* llFunc(CFunc f) { return static_cast<Function*>(f); }
- const Type* llType(const AType* t);
+ const Type* llType(const AST* t);
LLVMContext context;
Module* module;
@@ -127,41 +127,42 @@ LLVMEngine::~LLVMEngine()
}
const Type*
-LLVMEngine::llType(const AType* t)
+LLVMEngine::llType(const AST* t)
{
if (t == NULL) {
return NULL;
- } else if (t->kind == AType::VAR) {
+ } else if (AType::is_var(t)) {
// Kludge for _me closure parameter, will be casted
return PointerType::get(Type::getInt8Ty(context), NULL);
- } else if (t->kind == AType::NAME) {
- if (t->head()->str() == "Nothing") return Type::getVoidTy(context);
- if (t->head()->str() == "Bool") return Type::getInt1Ty(context);
- if (t->head()->str() == "Int") return Type::getInt32Ty(context);
- if (t->head()->str() == "Float") return Type::getFloatTy(context);
- if (t->head()->str() == "String") return PointerType::get(Type::getInt8Ty(context), NULL);
- if (t->head()->str() == "Quote") return PointerType::get(Type::getInt8Ty(context), NULL);
- } else if (t->kind == AType::EXPR && t->head()->str() == "Fn") {
- AType::const_iterator i = t->begin();
- const ATuple* protT = (*++i)->to_tuple();
- const AType* retT = (*++i)->as_type();
+ } else if (AType::is_name(t)) {
+ const std::string sym(t->as_symbol()->sym());
+ if (sym == "Nothing") return Type::getVoidTy(context);
+ if (sym == "Bool") return Type::getInt1Ty(context);
+ if (sym == "Int") return Type::getInt32Ty(context);
+ if (sym == "Float") return Type::getFloatTy(context);
+ if (sym == "String") return PointerType::get(Type::getInt8Ty(context), NULL);
+ if (sym == "Quote") return PointerType::get(Type::getInt8Ty(context), NULL);
+ } else if (is_form(t, "Fn")) {
+ ATuple::const_iterator i = t->as_tuple()->begin();
+ const ATuple* protT = (*++i)->to_tuple();
+ const AST* retT = (*++i);
if (!llType(retT))
return NULL;
vector<const Type*> cprot;
FOREACHP(ATuple::const_iterator, i, protT) {
- const Type* lt = llType((*i)->to_type());
+ const Type* lt = llType(*i);
if (!lt)
return NULL;
cprot.push_back(lt);
}
return PointerType::get(FunctionType::get(llType(retT), cprot, false), 0);
- } else if (t->kind == AType::EXPR && isupper(t->head()->str()[0])) {
+ } else if (AType::is_expr(t) && isupper(t->as_tuple()->head()->str()[0])) {
vector<const Type*> ctypes;
ctypes.push_back(PointerType::get(Type::getInt8Ty(context), NULL)); // RTTI
- for (AType::const_iterator i = t->iter_at(1); i != t->end(); ++i) {
- const Type* lt = llType((*i)->to_type());
+ for (ATuple::const_iterator i = t->as_tuple()->iter_at(1); i != t->as_tuple()->end(); ++i) {
+ const Type* lt = llType(*i);
if (!lt)
return NULL;
ctypes.push_back(lt);
@@ -180,25 +181,25 @@ bitsToBytes(size_t bits)
}
CVal
-LLVMEngine::compileCall(CEnv& cenv, CFunc f, const AType* funcT, const vector<CVal>& args)
+LLVMEngine::compileCall(CEnv& cenv, CFunc f, const ATuple* funcT, const vector<CVal>& args)
{
vector<Value*> llArgs(*reinterpret_cast<const vector<Value*>*>(&args));
Value* closure = builder.CreateBitCast(llArgs[0],
- llType(funcT->prot()->head()->as_type()),
+ llType(funcT->prot()->head()),
cenv.penv.gensymstr("you"));
llArgs[0] = closure;
return builder.CreateCall(llFunc(f), llArgs.begin(), llArgs.end());
}
CVal
-LLVMEngine::compileCons(CEnv& cenv, const AType* type, CVal rtti, const vector<CVal>& fields)
+LLVMEngine::compileCons(CEnv& cenv, const ATuple* type, CVal rtti, const vector<CVal>& fields)
{
// Find size of memory required
size_t s = engine->getTargetData()->getTypeSizeInBits(
PointerType::get(Type::getInt8Ty(context), NULL));
assert(type->begin() != type->end());
- for (AType::const_iterator i = type->iter_at(1); i != type->end(); ++i)
- s += engine->getTargetData()->getTypeSizeInBits(llType((*i)->as_type()));
+ for (ATuple::const_iterator i = type->iter_at(1); i != type->end(); ++i)
+ s += engine->getTargetData()->getTypeSizeInBits(llType(*i));
// Allocate struct
Value* structSize = ConstantInt::get(Type::getInt32Ty(context), bitsToBytes(s));
@@ -247,19 +248,18 @@ LLVMEngine::compileString(CEnv& cenv, const char* str)
CFunc
LLVMEngine::startFn(
- CEnv& cenv, const std::string& name, const ATuple* args, const AType* type)
+ CEnv& cenv, const std::string& name, const ATuple* args, const ATuple* type)
{
- const AType* argsT = type->prot()->as_type();
- const AType* retT = type->list_last()->as_type();
+ const ATuple* argsT = type->prot();
+ const AST* retT = type->list_last();
Function::LinkageTypes linkage = Function::ExternalLinkage;
vector<const Type*> cprot;
FOREACHP(ATuple::const_iterator, i, argsT) {
- const AType* at = (*i)->as_type();
- THROW_IF(!llType(at), Cursor(), string("non-concrete parameter :: ")
- + at->str())
- cprot.push_back(llType(at));
+ THROW_IF(!llType(*i), Cursor(), string("non-concrete parameter :: ")
+ + (*i)->str())
+ cprot.push_back(llType(*i));
}
THROW_IF(!llType(retT), Cursor(),
@@ -285,12 +285,12 @@ LLVMEngine::startFn(
}
void
-LLVMEngine::pushFnArgs(CEnv& cenv, const ATuple* prot, const AType* type, CFunc cfunc)
+LLVMEngine::pushFnArgs(CEnv& cenv, const ATuple* prot, const ATuple* type, CFunc cfunc)
{
cenv.push();
- const AType* argsT = type->prot()->as_type();
- Function* f = llFunc(cfunc);
+ const ATuple* argsT = type->prot();
+ Function* f = llFunc(cfunc);
// Bind argument values in CEnv
ATuple::const_iterator p = prot->begin();
@@ -298,7 +298,7 @@ LLVMEngine::pushFnArgs(CEnv& cenv, const ATuple* prot, const AType* type, CFunc
assert(prot->size() == argsT->size());
assert(prot->size() == f->num_args());
for (Function::arg_iterator a = f->arg_begin(); a != f->arg_end(); ++a, ++p, ++pT) {
- const AType* t = cenv.resolveType((*pT)->as_type());
+ const AST* t = cenv.resolveType(*pT);
THROW_IF(!llType(t), (*p)->loc, "untyped parameter\n");
cenv.def((*p)->as_symbol(), *p, t, &*a);
}
@@ -332,7 +332,7 @@ LLVMEngine::compileIf(CEnv& cenv, const AST* cond, const AST* then, const AST* a
BasicBlock* thenBB = BasicBlock::Create(context, (format("then%1%") % labelIndex).str());
BasicBlock* nextBB = BasicBlock::Create(context, (format("else%1%") % labelIndex).str());
- const AType* type = cenv.type(then);
+ const AST* type = cenv.type(then);
++labelIndex;
@@ -365,11 +365,10 @@ LLVMEngine::compileIf(CEnv& cenv, const AST* cond, const AST* then, const AST* a
}
CVal
-LLVMEngine::compileIsA(CEnv& cenv, CVal rtti, const ASymbol* tag)
+LLVMEngine::compileIsA(CEnv& cenv, CVal rtti, CVal tag)
{
- LLVMEngine* engine = reinterpret_cast<LLVMEngine*>(cenv.engine());
- const AType* patT = new AType(tag, 0, Cursor());
- Value* typeV = llVal(resp_compile(cenv, patT));
+ LLVMEngine* engine = reinterpret_cast<LLVMEngine*>(cenv.engine());
+ Value* typeV = llVal(tag);
return engine->builder.CreateICmp(CmpInst::ICMP_EQ, llVal(rtti), typeV);
}
@@ -421,7 +420,7 @@ LLVMEngine::compilePrimitive(CEnv& cenv, const ATuple* prim)
}
CVal
-LLVMEngine::compileGlobalSet(CEnv& cenv, const string& sym, CVal val, const AType* type)
+LLVMEngine::compileGlobalSet(CEnv& cenv, const string& sym, CVal val, const AST* type)
{
LLVMEngine* engine = reinterpret_cast<LLVMEngine*>(cenv.engine());
GlobalVariable* global = new GlobalVariable(*module, llType(type), false,
@@ -449,7 +448,7 @@ LLVMEngine::writeModule(CEnv& cenv, std::ostream& os)
}
const string
-LLVMEngine::call(CEnv& cenv, CFunc f, const AType* retT)
+LLVMEngine::call(CEnv& cenv, CFunc f, const AST* retT)
{
void* fp = engine->getPointerToFunction(llFunc(f));
const Type* t = llType(retT);
@@ -463,7 +462,7 @@ LLVMEngine::call(CEnv& cenv, CFunc f, const AType* retT)
ss << showpoint << ((float (*)())fp)();
} else if (t == Type::getInt1Ty(context)) {
ss << (((bool (*)())fp)() ? "#t" : "#f");
- } else if (retT->head()->str() == "String") {
+ } else if (retT->str() == "String") {
const std::string s(((char* (*)())fp)());
ss << "\"";
for (std::string::const_iterator i = s.begin(); i != s.end(); ++i) {