merge iblock

This commit is contained in:
blueloveTH 2024-05-20 12:47:20 +08:00
parent 4c2f475671
commit ff52c52349
3 changed files with 7 additions and 9 deletions

View File

@ -56,13 +56,13 @@ struct CodeObject {
struct LineInfo{ struct LineInfo{
int lineno; // line number for each bytecode int lineno; // line number for each bytecode
bool is_virtual; // whether this bytecode is virtual (not in source code) bool is_virtual; // whether this bytecode is virtual (not in source code)
int iblock; // block index
}; };
std::shared_ptr<SourceData> src; std::shared_ptr<SourceData> src;
Str name; Str name;
std::vector<Bytecode> codes; std::vector<Bytecode> codes;
std::vector<int> iblocks; // block index for each bytecode
std::vector<LineInfo> lines; std::vector<LineInfo> lines;
small_vector_2<PyVar, 8> consts; // constants small_vector_2<PyVar, 8> consts; // constants
@ -77,7 +77,7 @@ struct CodeObject {
int end_line; int end_line;
const CodeBlock& _get_block_codei(int codei) const{ 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); CodeObject(std::shared_ptr<SourceData> src, const Str& name);

View File

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

View File

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