raz / test / a64 / Encode.raz

308 lines · 9.2 KiB · raw · history · last change e99cab9b

1(* A64's encodings, checked against Apple's assembler: its assembler on
2 Asm.txt, then every routine of its interface (api), into one
3 procedure's chunk, which is printed a word to a line in hex (a run of
4 nop as one line), then the fixups. Api.s holds the same instructions
5 as api's calls, a line for each, and expect.sh makes Encode.expect
6 from Asm.txt and it with cc and objdump on an arm64 Mac. The
7 assembler goes first, at the chunk's start, as a code procedure is:
8 a tbz's link, the place of the jump before it on its label's chain,
9 counts from there in 14 bits. The interface's last jumps are the
10 farthest b.cond and cbz reach, 1 MB. test/check.sh compiles this with
11 -I src, as A64 isn't in the compiler yet. *)
12module Encode;
13import Fmt, Diag, Scan, Chunk, A := A64;
14import SYSTEM;
15
16var
17 body, proc1, glob, byte: INT; (* chunks: the code, and three it names *)
18
19(* The calls, in Api.s's order *)
20proc api;
21var L, L2, top: INT;
22begin
23 (* moves *)
24 A.movRI(0, 0);
25 A.movRI(1, 1234H);
26 A.movRI(2, 12340000H);
27 A.movRI(3, -1);
28 A.movRI(4, -10001H);
29 A.movRI(5, 00FF00FF00FF00FFH);
30 A.movRI(6, 123456789ABCDEF0H);
31 A.movRI(7, I64(0FFFF1234FFFF5678H));
32 A.movRI(8, 100000001H);
33 A.movRI(9, 5555555555555555H);
34 A.movRI(10, I64(8000000000000000H));
35 A.movRI(11, 7FFFFFFFFFFFFFFFH);
36 A.movRI(12, 0FFFFFFFFH);
37 A.movRI(13, 0FFFF0000ABCDH);
38 A.movRR(0, 1);
39 A.movRR(A.SP, A.FP);
40 A.movRR(A.FP, A.SP);
41 (* two registers *)
42 A.alu(A.opAdd, 0, 1, 2);
43 A.alu(A.opAdds, 3, 4, 5);
44 A.alu(A.opSub, 6, 7, 8);
45 A.alu(A.opSubs, 9, 10, 11);
46 A.alu(A.opAnd, 12, 13, 14);
47 A.alu(A.opOrr, 15, 16, 17);
48 A.alu(A.opEor, 19, 20, 21);
49 A.alu(A.opAnds, 22, 23, 24);
50 A.alu(A.opBic, 25, 26, 27);
51 A.alu(A.opOrn, 28, 29, 30);
52 A.aluShift(A.opAdd, 0, 1, 2, A.shLsl, 3);
53 A.aluShift(A.opSub, 3, 4, 5, A.shAsr, 63);
54 A.aluShift(A.opOrr, 6, 7, 8, A.shRor, 1);
55 (* immediates *)
56 A.aluI(A.opAdd, 0, 1, 4095);
57 A.aluI(A.opAdd, 0, 1, 4096);
58 A.aluI(A.opAdd, A.SP, A.SP, 16);
59 A.aluI(A.opSub, A.SP, A.SP, 123000H);
60 A.aluI(A.opAdd, 0, 1, -8);
61 A.aluI(A.opSub, 2, 3, 12345H);
62 A.aluI(A.opAdd, 4, A.SP, 1234567H);
63 A.aluI(A.opAdds, 5, 6, 12345H);
64 A.aluI(A.opAnd, 0, 1, 0FFH);
65 A.aluI(A.opOrr, A.SP, 1, 0F0H);
66 A.aluI(A.opEor, 2, 3, 1234H);
67 A.cmp(0, 1);
68 A.cmpI(A.SP, 32);
69 A.cmpI(2, -5);
70 A.cmpI(3, 1001001H);
71 A.alu(A.opAnds, A.ZR, 4, 5);
72 A.aluI(A.opAnds, A.ZR, 6, 1);
73 A.aluI(A.opAnds, A.ZR, 7, I64(8000000000000000H));
74 A.neg(0, 1);
75 A.mvn(2, 3);
76 (* shifts and fields *)
77 A.shift(A.shLsl, 0, 1, 2, 8);
78 A.shift(A.shLsr, 3, 4, 5, 8);
79 A.shift(A.shAsr, 6, 7, 8, 8);
80 A.shift(A.shRor, 9, 10, 11, 8);
81 A.shift(A.shLsl, 3, 4, 5, 4);
82 A.shiftI(A.shLsl, 0, 1, 3, 8);
83 A.shiftI(A.shLsr, 0, 1, 63, 8);
84 A.shiftI(A.shAsr, 2, 3, 1, 8);
85 A.shiftI(A.shRor, 4, 5, 7, 8);
86 A.shiftI(A.shLsl, 6, 7, 31, 4);
87 A.shiftI(A.shRor, 8, 9, 16, 4);
88 A.shiftI(A.shLsl, 0, 1, 0, 8);
89 A.field(false, 0, 1, 8, 8);
90 A.field(true, 2, 3, 0, 32);
91 A.extend(0, 1, 1, true);
92 A.extend(2, 3, 2, true);
93 A.extend(4, 5, 4, true);
94 A.extend(6, 7, 1, false);
95 A.extend(8, 9, 2, false);
96 A.extend(10, 11, 4, false);
97 (* multiply, divide, select, count *)
98 A.mul(0, 1, 2);
99 A.madd(3, 4, 5, 6, false);
100 A.madd(7, 8, 9, 10, true);
101 A.mulHigh(true, 0, 1, 2);
102 A.mulHigh(false, 3, 4, 5);
103 A.divide(true, 3, 4, 5);
104 A.divide(false, 6, 7, 8);
105 A.csel(A.csSel, A.ccEQ, 0, 1, 2);
106 A.csel(A.csInc, A.ccNE, 3, 4, 5);
107 A.csel(A.csNeg, A.ccLT, 6, 7, 8);
108 A.cset(A.ccGT, 9);
109 A.clz(0, 1, 8);
110 A.clz(2, 3, 4);
111 A.rbit(4, 5, 8);
112 (* loads and stores *)
113 A.load(0, 1, 0, 8, false);
114 A.load(0, 1, 32760, 8, false);
115 A.load(2, 3, 32768, 8, false);
116 A.load(4, 5, -8, 8, false);
117 A.load(4, 5, -256, 8, false);
118 A.load(6, 7, 3, 8, false);
119 A.load(8, 9, 4095, 1, false);
120 A.load(10, 11, 4, 1, true);
121 A.load(12, 13, 8190, 2, false);
122 A.load(14, 15, 6, 2, true);
123 A.load(0, A.SP, 16380, 4, false);
124 A.load(1, 2, 4, 4, true);
125 A.load(3, 4, -1, 1, true);
126 A.store(0, 1, 8, 8);
127 A.store(2, A.SP, 0, 4);
128 A.store(3, 4, 1, 2);
129 A.store(5, 6, 4095, 1);
130 A.store(A.ZR, 7, 0, 8);
131 A.loadR(0, 1, 2, 8, false, true);
132 A.loadR(3, 4, 5, 1, true, false);
133 A.loadR(6, 7, 8, 4, false, true);
134 A.storeR(9, 10, 11, 2, true);
135 A.storeR(12, 13, 14, 8, false);
136 A.loadIdx(0, A.SP, 16, 8, A.mPost);
137 A.storeIdx(1, A.SP, -16, 8, A.mPre);
138 A.storeIdx(2, 3, -256, 4, A.mPre);
139 A.loadIdx(4, 5, 255, 1, A.mPost);
140 A.loadF(0, 1, 8, 8);
141 A.loadF(2, 3, 4, 4);
142 A.loadF(4, 5, 32, 16);
143 A.storeF(6, A.SP, 65520, 16);
144 A.storeF(7, 8, -4, 8);
145 A.storeF(9, 10, 0, 4);
146 A.stp(A.FP, A.LR, A.SP, -16, A.mPre);
147 A.ldp(A.FP, A.LR, A.SP, 16, A.mPost);
148 A.stp(19, 20, A.SP, 504, A.mOff);
149 A.ldp(21, 22, 1, -512, A.mOff);
150 (* atomics and the system's *)
151 A.ldaddal(1, 0, 2, 8);
152 A.ldaddal(3, 4, 5, 4);
153 A.swpal(6, 7, 8, 8);
154 A.swpal(9, 10, 11, 4);
155 A.casal(12, 13, 14, 8);
156 A.casal(15, 16, 17, 4);
157 A.dmb();
158 A.svc(80H);
159 A.svc(0);
160 A.brk(1);
161 A.brk(0F000H);
162 (* reals *)
163 A.fop(A.fMul, 0, 1, 2, false);
164 A.fop(A.fDiv, 0, 1, 2, false);
165 A.fop(A.fAdd, 0, 1, 2, false);
166 A.fop(A.fSub, 0, 1, 2, false);
167 A.fop(A.fMax, 0, 1, 2, false);
168 A.fop(A.fMin, 0, 1, 2, false);
169 A.fop(A.fAdd, 3, 4, 5, true);
170 A.fop1(A.fMov, 0, 1, false);
171 A.fop1(A.fAbs, 0, 1, false);
172 A.fop1(A.fNeg, 0, 1, false);
173 A.fop1(A.fSqrt, 0, 1, false);
174 A.fop1(A.fSqrt, 6, 7, true);
175 A.fcvt(0, 1, false);
176 A.fcvt(2, 3, true);
177 A.fcmp(0, 1, false);
178 A.fcmp(2, 3, true);
179 A.fcmp(4, -1, false);
180 A.fcmp(5, -1, true);
181 A.scvtf(0, 1, false, true);
182 A.scvtf(2, 3, true, true);
183 A.scvtf(4, 5, false, false);
184 A.fcvtz(0, 1, false, true);
185 A.fcvtz(2, 3, true, false);
186 A.fmovFR(0, 1, false);
187 A.fmovFR(2, 3, true);
188 A.fmovRF(4, 5, false);
189 A.fmovRF(6, 7, true);
190 A.cnt(16, 17);
191 A.addv(18, 19);
192 (* addresses, calls *)
193 A.adr(0, 0);
194 A.adr(1, -4);
195 A.adr(2, 1048572);
196 A.addrChunk(0, glob, 0);
197 A.loadChunk(1, glob, 0, 8, false);
198 A.storeChunk(2, glob, 0, 4);
199 A.loadChunkF(3, glob, 0, 8);
200 A.storeChunkF(4, glob, 0, 4);
201 A.loadChunk(5, byte, 0, 8, false);
202 A.callChunk(proc1);
203 A.jmpChunk(proc1);
204 A.callBack(0);
205 A.callR(5);
206 A.jmpR(6);
207 A.ret();
208 (* jumps within the procedure: forward on a chain, back *)
209 L := A.NoLabel;
210 A.jcc(A.ccEQ, L);
211 A.cbz(false, 3, L);
212 A.cbz(true, 4, L);
213 A.tbz(false, 5, 3, L);
214 A.tbz(true, 6, 40, L);
215 A.jmp(L);
216 A.fixLink(L);
217 top := Chunk.pc;
218 A.movRR(0, 1);
219 A.jccBack(A.ccNE, top);
220 A.jmpBack(top);
221 L := A.NoLabel; L2 := A.NoLabel;
222 A.jmp(L);
223 A.jcc(A.ccLO, L2);
224 A.jmp(L);
225 A.fixLink(A.joinLinks(L, L2));
226 L := A.NoLabel;
227 A.movRR(1, 2);
228 A.jmp(L);
229 A.dropJumpTo(L);
230 A.fixLink(L);
231 A.align(16);
232 (* the farthest: b.cond and cbz to 1 MB on, and b.cond 1 MB back *)
233 L := A.NoLabel;
234 A.jcc(A.ccNE, L);
235 A.cbz(false, 1, L);
236 A.align(100000H);
237 A.fixLink(L);
238 A.jccBack(A.ccEQ, 0);
239end;
240
241(* The test's reader of a constant: a number, perhaps negative *)
242proc konst(_whole: BOOL): (I64, ERROR);
243var neg: BOOL; v: I64;
244begin
245 neg := Scan.tok = Scan.sMinus;
246 if neg then Scan.next; end;
247 if Scan.tok # Scan.sInt then return 0, Scan.mark("expected a number"); end;
248 v := Scan.ival;
249 Scan.next;
250 if neg then return -v, nil; end;
251 return v, nil;
252end;
253
254(* and of a name: proc1 and glob *)
255proc name: (INT, ERROR);
256begin
257 if Scan.nameOf(Scan.id) = "proc1" then Scan.next; return proc1, nil; end;
258 if Scan.nameOf(Scan.id) = "glob" then Scan.next; return glob, nil; end;
259 return -1, nil;
260end;
261
262proc chunk(n: []const U8; sec: Chunk.Section): INT;
263begin
264 return Chunk.newChunk(sec, -1, Scan.intern(SYSTEM.PTR(n), LEN(n)));
265end;
266
267const
268 Kinds: [4][]const U8 = ["", "ARM64_RELOC_BRANCH26", "ARM64_RELOC_PAGE21", "ARM64_RELOC_PAGEOFF12"];
269 Nop = 0D503201FH;
270
271var
272 err: ERROR;
273 i, n, w: INT;
274 why: []const U8;
275
276begin
277 body := chunk("code", Chunk.secText);
278 proc1 := chunk("proc1", Chunk.secText);
279 glob := chunk("glob", Chunk.secBss);
280 Chunk.setBss(glob, 8, 8);
281 byte := chunk("byte", Chunk.secBss);
282 Chunk.setBss(byte, 8, 1);
283 Chunk.beginChunk(body);
284 why := Scan.open("test/a64/Asm.txt");
285 if LEN(why) > 0 then Fmt.print("test/a64/Asm.txt: {}\n", [Fmt.str(why)]); end;
286 Scan.next;
287 err := A.assemble(konst, name);
288 if err # nil then Fmt.print("the assembler failed\n", []); end;
289 api();
290 Chunk.endChunk(body);
291 i := 0;
292 while i < Chunk.pc do
293 w := Chunk.get4At(i) & 0FFFFFFFFH;
294 n := 0;
295 while (i < Chunk.pc) and (w = Nop) and ((Chunk.get4At(i) & 0FFFFFFFFH) = Nop) do INC(n); INC(i, 4); end;
296 if n > 0 then
297 Fmt.print("nop * {}\n", [Fmt.int(n)]);
298 else
299 Fmt.print("{08}\n", [Fmt.lowHex(U64(w))]);
300 INC(i, 4);
301 end;
302 end;
303 for i := 0 to Chunk.numFixups - 1 do
304 Fmt.print("reloc {016} {} _{}\n", [Fmt.lowHex(U64(Chunk.fixups[i].at)),
305 Fmt.str(Kinds[INT(Chunk.fixups[i].kind)]), Fmt.str(Scan.nameOf(Chunk.chunks[Chunk.fixups[i].chunk].name))]);
306 end;
307 Diag.finish;
308end Encode.