codeobject.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. #pragma once
  2. #include "obj.h"
  3. #include "ref.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;
  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. int parent; // parent index in blocks
  35. int start; // start index of this block in codes, inclusive
  36. int end; // end index of this block in codes, exclusive
  37. std::string to_string() const {
  38. if(parent == -1) return "";
  39. return "[B:" + std::to_string(type) + "]";
  40. }
  41. };
  42. struct CodeObject {
  43. pkpy::shared_ptr<SourceData> src;
  44. Str name;
  45. CodeObject(pkpy::shared_ptr<SourceData> src, Str name) {
  46. this->src = src;
  47. this->name = name;
  48. }
  49. std::vector<Bytecode> codes;
  50. pkpy::List consts;
  51. std::vector<std::pair<Str, NameScope>> names;
  52. emhash8::HashMap<Str, int> global_names;
  53. std::vector<CodeBlock> blocks = { CodeBlock{NO_BLOCK, -1} };
  54. emhash8::HashMap<Str, int> labels;
  55. bool add_label(const Str& label){
  56. if(labels.contains(label)) return false;
  57. labels[label] = codes.size();
  58. return true;
  59. }
  60. int add_name(Str name, NameScope scope){
  61. if(scope == NAME_LOCAL && global_names.contains(name)) scope = NAME_GLOBAL;
  62. auto p = std::make_pair(name, scope);
  63. for(int i=0; i<names.size(); i++){
  64. if(names[i] == p) return i;
  65. }
  66. names.push_back(p);
  67. return names.size() - 1;
  68. }
  69. int add_const(PyVar v){
  70. consts.push_back(v);
  71. return consts.size() - 1;
  72. }
  73. void optimize(){
  74. for(int i=0; i<codes.size(); i++){}
  75. }
  76. /************************************************/
  77. int _curr_block_i = 0;
  78. bool _rvalue = false;
  79. bool _is_curr_block_loop() const {
  80. return blocks[_curr_block_i].type == FOR_LOOP || blocks[_curr_block_i].type == WHILE_LOOP;
  81. }
  82. void _enter_block(CodeBlockType type){
  83. blocks.push_back(CodeBlock{type, _curr_block_i, (int)codes.size()});
  84. _curr_block_i = blocks.size()-1;
  85. }
  86. void _exit_block(){
  87. blocks[_curr_block_i].end = codes.size();
  88. _curr_block_i = blocks[_curr_block_i].parent;
  89. if(_curr_block_i < 0) UNREACHABLE();
  90. }
  91. /************************************************/
  92. };
  93. static thread_local i64 kFrameGlobalId = 0;
  94. struct Frame {
  95. std::vector<PyVar> _data;
  96. int _ip = -1;
  97. int _next_ip = 0;
  98. const CodeObject_ co;
  99. PyVar _module;
  100. pkpy::shared_ptr<pkpy::NameDict> _locals;
  101. const i64 id;
  102. std::stack<std::pair<int, std::vector<PyVar>>> s_try_block;
  103. inline pkpy::NameDict& f_locals() noexcept { return *_locals; }
  104. inline pkpy::NameDict& f_globals() noexcept { return _module->attribs; }
  105. Frame(const CodeObject_ co, PyVar _module, pkpy::shared_ptr<pkpy::NameDict> _locals)
  106. : co(co), _module(_module), _locals(_locals), id(kFrameGlobalId++) { }
  107. inline const Bytecode& next_bytecode() {
  108. _ip = _next_ip;
  109. _next_ip = _ip + 1;
  110. return co->codes[_ip];
  111. }
  112. Str snapshot(){
  113. int line = co->codes[_ip].line;
  114. return co->src->snapshot(line);
  115. }
  116. Str stack_info(){
  117. _StrStream ss;
  118. ss << "[";
  119. for(int i=0; i<_data.size(); i++){
  120. ss << OBJ_TP_NAME(_data[i]);
  121. if(i != _data.size()-1) ss << ", ";
  122. }
  123. ss << "]";
  124. return ss.str();
  125. }
  126. inline bool has_next_bytecode() const {
  127. return _next_ip < co->codes.size();
  128. }
  129. inline PyVar pop(){
  130. if(_data.empty()) throw std::runtime_error("_data.empty() is true");
  131. PyVar v = std::move(_data.back());
  132. _data.pop_back();
  133. return v;
  134. }
  135. inline void _pop(){
  136. if(_data.empty()) throw std::runtime_error("_data.empty() is true");
  137. _data.pop_back();
  138. }
  139. inline void try_deref(VM*, PyVar&);
  140. inline PyVar pop_value(VM* vm){
  141. PyVar value = pop();
  142. try_deref(vm, value);
  143. return value;
  144. }
  145. inline PyVar top_value(VM* vm){
  146. PyVar value = top();
  147. try_deref(vm, value);
  148. return value;
  149. }
  150. inline PyVar& top(){
  151. if(_data.empty()) throw std::runtime_error("_data.empty() is true");
  152. return _data.back();
  153. }
  154. inline PyVar top_value_offset(VM* vm, int n){
  155. PyVar value = _data[_data.size() + n];
  156. try_deref(vm, value);
  157. return value;
  158. }
  159. template<typename T>
  160. inline void push(T&& obj){ _data.push_back(std::forward<T>(obj)); }
  161. inline void jump_abs(int i){ _next_ip = i; }
  162. inline void jump_rel(int i){ _next_ip += i; }
  163. inline void on_try_block_enter(){
  164. s_try_block.push(std::make_pair(co->codes[_ip].block, _data));
  165. }
  166. inline void on_try_block_exit(){
  167. s_try_block.pop();
  168. }
  169. bool jump_to_exception_handler(){
  170. if(s_try_block.empty()) return false;
  171. PyVar obj = pop();
  172. auto& p = s_try_block.top();
  173. _data = std::move(p.second);
  174. _data.push_back(obj);
  175. _next_ip = co->blocks[p.first].end;
  176. on_try_block_exit();
  177. return true;
  178. }
  179. void jump_abs_safe(int target){
  180. const Bytecode& prev = co->codes[_ip];
  181. int i = prev.block;
  182. _next_ip = target;
  183. if(_next_ip >= co->codes.size()){
  184. while(i>=0){
  185. if(co->blocks[i].type == FOR_LOOP) pop();
  186. else if(co->blocks[i].type == TRY_EXCEPT) on_try_block_exit();
  187. i = co->blocks[i].parent;
  188. }
  189. }else{
  190. const Bytecode& next = co->codes[target];
  191. while(i>=0 && i!=next.block){
  192. if(co->blocks[i].type == FOR_LOOP) pop();
  193. else if(co->blocks[i].type == TRY_EXCEPT) on_try_block_exit();
  194. i = co->blocks[i].parent;
  195. }
  196. if(i!=next.block) throw std::runtime_error("invalid jump");
  197. }
  198. }
  199. pkpy::Args pop_n_values_reversed(VM* vm, int n){
  200. pkpy::Args v(n);
  201. for(int i=n-1; i>=0; i--){
  202. v[i] = pop();
  203. try_deref(vm, v[i]);
  204. }
  205. return v;
  206. }
  207. pkpy::Args pop_n_reversed(int n){
  208. pkpy::Args v(n);
  209. for(int i=n-1; i>=0; i--) v[i] = pop();
  210. return v;
  211. }
  212. };