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