1 //=== M68kInstrFormats.td - M68k Instruction Formats ---*- tablegen -*-===//
2 // The LLVM Compiler Infrastructure
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //===----------------------------------------------------------------------===//
9 /// This file contains M68k instruction formats.
11 /// Since M68k has quite a lot memory addressing modes there are more
12 /// instruction prefixes than just i, r and m:
13 /// TSF Since Form Letter Description
14 /// 00 M68000 Dn or An r any register
15 /// 01 M68000 Dn d data register direct
16 /// 02 M68000 An a address register direct
17 /// 03 M68000 (An) j address register indirect
18 /// 04 M68000 (An)+ o address register indirect with postincrement
19 /// 05 M68000 -(An) e address register indirect with predecrement
20 /// 06 M68000 (i,An) p address register indirect with displacement
21 /// 10 M68000 (i,An,Xn.L) f address register indirect with index and scale = 1
22 /// 07 M68000 (i,An,Xn.W) F address register indirect with index and scale = 1
23 /// 12 M68020 (i,An,Xn.L,SCALE) g address register indirect with index
24 /// 11 M68020 (i,An,Xn.W,SCALE) G address register indirect with index
25 /// 14 M68020 ([bd,An],Xn.L,SCALE,od) u memory indirect postindexed mode
26 /// 13 M68020 ([bd,An],Xn.W,SCALE,od) U memory indirect postindexed mode
27 /// 16 M68020 ([bd,An,Xn.L,SCALE],od) v memory indirect preindexed mode
28 /// 15 M68020 ([bd,An,Xn.W,SCALE],od) V memory indirect preindexed mode
29 /// 20 M68000 abs.L b absolute long address
30 /// 17 M68000 abs.W B absolute short address
31 /// 21 M68000 (i,PC) q program counter with displacement
32 /// 23 M68000 (i,PC,Xn.L) k program counter with index and scale = 1
33 /// 22 M68000 (i,PC,Xn.W) K program counter with index and scale = 1
34 /// 25 M68020 (i,PC,Xn.L,SCALE) l program counter with index
35 /// 24 M68020 (i,PC,Xn.W,SCALE) L program counter with index
36 /// 27 M68020 ([bd,PC],Xn.L,SCALE,od) x program counter memory indirect postindexed mode
37 /// 26 M68020 ([bd,PC],Xn.W,SCALE,od) X program counter memory indirect postindexed mode
38 /// 31 M68020 ([bd,PC,Xn.L,SCALE],od) y program counter memory indirect preindexed mode
39 /// 30 M68020 ([bd,PC,Xn.W,SCALE],od) Y program counter memory indirect preindexed mode
40 /// 32 M68000 #immediate i immediate data
42 /// NOTE that long form is always lowercase, word variants are capitalized
44 /// Operand can be qualified with size where appropriate to force a particular
45 /// instruction encoding, e.g.:
46 /// (i8,An,Xn.W) f8 1 extension word
47 /// (i16,An,Xn.W) f16 2 extension words
48 /// (i32,An,Xn.W) f32 3 extension words
50 /// Form without size qualifier will adapt to operand size automatically, e.g.:
51 /// (i,An,Xn.W) f 1, 2 or 3 extension words
53 /// Some forms already imply a particular size of their operands, e.g.:
54 /// (i,An) p 1 extension word and i is 16bit
56 /// Operand order follows x86 Intel order(destination before source), e.g.:
57 /// MOV8df MOVE (4,A0,D0), D1
59 /// Number after instruction mnemonics determines the size of the data
61 //===----------------------------------------------------------------------===//
63 /// ??? Is it possible to use this stuff for disassembling?
64 /// NOTE 1: In case of conditional beads(DA, DAReg), cond part is able to
65 /// consume any bit, though a more general instructions must be chosen, e.g.
68 //===----------------------------------------------------------------------===//
69 // Encoding primitives
70 //===----------------------------------------------------------------------===//
72 class MxBead<bits<4> type, bit b4 = 0, bit b5 = 0, bit b6 = 0, bit b7 = 0> {
73 bits<8> Value = 0b00000000;
74 let Value{3-0} = type;
81 /// System beads, allow to control beading flow
82 def MxBeadTerm : MxBead<0x0, 0, 0, 0, 0>;
83 def MxBeadIgnore : MxBead<0x0, 1, 0, 0, 0>;
85 /// Add plain bit to the instruction
86 class MxBead1Bit <bits<1> b> : MxBead<0x1, b>;
87 class MxBead2Bits <bits<2> b> : MxBead<0x2, b{0}, b{1}>;
88 class MxBead3Bits <bits<3> b> : MxBead<0x3, b{0}, b{1}, b{2}>;
89 class MxBead4Bits <bits<4> b> : MxBead<0x4, b{0}, b{1}, b{2}, b{3}>;
91 /// bits<3> o - operand number
92 /// bit a - use alternative, used to select index register or
93 /// outer displacement/immediate
94 /// suffix NP means non-padded
95 class MxBeadDAReg <bits<3> o, bit a = 0> : MxBead<0x5, o{0}, o{1}, o{2}, a>;
96 class MxBeadDA <bits<3> o, bit a = 0> : MxBead<0x6, o{0}, o{1}, o{2}, a>;
97 class MxBeadReg <bits<3> o, bit a = 0> : MxBead<0x7, o{0}, o{1}, o{2}, a>;
98 class MxBeadDReg <bits<3> o, bit a = 0> : MxBead<0x8, o{0}, o{1}, o{2}, a>;
99 class MxBead8Disp <bits<3> o, bit a = 0> : MxBead<0x9, o{0}, o{1}, o{2}, a>;
101 /// Add Immediate to the instruction. 8-bit version is padded with zeros to fit
103 class MxBead8Imm <bits<3> o, bit a = 0> : MxBead<0xA, o{0}, o{1}, o{2}, a>;
104 class MxBead16Imm <bits<3> o, bit a = 0> : MxBead<0xB, o{0}, o{1}, o{2}, a>;
105 class MxBead32Imm <bits<3> o, bit a = 0> : MxBead<0xC, o{0}, o{1}, o{2}, a>;
107 /// Encodes an immediate 0-7(alt. 1-8) into 3 bit field
108 class MxBead3Imm <bits<3> o, bit a = 0> : MxBead<0xD, o{0}, o{1}, o{2}, a>;
111 class MxEncoding<MxBead n0 = MxBeadTerm, MxBead n1 = MxBeadTerm,
112 MxBead n2 = MxBeadTerm, MxBead n3 = MxBeadTerm,
113 MxBead n4 = MxBeadTerm, MxBead n5 = MxBeadTerm,
114 MxBead n6 = MxBeadTerm, MxBead n7 = MxBeadTerm,
115 MxBead n8 = MxBeadTerm, MxBead n9 = MxBeadTerm,
116 MxBead n10 = MxBeadTerm, MxBead n11 = MxBeadTerm,
117 MxBead n12 = MxBeadTerm, MxBead n13 = MxBeadTerm,
118 MxBead n14 = MxBeadTerm, MxBead n15 = MxBeadTerm,
119 MxBead n16 = MxBeadTerm, MxBead n17 = MxBeadTerm,
120 MxBead n18 = MxBeadTerm, MxBead n19 = MxBeadTerm,
121 MxBead n20 = MxBeadTerm, MxBead n21 = MxBeadTerm,
122 MxBead n22 = MxBeadTerm, MxBead n23 = MxBeadTerm> {
124 let Value{7-0} = n0.Value;
125 let Value{15-8} = n1.Value;
126 let Value{23-16} = n2.Value;
127 let Value{31-24} = n3.Value;
128 let Value{39-32} = n4.Value;
129 let Value{47-40} = n5.Value;
130 let Value{55-48} = n6.Value;
131 let Value{63-56} = n7.Value;
132 let Value{71-64} = n8.Value;
133 let Value{79-72} = n9.Value;
134 let Value{87-80} = n10.Value;
135 let Value{95-88} = n11.Value;
136 let Value{103-96} = n12.Value;
137 let Value{111-104} = n13.Value;
138 let Value{119-112} = n14.Value;
139 let Value{127-120} = n15.Value;
140 let Value{135-128} = n16.Value;
141 let Value{143-136} = n17.Value;
142 let Value{151-144} = n18.Value;
143 let Value{159-152} = n19.Value;
144 let Value{167-160} = n20.Value;
145 let Value{175-168} = n21.Value;
146 let Value{183-176} = n22.Value;
147 let Value{191-184} = n23.Value;
150 class MxEncFixed<bits<16> value> : MxEncoding {
151 let Value{7-0} = MxBead4Bits<value{3-0}>.Value;
152 let Value{15-8} = MxBead4Bits<value{7-4}>.Value;
153 let Value{23-16} = MxBead4Bits<value{11-8}>.Value;
154 let Value{31-24} = MxBead4Bits<value{15-12}>.Value;
157 //===----------------------------------------------------------------------===//
158 // Encoding composites
160 // These must be lowered to MxEncoding by instr specific wrappers
162 // HERE BE DRAGONS...
163 //===----------------------------------------------------------------------===//
165 class MxEncByte<bits<8> value> : MxEncoding {
166 MxBead4Bits LO = MxBead4Bits<value{3-0}>;
167 MxBead4Bits HI = MxBead4Bits<value{7-4}>;
170 def MxEncEmpty : MxEncoding;
173 /// M68k Standard Effective Address layout:
175 /// :-------------------:
176 /// | 5 4 3 | 2 1 0 |
178 /// :-------------------:
180 /// If the EA is a direct register mode, bits 4 and 5 are 0, and the register
181 /// number will be encoded in bit 0 - 3. Since the first address register's
182 /// (A0) register number is 8, we can easily tell data registers from
183 /// address registers by only inspecting bit 3 (i.e. if bit 3 is set, it's an
184 /// address register).
187 /// But MOVE instruction uses reversed layout for destination EA:
189 /// :-------------------:
190 /// | 5 4 3 | 2 1 0 |
192 /// :-------------------:
194 /// And this complicates things a bit because the DA bit is now separated from
195 /// the register and we have to encode those separately using MxBeadDA<opN>
197 class MxEncEA<MxBead reg, MxBead mode, MxBead da = MxBeadIgnore> {
203 // FIXME: Is there a way to factorize the addressing mode suffix (i.e.
204 // 'r', 'd', 'a' etc.) and use something like multiclass to replace?
205 def MxEncEAr_0: MxEncEA<MxBeadDAReg<0>, MxBead2Bits<0b00>>;
206 def MxEncEAd_0: MxEncEA<MxBeadDReg<0>, MxBead2Bits<0b00>, MxBead1Bit<0>>;
207 def MxEncEAa_0: MxEncEA<MxBeadReg<0>, MxBead2Bits<0b00>, MxBead1Bit<1>>;
208 def MxEncEAj_0: MxEncEA<MxBeadReg<0>, MxBead2Bits<0b01>, MxBead1Bit<0>>;
209 def MxEncEAo_0: MxEncEA<MxBeadReg<0>, MxBead2Bits<0b01>, MxBead1Bit<1>>;
210 def MxEncEAe_0: MxEncEA<MxBeadReg<0>, MxBead2Bits<0b10>, MxBead1Bit<0>>;
211 def MxEncEAp_0: MxEncEA<MxBeadReg<0>, MxBead2Bits<0b10>, MxBead1Bit<1>>;
212 def MxEncEAf_0: MxEncEA<MxBeadReg<0>, MxBead2Bits<0b11>, MxBead1Bit<0>>;
214 def MxEncEAa_0_reflected : MxEncEA<MxBeadReg<0>, MxBead3Bits<0b001>>;
215 def MxEncEAr_0_reflected : MxEncEA<MxBeadReg<0>, MxBead2Bits<0b00>, MxBeadDA<0>>;
217 def MxEncEAr_1: MxEncEA<MxBeadDAReg<1>, MxBead2Bits<0b00>>;
218 def MxEncEAd_1: MxEncEA<MxBeadDReg<1>, MxBead2Bits<0b00>, MxBead1Bit<0>>;
219 def MxEncEAa_1: MxEncEA<MxBeadReg<1>, MxBead2Bits<0b00>, MxBead1Bit<1>>;
220 def MxEncEAj_1: MxEncEA<MxBeadReg<1>, MxBead2Bits<0b01>, MxBead1Bit<0>>;
221 def MxEncEAo_1: MxEncEA<MxBeadReg<1>, MxBead2Bits<0b01>, MxBead1Bit<1>>;
222 def MxEncEAe_1: MxEncEA<MxBeadReg<1>, MxBead2Bits<0b10>, MxBead1Bit<0>>;
223 def MxEncEAp_1: MxEncEA<MxBeadReg<1>, MxBead2Bits<0b10>, MxBead1Bit<1>>;
224 def MxEncEAf_1: MxEncEA<MxBeadReg<1>, MxBead2Bits<0b11>, MxBead1Bit<0>>;
226 def MxEncEAr_2: MxEncEA<MxBeadDAReg<2>, MxBead2Bits<0b00>>;
227 def MxEncEAd_2: MxEncEA<MxBeadDReg<2>, MxBead2Bits<0b00>, MxBead1Bit<0>>;
228 def MxEncEAa_2: MxEncEA<MxBeadReg<2>, MxBead2Bits<0b00>, MxBead1Bit<1>>;
229 def MxEncEAj_2: MxEncEA<MxBeadReg<2>, MxBead2Bits<0b01>, MxBead1Bit<0>>;
230 def MxEncEAo_2: MxEncEA<MxBeadReg<2>, MxBead2Bits<0b01>, MxBead1Bit<1>>;
231 def MxEncEAe_2: MxEncEA<MxBeadReg<2>, MxBead2Bits<0b10>, MxBead1Bit<0>>;
232 def MxEncEAp_2: MxEncEA<MxBeadReg<2>, MxBead2Bits<0b10>, MxBead1Bit<1>>;
233 def MxEncEAf_2: MxEncEA<MxBeadReg<2>, MxBead2Bits<0b11>, MxBead1Bit<0>>;
235 def MxEncEAb : MxEncEA<MxBead3Bits<0b001>, MxBead2Bits<0b11>, MxBead1Bit<1>>;
236 def MxEncEAq : MxEncEA<MxBead3Bits<0b010>, MxBead2Bits<0b11>, MxBead1Bit<1>>;
237 def MxEncEAk : MxEncEA<MxBead3Bits<0b011>, MxBead2Bits<0b11>, MxBead1Bit<1>>;
238 def MxEncEAi : MxEncEA<MxBead3Bits<0b100>, MxBead2Bits<0b11>, MxBead1Bit<1>>;
240 // Allows you to specify each bit of opcode
241 class MxEncOpMode<MxBead b0, MxBead b1 = MxBeadIgnore, MxBead b2 = MxBeadIgnore> {
248 def MxOpMode8dEA : MxEncOpMode<MxBead3Bits<0b000>>;
249 def MxOpMode16dEA : MxEncOpMode<MxBead3Bits<0b001>>;
250 def MxOpMode32dEA : MxEncOpMode<MxBead3Bits<0b010>>;
253 def MxOpMode16aEA : MxEncOpMode<MxBead3Bits<0b011>>;
254 def MxOpMode32aEA : MxEncOpMode<MxBead3Bits<0b111>>;
257 // As you might noticed this guy is special... Since M68k differentiates
258 // between Data and Address registers we required to use different OPMODE codes
259 // for Address registers DST operands. One way of dealing with it is to use
260 // separate tablegen instructions, but in this case it would force Register
261 // Allocator to use specific Register Classes and eventually will lead to
262 // superfluous moves. Another approach is to use reg-variadic encoding which will
263 // change OPMODE base on Register Class used. Luckily, all the bits that differ go
264 // from 0 to 1 and can be encoded with MxBeadDA.
265 // Basically, if the register used is of Data type these encodings will be
266 // the same as MxOpMode{16,32}dEA above and used with regular instructions(e.g. ADD,
267 // SUB), but if the register is of Address type the appropriate bits will flip and
268 // the instructions become of *A type(e.g ADDA, SUBA).
269 def MxOpMode16rEA : MxEncOpMode<MxBead1Bit<1>, MxBeadDA<0>, MxBead1Bit<0>>;
270 def MxOpMode32rEA : MxEncOpMode<MxBeadDA<0>, MxBead1Bit<1>, MxBeadDA<0>>;
273 def MxOpMode8EAd : MxEncOpMode<MxBead3Bits<0b100>>;
274 def MxOpMode16EAd : MxEncOpMode<MxBead3Bits<0b101>>;
275 def MxOpMode32EAd : MxEncOpMode<MxBead3Bits<0b110>>;
278 // Represents two types of extension word:
279 // - Imm extension word
280 // - Brief extension word
281 class MxEncExt<MxBead imm = MxBeadIgnore, MxBead b8 = MxBeadIgnore,
282 MxBead scale = MxBeadIgnore, MxBead wl = MxBeadIgnore,
283 MxBead daReg = MxBeadIgnore> {
286 MxBead Scale = scale;
288 MxBead DAReg = daReg;
291 def MxExtEmpty : MxEncExt;
293 // These handle encoding of displacement fields, absolute addresses and
294 // immediate values, since encoding for these categories is mainly the same,
295 // with exception of some weird immediates.
296 def MxExtI8_0 : MxEncExt<MxBead8Imm<0>>;
297 def MxExtI16_0 : MxEncExt<MxBead16Imm<0>>;
298 def MxExtI32_0 : MxEncExt<MxBead32Imm<0>>;
300 def MxExtI8_1 : MxEncExt<MxBead8Imm<1>>;
301 def MxExtI16_1 : MxEncExt<MxBead16Imm<1>>;
302 def MxExtI32_1 : MxEncExt<MxBead32Imm<1>>;
304 def MxExtI8_2 : MxEncExt<MxBead8Imm<2>>;
305 def MxExtI16_2 : MxEncExt<MxBead16Imm<2>>;
306 def MxExtI32_2 : MxEncExt<MxBead32Imm<2>>;
308 // NOTE They are all using Long Xn
309 def MxExtBrief_0 : MxEncExt<MxBead8Disp<0>, MxBead1Bit<0b0>,
310 MxBead2Bits<0b00>, MxBead1Bit<1>,
313 def MxExtBrief_1 : MxEncExt<MxBead8Disp<1>, MxBead1Bit<0b0>,
314 MxBead2Bits<0b00>, MxBead1Bit<1>,
317 def MxExtBrief_2 : MxEncExt<MxBead8Disp<2>, MxBead1Bit<0b0>,
318 MxBead2Bits<0b00>, MxBead1Bit<1>,
321 def MxExtBrief_3 : MxEncExt<MxBead8Disp<3>, MxBead1Bit<0b0>,
322 MxBead2Bits<0b00>, MxBead1Bit<1>,
325 def MxExtBrief_4 : MxEncExt<MxBead8Disp<4>, MxBead1Bit<0b0>,
326 MxBead2Bits<0b00>, MxBead1Bit<1>,
329 class MxEncSize<bits<2> value> : MxBead2Bits<value>;
330 def MxEncSize8 : MxEncSize<0b00>;
331 def MxEncSize16 : MxEncSize<0b01>;
332 def MxEncSize32 : MxEncSize<0b10>;
333 def MxEncSize64 : MxEncSize<0b11>;
335 // M68k INSTRUCTION. Most instructions specify the location of an operand by
336 // using the effective address field in the operation word. The effective address
337 // is composed of two 3-bit fields: the mode field and the register field. The
338 // value in the mode field selects the different address modes. The register
339 // field contains the number of a register. The effective address field may
340 // require additional information to fully specify the operand. This additional
341 // information, called the effective address extension, is contained in the
342 // following word or words and is considered part of the instruction. The
343 // effective address modes are grouped into three categories: register direct,
344 // memory addressing, and special.
345 class MxInst<dag outs, dag ins,
347 list<dag> pattern = [],
348 MxEncoding beads = MxEncEmpty,
349 InstrItinClass itin = NoItinerary>
351 let Namespace = "M68k";
352 let OutOperandList = outs;
353 let InOperandList = ins;
354 let AsmString = asmStr;
355 let Pattern = pattern;
356 let Itinerary = itin;
359 field bits<192> Beads = beads.Value;
364 let UseLogicalOperandMappings = 1;
367 // M68k PSEUDO INSTRUCTION
368 class MxPseudo<dag outs, dag ins, list<dag> pattern = []>
369 : MxInst<outs, ins, "; error: this should not be emitted", pattern> {