codeobject.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. #define BC_NOARG -1
  29. #define 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. CodeBlock(CodeBlockType type, int parent, int for_loop_depth, int start):
  37. type(type), parent(parent), for_loop_depth(for_loop_depth), start(start), end(-1) {}
  38. };
  39. struct CodeObject {
  40. shared_ptr<SourceData> src;
  41. Str name;
  42. bool is_generator = false;
  43. CodeObject(shared_ptr<SourceData> src, const Str& name):
  44. src(src), name(name) {}
  45. std::vector<Bytecode> codes;
  46. std::vector<int> lines; // line number for each bytecode
  47. List consts;
  48. std::vector<StrName> varnames; // local variables
  49. NameDictInt varnames_inv;
  50. std::set<Str> global_names;
  51. std::vector<CodeBlock> blocks = { CodeBlock(NO_BLOCK, -1, 0, 0) };
  52. NameDictInt labels;
  53. std::vector<FuncDecl_> func_decls;
  54. void optimize(VM* vm);
  55. void _gc_mark() const {
  56. for(PyObject* v : consts) OBJ_MARK(v);
  57. for(auto& decl: func_decls) decl->_gc_mark();
  58. }
  59. };
  60. } // namespace pkpy