diff --git a/docs/private b/docs/private index 63f53bbd..a5d43baa 160000 --- a/docs/private +++ b/docs/private @@ -1 +1 @@ -Subproject commit 63f53bbde9220ecb8f9830ebb5f5761fa0348624 +Subproject commit a5d43baaaf5f7b40c240c29a44756689b5dc7c2f diff --git a/lang/src/compiler/emit/common/call_emit_box.hako b/lang/src/compiler/emit/common/call_emit_box.hako index 0a15d049..a32bb150 100644 --- a/lang/src/compiler/emit/common/call_emit_box.hako +++ b/lang/src/compiler/emit/common/call_emit_box.hako @@ -41,6 +41,19 @@ static box CallEmitBox { return {op: "mir_call", dst: dst, callee: callee, args: arg_ids} } + make_mir_call_closure(params, captures, me_capture, dst) { + local callee = {type: "Closure", params: params, captures: captures} + if me_capture != null { + callee.me_capture = me_capture + } + return {op: "mir_call", dst: dst, callee: callee, args: []} + } + + make_mir_call_value(func_vid, arg_ids, dst) { + local callee = {type: "Value", value: func_vid} + return {op: "mir_call", dst: dst, callee: callee, args: arg_ids} + } + _canonical_module_name(name, arity) { if name == null { return "" } if name.indexOf("/") >= 0 { return name } diff --git a/lang/src/llvm_ir/instructions/mir_call.hako b/lang/src/llvm_ir/instructions/mir_call.hako index 80de76e9..d9df4657 100644 --- a/lang/src/llvm_ir/instructions/mir_call.hako +++ b/lang/src/llvm_ir/instructions/mir_call.hako @@ -114,71 +114,33 @@ static box LLVMMirCallInstructionBox { } // Stage 4: Closure creation - // TODO: CallEmitBox に make_mir_call_closure を追加する必要あり + // CallEmitBox.make_mir_call_closure を活用 _lower_closure(self, callee, args, dst) { - // Placeholder: 将来実装 local params = callee.get("params") local captures = callee.get("captures") + local me_capture = callee.get("me_capture") - local json = "{" - json = json + "\"op\":\"mir_call\"" - json = json + ",\"dst\":" + dst.toString() - json = json + ",\"callee\":{\"type\":\"Closure\"" - - // params 配列 - if params != null { - json = json + ",\"params\":" + me._array_to_json(self, params) + if params == null { + params = new ArrayBox() + } + if captures == null { + captures = new ArrayBox() } - // captures 配列 - if captures != null { - json = json + ",\"captures\":" + me._array_to_json(self, captures) - } - - json = json + "}" - json = json + ",\"args\":" + me._array_to_json(self, args) - json = json + "}" - - return json + local json_obj = CallEmitBox.make_mir_call_closure(params, captures, me_capture, dst) + return JsonEmitBox.to_json_instruction(json_obj) } // Stage 4: Value call (dynamic function value) - // TODO: CallEmitBox に make_mir_call_value を追加する必要あり + // CallEmitBox.make_mir_call_value を活用 _lower_value(self, callee, args, dst) { - // Placeholder: 将来実装 local func_vid = callee.get("value") if func_vid == null { return me._mir_call_error_fallback(self, "Value: value is null") } - local json = "{" - json = json + "\"op\":\"mir_call\"" - json = json + ",\"dst\":" + dst.toString() - json = json + ",\"callee\":{\"type\":\"Value\",\"value\":" + func_vid.toString() + "}" - json = json + ",\"args\":" + me._array_to_json(self, args) - json = json + "}" - - return json - } - - // Helper: ArrayBox を JSON 配列に変換 - _array_to_json(self, arr) { - if arr == null { - return "[]" - } - local json = "[" - local size = arr.size() - local i = 0 - loop(i < size) { - if i > 0 { - json = json + "," - } - local val = arr.get(i) - json = json + val.toString() - i = i + 1 - } - json = json + "]" - return json + local json_obj = CallEmitBox.make_mir_call_value(func_vid, args, dst) + return JsonEmitBox.to_json_instruction(json_obj) } // エラーフォールバック: 空の MIR Call を返す diff --git a/tests/phase33/smoke/mir_call/closure_simple.hako b/tests/phase33/smoke/mir_call/closure_simple.hako new file mode 100644 index 00000000..4cb33b66 --- /dev/null +++ b/tests/phase33/smoke/mir_call/closure_simple.hako @@ -0,0 +1,58 @@ +// MIR Call Smoke Test 2: Closure creation +// Tests Closure callee type + +using "lang/src/llvm_ir/instructions/mir_call.hako" as MirCallInst + +static box Main { + main() { + print("=== MIR Call Smoke Test 2: closure_simple ===") + + // Simulate: |x, y| { x + y } + local callee = new MapBox() + callee.set("type", "Closure") + + local params = new ArrayBox() + params.push(1) // param x + params.push(2) // param y + callee.set("params", params) + + local captures = new ArrayBox() + captures.push(3) // captured variable + callee.set("captures", captures) + + local args = new ArrayBox() + + // Generate mir_call JSON + local json = MirCallInst.lower_mir_call(null, callee, args, 4, null) + + // Verify JSON structure + local has_mir_call = json.indexOf("\"op\":\"mir_call\"") + if has_mir_call < 0 { + print("ERROR: op not found") + return 1 + } + + local has_closure = json.indexOf("\"type\":\"Closure\"") + if has_closure < 0 { + print("ERROR: Closure type not found") + return 1 + } + + local has_params = json.indexOf("\"params\"") + if has_params < 0 { + print("ERROR: params not found") + return 1 + } + + local has_captures = json.indexOf("\"captures\"") + if has_captures < 0 { + print("ERROR: captures not found") + return 1 + } + + print("✓ PASS: closure_simple mir_call generated") + print("Note: Actual execution requires C++ backend implementation") + + return 0 + } +} diff --git a/tests/phase33/smoke/mir_call/value_simple.hako b/tests/phase33/smoke/mir_call/value_simple.hako new file mode 100644 index 00000000..bfd709ff --- /dev/null +++ b/tests/phase33/smoke/mir_call/value_simple.hako @@ -0,0 +1,46 @@ +// MIR Call Smoke Test 3: Value call (dynamic function) +// Tests Value callee type + +using "lang/src/llvm_ir/instructions/mir_call.hako" as MirCallInst + +static box Main { + main() { + print("=== MIR Call Smoke Test 3: value_simple ===") + + // Simulate: func_value(arg1, arg2) + local callee = new MapBox() + callee.set("type", "Value") + callee.set("value", 5) // register holding function value + + local args = new ArrayBox() + args.push(6) // arg1 + args.push(7) // arg2 + + // Generate mir_call JSON + local json = MirCallInst.lower_mir_call(null, callee, args, 8, null) + + // Verify JSON structure + local has_mir_call = json.indexOf("\"op\":\"mir_call\"") + if has_mir_call < 0 { + print("ERROR: op not found") + return 1 + } + + local has_value = json.indexOf("\"type\":\"Value\"") + if has_value < 0 { + print("ERROR: Value type not found") + return 1 + } + + local has_value_id = json.indexOf("\"value\":5") + if has_value_id < 0 { + print("ERROR: value ID not found") + return 1 + } + + print("✓ PASS: value_simple mir_call generated") + print("Note: Actual execution requires C++ backend implementation") + + return 0 + } +}