blob: e767001e1d8b3f6697720320b85b800bade3da95 [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 Murdoch3ef787d2012-04-12 10:51:47 +010090const int kConstantPoolMarkerMask = 0xffe00000;
91const int kConstantPoolMarker = 0x0c000000;
92const 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 Murdoch3ef787d2012-04-12 10:51:47 +010095const int kNumRegisters = 16;
Steve Blocka7e24c12009-10-30 11:49:00 +000096
Steve Blockd0582a62009-12-15 09:54:21 +000097// VFP support.
Ben Murdoch3ef787d2012-04-12 10:51:47 +010098const int kNumVFPSingleRegisters = 32;
99const int kNumVFPDoubleRegisters = 16;
100const int kNumVFPRegisters = kNumVFPSingleRegisters + kNumVFPDoubleRegisters;
Steve Blockd0582a62009-12-15 09:54:21 +0000101
Steve Blocka7e24c12009-10-30 11:49:00 +0000102// PC is register 15.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100103const int kPCRegister = 15;
104const int kNoRegister = -1;
Steve Blocka7e24c12009-10-30 11:49:00 +0000105
Steve Block1e0659c2011-05-24 12:43:12 +0100106// -----------------------------------------------------------------------------
107// Conditions.
108
Steve Blocka7e24c12009-10-30 11:49:00 +0000109// Defines constants and accessor classes to assemble, disassemble and
110// simulate ARM instructions.
111//
112// Section references in the code refer to the "ARM Architecture Reference
113// Manual" from July 2005 (available at http://www.arm.com/miscPDFs/14128.pdf)
114//
115// Constants for specific fields are defined in their respective named enums.
116// General constants are in an anonymous enum in class Instr.
117
Steve Blocka7e24c12009-10-30 11:49:00 +0000118// Values for the condition field as defined in section A3.2
119enum Condition {
Steve Block1e0659c2011-05-24 12:43:12 +0100120 kNoCondition = -1,
121
122 eq = 0 << 28, // Z set Equal.
123 ne = 1 << 28, // Z clear Not equal.
124 cs = 2 << 28, // C set Unsigned higher or same.
125 cc = 3 << 28, // C clear Unsigned lower.
126 mi = 4 << 28, // N set Negative.
127 pl = 5 << 28, // N clear Positive or zero.
128 vs = 6 << 28, // V set Overflow.
129 vc = 7 << 28, // V clear No overflow.
130 hi = 8 << 28, // C set, Z clear Unsigned higher.
131 ls = 9 << 28, // C clear or Z set Unsigned lower or same.
132 ge = 10 << 28, // N == V Greater or equal.
133 lt = 11 << 28, // N != V Less than.
134 gt = 12 << 28, // Z clear, N == V Greater than.
135 le = 13 << 28, // Z set or N != V Less then or equal
136 al = 14 << 28, // Always.
137
138 kSpecialCondition = 15 << 28, // Special condition (refer to section A3.2.1).
139 kNumberOfConditions = 16,
140
141 // Aliases.
142 hs = cs, // C set Unsigned higher or same.
143 lo = cc // C clear Unsigned lower.
Steve Blocka7e24c12009-10-30 11:49:00 +0000144};
145
146
Steve Block1e0659c2011-05-24 12:43:12 +0100147inline Condition NegateCondition(Condition cond) {
148 ASSERT(cond != al);
149 return static_cast<Condition>(cond ^ ne);
150}
151
152
153// Corresponds to transposing the operands of a comparison.
154inline Condition ReverseCondition(Condition cond) {
155 switch (cond) {
156 case lo:
157 return hi;
158 case hi:
159 return lo;
160 case hs:
161 return ls;
162 case ls:
163 return hs;
164 case lt:
165 return gt;
166 case gt:
167 return lt;
168 case ge:
169 return le;
170 case le:
171 return ge;
172 default:
173 return cond;
174 };
175}
176
177
178// -----------------------------------------------------------------------------
179// Instructions encoding.
180
181// Instr is merely used by the Assembler to distinguish 32bit integers
182// representing instructions from usual 32 bit values.
183// Instruction objects are pointers to 32bit values, and provide methods to
184// access the various ISA fields.
185typedef int32_t Instr;
186
187
Steve Blocka7e24c12009-10-30 11:49:00 +0000188// Opcodes for Data-processing instructions (instructions with a type 0 and 1)
189// as defined in section A3.4
190enum Opcode {
Steve Block1e0659c2011-05-24 12:43:12 +0100191 AND = 0 << 21, // Logical AND.
192 EOR = 1 << 21, // Logical Exclusive OR.
193 SUB = 2 << 21, // Subtract.
194 RSB = 3 << 21, // Reverse Subtract.
195 ADD = 4 << 21, // Add.
196 ADC = 5 << 21, // Add with Carry.
197 SBC = 6 << 21, // Subtract with Carry.
198 RSC = 7 << 21, // Reverse Subtract with Carry.
199 TST = 8 << 21, // Test.
200 TEQ = 9 << 21, // Test Equivalence.
201 CMP = 10 << 21, // Compare.
202 CMN = 11 << 21, // Compare Negated.
203 ORR = 12 << 21, // Logical (inclusive) OR.
204 MOV = 13 << 21, // Move.
205 BIC = 14 << 21, // Bit Clear.
206 MVN = 15 << 21 // Move Not.
Steve Blocka7e24c12009-10-30 11:49:00 +0000207};
208
209
Steve Block6ded16b2010-05-10 14:33:55 +0100210// The bits for bit 7-4 for some type 0 miscellaneous instructions.
211enum MiscInstructionsBits74 {
212 // With bits 22-21 01.
Steve Block1e0659c2011-05-24 12:43:12 +0100213 BX = 1 << 4,
214 BXJ = 2 << 4,
215 BLX = 3 << 4,
216 BKPT = 7 << 4,
Steve Blocka7e24c12009-10-30 11:49:00 +0000217
Steve Block6ded16b2010-05-10 14:33:55 +0100218 // With bits 22-21 11.
Steve Block1e0659c2011-05-24 12:43:12 +0100219 CLZ = 1 << 4
220};
221
222
223// Instruction encoding bits and masks.
224enum {
225 H = 1 << 5, // Halfword (or byte).
226 S6 = 1 << 6, // Signed (or unsigned).
227 L = 1 << 20, // Load (or store).
228 S = 1 << 20, // Set condition code (or leave unchanged).
229 W = 1 << 21, // Writeback base register (or leave unchanged).
230 A = 1 << 21, // Accumulate in multiply instruction (or not).
231 B = 1 << 22, // Unsigned byte (or word).
232 N = 1 << 22, // Long (or short).
233 U = 1 << 23, // Positive (or negative) offset/index.
234 P = 1 << 24, // Offset/pre-indexed addressing (or post-indexed addressing).
235 I = 1 << 25, // Immediate shifter operand (or not).
236
237 B4 = 1 << 4,
238 B5 = 1 << 5,
239 B6 = 1 << 6,
240 B7 = 1 << 7,
241 B8 = 1 << 8,
242 B9 = 1 << 9,
243 B12 = 1 << 12,
244 B16 = 1 << 16,
245 B18 = 1 << 18,
246 B19 = 1 << 19,
247 B20 = 1 << 20,
248 B21 = 1 << 21,
249 B22 = 1 << 22,
250 B23 = 1 << 23,
251 B24 = 1 << 24,
252 B25 = 1 << 25,
253 B26 = 1 << 26,
254 B27 = 1 << 27,
255 B28 = 1 << 28,
256
257 // Instruction bit masks.
258 kCondMask = 15 << 28,
259 kALUMask = 0x6f << 21,
260 kRdMask = 15 << 12, // In str instruction.
261 kCoprocessorMask = 15 << 8,
262 kOpCodeMask = 15 << 21, // In data-processing instructions.
263 kImm24Mask = (1 << 24) - 1,
264 kOff12Mask = (1 << 12) - 1
265};
266
267
268// -----------------------------------------------------------------------------
269// Addressing modes and instruction variants.
270
271// Condition code updating mode.
272enum SBit {
273 SetCC = 1 << 20, // Set condition code.
274 LeaveCC = 0 << 20 // Leave condition code unchanged.
275};
276
277
278// Status register selection.
279enum SRegister {
280 CPSR = 0 << 22,
281 SPSR = 1 << 22
Steve Blocka7e24c12009-10-30 11:49:00 +0000282};
283
284
Steve Blocka7e24c12009-10-30 11:49:00 +0000285// Shifter types for Data-processing operands as defined in section A5.1.2.
Steve Block1e0659c2011-05-24 12:43:12 +0100286enum ShiftOp {
287 LSL = 0 << 5, // Logical shift left.
288 LSR = 1 << 5, // Logical shift right.
289 ASR = 2 << 5, // Arithmetic shift right.
290 ROR = 3 << 5, // Rotate right.
291
292 // RRX is encoded as ROR with shift_imm == 0.
293 // Use a special code to make the distinction. The RRX ShiftOp is only used
294 // as an argument, and will never actually be encoded. The Assembler will
295 // detect it and emit the correct ROR shift operand with shift_imm == 0.
296 RRX = -1,
297 kNumberOfShifts = 4
Steve Blocka7e24c12009-10-30 11:49:00 +0000298};
299
300
Steve Block1e0659c2011-05-24 12:43:12 +0100301// Status register fields.
302enum SRegisterField {
303 CPSR_c = CPSR | 1 << 16,
304 CPSR_x = CPSR | 1 << 17,
305 CPSR_s = CPSR | 1 << 18,
306 CPSR_f = CPSR | 1 << 19,
307 SPSR_c = SPSR | 1 << 16,
308 SPSR_x = SPSR | 1 << 17,
309 SPSR_s = SPSR | 1 << 18,
310 SPSR_f = SPSR | 1 << 19
311};
312
313// Status register field mask (or'ed SRegisterField enum values).
314typedef uint32_t SRegisterFieldMask;
315
316
317// Memory operand addressing mode.
318enum AddrMode {
319 // Bit encoding P U W.
320 Offset = (8|4|0) << 21, // Offset (without writeback to base).
321 PreIndex = (8|4|1) << 21, // Pre-indexed addressing with writeback.
322 PostIndex = (0|4|0) << 21, // Post-indexed addressing with writeback.
323 NegOffset = (8|0|0) << 21, // Negative offset (without writeback to base).
324 NegPreIndex = (8|0|1) << 21, // Negative pre-indexed with writeback.
325 NegPostIndex = (0|0|0) << 21 // Negative post-indexed with writeback.
326};
327
328
329// Load/store multiple addressing mode.
330enum BlockAddrMode {
331 // Bit encoding P U W .
332 da = (0|0|0) << 21, // Decrement after.
333 ia = (0|4|0) << 21, // Increment after.
334 db = (8|0|0) << 21, // Decrement before.
335 ib = (8|4|0) << 21, // Increment before.
336 da_w = (0|0|1) << 21, // Decrement after with writeback to base.
337 ia_w = (0|4|1) << 21, // Increment after with writeback to base.
338 db_w = (8|0|1) << 21, // Decrement before with writeback to base.
339 ib_w = (8|4|1) << 21, // Increment before with writeback to base.
340
341 // Alias modes for comparison when writeback does not matter.
342 da_x = (0|0|0) << 21, // Decrement after.
343 ia_x = (0|4|0) << 21, // Increment after.
344 db_x = (8|0|0) << 21, // Decrement before.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100345 ib_x = (8|4|0) << 21, // Increment before.
346
347 kBlockAddrModeMask = (8|4|1) << 21
Steve Block1e0659c2011-05-24 12:43:12 +0100348};
349
350
351// Coprocessor load/store operand size.
352enum LFlag {
353 Long = 1 << 22, // Long load/store coprocessor.
354 Short = 0 << 22 // Short load/store coprocessor.
355};
356
357
358// -----------------------------------------------------------------------------
359// Supervisor Call (svc) specific support.
360
Steve Blocka7e24c12009-10-30 11:49:00 +0000361// Special Software Interrupt codes when used in the presence of the ARM
362// simulator.
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800363// svc (formerly swi) provides a 24bit immediate value. Use bits 22:0 for
364// standard SoftwareInterrupCode. Bit 23 is reserved for the stop feature.
Steve Blocka7e24c12009-10-30 11:49:00 +0000365enum SoftwareInterruptCodes {
366 // transition to C code
Steve Block1e0659c2011-05-24 12:43:12 +0100367 kCallRtRedirected= 0x10,
Steve Blocka7e24c12009-10-30 11:49:00 +0000368 // break point
Steve Block1e0659c2011-05-24 12:43:12 +0100369 kBreakpoint= 0x20,
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800370 // stop
Steve Block1e0659c2011-05-24 12:43:12 +0100371 kStopCode = 1 << 23
Steve Blocka7e24c12009-10-30 11:49:00 +0000372};
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100373const uint32_t kStopCodeMask = kStopCode - 1;
374const uint32_t kMaxStopCode = kStopCode - 1;
375const int32_t kDefaultStopCode = -1;
Steve Blocka7e24c12009-10-30 11:49:00 +0000376
377
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100378// Type of VFP register. Determines register encoding.
379enum VFPRegPrecision {
380 kSinglePrecision = 0,
381 kDoublePrecision = 1
382};
383
Steve Block1e0659c2011-05-24 12:43:12 +0100384
385// VFP FPSCR constants.
386enum VFPConversionMode {
387 kFPSCRRounding = 0,
388 kDefaultRoundToZero = 1
Russell Brenner90bac252010-11-18 13:33:46 -0800389};
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100390
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100391// This mask does not include the "inexact" or "input denormal" cumulative
392// exceptions flags, because we usually don't want to check for it.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100393const uint32_t kVFPExceptionMask = 0xf;
394const uint32_t kVFPInvalidOpExceptionBit = 1 << 0;
395const uint32_t kVFPOverflowExceptionBit = 1 << 2;
396const uint32_t kVFPUnderflowExceptionBit = 1 << 3;
397const uint32_t kVFPInexactExceptionBit = 1 << 4;
398const uint32_t kVFPFlushToZeroMask = 1 << 24;
Steve Block1e0659c2011-05-24 12:43:12 +0100399
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100400const uint32_t kVFPNConditionFlagBit = 1 << 31;
401const uint32_t kVFPZConditionFlagBit = 1 << 30;
402const uint32_t kVFPCConditionFlagBit = 1 << 29;
403const uint32_t kVFPVConditionFlagBit = 1 << 28;
Steve Blocka7e24c12009-10-30 11:49:00 +0000404
405
Steve Block1e0659c2011-05-24 12:43:12 +0100406// VFP rounding modes. See ARM DDI 0406B Page A2-29.
407enum VFPRoundingMode {
408 RN = 0 << 22, // Round to Nearest.
409 RP = 1 << 22, // Round towards Plus Infinity.
410 RM = 2 << 22, // Round towards Minus Infinity.
411 RZ = 3 << 22, // Round towards zero.
412
413 // Aliases.
414 kRoundToNearest = RN,
415 kRoundToPlusInf = RP,
416 kRoundToMinusInf = RM,
417 kRoundToZero = RZ
418};
419
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100420const uint32_t kVFPRoundingModeMask = 3 << 22;
Steve Block1e0659c2011-05-24 12:43:12 +0100421
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100422enum CheckForInexactConversion {
423 kCheckForInexactConversion,
424 kDontCheckForInexactConversion
425};
426
Steve Block1e0659c2011-05-24 12:43:12 +0100427// -----------------------------------------------------------------------------
428// Hints.
429
430// Branch hints are not used on the ARM. They are defined so that they can
431// appear in shared function signatures, but will be ignored in ARM
432// implementations.
433enum Hint { no_hint };
434
435// Hints are not used on the arm. Negating is trivial.
436inline Hint NegateHint(Hint ignored) { return no_hint; }
437
438
439// -----------------------------------------------------------------------------
440// Specific instructions, constants, and masks.
441// These constants are declared in assembler-arm.cc, as they use named registers
442// and other constants.
443
444
445// add(sp, sp, 4) instruction (aka Pop())
446extern const Instr kPopInstruction;
447
448// str(r, MemOperand(sp, 4, NegPreIndex), al) instruction (aka push(r))
449// register r is not encoded.
450extern const Instr kPushRegPattern;
451
452// ldr(r, MemOperand(sp, 4, PostIndex), al) instruction (aka pop(r))
453// register r is not encoded.
454extern const Instr kPopRegPattern;
455
456// mov lr, pc
457extern const Instr kMovLrPc;
458// ldr rd, [pc, #offset]
459extern const Instr kLdrPCMask;
460extern const Instr kLdrPCPattern;
461// blxcc rm
462extern const Instr kBlxRegMask;
463
464extern const Instr kBlxRegPattern;
465
466extern const Instr kMovMvnMask;
467extern const Instr kMovMvnPattern;
468extern const Instr kMovMvnFlip;
469extern const Instr kMovLeaveCCMask;
470extern const Instr kMovLeaveCCPattern;
471extern const Instr kMovwMask;
472extern const Instr kMovwPattern;
473extern const Instr kMovwLeaveCCFlip;
474extern const Instr kCmpCmnMask;
475extern const Instr kCmpCmnPattern;
476extern const Instr kCmpCmnFlip;
477extern const Instr kAddSubFlip;
478extern const Instr kAndBicFlip;
479
480// A mask for the Rd register for push, pop, ldr, str instructions.
481extern const Instr kLdrRegFpOffsetPattern;
482
483extern const Instr kStrRegFpOffsetPattern;
484
485extern const Instr kLdrRegFpNegOffsetPattern;
486
487extern const Instr kStrRegFpNegOffsetPattern;
488
489extern const Instr kLdrStrInstrTypeMask;
490extern const Instr kLdrStrInstrArgumentMask;
491extern const Instr kLdrStrOffsetMask;
492
493
494// -----------------------------------------------------------------------------
495// Instruction abstraction.
496
497// The class Instruction enables access to individual fields defined in the ARM
Steve Blocka7e24c12009-10-30 11:49:00 +0000498// architecture instruction set encoding as described in figure A3-1.
Steve Block1e0659c2011-05-24 12:43:12 +0100499// Note that the Assembler uses typedef int32_t Instr.
Steve Blocka7e24c12009-10-30 11:49:00 +0000500//
501// Example: Test whether the instruction at ptr does set the condition code
502// bits.
503//
504// bool InstructionSetsConditionCodes(byte* ptr) {
Steve Block1e0659c2011-05-24 12:43:12 +0100505// Instruction* instr = Instruction::At(ptr);
506// int type = instr->TypeValue();
Steve Blocka7e24c12009-10-30 11:49:00 +0000507// return ((type == 0) || (type == 1)) && instr->HasS();
508// }
509//
Steve Block1e0659c2011-05-24 12:43:12 +0100510class Instruction {
Steve Blocka7e24c12009-10-30 11:49:00 +0000511 public:
512 enum {
513 kInstrSize = 4,
514 kInstrSizeLog2 = 2,
515 kPCReadOffset = 8
516 };
517
Steve Block1e0659c2011-05-24 12:43:12 +0100518 // Helper macro to define static accessors.
519 // We use the cast to char* trick to bypass the strict anti-aliasing rules.
520 #define DECLARE_STATIC_TYPED_ACCESSOR(return_type, Name) \
521 static inline return_type Name(Instr instr) { \
522 char* temp = reinterpret_cast<char*>(&instr); \
523 return reinterpret_cast<Instruction*>(temp)->Name(); \
524 }
525
526 #define DECLARE_STATIC_ACCESSOR(Name) DECLARE_STATIC_TYPED_ACCESSOR(int, Name)
527
Steve Blocka7e24c12009-10-30 11:49:00 +0000528 // Get the raw instruction bits.
Steve Block1e0659c2011-05-24 12:43:12 +0100529 inline Instr InstructionBits() const {
530 return *reinterpret_cast<const Instr*>(this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000531 }
532
533 // Set the raw instruction bits to value.
Steve Block1e0659c2011-05-24 12:43:12 +0100534 inline void SetInstructionBits(Instr value) {
535 *reinterpret_cast<Instr*>(this) = value;
Steve Blocka7e24c12009-10-30 11:49:00 +0000536 }
537
538 // Read one particular bit out of the instruction bits.
539 inline int Bit(int nr) const {
540 return (InstructionBits() >> nr) & 1;
541 }
542
Steve Block1e0659c2011-05-24 12:43:12 +0100543 // Read a bit field's value out of the instruction bits.
Steve Blocka7e24c12009-10-30 11:49:00 +0000544 inline int Bits(int hi, int lo) const {
545 return (InstructionBits() >> lo) & ((2 << (hi - lo)) - 1);
546 }
547
Steve Block1e0659c2011-05-24 12:43:12 +0100548 // Read a bit field out of the instruction bits.
549 inline int BitField(int hi, int lo) const {
550 return InstructionBits() & (((2 << (hi - lo)) - 1) << lo);
551 }
552
553 // Static support.
554
555 // Read one particular bit out of the instruction bits.
556 static inline int Bit(Instr instr, int nr) {
557 return (instr >> nr) & 1;
558 }
559
560 // Read the value of a bit field out of the instruction bits.
561 static inline int Bits(Instr instr, int hi, int lo) {
562 return (instr >> lo) & ((2 << (hi - lo)) - 1);
563 }
564
565
566 // Read a bit field out of the instruction bits.
567 static inline int BitField(Instr instr, int hi, int lo) {
568 return instr & (((2 << (hi - lo)) - 1) << lo);
569 }
570
Steve Blocka7e24c12009-10-30 11:49:00 +0000571
572 // Accessors for the different named fields used in the ARM encoding.
573 // The naming of these accessor corresponds to figure A3-1.
Steve Block1e0659c2011-05-24 12:43:12 +0100574 //
575 // Two kind of accessors are declared:
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100576 // - <Name>Field() will return the raw field, i.e. the field's bits at their
Steve Block1e0659c2011-05-24 12:43:12 +0100577 // original place in the instruction encoding.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100578 // e.g. if instr is the 'addgt r0, r1, r2' instruction, encoded as
579 // 0xC0810002 ConditionField(instr) will return 0xC0000000.
Steve Block1e0659c2011-05-24 12:43:12 +0100580 // - <Name>Value() will return the field value, shifted back to bit 0.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100581 // e.g. if instr is the 'addgt r0, r1, r2' instruction, encoded as
582 // 0xC0810002 ConditionField(instr) will return 0xC.
Steve Block1e0659c2011-05-24 12:43:12 +0100583
584
Steve Blocka7e24c12009-10-30 11:49:00 +0000585 // Generally applicable fields
Steve Block1e0659c2011-05-24 12:43:12 +0100586 inline Condition ConditionValue() const {
Steve Blocka7e24c12009-10-30 11:49:00 +0000587 return static_cast<Condition>(Bits(31, 28));
588 }
Steve Block1e0659c2011-05-24 12:43:12 +0100589 inline Condition ConditionField() const {
590 return static_cast<Condition>(BitField(31, 28));
591 }
592 DECLARE_STATIC_TYPED_ACCESSOR(Condition, ConditionValue);
593 DECLARE_STATIC_TYPED_ACCESSOR(Condition, ConditionField);
Steve Blocka7e24c12009-10-30 11:49:00 +0000594
Steve Block1e0659c2011-05-24 12:43:12 +0100595 inline int TypeValue() const { return Bits(27, 25); }
Steve Blocka7e24c12009-10-30 11:49:00 +0000596
Steve Block1e0659c2011-05-24 12:43:12 +0100597 inline int RnValue() const { return Bits(19, 16); }
598 DECLARE_STATIC_ACCESSOR(RnValue);
599 inline int RdValue() const { return Bits(15, 12); }
600 DECLARE_STATIC_ACCESSOR(RdValue);
601
602 inline int CoprocessorValue() const { return Bits(11, 8); }
Steve Blockd0582a62009-12-15 09:54:21 +0000603 // Support for VFP.
604 // Vn(19-16) | Vd(15-12) | Vm(3-0)
Steve Block1e0659c2011-05-24 12:43:12 +0100605 inline int VnValue() const { return Bits(19, 16); }
606 inline int VmValue() const { return Bits(3, 0); }
607 inline int VdValue() const { return Bits(15, 12); }
608 inline int NValue() const { return Bit(7); }
609 inline int MValue() const { return Bit(5); }
610 inline int DValue() const { return Bit(22); }
611 inline int RtValue() const { return Bits(15, 12); }
612 inline int PValue() const { return Bit(24); }
613 inline int UValue() const { return Bit(23); }
614 inline int Opc1Value() const { return (Bit(23) << 2) | Bits(21, 20); }
615 inline int Opc2Value() const { return Bits(19, 16); }
616 inline int Opc3Value() const { return Bits(7, 6); }
617 inline int SzValue() const { return Bit(8); }
618 inline int VLValue() const { return Bit(20); }
619 inline int VCValue() const { return Bit(8); }
620 inline int VAValue() const { return Bits(23, 21); }
621 inline int VBValue() const { return Bits(6, 5); }
622 inline int VFPNRegValue(VFPRegPrecision pre) {
623 return VFPGlueRegValue(pre, 16, 7);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100624 }
Steve Block1e0659c2011-05-24 12:43:12 +0100625 inline int VFPMRegValue(VFPRegPrecision pre) {
626 return VFPGlueRegValue(pre, 0, 5);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100627 }
Steve Block1e0659c2011-05-24 12:43:12 +0100628 inline int VFPDRegValue(VFPRegPrecision pre) {
629 return VFPGlueRegValue(pre, 12, 22);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100630 }
Steve Blockd0582a62009-12-15 09:54:21 +0000631
Steve Blocka7e24c12009-10-30 11:49:00 +0000632 // Fields used in Data processing instructions
Steve Block1e0659c2011-05-24 12:43:12 +0100633 inline int OpcodeValue() const {
Steve Blocka7e24c12009-10-30 11:49:00 +0000634 return static_cast<Opcode>(Bits(24, 21));
635 }
Steve Block1e0659c2011-05-24 12:43:12 +0100636 inline Opcode OpcodeField() const {
637 return static_cast<Opcode>(BitField(24, 21));
638 }
639 inline int SValue() const { return Bit(20); }
Steve Blocka7e24c12009-10-30 11:49:00 +0000640 // with register
Steve Block1e0659c2011-05-24 12:43:12 +0100641 inline int RmValue() const { return Bits(3, 0); }
642 DECLARE_STATIC_ACCESSOR(RmValue);
643 inline int ShiftValue() const { return static_cast<ShiftOp>(Bits(6, 5)); }
644 inline ShiftOp ShiftField() const {
645 return static_cast<ShiftOp>(BitField(6, 5));
646 }
647 inline int RegShiftValue() const { return Bit(4); }
648 inline int RsValue() const { return Bits(11, 8); }
649 inline int ShiftAmountValue() const { return Bits(11, 7); }
Steve Blocka7e24c12009-10-30 11:49:00 +0000650 // with immediate
Steve Block1e0659c2011-05-24 12:43:12 +0100651 inline int RotateValue() const { return Bits(11, 8); }
652 inline int Immed8Value() const { return Bits(7, 0); }
653 inline int Immed4Value() const { return Bits(19, 16); }
654 inline int ImmedMovwMovtValue() const {
655 return Immed4Value() << 12 | Offset12Value(); }
Steve Blocka7e24c12009-10-30 11:49:00 +0000656
657 // Fields used in Load/Store instructions
Steve Block1e0659c2011-05-24 12:43:12 +0100658 inline int PUValue() const { return Bits(24, 23); }
659 inline int PUField() const { return BitField(24, 23); }
660 inline int BValue() const { return Bit(22); }
661 inline int WValue() const { return Bit(21); }
662 inline int LValue() const { return Bit(20); }
Steve Blocka7e24c12009-10-30 11:49:00 +0000663 // with register uses same fields as Data processing instructions above
664 // with immediate
Steve Block1e0659c2011-05-24 12:43:12 +0100665 inline int Offset12Value() const { return Bits(11, 0); }
Steve Blocka7e24c12009-10-30 11:49:00 +0000666 // multiple
Steve Block1e0659c2011-05-24 12:43:12 +0100667 inline int RlistValue() const { return Bits(15, 0); }
Steve Blocka7e24c12009-10-30 11:49:00 +0000668 // extra loads and stores
Steve Block1e0659c2011-05-24 12:43:12 +0100669 inline int SignValue() const { return Bit(6); }
670 inline int HValue() const { return Bit(5); }
671 inline int ImmedHValue() const { return Bits(11, 8); }
672 inline int ImmedLValue() const { return Bits(3, 0); }
Steve Blocka7e24c12009-10-30 11:49:00 +0000673
674 // Fields used in Branch instructions
Steve Block1e0659c2011-05-24 12:43:12 +0100675 inline int LinkValue() const { return Bit(24); }
676 inline int SImmed24Value() const { return ((InstructionBits() << 8) >> 8); }
Steve Blocka7e24c12009-10-30 11:49:00 +0000677
678 // Fields used in Software interrupt instructions
Steve Block1e0659c2011-05-24 12:43:12 +0100679 inline SoftwareInterruptCodes SvcValue() const {
Steve Blocka7e24c12009-10-30 11:49:00 +0000680 return static_cast<SoftwareInterruptCodes>(Bits(23, 0));
681 }
682
683 // Test for special encodings of type 0 instructions (extra loads and stores,
684 // as well as multiplications).
685 inline bool IsSpecialType0() const { return (Bit(7) == 1) && (Bit(4) == 1); }
686
Steve Block6ded16b2010-05-10 14:33:55 +0100687 // Test for miscellaneous instructions encodings of type 0 instructions.
688 inline bool IsMiscType0() const { return (Bit(24) == 1)
689 && (Bit(23) == 0)
690 && (Bit(20) == 0)
691 && ((Bit(7) == 0)); }
692
Steve Block1e0659c2011-05-24 12:43:12 +0100693 // Test for a stop instruction.
694 inline bool IsStop() const {
695 return (TypeValue() == 7) && (Bit(24) == 1) && (SvcValue() >= kStopCode);
696 }
697
Steve Blocka7e24c12009-10-30 11:49:00 +0000698 // Special accessors that test for existence of a value.
Steve Block1e0659c2011-05-24 12:43:12 +0100699 inline bool HasS() const { return SValue() == 1; }
700 inline bool HasB() const { return BValue() == 1; }
701 inline bool HasW() const { return WValue() == 1; }
702 inline bool HasL() const { return LValue() == 1; }
703 inline bool HasU() const { return UValue() == 1; }
704 inline bool HasSign() const { return SignValue() == 1; }
705 inline bool HasH() const { return HValue() == 1; }
706 inline bool HasLink() const { return LinkValue() == 1; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000707
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100708 // Decoding the double immediate in the vmov instruction.
709 double DoubleImmedVmov() const;
710
Steve Blocka7e24c12009-10-30 11:49:00 +0000711 // Instructions are read of out a code stream. The only way to get a
712 // reference to an instruction is to convert a pointer. There is no way
Steve Block1e0659c2011-05-24 12:43:12 +0100713 // to allocate or create instances of class Instruction.
714 // Use the At(pc) function to create references to Instruction.
715 static Instruction* At(byte* pc) {
716 return reinterpret_cast<Instruction*>(pc);
717 }
718
Steve Blocka7e24c12009-10-30 11:49:00 +0000719
720 private:
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100721 // Join split register codes, depending on single or double precision.
722 // four_bit is the position of the least-significant bit of the four
723 // bit specifier. one_bit is the position of the additional single bit
724 // specifier.
Steve Block1e0659c2011-05-24 12:43:12 +0100725 inline int VFPGlueRegValue(VFPRegPrecision pre, int four_bit, int one_bit) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100726 if (pre == kSinglePrecision) {
727 return (Bits(four_bit + 3, four_bit) << 1) | Bit(one_bit);
728 }
729 return (Bit(one_bit) << 4) | Bits(four_bit + 3, four_bit);
730 }
731
Steve Block1e0659c2011-05-24 12:43:12 +0100732 // We need to prevent the creation of instances of class Instruction.
733 DISALLOW_IMPLICIT_CONSTRUCTORS(Instruction);
Steve Blocka7e24c12009-10-30 11:49:00 +0000734};
735
736
737// Helper functions for converting between register numbers and names.
738class Registers {
739 public:
740 // Return the name of the register.
741 static const char* Name(int reg);
742
743 // Lookup the register number for the name provided.
744 static int Number(const char* name);
745
746 struct RegisterAlias {
747 int reg;
Steve Blockd0582a62009-12-15 09:54:21 +0000748 const char* name;
Steve Blocka7e24c12009-10-30 11:49:00 +0000749 };
750
751 private:
752 static const char* names_[kNumRegisters];
753 static const RegisterAlias aliases_[];
754};
755
Steve Blockd0582a62009-12-15 09:54:21 +0000756// Helper functions for converting between VFP register numbers and names.
757class VFPRegisters {
758 public:
759 // Return the name of the register.
Steve Block6ded16b2010-05-10 14:33:55 +0100760 static const char* Name(int reg, bool is_double);
761
762 // Lookup the register number for the name provided.
763 // Set flag pointed by is_double to true if register
764 // is double-precision.
765 static int Number(const char* name, bool* is_double);
Steve Blockd0582a62009-12-15 09:54:21 +0000766
767 private:
768 static const char* names_[kNumVFPRegisters];
769};
Steve Blocka7e24c12009-10-30 11:49:00 +0000770
771
Steve Block1e0659c2011-05-24 12:43:12 +0100772} } // namespace v8::internal
Steve Blocka7e24c12009-10-30 11:49:00 +0000773
774#endif // V8_ARM_CONSTANTS_ARM_H_