@@ -70,7 +70,7 @@ class CopyCollector : private ValueVisitor<CopyCollector, heavy::Value> {
7070 heavy::EnvEntry VisitEnvEntry (heavy::EnvEntry const & EnvEntry) {
7171 heavy::Value Value = Visit (EnvEntry.Value );
7272 heavy::String* MangledName
73- = VisitString ( EnvEntry.MangledName );
73+ = cast<String>( Visit ( EnvEntry.MangledName ) );
7474 return heavy::EnvEntry{Value, MangledName};
7575 }
7676
@@ -96,7 +96,7 @@ class CopyCollector : private ValueVisitor<CopyCollector, heavy::Value> {
9696
9797 // ByteVector
9898 heavy::Value VisitByteVector (heavy::ByteVector* ByteVector) {
99- heavy::String* NewString = VisitString ( ByteVector->getString ());
99+ heavy::String* NewString = cast<String>( Visit ( ByteVector->getString () ));
100100 return new (NewHeap) heavy::ByteVector (NewString);
101101 }
102102
@@ -146,7 +146,6 @@ class CopyCollector : private ValueVisitor<CopyCollector, heavy::Value> {
146146 // ForwardRef
147147 heavy::Value VisitForwardRef (heavy::ForwardRef* ForwardRef) {
148148 // This is a wrapper for an already copied value.
149- // Unwrap it on the new heap.
150149 return ForwardRef->Val ;
151150 }
152151
@@ -246,6 +245,10 @@ class CopyCollector : private ValueVisitor<CopyCollector, heavy::Value> {
246245
247246 template <typename ...Args>
248247 heavy::Value Visit (heavy::Value OldVal) {
248+ // Handle the ValueSumTypes that alias ValueBase
249+ if (isa<Undefined>(OldVal))
250+ return VisitUndefined (cast<Undefined>(OldVal));
251+
249252 void * ValueBase = OldVal.get <ValueSumType::ValueBase>();
250253 if (ValueBase == nullptr )
251254 return OldVal;
@@ -260,6 +263,7 @@ class CopyCollector : private ValueVisitor<CopyCollector, heavy::Value> {
260263
261264 heavy::Value NewVal = Base::Visit (OldVal);
262265 // Overwrite the OldVal with a ForwardRef.
266+ assert (NewVal && " expecting valid value" );
263267 new (ValueBase) ForwardRef (NewVal);
264268
265269 return NewVal;
@@ -283,10 +287,11 @@ class CopyCollector : private ValueVisitor<CopyCollector, heavy::Value> {
283287
284288 // The module itself is not garbage collected,
285289 // but it has contained objects that are.
286- Module->Cleanup = cast_or_null<Lambda>(Visit (Module->Cleanup ));
290+ if (Module->Cleanup )
291+ Module->Cleanup = cast_or_null<Lambda>(Visit (Module->Cleanup ));
287292 for (auto & DensePair : Module->Map ) {
288- DensePair.getFirst () = VisitString ( DensePair.getFirst ());
289- DensePair.getSecond () = VisitString ( DensePair.getSecond ());
293+ DensePair.getFirst () = cast<String>( Visit ( DensePair.getFirst () ));
294+ DensePair.getSecond () = cast<String>( Visit ( DensePair.getSecond () ));
290295 }
291296 return Module;
292297 }
@@ -303,14 +308,14 @@ class CopyCollector : private ValueVisitor<CopyCollector, heavy::Value> {
303308 if (OpGen->TopLevelHandler )
304309 OpGen->TopLevelHandler = Visit (OpGen->TopLevelHandler );
305310 if (OpGen->LibraryEnvProc )
306- OpGen->LibraryEnvProc = VisitBinding ( OpGen->LibraryEnvProc );
311+ OpGen->LibraryEnvProc = cast<Binding>( Visit ( OpGen->LibraryEnvProc ) );
307312 // Note that the BindingTable has no unique ownership of the Bindings
308313 // as they are all pushed to the Environment.
309314 }
310315
311316 for (auto & DensePair : Env->EnvMap ) {
312- DensePair.getFirst () = VisitString ( DensePair.getFirst ());
313- DensePair.getSecond () = VisitString ( DensePair.getSecond ());
317+ DensePair.getFirst () = cast<String>( Visit ( DensePair.getFirst () ));
318+ DensePair.getSecond () = cast<String>( Visit ( DensePair.getSecond () ));
314319 }
315320
316321 VisitedSpecials.push_back (Env);
@@ -319,6 +324,14 @@ class CopyCollector : private ValueVisitor<CopyCollector, heavy::Value> {
319324};
320325
321326void Context::CollectGarbage () {
327+ // FIXME The continuation stack and escape procedures
328+ // are saved as opaque string objects.
329+ // Wrap these in some kind of "ContStack" value
330+ // and make a visitor for their captures.
331+ llvm::errs () << " NOT COLLECTING GARBAGE: " << getBytesAllocated () << " \n " ;
332+ MaxHint *= 2 ;
333+ return ;
334+
322335 // Create NewHeap
323336 Heap::AllocatorTy NewHeap;
324337 CopyCollector GC (NewHeap, this ->TrashHeap );
@@ -352,10 +365,16 @@ void Context::CollectGarbage() {
352365 ValAttr = LiteralOp.getInputAttr ();
353366 else if (auto MatchOp = dyn_cast<heavy::MatchOp>(Op))
354367 ValAttr = MatchOp.getValAttr ();
355- GC.VisitRootNode (ValAttr.getCachedValue ());
368+
369+ // Just clear the value and the attr will rebuild it
370+ // from its expression when and if it is requested.
371+ if (ValAttr)
372+ ValAttr.getCachedValue () = Value ();
356373 };
357374 ModuleOp->walk (WalkerFn);
375+
358376 }
377+ ReplaceHeap (std::move (NewHeap));
359378}
360379
361380String* IdTable::CreateIdTableEntry (llvm::StringRef Str) {
0 commit comments