blob: 0ac567cb11f8ccb3252e1f9d4da8491e6d203d81 [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 Block1e0659c2011-05-24 12:43:12 +010089namespace v8 {
90namespace internal {
Steve Blocka7e24c12009-10-30 11:49:00 +000091
Steve Block44f0eee2011-05-26 01:26:41 +010092// Constant pool marker.
93static const int kConstantPoolMarkerMask = 0xffe00000;
94static const int kConstantPoolMarker = 0x0c000000;
95static const int kConstantPoolLengthMask = 0x001ffff;
96
Steve Blocka7e24c12009-10-30 11:49:00 +000097// Number of registers in normal ARM mode.
98static const int kNumRegisters = 16;
99
Steve Blockd0582a62009-12-15 09:54:21 +0000100// VFP support.
Steve Block6ded16b2010-05-10 14:33:55 +0100101static const int kNumVFPSingleRegisters = 32;
102static const int kNumVFPDoubleRegisters = 16;
103static const int kNumVFPRegisters =
104 kNumVFPSingleRegisters + kNumVFPDoubleRegisters;
Steve Blockd0582a62009-12-15 09:54:21 +0000105
Steve Blocka7e24c12009-10-30 11:49:00 +0000106// PC is register 15.
107static const int kPCRegister = 15;
108static const int kNoRegister = -1;
109
Steve Block1e0659c2011-05-24 12:43:12 +0100110// -----------------------------------------------------------------------------
111// Conditions.
112
Steve Blocka7e24c12009-10-30 11:49:00 +0000113// Defines constants and accessor classes to assemble, disassemble and
114// simulate ARM instructions.
115//
116// Section references in the code refer to the "ARM Architecture Reference
117// Manual" from July 2005 (available at http://www.arm.com/miscPDFs/14128.pdf)
118//
119// Constants for specific fields are defined in their respective named enums.
120// General constants are in an anonymous enum in class Instr.
121
Steve Blocka7e24c12009-10-30 11:49:00 +0000122// Values for the condition field as defined in section A3.2
123enum Condition {
Steve Block1e0659c2011-05-24 12:43:12 +0100124 kNoCondition = -1,
125
126 eq = 0 << 28, // Z set Equal.
127 ne = 1 << 28, // Z clear Not equal.
128 cs = 2 << 28, // C set Unsigned higher or same.
129 cc = 3 << 28, // C clear Unsigned lower.
130 mi = 4 << 28, // N set Negative.
131 pl = 5 << 28, // N clear Positive or zero.
132 vs = 6 << 28, // V set Overflow.
133 vc = 7 << 28, // V clear No overflow.
134 hi = 8 << 28, // C set, Z clear Unsigned higher.
135 ls = 9 << 28, // C clear or Z set Unsigned lower or same.
136 ge = 10 << 28, // N == V Greater or equal.
137 lt = 11 << 28, // N != V Less than.
138 gt = 12 << 28, // Z clear, N == V Greater than.
139 le = 13 << 28, // Z set or N != V Less then or equal
140 al = 14 << 28, // Always.
141
142 kSpecialCondition = 15 << 28, // Special condition (refer to section A3.2.1).
143 kNumberOfConditions = 16,
144
145 // Aliases.
146 hs = cs, // C set Unsigned higher or same.
147 lo = cc // C clear Unsigned lower.
Steve Blocka7e24c12009-10-30 11:49:00 +0000148};
149
150
Steve Block1e0659c2011-05-24 12:43:12 +0100151inline Condition NegateCondition(Condition cond) {
152 ASSERT(cond != al);
153 return static_cast<Condition>(cond ^ ne);
154}
155
156
157// Corresponds to transposing the operands of a comparison.
158inline Condition ReverseCondition(Condition cond) {
159 switch (cond) {
160 case lo:
161 return hi;
162 case hi:
163 return lo;
164 case hs:
165 return ls;
166 case ls:
167 return hs;
168 case lt:
169 return gt;
170 case gt:
171 return lt;
172 case ge:
173 return le;
174 case le:
175 return ge;
176 default:
177 return cond;
178 };
179}
180
181
182// -----------------------------------------------------------------------------
183// Instructions encoding.
184
185// Instr is merely used by the Assembler to distinguish 32bit integers
186// representing instructions from usual 32 bit values.
187// Instruction objects are pointers to 32bit values, and provide methods to
188// access the various ISA fields.
189typedef int32_t Instr;
190
191
Steve Blocka7e24c12009-10-30 11:49:00 +0000192// Opcodes for Data-processing instructions (instructions with a type 0 and 1)
193// as defined in section A3.4
194enum Opcode {
Steve Block1e0659c2011-05-24 12:43:12 +0100195 AND = 0 << 21, // Logical AND.
196 EOR = 1 << 21, // Logical Exclusive OR.
197 SUB = 2 << 21, // Subtract.
198 RSB = 3 << 21, // Reverse Subtract.
199 ADD = 4 << 21, // Add.
200 ADC = 5 << 21, // Add with Carry.
201 SBC = 6 << 21, // Subtract with Carry.
202 RSC = 7 << 21, // Reverse Subtract with Carry.
203 TST = 8 << 21, // Test.
204 TEQ = 9 << 21, // Test Equivalence.
205 CMP = 10 << 21, // Compare.
206 CMN = 11 << 21, // Compare Negated.
207 ORR = 12 << 21, // Logical (inclusive) OR.
208 MOV = 13 << 21, // Move.
209 BIC = 14 << 21, // Bit Clear.
210 MVN = 15 << 21 // Move Not.
Steve Blocka7e24c12009-10-30 11:49:00 +0000211};
212
213
Steve Block6ded16b2010-05-10 14:33:55 +0100214// The bits for bit 7-4 for some type 0 miscellaneous instructions.
215enum MiscInstructionsBits74 {
216 // With bits 22-21 01.
Steve Block1e0659c2011-05-24 12:43:12 +0100217 BX = 1 << 4,
218 BXJ = 2 << 4,
219 BLX = 3 << 4,
220 BKPT = 7 << 4,
Steve Blocka7e24c12009-10-30 11:49:00 +0000221
Steve Block6ded16b2010-05-10 14:33:55 +0100222 // With bits 22-21 11.
Steve Block1e0659c2011-05-24 12:43:12 +0100223 CLZ = 1 << 4
224};
225
226
227// Instruction encoding bits and masks.
228enum {
229 H = 1 << 5, // Halfword (or byte).
230 S6 = 1 << 6, // Signed (or unsigned).
231 L = 1 << 20, // Load (or store).
232 S = 1 << 20, // Set condition code (or leave unchanged).
233 W = 1 << 21, // Writeback base register (or leave unchanged).
234 A = 1 << 21, // Accumulate in multiply instruction (or not).
235 B = 1 << 22, // Unsigned byte (or word).
236 N = 1 << 22, // Long (or short).
237 U = 1 << 23, // Positive (or negative) offset/index.
238 P = 1 << 24, // Offset/pre-indexed addressing (or post-indexed addressing).
239 I = 1 << 25, // Immediate shifter operand (or not).
240
241 B4 = 1 << 4,
242 B5 = 1 << 5,
243 B6 = 1 << 6,
244 B7 = 1 << 7,
245 B8 = 1 << 8,
246 B9 = 1 << 9,
247 B12 = 1 << 12,
248 B16 = 1 << 16,
249 B18 = 1 << 18,
250 B19 = 1 << 19,
251 B20 = 1 << 20,
252 B21 = 1 << 21,
253 B22 = 1 << 22,
254 B23 = 1 << 23,
255 B24 = 1 << 24,
256 B25 = 1 << 25,
257 B26 = 1 << 26,
258 B27 = 1 << 27,
259 B28 = 1 << 28,
260
261 // Instruction bit masks.
262 kCondMask = 15 << 28,
263 kALUMask = 0x6f << 21,
264 kRdMask = 15 << 12, // In str instruction.
265 kCoprocessorMask = 15 << 8,
266 kOpCodeMask = 15 << 21, // In data-processing instructions.
267 kImm24Mask = (1 << 24) - 1,
268 kOff12Mask = (1 << 12) - 1
269};
270
271
272// -----------------------------------------------------------------------------
273// Addressing modes and instruction variants.
274
275// Condition code updating mode.
276enum SBit {
277 SetCC = 1 << 20, // Set condition code.
278 LeaveCC = 0 << 20 // Leave condition code unchanged.
279};
280
281
282// Status register selection.
283enum SRegister {
284 CPSR = 0 << 22,
285 SPSR = 1 << 22
Steve Blocka7e24c12009-10-30 11:49:00 +0000286};
287
288
Steve Blocka7e24c12009-10-30 11:49:00 +0000289// Shifter types for Data-processing operands as defined in section A5.1.2.
Steve Block1e0659c2011-05-24 12:43:12 +0100290enum ShiftOp {
291 LSL = 0 << 5, // Logical shift left.
292 LSR = 1 << 5, // Logical shift right.
293 ASR = 2 << 5, // Arithmetic shift right.
294 ROR = 3 << 5, // Rotate right.
295
296 // RRX is encoded as ROR with shift_imm == 0.
297 // Use a special code to make the distinction. The RRX ShiftOp is only used
298 // as an argument, and will never actually be encoded. The Assembler will
299 // detect it and emit the correct ROR shift operand with shift_imm == 0.
300 RRX = -1,
301 kNumberOfShifts = 4
Steve Blocka7e24c12009-10-30 11:49:00 +0000302};
303
304
Steve Block1e0659c2011-05-24 12:43:12 +0100305// Status register fields.
306enum SRegisterField {
307 CPSR_c = CPSR | 1 << 16,
308 CPSR_x = CPSR | 1 << 17,
309 CPSR_s = CPSR | 1 << 18,
310 CPSR_f = CPSR | 1 << 19,
311 SPSR_c = SPSR | 1 << 16,
312 SPSR_x = SPSR | 1 << 17,
313 SPSR_s = SPSR | 1 << 18,
314 SPSR_f = SPSR | 1 << 19
315};
316
317// Status register field mask (or'ed SRegisterField enum values).
318typedef uint32_t SRegisterFieldMask;
319
320
321// Memory operand addressing mode.
322enum AddrMode {
323 // Bit encoding P U W.
324 Offset = (8|4|0) << 21, // Offset (without writeback to base).
325 PreIndex = (8|4|1) << 21, // Pre-indexed addressing with writeback.
326 PostIndex = (0|4|0) << 21, // Post-indexed addressing with writeback.
327 NegOffset = (8|0|0) << 21, // Negative offset (without writeback to base).
328 NegPreIndex = (8|0|1) << 21, // Negative pre-indexed with writeback.
329 NegPostIndex = (0|0|0) << 21 // Negative post-indexed with writeback.
330};
331
332
333// Load/store multiple addressing mode.
334enum BlockAddrMode {
335 // Bit encoding P U W .
336 da = (0|0|0) << 21, // Decrement after.
337 ia = (0|4|0) << 21, // Increment after.
338 db = (8|0|0) << 21, // Decrement before.
339 ib = (8|4|0) << 21, // Increment before.
340 da_w = (0|0|1) << 21, // Decrement after with writeback to base.
341 ia_w = (0|4|1) << 21, // Increment after with writeback to base.
342 db_w = (8|0|1) << 21, // Decrement before with writeback to base.
343 ib_w = (8|4|1) << 21, // Increment before with writeback to base.
344
345 // Alias modes for comparison when writeback does not matter.
346 da_x = (0|0|0) << 21, // Decrement after.
347 ia_x = (0|4|0) << 21, // Increment after.
348 db_x = (8|0|0) << 21, // Decrement before.
349 ib_x = (8|4|0) << 21 // Increment before.
350};
351
352
353// Coprocessor load/store operand size.
354enum LFlag {
355 Long = 1 << 22, // Long load/store coprocessor.
356 Short = 0 << 22 // Short load/store coprocessor.
357};
358
359
360// -----------------------------------------------------------------------------
361// Supervisor Call (svc) specific support.
362
Steve Blocka7e24c12009-10-30 11:49:00 +0000363// Special Software Interrupt codes when used in the presence of the ARM
364// simulator.
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800365// svc (formerly swi) provides a 24bit immediate value. Use bits 22:0 for
366// standard SoftwareInterrupCode. Bit 23 is reserved for the stop feature.
Steve Blocka7e24c12009-10-30 11:49:00 +0000367enum SoftwareInterruptCodes {
368 // transition to C code
Steve Block1e0659c2011-05-24 12:43:12 +0100369 kCallRtRedirected= 0x10,
Steve Blocka7e24c12009-10-30 11:49:00 +0000370 // break point
Steve Block1e0659c2011-05-24 12:43:12 +0100371 kBreakpoint= 0x20,
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800372 // stop
Steve Block1e0659c2011-05-24 12:43:12 +0100373 kStopCode = 1 << 23
Steve Blocka7e24c12009-10-30 11:49:00 +0000374};
Steve Block1e0659c2011-05-24 12:43:12 +0100375static const uint32_t kStopCodeMask = kStopCode - 1;
376static const uint32_t kMaxStopCode = kStopCode - 1;
377static const int32_t kDefaultStopCode = -1;
Steve Blocka7e24c12009-10-30 11:49:00 +0000378
379
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100380// Type of VFP register. Determines register encoding.
381enum VFPRegPrecision {
382 kSinglePrecision = 0,
383 kDoublePrecision = 1
384};
385
Steve Block1e0659c2011-05-24 12:43:12 +0100386
387// VFP FPSCR constants.
388enum VFPConversionMode {
389 kFPSCRRounding = 0,
390 kDefaultRoundToZero = 1
Russell Brenner90bac252010-11-18 13:33:46 -0800391};
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100392
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100393// This mask does not include the "inexact" or "input denormal" cumulative
394// exceptions flags, because we usually don't want to check for it.
Steve Block1e0659c2011-05-24 12:43:12 +0100395static const uint32_t kVFPExceptionMask = 0xf;
Steve Block44f0eee2011-05-26 01:26:41 +0100396static const uint32_t kVFPInvalidOpExceptionBit = 1 << 0;
397static const uint32_t kVFPOverflowExceptionBit = 1 << 2;
398static const uint32_t kVFPUnderflowExceptionBit = 1 << 3;
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100399static const uint32_t kVFPInexactExceptionBit = 1 << 4;
Steve Block1e0659c2011-05-24 12:43:12 +0100400static const uint32_t kVFPFlushToZeroMask = 1 << 24;
Steve Block1e0659c2011-05-24 12:43:12 +0100401
402static const uint32_t kVFPNConditionFlagBit = 1 << 31;
403static const uint32_t kVFPZConditionFlagBit = 1 << 30;
404static const uint32_t kVFPCConditionFlagBit = 1 << 29;
405static const uint32_t kVFPVConditionFlagBit = 1 << 28;
Steve Blocka7e24c12009-10-30 11:49:00 +0000406
407
Steve Block1e0659c2011-05-24 12:43:12 +0100408// VFP rounding modes. See ARM DDI 0406B Page A2-29.
409enum VFPRoundingMode {
410 RN = 0 << 22, // Round to Nearest.
411 RP = 1 << 22, // Round towards Plus Infinity.
412 RM = 2 << 22, // Round towards Minus Infinity.
413 RZ = 3 << 22, // Round towards zero.
414
415 // Aliases.
416 kRoundToNearest = RN,
417 kRoundToPlusInf = RP,
418 kRoundToMinusInf = RM,
419 kRoundToZero = RZ
420};
421
422static const uint32_t kVFPRoundingModeMask = 3 << 22;
423
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100424enum CheckForInexactConversion {
425 kCheckForInexactConversion,
426 kDontCheckForInexactConversion
427};
428
Steve Block1e0659c2011-05-24 12:43:12 +0100429// -----------------------------------------------------------------------------
430// Hints.
431
432// Branch hints are not used on the ARM. They are defined so that they can
433// appear in shared function signatures, but will be ignored in ARM
434// implementations.
435enum Hint { no_hint };
436
437// Hints are not used on the arm. Negating is trivial.
438inline Hint NegateHint(Hint ignored) { return no_hint; }
439
440
441// -----------------------------------------------------------------------------
442// Specific instructions, constants, and masks.
443// These constants are declared in assembler-arm.cc, as they use named registers
444// and other constants.
445
446
447// add(sp, sp, 4) instruction (aka Pop())
448extern const Instr kPopInstruction;
449
450// str(r, MemOperand(sp, 4, NegPreIndex), al) instruction (aka push(r))
451// register r is not encoded.
452extern const Instr kPushRegPattern;
453
454// ldr(r, MemOperand(sp, 4, PostIndex), al) instruction (aka pop(r))
455// register r is not encoded.
456extern const Instr kPopRegPattern;
457
458// mov lr, pc
459extern const Instr kMovLrPc;
460// ldr rd, [pc, #offset]
461extern const Instr kLdrPCMask;
462extern const Instr kLdrPCPattern;
463// blxcc rm
464extern const Instr kBlxRegMask;
465
466extern const Instr kBlxRegPattern;
467
468extern const Instr kMovMvnMask;
469extern const Instr kMovMvnPattern;
470extern const Instr kMovMvnFlip;
471extern const Instr kMovLeaveCCMask;
472extern const Instr kMovLeaveCCPattern;
473extern const Instr kMovwMask;
474extern const Instr kMovwPattern;
475extern const Instr kMovwLeaveCCFlip;
476extern const Instr kCmpCmnMask;
477extern const Instr kCmpCmnPattern;
478extern const Instr kCmpCmnFlip;
479extern const Instr kAddSubFlip;
480extern const Instr kAndBicFlip;
481
482// A mask for the Rd register for push, pop, ldr, str instructions.
483extern const Instr kLdrRegFpOffsetPattern;
484
485extern const Instr kStrRegFpOffsetPattern;
486
487extern const Instr kLdrRegFpNegOffsetPattern;
488
489extern const Instr kStrRegFpNegOffsetPattern;
490
491extern const Instr kLdrStrInstrTypeMask;
492extern const Instr kLdrStrInstrArgumentMask;
493extern const Instr kLdrStrOffsetMask;
494
495
496// -----------------------------------------------------------------------------
497// Instruction abstraction.
498
499// The class Instruction enables access to individual fields defined in the ARM
Steve Blocka7e24c12009-10-30 11:49:00 +0000500// architecture instruction set encoding as described in figure A3-1.
Steve Block1e0659c2011-05-24 12:43:12 +0100501// Note that the Assembler uses typedef int32_t Instr.
Steve Blocka7e24c12009-10-30 11:49:00 +0000502//
503// Example: Test whether the instruction at ptr does set the condition code
504// bits.
505//
506// bool InstructionSetsConditionCodes(byte* ptr) {
Steve Block1e0659c2011-05-24 12:43:12 +0100507// Instruction* instr = Instruction::At(ptr);
508// int type = instr->TypeValue();
Steve Blocka7e24c12009-10-30 11:49:00 +0000509// return ((type == 0) || (type == 1)) && instr->HasS();
510// }
511//
Steve Block1e0659c2011-05-24 12:43:12 +0100512class Instruction {
Steve Blocka7e24c12009-10-30 11:49:00 +0000513 public:
514 enum {
515 kInstrSize = 4,
516 kInstrSizeLog2 = 2,
517 kPCReadOffset = 8
518 };
519
Steve Block1e0659c2011-05-24 12:43:12 +0100520 // Helper macro to define static accessors.
521 // We use the cast to char* trick to bypass the strict anti-aliasing rules.
522 #define DECLARE_STATIC_TYPED_ACCESSOR(return_type, Name) \
523 static inline return_type Name(Instr instr) { \
524 char* temp = reinterpret_cast<char*>(&instr); \
525 return reinterpret_cast<Instruction*>(temp)->Name(); \
526 }
527
528 #define DECLARE_STATIC_ACCESSOR(Name) DECLARE_STATIC_TYPED_ACCESSOR(int, Name)
529
Steve Blocka7e24c12009-10-30 11:49:00 +0000530 // Get the raw instruction bits.
Steve Block1e0659c2011-05-24 12:43:12 +0100531 inline Instr InstructionBits() const {
532 return *reinterpret_cast<const Instr*>(this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000533 }
534
535 // Set the raw instruction bits to value.
Steve Block1e0659c2011-05-24 12:43:12 +0100536 inline void SetInstructionBits(Instr value) {
537 *reinterpret_cast<Instr*>(this) = value;
Steve Blocka7e24c12009-10-30 11:49:00 +0000538 }
539
540 // Read one particular bit out of the instruction bits.
541 inline int Bit(int nr) const {
542 return (InstructionBits() >> nr) & 1;
543 }
544
Steve Block1e0659c2011-05-24 12:43:12 +0100545 // Read a bit field's value out of the instruction bits.
Steve Blocka7e24c12009-10-30 11:49:00 +0000546 inline int Bits(int hi, int lo) const {
547 return (InstructionBits() >> lo) & ((2 << (hi - lo)) - 1);
548 }
549
Steve Block1e0659c2011-05-24 12:43:12 +0100550 // Read a bit field out of the instruction bits.
551 inline int BitField(int hi, int lo) const {
552 return InstructionBits() & (((2 << (hi - lo)) - 1) << lo);
553 }
554
555 // Static support.
556
557 // Read one particular bit out of the instruction bits.
558 static inline int Bit(Instr instr, int nr) {
559 return (instr >> nr) & 1;
560 }
561
562 // Read the value of a bit field out of the instruction bits.
563 static inline int Bits(Instr instr, int hi, int lo) {
564 return (instr >> lo) & ((2 << (hi - lo)) - 1);
565 }
566
567
568 // Read a bit field out of the instruction bits.
569 static inline int BitField(Instr instr, int hi, int lo) {
570 return instr & (((2 << (hi - lo)) - 1) << lo);
571 }
572
Steve Blocka7e24c12009-10-30 11:49:00 +0000573
574 // Accessors for the different named fields used in the ARM encoding.
575 // The naming of these accessor corresponds to figure A3-1.
Steve Block1e0659c2011-05-24 12:43:12 +0100576 //
577 // Two kind of accessors are declared:
578 // - <Name>Field() will return the raw field, ie the field's bits at their
579 // original place in the instruction encoding.
580 // eg. if instr is the 'addgt r0, r1, r2' instruction, encoded as 0xC0810002
581 // ConditionField(instr) will return 0xC0000000.
582 // - <Name>Value() will return the field value, shifted back to bit 0.
583 // eg. if instr is the 'addgt r0, r1, r2' instruction, encoded as 0xC0810002
584 // ConditionField(instr) will return 0xC.
585
586
Steve Blocka7e24c12009-10-30 11:49:00 +0000587 // Generally applicable fields
Steve Block1e0659c2011-05-24 12:43:12 +0100588 inline Condition ConditionValue() const {
Steve Blocka7e24c12009-10-30 11:49:00 +0000589 return static_cast<Condition>(Bits(31, 28));
590 }
Steve Block1e0659c2011-05-24 12:43:12 +0100591 inline Condition ConditionField() const {
592 return static_cast<Condition>(BitField(31, 28));
593 }
594 DECLARE_STATIC_TYPED_ACCESSOR(Condition, ConditionValue);
595 DECLARE_STATIC_TYPED_ACCESSOR(Condition, ConditionField);
Steve Blocka7e24c12009-10-30 11:49:00 +0000596
Steve Block1e0659c2011-05-24 12:43:12 +0100597 inline int TypeValue() const { return Bits(27, 25); }
Steve Blocka7e24c12009-10-30 11:49:00 +0000598
Steve Block1e0659c2011-05-24 12:43:12 +0100599 inline int RnValue() const { return Bits(19, 16); }
600 DECLARE_STATIC_ACCESSOR(RnValue);
601 inline int RdValue() const { return Bits(15, 12); }
602 DECLARE_STATIC_ACCESSOR(RdValue);
603
604 inline int CoprocessorValue() const { return Bits(11, 8); }
Steve Blockd0582a62009-12-15 09:54:21 +0000605 // Support for VFP.
606 // Vn(19-16) | Vd(15-12) | Vm(3-0)
Steve Block1e0659c2011-05-24 12:43:12 +0100607 inline int VnValue() const { return Bits(19, 16); }
608 inline int VmValue() const { return Bits(3, 0); }
609 inline int VdValue() const { return Bits(15, 12); }
610 inline int NValue() const { return Bit(7); }
611 inline int MValue() const { return Bit(5); }
612 inline int DValue() const { return Bit(22); }
613 inline int RtValue() const { return Bits(15, 12); }
614 inline int PValue() const { return Bit(24); }
615 inline int UValue() const { return Bit(23); }
616 inline int Opc1Value() const { return (Bit(23) << 2) | Bits(21, 20); }
617 inline int Opc2Value() const { return Bits(19, 16); }
618 inline int Opc3Value() const { return Bits(7, 6); }
619 inline int SzValue() const { return Bit(8); }
620 inline int VLValue() const { return Bit(20); }
621 inline int VCValue() const { return Bit(8); }
622 inline int VAValue() const { return Bits(23, 21); }
623 inline int VBValue() const { return Bits(6, 5); }
624 inline int VFPNRegValue(VFPRegPrecision pre) {
625 return VFPGlueRegValue(pre, 16, 7);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100626 }
Steve Block1e0659c2011-05-24 12:43:12 +0100627 inline int VFPMRegValue(VFPRegPrecision pre) {
628 return VFPGlueRegValue(pre, 0, 5);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100629 }
Steve Block1e0659c2011-05-24 12:43:12 +0100630 inline int VFPDRegValue(VFPRegPrecision pre) {
631 return VFPGlueRegValue(pre, 12, 22);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100632 }
Steve Blockd0582a62009-12-15 09:54:21 +0000633
Steve Blocka7e24c12009-10-30 11:49:00 +0000634 // Fields used in Data processing instructions
Steve Block1e0659c2011-05-24 12:43:12 +0100635 inline int OpcodeValue() const {
Steve Blocka7e24c12009-10-30 11:49:00 +0000636 return static_cast<Opcode>(Bits(24, 21));
637 }
Steve Block1e0659c2011-05-24 12:43:12 +0100638 inline Opcode OpcodeField() const {
639 return static_cast<Opcode>(BitField(24, 21));
640 }
641 inline int SValue() const { return Bit(20); }
Steve Blocka7e24c12009-10-30 11:49:00 +0000642 // with register
Steve Block1e0659c2011-05-24 12:43:12 +0100643 inline int RmValue() const { return Bits(3, 0); }
644 DECLARE_STATIC_ACCESSOR(RmValue);
645 inline int ShiftValue() const { return static_cast<ShiftOp>(Bits(6, 5)); }
646 inline ShiftOp ShiftField() const {
647 return static_cast<ShiftOp>(BitField(6, 5));
648 }
649 inline int RegShiftValue() const { return Bit(4); }
650 inline int RsValue() const { return Bits(11, 8); }
651 inline int ShiftAmountValue() const { return Bits(11, 7); }
Steve Blocka7e24c12009-10-30 11:49:00 +0000652 // with immediate
Steve Block1e0659c2011-05-24 12:43:12 +0100653 inline int RotateValue() const { return Bits(11, 8); }
654 inline int Immed8Value() const { return Bits(7, 0); }
655 inline int Immed4Value() const { return Bits(19, 16); }
656 inline int ImmedMovwMovtValue() const {
657 return Immed4Value() << 12 | Offset12Value(); }
Steve Blocka7e24c12009-10-30 11:49:00 +0000658
659 // Fields used in Load/Store instructions
Steve Block1e0659c2011-05-24 12:43:12 +0100660 inline int PUValue() const { return Bits(24, 23); }
661 inline int PUField() const { return BitField(24, 23); }
662 inline int BValue() const { return Bit(22); }
663 inline int WValue() const { return Bit(21); }
664 inline int LValue() const { return Bit(20); }
Steve Blocka7e24c12009-10-30 11:49:00 +0000665 // with register uses same fields as Data processing instructions above
666 // with immediate
Steve Block1e0659c2011-05-24 12:43:12 +0100667 inline int Offset12Value() const { return Bits(11, 0); }
Steve Blocka7e24c12009-10-30 11:49:00 +0000668 // multiple
Steve Block1e0659c2011-05-24 12:43:12 +0100669 inline int RlistValue() const { return Bits(15, 0); }
Steve Blocka7e24c12009-10-30 11:49:00 +0000670 // extra loads and stores
Steve Block1e0659c2011-05-24 12:43:12 +0100671 inline int SignValue() const { return Bit(6); }
672 inline int HValue() const { return Bit(5); }
673 inline int ImmedHValue() const { return Bits(11, 8); }
674 inline int ImmedLValue() const { return Bits(3, 0); }
Steve Blocka7e24c12009-10-30 11:49:00 +0000675
676 // Fields used in Branch instructions
Steve Block1e0659c2011-05-24 12:43:12 +0100677 inline int LinkValue() const { return Bit(24); }
678 inline int SImmed24Value() const { return ((InstructionBits() << 8) >> 8); }
Steve Blocka7e24c12009-10-30 11:49:00 +0000679
680 // Fields used in Software interrupt instructions
Steve Block1e0659c2011-05-24 12:43:12 +0100681 inline SoftwareInterruptCodes SvcValue() const {
Steve Blocka7e24c12009-10-30 11:49:00 +0000682 return static_cast<SoftwareInterruptCodes>(Bits(23, 0));
683 }
684
685 // Test for special encodings of type 0 instructions (extra loads and stores,
686 // as well as multiplications).
687 inline bool IsSpecialType0() const { return (Bit(7) == 1) && (Bit(4) == 1); }
688
Steve Block6ded16b2010-05-10 14:33:55 +0100689 // Test for miscellaneous instructions encodings of type 0 instructions.
690 inline bool IsMiscType0() const { return (Bit(24) == 1)
691 && (Bit(23) == 0)
692 && (Bit(20) == 0)
693 && ((Bit(7) == 0)); }
694
Steve Block1e0659c2011-05-24 12:43:12 +0100695 // Test for a stop instruction.
696 inline bool IsStop() const {
697 return (TypeValue() == 7) && (Bit(24) == 1) && (SvcValue() >= kStopCode);
698 }
699
Steve Blocka7e24c12009-10-30 11:49:00 +0000700 // Special accessors that test for existence of a value.
Steve Block1e0659c2011-05-24 12:43:12 +0100701 inline bool HasS() const { return SValue() == 1; }
702 inline bool HasB() const { return BValue() == 1; }
703 inline bool HasW() const { return WValue() == 1; }
704 inline bool HasL() const { return LValue() == 1; }
705 inline bool HasU() const { return UValue() == 1; }
706 inline bool HasSign() const { return SignValue() == 1; }
707 inline bool HasH() const { return HValue() == 1; }
708 inline bool HasLink() const { return LinkValue() == 1; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000709
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100710 // Decoding the double immediate in the vmov instruction.
711 double DoubleImmedVmov() const;
712
Steve Blocka7e24c12009-10-30 11:49:00 +0000713 // Instructions are read of out a code stream. The only way to get a
714 // reference to an instruction is to convert a pointer. There is no way
Steve Block1e0659c2011-05-24 12:43:12 +0100715 // to allocate or create instances of class Instruction.
716 // Use the At(pc) function to create references to Instruction.
717 static Instruction* At(byte* pc) {
718 return reinterpret_cast<Instruction*>(pc);
719 }
720
Steve Blocka7e24c12009-10-30 11:49:00 +0000721
722 private:
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100723 // Join split register codes, depending on single or double precision.
724 // four_bit is the position of the least-significant bit of the four
725 // bit specifier. one_bit is the position of the additional single bit
726 // specifier.
Steve Block1e0659c2011-05-24 12:43:12 +0100727 inline int VFPGlueRegValue(VFPRegPrecision pre, int four_bit, int one_bit) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100728 if (pre == kSinglePrecision) {
729 return (Bits(four_bit + 3, four_bit) << 1) | Bit(one_bit);
730 }
731 return (Bit(one_bit) << 4) | Bits(four_bit + 3, four_bit);
732 }
733
Steve Block1e0659c2011-05-24 12:43:12 +0100734 // We need to prevent the creation of instances of class Instruction.
735 DISALLOW_IMPLICIT_CONSTRUCTORS(Instruction);
Steve Blocka7e24c12009-10-30 11:49:00 +0000736};
737
738
739// Helper functions for converting between register numbers and names.
740class Registers {
741 public:
742 // Return the name of the register.
743 static const char* Name(int reg);
744
745 // Lookup the register number for the name provided.
746 static int Number(const char* name);
747
748 struct RegisterAlias {
749 int reg;
Steve Blockd0582a62009-12-15 09:54:21 +0000750 const char* name;
Steve Blocka7e24c12009-10-30 11:49:00 +0000751 };
752
753 private:
754 static const char* names_[kNumRegisters];
755 static const RegisterAlias aliases_[];
756};
757
Steve Blockd0582a62009-12-15 09:54:21 +0000758// Helper functions for converting between VFP register numbers and names.
759class VFPRegisters {
760 public:
761 // Return the name of the register.
Steve Block6ded16b2010-05-10 14:33:55 +0100762 static const char* Name(int reg, bool is_double);
763
764 // Lookup the register number for the name provided.
765 // Set flag pointed by is_double to true if register
766 // is double-precision.
767 static int Number(const char* name, bool* is_double);
Steve Blockd0582a62009-12-15 09:54:21 +0000768
769 private:
770 static const char* names_[kNumVFPRegisters];
771};
Steve Blocka7e24c12009-10-30 11:49:00 +0000772
773
Steve Block1e0659c2011-05-24 12:43:12 +0100774} } // namespace v8::internal
Steve Blocka7e24c12009-10-30 11:49:00 +0000775
776#endif // V8_ARM_CONSTANTS_ARM_H_