codeobject.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 {
  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. uint16_t op;
  18. uint16_t block;
  19. int arg;
  20. };
  21. enum CodeBlockType {
  22. NO_BLOCK,
  23. FOR_LOOP,
  24. WHILE_LOOP,
  25. CONTEXT_MANAGER,
  26. TRY_EXCEPT,
  27. };
  28. inline const int BC_NOARG = -1;
  29. inline const int BC_KEEPLINE = -1;
  30. struct CodeBlock {
  31. CodeBlockType type;
  32. int parent; // parent index in blocks
  33. int for_loop_depth; // this is used for exception handling
  34. int start; // start index of this block in codes, inclusive
  35. int end; // end index of this block in codes, exclusive
  36. int end2; // ...
  37. CodeBlock(CodeBlockType type, int parent, int for_loop_depth, int start):
  38. type(type), parent(parent), for_loop_depth(for_loop_depth), start(start), end(-1), end2(-1) {}
  39. int get_break_end() const{
  40. if(end2 != -1) return end2;
  41. return end;
  42. }
  43. };
  44. struct CodeObject;
  45. struct FuncDecl;
  46. using CodeObject_ = std::shared_ptr<CodeObject>;
  47. using FuncDecl_ = std::shared_ptr<FuncDecl>;
  48. struct CodeObjectSerializer{
  49. std::string buffer;
  50. int depth = 0;
  51. std::set<StrName> names;
  52. static const char END = '\n';
  53. CodeObjectSerializer();
  54. void write_int(i64 v);
  55. void write_float(f64 v);
  56. void write_str(const Str& v);
  57. void write_none();
  58. void write_ellipsis();
  59. void write_bool(bool v);
  60. void write_begin_mark();
  61. void write_name(StrName name);
  62. void write_end_mark();
  63. template<typename T>
  64. void write_bytes(T v){
  65. static_assert(std::is_trivially_copyable<T>::value);
  66. buffer += 'x';
  67. char* p = (char*)&v;
  68. for(int i=0; i<sizeof(T); i++){
  69. char c = p[i];
  70. buffer += "0123456789abcdef"[(c >> 4) & 0xf];
  71. buffer += "0123456789abcdef"[c & 0xf];
  72. }
  73. buffer += END;
  74. }
  75. void write_object(VM* vm, PyObject* obj);
  76. void write_code(VM* vm, const CodeObject* co);
  77. std::string str();
  78. };
  79. struct CodeObject {
  80. std::shared_ptr<SourceData> src;
  81. Str name;
  82. bool is_generator = false;
  83. std::vector<Bytecode> codes;
  84. std::vector<int> lines; // line number for each bytecode
  85. List consts;
  86. std::vector<StrName> varnames; // local variables
  87. NameDictInt varnames_inv;
  88. std::vector<CodeBlock> blocks = { CodeBlock(NO_BLOCK, -1, 0, 0) };
  89. NameDictInt labels;
  90. std::vector<FuncDecl_> func_decls;
  91. CodeObject(std::shared_ptr<SourceData> src, const Str& name);
  92. void _gc_mark() const;
  93. void write(VM* vm, CodeObjectSerializer& ss) const;
  94. Str serialize(VM* vm) const;
  95. };
  96. struct FuncDecl {
  97. struct KwArg {
  98. int key; // index in co->varnames
  99. PyObject* value; // default value
  100. };
  101. CodeObject_ code; // code object of this function
  102. pod_vector<int> args; // indices in co->varnames
  103. pod_vector<KwArg> kwargs; // indices in co->varnames
  104. int starred_arg = -1; // index in co->varnames, -1 if no *arg
  105. int starred_kwarg = -1; // index in co->varnames, -1 if no **kwarg
  106. bool nested = false; // whether this function is nested
  107. Str signature; // signature of this function
  108. Str docstring; // docstring of this function
  109. void _gc_mark() const;
  110. };
  111. struct UserData{
  112. char data[15];
  113. bool empty;
  114. UserData(): empty(true) {}
  115. template<typename T>
  116. UserData(T t): empty(false){
  117. static_assert(std::is_trivially_copyable_v<T>);
  118. static_assert(sizeof(T) <= sizeof(data));
  119. memcpy(data, &t, sizeof(T));
  120. }
  121. template <typename T>
  122. T get() const{
  123. static_assert(std::is_trivially_copyable_v<T>);
  124. static_assert(sizeof(T) <= sizeof(data));
  125. #if PK_DEBUG_EXTRA_CHECK
  126. PK_ASSERT(!empty);
  127. #endif
  128. return reinterpret_cast<const T&>(data);
  129. }
  130. };
  131. struct NativeFunc {
  132. NativeFuncC f;
  133. // old style argc-based call
  134. int argc;
  135. // new style decl-based call
  136. FuncDecl_ decl;
  137. UserData _userdata;
  138. void set_userdata(UserData data) {
  139. if(!_userdata.empty && !data.empty){
  140. // override is not supported
  141. throw std::runtime_error("userdata already set");
  142. }
  143. _userdata = data;
  144. }
  145. NativeFunc(NativeFuncC f, int argc, bool method);
  146. NativeFunc(NativeFuncC f, FuncDecl_ decl);
  147. void check_size(VM* vm, ArgsView args) const;
  148. PyObject* call(VM* vm, ArgsView args) const;
  149. };
  150. struct Function{
  151. FuncDecl_ decl;
  152. PyObject* _module;
  153. NameDict_ _closure;
  154. };
  155. template<>
  156. struct Py_<Function> final: PyObject {
  157. Function _value;
  158. template<typename... Args>
  159. Py_(Type type, Args&&... args): PyObject(type), _value(std::forward<Args>(args)...) {
  160. enable_instance_dict();
  161. }
  162. void _obj_gc_mark() override {
  163. _value.decl->_gc_mark();
  164. if(_value._module != nullptr) PK_OBJ_MARK(_value._module);
  165. if(_value._closure != nullptr) gc_mark_namedict(*_value._closure);
  166. }
  167. };
  168. template<>
  169. struct Py_<NativeFunc> final: PyObject {
  170. NativeFunc _value;
  171. template<typename... Args>
  172. Py_(Type type, Args&&... args): PyObject(type), _value(std::forward<Args>(args)...) {
  173. enable_instance_dict();
  174. }
  175. void _obj_gc_mark() override {
  176. if(_value.decl != nullptr){
  177. _value.decl->_gc_mark();
  178. }
  179. }
  180. };
  181. template<typename T>
  182. T lambda_get_userdata(PyObject** p){
  183. if(p[-1] != PY_NULL) return PK_OBJ_GET(NativeFunc, p[-1])._userdata.get<T>();
  184. else return PK_OBJ_GET(NativeFunc, p[-2])._userdata.get<T>();
  185. }
  186. } // namespace pkpy