codeobject.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. bool is_generator;
  55. std::vector<Bytecode> codes;
  56. std::vector<int> iblocks; // block index for each bytecode
  57. std::vector<LineInfo> lines;
  58. small_vector_no_copy_and_move<PyObject*, 8> consts; // constants
  59. small_vector_no_copy_and_move<StrName, 8> varnames; // local variables
  60. NameDictInt varnames_inv;
  61. std::vector<CodeBlock> blocks;
  62. NameDictInt labels;
  63. std::vector<FuncDecl_> func_decls;
  64. int start_line;
  65. int end_line;
  66. const CodeBlock& _get_block_codei(int codei) const{
  67. return blocks[iblocks[codei]];
  68. }
  69. CodeObject(std::shared_ptr<SourceData> src, const Str& name);
  70. void _gc_mark() const;
  71. };
  72. struct FuncDecl {
  73. struct KwArg {
  74. int index; // index in co->varnames
  75. StrName key; // name of this argument
  76. PyObject* value; // default value
  77. };
  78. CodeObject_ code; // code object of this function
  79. small_vector_no_copy_and_move<int, 6> args; // indices in co->varnames
  80. small_vector_no_copy_and_move<KwArg, 6> kwargs; // indices in co->varnames
  81. int starred_arg = -1; // index in co->varnames, -1 if no *arg
  82. int starred_kwarg = -1; // index in co->varnames, -1 if no **kwarg
  83. bool nested = false; // whether this function is nested
  84. Str signature; // signature of this function
  85. Str docstring; // docstring of this function
  86. bool is_simple;
  87. NameDictInt kw_to_index;
  88. void add_kwarg(int index, StrName key, PyObject* value){
  89. kw_to_index.set(key, index);
  90. kwargs.push_back(KwArg{index, key, value});
  91. }
  92. void _gc_mark() const;
  93. };
  94. struct UserData{
  95. char data[12];
  96. bool empty;
  97. UserData(): empty(true) {}
  98. template<typename T>
  99. UserData(T t): empty(false){
  100. static_assert(std::is_trivially_copyable_v<T>);
  101. static_assert(sizeof(T) <= sizeof(data));
  102. memcpy(data, &t, sizeof(T));
  103. }
  104. template <typename T>
  105. T get() const{
  106. static_assert(std::is_trivially_copyable_v<T>);
  107. static_assert(sizeof(T) <= sizeof(data));
  108. #if PK_DEBUG_EXTRA_CHECK
  109. PK_ASSERT(!empty);
  110. #endif
  111. return reinterpret_cast<const T&>(data);
  112. }
  113. };
  114. struct NativeFunc {
  115. NativeFuncC f;
  116. // old style argc-based call
  117. int argc;
  118. // new style decl-based call
  119. FuncDecl_ decl;
  120. UserData _userdata;
  121. void set_userdata(UserData data) {
  122. if(!_userdata.empty && !data.empty){
  123. // override is not supported
  124. throw std::runtime_error("userdata already set");
  125. }
  126. _userdata = data;
  127. }
  128. NativeFunc(NativeFuncC f, int argc, bool method);
  129. NativeFunc(NativeFuncC f, FuncDecl_ decl);
  130. void check_size(VM* vm, ArgsView args) const;
  131. PyObject* call(VM* vm, ArgsView args) const;
  132. };
  133. struct Function{
  134. FuncDecl_ decl;
  135. PyObject* _module; // weak ref
  136. PyObject* _class; // weak ref
  137. NameDict_ _closure;
  138. explicit Function(FuncDecl_ decl, PyObject* _module, PyObject* _class, NameDict_ _closure):
  139. decl(decl), _module(_module), _class(_class), _closure(_closure) {}
  140. };
  141. template<>
  142. struct Py_<Function> final: PyObject {
  143. Function _value;
  144. template<typename... Args>
  145. Py_(Type type, Args&&... args): PyObject(type), _value(std::forward<Args>(args)...) {
  146. // _enable_instance_dict();
  147. }
  148. void _obj_gc_mark() override {
  149. _value.decl->_gc_mark();
  150. if(_value._closure != nullptr) gc_mark_namedict(*_value._closure);
  151. }
  152. void* _value_ptr() override {
  153. return &_value;
  154. }
  155. };
  156. template<>
  157. struct Py_<NativeFunc> final: PyObject {
  158. NativeFunc _value;
  159. template<typename... Args>
  160. Py_(Type type, Args&&... args): PyObject(type), _value(std::forward<Args>(args)...) {
  161. // _enable_instance_dict();
  162. }
  163. void _obj_gc_mark() override {
  164. if(_value.decl != nullptr){
  165. _value.decl->_gc_mark();
  166. }
  167. }
  168. void* _value_ptr() override {
  169. return &_value;
  170. }
  171. };
  172. template<typename T>
  173. T lambda_get_userdata(PyObject** p){
  174. if(p[-1] != PY_NULL) return PK_OBJ_GET(NativeFunc, p[-1])._userdata.get<T>();
  175. else return PK_OBJ_GET(NativeFunc, p[-2])._userdata.get<T>();
  176. }
  177. } // namespace pkpy