vm.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. py_CFunction on_end_subclass; // 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(const py_Ref slice, int length, int* start, int* stop, int* step);
  47. bool pk__normalize_index(int* index, int length);
  48. typedef enum pk_FrameResult {
  49. RES_RETURN,
  50. RES_CALL,
  51. RES_YIELD,
  52. RES_ERROR,
  53. } pk_FrameResult;
  54. pk_FrameResult pk_VM__run_top_frame(pk_VM* self);
  55. py_Type pk_newtype(const char* name,
  56. py_Type base,
  57. const py_GlobalRef module,
  58. void (*dtor)(void*),
  59. bool is_python,
  60. bool is_sealed);
  61. pk_FrameResult pk_VM__vectorcall(pk_VM* self, uint16_t argc, uint16_t kwargc, bool opcall);
  62. const char* pk_opname(Opcode op);
  63. py_TValue* pk_arrayview(py_Ref self, int* length);
  64. int pk_arrayeq(py_TValue* lhs, int lhs_length, py_TValue* rhs, int rhs_length);
  65. /// Assumes [a, b] are on the stack, performs a binary op.
  66. /// The result is stored in `self->last_retval`.
  67. /// The stack remains unchanged.
  68. bool pk_stack_binaryop(pk_VM* self, py_Name op, py_Name rop);
  69. void pk_print_stack(pk_VM* self, Frame* frame, Bytecode byte);
  70. // type registration
  71. void pk_object__register();
  72. void pk_number__register();
  73. py_Type pk_str__register();
  74. py_Type pk_str_iterator__register();
  75. py_Type pk_bytes__register();
  76. py_Type pk_dict__register();
  77. py_Type pk_list__register();
  78. py_Type pk_tuple__register();
  79. py_Type pk_array_iterator__register();
  80. py_Type pk_slice__register();
  81. py_Type pk_function__register();
  82. py_Type pk_nativefunc__register();
  83. py_Type pk_range__register();
  84. py_Type pk_range_iterator__register();
  85. py_Type pk_BaseException__register();
  86. py_Type pk_Exception__register();
  87. py_TValue pk_builtins__register();
  88. #ifdef __cplusplus
  89. }
  90. #endif