1 /*------------------------------------------------------------------*/
3 /* Name - mini-s390.c */
5 /* Function - S/390 backend for the Mono code generator. */
7 /* Name - Neale Ferguson (Neale.Ferguson@SoftwareAG-usa.com) */
9 /* Date - January, 2004 */
11 /* Derivation - From mini-x86 & mini-ppc by - */
12 /* Paolo Molaro (lupus@ximian.com) */
13 /* Dietmar Maurer (dietmar@ximian.com) */
15 /*------------------------------------------------------------------*/
17 /*------------------------------------------------------------------*/
19 /*------------------------------------------------------------------*/
21 #define MAX_ARCH_DELEGATE_PARAMS 7
23 #define EMIT_COND_BRANCH(ins,cond) \
25 if (ins->inst_true_bb->native_offset) { \
27 displace = ((cfg->native_code + \
28 ins->inst_true_bb->native_offset) - code) / 2; \
29 if (s390_is_imm16(displace)) { \
30 s390_brc (code, cond, displace); \
32 s390_jcl (code, cond, displace); \
35 mono_add_patch_info (cfg, code - cfg->native_code, \
36 MONO_PATCH_INFO_BB, ins->inst_true_bb); \
37 s390_jcl (code, cond, 0); \
41 #define EMIT_UNCOND_BRANCH(ins) \
43 if (ins->inst_target_bb->native_offset) { \
45 displace = ((cfg->native_code + \
46 ins->inst_target_bb->native_offset) - code) / 2; \
47 if (s390_is_imm16(displace)) { \
48 s390_brc (code, S390_CC_UN, displace); \
50 s390_jcl (code, S390_CC_UN, displace); \
53 mono_add_patch_info (cfg, code - cfg->native_code, \
54 MONO_PATCH_INFO_BB, ins->inst_target_bb); \
55 s390_jcl (code, S390_CC_UN, 0); \
59 #define EMIT_COND_SYSTEM_EXCEPTION(cond,exc_name) \
61 mono_add_patch_info (cfg, code - cfg->native_code, \
62 MONO_PATCH_INFO_EXC, exc_name); \
63 s390_jcl (code, cond, 0); \
66 #define CHECK_SRCDST_COM \
67 if (ins->dreg == ins->sreg2) { \
71 if (ins->dreg != ins->sreg1) { \
72 s390_lgr (code, ins->dreg, ins->sreg1); \
76 #define CHECK_SRCDST_NCOM \
77 if (ins->dreg == ins->sreg2) { \
79 s390_lgr (code, s390_r13, ins->sreg2); \
83 if (ins->dreg != ins->sreg1) { \
84 s390_lgr (code, ins->dreg, ins->sreg1); \
87 #define CHECK_SRCDST_COM_I \
88 if (ins->dreg == ins->sreg2) { \
92 if (ins->dreg != ins->sreg1) { \
93 s390_lgfr (code, ins->dreg, ins->sreg1); \
97 #define CHECK_SRCDST_NCOM_I \
98 if (ins->dreg == ins->sreg2) { \
100 s390_lgfr (code, s390_r13, ins->sreg2); \
104 if (ins->dreg != ins->sreg1) { \
105 s390_lgfr (code, ins->dreg, ins->sreg1); \
108 #define CHECK_SRCDST_COM_F \
109 if (ins->dreg == ins->sreg2) { \
113 if (ins->dreg != ins->sreg1) { \
114 s390_ldr (code, ins->dreg, ins->sreg1); \
118 #define CHECK_SRCDST_NCOM_F \
119 if (ins->dreg == ins->sreg2) { \
121 s390_ldr (code, s390_r13, ins->sreg2); \
125 if (ins->dreg != ins->sreg1) { \
126 s390_ldr (code, ins->dreg, ins->sreg1); \
129 #define MONO_EMIT_NEW_MOVE(cfg,dest,offset,src,imm,size) do { \
132 MONO_INST_NEW (cfg, inst, OP_NOP); \
135 inst->inst_offset = offset; \
137 inst->inst_imm = imm; \
139 if (s390_is_uimm12(offset)) { \
141 inst->inst_offset = offset; \
143 dReg = mono_alloc_preg (cfg); \
144 MONO_EMIT_NEW_BIALU_IMM(cfg, OP_ADD_IMM, \
145 dReg, dest, offset); \
147 inst->inst_offset = 0; \
149 if (s390_is_uimm12(imm)) { \
151 inst->inst_imm = imm; \
153 sReg = mono_alloc_preg (cfg); \
154 MONO_EMIT_NEW_BIALU_IMM(cfg, OP_ADD_IMM, \
156 inst->sreg1 = sReg; \
157 inst->inst_imm = 0; \
160 inst->opcode = OP_S390_MOVE; \
161 inst->backend.size = size; \
162 MONO_ADD_INS (cfg->cbb, inst); \
165 #define MONO_OUTPUT_VTR(cfg, size, dr, sr, so) do { \
166 int reg = mono_alloc_preg (cfg); \
169 MONO_EMIT_NEW_ICONST(cfg, reg, 0); \
172 MONO_EMIT_NEW_LOAD_MEMBASE_OP(cfg, OP_LOADU1_MEMBASE, \
176 MONO_EMIT_NEW_LOAD_MEMBASE_OP(cfg, OP_LOADU2_MEMBASE, \
180 MONO_EMIT_NEW_LOAD_MEMBASE_OP(cfg, OP_LOADI4_MEMBASE, \
184 MONO_EMIT_NEW_LOAD_MEMBASE_OP(cfg, OP_LOADI8_MEMBASE, \
188 mono_call_inst_add_outarg_reg(cfg, call, reg, dr, FALSE); \
191 #define MONO_OUTPUT_VTS(cfg, size, dr, dx, sr, so) do { \
195 tmpr = mono_alloc_preg (cfg); \
196 MONO_EMIT_NEW_ICONST(cfg, tmpr, 0); \
197 MONO_EMIT_NEW_STORE_MEMBASE(cfg, OP_STORE_MEMBASE_REG, \
201 tmpr = mono_alloc_preg (cfg); \
202 MONO_EMIT_NEW_LOAD_MEMBASE_OP(cfg, OP_LOADU1_MEMBASE, \
204 MONO_EMIT_NEW_STORE_MEMBASE(cfg, OP_STORE_MEMBASE_REG, \
208 tmpr = mono_alloc_preg (cfg); \
209 MONO_EMIT_NEW_LOAD_MEMBASE_OP(cfg, OP_LOADU2_MEMBASE, \
211 MONO_EMIT_NEW_STORE_MEMBASE(cfg, OP_STORE_MEMBASE_REG, \
215 tmpr = mono_alloc_preg (cfg); \
216 MONO_EMIT_NEW_LOAD_MEMBASE_OP(cfg, OP_LOADI4_MEMBASE, \
218 MONO_EMIT_NEW_STORE_MEMBASE(cfg, OP_STORE_MEMBASE_REG, \
222 MONO_EMIT_NEW_MOVE (cfg, dr, dx, sr, so, size); \
228 #define DEBUG(a) if (cfg->verbose_level > 1) a
232 #define S390_TRACE_STACK_SIZE (5*sizeof(gpointer)+4*sizeof(gdouble))
234 #define BREAKPOINT_SIZE sizeof(breakpoint_t)
235 #define S390X_NOP_SIZE sizeof(I_Format)
237 #define MAX(a, b) ((a) > (b) ? (a) : (b))
240 * imt thunking size values
243 #define LOADCON_SIZE 20
247 #define ENABLE_WRONG_METHOD_CHECK 0
249 #define mono_mini_arch_lock() EnterCriticalSection (&mini_arch_mutex)
250 #define mono_mini_arch_unlock() LeaveCriticalSection (&mini_arch_mutex)
252 /*========================= End of Defines =========================*/
254 /*------------------------------------------------------------------*/
255 /* I n c l u d e s */
256 /*------------------------------------------------------------------*/
261 #include <mono/metadata/appdomain.h>
262 #include <mono/metadata/debug-helpers.h>
263 #include <mono/metadata/profiler-private.h>
264 #include <mono/utils/mono-math.h>
265 #include <mono/utils/mono-mmap.h>
266 #include <mono/utils/mono-hwcap-s390x.h>
268 #include "mini-s390x.h"
269 #include "cpu-s390x.h"
270 #include "jit-icalls.h"
274 /*========================= End of Includes ========================*/
276 /*------------------------------------------------------------------*/
277 /* T y p e d e f s */
278 /*------------------------------------------------------------------*/
290 /*------------------------------------------------------------------*/
291 /* Used by the instrument_emit_epilog */
292 /*------------------------------------------------------------------*/
303 typedef struct InstList InstList;
317 RegTypeStructByValInFP,
319 RegTypeStructByAddrOnStack
323 gint32 offset; /* offset from caller's stack */
324 gint32 offparm; /* offset from callee's stack */
325 guint16 vtsize; /* in param area */
328 guint32 size; /* Size of structure used by RegTypeStructByVal */
329 gint32 type; /* Data type of argument */
345 gint64 gr[5]; /* R2-R6 */
346 gdouble fp[3]; /* F0-F2 */
347 } __attribute__ ((packed)) RegParm;
355 } __attribute__ ((packed)) breakpoint_t;
357 /*========================= End of Typedefs ========================*/
359 /*------------------------------------------------------------------*/
360 /* P r o t o t y p e s */
361 /*------------------------------------------------------------------*/
363 static void indent (int);
364 static guint8 * backUpStackPtr(MonoCompile *, guint8 *);
365 static void decodeParm (MonoType *, void *, int);
366 static void enter_method (MonoMethod *, RegParm *, char *);
367 static void leave_method (MonoMethod *, ...);
368 static inline void add_general (guint *, size_data *, ArgInfo *);
369 static inline void add_stackParm (guint *, size_data *, ArgInfo *, gint);
370 static inline void add_float (guint *, size_data *, ArgInfo *);
371 static CallInfo * get_call_info (MonoCompile *, MonoMemPool *, MonoMethodSignature *);
372 static guchar * emit_float_to_int (MonoCompile *, guchar *, int, int, int, gboolean);
373 static guint8 * emit_load_volatile_arguments (guint8 *, MonoCompile *);
374 static __inline__ void emit_unwind_regs(MonoCompile *, guint8 *, int, int, long);
376 /*========================= End of Prototypes ======================*/
378 /*------------------------------------------------------------------*/
379 /* G l o b a l V a r i a b l e s */
380 /*------------------------------------------------------------------*/
382 int mono_exc_esp_offset = 0;
384 static int indent_level = 0;
386 static gint appdomain_tls_offset = -1,
388 lmf_addr_tls_offset = -1;
390 pthread_key_t lmf_addr_key;
392 gboolean lmf_addr_key_inited = FALSE;
398 extern __thread MonoDomain *tls_appdomain;
399 extern __thread MonoThread *tls_current_object;
400 extern __thread gpointer mono_lmf_addr;
405 * The code generated for sequence points reads from this location,
406 * which is made read-only when single stepping is enabled.
408 static gpointer ss_trigger_page;
411 * Enabled breakpoints read from this trigger page
413 static gpointer bp_trigger_page;
415 breakpoint_t breakpointCode;
417 /*====================== End of Global Variables ===================*/
419 /*------------------------------------------------------------------*/
421 /* Name - mono_arch_regname */
423 /* Function - Returns the name of the register specified by */
424 /* the input parameter. */
426 /*------------------------------------------------------------------*/
429 mono_arch_regname (int reg) {
430 static const char * rnames[] = {
431 "s390_r0", "s390_sp", "s390_r2", "s390_r3", "s390_r4",
432 "s390_r5", "s390_r6", "s390_r7", "s390_r8", "s390_r9",
433 "s390_r10", "s390_r11", "s390_r12", "s390_r13", "s390_r14",
437 if (reg >= 0 && reg < 16)
443 /*========================= End of Function ========================*/
445 /*------------------------------------------------------------------*/
447 /* Name - mono_arch_fregname */
449 /* Function - Returns the name of the register specified by */
450 /* the input parameter. */
452 /*------------------------------------------------------------------*/
455 mono_arch_fregname (int reg) {
456 static const char * rnames[] = {
457 "s390_f0", "s390_f1", "s390_f2", "s390_f3", "s390_f4",
458 "s390_f5", "s390_f6", "s390_f7", "s390_f8", "s390_f9",
459 "s390_f10", "s390_f11", "s390_f12", "s390_f13", "s390_f14",
463 if (reg >= 0 && reg < 16)
469 /*========================= End of Function ========================*/
471 /*------------------------------------------------------------------*/
473 /* Name - arch_get_argument_info */
475 /* Function - Gathers information on parameters such as size, */
476 /* alignment, and padding. arg_info should be large */
477 /* enough to hold param_count + 1 entries. */
479 /* Parameters - @csig - Method signature */
480 /* @param_count - No. of parameters to consider */
481 /* @arg_info - An array to store the result info */
483 /* Returns - Size of the activation frame */
485 /*------------------------------------------------------------------*/
488 mono_arch_get_argument_info (MonoGenericSharingContext *gsctx, MonoMethodSignature *csig,
490 MonoJitArgumentInfo *arg_info)
492 int k, frame_size = 0;
493 int size, align, pad;
496 if (MONO_TYPE_ISSTRUCT (csig->ret)) {
497 frame_size += sizeof (gpointer);
501 arg_info [0].offset = offset;
504 frame_size += sizeof (gpointer);
508 arg_info [0].size = frame_size;
510 for (k = 0; k < param_count; k++) {
513 size = mono_type_native_stack_size (csig->params [k], (guint32 *) &align);
515 size = mini_type_stack_size (NULL, csig->params [k], &align);
517 frame_size += pad = (align - (frame_size & (align - 1))) & (align - 1);
518 arg_info [k].pad = pad;
520 arg_info [k + 1].pad = 0;
521 arg_info [k + 1].size = size;
523 arg_info [k + 1].offset = offset;
527 align = MONO_ARCH_FRAME_ALIGNMENT;
528 frame_size += pad = (align - (frame_size & (align - 1))) & (align - 1);
529 arg_info [k].pad = pad;
534 /*========================= End of Function ========================*/
536 /*------------------------------------------------------------------*/
538 /* Name - emit_unwind_regs. */
540 /* Function - Determines if a value can be returned in one or */
543 /*------------------------------------------------------------------*/
545 static void __inline__
546 emit_unwind_regs(MonoCompile *cfg, guint8 *code, int start, int end, long offset)
550 for (i = start; i < end; i++) {
551 mono_emit_unwind_op_offset (cfg, code, i, offset);
552 offset += sizeof(gulong);
556 /*========================= End of Function ========================*/
558 /*------------------------------------------------------------------*/
560 /* Name - retFitsInReg. */
562 /* Function - Determines if a value can be returned in one or */
565 /*------------------------------------------------------------------*/
567 static inline gboolean
568 retFitsInReg(guint32 size)
583 /*========================= End of Function ========================*/
585 /*------------------------------------------------------------------*/
587 /* Name - backStackPtr. */
589 /* Function - Restore Stack Pointer to previous frame. */
591 /*------------------------------------------------------------------*/
593 static inline guint8 *
594 backUpStackPtr(MonoCompile *cfg, guint8 *code)
596 int stackSize = cfg->stack_usage;
598 if (cfg->frame_reg != STK_BASE)
599 s390_lgr (code, STK_BASE, cfg->frame_reg);
601 if (s390_is_imm16 (stackSize)) {
602 s390_aghi (code, STK_BASE, stackSize);
604 while (stackSize > 32767) {
605 s390_aghi (code, STK_BASE, 32767);
608 s390_aghi (code, STK_BASE, stackSize);
613 /*========================= End of Function ========================*/
615 /*------------------------------------------------------------------*/
619 /* Function - Perform nice indenting to current level */
621 /*------------------------------------------------------------------*/
627 indent_level += diff;
634 indent_level += diff;
637 /*========================= End of Function ========================*/
639 /*------------------------------------------------------------------*/
641 /* Name - cvtMonoType */
643 /* Function - Convert a mono-type to a string. */
645 /*------------------------------------------------------------------*/
648 cvtMonoType(MonoTypeEnum t)
653 return "MONO_TYPE_END";
655 return "MONO_TYPE_VOID";
656 case MONO_TYPE_BOOLEAN:
657 return "MONO_TYPE_BOOLEAN";
659 return "MONO_TYPE_CHAR";
661 return "MONO_TYPE_I1";
663 return "MONO_TYPE_U1";
665 return "MONO_TYPE_I2";
667 return "MONO_TYPE_U2";
669 return "MONO_TYPE_I4";
671 return "MONO_TYPE_U4";
673 return "MONO_TYPE_I8";
675 return "MONO_TYPE_U8";
677 return "MONO_TYPE_R4";
679 return "MONO_TYPE_R8";
680 case MONO_TYPE_STRING:
681 return "MONO_TYPE_STRING";
683 return "MONO_TYPE_PTR";
684 case MONO_TYPE_BYREF:
685 return "MONO_TYPE_BYREF";
686 case MONO_TYPE_VALUETYPE:
687 return "MONO_TYPE_VALUETYPE";
688 case MONO_TYPE_CLASS:
689 return "MONO_TYPE_CLASS";
691 return "MONO_TYPE_VAR";
692 case MONO_TYPE_ARRAY:
693 return "MONO_TYPE_ARRAY";
694 case MONO_TYPE_GENERICINST:
695 return "MONO_TYPE_GENERICINST";
696 case MONO_TYPE_TYPEDBYREF:
697 return "MONO_TYPE_TYPEDBYREF";
699 return "MONO_TYPE_I";
701 return "MONO_TYPE_U";
702 case MONO_TYPE_FNPTR:
703 return "MONO_TYPE_FNPTR";
704 case MONO_TYPE_OBJECT:
705 return "MONO_TYPE_OBJECT";
706 case MONO_TYPE_SZARRAY:
707 return "MONO_TYPE_SZARRAY";
709 return "MONO_TYPE_MVAR";
710 case MONO_TYPE_CMOD_REQD:
711 return "MONO_TYPE_CMOD_REQD";
712 case MONO_TYPE_CMOD_OPT:
713 return "MONO_TYPE_CMOD_OPT";
714 case MONO_TYPE_INTERNAL:
715 return "MONO_TYPE_INTERNAL";
716 case MONO_TYPE_MODIFIER:
717 return "MONO_TYPE_MODIFIER";
718 case MONO_TYPE_SENTINEL:
719 return "MONO_TYPE_SENTINEL";
720 case MONO_TYPE_PINNED:
721 return "MONO_TYPE_PINNED";
728 /*========================= End of Function ========================*/
730 /*------------------------------------------------------------------*/
732 /* Name - decodeParm */
734 /* Function - Decode a parameter for the trace. */
736 /*------------------------------------------------------------------*/
739 decodeParm(MonoType *type, void *curParm, int size)
744 printf("[BYREF:%p], ", *((char **) curParm));
746 simpleType = mono_type_get_underlying_type(type)->type;
748 switch (simpleType) {
750 printf ("[INTPTR:%p], ", *((int **) curParm));
753 printf ("[UINTPTR:%p], ", *((int **) curParm));
755 case MONO_TYPE_BOOLEAN :
756 printf ("[BOOL:%ld], ", *((gint64 *) curParm));
758 case MONO_TYPE_CHAR :
759 printf ("[CHAR:%c], ", *((int *) curParm));
762 printf ("[INT1:%ld], ", *((gint64 *) curParm));
765 printf ("[INT2:%ld], ", *((gint64 *) curParm));
768 printf ("[INT4:%ld], ", *((gint64 *) curParm));
771 printf ("[UINT1:%lu], ", *((guint64 *) curParm));
774 printf ("[UINT2:%lu], ", *((guint64 *) curParm));
777 printf ("[UINT4:%lu], ", *((guint64 *) curParm));
780 printf ("[UINT8:%lu], ", *((guint64 *) curParm));
782 case MONO_TYPE_STRING : {
783 MonoString *s = *((MonoString **) curParm);
785 g_assert (((MonoObject *) s)->vtable->klass == mono_defaults.string_class);
786 printf("[STRING:%p:%s], ", s, mono_string_to_utf8(s));
788 printf("[STRING:null], ");
792 case MONO_TYPE_CLASS :
793 case MONO_TYPE_OBJECT : {
794 MonoObject *obj = *((MonoObject **) curParm);
796 if ((obj) && (obj->vtable)) {
797 printf("[CLASS/OBJ:");
798 class = obj->vtable->klass;
799 printf("%p [%p] ",obj,curParm);
800 if (class == mono_defaults.string_class) {
801 printf("[STRING:%p:%s]",
802 obj, mono_string_to_utf8 ((MonoString *) obj));
803 } else if (class == mono_defaults.int32_class) {
804 printf("[INT32:%p:%d]",
805 obj, *(gint32 *)((char *)obj + sizeof (MonoObject)));
808 class->name_space, class->name, obj);
811 printf("[OBJECT:null], ");
816 printf("[PTR:%p], ", *((gpointer **) (curParm)));
818 case MONO_TYPE_FNPTR :
819 printf("[FNPTR:%p], ", *((gpointer **) (curParm)));
821 case MONO_TYPE_ARRAY :
822 printf("[ARRAY:%p], ", *((gpointer **) (curParm)));
824 case MONO_TYPE_SZARRAY :
825 printf("[SZARRAY:%p], ", *((gpointer **) (curParm)));
828 printf("[INT8:%ld], ", *((gint64 *) (curParm)));
831 printf("[FLOAT4:%g], ", *((float *) (curParm)));
834 printf("[FLOAT8:%g], ", *((double *) (curParm)));
836 case MONO_TYPE_VALUETYPE : {
838 MonoMarshalType *info;
840 if (type->data.klass->enumtype) {
841 simpleType = mono_class_enum_basetype (type->data.klass)->type;
842 printf("{VALUETYPE} - ");
846 info = mono_marshal_load_type_info (type->data.klass);
848 if ((info->native_size == sizeof(float)) &&
849 (info->num_fields == 1) &&
850 (info->fields[0].field->type->type == MONO_TYPE_R4)) {
851 printf("[FLOAT4:%f], ", *((float *) (curParm)));
855 if ((info->native_size == sizeof(double)) &&
856 (info->num_fields == 1) &&
857 (info->fields[0].field->type->type == MONO_TYPE_R8)) {
858 printf("[FLOAT8:%g], ", *((double *) (curParm)));
862 printf("[VALUETYPE:");
863 for (i = 0; i < size; i++)
864 printf("%02x,", *((guint8 *)curParm+i));
868 case MONO_TYPE_TYPEDBYREF: {
870 printf("[TYPEDBYREF:");
871 for (i = 0; i < size; i++)
872 printf("%02x,", *((guint8 *)curParm+i));
877 printf("[%s], ",cvtMonoType(simpleType));
882 /*========================= End of Function ========================*/
884 /*------------------------------------------------------------------*/
886 /* Name - enter_method */
888 /* Function - Perform tracing of the entry to the current */
891 /*------------------------------------------------------------------*/
894 enter_method (MonoMethod *method, RegParm *rParm, char *sp)
896 int i, oParm = 0, iParm = 0;
899 MonoMethodSignature *sig;
906 fname = mono_method_full_name (method, TRUE);
908 printf ("ENTER: %s ", fname);
911 ip = (*(guint64 *) (sp+S390_RET_ADDR_OFFSET));
912 printf ("ip: %p sp: %p - ", (gpointer) ip, sp);
917 sig = mono_method_signature (method);
919 cinfo = get_call_info (NULL, NULL, sig);
921 if (cinfo->struct_ret) {
922 printf ("[STRUCTRET:%p], ", (gpointer) rParm->gr[0]);
927 gpointer *this = (gpointer *) rParm->gr[iParm];
928 obj = (MonoObject *) this;
929 switch(method->klass->this_arg.type) {
930 case MONO_TYPE_VALUETYPE:
932 guint64 *value = (guint64 *) ((uintptr_t)this + sizeof(MonoObject));
933 printf("this:[value:%p:%016lx], ", this, *value);
935 printf ("this:[NULL], ");
937 case MONO_TYPE_STRING:
940 class = obj->vtable->klass;
941 if (class == mono_defaults.string_class) {
942 printf ("this:[STRING:%p:%s], ",
943 obj, mono_string_to_utf8 ((MonoString *)obj));
945 printf ("this:%p[%s.%s], ",
946 obj, class->name_space, class->name);
949 printf("vtable:[NULL], ");
951 printf ("this:[NULL], ");
954 printf("this[%s]: %p, ",cvtMonoType(method->klass->this_arg.type),this);
959 for (i = 0; i < sig->param_count; ++i) {
960 ainfo = &cinfo->args[i + oParm];
961 switch (ainfo->regtype) {
962 case RegTypeGeneral :
963 decodeParm(sig->params[i], &(rParm->gr[ainfo->reg-2]), ainfo->size);
966 decodeParm(sig->params[i], &(rParm->fp[ainfo->reg]), ainfo->size);
969 decodeParm(sig->params[i], sp+ainfo->offset, ainfo->size);
971 case RegTypeStructByVal :
972 if (ainfo->reg != STK_BASE) {
973 int offset = sizeof(glong) - ainfo->size;
974 curParm = &(rParm->gr[ainfo->reg-2])+offset;
977 curParm = sp+ainfo->offset;
979 if (retFitsInReg (ainfo->vtsize))
980 decodeParm(sig->params[i],
984 decodeParm(sig->params[i],
985 *((char **) curParm),
988 case RegTypeStructByAddr :
989 if (ainfo->reg != STK_BASE)
990 curParm = &(rParm->gr[ainfo->reg-2]);
992 curParm = sp+ainfo->offset;
994 decodeParm(sig->params[i],
995 *((char **) curParm),
1007 /*========================= End of Function ========================*/
1009 /*------------------------------------------------------------------*/
1011 /* Name - leave_method */
1015 /*------------------------------------------------------------------*/
1018 leave_method (MonoMethod *method, ...)
1025 va_start(ap, method);
1027 fname = mono_method_full_name (method, TRUE);
1029 printf ("LEAVE: %s", fname);
1032 type = mono_method_signature (method)->ret;
1035 switch (type->type) {
1036 case MONO_TYPE_VOID:
1038 case MONO_TYPE_BOOLEAN: {
1039 int val = va_arg (ap, int);
1041 printf ("[TRUE:%d]", val);
1047 case MONO_TYPE_CHAR: {
1048 int val = va_arg (ap, int);
1049 printf ("[CHAR:%d]", val);
1052 case MONO_TYPE_I1: {
1053 int val = va_arg (ap, int);
1054 printf ("[INT1:%d]", val);
1057 case MONO_TYPE_U1: {
1058 int val = va_arg (ap, int);
1059 printf ("[UINT1:%d]", val);
1062 case MONO_TYPE_I2: {
1063 int val = va_arg (ap, int);
1064 printf ("[INT2:%d]", val);
1067 case MONO_TYPE_U2: {
1068 int val = va_arg (ap, int);
1069 printf ("[UINT2:%d]", val);
1072 case MONO_TYPE_I4: {
1073 int val = va_arg (ap, int);
1074 printf ("[INT4:%d]", val);
1077 case MONO_TYPE_U4: {
1078 int val = va_arg (ap, int);
1079 printf ("[UINT4:%d]", val);
1083 gint64 val = va_arg (ap, gint64);
1084 printf ("[INT:%ld]", val);
1089 gint64 val = va_arg (ap, gint64);
1090 printf ("[UINT:%lu]", val);
1094 case MONO_TYPE_STRING: {
1095 MonoString *s = va_arg (ap, MonoString *);
1098 g_assert (((MonoObject *)s)->vtable->klass == mono_defaults.string_class);
1099 printf ("[STRING:%p:%s]", s, mono_string_to_utf8 (s));
1101 printf ("[STRING:null], ");
1104 case MONO_TYPE_CLASS:
1105 case MONO_TYPE_OBJECT: {
1106 MonoObject *o = va_arg (ap, MonoObject *);
1108 if ((o) && (o->vtable)) {
1109 if (o->vtable->klass == mono_defaults.boolean_class) {
1110 printf ("[BOOLEAN:%p:%d]", o, *((guint8 *)o + sizeof (MonoObject)));
1111 } else if (o->vtable->klass == mono_defaults.int32_class) {
1112 printf ("[INT32:%p:%d]", o, *((gint32 *)((char *)o + sizeof (MonoObject))));
1113 } else if (o->vtable->klass == mono_defaults.int64_class) {
1114 printf ("[INT64:%p:%ld]", o, *((gint64 *)((char *)o + sizeof (MonoObject))));
1116 printf ("[%s.%s:%p]", o->vtable->klass->name_space, o->vtable->klass->name, o);
1118 printf ("[OBJECT:%p]", o);
1123 case MONO_TYPE_FNPTR:
1124 case MONO_TYPE_ARRAY:
1125 case MONO_TYPE_SZARRAY: {
1126 gpointer p = va_arg (ap, gpointer);
1127 printf ("[result=%p]", p);
1130 case MONO_TYPE_I8: {
1131 gint64 l = va_arg (ap, gint64);
1132 printf ("[LONG:%ld]", l);
1135 case MONO_TYPE_U8: {
1136 guint64 l = va_arg (ap, guint64);
1137 printf ("[ULONG:%lu]", l);
1140 case MONO_TYPE_R4: {
1141 double f = va_arg (ap, double);
1142 printf ("[FLOAT4:%g]\n", f);
1145 case MONO_TYPE_R8: {
1146 double f = va_arg (ap, double);
1147 printf ("[FLOAT8:%g]\n", f);
1150 case MONO_TYPE_VALUETYPE: {
1151 MonoMarshalType *info;
1152 if (type->data.klass->enumtype) {
1153 type = mono_class_enum_basetype (type->data.klass);
1158 info = mono_marshal_load_type_info (type->data.klass);
1160 if ((info->native_size == sizeof(float)) &&
1161 (info->num_fields == 1) &&
1162 (info->fields[0].field->type->type == MONO_TYPE_R4)) {
1163 double f = va_arg (ap, double);
1164 printf("[FLOAT4:%g]\n", (double) f);
1168 if ((info->native_size == sizeof(double)) &&
1169 (info->num_fields == 1) &&
1170 (info->fields[0].field->type->type == MONO_TYPE_R8)) {
1171 double f = va_arg (ap, double);
1172 printf("[FLOAT8:%g]\n", f);
1176 size = mono_type_size (type, &align);
1179 guint32 p = va_arg (ap, guint32);
1180 printf ("[%02x]\n",p);
1184 guint32 p = va_arg (ap, guint32);
1185 printf ("[%04x]\n",p);
1189 guint32 p = va_arg (ap, guint32);
1190 printf ("[%08x]\n",p);
1194 guint64 p = va_arg (ap, guint64);
1195 printf ("[%016lx]\n",p);
1199 gpointer p = va_arg (ap, gpointer);
1200 printf ("[VALUETYPE] %p\n",p);
1206 case MONO_TYPE_TYPEDBYREF: {
1207 guint8 *p = va_arg (ap, gpointer);
1209 size = mono_type_size (type, &align);
1216 for (j = 0; p && j < size; j++)
1217 printf ("%02x,", p [j]);
1221 printf ("[TYPEDBYREF]\n");
1226 printf ("(unknown return type %x)",
1227 mono_method_signature (method)->ret->type);
1230 ip = ((gint64) __builtin_return_address (0));
1231 printf (" ip: %p\n", (gpointer) ip);
1234 /*========================= End of Function ========================*/
1236 /*------------------------------------------------------------------*/
1238 /* Name - mono_arch_cpu_init */
1240 /* Function - Perform CPU specific initialization to execute */
1243 /*------------------------------------------------------------------*/
1246 mono_arch_cpu_init (void)
1250 /*========================= End of Function ========================*/
1252 /*------------------------------------------------------------------*/
1254 /* Name - mono_arch_init. */
1256 /* Function - Initialize architecture specific code. */
1258 /*------------------------------------------------------------------*/
1261 mono_arch_init (void)
1267 * When we do an architectural level set at z9 or better
1268 * we can use the STFLE instruction to show us
1269 * what hardware facilities are available
1271 int lFacility = sizeof(facs) % 8;
1273 memset((char *) &facs, 0, sizeof(facs));
1275 __asm__ (" lgfr 0,%1\n"
1277 : "=m" (facs) : "r" (lFacility) : "0", "cc");
1280 ss_trigger_page = mono_valloc (NULL, mono_pagesize (), MONO_MMAP_READ);
1281 bp_trigger_page = mono_valloc (NULL, mono_pagesize (), MONO_MMAP_READ);
1282 mono_mprotect (bp_trigger_page, mono_pagesize (), 0);
1284 code = (guint8 *) &breakpointCode;
1285 s390_basr(code, s390_r13, 0);
1287 s390_llong(code, 0);
1288 s390_lg(code, s390_r13, 0, s390_r13, 4);
1289 s390_lg(code, s390_r0, 0, s390_r13, 0);
1292 /*========================= End of Function ========================*/
1294 /*------------------------------------------------------------------*/
1296 /* Name - mono_arch_cleanup. */
1298 /* Function - Cleanup architecture specific code . */
1300 /*------------------------------------------------------------------*/
1303 mono_arch_cleanup (void)
1307 /*========================= End of Function ========================*/
1309 /*------------------------------------------------------------------*/
1311 /* Name - mono_arch_cpu_optimizations */
1313 /* Function - Returns the optimizations supported on this CPU */
1315 /*------------------------------------------------------------------*/
1318 mono_arch_cpu_optimizations (guint32 *exclude_mask)
1322 /*----------------------------------------------------------*/
1323 /* No s390-specific optimizations yet */
1324 /*----------------------------------------------------------*/
1325 *exclude_mask = MONO_OPT_LINEARS;
1329 /*========================= End of Function ========================*/
1331 /*------------------------------------------------------------------*/
1333 /* Name - mono_arch_cpu_enumerate_simd_versions */
1335 /* Function - Returns the SIMD instruction sets on this CPU */
1337 /*------------------------------------------------------------------*/
1339 mono_arch_cpu_enumerate_simd_versions (void)
1341 /* SIMD is currently unimplemented */
1344 /*========================= End of Function ========================*/
1346 /*------------------------------------------------------------------*/
1348 /* Name - mono_arch_get_allocatable_int_vars */
1352 /*------------------------------------------------------------------*/
1355 mono_arch_get_allocatable_int_vars (MonoCompile *cfg)
1360 for (i = 0; i < cfg->num_varinfo; i++) {
1361 MonoInst *ins = cfg->varinfo [i];
1362 MonoMethodVar *vmv = MONO_VARINFO (cfg, i);
1365 if (vmv->range.first_use.abs_pos >= vmv->range.last_use.abs_pos)
1368 if (ins->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT) ||
1369 (ins->opcode != OP_LOCAL && ins->opcode != OP_ARG))
1372 /* we can only allocate 32 bit values */
1373 if (mono_is_regsize_var(ins->inst_vtype)) {
1374 g_assert (MONO_VARINFO (cfg, i)->reg == -1);
1375 g_assert (i == vmv->idx);
1376 vars = mono_varlist_insert_sorted (cfg, vars, vmv, FALSE);
1383 /*========================= End of Function ========================*/
1385 /*------------------------------------------------------------------*/
1387 /* Name - mono_arch_global_int_regs */
1389 /* Function - Return a list of usable integer registers. */
1391 /*------------------------------------------------------------------*/
1394 mono_arch_get_global_int_regs (MonoCompile *cfg)
1397 MonoMethodHeader *header;
1400 header = cfg->header;
1401 if ((cfg->flags & MONO_CFG_HAS_ALLOCA) || header->num_clauses)
1402 cfg->frame_reg = s390_r11;
1405 /* FIXME: s390_r12 is reserved for bkchain_reg. Only reserve it if needed */
1407 for (i = 8; i < top; ++i) {
1408 if (cfg->frame_reg != i)
1409 regs = g_list_prepend (regs, GUINT_TO_POINTER (i));
1415 /*========================= End of Function ========================*/
1417 /*------------------------------------------------------------------*/
1419 /* Name - mono_arch_flush_icache */
1421 /* Function - Flush the CPU icache. */
1423 /*------------------------------------------------------------------*/
1426 mono_arch_flush_icache (guint8 *code, gint size)
1430 /*========================= End of Function ========================*/
1432 /*------------------------------------------------------------------*/
1434 /* Name - add_general */
1436 /* Function - Determine code and stack size incremements for a */
1439 /*------------------------------------------------------------------*/
1442 add_general (guint *gr, size_data *sz, ArgInfo *ainfo)
1444 if (*gr > S390_LAST_ARG_REG) {
1445 sz->stack_size = S390_ALIGN(sz->stack_size, sizeof(long));
1446 ainfo->offset = sz->stack_size;
1447 ainfo->reg = STK_BASE;
1448 ainfo->regtype = RegTypeBase;
1449 sz->stack_size += sizeof(long);
1450 sz->local_size += sizeof(long);
1451 sz->offStruct += sizeof(long);
1452 sz->code_size += 12;
1460 /*========================= End of Function ========================*/
1462 /*------------------------------------------------------------------*/
1464 /* Name - add_stackParm */
1466 /* Function - Determine code and stack size incremements for a */
1469 /*------------------------------------------------------------------*/
1472 add_stackParm (guint *gr, size_data *sz, ArgInfo *ainfo, gint size)
1474 if (*gr > S390_LAST_ARG_REG) {
1475 sz->stack_size = S390_ALIGN(sz->stack_size, sizeof(long));
1476 ainfo->reg = STK_BASE;
1477 ainfo->offset = sz->stack_size;
1478 ainfo->regtype = RegTypeStructByAddrOnStack;
1479 sz->stack_size += sizeof (gpointer);
1480 sz->parm_size += sizeof(gpointer);
1481 sz->offStruct += sizeof(gpointer);
1484 ainfo->offset = sz->stack_size;
1485 ainfo->regtype = RegTypeStructByAddr;
1488 ainfo->offparm = sz->offset;
1489 sz->offset = S390_ALIGN(sz->offset+size, sizeof(long));
1491 ainfo->vtsize = size;
1492 sz->parm_size += size;
1495 /*========================= End of Function ========================*/
1497 /*------------------------------------------------------------------*/
1499 /* Name - add_float */
1501 /* Function - Determine code and stack size incremements for a */
1502 /* float parameter. */
1504 /*------------------------------------------------------------------*/
1507 add_float (guint *fr, size_data *sz, ArgInfo *ainfo)
1509 if ((*fr) <= S390_LAST_FPARG_REG) {
1510 ainfo->regtype = RegTypeFP;
1516 ainfo->offset = sz->stack_size;
1517 ainfo->reg = STK_BASE;
1518 ainfo->regtype = RegTypeBase;
1520 sz->stack_size += sizeof(double);
1521 sz->local_size += sizeof(double);
1522 sz->offStruct += sizeof(double);
1526 /*========================= End of Function ========================*/
1528 /*------------------------------------------------------------------*/
1530 /* Name - get_call_info */
1532 /* Function - Determine the amount of space required for code */
1533 /* and stack. In addition determine starting points */
1534 /* for stack-based parameters, and area for struct- */
1535 /* ures being returned on the stack. */
1537 /*------------------------------------------------------------------*/
1540 get_call_info (MonoCompile *cfg, MonoMemPool *mp, MonoMethodSignature *sig)
1542 guint i, fr, gr, size, pstart;
1543 int nParm = sig->hasthis + sig->param_count;
1545 guint32 simpleType, align;
1546 gboolean is_pinvoke = sig->pinvoke;
1549 MonoGenericSharingContext *gsctx = cfg ? cfg->generic_sharing_context : NULL;
1552 cinfo = mono_mempool_alloc0 (mp, sizeof (CallInfo) + sizeof (ArgInfo) * nParm);
1554 cinfo = g_malloc0 (sizeof (CallInfo) + sizeof (ArgInfo) * nParm);
1559 cinfo->struct_ret = 0;
1563 sz->offStruct = S390_MINIMAL_STACK_SIZE;
1564 sz->stack_size = S390_MINIMAL_STACK_SIZE;
1571 /*----------------------------------------------------------*/
1572 /* We determine the size of the return code/stack in case we*/
1573 /* need to reserve a register to be used to address a stack */
1574 /* area that the callee will use. */
1575 /*----------------------------------------------------------*/
1577 ret_type = mini_type_get_underlying_type (gsctx, sig->ret);
1578 ret_type = mini_get_basic_type_from_generic (gsctx, ret_type);
1579 simpleType = ret_type->type;
1581 switch (simpleType) {
1582 case MONO_TYPE_BOOLEAN:
1587 case MONO_TYPE_CHAR:
1592 case MONO_TYPE_CLASS:
1593 case MONO_TYPE_OBJECT:
1594 case MONO_TYPE_SZARRAY:
1595 case MONO_TYPE_ARRAY:
1597 case MONO_TYPE_FNPTR:
1598 case MONO_TYPE_STRING:
1599 cinfo->ret.reg = s390_r2;
1604 cinfo->ret.reg = s390_f0;
1609 cinfo->ret.reg = s390_r2;
1612 case MONO_TYPE_GENERICINST:
1613 if (!mono_type_generic_inst_is_valuetype (sig->ret)) {
1614 cinfo->ret.reg = s390_r2;
1619 case MONO_TYPE_VALUETYPE: {
1620 MonoClass *klass = mono_class_from_mono_type (sig->ret);
1621 if (klass->enumtype) {
1622 simpleType = mono_class_enum_basetype (klass)->type;
1625 size = mini_type_stack_size_full (gsctx, &klass->byval_arg, NULL, sig->pinvoke);
1627 cinfo->struct_ret = 1;
1628 cinfo->ret.size = size;
1629 cinfo->ret.vtsize = size;
1632 case MONO_TYPE_TYPEDBYREF:
1633 size = sizeof (MonoTypedRef);
1634 cinfo->struct_ret = 1;
1635 cinfo->ret.size = size;
1636 cinfo->ret.vtsize = size;
1638 case MONO_TYPE_VOID:
1641 g_error ("Can't handle as return value 0x%x", sig->ret->type);
1647 * To simplify get_this_arg_reg () and LLVM integration, emit the vret arg after
1648 * the first argument, allowing 'this' to be always passed in the first arg reg.
1649 * Also do this if the first argument is a reference type, since virtual calls
1650 * are sometimes made using calli without sig->hasthis set, like in the delegate
1653 if (cinfo->struct_ret && !is_pinvoke &&
1655 (sig->param_count > 0 &&
1656 MONO_TYPE_IS_REFERENCE (mini_type_get_underlying_type (gsctx, sig->params [0]))))) {
1658 cinfo->args[nParm].size = sizeof (gpointer);
1659 add_general (&gr, sz, cinfo->args + nParm);
1661 cinfo->args[nParm].size = sizeof (gpointer);
1662 add_general (&gr, sz, &cinfo->args [sig->hasthis + nParm]);
1666 cinfo->vret_arg_index = 1;
1667 cinfo->ret.reg = gr;
1672 cinfo->args[nParm].size = sizeof (gpointer);
1673 add_general (&gr, sz, cinfo->args + nParm);
1677 if (cinfo->struct_ret) {
1678 cinfo->ret.reg = gr;
1683 if ((sig->call_convention == MONO_CALL_VARARG) && (sig->param_count == 0)) {
1684 gr = S390_LAST_ARG_REG + 1;
1685 fr = S390_LAST_FPARG_REG + 1;
1687 /* Emit the signature cookie just before the implicit arguments */
1688 add_general (&gr, sz, &cinfo->sigCookie);
1691 /*----------------------------------------------------------*/
1692 /* We determine the size of the parameter code and stack */
1693 /* requirements by checking the types and sizes of the */
1695 /*----------------------------------------------------------*/
1697 for (i = pstart; i < sig->param_count; ++i) {
1700 /*--------------------------------------------------*/
1701 /* Handle vararg type calls. All args are put on */
1703 /*--------------------------------------------------*/
1704 if ((sig->call_convention == MONO_CALL_VARARG) &&
1705 (i == sig->sentinelpos)) {
1706 gr = S390_LAST_ARG_REG + 1;
1707 fr = S390_LAST_FPARG_REG + 1;
1708 add_general (&gr, sz, &cinfo->sigCookie);
1711 if (sig->params [i]->byref) {
1712 add_general (&gr, sz, cinfo->args+nParm);
1713 cinfo->args[nParm].size = sizeof(gpointer);
1718 ptype = mini_type_get_underlying_type (gsctx, sig->params [i]);
1719 simpleType = ptype->type;
1720 cinfo->args[nParm].type = simpleType;
1721 switch (simpleType) {
1722 case MONO_TYPE_BOOLEAN:
1725 cinfo->args[nParm].size = sizeof(char);
1726 add_general (&gr, sz, cinfo->args+nParm);
1731 case MONO_TYPE_CHAR:
1732 cinfo->args[nParm].size = sizeof(short);
1733 add_general (&gr, sz, cinfo->args+nParm);
1738 cinfo->args[nParm].size = sizeof(int);
1739 add_general (&gr, sz, cinfo->args+nParm);
1745 case MONO_TYPE_FNPTR:
1746 case MONO_TYPE_CLASS:
1747 case MONO_TYPE_OBJECT:
1748 case MONO_TYPE_STRING:
1749 case MONO_TYPE_SZARRAY:
1750 case MONO_TYPE_ARRAY:
1751 cinfo->args[nParm].size = sizeof(gpointer);
1752 add_general (&gr, sz, cinfo->args+nParm);
1757 cinfo->args[nParm].size = sizeof(long long);
1758 add_general (&gr, sz, cinfo->args+nParm);
1762 cinfo->args[nParm].size = sizeof(float);
1763 add_float (&fr, sz, cinfo->args+nParm);
1767 cinfo->args[nParm].size = sizeof(double);
1768 add_float (&fr, sz, cinfo->args+nParm);
1771 case MONO_TYPE_GENERICINST:
1772 if (!mono_type_generic_inst_is_valuetype (ptype)) {
1773 cinfo->args[nParm].size = sizeof(gpointer);
1774 add_general (&gr, sz, cinfo->args+nParm);
1779 case MONO_TYPE_VALUETYPE: {
1780 MonoMarshalType *info;
1781 MonoClass *klass = mono_class_from_mono_type (ptype);
1784 size = mono_class_native_size(klass, NULL);
1786 size = mono_class_value_size(klass, NULL);
1788 if (simpleType != MONO_TYPE_GENERICINST) {
1789 info = mono_marshal_load_type_info(klass);
1791 if ((info->native_size == sizeof(float)) &&
1792 (info->num_fields == 1) &&
1793 (info->fields[0].field->type->type == MONO_TYPE_R4)) {
1794 cinfo->args[nParm].size = sizeof(float);
1795 add_float(&fr, sz, cinfo->args+nParm);
1800 if ((info->native_size == sizeof(double)) &&
1801 (info->num_fields == 1) &&
1802 (info->fields[0].field->type->type == MONO_TYPE_R8)) {
1803 cinfo->args[nParm].size = sizeof(double);
1804 add_float(&fr, sz, cinfo->args+nParm);
1810 cinfo->args[nParm].vtsize = 0;
1811 cinfo->args[nParm].size = 0;
1814 /*----------------------------------*/
1815 /* On S/390, structures of size 1, */
1816 /* 2, 4, and 8 bytes are passed in */
1817 /* (a) register(s). */
1818 /*----------------------------------*/
1824 add_general(&gr, sz, cinfo->args+nParm);
1825 cinfo->args[nParm].size = size;
1826 cinfo->args[nParm].regtype = RegTypeStructByVal;
1828 sz->local_size += sizeof(long);
1831 add_stackParm(&gr, sz, cinfo->args+nParm, size);
1836 case MONO_TYPE_TYPEDBYREF: {
1837 int size = sizeof (MonoTypedRef);
1839 cinfo->args[nParm].vtsize = 0;
1840 cinfo->args[nParm].size = 0;
1843 /*----------------------------------*/
1844 /* On S/390, structures of size 1, */
1845 /* 2, 4, and 8 bytes are passed in */
1846 /* (a) register(s). */
1847 /*----------------------------------*/
1853 add_general(&gr, sz, cinfo->args+nParm);
1854 cinfo->args[nParm].size = size;
1855 cinfo->args[nParm].regtype = RegTypeStructByVal;
1857 sz->local_size += sizeof(long);
1860 add_stackParm(&gr, sz, cinfo->args+nParm, size);
1866 g_error ("Can't trampoline 0x%x", ptype);
1870 /*----------------------------------------------------------*/
1871 /* Handle the case where there are no implicit arguments */
1872 /*----------------------------------------------------------*/
1873 if ((sig->call_convention == MONO_CALL_VARARG) &&
1876 (sig->param_count == sig->sentinelpos)) {
1877 gr = S390_LAST_ARG_REG + 1;
1878 fr = S390_LAST_FPARG_REG + 1;
1879 add_general (&gr, sz, &cinfo->sigCookie);
1882 /*----------------------------------------------------------*/
1883 /* If we are passing a structure back then if it won't be */
1884 /* in a register(s) then we make room at the end of the */
1885 /* parameters that may have been placed on the stack */
1886 /*----------------------------------------------------------*/
1887 if (cinfo->struct_ret) {
1888 cinfo->ret.offset = sz->stack_size;
1889 switch (cinfo->ret.size) {
1897 sz->stack_size += S390_ALIGN(cinfo->ret.size, align);
1902 sz->stack_size = sz->stack_size + sz->local_size + sz->parm_size +
1904 sz->stack_size = S390_ALIGN(sz->stack_size, sizeof(long));
1909 /*========================= End of Function ========================*/
1911 /*------------------------------------------------------------------*/
1913 /* Name - mono_arch_allocate_vars */
1915 /* Function - Set var information according to the calling */
1916 /* convention for S/390. The local var stuff should */
1917 /* most likely be split in another method. */
1919 /* Parameter - @m - Compile unit. */
1921 /*------------------------------------------------------------------*/
1924 mono_arch_allocate_vars (MonoCompile *cfg)
1926 MonoMethodSignature *sig;
1927 MonoMethodHeader *header;
1930 int iParm, iVar, offset, align, size, curinst;
1931 int frame_reg = STK_BASE;
1934 header = cfg->header;
1936 cfg->flags |= MONO_CFG_HAS_SPILLUP;
1938 /*---------------------------------------------------------*/
1939 /* We use the frame register also for any method that has */
1940 /* filter clauses. This way, when the handlers are called, */
1941 /* the code will reference local variables using the frame */
1942 /* reg instead of the stack pointer: if we had to restore */
1943 /* the stack pointer, we'd corrupt the method frames that */
1944 /* are already on the stack (since filters get called */
1945 /* before stack unwinding happens) when the filter code */
1946 /* would call any method. */
1947 /*---------------------------------------------------------*/
1948 if ((cfg->flags & MONO_CFG_HAS_ALLOCA) || header->num_clauses)
1949 frame_reg = s390_r11;
1951 cfg->frame_reg = frame_reg;
1953 cfg->arch.bkchain_reg = -1;
1955 if (frame_reg != STK_BASE)
1956 cfg->used_int_regs |= 1 << frame_reg;
1958 sig = mono_method_signature (cfg->method);
1960 cinfo = get_call_info (cfg, cfg->mempool, sig);
1962 if (!cinfo->struct_ret) {
1963 switch (mini_type_get_underlying_type (cfg->generic_sharing_context, sig->ret)->type) {
1964 case MONO_TYPE_VOID:
1967 cfg->ret->opcode = OP_REGVAR;
1968 cfg->ret->dreg = s390_r2;
1973 /*--------------------------------------------------------------*/
1974 /* local vars are at a positive offset from the stack pointer */
1975 /* also note that if the function uses alloca, we use s390_r11 */
1976 /* to point at the local variables. */
1977 /* add parameter area size for called functions */
1978 /*--------------------------------------------------------------*/
1979 if (cfg->param_area == 0)
1980 offset = S390_MINIMAL_STACK_SIZE;
1982 offset = cfg->param_area;
1984 cfg->sig_cookie = 0;
1986 if (cinfo->struct_ret) {
1987 inst = cfg->vret_addr;
1988 offset = S390_ALIGN(offset, sizeof(gpointer));
1989 inst->inst_offset = offset;
1990 inst->opcode = OP_REGOFFSET;
1991 inst->inst_basereg = frame_reg;
1992 offset += sizeof(gpointer);
1993 if (G_UNLIKELY (cfg->verbose_level > 1)) {
1994 printf ("vret_addr =");
1995 mono_print_ins (cfg->vret_addr);
2000 inst = cfg->args [0];
2001 if (inst->opcode != OP_REGVAR) {
2002 inst->opcode = OP_REGOFFSET;
2003 inst->inst_basereg = frame_reg;
2004 offset = S390_ALIGN(offset, sizeof(gpointer));
2005 inst->inst_offset = offset;
2006 offset += sizeof (gpointer);
2013 eArg = sig->param_count + sArg;
2015 if (sig->call_convention == MONO_CALL_VARARG)
2016 cfg->sig_cookie += S390_MINIMAL_STACK_SIZE;
2018 for (iParm = sArg; iParm < eArg; ++iParm) {
2019 inst = cfg->args [curinst];
2020 if (inst->opcode != OP_REGVAR) {
2021 switch (cinfo->args[iParm].regtype) {
2022 case RegTypeStructByAddr : {
2025 size = sizeof (gpointer);
2027 inst->opcode = OP_REGOFFSET;
2028 inst->inst_basereg = frame_reg;
2029 offset = S390_ALIGN (offset, sizeof (gpointer));
2030 inst->inst_offset = offset;
2032 /* Add a level of indirection */
2033 MONO_INST_NEW (cfg, indir, 0);
2035 inst->opcode = OP_VTARG_ADDR;
2036 inst->inst_left = indir;
2039 case RegTypeStructByAddrOnStack : {
2042 size = sizeof (gpointer);
2044 /* Similar to the == STK_BASE case below */
2045 cfg->arch.bkchain_reg = s390_r12;
2046 cfg->used_int_regs |= 1 << cfg->arch.bkchain_reg;
2048 inst->opcode = OP_REGOFFSET;
2049 inst->dreg = mono_alloc_preg (cfg);
2050 inst->inst_basereg = cfg->arch.bkchain_reg;
2051 inst->inst_offset = cinfo->args [iParm].offset;
2053 /* Add a level of indirection */
2054 MONO_INST_NEW (cfg, indir, 0);
2056 inst->opcode = OP_VTARG_ADDR;
2057 inst->inst_left = indir;
2060 case RegTypeStructByVal :
2061 size = cinfo->args[iParm].size;
2062 offset = S390_ALIGN(offset, size);
2063 inst->opcode = OP_REGOFFSET;
2064 inst->inst_basereg = frame_reg;
2065 inst->inst_offset = offset;
2068 if (cinfo->args [iParm].reg == STK_BASE) {
2070 * These arguments are in the previous frame, so we can't
2071 * compute their offset from the current frame pointer right
2072 * now, since cfg->stack_offset is not yet known, so dedicate a
2073 * register holding the previous frame pointer.
2075 cfg->arch.bkchain_reg = s390_r12;
2076 cfg->used_int_regs |= 1 << cfg->arch.bkchain_reg;
2078 inst->opcode = OP_REGOFFSET;
2079 inst->inst_basereg = cfg->arch.bkchain_reg;
2080 size = (cinfo->args[iParm].size < 8
2081 ? 8 - cinfo->args[iParm].size
2083 inst->inst_offset = cinfo->args [iParm].offset + size;
2084 size = sizeof (long);
2086 inst->opcode = OP_REGOFFSET;
2087 inst->inst_basereg = frame_reg;
2088 size = (cinfo->args[iParm].size < 8
2091 offset = S390_ALIGN(offset, size);
2092 if (cfg->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE)
2093 inst->inst_offset = offset;
2095 inst->inst_offset = offset + (8 - size);
2100 if ((sig->call_convention == MONO_CALL_VARARG) &&
2101 (cinfo->args[iParm].regtype != RegTypeGeneral) &&
2102 (iParm < sig->sentinelpos))
2103 cfg->sig_cookie += size;
2104 printf("%s %4d cookine %x\n",__FUNCTION__,__LINE__,cfg->sig_cookie);
2107 offset += MAX(size, 8);
2112 cfg->locals_min_stack_offset = offset;
2114 curinst = cfg->locals_start;
2115 for (iVar = curinst; iVar < cfg->num_varinfo; ++iVar) {
2116 inst = cfg->varinfo [iVar];
2117 if ((inst->flags & MONO_INST_IS_DEAD) ||
2118 (inst->opcode == OP_REGVAR))
2121 /*--------------------------------------------------*/
2122 /* inst->backend.is_pinvoke indicates native sized */
2123 /* value typs this is used by the pinvoke wrappers */
2124 /* when they call functions returning structure */
2125 /*--------------------------------------------------*/
2126 if (inst->backend.is_pinvoke && MONO_TYPE_ISSTRUCT (inst->inst_vtype))
2127 size = mono_class_native_size (mono_class_from_mono_type(inst->inst_vtype),
2128 (guint32 *) &align);
2130 size = mono_type_size (inst->inst_vtype, &align);
2132 offset = S390_ALIGN(offset, align);
2133 inst->inst_offset = offset;
2134 inst->opcode = OP_REGOFFSET;
2135 inst->inst_basereg = frame_reg;
2137 DEBUG (g_print("allocating local %d to %ld, size: %d\n",
2138 iVar, inst->inst_offset, size));
2141 cfg->locals_max_stack_offset = offset;
2143 /*------------------------------------------------------*/
2144 /* Allow space for the trace method stack area if needed*/
2145 /*------------------------------------------------------*/
2146 if ((mono_jit_trace_calls != NULL && mono_trace_eval (cfg->method))
2147 || (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE))
2148 offset += S390_TRACE_STACK_SIZE;
2150 /*------------------------------------------------------*/
2151 /* Reserve space to save LMF and caller saved registers */
2152 /*------------------------------------------------------*/
2153 if (cfg->method->save_lmf)
2154 offset += sizeof (MonoLMF);
2156 /*------------------------------------------------------*/
2157 /* align the offset */
2158 /*------------------------------------------------------*/
2159 cfg->stack_offset = S390_ALIGN(offset, S390_STACK_ALIGNMENT);
2161 /*------------------------------------------------------*/
2162 /* Fix offsets for args whose value is in parent frame */
2163 /*------------------------------------------------------*/
2164 for (iParm = sArg; iParm < eArg; ++iParm) {
2165 inst = cfg->args [iParm];
2167 if (inst->opcode == OP_S390_STKARG) {
2168 inst->opcode = OP_REGOFFSET;
2169 inst->inst_offset += cfg->stack_offset;
2174 /*========================= End of Function ========================*/
2176 /*------------------------------------------------------------------*/
2178 /* Name - mono_arch_create_vars */
2180 /*------------------------------------------------------------------*/
2183 mono_arch_create_vars (MonoCompile *cfg)
2185 MonoMethodSignature *sig;
2188 sig = mono_method_signature (cfg->method);
2190 cinfo = get_call_info (cfg, cfg->mempool, sig);
2192 if (cinfo->struct_ret) {
2193 cfg->vret_addr = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_ARG);
2194 if (G_UNLIKELY (cfg->verbose_level > 1)) {
2195 printf ("vret_addr = ");
2196 mono_print_ins (cfg->vret_addr);
2201 /*========================= End of Function ========================*/
2203 /*------------------------------------------------------------------*/
2205 /* Name - add_outarg_reg2. */
2207 /*------------------------------------------------------------------*/
2210 add_outarg_reg2 (MonoCompile *cfg, MonoCallInst *call, ArgStorage storage, int reg, MonoInst *tree)
2215 case RegTypeGeneral:
2216 MONO_INST_NEW (cfg, ins, OP_MOVE);
2217 ins->dreg = mono_alloc_ireg (cfg);
2218 ins->sreg1 = tree->dreg;
2219 MONO_ADD_INS (cfg->cbb, ins);
2220 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, reg, FALSE);
2223 MONO_INST_NEW (cfg, ins, OP_FMOVE);
2224 ins->dreg = mono_alloc_freg (cfg);
2225 ins->sreg1 = tree->dreg;
2226 MONO_ADD_INS (cfg->cbb, ins);
2227 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, reg, TRUE);
2230 MONO_INST_NEW (cfg, ins, OP_S390_SETF4RET);
2231 ins->dreg = mono_alloc_freg (cfg);
2232 ins->sreg1 = tree->dreg;
2233 MONO_ADD_INS (cfg->cbb, ins);
2234 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, reg, TRUE);
2237 g_assert_not_reached ();
2241 /*========================= End of Function ========================*/
2243 /*------------------------------------------------------------------*/
2245 /* Name - emit_sig_cookie. */
2247 /*------------------------------------------------------------------*/
2250 emit_sig_cookie (MonoCompile *cfg, MonoCallInst *call, CallInfo *cinfo)
2252 MonoMethodSignature *tmpSig;
2255 cfg->disable_aot = TRUE;
2257 /*----------------------------------------------------------*/
2258 /* mono_ArgIterator_Setup assumes the signature cookie is */
2259 /* passed first and all the arguments which were before it */
2260 /* passed on the stack after the signature. So compensate */
2261 /* by passing a different signature. */
2262 /*----------------------------------------------------------*/
2263 tmpSig = mono_metadata_signature_dup (call->signature);
2264 tmpSig->param_count -= call->signature->sentinelpos;
2265 tmpSig->sentinelpos = 0;
2266 if (tmpSig->param_count > 0)
2267 memcpy (tmpSig->params,
2268 call->signature->params + call->signature->sentinelpos,
2269 tmpSig->param_count * sizeof(MonoType *));
2271 MONO_INST_NEW (cfg, sig_arg, OP_ICONST);
2272 sig_arg->dreg = mono_alloc_ireg (cfg);
2273 sig_arg->inst_p0 = tmpSig;
2274 MONO_ADD_INS (cfg->cbb, sig_arg);
2276 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, STK_BASE,
2277 cinfo->sigCookie.offset, sig_arg->dreg);
2280 /*========================= End of Function ========================*/
2282 /*------------------------------------------------------------------*/
2284 /* Name - mono_arch_emit_call */
2286 /*------------------------------------------------------------------*/
2289 mono_arch_emit_call (MonoCompile *cfg, MonoCallInst *call)
2292 MonoMethodSignature *sig;
2294 int i, n, lParamArea;
2296 ArgInfo *ainfo = NULL;
2298 MonoMethodHeader *header;
2301 sig = call->signature;
2302 n = sig->param_count + sig->hasthis;
2303 DEBUG (g_print ("Call requires: %d parameters\n",n));
2305 cinfo = get_call_info (cfg, cfg->mempool, sig);
2307 stackSize = cinfo->sz.stack_size + cinfo->sz.local_size +
2308 cinfo->sz.parm_size + cinfo->sz.offset;
2309 call->stack_usage = MAX(stackSize, call->stack_usage);
2310 lParamArea = MAX((call->stack_usage-S390_MINIMAL_STACK_SIZE-cinfo->sz.parm_size), 0);
2311 cfg->param_area = MAX(((signed) cfg->param_area), lParamArea);
2312 cfg->flags |= MONO_CFG_HAS_CALLS;
2314 if (cinfo->struct_ret) {
2315 MONO_INST_NEW (cfg, ins, OP_MOVE);
2316 ins->sreg1 = call->vret_var->dreg;
2317 ins->dreg = mono_alloc_preg (cfg);
2318 MONO_ADD_INS (cfg->cbb, ins);
2319 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, cinfo->ret.reg, FALSE);
2322 header = cfg->header;
2323 if ((cfg->flags & MONO_CFG_HAS_ALLOCA) || header->num_clauses)
2328 for (i = 0; i < n; ++i) {
2331 ainfo = cinfo->args + i;
2332 if (i >= sig->hasthis)
2333 t = sig->params [i - sig->hasthis];
2335 t = &mono_defaults.int_class->byval_arg;
2336 t = mini_type_get_underlying_type (cfg->generic_sharing_context, t);
2338 in = call->args [i];
2340 if ((sig->call_convention == MONO_CALL_VARARG) &&
2342 (i == sig->sentinelpos)) {
2343 emit_sig_cookie (cfg, call, cinfo);
2346 switch (ainfo->regtype) {
2347 case RegTypeGeneral:
2348 add_outarg_reg2 (cfg, call, ainfo->regtype, ainfo->reg, in);
2351 if (MONO_TYPE_ISSTRUCT (t)) {
2352 /* Valuetype passed in one fp register */
2353 ainfo->regtype = RegTypeStructByValInFP;
2356 if (ainfo->size == 4)
2357 ainfo->regtype = RegTypeFPR4;
2358 add_outarg_reg2 (cfg, call, ainfo->regtype, ainfo->reg, in);
2361 case RegTypeStructByVal:
2362 case RegTypeStructByAddr:
2363 case RegTypeStructByAddrOnStack: {
2367 if (sig->params [i - sig->hasthis]->type == MONO_TYPE_TYPEDBYREF) {
2368 size = sizeof (MonoTypedRef);
2369 align = sizeof (gpointer);
2373 size = mono_type_native_stack_size (&in->klass->byval_arg, &align);
2376 * Other backends use mono_type_stack_size (), but that
2377 * aligns the size to 8, which is larger than the size of
2378 * the source, leading to reads of invalid memory if the
2379 * source is at the end of address space.
2381 size = mono_class_value_size (in->klass, &align);
2384 g_assert (in->klass);
2386 ainfo->offparm += cinfo->sz.offStruct;
2388 MONO_INST_NEW (cfg, ins, OP_OUTARG_VT);
2389 ins->sreg1 = in->dreg;
2390 ins->klass = in->klass;
2391 ins->backend.size = ainfo->size;
2392 ins->inst_p0 = call;
2393 ins->inst_p1 = mono_mempool_alloc (cfg->mempool, sizeof (ArgInfo));
2394 memcpy (ins->inst_p1, ainfo, sizeof (ArgInfo));
2396 MONO_ADD_INS (cfg->cbb, ins);
2398 if (ainfo->regtype == RegTypeStructByAddr) {
2400 * We use OP_OUTARG_VT to copy the valuetype to a stack location, then
2401 * use the normal OUTARG opcodes to pass the address of the location to
2404 int treg = mono_alloc_preg (cfg);
2405 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ADD_IMM, treg,
2406 frmReg, ainfo->offparm);
2407 mono_call_inst_add_outarg_reg (cfg, call, treg, ainfo->reg, FALSE);
2408 } else if (ainfo->regtype == RegTypeStructByAddrOnStack) {
2409 /* The address of the valuetype is passed on the stack */
2410 int treg = mono_alloc_preg (cfg);
2411 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ADD_IMM, treg,
2412 frmReg, ainfo->offparm);
2413 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG,
2414 ainfo->reg, ainfo->offset, treg);
2416 if (cfg->compute_gc_maps) {
2419 EMIT_NEW_GC_PARAM_SLOT_LIVENESS_DEF (cfg, def, ainfo->offset, t);
2425 if (!t->byref && t->type == MONO_TYPE_R4) {
2426 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORER4_MEMBASE_REG,
2427 STK_BASE, ainfo->offset + 4,
2429 } else if (!t->byref && (t->type == MONO_TYPE_R8)) {
2430 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORER8_MEMBASE_REG,
2431 STK_BASE, ainfo->offset,
2434 MONO_INST_NEW (cfg, ins, OP_STORE_MEMBASE_REG);
2435 ins->inst_destbasereg = STK_BASE;
2436 ins->inst_offset = ainfo->offset;
2437 ins->sreg1 = in->dreg;
2440 /* This is needed by MonoTypedRef->value to point to the correct data */
2441 if ((sig->call_convention == MONO_CALL_VARARG) &&
2442 (i >= sig->sentinelpos)) {
2443 switch (ainfo->size) {
2445 ins->opcode = OP_STOREI1_MEMBASE_REG;
2448 ins->opcode = OP_STOREI2_MEMBASE_REG;
2451 ins->opcode = OP_STOREI4_MEMBASE_REG;
2459 MONO_ADD_INS (cfg->cbb, ins);
2463 g_assert_not_reached ();
2469 * Handle the case where there are no implicit arguments
2471 if ((sig->call_convention == MONO_CALL_VARARG) &&
2473 (i == sig->sentinelpos)) {
2474 emit_sig_cookie (cfg, call, cinfo);
2478 /*========================= End of Function ========================*/
2480 /*------------------------------------------------------------------*/
2482 /* Name - mono_arch_emit_outarg_vt */
2484 /*------------------------------------------------------------------*/
2487 mono_arch_emit_outarg_vt (MonoCompile *cfg, MonoInst *ins, MonoInst *src)
2489 MonoCallInst *call = (MonoCallInst*)ins->inst_p0;
2490 ArgInfo *ainfo = (ArgInfo*)ins->inst_p1;
2491 int size = ins->backend.size;
2493 if (ainfo->regtype == RegTypeStructByVal) {
2495 arg->ins.sreg1 = ainfo->reg;
2496 arg->ins.opcode = OP_OUTARG_VT;
2497 arg->size = ainfo->size;
2498 arg->offset = ainfo->offset;
2499 arg->offPrm = ainfo->offparm + cinfo->sz.offStruct;
2501 if (ainfo->reg != STK_BASE) {
2502 MONO_OUTPUT_VTR (cfg, size, ainfo->reg, src->dreg, 0);
2504 MONO_OUTPUT_VTS (cfg, size, ainfo->reg, ainfo->offset,
2507 } else if (ainfo->regtype == RegTypeStructByValInFP) {
2508 int dreg = mono_alloc_freg (cfg);
2510 if (ainfo->size == 4) {
2511 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADR4_MEMBASE, dreg, src->dreg, 0);
2512 MONO_EMIT_NEW_UNALU (cfg, OP_S390_SETF4RET, dreg, dreg);
2514 g_assert (ainfo->size == 8);
2516 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADR8_MEMBASE, dreg, src->dreg, 0);
2519 mono_call_inst_add_outarg_reg (cfg, call, dreg, ainfo->reg, TRUE);
2521 MonoMethodHeader *header;
2524 header = mono_method_get_header (cfg->method);
2525 if ((cfg->flags & MONO_CFG_HAS_ALLOCA) || header->num_clauses)
2530 MONO_EMIT_NEW_MOVE (cfg, srcReg, ainfo->offparm,
2531 src->dreg, 0, size);
2533 if (cfg->compute_gc_maps) {
2536 EMIT_NEW_GC_PARAM_SLOT_LIVENESS_DEF (cfg, def, ainfo->offset, &ins->klass->byval_arg);
2541 /*========================= End of Function ========================*/
2543 /*------------------------------------------------------------------*/
2545 /* Name - mono_arch_emit_setret */
2547 /*------------------------------------------------------------------*/
2550 mono_arch_emit_setret (MonoCompile *cfg, MonoMethod *method, MonoInst *val)
2552 MonoType *ret = mini_type_get_underlying_type (cfg->generic_sharing_context,
2553 mono_method_signature (method)->ret);
2556 if (ret->type == MONO_TYPE_R4) {
2557 MONO_EMIT_NEW_UNALU (cfg, OP_S390_SETF4RET, s390_f0, val->dreg);
2559 } else if (ret->type == MONO_TYPE_R8) {
2560 MONO_EMIT_NEW_UNALU (cfg, OP_FMOVE, s390_f0, val->dreg);
2565 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, cfg->ret->dreg, val->dreg);
2568 /*========================= End of Function ========================*/
2570 /*------------------------------------------------------------------*/
2572 /* Name - mono_arch_instrument_mem_needs */
2574 /* Function - Allow tracing to work with this interface (with */
2575 /* an optional argument). */
2577 /*------------------------------------------------------------------*/
2580 mono_arch_instrument_mem_needs (MonoMethod *method, int *stack, int *code)
2582 /* no stack room needed now (may be needed for FASTCALL-trace support) */
2584 /* split prolog-epilog requirements? */
2585 *code = 50; /* max bytes needed: check this number */
2588 /*========================= End of Function ========================*/
2590 /*------------------------------------------------------------------*/
2592 /* Name - mono_arch_instrument_prolog */
2594 /* Function - Create an "instrumented" prolog. */
2596 /*------------------------------------------------------------------*/
2599 mono_arch_instrument_prolog (MonoCompile *cfg, void *func, void *p,
2600 gboolean enable_arguments)
2607 parmOffset = cfg->stack_usage - S390_TRACE_STACK_SIZE;
2608 if (cfg->method->save_lmf)
2609 parmOffset -= sizeof(MonoLMF);
2610 fpOffset = parmOffset + (5*sizeof(gpointer));
2611 if ((!mono_hwcap_s390x_has_ld) && (fpOffset > 4096)) {
2612 s390_lgr (code, s390_r12, STK_BASE);
2614 while (fpOffset > 4096) {
2615 s390_aghi (code, baseReg, 4096);
2623 s390_stmg (code, s390_r2, s390_r6, STK_BASE, parmOffset);
2624 if (mono_hwcap_s390x_has_ld) {
2625 s390_stdy (code, s390_f0, 0, STK_BASE, fpOffset);
2626 s390_stdy (code, s390_f2, 0, STK_BASE, fpOffset+sizeof(gdouble));
2627 s390_stdy (code, s390_f4, 0, STK_BASE, fpOffset+2*sizeof(gdouble));
2628 s390_stdy (code, s390_f6, 0, STK_BASE, fpOffset+3*sizeof(gdouble));
2630 s390_std (code, s390_f0, 0, baseReg, fpOffset);
2631 s390_std (code, s390_f2, 0, baseReg, fpOffset+sizeof(gdouble));
2632 s390_std (code, s390_f4, 0, baseReg, fpOffset+2*sizeof(gdouble));
2633 s390_std (code, s390_f6, 0, baseReg, fpOffset+3*sizeof(gdouble));
2635 s390_basr (code, s390_r13, 0);
2637 s390_llong(code, cfg->method);
2638 s390_llong(code, func);
2639 s390_lg (code, s390_r2, 0, s390_r13, 4);
2640 if (mono_hwcap_s390x_has_ld)
2641 s390_lay (code, s390_r3, 0, STK_BASE, parmOffset);
2643 s390_la (code, s390_r3, 0, baseReg, parmOffset);
2644 s390_lgr (code, s390_r4, STK_BASE);
2645 s390_aghi (code, s390_r4, cfg->stack_usage);
2646 s390_lg (code, s390_r1, 0, s390_r13, 12);
2647 s390_basr (code, s390_r14, s390_r1);
2648 if (mono_hwcap_s390x_has_ld) {
2649 s390_ldy (code, s390_f6, 0, STK_BASE, fpOffset+3*sizeof(gdouble));
2650 s390_ldy (code, s390_f4, 0, STK_BASE, fpOffset+2*sizeof(gdouble));
2651 s390_ldy (code, s390_f2, 0, STK_BASE, fpOffset+sizeof(gdouble));
2652 s390_ldy (code, s390_f0, 0, STK_BASE, fpOffset);
2654 s390_ld (code, s390_f6, 0, baseReg, fpOffset+3*sizeof(gdouble));
2655 s390_ld (code, s390_f4, 0, baseReg, fpOffset+2*sizeof(gdouble));
2656 s390_ld (code, s390_f2, 0, baseReg, fpOffset+sizeof(gdouble));
2657 s390_ld (code, s390_f0, 0, baseReg, fpOffset);
2659 s390_lmg (code, s390_r2, s390_r6, STK_BASE, parmOffset);
2664 /*========================= End of Function ========================*/
2666 /*------------------------------------------------------------------*/
2668 /* Name - mono_arch_instrument_epilog */
2670 /* Function - Create an epilog that will handle the returned */
2671 /* values used in instrumentation. */
2673 /*------------------------------------------------------------------*/
2676 mono_arch_instrument_epilog_full (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments, gboolean preserve_argument_registers)
2679 int save_mode = SAVE_NONE,
2682 MonoMethod *method = cfg->method;
2683 int rtype = mini_type_get_underlying_type (cfg->generic_sharing_context,
2684 mono_method_signature (method)->ret)->type;
2686 offset = code - cfg->native_code;
2687 /*-----------------------------------------*/
2688 /* We need about 128 bytes of instructions */
2689 /*-----------------------------------------*/
2690 if (offset > (cfg->code_size - 128)) {
2691 cfg->code_size *= 2;
2692 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
2693 code = cfg->native_code + offset;
2696 saveOffset = cfg->stack_usage - S390_TRACE_STACK_SIZE;
2697 if (method->save_lmf)
2698 saveOffset -= sizeof(MonoLMF);
2702 case MONO_TYPE_VOID:
2703 /* special case string .ctor icall */
2704 if (strcmp (".ctor", method->name) && method->klass == mono_defaults.string_class)
2705 save_mode = SAVE_ONE;
2707 save_mode = SAVE_NONE;
2711 save_mode = SAVE_ONE;
2714 save_mode = SAVE_R4;
2717 save_mode = SAVE_R8;
2719 case MONO_TYPE_VALUETYPE:
2720 if (mono_method_signature (method)->ret->data.klass->enumtype) {
2721 rtype = mono_class_enum_basetype (mono_method_signature (method)->ret->data.klass)->type;
2724 save_mode = SAVE_STRUCT;
2727 save_mode = SAVE_ONE;
2731 switch (save_mode) {
2733 s390_stg (code, s390_r2, 0, cfg->frame_reg, saveOffset);
2734 if (enable_arguments) {
2735 s390_lgr (code, s390_r3, s390_r2);
2739 s390_std (code, s390_f0, 0, cfg->frame_reg, saveOffset);
2740 if (enable_arguments) {
2741 s390_ldebr (code, s390_f0, s390_f0);
2745 s390_std (code, s390_f0, 0, cfg->frame_reg, saveOffset);
2748 s390_stg (code, s390_r2, 0, cfg->frame_reg, saveOffset);
2749 if (enable_arguments) {
2750 s390_lg (code, s390_r3, 0, cfg->frame_reg,
2751 S390_MINIMAL_STACK_SIZE+cfg->param_area);
2759 s390_basr (code, s390_r13, 0);
2761 s390_llong(code, cfg->method);
2762 s390_llong(code, func);
2763 s390_lg (code, s390_r2, 0, s390_r13, 4);
2764 s390_lg (code, s390_r1, 0, s390_r13, 12);
2765 s390_basr (code, s390_r14, s390_r1);
2767 switch (save_mode) {
2769 s390_lg (code, s390_r2, 0, cfg->frame_reg, saveOffset);
2773 s390_ld (code, s390_f0, 0, cfg->frame_reg, saveOffset);
2776 s390_lg (code, s390_r2, 0, cfg->frame_reg, saveOffset);
2786 /*========================= End of Function ========================*/
2788 /*------------------------------------------------------------------*/
2790 /* Name - mono_arch_peephole_pass_1 */
2792 /* Function - Form a peephole pass at the code looking for */
2793 /* simple optimizations. */
2795 /*------------------------------------------------------------------*/
2798 mono_arch_peephole_pass_1 (MonoCompile *cfg, MonoBasicBlock *bb)
2802 /*========================= End of Function ========================*/
2804 /*------------------------------------------------------------------*/
2806 /* Name - mono_arch_peephole_pass_2 */
2808 /* Function - Form a peephole pass at the code looking for */
2809 /* simple optimizations. */
2811 /*------------------------------------------------------------------*/
2814 mono_arch_peephole_pass_2 (MonoCompile *cfg, MonoBasicBlock *bb)
2818 MONO_BB_FOR_EACH_INS_SAFE (bb, n, ins) {
2819 mono_peephole_ins (bb, ins);
2823 /*========================= End of Function ========================*/
2825 /*------------------------------------------------------------------*/
2827 /* Name - mono_arch_lowering_pass. */
2829 /*------------------------------------------------------------------*/
2832 mono_arch_lowering_pass (MonoCompile *cfg, MonoBasicBlock *bb)
2834 MonoInst *ins, *next;
2836 MONO_BB_FOR_EACH_INS_SAFE (bb, next, ins) {
2837 switch (ins->opcode) {
2842 case OP_IDIV_UN_IMM:
2843 case OP_IREM_UN_IMM:
2847 case OP_LOCALLOC_IMM:
2848 mono_decompose_op_imm (cfg, bb, ins);
2851 if (!s390_is_imm16 (ins->inst_imm))
2852 /* This is created by the memcpy code which ignores is_inst_imm */
2853 mono_decompose_op_imm (cfg, bb, ins);
2860 bb->max_vreg = cfg->next_vreg;
2863 /*========================= End of Function ========================*/
2865 /*------------------------------------------------------------------*/
2867 /* Name - emit_float_to_int */
2869 /* Function - Create instructions which will convert a floating */
2870 /* point value to integer. */
2872 /*------------------------------------------------------------------*/
2875 emit_float_to_int (MonoCompile *cfg, guchar *code, int dreg, int sreg, int size, gboolean is_signed)
2877 /* sreg is a float, dreg is an integer reg. */
2879 s390_cgdbr (code, dreg, 5, sreg);
2882 s390_lghi (code, s390_r0, 0);
2883 s390_lghi (code, s390_r13, 0xff);
2884 s390_ltgr (code, dreg, dreg);
2886 s390_lghi (code, s390_r0, 0x80);
2887 s390_ngr (code, dreg, s390_r13);
2888 s390_ogr (code, dreg, s390_r0);
2893 s390_basr (code, s390_r13, 0);
2895 s390_llong (code, 0x41e0000000000000llu);
2896 s390_llong (code, 0x41f0000000000000llu);
2897 s390_ldr (code, s390_f15, sreg);
2898 s390_cdb (code, s390_f15, 0, s390_r13, 4);
2899 s390_jl (code, 0); CODEPTR (code, o[0]);
2900 s390_sdb (code, s390_f15, 0, s390_r13, 12);
2901 s390_cfdbr (code, dreg, 7, s390_f15);
2903 PTRSLOT (code, o[0]);
2904 s390_cfdbr (code, dreg, 5, sreg);
2907 s390_lghi (code, s390_r0, 0xff);
2908 s390_ngr (code, dreg, s390_r0);
2911 s390_lghi (code, s390_r0, -1);
2912 s390_srlg (code, s390_r0, s390_r0, 0, 16);
2913 s390_ngr (code, dreg, s390_r0);
2920 /*========================= End of Function ========================*/
2922 /*------------------------------------------------------------------*/
2924 /* Name - gboolean_is_unsigned. */
2926 /* Function - Return TRUE if next opcode is checking for un- */
2929 /*------------------------------------------------------------------*/
2932 gboolean is_unsigned (MonoInst *next)
2935 (((next->opcode >= OP_IBNE_UN) &&
2936 (next->opcode <= OP_IBLT_UN)) ||
2937 ((next->opcode >= OP_LBNE_UN) &&
2938 (next->opcode <= OP_LBLT_UN)) ||
2939 ((next->opcode >= OP_COND_EXC_NE_UN) &&
2940 (next->opcode <= OP_COND_EXC_LT_UN)) ||
2941 ((next->opcode >= OP_COND_EXC_INE_UN) &&
2942 (next->opcode <= OP_COND_EXC_ILT_UN)) ||
2943 ((next->opcode == OP_CLT_UN) ||
2944 (next->opcode == OP_CGT_UN)) ||
2945 ((next->opcode == OP_ICLT_UN) ||
2946 (next->opcode == OP_ICGT_UN) ||
2947 (next->opcode == OP_LCLT_UN) ||
2948 (next->opcode == OP_LCGT_UN))))
2954 /*========================= End of Function ========================*/
2956 /*------------------------------------------------------------------*/
2958 /* Name - mono_arch_output_basic_block */
2960 /* Function - Perform the "real" work of emitting instructions */
2961 /* that will do the work of in the basic block. */
2963 /*------------------------------------------------------------------*/
2966 mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
2971 guint8 *code = cfg->native_code + cfg->code_len;
2972 guint last_offset = 0;
2973 int max_len, cpos, src2;
2975 /* we don't align basic blocks of loops on s390 */
2977 if (cfg->verbose_level > 2)
2978 g_print ("Basic block %d starting at offset 0x%x\n", bb->block_num, bb->native_offset);
2980 cpos = bb->max_offset;
2982 if (cfg->prof_options & MONO_PROFILE_COVERAGE) {
2983 //MonoCoverageInfo *cov = mono_get_coverage_info (cfg->method);
2984 //g_assert (!mono_compile_aot);
2987 // cov->data [bb->dfn].iloffset = bb->cil_code - cfg->cil_code;
2988 /* this is not thread save, but good enough */
2989 /* fixme: howto handle overflows? */
2990 //x86_inc_mem (code, &cov->data [bb->dfn].count);
2993 MONO_BB_FOR_EACH_INS (bb, ins) {
2994 offset = code - cfg->native_code;
2996 max_len = ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
2998 if (offset > (cfg->code_size - max_len - 16)) {
2999 cfg->code_size *= 2;
3000 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
3001 code = cfg->native_code + offset;
3004 mono_debug_record_line_number (cfg, ins, offset);
3006 switch (ins->opcode) {
3007 case OP_STOREI1_MEMBASE_IMM: {
3008 s390_lghi (code, s390_r0, ins->inst_imm);
3009 S390_LONG (code, stcy, stc, s390_r0, 0,
3010 ins->inst_destbasereg, ins->inst_offset);
3013 case OP_STOREI2_MEMBASE_IMM: {
3014 s390_lghi (code, s390_r0, ins->inst_imm);
3015 S390_LONG (code, sthy, sth, s390_r0, 0,
3016 ins->inst_destbasereg, ins->inst_offset);
3019 case OP_STOREI4_MEMBASE_IMM: {
3020 if (s390_is_imm16(ins->inst_imm)) {
3021 s390_lghi (code, s390_r0, ins->inst_imm);
3023 s390_basr (code, s390_r13, 0);
3025 s390_llong(code, ins->inst_imm);
3026 s390_lg (code, s390_r0, 0, s390_r13, 4);
3028 S390_LONG (code, sty, st, s390_r0, 0,
3029 ins->inst_destbasereg, ins->inst_offset);
3032 case OP_STORE_MEMBASE_IMM:
3033 case OP_STOREI8_MEMBASE_IMM: {
3034 if (s390_is_imm16(ins->inst_imm)) {
3035 s390_lghi (code, s390_r0, ins->inst_imm);
3037 s390_basr (code, s390_r13, 0);
3039 s390_llong(code, ins->inst_imm);
3040 s390_lg (code, s390_r0, 0, s390_r13, 4);
3042 S390_LONG (code, stg, stg, s390_r0, 0,
3043 ins->inst_destbasereg, ins->inst_offset);
3046 case OP_STOREI1_MEMBASE_REG: {
3047 S390_LONG (code, stcy, stc, ins->sreg1, 0,
3048 ins->inst_destbasereg, ins->inst_offset);
3051 case OP_STOREI2_MEMBASE_REG: {
3052 S390_LONG (code, sthy, sth, ins->sreg1, 0,
3053 ins->inst_destbasereg, ins->inst_offset);
3056 case OP_STOREI4_MEMBASE_REG: {
3057 S390_LONG (code, sty, st, ins->sreg1, 0,
3058 ins->inst_destbasereg, ins->inst_offset);
3061 case OP_STORE_MEMBASE_REG:
3062 case OP_STOREI8_MEMBASE_REG: {
3063 S390_LONG (code, stg, stg, ins->sreg1, 0,
3064 ins->inst_destbasereg, ins->inst_offset);
3068 g_assert_not_reached ();
3070 case OP_LOAD_MEMBASE:
3071 case OP_LOADI8_MEMBASE: {
3072 S390_LONG (code, lg, lg, ins->dreg, 0,
3073 ins->inst_basereg, ins->inst_offset);
3076 case OP_LOADI4_MEMBASE: {
3077 S390_LONG (code, lgf, lgf, ins->dreg, 0,
3078 ins->inst_basereg, ins->inst_offset);
3081 case OP_LOADU4_MEMBASE: {
3082 S390_LONG (code, llgf, llgf, ins->dreg, 0,
3083 ins->inst_basereg, ins->inst_offset);
3086 case OP_LOADU1_MEMBASE: {
3087 S390_LONG (code, llgc, llgc, ins->dreg, 0,
3088 ins->inst_basereg, ins->inst_offset);
3091 case OP_LOADI1_MEMBASE: {
3092 S390_LONG (code, lgb, lgb, ins->dreg, 0,
3093 ins->inst_basereg, ins->inst_offset);
3096 case OP_LOADU2_MEMBASE: {
3097 S390_LONG (code, llgh, llgh, ins->dreg, 0,
3098 ins->inst_basereg, ins->inst_offset);
3101 case OP_LOADI2_MEMBASE: {
3102 S390_LONG (code, lgh, lgh, ins->dreg, 0,
3103 ins->inst_basereg, ins->inst_offset);
3106 case OP_LCONV_TO_I1: {
3108 s390_lgbr (code, ins->dreg, ins->sreg1);
3110 s390_sllg (code, ins->dreg, ins->sreg1, 0, 56);
3111 s390_srag (code, ins->dreg, ins->dreg, 0, 56);
3115 case OP_LCONV_TO_I2: {
3117 s390_lghr (code, ins->dreg, ins->sreg1);
3119 s390_sllg (code, ins->dreg, ins->sreg1, 0, 48);
3120 s390_srag (code, ins->dreg, ins->dreg, 0, 48);
3124 case OP_LCONV_TO_U1: {
3126 s390_llghr (code, ins->dreg, ins->sreg1);
3128 if (ins->dreg != ins->sreg1)
3129 s390_lgr (code, ins->dreg, ins->sreg1);
3130 s390_lghi (code, s390_r0, 0xff);
3131 s390_ngr (code, ins->dreg, s390_r0);
3135 case OP_LCONV_TO_U2: {
3137 s390_llghr (code, ins->dreg, ins->sreg1);
3139 if (ins->dreg != ins->sreg1)
3140 s390_lgr (code, ins->dreg, ins->sreg1);
3141 s390_lghi (code, s390_r0, -1);
3142 s390_srlg (code, s390_r0, s390_r0, 0, 48);
3143 s390_ngr (code, ins->dreg, s390_r0);
3147 case OP_ICONV_TO_I1: {
3149 s390_lbr (code, ins->dreg, ins->sreg1);
3151 if (ins->dreg != ins->sreg1)
3152 s390_lr (code, ins->dreg, ins->sreg1);
3153 s390_sll (code, ins->dreg, 0, 24);
3154 s390_sra (code, ins->dreg, 0, 24);
3159 case OP_ICONV_TO_I2: {
3161 s390_lhr (code, ins->dreg, ins->sreg1);
3163 if (ins->dreg != ins->sreg1)
3164 s390_lr (code, ins->dreg, ins->sreg1);
3165 s390_sll (code, ins->dreg, 0, 16);
3166 s390_sra (code, ins->dreg, 0, 16);
3170 case OP_ICONV_TO_U1: {
3172 s390_llcr (code, ins->dreg, ins->sreg1);
3174 if (ins->dreg != ins->sreg1)
3175 s390_lr (code, ins->dreg, ins->sreg1);
3176 s390_lhi (code, s390_r0, 0xff);
3177 s390_nr (code, ins->dreg, s390_r0);
3181 case OP_ICONV_TO_U2: {
3183 s390_llhr (code, ins->dreg, ins->sreg1);
3185 if (ins->dreg != ins->sreg1)
3186 s390_lr (code, ins->dreg, ins->sreg1);
3187 s390_lhi (code, s390_r0, -1);
3188 s390_srl (code, s390_r0, 0, 16);
3189 s390_nr (code, ins->dreg, s390_r0);
3195 if (is_unsigned (ins->next))
3196 s390_clgr (code, ins->sreg1, ins->sreg2);
3198 s390_cgr (code, ins->sreg1, ins->sreg2);
3202 if (is_unsigned (ins->next))
3203 s390_clr (code, ins->sreg1, ins->sreg2);
3205 s390_cr (code, ins->sreg1, ins->sreg2);
3208 case OP_COMPARE_IMM:
3209 case OP_LCOMPARE_IMM: {
3210 if (s390_is_imm16 (ins->inst_imm)) {
3211 s390_lghi (code, s390_r0, ins->inst_imm);
3212 if (is_unsigned (ins->next))
3213 s390_clgr (code, ins->sreg1, s390_r0);
3215 s390_cgr (code, ins->sreg1, s390_r0);
3217 s390_basr (code, s390_r13, 0);
3219 s390_llong(code, ins->inst_imm);
3220 if (is_unsigned (ins->next))
3221 s390_clg (code, ins->sreg1, 0, s390_r13, 4);
3223 s390_cg (code, ins->sreg1, 0, s390_r13, 4);
3227 case OP_ICOMPARE_IMM: {
3228 if (s390_is_imm16 (ins->inst_imm)) {
3229 s390_lghi (code, s390_r0, ins->inst_imm);
3230 if (is_unsigned (ins->next))
3231 s390_clr (code, ins->sreg1, s390_r0);
3233 s390_cr (code, ins->sreg1, s390_r0);
3236 s390_basr (code, s390_r13, 0);
3238 s390_word (code, ins->inst_imm);
3239 if (is_unsigned (ins->next))
3240 s390_cl (code, ins->sreg1, 0, s390_r13, 4);
3242 s390_c (code, ins->sreg1, 0, s390_r13, 4);
3247 s390_basr (code, s390_r13, 0);
3249 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_ABS,
3251 s390_llong (code, mono_break);
3252 s390_lg (code, s390_r14, 0, s390_r13, 4);
3253 s390_basr (code, s390_r14, s390_r14);
3258 s390_agr (code, ins->dreg, src2);
3263 s390_agr (code, ins->dreg, src2);
3268 s390_alcgr (code, ins->dreg, src2);
3272 if (ins->dreg != ins->sreg1) {
3273 s390_lgr (code, ins->dreg, ins->sreg1);
3275 if (s390_is_imm16 (ins->inst_imm)) {
3276 s390_aghi (code, ins->dreg, ins->inst_imm);
3278 s390_basr (code, s390_r13, 0);
3280 s390_llong(code, ins->inst_imm);
3281 s390_ag (code, ins->dreg, 0, s390_r13, 4);
3286 if (ins->dreg != ins->sreg1) {
3287 s390_lgr (code, ins->dreg, ins->sreg1);
3289 g_assert (s390_is_imm16 (ins->inst_imm));
3290 s390_aghi (code, ins->dreg, ins->inst_imm);
3294 if (ins->dreg != ins->sreg1) {
3295 s390_lgr (code, ins->dreg, ins->sreg1);
3297 if (s390_is_imm16 (ins->inst_imm)) {
3298 s390_lghi (code, s390_r0, ins->inst_imm);
3299 s390_alcgr (code, ins->dreg, s390_r0);
3301 s390_basr (code, s390_r13, 0);
3303 s390_llong (code, ins->inst_imm);
3304 s390_lg (code, s390_r13, 0, s390_r13, 4);
3305 s390_alcgr (code, ins->dreg, s390_r13);
3310 case OP_S390_IADD_OVF: {
3312 s390_ar (code, ins->dreg, src2);
3313 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_OV, "OverflowException");
3314 s390_lgfr (code, ins->dreg, ins->dreg);
3317 case OP_IADD_OVF_UN:
3318 case OP_S390_IADD_OVF_UN: {
3320 s390_algr (code, ins->dreg, src2);
3321 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_CY, "OverflowException");
3322 s390_llgfr (code, ins->dreg, ins->dreg);
3325 case OP_ADD_OVF_CARRY: {
3327 s390_lghi (code, s390_r0, 0);
3328 s390_lgr (code, s390_r1, s390_r0);
3329 s390_alcgr (code, s390_r0, s390_r1);
3330 s390_agr (code, ins->dreg, src2);
3331 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_OV, "OverflowException");
3332 s390_agr (code, ins->dreg, s390_r0);
3333 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_OV, "OverflowException");
3336 case OP_ADD_OVF_UN_CARRY: {
3338 s390_alcgr (code, ins->dreg, src2);
3339 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_CY, "OverflowException");
3344 s390_sgr (code, ins->dreg, src2);
3349 s390_sgr (code, ins->dreg, src2);
3354 s390_slbgr(code, ins->dreg, src2);
3358 if (ins->dreg != ins->sreg1) {
3359 s390_lgr (code, ins->dreg, ins->sreg1);
3361 if (s390_is_imm16 (-ins->inst_imm)) {
3362 s390_lghi (code, s390_r0, ins->inst_imm);
3363 s390_slgr (code, ins->dreg, s390_r0);
3365 s390_basr (code, s390_r13, 0);
3367 s390_llong(code, ins->inst_imm);
3368 s390_slg (code, ins->dreg, 0, s390_r13, 4);
3373 if (ins->dreg != ins->sreg1) {
3374 s390_lgr (code, ins->dreg, ins->sreg1);
3376 if (s390_is_imm16 (-ins->inst_imm)) {
3377 s390_lghi (code, s390_r0, ins->inst_imm);
3378 s390_slgr (code, ins->dreg, s390_r0);
3380 s390_basr (code, s390_r13, 0);
3382 s390_llong(code, ins->inst_imm);
3383 s390_slg (code, ins->dreg, 0, s390_r13, 4);
3388 if (ins->dreg != ins->sreg1) {
3389 s390_lgr (code, ins->dreg, ins->sreg1);
3391 if (s390_is_imm16 (-ins->inst_imm)) {
3392 s390_lghi (code, s390_r0, ins->inst_imm);
3393 s390_slbgr (code, ins->dreg, s390_r0);
3395 s390_basr (code, s390_r13, 0);
3397 s390_llong(code, ins->inst_imm);
3398 s390_slbg (code, ins->dreg, 0, s390_r13, 4);
3402 case OP_SUB_OVF_CARRY: {
3404 s390_lghi (code, s390_r0, 0);
3405 s390_lgr (code, s390_r1, s390_r0);
3406 s390_slbgr (code, s390_r0, s390_r1);
3407 s390_sgr (code, ins->dreg, src2);
3408 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_OV, "OverflowException");
3409 s390_agr (code, ins->dreg, s390_r0);
3410 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_OV, "OverflowException");
3413 case OP_SUB_OVF_UN_CARRY: {
3415 s390_slbgr (code, ins->dreg, src2);
3416 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_NC, "OverflowException");
3420 if (ins->sreg1 == ins->dreg) {
3421 s390_ngr (code, ins->dreg, ins->sreg2);
3424 if (ins->sreg2 == ins->dreg) {
3425 s390_ngr (code, ins->dreg, ins->sreg1);
3428 s390_lgr (code, ins->dreg, ins->sreg1);
3429 s390_ngr (code, ins->dreg, ins->sreg2);
3435 if (ins->dreg != ins->sreg1) {
3436 s390_lgr (code, ins->dreg, ins->sreg1);
3438 if (s390_is_imm16 (ins->inst_imm)) {
3439 s390_lghi (code, s390_r0, ins->inst_imm);
3440 s390_ngr (code, ins->dreg, s390_r0);
3442 s390_basr (code, s390_r13, 0);
3444 s390_llong(code, ins->inst_imm);
3445 s390_ng (code, ins->dreg, 0, s390_r13, 4);
3450 s390_lgr (code, s390_r1, ins->sreg1);
3451 s390_dsgr (code, s390_r0, ins->sreg2);
3452 s390_lgr (code, ins->dreg, s390_r1);
3456 s390_lgr (code, s390_r1, ins->sreg1);
3457 s390_lghi (code, s390_r0, 0);
3458 s390_dlgr (code, s390_r0, ins->sreg2);
3459 s390_lgr (code, ins->dreg, s390_r1);
3463 s390_lgr (code, s390_r1, ins->sreg1);
3464 s390_dsgr (code, s390_r0, ins->sreg2);
3465 s390_lgr (code, ins->dreg, s390_r0);
3469 s390_lgr (code, s390_r1, ins->sreg1);
3470 s390_lghi (code, s390_r0, 0);
3471 s390_dlgr (code, s390_r0, ins->sreg2);
3472 s390_lgr (code, ins->dreg, s390_r0);
3476 if (ins->sreg1 == ins->dreg) {
3477 s390_ogr (code, ins->dreg, ins->sreg2);
3480 if (ins->sreg2 == ins->dreg) {
3481 s390_ogr (code, ins->dreg, ins->sreg1);
3484 s390_lgr (code, ins->dreg, ins->sreg1);
3485 s390_ogr (code, ins->dreg, ins->sreg2);
3491 if (ins->dreg != ins->sreg1) {
3492 s390_lgr (code, ins->dreg, ins->sreg1);
3494 if (s390_is_imm16 (ins->inst_imm)) {
3495 s390_lghi (code, s390_r0, ins->inst_imm);
3496 s390_ogr (code, ins->dreg, s390_r0);
3498 s390_basr (code, s390_r13, 0);
3500 s390_llong(code, ins->inst_imm);
3501 s390_og (code, ins->dreg, 0, s390_r13, 4);
3506 if (ins->sreg1 == ins->dreg) {
3507 s390_xgr (code, ins->dreg, ins->sreg2);
3510 if (ins->sreg2 == ins->dreg) {
3511 s390_xgr (code, ins->dreg, ins->sreg1);
3514 s390_lgr (code, ins->dreg, ins->sreg1);
3515 s390_xgr (code, ins->dreg, ins->sreg2);
3521 if (ins->dreg != ins->sreg1) {
3522 s390_lgr (code, ins->dreg, ins->sreg1);
3524 if (s390_is_imm16 (ins->inst_imm)) {
3525 s390_lghi (code, s390_r0, ins->inst_imm);
3526 s390_xgr (code, ins->dreg, s390_r0);
3528 s390_basr (code, s390_r13, 0);
3530 s390_llong(code, ins->inst_imm);
3531 s390_xg (code, ins->dreg, 0, s390_r13, 4);
3537 s390_sllg (code, ins->dreg, ins->dreg, src2, 0);
3542 if (ins->sreg1 != ins->dreg) {
3543 s390_lgr (code, ins->dreg, ins->sreg1);
3545 s390_sllg (code, ins->dreg, ins->dreg, 0, (ins->inst_imm & 0x3f));
3550 s390_srag (code, ins->dreg, ins->dreg, src2, 0);
3555 if (ins->sreg1 != ins->dreg) {
3556 s390_lgr (code, ins->dreg, ins->sreg1);
3558 s390_srag (code, ins->dreg, ins->dreg, 0, (ins->inst_imm & 0x3f));
3562 case OP_LSHR_UN_IMM: {
3563 if (ins->sreg1 != ins->dreg) {
3564 s390_lgr (code, ins->dreg, ins->sreg1);
3566 s390_srlg (code, ins->dreg, ins->dreg, 0, (ins->inst_imm & 0x3f));
3571 s390_srlg (code, ins->dreg, ins->dreg, src2, 0);
3575 if (ins->sreg1 != ins->dreg) {
3576 s390_lgr (code, ins->dreg, ins->sreg1);
3578 s390_lghi (code, s390_r0, -1);
3579 s390_xgr (code, ins->dreg, s390_r0);
3583 s390_lcgr (code, ins->dreg, ins->sreg1);
3588 s390_msgr (code, ins->dreg, src2);
3593 if (ins->dreg != ins->sreg1) {
3594 s390_lgr (code, ins->dreg, ins->sreg1);
3596 if (s390_is_imm16 (ins->inst_imm)) {
3597 s390_lghi (code, s390_r13, ins->inst_imm);
3599 s390_basr (code, s390_r13, 0);
3601 s390_llong(code, ins->inst_imm);
3602 s390_lg (code, s390_r13, 0, s390_r13, 4);
3604 s390_msgr (code, ins->dreg, s390_r13);
3609 s390_ltgr (code, s390_r1, ins->sreg1);
3610 s390_jz (code, 0); CODEPTR(code, o[0]);
3611 s390_ltgr (code, s390_r0, ins->sreg2);
3613 s390_lghi (code, s390_r1, 0);
3614 s390_j (code, 0); CODEPTR(code, o[1]);
3615 s390_xgr (code, s390_r0, s390_r1);
3616 s390_msgr (code, s390_r1, ins->sreg2);
3617 s390_xgr (code, s390_r0, s390_r1);
3618 s390_srlg (code, s390_r0, s390_r0, 0, 63);
3619 s390_ltgr (code, s390_r0, s390_r0);
3620 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_NZ, "OverflowException");
3621 PTRSLOT (code, o[0]);
3622 PTRSLOT (code, o[1]);
3623 s390_lgr (code, ins->dreg, s390_r1);
3626 case OP_LMUL_OVF_UN: {
3627 s390_lghi (code, s390_r0, 0);
3628 s390_lgr (code, s390_r1, ins->sreg1);
3629 s390_mlgr (code, s390_r0, ins->sreg2);
3630 s390_ltgr (code, s390_r0, s390_r0);
3631 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_NZ, "OverflowException");
3632 s390_lgr (code, ins->dreg, s390_r1);
3636 g_assert_not_reached ();
3638 s390_algr (code, ins->dreg, src2);
3643 s390_agr (code, ins->dreg, src2);
3647 g_assert_not_reached ();
3649 s390_alcgr (code, ins->dreg, src2);
3653 if (ins->dreg != ins->sreg1) {
3654 s390_lgfr (code, ins->dreg, ins->sreg1);
3656 if (s390_is_imm16 (ins->inst_imm)) {
3657 s390_aghi(code, ins->dreg, ins->inst_imm);
3659 s390_basr (code, s390_r13, 0);
3661 s390_word (code, ins->inst_imm);
3662 s390_agf (code, ins->dreg, 0, s390_r13, 4);
3667 if (ins->dreg != ins->sreg1) {
3668 s390_lgfr (code, ins->dreg, ins->sreg1);
3670 if (s390_is_imm16 (ins->inst_imm)) {
3671 s390_lghi (code, s390_r0, ins->inst_imm);
3672 s390_alcgr (code, ins->dreg, s390_r0);
3674 s390_basr (code, s390_r13, 0);
3676 s390_word (code, ins->inst_imm);
3677 s390_lgf (code, s390_r13, 0, s390_r13, 4);
3678 s390_alcgr (code, ins->dreg, s390_r13);
3683 case OP_S390_LADD_OVF: {
3685 s390_agr (code, ins->dreg, src2);
3686 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_OV, "OverflowException");
3689 case OP_LADD_OVF_UN:
3690 case OP_S390_LADD_OVF_UN: {
3692 s390_algr (code, ins->dreg, src2);
3693 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_CY, "OverflowException");
3697 CHECK_SRCDST_NCOM_I;
3698 s390_slgr (code, ins->dreg, src2);
3702 CHECK_SRCDST_NCOM_I;
3703 s390_sgr (code, ins->dreg, src2);
3707 CHECK_SRCDST_NCOM_I;
3708 s390_slbgr (code, ins->dreg, src2);
3712 if (ins->dreg != ins->sreg1) {
3713 s390_lgfr (code, ins->dreg, ins->sreg1);
3715 if (s390_is_imm16 (-ins->inst_imm)) {
3716 s390_aghi (code, ins->dreg, -ins->inst_imm);
3718 s390_basr (code, s390_r13, 0);
3720 s390_word (code, ins->inst_imm);
3721 s390_sgf (code, ins->dreg, 0, s390_r13, 4);
3726 s390_basr (code, s390_r13, 0);
3728 s390_word (code, ins->inst_imm);
3729 s390_slgf (code, ins->dreg, 0, s390_r13, 4);
3733 case OP_S390_ISUB_OVF: {
3735 s390_sr (code, ins->dreg, src2);
3736 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_OV, "OverflowException");
3737 s390_lgfr (code, ins->dreg, ins->dreg);
3740 case OP_ISUB_OVF_UN:
3741 case OP_S390_ISUB_OVF_UN: {
3743 s390_slr (code, ins->dreg, src2);
3744 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_NC, "OverflowException");
3745 s390_llgfr(code, ins->dreg, ins->dreg);
3749 case OP_S390_LSUB_OVF: {
3751 s390_sgr (code, ins->dreg, src2);
3752 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_OV, "OverflowException");
3755 case OP_LSUB_OVF_UN:
3756 case OP_S390_LSUB_OVF_UN: {
3758 s390_slgr (code, ins->dreg, src2);
3759 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_NC, "OverflowException");
3763 CHECK_SRCDST_NCOM_I;
3764 s390_ngr (code, ins->dreg, src2);
3768 if (ins->dreg != ins->sreg1) {
3769 s390_lgfr (code, ins->dreg, ins->sreg1);
3771 if (s390_is_imm16 (ins->inst_imm)) {
3772 s390_lghi (code, s390_r0, ins->inst_imm);
3773 s390_ngr (code, ins->dreg, s390_r0);
3775 s390_basr (code, s390_r13, 0);
3777 s390_llong(code, ins->inst_imm);
3778 s390_ng (code, ins->dreg, 0, s390_r13, 4);
3783 s390_lgfr (code, s390_r0, ins->sreg1);
3784 s390_srda (code, s390_r0, 0, 32);
3785 s390_dr (code, s390_r0, ins->sreg2);
3786 s390_lgfr (code, ins->dreg, s390_r1);
3790 s390_lgfr (code, s390_r0, ins->sreg1);
3791 s390_srdl (code, s390_r0, 0, 32);
3792 s390_dlr (code, s390_r0, ins->sreg2);
3793 s390_lgfr (code, ins->dreg, s390_r1);
3797 if (s390_is_imm16 (ins->inst_imm)) {
3798 s390_lghi (code, s390_r13, ins->inst_imm);
3799 s390_lgfr (code, s390_r0, ins->sreg1);
3801 s390_basr (code, s390_r13, 0);
3803 s390_word (code, ins->inst_imm);
3804 s390_lgfr (code, s390_r0, ins->sreg1);
3805 s390_lgf (code, s390_r13, 0, s390_r13, 4);
3807 s390_srda (code, s390_r0, 0, 32);
3808 s390_dr (code, s390_r0, ins->sreg2);
3809 s390_lgfr (code, ins->dreg, s390_r1);
3813 s390_lgfr (code, s390_r0, ins->sreg1);
3814 s390_srda (code, s390_r0, 0, 32);
3815 s390_dr (code, s390_r0, ins->sreg2);
3816 s390_lgfr (code, ins->dreg, s390_r0);
3819 s390_lgfr (code, s390_r0, ins->sreg1);
3820 s390_srdl (code, s390_r0, 0, 32);
3821 s390_dlr (code, s390_r0, ins->sreg2);
3822 s390_lgfr (code, ins->dreg, s390_r0);
3826 if (s390_is_imm16 (ins->inst_imm)) {
3827 s390_lghi (code, s390_r13, ins->inst_imm);
3828 s390_lgfr (code, s390_r0, ins->sreg1);
3830 s390_basr (code, s390_r13, 0);
3832 s390_word (code, ins->inst_imm);
3833 s390_lgfr (code, s390_r0, ins->sreg1);
3834 s390_lgf (code, s390_r13, 0, s390_r13, 4);
3836 s390_srda (code, s390_r0, 0, 32);
3837 s390_dr (code, s390_r0, ins->sreg2);
3838 s390_lgfr (code, ins->dreg, s390_r0);
3843 s390_ogr (code, ins->dreg, src2);
3847 if (ins->dreg != ins->sreg1) {
3848 s390_lgfr (code, ins->dreg, ins->sreg1);
3850 if (s390_is_imm16 (ins->inst_imm)) {
3851 s390_lghi (code, s390_r0, ins->inst_imm);
3852 s390_ogr (code, ins->dreg, s390_r0);
3854 s390_basr (code, s390_r13, 0);
3856 s390_llong(code, ins->inst_imm);
3857 s390_og (code, ins->dreg, 0, s390_r13, 4);
3863 s390_xgr (code, ins->dreg, src2);
3867 if (ins->dreg != ins->sreg1) {
3868 s390_lgfr (code, ins->dreg, ins->sreg1);
3870 if (s390_is_imm16 (ins->inst_imm)) {
3871 s390_lghi (code, s390_r0, ins->inst_imm);
3872 s390_xgr (code, ins->dreg, s390_r0);
3874 s390_basr (code, s390_r13, 0);
3876 s390_llong(code, ins->inst_imm);
3877 s390_xg (code, ins->dreg, 0, s390_r13, 4);
3883 s390_sll (code, ins->dreg, src2, 0);
3887 if (ins->sreg1 != ins->dreg) {
3888 s390_lgfr (code, ins->dreg, ins->sreg1);
3890 s390_sll (code, ins->dreg, 0, (ins->inst_imm & 0x1f));
3895 s390_sra (code, ins->dreg, src2, 0);
3899 if (ins->sreg1 != ins->dreg) {
3900 s390_lgfr (code, ins->dreg, ins->sreg1);
3902 s390_sra (code, ins->dreg, 0, (ins->inst_imm & 0x1f));
3905 case OP_ISHR_UN_IMM: {
3906 if (ins->sreg1 != ins->dreg) {
3907 s390_lgfr (code, ins->dreg, ins->sreg1);
3909 s390_srl (code, ins->dreg, 0, (ins->inst_imm & 0x1f));
3914 s390_srl (code, ins->dreg, src2, 0);
3918 if (ins->sreg1 != ins->dreg) {
3919 s390_lgfr (code, ins->dreg, ins->sreg1);
3921 s390_lghi (code, s390_r0, -1);
3922 s390_xgr (code, ins->dreg, s390_r0);
3926 s390_lcgr (code, ins->dreg, ins->sreg1);
3931 s390_msr (code, ins->dreg, src2);
3935 if (ins->dreg != ins->sreg1) {
3936 s390_lgfr (code, ins->dreg, ins->sreg1);
3938 if (s390_is_imm16 (ins->inst_imm)) {
3939 s390_lghi (code, s390_r13, ins->inst_imm);
3941 s390_basr (code, s390_r13, 0);
3943 s390_word (code, ins->inst_imm);
3944 s390_lgf (code, s390_r13, 0, s390_r13, 4);
3946 s390_msr (code, ins->dreg, s390_r13);
3951 s390_ltr (code, s390_r1, ins->sreg1);
3952 s390_jz (code, 0); CODEPTR(code, o[0]);
3953 s390_ltr (code, s390_r0, ins->sreg2);
3955 s390_lhi (code, s390_r1, 0);
3956 s390_j (code, 0); CODEPTR(code, o[1]);
3957 s390_xr (code, s390_r0, s390_r1);
3958 s390_msr (code, s390_r1, ins->sreg2);
3959 s390_xr (code, s390_r0, s390_r1);
3960 s390_srl (code, s390_r0, 0, 31);
3961 s390_ltr (code, s390_r0, s390_r0);
3962 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_NZ, "OverflowException");
3963 PTRSLOT (code, o[0]);
3964 PTRSLOT (code, o[1]);
3965 s390_lgfr (code, ins->dreg, s390_r1);
3968 case OP_IMUL_OVF_UN: {
3969 s390_lhi (code, s390_r0, 0);
3970 s390_lr (code, s390_r1, ins->sreg1);
3971 s390_mlr (code, s390_r0, ins->sreg2);
3972 s390_ltr (code, s390_r0, s390_r0);
3973 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_NZ, "OverflowException");
3974 s390_lgfr (code, ins->dreg, s390_r1);
3979 if (s390_is_imm16(ins->inst_c0)) {
3980 s390_lghi (code, ins->dreg, ins->inst_c0);
3982 s390_basr (code, s390_r13, 0);
3984 s390_llong(code, ins->inst_c0);
3985 s390_lg (code, ins->dreg, 0, s390_r13, 4);
3990 s390_basr (code, s390_r13, 0);
3992 mono_add_patch_info (cfg, code - cfg->native_code,
3993 (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
3994 s390_llong(code, 0);
3995 s390_lg (code,ins->dreg, 0, s390_r13, 4);
3998 case OP_JUMP_TABLE: {
3999 mono_add_patch_info (cfg, code - cfg->native_code,
4000 (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
4001 s390_basr (code, s390_r13, 0);
4003 s390_llong (code, 0);
4004 s390_lg (code, ins->dreg, 0, s390_r13, 4);
4008 if (ins->dreg != ins->sreg1) {
4009 s390_lgr (code, ins->dreg, ins->sreg1);
4013 case OP_LCONV_TO_I8:
4015 s390_lgfr (code, ins->dreg, ins->sreg1);
4017 case OP_LCONV_TO_I4:
4018 s390_lgfr (code, ins->dreg, ins->sreg1);
4021 case OP_LCONV_TO_U8:
4022 case OP_LCONV_TO_U4:
4024 s390_llgfr (code, ins->dreg, ins->sreg1);
4026 case OP_LCONV_TO_OVF_U4:
4027 s390_basr (code, s390_r13, 0);
4029 s390_llong(code, 4294967295);
4030 s390_clg (code, ins->sreg1, 0, s390_r13, 4);
4031 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_GT, "OverflowException");
4032 s390_ltgr (code, ins->sreg1, ins->sreg1);
4033 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_LT, "OverflowException");
4034 s390_llgfr(code, ins->dreg, ins->sreg1);
4036 case OP_LCONV_TO_OVF_I4_UN:
4037 s390_basr (code, s390_r13, 0);
4039 s390_llong(code, 2147483647);
4040 s390_cg (code, ins->sreg1, 0, s390_r13, 4);
4041 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_GT, "OverflowException");
4042 s390_ltgr (code, ins->sreg1, ins->sreg1);
4043 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_LT, "OverflowException");
4044 s390_lgfr (code, ins->dreg, ins->sreg1);
4047 case OP_FCONV_TO_R4: {
4048 if (ins->dreg != ins->sreg1) {
4049 s390_ldr (code, ins->dreg, ins->sreg1);
4053 case OP_S390_SETF4RET: {
4054 s390_ledbr (code, ins->dreg, ins->sreg1);
4058 if (s390_is_imm16 (ins->inst_offset)) {
4059 s390_lghi (code, s390_r13, ins->inst_offset);
4061 s390_bras (code, s390_r13, 0);
4063 s390_llong(code, ins->inst_offset);
4064 s390_lg (code, s390_r13, 0, s390_r13, 4);
4066 s390_ear (code, s390_r1, 0);
4067 s390_sllg(code, s390_r1, s390_r1, 0, 32);
4068 s390_ear (code, s390_r1, 1);
4069 s390_lg (code, ins->dreg, s390_r13, s390_r1, 0);
4073 if (cfg->method->save_lmf)
4074 restoreLMF(code, cfg->frame_reg, cfg->stack_usage);
4076 if (cfg->flags & MONO_CFG_HAS_TAIL) {
4077 code = emit_load_volatile_arguments (code, cfg);
4080 code = backUpStackPtr(cfg, code);
4081 s390_lg (code, s390_r14, 0, cfg->frame_reg, S390_RET_ADDR_OFFSET);
4082 mono_add_patch_info (cfg, code - cfg->native_code,
4083 MONO_PATCH_INFO_METHOD_JUMP,
4085 s390_jcl (code, S390_CC_UN, 0);
4088 case OP_CHECK_THIS: {
4089 /* ensure ins->sreg1 is not NULL */
4090 s390_lg (code, s390_r0, 0, ins->sreg1, 0);
4091 s390_ltgr (code, s390_r0, s390_r0);
4092 // EMIT_COND_SYSTEM_EXCEPTION (S390_CC_ZR, "NullReferenceException");
4096 int offset = cfg->sig_cookie + cfg->stack_usage;
4098 if (s390_is_imm16 (offset))
4099 s390_lghi (code, s390_r0, offset);
4101 s390_basr (code, s390_r13, 0);
4103 s390_llong(code, offset);
4104 s390_lg (code, s390_r0, 0, s390_r13, 0);
4106 s390_agr (code, s390_r0, cfg->frame_reg);
4107 s390_stg (code, s390_r0, 0, ins->sreg1, 0);
4111 s390_basr (code, s390_r13, 0);
4113 call = (MonoCallInst*)ins;
4114 if (ins->flags & MONO_INST_HAS_METHOD)
4115 mono_add_patch_info (cfg, code-cfg->native_code,
4116 MONO_PATCH_INFO_METHOD,
4119 mono_add_patch_info (cfg, code-cfg->native_code,
4120 MONO_PATCH_INFO_ABS,
4122 s390_llong(code, 0);
4123 s390_lg (code, s390_r14, 0, s390_r13, 4);
4124 s390_basr (code, s390_r14, s390_r14);
4125 if (call->signature->ret->type == MONO_TYPE_R4)
4126 s390_ldebr (code, s390_f0, s390_f0);
4134 s390_basr (code, s390_r13, 0);
4136 call = (MonoCallInst*)ins;
4137 if (ins->flags & MONO_INST_HAS_METHOD)
4138 mono_add_patch_info (cfg, code-cfg->native_code,
4139 MONO_PATCH_INFO_METHOD,
4142 mono_add_patch_info (cfg, code-cfg->native_code,
4143 MONO_PATCH_INFO_ABS,
4145 s390_llong(code, 0);
4146 s390_lg (code, s390_r14, 0, s390_r13, 4);
4147 s390_basr (code, s390_r14, s390_r14);
4150 case OP_FCALL_REG: {
4151 call = (MonoCallInst*)ins;
4152 s390_lgr (code, s390_r1, ins->sreg1);
4153 s390_basr (code, s390_r14, s390_r1);
4154 if (call->signature->ret->type == MONO_TYPE_R4)
4155 s390_ldebr (code, s390_f0, s390_f0);
4161 case OP_VOIDCALL_REG:
4163 s390_lgr (code, s390_r1, ins->sreg1);
4164 s390_basr (code, s390_r14, s390_r1);
4167 case OP_FCALL_MEMBASE: {
4168 call = (MonoCallInst*)ins;
4169 s390_lg (code, s390_r1, 0, ins->sreg1, ins->inst_offset);
4170 s390_basr (code, s390_r14, s390_r1);
4171 if (call->signature->ret->type == MONO_TYPE_R4)
4172 s390_ldebr (code, s390_f0, s390_f0);
4175 case OP_LCALL_MEMBASE:
4176 case OP_VCALL_MEMBASE:
4177 case OP_VCALL2_MEMBASE:
4178 case OP_VOIDCALL_MEMBASE:
4179 case OP_CALL_MEMBASE: {
4180 s390_lg (code, s390_r1, 0, ins->sreg1, ins->inst_offset);
4181 s390_basr (code, s390_r14, s390_r1);
4188 if (cfg->param_area == 0)
4189 alloca_skip = S390_MINIMAL_STACK_SIZE;
4191 alloca_skip = cfg->param_area;
4193 area_offset = S390_ALIGN(alloca_skip, S390_STACK_ALIGNMENT);
4194 s390_lgr (code, s390_r1, ins->sreg1);
4195 if (ins->flags & MONO_INST_INIT)
4196 s390_lgr (code, s390_r0, ins->sreg1);
4197 s390_aghi (code, s390_r1, 14);
4198 s390_srlg (code, s390_r1, s390_r1, 0, 3);
4199 s390_sllg (code, s390_r1, s390_r1, 0, 3);
4200 if (cfg->method->save_lmf) {
4201 /*----------------------------------*/
4202 /* we have to adjust lmf ebp value */
4203 /*----------------------------------*/
4204 int lmfOffset = cfg->stack_usage - sizeof(MonoLMF);
4206 s390_lgr (code, s390_r13, cfg->frame_reg);
4207 if (s390_is_imm16(lmfOffset))
4208 s390_aghi (code, s390_r13, lmfOffset);
4210 s390_basr (code, s390_r14, 0);
4212 s390_word (code, lmfOffset);
4213 s390_agf (code, s390_r13, 0, s390_r14, 4);
4215 s390_lgr (code, s390_r14, STK_BASE);
4216 s390_sgr (code, s390_r14, s390_r1);
4217 s390_stg (code, s390_r14, 0, s390_r13,
4218 G_STRUCT_OFFSET(MonoLMF, ebp));
4220 s390_lg (code, s390_r13, 0, STK_BASE, 0);
4221 s390_sgr (code, STK_BASE, s390_r1);
4222 s390_stg (code, s390_r13, 0, STK_BASE, 0);
4223 s390_la (code, ins->dreg, 0, STK_BASE, area_offset);
4224 s390_srlg (code, ins->dreg, ins->dreg, 0, 3);
4225 s390_sllg (code, ins->dreg, ins->dreg, 0, 3);
4226 if (ins->flags & MONO_INST_INIT) {
4227 s390_lgr (code, s390_r1, s390_r0);
4228 s390_lgr (code, s390_r0, ins->dreg);
4229 s390_lgr (code, s390_r14, s390_r12);
4230 s390_lghi (code, s390_r13, 0);
4231 s390_mvcle(code, s390_r0, s390_r12, 0, 0);
4233 s390_lgr (code, s390_r12, s390_r14);
4238 s390_lgr (code, s390_r2, ins->sreg1);
4239 s390_basr (code, s390_r13, 0);
4241 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
4242 (gpointer) "mono_arch_throw_exception");
4243 s390_llong(code, 0);
4244 s390_lg (code, s390_r14, 0, s390_r13, 4);
4245 s390_basr (code, s390_r14, s390_r14);
4249 s390_lgr (code, s390_r2, ins->sreg1);
4250 s390_basr (code, s390_r13, 0);
4252 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
4253 (gpointer) "mono_arch_rethrow_exception");
4254 s390_llong(code, 0);
4255 s390_lg (code, s390_r14, 0, s390_r13, 4);
4256 s390_basr (code, s390_r14, s390_r14);
4259 case OP_START_HANDLER: {
4260 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
4262 S390_LONG (code, stg, stg, s390_r14, 0,
4263 spvar->inst_basereg,
4264 spvar->inst_offset);
4267 case OP_ENDFILTER: {
4268 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
4270 if (ins->sreg1 != s390_r2)
4271 s390_lgr(code, s390_r2, ins->sreg1);
4272 S390_LONG (code, lg, lg, s390_r14, 0,
4273 spvar->inst_basereg,
4274 spvar->inst_offset);
4275 s390_br (code, s390_r14);
4278 case OP_ENDFINALLY: {
4279 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
4281 S390_LONG (code, lg, lg, s390_r14, 0,
4282 spvar->inst_basereg,
4283 spvar->inst_offset);
4284 s390_br (code, s390_r14);
4287 case OP_CALL_HANDLER: {
4288 mono_add_patch_info (cfg, code-cfg->native_code,
4289 MONO_PATCH_INFO_BB, ins->inst_target_bb);
4290 s390_brasl (code, s390_r14, 0);
4291 mono_cfg_add_try_hole (cfg, ins->inst_eh_block, code, bb);
4295 ins->inst_c0 = code - cfg->native_code;
4298 case OP_RELAXED_NOP:
4301 case OP_DUMMY_STORE:
4302 case OP_NOT_REACHED:
4306 case OP_SEQ_POINT: {
4309 if (cfg->compile_aot)
4313 * Read from the single stepping trigger page. This will cause a
4314 * SIGSEGV when single stepping is enabled.
4315 * We do this _before_ the breakpoint, so single stepping after
4316 * a breakpoint is hit will step to the next IL offset.
4318 if (ins->flags & MONO_INST_SINGLE_STEP_LOC) {
4319 breakpointCode.pTrigger = ss_trigger_page;
4320 memcpy(code, (void *) &breakpointCode, BREAKPOINT_SIZE);
4321 code += BREAKPOINT_SIZE;
4324 mono_add_seq_point (cfg, bb, ins, code - cfg->native_code);
4327 * A placeholder for a possible breakpoint inserted by
4328 * mono_arch_set_breakpoint ().
4330 for (i = 0; i < (BREAKPOINT_SIZE / S390X_NOP_SIZE); ++i)
4336 EMIT_UNCOND_BRANCH(ins);
4339 s390_br (code, ins->sreg1);
4345 s390_lghi(code, ins->dreg, 1);
4347 s390_lghi(code, ins->dreg, 0);
4353 s390_lghi(code, ins->dreg, 1);
4355 s390_lghi(code, ins->dreg, 0);
4361 s390_lghi(code, ins->dreg, 1);
4363 s390_lghi(code, ins->dreg, 0);
4369 s390_lghi(code, ins->dreg, 1);
4371 s390_lghi(code, ins->dreg, 0);
4377 s390_lghi(code, ins->dreg, 1);
4379 s390_lghi(code, ins->dreg, 0);
4382 case OP_COND_EXC_EQ:
4383 case OP_COND_EXC_IEQ:
4384 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_EQ, ins->inst_p1);
4386 case OP_COND_EXC_NE_UN:
4387 case OP_COND_EXC_INE_UN:
4388 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_NE, ins->inst_p1);
4390 case OP_COND_EXC_LT:
4391 case OP_COND_EXC_ILT:
4392 case OP_COND_EXC_LT_UN:
4393 case OP_COND_EXC_ILT_UN:
4394 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_LT, ins->inst_p1);
4396 case OP_COND_EXC_GT:
4397 case OP_COND_EXC_IGT:
4398 case OP_COND_EXC_GT_UN:
4399 case OP_COND_EXC_IGT_UN:
4400 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_GT, ins->inst_p1);
4402 case OP_COND_EXC_GE:
4403 case OP_COND_EXC_IGE:
4404 case OP_COND_EXC_GE_UN:
4405 case OP_COND_EXC_IGE_UN:
4406 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_GE, ins->inst_p1);
4408 case OP_COND_EXC_LE:
4409 case OP_COND_EXC_ILE:
4410 case OP_COND_EXC_LE_UN:
4411 case OP_COND_EXC_ILE_UN:
4412 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_LE, ins->inst_p1);
4414 case OP_COND_EXC_OV:
4415 case OP_COND_EXC_IOV:
4416 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_OV, ins->inst_p1);
4418 case OP_COND_EXC_NO:
4419 case OP_COND_EXC_INO:
4420 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_NO, ins->inst_p1);
4423 case OP_COND_EXC_IC:
4424 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_CY, ins->inst_p1);
4426 case OP_COND_EXC_NC:
4427 case OP_COND_EXC_INC:
4428 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_NC, ins->inst_p1);
4432 EMIT_COND_BRANCH (ins, S390_CC_EQ);
4436 EMIT_COND_BRANCH (ins, S390_CC_NE);
4442 EMIT_COND_BRANCH (ins, S390_CC_LT);
4448 EMIT_COND_BRANCH (ins, S390_CC_GT);
4454 EMIT_COND_BRANCH (ins, S390_CC_GE);
4460 EMIT_COND_BRANCH (ins, S390_CC_LE);
4463 /* floating point opcodes */
4465 if (*((double *) ins->inst_p0) == 0) {
4466 s390_lzdr (code, ins->dreg);
4468 s390_basr (code, s390_r13, 0);
4470 s390_llong (code, ins->inst_p0);
4471 s390_lg (code, s390_r13, 0, s390_r13, 4);
4472 s390_ld (code, ins->dreg, 0, s390_r13, 0);
4477 if (*((float *) ins->inst_p0) == 0) {
4478 s390_lzdr (code, ins->dreg);
4480 s390_basr (code, s390_r13, 0);
4482 s390_llong(code, ins->inst_p0);
4483 s390_lg (code, s390_r13, 0, s390_r13, 4);
4484 s390_ldeb (code, ins->dreg, 0, s390_r13, 0);
4488 case OP_STORER8_MEMBASE_REG: {
4489 S390_LONG (code, stdy, std, ins->sreg1, 0,
4490 ins->inst_destbasereg, ins->inst_offset);
4493 case OP_LOADR8_MEMBASE: {
4494 S390_LONG (code, ldy, ld, ins->dreg, 0,
4495 ins->inst_basereg, ins->inst_offset);
4498 case OP_STORER4_MEMBASE_REG: {
4499 s390_ledbr (code, s390_f15, ins->sreg1);
4500 S390_LONG (code, stey, ste, s390_f15, 0,
4501 ins->inst_destbasereg, ins->inst_offset);
4504 case OP_LOADR4_MEMBASE: {
4505 S390_LONG (code, ldy, ld, s390_f15, 0,
4506 ins->inst_basereg, ins->inst_offset);
4507 s390_ldebr (code, ins->dreg, s390_f15);
4510 case OP_ICONV_TO_R_UN: {
4511 s390_cdfbr (code, ins->dreg, ins->sreg1);
4512 s390_ltr (code, ins->sreg1, ins->sreg1);
4513 s390_jnl (code, 12);
4514 s390_basr (code, s390_r13, 0);
4516 s390_word (code, 0x41f00000);
4517 s390_word (code, 0);
4518 s390_adb (code, ins->dreg, 0, s390_r13, 4);
4521 case OP_LCONV_TO_R_UN: {
4522 s390_cdgbr (code, ins->dreg, ins->sreg1);
4523 s390_ltgr (code, ins->sreg1, ins->sreg1);
4524 s390_jnl (code, 12);
4525 s390_basr (code, s390_r13, 0);
4527 s390_word (code, 0x41f00000);
4528 s390_word (code, 0);
4529 s390_adb (code, ins->dreg, 0, s390_r13, 4);
4532 case OP_LCONV_TO_R4:
4533 case OP_ICONV_TO_R4: {
4534 s390_cdgbr (code, ins->dreg, ins->sreg1);
4537 case OP_LCONV_TO_R8:
4538 case OP_ICONV_TO_R8: {
4539 s390_cdgbr (code, ins->dreg, ins->sreg1);
4542 case OP_FCONV_TO_I1:
4543 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 1, TRUE);
4545 case OP_FCONV_TO_U1:
4546 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 1, FALSE);
4548 case OP_FCONV_TO_I2:
4549 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 2, TRUE);
4551 case OP_FCONV_TO_U2:
4552 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 2, FALSE);
4554 case OP_FCONV_TO_I4:
4556 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 4, TRUE);
4558 case OP_FCONV_TO_U4:
4560 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 4, FALSE);
4562 case OP_FCONV_TO_I8:
4563 s390_cgdbr (code, ins->dreg, 5, ins->sreg1);
4565 case OP_LCONV_TO_OVF_I: {
4566 /* Valid ints: 0xffffffff:8000000 to 00000000:0x7f000000 */
4568 s390_ltgr (code, ins->sreg2, ins->sreg2);
4569 s390_jnl (code, 0); CODEPTR(code, o[0]);
4570 s390_ltgr (code, ins->sreg1, ins->sreg1);
4571 s390_jnl (code, 0); CODEPTR(code, o[1]);
4572 s390_lhi (code, s390_r13, -1);
4573 s390_cgr (code, ins->sreg1, s390_r13);
4574 s390_jnz (code, 0); CODEPTR(code, o[2]);
4575 if (ins->dreg != ins->sreg2)
4576 s390_lgr (code, ins->dreg, ins->sreg2);
4577 s390_j (code, 0); CODEPTR(code, o[3]);
4578 PTRSLOT(code, o[0]);
4579 s390_jz (code, 0); CODEPTR(code, o[4]);
4580 PTRSLOT(code, o[1]);
4581 PTRSLOT(code, o[2]);
4582 mono_add_patch_info (cfg, code - cfg->native_code,
4583 MONO_PATCH_INFO_EXC, "OverflowException");
4584 s390_brasl (code, s390_r14, 0);
4585 PTRSLOT(code, o[3]);
4586 PTRSLOT(code, o[4]);
4590 s390_lpdbr (code, ins->dreg, ins->sreg1);
4594 s390_sqdbr (code, ins->dreg, ins->sreg1);
4599 s390_adbr (code, ins->dreg, src2);
4603 CHECK_SRCDST_NCOM_F;
4604 s390_sdbr (code, ins->dreg, src2);
4609 s390_mdbr (code, ins->dreg, src2);
4613 CHECK_SRCDST_NCOM_F;
4614 s390_ddbr (code, ins->dreg, src2);
4618 s390_lcdbr (code, ins->dreg, ins->sreg1);
4622 CHECK_SRCDST_NCOM_F;
4623 s390_didbr (code, ins->dreg, src2, 5, s390_f15);
4627 s390_cdbr (code, ins->sreg1, ins->sreg2);
4631 s390_cdbr (code, ins->sreg1, ins->sreg2);
4632 s390_lghi (code, ins->dreg, 1);
4634 s390_lghi (code, ins->dreg, 0);
4638 s390_cdbr (code, ins->sreg1, ins->sreg2);
4639 s390_lghi (code, ins->dreg, 1);
4641 s390_lghi (code, ins->dreg, 0);
4645 s390_cdbr (code, ins->sreg1, ins->sreg2);
4646 s390_lghi (code, ins->dreg, 1);
4648 s390_lghi (code, ins->dreg, 0);
4652 s390_cdbr (code, ins->sreg1, ins->sreg2);
4653 s390_lghi (code, ins->dreg, 1);
4655 s390_lghi (code, ins->dreg, 0);
4659 s390_cdbr (code, ins->sreg1, ins->sreg2);
4660 s390_lghi (code, ins->dreg, 1);
4662 s390_lghi (code, ins->dreg, 0);
4667 s390_jo (code, 0); CODEPTR(code, o);
4668 EMIT_COND_BRANCH (ins, S390_CC_EQ);
4673 EMIT_COND_BRANCH (ins, S390_CC_NE|S390_CC_OV);
4677 s390_jo (code, 0); CODEPTR(code, o);
4678 EMIT_COND_BRANCH (ins, S390_CC_LT);
4683 EMIT_COND_BRANCH (ins, S390_CC_LT|S390_CC_OV);
4687 s390_jo (code, 0); CODEPTR(code, o);
4688 EMIT_COND_BRANCH (ins, S390_CC_GT);
4693 EMIT_COND_BRANCH (ins, S390_CC_GT|S390_CC_OV);
4697 s390_jo (code, 0); CODEPTR(code, o);
4698 EMIT_COND_BRANCH (ins, S390_CC_GE);
4703 EMIT_COND_BRANCH (ins, S390_CC_GE|S390_CC_OV);
4707 s390_jo (code, 0); CODEPTR(code, o);
4708 EMIT_COND_BRANCH (ins, S390_CC_LE);
4713 EMIT_COND_BRANCH (ins, S390_CC_LE|S390_CC_OV);
4717 s390_lhi (code, s390_r13, 0x7f);
4718 s390_tcdb (code, ins->sreg1, 0, s390_r13, 0);
4719 s390_jz (code, 0); CODEPTR(code, o);
4720 mono_add_patch_info (cfg, code - cfg->native_code,
4721 MONO_PATCH_INFO_EXC, "ArithmeticException");
4722 s390_brasl (code, s390_r14,0);
4726 case OP_S390_MOVE: {
4727 if (ins->backend.size > 0) {
4728 if (ins->backend.size <= 256) {
4729 s390_mvc (code, ins->backend.size, ins->dreg,
4730 ins->inst_offset, ins->sreg1, ins->inst_imm);
4732 s390_lgr (code, s390_r0, ins->dreg);
4733 if (ins->inst_offset > 0) {
4734 if (s390_is_imm16 (ins->inst_offset)) {
4735 s390_aghi (code, s390_r0, ins->inst_offset);
4737 s390_basr (code, s390_r13, 0);
4739 s390_llong(code, ins->inst_offset);
4740 s390_ag (code, s390_r0, 0, s390_r13, 4);
4743 s390_lgr (code, s390_r12, ins->sreg1);
4744 if (ins->inst_imm > 0) {
4745 if (s390_is_imm16 (ins->inst_imm)) {
4746 s390_aghi (code, s390_r12, ins->inst_imm);
4748 s390_basr (code, s390_r13, 0);
4750 s390_llong(code, ins->inst_imm);
4751 s390_ag (code, s390_r12, 0, s390_r13, 4);
4754 if (s390_is_imm16 (ins->backend.size)) {
4755 s390_lghi (code, s390_r1, ins->backend.size);
4757 s390_basr (code, s390_r13, 0);
4759 s390_llong(code, ins->backend.size);
4760 s390_lg (code, s390_r1, 0, s390_r13, 4);
4762 s390_lgr (code, s390_r13, s390_r1);
4763 s390_mvcle(code, s390_r0, s390_r12, 0, 0);
4769 case OP_ATOMIC_ADD_I8: {
4770 s390_lgr (code, s390_r1, ins->sreg2);
4771 s390_lg (code, s390_r0, 0, ins->inst_basereg, ins->inst_offset);
4772 s390_agr (code, s390_r1, s390_r0);
4773 s390_csg (code, s390_r0, s390_r1, ins->inst_basereg, ins->inst_offset);
4774 s390_jnz (code, -10);
4775 s390_lgr (code, ins->dreg, s390_r1);
4778 case OP_ATOMIC_ADD_NEW_I8: {
4779 s390_lgr (code, s390_r1, ins->sreg2);
4780 s390_lg (code, s390_r0, 0, ins->inst_basereg, ins->inst_offset);
4781 s390_agr (code, s390_r1, s390_r0);
4782 s390_csg (code, s390_r0, s390_r1, ins->inst_basereg, ins->inst_offset);
4783 s390_jnz (code, -10);
4784 s390_lgr (code, ins->dreg, s390_r1);
4787 case OP_ATOMIC_EXCHANGE_I8: {
4788 s390_lg (code, s390_r0, 0, ins->inst_basereg, ins->inst_offset);
4789 s390_csg (code, s390_r0, ins->sreg2, ins->inst_basereg, ins->inst_offset);
4790 s390_jnz (code, -6);
4791 s390_lgr (code, ins->dreg, s390_r0);
4794 case OP_ATOMIC_ADD_I4: {
4795 s390_lgfr(code, s390_r1, ins->sreg2);
4796 s390_lgf (code, s390_r0, 0, ins->inst_basereg, ins->inst_offset);
4797 s390_agr (code, s390_r1, s390_r0);
4798 s390_cs (code, s390_r0, s390_r1, ins->inst_basereg, ins->inst_offset);
4799 s390_jnz (code, -9);
4800 s390_lgfr(code, ins->dreg, s390_r1);
4803 case OP_ATOMIC_ADD_NEW_I4: {
4804 s390_lgfr(code, s390_r1, ins->sreg2);
4805 s390_lgf (code, s390_r0, 0, ins->inst_basereg, ins->inst_offset);
4806 s390_agr (code, s390_r1, s390_r0);
4807 s390_cs (code, s390_r0, s390_r1, ins->inst_basereg, ins->inst_offset);
4808 s390_jnz (code, -9);
4809 s390_lgfr(code, ins->dreg, s390_r1);
4812 case OP_ATOMIC_EXCHANGE_I4: {
4813 s390_l (code, s390_r0, 0, ins->inst_basereg, ins->inst_offset);
4814 s390_cs (code, s390_r0, ins->sreg2, ins->inst_basereg, ins->inst_offset);
4815 s390_jnz (code, -4);
4816 s390_lgfr(code, ins->dreg, s390_r0);
4819 case OP_S390_BKCHAIN: {
4820 s390_lgr (code, ins->dreg, ins->sreg1);
4821 if (s390_is_imm16 (cfg->stack_offset)) {
4822 s390_aghi (code, ins->dreg, cfg->stack_offset);
4824 s390_basr (code, s390_r13, 0);
4826 s390_llong(code, cfg->stack_offset);
4827 s390_ag (code, ins->dreg, 0, s390_r13, 4);
4831 case OP_MEMORY_BARRIER: {
4834 case OP_GC_LIVENESS_DEF:
4835 case OP_GC_LIVENESS_USE:
4836 case OP_GC_PARAM_SLOT_LIVENESS_DEF:
4837 ins->backend.pc_offset = code - cfg->native_code;
4839 case OP_GC_SPILL_SLOT_LIVENESS_DEF:
4840 ins->backend.pc_offset = code - cfg->native_code;
4841 bb->spill_slot_defs = g_slist_prepend_mempool (cfg->mempool, bb->spill_slot_defs, ins);
4844 g_warning ("unknown opcode %s in %s()\n", mono_inst_name (ins->opcode), __FUNCTION__);
4845 g_assert_not_reached ();
4848 if ((cfg->opt & MONO_OPT_BRANCH) && ((code - cfg->native_code - offset) > max_len)) {
4849 g_warning ("wrong maximal instruction length of instruction %s (expected %d, got %ld)",
4850 mono_inst_name (ins->opcode), max_len, code - cfg->native_code - offset);
4851 g_assert_not_reached ();
4856 last_offset = offset;
4859 cfg->code_len = code - cfg->native_code;
4862 /*========================= End of Function ========================*/
4864 /*------------------------------------------------------------------*/
4866 /* Name - mono_arch_register_lowlevel_calls */
4868 /* Function - Register routines to help with --trace operation. */
4870 /*------------------------------------------------------------------*/
4873 mono_arch_register_lowlevel_calls (void)
4877 /*========================= End of Function ========================*/
4879 /*------------------------------------------------------------------*/
4881 /* Name - mono_arch_patch_code */
4883 /* Function - Process the patch data created during the */
4884 /* instruction build process. This resolves jumps, */
4885 /* calls, variables etc. */
4887 /*------------------------------------------------------------------*/
4890 mono_arch_patch_code (MonoMethod *method, MonoDomain *domain,
4891 guint8 *code, MonoJumpInfo *ji, MonoCodeManager *dyn_code_mp, gboolean run_cctors)
4893 MonoJumpInfo *patch_info;
4895 for (patch_info = ji; patch_info; patch_info = patch_info->next) {
4896 unsigned char *ip = patch_info->ip.i + code;
4897 gconstpointer target = NULL;
4899 target = mono_resolve_patch_target (method, domain, code,
4900 patch_info, run_cctors);
4902 switch (patch_info->type) {
4903 case MONO_PATCH_INFO_IP:
4904 case MONO_PATCH_INFO_EXC_NAME:
4905 case MONO_PATCH_INFO_LDSTR:
4906 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
4907 case MONO_PATCH_INFO_LDTOKEN:
4908 case MONO_PATCH_INFO_EXC:
4909 case MONO_PATCH_INFO_ABS:
4910 case MONO_PATCH_INFO_METHOD:
4911 case MONO_PATCH_INFO_RGCTX_FETCH:
4912 case MONO_PATCH_INFO_INTERNAL_METHOD:
4913 case MONO_PATCH_INFO_CLASS_INIT:
4914 case MONO_PATCH_INFO_JIT_ICALL_ADDR:
4915 case MONO_PATCH_INFO_GENERIC_CLASS_INIT:
4916 s390_patch_addr (ip, (guint64) target);
4918 case MONO_PATCH_INFO_SWITCH:
4919 /*----------------------------------*/
4920 /* ip points at the basr r13,0/j +4 */
4921 /* instruction the vtable value */
4922 /* follows this (i.e. ip+6) */
4923 /*----------------------------------*/
4924 *((gconstpointer *)(ip+6)) = target;
4926 case MONO_PATCH_INFO_METHODCONST:
4927 case MONO_PATCH_INFO_CLASS:
4928 case MONO_PATCH_INFO_IMAGE:
4929 case MONO_PATCH_INFO_FIELD:
4930 case MONO_PATCH_INFO_IID:
4931 target = S390_RELATIVE(target, ip);
4932 s390_patch_rel (ip, (guint64) target);
4934 case MONO_PATCH_INFO_R4:
4935 case MONO_PATCH_INFO_R8:
4936 case MONO_PATCH_INFO_METHOD_REL:
4937 g_assert_not_reached ();
4940 target = S390_RELATIVE(target, ip);
4942 s390_patch_rel (ip, (guint64) target);
4947 /*========================= End of Function ========================*/
4949 /*------------------------------------------------------------------*/
4951 /* Name - emit_load_volatile_arguments */
4953 /* Function - Emit the instructions to reload parameter regist- */
4954 /* registers for use with "tail" operations. */
4956 /* The register loading operations performed here */
4957 /* are the mirror of the store operations performed */
4958 /* in mono_arch_emit_prolog and need to be kept in */
4959 /* synchronization with it. */
4961 /*------------------------------------------------------------------*/
4964 emit_load_volatile_arguments (guint8 *code, MonoCompile *cfg)
4967 MonoMethod *method = cfg->method;
4968 MonoMethodSignature *sig = mono_method_signature(method);
4972 cinfo = get_call_info (NULL, NULL, sig);
4974 if (cinfo->struct_ret) {
4975 ArgInfo *ainfo = &cinfo->ret;
4976 inst = cfg->vret_addr;
4977 s390_lg (code, ainfo->reg, 0, inst->inst_basereg, inst->inst_offset);
4980 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
4981 ArgInfo *ainfo = cinfo->args + i;
4982 inst = cfg->args [pos];
4984 if (inst->opcode == OP_REGVAR) {
4985 if (ainfo->regtype == RegTypeGeneral)
4986 s390_lgr (code, ainfo->reg, inst->dreg);
4987 else if (ainfo->regtype == RegTypeFP) {
4988 if (inst->dreg != ainfo->reg) {
4989 if (ainfo->size == 4) {
4990 s390_ldebr (code, ainfo->reg, inst->dreg);
4992 s390_ldr (code, ainfo->reg, inst->dreg);
4996 else if (ainfo->regtype == RegTypeBase) {
4998 g_assert_not_reached ();
5000 if (ainfo->regtype == RegTypeGeneral) {
5001 if (!((ainfo->reg >= 2) && (ainfo->reg <= 6)))
5002 g_assert_not_reached();
5003 switch (ainfo->size) {
5005 s390_llgc (code, ainfo->reg, 0, inst->inst_basereg, inst->inst_offset);
5008 s390_lgh (code, ainfo->reg, 0, inst->inst_basereg, inst->inst_offset);
5011 s390_lgf (code, ainfo->reg, 0, inst->inst_basereg, inst->inst_offset);
5014 s390_lg (code, ainfo->reg, 0, inst->inst_basereg, inst->inst_offset);
5017 } else if (ainfo->regtype == RegTypeBase) {
5018 } else if (ainfo->regtype == RegTypeFP) {
5019 if (ainfo->size == 8)
5020 s390_ld (code, ainfo->reg, 0, inst->inst_basereg, inst->inst_offset);
5021 else if (ainfo->size == 4)
5022 s390_le (code, ainfo->reg, 0, inst->inst_basereg, inst->inst_offset);
5024 g_assert_not_reached ();
5025 } else if (ainfo->regtype == RegTypeStructByVal) {
5026 if (ainfo->reg != STK_BASE) {
5027 switch (ainfo->size) {
5029 s390_llgc (code, ainfo->reg, 0, inst->inst_basereg, inst->inst_offset);
5032 s390_lgh (code, ainfo->reg, 0, inst->inst_basereg, inst->inst_offset);
5035 s390_lgf (code, ainfo->reg, 0, inst->inst_basereg, inst->inst_offset);
5038 s390_lg (code, ainfo->reg, 0, inst->inst_basereg, inst->inst_offset);
5042 } else if (ainfo->regtype == RegTypeStructByAddr) {
5043 if (ainfo->reg != STK_BASE) {
5044 s390_lg (code, ainfo->reg, 0, inst->inst_basereg, inst->inst_offset);
5047 g_assert_not_reached ();
5055 /*========================= End of Function ========================*/
5057 /*------------------------------------------------------------------*/
5059 /* Name - mono_arch_emit_prolog */
5061 /* Function - Create the instruction sequence for a function */
5064 /*------------------------------------------------------------------*/
5067 mono_arch_emit_prolog (MonoCompile *cfg)
5069 MonoMethod *method = cfg->method;
5071 MonoMethodSignature *sig;
5073 long alloc_size, pos, max_offset, i, cfa_offset = 0;
5080 cfg->code_size = 512;
5082 if (mono_jit_trace_calls != NULL && mono_trace_eval (method)) {
5084 cfg->code_size += 256;
5085 } else if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
5086 cfg->code_size += 256;
5088 if (method->save_lmf)
5089 cfg->code_size += 200;
5091 cfg->native_code = code = g_malloc (cfg->code_size);
5093 mono_emit_unwind_op_def_cfa (cfg, code, STK_BASE, 0);
5094 emit_unwind_regs(cfg, code, s390_r6, s390_r14, S390_REG_SAVE_OFFSET);
5095 s390_stmg (code, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
5096 mono_emit_unwind_op_offset (cfg, code, s390_r14, S390_RET_ADDR_OFFSET);
5098 if (cfg->arch.bkchain_reg != -1)
5099 s390_lgr (code, cfg->arch.bkchain_reg, STK_BASE);
5101 if (cfg->flags & MONO_CFG_HAS_ALLOCA) {
5102 cfg->used_int_regs |= 1 << 11;
5105 alloc_size = cfg->stack_offset;
5107 cfg->stack_usage = cfa_offset = alloc_size;
5108 mono_emit_unwind_op_def_cfa_offset (cfg, code, alloc_size);
5109 s390_lgr (code, s390_r11, STK_BASE);
5110 if (s390_is_imm16 (alloc_size)) {
5111 s390_aghi (code, STK_BASE, -alloc_size);
5113 int stackSize = alloc_size;
5114 while (stackSize > 32767) {
5115 s390_aghi (code, STK_BASE, -32767);
5118 s390_aghi (code, STK_BASE, -stackSize);
5120 s390_stg (code, s390_r11, 0, STK_BASE, 0);
5122 if (cfg->frame_reg != STK_BASE)
5123 s390_lgr (code, s390_r11, STK_BASE);
5125 mono_emit_unwind_op_def_cfa_reg (cfg, code, cfg->frame_reg);
5127 /* store runtime generic context */
5128 if (cfg->rgctx_var) {
5129 g_assert (cfg->rgctx_var->opcode == OP_REGOFFSET);
5131 s390_stg (code, MONO_ARCH_RGCTX_REG, 0,
5132 cfg->rgctx_var->inst_basereg,
5133 cfg->rgctx_var->inst_offset);
5136 /* compute max_offset in order to use short forward jumps
5137 * we always do it on s390 because the immediate displacement
5138 * for jumps is too small
5141 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5143 bb->max_offset = max_offset;
5145 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
5148 MONO_BB_FOR_EACH_INS (bb, ins)
5149 max_offset += ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
5152 /* load arguments allocated to register from the stack */
5153 sig = mono_method_signature (method);
5156 cinfo = get_call_info (cfg, cfg->mempool, sig);
5158 if (cinfo->struct_ret) {
5159 ArgInfo *ainfo = &cinfo->ret;
5160 inst = cfg->vret_addr;
5161 inst->backend.size = ainfo->vtsize;
5162 s390_stg (code, ainfo->reg, 0, inst->inst_basereg, inst->inst_offset);
5165 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
5166 ArgInfo *ainfo = cinfo->args + i;
5167 inst = cfg->args [pos];
5169 if (inst->opcode == OP_VTARG_ADDR)
5170 inst = inst->inst_left;
5172 if (inst->opcode == OP_REGVAR) {
5173 if (ainfo->regtype == RegTypeGeneral)
5174 s390_lgr (code, inst->dreg, ainfo->reg);
5175 else if (ainfo->regtype == RegTypeFP) {
5176 if (inst->dreg != ainfo->reg) {
5177 if (ainfo->size == 4) {
5178 s390_ledbr (code, inst->dreg, ainfo->reg);
5180 s390_ldr (code, inst->dreg, ainfo->reg);
5184 else if (ainfo->regtype == RegTypeBase) {
5185 s390_lgr (code, s390_r13, STK_BASE);
5186 s390_aghi (code, s390_r13, alloc_size);
5187 s390_lg (code, inst->dreg, 0, s390_r13, ainfo->offset);
5189 g_assert_not_reached ();
5191 if (cfg->verbose_level > 2)
5192 g_print ("Argument %d assigned to register %s\n",
5193 pos, mono_arch_regname (inst->dreg));
5195 if (ainfo->regtype == RegTypeGeneral) {
5196 if (!((ainfo->reg >= 2) && (ainfo->reg <= 6)))
5197 g_assert_not_reached();
5198 switch (ainfo->size) {
5200 s390_stc (code, ainfo->reg, 0, inst->inst_basereg, inst->inst_offset);
5203 s390_sth (code, ainfo->reg, 0, inst->inst_basereg, inst->inst_offset);
5206 s390_st (code, ainfo->reg, 0, inst->inst_basereg, inst->inst_offset);
5209 s390_stg (code, ainfo->reg, 0, inst->inst_basereg, inst->inst_offset);
5212 } else if (ainfo->regtype == RegTypeBase) {
5213 } else if (ainfo->regtype == RegTypeFP) {
5214 if (ainfo->size == 8)
5215 s390_std (code, ainfo->reg, 0, inst->inst_basereg, inst->inst_offset);
5216 else if (ainfo->size == 4)
5217 s390_ste (code, ainfo->reg, 0, inst->inst_basereg, inst->inst_offset);
5219 g_assert_not_reached ();
5220 } else if (ainfo->regtype == RegTypeStructByVal) {
5221 int doffset = inst->inst_offset;
5223 if (ainfo->reg != STK_BASE)
5227 s390_lgr (code, s390_r13, STK_BASE);
5228 s390_aghi (code, s390_r13, alloc_size);
5231 size = (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE
5232 ? mono_class_native_size(mono_class_from_mono_type(inst->inst_vtype), NULL)
5237 if (ainfo->reg == STK_BASE)
5238 s390_ic (code, reg, 0, s390_r13, ainfo->offset+7);
5239 s390_stc (code, reg, 0, inst->inst_basereg, doffset);
5242 if (ainfo->reg == STK_BASE)
5243 s390_lh (code, reg, 0, s390_r13, ainfo->offset+6);
5244 s390_sth (code, reg, 0, inst->inst_basereg, doffset);
5247 if (ainfo->reg == STK_BASE)
5248 s390_l (code, reg, 0, s390_r13, ainfo->offset+4);
5249 s390_st (code, reg, 0, inst->inst_basereg, doffset);
5252 if (ainfo->reg == STK_BASE)
5253 s390_lg (code, reg, 0, s390_r13, ainfo->offset);
5254 s390_stg (code, reg, 0, inst->inst_basereg, doffset);
5257 } else if (ainfo->regtype == RegTypeStructByAddr) {
5258 s390_stg (code, ainfo->reg, 0, inst->inst_basereg, inst->inst_offset);
5259 } else if (ainfo->regtype == RegTypeStructByAddrOnStack) {
5261 g_assert_not_reached ();
5266 if (method->save_lmf) {
5267 /*---------------------------------------------------------------*/
5268 /* build the MonoLMF structure on the stack - see mini-s390x.h */
5269 /*---------------------------------------------------------------*/
5270 lmfOffset = alloc_size - sizeof(MonoLMF);
5272 s390_lgr (code, s390_r13, cfg->frame_reg);
5273 s390_aghi (code, s390_r13, lmfOffset);
5275 /*---------------------------------------------------------------*/
5276 /* Preserve the parameter registers while we fix up the lmf */
5277 /*---------------------------------------------------------------*/
5278 s390_stmg (code, s390_r2, s390_r6, s390_r13,
5279 G_STRUCT_OFFSET(MonoLMF, pregs[0]));
5281 /*---------------------------------------------------------------*/
5282 /* On return from this call r2 have the address of the &lmf */
5283 /*---------------------------------------------------------------*/
5284 if (lmf_addr_tls_offset == -1) {
5285 s390_basr(code, s390_r14, 0);
5287 mono_add_patch_info (cfg, code - cfg->native_code,
5288 MONO_PATCH_INFO_INTERNAL_METHOD,
5289 (gpointer)"mono_get_lmf_addr");
5290 s390_llong(code, 0);
5291 s390_lg (code, s390_r1, 0, s390_r14, 4);
5292 s390_basr (code, s390_r14, s390_r1);
5294 /*-------------------------------------------------------*/
5295 /* Get LMF by getting value from thread level storage */
5296 /*-------------------------------------------------------*/
5297 s390_ear (code, s390_r1, 0);
5298 s390_sllg(code, s390_r1, s390_r1, 0, 32);
5299 s390_ear (code, s390_r1, 1);
5300 s390_lg (code, s390_r2, 0, s390_r1, lmf_addr_tls_offset);
5303 /*---------------------------------------------------------------*/
5304 /* Set lmf.lmf_addr = jit_tls->lmf */
5305 /*---------------------------------------------------------------*/
5306 s390_stg (code, s390_r2, 0, s390_r13,
5307 G_STRUCT_OFFSET(MonoLMF, lmf_addr));
5309 /*---------------------------------------------------------------*/
5310 /* Get current lmf */
5311 /*---------------------------------------------------------------*/
5312 s390_lg (code, s390_r0, 0, s390_r2, 0);
5314 /*---------------------------------------------------------------*/
5315 /* Set our lmf as the current lmf */
5316 /*---------------------------------------------------------------*/
5317 s390_stg (code, s390_r13, 0, s390_r2, 0);
5319 /*---------------------------------------------------------------*/
5320 /* Have our lmf.previous_lmf point to the last lmf */
5321 /*---------------------------------------------------------------*/
5322 s390_stg (code, s390_r0, 0, s390_r13,
5323 G_STRUCT_OFFSET(MonoLMF, previous_lmf));
5325 /*---------------------------------------------------------------*/
5326 /* save method info */
5327 /*---------------------------------------------------------------*/
5328 s390_basr (code, s390_r1, 0);
5330 s390_llong (code, method);
5331 s390_lg (code, s390_r1, 0, s390_r1, 4);
5332 s390_stg (code, s390_r1, 0, s390_r13,
5333 G_STRUCT_OFFSET(MonoLMF, method));
5335 /*---------------------------------------------------------------*/
5336 /* save the current IP */
5337 /*---------------------------------------------------------------*/
5338 s390_stg (code, STK_BASE, 0, s390_r13, G_STRUCT_OFFSET(MonoLMF, ebp));
5339 s390_basr (code, s390_r1, 0);
5340 s390_stg (code, s390_r1, 0, s390_r13, G_STRUCT_OFFSET(MonoLMF, eip));
5342 /*---------------------------------------------------------------*/
5343 /* Save general and floating point registers */
5344 /*---------------------------------------------------------------*/
5345 s390_stmg (code, s390_r2, s390_r12, s390_r13,
5346 G_STRUCT_OFFSET(MonoLMF, gregs[2]));
5347 for (i = 0; i < 16; i++) {
5348 s390_std (code, i, 0, s390_r13,
5349 G_STRUCT_OFFSET(MonoLMF, fregs[i]));
5352 /*---------------------------------------------------------------*/
5353 /* Restore the parameter registers now that we've set up the lmf */
5354 /*---------------------------------------------------------------*/
5355 s390_lmg (code, s390_r2, s390_r6, s390_r13,
5356 G_STRUCT_OFFSET(MonoLMF, pregs[0]));
5360 code = mono_arch_instrument_prolog(cfg, enter_method, code, TRUE);
5362 cfg->code_len = code - cfg->native_code;
5363 g_assert (cfg->code_len < cfg->code_size);
5368 /*========================= End of Function ========================*/
5370 /*------------------------------------------------------------------*/
5372 /* Name - mono_arch_emit_epilog */
5374 /* Function - Emit the instructions for a function epilog. */
5376 /*------------------------------------------------------------------*/
5379 mono_arch_emit_epilog (MonoCompile *cfg)
5381 MonoMethod *method = cfg->method;
5384 int max_epilog_size = 96;
5386 if (cfg->method->save_lmf)
5387 max_epilog_size += 128;
5389 if (mono_jit_trace_calls != NULL)
5390 max_epilog_size += 128;
5391 else if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
5392 max_epilog_size += 128;
5394 while ((cfg->code_len + max_epilog_size) > (cfg->code_size - 16)) {
5395 cfg->code_size *= 2;
5396 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
5397 cfg->stat_code_reallocs++;
5400 code = cfg->native_code + cfg->code_len;
5402 if (mono_jit_trace_calls != NULL && mono_trace_eval (method)) {
5403 code = mono_arch_instrument_epilog (cfg, leave_method, code, TRUE);
5407 if (method->save_lmf)
5408 restoreLMF(code, cfg->frame_reg, cfg->stack_usage);
5410 if (cfg->flags & MONO_CFG_HAS_ALLOCA) {
5411 s390_lg (code, STK_BASE, 0, STK_BASE, 0);
5413 code = backUpStackPtr(cfg, code);
5415 s390_lmg (code, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
5416 s390_br (code, s390_r14);
5418 cfg->code_len = code - cfg->native_code;
5420 g_assert (cfg->code_len < cfg->code_size);
5424 /*========================= End of Function ========================*/
5426 /*------------------------------------------------------------------*/
5428 /* Name - mono_arch_emit_exceptions */
5430 /* Function - Emit the blocks to handle exception conditions. */
5432 /*------------------------------------------------------------------*/
5435 mono_arch_emit_exceptions (MonoCompile *cfg)
5437 MonoJumpInfo *patch_info;
5443 MonoClass *exc_classes [MAX_EXC];
5444 guint8 *exc_throw_start [MAX_EXC];
5446 for (patch_info = cfg->patch_info;
5448 patch_info = patch_info->next) {
5449 if (patch_info->type == MONO_PATCH_INFO_EXC)
5453 code_size = exc_count * 48;
5455 while ((cfg->code_len + code_size) > (cfg->code_size - 16)) {
5456 cfg->code_size *= 2;
5457 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
5458 cfg->stat_code_reallocs++;
5461 code = cfg->native_code + cfg->code_len;
5463 /*---------------------------------------------------------------------*/
5464 /* Add code to raise exceptions */
5465 /*---------------------------------------------------------------------*/
5466 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
5467 switch (patch_info->type) {
5468 case MONO_PATCH_INFO_EXC: {
5469 guint8 *ip = patch_info->ip.i + cfg->native_code;
5470 MonoClass *exc_class;
5473 /*-----------------------------------------------------*/
5474 /* Patch the branch in epilog to come here */
5475 /*-----------------------------------------------------*/
5476 s390_patch_rel (ip + 2, (guint64) S390_RELATIVE(code,ip));
5478 exc_class = mono_class_from_name (mono_defaults.corlib,
5480 patch_info->data.name);
5481 g_assert (exc_class);
5482 throw_ip = patch_info->ip.i;
5484 for (iExc = 0; iExc < nThrows; ++iExc)
5485 if (exc_classes [iExc] == exc_class)
5488 if (iExc < nThrows) {
5489 s390_jcl (code, S390_CC_UN,
5490 (guint64) exc_throw_start [iExc]);
5491 patch_info->type = MONO_PATCH_INFO_NONE;
5494 if (nThrows < MAX_EXC) {
5495 exc_classes [nThrows] = exc_class;
5496 exc_throw_start [nThrows] = code;
5499 /*---------------------------------------------*/
5500 /* Patch the parameter passed to the handler */
5501 /*---------------------------------------------*/
5502 s390_basr (code, s390_r13, 0);
5504 // s390_llong(code, patch_info->data.target);
5505 s390_llong(code, exc_class->type_token);
5506 /*---------------------------------------------*/
5507 /* Load return address & parameter register */
5508 /*---------------------------------------------*/
5509 s390_larl (code, s390_r14, (guint64)S390_RELATIVE((patch_info->ip.i +
5510 cfg->native_code + 8), code));
5511 s390_lg (code, s390_r2, 0, s390_r13, 4);
5512 /*---------------------------------------------*/
5513 /* Reuse the current patch to set the jump */
5514 /*---------------------------------------------*/
5515 s390_basr (code, s390_r13, 0);
5517 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
5518 patch_info->data.name = "mono_arch_throw_corlib_exception";
5519 patch_info->ip.i = code - cfg->native_code;
5520 s390_llong(code, 0);
5521 s390_lg (code, s390_r1, 0, s390_r13, 4);
5522 s390_br (code, s390_r1);
5532 cfg->code_len = code - cfg->native_code;
5534 g_assert (cfg->code_len < cfg->code_size);
5538 /*========================= End of Function ========================*/
5540 /*------------------------------------------------------------------*/
5542 /* Name - mono_arch_finish_init */
5544 /* Function - Setup the JIT's Thread Level Specific Data. */
5546 /*------------------------------------------------------------------*/
5549 mono_arch_finish_init (void)
5551 appdomain_tls_offset = mono_domain_get_tls_offset();
5552 lmf_tls_offset = mono_get_lmf_tls_offset();
5553 lmf_addr_tls_offset = mono_get_lmf_addr_tls_offset();
5556 /*========================= End of Function ========================*/
5558 /*------------------------------------------------------------------*/
5560 /* Name - mono_arch_free_jit_tls_data */
5562 /* Function - Free tls data. */
5564 /*------------------------------------------------------------------*/
5567 mono_arch_free_jit_tls_data (MonoJitTlsData *tls)
5571 /*========================= End of Function ========================*/
5573 /*------------------------------------------------------------------*/
5575 /* Name - mono_arch_emit_inst_for_method */
5577 /*------------------------------------------------------------------*/
5580 mono_arch_emit_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
5585 /*========================= End of Function ========================*/
5587 /*------------------------------------------------------------------*/
5589 /* Name - mono_arch_decompose_opts */
5591 /* Function - Decompose opcode into a System z opcode. */
5593 /*------------------------------------------------------------------*/
5596 mono_arch_decompose_opts (MonoCompile *cfg, MonoInst *ins)
5599 * Have to rename these to avoid being decomposed normally, since the normal
5600 * decomposition does not work on S390.
5602 switch (ins->opcode) {
5604 ins->opcode = OP_S390_ISUB_OVF;
5606 case OP_ISUB_OVF_UN:
5607 ins->opcode = OP_S390_ISUB_OVF_UN;
5610 ins->opcode = OP_S390_IADD_OVF;
5612 case OP_IADD_OVF_UN:
5613 ins->opcode = OP_S390_IADD_OVF_UN;
5616 ins->opcode = OP_S390_LADD_OVF;
5618 case OP_LADD_OVF_UN:
5619 ins->opcode = OP_S390_LADD_OVF_UN;
5622 ins->opcode = OP_S390_LSUB_OVF;
5624 case OP_LSUB_OVF_UN:
5625 ins->opcode = OP_S390_LSUB_OVF_UN;
5632 /*========================= End of Function ========================*/
5634 /*------------------------------------------------------------------*/
5636 /* Name - mono_arch_print_tree */
5638 /* Function - Print platform-specific opcode details. */
5640 /* Returns - 1 - opcode details have been printed */
5641 /* 0 - opcode details have not been printed */
5643 /*------------------------------------------------------------------*/
5646 mono_arch_print_tree (MonoInst *tree, int arity)
5650 switch (tree->opcode) {
5651 case OP_S390_LOADARG:
5652 case OP_S390_ARGREG:
5653 case OP_S390_ARGPTR:
5654 printf ("[0x%lx(%s)]", tree->inst_offset,
5655 mono_arch_regname (tree->inst_basereg));
5658 case OP_S390_STKARG:
5659 printf ("[0x%lx(previous_frame)]",
5664 printf ("[0x%lx(%d,%s),0x%lx(%s)]",
5665 tree->inst_offset, tree->backend.size,
5666 mono_arch_regname(tree->dreg),
5668 mono_arch_regname(tree->sreg1));
5671 case OP_S390_SETF4RET:
5672 printf ("[f%s,f%s]",
5673 mono_arch_regname (tree->dreg),
5674 mono_arch_regname (tree->sreg1));
5678 printf ("[0x%lx(0x%lx,%s)]", tree->inst_offset,
5680 mono_arch_regname (tree->sreg1));
5683 case OP_S390_BKCHAIN:
5684 printf ("[previous_frame(%s)]",
5685 mono_arch_regname (tree->sreg1));
5693 /*========================= End of Function ========================*/
5695 /*------------------------------------------------------------------*/
5697 /* Name - mono_arch_regalloc_cost */
5699 /* Function - Determine the cost, in the number of memory */
5700 /* references, of the action of allocating the var- */
5701 /* iable VMV into a register during global register */
5704 /* Returns - Cost */
5706 /*------------------------------------------------------------------*/
5709 mono_arch_regalloc_cost (MonoCompile *cfg, MonoMethodVar *vmv)
5715 /*========================= End of Function ========================*/
5717 /*------------------------------------------------------------------*/
5719 /* Name - mono_arch_get_domain_intrinsic */
5725 /*------------------------------------------------------------------*/
5728 mono_arch_get_domain_intrinsic (MonoCompile* cfg)
5732 if (appdomain_tls_offset == -1)
5735 MONO_INST_NEW (cfg, ins, OP_TLS_GET);
5736 ins->inst_offset = appdomain_tls_offset;
5740 /*========================= End of Function ========================*/
5742 /*------------------------------------------------------------------*/
5744 /* Name - mono_arch_flush_register_windows */
5750 /*------------------------------------------------------------------*/
5753 mono_arch_flush_register_windows (void)
5757 /*========================= End of Function ========================*/
5759 /*------------------------------------------------------------------*/
5761 /* Name - mono_arch_is_inst_imm */
5763 /* Function - Determine if operand qualifies as an immediate */
5764 /* value. For s390 this is a value -32768-32768 */
5766 /* Returns - True|False - is [not] immediate value. */
5768 /*------------------------------------------------------------------*/
5771 mono_arch_is_inst_imm (gint64 imm)
5773 return s390_is_imm16 (imm);
5776 /*========================= End of Function ========================*/
5778 /*------------------------------------------------------------------*/
5780 /* Name - mono_arch_get_patch_offset */
5782 /* Function - Dummy entry point until s390x supports aot. */
5784 /* Returns - Offset for patch. */
5786 /*------------------------------------------------------------------*/
5789 mono_arch_get_patch_offset (guint8 *code)
5794 /*========================= End of Function ========================*/
5796 /*------------------------------------------------------------------*/
5798 /* Name - mono_arch_context_get_int_reg. */
5802 /* Returns - Return a register from the context. */
5804 /*------------------------------------------------------------------*/
5807 mono_arch_context_get_int_reg (MonoContext *ctx, int reg)
5809 return ((mgreg_t) ctx->uc_mcontext.gregs[reg]);
5812 /*========================= End of Function ========================*/
5814 /*------------------------------------------------------------------*/
5816 /* Name - mono_arch_context_set_int_reg. */
5818 /* Function - Set a value in a specified register. */
5820 /*------------------------------------------------------------------*/
5823 mono_arch_context_set_int_reg (MonoContext *ctx, int reg, mgreg_t val)
5825 ctx->uc_mcontext.gregs[reg] = val;
5828 /*========================= End of Function ========================*/
5830 /*------------------------------------------------------------------*/
5832 /* Name - mono_arch_get_this_arg_from_call. */
5836 /*------------------------------------------------------------------*/
5839 mono_arch_get_this_arg_from_call (mgreg_t *regs, guint8 *code)
5841 MonoLMF *lmf = (MonoLMF *) ((gchar *) regs - sizeof(MonoLMF));
5843 return (gpointer) lmf->gregs [s390_r2];
5846 /*========================= End of Function ========================*/
5848 /*------------------------------------------------------------------*/
5850 /* Name - get_delegate_invoke_impl. */
5854 /*------------------------------------------------------------------*/
5857 get_delegate_invoke_impl (gboolean has_target, guint32 param_count, guint32 *code_len, gboolean aot)
5859 guint8 *code, *start;
5864 start = code = mono_global_codeman_reserve (size);
5866 /* Replace the this argument with the target */
5867 s390_lg (code, s390_r1, 0, s390_r2, G_STRUCT_OFFSET(MonoDelegate, method_ptr));
5868 s390_lg (code, s390_r2, 0, s390_r2, G_STRUCT_OFFSET(MonoDelegate, target));
5869 s390_br (code, s390_r1);
5870 g_assert ((code - start) <= size);
5872 mono_arch_flush_icache (start, size);
5876 size = 32 + param_count * 8;
5877 start = code = mono_global_codeman_reserve (size);
5879 s390_lg (code, s390_r1, 0, s390_r2, G_STRUCT_OFFSET(MonoDelegate, method_ptr));
5880 /* slide down the arguments */
5881 for (i = 0; i < param_count; ++i) {
5882 s390_lgr (code, (s390_r2 + i), (s390_r2 + i + 1));
5884 s390_br (code, s390_r1);
5886 g_assert ((code - start) <= size);
5888 mono_arch_flush_icache (start, size);
5892 *code_len = code - start;
5897 /*========================= End of Function ========================*/
5899 /*------------------------------------------------------------------*/
5901 /* Name - mono_arch_get_delegate_invoke_impls. */
5905 /*------------------------------------------------------------------*/
5908 mono_arch_get_delegate_invoke_impls (void)
5916 code = get_delegate_invoke_impl (TRUE, 0, &code_len, TRUE);
5917 res = g_slist_prepend (res, mono_tramp_info_create ("delegate_invoke_impl_has_target", code, code_len, NULL, NULL));
5919 for (i = 0; i < MAX_ARCH_DELEGATE_PARAMS; ++i) {
5920 code = get_delegate_invoke_impl (FALSE, i, &code_len, TRUE);
5921 tramp_name = g_strdup_printf ("delegate_invoke_impl_target_%d", i);
5922 res = g_slist_prepend (res, mono_tramp_info_create (tramp_name, code, code_len, NULL, NULL));
5923 g_free (tramp_name);
5929 /*========================= End of Function ========================*/
5931 /*------------------------------------------------------------------*/
5933 /* Name - mono_arch_get_delegate_invoke_impl. */
5937 /*------------------------------------------------------------------*/
5940 mono_arch_get_delegate_invoke_impl (MonoMethodSignature *sig, gboolean has_target)
5942 guint8 *code, *start;
5944 /* FIXME: Support more cases */
5945 if (MONO_TYPE_ISSTRUCT (sig->ret))
5949 static guint8* cached = NULL;
5955 start = mono_aot_get_trampoline ("delegate_invoke_impl_has_target");
5957 start = get_delegate_invoke_impl (TRUE, 0, NULL, FALSE);
5959 mono_memory_barrier ();
5963 static guint8* cache [MAX_ARCH_DELEGATE_PARAMS + 1] = {NULL};
5966 if (sig->param_count > MAX_ARCH_DELEGATE_PARAMS)
5968 for (i = 0; i < sig->param_count; ++i)
5969 if (!mono_is_regsize_var (sig->params [i]))
5973 code = cache [sig->param_count];
5977 if (mono_aot_only) {
5978 char *name = g_strdup_printf ("delegate_invoke_impl_target_%d", sig->param_count);
5979 start = mono_aot_get_trampoline (name);
5982 start = get_delegate_invoke_impl (FALSE, sig->param_count, NULL, FALSE);
5985 mono_memory_barrier ();
5987 cache [sig->param_count] = start;
5992 /*========================= End of Function ========================*/
5994 /*------------------------------------------------------------------*/
5996 /* Name - mono_arch_build_imt_thunk. */
6000 /*------------------------------------------------------------------*/
6003 mono_arch_build_imt_thunk (MonoVTable *vtable, MonoDomain *domain,
6004 MonoIMTCheckItem **imt_entries, int count,
6005 gpointer fail_tramp)
6009 guchar *code, *start;
6011 for (i = 0; i < count; ++i) {
6012 MonoIMTCheckItem *item = imt_entries [i];
6013 if (item->is_equals) {
6014 if (item->check_target_idx) {
6015 if (!item->compare_done)
6016 item->chunk_size += CMP_SIZE + JUMP_SIZE;
6017 if (item->has_target_code)
6018 item->chunk_size += BR_SIZE + JUMP_SIZE + LOADCON_SIZE;
6020 item->chunk_size += BR_SIZE + JUMP_SIZE + LOADCON_SIZE +
6024 item->chunk_size += CMP_SIZE + 2 * BR_SIZE + JUMP_SIZE +
6026 if (!item->has_target_code)
6027 item->chunk_size += LOAD_SIZE;
6029 item->chunk_size += LOADCON_SIZE + LOAD_SIZE + BR_SIZE;
6030 #if ENABLE_WRONG_METHOD_CHECK
6031 item->chunk_size += CMP_SIZE + JUMP_SIZE;
6036 item->chunk_size += CMP_SIZE + JUMP_SIZE;
6037 imt_entries [item->check_target_idx]->compare_done = TRUE;
6039 size += item->chunk_size;
6043 code = mono_method_alloc_generic_virtual_thunk (domain, size);
6045 code = mono_domain_code_reserve (domain, size);
6049 for (i = 0; i < count; ++i) {
6050 MonoIMTCheckItem *item = imt_entries [i];
6051 item->code_target = (guint8 *) code;
6052 if (item->is_equals) {
6053 if (item->check_target_idx) {
6054 if (!item->compare_done) {
6055 s390_basr (code, s390_r13, s390_r0);
6057 s390_llong(code, item->key);
6058 s390_lg (code, s390_r0, 0, s390_r13, 4);
6059 s390_cgr (code, s390_r0, MONO_ARCH_IMT_REG);
6061 item->jmp_code = (guint8*) code;
6062 s390_jcl (code, S390_CC_NE, 0);
6064 s390_basr (code, s390_r13, s390_r0);
6066 if (item->has_target_code) {
6067 s390_llong(code, item->value.target_code);
6068 s390_lg (code, s390_r1, 0, s390_r13, 4);
6070 s390_llong(code, (&(vtable->vtable [item->value.vtable_slot])));
6071 s390_lg (code, s390_r1, 0, s390_r13, 4);
6072 s390_lg (code, s390_r1, 0, s390_r1, 0);
6074 s390_br (code, s390_r1);
6079 s390_basr (code, s390_r13, s390_r0);
6081 s390_llong(code, item->key);
6082 s390_lg (code, s390_r0, 0, s390_r13, 4);
6083 s390_cgr (code, s390_r0, MONO_ARCH_IMT_REG);
6084 item->jmp_code = (guint8*) code;
6085 s390_jcl (code, S390_CC_NE, 0);
6086 s390_basr (code, s390_r13, s390_r0);
6088 if (item->has_target_code) {
6089 s390_llong(code, item->value.target_code);
6090 s390_lg (code, s390_r1, 0, s390_r13, 4);
6093 s390_llong(code, (&(vtable->vtable [item->value.vtable_slot])));
6094 s390_lg (code, s390_r1, 0, s390_r13, 4);
6095 s390_lg (code, s390_r1, 0, s390_r1, 0);
6097 s390_br (code, s390_r1);
6098 target = S390_RELATIVE(code, item->jmp_code);
6099 s390_patch_rel(item->jmp_code+2, target);
6100 s390_basr (code, s390_r13, s390_r0);
6102 s390_llong(code, fail_tramp);
6103 s390_lg (code, s390_r1, 0, s390_r13, 4);
6104 s390_br (code, s390_r1);
6105 item->jmp_code = NULL;
6107 /* enable the commented code to assert on wrong method */
6108 #if ENABLE_WRONG_METHOD_CHECK
6109 g_assert_not_reached ();
6111 s390_basr (code, s390_r13, s390_r0);
6113 s390_llong(code, (&(vtable->vtable [item->value.vtable_slot])));
6114 s390_lg (code, s390_r1, 0, s390_r13, 4);
6115 s390_lg (code, s390_r1, 0, s390_r1, 0);
6116 s390_br (code, s390_r1);
6117 #if ENABLE_WRONG_METHOD_CHECK
6118 g_assert_not_reached ();
6123 s390_basr (code, s390_r13, s390_r0);
6125 s390_llong(code, item->key);
6126 s390_lg (code, s390_r0, 0, s390_r13, 4);
6127 s390_cgr (code, MONO_ARCH_IMT_REG, s390_r0);
6128 item->jmp_code = (guint8 *) code;
6129 s390_jcl (code, S390_CC_GE, 0);
6132 /* patch the branches to get to the target items */
6133 for (i = 0; i < count; ++i) {
6134 MonoIMTCheckItem *item = imt_entries [i];
6135 if (item->jmp_code) {
6136 if (item->check_target_idx) {
6138 offset = S390_RELATIVE(imt_entries [item->check_target_idx]->code_target,
6140 s390_patch_rel ((guchar *) item->jmp_code + 2, (guint64) offset);
6145 mono_arch_flush_icache ((guint8*)start, (code - start));
6148 mono_stats.imt_thunks_size += (code - start);
6150 g_assert (code - start <= size);
6155 /*========================= End of Function ========================*/
6157 /*------------------------------------------------------------------*/
6159 /* Name - mono_arch_find_imt_method. */
6161 /* Function - Get the method address from MONO_ARCH_IMT_REG */
6162 /* found in the save area. */
6164 /*------------------------------------------------------------------*/
6167 mono_arch_find_imt_method (mgreg_t *regs, guint8 *code)
6169 MonoLMF *lmf = (MonoLMF *) ((gchar *) regs - sizeof(MonoLMF));
6171 return ((MonoMethod *) lmf->gregs [MONO_ARCH_IMT_REG]);
6174 /*========================= End of Function ========================*/
6176 /*------------------------------------------------------------------*/
6178 /* Name - mono_arch_find_static_call_vtable */
6180 /* Function - Find the static call vtable. */
6182 /*------------------------------------------------------------------*/
6185 mono_arch_find_static_call_vtable (mgreg_t *regs, guint8 *code)
6187 mgreg_t *r = (mgreg_t*)regs;
6189 return (MonoVTable*)(gsize) r [MONO_ARCH_RGCTX_REG];
6192 /*========================= End of Function ========================*/
6194 /*------------------------------------------------------------------*/
6196 /* Name - mono_arch_get_cie_program */
6198 /* Function - Find the static call vtable. */
6200 /*------------------------------------------------------------------*/
6203 mono_arch_get_cie_program (void)
6207 mono_add_unwind_op_def_cfa (l, NULL, NULL, STK_BASE, 0);
6212 /*========================= End of Function ========================*/
6214 #ifdef MONO_ARCH_SOFT_DEBUG_SUPPORTED
6216 /*------------------------------------------------------------------*/
6218 /* Name - mono_arch_set_breakpoint. */
6220 /* Function - Set a breakpoint at the native code corresponding */
6221 /* to JI at NATIVE_OFFSET. The location should */
6222 /* contain code emitted by OP_SEQ_POINT. */
6224 /*------------------------------------------------------------------*/
6227 mono_arch_set_breakpoint (MonoJitInfo *ji, guint8 *ip)
6231 breakpointCode.pTrigger = bp_trigger_page;
6232 memcpy(code, (void *) &breakpointCode, BREAKPOINT_SIZE);
6233 code += BREAKPOINT_SIZE;
6236 /*========================= End of Function ========================*/
6238 /*------------------------------------------------------------------*/
6240 /* Name - mono_arch_clear_breakpoint. */
6242 /* Function - Clear the breakpoint at IP. */
6244 /*------------------------------------------------------------------*/
6247 mono_arch_clear_breakpoint (MonoJitInfo *ji, guint8 *ip)
6252 for (i = 0; i < (BREAKPOINT_SIZE / S390X_NOP_SIZE); i++)
6256 /*========================= End of Function ========================*/
6258 /*------------------------------------------------------------------*/
6260 /* Name - mono_arch_is_breakpoint_event. */
6264 /*------------------------------------------------------------------*/
6267 mono_arch_is_breakpoint_event (void *info, void *sigctx)
6269 siginfo_t* sinfo = (siginfo_t*) info;
6270 /* Sometimes the address is off by 4 */
6271 if (sinfo->si_addr >= bp_trigger_page && (guint8*)sinfo->si_addr <= (guint8*)bp_trigger_page + 128)
6277 /*========================= End of Function ========================*/
6279 /*------------------------------------------------------------------*/
6281 /* Name - mono_arch_skip_breakpoint. */
6283 /* Function - Modify the CTX so the IP is placed after the */
6284 /* breakpoint instruction, so when we resume, the */
6285 /* instruction is not executed again. */
6287 /*------------------------------------------------------------------*/
6290 mono_arch_skip_breakpoint (MonoContext *ctx, MonoJitInfo *ji)
6292 MONO_CONTEXT_SET_IP (ctx, (guint8*)MONO_CONTEXT_GET_IP (ctx) + BREAKPOINT_SIZE);
6295 /*========================= End of Function ========================*/
6297 /*------------------------------------------------------------------*/
6299 /* Name - mono_arch_start_single_stepping. */
6301 /* Function - Start single stepping. */
6303 /*------------------------------------------------------------------*/
6306 mono_arch_start_single_stepping (void)
6308 mono_mprotect (ss_trigger_page, mono_pagesize (), 0);
6311 /*========================= End of Function ========================*/
6313 /*------------------------------------------------------------------*/
6315 /* Name - mono_arch_stop_single_stepping. */
6317 /* Function - Stop single stepping. */
6319 /*------------------------------------------------------------------*/
6322 mono_arch_stop_single_stepping (void)
6324 mono_mprotect (ss_trigger_page, mono_pagesize (), MONO_MMAP_READ);
6327 /*========================= End of Function ========================*/
6329 /*------------------------------------------------------------------*/
6331 /* Name - mono_arch_is_single_step_event. */
6333 /* Function - Return whether the machine state in sigctx cor- */
6334 /* responds to a single step event. */
6336 /*------------------------------------------------------------------*/
6339 mono_arch_is_single_step_event (void *info, void *sigctx)
6341 siginfo_t* sinfo = (siginfo_t*) info;
6343 /* Sometimes the address is off by 4 */
6344 if (sinfo->si_addr >= ss_trigger_page && (guint8*)sinfo->si_addr <= (guint8*)ss_trigger_page + 128)
6350 /*========================= End of Function ========================*/
6352 /*------------------------------------------------------------------*/
6354 /* Name - mono_arch_skip_single_step. */
6356 /* Function - Modify the ctx so the IP is placed after the */
6357 /* single step trigger instruction, so that the */
6358 /* instruction is not executed again. */
6360 /*------------------------------------------------------------------*/
6363 mono_arch_skip_single_step (MonoContext *ctx)
6365 MONO_CONTEXT_SET_IP (ctx, (guint8*)MONO_CONTEXT_GET_IP (ctx) + BREAKPOINT_SIZE);
6368 /*========================= End of Function ========================*/
6370 /*------------------------------------------------------------------*/
6372 /* Name - mono_arch_create_seq_point_info. */
6374 /* Function - Return a pointer to a data struction which is */
6375 /* used by the sequence point implementation in */
6378 /*------------------------------------------------------------------*/
6381 mono_arch_get_seq_point_info (MonoDomain *domain, guint8 *code)
6387 /*========================= End of Function ========================*/