Merge pull request #6721 from jashook/disable_jit32_tests
[platform/upstream/coreclr.git] / src / jit / emitarm64.h
1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4
5 #if defined(_TARGET_ARM64_)
6
7 // The ARM64 instructions are all 32 bits in size.
8 // we use an unsigned int to hold the encoded instructions.
9 // This typedef defines the type that we use to hold encoded instructions.
10 //
11 typedef unsigned int code_t;
12
13 static bool strictArmAsm;
14
15 /************************************************************************/
16 /*         Routines that compute the size of / encode instructions      */
17 /************************************************************************/
18
19 struct CnsVal
20 {
21     ssize_t cnsVal;
22 #ifdef RELOC_SUPPORT
23     bool cnsReloc;
24 #endif
25 };
26
27 #ifdef DEBUG
28
29 /************************************************************************/
30 /*             Debug-only routines to display instructions              */
31 /************************************************************************/
32
33 const char* emitFPregName(unsigned reg, bool varName = true);
34 const char* emitVectorRegName(regNumber reg);
35
36 void emitDispInst(instruction ins);
37 void emitDispReloc(int value, bool addComma);
38 void emitDispImm(ssize_t imm, bool addComma, bool alwaysHex = false);
39 void emitDispFloatZero();
40 void emitDispFloatImm(ssize_t imm8);
41 void emitDispImmOptsLSL12(ssize_t imm, insOpts opt);
42 void emitDispCond(insCond cond);
43 void emitDispFlags(insCflags flags);
44 void emitDispBarrier(insBarrier barrier);
45 void emitDispShiftOpts(insOpts opt);
46 void emitDispExtendOpts(insOpts opt);
47 void emitDispLSExtendOpts(insOpts opt);
48 void emitDispReg(regNumber reg, emitAttr attr, bool addComma);
49 void emitDispVectorReg(regNumber reg, insOpts opt, bool addComma);
50 void emitDispVectorRegIndex(regNumber reg, emitAttr elemsize, ssize_t index, bool addComma);
51 void emitDispArrangement(insOpts opt);
52 void emitDispShiftedReg(regNumber reg, insOpts opt, ssize_t imm, emitAttr attr);
53 void emitDispExtendReg(regNumber reg, insOpts opt, ssize_t imm);
54 void emitDispAddrRI(regNumber reg, insOpts opt, ssize_t imm);
55 void emitDispAddrRRExt(regNumber reg1, regNumber reg2, insOpts opt, bool isScaled, emitAttr size);
56
57 void emitDispIns(instrDesc* id,
58                  bool       isNew,
59                  bool       doffs,
60                  bool       asmfm,
61                  unsigned   offs  = 0,
62                  BYTE*      pCode = 0,
63                  size_t     sz    = 0,
64                  insGroup*  ig    = NULL);
65 #endif // DEBUG
66
67 /************************************************************************/
68 /*  Private members that deal with target-dependent instr. descriptors  */
69 /************************************************************************/
70
71 private:
72 instrDesc* emitNewInstrAmd(emitAttr attr, int dsp);
73 instrDesc* emitNewInstrAmdCns(emitAttr attr, int dsp, int cns);
74
75 instrDesc* emitNewInstrCallDir(int              argCnt,
76                                VARSET_VALARG_TP GCvars,
77                                regMaskTP        gcrefRegs,
78                                regMaskTP        byrefRegs,
79                                emitAttr         retSize,
80                                emitAttr         secondRetSize);
81
82 instrDesc* emitNewInstrCallInd(int              argCnt,
83                                ssize_t          disp,
84                                VARSET_VALARG_TP GCvars,
85                                regMaskTP        gcrefRegs,
86                                regMaskTP        byrefRegs,
87                                emitAttr         retSize,
88                                emitAttr         secondRetSize);
89
90 void emitGetInsCns(instrDesc* id, CnsVal* cv);
91 ssize_t emitGetInsAmdCns(instrDesc* id, CnsVal* cv);
92 void emitGetInsDcmCns(instrDesc* id, CnsVal* cv);
93 ssize_t emitGetInsAmdAny(instrDesc* id);
94
95 /************************************************************************/
96 /*               Private helpers for instruction output                 */
97 /************************************************************************/
98
99 private:
100 bool emitInsIsCompare(instruction ins);
101 bool emitInsIsLoad(instruction ins);
102 bool emitInsIsStore(instruction ins);
103 bool emitInsIsLoadOrStore(instruction ins);
104 emitAttr emitInsAdjustLoadStoreAttr(instruction ins, emitAttr attr);
105 emitAttr emitInsTargetRegSize(instrDesc* id);
106 emitAttr emitInsLoadStoreSize(instrDesc* id);
107
108 emitter::insFormat emitInsFormat(instruction ins);
109 emitter::code_t emitInsCode(instruction ins, insFormat fmt);
110
111 // Generate code for a load or store operation and handle the case of contained GT_LEA op1 with [base + index<<scale +
112 // offset]
113 void emitInsLoadStoreOp(instruction ins, emitAttr attr, regNumber dataReg, GenTreeIndir* indir);
114
115 //  Emit the 32-bit Arm64 instruction 'code' into the 'dst'  buffer
116 static unsigned emitOutput_Instr(BYTE* dst, code_t code);
117
118 // A helper method to return the natural scale for an EA 'size'
119 static unsigned NaturalScale_helper(emitAttr size);
120
121 // A helper method to perform a Rotate-Right shift operation
122 static UINT64 ROR_helper(UINT64 value, unsigned sh, unsigned width);
123
124 // A helper method to perform a 'NOT' bitwise complement operation
125 static UINT64 NOT_helper(UINT64 value, unsigned width);
126
127 // A helper method to perform a bit Replicate operation
128 static UINT64 Replicate_helper(UINT64 value, unsigned width, emitAttr size);
129
130 /************************************************************************
131 *
132 * This union is used to to encode/decode the special ARM64 immediate values
133 * that is listed as imm(N,r,s) and referred to as 'bitmask immediate'
134 */
135
136 union bitMaskImm {
137     struct
138     {
139         unsigned immS : 6; // bits 0..5
140         unsigned immR : 6; // bits 6..11
141         unsigned immN : 1; // bits 12
142     };
143     unsigned immNRS; // concat N:R:S forming a 13-bit unsigned immediate
144 };
145
146 /************************************************************************
147 *
148 *  Convert between a 64-bit immediate and its 'bitmask immediate'
149 *   representation imm(i16,hw)
150 */
151
152 static emitter::bitMaskImm emitEncodeBitMaskImm(INT64 imm, emitAttr size);
153
154 static INT64 emitDecodeBitMaskImm(const emitter::bitMaskImm bmImm, emitAttr size);
155
156 /************************************************************************
157 *
158 * This union is used to to encode/decode the special ARM64 immediate values
159 * that is listed as imm(i16,hw) and referred to as 'halfword immediate'
160 */
161
162 union halfwordImm {
163     struct
164     {
165         unsigned immVal : 16; // bits  0..15
166         unsigned immHW : 2;   // bits 16..17
167     };
168     unsigned immHWVal; // concat HW:Val forming a 18-bit unsigned immediate
169 };
170
171 /************************************************************************
172 *
173 *  Convert between a 64-bit immediate and its 'halfword immediate'
174 *   representation imm(i16,hw)
175 */
176
177 static emitter::halfwordImm emitEncodeHalfwordImm(INT64 imm, emitAttr size);
178
179 static INT64 emitDecodeHalfwordImm(const emitter::halfwordImm hwImm, emitAttr size);
180
181 /************************************************************************
182 *
183 * This union is used to encode/decode the special ARM64 immediate values
184 * that is listed as imm(i16,by) and referred to as 'byteShifted immediate'
185 */
186
187 union byteShiftedImm {
188     struct
189     {
190         unsigned immVal : 8;  // bits  0..7
191         unsigned immBY : 2;   // bits  8..9
192         unsigned immOnes : 1; // bit   10
193     };
194     unsigned immBSVal; // concat Ones:BY:Val forming a 10-bit unsigned immediate
195 };
196
197 /************************************************************************
198 *
199 *  Convert between a 16/32-bit immediate and its 'byteShifted immediate'
200 *   representation imm(i8,by)
201 */
202
203 static emitter::byteShiftedImm emitEncodeByteShiftedImm(INT64 imm, emitAttr size, bool allow_MSL);
204
205 static INT32 emitDecodeByteShiftedImm(const emitter::byteShiftedImm bsImm, emitAttr size);
206
207 /************************************************************************
208 *
209 * This union is used to to encode/decode the special ARM64 immediate values
210 * that are use for FMOV immediate and referred to as 'float 8-bit immediate'
211 */
212
213 union floatImm8 {
214     struct
215     {
216         unsigned immMant : 4; // bits 0..3
217         unsigned immExp : 3;  // bits 4..6
218         unsigned immSign : 1; // bits 7
219     };
220     unsigned immFPIVal; // concat Sign:Exp:Mant forming an 8-bit unsigned immediate
221 };
222
223 /************************************************************************
224 *
225 *  Convert between a double and its 'float 8-bit immediate' representation
226 */
227
228 static emitter::floatImm8 emitEncodeFloatImm8(double immDbl);
229
230 static double emitDecodeFloatImm8(const emitter::floatImm8 fpImm);
231
232 /************************************************************************
233 *
234 *  This union is used to to encode/decode the cond, nzcv and imm5 values for
235 *   instructions that use them in the small constant immediate field
236 */
237
238 union condFlagsImm {
239     struct
240     {
241         insCond   cond : 4;  // bits  0..3
242         insCflags flags : 4; // bits  4..7
243         unsigned  imm5 : 5;  // bits  8..12
244     };
245     unsigned immCFVal; // concat imm5:flags:cond forming an 13-bit unsigned immediate
246 };
247
248 // Returns an encoding for the specified register used in the 'Rd' position
249 static code_t insEncodeReg_Rd(regNumber reg);
250
251 // Returns an encoding for the specified register used in the 'Rt' position
252 static code_t insEncodeReg_Rt(regNumber reg);
253
254 // Returns an encoding for the specified register used in the 'Rn' position
255 static code_t insEncodeReg_Rn(regNumber reg);
256
257 // Returns an encoding for the specified register used in the 'Rm' position
258 static code_t insEncodeReg_Rm(regNumber reg);
259
260 // Returns an encoding for the specified register used in the 'Ra' position
261 static code_t insEncodeReg_Ra(regNumber reg);
262
263 // Returns an encoding for the specified register used in the 'Vd' position
264 static code_t insEncodeReg_Vd(regNumber reg);
265
266 // Returns an encoding for the specified register used in the 'Vt' position
267 static code_t insEncodeReg_Vt(regNumber reg);
268
269 // Returns an encoding for the specified register used in the 'Vn' position
270 static code_t insEncodeReg_Vn(regNumber reg);
271
272 // Returns an encoding for the specified register used in the 'Vm' position
273 static code_t insEncodeReg_Vm(regNumber reg);
274
275 // Returns an encoding for the specified register used in the 'Va' position
276 static code_t insEncodeReg_Va(regNumber reg);
277
278 // Returns an encoding for the imm which represents the condition code.
279 static code_t insEncodeCond(insCond cond);
280
281 // Returns an encoding for the imm whioch represents the 'condition code'
282 //  with the lowest bit inverted (marked by invert(<cond>) in the architecture manual.
283 static code_t insEncodeInvertedCond(insCond cond);
284
285 // Returns an encoding for the imm which represents the flags.
286 static code_t insEncodeFlags(insCflags flags);
287
288 // Returns the encoding for the Shift Count bits to be used for Arm64 encodings
289 static code_t insEncodeShiftCount(ssize_t imm, emitAttr size);
290
291 // Returns the encoding to select the datasize for most Arm64 instructions
292 static code_t insEncodeDatasize(emitAttr size);
293
294 // Returns the encoding to select the datasize for the general load/store Arm64 instructions
295 static code_t insEncodeDatasizeLS(code_t code, emitAttr size);
296
297 // Returns the encoding to select the datasize for the vector load/store Arm64 instructions
298 static code_t insEncodeDatasizeVLS(code_t code, emitAttr size);
299
300 // Returns the encoding to select the datasize for the vector load/store pair Arm64 instructions
301 static code_t insEncodeDatasizeVPLS(code_t code, emitAttr size);
302
303 // Returns the encoding to select the datasize for bitfield Arm64 instructions
304 static code_t insEncodeDatasizeBF(code_t code, emitAttr size);
305
306 // Returns the encoding to select the vectorsize for SIMD Arm64 instructions
307 static code_t insEncodeVectorsize(emitAttr size);
308
309 // Returns the encoding to select 'index' for an Arm64 vector elem instruction
310 static code_t insEncodeVectorIndex(emitAttr elemsize, ssize_t index);
311
312 // Returns the encoding to select 'index2' for an Arm64 'ins' elem instruction
313 static code_t insEncodeVectorIndex2(emitAttr elemsize, ssize_t index2);
314
315 // Returns the encoding to select 'index' for an Arm64 'mul' elem instruction
316 static code_t insEncodeVectorIndexLMH(emitAttr elemsize, ssize_t index);
317
318 // Returns the encoding to shift by 'shift' bits for an Arm64 vector or scalar instruction
319 static code_t insEncodeVectorShift(emitAttr size, ssize_t shift);
320
321 // Returns the encoding to select the 1/2/4/8 byte elemsize for an Arm64 vector instruction
322 static code_t insEncodeElemsize(emitAttr size);
323
324 // Returns the encoding to select the 4/8 byte elemsize for an Arm64 float vector instruction
325 static code_t insEncodeFloatElemsize(emitAttr size);
326
327 // Returns the encoding to select the index for an Arm64 float vector by elem instruction
328 static code_t insEncodeFloatIndex(emitAttr elemsize, ssize_t index);
329
330 // Returns the encoding to select the 'conversion' operation for a type 'fmt' Arm64 instruction
331 static code_t insEncodeConvertOpt(insFormat fmt, insOpts conversion);
332
333 // Returns the encoding to have the Rn register of a ld/st reg be Pre/Post/Not indexed updated
334 static code_t insEncodeIndexedOpt(insOpts opt);
335
336 // Returns the encoding to have the Rn register of a ld/st pair be Pre/Post/Not indexed updated
337 static code_t insEncodePairIndexedOpt(instruction ins, insOpts opt);
338
339 // Returns the encoding to apply a Shift Type on the Rm register
340 static code_t insEncodeShiftType(insOpts opt);
341
342 // Returns the encoding to apply a 12 bit left shift to the immediate
343 static code_t insEncodeShiftImm12(insOpts opt);
344
345 // Returns the encoding to have the Rm register use an extend operation
346 static code_t insEncodeExtend(insOpts opt);
347
348 // Returns the encoding to scale the Rm register by {0,1,2,3,4} in an extend operation
349 static code_t insEncodeExtendScale(ssize_t imm);
350
351 // Returns the encoding to have the Rm register be auto scaled by the ld/st size
352 static code_t insEncodeReg3Scale(bool isScaled);
353
354 // Returns true if 'reg' represents an integer register.
355 static bool isIntegerRegister(regNumber reg)
356 {
357     return (reg >= REG_INT_FIRST) && (reg <= REG_INT_LAST);
358 }
359
360 // Returns true if 'value' is a legal unsigned immediate 8 bit encoding (such as for fMOV).
361 static bool isValidUimm8(ssize_t value)
362 {
363     return (0 <= value) && (value <= 0xFFLL);
364 };
365
366 // Returns true if 'value' is a legal unsigned immediate 12 bit encoding (such as for CMP, CMN).
367 static bool isValidUimm12(ssize_t value)
368 {
369     return (0 <= value) && (value <= 0xFFFLL);
370 };
371
372 // Returns true if 'value' is a legal unsigned immediate 16 bit encoding (such as for MOVZ, MOVN, MOVK).
373 static bool isValidUimm16(ssize_t value)
374 {
375     return (0 <= value) && (value <= 0xFFFFLL);
376 };
377
378 // Returns true if 'value' is a legal signed immediate 26 bit encoding (such as for B or BL).
379 static bool isValidSimm26(ssize_t value)
380 {
381     return (-0x2000000LL <= value) && (value <= 0x1FFFFFFLL);
382 };
383
384 // Returns true if 'value' is a legal signed immediate 19 bit encoding (such as for B.cond, CBNZ, CBZ).
385 static bool isValidSimm19(ssize_t value)
386 {
387     return (-0x40000LL <= value) && (value <= 0x3FFFFLL);
388 };
389
390 // Returns true if 'value' is a legal signed immediate 14 bit encoding (such as for TBNZ, TBZ).
391 static bool isValidSimm14(ssize_t value)
392 {
393     return (-0x2000LL <= value) && (value <= 0x1FFFLL);
394 };
395
396 // Returns true if 'value' represents a valid 'bitmask immediate' encoding.
397 static bool isValidImmNRS(size_t value, emitAttr size)
398 {
399     return (value >= 0) && (value < 0x2000);
400 } // any unsigned 13-bit immediate
401
402 // Returns true if 'value' represents a valid 'halfword immediate' encoding.
403 static bool isValidImmHWVal(size_t value, emitAttr size)
404 {
405     return (value >= 0) && (value < 0x40000);
406 } // any unsigned 18-bit immediate
407
408 // Returns true if 'value' represents a valid 'byteShifted immediate' encoding.
409 static bool isValidImmBSVal(size_t value, emitAttr size)
410 {
411     return (value >= 0) && (value < 0x800);
412 } // any unsigned 11-bit immediate
413
414 //  The return value replaces REG_ZR with REG_SP
415 static regNumber encodingZRtoSP(regNumber reg)
416 {
417     return (reg == REG_ZR) ? REG_SP : reg;
418 } // ZR (R31) encodes the SP register
419
420 //  The return value replaces REG_SP with REG_ZR
421 static regNumber encodingSPtoZR(regNumber reg)
422 {
423     return (reg == REG_SP) ? REG_ZR : reg;
424 } // SP is encoded using ZR (R31)
425
426 //  For the given 'ins' returns the reverse instruction, if one exists, otherwise returns INS_INVALID
427 static instruction insReverse(instruction ins);
428
429 //  For the given 'datasize' and 'elemsize' returns the insOpts that specifies the vector register arrangement
430 static insOpts optMakeArrangement(emitAttr datasize, emitAttr elemsize);
431
432 //    For the given 'datasize' and 'opt' returns true if it specifies a valid vector register arrangement
433 static bool isValidArrangement(emitAttr datasize, insOpts opt);
434
435 //  For the given 'arrangement' returns the 'datasize' specified by the vector register arrangement
436 static emitAttr optGetDatasize(insOpts arrangement);
437
438 //  For the given 'arrangement' returns the 'elemsize' specified by the vector register arrangement
439 static emitAttr optGetElemsize(insOpts arrangement);
440
441 //  For the given 'arrangement' returns the 'widen-arrangement' specified by the vector register arrangement
442 static insOpts optWidenElemsize(insOpts arrangement);
443
444 //  For the given 'conversion' returns the 'dstsize' specified by the conversion option
445 static emitAttr optGetDstsize(insOpts conversion);
446
447 //  For the given 'conversion' returns the 'srcsize' specified by the conversion option
448 static emitAttr optGetSrcsize(insOpts conversion);
449
450 //    For the given 'datasize', 'elemsize' and 'index' returns true, if it specifies a valid 'index'
451 //    for an element of size 'elemsize' in a vector register of size 'datasize'
452 static bool isValidVectorIndex(emitAttr datasize, emitAttr elemsize, ssize_t index);
453
454 /************************************************************************/
455 /*           Public inline informational methods                        */
456 /************************************************************************/
457
458 public:
459 // true if this 'imm' can be encoded as a input operand to a mov instruction
460 static bool emitIns_valid_imm_for_mov(INT64 imm, emitAttr size);
461
462 // true if this 'imm' can be encoded as a input operand to a vector movi instruction
463 static bool emitIns_valid_imm_for_movi(INT64 imm, emitAttr size);
464
465 // true if this 'immDbl' can be encoded as a input operand to a fmov instruction
466 static bool emitIns_valid_imm_for_fmov(double immDbl);
467
468 // true if this 'imm' can be encoded as a input operand to an add instruction
469 static bool emitIns_valid_imm_for_add(INT64 imm, emitAttr size);
470
471 // true if this 'imm' can be encoded as a input operand to a cmp instruction
472 static bool emitIns_valid_imm_for_cmp(INT64 imm, emitAttr size);
473
474 // true if this 'imm' can be encoded as a input operand to an alu instruction
475 static bool emitIns_valid_imm_for_alu(INT64 imm, emitAttr size);
476
477 // true if this 'imm' can be encoded as the offset in a ldr/str instruction
478 static bool emitIns_valid_imm_for_ldst_offset(INT64 imm, emitAttr size);
479
480 // true if 'imm' can use the left shifted by 12 bits encoding
481 static bool canEncodeWithShiftImmBy12(INT64 imm);
482
483 // Normalize the 'imm' so that the upper bits, as defined by 'size' are zero
484 static INT64 normalizeImm64(INT64 imm, emitAttr size);
485
486 // Normalize the 'imm' so that the upper bits, as defined by 'size' are zero
487 static INT32 normalizeImm32(INT32 imm, emitAttr size);
488
489 // true if 'imm' can be encoded using a 'bitmask immediate', also returns the encoding if wbBMI is non-null
490 static bool canEncodeBitMaskImm(INT64 imm, emitAttr size, emitter::bitMaskImm* wbBMI = nullptr);
491
492 // true if 'imm' can be encoded using a 'halfword immediate', also returns the encoding if wbHWI is non-null
493 static bool canEncodeHalfwordImm(INT64 imm, emitAttr size, emitter::halfwordImm* wbHWI = nullptr);
494
495 // true if 'imm' can be encoded using a 'byteShifted immediate', also returns the encoding if wbBSI is non-null
496 static bool canEncodeByteShiftedImm(INT64 imm, emitAttr size, bool allow_MSL, emitter::byteShiftedImm* wbBSI = nullptr);
497
498 // true if 'immDbl' can be encoded using a 'float immediate', also returns the encoding if wbFPI is non-null
499 static bool canEncodeFloatImm8(double immDbl, emitter::floatImm8* wbFPI = nullptr);
500
501 // Returns the number of bits used by the given 'size'.
502 inline static unsigned getBitWidth(emitAttr size)
503 {
504     assert(size <= EA_8BYTE);
505     return (unsigned)size * BITS_PER_BYTE;
506 }
507
508 // Returns true if the imm represents a valid bit shift or bit position for the given 'size' [0..31] or [0..63]
509 inline static unsigned isValidImmShift(ssize_t imm, emitAttr size)
510 {
511     return (imm >= 0) && (imm < getBitWidth(size));
512 }
513
514 inline static bool isValidGeneralDatasize(emitAttr size)
515 {
516     return (size == EA_8BYTE) || (size == EA_4BYTE);
517 }
518
519 inline static bool isValidScalarDatasize(emitAttr size)
520 {
521     return (size == EA_8BYTE) || (size == EA_4BYTE);
522 }
523
524 inline static bool isValidVectorDatasize(emitAttr size)
525 {
526     return (size == EA_16BYTE) || (size == EA_8BYTE);
527 }
528
529 inline static bool isValidGeneralLSDatasize(emitAttr size)
530 {
531     return (size == EA_8BYTE) || (size == EA_4BYTE) || (size == EA_2BYTE) || (size == EA_1BYTE);
532 }
533
534 inline static bool isValidVectorLSDatasize(emitAttr size)
535 {
536     return (size == EA_16BYTE) || (size == EA_8BYTE) || (size == EA_4BYTE) || (size == EA_2BYTE) || (size == EA_1BYTE);
537 }
538
539 inline static bool isValidVectorLSPDatasize(emitAttr size)
540 {
541     return (size == EA_16BYTE) || (size == EA_8BYTE) || (size == EA_4BYTE);
542 }
543
544 inline static bool isValidVectorElemsize(emitAttr size)
545 {
546     return (size == EA_8BYTE) || (size == EA_4BYTE) || (size == EA_2BYTE) || (size == EA_1BYTE);
547 }
548
549 inline static bool isValidVectorFcvtsize(emitAttr size)
550 {
551     return (size == EA_8BYTE) || (size == EA_4BYTE) || (size == EA_2BYTE);
552 }
553
554 inline static bool isValidVectorElemsizeFloat(emitAttr size)
555 {
556     return (size == EA_8BYTE) || (size == EA_4BYTE);
557 }
558
559 inline static bool isGeneralRegister(regNumber reg)
560 {
561     return (reg >= REG_INT_FIRST) && (reg <= REG_LR);
562 } // Excludes REG_ZR
563
564 inline static bool isGeneralRegisterOrZR(regNumber reg)
565 {
566     return (reg >= REG_INT_FIRST) && (reg <= REG_ZR);
567 } // Includes REG_ZR
568
569 inline static bool isGeneralRegisterOrSP(regNumber reg)
570 {
571     return isGeneralRegister(reg) || (reg == REG_SP);
572 } // Includes REG_SP, Excludes REG_ZR
573
574 inline static bool isVectorRegister(regNumber reg)
575 {
576     return (reg >= REG_FP_FIRST && reg <= REG_FP_LAST);
577 }
578
579 inline static bool isFloatReg(regNumber reg)
580 {
581     return isVectorRegister(reg);
582 }
583
584 inline static bool insOptsNone(insOpts opt)
585 {
586     return (opt == INS_OPTS_NONE);
587 }
588
589 inline static bool insOptsIndexed(insOpts opt)
590 {
591     return (opt == INS_OPTS_PRE_INDEX) || (opt == INS_OPTS_POST_INDEX);
592 }
593
594 inline static bool insOptsPreIndex(insOpts opt)
595 {
596     return (opt == INS_OPTS_PRE_INDEX);
597 }
598
599 inline static bool insOptsPostIndex(insOpts opt)
600 {
601     return (opt == INS_OPTS_POST_INDEX);
602 }
603
604 inline static bool insOptsLSL12(insOpts opt) // special 12-bit shift only used for imm12
605 {
606     return (opt == INS_OPTS_LSL12);
607 }
608
609 inline static bool insOptsAnyShift(insOpts opt)
610 {
611     return ((opt >= INS_OPTS_LSL) && (opt <= INS_OPTS_ROR));
612 }
613
614 inline static bool insOptsAluShift(insOpts opt) // excludes ROR
615 {
616     return ((opt >= INS_OPTS_LSL) && (opt <= INS_OPTS_ASR));
617 }
618
619 inline static bool insOptsVectorImmShift(insOpts opt)
620 {
621     return ((opt == INS_OPTS_LSL) || (opt == INS_OPTS_MSL));
622 }
623
624 inline static bool insOptsLSL(insOpts opt)
625 {
626     return (opt == INS_OPTS_LSL);
627 }
628
629 inline static bool insOptsLSR(insOpts opt)
630 {
631     return (opt == INS_OPTS_LSR);
632 }
633
634 inline static bool insOptsASR(insOpts opt)
635 {
636     return (opt == INS_OPTS_ASR);
637 }
638
639 inline static bool insOptsROR(insOpts opt)
640 {
641     return (opt == INS_OPTS_ROR);
642 }
643
644 inline static bool insOptsAnyExtend(insOpts opt)
645 {
646     return ((opt >= INS_OPTS_UXTB) && (opt <= INS_OPTS_SXTX));
647 }
648
649 inline static bool insOptsLSExtend(insOpts opt)
650 {
651     return ((opt == INS_OPTS_NONE) || (opt == INS_OPTS_LSL) || (opt == INS_OPTS_UXTW) || (opt == INS_OPTS_SXTW) ||
652             (opt == INS_OPTS_UXTX) || (opt == INS_OPTS_SXTX));
653 }
654
655 inline static bool insOpts32BitExtend(insOpts opt)
656 {
657     return ((opt == INS_OPTS_UXTW) || (opt == INS_OPTS_SXTW));
658 }
659
660 inline static bool insOpts64BitExtend(insOpts opt)
661 {
662     return ((opt == INS_OPTS_UXTX) || (opt == INS_OPTS_SXTX));
663 }
664
665 inline static bool insOptsAnyArrangement(insOpts opt)
666 {
667     return ((opt >= INS_OPTS_8B) && (opt <= INS_OPTS_2D));
668 }
669
670 inline static bool insOptsConvertFloatToFloat(insOpts opt)
671 {
672     return ((opt >= INS_OPTS_S_TO_D) && (opt <= INS_OPTS_D_TO_H));
673 }
674
675 inline static bool insOptsConvertFloatToInt(insOpts opt)
676 {
677     return ((opt >= INS_OPTS_S_TO_4BYTE) && (opt <= INS_OPTS_D_TO_8BYTE));
678 }
679
680 inline static bool insOptsConvertIntToFloat(insOpts opt)
681 {
682     return ((opt >= INS_OPTS_4BYTE_TO_S) && (opt <= INS_OPTS_8BYTE_TO_D));
683 }
684
685 static bool isValidImmCond(ssize_t imm);
686 static bool isValidImmCondFlags(ssize_t imm);
687 static bool isValidImmCondFlagsImm5(ssize_t imm);
688
689 /************************************************************************/
690 /*           The public entry points to output instructions             */
691 /************************************************************************/
692
693 public:
694 void emitIns(instruction ins);
695
696 void emitIns_I(instruction ins, emitAttr attr, ssize_t imm);
697
698 void emitIns_R(instruction ins, emitAttr attr, regNumber reg);
699
700 void emitIns_R_I(instruction ins, emitAttr attr, regNumber reg, ssize_t imm, insOpts opt = INS_OPTS_NONE);
701
702 void emitIns_R_F(instruction ins, emitAttr attr, regNumber reg, double immDbl, insOpts opt = INS_OPTS_NONE);
703
704 void emitIns_R_R(instruction ins, emitAttr attr, regNumber reg1, regNumber reg2, insOpts opt = INS_OPTS_NONE);
705
706 void emitIns_R_R(instruction ins, emitAttr attr, regNumber reg1, regNumber reg2, insFlags flags)
707 {
708     emitIns_R_R(ins, attr, reg1, reg2);
709 }
710
711 void emitIns_R_I_I(
712     instruction ins, emitAttr attr, regNumber reg1, ssize_t imm1, ssize_t imm2, insOpts opt = INS_OPTS_NONE);
713
714 void emitIns_R_R_I(
715     instruction ins, emitAttr attr, regNumber reg1, regNumber reg2, ssize_t imm, insOpts opt = INS_OPTS_NONE);
716
717 // Checks for a large immediate that needs a second instruction
718 void emitIns_R_R_Imm(instruction ins, emitAttr attr, regNumber reg1, regNumber reg2, ssize_t imm);
719
720 void emitIns_R_R_R(
721     instruction ins, emitAttr attr, regNumber reg1, regNumber reg2, regNumber reg3, insOpts opt = INS_OPTS_NONE);
722
723 void emitIns_R_R_R_I(instruction ins,
724                      emitAttr    attr,
725                      regNumber   reg1,
726                      regNumber   reg2,
727                      regNumber   reg3,
728                      ssize_t     imm,
729                      insOpts     opt = INS_OPTS_NONE);
730
731 void emitIns_R_R_R_Ext(instruction ins,
732                        emitAttr    attr,
733                        regNumber   reg1,
734                        regNumber   reg2,
735                        regNumber   reg3,
736                        insOpts     opt         = INS_OPTS_NONE,
737                        int         shiftAmount = -1);
738
739 void emitIns_R_R_I_I(instruction ins, emitAttr attr, regNumber reg1, regNumber reg2, int imm1, int imm2);
740
741 void emitIns_R_R_R_R(instruction ins, emitAttr attr, regNumber reg1, regNumber reg2, regNumber reg3, regNumber reg4);
742
743 void emitIns_R_COND(instruction ins, emitAttr attr, regNumber reg, insCond cond);
744
745 void emitIns_R_R_COND(instruction ins, emitAttr attr, regNumber reg1, regNumber reg2, insCond cond);
746
747 void emitIns_R_R_R_COND(instruction ins, emitAttr attr, regNumber reg1, regNumber reg2, regNumber reg3, insCond cond);
748
749 void emitIns_R_R_FLAGS_COND(
750     instruction ins, emitAttr attr, regNumber reg1, regNumber reg2, insCflags flags, insCond cond);
751
752 void emitIns_R_I_FLAGS_COND(instruction ins, emitAttr attr, regNumber reg1, int imm, insCflags flags, insCond cond);
753
754 void emitIns_BARR(instruction ins, insBarrier barrier);
755
756 void emitIns_C(instruction ins, emitAttr attr, CORINFO_FIELD_HANDLE fdlHnd, int offs);
757
758 void emitIns_S(instruction ins, emitAttr attr, int varx, int offs);
759
760 void emitIns_S_R(instruction ins, emitAttr attr, regNumber ireg, int varx, int offs);
761
762 void emitIns_R_S(instruction ins, emitAttr attr, regNumber ireg, int varx, int offs);
763
764 void emitIns_S_I(instruction ins, emitAttr attr, int varx, int offs, int val);
765
766 void emitIns_R_C(
767     instruction ins, emitAttr attr, regNumber reg, regNumber tmpReg, CORINFO_FIELD_HANDLE fldHnd, int offs);
768
769 void emitIns_C_R(instruction ins, emitAttr attr, CORINFO_FIELD_HANDLE fldHnd, regNumber reg, int offs);
770
771 void emitIns_C_I(instruction ins, emitAttr attr, CORINFO_FIELD_HANDLE fdlHnd, ssize_t offs, ssize_t val);
772
773 void emitIns_R_L(instruction ins, emitAttr attr, BasicBlock* dst, regNumber reg);
774
775 void emitIns_R_D(instruction ins, emitAttr attr, unsigned offs, regNumber reg);
776
777 void emitIns_J_R(instruction ins, emitAttr attr, BasicBlock* dst, regNumber reg);
778
779 void emitIns_I_AR(
780     instruction ins, emitAttr attr, int val, regNumber reg, int offs, int memCookie = 0, void* clsCookie = NULL);
781
782 void emitIns_R_AR(
783     instruction ins, emitAttr attr, regNumber ireg, regNumber reg, int offs, int memCookie = 0, void* clsCookie = NULL);
784
785 void emitIns_R_AI(instruction ins, emitAttr attr, regNumber ireg, ssize_t disp);
786
787 void emitIns_AR_R(
788     instruction ins, emitAttr attr, regNumber ireg, regNumber reg, int offs, int memCookie = 0, void* clsCookie = NULL);
789
790 void emitIns_R_ARR(instruction ins, emitAttr attr, regNumber ireg, regNumber reg, regNumber rg2, int disp);
791
792 void emitIns_ARR_R(instruction ins, emitAttr attr, regNumber ireg, regNumber reg, regNumber rg2, int disp);
793
794 void emitIns_R_ARX(
795     instruction ins, emitAttr attr, regNumber ireg, regNumber reg, regNumber rg2, unsigned mul, int disp);
796
797 enum EmitCallType
798 {
799
800     // I have included here, but commented out, all the values used by the x86 emitter.
801     // However, ARM has a much reduced instruction set, and so the ARM emitter only
802     // supports a subset of the x86 variants.  By leaving them commented out, it becomes
803     // a compile time error if code tries to use them (and hopefully see this comment
804     // and know why they are unavailible on ARM), while making it easier to stay
805     // in-sync with x86 and possibly add them back in if needed.
806
807     EC_FUNC_TOKEN, //   Direct call to a helper/static/nonvirtual/global method
808                    //  EC_FUNC_TOKEN_INDIR,    // Indirect call to a helper/static/nonvirtual/global method
809     EC_FUNC_ADDR,  // Direct call to an absolute address
810
811     //  EC_FUNC_VIRTUAL,        // Call to a virtual method (using the vtable)
812     EC_INDIR_R, // Indirect call via register
813                 //  EC_INDIR_SR,            // Indirect call via stack-reference (local var)
814                 //  EC_INDIR_C,             // Indirect call via static class var
815                 //  EC_INDIR_ARD,           // Indirect call via an addressing mode
816
817     EC_COUNT
818 };
819
820 void emitIns_Call(EmitCallType          callType,
821                   CORINFO_METHOD_HANDLE methHnd,
822                   INDEBUG_LDISASM_COMMA(CORINFO_SIG_INFO* sigInfo) // used to report call sites to the EE
823                   void*            addr,
824                   ssize_t          argSize,
825                   emitAttr         retSize,
826                   emitAttr         secondRetSize,
827                   VARSET_VALARG_TP ptrVars,
828                   regMaskTP        gcrefRegs,
829                   regMaskTP        byrefRegs,
830                   IL_OFFSETX       ilOffset      = BAD_IL_OFFSET,
831                   regNumber        ireg          = REG_NA,
832                   regNumber        xreg          = REG_NA,
833                   unsigned         xmul          = 0,
834                   ssize_t          disp          = 0,
835                   bool             isJump        = false,
836                   bool             isNoGC        = false,
837                   bool             isProfLeaveCB = false);
838
839 BYTE* emitOutputLJ(insGroup* ig, BYTE* dst, instrDesc* i);
840 unsigned emitOutputCall(insGroup* ig, BYTE* dst, instrDesc* i, code_t code);
841 BYTE* emitOutputLoadLabel(BYTE* dst, BYTE* srcAddr, BYTE* dstAddr, instrDescJmp* id);
842 BYTE* emitOutputShortBranch(BYTE* dst, instruction ins, insFormat fmt, ssize_t distVal, instrDescJmp* id);
843 BYTE* emitOutputShortAddress(BYTE* dst, instruction ins, insFormat fmt, ssize_t distVal, regNumber reg);
844 BYTE* emitOutputShortConstant(
845     BYTE* dst, instruction ins, insFormat fmt, ssize_t distVal, regNumber reg, emitAttr opSize);
846
847 /*****************************************************************************
848  *
849  *  Given an instrDesc, return true if it's a conditional jump.
850  */
851
852 inline bool emitIsCondJump(instrDesc* jmp)
853 {
854     return ((jmp->idInsFmt() == IF_BI_0B) || (jmp->idInsFmt() == IF_LARGEJMP));
855 }
856
857 /*****************************************************************************
858  *
859  *  Given an instrDesc, return true if it's a compare and jump.
860  */
861
862 inline bool emitIsCmpJump(instrDesc* jmp)
863 {
864     return ((jmp->idInsFmt() == IF_BI_1A) || (jmp->idInsFmt() == IF_BI_1B));
865 }
866
867 /*****************************************************************************
868  *
869  *  Given a instrDesc, return true if it's an unconditional jump.
870  */
871
872 inline bool emitIsUncondJump(instrDesc* jmp)
873 {
874     return (jmp->idInsFmt() == IF_BI_0A);
875 }
876
877 /*****************************************************************************
878  *
879  *  Given a instrDesc, return true if it's a direct call.
880  */
881
882 inline bool emitIsDirectCall(instrDesc* call)
883 {
884     return (call->idInsFmt() == IF_BI_0C);
885 }
886
887 /*****************************************************************************
888  *
889  *  Given a instrDesc, return true if it's a load label instruction.
890  */
891
892 inline bool emitIsLoadLabel(instrDesc* jmp)
893 {
894     return ((jmp->idInsFmt() == IF_DI_1E) || // adr or arp
895             (jmp->idInsFmt() == IF_LARGEADR));
896 }
897
898 /*****************************************************************************
899 *
900 *  Given a instrDesc, return true if it's a load constant instruction.
901 */
902
903 inline bool emitIsLoadConstant(instrDesc* jmp)
904 {
905     return ((jmp->idInsFmt() == IF_LS_1A) || // ldr
906             (jmp->idInsFmt() == IF_LARGELDC));
907 }
908
909 #endif // _TARGET_ARM64_