codeobject.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. int line;
  19. uint16_t block; // the block id of this bytecode
  20. };
  21. _Str pad(const _Str& s, const int n){
  22. if(s.size() >= n) return s.substr(0, n);
  23. return s + std::string(n - s.size(), ' ');
  24. }
  25. enum CodeBlockType {
  26. NO_BLOCK,
  27. FOR_LOOP,
  28. WHILE_LOOP,
  29. CONTEXT_MANAGER,
  30. TRY_EXCEPT,
  31. };
  32. struct CodeBlock {
  33. CodeBlockType type;
  34. std::vector<int> id;
  35. int parent; // parent index in co_blocks
  36. int start; // start index of this block in co_code, inclusive
  37. int end; // end index of this block in co_code, exclusive
  38. std::string toString() const {
  39. if(parent == -1) return "";
  40. std::string s = "[";
  41. for(int i = 0; i < id.size(); i++){
  42. s += std::to_string(id[i]);
  43. if(i != id.size()-1) s += "-";
  44. }
  45. s += ": type=";
  46. s += std::to_string(type);
  47. s += "]";
  48. return s;
  49. }
  50. bool operator==(const std::vector<int>& other) const {
  51. return id == other;
  52. }
  53. bool operator!=(const std::vector<int>& other) const {
  54. return id != other;
  55. }
  56. int depth() const {
  57. return id.size();
  58. }
  59. };
  60. struct CodeObject {
  61. _Source src;
  62. _Str name;
  63. CodeObject(_Source src, _Str name) {
  64. this->src = src;
  65. this->name = name;
  66. }
  67. CompileMode mode() const {
  68. return src->mode;
  69. }
  70. std::vector<ByteCode> co_code;
  71. PyVarList co_consts;
  72. std::vector<std::pair<_Str, NameScope>> co_names;
  73. std::vector<_Str> co_global_names;
  74. std::vector<CodeBlock> co_blocks = { CodeBlock{NO_BLOCK, {}, -1} };
  75. // tmp variables
  76. int _currBlockIndex = 0;
  77. bool __isCurrBlockLoop() const {
  78. return co_blocks[_currBlockIndex].type == FOR_LOOP || co_blocks[_currBlockIndex].type == WHILE_LOOP;
  79. }
  80. void __enterBlock(CodeBlockType type){
  81. const CodeBlock& currBlock = co_blocks[_currBlockIndex];
  82. std::vector<int> copy(currBlock.id);
  83. copy.push_back(-1);
  84. int t = 0;
  85. while(true){
  86. copy[copy.size()-1] = t;
  87. auto it = std::find(co_blocks.begin(), co_blocks.end(), copy);
  88. if(it == co_blocks.end()) break;
  89. t++;
  90. }
  91. co_blocks.push_back(CodeBlock{type, copy, _currBlockIndex, (int)co_code.size()});
  92. _currBlockIndex = co_blocks.size()-1;
  93. }
  94. void __exitBlock(){
  95. co_blocks[_currBlockIndex].end = co_code.size();
  96. _currBlockIndex = co_blocks[_currBlockIndex].parent;
  97. if(_currBlockIndex < 0) UNREACHABLE();
  98. }
  99. // for goto use
  100. // goto/label should be put at toplevel statements
  101. emhash8::HashMap<_Str, int> co_labels;
  102. void addLabel(const _Str& label){
  103. if(co_labels.find(label) != co_labels.end()){
  104. _Str msg = "label '" + label + "' already exists";
  105. throw std::runtime_error(msg.c_str());
  106. }
  107. co_labels[label] = co_code.size();
  108. }
  109. int addName(_Str name, NameScope scope){
  110. if(scope == NAME_LOCAL && std::find(co_global_names.begin(), co_global_names.end(), name) != co_global_names.end()){
  111. scope = NAME_GLOBAL;
  112. }
  113. auto p = std::make_pair(name, scope);
  114. for(int i=0; i<co_names.size(); i++){
  115. if(co_names[i] == p) return i;
  116. }
  117. co_names.push_back(p);
  118. return co_names.size() - 1;
  119. }
  120. int addConst(PyVar v){
  121. co_consts.push_back(v);
  122. return co_consts.size() - 1;
  123. }
  124. void optimize_level_1(){
  125. for(int i=0; i<co_code.size(); i++){
  126. if(co_code[i].op >= OP_BINARY_OP && co_code[i].op <= OP_CONTAINS_OP){
  127. for(int j=0; j<2; j++){
  128. ByteCode& bc = co_code[i-j-1];
  129. if(bc.op >= OP_LOAD_CONST && bc.op <= OP_LOAD_NAME_REF){
  130. if(bc.op == OP_LOAD_NAME_REF){
  131. bc.op = OP_LOAD_NAME;
  132. }
  133. }else{
  134. break;
  135. }
  136. }
  137. }else if(co_code[i].op == OP_CALL){
  138. int ARGC = co_code[i].arg & 0xFFFF;
  139. int KWARGC = (co_code[i].arg >> 16) & 0xFFFF;
  140. if(KWARGC != 0) continue;
  141. for(int j=0; j<ARGC+1; j++){
  142. ByteCode& bc = co_code[i-j-1];
  143. if(bc.op >= OP_LOAD_CONST && bc.op <= OP_LOAD_NAME_REF){
  144. if(bc.op == OP_LOAD_NAME_REF){
  145. bc.op = OP_LOAD_NAME;
  146. }
  147. }else{
  148. break;
  149. }
  150. }
  151. }
  152. }
  153. }
  154. void optimize(int level=1){
  155. optimize_level_1();
  156. }
  157. };
  158. class Frame {
  159. private:
  160. std::vector<PyVar> s_data;
  161. int ip = -1;
  162. int next_ip = 0;
  163. public:
  164. const _Code code;
  165. PyVar _module;
  166. PyVarDict f_locals;
  167. inline PyVarDict copy_f_locals() const {
  168. return f_locals;
  169. }
  170. inline PyVarDict& f_globals(){
  171. return _module->attribs;
  172. }
  173. Frame(const _Code code, PyVar _module, PyVarDict&& locals)
  174. : code(code), _module(_module), f_locals(std::move(locals)) {
  175. }
  176. inline const ByteCode& next_bytecode() {
  177. ip = next_ip;
  178. next_ip = ip + 1;
  179. return code->co_code[ip];
  180. }
  181. _Str errorSnapshot(){
  182. int line = code->co_code[ip].line;
  183. return code->src->snapshot(line);
  184. }
  185. inline int stackSize() const {
  186. return s_data.size();
  187. }
  188. inline bool has_next_bytecode() const {
  189. return next_ip < code->co_code.size();
  190. }
  191. inline PyVar __pop(){
  192. if(s_data.empty()) throw std::runtime_error("s_data.empty() is true");
  193. PyVar v = std::move(s_data.back());
  194. s_data.pop_back();
  195. return v;
  196. }
  197. inline void try_deref(VM*, PyVar&);
  198. inline PyVar popValue(VM* vm){
  199. PyVar value = __pop();
  200. try_deref(vm, value);
  201. return value;
  202. }
  203. inline PyVar topValue(VM* vm){
  204. PyVar value = __top();
  205. try_deref(vm, value);
  206. return value;
  207. }
  208. inline PyVar& __top(){
  209. if(s_data.empty()) throw std::runtime_error("s_data.empty() is true");
  210. return s_data.back();
  211. }
  212. inline PyVar __topValueN(VM* vm, int n=-1){
  213. PyVar value = s_data[s_data.size() + n];
  214. try_deref(vm, value);
  215. return value;
  216. }
  217. template<typename T>
  218. inline void push(T&& obj){
  219. s_data.push_back(std::forward<T>(obj));
  220. }
  221. inline void jumpAbsolute(int i){
  222. next_ip = i;
  223. }
  224. void jumpAbsoluteSafe(int target){
  225. const ByteCode& prev = code->co_code[ip];
  226. int i = prev.block;
  227. next_ip = target;
  228. if(next_ip >= code->co_code.size()){
  229. while(i>=0){
  230. if(code->co_blocks[i].type == FOR_LOOP) __pop();
  231. i = code->co_blocks[i].parent;
  232. }
  233. }else{
  234. const ByteCode& next = code->co_code[target];
  235. while(i>=0 && i!=next.block){
  236. if(code->co_blocks[i].type == FOR_LOOP) __pop();
  237. i = code->co_blocks[i].parent;
  238. }
  239. if(i!=next.block) throw std::runtime_error(
  240. "invalid jump from " + code->co_blocks[prev.block].toString() + " to " + code->co_blocks[next.block].toString()
  241. );
  242. }
  243. }
  244. pkpy::ArgList popNValuesReversed(VM* vm, int n){
  245. int new_size = s_data.size() - n;
  246. if(new_size < 0) throw std::runtime_error("stackSize() < n");
  247. pkpy::ArgList v(n);
  248. for(int i=n-1; i>=0; i--){
  249. v._index(i) = std::move(s_data[new_size + i]);
  250. try_deref(vm, v._index(i));
  251. }
  252. s_data.resize(new_size);
  253. return v;
  254. }
  255. PyVarList popNValuesReversedUnlimited(VM* vm, int n){
  256. PyVarList v(n);
  257. for(int i=n-1; i>=0; i--) v[i] = popValue(vm);
  258. return v;
  259. }
  260. pkpy::ArgList __popNReversed(int n){
  261. pkpy::ArgList v(n);
  262. for(int i=n-1; i>=0; i--) v._index(i) = __pop();
  263. return v;
  264. }
  265. };