From d16fabeecfaf80f49c177efca453ddc7b6370785 Mon Sep 17 00:00:00 2001
From: MilkBlock <mineral@gmail.com>
Date: Mon, 13 May 2024 01:09:23 +0800
Subject: [PATCH] revision of dug because the revision of cfg_node
 op_jump_instr

---
 demo_expr.nhwc           | 22 ++++++++++--------
 demo_fibonacci.nhwc      | 36 ++++++++++++++---------------
 src/toolkit/gen_dug.rs   | 18 ++-------------
 src/toolkit/gen_ssa.rs   | 49 ++++++++++++++++++++++++++++------------
 src/toolkit/simulator.rs | 10 ++++----
 5 files changed, 73 insertions(+), 62 deletions(-)

diff --git a/demo_expr.nhwc b/demo_expr.nhwc
index 29885f0..ce19937 100644
--- a/demo_expr.nhwc
+++ b/demo_expr.nhwc
@@ -1,10 +1,14 @@
  0    Define main _s0 [] -> main_ret _s0 
-         1    Alloc i32 %temp_0_s2 _i0 
-         2    temp_0_s2 _i1 = Add i32 2 _s0, 3 _s0 
-         3    Alloc i32 %temp_1_s2 _i0 
-         4    temp_1_s2 _i1 = Mul i32 4 _s0, temp_0_s2 _i1 
-         5    Alloc i32 %temp_2_s2 _i0 
-         6    temp_2_s2 _i1 = Add i32 temp_1_s2 _i1, 5 _s0 
-         7    Alloc i32 %a_s2 _i0 = temp_2_s2 _i1 
-         8    breakpoint m _s4 ! 
-         9    ret a_s2 _i0 
+         2    alloc i32 temp_0 _s2 
+         5    alloc i32 temp_1 _s2 
+         8    alloc i32 temp_2 _s2 
+         11   alloc i32 a _s2 
+         1    new_var i32 %temp_00 
+         3    temp_01 = Add i32 2 _s0, 3 _s0 
+         4    new_var i32 %temp_10 
+         6    temp_11 = Mul i32 4 _s0, temp_01 
+         7    new_var i32 %temp_20 
+         9    temp_21 = Add i32 temp_11, 5 _s0 
+         10   new_var i32 %a0 = temp_21 
+         12   breakpoint m _s4 ! 
+         13   ret a0 
diff --git a/demo_fibonacci.nhwc b/demo_fibonacci.nhwc
index ca76a0b..7a8ce31 100644
--- a/demo_fibonacci.nhwc
+++ b/demo_fibonacci.nhwc
@@ -1,9 +1,9 @@
  1    Define main _s0 [] -> main_ret _s0 
          33   alloc i32 temp_7 _s12 
          36   alloc i32 a _s12 
-         32   new_var i32 %temp_7 _s12 
-         34   temp_7 _s12 =  Call i32 Fibo _s0(6 _s0) 
-         35   new_var i32 %a _s12 = temp_7 _s12 
+         32   new_var i32 %temp_70 
+         34   temp_71 =  Call i32 Fibo _s0(6 _s0) 
+         35   new_var i32 %a0 = temp_71 
          37   ret 0 _s0 
  0    Define Fibo _s0 [n0] -> Fibo_ret _s0 
          3    alloc i32 temp_0 _s7 
@@ -15,24 +15,24 @@
          29   alloc i1 temp_6 _s7 
          22   new_var i1 %temp_50 
          24   temp_51 = icmp i1 Eq n0, 0 _s0 
-         25   br i1 temp_5 _s4, label branch.true: _s5, label branch.false: _s5 
+         25   br i1 temp_51, label branch.true: _s5, label branch.false: _s5 
      20   label branch.true: _s5: 
          19   ret 0 _s0 
      21   label branch.false: _s5: 
-         28   new_var i1 %temp_6 _s7 
-         30   temp_6 _s7 = icmp i1 Eq n _s1, 1 _s0 
-         31   br i1 temp_6 _s7, label branch.true: _s8, label branch.false: _s8 
+         28   new_var i1 %temp_60 
+         30   temp_61 = icmp i1 Eq n0, 1 _s0 
+         31   br i1 temp_61, label branch.true: _s8, label branch.false: _s8 
      26   label branch.true: _s8: 
          18   ret 1 _s0 
      27   label branch.false: _s8: 
-         2    new_var i32 %temp_0 _s7 
-         4    temp_0 _s7 = Sub i32 n _s1, 2 _s0 
-         5    new_var i32 %temp_1 _s7 
-         7    temp_1 _s7 =  Call i32 Fibo _s0(temp_0 _s7) 
-         8    new_var i32 %temp_2 _s7 
-         10   temp_2 _s7 = Sub i32 n _s1, 1 _s0 
-         11   new_var i32 %temp_3 _s7 
-         13   temp_3 _s7 =  Call i32 Fibo _s0(temp_2 _s7) 
-         14   new_var i32 %temp_4 _s7 
-         16   temp_4 _s7 = Add i32 temp_3 _s7, temp_1 _s7 
-         17   ret temp_4 _s7 
+         2    new_var i32 %temp_00 
+         4    temp_01 = Sub i32 n0, 2 _s0 
+         5    new_var i32 %temp_10 
+         7    temp_11 =  Call i32 Fibo _s0(temp_01) 
+         8    new_var i32 %temp_20 
+         10   temp_21 = Sub i32 n0, 1 _s0 
+         11   new_var i32 %temp_30 
+         13   temp_31 =  Call i32 Fibo _s0(temp_21) 
+         14   new_var i32 %temp_40 
+         16   temp_41 = Add i32 temp_31, temp_11 
+         17   ret temp_41 
diff --git a/src/toolkit/gen_dug.rs b/src/toolkit/gen_dug.rs
index 26f04a6..9d972ba 100644
--- a/src/toolkit/gen_dug.rs
+++ b/src/toolkit/gen_dug.rs
@@ -90,25 +90,11 @@ pub fn get_cor_br_instr_of_phi_instr(cfg_graph:&CfgGraph,instr_slab:&InstrSlab,
     let phi_cfg_node  = phi_instr_struct.get_cfg_instr_idx()?.cfg_node;
     if node!(at phi_cfg_node in cfg_graph).cfg_node_type.is_gather(){
         let cfg_branch_node = find_branch_of_gather_upwnward(phi_cfg_node, cfg_graph)?;
-        Ok(get_br_instr_of_cfg_node(cfg_graph, instr_slab, cfg_branch_node, symtab)?)
+        Ok(node!(at cfg_branch_node in cfg_graph).op_jump_instr.ok_or(anyhow!("这个 cfg_node:{} 没有 jump_instr ",cfg_branch_node))?)
     }else if node!(at phi_cfg_node in cfg_graph).cfg_node_type.is_while_loop(){
-        Ok(get_br_instr_of_cfg_node(cfg_graph, instr_slab, phi_cfg_node, symtab)?)
+        Ok(node!(at phi_cfg_node in cfg_graph).op_jump_instr.ok_or(anyhow!("这个 cfg_node:{} 没有 jump_instr ",phi_cfg_node))?)
     }else{
         Err(anyhow!("这个 phi instr {:?} 没有对应的 jump_det ",phi_instr_struct))
     }
 
 }
-
-pub fn get_br_instr_of_cfg_node(cfg_graph:&CfgGraph,instr_slab:&InstrSlab, cfg_node:u32, symtab:&SymTab)-> Result<usize>{
-    let instrs = &node!(at cfg_node in cfg_graph).instrs;
-    // 这个instr 可能是 br 也有可能是直接 jump ,更有可能是空
-    if instrs.len()==0{
-        Err(anyhow!("无法获取此cfg_node:{} 的 br_instr 因为它其中没有任何instr",cfg_node))?
-    }
-    let br_instr = instrs[instrs.len()-1];
-    if instr!(at br_instr in instr_slab)?.instr_type.is_br(){
-        Ok(br_instr)
-    }else{
-        Err(anyhow!("这个 cfg_node:{} 的instr:{} 不是 jump_instr ",cfg_node,br_instr))
-    }
-}
\ No newline at end of file
diff --git a/src/toolkit/gen_ssa.rs b/src/toolkit/gen_ssa.rs
index 6ac2c3d..8871b94 100644
--- a/src/toolkit/gen_ssa.rs
+++ b/src/toolkit/gen_ssa.rs
@@ -4,7 +4,7 @@ use crate::{add_field, add_symbol, debug_info_green, debug_info_yellow, direct_c
 use itertools::{Itertools};
 
 use crate::toolkit::symtab::{SymTabEdge,SymTabGraph};
-use super::{cfg_node::{CfgGraph, CfgInstrIdx, InstrList}, context::DjGraph, def_use_node::DefUseGraph, etc, nhwc_instr::{InstrSlab, InstrType, PhiPair}, symbol::Symbol, symtab::{SymIdx, SymTab}};
+use super::{cfg_node::{CfgGraph, CfgInstrIdx, InCfgNodeInstrPos, InstrList}, context::DjGraph, def_use_node::DefUseGraph, etc, nhwc_instr::{InstrSlab, InstrType, PhiPair}, symbol::Symbol, symtab::{SymIdx, SymTab}};
 use anyhow::{anyhow, Result, Context};
 
 
@@ -222,7 +222,7 @@ pub fn find_recent_dom_instr_before(check_phi_instrs:bool,cfg_node:u32,symidx:&S
     {
         let instrs = &node!(at cfg_node in cfg_graph).instrs;
         if let Some(idx) = find_first_def_in_instr_vec(instrs, &symidx, instr_slab, Ordering::Less,Some(cfg_instr_pos))?{
-            return Ok(Some(CfgInstrIdx::new(cfg_node,idx, false)))
+            return Ok(Some(CfgInstrIdx::new(cfg_node,InCfgNodeInstrPos::InInstrs { instr_pos: idx })))
         }
     }
     // 如果在 这个instruction 的 def_vec 找到了想要找的符号,那么就直接 return 
@@ -230,7 +230,7 @@ pub fn find_recent_dom_instr_before(check_phi_instrs:bool,cfg_node:u32,symidx:&S
     if check_phi_instrs{
         let phi_instrs = &node!(at cfg_node in cfg_graph).phi_instrs;
         if let Some(idx) = find_first_def_in_instr_vec(phi_instrs, &symidx, instr_slab, Ordering::Less,None)?{
-            return Ok(Some(CfgInstrIdx::new(cfg_node,idx, true)))
+            return Ok(Some(CfgInstrIdx::new(cfg_node,InCfgNodeInstrPos::InPhi { phi_instr_pos: idx } )))
         }
     }
     // 检查所有 domiance node 
@@ -249,13 +249,13 @@ pub fn find_recent_dom_instr_before(check_phi_instrs:bool,cfg_node:u32,symidx:&S
                 {
                     let instrs = &node!(at domiance_cfg_node in cfg_graph).instrs;
                     if let Some(idx) = find_first_def_in_instr_vec(instrs, &symidx, instr_slab, Ordering::Less,None)?{
-                        return Ok(Some(CfgInstrIdx::new(domiance_cfg_node,idx, false)))
+                        return Ok(Some(CfgInstrIdx::new(domiance_cfg_node,InCfgNodeInstrPos::InInstrs { instr_pos: idx })))
                     }
                 }
                 if check_phi_instrs{
                     let phi_instrs = &node!(at cfg_node in cfg_graph).phi_instrs;
                     if let Some(idx) = find_first_def_in_instr_vec(phi_instrs, &symidx, instr_slab, Ordering::Less,None)?{
-                        return Ok(Some(CfgInstrIdx::new(domiance_cfg_node,idx, true)))
+                        return Ok(Some(CfgInstrIdx::new(domiance_cfg_node,InCfgNodeInstrPos::InPhi { phi_instr_pos: idx })))
                     }
                 }
             },
@@ -323,11 +323,24 @@ pub fn instr_is_dominated_by(instr1:usize, instr2:usize, cfg_graph:&CfgGraph, dj
     let cfg_instr_idx2 = instr_slab.get_instr(instr2)?.get_cfg_instr_idx()?;
     if cfg_instr_idx1.cfg_node == cfg_instr_idx2.cfg_node {
         // 如果处于同一个cfg_node ,那么考虑 是否是 phi instr
-        match (cfg_instr_idx1.is_in_phi,cfg_instr_idx2.is_in_phi){
-            (true, true) => Ok(cfg_instr_idx1.instr_pos > cfg_instr_idx2.instr_pos),
-            (true, false) => Ok(false),
-            (false, true) => Ok(true),
-            (false, false) =>Ok(cfg_instr_idx1.instr_pos > cfg_instr_idx2.instr_pos),
+        match (&cfg_instr_idx1.in_cfg_instr_pos,&cfg_instr_idx2.in_cfg_instr_pos){
+            (InCfgNodeInstrPos::InPhi { phi_instr_pos:phi_instr_pos1 }, InCfgNodeInstrPos::InPhi { phi_instr_pos:phi_instr_pos2 }) => Ok(phi_instr_pos1> phi_instr_pos2),
+            (InCfgNodeInstrPos::InPhi { phi_instr_pos }, InCfgNodeInstrPos::InInstrs { instr_pos}) => Ok(false),
+            (InCfgNodeInstrPos::InInstrs { instr_pos}, InCfgNodeInstrPos::InPhi { phi_instr_pos }) => Ok(true),
+            (InCfgNodeInstrPos::InInstrs { instr_pos:instr_pos1 }, InCfgNodeInstrPos::InInstrs { instr_pos:instr_pos2 }) =>Ok(instr_pos1 > instr_pos2),
+            (InCfgNodeInstrPos::InPhi { phi_instr_pos }, InCfgNodeInstrPos::InLabel {  }) => Ok(true),
+            (InCfgNodeInstrPos::InInstrs { instr_pos }, InCfgNodeInstrPos::InLabel {  }) => Ok(true),
+            (InCfgNodeInstrPos::InPhi { phi_instr_pos }, InCfgNodeInstrPos::InJump {  }) => Ok(false),
+            (InCfgNodeInstrPos::InInstrs { instr_pos }, InCfgNodeInstrPos::InJump {  }) => Ok(false),
+            (InCfgNodeInstrPos::InLabel {  }, InCfgNodeInstrPos::InPhi { phi_instr_pos }) => Ok(false),
+            (InCfgNodeInstrPos::InLabel {  }, InCfgNodeInstrPos::InInstrs { instr_pos }) => Ok(false),
+            (InCfgNodeInstrPos::InLabel {  }, InCfgNodeInstrPos::InLabel {  }) => Ok(false),
+            (InCfgNodeInstrPos::InLabel {  }, InCfgNodeInstrPos::InJump {  }) => Ok(false),
+            (InCfgNodeInstrPos::InJump {  }, InCfgNodeInstrPos::InPhi { phi_instr_pos }) => Ok(true),
+            (InCfgNodeInstrPos::InJump {  }, InCfgNodeInstrPos::InInstrs { instr_pos }) => Ok(true),
+            (InCfgNodeInstrPos::InJump {  }, InCfgNodeInstrPos::InLabel {  }) => Ok(true),
+            (InCfgNodeInstrPos::InJump {  }, InCfgNodeInstrPos::InJump {  }) => Ok(true),
+            // _ => Err(anyhow!("instr_is_dominated_by 只讨论 普通Instr 和 Phi_instr 之间的支配关系,无法识别 instr1:{} instr2:{}",instr1,instr2))
         }
     }else{
         // 如果不是同一个cfg_node ,只需要判断 whether cfg_node1 is dominated by cfg_node2
@@ -381,9 +394,15 @@ pub fn refresh_cfg_instr_idx_in_cfg_graph(cfg_graph:&mut CfgGraph,symtab:&SymTab
 pub fn refresh_cfg_instr_idx_in_cfg_node(cfg_graph:&mut CfgGraph,cfg_node:u32,symtab:&SymTab,instr_slab:&mut InstrSlab)->Result<()>{
     refresh_cfg_instr_idx_in_cfg_node_instrs(cfg_graph, cfg_node, symtab, instr_slab)?;
     refresh_cfg_instr_idx_in_cfg_node_phi_instrs(cfg_graph, cfg_node, symtab, instr_slab)?;
+    for &instr in node_mut!(at cfg_node in cfg_graph).op_label_instr.iter() {
+        instr_slab.get_mut_instr(instr).unwrap().add_cfg_instr_idx(CfgInstrIdx::new(cfg_node, InCfgNodeInstrPos::InLabel {  } ));   
+    }
+    for &instr in node_mut!(at cfg_node in cfg_graph).op_jump_instr.iter() {
+        instr_slab.get_mut_instr(instr).unwrap().add_cfg_instr_idx(CfgInstrIdx::new(cfg_node, InCfgNodeInstrPos::InJump {  } ));   
+    }
     Ok(())
 }
-pub fn refresh_cfg_instr_idx_in_cfg_node_instrs(cfg_graph:&mut CfgGraph,cfg_node:u32,_symtab:&SymTab,instr_slab:&mut InstrSlab)->Result<()>{
+fn refresh_cfg_instr_idx_in_cfg_node_instrs(cfg_graph:&mut CfgGraph,cfg_node:u32,_symtab:&SymTab,instr_slab:&mut InstrSlab)->Result<()>{
     let outdated = &mut node_mut!(at cfg_node in cfg_graph).instrs.outdated ;
     // 只有当instrList 是outdated 状态才进行操作
     if !*outdated {
@@ -393,11 +412,11 @@ pub fn refresh_cfg_instr_idx_in_cfg_node_instrs(cfg_graph:&mut CfgGraph,cfg_node
     // debug_info_yellow!("refresh node {}",cfg_node);
 
     for (instr_pos,&instr) in node_mut!(at cfg_node in cfg_graph).instrs.iter().enumerate() {
-        instr_slab.get_mut_instr(instr).unwrap().add_cfg_instr_idx(CfgInstrIdx::new(cfg_node, instr_pos, false));   
+        instr_slab.get_mut_instr(instr).unwrap().add_cfg_instr_idx(CfgInstrIdx::new(cfg_node, InCfgNodeInstrPos::InInstrs { instr_pos }));   
     }
     Ok(())
 }
-pub fn refresh_cfg_instr_idx_in_cfg_node_phi_instrs(cfg_graph:&mut CfgGraph,cfg_node:u32,_symtab:&SymTab,instr_slab:&mut InstrSlab)->Result<()>{
+fn refresh_cfg_instr_idx_in_cfg_node_phi_instrs(cfg_graph:&mut CfgGraph,cfg_node:u32,_symtab:&SymTab,instr_slab:&mut InstrSlab)->Result<()>{
     let outdated = &mut node_mut!(at cfg_node in cfg_graph).phi_instrs.outdated ;
     // 只有当instrList 是outdated 状态才进行操作
     if !*outdated {
@@ -407,12 +426,12 @@ pub fn refresh_cfg_instr_idx_in_cfg_node_phi_instrs(cfg_graph:&mut CfgGraph,cfg_
     // debug_info_yellow!("refresh phi node {}",cfg_node);
 
     for (instr_pos,&phi_instr) in node_mut!(at cfg_node in cfg_graph).phi_instrs.iter().enumerate() {
-        instr_slab.get_mut_instr(phi_instr).unwrap().add_cfg_instr_idx(CfgInstrIdx::new(cfg_node, instr_pos, true));   
+        instr_slab.get_mut_instr(phi_instr).unwrap().add_cfg_instr_idx(CfgInstrIdx::new(cfg_node, InCfgNodeInstrPos::InPhi { phi_instr_pos: instr_pos }));   
     }
     Ok(())
 }
 
-pub fn refresh_def_instr_vec_of_defined_symbol(cfg_graph:&mut CfgGraph,symtab:&mut SymTab,instr_slab:&mut InstrSlab )->Result<()>{
+fn refresh_def_instr_vec_of_defined_symbol(cfg_graph:&mut CfgGraph,symtab:&mut SymTab,instr_slab:&mut InstrSlab )->Result<()>{
     let cfg_dfs_vec = etc::dfs(cfg_graph, 0);
     for cfg_node in cfg_dfs_vec{
         for &instr in node!(at cfg_node in cfg_graph).iter_all_instrs(){
diff --git a/src/toolkit/simulator.rs b/src/toolkit/simulator.rs
index 669f055..b367c97 100644
--- a/src/toolkit/simulator.rs
+++ b/src/toolkit/simulator.rs
@@ -496,10 +496,12 @@ impl Simulator{
                 // 这是内存分配指令, 我们在内存分配的时候加入 src 变量
                 let simu_symtab = &mut self.simu_symtab;
                 let src_var_symidx = var_symidx.to_src_symidx();
-                add_symbol!({src_var_symidx.into_symbol()}
-                    with_field SIMU_OP_LAST_DEF_INSTR:{Some(instr)}
-                    to simu_symtab
-                );
+                if !simu_symtab.has_symbol(&src_var_symidx){
+                    add_symbol!({src_var_symidx.into_symbol()}
+                        with_field SIMU_OP_LAST_DEF_INSTR:{Some(instr)}
+                        to simu_symtab
+                    );
+                }
             },
         }
         Ok(instr_struct.clone())
-- 
GitLab