diff --git a/lang/src/mir/builder/internal/lower_return_bool_box.hako b/lang/src/mir/builder/internal/lower_return_bool_box.hako index b85da136..daf72f05 100644 --- a/lang/src/mir/builder/internal/lower_return_bool_box.hako +++ b/lang/src/mir/builder/internal/lower_return_bool_box.hako @@ -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) diff --git a/lang/src/mir/builder/internal/lower_return_logical_box.hako b/lang/src/mir/builder/internal/lower_return_logical_box.hako index 35f1dda8..792d71d7 100644 --- a/lang/src/mir/builder/internal/lower_return_logical_box.hako +++ b/lang/src/mir/builder/internal/lower_return_logical_box.hako @@ -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 + "]}]}" } } } diff --git a/lang/src/mir/builder/internal/runner_min_box.hako b/lang/src/mir/builder/internal/runner_min_box.hako index 6b83ae11..202bd330 100644 --- a/lang/src/mir/builder/internal/runner_min_box.hako +++ b/lang/src/mir/builder/internal/runner_min_box.hako @@ -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 } } diff --git a/tools/smokes/v2/profiles/quick/core/phase2044/hako_primary_no_fallback_return_logical_and_only_core_exec_canary_vm.sh b/tools/smokes/v2/profiles/quick/core/phase2044/hako_primary_no_fallback_return_logical_and_only_core_exec_canary_vm.sh new file mode 100644 index 00000000..db9ca285 --- /dev/null +++ b/tools/smokes/v2/profiles/quick/core/phase2044/hako_primary_no_fallback_return_logical_and_only_core_exec_canary_vm.sh @@ -0,0 +1,30 @@ +#!/usr/bin/env bash +set -euo pipefail + +SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"; if ROOT_GIT=$(git -C "$SCRIPT_DIR" rev-parse --show-toplevel 2>/dev/null); then ROOT="$ROOT_GIT"; else ROOT="$(cd "$SCRIPT_DIR/../../../../../../../../.." && pwd)"; fi +source "$ROOT/tools/smokes/v2/lib/test_runner.sh"; require_env || exit 2 + +prog_json_path="/tmp/prog_2044_return_logical_and_only_$$.json" +cat >"$prog_json_path" <<'JSON' +{"version":0,"kind":"Program","body":[ + {"type":"Return","expr":{"type":"Logical","op":"&&","lhs":{"type":"Bool","value":true},"rhs":{"type":"Bool","value":false}}} +]} +JSON + +set +e +HAKO_PRIMARY_NO_FALLBACK=1 \ +HAKO_MIR_BUILDER_INTERNAL=1 \ +NYASH_ENABLE_USING=1 HAKO_ENABLE_USING=1 \ +NYASH_USING_AST=1 NYASH_RESOLVE_FIX_BRACES=1 \ +NYASH_DISABLE_NY_COMPILER=1 NYASH_PARSER_STAGE3=1 HAKO_PARSER_STAGE3=1 \ +NYASH_ENTRY_ALLOW_TOPLEVEL_MAIN=1 \ +verify_program_via_builder_to_core "$prog_json_path" +rc=$? +set -e +rm -f "$prog_json_path" +if [ "$rc" -ne 0 ]; then + echo "[FAIL] Return(Logical AND only) rc=$rc (expected 0)" >&2; exit 1 +fi + +echo "[PASS] phase2044/hako_primary_no_fallback_return_logical_and_only_core_exec_canary_vm" +exit 0