Commits

David Lin  committed d360d7c

Resource Deallocation

  • Participants
  • Parent commits 2071497

Comments (0)

Files changed (3)

File lab_4/b98902077/compiler/Context.cpp

     m_VarTableList.push_back( vtable );
     
     // Save Stack
-    //llvm::Value* sp = m_Builder.CreateCall(m_StackSave, "___scope");
-    //(*vtable)["$stack_pointer"] = VariableAttr(sp);
+    llvm::Value* sp = m_Builder.CreateCall(m_StackSave, "___scope");
+    (*vtable)["$stack_pointer"] = VariableAttr(sp);
   }
   
   void Context::_codegen_scope_end()
     //
     VarTable* vtable = m_VarTableList.back();
     
-    // Free some memory
-    for (VarTable::iterator i = vtable->begin(), e = vtable->end(); 
-         i != e; ++i)
-    {
-      if (i->getValue().need_free) // The type is need to be freed
-      {
-        llvm::Value* localval = i->getValue().address;
-        
-        // Load from stack
-        llvm::Value* addr = m_Builder.CreateLoad(localval);
-        
-        // Cast addr to i8*
-        llvm::Value* i8addr = m_Builder.CreateBitCast(addr, m_Int8PtrTy);
-        
-        // Call Free
-        m_Builder.CreateCall(m_Free, i8addr);
-      }
-    }
-    
-    // resetore stack
+    // release resources
     _codegen_scope_interrupt(1);
     
     //
          i = m_VarTableList.rbegin(), e = m_VarTableList.rend();
          i != e && n > 0; ++i, --n)
     {
+      VarTable* vtable = *i;
+      
+      // Free memory
+      for (VarTable::iterator i2 = vtable->begin(), e2 = vtable->end(); 
+           i2 != e2; ++i2)
+      {
+        if (i2->getValue().need_free) // The type is need to be freed
+        {
+          llvm::Value* localval = i2->getValue().address;
+          
+          // Load from stack
+          llvm::Value* addr = m_Builder.CreateLoad(localval);
+          
+          // Cast addr to i8*
+          llvm::Value* i8addr = m_Builder.CreateBitCast(addr, m_Int8PtrTy);
+          
+          // Call Free
+          m_Builder.CreateCall(m_Free, i8addr);
+        }
+      }
+      
       // Restore Stack
-      //llvm::Value* sp = (**i)["$stack_pointer"].address;
-      //m_Builder.CreateCall(m_StackRestore, sp);
+      llvm::Value* sp = (*vtable)["$stack_pointer"].address;
+      m_Builder.CreateCall(m_StackRestore, sp);
     }
   }
   
       std::abort();
     }
     
-    // Clean up stack
+    // Clean up resources
     _codegen_scope_interrupt( depth - m_LoopDepth.back() );
     
     // jmp LoopEnd
       std::abort();
     }
     
-    // Clean up stack
+    // Clean up resources
     _codegen_scope_interrupt( depth - m_LoopDepth.back() );
     
     // jmp Loop

File lab_4/b98902077/test1.cmm

+{
+  while (1 == 1) {
+    int n;
+    scan(n);
+    if (n > 10) { break; }
+  }
+  println("YO");
+}

File lab_4/b98902077/test2.cmm

+{
+  while (1 == 1) {
+    int [3] arr;
+    int n;
+    
+    scan(n);
+    if (n > 10) { break;  }
+  }
+  println("YEAH");
+}