blueloveTH 1 سال پیش
والد
کامیت
ff52c52349
3فایلهای تغییر یافته به همراه7 افزوده شده و 9 حذف شده
  1. 3 3
      include/pocketpy/codeobject.h
  2. 1 3
      src/expr.cpp
  3. 3 3
      src/frame.cpp

+ 3 - 3
include/pocketpy/codeobject.h

@@ -56,17 +56,17 @@ struct CodeObject {
     struct LineInfo{
         int lineno;             // line number for each bytecode
         bool is_virtual;        // whether this bytecode is virtual (not in source code)
+        int iblock;             // block index
     };
 
     std::shared_ptr<SourceData> src;
     Str name;
 
     std::vector<Bytecode> codes;
-    std::vector<int> iblocks;       // block index for each bytecode
     std::vector<LineInfo> lines;
     
     small_vector_2<PyVar, 8> consts;         // constants
-    small_vector_2<StrName, 8> varnames;         // local variables
+    small_vector_2<StrName, 8> varnames;     // local variables
 
     NameDictInt varnames_inv;
     std::vector<CodeBlock> blocks;
@@ -77,7 +77,7 @@ struct CodeObject {
     int end_line;
 
     const CodeBlock& _get_block_codei(int codei) const{
-        return blocks[iblocks[codei]];
+        return blocks[lines[codei].iblock];
     }
 
     CodeObject(std::shared_ptr<SourceData> src, const Str& name);

+ 1 - 3
src/expr.cpp

@@ -54,8 +54,7 @@ namespace pkpy{
 
     int CodeEmitContext::emit_(Opcode opcode, uint16_t arg, int line, bool is_virtual) {
         co->codes.push_back(Bytecode{(uint8_t)opcode, arg});
-        co->iblocks.push_back(curr_block_i);
-        co->lines.push_back(CodeObject::LineInfo{line, is_virtual});
+        co->lines.push_back(CodeObject::LineInfo{line, is_virtual, curr_block_i});
         int i = co->codes.size() - 1;
         if(line == BC_KEEPLINE){
             if(i >= 1) co->lines[i].lineno = co->lines[i-1].lineno;
@@ -66,7 +65,6 @@ namespace pkpy{
 
     void CodeEmitContext::revert_last_emit_(){
         co->codes.pop_back();
-        co->iblocks.pop_back();
         co->lines.pop_back();
     }
 

+ 3 - 3
src/frame.cpp

@@ -25,7 +25,7 @@ namespace pkpy{
 
     int Frame::prepare_jump_exception_handler(ValueStack* _s){
         // try to find a parent try block
-        int block = co->iblocks[ip()];
+        int block = co->lines[ip()].iblock;
         while(block >= 0){
             if(co->blocks[block].type == CodeBlockType::TRY_EXCEPT) break;
             block = co->blocks[block].parent;
@@ -47,7 +47,7 @@ namespace pkpy{
     }
 
     void Frame::prepare_jump_break(ValueStack* _s, int target){
-        int i = co->iblocks[ip()];
+        int i = co->lines[ip()].iblock;
         if(target >= co->codes.size()){
             while(i>=0) i = _exit_block(_s, i);
         }else{
@@ -56,7 +56,7 @@ namespace pkpy{
             //     _ = 0
             // # if there is no op here, the block check will fail
             // while i: --i
-            int next_block = co->iblocks[target];
+            int next_block = co->lines[target].iblock;
             while(i>=0 && i!=next_block) i = _exit_block(_s, i);
             if(i!=next_block) throw std::runtime_error("invalid jump");
         }