fix(loop/loopform): 専用preheader作成でValueId定義エラー解消 (WIP)
**問題**: NYASH_LOOPFORM_PHI_V2=1 でValueId未定義エラー発生 - emit_preheader()が既存blockに命令追加 → forward reference - if-then内のloopでpreheader_id = current_block()が誤動作 **解決策**: LLVM canonical loop form準拠 - 専用preheaderブロック作成 (before_loop → preheader → header) - 変数スナップショットをnew_block()前に取得 - emit_jump(preheader)で明示的分離 **成果**: ✅ ValueId定義エラー完全解消 (ValueId(10) etc.) ✅ 詳細デバッグ出力追加 (variable_map追跡) ❌ 新しい型エラー発生 (要調査: variable_map不整合) **デバッグ出力**: - before_loop_id + variable_map size - Block IDs (preheader/header/body/latch/exit) - 変数ごとのparam判定 + iteration count **次のステップ**: variable_map変更点特定 - before_loop: args=ValueId(0) - prepare_structure: args=ValueId(1) ← なぜ変わる? 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@ -137,26 +137,46 @@ impl<'a> LoopBuilder<'a> {
|
||||
condition: ASTNode,
|
||||
body: Vec<ASTNode>,
|
||||
) -> Result<ValueId, String> {
|
||||
// Create loop structure blocks
|
||||
let preheader_id = self.current_block()?;
|
||||
// Create loop structure blocks following LLVM canonical form
|
||||
// We need a dedicated preheader block to materialize loop entry copies
|
||||
let before_loop_id = self.current_block()?;
|
||||
|
||||
// Capture variable snapshot BEFORE creating new blocks (at loop entry point)
|
||||
let current_vars = self.get_current_variable_map();
|
||||
|
||||
if std::env::var("NYASH_LOOPFORM_DEBUG").is_ok() {
|
||||
eprintln!("[loopform] before_loop_id={:?}, variable_map size={}",
|
||||
before_loop_id, current_vars.len());
|
||||
for (name, value) in ¤t_vars {
|
||||
eprintln!(" {} -> {:?}", name, value);
|
||||
}
|
||||
}
|
||||
|
||||
let preheader_id = self.new_block();
|
||||
let header_id = self.new_block();
|
||||
let body_id = self.new_block();
|
||||
let latch_id = self.new_block();
|
||||
let exit_id = self.new_block();
|
||||
|
||||
// Jump from current block to preheader
|
||||
self.emit_jump(preheader_id)?;
|
||||
|
||||
// Initialize LoopFormBuilder with preheader and header blocks
|
||||
let mut loopform = LoopFormBuilder::new(preheader_id, header_id);
|
||||
|
||||
// Capture current variable map snapshot at preheader
|
||||
let current_vars = self.get_current_variable_map();
|
||||
|
||||
// Pass 1: Prepare structure (allocate all ValueIds upfront)
|
||||
if std::env::var("NYASH_LOOPFORM_DEBUG").is_ok() {
|
||||
eprintln!("[loopform] variable_map at loop entry:");
|
||||
eprintln!("[loopform] Block IDs: preheader={:?}, header={:?}, body={:?}, latch={:?}, exit={:?}",
|
||||
preheader_id, header_id, body_id, latch_id, exit_id);
|
||||
eprintln!("[loopform] variable_map at loop entry (size={}):", current_vars.len());
|
||||
let mut loop_count = 0;
|
||||
for (name, value) in ¤t_vars {
|
||||
loop_count += 1;
|
||||
eprintln!(" [{}] {} -> {:?}", loop_count, name, value);
|
||||
let is_param = self.is_parameter(name);
|
||||
eprintln!(" {} -> {:?} (param={})", name, value, is_param);
|
||||
eprintln!(" param={}", is_param);
|
||||
}
|
||||
eprintln!("[loopform] iterated {} times", loop_count);
|
||||
}
|
||||
loopform.prepare_structure(self, ¤t_vars)?;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user