vm.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #pragma once
  2. #include "pocketpy/common/memorypool.h"
  3. #include "pocketpy/common/name.h"
  4. #include "pocketpy/objects/codeobject.h"
  5. #include "pocketpy/objects/bintree.h"
  6. #include "pocketpy/objects/container.h"
  7. #include "pocketpy/pocketpy.h"
  8. #include "pocketpy/interpreter/heap.h"
  9. #include "pocketpy/interpreter/frame.h"
  10. #include "pocketpy/interpreter/typeinfo.h"
  11. #include "pocketpy/interpreter/line_profiler.h"
  12. #include <time.h>
  13. // TODO:
  14. // 1. __eq__ and __ne__ fallbacks
  15. // 2. un-cleared exception detection
  16. // 3. super()
  17. // 4. stack balance guanrantee
  18. // 5. stack effect of each opcode
  19. // 6. py_TypeInfo
  20. typedef struct TraceInfo {
  21. SourceLocation prev_loc;
  22. py_TraceFunc func;
  23. } TraceInfo;
  24. typedef struct WatchdogInfo {
  25. clock_t max_reset_time;
  26. } WatchdogInfo;
  27. typedef struct TypePointer {
  28. py_TypeInfo* ti;
  29. py_Dtor dtor;
  30. } TypePointer;
  31. typedef struct py_ModuleInfo {
  32. c11_string* name;
  33. c11_string* package;
  34. c11_string* path;
  35. py_GlobalRef self; // weakref to the original module object
  36. } py_ModuleInfo;
  37. typedef struct VM {
  38. py_Frame* top_frame;
  39. BinTree modules;
  40. c11_vector /*TypePointer*/ types;
  41. py_GlobalRef builtins; // builtins module
  42. py_GlobalRef main; // __main__ module
  43. py_Callbacks callbacks;
  44. py_TValue last_retval;
  45. py_TValue unhandled_exc;
  46. int recursion_depth;
  47. int max_recursion_depth;
  48. py_TValue reg[8]; // users' registers
  49. void* ctx; // user-defined context
  50. CachedNames cached_names;
  51. py_StackRef curr_class;
  52. py_StackRef curr_decl_based_function; // this is for get current function without frame
  53. TraceInfo trace_info;
  54. WatchdogInfo watchdog_info;
  55. LineProfiler line_profiler;
  56. py_TValue vectorcall_buffer[PK_MAX_CO_VARNAMES];
  57. FixedMemoryPool pool_frame;
  58. ManagedHeap heap;
  59. ValueStack stack; // put `stack` at the end for better cache locality
  60. } VM;
  61. void VM__ctor(VM* self);
  62. void VM__dtor(VM* self);
  63. int VM__index(VM* self);
  64. void VM__push_frame(VM* self, py_Frame* frame);
  65. void VM__pop_frame(VM* self);
  66. bool pk__parse_int_slice(py_Ref slice,
  67. int length,
  68. int* restrict start,
  69. int* restrict stop,
  70. int* restrict step);
  71. bool pk__normalize_index(int* index, int length);
  72. bool pk__object_new(int argc, py_Ref argv);
  73. bool pk_wrapper__self(int argc, py_Ref argv);
  74. const char* pk_op2str(py_Name op);
  75. typedef enum FrameResult {
  76. RES_ERROR = 0,
  77. RES_RETURN = 1,
  78. RES_CALL = 2,
  79. RES_YIELD = 3,
  80. } FrameResult;
  81. FrameResult VM__run_top_frame(VM* self);
  82. FrameResult VM__vectorcall(VM* self, uint16_t argc, uint16_t kwargc, bool opcall);
  83. const char* pk_opname(Opcode op);
  84. int pk_arrayview(py_Ref self, py_TValue** p);
  85. bool pk_wrapper__arrayequal(py_Type type, int argc, py_Ref argv);
  86. bool pk_arraycontains(py_Ref self, py_Ref val);
  87. bool pk_loadmethod(py_StackRef self, py_Name name);
  88. bool pk_callmagic(py_Name name, int argc, py_Ref argv);
  89. bool pk_exec(CodeObject* co, py_Ref module);
  90. bool pk_execdyn(CodeObject* co, py_Ref module, py_Ref globals, py_Ref locals);
  91. /// Assumes [a, b] are on the stack, performs a binary op.
  92. /// The result is stored in `self->last_retval`.
  93. /// The stack remains unchanged.
  94. bool pk_stack_binaryop(VM* self, py_Name op, py_Name rop);
  95. void pk_print_stack(VM* self, py_Frame* frame, Bytecode byte);
  96. bool pk_format_object(VM* self, py_Ref val, c11_sv spec);
  97. // type registration
  98. void pk_object__register();
  99. void pk_number__register();
  100. py_Type pk_str__register();
  101. py_Type pk_str_iterator__register();
  102. py_Type pk_bytes__register();
  103. py_Type pk_dict__register();
  104. py_Type pk_dict_items__register();
  105. py_Type pk_list__register();
  106. py_Type pk_tuple__register();
  107. py_Type pk_list_iterator__register();
  108. py_Type pk_tuple_iterator__register();
  109. py_Type pk_slice__register();
  110. py_Type pk_function__register();
  111. py_Type pk_nativefunc__register();
  112. py_Type pk_boundmethod__register();
  113. py_Type pk_range__register();
  114. py_Type pk_range_iterator__register();
  115. py_Type pk_module__register();
  116. py_Type pk_BaseException__register();
  117. py_Type pk_Exception__register();
  118. py_Type pk_StopIteration__register();
  119. py_Type pk_super__register();
  120. py_Type pk_property__register();
  121. py_Type pk_staticmethod__register();
  122. py_Type pk_classmethod__register();
  123. py_Type pk_generator__register();
  124. py_Type pk_namedict__register();
  125. py_Type pk_code__register();
  126. py_GlobalRef pk_builtins__register();
  127. /* mappingproxy */
  128. void pk_mappingproxy__namedict(py_Ref out, py_Ref object);