blob: 823c6ff7e1317795b10516a0d2350b156e06fd17 [file] [log] [blame]
Ben Murdoch8b112d22011-06-08 16:22:53 +01001// Copyright 2011 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
Ben Murdoch8b112d22011-06-08 16:22:53 +010031// ARM EABI is required.
32#if defined(__arm__) && !defined(__ARM_EABI__)
33#error ARM EABI support is required.
Steve Blocka7e24c12009-10-30 11:49:00 +000034#endif
35
36// This means that interwork-compatible jump instructions are generated. We
37// want to generate them on the simulator too so it makes snapshots that can
38// be used on real hardware.
39#if defined(__THUMB_INTERWORK__) || !defined(__arm__)
40# define USE_THUMB_INTERWORK 1
41#endif
42
Steve Blockd0582a62009-12-15 09:54:21 +000043#if defined(__ARM_ARCH_7A__) || \
44 defined(__ARM_ARCH_7R__) || \
Steve Blocka7e24c12009-10-30 11:49:00 +000045 defined(__ARM_ARCH_7__)
Steve Blockd0582a62009-12-15 09:54:21 +000046# define CAN_USE_ARMV7_INSTRUCTIONS 1
Steve Blocka7e24c12009-10-30 11:49:00 +000047#endif
48
Steve Blockd0582a62009-12-15 09:54:21 +000049#if defined(__ARM_ARCH_6__) || \
50 defined(__ARM_ARCH_6J__) || \
51 defined(__ARM_ARCH_6K__) || \
52 defined(__ARM_ARCH_6Z__) || \
53 defined(__ARM_ARCH_6ZK__) || \
54 defined(__ARM_ARCH_6T2__) || \
55 defined(CAN_USE_ARMV7_INSTRUCTIONS)
Steve Blocka7e24c12009-10-30 11:49:00 +000056# define CAN_USE_ARMV6_INSTRUCTIONS 1
57#endif
58
Steve Blockd0582a62009-12-15 09:54:21 +000059#if defined(__ARM_ARCH_5T__) || \
60 defined(__ARM_ARCH_5TE__) || \
61 defined(CAN_USE_ARMV6_INSTRUCTIONS)
62# define CAN_USE_ARMV5_INSTRUCTIONS 1
63# define CAN_USE_THUMB_INSTRUCTIONS 1
Steve Blocka7e24c12009-10-30 11:49:00 +000064#endif
65
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +010066// Simulator should support ARM5 instructions and unaligned access by default.
Steve Blocka7e24c12009-10-30 11:49:00 +000067#if !defined(__arm__)
68# define CAN_USE_ARMV5_INSTRUCTIONS 1
69# define CAN_USE_THUMB_INSTRUCTIONS 1
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +010070
71# ifndef CAN_USE_UNALIGNED_ACCESSES
72# define CAN_USE_UNALIGNED_ACCESSES 1
73# endif
74
Steve Blocka7e24c12009-10-30 11:49:00 +000075#endif
76
Kristian Monsen25f61362010-05-21 11:50:48 +010077#if CAN_USE_UNALIGNED_ACCESSES
78#define V8_TARGET_CAN_READ_UNALIGNED 1
79#endif
80
Steve Block6ded16b2010-05-10 14:33:55 +010081// Using blx may yield better code, so use it when required or when available
82#if defined(USE_THUMB_INTERWORK) || defined(CAN_USE_ARMV5_INSTRUCTIONS)
83#define USE_BLX 1
84#endif
85
Steve Block1e0659c2011-05-24 12:43:12 +010086namespace v8 {
87namespace internal {
Steve Blocka7e24c12009-10-30 11:49:00 +000088
Steve Block44f0eee2011-05-26 01:26:41 +010089// Constant pool marker.
Ben Murdoch85b71792012-04-11 18:30:58 +010090static const int kConstantPoolMarkerMask = 0xffe00000;
91static const int kConstantPoolMarker = 0x0c000000;
92static const int kConstantPoolLengthMask = 0x001ffff;
Steve Block44f0eee2011-05-26 01:26:41 +010093
Steve Blocka7e24c12009-10-30 11:49:00 +000094// Number of registers in normal ARM mode.
Ben Murdoch85b71792012-04-11 18:30:58 +010095static const int kNumRegisters = 16;
Steve Blocka7e24c12009-10-30 11:49:00 +000096
Steve Blockd0582a62009-12-15 09:54:21 +000097// VFP support.
Ben Murdoch85b71792012-04-11 18:30:58 +010098static const int kNumVFPSingleRegisters = 32;
99static const int kNumVFPDoubleRegisters = 16;
100static const int kNumVFPRegisters =
101 kNumVFPSingleRegisters + kNumVFPDoubleRegisters;
Steve Blockd0582a62009-12-15 09:54:21 +0000102
Steve Blocka7e24c12009-10-30 11:49:00 +0000103// PC is register 15.
Ben Murdoch85b71792012-04-11 18:30:58 +0100104static const int kPCRegister = 15;
105static const int kNoRegister = -1;
Steve Blocka7e24c12009-10-30 11:49:00 +0000106
Steve Block1e0659c2011-05-24 12:43:12 +0100107// -----------------------------------------------------------------------------
108// Conditions.
109
Steve Blocka7e24c12009-10-30 11:49:00 +0000110// Defines constants and accessor classes to assemble, disassemble and
111// simulate ARM instructions.
112//
113// Section references in the code refer to the "ARM Architecture Reference
114// Manual" from July 2005 (available at http://www.arm.com/miscPDFs/14128.pdf)
115//
116// Constants for specific fields are defined in their respective named enums.
117// General constants are in an anonymous enum in class Instr.
118
Steve Blocka7e24c12009-10-30 11:49:00 +0000119// Values for the condition field as defined in section A3.2
120enum Condition {
Steve Block1e0659c2011-05-24 12:43:12 +0100121 kNoCondition = -1,
122
123 eq = 0 << 28, // Z set Equal.
124 ne = 1 << 28, // Z clear Not equal.
125 cs = 2 << 28, // C set Unsigned higher or same.
126 cc = 3 << 28, // C clear Unsigned lower.
127 mi = 4 << 28, // N set Negative.
128 pl = 5 << 28, // N clear Positive or zero.
129 vs = 6 << 28, // V set Overflow.
130 vc = 7 << 28, // V clear No overflow.
131 hi = 8 << 28, // C set, Z clear Unsigned higher.
132 ls = 9 << 28, // C clear or Z set Unsigned lower or same.
133 ge = 10 << 28, // N == V Greater or equal.
134 lt = 11 << 28, // N != V Less than.
135 gt = 12 << 28, // Z clear, N == V Greater than.
136 le = 13 << 28, // Z set or N != V Less then or equal
137 al = 14 << 28, // Always.
138
139 kSpecialCondition = 15 << 28, // Special condition (refer to section A3.2.1).
140 kNumberOfConditions = 16,
141
142 // Aliases.
143 hs = cs, // C set Unsigned higher or same.
144 lo = cc // C clear Unsigned lower.
Steve Blocka7e24c12009-10-30 11:49:00 +0000145};
146
147
Steve Block1e0659c2011-05-24 12:43:12 +0100148inline Condition NegateCondition(Condition cond) {
149 ASSERT(cond != al);
150 return static_cast<Condition>(cond ^ ne);
151}
152
153
154// Corresponds to transposing the operands of a comparison.
155inline Condition ReverseCondition(Condition cond) {
156 switch (cond) {
157 case lo:
158 return hi;
159 case hi:
160 return lo;
161 case hs:
162 return ls;
163 case ls:
164 return hs;
165 case lt:
166 return gt;
167 case gt:
168 return lt;
169 case ge:
170 return le;
171 case le:
172 return ge;
173 default:
174 return cond;
175 };
176}
177
178
179// -----------------------------------------------------------------------------
180// Instructions encoding.
181
182// Instr is merely used by the Assembler to distinguish 32bit integers
183// representing instructions from usual 32 bit values.
184// Instruction objects are pointers to 32bit values, and provide methods to
185// access the various ISA fields.
186typedef int32_t Instr;
187
188
Steve Blocka7e24c12009-10-30 11:49:00 +0000189// Opcodes for Data-processing instructions (instructions with a type 0 and 1)
190// as defined in section A3.4
191enum Opcode {
Steve Block1e0659c2011-05-24 12:43:12 +0100192 AND = 0 << 21, // Logical AND.
193 EOR = 1 << 21, // Logical Exclusive OR.
194 SUB = 2 << 21, // Subtract.
195 RSB = 3 << 21, // Reverse Subtract.
196 ADD = 4 << 21, // Add.
197 ADC = 5 << 21, // Add with Carry.
198 SBC = 6 << 21, // Subtract with Carry.
199 RSC = 7 << 21, // Reverse Subtract with Carry.
200 TST = 8 << 21, // Test.
201 TEQ = 9 << 21, // Test Equivalence.
202 CMP = 10 << 21, // Compare.
203 CMN = 11 << 21, // Compare Negated.
204 ORR = 12 << 21, // Logical (inclusive) OR.
205 MOV = 13 << 21, // Move.
206 BIC = 14 << 21, // Bit Clear.
207 MVN = 15 << 21 // Move Not.
Steve Blocka7e24c12009-10-30 11:49:00 +0000208};
209
210
Steve Block6ded16b2010-05-10 14:33:55 +0100211// The bits for bit 7-4 for some type 0 miscellaneous instructions.
212enum MiscInstructionsBits74 {
213 // With bits 22-21 01.
Steve Block1e0659c2011-05-24 12:43:12 +0100214 BX = 1 << 4,
215 BXJ = 2 << 4,
216 BLX = 3 << 4,
217 BKPT = 7 << 4,
Steve Blocka7e24c12009-10-30 11:49:00 +0000218
Steve Block6ded16b2010-05-10 14:33:55 +0100219 // With bits 22-21 11.
Steve Block1e0659c2011-05-24 12:43:12 +0100220 CLZ = 1 << 4
221};
222
223
224// Instruction encoding bits and masks.
225enum {
226 H = 1 << 5, // Halfword (or byte).
227 S6 = 1 << 6, // Signed (or unsigned).
228 L = 1 << 20, // Load (or store).
229 S = 1 << 20, // Set condition code (or leave unchanged).
230 W = 1 << 21, // Writeback base register (or leave unchanged).
231 A = 1 << 21, // Accumulate in multiply instruction (or not).
232 B = 1 << 22, // Unsigned byte (or word).
233 N = 1 << 22, // Long (or short).
234 U = 1 << 23, // Positive (or negative) offset/index.
235 P = 1 << 24, // Offset/pre-indexed addressing (or post-indexed addressing).
236 I = 1 << 25, // Immediate shifter operand (or not).
237
238 B4 = 1 << 4,
239 B5 = 1 << 5,
240 B6 = 1 << 6,
241 B7 = 1 << 7,
242 B8 = 1 << 8,
243 B9 = 1 << 9,
244 B12 = 1 << 12,
245 B16 = 1 << 16,
246 B18 = 1 << 18,
247 B19 = 1 << 19,
248 B20 = 1 << 20,
249 B21 = 1 << 21,
250 B22 = 1 << 22,
251 B23 = 1 << 23,
252 B24 = 1 << 24,
253 B25 = 1 << 25,
254 B26 = 1 << 26,
255 B27 = 1 << 27,
256 B28 = 1 << 28,
257
258 // Instruction bit masks.
259 kCondMask = 15 << 28,
260 kALUMask = 0x6f << 21,
261 kRdMask = 15 << 12, // In str instruction.
262 kCoprocessorMask = 15 << 8,
263 kOpCodeMask = 15 << 21, // In data-processing instructions.
264 kImm24Mask = (1 << 24) - 1,
265 kOff12Mask = (1 << 12) - 1
266};
267
268
269// -----------------------------------------------------------------------------
270// Addressing modes and instruction variants.
271
272// Condition code updating mode.
273enum SBit {
274 SetCC = 1 << 20, // Set condition code.
275 LeaveCC = 0 << 20 // Leave condition code unchanged.
276};
277
278
279// Status register selection.
280enum SRegister {
281 CPSR = 0 << 22,
282 SPSR = 1 << 22
Steve Blocka7e24c12009-10-30 11:49:00 +0000283};
284
285
Steve Blocka7e24c12009-10-30 11:49:00 +0000286// Shifter types for Data-processing operands as defined in section A5.1.2.
Steve Block1e0659c2011-05-24 12:43:12 +0100287enum ShiftOp {
288 LSL = 0 << 5, // Logical shift left.
289 LSR = 1 << 5, // Logical shift right.
290 ASR = 2 << 5, // Arithmetic shift right.
291 ROR = 3 << 5, // Rotate right.
292
293 // RRX is encoded as ROR with shift_imm == 0.
294 // Use a special code to make the distinction. The RRX ShiftOp is only used
295 // as an argument, and will never actually be encoded. The Assembler will
296 // detect it and emit the correct ROR shift operand with shift_imm == 0.
297 RRX = -1,
298 kNumberOfShifts = 4
Steve Blocka7e24c12009-10-30 11:49:00 +0000299};
300
301
Steve Block1e0659c2011-05-24 12:43:12 +0100302// Status register fields.
303enum SRegisterField {
304 CPSR_c = CPSR | 1 << 16,
305 CPSR_x = CPSR | 1 << 17,
306 CPSR_s = CPSR | 1 << 18,
307 CPSR_f = CPSR | 1 << 19,
308 SPSR_c = SPSR | 1 << 16,
309 SPSR_x = SPSR | 1 << 17,
310 SPSR_s = SPSR | 1 << 18,
311 SPSR_f = SPSR | 1 << 19
312};
313
314// Status register field mask (or'ed SRegisterField enum values).
315typedef uint32_t SRegisterFieldMask;
316
317
318// Memory operand addressing mode.
319enum AddrMode {
320 // Bit encoding P U W.
321 Offset = (8|4|0) << 21, // Offset (without writeback to base).
322 PreIndex = (8|4|1) << 21, // Pre-indexed addressing with writeback.
323 PostIndex = (0|4|0) << 21, // Post-indexed addressing with writeback.
324 NegOffset = (8|0|0) << 21, // Negative offset (without writeback to base).
325 NegPreIndex = (8|0|1) << 21, // Negative pre-indexed with writeback.
326 NegPostIndex = (0|0|0) << 21 // Negative post-indexed with writeback.
327};
328
329
330// Load/store multiple addressing mode.
331enum BlockAddrMode {
332 // Bit encoding P U W .
333 da = (0|0|0) << 21, // Decrement after.
334 ia = (0|4|0) << 21, // Increment after.
335 db = (8|0|0) << 21, // Decrement before.
336 ib = (8|4|0) << 21, // Increment before.
337 da_w = (0|0|1) << 21, // Decrement after with writeback to base.
338 ia_w = (0|4|1) << 21, // Increment after with writeback to base.
339 db_w = (8|0|1) << 21, // Decrement before with writeback to base.
340 ib_w = (8|4|1) << 21, // Increment before with writeback to base.
341
342 // Alias modes for comparison when writeback does not matter.
343 da_x = (0|0|0) << 21, // Decrement after.
344 ia_x = (0|4|0) << 21, // Increment after.
345 db_x = (8|0|0) << 21, // Decrement before.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100346 ib_x = (8|4|0) << 21, // Increment before.
347
348 kBlockAddrModeMask = (8|4|1) << 21
Steve Block1e0659c2011-05-24 12:43:12 +0100349};
350
351
352// Coprocessor load/store operand size.
353enum LFlag {
354 Long = 1 << 22, // Long load/store coprocessor.
355 Short = 0 << 22 // Short load/store coprocessor.
356};
357
358
359// -----------------------------------------------------------------------------
360// Supervisor Call (svc) specific support.
361
Steve Blocka7e24c12009-10-30 11:49:00 +0000362// Special Software Interrupt codes when used in the presence of the ARM
363// simulator.
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800364// svc (formerly swi) provides a 24bit immediate value. Use bits 22:0 for
365// standard SoftwareInterrupCode. Bit 23 is reserved for the stop feature.
Steve Blocka7e24c12009-10-30 11:49:00 +0000366enum SoftwareInterruptCodes {
367 // transition to C code
Steve Block1e0659c2011-05-24 12:43:12 +0100368 kCallRtRedirected= 0x10,
Steve Blocka7e24c12009-10-30 11:49:00 +0000369 // break point
Steve Block1e0659c2011-05-24 12:43:12 +0100370 kBreakpoint= 0x20,
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800371 // stop
Steve Block1e0659c2011-05-24 12:43:12 +0100372 kStopCode = 1 << 23
Steve Blocka7e24c12009-10-30 11:49:00 +0000373};
Ben Murdoch85b71792012-04-11 18:30:58 +0100374static const uint32_t kStopCodeMask = kStopCode - 1;
375static const uint32_t kMaxStopCode = kStopCode - 1;
376static const int32_t kDefaultStopCode = -1;
Steve Blocka7e24c12009-10-30 11:49:00 +0000377
378
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100379// Type of VFP register. Determines register encoding.
380enum VFPRegPrecision {
381 kSinglePrecision = 0,
382 kDoublePrecision = 1
383};
384
Steve Block1e0659c2011-05-24 12:43:12 +0100385
386// VFP FPSCR constants.
387enum VFPConversionMode {
388 kFPSCRRounding = 0,
389 kDefaultRoundToZero = 1
Russell Brenner90bac252010-11-18 13:33:46 -0800390};
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100391
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100392// This mask does not include the "inexact" or "input denormal" cumulative
393// exceptions flags, because we usually don't want to check for it.
Ben Murdoch85b71792012-04-11 18:30:58 +0100394static const uint32_t kVFPExceptionMask = 0xf;
395static const uint32_t kVFPInvalidOpExceptionBit = 1 << 0;
396static const uint32_t kVFPOverflowExceptionBit = 1 << 2;
397static const uint32_t kVFPUnderflowExceptionBit = 1 << 3;
398static const uint32_t kVFPInexactExceptionBit = 1 << 4;
399static const uint32_t kVFPFlushToZeroMask = 1 << 24;
Steve Block1e0659c2011-05-24 12:43:12 +0100400
Ben Murdoch85b71792012-04-11 18:30:58 +0100401static const uint32_t kVFPNConditionFlagBit = 1 << 31;
402static const uint32_t kVFPZConditionFlagBit = 1 << 30;
403static const uint32_t kVFPCConditionFlagBit = 1 << 29;
404static const uint32_t kVFPVConditionFlagBit = 1 << 28;
Steve Blocka7e24c12009-10-30 11:49:00 +0000405
406
Steve Block1e0659c2011-05-24 12:43:12 +0100407// VFP rounding modes. See ARM DDI 0406B Page A2-29.
408enum VFPRoundingMode {
409 RN = 0 << 22, // Round to Nearest.
410 RP = 1 << 22, // Round towards Plus Infinity.
411 RM = 2 << 22, // Round towards Minus Infinity.
412 RZ = 3 << 22, // Round towards zero.
413
414 // Aliases.
415 kRoundToNearest = RN,
416 kRoundToPlusInf = RP,
417 kRoundToMinusInf = RM,
418 kRoundToZero = RZ
419};
420
Ben Murdoch85b71792012-04-11 18:30:58 +0100421static const uint32_t kVFPRoundingModeMask = 3 << 22;
Steve Block1e0659c2011-05-24 12:43:12 +0100422
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100423enum CheckForInexactConversion {
424 kCheckForInexactConversion,
425 kDontCheckForInexactConversion
426};
427
Steve Block1e0659c2011-05-24 12:43:12 +0100428// -----------------------------------------------------------------------------
429// Hints.
430
431// Branch hints are not used on the ARM. They are defined so that they can
432// appear in shared function signatures, but will be ignored in ARM
433// implementations.
434enum Hint { no_hint };
435
436// Hints are not used on the arm. Negating is trivial.
437inline Hint NegateHint(Hint ignored) { return no_hint; }
438
439
440// -----------------------------------------------------------------------------
441// Specific instructions, constants, and masks.
442// These constants are declared in assembler-arm.cc, as they use named registers
443// and other constants.
444
445
446// add(sp, sp, 4) instruction (aka Pop())
447extern const Instr kPopInstruction;
448
449// str(r, MemOperand(sp, 4, NegPreIndex), al) instruction (aka push(r))
450// register r is not encoded.
451extern const Instr kPushRegPattern;
452
453// ldr(r, MemOperand(sp, 4, PostIndex), al) instruction (aka pop(r))
454// register r is not encoded.
455extern const Instr kPopRegPattern;
456
457// mov lr, pc
458extern const Instr kMovLrPc;
459// ldr rd, [pc, #offset]
460extern const Instr kLdrPCMask;
461extern const Instr kLdrPCPattern;
462// blxcc rm
463extern const Instr kBlxRegMask;
464
465extern const Instr kBlxRegPattern;
466
467extern const Instr kMovMvnMask;
468extern const Instr kMovMvnPattern;
469extern const Instr kMovMvnFlip;
470extern const Instr kMovLeaveCCMask;
471extern const Instr kMovLeaveCCPattern;
472extern const Instr kMovwMask;
473extern const Instr kMovwPattern;
474extern const Instr kMovwLeaveCCFlip;
475extern const Instr kCmpCmnMask;
476extern const Instr kCmpCmnPattern;
477extern const Instr kCmpCmnFlip;
478extern const Instr kAddSubFlip;
479extern const Instr kAndBicFlip;
480
481// A mask for the Rd register for push, pop, ldr, str instructions.
482extern const Instr kLdrRegFpOffsetPattern;
483
484extern const Instr kStrRegFpOffsetPattern;
485
486extern const Instr kLdrRegFpNegOffsetPattern;
487
488extern const Instr kStrRegFpNegOffsetPattern;
489
490extern const Instr kLdrStrInstrTypeMask;
491extern const Instr kLdrStrInstrArgumentMask;
492extern const Instr kLdrStrOffsetMask;
493
494
495// -----------------------------------------------------------------------------
496// Instruction abstraction.
497
498// The class Instruction enables access to individual fields defined in the ARM
Steve Blocka7e24c12009-10-30 11:49:00 +0000499// architecture instruction set encoding as described in figure A3-1.
Steve Block1e0659c2011-05-24 12:43:12 +0100500// Note that the Assembler uses typedef int32_t Instr.
Steve Blocka7e24c12009-10-30 11:49:00 +0000501//
502// Example: Test whether the instruction at ptr does set the condition code
503// bits.
504//
505// bool InstructionSetsConditionCodes(byte* ptr) {
Steve Block1e0659c2011-05-24 12:43:12 +0100506// Instruction* instr = Instruction::At(ptr);
507// int type = instr->TypeValue();
Steve Blocka7e24c12009-10-30 11:49:00 +0000508// return ((type == 0) || (type == 1)) && instr->HasS();
509// }
510//
Steve Block1e0659c2011-05-24 12:43:12 +0100511class Instruction {
Steve Blocka7e24c12009-10-30 11:49:00 +0000512 public:
513 enum {
514 kInstrSize = 4,
515 kInstrSizeLog2 = 2,
516 kPCReadOffset = 8
517 };
518
Steve Block1e0659c2011-05-24 12:43:12 +0100519 // Helper macro to define static accessors.
520 // We use the cast to char* trick to bypass the strict anti-aliasing rules.
521 #define DECLARE_STATIC_TYPED_ACCESSOR(return_type, Name) \
522 static inline return_type Name(Instr instr) { \
523 char* temp = reinterpret_cast<char*>(&instr); \
524 return reinterpret_cast<Instruction*>(temp)->Name(); \
525 }
526
527 #define DECLARE_STATIC_ACCESSOR(Name) DECLARE_STATIC_TYPED_ACCESSOR(int, Name)
528
Steve Blocka7e24c12009-10-30 11:49:00 +0000529 // Get the raw instruction bits.
Steve Block1e0659c2011-05-24 12:43:12 +0100530 inline Instr InstructionBits() const {
531 return *reinterpret_cast<const Instr*>(this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000532 }
533
534 // Set the raw instruction bits to value.
Steve Block1e0659c2011-05-24 12:43:12 +0100535 inline void SetInstructionBits(Instr value) {
536 *reinterpret_cast<Instr*>(this) = value;
Steve Blocka7e24c12009-10-30 11:49:00 +0000537 }
538
539 // Read one particular bit out of the instruction bits.
540 inline int Bit(int nr) const {
541 return (InstructionBits() >> nr) & 1;
542 }
543
Steve Block1e0659c2011-05-24 12:43:12 +0100544 // Read a bit field's value out of the instruction bits.
Steve Blocka7e24c12009-10-30 11:49:00 +0000545 inline int Bits(int hi, int lo) const {
546 return (InstructionBits() >> lo) & ((2 << (hi - lo)) - 1);
547 }
548
Steve Block1e0659c2011-05-24 12:43:12 +0100549 // Read a bit field out of the instruction bits.
550 inline int BitField(int hi, int lo) const {
551 return InstructionBits() & (((2 << (hi - lo)) - 1) << lo);
552 }
553
554 // Static support.
555
556 // Read one particular bit out of the instruction bits.
557 static inline int Bit(Instr instr, int nr) {
558 return (instr >> nr) & 1;
559 }
560
561 // Read the value of a bit field out of the instruction bits.
562 static inline int Bits(Instr instr, int hi, int lo) {
563 return (instr >> lo) & ((2 << (hi - lo)) - 1);
564 }
565
566
567 // Read a bit field out of the instruction bits.
568 static inline int BitField(Instr instr, int hi, int lo) {
569 return instr & (((2 << (hi - lo)) - 1) << lo);
570 }
571
Steve Blocka7e24c12009-10-30 11:49:00 +0000572
573 // Accessors for the different named fields used in the ARM encoding.
574 // The naming of these accessor corresponds to figure A3-1.
Steve Block1e0659c2011-05-24 12:43:12 +0100575 //
576 // Two kind of accessors are declared:
Ben Murdoch85b71792012-04-11 18:30:58 +0100577 // - <Name>Field() will return the raw field, ie the field's bits at their
Steve Block1e0659c2011-05-24 12:43:12 +0100578 // original place in the instruction encoding.
Ben Murdoch85b71792012-04-11 18:30:58 +0100579 // eg. if instr is the 'addgt r0, r1, r2' instruction, encoded as 0xC0810002
580 // ConditionField(instr) will return 0xC0000000.
Steve Block1e0659c2011-05-24 12:43:12 +0100581 // - <Name>Value() will return the field value, shifted back to bit 0.
Ben Murdoch85b71792012-04-11 18:30:58 +0100582 // eg. if instr is the 'addgt r0, r1, r2' instruction, encoded as 0xC0810002
583 // ConditionField(instr) will return 0xC.
Steve Block1e0659c2011-05-24 12:43:12 +0100584
585
Steve Blocka7e24c12009-10-30 11:49:00 +0000586 // Generally applicable fields
Steve Block1e0659c2011-05-24 12:43:12 +0100587 inline Condition ConditionValue() const {
Steve Blocka7e24c12009-10-30 11:49:00 +0000588 return static_cast<Condition>(Bits(31, 28));
589 }
Steve Block1e0659c2011-05-24 12:43:12 +0100590 inline Condition ConditionField() const {
591 return static_cast<Condition>(BitField(31, 28));
592 }
593 DECLARE_STATIC_TYPED_ACCESSOR(Condition, ConditionValue);
594 DECLARE_STATIC_TYPED_ACCESSOR(Condition, ConditionField);
Steve Blocka7e24c12009-10-30 11:49:00 +0000595
Steve Block1e0659c2011-05-24 12:43:12 +0100596 inline int TypeValue() const { return Bits(27, 25); }
Steve Blocka7e24c12009-10-30 11:49:00 +0000597
Steve Block1e0659c2011-05-24 12:43:12 +0100598 inline int RnValue() const { return Bits(19, 16); }
599 DECLARE_STATIC_ACCESSOR(RnValue);
600 inline int RdValue() const { return Bits(15, 12); }
601 DECLARE_STATIC_ACCESSOR(RdValue);
602
603 inline int CoprocessorValue() const { return Bits(11, 8); }
Steve Blockd0582a62009-12-15 09:54:21 +0000604 // Support for VFP.
605 // Vn(19-16) | Vd(15-12) | Vm(3-0)
Steve Block1e0659c2011-05-24 12:43:12 +0100606 inline int VnValue() const { return Bits(19, 16); }
607 inline int VmValue() const { return Bits(3, 0); }
608 inline int VdValue() const { return Bits(15, 12); }
609 inline int NValue() const { return Bit(7); }
610 inline int MValue() const { return Bit(5); }
611 inline int DValue() const { return Bit(22); }
612 inline int RtValue() const { return Bits(15, 12); }
613 inline int PValue() const { return Bit(24); }
614 inline int UValue() const { return Bit(23); }
615 inline int Opc1Value() const { return (Bit(23) << 2) | Bits(21, 20); }
616 inline int Opc2Value() const { return Bits(19, 16); }
617 inline int Opc3Value() const { return Bits(7, 6); }
618 inline int SzValue() const { return Bit(8); }
619 inline int VLValue() const { return Bit(20); }
620 inline int VCValue() const { return Bit(8); }
621 inline int VAValue() const { return Bits(23, 21); }
622 inline int VBValue() const { return Bits(6, 5); }
623 inline int VFPNRegValue(VFPRegPrecision pre) {
624 return VFPGlueRegValue(pre, 16, 7);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100625 }
Steve Block1e0659c2011-05-24 12:43:12 +0100626 inline int VFPMRegValue(VFPRegPrecision pre) {
627 return VFPGlueRegValue(pre, 0, 5);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100628 }
Steve Block1e0659c2011-05-24 12:43:12 +0100629 inline int VFPDRegValue(VFPRegPrecision pre) {
630 return VFPGlueRegValue(pre, 12, 22);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100631 }
Steve Blockd0582a62009-12-15 09:54:21 +0000632
Steve Blocka7e24c12009-10-30 11:49:00 +0000633 // Fields used in Data processing instructions
Steve Block1e0659c2011-05-24 12:43:12 +0100634 inline int OpcodeValue() const {
Steve Blocka7e24c12009-10-30 11:49:00 +0000635 return static_cast<Opcode>(Bits(24, 21));
636 }
Steve Block1e0659c2011-05-24 12:43:12 +0100637 inline Opcode OpcodeField() const {
638 return static_cast<Opcode>(BitField(24, 21));
639 }
640 inline int SValue() const { return Bit(20); }
Steve Blocka7e24c12009-10-30 11:49:00 +0000641 // with register
Steve Block1e0659c2011-05-24 12:43:12 +0100642 inline int RmValue() const { return Bits(3, 0); }
643 DECLARE_STATIC_ACCESSOR(RmValue);
644 inline int ShiftValue() const { return static_cast<ShiftOp>(Bits(6, 5)); }
645 inline ShiftOp ShiftField() const {
646 return static_cast<ShiftOp>(BitField(6, 5));
647 }
648 inline int RegShiftValue() const { return Bit(4); }
649 inline int RsValue() const { return Bits(11, 8); }
650 inline int ShiftAmountValue() const { return Bits(11, 7); }
Steve Blocka7e24c12009-10-30 11:49:00 +0000651 // with immediate
Steve Block1e0659c2011-05-24 12:43:12 +0100652 inline int RotateValue() const { return Bits(11, 8); }
653 inline int Immed8Value() const { return Bits(7, 0); }
654 inline int Immed4Value() const { return Bits(19, 16); }
655 inline int ImmedMovwMovtValue() const {
656 return Immed4Value() << 12 | Offset12Value(); }
Steve Blocka7e24c12009-10-30 11:49:00 +0000657
658 // Fields used in Load/Store instructions
Steve Block1e0659c2011-05-24 12:43:12 +0100659 inline int PUValue() const { return Bits(24, 23); }
660 inline int PUField() const { return BitField(24, 23); }
661 inline int BValue() const { return Bit(22); }
662 inline int WValue() const { return Bit(21); }
663 inline int LValue() const { return Bit(20); }
Steve Blocka7e24c12009-10-30 11:49:00 +0000664 // with register uses same fields as Data processing instructions above
665 // with immediate
Steve Block1e0659c2011-05-24 12:43:12 +0100666 inline int Offset12Value() const { return Bits(11, 0); }
Steve Blocka7e24c12009-10-30 11:49:00 +0000667 // multiple
Steve Block1e0659c2011-05-24 12:43:12 +0100668 inline int RlistValue() const { return Bits(15, 0); }
Steve Blocka7e24c12009-10-30 11:49:00 +0000669 // extra loads and stores
Steve Block1e0659c2011-05-24 12:43:12 +0100670 inline int SignValue() const { return Bit(6); }
671 inline int HValue() const { return Bit(5); }
672 inline int ImmedHValue() const { return Bits(11, 8); }
673 inline int ImmedLValue() const { return Bits(3, 0); }
Steve Blocka7e24c12009-10-30 11:49:00 +0000674
675 // Fields used in Branch instructions
Steve Block1e0659c2011-05-24 12:43:12 +0100676 inline int LinkValue() const { return Bit(24); }
677 inline int SImmed24Value() const { return ((InstructionBits() << 8) >> 8); }
Steve Blocka7e24c12009-10-30 11:49:00 +0000678
679 // Fields used in Software interrupt instructions
Steve Block1e0659c2011-05-24 12:43:12 +0100680 inline SoftwareInterruptCodes SvcValue() const {
Steve Blocka7e24c12009-10-30 11:49:00 +0000681 return static_cast<SoftwareInterruptCodes>(Bits(23, 0));
682 }
683
684 // Test for special encodings of type 0 instructions (extra loads and stores,
685 // as well as multiplications).
686 inline bool IsSpecialType0() const { return (Bit(7) == 1) && (Bit(4) == 1); }
687
Steve Block6ded16b2010-05-10 14:33:55 +0100688 // Test for miscellaneous instructions encodings of type 0 instructions.
689 inline bool IsMiscType0() const { return (Bit(24) == 1)
690 && (Bit(23) == 0)
691 && (Bit(20) == 0)
692 && ((Bit(7) == 0)); }
693
Steve Block1e0659c2011-05-24 12:43:12 +0100694 // Test for a stop instruction.
695 inline bool IsStop() const {
696 return (TypeValue() == 7) && (Bit(24) == 1) && (SvcValue() >= kStopCode);
697 }
698
Steve Blocka7e24c12009-10-30 11:49:00 +0000699 // Special accessors that test for existence of a value.
Steve Block1e0659c2011-05-24 12:43:12 +0100700 inline bool HasS() const { return SValue() == 1; }
701 inline bool HasB() const { return BValue() == 1; }
702 inline bool HasW() const { return WValue() == 1; }
703 inline bool HasL() const { return LValue() == 1; }
704 inline bool HasU() const { return UValue() == 1; }
705 inline bool HasSign() const { return SignValue() == 1; }
706 inline bool HasH() const { return HValue() == 1; }
707 inline bool HasLink() const { return LinkValue() == 1; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000708
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100709 // Decoding the double immediate in the vmov instruction.
710 double DoubleImmedVmov() const;
711
Steve Blocka7e24c12009-10-30 11:49:00 +0000712 // Instructions are read of out a code stream. The only way to get a
713 // reference to an instruction is to convert a pointer. There is no way
Steve Block1e0659c2011-05-24 12:43:12 +0100714 // to allocate or create instances of class Instruction.
715 // Use the At(pc) function to create references to Instruction.
716 static Instruction* At(byte* pc) {
717 return reinterpret_cast<Instruction*>(pc);
718 }
719
Steve Blocka7e24c12009-10-30 11:49:00 +0000720
721 private:
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100722 // Join split register codes, depending on single or double precision.
723 // four_bit is the position of the least-significant bit of the four
724 // bit specifier. one_bit is the position of the additional single bit
725 // specifier.
Steve Block1e0659c2011-05-24 12:43:12 +0100726 inline int VFPGlueRegValue(VFPRegPrecision pre, int four_bit, int one_bit) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100727 if (pre == kSinglePrecision) {
728 return (Bits(four_bit + 3, four_bit) << 1) | Bit(one_bit);
729 }
730 return (Bit(one_bit) << 4) | Bits(four_bit + 3, four_bit);
731 }
732
Steve Block1e0659c2011-05-24 12:43:12 +0100733 // We need to prevent the creation of instances of class Instruction.
734 DISALLOW_IMPLICIT_CONSTRUCTORS(Instruction);
Steve Blocka7e24c12009-10-30 11:49:00 +0000735};
736
737
738// Helper functions for converting between register numbers and names.
739class Registers {
740 public:
741 // Return the name of the register.
742 static const char* Name(int reg);
743
744 // Lookup the register number for the name provided.
745 static int Number(const char* name);
746
747 struct RegisterAlias {
748 int reg;
Steve Blockd0582a62009-12-15 09:54:21 +0000749 const char* name;
Steve Blocka7e24c12009-10-30 11:49:00 +0000750 };
751
752 private:
753 static const char* names_[kNumRegisters];
754 static const RegisterAlias aliases_[];
755};
756
Steve Blockd0582a62009-12-15 09:54:21 +0000757// Helper functions for converting between VFP register numbers and names.
758class VFPRegisters {
759 public:
760 // Return the name of the register.
Steve Block6ded16b2010-05-10 14:33:55 +0100761 static const char* Name(int reg, bool is_double);
762
763 // Lookup the register number for the name provided.
764 // Set flag pointed by is_double to true if register
765 // is double-precision.
766 static int Number(const char* name, bool* is_double);
Steve Blockd0582a62009-12-15 09:54:21 +0000767
768 private:
769 static const char* names_[kNumVFPRegisters];
770};
Steve Blocka7e24c12009-10-30 11:49:00 +0000771
772
Steve Block1e0659c2011-05-24 12:43:12 +0100773} } // namespace v8::internal
Steve Blocka7e24c12009-10-30 11:49:00 +0000774
775#endif // V8_ARM_CONSTANTS_ARM_H_