codeobject.h 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #pragma once
  2. #include "obj.h"
  3. #include "error.h"
  4. namespace pkpy{
  5. enum NameScope { NAME_LOCAL, NAME_GLOBAL };
  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. int line;
  21. };
  22. enum CodeBlockType {
  23. NO_BLOCK,
  24. FOR_LOOP,
  25. WHILE_LOOP,
  26. CONTEXT_MANAGER,
  27. TRY_EXCEPT,
  28. };
  29. #define BC_NOARG -1
  30. #define BC_KEEPLINE -1
  31. struct CodeBlock {
  32. CodeBlockType type;
  33. int parent; // parent index in blocks
  34. int start; // start index of this block in codes, inclusive
  35. int end; // end index of this block in codes, exclusive
  36. };
  37. struct CodeObject {
  38. shared_ptr<SourceData> src;
  39. Str name;
  40. bool is_generator = false;
  41. CodeObject(shared_ptr<SourceData> src, Str name) {
  42. this->src = src;
  43. this->name = name;
  44. }
  45. std::vector<Bytecode> codes;
  46. List consts;
  47. std::vector<StrName> names;
  48. std::set<Str> global_names;
  49. std::vector<CodeBlock> blocks = { CodeBlock{NO_BLOCK, -1} };
  50. std::map<StrName, int> labels;
  51. std::vector<FuncDecl_> func_decls;
  52. // may be.. just use a large NameDict?
  53. uint32_t perfect_locals_capacity = 2;
  54. uint32_t perfect_hash_seed = 0;
  55. void optimize(VM* vm);
  56. void _gc_mark() const {
  57. for(PyObject* v : consts) OBJ_MARK(v);
  58. for(auto& decl: func_decls) decl->_gc_mark();
  59. }
  60. };
  61. } // namespace pkpy