OSDN Git Service

Fix an invoke-interface bug that manifests itself with thread state warnings.
[android-x86/dalvik.git] / vm / compiler / codegen / arm / Assemble.c
1 /*
2  * Copyright (C) 2009 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 "Dalvik.h"
18 #include "libdex/OpCode.h"
19 #include "dexdump/OpCodeNames.h"
20
21 #include "../../CompilerInternals.h"
22 #include "ArmLIR.h"
23 #include <unistd.h>             /* for cacheflush */
24
25 /*
26  * opcode: ArmOpCode enum
27  * skeleton: pre-designated bit-pattern for this opcode
28  * k0: key to applying ds/de
29  * ds: dest start bit position
30  * de: dest end bit position
31  * k1: key to applying s1s/s1e
32  * s1s: src1 start bit position
33  * s1e: src1 end bit position
34  * k2: key to applying s2s/s2e
35  * s2s: src2 start bit position
36  * s2e: src2 end bit position
37  * operands: number of operands (for sanity check purposes)
38  * name: mnemonic name
39  * fmt: for pretty-prining
40  */
41 #define ENCODING_MAP(opcode, skeleton, k0, ds, de, k1, s1s, s1e, k2, s2s, s2e, \
42                      k3, k3s, k3e, flags, name, fmt, size) \
43         {skeleton, {{k0, ds, de}, {k1, s1s, s1e}, {k2, s2s, s2e}, \
44                     {k3, k3s, k3e}}, opcode, flags, name, fmt, size}
45
46 /* Instruction dump string format keys: !pf, where "!" is the start
47  * of the key, "p" is which numeric operand to use and "f" is the
48  * print format.
49  *
50  * [p]ositions:
51  *     0 -> operands[0] (dest)
52  *     1 -> operands[1] (src1)
53  *     2 -> operands[2] (src2)
54  *     3 -> operands[3] (extra)
55  *
56  * [f]ormats:
57  *     h -> 4-digit hex
58  *     d -> decimal
59  *     E -> decimal*4
60  *     F -> decimal*2
61  *     c -> branch condition (beq, bne, etc.)
62  *     t -> pc-relative target
63  *     u -> 1st half of bl[x] target
64  *     v -> 2nd half ob bl[x] target
65  *     R -> register list
66  *     s -> single precision floating point register
67  *     S -> double precision floating point register
68  *     m -> Thumb2 modified immediate
69  *     n -> complimented Thumb2 modified immediate
70  *     M -> Thumb2 16-bit zero-extended immediate
71  *     b -> 4-digit binary
72  *
73  *  [!] escape.  To insert "!", use "!!"
74  */
75 /* NOTE: must be kept in sync with enum ArmOpcode from ArmLIR.h */
76 ArmEncodingMap EncodingMap[ARM_LAST] = {
77     ENCODING_MAP(ARM_16BIT_DATA,    0x0000,
78                  BITBLT, 15, 0, UNUSED, -1, -1, UNUSED, -1, -1, UNUSED, -1, -1,
79                  IS_UNARY_OP,
80                  "data", "0x!0h(!0d)", 1),
81     ENCODING_MAP(THUMB_ADC_RR,        0x4140,
82                  BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1, UNUSED, -1, -1,
83                  IS_BINARY_OP | REG_DEF0_USE01 | SETS_CCODES | USES_CCODES,
84                  "adcs", "r!0d, r!1d", 1),
85     ENCODING_MAP(THUMB_ADD_RRI3,      0x1c00,
86                  BITBLT, 2, 0, BITBLT, 5, 3, BITBLT, 8, 6, UNUSED, -1, -1,
87                  IS_TERTIARY_OP | REG_DEF0_USE1 | SETS_CCODES,
88                  "adds", "r!0d, r!1d, #!2d", 1),
89     ENCODING_MAP(THUMB_ADD_RI8,       0x3000,
90                  BITBLT, 10, 8, BITBLT, 7, 0, UNUSED, -1, -1, UNUSED, -1, -1,
91                  IS_BINARY_OP | REG_DEF0_USE0 | SETS_CCODES,
92                  "adds", "r!0d, r!0d, #!1d", 1),
93     ENCODING_MAP(THUMB_ADD_RRR,       0x1800,
94                  BITBLT, 2, 0, BITBLT, 5, 3, BITBLT, 8, 6, UNUSED, -1, -1,
95                  IS_TERTIARY_OP | REG_DEF0_USE12 | SETS_CCODES,
96                  "adds", "r!0d, r!1d, r!2d", 1),
97     ENCODING_MAP(THUMB_ADD_RR_LH,     0x4440,
98                  BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1, UNUSED, -1, -1,
99                  IS_BINARY_OP | REG_DEF0_USE01,
100                  "add", "r!0d, r!1d", 1),
101     ENCODING_MAP(THUMB_ADD_RR_HL,     0x4480,
102                  BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1, UNUSED, -1, -1,
103                  IS_BINARY_OP | REG_DEF0_USE01,
104                  "add", "r!0d, r!1d", 1),
105     ENCODING_MAP(THUMB_ADD_RR_HH,     0x44c0,
106                  BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1, UNUSED, -1, -1,
107                  IS_BINARY_OP | REG_DEF0_USE01,
108                  "add", "r!0d, r!1d", 1),
109     ENCODING_MAP(THUMB_ADD_PC_REL,    0xa000,
110                  BITBLT, 10, 8, BITBLT, 7, 0, UNUSED, -1, -1, UNUSED, -1, -1,
111                  IS_TERTIARY_OP | IS_BRANCH,
112                  "add", "r!0d, pc, #!1E", 1),
113     ENCODING_MAP(THUMB_ADD_SP_REL,    0xa800,
114                  BITBLT, 10, 8, UNUSED, -1, -1, BITBLT, 7, 0, UNUSED, -1, -1,
115                  IS_TERTIARY_OP | REG_DEF_SP | REG_USE_SP,
116                  "add", "r!0d, sp, #!2E", 1),
117     ENCODING_MAP(THUMB_ADD_SPI7,      0xb000,
118                  BITBLT, 6, 0, UNUSED, -1, -1, UNUSED, -1, -1, UNUSED, -1, -1,
119                  IS_UNARY_OP | REG_DEF_SP | REG_USE_SP,
120                  "add", "sp, #!0d*4", 1),
121     ENCODING_MAP(THUMB_AND_RR,        0x4000,
122                  BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1, UNUSED, -1, -1,
123                  IS_BINARY_OP | REG_DEF0_USE01 | SETS_CCODES,
124                  "ands", "r!0d, r!1d", 1),
125     ENCODING_MAP(THUMB_ASR_RRI5,      0x1000,
126                  BITBLT, 2, 0, BITBLT, 5, 3, BITBLT, 10, 6, UNUSED, -1, -1,
127                  IS_TERTIARY_OP | REG_DEF0_USE1 | SETS_CCODES,
128                  "asrs", "r!0d, r!1d, #!2d", 1),
129     ENCODING_MAP(THUMB_ASR_RR,        0x4100,
130                  BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1, UNUSED, -1, -1,
131                  IS_BINARY_OP | REG_DEF0_USE01 | SETS_CCODES,
132                  "asrs", "r!0d, r!1d", 1),
133     ENCODING_MAP(THUMB_B_COND,        0xd000,
134                  BITBLT, 7, 0, BITBLT, 11, 8, UNUSED, -1, -1, UNUSED, -1, -1,
135                  IS_BINARY_OP | IS_BRANCH | USES_CCODES,
136                  "b!1c", "!0t", 1),
137     ENCODING_MAP(THUMB_B_UNCOND,      0xe000,
138                  BITBLT, 10, 0, UNUSED, -1, -1, UNUSED, -1, -1, UNUSED, -1, -1,
139                  NO_OPERAND | IS_BRANCH,
140                  "b", "!0t", 1),
141     ENCODING_MAP(THUMB_BIC_RR,        0x4380,
142                  BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1, UNUSED, -1, -1,
143                  IS_BINARY_OP | REG_DEF0_USE01 | SETS_CCODES,
144                  "bics", "r!0d, r!1d", 1),
145     ENCODING_MAP(THUMB_BKPT,          0xbe00,
146                  BITBLT, 7, 0, UNUSED, -1, -1, UNUSED, -1, -1, UNUSED, -1, -1,
147                  IS_UNARY_OP | IS_BRANCH,
148                  "bkpt", "!0d", 1),
149     ENCODING_MAP(THUMB_BLX_1,         0xf000,
150                  BITBLT, 10, 0, UNUSED, -1, -1, UNUSED, -1, -1, UNUSED, -1, -1,
151                  IS_BINARY_OP | IS_BRANCH | REG_DEF_LR,
152                  "blx_1", "!0u", 1),
153     ENCODING_MAP(THUMB_BLX_2,         0xe800,
154                  BITBLT, 10, 0, UNUSED, -1, -1, UNUSED, -1, -1, UNUSED, -1, -1,
155                  IS_BINARY_OP | IS_BRANCH | REG_DEF_LR,
156                  "blx_2", "!0v", 1),
157     ENCODING_MAP(THUMB_BL_1,          0xf000,
158                  BITBLT, 10, 0, UNUSED, -1, -1, UNUSED, -1, -1, UNUSED, -1, -1,
159                  IS_UNARY_OP | IS_BRANCH | REG_DEF_LR,
160                  "bl_1", "!0u", 1),
161     ENCODING_MAP(THUMB_BL_2,          0xf800,
162                  BITBLT, 10, 0, UNUSED, -1, -1, UNUSED, -1, -1, UNUSED, -1, -1,
163                  IS_UNARY_OP | IS_BRANCH | REG_DEF_LR,
164                  "bl_2", "!0v", 1),
165     ENCODING_MAP(THUMB_BLX_R,         0x4780,
166                  BITBLT, 6, 3, UNUSED, -1, -1, UNUSED, -1, -1, UNUSED, -1, -1,
167                  IS_UNARY_OP | REG_USE0 | IS_BRANCH | REG_DEF_LR,
168                  "blx", "r!0d", 1),
169     ENCODING_MAP(THUMB_BX,            0x4700,
170                  BITBLT, 6, 3, UNUSED, -1, -1, UNUSED, -1, -1, UNUSED, -1, -1,
171                  IS_UNARY_OP | IS_BRANCH,
172                  "bx", "r!0d", 1),
173     ENCODING_MAP(THUMB_CMN_RR,        0x42c0,
174                  BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1, UNUSED, -1, -1,
175                  IS_BINARY_OP | REG_USE01 | SETS_CCODES,
176                  "cmn", "r!0d, r!1d", 1),
177     ENCODING_MAP(THUMB_CMP_RI8,       0x2800,
178                  BITBLT, 10, 8, BITBLT, 7, 0, UNUSED, -1, -1, UNUSED, -1, -1,
179                  IS_BINARY_OP | REG_USE0 | SETS_CCODES,
180                  "cmp", "r!0d, #!1d", 1),
181     ENCODING_MAP(THUMB_CMP_RR,        0x4280,
182                  BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1, UNUSED, -1, -1,
183                  IS_BINARY_OP | REG_USE01 | SETS_CCODES,
184                  "cmp", "r!0d, r!1d", 1),
185     ENCODING_MAP(THUMB_CMP_LH,        0x4540,
186                  BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1, UNUSED, -1, -1,
187                  IS_BINARY_OP | REG_USE01 | SETS_CCODES,
188                  "cmp", "r!0d, r!1d", 1),
189     ENCODING_MAP(THUMB_CMP_HL,        0x4580,
190                  BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1, UNUSED, -1, -1,
191                  IS_BINARY_OP | REG_USE01 | SETS_CCODES,
192                  "cmp", "r!0d, r!1d", 1),
193     ENCODING_MAP(THUMB_CMP_HH,        0x45c0,
194                  BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1, UNUSED, -1, -1,
195                  IS_BINARY_OP | REG_USE01 | SETS_CCODES,
196                  "cmp", "r!0d, r!1d", 1),
197     ENCODING_MAP(THUMB_EOR_RR,        0x4040,
198                  BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1, UNUSED, -1, -1,
199                  IS_BINARY_OP | REG_DEF0_USE01 | SETS_CCODES,
200                  "eors", "r!0d, r!1d", 1),
201     ENCODING_MAP(THUMB_LDMIA,         0xc800,
202                  BITBLT, 10, 8, BITBLT, 7, 0, UNUSED, -1, -1, UNUSED, -1, -1,
203                  IS_BINARY_OP | REG_DEF0_USE0 | REG_DEF_LIST1,
204                  "ldmia", "r!0d!!, <!1R>", 1),
205     ENCODING_MAP(THUMB_LDR_RRI5,      0x6800,
206                  BITBLT, 2, 0, BITBLT, 5, 3, BITBLT, 10, 6, UNUSED, -1, -1,
207                  IS_TERTIARY_OP | REG_DEF0_USE1,
208                  "ldr", "r!0d, [r!1d, #!2E]", 1),
209     ENCODING_MAP(THUMB_LDR_RRR,       0x5800,
210                  BITBLT, 2, 0, BITBLT, 5, 3, BITBLT, 8, 6, UNUSED, -1, -1,
211                  IS_TERTIARY_OP | REG_DEF0_USE12,
212                  "ldr", "r!0d, [r!1d, r!2d]", 1),
213     ENCODING_MAP(THUMB_LDR_PC_REL,    0x4800,
214                  BITBLT, 10, 8, BITBLT, 7, 0, UNUSED, -1, -1, UNUSED, -1, -1,
215                  IS_TERTIARY_OP | REG_DEF0 | REG_USE_PC,
216                  "ldr", "r!0d, [pc, #!1E]", 1),
217     ENCODING_MAP(THUMB_LDR_SP_REL,    0x9800,
218                  BITBLT, 10, 8, UNUSED, -1, -1, BITBLT, 7, 0, UNUSED, -1, -1,
219                  IS_TERTIARY_OP | REG_DEF0 | REG_USE_SP,
220                  "ldr", "r!0d, [sp, #!2E]", 1),
221     ENCODING_MAP(THUMB_LDRB_RRI5,     0x7800,
222                  BITBLT, 2, 0, BITBLT, 5, 3, BITBLT, 10, 6, UNUSED, -1, -1,
223                  IS_TERTIARY_OP | REG_DEF0_USE1,
224                  "ldrb", "r!0d, [r!1d, #2d]", 1),
225     ENCODING_MAP(THUMB_LDRB_RRR,      0x5c00,
226                  BITBLT, 2, 0, BITBLT, 5, 3, BITBLT, 8, 6, UNUSED, -1, -1,
227                  IS_TERTIARY_OP | REG_DEF0_USE12,
228                  "ldrb", "r!0d, [r!1d, r!2d]", 1),
229     ENCODING_MAP(THUMB_LDRH_RRI5,     0x8800,
230                  BITBLT, 2, 0, BITBLT, 5, 3, BITBLT, 10, 6, UNUSED, -1, -1,
231                  IS_TERTIARY_OP | REG_DEF0_USE1,
232                  "ldrh", "r!0d, [r!1d, #!2F]", 1),
233     ENCODING_MAP(THUMB_LDRH_RRR,      0x5a00,
234                  BITBLT, 2, 0, BITBLT, 5, 3, BITBLT, 8, 6, UNUSED, -1, -1,
235                  IS_TERTIARY_OP | REG_DEF0_USE12,
236                  "ldrh", "r!0d, [r!1d, r!2d]", 1),
237     ENCODING_MAP(THUMB_LDRSB_RRR,     0x5600,
238                  BITBLT, 2, 0, BITBLT, 5, 3, BITBLT, 8, 6, UNUSED, -1, -1,
239                  IS_TERTIARY_OP | REG_DEF0_USE12,
240                  "ldrsb", "r!0d, [r!1d, r!2d]", 1),
241     ENCODING_MAP(THUMB_LDRSH_RRR,     0x5e00,
242                  BITBLT, 2, 0, BITBLT, 5, 3, BITBLT, 8, 6, UNUSED, -1, -1,
243                  IS_TERTIARY_OP | REG_DEF0_USE12,
244                  "ldrsh", "r!0d, [r!1d, r!2d]", 1),
245     ENCODING_MAP(THUMB_LSL_RRI5,      0x0000,
246                  BITBLT, 2, 0, BITBLT, 5, 3, BITBLT, 10, 6, UNUSED, -1, -1,
247                  IS_TERTIARY_OP | REG_DEF0_USE1 | SETS_CCODES,
248                  "lsls", "r!0d, r!1d, #!2d", 1),
249     ENCODING_MAP(THUMB_LSL_RR,        0x4080,
250                  BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1, UNUSED, -1, -1,
251                  IS_BINARY_OP | REG_DEF0_USE01 | SETS_CCODES,
252                  "lsls", "r!0d, r!1d", 1),
253     ENCODING_MAP(THUMB_LSR_RRI5,      0x0800,
254                  BITBLT, 2, 0, BITBLT, 5, 3, BITBLT, 10, 6, UNUSED, -1, -1,
255                  IS_TERTIARY_OP | REG_DEF0_USE1 | SETS_CCODES,
256                  "lsrs", "r!0d, r!1d, #!2d", 1),
257     ENCODING_MAP(THUMB_LSR_RR,        0x40c0,
258                  BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1, UNUSED, -1, -1,
259                  IS_BINARY_OP | REG_DEF0_USE01 | SETS_CCODES,
260                  "lsrs", "r!0d, r!1d", 1),
261     ENCODING_MAP(THUMB_MOV_IMM,       0x2000,
262                  BITBLT, 10, 8, BITBLT, 7, 0, UNUSED, -1, -1, UNUSED, -1, -1,
263                  IS_BINARY_OP | REG_DEF0 | SETS_CCODES,
264                  "movs", "r!0d, #!1d", 1),
265     ENCODING_MAP(THUMB_MOV_RR,        0x1c00,
266                  BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1, UNUSED, -1, -1,
267                  IS_BINARY_OP | REG_DEF0_USE1 | SETS_CCODES,
268                  "movs", "r!0d, r!1d", 1),
269     ENCODING_MAP(THUMB_MOV_RR_H2H,    0x46c0,
270                  BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1, UNUSED, -1, -1,
271                  IS_BINARY_OP | REG_DEF0_USE1,
272                  "mov", "r!0d, r!1d", 1),
273     ENCODING_MAP(THUMB_MOV_RR_H2L,    0x4640,
274                  BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1, UNUSED, -1, -1,
275                  IS_BINARY_OP | REG_DEF0_USE1,
276                  "mov", "r!0d, r!1d", 1),
277     ENCODING_MAP(THUMB_MOV_RR_L2H,    0x4680,
278                  BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1, UNUSED, -1, -1,
279                  IS_BINARY_OP | REG_DEF0_USE1,
280                  "mov", "r!0d, r!1d", 1),
281     ENCODING_MAP(THUMB_MUL,           0x4340,
282                  BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1, UNUSED, -1, -1,
283                  IS_BINARY_OP | REG_DEF0_USE01 | SETS_CCODES,
284                  "muls", "r!0d, r!1d", 1),
285     ENCODING_MAP(THUMB_MVN,           0x43c0,
286                  BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1, UNUSED, -1, -1,
287                  IS_BINARY_OP | REG_DEF0_USE1 | SETS_CCODES,
288                  "mvns", "r!0d, r!1d", 1),
289     ENCODING_MAP(THUMB_NEG,           0x4240,
290                  BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1, UNUSED, -1, -1,
291                  IS_BINARY_OP | REG_DEF0_USE1 | SETS_CCODES,
292                  "negs", "r!0d, r!1d", 1),
293     ENCODING_MAP(THUMB_ORR,           0x4300,
294                  BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1, UNUSED, -1, -1,
295                  IS_BINARY_OP | REG_DEF0_USE01 | SETS_CCODES,
296                  "orrs", "r!0d, r!1d", 1),
297     ENCODING_MAP(THUMB_POP,           0xbc00,
298                  BITBLT, 8, 0, UNUSED, -1, -1, UNUSED, -1, -1, UNUSED, -1, -1,
299                  IS_UNARY_OP | REG_DEF_SP | REG_USE_SP | REG_DEF_LIST0,
300                  "pop", "<!0R>", 1),
301     ENCODING_MAP(THUMB_PUSH,          0xb400,
302                  BITBLT, 8, 0, UNUSED, -1, -1, UNUSED, -1, -1, UNUSED, -1, -1,
303                  IS_UNARY_OP | REG_DEF_SP | REG_USE_SP | REG_USE_LIST0,
304                  "push", "<!0R>", 1),
305     ENCODING_MAP(THUMB_ROR_RR,        0x41c0,
306                  BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1, UNUSED, -1, -1,
307                  IS_BINARY_OP | REG_DEF0_USE01 | SETS_CCODES,
308                  "rors", "r!0d, r!1d", 1),
309     ENCODING_MAP(THUMB_SBC,           0x4180,
310                  BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1, UNUSED, -1, -1,
311                  IS_BINARY_OP | REG_DEF0_USE01 | USES_CCODES | SETS_CCODES,
312                  "sbcs", "r!0d, r!1d", 1),
313     ENCODING_MAP(THUMB_STMIA,         0xc000,
314                  BITBLT, 10, 8, BITBLT, 7, 0, UNUSED, -1, -1, UNUSED, -1, -1,
315                  IS_BINARY_OP | REG_DEF0 | REG_USE0 | REG_USE_LIST1,
316                  "stmia", "r!0d!!, <!1R>", 1),
317     ENCODING_MAP(THUMB_STR_RRI5,      0x6000,
318                  BITBLT, 2, 0, BITBLT, 5, 3, BITBLT, 10, 6, UNUSED, -1, -1,
319                  IS_TERTIARY_OP | REG_USE01,
320                  "str", "r!0d, [r!1d, #!2E]", 1),
321     ENCODING_MAP(THUMB_STR_RRR,       0x5000,
322                  BITBLT, 2, 0, BITBLT, 5, 3, BITBLT, 8, 6, UNUSED, -1, -1,
323                  IS_TERTIARY_OP | REG_USE012,
324                  "str", "r!0d, [r!1d, r!2d]", 1),
325     ENCODING_MAP(THUMB_STR_SP_REL,    0x9000,
326                  BITBLT, 10, 8, UNUSED, -1, -1, BITBLT, 7, 0, UNUSED, -1, -1,
327                  IS_TERTIARY_OP | REG_USE0 | REG_USE_SP,
328                  "str", "r!0d, [sp, #!2E]", 1),
329     ENCODING_MAP(THUMB_STRB_RRI5,     0x7000,
330                  BITBLT, 2, 0, BITBLT, 5, 3, BITBLT, 10, 6, UNUSED, -1, -1,
331                  IS_TERTIARY_OP | REG_USE01,
332                  "strb", "r!0d, [r!1d, #!2d]", 1),
333     ENCODING_MAP(THUMB_STRB_RRR,      0x5400,
334                  BITBLT, 2, 0, BITBLT, 5, 3, BITBLT, 8, 6, UNUSED, -1, -1,
335                  IS_TERTIARY_OP | REG_USE012,
336                  "strb", "r!0d, [r!1d, r!2d]", 1),
337     ENCODING_MAP(THUMB_STRH_RRI5,     0x8000,
338                  BITBLT, 2, 0, BITBLT, 5, 3, BITBLT, 10, 6, UNUSED, -1, -1,
339                  IS_TERTIARY_OP | REG_USE01,
340                  "strh", "r!0d, [r!1d, #!2F]", 1),
341     ENCODING_MAP(THUMB_STRH_RRR,      0x5200,
342                  BITBLT, 2, 0, BITBLT, 5, 3, BITBLT, 8, 6, UNUSED, -1, -1,
343                  IS_TERTIARY_OP | REG_USE012,
344                  "strh", "r!0d, [r!1d, r!2d]", 1),
345     ENCODING_MAP(THUMB_SUB_RRI3,      0x1e00,
346                  BITBLT, 2, 0, BITBLT, 5, 3, BITBLT, 8, 6, UNUSED, -1, -1,
347                  IS_TERTIARY_OP | REG_DEF0_USE1 | SETS_CCODES,
348                  "subs", "r!0d, r!1d, #!2d]", 1),
349     ENCODING_MAP(THUMB_SUB_RI8,       0x3800,
350                  BITBLT, 10, 8, BITBLT, 7, 0, UNUSED, -1, -1, UNUSED, -1, -1,
351                  IS_BINARY_OP | REG_DEF0_USE0 | SETS_CCODES,
352                  "subs", "r!0d, #!1d", 1),
353     ENCODING_MAP(THUMB_SUB_RRR,       0x1a00,
354                  BITBLT, 2, 0, BITBLT, 5, 3, BITBLT, 8, 6, UNUSED, -1, -1,
355                  IS_TERTIARY_OP | REG_DEF0_USE12 | SETS_CCODES,
356                  "subs", "r!0d, r!1d, r!2d", 1),
357     ENCODING_MAP(THUMB_SUB_SPI7,      0xb080,
358                  BITBLT, 6, 0, UNUSED, -1, -1, UNUSED, -1, -1, UNUSED, -1, -1,
359                  IS_UNARY_OP | REG_DEF_SP | REG_USE_SP,
360                  "sub", "sp, #!0d", 1),
361     ENCODING_MAP(THUMB_SWI,           0xdf00,
362                  BITBLT, 7, 0, UNUSED, -1, -1, UNUSED, -1, -1, UNUSED, -1, -1,
363                  IS_UNARY_OP | IS_BRANCH,
364                  "swi", "!0d", 1),
365     ENCODING_MAP(THUMB_TST,           0x4200,
366                  BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1, UNUSED, -1, -1,
367                  IS_UNARY_OP | REG_USE01 | SETS_CCODES,
368                  "tst", "r!0d, r!1d", 1),
369     ENCODING_MAP(THUMB2_VLDRS,       0xed900a00,
370                  SFP, 22, 12, BITBLT, 19, 16, BITBLT, 7, 0, UNUSED, -1, -1,
371                  IS_TERTIARY_OP | REG_DEF0_USE1,
372                  "vldr", "!0s, [r!1d, #!2E]", 2),
373     ENCODING_MAP(THUMB2_VLDRD,       0xed900b00,
374                  DFP, 22, 12, BITBLT, 19, 16, BITBLT, 7, 0, UNUSED, -1, -1,
375                  IS_TERTIARY_OP | REG_DEF0_USE1,
376                  "vldr", "!0S, [r!1d, #!2E]", 2),
377     ENCODING_MAP(THUMB2_VMULS,        0xee200a00,
378                  SFP, 22, 12, SFP, 7, 16, SFP, 5, 0, UNUSED, -1, -1,
379                  IS_TERTIARY_OP | REG_DEF0_USE12,
380                  "vmuls", "!0s, !1s, !2s", 2),
381     ENCODING_MAP(THUMB2_VMULD,        0xee200b00,
382                  DFP, 22, 12, DFP, 7, 16, DFP, 5, 0, UNUSED, -1, -1,
383                  IS_TERTIARY_OP | REG_DEF0_USE12,
384                  "vmuld", "!0S, !1S, !2S", 2),
385     ENCODING_MAP(THUMB2_VSTRS,       0xed800a00,
386                  SFP, 22, 12, BITBLT, 19, 16, BITBLT, 7, 0, UNUSED, -1, -1,
387                  IS_TERTIARY_OP | REG_USE01,
388                  "vstr", "!0s, [r!1d, #!2E]", 2),
389     ENCODING_MAP(THUMB2_VSTRD,       0xed800b00,
390                  DFP, 22, 12, BITBLT, 19, 16, BITBLT, 7, 0, UNUSED, -1, -1,
391                  IS_TERTIARY_OP | REG_USE01,
392                  "vstr", "!0S, [r!1d, #!2E]", 2),
393     ENCODING_MAP(THUMB2_VSUBS,        0xee300a40,
394                  SFP, 22, 12, SFP, 7, 16, SFP, 5, 0, UNUSED, -1, -1,
395                  IS_TERTIARY_OP | REG_DEF0_USE12,
396                  "vsub", "!0s, !1s, !2s", 2),
397     ENCODING_MAP(THUMB2_VSUBD,        0xee300b40,
398                  DFP, 22, 12, DFP, 7, 16, DFP, 5, 0, UNUSED, -1, -1,
399                  IS_TERTIARY_OP | REG_DEF0_USE12,
400                  "vsub", "!0S, !1S, !2S", 2),
401     ENCODING_MAP(THUMB2_VADDS,        0xee300a00,
402                  SFP, 22, 12, SFP, 7, 16, SFP, 5, 0, UNUSED, -1, -1,
403                  IS_TERTIARY_OP | REG_DEF0_USE12,
404                  "vadd", "!0s, !1s, !2s", 2),
405     ENCODING_MAP(THUMB2_VADDD,        0xee300b00,
406                  DFP, 22, 12, DFP, 7, 16, DFP, 5, 0, UNUSED, -1, -1,
407                  IS_TERTIARY_OP | REG_DEF0_USE12,
408                  "vadd", "!0S, !1S, !2S", 2),
409     ENCODING_MAP(THUMB2_VDIVS,        0xee800a00,
410                  SFP, 22, 12, SFP, 7, 16, SFP, 5, 0, UNUSED, -1, -1,
411                  IS_TERTIARY_OP | REG_DEF0_USE12,
412                  "vdivs", "!0s, !1s, !2s", 2),
413     ENCODING_MAP(THUMB2_VDIVD,        0xee800b00,
414                  DFP, 22, 12, DFP, 7, 16, DFP, 5, 0, UNUSED, -1, -1,
415                  IS_TERTIARY_OP | REG_DEF0_USE12,
416                  "vdivd", "!0S, !1S, !2S", 2),
417     ENCODING_MAP(THUMB2_VCVTIF,       0xeeb80ac0,
418                  SFP, 22, 12, SFP, 5, 0, UNUSED, -1, -1, UNUSED, -1, -1,
419                  IS_BINARY_OP | REG_DEF0_USE1,
420                  "vcvt.f32", "!0s, !1s", 2),
421     ENCODING_MAP(THUMB2_VCVTID,       0xeeb80bc0,
422                  DFP, 22, 12, SFP, 5, 0, UNUSED, -1, -1, UNUSED, -1, -1,
423                  IS_BINARY_OP | REG_DEF0_USE1,
424                  "vcvt.f64", "!0S, !1s", 2),
425     ENCODING_MAP(THUMB2_VCVTFI,       0xeebd0ac0,
426                  SFP, 22, 12, SFP, 5, 0, UNUSED, -1, -1, UNUSED, -1, -1,
427                  IS_BINARY_OP | REG_DEF0_USE1,
428                  "vcvt.s32.f32 ", "!0s, !1s", 2),
429     ENCODING_MAP(THUMB2_VCVTDI,       0xeebd0bc0,
430                  SFP, 22, 12, DFP, 5, 0, UNUSED, -1, -1, UNUSED, -1, -1,
431                  IS_BINARY_OP | REG_DEF0_USE1,
432                  "vcvt.s32.f64 ", "!0s, !1S", 2),
433     ENCODING_MAP(THUMB2_VCVTFD,       0xeeb70ac0,
434                  DFP, 22, 12, SFP, 5, 0, UNUSED, -1, -1, UNUSED, -1, -1,
435                  IS_BINARY_OP | REG_DEF0_USE1,
436                  "vcvt.f64.f32 ", "!0S, !1s", 2),
437     ENCODING_MAP(THUMB2_VCVTDF,       0xeeb70bc0,
438                  SFP, 22, 12, DFP, 5, 0, UNUSED, -1, -1, UNUSED, -1, -1,
439                  IS_BINARY_OP | REG_DEF0_USE1,
440                  "vcvt.f32.f64 ", "!0s, !1S", 2),
441     ENCODING_MAP(THUMB2_VSQRTS,       0xeeb10ac0,
442                  SFP, 22, 12, SFP, 5, 0, UNUSED, -1, -1, UNUSED, -1, -1,
443                  IS_BINARY_OP | REG_DEF0_USE1,
444                  "vsqrt.f32 ", "!0s, !1s", 2),
445     ENCODING_MAP(THUMB2_VSQRTD,       0xeeb10bc0,
446                  DFP, 22, 12, DFP, 5, 0, UNUSED, -1, -1, UNUSED, -1, -1,
447                  IS_BINARY_OP | REG_DEF0_USE1,
448                  "vsqrt.f64 ", "!0S, !1S", 2),
449     ENCODING_MAP(THUMB2_MOV_IMM_SHIFT, 0xf04f0000, /* no setflags encoding */
450                  BITBLT, 11, 8, MODIMM, -1, -1, UNUSED, -1, -1, UNUSED, -1, -1,
451                  IS_BINARY_OP | REG_DEF0,
452                  "mov", "r!0d, #!1m", 2),
453     ENCODING_MAP(THUMB2_MOV_IMM16,       0xf2400000,
454                  BITBLT, 11, 8, IMM16, -1, -1, UNUSED, -1, -1, UNUSED, -1, -1,
455                  IS_BINARY_OP | REG_DEF0,
456                  "mov", "r!0d, #!1M", 2),
457     ENCODING_MAP(THUMB2_STR_RRI12,       0xf8c00000,
458                  BITBLT, 15, 12, BITBLT, 19, 16, BITBLT, 11, 0, UNUSED, -1, -1,
459                  IS_TERTIARY_OP | REG_USE01,
460                  "str", "r!0d,[r!1d, #!2d", 2),
461     ENCODING_MAP(THUMB2_LDR_RRI12,       0xf8d00000,
462                  BITBLT, 15, 12, BITBLT, 19, 16, BITBLT, 11, 0, UNUSED, -1, -1,
463                  IS_TERTIARY_OP | REG_DEF0_USE1,
464                  "ldr", "r!0d,[r!1d, #!2d", 2),
465     ENCODING_MAP(THUMB2_STR_RRI8_PREDEC,       0xf8400c00,
466                  BITBLT, 15, 12, BITBLT, 19, 16, BITBLT, 8, 0, UNUSED, -1, -1,
467                  IS_TERTIARY_OP | REG_USE01,
468                  "str", "r!0d,[r!1d, #-!2d]", 2),
469     ENCODING_MAP(THUMB2_LDR_RRI8_PREDEC,       0xf8500c00,
470                  BITBLT, 15, 12, BITBLT, 19, 16, BITBLT, 8, 0, UNUSED, -1, -1,
471                  IS_TERTIARY_OP | REG_DEF0_USE1,
472                  "ldr", "r!0d,[r!1d, #-!2d]", 2),
473     ENCODING_MAP(THUMB2_CBNZ,       0xb900, /* Note: does not affect flags */
474                  BITBLT, 2, 0, IMM6, -1, -1, UNUSED, -1, -1, UNUSED, -1, -1,
475                  IS_BINARY_OP | REG_USE0 | IS_BRANCH,
476                  "cbnz", "r!0d,!1t", 1),
477     ENCODING_MAP(THUMB2_CBZ,       0xb100, /* Note: does not affect flags */
478                  BITBLT, 2, 0, IMM6, -1, -1, UNUSED, -1, -1, UNUSED, -1, -1,
479                  IS_BINARY_OP | REG_USE0 | IS_BRANCH,
480                  "cbz", "r!0d,!1t", 1),
481     ENCODING_MAP(THUMB2_ADD_RRI12,       0xf2000000,
482                  BITBLT, 11, 8, BITBLT, 19, 16, IMM12, -1, -1, UNUSED, -1, -1,
483                  IS_TERTIARY_OP | REG_DEF0_USE1,/* Note: doesn't affect flags */
484                  "add", "r!0d,r!1d,#!2d", 2),
485     ENCODING_MAP(THUMB2_MOV_RR,       0xea4f0000, /* no setflags encoding */
486                  BITBLT, 11, 8, BITBLT, 3, 0, UNUSED, -1, -1, UNUSED, -1, -1,
487                  IS_BINARY_OP | REG_DEF0_USE1,
488                  "mov", "r!0d, r!1d", 2),
489     ENCODING_MAP(THUMB2_VMOVS,       0xeeb00a40,
490                  SFP, 22, 12, SFP, 5, 0, UNUSED, -1, -1, UNUSED, -1, -1,
491                  IS_BINARY_OP | REG_DEF0_USE1,
492                  "vmov.f32 ", " !0s, !1s", 2),
493     ENCODING_MAP(THUMB2_VMOVD,       0xeeb00b40,
494                  DFP, 22, 12, DFP, 5, 0, UNUSED, -1, -1, UNUSED, -1, -1,
495                  IS_BINARY_OP | REG_DEF0_USE1,
496                  "vmov.f64 ", " !0S, !1S", 2),
497     ENCODING_MAP(THUMB2_LDMIA,         0xe8900000,
498                  BITBLT, 19, 16, BITBLT, 15, 0, UNUSED, -1, -1, UNUSED, -1, -1,
499                  IS_BINARY_OP | REG_DEF0_USE0 | REG_DEF_LIST1,
500                  "ldmia", "r!0d!!, <!1R>", 2),
501     ENCODING_MAP(THUMB2_STMIA,         0xe8800000,
502                  BITBLT, 19, 16, BITBLT, 15, 0, UNUSED, -1, -1, UNUSED, -1, -1,
503                  IS_BINARY_OP | REG_DEF0_USE0 | REG_USE_LIST1,
504                  "stmia", "r!0d!!, <!1R>", 2),
505     ENCODING_MAP(THUMB2_ADD_RRR,  0xeb100000, /* setflags encoding */
506                  BITBLT, 11, 8, BITBLT, 19, 16, BITBLT, 3, 0, SHIFT, -1, -1,
507                  IS_QUAD_OP | REG_DEF0_USE12 | SETS_CCODES,
508                  "adds", "r!0d, r!1d, r!2d", 2),
509     ENCODING_MAP(THUMB2_SUB_RRR,       0xebb00000, /* setflags enconding */
510                  BITBLT, 11, 8, BITBLT, 19, 16, BITBLT, 3, 0, SHIFT, -1, -1,
511                  IS_QUAD_OP | REG_DEF0_USE12 | SETS_CCODES,
512                  "subs", "r!0d, r!1d, r!2d", 2),
513     ENCODING_MAP(THUMB2_SBC_RRR,       0xeb700000, /* setflags encoding */
514                  BITBLT, 11, 8, BITBLT, 19, 16, BITBLT, 3, 0, SHIFT, -1, -1,
515                  IS_QUAD_OP | REG_DEF0_USE12 | USES_CCODES | SETS_CCODES,
516                  "sbcs", "r!0d, r!1d, r!2d", 2),
517     ENCODING_MAP(THUMB2_CMP_RR,       0xebb00f00,
518                  BITBLT, 19, 16, BITBLT, 3, 0, SHIFT, -1, -1, UNUSED, -1, -1,
519                  IS_TERTIARY_OP | REG_USE01 | SETS_CCODES,
520                  "cmp", "r!0d, r!1d", 2),
521     ENCODING_MAP(THUMB2_SUB_RRI12,       0xf2a00000,
522                  BITBLT, 11, 8, BITBLT, 19, 16, IMM12, -1, -1, UNUSED, -1, -1,
523                  IS_TERTIARY_OP | REG_DEF0_USE1,/* Note: doesn't affect flags */
524                  "sub", "r!0d,r!1d,#!2d", 2),
525     ENCODING_MAP(THUMB2_MVN_IMM_SHIFT,  0xf06f0000, /* no setflags encoding */
526                  BITBLT, 11, 8, MODIMM, -1, -1, UNUSED, -1, -1, UNUSED, -1, -1,
527                  IS_BINARY_OP | REG_DEF0,
528                  "mvn", "r!0d, #!1n", 2),
529     ENCODING_MAP(THUMB2_SEL,       0xfaa0f080,
530                  BITBLT, 11, 8, BITBLT, 19, 16, BITBLT, 3, 0, UNUSED, -1, -1,
531                  IS_TERTIARY_OP | REG_DEF0_USE12 | USES_CCODES,
532                  "sel", "r!0d, r!1d, r!2d", 2),
533     ENCODING_MAP(THUMB2_UBFX,       0xf3c00000,
534                  BITBLT, 11, 8, BITBLT, 19, 16, LSB, -1, -1, BWIDTH, 4, 0,
535                  IS_QUAD_OP | REG_DEF0_USE1,
536                  "ubfx", "r!0d, r!1d, #!2d, #!3d", 2),
537     ENCODING_MAP(THUMB2_SBFX,       0xf3400000,
538                  BITBLT, 11, 8, BITBLT, 19, 16, LSB, -1, -1, BWIDTH, 4, 0,
539                  IS_QUAD_OP | REG_DEF0_USE1,
540                  "sbfx", "r!0d, r!1d, #!2d, #!3d", 2),
541     ENCODING_MAP(THUMB2_LDR_RRR,    0xf8500000,
542                  BITBLT, 15, 12, BITBLT, 19, 16, BITBLT, 3, 0, BITBLT, 5, 4,
543                  IS_QUAD_OP | REG_DEF0_USE12,
544                  "ldr", "r!0d,[r!1d, r!2d, LSL #!3d]", 2),
545     ENCODING_MAP(THUMB2_LDRH_RRR,    0xf8300000,
546                  BITBLT, 15, 12, BITBLT, 19, 16, BITBLT, 3, 0, BITBLT, 5, 4,
547                  IS_QUAD_OP | REG_DEF0_USE12,
548                  "ldrh", "r!0d,[r!1d, r!2d, LSL #!3d]", 2),
549     ENCODING_MAP(THUMB2_LDRSH_RRR,    0xf9300000,
550                  BITBLT, 15, 12, BITBLT, 19, 16, BITBLT, 3, 0, BITBLT, 5, 4,
551                  IS_QUAD_OP | REG_DEF0_USE12,
552                  "ldrsh", "r!0d,[r!1d, r!2d, LSL #!3d]", 2),
553     ENCODING_MAP(THUMB2_LDRB_RRR,    0xf8100000,
554                  BITBLT, 15, 12, BITBLT, 19, 16, BITBLT, 3, 0, BITBLT, 5, 4,
555                  IS_QUAD_OP | REG_DEF0_USE12,
556                  "ldrb", "r!0d,[r!1d, r!2d, LSL #!3d]", 2),
557     ENCODING_MAP(THUMB2_LDRSB_RRR,    0xf9100000,
558                  BITBLT, 15, 12, BITBLT, 19, 16, BITBLT, 3, 0, BITBLT, 5, 4,
559                  IS_QUAD_OP | REG_DEF0_USE12,
560                  "ldrsb", "r!0d,[r!1d, r!2d, LSL #!3d]", 2),
561     ENCODING_MAP(THUMB2_STR_RRR,    0xf8400000,
562                  BITBLT, 15, 12, BITBLT, 19, 16, BITBLT, 3, 0, BITBLT, 5, 4,
563                  IS_QUAD_OP | REG_USE012,
564                  "str", "r!0d,[r!1d, r!2d, LSL #!3d]", 2),
565     ENCODING_MAP(THUMB2_STRH_RRR,    0xf8200000,
566                  BITBLT, 15, 12, BITBLT, 19, 16, BITBLT, 3, 0, BITBLT, 5, 4,
567                  IS_QUAD_OP | REG_USE012,
568                  "strh", "r!0d,[r!1d, r!2d, LSL #!3d]", 2),
569     ENCODING_MAP(THUMB2_STRB_RRR,    0xf8000000,
570                  BITBLT, 15, 12, BITBLT, 19, 16, BITBLT, 3, 0, BITBLT, 5, 4,
571                  IS_QUAD_OP | REG_USE012,
572                  "strb", "r!0d,[r!1d, r!2d, LSL #!3d]", 2),
573     ENCODING_MAP(THUMB2_LDRH_RRI12,       0xf8b00000,
574                  BITBLT, 15, 12, BITBLT, 19, 16, BITBLT, 11, 0, UNUSED, -1, -1,
575                  IS_TERTIARY_OP | REG_DEF0_USE1,
576                  "ldrh", "r!0d,[r!1d, #!2d", 2),
577     ENCODING_MAP(THUMB2_LDRSH_RRI12,       0xf9b00000,
578                  BITBLT, 15, 12, BITBLT, 19, 16, BITBLT, 11, 0, UNUSED, -1, -1,
579                  IS_TERTIARY_OP | REG_DEF0_USE1,
580                  "ldrsh", "r!0d,[r!1d, #!2d", 2),
581     ENCODING_MAP(THUMB2_LDRB_RRI12,       0xf8900000,
582                  BITBLT, 15, 12, BITBLT, 19, 16, BITBLT, 11, 0, UNUSED, -1, -1,
583                  IS_TERTIARY_OP | REG_DEF0_USE1,
584                  "ldrb", "r!0d,[r!1d, #!2d", 2),
585     ENCODING_MAP(THUMB2_LDRSB_RRI12,       0xf9900000,
586                  BITBLT, 15, 12, BITBLT, 19, 16, BITBLT, 11, 0, UNUSED, -1, -1,
587                  IS_TERTIARY_OP | REG_DEF0_USE1,
588                  "ldrsb", "r!0d,[r!1d, #!2d", 2),
589     ENCODING_MAP(THUMB2_STRH_RRI12,       0xf8a00000,
590                  BITBLT, 15, 12, BITBLT, 19, 16, BITBLT, 11, 0, UNUSED, -1, -1,
591                  IS_TERTIARY_OP | REG_USE01,
592                  "strh", "r!0d,[r!1d, #!2d", 2),
593     ENCODING_MAP(THUMB2_STRB_RRI12,       0xf8800000,
594                  BITBLT, 15, 12, BITBLT, 19, 16, BITBLT, 11, 0, UNUSED, -1, -1,
595                  IS_TERTIARY_OP | REG_USE01,
596                  "strb", "r!0d,[r!1d, #!2d", 2),
597     ENCODING_MAP(THUMB2_POP,           0xe8bd0000,
598                  BITBLT, 15, 0, UNUSED, -1, -1, UNUSED, -1, -1, UNUSED, -1, -1,
599                  IS_UNARY_OP | REG_DEF_SP | REG_USE_SP | REG_DEF_LIST0,
600                  "pop", "<!0R>", 2),
601     ENCODING_MAP(THUMB2_PUSH,          0xe8ad0000,
602                  BITBLT, 15, 0, UNUSED, -1, -1, UNUSED, -1, -1, UNUSED, -1, -1,
603                  IS_UNARY_OP | REG_DEF_SP | REG_USE_SP | REG_USE_LIST0,
604                  "push", "<!0R>", 2),
605     ENCODING_MAP(THUMB2_CMP_RI8, 0xf1b00f00,
606                  BITBLT, 19, 16, MODIMM, -1, -1, UNUSED, -1, -1, UNUSED, -1, -1,
607                  IS_BINARY_OP | REG_USE0 | SETS_CCODES,
608                  "cmp", "r!0d, #!1m", 2),
609     ENCODING_MAP(THUMB2_ADC_RRR,  0xeb500000, /* setflags encoding */
610                  BITBLT, 11, 8, BITBLT, 19, 16, BITBLT, 3, 0, SHIFT, -1, -1,
611                  IS_QUAD_OP | REG_DEF0_USE12 | SETS_CCODES,
612                  "acds", "r!0d, r!1d, r!2d, shift !3d", 2),
613     ENCODING_MAP(THUMB2_AND_RRR,  0xea000000,
614                  BITBLT, 11, 8, BITBLT, 19, 16, BITBLT, 3, 0, SHIFT, -1, -1,
615                  IS_QUAD_OP | REG_DEF0_USE12,
616                  "and", "r!0d, r!1d, r!2d, shift !3d", 2),
617     ENCODING_MAP(THUMB2_BIC_RRR,  0xea200000,
618                  BITBLT, 11, 8, BITBLT, 19, 16, BITBLT, 3, 0, SHIFT, -1, -1,
619                  IS_QUAD_OP | REG_DEF0_USE12,
620                  "bic", "r!0d, r!1d, r!2d, shift !3d", 2),
621     ENCODING_MAP(THUMB2_CMN_RR,  0xeb000000,
622                  BITBLT, 19, 16, BITBLT, 3, 0, SHIFT, -1, -1, UNUSED, -1, -1,
623                  IS_TERTIARY_OP | REG_DEF0_USE1 | SETS_CCODES,
624                  "cmn", "r!0d, r!1d, shift !2d", 2),
625     ENCODING_MAP(THUMB2_EOR_RRR,  0xea800000,
626                  BITBLT, 11, 8, BITBLT, 19, 16, BITBLT, 3, 0, SHIFT, -1, -1,
627                  IS_QUAD_OP | REG_DEF0_USE12,
628                  "eor", "r!0d, r!1d, r!2d, shift !3d", 2),
629     ENCODING_MAP(THUMB2_MUL_RRR,  0xfb00f000,
630                  BITBLT, 11, 8, BITBLT, 19, 16, BITBLT, 3, 0, UNUSED, -1, -1,
631                  IS_TERTIARY_OP | REG_DEF0_USE12,
632                  "mul", "r!0d, r!1d, r!2d", 2),
633     ENCODING_MAP(THUMB2_MVN_RR,  0xea6f0000,
634                  BITBLT, 11, 8, BITBLT, 3, 0, SHIFT, -1, -1, UNUSED, -1, -1,
635                  IS_TERTIARY_OP | REG_DEF0_USE1,
636                  "mvn", "r!0d, r!1d, shift !2d", 2),
637     ENCODING_MAP(THUMB2_RSUB_RRI8,       0xf1d00000,
638                  BITBLT, 11, 8, BITBLT, 19, 16, MODIMM, -1, -1, UNUSED, -1, -1,
639                  IS_TERTIARY_OP | REG_DEF0_USE1 | SETS_CCODES,
640                  "rsb", "r!0d,r!1d,#!2m", 2),
641     ENCODING_MAP(THUMB2_NEG_RR,       0xf1d00000, /* instance of rsub */
642                  BITBLT, 11, 8, BITBLT, 19, 16, UNUSED, -1, -1, UNUSED, -1, -1,
643                  IS_BINARY_OP | REG_DEF0_USE1 | SETS_CCODES,
644                  "neg", "r!0d,r!1d", 2),
645     ENCODING_MAP(THUMB2_ORR_RRR,  0xea400000,
646                  BITBLT, 11, 8, BITBLT, 19, 16, BITBLT, 3, 0, SHIFT, -1, -1,
647                  IS_QUAD_OP | REG_DEF0_USE12,
648                  "orr", "r!0d, r!1d, r!2d, shift !3d", 2),
649     ENCODING_MAP(THUMB2_TST_RR,       0xea100f00,
650                  BITBLT, 19, 16, BITBLT, 3, 0, SHIFT, -1, -1, UNUSED, -1, -1,
651                  IS_TERTIARY_OP | REG_USE01 | SETS_CCODES,
652                  "tst", "r!0d, r!1d, shift !2d", 2),
653     ENCODING_MAP(THUMB2_LSL_RRR,  0xfa00f000,
654                  BITBLT, 11, 8, BITBLT, 19, 16, BITBLT, 3, 0, UNUSED, -1, -1,
655                  IS_TERTIARY_OP | REG_DEF0_USE12,
656                  "lsl", "r!0d, r!1d, r!2d", 2),
657     ENCODING_MAP(THUMB2_LSR_RRR,  0xfa20f000,
658                  BITBLT, 11, 8, BITBLT, 19, 16, BITBLT, 3, 0, UNUSED, -1, -1,
659                  IS_TERTIARY_OP | REG_DEF0_USE12,
660                  "lsr", "r!0d, r!1d, r!2d", 2),
661     ENCODING_MAP(THUMB2_ASR_RRR,  0xfa40f000,
662                  BITBLT, 11, 8, BITBLT, 19, 16, BITBLT, 3, 0, UNUSED, -1, -1,
663                  IS_TERTIARY_OP | REG_DEF0_USE12,
664                  "asr", "r!0d, r!1d, r!2d", 2),
665     ENCODING_MAP(THUMB2_ROR_RRR,  0xfa60f000,
666                  BITBLT, 11, 8, BITBLT, 19, 16, BITBLT, 3, 0, UNUSED, -1, -1,
667                  IS_TERTIARY_OP | REG_DEF0_USE12,
668                  "ror", "r!0d, r!1d, r!2d", 2),
669     ENCODING_MAP(THUMB2_LSL_RRI5,  0xea4f0000,
670                  BITBLT, 11, 8, BITBLT, 3, 0, SHIFT5, -1, -1, UNUSED, -1, -1,
671                  IS_TERTIARY_OP | REG_DEF0_USE1,
672                  "lsl", "r!0d, r!1d, #!2d", 2),
673     ENCODING_MAP(THUMB2_LSR_RRI5,  0xea4f0010,
674                  BITBLT, 11, 8, BITBLT, 3, 0, SHIFT5, -1, -1, UNUSED, -1, -1,
675                  IS_TERTIARY_OP | REG_DEF0_USE1,
676                  "lsr", "r!0d, r!1d, #!2d", 2),
677     ENCODING_MAP(THUMB2_ASR_RRI5,  0xea4f0020,
678                  BITBLT, 11, 8, BITBLT, 3, 0, SHIFT5, -1, -1, UNUSED, -1, -1,
679                  IS_TERTIARY_OP | REG_DEF0_USE1,
680                  "asr", "r!0d, r!1d, #!2d", 2),
681     ENCODING_MAP(THUMB2_ROR_RRI5,  0xea4f0030,
682                  BITBLT, 11, 8, BITBLT, 3, 0, SHIFT5, -1, -1, UNUSED, -1, -1,
683                  IS_TERTIARY_OP | REG_DEF0_USE1,
684                  "ror", "r!0d, r!1d, #!2d", 2),
685     ENCODING_MAP(THUMB2_BIC_RRI8,  0xf0200000,
686                  BITBLT, 11, 8, BITBLT, 19, 16, MODIMM, -1, -1, UNUSED, -1, -1,
687                  IS_TERTIARY_OP | REG_DEF0_USE1,
688                  "bic", "r!0d, r!1d, #!2m", 2),
689     ENCODING_MAP(THUMB2_AND_RRI8,  0xf0000000,
690                  BITBLT, 11, 8, BITBLT, 19, 16, MODIMM, -1, -1, UNUSED, -1, -1,
691                  IS_TERTIARY_OP | REG_DEF0_USE1,
692                  "and", "r!0d, r!1d, #!2m", 2),
693     ENCODING_MAP(THUMB2_ORR_RRI8,  0xf0400000,
694                  BITBLT, 11, 8, BITBLT, 19, 16, MODIMM, -1, -1, UNUSED, -1, -1,
695                  IS_TERTIARY_OP | REG_DEF0_USE1,
696                  "orr", "r!0d, r!1d, #!2m", 2),
697     ENCODING_MAP(THUMB2_EOR_RRI8,  0xf0800000,
698                  BITBLT, 11, 8, BITBLT, 19, 16, MODIMM, -1, -1, UNUSED, -1, -1,
699                  IS_TERTIARY_OP | REG_DEF0_USE1,
700                  "eor", "r!0d, r!1d, #!2m", 2),
701     ENCODING_MAP(THUMB2_ADD_RRI8,  0xf1100000,
702                  BITBLT, 11, 8, BITBLT, 19, 16, MODIMM, -1, -1, UNUSED, -1, -1,
703                  IS_TERTIARY_OP | REG_DEF0_USE1 | SETS_CCODES,
704                  "adds", "r!0d, r!1d, #!2m", 2),
705     ENCODING_MAP(THUMB2_ADC_RRI8,  0xf1500000,
706                  BITBLT, 11, 8, BITBLT, 19, 16, MODIMM, -1, -1, UNUSED, -1, -1,
707                  IS_TERTIARY_OP | REG_DEF0_USE1 | SETS_CCODES | USES_CCODES,
708                  "adcs", "r!0d, r!1d, #!2m", 2),
709     ENCODING_MAP(THUMB2_SUB_RRI8,  0xf1b00000,
710                  BITBLT, 11, 8, BITBLT, 19, 16, MODIMM, -1, -1, UNUSED, -1, -1,
711                  IS_TERTIARY_OP | REG_DEF0_USE1 | SETS_CCODES,
712                  "subs", "r!0d, r!1d, #!2m", 2),
713     ENCODING_MAP(THUMB2_SBC_RRI8,  0xf1700000,
714                  BITBLT, 11, 8, BITBLT, 19, 16, MODIMM, -1, -1, UNUSED, -1, -1,
715                  IS_TERTIARY_OP | REG_DEF0_USE1 | SETS_CCODES | USES_CCODES,
716                  "sbcs", "r!0d, r!1d, #!2m", 2),
717     ENCODING_MAP(THUMB2_IT,  0xbf00,
718                  BITBLT, 7, 4, BITBLT, 3, 0, MODIMM, -1, -1, UNUSED, -1, -1,
719                  IS_BINARY_OP | IS_IT | USES_CCODES,
720                  "it:!1b", "!0c", 1),
721     ENCODING_MAP(THUMB2_FMSTAT,  0xeef1fa10,
722                  UNUSED, -1, -1, UNUSED, -1, -1, UNUSED, -1, -1, UNUSED, -1, -1,
723                  NO_OPERAND | SETS_CCODES,
724                  "fmstat", "", 2),
725     ENCODING_MAP(THUMB2_VCMPD,        0xeeb40b40,
726                  DFP, 22, 12, DFP, 5, 0, UNUSED, -1, -1, UNUSED, -1, -1,
727                  IS_BINARY_OP | REG_USE01,
728                  "vcmp.f64", "!0S, !1S", 2),
729     ENCODING_MAP(THUMB2_VCMPS,        0xeeb40a40,
730                  SFP, 22, 12, SFP, 5, 0, UNUSED, -1, -1, UNUSED, -1, -1,
731                  IS_BINARY_OP | REG_USE01,
732                  "vcmp.f32", "!0s, !1s", 2),
733     ENCODING_MAP(THUMB2_LDR_PC_REL12,       0xf8df0000,
734                  BITBLT, 15, 12, BITBLT, 11, 0, UNUSED, -1, -1, UNUSED, -1, -1,
735                  IS_TERTIARY_OP | REG_DEF0 | REG_USE_PC,
736                  "ldr", "r!0d,[rpc, #!1d", 2),
737     ENCODING_MAP(THUMB2_B_COND,        0xf0008000,
738                  BROFFSET, -1, -1, BITBLT, 25, 22, UNUSED, -1, -1,
739                  UNUSED, -1, -1,
740                  IS_BINARY_OP | IS_BRANCH | USES_CCODES,
741                  "b!1c", "!0t", 2),
742     ENCODING_MAP(THUMB2_VMOVD_RR,       0xeeb00b40,
743                  DFP, 22, 12, DFP, 5, 0, UNUSED, -1, -1, UNUSED, -1, -1,
744                  IS_BINARY_OP | REG_DEF0_USE1,
745                  "vmov.f64", "!0S, !1S", 2),
746     ENCODING_MAP(THUMB2_VMOVS_RR,       0xeeb00a40,
747                  SFP, 22, 12, SFP, 5, 0, UNUSED, -1, -1, UNUSED, -1, -1,
748                  IS_BINARY_OP | REG_DEF0_USE1,
749                  "vmov.f32", "!0S, !1S", 2),
750     ENCODING_MAP(THUMB2_FMRS,       0xee100a10,
751                  BITBLT, 15, 12, SFP, 8, 16, UNUSED, -1, -1, UNUSED, -1, -1,
752                  IS_BINARY_OP | REG_DEF0_USE1,
753                  "fmrs", "r!0d, !1s", 2),
754     ENCODING_MAP(THUMB2_FMSR,       0xee000a10,
755                  SFP, 8, 16, BITBLT, 15, 12, UNUSED, -1, -1, UNUSED, -1, -1,
756                  IS_BINARY_OP | REG_DEF0_USE1,
757                  "fmsr", "!0s, r!1d", 2),
758     ENCODING_MAP(THUMB2_FMRRD,       0xec500b10,
759                  BITBLT, 15, 12, BITBLT, 19, 16, DFP, 5, 0, UNUSED, -1, -1,
760                  IS_TERTIARY_OP | REG_DEF01_USE2,
761                  "fmrrd", "r!0d, r!1d, !2S", 2),
762     ENCODING_MAP(THUMB2_FMDRR,       0xec400b10,
763                  DFP, 5, 0, BITBLT, 15, 12, BITBLT, 19, 16, UNUSED, -1, -1,
764                  IS_TERTIARY_OP | REG_DEF0_USE12,
765                  "fmdrr", "!0S, r!1d, r!2d", 2),
766 };
767
768 /*
769  * The fake NOP of moving r0 to r0 actually will incur data stalls if r0 is
770  * not ready. Since r5 (rFP) is not updated often, it is less likely to
771  * generate unnecessary stall cycles.
772  */
773 #define PADDING_MOV_R5_R5               0x1C2D
774
775 /* Write the numbers in the literal pool to the codegen stream */
776 static void installDataContent(CompilationUnit *cUnit)
777 {
778     int *dataPtr = (int *) ((char *) cUnit->baseAddr + cUnit->dataOffset);
779     ArmLIR *dataLIR = (ArmLIR *) cUnit->wordList;
780     while (dataLIR) {
781         *dataPtr++ = dataLIR->operands[0];
782         dataLIR = NEXT_LIR(dataLIR);
783     }
784 }
785
786 /* Returns the size of a Jit trace description */
787 static int jitTraceDescriptionSize(const JitTraceDescription *desc)
788 {
789     int runCount;
790     for (runCount = 0; ; runCount++) {
791         if (desc->trace[runCount].frag.runEnd)
792            break;
793     }
794     return sizeof(JitCodeDesc) + ((runCount+1) * sizeof(JitTraceRun));
795 }
796
797 /* Return TRUE if error happens */
798 static bool assembleInstructions(CompilationUnit *cUnit, intptr_t startAddr)
799 {
800     short *bufferAddr = (short *) cUnit->codeBuffer;
801     ArmLIR *lir;
802
803     for (lir = (ArmLIR *) cUnit->firstLIRInsn; lir; lir = NEXT_LIR(lir)) {
804         if (lir->opCode < 0) {
805             if ((lir->opCode == ARM_PSEUDO_ALIGN4) &&
806                 /* 1 means padding is needed */
807                 (lir->operands[0] == 1)) {
808                 *bufferAddr++ = PADDING_MOV_R5_R5;
809             }
810             continue;
811         }
812
813         if (lir->isNop) {
814             continue;
815         }
816
817         if (lir->opCode == THUMB_LDR_PC_REL ||
818             lir->opCode == THUMB2_LDR_PC_REL12 ||
819             lir->opCode == THUMB_ADD_PC_REL) {
820             ArmLIR *lirTarget = (ArmLIR *) lir->generic.target;
821             intptr_t pc = (lir->generic.offset + 4) & ~3;
822             /*
823              * Allow an offset (stored in operands[2] to be added to the
824              * PC-relative target. Useful to get to a fixed field inside a
825              * chaining cell.
826              */
827             intptr_t target = lirTarget->generic.offset + lir->operands[2];
828             int delta = target - pc;
829             if (delta & 0x3) {
830                 LOGE("PC-rel distance is not multiples of 4: %d\n", delta);
831                 dvmAbort();
832             }
833             if ((lir->opCode == THUMB2_LDR_PC_REL12) && (delta > 4091)) {
834                 return true;
835             } else if (delta > 1020) {
836                 return true;
837             }
838             lir->operands[1] = (lir->opCode == THUMB2_LDR_PC_REL12) ?
839                                 delta : delta >> 2;
840         } else if (lir->opCode == THUMB2_CBNZ || lir->opCode == THUMB2_CBZ) {
841             ArmLIR *targetLIR = (ArmLIR *) lir->generic.target;
842             intptr_t pc = lir->generic.offset + 4;
843             intptr_t target = targetLIR->generic.offset;
844             int delta = target - pc;
845             if (delta > 126 || delta < 0) {
846                 /*
847                  * TODO: allow multiple kinds of assembler failure to allow
848                  * change of code patterns when things don't fit.
849                  */
850                 return true;
851             } else {
852                 lir->operands[1] = delta >> 1;
853             }
854         } else if (lir->opCode == THUMB_B_COND ||
855                    lir->opCode == THUMB2_B_COND) {
856             ArmLIR *targetLIR = (ArmLIR *) lir->generic.target;
857             intptr_t pc = lir->generic.offset + 4;
858             intptr_t target = targetLIR->generic.offset;
859             int delta = target - pc;
860             if ((lir->opCode == THUMB_B_COND) && (delta > 254 || delta < -256)) {
861                 return true;
862             }
863             lir->operands[0] = delta >> 1;
864         } else if (lir->opCode == THUMB_B_UNCOND) {
865             ArmLIR *targetLIR = (ArmLIR *) lir->generic.target;
866             intptr_t pc = lir->generic.offset + 4;
867             intptr_t target = targetLIR->generic.offset;
868             int delta = target - pc;
869             if (delta > 2046 || delta < -2048) {
870                 LOGE("Unconditional branch distance out of range: %d\n", delta);
871                 dvmAbort();
872             }
873             lir->operands[0] = delta >> 1;
874         } else if (lir->opCode == THUMB_BLX_1) {
875             assert(NEXT_LIR(lir)->opCode == THUMB_BLX_2);
876             /* curPC is Thumb */
877             intptr_t curPC = (startAddr + lir->generic.offset + 4) & ~3;
878             intptr_t target = lir->operands[1];
879
880             /* Match bit[1] in target with base */
881             if (curPC & 0x2) {
882                 target |= 0x2;
883             }
884             int delta = target - curPC;
885             assert((delta >= -(1<<22)) && (delta <= ((1<<22)-2)));
886
887             lir->operands[0] = (delta >> 12) & 0x7ff;
888             NEXT_LIR(lir)->operands[0] = (delta>> 1) & 0x7ff;
889         }
890
891         ArmEncodingMap *encoder = &EncodingMap[lir->opCode];
892         u4 bits = encoder->skeleton;
893         int i;
894         for (i = 0; i < 4; i++) {
895             u4 operand;
896             u4 value;
897             operand = lir->operands[i];
898             switch(encoder->fieldLoc[i].kind) {
899                 case UNUSED:
900                     break;
901                 case BROFFSET:
902                     value = ((operand  & 0x80000) >> 19) << 26;
903                     value |= ((operand & 0x40000) >> 18) << 11;
904                     value |= ((operand & 0x20000) >> 17) << 13;
905                     value |= ((operand & 0x1f800) >> 11) << 16;
906                     value |= (operand  & 0x007ff);
907                     break;
908                 case SHIFT5:
909                     value = ((operand & 0x1c) >> 2) << 12;
910                     value |= (operand & 0x03) << 6;
911                     bits |= value;
912                     break;
913                 case SHIFT:
914                     value = ((operand & 0x70) >> 4) << 12;
915                     value |= (operand & 0x0f) << 4;
916                     bits |= value;
917                     break;
918                 case BWIDTH:
919                     value = operand - 1;
920                     bits |= value;
921                     break;
922                 case LSB:
923                     value = ((operand & 0x1c) >> 2) << 12;
924                     value |= (operand & 0x03) << 6;
925                     bits |= value;
926                     break;
927                 case IMM6:
928                     value = ((operand & 0x20) >> 5) << 9;
929                     value |= (operand & 0x1f) << 3;
930                     bits |= value;
931                     break;
932                 case BITBLT:
933                     value = (operand << encoder->fieldLoc[i].start) &
934                             ((1 << (encoder->fieldLoc[i].end + 1)) - 1);
935                     bits |= value;
936                     break;
937                 case DFP: {
938                     int regName = (operand & FP_REG_MASK) >> 1;
939                     /* Snag the 1-bit slice and position it */
940                     value = ((regName & 0x10) >> 4) <<
941                             encoder->fieldLoc[i].end;
942                     /* Extract and position the 4-bit slice */
943                     value |= (regName & 0x0f) <<
944                             encoder->fieldLoc[i].start;
945                     bits |= value;
946                     break;
947                 }
948                 case SFP:
949                     /* Snag the 1-bit slice and position it */
950                     value = (operand & 0x1) <<
951                             encoder->fieldLoc[i].end;
952                     /* Extract and position the 4-bit slice */
953                     value |= ((operand & 0x1e) >> 1) <<
954                             encoder->fieldLoc[i].start;
955                     bits |= value;
956                     break;
957                 case IMM12:
958                 case MODIMM:
959                     value = ((operand & 0x800) >> 11) << 26;
960                     value |= ((operand & 0x700) >> 8) << 12;
961                     value |= operand & 0x0ff;
962                     bits |= value;
963                     break;
964                 case IMM16:
965                     value = ((operand & 0x0800) >> 11) << 26;
966                     value |= ((operand & 0xf000) >> 12) << 16;
967                     value |= ((operand & 0x0700) >> 8) << 12;
968                     value |= operand & 0x0ff;
969                     bits |= value;
970                     break;
971                 default:
972                     assert(0);
973             }
974         }
975         if (encoder->size == 2) {
976             *bufferAddr++ = (bits >> 16) & 0xffff;
977         }
978         *bufferAddr++ = bits & 0xffff;
979     }
980     return false;
981 }
982
983 /*
984  * Translation layout in the code cache.  Note that the codeAddress pointer
985  * in JitTable will point directly to the code body (field codeAddress).  The
986  * chain cell offset codeAddress - 2, and (if present) executionCount is at
987  * codeAddress - 6.
988  *
989  *      +----------------------------+
990  *      | Execution count            |  -> [Optional] 4 bytes
991  *      +----------------------------+
992  *   +--| Offset to chain cell counts|  -> 2 bytes
993  *   |  +----------------------------+
994  *   |  | Code body                  |  -> Start address for translation
995  *   |  |                            |     variable in 2-byte chunks
996  *   |  .                            .     (JitTable's codeAddress points here)
997  *   |  .                            .
998  *   |  |                            |
999  *   |  +----------------------------+
1000  *   |  | Chaining Cells             |  -> 8 bytes each, must be 4 byte aligned
1001  *   |  .                            .
1002  *   |  .                            .
1003  *   |  |                            |
1004  *   |  +----------------------------+
1005  *   +->| Chaining cell counts       |  -> 4 bytes, chain cell counts by type
1006  *      +----------------------------+
1007  *      | Trace description          |  -> variable sized
1008  *      .                            .
1009  *      |                            |
1010  *      +----------------------------+
1011  *      | Literal pool               |  -> 4-byte aligned, variable size
1012  *      .                            .
1013  *      .                            .
1014  *      |                            |
1015  *      +----------------------------+
1016  *
1017  * Go over each instruction in the list and calculate the offset from the top
1018  * before sending them off to the assembler. If out-of-range branch distance is
1019  * seen rearrange the instructions a bit to correct it.
1020  */
1021 void dvmCompilerAssembleLIR(CompilationUnit *cUnit, JitTranslationInfo *info)
1022 {
1023     LIR *lir;
1024     ArmLIR *armLIR;
1025     int offset = 0;
1026     int i;
1027     ChainCellCounts chainCellCounts;
1028     int descSize = jitTraceDescriptionSize(cUnit->traceDesc);
1029
1030     info->instructionSet = cUnit->instructionSet;
1031
1032     /* Beginning offset needs to allow space for chain cell offset */
1033     for (armLIR = (ArmLIR *) cUnit->firstLIRInsn;
1034          armLIR;
1035          armLIR = NEXT_LIR(armLIR)) {
1036         armLIR->generic.offset = offset;
1037         if (armLIR->opCode >= 0 && !armLIR->isNop) {
1038             armLIR->size = EncodingMap[armLIR->opCode].size * 2;
1039             offset += armLIR->size;
1040         } else if (armLIR->opCode == ARM_PSEUDO_ALIGN4) {
1041             if (offset & 0x2) {
1042                 offset += 2;
1043                 armLIR->operands[0] = 1;
1044             } else {
1045                 armLIR->operands[0] = 0;
1046             }
1047         }
1048         /* Pseudo opcodes don't consume space */
1049     }
1050
1051     /* Const values have to be word aligned */
1052     offset = (offset + 3) & ~3;
1053
1054     /* Add space for chain cell counts & trace description */
1055     u4 chainCellOffset = offset;
1056     ArmLIR *chainCellOffsetLIR = (ArmLIR *) cUnit->chainCellOffsetLIR;
1057     assert(chainCellOffsetLIR);
1058     assert(chainCellOffset < 0x10000);
1059     assert(chainCellOffsetLIR->opCode == ARM_16BIT_DATA &&
1060            chainCellOffsetLIR->operands[0] == CHAIN_CELL_OFFSET_TAG);
1061
1062     /*
1063      * Replace the CHAIN_CELL_OFFSET_TAG with the real value. If trace
1064      * profiling is enabled, subtract 4 (occupied by the counter word) from
1065      * the absolute offset as the value stored in chainCellOffsetLIR is the
1066      * delta from &chainCellOffsetLIR to &ChainCellCounts.
1067      */
1068     chainCellOffsetLIR->operands[0] =
1069         gDvmJit.profile ? (chainCellOffset - 4) : chainCellOffset;
1070
1071     offset += sizeof(chainCellCounts) + descSize;
1072
1073     assert((offset & 0x3) == 0);  /* Should still be word aligned */
1074
1075     /* Set up offsets for literals */
1076     cUnit->dataOffset = offset;
1077
1078     for (lir = cUnit->wordList; lir; lir = lir->next) {
1079         lir->offset = offset;
1080         offset += 4;
1081     }
1082
1083     cUnit->totalSize = offset;
1084
1085     if (gDvmJit.codeCacheByteUsed + cUnit->totalSize > CODE_CACHE_SIZE) {
1086         gDvmJit.codeCacheFull = true;
1087         cUnit->baseAddr = NULL;
1088         return;
1089     }
1090
1091     /* Allocate enough space for the code block */
1092     cUnit->codeBuffer = dvmCompilerNew(chainCellOffset, true);
1093     if (cUnit->codeBuffer == NULL) {
1094         LOGE("Code buffer allocation failure\n");
1095         cUnit->baseAddr = NULL;
1096         return;
1097     }
1098
1099     bool assemblerFailure = assembleInstructions(
1100         cUnit, (intptr_t) gDvmJit.codeCache + gDvmJit.codeCacheByteUsed);
1101
1102     /*
1103      * Currently the only reason that can cause the assembler to fail is due to
1104      * trace length - cut it in half and retry.
1105      */
1106     if (assemblerFailure) {
1107         cUnit->halveInstCount = true;
1108         return;
1109     }
1110
1111     /* Don't go all the way if the goal is just to get the verbose output */
1112     if (info->discardResult) return;
1113
1114     cUnit->baseAddr = (char *) gDvmJit.codeCache + gDvmJit.codeCacheByteUsed;
1115     gDvmJit.codeCacheByteUsed += offset;
1116
1117     /* Install the code block */
1118     memcpy((char*)cUnit->baseAddr, cUnit->codeBuffer, chainCellOffset);
1119     gDvmJit.numCompilations++;
1120
1121     /* Install the chaining cell counts */
1122     for (i=0; i< CHAINING_CELL_LAST; i++) {
1123         chainCellCounts.u.count[i] = cUnit->numChainingCells[i];
1124     }
1125     memcpy((char*)cUnit->baseAddr + chainCellOffset, &chainCellCounts,
1126            sizeof(chainCellCounts));
1127
1128     /* Install the trace description */
1129     memcpy((char*)cUnit->baseAddr + chainCellOffset + sizeof(chainCellCounts),
1130            cUnit->traceDesc, descSize);
1131
1132     /* Write the literals directly into the code cache */
1133     installDataContent(cUnit);
1134
1135     /* Flush dcache and invalidate the icache to maintain coherence */
1136     cacheflush((long)cUnit->baseAddr,
1137                (long)((char *) cUnit->baseAddr + offset), 0);
1138
1139     /* Record code entry point and instruction set */
1140     info->codeAddress = (char*)cUnit->baseAddr + cUnit->headerSize;
1141     /* If applicable, mark low bit to denote thumb */
1142     if (info->instructionSet != DALVIK_JIT_ARM)
1143         info->codeAddress = (char*)info->codeAddress + 1;
1144 }
1145
1146 static u4 assembleBXPair(int branchOffset)
1147 {
1148     u4 thumb1, thumb2;
1149
1150     if ((branchOffset < -2048) | (branchOffset > 2046)) {
1151         thumb1 =  (0xf000 | ((branchOffset>>12) & 0x7ff));
1152         thumb2 =  (0xf800 | ((branchOffset>> 1) & 0x7ff));
1153     } else {
1154         thumb1 =  (0xe000 | ((branchOffset>> 1) & 0x7ff));
1155         thumb2 =  0x4300;  /* nop -> or r0, r0 */
1156     }
1157
1158     return thumb2<<16 | thumb1;
1159 }
1160
1161 /*
1162  * Perform translation chain operation.
1163  * For ARM, we'll use a pair of thumb instructions to generate
1164  * an unconditional chaining branch of up to 4MB in distance.
1165  * Use a BL, though we don't really need the link.  The format is
1166  *     111HHooooooooooo
1167  * Where HH is 10 for the 1st inst, and 11 for the second and
1168  * the "o" field is each instruction's 11-bit contribution to the
1169  * 22-bit branch offset.
1170  * If the target is nearby, use a single-instruction bl.
1171  * If one or more threads is suspended, don't chain.
1172  */
1173 void* dvmJitChain(void* tgtAddr, u4* branchAddr)
1174 {
1175     int baseAddr = (u4) branchAddr + 4;
1176     int branchOffset = (int) tgtAddr - baseAddr;
1177     u4 newInst;
1178
1179     if (gDvm.sumThreadSuspendCount == 0) {
1180         assert((branchOffset >= -(1<<22)) && (branchOffset <= ((1<<22)-2)));
1181
1182         gDvmJit.translationChains++;
1183
1184         COMPILER_TRACE_CHAINING(
1185             LOGD("Jit Runtime: chaining 0x%x to 0x%x\n",
1186                  (int) branchAddr, (int) tgtAddr & -2));
1187
1188         newInst = assembleBXPair(branchOffset);
1189
1190         *branchAddr = newInst;
1191         cacheflush((long)branchAddr, (long)branchAddr + 4, 0);
1192     }
1193
1194     return tgtAddr;
1195 }
1196
1197 /*
1198  * This method is called from the invoke templates for virtual and interface
1199  * methods to speculatively setup a chain to the callee. The templates are
1200  * written in assembly and have setup method, cell, and clazz at r0, r2, and
1201  * r3 respectively, so there is a unused argument in the list. Upon return one
1202  * of the following three results may happen:
1203  *   1) Chain is not setup because the callee is native. Reset the rechain
1204  *      count to a big number so that it will take a long time before the next
1205  *      rechain attempt to happen.
1206  *   2) Chain is not setup because the callee has not been created yet. Reset
1207  *      the rechain count to a small number and retry in the near future.
1208  *   3) Ask all other threads to stop before patching this chaining cell.
1209  *      This is required because another thread may have passed the class check
1210  *      but hasn't reached the chaining cell yet to follow the chain. If we
1211  *      patch the content before halting the other thread, there could be a
1212  *      small window for race conditions to happen that it may follow the new
1213  *      but wrong chain to invoke a different method.
1214  */
1215 const Method *dvmJitToPatchPredictedChain(const Method *method,
1216                                           void *unused,
1217                                           PredictedChainingCell *cell,
1218                                           const ClassObject *clazz)
1219 {
1220 #if defined(WITH_SELF_VERIFICATION)
1221     /* Disable chaining and prevent this from triggering again for a while */
1222     cell->counter = PREDICTED_CHAIN_COUNTER_AVOID;
1223     cacheflush((long) cell, (long) (cell+1), 0);
1224     goto done;
1225 #else
1226     /* Don't come back here for a long time if the method is native */
1227     if (dvmIsNativeMethod(method)) {
1228         cell->counter = PREDICTED_CHAIN_COUNTER_AVOID;
1229         cacheflush((long) cell, (long) (cell+1), 0);
1230         COMPILER_TRACE_CHAINING(
1231             LOGD("Jit Runtime: predicted chain %p to native method %s ignored",
1232                  cell, method->name));
1233         goto done;
1234     }
1235     int tgtAddr = (int) dvmJitGetCodeAddr(method->insns);
1236
1237     /*
1238      * Compilation not made yet for the callee. Reset the counter to a small
1239      * value and come back to check soon.
1240      */
1241     if (tgtAddr == 0) {
1242         /*
1243          * Wait for a few invocations (currently set to be 16) before trying
1244          * to setup the chain again.
1245          */
1246         cell->counter = PREDICTED_CHAIN_COUNTER_DELAY;
1247         cacheflush((long) cell, (long) (cell+1), 0);
1248         COMPILER_TRACE_CHAINING(
1249             LOGD("Jit Runtime: predicted chain %p to method %s%s delayed",
1250                  cell, method->clazz->descriptor, method->name));
1251         goto done;
1252     }
1253
1254     /*
1255      * Bump up the counter first just in case other mutator threads are in
1256      * nearby territory to also attempt to rechain this cell. This is not
1257      * done in a thread-safe way and doesn't need to be since the consequence
1258      * of the race condition [rare] is two back-to-back suspend-all attempts,
1259      * which will be handled correctly.
1260      */
1261     cell->counter = PREDICTED_CHAIN_COUNTER_AVOID;
1262
1263     /* Stop the world */
1264     dvmSuspendAllThreads(SUSPEND_FOR_IC_PATCH);
1265
1266     int baseAddr = (int) cell + 4;   // PC is cur_addr + 4
1267     int branchOffset = tgtAddr - baseAddr;
1268
1269     COMPILER_TRACE_CHAINING(
1270         LOGD("Jit Runtime: predicted chain %p from %s to %s (%s) patched",
1271              cell, cell->clazz ? cell->clazz->descriptor : "NULL",
1272              clazz->descriptor,
1273              method->name));
1274
1275     cell->branch = assembleBXPair(branchOffset);
1276     cell->clazz = clazz;
1277     cell->method = method;
1278     /*
1279      * Reset the counter again in case other mutator threads got invoked
1280      * between the previous rest and dvmSuspendAllThreads call.
1281      */
1282     cell->counter = PREDICTED_CHAIN_COUNTER_RECHAIN;
1283
1284     cacheflush((long) cell, (long) (cell+1), 0);
1285
1286     /* All done - resume all other threads */
1287     dvmResumeAllThreads(SUSPEND_FOR_IC_PATCH);
1288 #endif
1289
1290 done:
1291     return method;
1292 }
1293
1294 /*
1295  * Unchain a trace given the starting address of the translation
1296  * in the code cache.  Refer to the diagram in dvmCompilerAssembleLIR.
1297  * Returns the address following the last cell unchained.  Note that
1298  * the incoming codeAddr is a thumb code address, and therefore has
1299  * the low bit set.
1300  */
1301 u4* dvmJitUnchain(void* codeAddr)
1302 {
1303     u2* pChainCellOffset = (u2*)((char*)codeAddr - 3);
1304     u2 chainCellOffset = *pChainCellOffset;
1305     ChainCellCounts *pChainCellCounts =
1306           (ChainCellCounts*)((char*)codeAddr + chainCellOffset - 3);
1307     int cellSize;
1308     u4* pChainCells;
1309     u4* pStart;
1310     u4 thumb1;
1311     u4 thumb2;
1312     u4 newInst;
1313     int i,j;
1314     PredictedChainingCell *predChainCell;
1315
1316     /* Get total count of chain cells */
1317     for (i = 0, cellSize = 0; i < CHAINING_CELL_LAST; i++) {
1318         if (i != CHAINING_CELL_INVOKE_PREDICTED) {
1319             cellSize += pChainCellCounts->u.count[i] * 2;
1320         } else {
1321             cellSize += pChainCellCounts->u.count[i] * 4;
1322         }
1323     }
1324
1325     /* Locate the beginning of the chain cell region */
1326     pStart = pChainCells = ((u4 *) pChainCellCounts) - cellSize;
1327
1328     /* The cells are sorted in order - walk through them and reset */
1329     for (i = 0; i < CHAINING_CELL_LAST; i++) {
1330         int elemSize = 2; /* Most chaining cell has two words */
1331         if (i == CHAINING_CELL_INVOKE_PREDICTED) {
1332             elemSize = 4;
1333         }
1334
1335         for (j = 0; j < pChainCellCounts->u.count[i]; j++) {
1336             int targetOffset;
1337             switch(i) {
1338                 case CHAINING_CELL_NORMAL:
1339                     targetOffset = offsetof(InterpState,
1340                           jitToInterpEntries.dvmJitToInterpNormal);
1341                     break;
1342                 case CHAINING_CELL_HOT:
1343                 case CHAINING_CELL_INVOKE_SINGLETON:
1344                     targetOffset = offsetof(InterpState,
1345                           jitToInterpEntries.dvmJitToTraceSelect);
1346                     break;
1347                 case CHAINING_CELL_INVOKE_PREDICTED:
1348                     targetOffset = 0;
1349                     predChainCell = (PredictedChainingCell *) pChainCells;
1350                     /* Reset the cell to the init state */
1351                     predChainCell->branch = PREDICTED_CHAIN_BX_PAIR_INIT;
1352                     predChainCell->clazz = PREDICTED_CHAIN_CLAZZ_INIT;
1353                     predChainCell->method = PREDICTED_CHAIN_METHOD_INIT;
1354                     predChainCell->counter = PREDICTED_CHAIN_COUNTER_INIT;
1355                     break;
1356 #if defined(WITH_SELF_VERIFICATION)
1357                 case CHAINING_CELL_BACKWARD_BRANCH:
1358                     targetOffset = offsetof(InterpState,
1359                           jitToInterpEntries.dvmJitToBackwardBranch);
1360                     break;
1361 #elif defined(WITH_JIT_TUNING)
1362                 case CHAINING_CELL_BACKWARD_BRANCH:
1363                     targetOffset = offsetof(InterpState,
1364                           jitToInterpEntries.dvmJitToInterpNormal);
1365                     break;
1366 #endif
1367                 default:
1368                     dvmAbort();
1369             }
1370             COMPILER_TRACE_CHAINING(
1371                 LOGD("Jit Runtime: unchaining 0x%x", (int)pChainCells));
1372             /*
1373              * Thumb code sequence for a chaining cell is:
1374              *     ldr  r0, rGLUE, #<word offset>
1375              *     blx  r0
1376              */
1377             if (i != CHAINING_CELL_INVOKE_PREDICTED) {
1378                 targetOffset = targetOffset >> 2;  /* convert to word offset */
1379                 thumb1 = 0x6800 | (targetOffset << 6) |
1380                          (rGLUE << 3) | (r0 << 0);
1381                 thumb2 = 0x4780 | (r0 << 3);
1382                 newInst = thumb2<<16 | thumb1;
1383                 *pChainCells = newInst;
1384             }
1385             pChainCells += elemSize;  /* Advance by a fixed number of words */
1386         }
1387     }
1388     return pChainCells;
1389 }
1390
1391 /* Unchain all translation in the cache. */
1392 void dvmJitUnchainAll()
1393 {
1394     u4* lowAddress = NULL;
1395     u4* highAddress = NULL;
1396     unsigned int i;
1397     if (gDvmJit.pJitEntryTable != NULL) {
1398         COMPILER_TRACE_CHAINING(LOGD("Jit Runtime: unchaining all"));
1399         dvmLockMutex(&gDvmJit.tableLock);
1400         for (i = 0; i < gDvmJit.jitTableSize; i++) {
1401             if (gDvmJit.pJitEntryTable[i].dPC &&
1402                    gDvmJit.pJitEntryTable[i].codeAddress) {
1403                 u4* lastAddress;
1404                 lastAddress =
1405                       dvmJitUnchain(gDvmJit.pJitEntryTable[i].codeAddress);
1406                 if (lowAddress == NULL ||
1407                       (u4*)gDvmJit.pJitEntryTable[i].codeAddress < lowAddress)
1408                     lowAddress = lastAddress;
1409                 if (lastAddress > highAddress)
1410                     highAddress = lastAddress;
1411             }
1412         }
1413         cacheflush((long)lowAddress, (long)highAddress, 0);
1414         dvmUnlockMutex(&gDvmJit.tableLock);
1415     }
1416 }
1417
1418 typedef struct jitProfileAddrToLine {
1419     u4 lineNum;
1420     u4 bytecodeOffset;
1421 } jitProfileAddrToLine;
1422
1423
1424 /* Callback function to track the bytecode offset/line number relationiship */
1425 static int addrToLineCb (void *cnxt, u4 bytecodeOffset, u4 lineNum)
1426 {
1427     jitProfileAddrToLine *addrToLine = (jitProfileAddrToLine *) cnxt;
1428
1429     /* Best match so far for this offset */
1430     if (addrToLine->bytecodeOffset >= bytecodeOffset) {
1431         addrToLine->lineNum = lineNum;
1432     }
1433     return 0;
1434 }
1435
1436 char *getTraceBase(const JitEntry *p)
1437 {
1438     return (char*)p->codeAddress -
1439         (6 + (p->u.info.instructionSet == DALVIK_JIT_ARM ? 0 : 1));
1440 }
1441
1442 /* Dumps profile info for a single trace */
1443 static int dumpTraceProfile(JitEntry *p)
1444 {
1445     ChainCellCounts* pCellCounts;
1446     char* traceBase;
1447     u4* pExecutionCount;
1448     u2* pCellOffset;
1449     JitTraceDescription *desc;
1450     const Method* method;
1451
1452     traceBase = getTraceBase(p);
1453
1454     if (p->codeAddress == NULL) {
1455         LOGD("TRACEPROFILE 0x%08x 0 NULL 0 0", (int)traceBase);
1456         return 0;
1457     }
1458
1459     pExecutionCount = (u4*) (traceBase);
1460     pCellOffset = (u2*) (traceBase + 4);
1461     pCellCounts = (ChainCellCounts*) ((char *)pCellOffset + *pCellOffset);
1462     desc = (JitTraceDescription*) ((char*)pCellCounts + sizeof(*pCellCounts));
1463     method = desc->method;
1464     char *methodDesc = dexProtoCopyMethodDescriptor(&method->prototype);
1465     jitProfileAddrToLine addrToLine = {0, desc->trace[0].frag.startOffset};
1466
1467     /*
1468      * We may end up decoding the debug information for the same method
1469      * multiple times, but the tradeoff is we don't need to allocate extra
1470      * space to store the addr/line mapping. Since this is a debugging feature
1471      * and done infrequently so the slower but simpler mechanism should work
1472      * just fine.
1473      */
1474     dexDecodeDebugInfo(method->clazz->pDvmDex->pDexFile,
1475                        dvmGetMethodCode(method),
1476                        method->clazz->descriptor,
1477                        method->prototype.protoIdx,
1478                        method->accessFlags,
1479                        addrToLineCb, NULL, &addrToLine);
1480
1481     LOGD("TRACEPROFILE 0x%08x % 10d [%#x(+%d), %d] %s%s;%s",
1482          (int)traceBase,
1483          *pExecutionCount,
1484          desc->trace[0].frag.startOffset,
1485          desc->trace[0].frag.numInsts,
1486          addrToLine.lineNum,
1487          method->clazz->descriptor, method->name, methodDesc);
1488     free(methodDesc);
1489
1490     return *pExecutionCount;
1491 }
1492
1493 /* Create a copy of the trace descriptor of an existing compilation */
1494 JitTraceDescription *dvmCopyTraceDescriptor(const u2 *pc)
1495 {
1496     JitEntry *jitEntry = dvmFindJitEntry(pc);
1497     if (jitEntry == NULL) return NULL;
1498
1499     /* Find out the startint point */
1500     char *traceBase = getTraceBase(jitEntry);
1501
1502     /* Then find out the starting point of the chaining cell */
1503     u2 *pCellOffset = (u2*) (traceBase + 4);
1504     ChainCellCounts *pCellCounts =
1505         (ChainCellCounts*) ((char *)pCellOffset + *pCellOffset);
1506
1507     /* From there we can find out the starting point of the trace descriptor */
1508     JitTraceDescription *desc =
1509         (JitTraceDescription*) ((char*)pCellCounts + sizeof(*pCellCounts));
1510
1511     /* Now make a copy and return */
1512     int descSize = jitTraceDescriptionSize(desc);
1513     JitTraceDescription *newCopy = (JitTraceDescription *) malloc(descSize);
1514     memcpy(newCopy, desc, descSize);
1515     return newCopy;
1516 }
1517
1518 /* Handy function to retrieve the profile count */
1519 static inline int getProfileCount(const JitEntry *entry)
1520 {
1521     if (entry->dPC == 0 || entry->codeAddress == 0)
1522         return 0;
1523     u4 *pExecutionCount = (u4 *) getTraceBase(entry);
1524
1525     return *pExecutionCount;
1526 }
1527
1528
1529 /* qsort callback function */
1530 static int sortTraceProfileCount(const void *entry1, const void *entry2)
1531 {
1532     const JitEntry *jitEntry1 = entry1;
1533     const JitEntry *jitEntry2 = entry2;
1534
1535     int count1 = getProfileCount(jitEntry1);
1536     int count2 = getProfileCount(jitEntry2);
1537     return (count1 == count2) ? 0 : ((count1 > count2) ? -1 : 1);
1538 }
1539
1540 /* Sort the trace profile counts and dump them */
1541 void dvmCompilerSortAndPrintTraceProfiles()
1542 {
1543     JitEntry *sortedEntries;
1544     int numTraces = 0;
1545     unsigned long counts = 0;
1546     unsigned int i;
1547
1548     /* Make sure that the table is not changing */
1549     dvmLockMutex(&gDvmJit.tableLock);
1550
1551     /* Sort the entries by descending order */
1552     sortedEntries = malloc(sizeof(JitEntry) * gDvmJit.jitTableSize);
1553     if (sortedEntries == NULL)
1554         goto done;
1555     memcpy(sortedEntries, gDvmJit.pJitEntryTable,
1556            sizeof(JitEntry) * gDvmJit.jitTableSize);
1557     qsort(sortedEntries, gDvmJit.jitTableSize, sizeof(JitEntry),
1558           sortTraceProfileCount);
1559
1560     /* Dump the sorted entries */
1561     for (i=0; i < gDvmJit.jitTableSize; i++) {
1562         if (sortedEntries[i].dPC != 0) {
1563             counts += dumpTraceProfile(&sortedEntries[i]);
1564             numTraces++;
1565         }
1566     }
1567     if (numTraces == 0)
1568         numTraces = 1;
1569     LOGD("JIT: Average execution count -> %d",(int)(counts / numTraces));
1570
1571     free(sortedEntries);
1572 done:
1573     dvmUnlockMutex(&gDvmJit.tableLock);
1574     return;
1575 }