blob: 0a5348079d8ca7ec3e9da3c50441b4138a68749a [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
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 */
Carl Shapiroa2e18e12011-06-21 18:57:55 -070016
17#ifndef ART_SRC_CONSTANTS_ARM_H_
18#define ART_SRC_CONSTANTS_ARM_H_
19
20#include <stdint.h>
Ian Rogersb033c752011-07-20 12:22:35 -070021#include <iosfwd>
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070022#include "casts.h"
23#include "globals.h"
24#include "logging.h"
Carl Shapiroa2e18e12011-06-21 18:57:55 -070025
26namespace art {
Ian Rogers2c8f6532011-09-02 17:16:34 -070027namespace arm {
Carl Shapiroa2e18e12011-06-21 18:57:55 -070028
29// Defines constants and accessor classes to assemble, disassemble and
30// simulate ARM instructions.
31//
32// Section references in the code refer to the "ARM Architecture Reference
33// Manual" from July 2005 (available at http://www.arm.com/miscPDFs/14128.pdf)
34//
35// Constants for specific fields are defined in their respective named enums.
36// General constants are in an anonymous enum in class Instr.
37
38
39// We support both VFPv3-D16 and VFPv3-D32 profiles, but currently only one at
40// a time, so that compile time optimizations can be applied.
41// Warning: VFPv3-D32 is untested.
42#define VFPv3_D16
43#if defined(VFPv3_D16) == defined(VFPv3_D32)
44#error "Exactly one of VFPv3_D16 or VFPv3_D32 can be defined at a time."
45#endif
46
47
48// Values for registers.
49enum Register {
50 R0 = 0,
51 R1 = 1,
52 R2 = 2,
53 R3 = 3,
54 R4 = 4,
55 R5 = 5,
56 R6 = 6,
57 R7 = 7,
58 R8 = 8,
59 R9 = 9,
60 R10 = 10,
61 R11 = 11,
62 R12 = 12,
63 R13 = 13,
64 R14 = 14,
65 R15 = 15,
Ian Rogersb033c752011-07-20 12:22:35 -070066 TR = 9, // thread register
Carl Shapiroa2e18e12011-06-21 18:57:55 -070067 FP = 11,
68 IP = 12,
69 SP = 13,
70 LR = 14,
71 PC = 15,
72 kNumberOfCoreRegisters = 16,
73 kNoRegister = -1,
74};
Elliott Hughes1f359b02011-07-17 14:27:17 -070075std::ostream& operator<<(std::ostream& os, const Register& rhs);
Carl Shapiroa2e18e12011-06-21 18:57:55 -070076
77
78enum ScaleFactor {
79 TIMES_1 = 0,
80 TIMES_2 = 1,
81 TIMES_4 = 2,
82 TIMES_8 = 3
83};
84
85
86// Values for single-precision floating point registers.
87enum SRegister {
88 S0 = 0,
89 S1 = 1,
90 S2 = 2,
91 S3 = 3,
92 S4 = 4,
93 S5 = 5,
94 S6 = 6,
95 S7 = 7,
96 S8 = 8,
97 S9 = 9,
98 S10 = 10,
99 S11 = 11,
100 S12 = 12,
101 S13 = 13,
102 S14 = 14,
103 S15 = 15,
104 S16 = 16,
105 S17 = 17,
106 S18 = 18,
107 S19 = 19,
108 S20 = 20,
109 S21 = 21,
110 S22 = 22,
111 S23 = 23,
112 S24 = 24,
113 S25 = 25,
114 S26 = 26,
115 S27 = 27,
116 S28 = 28,
117 S29 = 29,
118 S30 = 30,
119 S31 = 31,
120 kNumberOfSRegisters = 32,
121 kNoSRegister = -1,
122};
Elliott Hughes1f359b02011-07-17 14:27:17 -0700123std::ostream& operator<<(std::ostream& os, const SRegister& rhs);
Carl Shapiroa2e18e12011-06-21 18:57:55 -0700124
125
126// Values for double-precision floating point registers.
127enum DRegister {
128 D0 = 0,
129 D1 = 1,
130 D2 = 2,
131 D3 = 3,
132 D4 = 4,
133 D5 = 5,
134 D6 = 6,
135 D7 = 7,
136 D8 = 8,
137 D9 = 9,
138 D10 = 10,
139 D11 = 11,
140 D12 = 12,
141 D13 = 13,
142 D14 = 14,
143 D15 = 15,
144#ifdef VFPv3_D16
145 kNumberOfDRegisters = 16,
146#else
147 D16 = 16,
148 D17 = 17,
149 D18 = 18,
150 D19 = 19,
151 D20 = 20,
152 D21 = 21,
153 D22 = 22,
154 D23 = 23,
155 D24 = 24,
156 D25 = 25,
157 D26 = 26,
158 D27 = 27,
159 D28 = 28,
160 D29 = 29,
161 D30 = 30,
162 D31 = 31,
163 kNumberOfDRegisters = 32,
164#endif
165 kNumberOfOverlappingDRegisters = 16,
166 kNoDRegister = -1,
167};
Elliott Hughes1f359b02011-07-17 14:27:17 -0700168std::ostream& operator<<(std::ostream& os, const DRegister& rhs);
Carl Shapiroa2e18e12011-06-21 18:57:55 -0700169
170
171// Values for the condition field as defined in section A3.2.
172enum Condition {
173 kNoCondition = -1,
174 EQ = 0, // equal
175 NE = 1, // not equal
176 CS = 2, // carry set/unsigned higher or same
177 CC = 3, // carry clear/unsigned lower
178 MI = 4, // minus/negative
179 PL = 5, // plus/positive or zero
180 VS = 6, // overflow
181 VC = 7, // no overflow
182 HI = 8, // unsigned higher
183 LS = 9, // unsigned lower or same
184 GE = 10, // signed greater than or equal
185 LT = 11, // signed less than
186 GT = 12, // signed greater than
187 LE = 13, // signed less than or equal
188 AL = 14, // always (unconditional)
189 kSpecialCondition = 15, // special condition (refer to section A3.2.1)
190 kMaxCondition = 16,
191};
Elliott Hughes1f359b02011-07-17 14:27:17 -0700192std::ostream& operator<<(std::ostream& os, const Condition& rhs);
Carl Shapiroa2e18e12011-06-21 18:57:55 -0700193
194
195// Opcodes for Data-processing instructions (instructions with a type 0 and 1)
196// as defined in section A3.4
197enum Opcode {
198 kNoOperand = -1,
199 AND = 0, // Logical AND
200 EOR = 1, // Logical Exclusive OR
201 SUB = 2, // Subtract
202 RSB = 3, // Reverse Subtract
203 ADD = 4, // Add
204 ADC = 5, // Add with Carry
205 SBC = 6, // Subtract with Carry
206 RSC = 7, // Reverse Subtract with Carry
207 TST = 8, // Test
208 TEQ = 9, // Test Equivalence
209 CMP = 10, // Compare
210 CMN = 11, // Compare Negated
211 ORR = 12, // Logical (inclusive) OR
212 MOV = 13, // Move
213 BIC = 14, // Bit Clear
214 MVN = 15, // Move Not
215 kMaxOperand = 16
216};
217
218
219// Shifter types for Data-processing operands as defined in section A5.1.2.
220enum Shift {
221 kNoShift = -1,
222 LSL = 0, // Logical shift left
223 LSR = 1, // Logical shift right
224 ASR = 2, // Arithmetic shift right
225 ROR = 3, // Rotate right
226 kMaxShift = 4
227};
228
229
Carl Shapiroa2e18e12011-06-21 18:57:55 -0700230// Constants used for the decoding or encoding of the individual fields of
231// instructions. Based on the "Figure 3-1 ARM instruction set summary".
232enum InstructionFields {
233 kConditionShift = 28,
234 kConditionBits = 4,
235 kTypeShift = 25,
236 kTypeBits = 3,
237 kLinkShift = 24,
238 kLinkBits = 1,
239 kUShift = 23,
240 kUBits = 1,
241 kOpcodeShift = 21,
242 kOpcodeBits = 4,
243 kSShift = 20,
244 kSBits = 1,
245 kRnShift = 16,
246 kRnBits = 4,
247 kRdShift = 12,
248 kRdBits = 4,
249 kRsShift = 8,
250 kRsBits = 4,
251 kRmShift = 0,
252 kRmBits = 4,
253
254 // Immediate instruction fields encoding.
255 kRotateShift = 8,
256 kRotateBits = 4,
257 kImmed8Shift = 0,
258 kImmed8Bits = 8,
259
260 // Shift instruction register fields encodings.
261 kShiftImmShift = 7,
262 kShiftRegisterShift = 8,
263 kShiftImmBits = 5,
264 kShiftShift = 5,
265 kShiftBits = 2,
266
267 // Load/store instruction offset field encoding.
268 kOffset12Shift = 0,
269 kOffset12Bits = 12,
270 kOffset12Mask = 0x00000fff,
271
272 // Mul instruction register fields encodings.
273 kMulRdShift = 16,
274 kMulRdBits = 4,
275 kMulRnShift = 12,
276 kMulRnBits = 4,
277
278 kBranchOffsetMask = 0x00ffffff
279};
280
281
282// Size (in bytes) of registers.
283const int kRegisterSize = 4;
284
285// List of registers used in load/store multiple.
286typedef uint16_t RegList;
287
Carl Shapiroa2e18e12011-06-21 18:57:55 -0700288// The class Instr enables access to individual fields defined in the ARM
289// architecture instruction set encoding as described in figure A3-1.
290//
291// Example: Test whether the instruction at ptr does set the condition code
292// bits.
293//
294// bool InstructionSetsConditionCodes(byte* ptr) {
295// Instr* instr = Instr::At(ptr);
296// int type = instr->TypeField();
297// return ((type == 0) || (type == 1)) && instr->HasS();
298// }
299//
300class Instr {
301 public:
302 enum {
303 kInstrSize = 4,
304 kInstrSizeLog2 = 2,
305 kPCReadOffset = 8
306 };
307
Carl Shapiroa2e18e12011-06-21 18:57:55 -0700308 bool IsBreakPoint() {
309 return IsBkpt();
310 }
311
312 // Get the raw instruction bits.
313 inline int32_t InstructionBits() const {
314 return *reinterpret_cast<const int32_t*>(this);
315 }
316
317 // Set the raw instruction bits to value.
318 inline void SetInstructionBits(int32_t value) {
319 *reinterpret_cast<int32_t*>(this) = value;
320 }
321
322 // Read one particular bit out of the instruction bits.
323 inline int Bit(int nr) const {
324 return (InstructionBits() >> nr) & 1;
325 }
326
327 // Read a bit field out of the instruction bits.
328 inline int Bits(int shift, int count) const {
329 return (InstructionBits() >> shift) & ((1 << count) - 1);
330 }
331
332
333 // Accessors for the different named fields used in the ARM encoding.
334 // The naming of these accessor corresponds to figure A3-1.
335 // Generally applicable fields
336 inline Condition ConditionField() const {
337 return static_cast<Condition>(Bits(kConditionShift, kConditionBits));
338 }
339 inline int TypeField() const { return Bits(kTypeShift, kTypeBits); }
340
341 inline Register RnField() const { return static_cast<Register>(
342 Bits(kRnShift, kRnBits)); }
343 inline Register RdField() const { return static_cast<Register>(
344 Bits(kRdShift, kRdBits)); }
345
346 // Fields used in Data processing instructions
347 inline Opcode OpcodeField() const {
348 return static_cast<Opcode>(Bits(kOpcodeShift, kOpcodeBits));
349 }
350 inline int SField() const { return Bits(kSShift, kSBits); }
351 // with register
352 inline Register RmField() const {
353 return static_cast<Register>(Bits(kRmShift, kRmBits));
354 }
355 inline Shift ShiftField() const { return static_cast<Shift>(
356 Bits(kShiftShift, kShiftBits)); }
357 inline int RegShiftField() const { return Bit(4); }
358 inline Register RsField() const {
359 return static_cast<Register>(Bits(kRsShift, kRsBits));
360 }
361 inline int ShiftAmountField() const { return Bits(kShiftImmShift,
362 kShiftImmBits); }
363 // with immediate
364 inline int RotateField() const { return Bits(kRotateShift, kRotateBits); }
365 inline int Immed8Field() const { return Bits(kImmed8Shift, kImmed8Bits); }
366
367 // Fields used in Load/Store instructions
368 inline int PUField() const { return Bits(23, 2); }
369 inline int BField() const { return Bit(22); }
370 inline int WField() const { return Bit(21); }
371 inline int LField() const { return Bit(20); }
372 // with register uses same fields as Data processing instructions above
373 // with immediate
374 inline int Offset12Field() const { return Bits(kOffset12Shift,
375 kOffset12Bits); }
376 // multiple
377 inline int RlistField() const { return Bits(0, 16); }
378 // extra loads and stores
379 inline int SignField() const { return Bit(6); }
380 inline int HField() const { return Bit(5); }
381 inline int ImmedHField() const { return Bits(8, 4); }
382 inline int ImmedLField() const { return Bits(0, 4); }
383
384 // Fields used in Branch instructions
385 inline int LinkField() const { return Bits(kLinkShift, kLinkBits); }
386 inline int SImmed24Field() const { return ((InstructionBits() << 8) >> 8); }
387
388 // Fields used in Supervisor Call instructions
389 inline uint32_t SvcField() const { return Bits(0, 24); }
390
391 // Field used in Breakpoint instruction
392 inline uint16_t BkptField() const {
393 return ((Bits(8, 12) << 4) | Bits(0, 4));
394 }
395
396 // Field used in 16-bit immediate move instructions
397 inline uint16_t MovwField() const {
398 return ((Bits(16, 4) << 12) | Bits(0, 12));
399 }
400
401 // Field used in VFP float immediate move instruction
402 inline float ImmFloatField() const {
403 uint32_t imm32 = (Bit(19) << 31) | (((1 << 5) - Bit(18)) << 25) |
404 (Bits(16, 2) << 23) | (Bits(0, 4) << 19);
405 return bit_cast<float, uint32_t>(imm32);
406 }
407
408 // Field used in VFP double immediate move instruction
409 inline double ImmDoubleField() const {
410 uint64_t imm64 = (Bit(19)*(1LL << 63)) | (((1LL << 8) - Bit(18)) << 54) |
411 (Bits(16, 2)*(1LL << 52)) | (Bits(0, 4)*(1LL << 48));
412 return bit_cast<double, uint64_t>(imm64);
413 }
414
415 // Test for data processing instructions of type 0 or 1.
416 // See "ARM Architecture Reference Manual ARMv7-A and ARMv7-R edition",
417 // section A5.1 "ARM instruction set encoding".
418 inline bool IsDataProcessing() const {
Ian Rogersb033c752011-07-20 12:22:35 -0700419 CHECK_NE(ConditionField(), kSpecialCondition);
420 CHECK_EQ(Bits(26, 2), 0); // Type 0 or 1.
Carl Shapiroa2e18e12011-06-21 18:57:55 -0700421 return ((Bits(20, 5) & 0x19) != 0x10) &&
422 ((Bit(25) == 1) || // Data processing immediate.
423 (Bit(4) == 0) || // Data processing register.
424 (Bit(7) == 0)); // Data processing register-shifted register.
425 }
426
427 // Tests for special encodings of type 0 instructions (extra loads and stores,
428 // as well as multiplications, synchronization primitives, and miscellaneous).
429 // Can only be called for a type 0 or 1 instruction.
430 inline bool IsMiscellaneous() const {
Ian Rogersb033c752011-07-20 12:22:35 -0700431 CHECK_EQ(Bits(26, 2), 0); // Type 0 or 1.
Carl Shapiroa2e18e12011-06-21 18:57:55 -0700432 return ((Bit(25) == 0) && ((Bits(20, 5) & 0x19) == 0x10) && (Bit(7) == 0));
433 }
434 inline bool IsMultiplyOrSyncPrimitive() const {
Ian Rogersb033c752011-07-20 12:22:35 -0700435 CHECK_EQ(Bits(26, 2), 0); // Type 0 or 1.
Carl Shapiroa2e18e12011-06-21 18:57:55 -0700436 return ((Bit(25) == 0) && (Bits(4, 4) == 9));
437 }
438
439 // Test for Supervisor Call instruction.
440 inline bool IsSvc() const {
441 return ((InstructionBits() & 0xff000000) == 0xef000000);
442 }
443
444 // Test for Breakpoint instruction.
445 inline bool IsBkpt() const {
446 return ((InstructionBits() & 0xfff000f0) == 0xe1200070);
447 }
448
449 // VFP register fields.
450 inline SRegister SnField() const {
451 return static_cast<SRegister>((Bits(kRnShift, kRnBits) << 1) + Bit(7));
452 }
453 inline SRegister SdField() const {
454 return static_cast<SRegister>((Bits(kRdShift, kRdBits) << 1) + Bit(22));
455 }
456 inline SRegister SmField() const {
457 return static_cast<SRegister>((Bits(kRmShift, kRmBits) << 1) + Bit(5));
458 }
459 inline DRegister DnField() const {
460 return static_cast<DRegister>(Bits(kRnShift, kRnBits) + (Bit(7) << 4));
461 }
462 inline DRegister DdField() const {
463 return static_cast<DRegister>(Bits(kRdShift, kRdBits) + (Bit(22) << 4));
464 }
465 inline DRegister DmField() const {
466 return static_cast<DRegister>(Bits(kRmShift, kRmBits) + (Bit(5) << 4));
467 }
468
469 // Test for VFP data processing or single transfer instructions of type 7.
470 inline bool IsVFPDataProcessingOrSingleTransfer() const {
Ian Rogersb033c752011-07-20 12:22:35 -0700471 CHECK_NE(ConditionField(), kSpecialCondition);
472 CHECK_EQ(TypeField(), 7);
Carl Shapiroa2e18e12011-06-21 18:57:55 -0700473 return ((Bit(24) == 0) && (Bits(9, 3) == 5));
474 // Bit(4) == 0: Data Processing
475 // Bit(4) == 1: 8, 16, or 32-bit Transfer between ARM Core and VFP
476 }
477
478 // Test for VFP 64-bit transfer instructions of type 6.
479 inline bool IsVFPDoubleTransfer() const {
Ian Rogersb033c752011-07-20 12:22:35 -0700480 CHECK_NE(ConditionField(), kSpecialCondition);
481 CHECK_EQ(TypeField(), 6);
Carl Shapiroa2e18e12011-06-21 18:57:55 -0700482 return ((Bits(21, 4) == 2) && (Bits(9, 3) == 5) &&
483 ((Bits(4, 4) & 0xd) == 1));
484 }
485
486 // Test for VFP load and store instructions of type 6.
487 inline bool IsVFPLoadStore() const {
Ian Rogersb033c752011-07-20 12:22:35 -0700488 CHECK_NE(ConditionField(), kSpecialCondition);
489 CHECK_EQ(TypeField(), 6);
Carl Shapiroa2e18e12011-06-21 18:57:55 -0700490 return ((Bits(20, 5) & 0x12) == 0x10) && (Bits(9, 3) == 5);
491 }
492
493 // Special accessors that test for existence of a value.
494 inline bool HasS() const { return SField() == 1; }
495 inline bool HasB() const { return BField() == 1; }
496 inline bool HasW() const { return WField() == 1; }
497 inline bool HasL() const { return LField() == 1; }
498 inline bool HasSign() const { return SignField() == 1; }
499 inline bool HasH() const { return HField() == 1; }
500 inline bool HasLink() const { return LinkField() == 1; }
501
502 // Instructions are read out of a code stream. The only way to get a
503 // reference to an instruction is to convert a pointer. There is no way
504 // to allocate or create instances of class Instr.
505 // Use the At(pc) function to create references to Instr.
506 static Instr* At(uword pc) { return reinterpret_cast<Instr*>(pc); }
507 Instr* Next() { return this + kInstrSize; }
508
509 private:
510 // We need to prevent the creation of instances of class Instr.
511 DISALLOW_IMPLICIT_CONSTRUCTORS(Instr);
512};
513
Ian Rogers2c8f6532011-09-02 17:16:34 -0700514} // namespace arm
Carl Shapiroa2e18e12011-06-21 18:57:55 -0700515} // namespace art
516
517#endif // ART_SRC_CONSTANTS_ARM_H_