codeobject.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #pragma once
  2. #include "obj.h"
  3. #include "pointer.h"
  4. enum Opcode {
  5. #define OPCODE(name) OP_##name,
  6. #include "opcodes.h"
  7. #undef OPCODE
  8. };
  9. static const char* OP_NAMES[] = {
  10. #define OPCODE(name) #name,
  11. #include "opcodes.h"
  12. #undef OPCODE
  13. };
  14. struct ByteCode{
  15. uint8_t op;
  16. int arg;
  17. uint16_t line;
  18. };
  19. _Str pad(const _Str& s, const int n){
  20. return s + _Str(n - s.size(), ' ');
  21. }
  22. enum CompileMode {
  23. EXEC_MODE,
  24. EVAL_MODE,
  25. SINGLE_MODE
  26. };
  27. struct CodeObject {
  28. CompileMode mode = EXEC_MODE;
  29. std::vector<ByteCode> co_code;
  30. _Str co_filename;
  31. _Str co_name;
  32. PyVarList co_consts;
  33. std::vector<std::shared_ptr<NamePointer>> co_names;
  34. int addName(const _Str& name, NameScope scope){
  35. auto p = std::make_shared<NamePointer>(name, scope);
  36. for(int i=0; i<co_names.size(); i++){
  37. if(*co_names[i] == *p) return i;
  38. }
  39. co_names.push_back(p);
  40. return co_names.size() - 1;
  41. }
  42. int addConst(PyVar v){
  43. co_consts.push_back(v);
  44. return co_consts.size() - 1;
  45. }
  46. void __moveToEnd(int start, int end){
  47. auto _start = co_code.begin() + start;
  48. auto _end = co_code.begin() + end;
  49. co_code.insert(co_code.end(), _start, _end);
  50. for(int i=start; i<end; i++) co_code[i].op = OP_NO_OP;
  51. }
  52. _Str toString(){
  53. _StrStream ss;
  54. int prev_line = -1;
  55. for(int i=0; i<co_code.size(); i++){
  56. const ByteCode& byte = co_code[i];
  57. if(byte.op == OP_NO_OP) continue;
  58. _Str line = std::to_string(byte.line);
  59. if(byte.line == prev_line) line = "";
  60. else{
  61. if(prev_line != -1) ss << "\n";
  62. prev_line = byte.line;
  63. }
  64. ss << pad(line, 12) << " " << pad(std::to_string(i), 3);
  65. ss << " " << pad(OP_NAMES[byte.op], 20) << " ";
  66. ss << (byte.arg == -1 ? "" : std::to_string(byte.arg));
  67. if(i != co_code.size() - 1) ss << '\n';
  68. }
  69. _StrStream consts;
  70. consts << "co_consts: ";
  71. for(int i=0; i<co_consts.size(); i++){
  72. consts << co_consts[i]->getTypeName();
  73. if(i != co_consts.size() - 1) consts << ", ";
  74. }
  75. _StrStream names;
  76. names << "co_names: ";
  77. for(int i=0; i<co_names.size(); i++){
  78. names << co_names[i]->name;
  79. if(i != co_names.size() - 1) names << ", ";
  80. }
  81. ss << '\n' << consts.str() << '\n' << names.str() << '\n';
  82. for(int i=0; i<co_consts.size(); i++){
  83. auto fn = std::get_if<_Func>(&co_consts[i]->_native);
  84. if(fn) ss << '\n' << fn->code->co_name << ":\n" << fn->code->toString();
  85. }
  86. return _Str(ss);
  87. }
  88. };
  89. class Frame {
  90. private:
  91. std::vector<PyVar> s_data;
  92. int ip = 0;
  93. public:
  94. PyVarDict* f_globals;
  95. PyVarDict f_locals;
  96. const CodeObject* code;
  97. Frame(const CodeObject* code, PyVarDict locals, PyVarDict* globals)
  98. : code(code), f_locals(locals), f_globals(globals) {}
  99. inline const ByteCode& readCode() {
  100. return code->co_code[ip++];
  101. }
  102. int currentLine(){
  103. if(isEnd()) return -1;
  104. return code->co_code[ip].line;
  105. }
  106. int stackSize() const {
  107. return s_data.size();
  108. }
  109. inline bool isEnd() const {
  110. return ip >= code->co_code.size();
  111. }
  112. inline PyVar __pop(){
  113. PyVar v = s_data.back();
  114. s_data.pop_back();
  115. return v;
  116. }
  117. inline PyVar __deref_pointer(VM*, PyVar);
  118. inline PyVar popValue(VM* vm){
  119. return __deref_pointer(vm, __pop());
  120. }
  121. inline PyVar topValue(VM* vm){
  122. return __deref_pointer(vm, s_data.back());
  123. }
  124. inline PyVar topNValue(VM* vm, int n=-1){
  125. return __deref_pointer(vm, s_data[s_data.size() + n]);
  126. }
  127. inline void push(PyVar v){
  128. s_data.push_back(v);
  129. }
  130. inline void jumpTo(int i){
  131. this->ip = i;
  132. }
  133. PyVarList popNValuesReversed(VM* vm, int n){
  134. PyVarList v(n);
  135. for(int i=n-1; i>=0; i--) v[i] = popValue(vm);
  136. return v;
  137. }
  138. PyVarList __popNReversed(int n){
  139. PyVarList v(n);
  140. for(int i=n-1; i>=0; i--) v[i] = __pop();
  141. return v;
  142. }
  143. };