codeobject.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. #pragma once
  2. #include "obj.h"
  3. #include "pointer.h"
  4. #include "error.h"
  5. enum Opcode {
  6. #define OPCODE(name) OP_##name,
  7. #include "opcodes.h"
  8. #undef OPCODE
  9. };
  10. static const char* OP_NAMES[] = {
  11. #define OPCODE(name) #name,
  12. #include "opcodes.h"
  13. #undef OPCODE
  14. };
  15. struct ByteCode{
  16. uint8_t op;
  17. int arg;
  18. uint16_t line;
  19. };
  20. _Str pad(const _Str& s, const int n){
  21. return s + _Str(n - s.size(), ' ');
  22. }
  23. struct CodeObject {
  24. _Source src;
  25. _Str name;
  26. CodeObject(_Source src, _Str name, CompileMode mode=EXEC_MODE) {
  27. this->src = src;
  28. this->name = name;
  29. }
  30. std::vector<ByteCode> co_code;
  31. PyVarList co_consts;
  32. std::vector<std::shared_ptr<NamePointer>> co_names;
  33. std::vector<_Str> co_global_names;
  34. // for goto use
  35. // note: some opcodes moves the bytecode, such as listcomp
  36. // goto/label should be put at toplevel statements
  37. std::unordered_map<_Str, int> co_labels;
  38. void addLabel(const _Str& label){
  39. if(co_labels.find(label) != co_labels.end()){
  40. _Str msg = "label '" + label + "' already exists";
  41. throw std::runtime_error(msg.c_str());
  42. }
  43. co_labels[label] = co_code.size();
  44. }
  45. int addName(const _Str& name, NameScope scope){
  46. if(scope == NAME_LOCAL && std::find(co_global_names.begin(), co_global_names.end(), name) != co_global_names.end()){
  47. scope = NAME_GLOBAL;
  48. }
  49. auto p = std::make_shared<NamePointer>(name, scope);
  50. for(int i=0; i<co_names.size(); i++){
  51. if(*co_names[i] == *p) return i;
  52. }
  53. co_names.push_back(p);
  54. return co_names.size() - 1;
  55. }
  56. int addConst(PyVar v){
  57. co_consts.push_back(v);
  58. return co_consts.size() - 1;
  59. }
  60. void __moveToEnd(int start, int end){
  61. auto _start = co_code.begin() + start;
  62. auto _end = co_code.begin() + end;
  63. co_code.insert(co_code.end(), _start, _end);
  64. for(int i=start; i<end; i++) co_code[i].op = OP_NO_OP;
  65. }
  66. _Str toString(){
  67. _StrStream ss;
  68. int prev_line = -1;
  69. for(int i=0; i<co_code.size(); i++){
  70. const ByteCode& byte = co_code[i];
  71. if(byte.op == OP_NO_OP) continue;
  72. _Str line = std::to_string(byte.line);
  73. if(byte.line == prev_line) line = "";
  74. else{
  75. if(prev_line != -1) ss << "\n";
  76. prev_line = byte.line;
  77. }
  78. ss << pad(line, 12) << " " << pad(std::to_string(i), 3);
  79. ss << " " << pad(OP_NAMES[byte.op], 20) << " ";
  80. ss << (byte.arg == -1 ? "" : std::to_string(byte.arg));
  81. if(i != co_code.size() - 1) ss << '\n';
  82. }
  83. _StrStream consts;
  84. consts << "co_consts: ";
  85. for(int i=0; i<co_consts.size(); i++){
  86. consts << co_consts[i]->getTypeName();
  87. if(i != co_consts.size() - 1) consts << ", ";
  88. }
  89. _StrStream names;
  90. names << "co_names: ";
  91. for(int i=0; i<co_names.size(); i++){
  92. names << co_names[i]->name;
  93. if(i != co_names.size() - 1) names << ", ";
  94. }
  95. ss << '\n' << consts.str() << '\n' << names.str() << '\n';
  96. for(int i=0; i<co_consts.size(); i++){
  97. auto fn = std::get_if<_Func>(&co_consts[i]->_native);
  98. if(fn) ss << '\n' << (*fn)->code->name << ":\n" << (*fn)->code->toString();
  99. }
  100. return _Str(ss.str());
  101. }
  102. };
  103. class Frame {
  104. private:
  105. std::vector<PyVar> s_data;
  106. int ip = 0;
  107. std::stack<int> forLoops; // record the FOR_ITER bytecode index
  108. public:
  109. PyVar _module;
  110. PyVarDict f_locals;
  111. inline PyVarDict& f_globals(){
  112. return _module->attribs;
  113. }
  114. const CodeObject* code;
  115. Frame(const CodeObject* code, PyVar _module, const PyVarDict& locals)
  116. : code(code), _module(_module), f_locals(locals) {}
  117. inline const ByteCode& readCode() {
  118. return code->co_code[ip++];
  119. }
  120. _Str errorSnapshot(){
  121. int line = code->co_code[ip-1].line;
  122. return code->src->snapshot(line);
  123. }
  124. inline int stackSize() const {
  125. return s_data.size();
  126. }
  127. inline bool isCodeEnd() const {
  128. return ip >= code->co_code.size();
  129. }
  130. inline PyVar __pop(){
  131. if(s_data.empty()) throw std::runtime_error("s_data.empty() is true");
  132. PyVar v = std::move(s_data.back());
  133. s_data.pop_back();
  134. return v;
  135. }
  136. inline PyVar __deref_pointer(VM*, PyVar);
  137. inline PyVar popValue(VM* vm){
  138. return __deref_pointer(vm, __pop());
  139. }
  140. inline PyVar topValue(VM* vm){
  141. if(s_data.empty()) throw std::runtime_error("s_data.empty() is true");
  142. return __deref_pointer(vm, s_data.back());
  143. }
  144. inline PyVar& __top(){
  145. if(s_data.empty()) throw std::runtime_error("s_data.empty() is true");
  146. return s_data.back();
  147. }
  148. inline PyVar __topValueN(VM* vm, int n=-1){
  149. return __deref_pointer(vm, s_data[s_data.size() + n]);
  150. }
  151. inline void push(const PyVar& v){
  152. s_data.push_back(v);
  153. }
  154. inline void push(PyVar&& v){
  155. s_data.emplace_back(std::move(v));
  156. }
  157. void __reportForIter(){
  158. int lastIp = ip - 1;
  159. if(forLoops.empty()) forLoops.push(lastIp);
  160. else{
  161. if(forLoops.top() == lastIp) return;
  162. if(forLoops.top() < lastIp) forLoops.push(lastIp);
  163. else UNREACHABLE();
  164. }
  165. }
  166. inline void jump(int i){
  167. this->ip = i;
  168. }
  169. void safeJump(int i){
  170. this->ip = i;
  171. while(!forLoops.empty()){
  172. int start = forLoops.top();
  173. int end = code->co_code[start].arg;
  174. if(i < start || i >= end){
  175. //printf("%d <- [%d, %d)\n", i, start, end);
  176. __pop(); // pop the iterator
  177. forLoops.pop();
  178. }else{
  179. break;
  180. }
  181. }
  182. }
  183. PyVarList popNValuesReversed(VM* vm, int n){
  184. PyVarList v(n);
  185. for(int i=n-1; i>=0; i--) v[i] = std::move(popValue(vm));
  186. return v;
  187. }
  188. PyVarList __popNReversed(int n){
  189. PyVarList v(n);
  190. for(int i=n-1; i>=0; i--) v[i] = std::move(__pop());
  191. return v;
  192. }
  193. };