blueloveTH 1 ano atrás
pai
commit
fb2c168a20
5 arquivos alterados com 25 adições e 22 exclusões
  1. 4 0
      include/pocketpy/common.h
  2. 2 2
      include/pocketpy/frame.h
  3. 8 2
      include/pocketpy/vm.h
  4. 4 6
      src/ceval.cpp
  5. 7 12
      src/vm.cpp

+ 4 - 0
include/pocketpy/common.h

@@ -177,6 +177,8 @@ inline constexpr bool is_sso_v = is_integral_v<T> || is_floating_point_v<T>;
 template<typename T>
 using obj_get_t = std::conditional_t<is_sso_v<T>, T, T&>;
 
+struct const_sso_var {};
+
 struct PyVar final{
     Type type;
     bool is_sso;
@@ -186,6 +188,8 @@ struct PyVar final{
 
     // uninitialized
     PyVar() = default;
+    // constexpr initialized
+    constexpr PyVar(const const_sso_var&, Type type, i64 value): type(type), is_sso(true), flags(0), _0(0), _1(value) {}
     // zero initialized
     constexpr PyVar(std::nullptr_t): type(0), is_sso(false), flags(0), _0(0), _1(0) {}
     // PyObject* initialized (is_sso = false)

+ 2 - 2
include/pocketpy/frame.h

@@ -94,10 +94,10 @@ struct Frame {
     Frame(PyVar* p0, const CodeObject_& co, PyVar _module)
             : _ip(-1), _next_ip(0), _sp_base(p0), co(co.get()), _module(_module), _callable(nullptr), _locals(co.get(), p0) {}
 
-    int next_bytecode() {
+    Bytecode next_bytecode() {
         _ip = _next_ip++;
         PK_DEBUG_ASSERT(_ip >= 0 && _ip < co->codes.size());
-        return _ip;
+        return co->codes[_ip];
     }
 
     PyVar* actual_sp_base() const { return _locals.a; }

+ 8 - 2
include/pocketpy/vm.h

@@ -142,8 +142,7 @@ public:
         stack_no_copy<ArgsView> s_view;
     } __c;
 
-    PyVar None, True, False, NotImplemented;
-    PyVar StopIteration, Ellipsis;
+    PyVar StopIteration;        // a special Exception class
     PyVar builtins, _main;
 
     // typeid -> Type
@@ -182,6 +181,13 @@ public:
     static constexpr Type tp_super=Type(15), tp_exception=Type(16), tp_bytes=Type(17), tp_mappingproxy=Type(18);
     static constexpr Type tp_dict=Type(19), tp_property=Type(20), tp_star_wrapper=Type(21);
     static constexpr Type tp_staticmethod=Type(22), tp_classmethod=Type(23);
+    static constexpr Type tp_none=Type(24), tp_not_implemented=Type(25), tp_ellipsis=Type(26);
+
+    static constexpr PyVar True{const_sso_var(), tp_bool, 1};
+    static constexpr PyVar False{const_sso_var(), tp_bool, 0};
+    static constexpr PyVar None{const_sso_var(), tp_none, 0};
+    static constexpr PyVar NotImplemented{const_sso_var(), tp_not_implemented, 0};
+    static constexpr PyVar Ellipsis{const_sso_var(), tp_ellipsis, 0};
 
     const bool enable_os;
     VM(bool enable_os=true);

+ 4 - 6
src/ceval.cpp

@@ -110,14 +110,12 @@ PyVar VM::__run_top_frame(){
 __NEXT_FRAME:
     // cache
     const CodeObject* co = frame->co;
-    const Bytecode* co_codes = co->codes.data();
-
-    Bytecode byte = co_codes[frame->next_bytecode()];
+    Bytecode byte = frame->next_bytecode();
     CEVAL_STEP_CALLBACK();
 
-#define DISPATCH() { byte = co_codes[frame->next_bytecode()]; CEVAL_STEP_CALLBACK(); goto __NEXT_STEP;}
+#define DISPATCH() { byte = frame->next_bytecode(); CEVAL_STEP_CALLBACK(); break;}
 
-__NEXT_STEP:;
+for(;;){
 #if PK_DEBUG_CEVAL_STEP
     __log_s_data();
 #endif
@@ -993,7 +991,7 @@ __NEXT_STEP:;
     } DISPATCH()
     /*****************************************/
     }
-}
+}}
 /**********************************************************************/
             PK_UNREACHABLE()
         }catch(HandledException){

+ 7 - 12
src/vm.cpp

@@ -860,15 +860,13 @@ void VM::__init_builtin_types(){
     if(tp_staticmethod != _new_type("staticmethod")) exit(-3);
     if(tp_classmethod != _new_type("classmethod")) exit(-3);
 
+    if(tp_none != _new_type("NoneType")) exit(-3);
+    if(tp_not_implemented != _new_type("NotImplementedType")) exit(-3);
+    if(tp_ellipsis != _new_type("ellipsis")) exit(-3);
+
     // SyntaxError and IndentationError must be created here
     Type tp_syntax_error = _new_type("SyntaxError", tp_exception, true);
     Type tp_indentation_error = _new_type("IndentationError", tp_syntax_error, true);
-
-    this->None = heap._new<Dummy>(_new_type("NoneType"));
-    this->NotImplemented = heap._new<Dummy>(_new_type("NotImplementedType"));
-    this->Ellipsis = heap._new<Dummy>(_new_type("ellipsis"));
-    this->True = heap._new<Dummy>(tp_bool);
-    this->False = heap._new<Dummy>(tp_bool);
     this->StopIteration = _all_types[_new_type("StopIteration", tp_exception)].obj;
 
     this->builtins = new_module("builtins");
@@ -1002,21 +1000,18 @@ PyVar VM::vectorcall(int ARGC, int KWARGC, bool op_call){
     PyVar callable = p1[-ARGC-2];
     Type callable_t = _tp(callable);
 
-    int method_call = p0[1] != PY_NULL;
-
     // handle boundmethod, do a patch
     if(callable_t == tp_bound_method){
-        PK_DEBUG_ASSERT(!method_call)
+        PK_DEBUG_ASSERT(p0[1] == PY_NULL)
         BoundMethod& bm = PK_OBJ_GET(BoundMethod, callable);
         callable = bm.func;      // get unbound method
         callable_t = _tp(callable);
         p1[-(ARGC + 2)] = bm.func;
         p1[-(ARGC + 1)] = bm.self;
-        method_call = 1;
         // [unbound, self, args..., kwargs...]
     }
 
-    ArgsView args(p1 - ARGC - method_call, p1);
+    ArgsView args(p0[1]==PY_NULL ? (p0+2) : (p0+1), p1);
     ArgsView kwargs(p1, s_data._sp);
 
     PyVar* _base = args.begin();
@@ -1092,7 +1087,7 @@ PyVar VM::vectorcall(int ARGC, int KWARGC, bool op_call){
         // [type, NULL, args..., kwargs...]
         PyVar new_f = find_name_in_mro(PK_OBJ_GET(Type, callable), __new__);
         PyVar obj;
-        PK_DEBUG_ASSERT(new_f != nullptr && !method_call);
+        PK_DEBUG_ASSERT(new_f != nullptr && p0[1]==PY_NULL);
         if(new_f == __cached_object_new) {
             // fast path for object.__new__
             obj = vm->new_object<DummyInstance>(PK_OBJ_GET(Type, callable));