assemble_arm.cc revision 0d82948094d9a198e01aa95f64012bdedd5b6fc9
1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "arm_lir.h"
18#include "codegen_arm.h"
19#include "dex/quick/mir_to_lir-inl.h"
20
21namespace art {
22
23/*
24 * opcode: ArmOpcode enum
25 * skeleton: pre-designated bit-pattern for this opcode
26 * k0: key to applying ds/de
27 * ds: dest start bit position
28 * de: dest end bit position
29 * k1: key to applying s1s/s1e
30 * s1s: src1 start bit position
31 * s1e: src1 end bit position
32 * k2: key to applying s2s/s2e
33 * s2s: src2 start bit position
34 * s2e: src2 end bit position
35 * operands: number of operands (for sanity check purposes)
36 * name: mnemonic name
37 * fmt: for pretty-printing
38 */
39#define ENCODING_MAP(opcode, skeleton, k0, ds, de, k1, s1s, s1e, k2, s2s, s2e, \
40                     k3, k3s, k3e, flags, name, fmt, size, fixup) \
41        {skeleton, {{k0, ds, de}, {k1, s1s, s1e}, {k2, s2s, s2e}, \
42                    {k3, k3s, k3e}}, opcode, flags, name, fmt, size, fixup}
43
44/* Instruction dump string format keys: !pf, where "!" is the start
45 * of the key, "p" is which numeric operand to use and "f" is the
46 * print format.
47 *
48 * [p]ositions:
49 *     0 -> operands[0] (dest)
50 *     1 -> operands[1] (src1)
51 *     2 -> operands[2] (src2)
52 *     3 -> operands[3] (extra)
53 *
54 * [f]ormats:
55 *     h -> 4-digit hex
56 *     d -> decimal
57 *     E -> decimal*4
58 *     F -> decimal*2
59 *     c -> branch condition (beq, bne, etc.)
60 *     t -> pc-relative target
61 *     u -> 1st half of bl[x] target
62 *     v -> 2nd half ob bl[x] target
63 *     R -> register list
64 *     s -> single precision floating point register
65 *     S -> double precision floating point register
66 *     m -> Thumb2 modified immediate
67 *     n -> complimented Thumb2 modified immediate
68 *     M -> Thumb2 16-bit zero-extended immediate
69 *     b -> 4-digit binary
70 *     B -> dmb option string (sy, st, ish, ishst, nsh, hshst)
71 *     H -> operand shift
72 *     C -> core register name
73 *     P -> fp cs register list (base of s16)
74 *     Q -> fp cs register list (base of s0)
75 *
76 *  [!] escape.  To insert "!", use "!!"
77 */
78/* NOTE: must be kept in sync with enum ArmOpcode from LIR.h */
79const ArmEncodingMap ArmMir2Lir::EncodingMap[kArmLast] = {
80    ENCODING_MAP(kArm16BitData,    0x0000,
81                 kFmtBitBlt, 15, 0, kFmtUnused, -1, -1, kFmtUnused, -1, -1,
82                 kFmtUnused, -1, -1, IS_UNARY_OP, "data", "0x!0h(!0d)", 2, kFixupNone),
83    ENCODING_MAP(kThumbAdcRR,        0x4140,
84                 kFmtBitBlt, 2, 0, kFmtBitBlt, 5, 3, kFmtUnused, -1, -1,
85                 kFmtUnused, -1, -1,
86                 IS_BINARY_OP | REG_DEF0_USE01 | SETS_CCODES | USES_CCODES,
87                 "adcs", "!0C, !1C", 2, kFixupNone),
88    ENCODING_MAP(kThumbAddRRI3,      0x1c00,
89                 kFmtBitBlt, 2, 0, kFmtBitBlt, 5, 3, kFmtBitBlt, 8, 6,
90                 kFmtUnused, -1, -1,
91                 IS_TERTIARY_OP | REG_DEF0_USE1 | SETS_CCODES,
92                 "adds", "!0C, !1C, #!2d", 2, kFixupNone),
93    ENCODING_MAP(kThumbAddRI8,       0x3000,
94                 kFmtBitBlt, 10, 8, kFmtBitBlt, 7, 0, kFmtUnused, -1, -1,
95                 kFmtUnused, -1, -1,
96                 IS_BINARY_OP | REG_DEF0_USE0 | SETS_CCODES,
97                 "adds", "!0C, !0C, #!1d", 2, kFixupNone),
98    ENCODING_MAP(kThumbAddRRR,       0x1800,
99                 kFmtBitBlt, 2, 0, kFmtBitBlt, 5, 3, kFmtBitBlt, 8, 6,
100                 kFmtUnused, -1, -1,
101                 IS_TERTIARY_OP | REG_DEF0_USE12 | SETS_CCODES,
102                 "adds", "!0C, !1C, !2C", 2, kFixupNone),
103    ENCODING_MAP(kThumbAddRRLH,     0x4440,
104                 kFmtBitBlt, 2, 0, kFmtBitBlt, 5, 3, kFmtUnused, -1, -1,
105                 kFmtUnused, -1, -1, IS_BINARY_OP | REG_DEF0_USE01,
106                 "add", "!0C, !1C", 2, kFixupNone),
107    ENCODING_MAP(kThumbAddRRHL,     0x4480,
108                 kFmtBitBlt, 2, 0, kFmtBitBlt, 5, 3, kFmtUnused, -1, -1,
109                 kFmtUnused, -1, -1, IS_BINARY_OP | REG_DEF0_USE01,
110                 "add", "!0C, !1C", 2, kFixupNone),
111    ENCODING_MAP(kThumbAddRRHH,     0x44c0,
112                 kFmtBitBlt, 2, 0, kFmtBitBlt, 5, 3, kFmtUnused, -1, -1,
113                 kFmtUnused, -1, -1, IS_BINARY_OP | REG_DEF0_USE01,
114                 "add", "!0C, !1C", 2, kFixupNone),
115    ENCODING_MAP(kThumbAddPcRel,    0xa000,
116                 kFmtBitBlt, 10, 8, kFmtBitBlt, 7, 0, kFmtUnused, -1, -1,
117                 kFmtUnused, -1, -1, IS_TERTIARY_OP | IS_BRANCH | NEEDS_FIXUP,
118                 "add", "!0C, pc, #!1E", 2, kFixupLoad),
119    ENCODING_MAP(kThumbAddSpRel,    0xa800,
120                 kFmtBitBlt, 10, 8, kFmtSkip, -1, -1, kFmtBitBlt, 7, 0,
121                 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF_SP | REG_USE_SP,
122                 "add", "!0C, sp, #!2E", 2, kFixupNone),
123    ENCODING_MAP(kThumbAddSpI7,      0xb000,
124                 kFmtBitBlt, 6, 0, kFmtUnused, -1, -1, kFmtUnused, -1, -1,
125                 kFmtUnused, -1, -1, IS_UNARY_OP | REG_DEF_SP | REG_USE_SP,
126                 "add", "sp, #!0d*4", 2, kFixupNone),
127    ENCODING_MAP(kThumbAndRR,        0x4000,
128                 kFmtBitBlt, 2, 0, kFmtBitBlt, 5, 3, kFmtUnused, -1, -1,
129                 kFmtUnused, -1, -1,
130                 IS_BINARY_OP | REG_DEF0_USE01 | SETS_CCODES,
131                 "ands", "!0C, !1C", 2, kFixupNone),
132    ENCODING_MAP(kThumbAsrRRI5,      0x1000,
133                 kFmtBitBlt, 2, 0, kFmtBitBlt, 5, 3, kFmtBitBlt, 10, 6,
134                 kFmtUnused, -1, -1,
135                 IS_TERTIARY_OP | REG_DEF0_USE1 | SETS_CCODES,
136                 "asrs", "!0C, !1C, #!2d", 2, kFixupNone),
137    ENCODING_MAP(kThumbAsrRR,        0x4100,
138                 kFmtBitBlt, 2, 0, kFmtBitBlt, 5, 3, kFmtUnused, -1, -1,
139                 kFmtUnused, -1, -1,
140                 IS_BINARY_OP | REG_DEF0_USE01 | SETS_CCODES,
141                 "asrs", "!0C, !1C", 2, kFixupNone),
142    ENCODING_MAP(kThumbBCond,        0xd000,
143                 kFmtBitBlt, 7, 0, kFmtBitBlt, 11, 8, kFmtUnused, -1, -1,
144                 kFmtUnused, -1, -1, IS_BINARY_OP | IS_BRANCH | USES_CCODES |
145                 NEEDS_FIXUP, "b!1c", "!0t", 2, kFixupCondBranch),
146    ENCODING_MAP(kThumbBUncond,      0xe000,
147                 kFmtBitBlt, 10, 0, kFmtUnused, -1, -1, kFmtUnused, -1, -1,
148                 kFmtUnused, -1, -1, IS_UNARY_OP | IS_BRANCH | NEEDS_FIXUP,
149                 "b", "!0t", 2, kFixupT1Branch),
150    ENCODING_MAP(kThumbBicRR,        0x4380,
151                 kFmtBitBlt, 2, 0, kFmtBitBlt, 5, 3, kFmtUnused, -1, -1,
152                 kFmtUnused, -1, -1,
153                 IS_BINARY_OP | REG_DEF0_USE01 | SETS_CCODES,
154                 "bics", "!0C, !1C", 2, kFixupNone),
155    ENCODING_MAP(kThumbBkpt,          0xbe00,
156                 kFmtBitBlt, 7, 0, kFmtUnused, -1, -1, kFmtUnused, -1, -1,
157                 kFmtUnused, -1, -1, IS_UNARY_OP | IS_BRANCH,
158                 "bkpt", "!0d", 2, kFixupNone),
159    ENCODING_MAP(kThumbBlx1,         0xf000,
160                 kFmtBitBlt, 10, 0, kFmtUnused, -1, -1, kFmtUnused, -1, -1,
161                 kFmtUnused, -1, -1, IS_BINARY_OP | IS_BRANCH | REG_DEF_LR |
162                 NEEDS_FIXUP, "blx_1", "!0u", 2, kFixupBlx1),
163    ENCODING_MAP(kThumbBlx2,         0xe800,
164                 kFmtBitBlt, 10, 0, kFmtUnused, -1, -1, kFmtUnused, -1, -1,
165                 kFmtUnused, -1, -1, IS_BINARY_OP | IS_BRANCH | REG_DEF_LR |
166                 NEEDS_FIXUP, "blx_2", "!0v", 2, kFixupLabel),
167    ENCODING_MAP(kThumbBl1,          0xf000,
168                 kFmtBitBlt, 10, 0, kFmtUnused, -1, -1, kFmtUnused, -1, -1,
169                 kFmtUnused, -1, -1, IS_UNARY_OP | IS_BRANCH | REG_DEF_LR | NEEDS_FIXUP,
170                 "bl_1", "!0u", 2, kFixupBl1),
171    ENCODING_MAP(kThumbBl2,          0xf800,
172                 kFmtBitBlt, 10, 0, kFmtUnused, -1, -1, kFmtUnused, -1, -1,
173                 kFmtUnused, -1, -1, IS_UNARY_OP | IS_BRANCH | REG_DEF_LR | NEEDS_FIXUP,
174                 "bl_2", "!0v", 2, kFixupLabel),
175    ENCODING_MAP(kThumbBlxR,         0x4780,
176                 kFmtBitBlt, 6, 3, kFmtUnused, -1, -1, kFmtUnused, -1, -1,
177                 kFmtUnused, -1, -1,
178                 IS_UNARY_OP | REG_USE0 | IS_BRANCH | REG_DEF_LR,
179                 "blx", "!0C", 2, kFixupNone),
180    ENCODING_MAP(kThumbBx,            0x4700,
181                 kFmtBitBlt, 6, 3, kFmtUnused, -1, -1, kFmtUnused, -1, -1,
182                 kFmtUnused, -1, -1, IS_UNARY_OP | IS_BRANCH,
183                 "bx", "!0C", 2, kFixupNone),
184    ENCODING_MAP(kThumbCmnRR,        0x42c0,
185                 kFmtBitBlt, 2, 0, kFmtBitBlt, 5, 3, kFmtUnused, -1, -1,
186                 kFmtUnused, -1, -1, IS_BINARY_OP | REG_USE01 | SETS_CCODES,
187                 "cmn", "!0C, !1C", 2, kFixupNone),
188    ENCODING_MAP(kThumbCmpRI8,       0x2800,
189                 kFmtBitBlt, 10, 8, kFmtBitBlt, 7, 0, kFmtUnused, -1, -1,
190                 kFmtUnused, -1, -1, IS_BINARY_OP | REG_USE0 | SETS_CCODES,
191                 "cmp", "!0C, #!1d", 2, kFixupNone),
192    ENCODING_MAP(kThumbCmpRR,        0x4280,
193                 kFmtBitBlt, 2, 0, kFmtBitBlt, 5, 3, kFmtUnused, -1, -1,
194                 kFmtUnused, -1, -1, IS_BINARY_OP | REG_USE01 | SETS_CCODES,
195                 "cmp", "!0C, !1C", 2, kFixupNone),
196    ENCODING_MAP(kThumbCmpLH,        0x4540,
197                 kFmtBitBlt, 2, 0, kFmtBitBlt, 5, 3, kFmtUnused, -1, -1,
198                 kFmtUnused, -1, -1, IS_BINARY_OP | REG_USE01 | SETS_CCODES,
199                 "cmp", "!0C, !1C", 2, kFixupNone),
200    ENCODING_MAP(kThumbCmpHL,        0x4580,
201                 kFmtBitBlt, 2, 0, kFmtBitBlt, 5, 3, kFmtUnused, -1, -1,
202                 kFmtUnused, -1, -1, IS_BINARY_OP | REG_USE01 | SETS_CCODES,
203                 "cmp", "!0C, !1C", 2, kFixupNone),
204    ENCODING_MAP(kThumbCmpHH,        0x45c0,
205                 kFmtBitBlt, 2, 0, kFmtBitBlt, 5, 3, kFmtUnused, -1, -1,
206                 kFmtUnused, -1, -1, IS_BINARY_OP | REG_USE01 | SETS_CCODES,
207                 "cmp", "!0C, !1C", 2, kFixupNone),
208    ENCODING_MAP(kThumbEorRR,        0x4040,
209                 kFmtBitBlt, 2, 0, kFmtBitBlt, 5, 3, kFmtUnused, -1, -1,
210                 kFmtUnused, -1, -1,
211                 IS_BINARY_OP | REG_DEF0_USE01 | SETS_CCODES,
212                 "eors", "!0C, !1C", 2, kFixupNone),
213    ENCODING_MAP(kThumbLdmia,         0xc800,
214                 kFmtBitBlt, 10, 8, kFmtBitBlt, 7, 0, kFmtUnused, -1, -1,
215                 kFmtUnused, -1, -1,
216                 IS_BINARY_OP | REG_DEF0_USE0 | REG_DEF_LIST1 | IS_LOAD,
217                 "ldmia", "!0C!!, <!1R>", 2, kFixupNone),
218    ENCODING_MAP(kThumbLdrRRI5,      0x6800,
219                 kFmtBitBlt, 2, 0, kFmtBitBlt, 5, 3, kFmtBitBlt, 10, 6,
220                 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE1 | IS_LOAD,
221                 "ldr", "!0C, [!1C, #!2E]", 2, kFixupNone),
222    ENCODING_MAP(kThumbLdrRRR,       0x5800,
223                 kFmtBitBlt, 2, 0, kFmtBitBlt, 5, 3, kFmtBitBlt, 8, 6,
224                 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE12 | IS_LOAD,
225                 "ldr", "!0C, [!1C, !2C]", 2, kFixupNone),
226    ENCODING_MAP(kThumbLdrPcRel,    0x4800,
227                 kFmtBitBlt, 10, 8, kFmtBitBlt, 7, 0, kFmtUnused, -1, -1,
228                 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0 | REG_USE_PC
229                 | IS_LOAD | NEEDS_FIXUP, "ldr", "!0C, [pc, #!1E]", 2, kFixupLoad),
230    ENCODING_MAP(kThumbLdrSpRel,    0x9800,
231                 kFmtBitBlt, 10, 8, kFmtSkip, -1, -1, kFmtBitBlt, 7, 0,
232                 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0 | REG_USE_SP
233                 | IS_LOAD, "ldr", "!0C, [sp, #!2E]", 2, kFixupNone),
234    ENCODING_MAP(kThumbLdrbRRI5,     0x7800,
235                 kFmtBitBlt, 2, 0, kFmtBitBlt, 5, 3, kFmtBitBlt, 10, 6,
236                 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE1 | IS_LOAD,
237                 "ldrb", "!0C, [!1C, #2d]", 2, kFixupNone),
238    ENCODING_MAP(kThumbLdrbRRR,      0x5c00,
239                 kFmtBitBlt, 2, 0, kFmtBitBlt, 5, 3, kFmtBitBlt, 8, 6,
240                 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE12 | IS_LOAD,
241                 "ldrb", "!0C, [!1C, !2C]", 2, kFixupNone),
242    ENCODING_MAP(kThumbLdrhRRI5,     0x8800,
243                 kFmtBitBlt, 2, 0, kFmtBitBlt, 5, 3, kFmtBitBlt, 10, 6,
244                 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE1 | IS_LOAD,
245                 "ldrh", "!0C, [!1C, #!2F]", 2, kFixupNone),
246    ENCODING_MAP(kThumbLdrhRRR,      0x5a00,
247                 kFmtBitBlt, 2, 0, kFmtBitBlt, 5, 3, kFmtBitBlt, 8, 6,
248                 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE12 | IS_LOAD,
249                 "ldrh", "!0C, [!1C, !2C]", 2, kFixupNone),
250    ENCODING_MAP(kThumbLdrsbRRR,     0x5600,
251                 kFmtBitBlt, 2, 0, kFmtBitBlt, 5, 3, kFmtBitBlt, 8, 6,
252                 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE12 | IS_LOAD,
253                 "ldrsb", "!0C, [!1C, !2C]", 2, kFixupNone),
254    ENCODING_MAP(kThumbLdrshRRR,     0x5e00,
255                 kFmtBitBlt, 2, 0, kFmtBitBlt, 5, 3, kFmtBitBlt, 8, 6,
256                 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE12 | IS_LOAD,
257                 "ldrsh", "!0C, [!1C, !2C]", 2, kFixupNone),
258    ENCODING_MAP(kThumbLslRRI5,      0x0000,
259                 kFmtBitBlt, 2, 0, kFmtBitBlt, 5, 3, kFmtBitBlt, 10, 6,
260                 kFmtUnused, -1, -1,
261                 IS_TERTIARY_OP | REG_DEF0_USE1 | SETS_CCODES,
262                 "lsls", "!0C, !1C, #!2d", 2, kFixupNone),
263    ENCODING_MAP(kThumbLslRR,        0x4080,
264                 kFmtBitBlt, 2, 0, kFmtBitBlt, 5, 3, kFmtUnused, -1, -1,
265                 kFmtUnused, -1, -1,
266                 IS_BINARY_OP | REG_DEF0_USE01 | SETS_CCODES,
267                 "lsls", "!0C, !1C", 2, kFixupNone),
268    ENCODING_MAP(kThumbLsrRRI5,      0x0800,
269                 kFmtBitBlt, 2, 0, kFmtBitBlt, 5, 3, kFmtBitBlt, 10, 6,
270                 kFmtUnused, -1, -1,
271                 IS_TERTIARY_OP | REG_DEF0_USE1 | SETS_CCODES,
272                 "lsrs", "!0C, !1C, #!2d", 2, kFixupNone),
273    ENCODING_MAP(kThumbLsrRR,        0x40c0,
274                 kFmtBitBlt, 2, 0, kFmtBitBlt, 5, 3, kFmtUnused, -1, -1,
275                 kFmtUnused, -1, -1,
276                 IS_BINARY_OP | REG_DEF0_USE01 | SETS_CCODES,
277                 "lsrs", "!0C, !1C", 2, kFixupNone),
278    ENCODING_MAP(kThumbMovImm,       0x2000,
279                 kFmtBitBlt, 10, 8, kFmtBitBlt, 7, 0, kFmtUnused, -1, -1,
280                 kFmtUnused, -1, -1,
281                 IS_BINARY_OP | REG_DEF0 | SETS_CCODES,
282                 "movs", "!0C, #!1d", 2, kFixupNone),
283    ENCODING_MAP(kThumbMovRR,        0x1c00,
284                 kFmtBitBlt, 2, 0, kFmtBitBlt, 5, 3, kFmtUnused, -1, -1,
285                 kFmtUnused, -1, -1,
286                 IS_BINARY_OP | REG_DEF0_USE1 | SETS_CCODES,
287                 "movs", "!0C, !1C", 2, kFixupNone),
288    ENCODING_MAP(kThumbMovRR_H2H,    0x46c0,
289                 kFmtBitBlt, 2, 0, kFmtBitBlt, 5, 3, kFmtUnused, -1, -1,
290                 kFmtUnused, -1, -1, IS_BINARY_OP | REG_DEF0_USE1,
291                 "mov", "!0C, !1C", 2, kFixupNone),
292    ENCODING_MAP(kThumbMovRR_H2L,    0x4640,
293                 kFmtBitBlt, 2, 0, kFmtBitBlt, 5, 3, kFmtUnused, -1, -1,
294                 kFmtUnused, -1, -1, IS_BINARY_OP | REG_DEF0_USE1,
295                 "mov", "!0C, !1C", 2, kFixupNone),
296    ENCODING_MAP(kThumbMovRR_L2H,    0x4680,
297                 kFmtBitBlt, 2, 0, kFmtBitBlt, 5, 3, kFmtUnused, -1, -1,
298                 kFmtUnused, -1, -1, IS_BINARY_OP | REG_DEF0_USE1,
299                 "mov", "!0C, !1C", 2, kFixupNone),
300    ENCODING_MAP(kThumbMul,           0x4340,
301                 kFmtBitBlt, 2, 0, kFmtBitBlt, 5, 3, kFmtUnused, -1, -1,
302                 kFmtUnused, -1, -1,
303                 IS_BINARY_OP | REG_DEF0_USE01 | SETS_CCODES,
304                 "muls", "!0C, !1C", 2, kFixupNone),
305    ENCODING_MAP(kThumbMvn,           0x43c0,
306                 kFmtBitBlt, 2, 0, kFmtBitBlt, 5, 3, kFmtUnused, -1, -1,
307                 kFmtUnused, -1, -1,
308                 IS_BINARY_OP | REG_DEF0_USE1 | SETS_CCODES,
309                 "mvns", "!0C, !1C", 2, kFixupNone),
310    ENCODING_MAP(kThumbNeg,           0x4240,
311                 kFmtBitBlt, 2, 0, kFmtBitBlt, 5, 3, kFmtUnused, -1, -1,
312                 kFmtUnused, -1, -1,
313                 IS_BINARY_OP | REG_DEF0_USE1 | SETS_CCODES,
314                 "negs", "!0C, !1C", 2, kFixupNone),
315    ENCODING_MAP(kThumbOrr,           0x4300,
316                 kFmtBitBlt, 2, 0, kFmtBitBlt, 5, 3, kFmtUnused, -1, -1,
317                 kFmtUnused, -1, -1,
318                 IS_BINARY_OP | REG_DEF0_USE01 | SETS_CCODES,
319                 "orrs", "!0C, !1C", 2, kFixupNone),
320    ENCODING_MAP(kThumbPop,           0xbc00,
321                 kFmtBitBlt, 8, 0, kFmtUnused, -1, -1, kFmtUnused, -1, -1,
322                 kFmtUnused, -1, -1,
323                 IS_UNARY_OP | REG_DEF_SP | REG_USE_SP | REG_DEF_LIST0
324                 | IS_LOAD, "pop", "<!0R>", 2, kFixupNone),
325    ENCODING_MAP(kThumbPush,          0xb400,
326                 kFmtBitBlt, 8, 0, kFmtUnused, -1, -1, kFmtUnused, -1, -1,
327                 kFmtUnused, -1, -1,
328                 IS_UNARY_OP | REG_DEF_SP | REG_USE_SP | REG_USE_LIST0
329                 | IS_STORE, "push", "<!0R>", 2, kFixupNone),
330    ENCODING_MAP(kThumbRorRR,        0x41c0,
331                 kFmtBitBlt, 2, 0, kFmtBitBlt, 5, 3, kFmtUnused, -1, -1,
332                 kFmtUnused, -1, -1,
333                 IS_BINARY_OP | REG_DEF0_USE01 | SETS_CCODES,
334                 "rors", "!0C, !1C", 2, kFixupNone),
335    ENCODING_MAP(kThumbSbc,           0x4180,
336                 kFmtBitBlt, 2, 0, kFmtBitBlt, 5, 3, kFmtUnused, -1, -1,
337                 kFmtUnused, -1, -1,
338                 IS_BINARY_OP | REG_DEF0_USE01 | USES_CCODES | SETS_CCODES,
339                 "sbcs", "!0C, !1C", 2, kFixupNone),
340    ENCODING_MAP(kThumbStmia,         0xc000,
341                 kFmtBitBlt, 10, 8, kFmtBitBlt, 7, 0, kFmtUnused, -1, -1,
342                 kFmtUnused, -1, -1,
343                 IS_BINARY_OP | REG_DEF0 | REG_USE0 | REG_USE_LIST1 | IS_STORE,
344                 "stmia", "!0C!!, <!1R>", 2, kFixupNone),
345    ENCODING_MAP(kThumbStrRRI5,      0x6000,
346                 kFmtBitBlt, 2, 0, kFmtBitBlt, 5, 3, kFmtBitBlt, 10, 6,
347                 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_USE01 | IS_STORE,
348                 "str", "!0C, [!1C, #!2E]", 2, kFixupNone),
349    ENCODING_MAP(kThumbStrRRR,       0x5000,
350                 kFmtBitBlt, 2, 0, kFmtBitBlt, 5, 3, kFmtBitBlt, 8, 6,
351                 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_USE012 | IS_STORE,
352                 "str", "!0C, [!1C, !2C]", 2, kFixupNone),
353    ENCODING_MAP(kThumbStrSpRel,    0x9000,
354                 kFmtBitBlt, 10, 8, kFmtSkip, -1, -1, kFmtBitBlt, 7, 0,
355                 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_USE0 | REG_USE_SP
356                 | IS_STORE, "str", "!0C, [sp, #!2E]", 2, kFixupNone),
357    ENCODING_MAP(kThumbStrbRRI5,     0x7000,
358                 kFmtBitBlt, 2, 0, kFmtBitBlt, 5, 3, kFmtBitBlt, 10, 6,
359                 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_USE01 | IS_STORE,
360                 "strb", "!0C, [!1C, #!2d]", 2, kFixupNone),
361    ENCODING_MAP(kThumbStrbRRR,      0x5400,
362                 kFmtBitBlt, 2, 0, kFmtBitBlt, 5, 3, kFmtBitBlt, 8, 6,
363                 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_USE012 | IS_STORE,
364                 "strb", "!0C, [!1C, !2C]", 2, kFixupNone),
365    ENCODING_MAP(kThumbStrhRRI5,     0x8000,
366                 kFmtBitBlt, 2, 0, kFmtBitBlt, 5, 3, kFmtBitBlt, 10, 6,
367                 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_USE01 | IS_STORE,
368                 "strh", "!0C, [!1C, #!2F]", 2, kFixupNone),
369    ENCODING_MAP(kThumbStrhRRR,      0x5200,
370                 kFmtBitBlt, 2, 0, kFmtBitBlt, 5, 3, kFmtBitBlt, 8, 6,
371                 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_USE012 | IS_STORE,
372                 "strh", "!0C, [!1C, !2C]", 2, kFixupNone),
373    ENCODING_MAP(kThumbSubRRI3,      0x1e00,
374                 kFmtBitBlt, 2, 0, kFmtBitBlt, 5, 3, kFmtBitBlt, 8, 6,
375                 kFmtUnused, -1, -1,
376                 IS_TERTIARY_OP | REG_DEF0_USE1 | SETS_CCODES,
377                 "subs", "!0C, !1C, #!2d", 2, kFixupNone),
378    ENCODING_MAP(kThumbSubRI8,       0x3800,
379                 kFmtBitBlt, 10, 8, kFmtBitBlt, 7, 0, kFmtUnused, -1, -1,
380                 kFmtUnused, -1, -1,
381                 IS_BINARY_OP | REG_DEF0_USE0 | SETS_CCODES,
382                 "subs", "!0C, #!1d", 2, kFixupNone),
383    ENCODING_MAP(kThumbSubRRR,       0x1a00,
384                 kFmtBitBlt, 2, 0, kFmtBitBlt, 5, 3, kFmtBitBlt, 8, 6,
385                 kFmtUnused, -1, -1,
386                 IS_TERTIARY_OP | REG_DEF0_USE12 | SETS_CCODES,
387                 "subs", "!0C, !1C, !2C", 2, kFixupNone),
388    ENCODING_MAP(kThumbSubSpI7,      0xb080,
389                 kFmtBitBlt, 6, 0, kFmtUnused, -1, -1, kFmtUnused, -1, -1,
390                 kFmtUnused, -1, -1,
391                 IS_UNARY_OP | REG_DEF_SP | REG_USE_SP,
392                 "sub", "sp, #!0d*4", 2, kFixupNone),
393    ENCODING_MAP(kThumbSwi,           0xdf00,
394                 kFmtBitBlt, 7, 0, kFmtUnused, -1, -1, kFmtUnused, -1, -1,
395                 kFmtUnused, -1, -1, IS_UNARY_OP | IS_BRANCH,
396                 "swi", "!0d", 2, kFixupNone),
397    ENCODING_MAP(kThumbTst,           0x4200,
398                 kFmtBitBlt, 2, 0, kFmtBitBlt, 5, 3, kFmtUnused, -1, -1,
399                 kFmtUnused, -1, -1, IS_UNARY_OP | REG_USE01 | SETS_CCODES,
400                 "tst", "!0C, !1C", 2, kFixupNone),
401    ENCODING_MAP(kThumb2Vldrs,       0xed900a00,
402                 kFmtSfp, 22, 12, kFmtBitBlt, 19, 16, kFmtBitBlt, 7, 0,
403                 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE1 | IS_LOAD |
404                 REG_DEF_LR | NEEDS_FIXUP, "vldr", "!0s, [!1C, #!2E]", 4, kFixupVLoad),
405    ENCODING_MAP(kThumb2Vldrd,       0xed900b00,
406                 kFmtDfp, 22, 12, kFmtBitBlt, 19, 16, kFmtBitBlt, 7, 0,
407                 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE1 | IS_LOAD |
408                 REG_DEF_LR | NEEDS_FIXUP, "vldr", "!0S, [!1C, #!2E]", 4, kFixupVLoad),
409    ENCODING_MAP(kThumb2Vmuls,        0xee200a00,
410                 kFmtSfp, 22, 12, kFmtSfp, 7, 16, kFmtSfp, 5, 0,
411                 kFmtUnused, -1, -1,
412                 IS_TERTIARY_OP | REG_DEF0_USE12,
413                 "vmuls", "!0s, !1s, !2s", 4, kFixupNone),
414    ENCODING_MAP(kThumb2Vmuld,        0xee200b00,
415                 kFmtDfp, 22, 12, kFmtDfp, 7, 16, kFmtDfp, 5, 0,
416                 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE12,
417                 "vmuld", "!0S, !1S, !2S", 4, kFixupNone),
418    ENCODING_MAP(kThumb2Vstrs,       0xed800a00,
419                 kFmtSfp, 22, 12, kFmtBitBlt, 19, 16, kFmtBitBlt, 7, 0,
420                 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_USE01 | IS_STORE,
421                 "vstr", "!0s, [!1C, #!2E]", 4, kFixupNone),
422    ENCODING_MAP(kThumb2Vstrd,       0xed800b00,
423                 kFmtDfp, 22, 12, kFmtBitBlt, 19, 16, kFmtBitBlt, 7, 0,
424                 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_USE01 | IS_STORE,
425                 "vstr", "!0S, [!1C, #!2E]", 4, kFixupNone),
426    ENCODING_MAP(kThumb2Vsubs,        0xee300a40,
427                 kFmtSfp, 22, 12, kFmtSfp, 7, 16, kFmtSfp, 5, 0,
428                 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE12,
429                 "vsub", "!0s, !1s, !2s", 4, kFixupNone),
430    ENCODING_MAP(kThumb2Vsubd,        0xee300b40,
431                 kFmtDfp, 22, 12, kFmtDfp, 7, 16, kFmtDfp, 5, 0,
432                 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE12,
433                 "vsub", "!0S, !1S, !2S", 4, kFixupNone),
434    ENCODING_MAP(kThumb2Vadds,        0xee300a00,
435                 kFmtSfp, 22, 12, kFmtSfp, 7, 16, kFmtSfp, 5, 0,
436                 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE12,
437                 "vadd", "!0s, !1s, !2s", 4, kFixupNone),
438    ENCODING_MAP(kThumb2Vaddd,        0xee300b00,
439                 kFmtDfp, 22, 12, kFmtDfp, 7, 16, kFmtDfp, 5, 0,
440                 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE12,
441                 "vadd", "!0S, !1S, !2S", 4, kFixupNone),
442    ENCODING_MAP(kThumb2Vdivs,        0xee800a00,
443                 kFmtSfp, 22, 12, kFmtSfp, 7, 16, kFmtSfp, 5, 0,
444                 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE12,
445                 "vdivs", "!0s, !1s, !2s", 4, kFixupNone),
446    ENCODING_MAP(kThumb2Vdivd,        0xee800b00,
447                 kFmtDfp, 22, 12, kFmtDfp, 7, 16, kFmtDfp, 5, 0,
448                 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE12,
449                 "vdivd", "!0S, !1S, !2S", 4, kFixupNone),
450    ENCODING_MAP(kThumb2VcvtIF,       0xeeb80ac0,
451                 kFmtSfp, 22, 12, kFmtSfp, 5, 0, kFmtUnused, -1, -1,
452                 kFmtUnused, -1, -1, IS_BINARY_OP | REG_DEF0_USE1,
453                 "vcvt.f32", "!0s, !1s", 4, kFixupNone),
454    ENCODING_MAP(kThumb2VcvtID,       0xeeb80bc0,
455                 kFmtDfp, 22, 12, kFmtSfp, 5, 0, kFmtUnused, -1, -1,
456                 kFmtUnused, -1, -1, IS_BINARY_OP | REG_DEF0_USE1,
457                 "vcvt.f64", "!0S, !1s", 4, kFixupNone),
458    ENCODING_MAP(kThumb2VcvtFI,       0xeebd0ac0,
459                 kFmtSfp, 22, 12, kFmtSfp, 5, 0, kFmtUnused, -1, -1,
460                 kFmtUnused, -1, -1, IS_BINARY_OP | REG_DEF0_USE1,
461                 "vcvt.s32.f32 ", "!0s, !1s", 4, kFixupNone),
462    ENCODING_MAP(kThumb2VcvtDI,       0xeebd0bc0,
463                 kFmtSfp, 22, 12, kFmtDfp, 5, 0, kFmtUnused, -1, -1,
464                 kFmtUnused, -1, -1, IS_BINARY_OP | REG_DEF0_USE1,
465                 "vcvt.s32.f64 ", "!0s, !1S", 4, kFixupNone),
466    ENCODING_MAP(kThumb2VcvtFd,       0xeeb70ac0,
467                 kFmtDfp, 22, 12, kFmtSfp, 5, 0, kFmtUnused, -1, -1,
468                 kFmtUnused, -1, -1, IS_BINARY_OP | REG_DEF0_USE1,
469                 "vcvt.f64.f32 ", "!0S, !1s", 4, kFixupNone),
470    ENCODING_MAP(kThumb2VcvtDF,       0xeeb70bc0,
471                 kFmtSfp, 22, 12, kFmtDfp, 5, 0, kFmtUnused, -1, -1,
472                 kFmtUnused, -1, -1, IS_BINARY_OP | REG_DEF0_USE1,
473                 "vcvt.f32.f64 ", "!0s, !1S", 4, kFixupNone),
474    ENCODING_MAP(kThumb2Vsqrts,       0xeeb10ac0,
475                 kFmtSfp, 22, 12, kFmtSfp, 5, 0, kFmtUnused, -1, -1,
476                 kFmtUnused, -1, -1, IS_BINARY_OP | REG_DEF0_USE1,
477                 "vsqrt.f32 ", "!0s, !1s", 4, kFixupNone),
478    ENCODING_MAP(kThumb2Vsqrtd,       0xeeb10bc0,
479                 kFmtDfp, 22, 12, kFmtDfp, 5, 0, kFmtUnused, -1, -1,
480                 kFmtUnused, -1, -1, IS_BINARY_OP | REG_DEF0_USE1,
481                 "vsqrt.f64 ", "!0S, !1S", 4, kFixupNone),
482    ENCODING_MAP(kThumb2MovImmShift, 0xf04f0000, /* no setflags encoding */
483                 kFmtBitBlt, 11, 8, kFmtModImm, -1, -1, kFmtUnused, -1, -1,
484                 kFmtUnused, -1, -1, IS_BINARY_OP | REG_DEF0,
485                 "mov", "!0C, #!1m", 4, kFixupNone),
486    ENCODING_MAP(kThumb2MovImm16,       0xf2400000,
487                 kFmtBitBlt, 11, 8, kFmtImm16, -1, -1, kFmtUnused, -1, -1,
488                 kFmtUnused, -1, -1, IS_BINARY_OP | REG_DEF0,
489                 "mov", "!0C, #!1M", 4, kFixupNone),
490    ENCODING_MAP(kThumb2StrRRI12,       0xf8c00000,
491                 kFmtBitBlt, 15, 12, kFmtBitBlt, 19, 16, kFmtBitBlt, 11, 0,
492                 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_USE01 | IS_STORE,
493                 "str", "!0C, [!1C, #!2d]", 4, kFixupNone),
494    ENCODING_MAP(kThumb2LdrRRI12,       0xf8d00000,
495                 kFmtBitBlt, 15, 12, kFmtBitBlt, 19, 16, kFmtBitBlt, 11, 0,
496                 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE1 | IS_LOAD,
497                 "ldr", "!0C, [!1C, #!2d]", 4, kFixupNone),
498    ENCODING_MAP(kThumb2StrRRI8Predec,       0xf8400c00,
499                 kFmtBitBlt, 15, 12, kFmtBitBlt, 19, 16, kFmtBitBlt, 8, 0,
500                 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_USE01 | IS_STORE,
501                 "str", "!0C, [!1C, #-!2d]", 4, kFixupNone),
502    ENCODING_MAP(kThumb2LdrRRI8Predec,       0xf8500c00,
503                 kFmtBitBlt, 15, 12, kFmtBitBlt, 19, 16, kFmtBitBlt, 8, 0,
504                 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE1 | IS_LOAD,
505                 "ldr", "!0C, [!1C, #-!2d]", 4, kFixupNone),
506    ENCODING_MAP(kThumb2Cbnz,       0xb900, /* Note: does not affect flags */
507                 kFmtBitBlt, 2, 0, kFmtImm6, -1, -1, kFmtUnused, -1, -1,
508                 kFmtUnused, -1, -1, IS_BINARY_OP | REG_USE0 | IS_BRANCH |
509                 NEEDS_FIXUP, "cbnz", "!0C,!1t", 2, kFixupCBxZ),
510    ENCODING_MAP(kThumb2Cbz,       0xb100, /* Note: does not affect flags */
511                 kFmtBitBlt, 2, 0, kFmtImm6, -1, -1, kFmtUnused, -1, -1,
512                 kFmtUnused, -1, -1, IS_BINARY_OP | REG_USE0 | IS_BRANCH |
513                 NEEDS_FIXUP, "cbz", "!0C,!1t", 2, kFixupCBxZ),
514    ENCODING_MAP(kThumb2AddRRI12,       0xf2000000,
515                 kFmtBitBlt, 11, 8, kFmtBitBlt, 19, 16, kFmtImm12, -1, -1,
516                 kFmtUnused, -1, -1,
517                 IS_TERTIARY_OP | REG_DEF0_USE1,/* Note: doesn't affect flags */
518                 "add", "!0C,!1C,#!2d", 4, kFixupNone),
519    ENCODING_MAP(kThumb2MovRR,       0xea4f0000, /* no setflags encoding */
520                 kFmtBitBlt, 11, 8, kFmtBitBlt, 3, 0, kFmtUnused, -1, -1,
521                 kFmtUnused, -1, -1, IS_BINARY_OP | REG_DEF0_USE1,
522                 "mov", "!0C, !1C", 4, kFixupNone),
523    ENCODING_MAP(kThumb2Vmovs,       0xeeb00a40,
524                 kFmtSfp, 22, 12, kFmtSfp, 5, 0, kFmtUnused, -1, -1,
525                 kFmtUnused, -1, -1, IS_BINARY_OP | REG_DEF0_USE1,
526                 "vmov.f32 ", " !0s, !1s", 4, kFixupNone),
527    ENCODING_MAP(kThumb2Vmovd,       0xeeb00b40,
528                 kFmtDfp, 22, 12, kFmtDfp, 5, 0, kFmtUnused, -1, -1,
529                 kFmtUnused, -1, -1, IS_BINARY_OP | REG_DEF0_USE1,
530                 "vmov.f64 ", " !0S, !1S", 4, kFixupNone),
531    ENCODING_MAP(kThumb2Ldmia,         0xe8900000,
532                 kFmtBitBlt, 19, 16, kFmtBitBlt, 15, 0, kFmtUnused, -1, -1,
533                 kFmtUnused, -1, -1,
534                 IS_BINARY_OP | REG_DEF0_USE0 | REG_DEF_LIST1 | IS_LOAD,
535                 "ldmia", "!0C!!, <!1R>", 4, kFixupNone),
536    ENCODING_MAP(kThumb2Stmia,         0xe8800000,
537                 kFmtBitBlt, 19, 16, kFmtBitBlt, 15, 0, kFmtUnused, -1, -1,
538                 kFmtUnused, -1, -1,
539                 IS_BINARY_OP | REG_DEF0_USE0 | REG_USE_LIST1 | IS_STORE,
540                 "stmia", "!0C!!, <!1R>", 4, kFixupNone),
541    ENCODING_MAP(kThumb2AddRRR,  0xeb100000, /* setflags encoding */
542                 kFmtBitBlt, 11, 8, kFmtBitBlt, 19, 16, kFmtBitBlt, 3, 0,
543                 kFmtShift, -1, -1,
544                 IS_QUAD_OP | REG_DEF0_USE12 | SETS_CCODES,
545                 "adds", "!0C, !1C, !2C!3H", 4, kFixupNone),
546    ENCODING_MAP(kThumb2SubRRR,       0xebb00000, /* setflags enconding */
547                 kFmtBitBlt, 11, 8, kFmtBitBlt, 19, 16, kFmtBitBlt, 3, 0,
548                 kFmtShift, -1, -1,
549                 IS_QUAD_OP | REG_DEF0_USE12 | SETS_CCODES,
550                 "subs", "!0C, !1C, !2C!3H", 4, kFixupNone),
551    ENCODING_MAP(kThumb2SbcRRR,       0xeb700000, /* setflags encoding */
552                 kFmtBitBlt, 11, 8, kFmtBitBlt, 19, 16, kFmtBitBlt, 3, 0,
553                 kFmtShift, -1, -1,
554                 IS_QUAD_OP | REG_DEF0_USE12 | USES_CCODES | SETS_CCODES,
555                 "sbcs", "!0C, !1C, !2C!3H", 4, kFixupNone),
556    ENCODING_MAP(kThumb2CmpRR,       0xebb00f00,
557                 kFmtBitBlt, 19, 16, kFmtBitBlt, 3, 0, kFmtShift, -1, -1,
558                 kFmtUnused, -1, -1,
559                 IS_TERTIARY_OP | REG_USE01 | SETS_CCODES,
560                 "cmp", "!0C, !1C", 4, kFixupNone),
561    ENCODING_MAP(kThumb2SubRRI12,       0xf2a00000,
562                 kFmtBitBlt, 11, 8, kFmtBitBlt, 19, 16, kFmtImm12, -1, -1,
563                 kFmtUnused, -1, -1,
564                 IS_TERTIARY_OP | REG_DEF0_USE1,/* Note: doesn't affect flags */
565                 "sub", "!0C,!1C,#!2d", 4, kFixupNone),
566    ENCODING_MAP(kThumb2MvnImm12,  0xf06f0000, /* no setflags encoding */
567                 kFmtBitBlt, 11, 8, kFmtImm12, -1, -1, kFmtUnused, -1, -1,
568                 kFmtUnused, -1, -1, IS_BINARY_OP | REG_DEF0,
569                 "mvn", "!0C, #!1n", 4, kFixupNone),
570    ENCODING_MAP(kThumb2Sel,       0xfaa0f080,
571                 kFmtBitBlt, 11, 8, kFmtBitBlt, 19, 16, kFmtBitBlt, 3, 0,
572                 kFmtUnused, -1, -1,
573                 IS_TERTIARY_OP | REG_DEF0_USE12 | USES_CCODES,
574                 "sel", "!0C, !1C, !2C", 4, kFixupNone),
575    ENCODING_MAP(kThumb2Ubfx,       0xf3c00000,
576                 kFmtBitBlt, 11, 8, kFmtBitBlt, 19, 16, kFmtLsb, -1, -1,
577                 kFmtBWidth, 4, 0, IS_QUAD_OP | REG_DEF0_USE1,
578                 "ubfx", "!0C, !1C, #!2d, #!3d", 4, kFixupNone),
579    ENCODING_MAP(kThumb2Sbfx,       0xf3400000,
580                 kFmtBitBlt, 11, 8, kFmtBitBlt, 19, 16, kFmtLsb, -1, -1,
581                 kFmtBWidth, 4, 0, IS_QUAD_OP | REG_DEF0_USE1,
582                 "sbfx", "!0C, !1C, #!2d, #!3d", 4, kFixupNone),
583    ENCODING_MAP(kThumb2LdrRRR,    0xf8500000,
584                 kFmtBitBlt, 15, 12, kFmtBitBlt, 19, 16, kFmtBitBlt, 3, 0,
585                 kFmtBitBlt, 5, 4, IS_QUAD_OP | REG_DEF0_USE12 | IS_LOAD,
586                 "ldr", "!0C, [!1C, !2C, LSL #!3d]", 4, kFixupNone),
587    ENCODING_MAP(kThumb2LdrhRRR,    0xf8300000,
588                 kFmtBitBlt, 15, 12, kFmtBitBlt, 19, 16, kFmtBitBlt, 3, 0,
589                 kFmtBitBlt, 5, 4, IS_QUAD_OP | REG_DEF0_USE12 | IS_LOAD,
590                 "ldrh", "!0C, [!1C, !2C, LSL #!3d]", 4, kFixupNone),
591    ENCODING_MAP(kThumb2LdrshRRR,    0xf9300000,
592                 kFmtBitBlt, 15, 12, kFmtBitBlt, 19, 16, kFmtBitBlt, 3, 0,
593                 kFmtBitBlt, 5, 4, IS_QUAD_OP | REG_DEF0_USE12 | IS_LOAD,
594                 "ldrsh", "!0C, [!1C, !2C, LSL #!3d]", 4, kFixupNone),
595    ENCODING_MAP(kThumb2LdrbRRR,    0xf8100000,
596                 kFmtBitBlt, 15, 12, kFmtBitBlt, 19, 16, kFmtBitBlt, 3, 0,
597                 kFmtBitBlt, 5, 4, IS_QUAD_OP | REG_DEF0_USE12 | IS_LOAD,
598                 "ldrb", "!0C, [!1C, !2C, LSL #!3d]", 4, kFixupNone),
599    ENCODING_MAP(kThumb2LdrsbRRR,    0xf9100000,
600                 kFmtBitBlt, 15, 12, kFmtBitBlt, 19, 16, kFmtBitBlt, 3, 0,
601                 kFmtBitBlt, 5, 4, IS_QUAD_OP | REG_DEF0_USE12 | IS_LOAD,
602                 "ldrsb", "!0C, [!1C, !2C, LSL #!3d]", 4, kFixupNone),
603    ENCODING_MAP(kThumb2StrRRR,    0xf8400000,
604                 kFmtBitBlt, 15, 12, kFmtBitBlt, 19, 16, kFmtBitBlt, 3, 0,
605                 kFmtBitBlt, 5, 4, IS_QUAD_OP | REG_USE012 | IS_STORE,
606                 "str", "!0C, [!1C, !2C, LSL #!3d]", 4, kFixupNone),
607    ENCODING_MAP(kThumb2StrhRRR,    0xf8200000,
608                 kFmtBitBlt, 15, 12, kFmtBitBlt, 19, 16, kFmtBitBlt, 3, 0,
609                 kFmtBitBlt, 5, 4, IS_QUAD_OP | REG_USE012 | IS_STORE,
610                 "strh", "!0C, [!1C, !2C, LSL #!3d]", 4, kFixupNone),
611    ENCODING_MAP(kThumb2StrbRRR,    0xf8000000,
612                 kFmtBitBlt, 15, 12, kFmtBitBlt, 19, 16, kFmtBitBlt, 3, 0,
613                 kFmtBitBlt, 5, 4, IS_QUAD_OP | REG_USE012 | IS_STORE,
614                 "strb", "!0C, [!1C, !2C, LSL #!3d]", 4, kFixupNone),
615    ENCODING_MAP(kThumb2LdrhRRI12,       0xf8b00000,
616                 kFmtBitBlt, 15, 12, kFmtBitBlt, 19, 16, kFmtBitBlt, 11, 0,
617                 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE1 | IS_LOAD,
618                 "ldrh", "!0C, [!1C, #!2d]", 4, kFixupNone),
619    ENCODING_MAP(kThumb2LdrshRRI12,       0xf9b00000,
620                 kFmtBitBlt, 15, 12, kFmtBitBlt, 19, 16, kFmtBitBlt, 11, 0,
621                 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE1 | IS_LOAD,
622                 "ldrsh", "!0C, [!1C, #!2d]", 4, kFixupNone),
623    ENCODING_MAP(kThumb2LdrbRRI12,       0xf8900000,
624                 kFmtBitBlt, 15, 12, kFmtBitBlt, 19, 16, kFmtBitBlt, 11, 0,
625                 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE1 | IS_LOAD,
626                 "ldrb", "!0C, [!1C, #!2d]", 4, kFixupNone),
627    ENCODING_MAP(kThumb2LdrsbRRI12,       0xf9900000,
628                 kFmtBitBlt, 15, 12, kFmtBitBlt, 19, 16, kFmtBitBlt, 11, 0,
629                 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE1 | IS_LOAD,
630                 "ldrsb", "!0C, [!1C, #!2d]", 4, kFixupNone),
631    ENCODING_MAP(kThumb2StrhRRI12,       0xf8a00000,
632                 kFmtBitBlt, 15, 12, kFmtBitBlt, 19, 16, kFmtBitBlt, 11, 0,
633                 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_USE01 | IS_STORE,
634                 "strh", "!0C, [!1C, #!2d]", 4, kFixupNone),
635    ENCODING_MAP(kThumb2StrbRRI12,       0xf8800000,
636                 kFmtBitBlt, 15, 12, kFmtBitBlt, 19, 16, kFmtBitBlt, 11, 0,
637                 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_USE01 | IS_STORE,
638                 "strb", "!0C, [!1C, #!2d]", 4, kFixupNone),
639    ENCODING_MAP(kThumb2Pop,           0xe8bd0000,
640                 kFmtBitBlt, 15, 0, kFmtUnused, -1, -1, kFmtUnused, -1, -1,
641                 kFmtUnused, -1, -1,
642                 IS_UNARY_OP | REG_DEF_SP | REG_USE_SP | REG_DEF_LIST0
643                 | IS_LOAD | NEEDS_FIXUP, "pop", "<!0R>", 4, kFixupPushPop),
644    ENCODING_MAP(kThumb2Push,          0xe92d0000,
645                 kFmtBitBlt, 15, 0, kFmtUnused, -1, -1, kFmtUnused, -1, -1,
646                 kFmtUnused, -1, -1,
647                 IS_UNARY_OP | REG_DEF_SP | REG_USE_SP | REG_USE_LIST0
648                 | IS_STORE | NEEDS_FIXUP, "push", "<!0R>", 4, kFixupPushPop),
649    ENCODING_MAP(kThumb2CmpRI12, 0xf1b00f00,
650                 kFmtBitBlt, 19, 16, kFmtModImm, -1, -1, kFmtUnused, -1, -1,
651                 kFmtUnused, -1, -1,
652                 IS_BINARY_OP | REG_USE0 | SETS_CCODES,
653                 "cmp", "!0C, #!1m", 4, kFixupNone),
654    ENCODING_MAP(kThumb2AdcRRR,  0xeb500000, /* setflags encoding */
655                 kFmtBitBlt, 11, 8, kFmtBitBlt, 19, 16, kFmtBitBlt, 3, 0,
656                 kFmtShift, -1, -1,
657                 IS_QUAD_OP | REG_DEF0_USE12 | SETS_CCODES,
658                 "adcs", "!0C, !1C, !2C!3H", 4, kFixupNone),
659    ENCODING_MAP(kThumb2AndRRR,  0xea000000,
660                 kFmtBitBlt, 11, 8, kFmtBitBlt, 19, 16, kFmtBitBlt, 3, 0,
661                 kFmtShift, -1, -1, IS_QUAD_OP | REG_DEF0_USE12,
662                 "and", "!0C, !1C, !2C!3H", 4, kFixupNone),
663    ENCODING_MAP(kThumb2BicRRR,  0xea200000,
664                 kFmtBitBlt, 11, 8, kFmtBitBlt, 19, 16, kFmtBitBlt, 3, 0,
665                 kFmtShift, -1, -1, IS_QUAD_OP | REG_DEF0_USE12,
666                 "bic", "!0C, !1C, !2C!3H", 4, kFixupNone),
667    ENCODING_MAP(kThumb2CmnRR,  0xeb000000,
668                 kFmtBitBlt, 19, 16, kFmtBitBlt, 3, 0, kFmtShift, -1, -1,
669                 kFmtUnused, -1, -1,
670                 IS_TERTIARY_OP | REG_DEF0_USE1 | SETS_CCODES,
671                 "cmn", "!0C, !1C, shift !2d", 4, kFixupNone),
672    ENCODING_MAP(kThumb2EorRRR,  0xea800000,
673                 kFmtBitBlt, 11, 8, kFmtBitBlt, 19, 16, kFmtBitBlt, 3, 0,
674                 kFmtShift, -1, -1, IS_QUAD_OP | REG_DEF0_USE12,
675                 "eor", "!0C, !1C, !2C!3H", 4, kFixupNone),
676    ENCODING_MAP(kThumb2MulRRR,  0xfb00f000,
677                 kFmtBitBlt, 11, 8, kFmtBitBlt, 19, 16, kFmtBitBlt, 3, 0,
678                 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE12,
679                 "mul", "!0C, !1C, !2C", 4, kFixupNone),
680    ENCODING_MAP(kThumb2MnvRR,  0xea6f0000,
681                 kFmtBitBlt, 11, 8, kFmtBitBlt, 3, 0, kFmtShift, -1, -1,
682                 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE1,
683                 "mvn", "!0C, !1C, shift !2d", 4, kFixupNone),
684    ENCODING_MAP(kThumb2RsubRRI8,       0xf1d00000,
685                 kFmtBitBlt, 11, 8, kFmtBitBlt, 19, 16, kFmtModImm, -1, -1,
686                 kFmtUnused, -1, -1,
687                 IS_TERTIARY_OP | REG_DEF0_USE1 | SETS_CCODES,
688                 "rsb", "!0C,!1C,#!2m", 4, kFixupNone),
689    ENCODING_MAP(kThumb2NegRR,       0xf1d00000, /* instance of rsub */
690                 kFmtBitBlt, 11, 8, kFmtBitBlt, 19, 16, kFmtUnused, -1, -1,
691                 kFmtUnused, -1, -1,
692                 IS_BINARY_OP | REG_DEF0_USE1 | SETS_CCODES,
693                 "neg", "!0C,!1C", 4, kFixupNone),
694    ENCODING_MAP(kThumb2OrrRRR,  0xea400000,
695                 kFmtBitBlt, 11, 8, kFmtBitBlt, 19, 16, kFmtBitBlt, 3, 0,
696                 kFmtShift, -1, -1, IS_QUAD_OP | REG_DEF0_USE12,
697                 "orr", "!0C, !1C, !2C!3H", 4, kFixupNone),
698    ENCODING_MAP(kThumb2TstRR,       0xea100f00,
699                 kFmtBitBlt, 19, 16, kFmtBitBlt, 3, 0, kFmtShift, -1, -1,
700                 kFmtUnused, -1, -1,
701                 IS_TERTIARY_OP | REG_USE01 | SETS_CCODES,
702                 "tst", "!0C, !1C, shift !2d", 4, kFixupNone),
703    ENCODING_MAP(kThumb2LslRRR,  0xfa00f000,
704                 kFmtBitBlt, 11, 8, kFmtBitBlt, 19, 16, kFmtBitBlt, 3, 0,
705                 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE12,
706                 "lsl", "!0C, !1C, !2C", 4, kFixupNone),
707    ENCODING_MAP(kThumb2LsrRRR,  0xfa20f000,
708                 kFmtBitBlt, 11, 8, kFmtBitBlt, 19, 16, kFmtBitBlt, 3, 0,
709                 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE12,
710                 "lsr", "!0C, !1C, !2C", 4, kFixupNone),
711    ENCODING_MAP(kThumb2AsrRRR,  0xfa40f000,
712                 kFmtBitBlt, 11, 8, kFmtBitBlt, 19, 16, kFmtBitBlt, 3, 0,
713                 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE12,
714                 "asr", "!0C, !1C, !2C", 4, kFixupNone),
715    ENCODING_MAP(kThumb2RorRRR,  0xfa60f000,
716                 kFmtBitBlt, 11, 8, kFmtBitBlt, 19, 16, kFmtBitBlt, 3, 0,
717                 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE12,
718                 "ror", "!0C, !1C, !2C", 4, kFixupNone),
719    ENCODING_MAP(kThumb2LslRRI5,  0xea4f0000,
720                 kFmtBitBlt, 11, 8, kFmtBitBlt, 3, 0, kFmtShift5, -1, -1,
721                 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE1,
722                 "lsl", "!0C, !1C, #!2d", 4, kFixupNone),
723    ENCODING_MAP(kThumb2LsrRRI5,  0xea4f0010,
724                 kFmtBitBlt, 11, 8, kFmtBitBlt, 3, 0, kFmtShift5, -1, -1,
725                 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE1,
726                 "lsr", "!0C, !1C, #!2d", 4, kFixupNone),
727    ENCODING_MAP(kThumb2AsrRRI5,  0xea4f0020,
728                 kFmtBitBlt, 11, 8, kFmtBitBlt, 3, 0, kFmtShift5, -1, -1,
729                 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE1,
730                 "asr", "!0C, !1C, #!2d", 4, kFixupNone),
731    ENCODING_MAP(kThumb2RorRRI5,  0xea4f0030,
732                 kFmtBitBlt, 11, 8, kFmtBitBlt, 3, 0, kFmtShift5, -1, -1,
733                 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE1,
734                 "ror", "!0C, !1C, #!2d", 4, kFixupNone),
735    ENCODING_MAP(kThumb2BicRRI8,  0xf0200000,
736                 kFmtBitBlt, 11, 8, kFmtBitBlt, 19, 16, kFmtModImm, -1, -1,
737                 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE1,
738                 "bic", "!0C, !1C, #!2m", 4, kFixupNone),
739    ENCODING_MAP(kThumb2AndRRI8,  0xf0000000,
740                 kFmtBitBlt, 11, 8, kFmtBitBlt, 19, 16, kFmtModImm, -1, -1,
741                 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE1,
742                 "and", "!0C, !1C, #!2m", 4, kFixupNone),
743    ENCODING_MAP(kThumb2OrrRRI8,  0xf0400000,
744                 kFmtBitBlt, 11, 8, kFmtBitBlt, 19, 16, kFmtModImm, -1, -1,
745                 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE1,
746                 "orr", "!0C, !1C, #!2m", 4, kFixupNone),
747    ENCODING_MAP(kThumb2EorRRI8,  0xf0800000,
748                 kFmtBitBlt, 11, 8, kFmtBitBlt, 19, 16, kFmtModImm, -1, -1,
749                 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE1,
750                 "eor", "!0C, !1C, #!2m", 4, kFixupNone),
751    ENCODING_MAP(kThumb2AddRRI8,  0xf1100000,
752                 kFmtBitBlt, 11, 8, kFmtBitBlt, 19, 16, kFmtModImm, -1, -1,
753                 kFmtUnused, -1, -1,
754                 IS_TERTIARY_OP | REG_DEF0_USE1 | SETS_CCODES,
755                 "adds", "!0C, !1C, #!2m", 4, kFixupNone),
756    ENCODING_MAP(kThumb2AdcRRI8,  0xf1500000,
757                 kFmtBitBlt, 11, 8, kFmtBitBlt, 19, 16, kFmtModImm, -1, -1,
758                 kFmtUnused, -1, -1,
759                 IS_TERTIARY_OP | REG_DEF0_USE1 | SETS_CCODES | USES_CCODES,
760                 "adcs", "!0C, !1C, #!2m", 4, kFixupNone),
761    ENCODING_MAP(kThumb2SubRRI8,  0xf1b00000,
762                 kFmtBitBlt, 11, 8, kFmtBitBlt, 19, 16, kFmtModImm, -1, -1,
763                 kFmtUnused, -1, -1,
764                 IS_TERTIARY_OP | REG_DEF0_USE1 | SETS_CCODES,
765                 "subs", "!0C, !1C, #!2m", 4, kFixupNone),
766    ENCODING_MAP(kThumb2SbcRRI8,  0xf1700000,
767                 kFmtBitBlt, 11, 8, kFmtBitBlt, 19, 16, kFmtModImm, -1, -1,
768                 kFmtUnused, -1, -1,
769                 IS_TERTIARY_OP | REG_DEF0_USE1 | SETS_CCODES | USES_CCODES,
770                 "sbcs", "!0C, !1C, #!2m", 4, kFixupNone),
771    ENCODING_MAP(kThumb2It,  0xbf00,
772                 kFmtBitBlt, 7, 4, kFmtBitBlt, 3, 0, kFmtModImm, -1, -1,
773                 kFmtUnused, -1, -1, IS_BINARY_OP | IS_IT | USES_CCODES,
774                 "it:!1b", "!0c", 2, kFixupNone),
775    ENCODING_MAP(kThumb2Fmstat,  0xeef1fa10,
776                 kFmtUnused, -1, -1, kFmtUnused, -1, -1, kFmtUnused, -1, -1,
777                 kFmtUnused, -1, -1, NO_OPERAND | SETS_CCODES,
778                 "fmstat", "", 4, kFixupNone),
779    ENCODING_MAP(kThumb2Vcmpd,        0xeeb40b40,
780                 kFmtDfp, 22, 12, kFmtDfp, 5, 0, kFmtUnused, -1, -1,
781                 kFmtUnused, -1, -1, IS_BINARY_OP | REG_USE01,
782                 "vcmp.f64", "!0S, !1S", 4, kFixupNone),
783    ENCODING_MAP(kThumb2Vcmps,        0xeeb40a40,
784                 kFmtSfp, 22, 12, kFmtSfp, 5, 0, kFmtUnused, -1, -1,
785                 kFmtUnused, -1, -1, IS_BINARY_OP | REG_USE01,
786                 "vcmp.f32", "!0s, !1s", 4, kFixupNone),
787    ENCODING_MAP(kThumb2LdrPcRel12,       0xf8df0000,
788                 kFmtBitBlt, 15, 12, kFmtBitBlt, 11, 0, kFmtUnused, -1, -1,
789                 kFmtUnused, -1, -1,
790                 IS_TERTIARY_OP | REG_DEF0 | REG_USE_PC | IS_LOAD | NEEDS_FIXUP,
791                 "ldr", "!0C, [r15pc, #!1d]", 4, kFixupLoad),
792    ENCODING_MAP(kThumb2BCond,        0xf0008000,
793                 kFmtBrOffset, -1, -1, kFmtBitBlt, 25, 22, kFmtUnused, -1, -1,
794                 kFmtUnused, -1, -1,
795                 IS_BINARY_OP | IS_BRANCH | USES_CCODES | NEEDS_FIXUP,
796                 "b!1c", "!0t", 4, kFixupCondBranch),
797    ENCODING_MAP(kThumb2Vmovd_RR,       0xeeb00b40,
798                 kFmtDfp, 22, 12, kFmtDfp, 5, 0, kFmtUnused, -1, -1,
799                 kFmtUnused, -1, -1, IS_BINARY_OP | REG_DEF0_USE1,
800                 "vmov.f64", "!0S, !1S", 4, kFixupNone),
801    ENCODING_MAP(kThumb2Vmovs_RR,       0xeeb00a40,
802                 kFmtSfp, 22, 12, kFmtSfp, 5, 0, kFmtUnused, -1, -1,
803                 kFmtUnused, -1, -1, IS_BINARY_OP | REG_DEF0_USE1,
804                 "vmov.f32", "!0s, !1s", 4, kFixupNone),
805    ENCODING_MAP(kThumb2Fmrs,       0xee100a10,
806                 kFmtBitBlt, 15, 12, kFmtSfp, 7, 16, kFmtUnused, -1, -1,
807                 kFmtUnused, -1, -1, IS_BINARY_OP | REG_DEF0_USE1,
808                 "fmrs", "!0C, !1s", 4, kFixupNone),
809    ENCODING_MAP(kThumb2Fmsr,       0xee000a10,
810                 kFmtSfp, 7, 16, kFmtBitBlt, 15, 12, kFmtUnused, -1, -1,
811                 kFmtUnused, -1, -1, IS_BINARY_OP | REG_DEF0_USE1,
812                 "fmsr", "!0s, !1C", 4, kFixupNone),
813    ENCODING_MAP(kThumb2Fmrrd,       0xec500b10,
814                 kFmtBitBlt, 15, 12, kFmtBitBlt, 19, 16, kFmtDfp, 5, 0,
815                 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF01_USE2,
816                 "fmrrd", "!0C, !1C, !2S", 4, kFixupNone),
817    ENCODING_MAP(kThumb2Fmdrr,       0xec400b10,
818                 kFmtDfp, 5, 0, kFmtBitBlt, 15, 12, kFmtBitBlt, 19, 16,
819                 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE12,
820                 "fmdrr", "!0S, !1C, !2C", 4, kFixupNone),
821    ENCODING_MAP(kThumb2Vabsd,       0xeeb00bc0,
822                 kFmtDfp, 22, 12, kFmtDfp, 5, 0, kFmtUnused, -1, -1,
823                 kFmtUnused, -1, -1, IS_BINARY_OP | REG_DEF0_USE1,
824                 "vabs.f64", "!0S, !1S", 4, kFixupNone),
825    ENCODING_MAP(kThumb2Vabss,       0xeeb00ac0,
826                 kFmtSfp, 22, 12, kFmtSfp, 5, 0, kFmtUnused, -1, -1,
827                 kFmtUnused, -1, -1, IS_BINARY_OP | REG_DEF0_USE1,
828                 "vabs.f32", "!0s, !1s", 4, kFixupNone),
829    ENCODING_MAP(kThumb2Vnegd,       0xeeb10b40,
830                 kFmtDfp, 22, 12, kFmtDfp, 5, 0, kFmtUnused, -1, -1,
831                 kFmtUnused, -1, -1, IS_BINARY_OP | REG_DEF0_USE1,
832                 "vneg.f64", "!0S, !1S", 4, kFixupNone),
833    ENCODING_MAP(kThumb2Vnegs,       0xeeb10a40,
834                 kFmtSfp, 22, 12, kFmtSfp, 5, 0, kFmtUnused, -1, -1,
835                 kFmtUnused, -1, -1, IS_BINARY_OP | REG_DEF0_USE1,
836                 "vneg.f32", "!0s, !1s", 4, kFixupNone),
837    ENCODING_MAP(kThumb2Vmovs_IMM8,       0xeeb00a00,
838                 kFmtSfp, 22, 12, kFmtFPImm, 16, 0, kFmtUnused, -1, -1,
839                 kFmtUnused, -1, -1, IS_BINARY_OP | REG_DEF0,
840                 "vmov.f32", "!0s, #0x!1h", 4, kFixupNone),
841    ENCODING_MAP(kThumb2Vmovd_IMM8,       0xeeb00b00,
842                 kFmtDfp, 22, 12, kFmtFPImm, 16, 0, kFmtUnused, -1, -1,
843                 kFmtUnused, -1, -1, IS_BINARY_OP | REG_DEF0,
844                 "vmov.f64", "!0S, #0x!1h", 4, kFixupNone),
845    ENCODING_MAP(kThumb2Mla,  0xfb000000,
846                 kFmtBitBlt, 11, 8, kFmtBitBlt, 19, 16, kFmtBitBlt, 3, 0,
847                 kFmtBitBlt, 15, 12,
848                 IS_QUAD_OP | REG_DEF0 | REG_USE1 | REG_USE2 | REG_USE3,
849                 "mla", "!0C, !1C, !2C, !3C", 4, kFixupNone),
850    ENCODING_MAP(kThumb2Umull,  0xfba00000,
851                 kFmtBitBlt, 15, 12, kFmtBitBlt, 11, 8, kFmtBitBlt, 19, 16,
852                 kFmtBitBlt, 3, 0,
853                 IS_QUAD_OP | REG_DEF0 | REG_DEF1 | REG_USE2 | REG_USE3,
854                 "umull", "!0C, !1C, !2C, !3C", 4, kFixupNone),
855    ENCODING_MAP(kThumb2Ldrex,       0xe8500f00,
856                 kFmtBitBlt, 15, 12, kFmtBitBlt, 19, 16, kFmtBitBlt, 7, 0,
857                 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE1 | IS_LOAD,
858                 "ldrex", "!0C, [!1C, #!2E]", 4, kFixupNone),
859    ENCODING_MAP(kThumb2Strex,       0xe8400000,
860                 kFmtBitBlt, 11, 8, kFmtBitBlt, 15, 12, kFmtBitBlt, 19, 16,
861                 kFmtBitBlt, 7, 0, IS_QUAD_OP | REG_DEF0_USE12 | IS_STORE,
862                 "strex", "!0C,!1C, [!2C, #!2E]", 4, kFixupNone),
863    ENCODING_MAP(kThumb2Clrex,       0xf3bf8f2f,
864                 kFmtUnused, -1, -1, kFmtUnused, -1, -1, kFmtUnused, -1, -1,
865                 kFmtUnused, -1, -1, NO_OPERAND,
866                 "clrex", "", 4, kFixupNone),
867    ENCODING_MAP(kThumb2Bfi,         0xf3600000,
868                 kFmtBitBlt, 11, 8, kFmtBitBlt, 19, 16, kFmtShift5, -1, -1,
869                 kFmtBitBlt, 4, 0, IS_QUAD_OP | REG_DEF0_USE1,
870                 "bfi", "!0C,!1C,#!2d,#!3d", 4, kFixupNone),
871    ENCODING_MAP(kThumb2Bfc,         0xf36f0000,
872                 kFmtBitBlt, 11, 8, kFmtShift5, -1, -1, kFmtBitBlt, 4, 0,
873                 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0,
874                 "bfc", "!0C,#!1d,#!2d", 4, kFixupNone),
875    ENCODING_MAP(kThumb2Dmb,         0xf3bf8f50,
876                 kFmtBitBlt, 3, 0, kFmtUnused, -1, -1, kFmtUnused, -1, -1,
877                 kFmtUnused, -1, -1, IS_UNARY_OP,
878                 "dmb", "#!0B", 4, kFixupNone),
879    ENCODING_MAP(kThumb2LdrPcReln12,       0xf85f0000,
880                 kFmtBitBlt, 15, 12, kFmtBitBlt, 11, 0, kFmtUnused, -1, -1,
881                 kFmtUnused, -1, -1,
882                 IS_BINARY_OP | REG_DEF0 | REG_USE_PC | IS_LOAD,
883                 "ldr", "!0C, [r15pc, -#!1d]", 4, kFixupNone),
884    ENCODING_MAP(kThumb2Stm,          0xe9000000,
885                 kFmtBitBlt, 19, 16, kFmtBitBlt, 12, 0, kFmtUnused, -1, -1,
886                 kFmtUnused, -1, -1,
887                 IS_BINARY_OP | REG_USE0 | REG_USE_LIST1 | IS_STORE,
888                 "stm", "!0C, <!1R>", 4, kFixupNone),
889    ENCODING_MAP(kThumbUndefined,       0xde00,
890                 kFmtUnused, -1, -1, kFmtUnused, -1, -1, kFmtUnused, -1, -1,
891                 kFmtUnused, -1, -1, NO_OPERAND,
892                 "undefined", "", 2, kFixupNone),
893    // NOTE: vpop, vpush hard-encoded for s16+ reg list
894    ENCODING_MAP(kThumb2VPopCS,       0xecbd8a00,
895                 kFmtBitBlt, 7, 0, kFmtUnused, -1, -1, kFmtUnused, -1, -1,
896                 kFmtUnused, -1, -1,
897                 IS_UNARY_OP | REG_DEF_SP | REG_USE_SP | REG_DEF_FPCS_LIST0
898                 | IS_LOAD, "vpop", "<!0P>", 4, kFixupNone),
899    ENCODING_MAP(kThumb2VPushCS,      0xed2d8a00,
900                 kFmtBitBlt, 7, 0, kFmtUnused, -1, -1, kFmtUnused, -1, -1,
901                 kFmtUnused, -1, -1,
902                 IS_UNARY_OP | REG_DEF_SP | REG_USE_SP | REG_USE_FPCS_LIST0
903                 | IS_STORE, "vpush", "<!0P>", 4, kFixupNone),
904    ENCODING_MAP(kThumb2Vldms,        0xec900a00,
905                 kFmtBitBlt, 19, 16, kFmtSfp, 22, 12, kFmtBitBlt, 7, 0,
906                 kFmtUnused, -1, -1,
907                 IS_TERTIARY_OP | REG_USE0 | REG_DEF_FPCS_LIST2
908                 | IS_LOAD, "vldms", "!0C, <!2Q>", 4, kFixupNone),
909    ENCODING_MAP(kThumb2Vstms,        0xec800a00,
910                 kFmtBitBlt, 19, 16, kFmtSfp, 22, 12, kFmtBitBlt, 7, 0,
911                 kFmtUnused, -1, -1,
912                 IS_TERTIARY_OP | REG_USE0 | REG_USE_FPCS_LIST2
913                 | IS_STORE, "vstms", "!0C, <!2Q>", 4, kFixupNone),
914    ENCODING_MAP(kThumb2BUncond,      0xf0009000,
915                 kFmtOff24, -1, -1, kFmtUnused, -1, -1, kFmtUnused, -1, -1,
916                 kFmtUnused, -1, -1, NO_OPERAND | IS_BRANCH,
917                 "b", "!0t", 4, kFixupT2Branch),
918    ENCODING_MAP(kThumb2MovImm16H,       0xf2c00000,
919                 kFmtBitBlt, 11, 8, kFmtImm16, -1, -1, kFmtUnused, -1, -1,
920                 kFmtUnused, -1, -1, IS_BINARY_OP | REG_DEF0 | REG_USE0,
921                 "movt", "!0C, #!1M", 4, kFixupNone),
922    ENCODING_MAP(kThumb2AddPCR,      0x4487,
923                 kFmtBitBlt, 6, 3, kFmtUnused, -1, -1, kFmtUnused, -1, -1,
924                 kFmtUnused, -1, -1,
925                 IS_UNARY_OP | REG_USE0 | IS_BRANCH | NEEDS_FIXUP,
926                 "add", "rPC, !0C", 2, kFixupLabel),
927    ENCODING_MAP(kThumb2Adr,         0xf20f0000,
928                 kFmtBitBlt, 11, 8, kFmtImm12, -1, -1, kFmtUnused, -1, -1,
929                 kFmtUnused, -1, -1,
930                 /* Note: doesn't affect flags */
931                 IS_TERTIARY_OP | REG_DEF0 | NEEDS_FIXUP,
932                 "adr", "!0C,#!1d", 4, kFixupAdr),
933    ENCODING_MAP(kThumb2MovImm16LST,     0xf2400000,
934                 kFmtBitBlt, 11, 8, kFmtImm16, -1, -1, kFmtUnused, -1, -1,
935                 kFmtUnused, -1, -1, IS_BINARY_OP | REG_DEF0 | NEEDS_FIXUP,
936                 "mov", "!0C, #!1M", 4, kFixupMovImmLST),
937    ENCODING_MAP(kThumb2MovImm16HST,     0xf2c00000,
938                 kFmtBitBlt, 11, 8, kFmtImm16, -1, -1, kFmtUnused, -1, -1,
939                 kFmtUnused, -1, -1, IS_BINARY_OP | REG_DEF0 | REG_USE0 | NEEDS_FIXUP,
940                 "movt", "!0C, #!1M", 4, kFixupMovImmHST),
941    ENCODING_MAP(kThumb2LdmiaWB,         0xe8b00000,
942                 kFmtBitBlt, 19, 16, kFmtBitBlt, 15, 0, kFmtUnused, -1, -1,
943                 kFmtUnused, -1, -1,
944                 IS_BINARY_OP | REG_DEF0_USE0 | REG_DEF_LIST1 | IS_LOAD,
945                 "ldmia", "!0C!!, <!1R>", 4, kFixupNone),
946    ENCODING_MAP(kThumb2SubsRRI12,       0xf1b00000,
947                 kFmtBitBlt, 11, 8, kFmtBitBlt, 19, 16, kFmtImm12, -1, -1,
948                 kFmtUnused, -1, -1,
949                 IS_TERTIARY_OP | REG_DEF0_USE1 | SETS_CCODES,
950                 "subs", "!0C,!1C,#!2d", 4, kFixupNone),
951    ENCODING_MAP(kThumb2OrrRRRs,  0xea500000,
952                 kFmtBitBlt, 11, 8, kFmtBitBlt, 19, 16, kFmtBitBlt, 3, 0,
953                 kFmtShift, -1, -1, IS_QUAD_OP | REG_DEF0_USE12 | SETS_CCODES,
954                 "orrs", "!0C, !1C, !2C!3H", 4, kFixupNone),
955    ENCODING_MAP(kThumb2Push1,    0xf84d0d04,
956                 kFmtBitBlt, 15, 12, kFmtUnused, -1, -1, kFmtUnused, -1, -1,
957                 kFmtUnused, -1, -1,
958                 IS_UNARY_OP | REG_DEF_SP | REG_USE_SP | REG_USE0
959                 | IS_STORE, "push1", "!0C", 4, kFixupNone),
960    ENCODING_MAP(kThumb2Pop1,    0xf85d0b04,
961                 kFmtBitBlt, 15, 12, kFmtUnused, -1, -1, kFmtUnused, -1, -1,
962                 kFmtUnused, -1, -1,
963                 IS_UNARY_OP | REG_DEF_SP | REG_USE_SP | REG_DEF0
964                 | IS_LOAD, "pop1", "!0C", 4, kFixupNone),
965    ENCODING_MAP(kThumb2RsubRRR,  0xebd00000, /* setflags encoding */
966                 kFmtBitBlt, 11, 8, kFmtBitBlt, 19, 16, kFmtBitBlt, 3, 0,
967                 kFmtShift, -1, -1,
968                 IS_QUAD_OP | REG_DEF0_USE12 | SETS_CCODES,
969                 "rsbs", "!0C, !1C, !2C!3H", 4, kFixupNone),
970    ENCODING_MAP(kThumb2Smull,  0xfb800000,
971                 kFmtBitBlt, 15, 12, kFmtBitBlt, 11, 8, kFmtBitBlt, 19, 16,
972                 kFmtBitBlt, 3, 0,
973                 IS_QUAD_OP | REG_DEF0 | REG_DEF1 | REG_USE2 | REG_USE3,
974                 "smull", "!0C, !1C, !2C, !3C", 4, kFixupNone),
975    ENCODING_MAP(kThumb2LdrdPcRel8,  0xe9df0000,
976                 kFmtBitBlt, 15, 12, kFmtBitBlt, 11, 8, kFmtBitBlt, 7, 0,
977                 kFmtUnused, -1, -1,
978                 IS_TERTIARY_OP | REG_DEF0 | REG_DEF1 | REG_USE_PC | IS_LOAD | NEEDS_FIXUP,
979                 "ldrd", "!0C, !1C, [pc, #!2E]", 4, kFixupLoad),
980    ENCODING_MAP(kThumb2LdrdI8, 0xe9d00000,
981                 kFmtBitBlt, 15, 12, kFmtBitBlt, 11, 8, kFmtBitBlt, 19, 16,
982                 kFmtBitBlt, 7, 0,
983                 IS_QUAD_OP | REG_DEF0 | REG_DEF1 | REG_USE2 | IS_LOAD,
984                 "ldrd", "!0C, !1C, [!2C, #!3E]", 4, kFixupNone),
985    ENCODING_MAP(kThumb2StrdI8, 0xe9c00000,
986                 kFmtBitBlt, 15, 12, kFmtBitBlt, 11, 8, kFmtBitBlt, 19, 16,
987                 kFmtBitBlt, 7, 0,
988                 IS_QUAD_OP | REG_USE0 | REG_USE1 | REG_USE2 | IS_STORE,
989                 "strd", "!0C, !1C, [!2C, #!3E]", 4, kFixupNone),
990};
991
992// new_lir replaces orig_lir in the pcrel_fixup list.
993void ArmMir2Lir::ReplaceFixup(LIR* prev_lir, LIR* orig_lir, LIR* new_lir) {
994  new_lir->u.a.pcrel_next = orig_lir->u.a.pcrel_next;
995  if (UNLIKELY(prev_lir == NULL)) {
996    first_fixup_ = new_lir;
997  } else {
998    prev_lir->u.a.pcrel_next = new_lir;
999  }
1000  orig_lir->flags.fixup = kFixupNone;
1001}
1002
1003// new_lir is inserted before orig_lir in the pcrel_fixup list.
1004void ArmMir2Lir::InsertFixupBefore(LIR* prev_lir, LIR* orig_lir, LIR* new_lir) {
1005  new_lir->u.a.pcrel_next = orig_lir;
1006  if (UNLIKELY(prev_lir == NULL)) {
1007    first_fixup_ = new_lir;
1008  } else {
1009    DCHECK(prev_lir->u.a.pcrel_next == orig_lir);
1010    prev_lir->u.a.pcrel_next = new_lir;
1011  }
1012}
1013
1014/*
1015 * The fake NOP of moving r0 to r0 actually will incur data stalls if r0 is
1016 * not ready. Since r5FP is not updated often, it is less likely to
1017 * generate unnecessary stall cycles.
1018 * TUNING: No longer true - find new NOP pattern.
1019 */
1020#define PADDING_MOV_R5_R5               0x1C2D
1021
1022void ArmMir2Lir::EncodeLIR(LIR* lir) {
1023  int opcode = lir->opcode;
1024  if (IsPseudoLirOp(opcode)) {
1025    if (UNLIKELY(opcode == kPseudoPseudoAlign4)) {
1026      // Note: size for this opcode will be either 0 or 2 depending on final alignment.
1027      lir->u.a.bytes[0] = (PADDING_MOV_R5_R5 & 0xff);
1028      lir->u.a.bytes[1] = ((PADDING_MOV_R5_R5 >> 8) & 0xff);
1029      lir->flags.size = (lir->offset & 0x2);
1030    }
1031  } else if (LIKELY(!lir->flags.is_nop)) {
1032    const ArmEncodingMap *encoder = &EncodingMap[lir->opcode];
1033    uint32_t bits = encoder->skeleton;
1034    for (int i = 0; i < 4; i++) {
1035      uint32_t operand;
1036      uint32_t value;
1037      operand = lir->operands[i];
1038      ArmEncodingKind kind = encoder->field_loc[i].kind;
1039      if (LIKELY(kind == kFmtBitBlt)) {
1040        value = (operand << encoder->field_loc[i].start) &
1041            ((1 << (encoder->field_loc[i].end + 1)) - 1);
1042        bits |= value;
1043      } else {
1044        switch (encoder->field_loc[i].kind) {
1045          case kFmtSkip:
1046            break;  // Nothing to do, but continue to next.
1047          case kFmtUnused:
1048            i = 4;  // Done, break out of the enclosing loop.
1049            break;
1050          case kFmtFPImm:
1051            value = ((operand & 0xF0) >> 4) << encoder->field_loc[i].end;
1052            value |= (operand & 0x0F) << encoder->field_loc[i].start;
1053            bits |= value;
1054            break;
1055          case kFmtBrOffset:
1056            value = ((operand  & 0x80000) >> 19) << 26;
1057            value |= ((operand & 0x40000) >> 18) << 11;
1058            value |= ((operand & 0x20000) >> 17) << 13;
1059            value |= ((operand & 0x1f800) >> 11) << 16;
1060            value |= (operand  & 0x007ff);
1061            bits |= value;
1062            break;
1063          case kFmtShift5:
1064            value = ((operand & 0x1c) >> 2) << 12;
1065            value |= (operand & 0x03) << 6;
1066            bits |= value;
1067            break;
1068          case kFmtShift:
1069            value = ((operand & 0x70) >> 4) << 12;
1070            value |= (operand & 0x0f) << 4;
1071            bits |= value;
1072            break;
1073          case kFmtBWidth:
1074            value = operand - 1;
1075            bits |= value;
1076            break;
1077          case kFmtLsb:
1078            value = ((operand & 0x1c) >> 2) << 12;
1079            value |= (operand & 0x03) << 6;
1080            bits |= value;
1081            break;
1082          case kFmtImm6:
1083            value = ((operand & 0x20) >> 5) << 9;
1084            value |= (operand & 0x1f) << 3;
1085            bits |= value;
1086            break;
1087          case kFmtDfp: {
1088            DCHECK(ARM_DOUBLEREG(operand));
1089            DCHECK_EQ((operand & 0x1), 0U);
1090            uint32_t reg_name = (operand & ARM_FP_REG_MASK) >> 1;
1091            /* Snag the 1-bit slice and position it */
1092            value = ((reg_name & 0x10) >> 4) << encoder->field_loc[i].end;
1093            /* Extract and position the 4-bit slice */
1094            value |= (reg_name & 0x0f) << encoder->field_loc[i].start;
1095            bits |= value;
1096            break;
1097          }
1098          case kFmtSfp:
1099            DCHECK(ARM_SINGLEREG(operand));
1100            /* Snag the 1-bit slice and position it */
1101            value = (operand & 0x1) << encoder->field_loc[i].end;
1102            /* Extract and position the 4-bit slice */
1103            value |= ((operand & 0x1e) >> 1) << encoder->field_loc[i].start;
1104            bits |= value;
1105            break;
1106          case kFmtImm12:
1107          case kFmtModImm:
1108            value = ((operand & 0x800) >> 11) << 26;
1109            value |= ((operand & 0x700) >> 8) << 12;
1110            value |= operand & 0x0ff;
1111            bits |= value;
1112            break;
1113          case kFmtImm16:
1114            value = ((operand & 0x0800) >> 11) << 26;
1115            value |= ((operand & 0xf000) >> 12) << 16;
1116            value |= ((operand & 0x0700) >> 8) << 12;
1117            value |= operand & 0x0ff;
1118            bits |= value;
1119            break;
1120          case kFmtOff24: {
1121            uint32_t signbit = (operand >> 31) & 0x1;
1122            uint32_t i1 = (operand >> 22) & 0x1;
1123            uint32_t i2 = (operand >> 21) & 0x1;
1124            uint32_t imm10 = (operand >> 11) & 0x03ff;
1125            uint32_t imm11 = operand & 0x07ff;
1126            uint32_t j1 = (i1 ^ signbit) ? 0 : 1;
1127            uint32_t j2 = (i2 ^ signbit) ? 0 : 1;
1128            value = (signbit << 26) | (j1 << 13) | (j2 << 11) | (imm10 << 16) |
1129                imm11;
1130            bits |= value;
1131            }
1132            break;
1133          default:
1134            LOG(FATAL) << "Bad fmt:" << encoder->field_loc[i].kind;
1135        }
1136      }
1137    }
1138    if (encoder->size == 4) {
1139      lir->u.a.bytes[0] = ((bits >> 16) & 0xff);
1140      lir->u.a.bytes[1] = ((bits >> 24) & 0xff);
1141      lir->u.a.bytes[2] = (bits & 0xff);
1142      lir->u.a.bytes[3] = ((bits >> 8) & 0xff);
1143    } else {
1144      DCHECK_EQ(encoder->size, 2);
1145      lir->u.a.bytes[0] = (bits & 0xff);
1146      lir->u.a.bytes[1] = ((bits >> 8) & 0xff);
1147    }
1148    lir->flags.size = encoder->size;
1149  }
1150}
1151
1152// Assemble the LIR into binary instruction format.
1153void ArmMir2Lir::AssembleLIR() {
1154  LIR* lir;
1155  LIR* prev_lir;
1156  int assembler_retries = 0;
1157  CodeOffset starting_offset = EncodeRange(first_lir_insn_, last_lir_insn_, 0);
1158  data_offset_ = (starting_offset + 0x3) & ~0x3;
1159  int32_t offset_adjustment;
1160  AssignDataOffsets();
1161
1162  /*
1163   * Note: generation must be 1 on first pass (to distinguish from initialized state of 0 for non-visited nodes).
1164   * Start at zero here, and bit will be flipped to 1 on entry to the loop.
1165   */
1166  int generation = 0;
1167  while (true) {
1168    offset_adjustment = 0;
1169    AssemblerStatus res = kSuccess;  // Assume success
1170    generation ^= 1;
1171    // Note: nodes requring possible fixup linked in ascending order.
1172    lir = first_fixup_;
1173    prev_lir = NULL;
1174    while (lir != NULL) {
1175      /*
1176       * NOTE: the lir being considered here will be encoded following the switch (so long as
1177       * we're not in a retry situation).  However, any new non-pc_rel instructions inserted
1178       * due to retry must be explicitly encoded at the time of insertion.  Note that
1179       * inserted instructions don't need use/def flags, but do need size and pc-rel status
1180       * properly updated.
1181       */
1182      lir->offset += offset_adjustment;
1183      // During pass, allows us to tell whether a node has been updated with offset_adjustment yet.
1184      lir->flags.generation = generation;
1185      switch (static_cast<FixupKind>(lir->flags.fixup)) {
1186        case kFixupLabel:
1187        case kFixupNone:
1188          break;
1189        case kFixupVLoad:
1190          if (lir->operands[1] != r15pc) {
1191            break;
1192          }
1193          // NOTE: intentional fallthrough.
1194        case kFixupLoad: {
1195          /*
1196           * PC-relative loads are mostly used to load immediates
1197           * that are too large to materialize directly in one shot.
1198           * However, if the load displacement exceeds the limit,
1199           * we revert to a multiple-instruction materialization sequence.
1200           */
1201          LIR *lir_target = lir->target;
1202          CodeOffset pc = (lir->offset + 4) & ~3;
1203          CodeOffset target = lir_target->offset +
1204              ((lir_target->flags.generation == lir->flags.generation) ? 0 : offset_adjustment);
1205          int32_t delta = target - pc;
1206          if (res != kSuccess) {
1207            /*
1208             * In this case, we're just estimating and will do it again for real.  Ensure offset
1209             * is legal.
1210             */
1211            delta &= ~0x3;
1212          }
1213          DCHECK_EQ((delta & 0x3), 0);
1214          // First, a sanity check for cases we shouldn't see now
1215          if (kIsDebugBuild && (((lir->opcode == kThumbAddPcRel) && (delta > 1020)) ||
1216              ((lir->opcode == kThumbLdrPcRel) && (delta > 1020)))) {
1217            // Shouldn't happen in current codegen.
1218            LOG(FATAL) << "Unexpected pc-rel offset " << delta;
1219          }
1220          // Now, check for the difficult cases
1221          if (((lir->opcode == kThumb2LdrPcRel12) && (delta > 4091)) ||
1222              ((lir->opcode == kThumb2LdrdPcRel8) && (delta > 1020)) ||
1223              ((lir->opcode == kThumb2Vldrs) && (delta > 1020)) ||
1224              ((lir->opcode == kThumb2Vldrd) && (delta > 1020))) {
1225            /*
1226             * Note: The reason vldrs/vldrd include rARM_LR in their use/def masks is that we
1227             * sometimes have to use it to fix up out-of-range accesses.  This is where that
1228             * happens.
1229             */
1230            int base_reg = ((lir->opcode == kThumb2LdrdPcRel8) ||
1231                            (lir->opcode == kThumb2LdrPcRel12)) ?  lir->operands[0] : rARM_LR;
1232
1233            // Add new Adr to generate the address.
1234            LIR* new_adr = RawLIR(lir->dalvik_offset, kThumb2Adr,
1235                       base_reg, 0, 0, 0, 0, lir->target);
1236            new_adr->offset = lir->offset;
1237            new_adr->flags.fixup = kFixupAdr;
1238            new_adr->flags.size = EncodingMap[kThumb2Adr].size;
1239            InsertLIRBefore(lir, new_adr);
1240            lir->offset += new_adr->flags.size;
1241            offset_adjustment += new_adr->flags.size;
1242
1243            // lir no longer pcrel, unlink and link in new_adr.
1244            ReplaceFixup(prev_lir, lir, new_adr);
1245
1246            // Convert to normal load.
1247            offset_adjustment -= lir->flags.size;
1248            if (lir->opcode == kThumb2LdrPcRel12) {
1249              lir->opcode = kThumb2LdrRRI12;
1250            } else if (lir->opcode == kThumb2LdrdPcRel8) {
1251              lir->opcode = kThumb2LdrdI8;
1252            }
1253            lir->flags.size = EncodingMap[lir->opcode].size;
1254            offset_adjustment += lir->flags.size;
1255            // Change the load to be relative to the new Adr base.
1256            if (lir->opcode == kThumb2LdrdI8) {
1257              lir->operands[3] = 0;
1258              lir->operands[2] = base_reg;
1259            } else {
1260              lir->operands[2] = 0;
1261              lir->operands[1] = base_reg;
1262            }
1263            // Must redo encoding here - won't ever revisit this node.
1264            EncodeLIR(lir);
1265            prev_lir = new_adr;  // Continue scan with new_adr;
1266            lir = new_adr->u.a.pcrel_next;
1267            res = kRetryAll;
1268            continue;
1269          } else {
1270            if ((lir->opcode == kThumb2Vldrs) ||
1271                (lir->opcode == kThumb2Vldrd) ||
1272                (lir->opcode == kThumb2LdrdPcRel8)) {
1273              lir->operands[2] = delta >> 2;
1274            } else {
1275              lir->operands[1] = (lir->opcode == kThumb2LdrPcRel12) ?  delta :
1276                  delta >> 2;
1277            }
1278          }
1279          break;
1280        }
1281        case kFixupCBxZ: {
1282          LIR *target_lir = lir->target;
1283          CodeOffset pc = lir->offset + 4;
1284          CodeOffset target = target_lir->offset +
1285              ((target_lir->flags.generation == lir->flags.generation) ? 0 : offset_adjustment);
1286          int32_t delta = target - pc;
1287          if (delta > 126 || delta < 0) {
1288            /*
1289             * Convert to cmp rx,#0 / b[eq/ne] tgt pair
1290             * Make new branch instruction and insert after
1291             */
1292            LIR* new_inst =
1293              RawLIR(lir->dalvik_offset, kThumbBCond, 0,
1294                     (lir->opcode == kThumb2Cbz) ? kArmCondEq : kArmCondNe,
1295                     0, 0, 0, lir->target);
1296            InsertLIRAfter(lir, new_inst);
1297
1298            /* Convert the cb[n]z to a cmp rx, #0 ] */
1299            // Subtract the old size.
1300            offset_adjustment -= lir->flags.size;
1301            lir->opcode = kThumbCmpRI8;
1302            /* operand[0] is src1 in both cb[n]z & CmpRI8 */
1303            lir->operands[1] = 0;
1304            lir->target = 0;
1305            EncodeLIR(lir);   // NOTE: sets flags.size.
1306            // Add back the new size.
1307            DCHECK_EQ(lir->flags.size, static_cast<uint32_t>(EncodingMap[lir->opcode].size));
1308            offset_adjustment += lir->flags.size;
1309            // Set up the new following inst.
1310            new_inst->offset = lir->offset + lir->flags.size;
1311            new_inst->flags.fixup = kFixupCondBranch;
1312            new_inst->flags.size = EncodingMap[new_inst->opcode].size;
1313            offset_adjustment += new_inst->flags.size;
1314
1315            // lir no longer pcrel, unlink and link in new_inst.
1316            ReplaceFixup(prev_lir, lir, new_inst);
1317            prev_lir = new_inst;  // Continue with the new instruction.
1318            lir = new_inst->u.a.pcrel_next;
1319            res = kRetryAll;
1320            continue;
1321          } else {
1322            lir->operands[1] = delta >> 1;
1323          }
1324          break;
1325        }
1326        case kFixupPushPop: {
1327          if (__builtin_popcount(lir->operands[0]) == 1) {
1328            /*
1329             * The standard push/pop multiple instruction
1330             * requires at least two registers in the list.
1331             * If we've got just one, switch to the single-reg
1332             * encoding.
1333             */
1334            lir->opcode = (lir->opcode == kThumb2Push) ? kThumb2Push1 :
1335                kThumb2Pop1;
1336            int reg = 0;
1337            while (lir->operands[0]) {
1338              if (lir->operands[0] & 0x1) {
1339                break;
1340              } else {
1341                reg++;
1342                lir->operands[0] >>= 1;
1343              }
1344            }
1345            lir->operands[0] = reg;
1346            // This won't change again, don't bother unlinking, just reset fixup kind
1347            lir->flags.fixup = kFixupNone;
1348          }
1349          break;
1350        }
1351        case kFixupCondBranch: {
1352          LIR *target_lir = lir->target;
1353          int32_t delta = 0;
1354          DCHECK(target_lir);
1355          CodeOffset pc = lir->offset + 4;
1356          CodeOffset target = target_lir->offset +
1357              ((target_lir->flags.generation == lir->flags.generation) ? 0 : offset_adjustment);
1358          delta = target - pc;
1359          if ((lir->opcode == kThumbBCond) && (delta > 254 || delta < -256)) {
1360            offset_adjustment -= lir->flags.size;
1361            lir->opcode = kThumb2BCond;
1362            lir->flags.size = EncodingMap[lir->opcode].size;
1363            // Fixup kind remains the same.
1364            offset_adjustment += lir->flags.size;
1365            res = kRetryAll;
1366          }
1367          lir->operands[0] = delta >> 1;
1368          break;
1369        }
1370        case kFixupT2Branch: {
1371          LIR *target_lir = lir->target;
1372          CodeOffset pc = lir->offset + 4;
1373          CodeOffset target = target_lir->offset +
1374              ((target_lir->flags.generation == lir->flags.generation) ? 0 : offset_adjustment);
1375          int32_t delta = target - pc;
1376          lir->operands[0] = delta >> 1;
1377          if (!(cu_->disable_opt & (1 << kSafeOptimizations)) && lir->operands[0] == 0) {
1378            // Useless branch
1379            offset_adjustment -= lir->flags.size;
1380            lir->flags.is_nop = true;
1381            // Don't unlink - just set to do-nothing.
1382            lir->flags.fixup = kFixupNone;
1383            res = kRetryAll;
1384          }
1385          break;
1386        }
1387        case kFixupT1Branch: {
1388          LIR *target_lir = lir->target;
1389          CodeOffset pc = lir->offset + 4;
1390          CodeOffset target = target_lir->offset +
1391              ((target_lir->flags.generation == lir->flags.generation) ? 0 : offset_adjustment);
1392          int32_t delta = target - pc;
1393          if (delta > 2046 || delta < -2048) {
1394            // Convert to Thumb2BCond w/ kArmCondAl
1395            offset_adjustment -= lir->flags.size;
1396            lir->opcode = kThumb2BUncond;
1397            lir->operands[0] = 0;
1398            lir->flags.size = EncodingMap[lir->opcode].size;
1399            lir->flags.fixup = kFixupT2Branch;
1400            offset_adjustment += lir->flags.size;
1401            res = kRetryAll;
1402          } else {
1403            lir->operands[0] = delta >> 1;
1404            if (!(cu_->disable_opt & (1 << kSafeOptimizations)) && lir->operands[0] == -1) {
1405              // Useless branch
1406              offset_adjustment -= lir->flags.size;
1407              lir->flags.is_nop = true;
1408              // Don't unlink - just set to do-nothing.
1409              lir->flags.fixup = kFixupNone;
1410              res = kRetryAll;
1411            }
1412          }
1413          break;
1414        }
1415        case kFixupBlx1: {
1416          DCHECK(NEXT_LIR(lir)->opcode == kThumbBlx2);
1417          /* cur_pc is Thumb */
1418          CodeOffset cur_pc = (lir->offset + 4) & ~3;
1419          CodeOffset target = lir->operands[1];
1420
1421          /* Match bit[1] in target with base */
1422          if (cur_pc & 0x2) {
1423            target |= 0x2;
1424          }
1425          int32_t delta = target - cur_pc;
1426          DCHECK((delta >= -(1<<22)) && (delta <= ((1<<22)-2)));
1427
1428          lir->operands[0] = (delta >> 12) & 0x7ff;
1429          NEXT_LIR(lir)->operands[0] = (delta>> 1) & 0x7ff;
1430          break;
1431        }
1432        case kFixupBl1: {
1433          DCHECK(NEXT_LIR(lir)->opcode == kThumbBl2);
1434          /* Both cur_pc and target are Thumb */
1435          CodeOffset cur_pc = lir->offset + 4;
1436          CodeOffset target = lir->operands[1];
1437
1438          int32_t delta = target - cur_pc;
1439          DCHECK((delta >= -(1<<22)) && (delta <= ((1<<22)-2)));
1440
1441          lir->operands[0] = (delta >> 12) & 0x7ff;
1442          NEXT_LIR(lir)->operands[0] = (delta>> 1) & 0x7ff;
1443          break;
1444        }
1445        case kFixupAdr: {
1446          EmbeddedData *tab_rec = reinterpret_cast<EmbeddedData*>(UnwrapPointer(lir->operands[2]));
1447          LIR* target = lir->target;
1448          int32_t target_disp = (tab_rec != NULL) ?  tab_rec->offset + offset_adjustment
1449              : target->offset + ((target->flags.generation == lir->flags.generation) ? 0 : offset_adjustment);
1450          int32_t disp = target_disp - ((lir->offset + 4) & ~3);
1451          if (disp < 4096) {
1452            lir->operands[1] = disp;
1453          } else {
1454            // convert to ldimm16l, ldimm16h, add tgt, pc, operands[0]
1455            // TUNING: if this case fires often, it can be improved.  Not expected to be common.
1456            LIR *new_mov16L =
1457                RawLIR(lir->dalvik_offset, kThumb2MovImm16LST, lir->operands[0], 0,
1458                       WrapPointer(lir), WrapPointer(tab_rec), 0, lir->target);
1459            new_mov16L->flags.size = EncodingMap[new_mov16L->opcode].size;
1460            new_mov16L->flags.fixup = kFixupMovImmLST;
1461            new_mov16L->offset = lir->offset;
1462            // Link the new instruction, retaining lir.
1463            InsertLIRBefore(lir, new_mov16L);
1464            lir->offset += new_mov16L->flags.size;
1465            offset_adjustment += new_mov16L->flags.size;
1466            InsertFixupBefore(prev_lir, lir, new_mov16L);
1467            prev_lir = new_mov16L;   // Now we've got a new prev.
1468            LIR *new_mov16H =
1469                RawLIR(lir->dalvik_offset, kThumb2MovImm16HST, lir->operands[0], 0,
1470                       WrapPointer(lir), WrapPointer(tab_rec), 0, lir->target);
1471            new_mov16H->flags.size = EncodingMap[new_mov16H->opcode].size;
1472            new_mov16H->flags.fixup = kFixupMovImmHST;
1473            new_mov16H->offset = lir->offset;
1474            // Link the new instruction, retaining lir.
1475            InsertLIRBefore(lir, new_mov16H);
1476            lir->offset += new_mov16H->flags.size;
1477            offset_adjustment += new_mov16H->flags.size;
1478            InsertFixupBefore(prev_lir, lir, new_mov16H);
1479            prev_lir = new_mov16H;  // Now we've got a new prev.
1480
1481            offset_adjustment -= lir->flags.size;
1482            if (ARM_LOWREG(lir->operands[0])) {
1483              lir->opcode = kThumbAddRRLH;
1484            } else {
1485              lir->opcode = kThumbAddRRHH;
1486            }
1487            lir->operands[1] = rARM_PC;
1488            lir->flags.size = EncodingMap[lir->opcode].size;
1489            offset_adjustment += lir->flags.size;
1490            // Must stay in fixup list and have offset updated; will be used by LST/HSP pair.
1491            lir->flags.fixup = kFixupNone;
1492            res = kRetryAll;
1493          }
1494          break;
1495        }
1496        case kFixupMovImmLST: {
1497          // operands[1] should hold disp, [2] has add, [3] has tab_rec
1498          LIR *addPCInst = reinterpret_cast<LIR*>(UnwrapPointer(lir->operands[2]));
1499          EmbeddedData *tab_rec = reinterpret_cast<EmbeddedData*>(UnwrapPointer(lir->operands[3]));
1500          // If tab_rec is null, this is a literal load. Use target
1501          LIR* target = lir->target;
1502          int32_t target_disp = tab_rec ? tab_rec->offset : target->offset;
1503          lir->operands[1] = (target_disp - (addPCInst->offset + 4)) & 0xffff;
1504          break;
1505        }
1506        case kFixupMovImmHST: {
1507          // operands[1] should hold disp, [2] has add, [3] has tab_rec
1508          LIR *addPCInst = reinterpret_cast<LIR*>(UnwrapPointer(lir->operands[2]));
1509          EmbeddedData *tab_rec = reinterpret_cast<EmbeddedData*>(UnwrapPointer(lir->operands[3]));
1510          // If tab_rec is null, this is a literal load. Use target
1511          LIR* target = lir->target;
1512          int32_t target_disp = tab_rec ? tab_rec->offset : target->offset;
1513          lir->operands[1] =
1514              ((target_disp - (addPCInst->offset + 4)) >> 16) & 0xffff;
1515          break;
1516        }
1517        case kFixupAlign4: {
1518          int32_t required_size = lir->offset & 0x2;
1519          if (lir->flags.size != required_size) {
1520            offset_adjustment += required_size - lir->flags.size;
1521            lir->flags.size = required_size;
1522            res = kRetryAll;
1523          }
1524          break;
1525        }
1526        default:
1527          LOG(FATAL) << "Unexpected case " << lir->flags.fixup;
1528      }
1529      /*
1530       * If one of the pc-relative instructions expanded we'll have
1531       * to make another pass.  Don't bother to fully assemble the
1532       * instruction.
1533       */
1534      if (res == kSuccess) {
1535        EncodeLIR(lir);
1536        if (assembler_retries == 0) {
1537          // Go ahead and fix up the code buffer image.
1538          for (int i = 0; i < lir->flags.size; i++) {
1539            code_buffer_[lir->offset + i] = lir->u.a.bytes[i];
1540          }
1541        }
1542      }
1543      prev_lir = lir;
1544      lir = lir->u.a.pcrel_next;
1545    }
1546
1547    if (res == kSuccess) {
1548      break;
1549    } else {
1550      assembler_retries++;
1551      if (assembler_retries > MAX_ASSEMBLER_RETRIES) {
1552        CodegenDump();
1553        LOG(FATAL) << "Assembler error - too many retries";
1554      }
1555      starting_offset += offset_adjustment;
1556      data_offset_ = (starting_offset + 0x3) & ~0x3;
1557      AssignDataOffsets();
1558    }
1559  }
1560
1561  // Rebuild the CodeBuffer if we had to retry; otherwise it should be good as-is.
1562  if (assembler_retries != 0) {
1563    code_buffer_.clear();
1564    for (LIR* lir = first_lir_insn_; lir != NULL; lir = NEXT_LIR(lir)) {
1565      if (lir->flags.is_nop) {
1566        continue;
1567      } else  {
1568        for (int i = 0; i < lir->flags.size; i++) {
1569          code_buffer_.push_back(lir->u.a.bytes[i]);
1570        }
1571      }
1572    }
1573  }
1574
1575  data_offset_ = (code_buffer_.size() + 0x3) & ~0x3;
1576
1577  // Install literals
1578  InstallLiteralPools();
1579
1580  // Install switch tables
1581  InstallSwitchTables();
1582
1583  // Install fill array data
1584  InstallFillArrayData();
1585
1586  // Create the mapping table and native offset to reference map.
1587  CreateMappingTables();
1588
1589  CreateNativeGcMap();
1590}
1591
1592int ArmMir2Lir::GetInsnSize(LIR* lir) {
1593  DCHECK(!IsPseudoLirOp(lir->opcode));
1594  return EncodingMap[lir->opcode].size;
1595}
1596
1597// Encode instruction bit pattern and assign offsets.
1598uint32_t ArmMir2Lir::EncodeRange(LIR* head_lir, LIR* tail_lir, uint32_t offset) {
1599  LIR* end_lir = tail_lir->next;
1600
1601  /*
1602   * A significant percentage of methods can be assembled in a single pass.  We'll
1603   * go ahead and build the code image here, leaving holes for pc-relative fixup
1604   * codes.  If the code size changes during that pass, we'll have to throw away
1605   * this work - but if not, we're ready to go.
1606   */
1607  code_buffer_.reserve(estimated_native_code_size_ + 256);  // Add a little slop.
1608  LIR* last_fixup = NULL;
1609  for (LIR* lir = head_lir; lir != end_lir; lir = NEXT_LIR(lir)) {
1610    lir->offset = offset;
1611    if (!lir->flags.is_nop) {
1612      if (lir->flags.fixup != kFixupNone) {
1613        if (!IsPseudoLirOp(lir->opcode)) {
1614          lir->flags.size = EncodingMap[lir->opcode].size;
1615          lir->flags.fixup = EncodingMap[lir->opcode].fixup;
1616        } else if (UNLIKELY(lir->opcode == kPseudoPseudoAlign4)) {
1617          lir->flags.size = (offset & 0x2);
1618          lir->flags.fixup = kFixupAlign4;
1619        } else {
1620          lir->flags.size = 0;
1621          lir->flags.fixup = kFixupLabel;
1622        }
1623        // Link into the fixup chain.
1624        lir->flags.use_def_invalid = true;
1625        lir->u.a.pcrel_next = NULL;
1626        if (first_fixup_ == NULL) {
1627          first_fixup_ = lir;
1628        } else {
1629          last_fixup->u.a.pcrel_next = lir;
1630        }
1631        last_fixup = lir;
1632      } else {
1633        EncodeLIR(lir);
1634      }
1635      for (int i = 0; i < lir->flags.size; i++) {
1636        code_buffer_.push_back(lir->u.a.bytes[i]);
1637      }
1638      offset += lir->flags.size;
1639    }
1640  }
1641  return offset;
1642}
1643
1644void ArmMir2Lir::AssignDataOffsets() {
1645  /* Set up offsets for literals */
1646  CodeOffset offset = data_offset_;
1647
1648  offset = AssignLiteralOffset(offset);
1649
1650  offset = AssignSwitchTablesOffset(offset);
1651
1652  total_size_ = AssignFillArrayDataOffset(offset);
1653}
1654
1655}  // namespace art
1656