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