diff options
-rw-r--r-- | gc.cpp | 14 | ||||
-rw-r--r-- | llvm.cpp | 8 | ||||
-rw-r--r-- | tuplr.cpp | 4 | ||||
-rw-r--r-- | tuplr.hpp | 35 | ||||
-rw-r--r-- | typing.cpp | 2 |
5 files changed, 33 insertions, 30 deletions
@@ -25,18 +25,18 @@ void* GC::alloc(size_t size) { void* ret = malloc(size); - _heap.push_back((AST*)ret); + _heap.push_back((Object*)ret); return ret; } inline void -mark(CEnv& cenv, const AST* ast) +mark(CEnv& cenv, const Object* obj) { - if (!ast || ast->used) + if (!obj || obj->used) return; - ast->used = true; - const ATuple* tup = ast->to<const ATuple*>(); + obj->used = true; + const ATuple* tup = dynamic_cast<const ATuple*>(obj); if (tup) { FOREACH(ATuple::const_iterator, i, *tup) { mark(cenv, *i); @@ -57,10 +57,10 @@ GC::collect(CEnv& cenv, const Roots& roots) if ((*i)->used) { (*i)->used = false; } else { - AType* t = (*i)->to<AType*>(); + AType* t = dynamic_cast<AType*>(*i); // Don't delete types that are keys in the current type substitution if (!t || cenv.tsubst.find(t) == cenv.tsubst.end()) { - (*i)->~AST(); + (*i)->~Object(); free(*i); _heap.erase(i); } @@ -226,7 +226,7 @@ AClosure::liftCall(CEnv& cenv, const vector<AType*>& argsT) throw Error(loc, "unable to resolve concrete type for function"); } - AST::pool.addRoot(thisType); + Object::pool.addRoot(thisType); if (funcs.find(thisType)) return; @@ -580,7 +580,7 @@ eval(CEnv& cenv, const string& name, istream& is) cenv.out << call(resultType, llengine(cenv)->engine->getPointerToFunction(f)) << " : " << resultType << endl; - AST::pool.collect(cenv, AST::pool.roots()); + Object::pool.collect(cenv, Object::pool.roots()); } catch (Error& e) { cenv.err << e.what() << endl; @@ -635,7 +635,7 @@ repl(CEnv& cenv) if (body->to<ADefinition*>()) cenv.lock(body); - AST::pool.collect(cenv, AST::pool.roots()); + Object::pool.collect(cenv, Object::pool.roots()); cenv.tsubst = oldSubst; @@ -663,7 +663,7 @@ newCenv(PEnv& penv, TEnv& tenv) void freeCenv(CEnv* cenv) { - AST::pool.collect(*cenv, GC::Roots()); + Object::pool.collect(*cenv, GC::Roots()); delete (LLVMEngine*)cenv->engine(); delete cenv; } @@ -27,7 +27,7 @@ using namespace std; using boost::format; Funcs AConsCall::funcs; -GC AST::pool; +GC Object::pool; template<typename Atom> ostream& @@ -258,7 +258,7 @@ main(int argc, char** argv) CEnv* cenv = newCenv(penv, tenv); cenv->push(); - AST::pool.lock(); + Object::pool.lock(); map<string,string> args; list<string> files; @@ -138,16 +138,15 @@ typedef void* CEngine; ///< Compiler Engine (opaque) * Garbage Collector * ***************************************************************************/ -struct AST; ///< Abstract Syntax Tree node -struct CEnv; ///< Compile-Time Environment +struct Object; ///< Object (AST nodes and runtime data) +struct CEnv; ///< Compile-Time Environment -extern ostream& operator<<(ostream& out, const AST* ast); struct GC { - typedef std::list<const AST*> Roots; - typedef std::list<AST*> Heap; + typedef std::list<const Object*> Roots; + typedef std::list<Object*> Heap; void* alloc(size_t size); void collect(CEnv& cenv, const Roots& roots); - const AST* addRoot(const AST* ast) { if (ast) { cout << "ADD ROOT " << ast << endl; _roots.push_back(ast); } return ast; } + const Object* addRoot(const Object* obj) { if (obj) _roots.push_back(obj); return obj; } void lock() { _roots.insert(_roots.end(), _heap.begin(), _heap.end()); } const Roots& roots() const { return _roots; } private: @@ -155,6 +154,16 @@ private: Roots _roots; }; +struct Object { + Object() : used(false) {} + virtual ~Object() {} + + mutable bool used; + + static void* operator new(size_t size) { return pool.alloc(size); } + static void operator delete(void* ptr) {} + static GC pool; +}; /*************************************************************************** * Abstract Syntax Tree * @@ -164,13 +173,13 @@ struct Constraint; ///< Type Constraint struct TEnv; ///< Type-Time Environment struct Constraints; struct Subst; +struct AST; extern ostream& operator<<(ostream& out, const AST* ast); /// Base class for all AST nodes -struct AST { - AST(Cursor c=Cursor()) : loc(c), used(false) {} - virtual ~AST() {} +struct AST : public Object { + AST(Cursor c=Cursor()) : loc(c) {} virtual bool operator==(const AST& o) const = 0; virtual bool contains(const AST* child) const { return false; } virtual void constrain(TEnv& tenv, Constraints& c) const {} @@ -185,12 +194,6 @@ struct AST { return t; } Cursor loc; - mutable bool used; - - static void* operator new(size_t size) { return pool.alloc(size); } - static void operator delete(void* ptr) {} - - static GC pool; }; /// Literal value @@ -582,7 +585,7 @@ struct CEnv { CValue compile(AST* obj); void optimise(CFunction f); void write(std::ostream& os); - void lock(AST* ast) { AST::pool.addRoot(ast); AST::pool.addRoot(type(ast)); } + void lock(AST* ast) { Object::pool.addRoot(ast); Object::pool.addRoot(type(ast)); } AType* type(AST* ast, const Subst& subst = Subst()) const { ASymbol* sym = ast->to<ASymbol*>(); if (sym) @@ -108,7 +108,7 @@ AClosure::constrain(TEnv& tenv, Constraints& c) const genericType = new AType(loc, tenv.penv.sym("Fn"), tsubst.apply(protT), tsubst.apply(bodyT), 0); tenv.genericTypes.insert(make_pair(this, genericType)); - AST::pool.addRoot(genericType); + Object::pool.addRoot(genericType); tenv.pop(); subst = tsubst; |