codeobject.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #pragma once
  2. #include "obj.h"
  3. #include "error.h"
  4. namespace pkpy{
  5. enum NameScope { NAME_LOCAL, NAME_GLOBAL, NAME_GLOBAL_UNKNOWN };
  6. enum Opcode: uint8_t {
  7. #define OPCODE(name) OP_##name,
  8. #include "opcodes.h"
  9. #undef OPCODE
  10. };
  11. inline const char* OP_NAMES[] = {
  12. #define OPCODE(name) #name,
  13. #include "opcodes.h"
  14. #undef OPCODE
  15. };
  16. struct Bytecode{
  17. uint8_t op;
  18. uint16_t arg;
  19. };
  20. enum class CodeBlockType {
  21. NO_BLOCK,
  22. FOR_LOOP,
  23. WHILE_LOOP,
  24. CONTEXT_MANAGER,
  25. TRY_EXCEPT,
  26. };
  27. inline const uint8_t BC_NOARG = 0;
  28. inline const int BC_KEEPLINE = -1;
  29. struct CodeBlock {
  30. CodeBlockType type;
  31. int parent; // parent index in blocks
  32. int base_stack_size; // this is used for exception handling
  33. int start; // start index of this block in codes, inclusive
  34. int end; // end index of this block in codes, exclusive
  35. int end2; // ...
  36. CodeBlock(CodeBlockType type, int parent, int base_stack_size, int start):
  37. type(type), parent(parent), base_stack_size(base_stack_size), start(start), end(-1), end2(-1) {}
  38. int get_break_end() const{
  39. if(end2 != -1) return end2;
  40. return end;
  41. }
  42. };
  43. struct CodeObject;
  44. struct FuncDecl;
  45. using CodeObject_ = std::shared_ptr<CodeObject>;
  46. using FuncDecl_ = std::shared_ptr<FuncDecl>;
  47. struct CodeObject {
  48. struct LineInfo{
  49. int lineno; // line number for each bytecode
  50. bool is_virtual; // whether this bytecode is virtual (not in source code)
  51. };
  52. std::shared_ptr<SourceData> src;
  53. Str name;
  54. std::vector<Bytecode> codes;
  55. std::vector<int> iblocks; // block index for each bytecode
  56. std::vector<LineInfo> lines;
  57. small_vector_no_copy_and_move<PyObject*, 8> consts; // constants
  58. small_vector_no_copy_and_move<StrName, 8> varnames; // local variables
  59. NameDictInt varnames_inv;
  60. std::vector<CodeBlock> blocks;
  61. NameDictInt labels;
  62. std::vector<FuncDecl_> func_decls;
  63. int start_line;
  64. int end_line;
  65. const CodeBlock& _get_block_codei(int codei) const{
  66. return blocks[iblocks[codei]];
  67. }
  68. CodeObject(std::shared_ptr<SourceData> src, const Str& name);
  69. void _gc_mark() const;
  70. };
  71. enum class FuncType{
  72. UNSET,
  73. NORMAL,
  74. SIMPLE,
  75. EMPTY,
  76. GENERATOR,
  77. };
  78. struct FuncDecl {
  79. struct KwArg {
  80. int index; // index in co->varnames
  81. StrName key; // name of this argument
  82. PyObject* value; // default value
  83. };
  84. CodeObject_ code; // code object of this function
  85. small_vector_no_copy_and_move<int, 6> args; // indices in co->varnames
  86. small_vector_no_copy_and_move<KwArg, 6> kwargs; // indices in co->varnames
  87. int starred_arg = -1; // index in co->varnames, -1 if no *arg
  88. int starred_kwarg = -1; // index in co->varnames, -1 if no **kwarg
  89. bool nested = false; // whether this function is nested
  90. Str signature; // signature of this function
  91. Str docstring; // docstring of this function
  92. FuncType type = FuncType::UNSET;
  93. NameDictInt kw_to_index;
  94. void add_kwarg(int index, StrName key, PyObject* value){
  95. kw_to_index.set(key, index);
  96. kwargs.push_back(KwArg{index, key, value});
  97. }
  98. void _gc_mark() const;
  99. };
  100. struct UserData{
  101. char data[12];
  102. bool empty;
  103. UserData(): empty(true) {}
  104. template<typename T>
  105. UserData(T t): empty(false){
  106. static_assert(std::is_trivially_copyable_v<T>);
  107. static_assert(sizeof(T) <= sizeof(data));
  108. memcpy(data, &t, sizeof(T));
  109. }
  110. template <typename T>
  111. T get() const{
  112. static_assert(std::is_trivially_copyable_v<T>);
  113. static_assert(sizeof(T) <= sizeof(data));
  114. #if PK_DEBUG_EXTRA_CHECK
  115. PK_ASSERT(!empty);
  116. #endif
  117. return reinterpret_cast<const T&>(data);
  118. }
  119. };
  120. struct NativeFunc {
  121. NativeFuncC f;
  122. // old style argc-based call
  123. int argc;
  124. // new style decl-based call
  125. FuncDecl_ decl;
  126. UserData _userdata;
  127. void set_userdata(UserData data) {
  128. if(!_userdata.empty && !data.empty){
  129. // override is not supported
  130. throw std::runtime_error("userdata already set");
  131. }
  132. _userdata = data;
  133. }
  134. NativeFunc(NativeFuncC f, int argc, bool method);
  135. NativeFunc(NativeFuncC f, FuncDecl_ decl);
  136. void check_size(VM* vm, ArgsView args) const;
  137. PyObject* call(VM* vm, ArgsView args) const { return f(vm, args); }
  138. };
  139. struct Function{
  140. FuncDecl_ decl;
  141. PyObject* _module; // weak ref
  142. PyObject* _class; // weak ref
  143. NameDict_ _closure;
  144. explicit Function(FuncDecl_ decl, PyObject* _module, PyObject* _class, NameDict_ _closure):
  145. decl(decl), _module(_module), _class(_class), _closure(_closure) {}
  146. };
  147. template<>
  148. struct Py_<Function> final: PyObject {
  149. Function _value;
  150. template<typename... Args>
  151. Py_(Type type, Args&&... args): PyObject(type), _value(std::forward<Args>(args)...) {
  152. // _enable_instance_dict();
  153. }
  154. void _obj_gc_mark() override {
  155. _value.decl->_gc_mark();
  156. if(_value._closure != nullptr) gc_mark_namedict(*_value._closure);
  157. }
  158. void* _value_ptr() override {
  159. return &_value;
  160. }
  161. };
  162. template<>
  163. struct Py_<NativeFunc> final: PyObject {
  164. NativeFunc _value;
  165. template<typename... Args>
  166. Py_(Type type, Args&&... args): PyObject(type), _value(std::forward<Args>(args)...) {
  167. // _enable_instance_dict();
  168. }
  169. void _obj_gc_mark() override {
  170. if(_value.decl != nullptr){
  171. _value.decl->_gc_mark();
  172. }
  173. }
  174. void* _value_ptr() override {
  175. return &_value;
  176. }
  177. };
  178. template<typename T>
  179. T lambda_get_userdata(PyObject** p){
  180. if(p[-1] != PY_NULL) return PK_OBJ_GET(NativeFunc, p[-1])._userdata.get<T>();
  181. else return PK_OBJ_GET(NativeFunc, p[-2])._userdata.get<T>();
  182. }
  183. } // namespace pkpy