dom_test.nx source
↩ module page · 83 lines · 1823 B
1// dom_test.nx -- self-test for dom.nx.
2
3
4import "dom.nx"
5
6
7// ---- self-test ----
8//
9// Build a 5-block diamond CFG and verify the computed idoms.
10//
11// 0 (entry)
12// |
13// 1
14// / \
15// 2 3
16// \ /
17// 4 (merge)
18//
19// Expected idoms:
20// 0 -> 0 (self for entry)
21// 1 -> 0
22// 2 -> 1
23// 3 -> 1
24// 4 -> 1 (both 2 and 3 branch to 4, so the immediate dominator
25// of 4 is the common ancestor of 2 and 3 -- which is 1)
26
27func main() -> i64 {
28 // Allocate 5 Block structs = 5 * 80 = 400 bytes.
29 let buf: *u8 = sys_mmap(512)
30 let blocks: *Block = buf as *Block
31 let base: i64 = blocks as i64
32
33 // bb 0: succs = [1]; no preds
34 let b0: *Block = (base + 0 * 80) as *Block
35 b0.id = 0
36 b0.n_succs = 1
37 b0.succ0 = 1
38 b0.n_preds = 0
39
40 // bb 1: succs = [2, 3]; preds = [0]
41 let b1: *Block = (base + 1 * 80) as *Block
42 b1.id = 1
43 b1.n_succs = 2
44 b1.succ0 = 2
45 b1.succ1 = 3
46 b1.n_preds = 1
47 b1.pred0 = 0
48
49 // bb 2: succs = [4]; preds = [1]
50 let b2: *Block = (base + 2 * 80) as *Block
51 b2.id = 2
52 b2.n_succs = 1
53 b2.succ0 = 4
54 b2.n_preds = 1
55 b2.pred0 = 1
56
57 // bb 3: succs = [4]; preds = [1]
58 let b3: *Block = (base + 3 * 80) as *Block
59 b3.id = 3
60 b3.n_succs = 1
61 b3.succ0 = 4
62 b3.n_preds = 1
63 b3.pred0 = 1
64
65 // bb 4: no succs; preds = [2, 3]
66 let b4: *Block = (base + 4 * 80) as *Block
67 b4.id = 4
68 b4.n_succs = 0
69 b4.n_preds = 2
70 b4.pred0 = 2
71 b4.pred1 = 3
72
73 dom_compute(blocks, 5, 0)
74
75 // Verify idoms.
76 if b0.idom != 0 { return 1 }
77 if b1.idom != 0 { return 2 }
78 if b2.idom != 1 { return 3 }
79 if b3.idom != 1 { return 4 }
80 if b4.idom != 1 { return 5 }
81
82 return 0
83}