Files
hakorune/src/llvm_py/instructions/copy.py
nyash-codex 65763c1ed6 feat(llvm): Phase 97 - Call/PHI/Plugin強化 + リファクタリング準備
## 概要
LLVM backend のCall処理、PHI wiring、Plugin loader を強化。
次のリファクタリング(箱化モジュール化)のための準備も含む。

## 変更内容

### LLVM Call処理強化
- `mir_call/__init__.py`: Call ルーティングロジック改善
- `mir_call/global_call.py`: print処理の marshal強化
- `mir_call/method_call.py`: メソッド呼び出し処理改善
- `boxcall.py`: BoxCall処理改善

### PHI処理強化
- `phi_manager.py`: PHI管理改善
- `phi_wiring/wiring.py`: PHI配線ロジック強化(+17行)
- `phi_wiring/tagging.py`: Type tagging改善
- `resolver.py`: Value解決ロジック強化(+34行)

### Copy伝播
- `copy.py`: Copy命令のType tag伝播追加(+10行)

### Plugin loader強化
- `library.rs`: エラー出力改善、[plugin/missing]ログ追加(+34行)
- fail-fast強化

### テスト
- `phase97_json_loader_escape_llvm_exe.sh`: Phase 97 E2Eテスト追加
- `phase97_next_non_ws_llvm_exe.sh`: Phase 97 E2Eテスト追加

### その他
- `nyash_kernel/lib.rs`: Kernel側の改善(+23行)

## 統計
- 14ファイル変更
- +256行 / -53行 = +203 net

## 次のリファクタリング準備
以下の箇所がリファクタリング対象として識別済み:
1. Call ルーティング箱の明文化
2. print の marshal 箱
3. TypeFacts/Tagging 箱の一本化
4. PHI Snapshot 契約のSSOT
5. Plugin loader のエラー出力統合

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

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2025-12-17 03:51:03 +09:00

61 lines
2.3 KiB
Python

"""
Copy instruction lowering
MIR13 PHI-off uses explicit copies along edges/blocks to model merges.
"""
import llvmlite.ir as ir
from typing import Dict, Optional, Any
from utils.values import resolve_i64_strict, safe_vmap_write
def lower_copy(
builder: ir.IRBuilder,
dst: int,
src: int,
vmap: Dict[int, ir.Value],
resolver=None,
current_block=None,
preds=None,
block_end_values=None,
bb_map=None,
ctx: Optional[Any] = None,
):
"""Lower a copy by mapping dst to src value in the current block scope.
Prefer same-block SSA from vmap; fallback to resolver to preserve
dominance and to localize values across predecessors.
"""
# If BuildCtx is provided, prefer its maps for consistency.
if ctx is not None:
try:
if getattr(ctx, 'resolver', None) is not None:
resolver = ctx.resolver
if getattr(ctx, 'vmap', None) is not None and vmap is None:
vmap = ctx.vmap
if getattr(ctx, 'preds', None) is not None and preds is None:
preds = ctx.preds
if getattr(ctx, 'block_end_values', None) is not None and block_end_values is None:
block_end_values = ctx.block_end_values
if getattr(ctx, 'bb_map', None) is not None and bb_map is None:
bb_map = ctx.bb_map
except Exception:
pass
# Prefer local SSA; resolve otherwise to preserve dominance
val = resolve_i64_strict(resolver, src, current_block, preds, block_end_values, vmap, bb_map)
if val is None:
val = ir.Constant(ir.IntType(64), 0)
# Phase 131-12-P1: Object identity trace before write
import os, sys
if os.environ.get('NYASH_LLVM_VMAP_TRACE') == '1':
print(f"[vmap/id] copy dst={dst} src={src} vmap id={id(vmap)} before_write", file=sys.stderr)
safe_vmap_write(vmap, dst, val, "copy", resolver=resolver)
# TypeFacts propagation (SSOT): preserve "stringish" tagging across Copy.
# Many MIR patterns materialize a temp then Copy into a local; without this,
# string equality/concat may incorrectly fall back to integer/handle ops.
try:
if resolver is not None and hasattr(resolver, "is_stringish") and resolver.is_stringish(src):
if hasattr(resolver, "mark_string"):
resolver.mark_string(dst)
except Exception:
pass