diff options
Diffstat (limited to 'src/llvm.cpp')
-rw-r--r-- | src/llvm.cpp | 53 |
1 files changed, 25 insertions, 28 deletions
diff --git a/src/llvm.cpp b/src/llvm.cpp index 31e6f6d..eabe508 100644 --- a/src/llvm.cpp +++ b/src/llvm.cpp @@ -99,14 +99,14 @@ struct LLVMEngine : public Engine { throw Error(t->loc, string("Unknown primitive type `") + t->str() + "'"); } 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 ATuple* protT = (*++i)->to_tuple(); + const AType* retT = (*++i)->as_type(); if (!llType(retT)) return NULL; vector<const Type*> cprot; FOREACHP(ATuple::const_iterator, i, protT) { - const Type* lt = llType((*i)->to<const AType*>()); + const Type* lt = llType((*i)->to_type()); if (!lt) return NULL; cprot.push_back(lt); @@ -117,7 +117,7 @@ struct LLVMEngine : public Engine { 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<const AType*>()); + const Type* lt = llType((*i)->to_type()); if (!lt) return NULL; ctypes.push_back(lt); @@ -131,14 +131,14 @@ struct LLVMEngine : public Engine { CFunc startFunction(CEnv& cenv, const std::string& name, const ATuple* args, const AType* type) { - const AType* argsT = type->prot()->as<const AType*>(); - const AType* retT = type->list_last()->as<const AType*>(); + const AType* argsT = type->prot()->as_type(); + const AType* retT = type->list_last()->as_type(); Function::LinkageTypes linkage = Function::ExternalLinkage; vector<const Type*> cprot; FOREACHP(ATuple::const_iterator, i, argsT) { - const AType* at = (*i)->as<const AType*>(); + const AType* at = (*i)->as_type(); THROW_IF(!llType(at), Cursor(), string("non-concrete parameter :: ") + at->str()) cprot.push_back(llType(at)); @@ -158,7 +158,7 @@ struct LLVMEngine : public Engine { // Set argument names in generated code Function::arg_iterator a = f->arg_begin(); for (ATuple::const_iterator i = args->begin(); i != args->end(); ++a, ++i) - a->setName((*i)->as<const ASymbol*>()->cppstr); + a->setName((*i)->as_symbol()->cppstr); BasicBlock* bb = BasicBlock::Create(context, "entry", f); builder.SetInsertPoint(bb); @@ -186,7 +186,7 @@ struct LLVMEngine : public Engine { CVal compileCall(CEnv& cenv, CFunc f, const AType* 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<const AType*>()), + llType(funcT->prot()->head()->as_type()), cenv.penv.gensymstr("you")); llArgs[0] = closure; return builder.CreateCall(llFunc(f), llArgs.begin(), llArgs.end()); @@ -279,7 +279,7 @@ LLVMEngine::compileTup(CEnv& cenv, const AType* type, CVal rtti, const vector<CV 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<const AType*>())); + s += engine->getTargetData()->getTypeSizeInBits(llType((*i)->as_type())); // Allocate struct Value* structSize = ConstantInt::get(Type::getInt32Ty(context), bitsToBytes(s)); @@ -308,19 +308,16 @@ LLVMEngine::compileDot(CEnv& cenv, CVal tup, int32_t index) CVal LLVMEngine::compileLiteral(CEnv& cenv, const AST* lit) { - const ALiteral<int32_t>* ilit = dynamic_cast<const ALiteral<int32_t>*>(lit); - if (ilit) - return ConstantInt::get(Type::getInt32Ty(context), ilit->val, true); - - const ALiteral<float>* flit = dynamic_cast<const ALiteral<float>*>(lit); - if (flit) - return ConstantFP::get(Type::getFloatTy(context), flit->val); - - const ALiteral<bool>* blit = dynamic_cast<const ALiteral<bool>*>(lit); - if (blit) - return ConstantInt::get(Type::getInt1Ty(context), blit->val); - - throw Error(lit->loc, "Unknown literal type"); + switch (lit->tag()) { + case T_BOOL: + return ConstantInt::get(Type::getInt1Ty(context), ((const ALiteral<bool>*)lit)->val); + case T_FLOAT: + return ConstantFP::get(Type::getFloatTy(context), ((const ALiteral<float>*)lit)->val); + case T_INT32: + return ConstantInt::get(Type::getInt32Ty(context), ((const ALiteral<int32_t>*)lit)->val, true); + default: + throw Error(lit->loc, "Unknown literal type"); + } } CVal @@ -334,7 +331,7 @@ LLVMEngine::pushFunctionArgs(CEnv& cenv, const ATuple* fn, const AType* type, CF { cenv.push(); - const AType* argsT = type->prot()->as<const AType*>(); + const AType* argsT = type->prot()->as_type(); // Bind argument values in CEnv vector<Value*> args; @@ -343,10 +340,10 @@ LLVMEngine::pushFunctionArgs(CEnv& cenv, const ATuple* fn, const AType* type, CF assert(fn->prot()->size() == argsT->size()); assert(fn->prot()->size() == f->num_args()); for (Function::arg_iterator a = llFunc(f)->arg_begin(); a != llFunc(f)->arg_end(); ++a, ++p, ++pT) { - const AType* t = (*pT)->as<const AType*>(); + const AType* t = (*pT)->as_type(); const Type* lt = llType(t); THROW_IF(!lt, fn->loc, "untyped parameter\n"); - cenv.def((*p)->as<const ASymbol*>(), *p, t, &*a); + cenv.def((*p)->as_symbol(), *p, t, &*a); } } @@ -419,7 +416,7 @@ LLVMEngine::compileMatch(CEnv& cenv, const ATuple* match) for (ATuple::const_iterator i = match->iter_at(2); i != match->end(); ++idx) { const AST* pat = *i++; const AST* body = *i++; - const ASymbol* sym = pat->to<const ATuple*>()->head()->as<const ASymbol*>(); + const ASymbol* sym = pat->to_tuple()->head()->as_symbol(); const AType* patT = tup<AType>(Cursor(), const_cast<ASymbol*>(sym), 0); Value* typeV = llVal(resp_compile(cenv, patT)); @@ -465,7 +462,7 @@ LLVMEngine::compilePrimitive(CEnv& cenv, const ATuple* prim) bool isFloat = cenv.type(*++i)->str() == "Float"; Value* a = llVal(resp_compile(cenv, *i++)); Value* b = llVal(resp_compile(cenv, *i++)); - const string n = prim->head()->to<const ASymbol*>()->str(); + const string n = prim->head()->to_symbol()->str(); // Binary arithmetic operations Instruction::BinaryOps op = (Instruction::BinaryOps)0; |