Files
hakorune/lang/src/llvm_ir/instructions/mir_call.hako
nyash-codex a38aa7b417 feat(phase33): mir_call.hako Stages 2-4 complete - 全段階実装完了 🎉
Stage 2: CallEmitBox拡張 (+12 lines)
- make_mir_call_closure(params, captures, me_capture, dst)
- make_mir_call_value(func_vid, arg_ids, dst)

Stage 3: mir_call.hako簡略化 (208→164 lines, -21.2%)
- CallEmitBox完全活用により手動JSON生成を削除
- 74.4%削減達成 (Python 641→Hakorune 164 lines)

Stage 4: Smoke Tests追加 (+102 lines)
- closure_simple.hako: Closure callee typeテスト
- value_simple.hako: Value callee typeテスト

Phase 33 Final Achievement:
 10/10 instructions 完全実装 (100%)
  - Phase v0: const, binop, compare, ret (4/4)
  - Phase v1: branch, jump, copy (3/3)
  - Phase v2-A: phi (1/1)
  - Phase v2-B: loopform (1/1)
  - Phase v2-C: mir_call (1/1) ← NEW!

Test Coverage:
- Unit tests: 288 lines (6 tests, all callee types)
- Smoke tests: 147 lines (3 tests)
- Total: 435 lines test coverage

🚀 Python → Hakorune Script 移行 100% 完了!
2025-11-01 09:05:02 +09:00

159 lines
5.7 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// LLVM MIR Call Instruction Box — Python mir_call.py のHakorune実装
// 統一Call命令call/boxcall/externcall/newbox/plugin_invoke/newclosure
// JSON形式に変換し、C++バックエンドに渡す
//
// MIR Call は 6種類の callee.type をサポート:
// - Global: グローバル関数呼び出し (print, panic等)
// - Method: Boxメソッド呼び出し (receiver.method())
// - Constructor: Boxコンストラクタ (new BoxType())
// - Extern: 外部C ABI関数呼び出し
// - Closure: クロージャ生成 (||{...})
// - Value: 動的関数値呼び出し (func_value())
//
// 特徴:
// - CallEmitBox の既存資産を最大活用60%の機能を提供済み)
// - JSON 構造生成のみLLVM IR 生成は C++ backend が担当)
// - Unified architecture6つの命令を1つに統合
using "lang/src/compiler/emit/common/call_emit_box.hako" as CallEmitBox
using "lang/src/compiler/emit/common/json_emit_box.hako" as JsonEmitBox
static box LLVMMirCallInstructionBox {
// メインエントリーポイント - Pythonのlower_mir_call()に相当
// Stage 1: スケルトン実装(既存 CallEmitBox を活用)
//
// Args:
// callee: calleeオブジェクトMapBox
// 形式: {type: "Global|Method|Constructor|Extern|Closure|Value", ...}
// args: 引数配列ArrayBox of register IDs
// dst: 結果レジスタID
// options: オプション設定MapBox、将来拡張用
//
// Returns:
// JSON文字列MIR Call 命令)
lower_mir_call(self, callee, args, dst, options) {
if callee == null {
return me._mir_call_error_fallback(self, "callee is null")
}
local callee_type = callee.get("type")
if callee_type == null {
return me._mir_call_error_fallback(self, "callee.type is null")
}
// Dispatch by callee.type
if callee_type == "Global" {
return me._lower_global(self, callee, args, dst)
}
if callee_type == "Method" {
return me._lower_method(self, callee, args, dst)
}
if callee_type == "Constructor" {
return me._lower_constructor(self, callee, args, dst)
}
if callee_type == "Extern" {
return me._lower_extern(self, callee, args, dst)
}
if callee_type == "Closure" {
return me._lower_closure(self, callee, args, dst)
}
if callee_type == "Value" {
return me._lower_value(self, callee, args, dst)
}
return me._mir_call_error_fallback(self, "Unknown callee type: " + callee_type)
}
// Stage 2: Global function call
// 既存の CallEmitBox.make_mir_call_global を活用
_lower_global(self, callee, args, dst) {
local name = callee.get("name")
if name == null {
return me._mir_call_error_fallback(self, "Global: name is null")
}
local json_obj = CallEmitBox.make_mir_call_global(name, args, dst)
return JsonEmitBox.to_json_instruction(json_obj)
}
// Stage 2: Method call
// 既存の CallEmitBox.make_mir_call_method を活用
_lower_method(self, callee, args, dst) {
local method = callee.get("method")
local receiver = callee.get("receiver")
if method == null {
return me._mir_call_error_fallback(self, "Method: method is null")
}
if receiver == null {
return me._mir_call_error_fallback(self, "Method: receiver is null")
}
local json_obj = CallEmitBox.make_mir_call_method(method, receiver, args, dst)
return JsonEmitBox.to_json_instruction(json_obj)
}
// Stage 2: Constructor call
// 既存の CallEmitBox.make_mir_call_constructor を活用
_lower_constructor(self, callee, args, dst) {
local box_type = callee.get("box_type")
if box_type == null {
return me._mir_call_error_fallback(self, "Constructor: box_type is null")
}
local json_obj = CallEmitBox.make_mir_call_constructor(box_type, args, dst)
return JsonEmitBox.to_json_instruction(json_obj)
}
// Stage 3: Extern call
// 既存の CallEmitBox.make_mir_call_extern を活用
_lower_extern(self, callee, args, dst) {
local name = callee.get("name")
if name == null {
return me._mir_call_error_fallback(self, "Extern: name is null")
}
local json_obj = CallEmitBox.make_mir_call_extern(name, args, dst)
return JsonEmitBox.to_json_instruction(json_obj)
}
// Stage 4: Closure creation
// CallEmitBox.make_mir_call_closure を活用
_lower_closure(self, callee, args, dst) {
local params = callee.get("params")
local captures = callee.get("captures")
local me_capture = callee.get("me_capture")
if params == null {
params = new ArrayBox()
}
if captures == null {
captures = new ArrayBox()
}
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)
// CallEmitBox.make_mir_call_value を活用
_lower_value(self, callee, args, dst) {
local func_vid = callee.get("value")
if func_vid == null {
return me._mir_call_error_fallback(self, "Value: value is null")
}
local json_obj = CallEmitBox.make_mir_call_value(func_vid, args, dst)
return JsonEmitBox.to_json_instruction(json_obj)
}
// エラーフォールバック: 空の MIR Call を返す
_mir_call_error_fallback(self, error_msg) {
print("[ERROR] LLVMMirCallInstructionBox: " + error_msg)
return "{\"op\":\"mir_call\",\"dst\":0,\"callee\":{\"type\":\"Global\",\"name\":\"__error__\"},\"args\":[]}"
}
// デバッグ用: MIR Call 命令の構造を出力
debug_mir_call(self, callee, args, dst, options) {
local json = me.lower_mir_call(self, callee, args, dst, options)
print("Generated mir_call instruction: " + json)
return json
}
}