1 module lir_test;
2 
3 import all;
4 import amd64asm;
5 import lir_amd64;
6 
7 void main()
8 {
9 	// i32 sign(i32 number) {
10 	//     i32 result;
11 	//     if (number < 0) result = 0-1;
12 	//     else if (number > 0) result = 1;
13 	//     else result = 0;
14 	//     return result;
15 	// }
16 
17 	// function sign() {
18 	//   @0
19 	//     v43 = parameter0 users [i62, i93]
20 	//     jmp @1
21 	//   @1
22 	//     if v43 < c1 then @2 else @3
23 	//   @2
24 	//     jmp @6
25 	//   @3
26 	//     if v43 > c5 then @4 else @5
27 	//   @4
28 	//     jmp @6
29 	//   @5
30 	//     jmp @6
31 	//   @6
32 	//     v157 = phi152(c7 @5, c6 @4, c4 @2) users [i26]
33 	//     jmp @7
34 	//   @7
35 	//     return v157
36 	// }
37 
38 	// function sign() {
39 	//   @0
40 	//     mov vreg.0, ecx
41 	//     jmp @1
42 	//   @1
43 	//     cmp v43, c1
44 	//     jl @2
45 	//     jmp @3
46 	//   @2
47 	//     jmp @6
48 	//   @3
49 	//     cmp v43, c5
50 	//     jg @4
51 	//     jmp @5
52 	//   @4
53 	//     jmp @6
54 	//   @5
55 	//     jmp @6
56 	//   @6
57 	//     v157 = phi152(c7 @5, c6 @4, c4 @2)
58 	//     jmp @7
59 	//   @7
60 	//     return v157
61 	// }
62 
63 	writefln("Lir example");
64 	Driver driver;
65 	driver.initialize(null);
66 	scope(exit) driver.releaseMemory;
67 
68 	IrBuilder builder;
69 	IrFunction lir;
70 
71 	builder.context = &driver.context;
72 	builder.ir = &lir;
73 	lir.name = driver.context.idMap.getOrReg("sign");
74 
75 	lir.storage = driver.context.irBuffer.freePart;
76 	builder.setupEntryExitBlocks;
77 
78 	IrIndex block1 = builder.addBasicBlock();
79 	IrIndex block2 = builder.addBasicBlock();
80 	IrIndex block3 = builder.addBasicBlock();
81 	IrIndex block4 = builder.addBasicBlock();
82 	IrIndex block5 = builder.addBasicBlock();
83 	IrIndex block6 = builder.addBasicBlock();
84 
85 	builder.addBlockTarget(lir.entryBasicBlock, block1);
86 	builder.addBlockTarget(block1, block2);
87 	builder.addBlockTarget(block1, block3);
88 	builder.addBlockTarget(block2, block6);
89 	builder.addBlockTarget(block3, block4);
90 	builder.addBlockTarget(block3, block5);
91 	builder.addBlockTarget(block4, block6);
92 	builder.addBlockTarget(block5, block6);
93 	builder.addBlockTarget(block6, lir.exitBasicBlock);
94 
95 	IrIndex const_0 = driver.context.constants.add(IrConstant(0));
96 	IrIndex const_1 = driver.context.constants.add(IrConstant(1));
97 	IrIndex const_m1 = driver.context.constants.add(IrConstant(-1));
98 
99 	//     mov vreg.0, ecx
100 	IrIndex param0Value = builder.emitInstr!LirAmd64Instr_mov(lir.entryBasicBlock, amd64_reg.ax).result;
101 	//     jmp @1
102 	builder.emitInstr!LirAmd64Instr_jmp(lir.entryBasicBlock);
103 	//   @1
104 	//     cmp v43, c1
105 	//     jl @2
106 	builder.emitInstr!LirAmd64Instr_bin_branch(block1, IrBinaryCondition.l, param0Value, const_0);
107 	//     jmp @3
108 	builder.emitInstr!LirAmd64Instr_jmp(block1);
109 	//   @2
110 	//     jmp @6
111 	builder.emitInstr!LirAmd64Instr_jmp(block2);
112 	//   @3
113 	//     cmp v43, c5
114 	//     jg @4
115 	builder.emitInstr!LirAmd64Instr_bin_branch(block3, IrBinaryCondition.g, param0Value, const_0);
116 	//     jmp @5
117 	builder.emitInstr!LirAmd64Instr_jmp(block3);
118 	//   @4
119 	//     jmp @6
120 	builder.emitInstr!LirAmd64Instr_jmp(block4);
121 	//   @5
122 	//     jmp @6
123 	builder.emitInstr!LirAmd64Instr_jmp(block5);
124 	//   @6
125 	//     v157 = phi152(c7 @5, c6 @4, c4 @2)
126 	IrIndex phi = builder.addPhi(block6);
127 	IrIndex phiValue = lir.getPhi(phi).result;
128 	builder.addPhiArg(phi, block5, const_0);
129 	builder.addPhiArg(phi, block4, const_1);
130 	builder.addPhiArg(phi, block2, const_m1);
131 	builder.addUser(phi, const_0);
132 	builder.addUser(phi, const_1);
133 	builder.addUser(phi, const_m1);
134 	//     jmp @7
135 	builder.emitInstr!LirAmd64Instr_jmp(block6);
136 	//   @7
137 	//     return v157
138 	builder.emitInstr!LirAmd64Instr_mov(lir.exitBasicBlock, amd64_reg.ax, [phiValue]);
139 	builder.emitInstr!LirAmd64Instr_return(lir.exitBasicBlock);
140 
141 	validateIrFunction(driver.context, lir);
142 
143 
144 	FuncDumpSettings dumpSettings;
145 	dumpSettings.dumper = &dumpAmd64Instr;
146 	dumpFunction(lir, driver.context, dumpSettings);
147 }