blueloveTH пре 2 година
родитељ
комит
a8fef67dbd
5 измењених фајлова са 83 додато и 51 уклоњено
  1. 52 38
      src/frame.h
  2. 16 11
      src/gc.h
  3. 8 0
      src/namedict.h
  4. 1 1
      src/vector.h
  5. 6 1
      src/vm.h

+ 52 - 38
src/frame.h

@@ -112,46 +112,60 @@ template<> inline void gc_mark<Function>(Function& t){
     t._closure._gc_mark();
 }
 
-struct ValueStack {
-    PyObject** _begin;
-    PyObject** _sp;
-
-    ValueStack(int n=16): _begin((PyObject**)pool128.alloc(n * sizeof(void*))), _sp(_begin) { }
-
-    PyObject*& top(){ return _sp[-1]; }
-    PyObject* top() const { return _sp[-1]; }
-    PyObject*& second(){ return _sp[-2]; }
-    PyObject* second() const { return _sp[-2]; }
-    PyObject*& peek(int n){ return _sp[-n]; }
-    PyObject* peek(int n) const { return _sp[-n]; }
-    void push(PyObject* v){ *_sp++ = v; }
-    void pop(){ --_sp; }
-    PyObject* popx(){ return *--_sp; }
-    ArgsView view(int n){ return ArgsView(_sp-n, _sp); }
-    void shrink(int n){ _sp -= n; }
-    int size() const { return _sp - _begin; }
-    bool empty() const { return _sp == _begin; }
-    PyObject** begin() const { return _begin; }
-    PyObject** end() const { return _sp; }
-    void resize(int n) { _sp = _begin + n; }
-
-    ValueStack(ValueStack&& other) noexcept{
-        _begin = other._begin;
-        _sp = other._sp;
-        other._begin = nullptr;
-    }
-
-    ValueStack& operator=(ValueStack&& other) noexcept{
-        if(_begin != nullptr) pool128.dealloc(_begin);
-        _begin = other._begin;
-        _sp = other._sp;
-        other._begin = nullptr;
-        return *this;
-    }
-
-    ~ValueStack(){ if(_begin!=nullptr) pool128.dealloc(_begin); }
+struct ValueStack: pod_vector<PyObject*> {
+    PyObject*& top(){ return back(); }
+    PyObject* top() const { return back(); }
+    PyObject*& second(){ return (*this)[size()-2]; }
+    PyObject* second() const { return (*this)[size()-2]; }
+    PyObject*& peek(int n){ return (*this)[size()-n]; }
+    PyObject* peek(int n) const { return (*this)[size()-n]; }
+    void push(PyObject* v){ push_back(v); }
+    void pop(){ pop_back(); }
+    PyObject* popx(){ return popx_back(); }
+    ArgsView view(int n){ return ArgsView(end()-n, end()); }
+    void shrink(int n){ resize(size() - n); }
 };
 
+// struct ValueStack {
+//     PyObject** _begin;
+//     PyObject** _sp;
+
+//     ValueStack(int n=16): _begin((PyObject**)pool128.alloc(n * sizeof(void*))), _sp(_begin) { }
+
+//     PyObject*& top(){ return _sp[-1]; }
+//     PyObject* top() const { return _sp[-1]; }
+//     PyObject*& second(){ return _sp[-2]; }
+//     PyObject* second() const { return _sp[-2]; }
+//     PyObject*& peek(int n){ return _sp[-n]; }
+//     PyObject* peek(int n) const { return _sp[-n]; }
+//     void push(PyObject* v){ *_sp++ = v; }
+//     void pop(){ --_sp; }
+//     PyObject* popx(){ return *--_sp; }
+//     ArgsView view(int n){ return ArgsView(_sp-n, _sp); }
+//     void shrink(int n){ _sp -= n; }
+//     int size() const { return _sp - _begin; }
+//     bool empty() const { return _sp == _begin; }
+//     PyObject** begin() const { return _begin; }
+//     PyObject** end() const { return _sp; }
+//     void resize(int n) { _sp = _begin + n; }
+
+//     ValueStack(ValueStack&& other) noexcept{
+//         _begin = other._begin;
+//         _sp = other._sp;
+//         other._begin = nullptr;
+//     }
+
+//     ValueStack& operator=(ValueStack&& other) noexcept{
+//         if(_begin != nullptr) pool128.dealloc(_begin);
+//         _begin = other._begin;
+//         _sp = other._sp;
+//         other._begin = nullptr;
+//         return *this;
+//     }
+
+//     ~ValueStack(){ if(_begin!=nullptr) pool128.dealloc(_begin); }
+// };
+
 struct Frame {
     int _ip = -1;
     int _next_ip = 0;

+ 16 - 11
src/gc.h

@@ -56,15 +56,6 @@ struct ManagedHeap{
     inline static std::map<Type, int> deleted;
 #endif
 
-    ~ManagedHeap(){
-        for(PyObject* obj: _no_gc) obj->~PyObject(), pool64.dealloc(obj);
-#if DEBUG_GC_STATS
-        for(auto& [type, count]: deleted){
-            std::cout << "GC: " << obj_type_name(vm, type) << "=" << count << std::endl;
-        }
-#endif
-    }
-
     int sweep(){
         std::vector<PyObject*> alive;
         for(PyObject* obj: gen){
@@ -108,6 +99,16 @@ struct ManagedHeap{
     }
 
     void mark();
+
+    ~ManagedHeap(){
+        for(PyObject* obj: _no_gc) obj->~PyObject(), pool64.dealloc(obj);
+        for(PyObject* obj: gen) obj->~PyObject(), pool64.dealloc(obj);
+#if DEBUG_GC_STATS
+        for(auto& [type, count]: deleted){
+            std::cout << "GC: " << obj_type_name(vm, type) << "=" << count << std::endl;
+        }
+#endif
+    }
 };
 
 inline void FuncDecl::_gc_mark() const{
@@ -116,11 +117,15 @@ inline void FuncDecl::_gc_mark() const{
 }
 
 template<> inline void gc_mark<List>(List& t){
-    for(PyObject* obj: t) OBJ_MARK(obj);
+    for(PyObject* obj: t){
+        OBJ_MARK(obj);
+    }
 }
 
 template<> inline void gc_mark<Tuple>(Tuple& t){
-    for(int i=0; i<t.size(); i++) OBJ_MARK(t[i]);
+    for(PyObject* obj: t){
+        OBJ_MARK(obj);
+    }
 }
 
 template<> inline void gc_mark<NameDict>(NameDict& t){

+ 8 - 0
src/namedict.h

@@ -192,6 +192,14 @@ while(!_items[i].first.empty()) {       \
         }
         return v;
     }
+
+    void clear(){
+        for(uint16_t i=0; i<_capacity; i++){
+            _items[i].first = StrName();
+            _items[i].second = nullptr;
+        }
+        _size = 0;
+    }
 #undef HASH_PROBE
 #undef _hash
 };

+ 1 - 1
src/vector.h

@@ -71,6 +71,7 @@ struct pod_vector{
     }
 
     void pop_back() { _size--; }
+    T popx_back() { T t = std::move(_data[_size-1]); _size--; return t; }
     void extend(const pod_vector& other){
         for(int i=0; i<other.size(); i++) push_back(other[i]);
     }
@@ -89,7 +90,6 @@ struct pod_vector{
     int size() const { return _size; }
     T* data() { return _data; }
     const T* data() const { return _data; }
-    void pop_back_n(int n) { _size -= n; }
     void clear() { _size=0; }
 
     template<typename __ValueT>

+ 6 - 1
src/vm.h

@@ -326,7 +326,12 @@ public:
         return _all_types[OBJ_GET(Type, _t(obj->type)).index].obj;
     }
 
-    ~VM() { heap.collect(); }
+    ~VM() {
+        callstack.clear();
+        _all_types.clear();
+        _modules.clear();
+        _lazy_modules.clear();
+    }
 
     CodeObject_ compile(Str source, Str filename, CompileMode mode);
     PyObject* num_negated(PyObject* obj);