Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
use std::{collections::{VecDeque, HashMap}};

use crate::{ast::*, symbols::SymTab, regeister::{VirtualRegeister, ArgTunnel}};

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum LabelType {
  Other,
  // Else,
  Cond,
  Update,
}
pub struct Label {
  pub name: String,
  pub lt: LabelType,
}

impl Label {
  fn new(name: String,lt: LabelType) -> Self {
    Self {name, lt}
  }
}

pub struct BranchLabel {
  counter: u32,
  branch_stack: Vec<HashMap<LabelType, VecDeque<Label>>>,
  // queue: HashMap<LabelType, VecDeque<Label>>,
}

impl BranchLabel {
  fn init() -> Self {
    Self {
      counter: 0,
      branch_stack: vec![],
      // queue: HashMap::new(),
    }
  }

  fn create(&mut self, lt: LabelType) -> String {
    self.counter += 1;
    let name  = format!("L{}",self.counter);
    let queue = self.branch_stack.get_mut(0).unwrap();
    let n = name.clone();
    if queue.contains_key(&lt){}else{
      queue.insert(lt.clone(), VecDeque::new());
    }
    let bq = queue.get_mut(&lt).unwrap();
    bq.push_front(Label::new(name, lt));
    n
  }

  pub fn label(&self, lt: LabelType) -> &Label {
    let queue = self.branch_stack.get(0).unwrap();
    let q = queue.get(&lt).unwrap();
    // get last
    q.get(q.len() - 1).unwrap()
  }

  pub fn enter_branch(&mut self) {
    // stack
    self.branch_stack.push(HashMap::new());
  }

  pub fn leave_branch(&mut self) {
    // self.queue.clear();
    self.branch_stack.pop().unwrap();
  }

}

#[derive(Debug, Clone)]
pub struct IrProg {
  pub funcs: Vec<IrFunc>,
  pub global_vars: Vec<IrStmt>,
}

#[derive(Debug, Clone)]
pub struct IrFunc {
  pub name: String,
  pub params: Vec<IrStmt>,
  pub stmts: Vec<IrStmt>,
}

#[derive(Debug, Clone)]
pub enum IrStmt {
  Add(String, String, String),
  Sub(String, String, String),
  Mul(String, String, String),
  Div(String, String, String),
  Mod(String, String, String),
  Neg(String, String),
  Or(String, String, String),
  And(String, String, String),
  Equal(String, String, String, String),
  NotEqual(String, String, String, String),
  Lt(String, String, String),
  Let(String, String, String, String, String, String),
  Gt(String, String, String),
  Get(String, String, String, String, String, String),
  Ldc(i32, String),
  Ret(String),
  // env, id, reg
  Assign(Vec<u32>, String, String),
  // env, id
  Ref(Vec<u32>, String),
  // reg label
  Beq(String, String),
  // jump label
  Jmp(String),
  Label(String),
  // scope var name func name
  Param(Vec<u32>, String, String),
  // params (tmp reg, arg reg) label return reg
  Call(Vec<(String, String)>, String, String),
  // scope,name,reg, base reg, num
  Load(Vec<u32>, String, String, String, u32),
  // reg, global var name
  LoadSymbol(String, String),
  // var name val
  DeclGlobal(String, i32),
  // var name, 
  DeclGlobalArray(String, VecDeque<i32>),
  // scope name reg size
  Alloc(Vec<u32>, String, String, u32),
}

pub fn ast2ir(p: &Prog, s: &mut SymTab) -> IrProg {
  let mut bl = BranchLabel::init();
  let mut r = VirtualRegeister::init();
  let mut tunnel = ArgTunnel::init();
  let mut funcs = vec![];
  let mut global_vars = vec![];
  for f in &p.funcs {
    let func = func(&mut tunnel, f, s, &mut bl, &mut r);
    funcs.push(func);
  }
  for g in &p.global_vars {
    if !g.indexes.is_empty() {
      let var_name = g.name.clone();
      let indexes = g.indexes.clone();
      global_vars.push(IrStmt::DeclGlobalArray(var_name, indexes));
    } else {
      if g.expr.is_some() {
        let var_name = g.name.clone();
        let val: i32;
        let e = g.expr.as_ref().unwrap();
        match e {
            Expr::Unary(Unary::Int(i)) => {
              val = *i;
            }
            _ => panic!("initializer element is not constant")
        }
        global_vars.push(IrStmt::DeclGlobal(var_name, val));
      } else {
        panic!("global var is not inited");
      }
    }
  }
  IrProg {
    funcs, global_vars
  }
}

fn func(tunnel: &mut ArgTunnel, f: &Func, table: &mut SymTab, bl: &mut BranchLabel,r: &mut VirtualRegeister) -> IrFunc {
  let mut stmts = Vec::new();
  let mut params = Vec::new();
  // params.push(IrStmt::Label(f.name.clone()));
  // &f.params -> params
  arg(tunnel, f.name.clone(), &mut params, &f.params, table, bl);

  block(tunnel, &mut stmts, &f.stmt, table, bl, r);
  IrFunc {
    name: f.name.clone(),
    stmts,
    params,
  }
}

fn arg(tunnel: &mut ArgTunnel,func_name: String, params: &mut Vec<IrStmt>,ps: &Vec<Param>, table: &mut SymTab, _bl: &mut BranchLabel) {
  for s in ps {
    let n = &s.name;
    let scope = &s.scope;
    // alloc reg
    let entry = table.entry(scope, n);
    entry.and_modify(|s| {
      if s.alloc_phy_reg == false {
        let t = tunnel.set_arg(&func_name);
        s.reg = Some(t.to_string());
        s.alloc_virtual_reg = true;
        s.alloc_phy_reg = true; 
      } 
    });
    params.push(IrStmt::Param(scope.to_vec(), n.to_string(), func_name.to_string()))
  }
}

fn block(tunnel: &mut ArgTunnel,stmts: &mut Vec<IrStmt>,bts: &Vec<BlockItem>, table: &mut SymTab, bl: &mut BranchLabel,r: &mut VirtualRegeister) {
  for s in bts.iter() {
    match s {
        BlockItem::Stmt(s) => {
          stmt(tunnel, stmts,s,table, bl, r);
        },
        BlockItem::Decl(d) => {
          let name = &d.name;
          let scope = &d.scope;
          if !d.indexes.is_empty() {
            // temp array decl
            let mut size = 4;
            d.indexes.iter().for_each(|i| size *= i);
            let t = r.eat();
            // alloc reg
            let entry = table.entry(scope, name);
              entry.and_modify(|s| {
              if s.alloc_virtual_reg == false {
                s.reg = Some(t.to_string());
                s.alloc_virtual_reg = true; 
              } 
            });
            // alloc c 
            stmts.push(IrStmt::Alloc(scope.clone(),name.clone(), t,size.try_into().unwrap()));
          }
          if let Some(ex) = &d.expr { //when assign
       
            expr(tunnel, stmts, ex, table, bl, r);
            let t2 = r.near();// todo, noy use near api
             // alloc reg
            let entry = table.entry(scope, name);
            entry.and_modify(|s| {
              if s.alloc_virtual_reg == false {
                let t = r.eat();
                s.reg = Some(t.to_string());
                s.alloc_virtual_reg = true; 
              } 
            });
            
            stmts.push(IrStmt::Assign(scope.to_vec(), name.to_string(),t2));
          }
        },
    }
  }
}

fn stmt(tunnel: &mut ArgTunnel,stmts: &mut Vec<IrStmt>,s: &Stmt, table: &mut SymTab,bl: &mut BranchLabel,r: &mut VirtualRegeister) {
  match s {
      Stmt::Ret(e) => {
        expr(tunnel, stmts, e, table, bl, r);
        let t = r.near();
        stmts.push(IrStmt::Ret(t));
      }
      Stmt::Expr(e) => {
        if let Some(ex) = e {
          expr(tunnel, stmts, ex, table, bl, r);
        }
      },
      Stmt::If(e, t, l) => {
        // 1. create label
        // 2. add beq ir
        // 3. when has else, add jmp ir
        bl.enter_branch();
        let other_branch = bl.create(LabelType::Other);
        expr(tunnel, stmts, e, table, bl, r);
        let reg = r.near();
        
        stmt(tunnel, stmts, t, table, bl, r);
        if l.is_some() {
          let else_branch = bl.create(LabelType::Other); 
          stmts.push(IrStmt::Beq(reg, bl.label(LabelType::Other).name.clone()));
          let s1 = l.as_ref().unwrap();
          stmts.push(IrStmt::Jmp(bl.label(LabelType::Other).name.clone()));//when has else, jump to other
          stmts.push(IrStmt::Label(else_branch));
          stmt(tunnel, stmts, s1, table, bl, r)
        } else {
          stmts.push(IrStmt::Beq(reg, bl.label(LabelType::Other).name.clone()));
        }
        stmts.push(IrStmt::Label(other_branch));
        bl.leave_branch();
      },
      Stmt::Block(bts) => {
        block(tunnel, stmts, bts, table, bl, r)
      },
    Stmt::For(init, cond, update, s) => {
      bl.enter_branch();
       let other_branch = bl.create(LabelType::Other); 
       let cond_branch = bl.create(LabelType::Cond); 
      if let Some(ex) = init {
          expr(tunnel, stmts, ex, table, bl, r);
        }
        stmts.push(IrStmt::Label(bl.label(LabelType::Cond).name.clone()));// <--
        if let Some(ex) = cond {
          expr(tunnel, stmts, ex, table, bl, r);
        }
        stmts.push(IrStmt::Beq(r.near(), other_branch));//--------------------------
        stmt(tunnel, stmts, s, table, bl, r);// body
        bl.create(LabelType::Update);
        stmts.push(IrStmt::Label(bl.label(LabelType::Update).name.clone()));
        if let Some(ex) = update {
          expr(tunnel, stmts, ex, table, bl, r);
        }
        stmts.push(IrStmt::Jmp(cond_branch));//----------------------------
        stmts.push(IrStmt::Label(bl.label(LabelType::Other).name.clone()));// <----------
        bl.leave_branch();
    },
    Stmt::While(cond,s) => {
      bl.enter_branch();
      let other_branch = bl.create(LabelType::Other); 
      let cond_branch = bl.create(LabelType::Cond); 
      bl.create(LabelType::Update); 

      stmts.push(IrStmt::Label(bl.label(LabelType::Cond).name.clone()));
      stmts.push(IrStmt::Label(bl.label(LabelType::Update).name.clone()));
      expr(tunnel, stmts, cond, table, bl, r);
      stmts.push(IrStmt::Beq(r.near(), other_branch));
      stmt(tunnel, stmts, s, table, bl, r);
      stmts.push(IrStmt::Jmp(cond_branch));
      stmts.push(IrStmt::Label(bl.label(LabelType::Other).name.clone()));
      bl.leave_branch();
    },
    Stmt::Continue => {
      stmts.push(IrStmt::Jmp(bl.label(LabelType::Update).name.clone()));
    },
    Stmt::Break => {
      stmts.push(IrStmt::Jmp(bl.label(LabelType::Other).name.clone()));
    },
  }
}

fn expr(tunnel: &mut ArgTunnel,stmts: &mut Vec<IrStmt>, e: &Expr, table: &mut SymTab, bl: &mut BranchLabel,r: &mut VirtualRegeister) {
  bin_op(tunnel, stmts, e, table, bl, r)
}

fn bin_op(tunnel: &mut ArgTunnel,stmts: &mut Vec<IrStmt>,m: &Expr, table: &mut SymTab, bl: &mut BranchLabel,r: &mut VirtualRegeister) {
  match m {
    Expr::Mul(u, m1) => {
      bin_op(tunnel, stmts, u, table, bl, r);
      bin_op(tunnel, stmts, m1, table, bl, r);
      let t1 = r.near();
      let t2 = r.near();
      let t = r.eat();
      stmts.push(IrStmt::Mul(t1, t2, t));
    },
    Expr::Div(u, m1) => {
      bin_op(tunnel, stmts, u, table, bl, r);
      bin_op(tunnel, stmts, m1, table, bl, r);
      let t1 = r.near();
      let t2 = r.near();
      let t = r.eat();
      stmts.push(IrStmt::Div(t1, t2, t));
    },
    Expr::Mod(u, m1) => {
      bin_op(tunnel, stmts, u, table, bl, r);
      bin_op(tunnel, stmts, m1, table, bl, r);
      let t1 = r.near();
      let t2 = r.near();
      let t = r.eat();
      stmts.push(IrStmt::Mod(t1, t2, t));
    },
    Expr::Add(m,a1) => {
      bin_op(tunnel, stmts, m, table, bl, r);
      bin_op(tunnel, stmts, a1, table, bl, r);
      let t1 = r.near();
      let t2 = r.near();
      let t = r.eat();
      stmts.push(IrStmt::Add(t1, t2, t));
    },
    Expr::Sub(m,a1)=> {
      bin_op(tunnel, stmts, m, table, bl, r);
      bin_op(tunnel, stmts, a1, table, bl, r);
      let t1 = r.near();
      let t2 = r.near();
      let t = r.eat();
      stmts.push(IrStmt::Sub(t1, t2, t));
    },
    Expr::Unary(u) => unary(tunnel, stmts, u, table, bl, r),
    Expr::Lt(e, e1) => {
      bin_op(tunnel, stmts, e, table, bl, r);
      bin_op(tunnel, stmts, e1, table, bl, r);
      let t1 = r.near();
      let t2 = r.near();
      let t = r.eat();
      stmts.push(IrStmt::Lt(t1, t2, t));
    }
    Expr::Gt(e, e1) => {
      bin_op(tunnel, stmts, e, table, bl, r);
      bin_op(tunnel, stmts, e1, table, bl, r);
      let t1 = r.near();
      let t2 = r.near();
      let t = r.eat();
      stmts.push(IrStmt::Gt(t1, t2, t));
    }
    Expr::Let(e, e1) => {
      bin_op(tunnel, stmts, e, table, bl, r);
      bin_op(tunnel, stmts, e1, table, bl, r);
      let t1 = r.near();
      let t2 = r.near();
      let t = r.eat();

      let t3 = r.eat();

      let t4 = r.eat();

      let t5 = r.eat();
      stmts.push(IrStmt::Let(t1, t2, t, t3, t4, t5));
    }
    Expr::Get(e, e1) => {
      bin_op(tunnel, stmts, e, table, bl, r);
      bin_op(tunnel, stmts, e1, table, bl, r);
      let t1 = r.near();
      let t2 = r.near();
      let t = r.eat();

      let t3 = r.eat();

      let t4 = r.eat();

      let t5 = r.eat();
      stmts.push(IrStmt::Get(t1, t2, t, t3, t4, t5));
    }
    Expr::And(e, e1) => {
      bin_op(tunnel, stmts, e, table, bl, r);
      bin_op(tunnel, stmts, e1, table, bl, r);
      let t1 = r.near(); // s2
      let t2 = r.near(); // s1
      let t = r.eat(); // d
      stmts.push(IrStmt::And(t1, t2, t));
    }
    Expr::Or(e, e1) => {
      bin_op(tunnel, stmts, e, table, bl, r);
      bin_op(tunnel, stmts, e1, table, bl, r);
          let t1 = r.near();
          let t2 = r.near();
          let t = r.eat();
      stmts.push(IrStmt::Or(t1, t2, t));
    }
    Expr::NotEquals(e, e1) => {
      bin_op(tunnel, stmts, e, table, bl, r);
      bin_op(tunnel, stmts, e1, table, bl, r);
      let t1 = r.near();
      let t2 = r.near();
      let t = r.eat();
      let t3 = r.eat();
      stmts.push(IrStmt::NotEqual(t1, t2, t, t3));
    }
    Expr::Equals(e, e1) => {
      bin_op(tunnel, stmts, e, table, bl, r);
      bin_op(tunnel, stmts, e1, table, bl, r);
      let t1 = r.near();
      let t2 = r.near();
      let t = r.eat();

      let t3 = r.eat();
      stmts.push(IrStmt::Equal(t1, t2, t, t3));
    }
    Expr::Assign(env,id, e) => {
      let name = &**id;
      let n = &**env;
      bin_op(tunnel, stmts, e, table, bl, r);
      let t2 = r.near();// todo, noy use near api
        let entry = table.entry(n, name);
      entry.and_modify(|s| {
        if s.alloc_virtual_reg == false {
          let t = r.eat();
          s.reg = Some(t.to_string());
          s.alloc_virtual_reg = true; 
        } 
      });
      stmts.push(IrStmt::Assign(n.to_vec(), name.to_string(),t2));
    },
    Expr::Null => {},
    Expr::Cond(condition, then, other) => {
      // like if-else
      bl.enter_branch();
      let else_branch = bl.create(LabelType::Other);
      let other_branch = bl.create(LabelType::Other);
      expr(tunnel, stmts, condition, table, bl, r);
      let reg = r.near();
      stmts.push(IrStmt::Beq(reg, bl.label(LabelType::Other).name.clone()));
      expr(tunnel, stmts, then, table, bl, r);
      stmts.push(IrStmt::Jmp(bl.label(LabelType::Other).name.clone()));
      stmts.push(IrStmt::Label(else_branch));
      expr(tunnel, stmts, other, table, bl, r);
      stmts.push(IrStmt::Label(other_branch));
      bl.leave_branch();
    },
  }
}

fn unary(tunnel: &mut ArgTunnel,stmts: &mut Vec<IrStmt>, u: &Unary, table: &mut SymTab, bl: &mut BranchLabel,r: &mut VirtualRegeister) {
  match u {
        Unary::Int(y) => {
          let t = r.eat();
          stmts.push(IrStmt::Ldc(*y, t))
        },
        Unary::Neg(y) => { 
          unary(tunnel, stmts, &*y, table, bl, r);
          let t1 = r.near();
          let t2 = r.eat();
          stmts.push(IrStmt::Neg(t1, t2));
        },
        Unary::Primary(y) => {
          expr(tunnel, stmts, &*y, table, bl, r)
        }
        Unary::Identifier(env, id) => {
          // check decl, table exist
          // match x + y
          let name = &**id;
          // use var
          let var = table.extis(env, name);
          assert!(var.0);
          if var.1 == vec![1] {
            let reg = r.eat();
            // global var
            stmts.push(IrStmt::LoadSymbol(reg.clone(), name.clone()));
                  // alloc reg
            let entry = table.entry(&vec![1], name);
            entry.and_modify(|s| {
              if s.alloc_virtual_reg == false {
                s.reg = Some(reg.clone());
                s.alloc_virtual_reg = true;
              } 
            });
            stmts.push(IrStmt::Load(vec![1],name.clone(),r.eat(), reg.clone(), 0));
            r.put_near(reg.clone());
          } else {
            let reg = table.get(&var.1, name).reg.as_ref().unwrap();
            r.put_near(reg.clone());

            stmts.push(IrStmt::Ref(env.to_vec(), name.to_string()));
          }
          // println!("t:{:?} env {:?} var.1 {:?} n {}", table, env, var.1, name);
          
        },
        Unary::Call(call) => {
          // match func(1, 2)
          let mut params = vec![];
          let label = &call.name;
          for e in &call.params {
            expr(tunnel, stmts, e, table, bl, r);
            params.push((r.near(), tunnel.get_arg(label)));
          }
          assert!(tunnel.is_match(label));
          stmts.push(IrStmt::Call(params, label.to_string(), r.eat()));
        }
        Unary::Index(env, i) => {
          let name = &i.name;
          // use var
          let var = table.extis(env, name);
          assert!(var.0);
          // off = (range + index) * 4
          let mut offset = 0;
          let mut range = table.get(env, name).array_range.clone();
          for index in &i.index {
            range.pop_front().unwrap(); // high -> low
            let mut result = 1;
            range.iter().for_each(|r| result *= r);
            offset += index * result;
          }
          offset *= 4;
          if var.1 == vec![1] {
            let base_reg = r.eat();
            stmts.push(IrStmt::LoadSymbol(base_reg.clone(), name.clone()));
             // alloc reg
            let entry = table.entry(&vec![1], name);
            entry.and_modify(|s| {
              if s.alloc_virtual_reg == false {
                s.reg = Some(base_reg.clone());
                s.alloc_virtual_reg = true;
              } 
            });
            stmts.push(IrStmt::Load(vec![1],name.clone(),r.eat(),base_reg.clone(), offset.try_into().unwrap()));
            // global var
            
          } else {
            // temp var
            let base_reg = table.get(&var.1, name).reg.as_ref().unwrap();
            stmts.push(IrStmt::Load(var.1,name.clone(),r.eat(),base_reg.clone(), offset.try_into().unwrap()));
          }
        }
    }
}