[pure-lang-svn] SF.net SVN: pure-lang:[452] pure/trunk
Status: Beta
Brought to you by:
agraef
From: <ag...@us...> - 2008-08-07 08:07:14
|
Revision: 452 http://pure-lang.svn.sourceforge.net/pure-lang/?rev=452&view=rev Author: agraef Date: 2008-08-07 08:07:20 +0000 (Thu, 07 Aug 2008) Log Message: ----------- Allocate environments in FMap dynamically. Modified Paths: -------------- pure/trunk/interpreter.cc pure/trunk/interpreter.hh pure/trunk/lexer.ll Modified: pure/trunk/interpreter.cc =================================================================== --- pure/trunk/interpreter.cc 2008-08-05 05:45:03 UTC (rev 451) +++ pure/trunk/interpreter.cc 2008-08-07 08:07:20 UTC (rev 452) @@ -1929,6 +1929,28 @@ return os << ")"; } +FMap& FMap::operator= (const FMap& f) +{ + clear(); m.resize(f.m.size()); + for (size_t i = 0, n = f.m.size(); i < n; i++) m[i] = new EnvMap(*f.m[i]); + idx = f.idx; return *this; +} + +void FMap::clear() +{ + set<Env*> e; + for (size_t i = 0, n = m.size(); i < n; i++) { + for (EnvMap::iterator it = m[i]->begin(), end = m[i]->end(); + it != end; it++) + e.insert(it->second); + delete m[i]; + } + for (set<Env*>::iterator it = e.begin(), end = e.end(); + it != end; it++) + delete *it; + m.clear(); idx = 0; +} + Env& Env::operator= (const Env& e) { if (f) { @@ -2128,7 +2150,7 @@ fenv = *ei++; } assert(fenv->act_fmap().find(x.vtag()) != fenv->act_fmap().end()); - fenv = &fenv->fmap.act()[x.vtag()]; + fenv = fenv->fmap.act()[x.vtag()]; if (!fenv->local) break; // fenv now points to the environment of the (local) function assert(fenv != this && fenv->tag == x.vtag()); @@ -2188,7 +2210,8 @@ if (n == 2 && f.tag() == interp.symtab.catch_sym().f) { expr h = x.xval1().xval2(), y = x.xval2(); push("catch"); - Env& e = fmap.act()[-x.hash()] = Env(0, 0, y, true, true); + Env* eptr = fmap.act()[-x.hash()] = new Env(0, 0, y, true, true); + Env& e = *eptr; e.build_map(y); e.promote_map(); pop(); build_map(h); @@ -2205,14 +2228,16 @@ break; case EXPR::LAMBDA: { push("lambda"); - Env& e = fmap.act()[-x.hash()] = Env(0, 1, x.xval2(), true, true); + Env* eptr = fmap.act()[-x.hash()] = new Env(0, 1, x.xval2(), true, true); + Env& e = *eptr; e.build_map(x.xval2()); e.promote_map(); pop(); break; } case EXPR::CASE: { push("case"); - Env& e = fmap.act()[-x.hash()] = Env(0, 1, x.xval(), true, true); + Env* eptr = fmap.act()[-x.hash()] = new Env(0, 1, x.xval(), true, true); + Env& e = *eptr; e.build_map(*x.rules()); e.promote_map(); pop(); build_map(x.xval()); @@ -2229,13 +2254,13 @@ for (env::const_iterator p = fe->begin(); p != fe->end(); p++) { int32_t ftag = p->first; const env_info& info = p->second; - fmap.act()[ftag] = Env(ftag, info, false, true); + fmap.act()[ftag] = new Env(ftag, info, false, true); } // Now recursively build the maps for the child environments. for (env::const_iterator p = fe->begin(); p != fe->end(); p++) { int32_t ftag = p->first; const env_info& info = p->second; - Env& e = fmap.act()[ftag]; + Env& e = *fmap.act()[ftag]; e.build_map(info); e.promote_map(); } pop(); @@ -2259,7 +2284,8 @@ rulel::const_iterator s = r; expr y = (++s == end)?x:s->rhs; push("when"); - Env& e = fmap.act()[-y.hash()] = Env(0, 1, y, true, true); + Env* eptr = fmap.act()[-y.hash()] = new Env(0, 1, y, true, true); + Env& e = *eptr; e.build_map(x, s, end); e.promote_map(); pop(); build_map(r->rhs); @@ -3117,7 +3143,7 @@ rulel::const_iterator s = r; expr y = (++s == end)?x:s->rhs; assert(act.fmap.act().find(-y.hash()) != act.fmap.act().end()); - Env& e = act.fmap.act()[-y.hash()]; + Env& e = *act.fmap.act()[-y.hash()]; push("when", &e); fun_prolog("anonymous"); BasicBlock *bodybb = BasicBlock::Create("body"); @@ -3544,7 +3570,7 @@ int offs = idx-1; if (idx == 0) { // function in current environment ('with'-bound) - f = &act_env().fmap.act()[tag]; + f = act_env().fmap.act()[tag]; } else { // function in an outer environment, the de Bruijn index idx tells us // where on the current environment stack it's at @@ -3552,7 +3578,7 @@ size_t i = idx; for (; i > 0; e++, i--) assert(e != envstk.end()); // look up the function in the environment - f = &(*e)->fmap.act()[tag]; + f = (*e)->fmap.act()[tag]; } if (f->n == n) { // bingo! saturated call @@ -3691,7 +3717,7 @@ expr h = x.xval1().xval2(), y = x.xval2(); Env& act = act_env(); assert(act.fmap.act().find(-x.hash()) != act.fmap.act().end()); - Env& e = act.fmap.act()[-x.hash()]; + Env& e = *act.fmap.act()[-x.hash()]; push("catch", &e); fun_prolog("anonymous"); e.CreateRet(codegen(y)); @@ -3732,7 +3758,7 @@ case EXPR::LAMBDA: { Env& act = act_env(); assert(act.fmap.act().find(-x.hash()) != act.fmap.act().end()); - Env& e = act.fmap.act()[-x.hash()]; + Env& e = *act.fmap.act()[-x.hash()]; push("lambda", &e); fun("anonymous", x.pm(), true); pop(&e); @@ -3744,7 +3770,7 @@ // above) which gets applied to the subject term to be matched Env& act = act_env(); assert(act.fmap.act().find(-x.hash()) != act.fmap.act().end()); - Env& e = act.fmap.act()[-x.hash()]; + Env& e = *act.fmap.act()[-x.hash()]; push("case", &e); fun("anonymous", x.pm(), true); pop(&e); @@ -3771,15 +3797,15 @@ for (p = fe->begin(); p != fe->end(); p++) { int32_t ftag = p->first; assert(act.fmap.act().find(ftag) != act.fmap.act().end()); - Env& e = act.fmap.act()[ftag]; + Env& e = *act.fmap.act()[ftag]; push("with", &e); - act.fmap.act()[ftag].f = fun_prolog(symtab.sym(ftag).s); + act.fmap.act()[ftag]->f = fun_prolog(symtab.sym(ftag).s); pop(&e); } for (p = fe->begin(); p != fe->end(); p++) { int32_t ftag = p->first; const env_info& info = p->second; - Env& e = act.fmap.act()[ftag]; + Env& e = *act.fmap.act()[ftag]; push("with", &e); fun_body(info.m); pop(&e); @@ -4074,7 +4100,7 @@ assert(!envstk.empty()); if (idx == 0) { // function in current environment ('with'-bound) - Env& f = act_env().fmap.act()[tag]; + Env& f = *act_env().fmap.act()[tag]; return fbox(f, thunked); } // If we come here, the function is defined in an outer environment. Locate @@ -4084,7 +4110,7 @@ size_t i = idx; for (; i > 0; e++, i--) assert(e != envstk.end()); // look up the function in the environment - Env& f = (*e)->fmap.act()[tag]; + Env& f = *(*e)->fmap.act()[tag]; assert(f.f); // Now create the closure. This is essentially just like fbox(), but we are // called inside a nested environment here, and hence the de Bruijn indices @@ -5058,7 +5084,7 @@ while (r != rl.end()) { const rule& rr = rules[*r]; reduced.insert(*r); - f.fmap.set(*r); + f.fmap.select(*r); f.f->getBasicBlockList().push_back(rulebb); f.builder.SetInsertPoint(rulebb); #if DEBUG>1 Modified: pure/trunk/interpreter.hh =================================================================== --- pure/trunk/interpreter.hh 2008-08-05 05:45:03 UTC (rev 451) +++ pure/trunk/interpreter.hh 2008-08-07 08:07:20 UTC (rev 452) @@ -85,7 +85,7 @@ #define Builder llvm::IRBuilder typedef list<Env*> EnvStack; -typedef map<int32_t,Env> EnvMap; +typedef map<int32_t,Env*> EnvMap; typedef pair<int32_t,uint8_t> xmap_key; struct FMap { @@ -95,20 +95,15 @@ size_t idx; // constructor (create one empty map by default) FMap() : m(1), idx(0) { m[0] = new EnvMap; } - // clear local environments - void clear() - { for (size_t i = 0, n = m.size(); i < n; i++) delete m[i]; - m.clear(); idx = 0; } // assignment - FMap& operator= (const FMap& f) - { clear(); m.resize(f.m.size()); - for (size_t i = 0, n = f.m.size(); i < n; i++) m[i] = new EnvMap(*f.m[i]); - idx = f.idx; return *this; } + FMap& operator= (const FMap& f); + // clear local environments + void clear(); // set index to first, next and given map void first() { idx = 0; } void next() { if (++idx >= m.size()) { m.resize(idx+1); m[idx] = new EnvMap; } } - void set(size_t n) { if (m.size() > 1) idx = n; } + void select(size_t n) { if (m.size() > 1) idx = n; } // access the current map EnvMap& act() { return *m[idx]; } }; Modified: pure/trunk/lexer.ll =================================================================== --- pure/trunk/lexer.ll 2008-08-05 05:45:03 UTC (rev 451) +++ pure/trunk/lexer.ll 2008-08-07 08:07:20 UTC (rev 452) @@ -941,9 +941,13 @@ if (!f) return; // not used, probably a shadowed rule if (h && h != f) h->print(os); f->print(os); + set<Env*> e; for (size_t i = 0, n = fmap.m.size(); i < n; i++) { for (EnvMap::const_iterator it = fmap.m[i]->begin(), end = fmap.m[i]->end(); it != end; it++) - it->second.print(os); + if (e.find(it->second) == e.end()) { + it->second->print(os); + e.insert(it->second); + } } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |