diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/c.cpp | 36 | ||||
-rw-r--r-- | src/compile.cpp | 20 | ||||
-rw-r--r-- | src/llvm.cpp | 32 | ||||
-rw-r--r-- | src/repl.cpp | 8 | ||||
-rw-r--r-- | src/tuplr.hpp | 68 |
5 files changed, 82 insertions, 82 deletions
@@ -36,8 +36,8 @@ struct Function { string text; }; -static inline Value* llVal(CValue v) { return static_cast<Value*>(v); } -static inline Function* llFunc(CFunction f) { return static_cast<Function*>(f); } +static inline Value* llVal(CVal v) { return static_cast<Value*>(v); } +static inline Function* llFunc(CFunc f) { return static_cast<Function*>(f); } static const Type* llType(const AType* t) @@ -83,7 +83,7 @@ struct CEngine : public Engine { { } - CFunction startFunction(CEnv& cenv, + CFunc startFunction(CEnv& cenv, const std::string& name, const AType* retT, const ATuple& argsT, const vector<string> argNames) { @@ -112,35 +112,35 @@ struct CEngine : public Engine { return f; } - void finishFunction(CEnv& cenv, CFunction f, const AType* retT, CValue ret) { + void finishFunction(CEnv& cenv, CFunc f, const AType* retT, CVal ret) { out += "return " + *(Value*)ret + ";\n}\n\n"; } - void eraseFunction(CEnv& cenv, CFunction f) { + void eraseFunction(CEnv& cenv, CFunc f) { cenv.err << "C backend does not support JIT (eraseFunction)" << endl; } - CValue compileCall(CEnv& cenv, CFunction func, const vector<CValue>& args) { + CVal compileCall(CEnv& cenv, CFunc func, const vector<CVal>& args) { Value* varname = new string(cenv.penv.gensymstr("x")); Function* f = llFunc(func); out += (format("const %s %s = %s(") % f->returnType % *varname % f->name).str(); - FOREACH(vector<CValue>::const_iterator, i, args) + FOREACH(vector<CVal>::const_iterator, i, args) out += *llVal(*i); out += ");\n"; return varname; } - CFunction compileFunction(CEnv& cenv, AFn* fn, const AType& argsT); + CFunc compileFunction(CEnv& cenv, AFn* fn, const AType& argsT); - CValue compileLiteral(CEnv& cenv, AST* lit); - CValue compilePrimitive(CEnv& cenv, APrimitive* prim); - CValue compileIf(CEnv& cenv, AIf* aif); + CVal compileLiteral(CEnv& cenv, AST* lit); + CVal compilePrimitive(CEnv& cenv, APrimitive* prim); + CVal compileIf(CEnv& cenv, AIf* aif); void writeModule(CEnv& cenv, std::ostream& os) { os << out; } - const string call(CEnv& cenv, CFunction f, AType* retT) { + const string call(CEnv& cenv, CFunc f, AType* retT) { cenv.err << "C backend does not support JIT (call)" << endl; return ""; } @@ -158,13 +158,13 @@ tuplr_new_c_engine() * Code Generation * ***************************************************************************/ -CValue +CVal CEngine::compileLiteral(CEnv& cenv, AST* lit) { return new Value(lit->str()); } -CFunction +CFunc CEngine::compileFunction(CEnv& cenv, AFn* fn, const AType& argsT) { TEnv::GenericTypes::const_iterator gt = cenv.tenv.genericTypes.find(fn); @@ -184,7 +184,7 @@ CEngine::compileFunction(CEnv& cenv, AFn* fn, const AType& argsT) string("call has non-concrete type %1%\n") + thisType->str()); Object::pool.addRoot(thisType); - CFunction f = fn->impls.find(thisType); + CFunc f = fn->impls.find(thisType); if (f) return f; @@ -220,7 +220,7 @@ CEngine::compileFunction(CEnv& cenv, AFn* fn, const AType& argsT) // Define value first for recursion cenv.precompile(fn, f); fn->impls.push_back(make_pair(thisType, f)); - CValue retVal = NULL; + CVal retVal = NULL; for (size_t i = 2; i < fn->size(); ++i) retVal = cenv.compile(fn->at(i)); cenv.engine()->finishFunction(cenv, f, cenv.type(fn->at(fn->size() - 1)), retVal); @@ -233,7 +233,7 @@ CEngine::compileFunction(CEnv& cenv, AFn* fn, const AType& argsT) return f; } -CValue +CVal CEngine::compileIf(CEnv& cenv, AIf* aif) { CEngine* engine = reinterpret_cast<CEngine*>(cenv.engine()); @@ -261,7 +261,7 @@ CEngine::compileIf(CEnv& cenv, AIf* aif) return varname; } -CValue +CVal CEngine::compilePrimitive(CEnv& cenv, APrimitive* prim) { CEngine* engine = reinterpret_cast<CEngine*>(cenv.engine()); diff --git a/src/compile.cpp b/src/compile.cpp index 5b6dacd..2deb713 100644 --- a/src/compile.cpp +++ b/src/compile.cpp @@ -24,20 +24,20 @@ using namespace std; #define COMPILE_LITERAL(CT) \ -template<> CValue ALiteral<CT>::compile(CEnv& cenv) { \ +template<> CVal ALiteral<CT>::compile(CEnv& cenv) { \ return cenv.engine()->compileLiteral(cenv, this); \ } COMPILE_LITERAL(int32_t); COMPILE_LITERAL(float); COMPILE_LITERAL(bool); -CValue +CVal ASymbol::compile(CEnv& cenv) { return cenv.vals.ref(this); } -CValue +CVal AFn::compile(CEnv& cenv) { AType* aFnT = cenv.type(this); @@ -58,7 +58,7 @@ AFn::compile(CEnv& cenv) return tupPtr;*/ } -CValue +CVal ACall::compile(CEnv& cenv) { AFn* c = cenv.tenv.resolve(at(0))->to<AFn*>(); @@ -76,33 +76,33 @@ ACall::compile(CEnv& cenv) fnT.push_back(&protT); fnT.push_back(cenv.type(this)); - CFunction f = c->impls.find(&fnT); + CFunc f = c->impls.find(&fnT); THROW_IF(!f, loc, (format("callee failed to compile for type %1%") % fnT.str()).str()); - vector<CValue> args(size() - 1); + vector<CVal> args(size() - 1); for (size_t i = 0; i < args.size(); ++i) args[i] = cenv.compile(at(i + 1)); return cenv.engine()->compileCall(cenv, f, args); } -CValue +CVal ADef::compile(CEnv& cenv) { // Define stub first for recursion cenv.def(sym(), at(2), cenv.type(at(2)), NULL); - CValue val = cenv.compile(at(size() - 1)); + CVal val = cenv.compile(at(size() - 1)); cenv.vals.def(sym(), val); return val; } -CValue +CVal AIf::compile(CEnv& cenv) { return cenv.engine()->compileIf(cenv, this); } -CValue +CVal APrimitive::compile(CEnv& cenv) { return cenv.engine()->compilePrimitive(cenv, this); diff --git a/src/llvm.cpp b/src/llvm.cpp index c798d24..d6d29ce 100644 --- a/src/llvm.cpp +++ b/src/llvm.cpp @@ -39,8 +39,8 @@ using namespace llvm; using namespace std; using boost::format; -static inline Value* llVal(CValue v) { return static_cast<Value*>(v); } -static inline Function* llFunc(CFunction f) { return static_cast<Function*>(f); } +static inline Value* llVal(CVal v) { return static_cast<Value*>(v); } +static inline Function* llFunc(CFunc f) { return static_cast<Function*>(f); } static const Type* llType(const AType* t) @@ -100,7 +100,7 @@ struct LLVMEngine : public Engine { "tuplr_gc_allocate", module); } - CFunction startFunction(CEnv& cenv, + CFunc startFunction(CEnv& cenv, const std::string& name, const AType* retT, const ATuple& argsT, const vector<string> argNames) { @@ -133,7 +133,7 @@ struct LLVMEngine : public Engine { return f; } - void finishFunction(CEnv& cenv, CFunction f, const AType* retT, CValue ret) { + void finishFunction(CEnv& cenv, CFunc f, const AType* retT, CVal ret) { if (retT->concrete()) builder.CreateRet(llVal(ret)); else @@ -144,28 +144,28 @@ struct LLVMEngine : public Engine { opt.run(*static_cast<Function*>(f)); } - void eraseFunction(CEnv& cenv, CFunction f) { + void eraseFunction(CEnv& cenv, CFunc f) { if (f) llFunc(f)->eraseFromParent(); } - CValue compileCall(CEnv& cenv, CFunction f, const vector<CValue>& args) { + CVal compileCall(CEnv& cenv, CFunc f, const vector<CVal>& args) { const vector<Value*>& llArgs = *reinterpret_cast<const vector<Value*>*>(&args); return builder.CreateCall(llFunc(f), llArgs.begin(), llArgs.end()); } - CFunction compileFunction(CEnv& cenv, AFn* fn, const AType& argsT); + CFunc compileFunction(CEnv& cenv, AFn* fn, const AType& argsT); - CValue compileLiteral(CEnv& cenv, AST* lit); - CValue compilePrimitive(CEnv& cenv, APrimitive* prim); - CValue compileIf(CEnv& cenv, AIf* aif); + CVal compileLiteral(CEnv& cenv, AST* lit); + CVal compilePrimitive(CEnv& cenv, APrimitive* prim); + CVal compileIf(CEnv& cenv, AIf* aif); void writeModule(CEnv& cenv, std::ostream& os) { AssemblyAnnotationWriter writer; module->print(os, &writer); } - const string call(CEnv& cenv, CFunction f, AType* retT) { + const string call(CEnv& cenv, CFunc f, AType* retT) { void* fp = engine->getPointerToFunction(llFunc(f)); const Type* t = llType(retT); THROW_IF(!fp, Cursor(), "unable to get function pointer"); @@ -201,7 +201,7 @@ tuplr_new_llvm_engine() * Code Generation * ***************************************************************************/ -CValue +CVal LLVMEngine::compileLiteral(CEnv& cenv, AST* lit) { ALiteral<int32_t>* ilit = dynamic_cast<ALiteral<int32_t>*>(lit); @@ -219,7 +219,7 @@ LLVMEngine::compileLiteral(CEnv& cenv, AST* lit) throw Error(lit->loc, "Unknown literal type"); } -CFunction +CFunc LLVMEngine::compileFunction(CEnv& cenv, AFn* fn, const AType& argsT) { TEnv::GenericTypes::const_iterator gt = cenv.tenv.genericTypes.find(fn); @@ -317,7 +317,7 @@ LLVMEngine::compileFunction(CEnv& cenv, AFn* fn, const AType& argsT) // Define value first for recursion cenv.precompile(fn, f); fn->impls.push_back(make_pair(thisType, f)); - CValue retVal = NULL; + CVal retVal = NULL; for (size_t i = 2; i < fn->size(); ++i) retVal = cenv.compile(fn->at(i)); cenv.engine()->finishFunction(cenv, f, cenv.type(fn->at(fn->size() - 1)), retVal); @@ -331,7 +331,7 @@ LLVMEngine::compileFunction(CEnv& cenv, AFn* fn, const AType& argsT) return f; } -CValue +CVal LLVMEngine::compileIf(CEnv& cenv, AIf* aif) { typedef vector< pair<Value*, BasicBlock*> > Branches; @@ -376,7 +376,7 @@ LLVMEngine::compileIf(CEnv& cenv, AIf* aif) return pn; } -CValue +CVal LLVMEngine::compilePrimitive(CEnv& cenv, APrimitive* prim) { LLVMEngine* engine = reinterpret_cast<LLVMEngine*>(cenv.engine()); diff --git a/src/repl.cpp b/src/repl.cpp index 5c4d459..fa08238 100644 --- a/src/repl.cpp +++ b/src/repl.cpp @@ -58,7 +58,7 @@ readParseTypeCompile(CEnv& cenv, Cursor& cursor, istream& is, AST** exp, AST** r } static void -callPrintCollect(CEnv& cenv, CFunction f, AST* result, AType* resultT, bool execute) +callPrintCollect(CEnv& cenv, CFunc f, AST* result, AType* resultT, bool execute) { if (execute && resultT->concrete()) cenv.out << cenv.engine()->call(cenv, f, resultT); @@ -89,8 +89,8 @@ eval(CEnv& cenv, const string& name, istream& is, bool execute) //for (list< pair<SExp, AST*> >::const_iterator i = exprs.begin(); i != exprs.end(); ++i) // pprint(cout, i->second->cps(cenv.tenv, cenv.penv.sym("cont"))); - CValue val = NULL; - CFunction f = NULL; + CVal val = NULL; + CFunc f = NULL; if (resultT->concrete()) { // Create function for top-level of program f = cenv.engine()->startFunction(cenv, "main", resultT, ATuple(cursor)); @@ -131,7 +131,7 @@ repl(CEnv& cenv) if (!readParseTypeCompile(cenv, cursor, std::cin, &exp, &result, &resultT)) break; - CFunction f = NULL; + CFunc f = NULL; try { // Create function for this repl loop f = cenv.engine()->startFunction(cenv, replFnName, resultT, ATuple(cursor)); diff --git a/src/tuplr.hpp b/src/tuplr.hpp index 2814578..7295c5e 100644 --- a/src/tuplr.hpp +++ b/src/tuplr.hpp @@ -120,8 +120,8 @@ AST* readExpression(Cursor& cur, std::istream& in); * Backend Types * ***************************************************************************/ -typedef void* CValue; ///< Compiled value (opaque) -typedef void* CFunction; ///< Compiled function (opaque) +typedef void* CVal; ///< Compiled value (opaque) +typedef void* CFunc; ///< Compiled function (opaque) /*************************************************************************** @@ -193,7 +193,7 @@ struct AST : public Object { virtual void constrain(TEnv& tenv, Constraints& c) const {} virtual AST* cps(TEnv& tenv, AST* cont); virtual void lift(CEnv& cenv) {} - virtual CValue compile(CEnv& cenv) = 0; + virtual CVal compile(CEnv& cenv) = 0; string str() const { ostringstream ss; ss << this; return ss.str(); } template<typename T> T to() { return dynamic_cast<T>(this); } template<typename T> T const to() const { return dynamic_cast<T const>(this); } @@ -227,7 +227,7 @@ struct ALiteral : public AST { return (r && (val == r->val)); } void constrain(TEnv& tenv, Constraints& c) const; - CValue compile(CEnv& cenv); + CVal compile(CEnv& cenv); const T val; }; @@ -236,14 +236,14 @@ struct AString : public AST, public std::string { AString(Cursor c, const string& s) : AST(c), std::string(s) {} bool operator==(const AST& rhs) const { return this == &rhs; } void constrain(TEnv& tenv, Constraints& c) const; - CValue compile(CEnv& cenv) { return NULL; } + CVal compile(CEnv& cenv) { return NULL; } }; /// Symbol, e.g. "a" struct ASymbol : public AST { bool operator==(const AST& rhs) const { return this == &rhs; } void constrain(TEnv& tenv, Constraints& c) const; - CValue compile(CEnv& cenv); + CVal compile(CEnv& cenv); mutable LAddr addr; const string cppstr; private: @@ -302,7 +302,7 @@ struct ATuple : public AST { void constrain(TEnv& tenv, Constraints& c) const; void lift(CEnv& cenv) { FOREACH(iterator, t, *this) (*t)->lift(cenv); } - CValue compile(CEnv& cenv) { throw Error(loc, "tuple compiled"); } + CVal compile(CEnv& cenv) { throw Error(loc, "tuple compiled"); } private: size_t _len; @@ -315,7 +315,7 @@ struct AType : public ATuple { AType(Cursor c, unsigned i) : ATuple(c), kind(VAR), id(i) {} AType(Cursor c) : ATuple(c), kind(EXPR), id(0) {} AType(Cursor c, AST* ast, va_list args) : ATuple(c, ast, args), kind(EXPR), id(0) {} - CValue compile(CEnv& cenv) { return NULL; } + CVal compile(CEnv& cenv) { return NULL; } bool concrete() const { switch (kind) { case VAR: return false; @@ -392,12 +392,12 @@ struct AFn : public ATuple { void constrain(TEnv& tenv, Constraints& c) const; AST* cps(TEnv& tenv, AST* cont); void lift(CEnv& cenv); - CValue compile(CEnv& cenv); + CVal compile(CEnv& cenv); const ATuple* prot() const { return at(1)->to<const ATuple*>(); } ATuple* prot() { return at(1)->to<ATuple*>(); } /// System level implementations of this (polymorphic) fn - struct Impls : public list< pair<AType*, CFunction> > { - CFunction find(AType* type) const { + struct Impls : public list< pair<AType*, CFunc> > { + CFunc find(AType* type) const { for (const_iterator f = begin(); f != end(); ++f) if (*f->first == *type) return f->second; @@ -416,7 +416,7 @@ struct ACall : public ATuple { void constrain(TEnv& tenv, Constraints& c) const; AST* cps(TEnv& tenv, AST* cont); void lift(CEnv& cenv); - CValue compile(CEnv& cenv); + CVal compile(CEnv& cenv); }; /// Definition special form, e.g. "(def x 2)" @@ -435,7 +435,7 @@ struct ADef : public ACall { void constrain(TEnv& tenv, Constraints& c) const; AST* cps(TEnv& tenv, AST* cont); void lift(CEnv& cenv); - CValue compile(CEnv& cenv); + CVal compile(CEnv& cenv); }; /// Conditional special form, e.g. "(if cond thenexp elseexp)" @@ -444,7 +444,7 @@ struct AIf : public ACall { AIf(Cursor c, AST* ast, va_list args) : ACall(c, ast, args) {} void constrain(TEnv& tenv, Constraints& c) const; AST* cps(TEnv& tenv, AST* cont); - CValue compile(CEnv& cenv); + CVal compile(CEnv& cenv); }; /// Primitive (builtin arithmetic function), e.g. "(+ 2 3)" @@ -459,7 +459,7 @@ struct APrimitive : public ACall { } void constrain(TEnv& tenv, Constraints& c) const; AST* cps(TEnv& tenv, AST* cont); - CValue compile(CEnv& cenv); + CVal compile(CEnv& cenv); }; @@ -620,23 +620,23 @@ Subst unify(const Constraints& c); /// Compiler backend struct Engine { - virtual CFunction startFunction( + virtual CFunc startFunction( CEnv& cenv, const std::string& name, const AType* retT, const ATuple& argsT, const vector<string> argNames=vector<string>()) = 0; - virtual void finishFunction(CEnv& cenv, CFunction f, const AType* retT, CValue ret) = 0; - virtual void eraseFunction(CEnv& cenv, CFunction f) = 0; - virtual CFunction compileFunction(CEnv& cenv, AFn* fn, const AType& argsT) = 0; - virtual CValue compileLiteral(CEnv& cenv, AST* lit) = 0; - virtual CValue compileCall(CEnv& cenv, CFunction f, const vector<CValue>& args) = 0; - virtual CValue compilePrimitive(CEnv& cenv, APrimitive* prim) = 0; - virtual CValue compileIf(CEnv& cenv, AIf* aif) = 0; - virtual void writeModule(CEnv& cenv, std::ostream& os) = 0; + virtual void finishFunction(CEnv& cenv, CFunc f, const AType* retT, CVal ret) = 0; + virtual void eraseFunction(CEnv& cenv, CFunc f) = 0; + virtual CFunc compileFunction(CEnv& cenv, AFn* fn, const AType& argsT) = 0; + virtual CVal compileLiteral(CEnv& cenv, AST* lit) = 0; + virtual CVal compileCall(CEnv& cenv, CFunc f, const vector<CVal>& args) = 0; + virtual CVal compilePrimitive(CEnv& cenv, APrimitive* prim) = 0; + virtual CVal compileIf(CEnv& cenv, AIf* aif) = 0; + virtual void writeModule(CEnv& cenv, std::ostream& os) = 0; - virtual const string call(CEnv& cenv, CFunction f, AType* retT) = 0; + virtual const string call(CEnv& cenv, CFunc f, AType* retT) = 0; }; Engine* tuplr_new_llvm_engine(); @@ -650,14 +650,14 @@ struct CEnv { ~CEnv() { Object::pool.collect(GC::Roots()); } - typedef Env<const AST*, CValue> Vals; + typedef Env<const AST*, CVal> Vals; Engine* engine() { return _engine; } void push() { tenv.push(); vals.push(); } void pop() { tenv.pop(); vals.pop(); } - void precompile(AST* obj, CValue value) { vals.def(obj, value); } - CValue compile(AST* obj) { - CValue* v = vals.ref(obj); + void precompile(AST* obj, CVal value) { vals.def(obj, value); } + CVal compile(AST* obj) { + CVal* v = vals.ref(obj); return (v && *v) ? *v : vals.def(obj, obj->compile(*this)); } void lock(AST* ast) { Object::pool.addRoot(ast); Object::pool.addRoot(type(ast)); } @@ -667,7 +667,7 @@ struct CEnv { return sym->addr ? tenv.deref(sym->addr).second : NULL; return tsubst.apply(subst.apply(tenv.vars[ast]))->to<AType*>(); } - void def(const ASymbol* sym, AST* c, AType* t, CValue v) { + void def(const ASymbol* sym, AST* c, AType* t, CVal v) { tenv.def(sym, make_pair(c, t)); vals.def(sym, v); } @@ -691,9 +691,9 @@ private: * EVAL/REPL/MAIN * ***************************************************************************/ -void pprint(std::ostream& out, const AST* ast); -void initLang(PEnv& penv, TEnv& tenv); -int eval(CEnv& cenv, const string& name, istream& is, bool execute); -int repl(CEnv& cenv); +void pprint(std::ostream& out, const AST* ast); +void initLang(PEnv& penv, TEnv& tenv); +int eval(CEnv& cenv, const string& name, istream& is, bool execute); +int repl(CEnv& cenv); #endif // TUPLR_HPP |