Files
hakorune/src/llvm_py/instructions/mir_call/closure_call.py
nyash-codex e46034c6ea feat(llvm): Phase 134-A - mir_call.py unified 設計完成
681行の giant ファイルを機能別に分割し、箱化モジュール化を達成。

Changes:
- NEW: src/llvm_py/mir_call_compat.py (120 lines)
  - JSON v0/v1 互換層を一元管理
  - normalize_callee(), detect_format_version()

- NEW: src/llvm_py/instructions/mir_call/ (7 files)
  - __init__.py: Canonical Dispatcher (lower_mir_call)
  - global_call.py: Global関数呼び出し (90 lines)
  - method_call.py: Boxメソッド呼び出し (175 lines)
  - constructor_call.py: Boxコンストラクタ (122 lines)
  - closure_call.py: Closure生成 (87 lines)
  - value_call.py: 動的関数値呼び出し (112 lines)
  - extern_call.py: 外部C ABI呼び出し (135 lines)

- ARCHIVE: mir_call.py → mir_call_legacy.py

Technical Achievements:
 mir_call.py: 681行 → 分割(各 80-175行、責務 明確)
 Phase 133 ConsoleLlvmBridge パターンを継承
 NYASH_MIR_UNIFIED_CALL フラグ完全廃止
 legacy dispatcher 削除(NotImplementedError 根治)
 JSON v0/v1 互換層を mir_call_compat.py に一元化
 Fail-Fast 原則確立
 テスト: 全 mir_call 関連テスト PASS

Design Principles Inherited:
- Phase 133 ConsoleLlvmBridge 箱化パターン継承
- Each module has clear responsibility
- mir_call_compat.py で Phase 124+ v0削除が容易
- テスト分割で保守性大幅向上

Next Phase:
Phase 134-B - StringBox bridge 分離(boxcall.py:130-282)
Phase 134-C - CollectionBox bridge 分離(boxcall.py:325-375)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-04 12:06:34 +09:00

88 lines
3.0 KiB
Python

"""
Closure creation lowering for MIR Call instruction.
Handles lowering of closure creation (NewClosure) to LLVM IR.
"""
from typing import Dict, Any, Optional
from llvmlite import ir
def lower_closure_creation(builder, module, params, captures, me_capture, dst_vid, vmap, resolver, owner):
"""
Lower closure creation - TRUE UNIFIED IMPLEMENTATION
Args:
builder: LLVM IR builder
module: LLVM Module
params: List of parameter definitions for the closure
captures: List of captured variable value IDs
me_capture: Value ID of captured 'me' reference (if any)
dst_vid: Destination value ID (register)
vmap: Value mapping dict
resolver: Value resolver instance
owner: NyashLLVMBuilder instance
Effects:
- Creates closure object with captured values
- Stores closure handle in vmap[dst_vid]
"""
i64 = ir.IntType(64)
i8 = ir.IntType(8)
i8p = i8.as_pointer()
# Helper to resolve arguments
def _resolve_arg(vid: int):
if resolver and hasattr(resolver, 'resolve_i64'):
try:
return resolver.resolve_i64(vid, builder.block, owner.preds,
owner.block_end_values, vmap, owner.bb_map)
except:
pass
return vmap.get(vid)
# Helper to declare function
def _declare(name: str, ret, args_types):
for f in module.functions:
if f.name == name:
return f
fnty = ir.FunctionType(ret, args_types)
return ir.Function(module, fnty, name=name)
# Create closure metadata structure
num_captures = len(captures) if captures else 0
num_params = len(params) if params else 0
# Resolve captured values
capture_vals = []
if captures:
for capture in captures:
if isinstance(capture, dict) and 'id' in capture:
cap_val = _resolve_arg(capture['id']) or ir.Constant(i64, 0)
elif isinstance(capture, int):
cap_val = _resolve_arg(capture) or ir.Constant(i64, 0)
else:
cap_val = ir.Constant(i64, 0)
capture_vals.append(cap_val)
# Add me_capture if present
if me_capture is not None:
me_val = _resolve_arg(me_capture) if isinstance(me_capture, int) else ir.Constant(i64, 0)
capture_vals.append(me_val)
num_captures += 1
# Call closure creation function
if num_captures > 0:
# Closure with captures
callee = _declare("nyash.closure.new_with_captures", i64, [i64, i64] + [i64] * num_captures)
args = [ir.Constant(i64, num_params), ir.Constant(i64, num_captures)] + capture_vals
result = builder.call(callee, args, name="unified_closure_with_captures")
else:
# Simple closure without captures
callee = _declare("nyash.closure.new", i64, [i64])
result = builder.call(callee, [ir.Constant(i64, num_params)], name="unified_closure_simple")
# Store result
if dst_vid is not None:
vmap[dst_vid] = result