codeobject.h 6.3 KB

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