vm.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #pragma once
  2. #include "pocketpy/objects/codeobject.h"
  3. #include "pocketpy/pocketpy.h"
  4. #include "pocketpy/interpreter/gc.h"
  5. #include "pocketpy/interpreter/frame.h"
  6. #ifdef __cplusplus
  7. extern "C" {
  8. #endif
  9. typedef struct pk_TypeInfo {
  10. py_Name name;
  11. py_Type base;
  12. py_TValue self;
  13. py_TValue module; // the module where the type is defined
  14. bool is_python; // is it a python class? (not derived from c object)
  15. bool is_sealed; // can it be subclassed?
  16. void (*dtor)(void*);
  17. c11_vector /*T=py_Name*/ annotated_fields;
  18. void (*on_end_subclass)(struct pk_TypeInfo*); // backdoor for enum module
  19. /* Magic Slots */
  20. py_TValue magic[64];
  21. } pk_TypeInfo;
  22. typedef struct pk_VM {
  23. Frame* top_frame;
  24. pk_NameDict modules;
  25. c11_vector /*T=pk_TypeInfo*/ types;
  26. py_TValue builtins; // builtins module
  27. py_TValue main; // __main__ module
  28. void (*_ceval_on_step)(Frame*, Bytecode);
  29. unsigned char* (*_import_file)(const char*);
  30. void (*_stdout)(const char*, ...);
  31. void (*_stderr)(const char*, ...);
  32. py_TValue last_retval;
  33. py_TValue last_exception;
  34. bool is_stopiteration;
  35. py_TValue reg[8]; // users' registers
  36. py_TValue* __curr_class;
  37. FuncDecl_ __dynamic_func_decl;
  38. py_TValue __vectorcall_buffer[PK_MAX_CO_VARNAMES];
  39. pk_ManagedHeap heap;
  40. ValueStack stack; // put `stack` at the end for better cache locality
  41. } pk_VM;
  42. void pk_VM__ctor(pk_VM* self);
  43. void pk_VM__dtor(pk_VM* self);
  44. void pk_VM__push_frame(pk_VM* self, Frame* frame);
  45. void pk_VM__pop_frame(pk_VM* self);
  46. bool pk__parse_int_slice(py_Ref slice, int length, int* start, int* stop, int* step);
  47. bool pk__normalize_index(int* index, int length);
  48. void pk_list__mark(void* ud, void (*marker)(py_TValue*));
  49. void pk_dict__mark(void* ud, void (*marker)(py_TValue*));
  50. typedef enum pk_FrameResult {
  51. RES_RETURN,
  52. RES_CALL,
  53. RES_YIELD,
  54. RES_ERROR,
  55. } pk_FrameResult;
  56. pk_FrameResult pk_VM__run_top_frame(pk_VM* self);
  57. py_Type pk_newtype(const char* name,
  58. py_Type base,
  59. const py_GlobalRef module,
  60. void (*dtor)(void*),
  61. bool is_python,
  62. bool is_sealed);
  63. pk_FrameResult pk_VM__vectorcall(pk_VM* self, uint16_t argc, uint16_t kwargc, bool opcall);
  64. const char* pk_opname(Opcode op);
  65. py_TValue* pk_arrayview(py_Ref self, int* length);
  66. int pk_arrayequal(py_TValue* lhs, int lhs_length, py_TValue* rhs, int rhs_length);
  67. bool pk_arrayiter(py_Ref val);
  68. bool pk_arraycontains(py_Ref self, py_Ref val);
  69. /// Assumes [a, b] are on the stack, performs a binary op.
  70. /// The result is stored in `self->last_retval`.
  71. /// The stack remains unchanged.
  72. bool pk_stack_binaryop(pk_VM* self, py_Name op, py_Name rop);
  73. void pk_print_stack(pk_VM* self, Frame* frame, Bytecode byte);
  74. // type registration
  75. void pk_object__register();
  76. void pk_number__register();
  77. py_Type pk_str__register();
  78. py_Type pk_str_iterator__register();
  79. py_Type pk_bytes__register();
  80. py_Type pk_dict__register();
  81. py_Type pk_dict_items__register();
  82. py_Type pk_list__register();
  83. py_Type pk_tuple__register();
  84. py_Type pk_array_iterator__register();
  85. py_Type pk_slice__register();
  86. py_Type pk_function__register();
  87. py_Type pk_nativefunc__register();
  88. py_Type pk_range__register();
  89. py_Type pk_range_iterator__register();
  90. py_Type pk_BaseException__register();
  91. py_Type pk_Exception__register();
  92. py_TValue pk_builtins__register();
  93. #ifdef __cplusplus
  94. }
  95. #endif