codeobject.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #pragma once
  2. #include <stdbool.h>
  3. #include <stdint.h>
  4. #include "pocketpy/common/vector.h"
  5. #include "pocketpy/common/smallmap.h"
  6. #include "pocketpy/objects/base.h"
  7. #include "pocketpy/objects/sourcedata.h"
  8. #include "pocketpy/objects/namedict.h"
  9. #include "pocketpy/pocketpy.h"
  10. #define BC_NOARG 0
  11. #define BC_KEEPLINE -1
  12. typedef enum FuncType {
  13. FuncType_UNSET,
  14. FuncType_NORMAL,
  15. FuncType_SIMPLE,
  16. FuncType_GENERATOR,
  17. } FuncType;
  18. typedef enum NameScope {
  19. NAME_LOCAL,
  20. NAME_GLOBAL,
  21. } NameScope;
  22. typedef enum CodeBlockType {
  23. CodeBlockType_NO_BLOCK,
  24. CodeBlockType_WHILE_LOOP,
  25. CodeBlockType_TRY,
  26. /* context blocks (stack-based) */
  27. CodeBlockType_FOR_LOOP,
  28. CodeBlockType_WITH,
  29. /* context blocks (flag-based) */
  30. CodeBlockType_EXCEPT,
  31. CodeBlockType_FINALLY,
  32. } CodeBlockType;
  33. typedef enum Opcode {
  34. #define OPCODE(name) OP_##name,
  35. #include "pocketpy/xmacros/opcodes.h"
  36. #undef OPCODE
  37. } Opcode;
  38. typedef struct Bytecode {
  39. uint8_t op;
  40. uint16_t arg;
  41. } Bytecode;
  42. void Bytecode__set_signed_arg(Bytecode* self, int arg);
  43. bool Bytecode__is_forward_jump(const Bytecode* self);
  44. typedef struct CodeBlock {
  45. CodeBlockType type;
  46. int parent; // parent index in blocks
  47. int start; // start index of this block in codes, inclusive
  48. int end; // end index of this block in codes, exclusive
  49. int end2; // ...
  50. } CodeBlock;
  51. typedef struct BytecodeEx {
  52. int lineno; // line number for each bytecode
  53. bool is_virtual; // whether this bytecode is virtual (not in source code)
  54. int iblock; // block index
  55. } BytecodeEx;
  56. typedef struct CodeObject {
  57. SourceData_ src;
  58. c11_string* name;
  59. c11_vector /*T=Bytecode*/ codes;
  60. c11_vector /*T=CodeObjectByteCodeEx*/ codes_ex;
  61. c11_vector /*T=py_TValue*/ consts; // constants
  62. c11_vector /*T=py_Name*/ varnames; // local variables
  63. c11_vector /*T=py_Name*/ names;
  64. int nlocals;
  65. c11_smallmap_n2d varnames_inv;
  66. c11_smallmap_n2d names_inv;
  67. c11_vector /*T=CodeBlock*/ blocks;
  68. c11_vector /*T=FuncDecl_*/ func_decls;
  69. int start_line;
  70. int end_line;
  71. } CodeObject;
  72. void CodeObject__ctor(CodeObject* self, SourceData_ src, c11_sv name);
  73. void CodeObject__dtor(CodeObject* self);
  74. int CodeObject__add_varname(CodeObject* self, py_Name name);
  75. int CodeObject__add_name(CodeObject* self, py_Name name);
  76. void CodeObject__gc_mark(const CodeObject* self, c11_vector* p_stack);
  77. typedef struct FuncDeclKwArg {
  78. int index; // index in co->varnames
  79. py_Name key; // name of this argument
  80. py_TValue value; // default value
  81. } FuncDeclKwArg;
  82. typedef struct FuncDecl {
  83. RefCounted rc;
  84. CodeObject code; // strong ref
  85. c11_vector /*T=int*/ args; // indices in co->varnames
  86. c11_vector /*T=KwArg*/ kwargs; // indices in co->varnames
  87. int starred_arg; // index in co->varnames, -1 if no *arg
  88. int starred_kwarg; // index in co->varnames, -1 if no **kwarg
  89. bool nested; // whether this function is nested
  90. const char* docstring; // docstring of this function (weak ref)
  91. FuncType type;
  92. c11_smallmap_n2d kw_to_index;
  93. } FuncDecl;
  94. typedef FuncDecl* FuncDecl_;
  95. FuncDecl_ FuncDecl__rcnew(SourceData_ src, c11_sv name);
  96. bool FuncDecl__is_duplicated_arg(const FuncDecl* self, py_Name name);
  97. void FuncDecl__add_arg(FuncDecl* self, py_Name name);
  98. void FuncDecl__add_kwarg(FuncDecl* self, py_Name name, const py_TValue* value);
  99. void FuncDecl__add_starred_arg(FuncDecl* self, py_Name name);
  100. void FuncDecl__add_starred_kwarg(FuncDecl* self, py_Name name);
  101. void FuncDecl__gc_mark(const FuncDecl* self, c11_vector* p_stack);
  102. // runtime function
  103. typedef struct Function {
  104. FuncDecl_ decl;
  105. py_GlobalRef module; // maybe NULL, weak ref
  106. py_Ref globals; // maybe NULL, strong ref
  107. NameDict* closure; // maybe NULL, strong ref
  108. PyObject* clazz; // weak ref; for super()
  109. py_CFunction cfunc; // wrapped C function; for decl-based binding
  110. } Function;
  111. void Function__ctor(Function* self, FuncDecl_ decl, py_GlobalRef module, py_Ref globals);
  112. void Function__dtor(Function* self);