phase-20.45: Logical(AND) PRIMARY fix

- lower_return_logical_box.hako: emit MIR v0 as JSON string (functions[]/main/blocks.id)
- runner_min: adopt lower.logical before binop/int
- lower_return_bool_box.hako: restrict to Return(expr=Bool) to avoid logical bleed
- add canaries: PRIMARY AND-only; update OR canary; all phase2044 quick PASS
This commit is contained in:
nyash-codex
2025-11-05 21:09:11 +09:00
parent 96ea3892af
commit 755bbb7742
4 changed files with 49 additions and 17 deletions

View File

@ -8,9 +8,13 @@ static box LowerReturnBoolBox {
local s = "" + program_json
local k_ret = s.indexOf("\"type\":\"Return\"")
if k_ret < 0 { return null }
// find Bool value true/false after Return
local k_bool = s.indexOf("\"type\":\"Bool\"", k_ret)
// Restrict to Return(expr=Bool …): require expr 開始直後に Bool が来る
// 例: "expr":{"type":"Bool","value":true}
local k_expr = s.indexOf("\"expr\":", k_ret)
if k_expr < 0 { return null }
local k_bool = s.indexOf("\"type\":\"Bool\"", k_expr)
if k_bool < 0 { return null }
// Ensure this Bool belongs to the same expr (次の '}' までに value があることを確認)
local k_val = s.indexOf("\"value\":", k_bool)
if k_val < 0 { return null }
local is_true = JsonFragBox.read_bool_after(s, k_val+8)

View File

@ -62,23 +62,19 @@ static box LowerReturnLogicalBox {
}
if rhs_true == null { return null }
// Build MIR: const r1=lhs, rT=1, rF=0; branch on lhs
// For &&: if lhs==0 → ret 0; else ret rhs
// For ||: if lhs==1 → ret 1; else ret rhs
local b0 = new ArrayBox()
b0.push(MirSchemaBox.inst_const(1, lhs_true))
b0.push(MirSchemaBox.inst_branch(1, 1, 2))
// Build MIR(JSON v0) string directly (functions[]/name="main"/blocks.id)
local json_head = "{\"functions\":[{\"name\":\"main\",\"params\":[],\"locals\":[],\"blocks\":["
local bb0 = "{\"id\":0,\"instructions\":[{\"op\":\"const\",\"dst\":1,\"value\":{\"type\":\"i64\",\"value\":" + lhs_true + "}},{\"op\":\"branch\",\"cond\":1,\"then\":1,\"else\":2}]}"
if op == "&&" {
local b1 = new ArrayBox(); b1.push(MirSchemaBox.inst_const(3, rhs_true)); b1.push(MirSchemaBox.inst_ret(3))
local b2 = new ArrayBox(); b2.push(MirSchemaBox.inst_const(4, 0)); b2.push(MirSchemaBox.inst_ret(4))
local blocks = new ArrayBox(); blocks.push(MirSchemaBox.block(0, b0)); blocks.push(MirSchemaBox.block(1, b1)); blocks.push(MirSchemaBox.block(2, b2))
return MirSchemaBox.module(MirSchemaBox.fn_main(blocks))
// then: ret rhs_true; else: ret 0
local bb1 = "{\"id\":1,\"instructions\":[{\"op\":\"const\",\"dst\":3,\"value\":{\"type\":\"i64\",\"value\":" + rhs_true + "}},{\"op\":\"ret\",\"value\":3}]}"
local bb2 = "{\"id\":2,\"instructions\":[{\"op\":\"const\",\"dst\":4,\"value\":{\"type\":\"i64\",\"value\":0}},{\"op\":\"ret\",\"value\":4}]}"
return json_head + bb0 + "," + bb1 + "," + bb2 + "]}]}"
} else {
local b1 = new ArrayBox(); b1.push(MirSchemaBox.inst_const(3, 1)); b1.push(MirSchemaBox.inst_ret(3))
local b2 = new ArrayBox(); b2.push(MirSchemaBox.inst_const(4, rhs_true)); b2.push(MirSchemaBox.inst_ret(4))
local blocks = new ArrayBox(); blocks.push(MirSchemaBox.block(0, b0)); blocks.push(MirSchemaBox.block(1, b1)); blocks.push(MirSchemaBox.block(2, b2))
return MirSchemaBox.module(MirSchemaBox.fn_main(blocks))
// then: ret 1; else: ret rhs_true
local bb1 = "{\"id\":1,\"instructions\":[{\"op\":\"const\",\"dst\":3,\"value\":{\"type\":\"i64\",\"value\":1}},{\"op\":\"ret\",\"value\":3}]}"
local bb2 = "{\"id\":2,\"instructions\":[{\"op\":\"const\",\"dst\":4,\"value\":{\"type\":\"i64\",\"value\":" + rhs_true + "}},{\"op\":\"ret\",\"value\":4}]}"
return json_head + bb0 + "," + bb1 + "," + bb2 + "]}]}"
}
}
}

View File

@ -13,6 +13,7 @@ using "hako.mir.builder.internal.lower_typeop_check" as LowerTypeOpCheckBox
using "hako.mir.builder.internal.lower_typeop_cast" as LowerTypeOpCastBox
using "hako.mir.builder.internal.lower_return_int" as LowerReturnIntBox
using "hako.mir.builder.internal.lower_return_binop" as LowerReturnBinOpBox
using "hako.mir.builder.internal.lower.logical" as LowerReturnLogicalBox
static box BuilderRunnerMinBox {
run(program_json) {
@ -44,6 +45,7 @@ static box BuilderRunnerMinBox {
{ local o = LowerTypeOpCheckBox.try_lower(s); if o != null { return o } }
{ local o = LowerTypeOpCastBox.try_lower(s); if o != null { return o } }
}
{ local o = LowerReturnLogicalBox.try_lower(s); if o != null { return o } }
{ local o = LowerReturnBinOpBox.try_lower(s); if o != null { return o } }
{ local o = LowerReturnIntBox.try_lower(s); if o != null { return o } }
{ local o = LowerNewboxConstructorBox.try_lower(s); if o != null { return o } }