diff options
Diffstat (limited to 'llvm.cpp')
-rw-r--r-- | llvm.cpp | 88 |
1 files changed, 0 insertions, 88 deletions
@@ -486,94 +486,6 @@ APrimitive::compile(CEnv& cenv) throw Error(loc, "unknown primitive"); } -AType* -AConsCall::functionType(CEnv& cenv) -{ - ATuple* protTypes = new ATuple(loc, cenv.type(at(1)), cenv.type(at(2)), 0); - AType* cellType = new AType(loc, - cenv.penv.sym("Pair"), cenv.type(at(1)), cenv.type(at(2)), 0); - return new AType(at(0)->loc, cenv.penv.sym("Fn"), protTypes, cellType, 0); -} - -void -AConsCall::lift(CEnv& cenv) -{ - AType* funcType = functionType(cenv); - if (funcs.find(functionType(cenv))) - return; - - ACall::lift(cenv); - - ATuple* protT = new ATuple(loc, cenv.type(at(1)), cenv.type(at(2)), 0); - - vector<const Type*> types; - size_t sz = 0; - for (size_t i = 1; i < size(); ++i) { - const Type* t = lltype(cenv.type(at(i))); - types.push_back(t); - sz += t->getPrimitiveSizeInBits(); - } - sz = (sz % 8 == 0) ? sz / 8 : sz / 8 + 1; - - llvm::IRBuilder<>& builder = llengine(cenv)->builder; - - StructType* sT = StructType::get(types, false); - Type* pT = PointerType::get(sT, 0); - - // Write function declaration - vector<string> argNames; - argNames.push_back("car"); - argNames.push_back("cdr"); - Function* func = compileFunction(cenv, cenv.gensym("cons"), pT, *protT, loc, argNames); - - Value* mem = builder.CreateCall(LLVal(cenv.alloc), ConstantInt::get(Type::Int32Ty, sz), "mem"); - Value* cell = builder.CreateBitCast(mem, pT, "cell"); - Value* s = builder.CreateGEP(cell, ConstantInt::get(Type::Int32Ty, 0), "pair"); - Value* carP = builder.CreateStructGEP(s, 0, "car"); - Value* cdrP = builder.CreateStructGEP(s, 1, "cdr"); - - Function::arg_iterator ai = func->arg_begin(); - Value& carArg = *ai++; - Value& cdrArg = *ai++; - builder.CreateStore(&carArg, carP); - builder.CreateStore(&cdrArg, cdrP); - builder.CreateRet(cell); - - cenv.optimise(func); - funcs.push_back(make_pair(funcType, func)); -} - -CValue -AConsCall::compile(CEnv& cenv) -{ - vector<Value*> params(size() - 1); - for (size_t i = 1; i < size(); ++i) - params[i-1] = LLVal(cenv.compile(at(i))); - - return llengine(cenv)->builder.CreateCall(LLFunc(funcs.find(functionType(cenv))), - params.begin(), params.end()); -} - -CValue -ACarCall::compile(CEnv& cenv) -{ - AST* arg = cenv.tenv.resolve(at(1)); - Value* sP = LLVal(cenv.compile(arg)); - Value* s = llengine(cenv)->builder.CreateGEP(sP, ConstantInt::get(Type::Int32Ty, 0), "pair"); - Value* carP = llengine(cenv)->builder.CreateStructGEP(s, 0, "car"); - return llengine(cenv)->builder.CreateLoad(carP); -} - -CValue -ACdrCall::compile(CEnv& cenv) -{ - AST* arg = cenv.tenv.resolve(at(1)); - Value* sP = LLVal(cenv.compile(arg)); - Value* s = llengine(cenv)->builder.CreateGEP(sP, ConstantInt::get(Type::Int32Ty, 0), "pair"); - Value* cdrP = llengine(cenv)->builder.CreateStructGEP(s, 1, "cdr"); - return llengine(cenv)->builder.CreateLoad(cdrP); -} - /*************************************************************************** * EVAL/REPL * |