fix: if codegen bug

This commit is contained in:
Shunsuke Shibayama 2023-07-09 22:27:42 +09:00
parent 98c4b480b7
commit dc86be1125
2 changed files with 335 additions and 5 deletions

View file

@ -940,7 +940,7 @@ impl PyCodeGenerator {
/// 極力使わないこと
#[track_caller]
fn crash(&mut self, description: &str) -> ! {
if cfg!(feature = "debug") {
if cfg!(debug_assertions) || cfg!(feature = "debug") {
println!("current block: {}", self.cur_block());
panic!("internal error: {description}");
} else {
@ -1760,6 +1760,8 @@ impl PyCodeGenerator {
let cond = args.remove(0);
self.emit_expr(cond);
let idx_pop_jump_if_false = self.lasti();
self.write_instr(EXTENDED_ARG);
self.write_arg(0);
// Opcode310::POP_JUMP_IF_FALSE == Opcode311::POP_JUMP_FORWARD_IF_FALSE
self.write_instr(Opcode310::POP_JUMP_IF_FALSE);
// cannot detect where to jump to at this moment, so put as 0
@ -1775,7 +1777,9 @@ impl PyCodeGenerator {
}
}
if args.get(0).is_some() {
let mut idx_jump_forward = self.lasti();
let idx_jump_forward = self.lasti();
self.write_instr(EXTENDED_ARG);
self.write_arg(0);
self.write_instr(JUMP_FORWARD); // jump to end
self.write_arg(0);
// else block
@ -1784,7 +1788,7 @@ impl PyCodeGenerator {
} else {
self.lasti()
};
idx_jump_forward += self.calc_edit_jump(idx_pop_jump_if_false + 1, idx_else_begin);
self.fill_jump(idx_pop_jump_if_false + 1, idx_else_begin - 2);
match args.remove(0) {
Expr::Lambda(lambda) => {
// let params = self.gen_param_names(&lambda.params);
@ -1795,7 +1799,7 @@ impl PyCodeGenerator {
}
}
let idx_end = self.lasti();
self.calc_edit_jump(idx_jump_forward + 1, idx_end - idx_jump_forward - 2);
self.fill_jump(idx_jump_forward + 1, idx_end - idx_jump_forward - 2 - 1);
// FIXME: this is a hack to make sure the stack is balanced
while self.stack_len() != init_stack_len + 1 {
self.stack_dec();
@ -1809,7 +1813,7 @@ impl PyCodeGenerator {
} else {
self.lasti()
};
self.calc_edit_jump(idx_pop_jump_if_false + 1, idx_end);
self.fill_jump(idx_pop_jump_if_false + 1, idx_end - 2);
self.emit_load_const(ValueObj::None);
while self.stack_len() != init_stack_len + 1 {
self.stack_dec();

View file

@ -361,4 +361,330 @@ while! do! c > 0, do!:
print! c
c.dec!()
if! False:
do!:
x_1 = 1
x_2 = 1
x_3 = 1
x_4 = 1
x_5 = 1
x_6 = 1
x_7 = 1
x_8 = 1
x_9 = 1
x_10 = 1
x_11 = 1
x_12 = 1
x_13 = 1
x_14 = 1
x_15 = 1
x_16 = 1
x_17 = 1
x_18 = 1
x_19 = 1
x_20 = 1
x_21 = 1
x_22 = 1
x_23 = 1
x_24 = 1
x_25 = 1
x_26 = 1
x_27 = 1
x_28 = 1
x_29 = 1
x_30 = 1
x_31 = 1
x_32 = 1
x_33 = 1
x_34 = 1
x_35 = 1
x_36 = 1
x_37 = 1
x_38 = 1
x_39 = 1
x_40 = 1
x_41 = 1
x_42 = 1
x_43 = 1
x_44 = 1
x_45 = 1
x_46 = 1
x_47 = 1
x_48 = 1
x_49 = 1
x_50 = 1
x_51 = 1
x_52 = 1
x_53 = 1
x_54 = 1
x_55 = 1
x_56 = 1
x_57 = 1
x_58 = 1
x_59 = 1
x_60 = 1
x_61 = 1
x_62 = 1
x_63 = 1
x_64 = 1
x_65 = 1
x_66 = 1
x_67 = 1
x_68 = 1
x_69 = 1
x_70 = 1
print! x_1, x_2, x_3, x_4, x_5, x_6, x_7, x_8, x_9, x_10, x_11, x_12, x_13, x_14, x_15
print! x_16, x_17, x_18, x_19, x_20, x_21, x_22, x_23, x_24, x_25, x_26, x_27, x_28, x_29, x_30
print! x_31, x_32, x_33, x_34, x_35, x_36, x_37, x_38, x_39, x_40, x_41, x_42, x_43, x_44, x_45
print! x_46, x_47, x_48, x_49, x_50, x_51, x_52, x_53, x_54, x_55, x_56, x_57, x_58, x_59, x_60
print! x_61, x_62, x_63, x_64, x_65, x_66, x_67, x_68, x_69, x_70, True
do!:
x_1 = 1
x_2 = 1
x_3 = 1
x_4 = 1
x_5 = 1
x_6 = 1
x_7 = 1
x_8 = 1
x_9 = 1
x_10 = 1
x_11 = 1
x_12 = 1
x_13 = 1
x_14 = 1
x_15 = 1
x_16 = 1
x_17 = 1
x_18 = 1
x_19 = 1
x_20 = 1
x_21 = 1
x_22 = 1
x_23 = 1
x_24 = 1
x_25 = 1
x_26 = 1
x_27 = 1
x_28 = 1
x_29 = 1
x_30 = 1
x_31 = 1
x_32 = 1
x_33 = 1
x_34 = 1
x_35 = 1
x_36 = 1
x_37 = 1
x_38 = 1
x_39 = 1
x_40 = 1
x_41 = 1
x_42 = 1
x_43 = 1
x_44 = 1
x_45 = 1
x_46 = 1
x_47 = 1
x_48 = 1
x_49 = 1
x_50 = 1
x_51 = 1
x_52 = 1
x_53 = 1
x_54 = 1
x_55 = 1
x_56 = 1
x_57 = 1
x_58 = 1
x_59 = 1
x_60 = 1
x_61 = 1
x_62 = 1
x_63 = 1
x_64 = 1
x_65 = 1
x_66 = 1
x_67 = 1
x_68 = 1
x_69 = 1
x_70 = 1
print! x_1, x_2, x_3, x_4, x_5, x_6, x_7, x_8, x_9, x_10, x_11, x_12, x_13, x_14, x_15
print! x_16, x_17, x_18, x_19, x_20, x_21, x_22, x_23, x_24, x_25, x_26, x_27, x_28, x_29, x_30
print! x_31, x_32, x_33, x_34, x_35, x_36, x_37, x_38, x_39, x_40, x_41, x_42, x_43, x_44, x_45
print! x_46, x_47, x_48, x_49, x_50, x_51, x_52, x_53, x_54, x_55, x_56, x_57, x_58, x_59, x_60
print! x_61, x_62, x_63, x_64, x_65, x_66, x_67, x_68, x_69, x_70, False
if! False:
do!:
print! True
do!:
print! False
c1 = if True:
do 1
c2 = if False:
do 1
c3 = if True:
do:
x_1 = 1
x_2 = 1
x_3 = 1
x_4 = 1
x_5 = 1
x_6 = 1
x_7 = 1
x_8 = 1
x_9 = 1
x_10 = 1
x_11 = 1
x_12 = 1
x_13 = 1
x_14 = 1
x_15 = 1
x_16 = 1
x_17 = 1
x_18 = 1
x_19 = 1
x_20 = 1
x_21 = 1
x_22 = 1
x_23 = 1
x_24 = 1
x_25 = 1
x_26 = 1
x_27 = 1
x_28 = 1
x_29 = 1
x_30 = 1
x_31 = 1
x_32 = 1
x_33 = 1
x_34 = 1
x_35 = 1
x_36 = 1
x_37 = 1
x_38 = 1
x_39 = 1
x_40 = 1
x_41 = 1
x_42 = 1
x_43 = 1
x_44 = 1
x_45 = 1
x_46 = 1
x_47 = 1
x_48 = 1
x_49 = 1
x_50 = 1
x_51 = 1
x_52 = 1
x_53 = 1
x_54 = 1
x_55 = 1
x_56 = 1
x_57 = 1
x_58 = 1
x_59 = 1
x_60 = 1
x_61 = 1
x_62 = 1
x_63 = 1
x_64 = 1
x_65 = 1
x_66 = 1
x_67 = 1
x_68 = 1
x_69 = 1
x_70 = 1
log x_1, x_2, x_3, x_4, x_5, x_6, x_7, x_8, x_9, x_10, x_11, x_12, x_13, x_14, x_15
log x_16, x_17, x_18, x_19, x_20, x_21, x_22, x_23, x_24, x_25, x_26, x_27, x_28, x_29, x_30
log x_31, x_32, x_33, x_34, x_35, x_36, x_37, x_38, x_39, x_40, x_41, x_42, x_43, x_44, x_45
log x_46, x_47, x_48, x_49, x_50, x_51, x_52, x_53, x_54, x_55, x_56, x_57, x_58, x_59, x_60
log x_61, x_62, x_63, x_64, x_65, x_66, x_67, x_68, x_69, x_70
1
c4 = if False:
do:
x_1 = 1
x_2 = 1
x_3 = 1
x_4 = 1
x_5 = 1
x_6 = 1
x_7 = 1
x_8 = 1
x_9 = 1
x_10 = 1
x_11 = 1
x_12 = 1
x_13 = 1
x_14 = 1
x_15 = 1
x_16 = 1
x_17 = 1
x_18 = 1
x_19 = 1
x_20 = 1
x_21 = 1
x_22 = 1
x_23 = 1
x_24 = 1
x_25 = 1
x_26 = 1
x_27 = 1
x_28 = 1
x_29 = 1
x_30 = 1
x_31 = 1
x_32 = 1
x_33 = 1
x_34 = 1
x_35 = 1
x_36 = 1
x_37 = 1
x_38 = 1
x_39 = 1
x_40 = 1
x_41 = 1
x_42 = 1
x_43 = 1
x_44 = 1
x_45 = 1
x_46 = 1
x_47 = 1
x_48 = 1
x_49 = 1
x_50 = 1
x_51 = 1
x_52 = 1
x_53 = 1
x_54 = 1
x_55 = 1
x_56 = 1
x_57 = 1
x_58 = 1
x_59 = 1
x_60 = 1
x_61 = 1
x_62 = 1
x_63 = 1
x_64 = 1
x_65 = 1
x_66 = 1
x_67 = 1
x_68 = 1
x_69 = 1
x_70 = 1
log x_1, x_2, x_3, x_4, x_5, x_6, x_7, x_8, x_9, x_10, x_11, x_12, x_13, x_14, x_15
log x_16, x_17, x_18, x_19, x_20, x_21, x_22, x_23, x_24, x_25, x_26, x_27, x_28, x_29, x_30
log x_31, x_32, x_33, x_34, x_35, x_36, x_37, x_38, x_39, x_40, x_41, x_42, x_43, x_44, x_45
log x_46, x_47, x_48, x_49, x_50, x_51, x_52, x_53, x_54, x_55, x_56, x_57, x_58, x_59, x_60
log x_61, x_62, x_63, x_64, x_65, x_66, x_67, x_68, x_69, x_70
1
assert c1 == 1
assert c2 == None
assert c3 == 1
assert c4 == None
print! "done"