blob: b2b5cb56b0fdea31b8b98ca69cb0a191788337ed [file] [log] [blame]
Leon Clarked91b9f72010-01-27 17:25:45 +00001// Copyright 2010 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#ifndef V8_ARM_CONSTANTS_ARM_H_
29#define V8_ARM_CONSTANTS_ARM_H_
30
31// The simulator emulates the EABI so we define the USE_ARM_EABI macro if we
32// are not running on real ARM hardware. One reason for this is that the
33// old ABI uses fp registers in the calling convention and the simulator does
34// not simulate fp registers or coroutine instructions.
35#if defined(__ARM_EABI__) || !defined(__arm__)
36# define USE_ARM_EABI 1
37#endif
38
39// This means that interwork-compatible jump instructions are generated. We
40// want to generate them on the simulator too so it makes snapshots that can
41// be used on real hardware.
42#if defined(__THUMB_INTERWORK__) || !defined(__arm__)
43# define USE_THUMB_INTERWORK 1
44#endif
45
Steve Blockd0582a62009-12-15 09:54:21 +000046#if defined(__ARM_ARCH_7A__) || \
47 defined(__ARM_ARCH_7R__) || \
Steve Blocka7e24c12009-10-30 11:49:00 +000048 defined(__ARM_ARCH_7__)
Steve Blockd0582a62009-12-15 09:54:21 +000049# define CAN_USE_ARMV7_INSTRUCTIONS 1
Steve Blocka7e24c12009-10-30 11:49:00 +000050#endif
51
Steve Blockd0582a62009-12-15 09:54:21 +000052#if defined(__ARM_ARCH_6__) || \
53 defined(__ARM_ARCH_6J__) || \
54 defined(__ARM_ARCH_6K__) || \
55 defined(__ARM_ARCH_6Z__) || \
56 defined(__ARM_ARCH_6ZK__) || \
57 defined(__ARM_ARCH_6T2__) || \
58 defined(CAN_USE_ARMV7_INSTRUCTIONS)
Steve Blocka7e24c12009-10-30 11:49:00 +000059# define CAN_USE_ARMV6_INSTRUCTIONS 1
60#endif
61
Steve Blockd0582a62009-12-15 09:54:21 +000062#if defined(__ARM_ARCH_5T__) || \
63 defined(__ARM_ARCH_5TE__) || \
64 defined(CAN_USE_ARMV6_INSTRUCTIONS)
65# define CAN_USE_ARMV5_INSTRUCTIONS 1
66# define CAN_USE_THUMB_INSTRUCTIONS 1
Steve Blocka7e24c12009-10-30 11:49:00 +000067#endif
68
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +010069// Simulator should support ARM5 instructions and unaligned access by default.
Steve Blocka7e24c12009-10-30 11:49:00 +000070#if !defined(__arm__)
71# define CAN_USE_ARMV5_INSTRUCTIONS 1
72# define CAN_USE_THUMB_INSTRUCTIONS 1
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +010073
74# ifndef CAN_USE_UNALIGNED_ACCESSES
75# define CAN_USE_UNALIGNED_ACCESSES 1
76# endif
77
Steve Blocka7e24c12009-10-30 11:49:00 +000078#endif
79
Kristian Monsen25f61362010-05-21 11:50:48 +010080#if CAN_USE_UNALIGNED_ACCESSES
81#define V8_TARGET_CAN_READ_UNALIGNED 1
82#endif
83
Steve Block6ded16b2010-05-10 14:33:55 +010084// Using blx may yield better code, so use it when required or when available
85#if defined(USE_THUMB_INTERWORK) || defined(CAN_USE_ARMV5_INSTRUCTIONS)
86#define USE_BLX 1
87#endif
88
Steve Blocka7e24c12009-10-30 11:49:00 +000089namespace assembler {
90namespace arm {
91
92// Number of registers in normal ARM mode.
93static const int kNumRegisters = 16;
94
Steve Blockd0582a62009-12-15 09:54:21 +000095// VFP support.
Steve Block6ded16b2010-05-10 14:33:55 +010096static const int kNumVFPSingleRegisters = 32;
97static const int kNumVFPDoubleRegisters = 16;
98static const int kNumVFPRegisters =
99 kNumVFPSingleRegisters + kNumVFPDoubleRegisters;
Steve Blockd0582a62009-12-15 09:54:21 +0000100
Steve Blocka7e24c12009-10-30 11:49:00 +0000101// PC is register 15.
102static const int kPCRegister = 15;
103static const int kNoRegister = -1;
104
105// Defines constants and accessor classes to assemble, disassemble and
106// simulate ARM instructions.
107//
108// Section references in the code refer to the "ARM Architecture Reference
109// Manual" from July 2005 (available at http://www.arm.com/miscPDFs/14128.pdf)
110//
111// Constants for specific fields are defined in their respective named enums.
112// General constants are in an anonymous enum in class Instr.
113
114typedef unsigned char byte;
115
116// Values for the condition field as defined in section A3.2
117enum Condition {
118 no_condition = -1,
119 EQ = 0, // equal
120 NE = 1, // not equal
121 CS = 2, // carry set/unsigned higher or same
122 CC = 3, // carry clear/unsigned lower
123 MI = 4, // minus/negative
124 PL = 5, // plus/positive or zero
125 VS = 6, // overflow
126 VC = 7, // no overflow
127 HI = 8, // unsigned higher
128 LS = 9, // unsigned lower or same
129 GE = 10, // signed greater than or equal
130 LT = 11, // signed less than
131 GT = 12, // signed greater than
132 LE = 13, // signed less than or equal
133 AL = 14, // always (unconditional)
134 special_condition = 15, // special condition (refer to section A3.2.1)
135 max_condition = 16
136};
137
138
139// Opcodes for Data-processing instructions (instructions with a type 0 and 1)
140// as defined in section A3.4
141enum Opcode {
142 no_operand = -1,
143 AND = 0, // Logical AND
144 EOR = 1, // Logical Exclusive OR
145 SUB = 2, // Subtract
146 RSB = 3, // Reverse Subtract
147 ADD = 4, // Add
148 ADC = 5, // Add with Carry
149 SBC = 6, // Subtract with Carry
150 RSC = 7, // Reverse Subtract with Carry
151 TST = 8, // Test
152 TEQ = 9, // Test Equivalence
153 CMP = 10, // Compare
154 CMN = 11, // Compare Negated
155 ORR = 12, // Logical (inclusive) OR
156 MOV = 13, // Move
157 BIC = 14, // Bit Clear
158 MVN = 15, // Move Not
159 max_operand = 16
160};
161
162
Steve Block6ded16b2010-05-10 14:33:55 +0100163// The bits for bit 7-4 for some type 0 miscellaneous instructions.
164enum MiscInstructionsBits74 {
165 // With bits 22-21 01.
Steve Blocka7e24c12009-10-30 11:49:00 +0000166 BX = 1,
167 BXJ = 2,
168 BLX = 3,
Steve Block6ded16b2010-05-10 14:33:55 +0100169 BKPT = 7,
Steve Blocka7e24c12009-10-30 11:49:00 +0000170
Steve Block6ded16b2010-05-10 14:33:55 +0100171 // With bits 22-21 11.
Steve Blocka7e24c12009-10-30 11:49:00 +0000172 CLZ = 1
173};
174
175
Steve Blocka7e24c12009-10-30 11:49:00 +0000176// Shifter types for Data-processing operands as defined in section A5.1.2.
177enum Shift {
178 no_shift = -1,
179 LSL = 0, // Logical shift left
180 LSR = 1, // Logical shift right
181 ASR = 2, // Arithmetic shift right
182 ROR = 3, // Rotate right
183 max_shift = 4
184};
185
186
187// Special Software Interrupt codes when used in the presence of the ARM
188// simulator.
189enum SoftwareInterruptCodes {
190 // transition to C code
191 call_rt_redirected = 0x10,
192 // break point
193 break_point = 0x20
194};
195
196
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100197// Type of VFP register. Determines register encoding.
198enum VFPRegPrecision {
199 kSinglePrecision = 0,
200 kDoublePrecision = 1
201};
202
203
Steve Blocka7e24c12009-10-30 11:49:00 +0000204typedef int32_t instr_t;
205
206
207// The class Instr enables access to individual fields defined in the ARM
208// architecture instruction set encoding as described in figure A3-1.
209//
210// Example: Test whether the instruction at ptr does set the condition code
211// bits.
212//
213// bool InstructionSetsConditionCodes(byte* ptr) {
214// Instr* instr = Instr::At(ptr);
215// int type = instr->TypeField();
216// return ((type == 0) || (type == 1)) && instr->HasS();
217// }
218//
219class Instr {
220 public:
221 enum {
222 kInstrSize = 4,
223 kInstrSizeLog2 = 2,
224 kPCReadOffset = 8
225 };
226
227 // Get the raw instruction bits.
228 inline instr_t InstructionBits() const {
229 return *reinterpret_cast<const instr_t*>(this);
230 }
231
232 // Set the raw instruction bits to value.
233 inline void SetInstructionBits(instr_t value) {
234 *reinterpret_cast<instr_t*>(this) = value;
235 }
236
237 // Read one particular bit out of the instruction bits.
238 inline int Bit(int nr) const {
239 return (InstructionBits() >> nr) & 1;
240 }
241
242 // Read a bit field out of the instruction bits.
243 inline int Bits(int hi, int lo) const {
244 return (InstructionBits() >> lo) & ((2 << (hi - lo)) - 1);
245 }
246
247
248 // Accessors for the different named fields used in the ARM encoding.
249 // The naming of these accessor corresponds to figure A3-1.
250 // Generally applicable fields
251 inline Condition ConditionField() const {
252 return static_cast<Condition>(Bits(31, 28));
253 }
254 inline int TypeField() const { return Bits(27, 25); }
255
256 inline int RnField() const { return Bits(19, 16); }
257 inline int RdField() const { return Bits(15, 12); }
258
Leon Clarked91b9f72010-01-27 17:25:45 +0000259 inline int CoprocessorField() const { return Bits(11, 8); }
Steve Blockd0582a62009-12-15 09:54:21 +0000260 // Support for VFP.
261 // Vn(19-16) | Vd(15-12) | Vm(3-0)
262 inline int VnField() const { return Bits(19, 16); }
263 inline int VmField() const { return Bits(3, 0); }
264 inline int VdField() const { return Bits(15, 12); }
265 inline int NField() const { return Bit(7); }
266 inline int MField() const { return Bit(5); }
267 inline int DField() const { return Bit(22); }
268 inline int RtField() const { return Bits(15, 12); }
Leon Clarked91b9f72010-01-27 17:25:45 +0000269 inline int PField() const { return Bit(24); }
270 inline int UField() const { return Bit(23); }
Steve Block6ded16b2010-05-10 14:33:55 +0100271 inline int Opc1Field() const { return (Bit(23) << 2) | Bits(21, 20); }
272 inline int Opc2Field() const { return Bits(19, 16); }
273 inline int Opc3Field() const { return Bits(7, 6); }
274 inline int SzField() const { return Bit(8); }
275 inline int VLField() const { return Bit(20); }
276 inline int VCField() const { return Bit(8); }
277 inline int VAField() const { return Bits(23, 21); }
278 inline int VBField() const { return Bits(6, 5); }
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100279 inline int VFPNRegCode(VFPRegPrecision pre) {
280 return VFPGlueRegCode(pre, 16, 7);
281 }
282 inline int VFPMRegCode(VFPRegPrecision pre) {
283 return VFPGlueRegCode(pre, 0, 5);
284 }
285 inline int VFPDRegCode(VFPRegPrecision pre) {
286 return VFPGlueRegCode(pre, 12, 22);
287 }
Steve Blockd0582a62009-12-15 09:54:21 +0000288
Steve Blocka7e24c12009-10-30 11:49:00 +0000289 // Fields used in Data processing instructions
290 inline Opcode OpcodeField() const {
291 return static_cast<Opcode>(Bits(24, 21));
292 }
293 inline int SField() const { return Bit(20); }
294 // with register
295 inline int RmField() const { return Bits(3, 0); }
296 inline Shift ShiftField() const { return static_cast<Shift>(Bits(6, 5)); }
297 inline int RegShiftField() const { return Bit(4); }
298 inline int RsField() const { return Bits(11, 8); }
299 inline int ShiftAmountField() const { return Bits(11, 7); }
300 // with immediate
301 inline int RotateField() const { return Bits(11, 8); }
302 inline int Immed8Field() const { return Bits(7, 0); }
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100303 inline int Immed4Field() const { return Bits(19, 16); }
304 inline int ImmedMovwMovtField() const {
305 return Immed4Field() << 12 | Offset12Field(); }
Steve Blocka7e24c12009-10-30 11:49:00 +0000306
307 // Fields used in Load/Store instructions
308 inline int PUField() const { return Bits(24, 23); }
309 inline int BField() const { return Bit(22); }
310 inline int WField() const { return Bit(21); }
311 inline int LField() const { return Bit(20); }
312 // with register uses same fields as Data processing instructions above
313 // with immediate
314 inline int Offset12Field() const { return Bits(11, 0); }
315 // multiple
316 inline int RlistField() const { return Bits(15, 0); }
317 // extra loads and stores
318 inline int SignField() const { return Bit(6); }
319 inline int HField() const { return Bit(5); }
320 inline int ImmedHField() const { return Bits(11, 8); }
321 inline int ImmedLField() const { return Bits(3, 0); }
322
323 // Fields used in Branch instructions
324 inline int LinkField() const { return Bit(24); }
325 inline int SImmed24Field() const { return ((InstructionBits() << 8) >> 8); }
326
327 // Fields used in Software interrupt instructions
328 inline SoftwareInterruptCodes SwiField() const {
329 return static_cast<SoftwareInterruptCodes>(Bits(23, 0));
330 }
331
332 // Test for special encodings of type 0 instructions (extra loads and stores,
333 // as well as multiplications).
334 inline bool IsSpecialType0() const { return (Bit(7) == 1) && (Bit(4) == 1); }
335
Steve Block6ded16b2010-05-10 14:33:55 +0100336 // Test for miscellaneous instructions encodings of type 0 instructions.
337 inline bool IsMiscType0() const { return (Bit(24) == 1)
338 && (Bit(23) == 0)
339 && (Bit(20) == 0)
340 && ((Bit(7) == 0)); }
341
Steve Blocka7e24c12009-10-30 11:49:00 +0000342 // Special accessors that test for existence of a value.
343 inline bool HasS() const { return SField() == 1; }
344 inline bool HasB() const { return BField() == 1; }
345 inline bool HasW() const { return WField() == 1; }
346 inline bool HasL() const { return LField() == 1; }
Leon Clarked91b9f72010-01-27 17:25:45 +0000347 inline bool HasU() const { return UField() == 1; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000348 inline bool HasSign() const { return SignField() == 1; }
349 inline bool HasH() const { return HField() == 1; }
350 inline bool HasLink() const { return LinkField() == 1; }
351
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100352 // Decoding the double immediate in the vmov instruction.
353 double DoubleImmedVmov() const;
354
Steve Blocka7e24c12009-10-30 11:49:00 +0000355 // Instructions are read of out a code stream. The only way to get a
356 // reference to an instruction is to convert a pointer. There is no way
357 // to allocate or create instances of class Instr.
358 // Use the At(pc) function to create references to Instr.
359 static Instr* At(byte* pc) { return reinterpret_cast<Instr*>(pc); }
360
361 private:
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100362 // Join split register codes, depending on single or double precision.
363 // four_bit is the position of the least-significant bit of the four
364 // bit specifier. one_bit is the position of the additional single bit
365 // specifier.
366 inline int VFPGlueRegCode(VFPRegPrecision pre, int four_bit, int one_bit) {
367 if (pre == kSinglePrecision) {
368 return (Bits(four_bit + 3, four_bit) << 1) | Bit(one_bit);
369 }
370 return (Bit(one_bit) << 4) | Bits(four_bit + 3, four_bit);
371 }
372
Steve Blocka7e24c12009-10-30 11:49:00 +0000373 // We need to prevent the creation of instances of class Instr.
374 DISALLOW_IMPLICIT_CONSTRUCTORS(Instr);
375};
376
377
378// Helper functions for converting between register numbers and names.
379class Registers {
380 public:
381 // Return the name of the register.
382 static const char* Name(int reg);
383
384 // Lookup the register number for the name provided.
385 static int Number(const char* name);
386
387 struct RegisterAlias {
388 int reg;
Steve Blockd0582a62009-12-15 09:54:21 +0000389 const char* name;
Steve Blocka7e24c12009-10-30 11:49:00 +0000390 };
391
392 private:
393 static const char* names_[kNumRegisters];
394 static const RegisterAlias aliases_[];
395};
396
Steve Blockd0582a62009-12-15 09:54:21 +0000397// Helper functions for converting between VFP register numbers and names.
398class VFPRegisters {
399 public:
400 // Return the name of the register.
Steve Block6ded16b2010-05-10 14:33:55 +0100401 static const char* Name(int reg, bool is_double);
402
403 // Lookup the register number for the name provided.
404 // Set flag pointed by is_double to true if register
405 // is double-precision.
406 static int Number(const char* name, bool* is_double);
Steve Blockd0582a62009-12-15 09:54:21 +0000407
408 private:
409 static const char* names_[kNumVFPRegisters];
410};
Steve Blocka7e24c12009-10-30 11:49:00 +0000411
412
413} } // namespace assembler::arm
414
415#endif // V8_ARM_CONSTANTS_ARM_H_