codeobject.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. NAME_GLOBAL_UNKNOWN,
  22. } NameScope;
  23. typedef enum CodeBlockType {
  24. CodeBlockType_NO_BLOCK,
  25. CodeBlockType_WHILE_LOOP,
  26. CodeBlockType_TRY,
  27. /* context blocks (stack-based) */
  28. CodeBlockType_FOR_LOOP,
  29. CodeBlockType_WITH,
  30. /* context blocks (flag-based) */
  31. CodeBlockType_EXCEPT,
  32. CodeBlockType_FINALLY,
  33. } CodeBlockType;
  34. typedef enum Opcode {
  35. #define OPCODE(name) OP_##name,
  36. #include "pocketpy/xmacros/opcodes.h"
  37. #undef OPCODE
  38. } Opcode;
  39. typedef struct Bytecode {
  40. uint8_t op;
  41. uint16_t arg;
  42. } Bytecode;
  43. void Bytecode__set_signed_arg(Bytecode* self, int arg);
  44. bool Bytecode__is_forward_jump(const Bytecode* self);
  45. typedef struct CodeBlock {
  46. CodeBlockType type;
  47. int parent; // parent index in blocks
  48. int start; // start index of this block in codes, inclusive
  49. int end; // end index of this block in codes, exclusive
  50. int end2; // ...
  51. } CodeBlock;
  52. typedef struct BytecodeEx {
  53. int lineno; // line number for each bytecode
  54. bool is_virtual; // whether this bytecode is virtual (not in source code)
  55. int iblock; // block index
  56. } BytecodeEx;
  57. typedef struct CodeObject {
  58. SourceData_ src;
  59. c11_string* name;
  60. c11_vector /*T=Bytecode*/ codes;
  61. c11_vector /*T=CodeObjectByteCodeEx*/ codes_ex;
  62. c11_vector /*T=py_TValue*/ consts; // constants
  63. c11_vector /*T=py_Name*/ varnames; // local variables
  64. int nlocals; // cached varnames.size()
  65. c11_smallmap_n2i varnames_inv;
  66. c11_vector /*T=CodeBlock*/ blocks;
  67. c11_vector /*T=FuncDecl_*/ func_decls;
  68. int start_line;
  69. int end_line;
  70. } CodeObject;
  71. void CodeObject__ctor(CodeObject* self, SourceData_ src, c11_sv name);
  72. void CodeObject__dtor(CodeObject* self);
  73. int CodeObject__add_varname(CodeObject* self, py_Name name);
  74. void CodeObject__gc_mark(const CodeObject* self);
  75. typedef struct FuncDeclKwArg {
  76. int index; // index in co->varnames
  77. uint16_t key; // name of this argument
  78. py_TValue value; // default value
  79. } FuncDeclKwArg;
  80. typedef struct FuncDecl {
  81. RefCounted rc;
  82. CodeObject code; // strong ref
  83. c11_vector /*T=int*/ args; // indices in co->varnames
  84. c11_vector /*T=KwArg*/ kwargs; // indices in co->varnames
  85. int starred_arg; // index in co->varnames, -1 if no *arg
  86. int starred_kwarg; // index in co->varnames, -1 if no **kwarg
  87. bool nested; // whether this function is nested
  88. const char* docstring; // docstring of this function (weak ref)
  89. FuncType type;
  90. c11_smallmap_n2i kw_to_index;
  91. } FuncDecl;
  92. typedef FuncDecl* FuncDecl_;
  93. FuncDecl_ FuncDecl__rcnew(SourceData_ src, c11_sv name);
  94. bool FuncDecl__is_duplicated_arg(const FuncDecl* self, py_Name name);
  95. void FuncDecl__add_arg(FuncDecl* self, py_Name name);
  96. void FuncDecl__add_kwarg(FuncDecl* self, py_Name name, const py_TValue* value);
  97. void FuncDecl__add_starred_arg(FuncDecl* self, py_Name name);
  98. void FuncDecl__add_starred_kwarg(FuncDecl* self, py_Name name);
  99. void FuncDecl__gc_mark(const FuncDecl* self);
  100. // runtime function
  101. typedef struct Function {
  102. FuncDecl_ decl;
  103. py_TValue module; // weak ref
  104. PyObject* clazz; // weak ref
  105. NameDict* closure; // strong ref
  106. py_CFunction cfunc; // wrapped C function
  107. } Function;
  108. void Function__ctor(Function* self, FuncDecl_ decl, py_TValue* module);
  109. void Function__dtor(Function* self);