codeobject.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. #ifdef __cplusplus
  11. extern "C" {
  12. #endif
  13. #define BC_NOARG 0
  14. #define BC_KEEPLINE -1
  15. typedef enum FuncType {
  16. FuncType_UNSET,
  17. FuncType_NORMAL,
  18. FuncType_SIMPLE,
  19. FuncType_GENERATOR,
  20. } FuncType;
  21. typedef enum NameScope {
  22. NAME_LOCAL,
  23. NAME_GLOBAL,
  24. NAME_GLOBAL_UNKNOWN,
  25. } NameScope;
  26. typedef enum CodeBlockType {
  27. CodeBlockType_NO_BLOCK,
  28. CodeBlockType_FOR_LOOP,
  29. CodeBlockType_WHILE_LOOP,
  30. CodeBlockType_CONTEXT_MANAGER,
  31. CodeBlockType_TRY_EXCEPT,
  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. int nlocals; // cached varnames.size()
  64. c11_smallmap_n2i varnames_inv;
  65. c11_smallmap_n2i labels;
  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. FuncDecl_ FuncDecl__build(c11_sv name,
  100. c11_sv* args,
  101. int argc,
  102. c11_sv starred_arg,
  103. c11_sv* kwargs,
  104. int kwargc,
  105. py_Ref kwdefaults, // a tuple contains default values
  106. c11_sv starred_kwarg,
  107. const char* docstring);
  108. // runtime function
  109. typedef struct Function {
  110. FuncDecl_ decl;
  111. py_TValue module; // weak ref
  112. PyObject* clazz; // weak ref
  113. NameDict* closure; // strong ref
  114. py_CFunction cfunc; // wrapped C function
  115. } Function;
  116. void Function__ctor(Function* self, FuncDecl_ decl, py_TValue* module);
  117. void Function__dtor(Function* self);
  118. #ifdef __cplusplus
  119. }
  120. #endif