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 /*========================= End of Defines =========================*/
251 /*------------------------------------------------------------------*/
252 /* I n c l u d e s */
253 /*------------------------------------------------------------------*/
258 #include <mono/metadata/appdomain.h>
259 #include <mono/metadata/debug-helpers.h>
260 #include <mono/metadata/profiler-private.h>
261 #include <mono/utils/mono-math.h>
262 #include <mono/utils/mono-mmap.h>
264 #include "mini-s390x.h"
265 #include "cpu-s390x.h"
266 #include "jit-icalls.h"
269 /*========================= End of Includes ========================*/
271 /*------------------------------------------------------------------*/
272 /* T y p e d e f s */
273 /*------------------------------------------------------------------*/
285 /*------------------------------------------------------------------*/
286 /* Used by the instrument_emit_epilog */
287 /*------------------------------------------------------------------*/
298 typedef struct InstList InstList;
312 RegTypeStructByValInFP,
314 RegTypeStructByAddrOnStack
318 gint32 offset; /* offset from caller's stack */
319 gint32 offparm; /* offset from callee's stack */
320 guint16 vtsize; /* in param area */
323 guint32 size; /* Size of structure used by RegTypeStructByVal */
324 gint32 type; /* Data type of argument */
340 gint64 gr[5]; /* R2-R6 */
341 gdouble fp[3]; /* F0-F2 */
342 } __attribute__ ((packed)) RegParm;
350 } __attribute__ ((packed)) breakpoint_t;
352 /*========================= End of Typedefs ========================*/
354 /*------------------------------------------------------------------*/
355 /* P r o t o t y p e s */
356 /*------------------------------------------------------------------*/
358 static void indent (int);
359 static guint8 * backUpStackPtr(MonoCompile *, guint8 *);
360 static void decodeParm (MonoType *, void *, int);
361 static void enter_method (MonoMethod *, RegParm *, char *);
362 static void leave_method (MonoMethod *, ...);
363 static gboolean is_regsize_var (MonoType *);
364 static inline void add_general (guint *, size_data *, ArgInfo *);
365 static inline void add_stackParm (guint *, size_data *, ArgInfo *, gint);
366 static inline void add_float (guint *, size_data *, ArgInfo *);
367 static CallInfo * get_call_info (MonoCompile *, MonoMemPool *, MonoMethodSignature *, gboolean);
368 static guchar * emit_float_to_int (MonoCompile *, guchar *, int, int, int, gboolean);
369 gpointer mono_arch_get_lmf_addr (void);
370 static guint8 * emit_load_volatile_arguments (guint8 *, MonoCompile *);
371 static void catch_SIGILL(int, siginfo_t *, void *);
372 static __inline__ void emit_unwind_regs(MonoCompile *, guint8 *, int, int, long);
374 /*========================= End of Prototypes ======================*/
376 /*------------------------------------------------------------------*/
377 /* G l o b a l V a r i a b l e s */
378 /*------------------------------------------------------------------*/
380 int mono_exc_esp_offset = 0;
382 static int indent_level = 0;
386 static gboolean tls_offset_inited = FALSE;
388 static int appdomain_tls_offset = -1,
389 thread_tls_offset = -1;
391 pthread_key_t lmf_addr_key;
393 gboolean lmf_addr_key_inited = FALSE;
399 extern __thread MonoDomain *tls_appdomain;
400 extern __thread MonoThread *tls_current_object;
401 extern __thread gpointer mono_lmf_addr;
406 * The code generated for sequence points reads from this location,
407 * which is made read-only when single stepping is enabled.
409 static gpointer ss_trigger_page;
412 * Enabled breakpoints read from this trigger page
414 static gpointer bp_trigger_page;
416 breakpoint_t breakpointCode;
418 /*====================== End of Global Variables ===================*/
420 /*------------------------------------------------------------------*/
422 /* Name - mono_arch_regname */
424 /* Function - Returns the name of the register specified by */
425 /* the input parameter. */
427 /*------------------------------------------------------------------*/
430 mono_arch_regname (int reg) {
431 static const char * rnames[] = {
432 "s390_r0", "s390_sp", "s390_r2", "s390_r3", "s390_r4",
433 "s390_r5", "s390_r6", "s390_r7", "s390_r8", "s390_r9",
434 "s390_r10", "s390_r11", "s390_r12", "s390_r13", "s390_r14",
438 if (reg >= 0 && reg < 16)
444 /*========================= End of Function ========================*/
446 /*------------------------------------------------------------------*/
448 /* Name - mono_arch_fregname */
450 /* Function - Returns the name of the register specified by */
451 /* the input parameter. */
453 /*------------------------------------------------------------------*/
456 mono_arch_fregname (int reg) {
457 static const char * rnames[] = {
458 "s390_f0", "s390_f1", "s390_f2", "s390_f3", "s390_f4",
459 "s390_f5", "s390_f6", "s390_f7", "s390_f8", "s390_f9",
460 "s390_f10", "s390_f11", "s390_f12", "s390_f13", "s390_f14",
464 if (reg >= 0 && reg < 16)
470 /*========================= End of Function ========================*/
472 /*------------------------------------------------------------------*/
474 /* Name - arch_get_argument_info */
476 /* Function - Gathers information on parameters such as size, */
477 /* alignment, and padding. arg_info should be large */
478 /* enough to hold param_count + 1 entries. */
480 /* Parameters - @csig - Method signature */
481 /* @param_count - No. of parameters to consider */
482 /* @arg_info - An array to store the result info */
484 /* Returns - Size of the activation frame */
486 /*------------------------------------------------------------------*/
489 mono_arch_get_argument_info (MonoMethodSignature *csig,
491 MonoJitArgumentInfo *arg_info)
493 int k, frame_size = 0;
494 int size, align, pad;
497 if (MONO_TYPE_ISSTRUCT (csig->ret)) {
498 frame_size += sizeof (gpointer);
502 arg_info [0].offset = offset;
505 frame_size += sizeof (gpointer);
509 arg_info [0].size = frame_size;
511 for (k = 0; k < param_count; k++) {
514 size = mono_type_native_stack_size (csig->params [k], (guint32 *) &align);
516 size = mini_type_stack_size (NULL, csig->params [k], &align);
518 frame_size += pad = (align - (frame_size & (align - 1))) & (align - 1);
519 arg_info [k].pad = pad;
521 arg_info [k + 1].pad = 0;
522 arg_info [k + 1].size = size;
524 arg_info [k + 1].offset = offset;
528 align = MONO_ARCH_FRAME_ALIGNMENT;
529 frame_size += pad = (align - (frame_size & (align - 1))) & (align - 1);
530 arg_info [k].pad = pad;
535 /*========================= End of Function ========================*/
537 /*------------------------------------------------------------------*/
539 /* Name - emit_unwind_regs. */
541 /* Function - Determines if a value can be returned in one or */
544 /*------------------------------------------------------------------*/
546 static void __inline__
547 emit_unwind_regs(MonoCompile *cfg, guint8 *code, int start, int end, long offset)
551 for (i = start; i < end; i++) {
552 mono_emit_unwind_op_offset (cfg, code, i, offset);
553 offset += sizeof(gulong);
557 /*========================= End of Function ========================*/
559 /*------------------------------------------------------------------*/
561 /* Name - retFitsInReg. */
563 /* Function - Determines if a value can be returned in one or */
566 /*------------------------------------------------------------------*/
568 static inline gboolean
569 retFitsInReg(guint32 size)
584 /*========================= End of Function ========================*/
586 /*------------------------------------------------------------------*/
588 /* Name - backStackPtr. */
590 /* Function - Restore Stack Pointer to previous frame. */
592 /*------------------------------------------------------------------*/
594 static inline guint8 *
595 backUpStackPtr(MonoCompile *cfg, guint8 *code)
597 int stackSize = cfg->stack_usage;
599 if (cfg->frame_reg != STK_BASE)
600 s390_lgr (code, STK_BASE, cfg->frame_reg);
602 if (s390_is_imm16 (stackSize)) {
603 s390_aghi (code, STK_BASE, stackSize);
605 while (stackSize > 32767) {
606 s390_aghi (code, STK_BASE, 32767);
609 s390_aghi (code, STK_BASE, stackSize);
614 /*========================= End of Function ========================*/
616 /*------------------------------------------------------------------*/
620 /* Function - Perform nice indenting to current level */
622 /*------------------------------------------------------------------*/
628 indent_level += diff;
635 indent_level += diff;
638 /*========================= End of Function ========================*/
640 /*------------------------------------------------------------------*/
642 /* Name - decodeParm */
644 /* Function - Decode a parameter for the trace. */
646 /*------------------------------------------------------------------*/
649 decodeParm(MonoType *type, void *curParm, int size)
654 printf("[BYREF:%p], ", *((char **) curParm));
656 simpleType = mono_type_get_underlying_type(type)->type;
658 switch (simpleType) {
660 printf ("[INTPTR:%p], ", *((int **) curParm));
663 printf ("[UINTPTR:%p], ", *((int **) curParm));
665 case MONO_TYPE_BOOLEAN :
666 printf ("[BOOL:%ld], ", *((gint64 *) curParm));
668 case MONO_TYPE_CHAR :
669 printf ("[CHAR:%c], ", *((int *) curParm));
672 printf ("[INT1:%ld], ", *((gint64 *) curParm));
675 printf ("[INT2:%ld], ", *((gint64 *) curParm));
678 printf ("[INT4:%ld], ", *((gint64 *) curParm));
681 printf ("[UINT1:%lu], ", *((guint64 *) curParm));
684 printf ("[UINT2:%lu], ", *((guint64 *) curParm));
687 printf ("[UINT4:%lu], ", *((guint64 *) curParm));
690 printf ("[UINT8:%lu], ", *((guint64 *) curParm));
692 case MONO_TYPE_STRING : {
693 MonoString *s = *((MonoString **) curParm);
695 g_assert (((MonoObject *) s)->vtable->klass == mono_defaults.string_class);
696 printf("[STRING:%p:%s], ", s, mono_string_to_utf8(s));
698 printf("[STRING:null], ");
702 case MONO_TYPE_CLASS :
703 case MONO_TYPE_OBJECT : {
704 MonoObject *obj = *((MonoObject **) curParm);
706 if ((obj) && (obj->vtable)) {
707 printf("[CLASS/OBJ:");
708 class = obj->vtable->klass;
709 printf("%p [%p] ",obj,curParm);
710 if (class == mono_defaults.string_class) {
711 printf("[STRING:%p:%s]",
712 obj, mono_string_to_utf8 ((MonoString *) obj));
713 } else if (class == mono_defaults.int32_class) {
714 printf("[INT32:%p:%d]",
715 obj, *(gint32 *)((char *)obj + sizeof (MonoObject)));
718 class->name_space, class->name, obj);
721 printf("[OBJECT:null], ");
726 printf("[PTR:%p], ", *((gpointer **) (curParm)));
728 case MONO_TYPE_FNPTR :
729 printf("[FNPTR:%p], ", *((gpointer **) (curParm)));
731 case MONO_TYPE_ARRAY :
732 printf("[ARRAY:%p], ", *((gpointer **) (curParm)));
734 case MONO_TYPE_SZARRAY :
735 printf("[SZARRAY:%p], ", *((gpointer **) (curParm)));
738 printf("[INT8:%ld], ", *((gint64 *) (curParm)));
741 printf("[FLOAT4:%g], ", *((float *) (curParm)));
744 printf("[FLOAT8:%g], ", *((double *) (curParm)));
746 case MONO_TYPE_VALUETYPE : {
748 MonoMarshalType *info;
750 if (type->data.klass->enumtype) {
751 simpleType = mono_class_enum_basetype (type->data.klass)->type;
752 printf("{VALUETYPE} - ");
756 info = mono_marshal_load_type_info (type->data.klass);
758 if ((info->native_size == sizeof(float)) &&
759 (info->num_fields == 1) &&
760 (info->fields[0].field->type->type == MONO_TYPE_R4)) {
761 printf("[FLOAT4:%f], ", *((float *) (curParm)));
765 if ((info->native_size == sizeof(double)) &&
766 (info->num_fields == 1) &&
767 (info->fields[0].field->type->type == MONO_TYPE_R8)) {
768 printf("[FLOAT8:%g], ", *((double *) (curParm)));
772 printf("[VALUETYPE:");
773 for (i = 0; i < size; i++)
774 printf("%02x,", *((guint8 *)curParm+i));
778 case MONO_TYPE_TYPEDBYREF: {
780 printf("[TYPEDBYREF:");
781 for (i = 0; i < size; i++)
782 printf("%02x,", *((guint8 *)curParm+i));
787 printf("[?? - %d], ",simpleType);
792 /*========================= End of Function ========================*/
794 /*------------------------------------------------------------------*/
796 /* Name - enter_method */
798 /* Function - Perform tracing of the entry to the current */
801 /*------------------------------------------------------------------*/
804 enter_method (MonoMethod *method, RegParm *rParm, char *sp)
806 int i, oParm = 0, iParm = 0;
809 MonoMethodSignature *sig;
816 fname = mono_method_full_name (method, TRUE);
818 printf ("ENTER: %s ", fname);
821 ip = (*(guint64 *) (sp+S390_RET_ADDR_OFFSET));
822 printf ("ip: %p sp: %p - ", (gpointer) ip, sp);
827 sig = mono_method_signature (method);
829 cinfo = get_call_info (NULL, NULL, sig, sig->pinvoke);
831 if (cinfo->struct_ret) {
832 printf ("[STRUCTRET:%p], ", (gpointer) rParm->gr[0]);
837 gpointer *this = (gpointer *) rParm->gr[iParm];
838 obj = (MonoObject *) this;
839 if (method->klass->valuetype) {
841 printf("this:[value:%p:%016lx], ",
842 this, *((guint64 *)(this+sizeof(MonoObject))));
844 printf ("this:[NULL], ");
847 class = obj->vtable->klass;
848 if (class == mono_defaults.string_class) {
849 printf ("this:[STRING:%p:%s], ",
850 obj, mono_string_to_utf8 ((MonoString *)obj));
852 printf ("this:%p[%s.%s], ",
853 obj, class->name_space, class->name);
856 printf ("this:NULL, ");
861 for (i = 0; i < sig->param_count; ++i) {
862 ainfo = &cinfo->args[i + oParm];
863 switch (ainfo->regtype) {
864 case RegTypeGeneral :
865 decodeParm(sig->params[i], &(rParm->gr[ainfo->reg-2]), ainfo->size);
868 decodeParm(sig->params[i], &(rParm->fp[ainfo->reg]), ainfo->size);
871 decodeParm(sig->params[i], sp+ainfo->offset, ainfo->size);
873 case RegTypeStructByVal :
874 if (ainfo->reg != STK_BASE) {
875 int offset = sizeof(glong) - ainfo->size;
876 curParm = &(rParm->gr[ainfo->reg-2])+offset;
879 curParm = sp+ainfo->offset;
881 if (retFitsInReg (ainfo->vtsize))
882 decodeParm(sig->params[i],
886 decodeParm(sig->params[i],
887 *((char **) curParm),
890 case RegTypeStructByAddr :
891 if (ainfo->reg != STK_BASE)
892 curParm = &(rParm->gr[ainfo->reg-2]);
894 curParm = sp+ainfo->offset;
896 decodeParm(sig->params[i],
897 *((char **) curParm),
909 /*========================= End of Function ========================*/
911 /*------------------------------------------------------------------*/
913 /* Name - leave_method */
917 /*------------------------------------------------------------------*/
920 leave_method (MonoMethod *method, ...)
927 va_start(ap, method);
929 fname = mono_method_full_name (method, TRUE);
931 printf ("LEAVE: %s", fname);
934 type = mono_method_signature (method)->ret;
937 switch (type->type) {
940 case MONO_TYPE_BOOLEAN: {
941 int val = va_arg (ap, int);
943 printf ("[TRUE:%d]", val);
949 case MONO_TYPE_CHAR: {
950 int val = va_arg (ap, int);
951 printf ("[CHAR:%d]", val);
955 int val = va_arg (ap, int);
956 printf ("[INT1:%d]", val);
960 int val = va_arg (ap, int);
961 printf ("[UINT1:%d]", val);
965 int val = va_arg (ap, int);
966 printf ("[INT2:%d]", val);
970 int val = va_arg (ap, int);
971 printf ("[UINT2:%d]", val);
975 int val = va_arg (ap, int);
976 printf ("[INT4:%d]", val);
980 int val = va_arg (ap, int);
981 printf ("[UINT4:%d]", val);
985 gint64 val = va_arg (ap, gint64);
986 printf ("[INT:%ld]", val);
991 gint64 val = va_arg (ap, gint64);
992 printf ("[UINT:%lu]", val);
996 case MONO_TYPE_STRING: {
997 MonoString *s = va_arg (ap, MonoString *);
1000 g_assert (((MonoObject *)s)->vtable->klass == mono_defaults.string_class);
1001 printf ("[STRING:%p:%s]", s, mono_string_to_utf8 (s));
1003 printf ("[STRING:null], ");
1006 case MONO_TYPE_CLASS:
1007 case MONO_TYPE_OBJECT: {
1008 MonoObject *o = va_arg (ap, MonoObject *);
1010 if ((o) && (o->vtable)) {
1011 if (o->vtable->klass == mono_defaults.boolean_class) {
1012 printf ("[BOOLEAN:%p:%d]", o, *((guint8 *)o + sizeof (MonoObject)));
1013 } else if (o->vtable->klass == mono_defaults.int32_class) {
1014 printf ("[INT32:%p:%d]", o, *((gint32 *)((char *)o + sizeof (MonoObject))));
1015 } else if (o->vtable->klass == mono_defaults.int64_class) {
1016 printf ("[INT64:%p:%ld]", o, *((gint64 *)((char *)o + sizeof (MonoObject))));
1018 printf ("[%s.%s:%p]", o->vtable->klass->name_space, o->vtable->klass->name, o);
1020 printf ("[OBJECT:%p]", o);
1025 case MONO_TYPE_FNPTR:
1026 case MONO_TYPE_ARRAY:
1027 case MONO_TYPE_SZARRAY: {
1028 gpointer p = va_arg (ap, gpointer);
1029 printf ("[result=%p]", p);
1032 case MONO_TYPE_I8: {
1033 gint64 l = va_arg (ap, gint64);
1034 printf ("[LONG:%ld]", l);
1037 case MONO_TYPE_U8: {
1038 guint64 l = va_arg (ap, guint64);
1039 printf ("[ULONG:%lu]", l);
1042 case MONO_TYPE_R4: {
1043 double f = va_arg (ap, double);
1044 printf ("[FLOAT4:%g]\n", f);
1047 case MONO_TYPE_R8: {
1048 double f = va_arg (ap, double);
1049 printf ("[FLOAT8:%g]\n", f);
1052 case MONO_TYPE_VALUETYPE: {
1053 MonoMarshalType *info;
1054 if (type->data.klass->enumtype) {
1055 type = mono_class_enum_basetype (type->data.klass);
1060 info = mono_marshal_load_type_info (type->data.klass);
1062 if ((info->native_size == sizeof(float)) &&
1063 (info->num_fields == 1) &&
1064 (info->fields[0].field->type->type == MONO_TYPE_R4)) {
1065 double f = va_arg (ap, double);
1066 printf("[FLOAT4:%g]\n", (double) f);
1070 if ((info->native_size == sizeof(double)) &&
1071 (info->num_fields == 1) &&
1072 (info->fields[0].field->type->type == MONO_TYPE_R8)) {
1073 double f = va_arg (ap, double);
1074 printf("[FLOAT8:%g]\n", f);
1078 size = mono_type_size (type, &align);
1081 guint32 p = va_arg (ap, guint32);
1082 printf ("[%02x]\n",p);
1086 guint32 p = va_arg (ap, guint32);
1087 printf ("[%04x]\n",p);
1091 guint32 p = va_arg (ap, guint32);
1092 printf ("[%08x]\n",p);
1096 guint64 p = va_arg (ap, guint64);
1097 printf ("[%016lx]\n",p);
1101 gpointer p = va_arg (ap, gpointer);
1102 printf ("[VALUETYPE] %p\n",p);
1108 case MONO_TYPE_TYPEDBYREF: {
1109 guint8 *p = va_arg (ap, gpointer);
1111 size = mono_type_size (type, &align);
1118 for (j = 0; p && j < size; j++)
1119 printf ("%02x,", p [j]);
1123 printf ("[TYPEDBYREF]\n");
1128 printf ("(unknown return type %x)",
1129 mono_method_signature (method)->ret->type);
1132 ip = ((gint64) __builtin_return_address (0));
1133 printf (" ip: %p\n", (gpointer) ip);
1136 /*========================= End of Function ========================*/
1138 /*------------------------------------------------------------------*/
1140 /* Name - catch_SIGILL */
1142 /* Function - Catch SIGILL as a result of testing for long */
1143 /* displacement facility. */
1145 /*------------------------------------------------------------------*/
1148 catch_SIGILL(int sigNo, siginfo_t *info, void *act) {
1154 /*========================= End of Function ========================*/
1156 /*------------------------------------------------------------------*/
1158 /* Name - mono_arch_cpu_init */
1160 /* Function - Perform CPU specific initialization to execute */
1163 /*------------------------------------------------------------------*/
1166 mono_arch_cpu_init (void)
1168 struct sigaction sa,
1172 /*--------------------------------------*/
1173 /* Set default rounding mode for FP */
1174 /*--------------------------------------*/
1175 __asm__ ("SRNM\t%0\n\t"
1178 /*--------------------------------------*/
1179 /* Determine if we have long displace- */
1180 /* ment facility on this processor */
1181 /*--------------------------------------*/
1182 sa.sa_sigaction = catch_SIGILL;
1183 sigemptyset (&sa.sa_mask);
1184 sa.sa_flags = SA_SIGINFO;
1186 sigaction (SIGILL, &sa, oldSa);
1188 /*--------------------------------------*/
1189 /* We test by executing the STY inst */
1190 /*--------------------------------------*/
1191 __asm__ ("LGHI\t0,1\n\t"
1193 ".byte\t0xe3,0x00,0x10,0x00,0x00,0x50\n\t"
1194 : "=m" (has_ld) : : "0", "1");
1196 sigaction (SIGILL, oldSa, NULL);
1199 /*========================= End of Function ========================*/
1201 /*------------------------------------------------------------------*/
1203 /* Name - mono_arch_init. */
1205 /* Function - Initialize architecture specific code. */
1207 /*------------------------------------------------------------------*/
1210 mono_arch_init (void)
1216 * When we do an architectural level set at z9 or better
1217 * we can use the STFLE instruction to show us
1218 * what hardware facilities are available
1220 int lFacility = sizeof(facs) % 8;
1222 memset((char *) &facs, 0, sizeof(facs));
1224 __asm__ (" lgfr 0,%1\n"
1226 : "=m" (facs) : "r" (lFacility) : "0", "cc");
1229 ss_trigger_page = mono_valloc (NULL, mono_pagesize (), MONO_MMAP_READ);
1230 bp_trigger_page = mono_valloc (NULL, mono_pagesize (), MONO_MMAP_READ);
1231 mono_mprotect (bp_trigger_page, mono_pagesize (), 0);
1233 code = (guint8 *) &breakpointCode;
1234 s390_basr(code, s390_r13, 0);
1236 s390_llong(code, 0);
1237 s390_lg(code, s390_r13, 0, s390_r13, 4);
1238 s390_lg(code, s390_r0, 0, s390_r13, 0);
1241 /*========================= End of Function ========================*/
1243 /*------------------------------------------------------------------*/
1245 /* Name - mono_arch_cleanup. */
1247 /* Function - Cleanup architecture specific code . */
1249 /*------------------------------------------------------------------*/
1252 mono_arch_cleanup (void)
1256 /*========================= End of Function ========================*/
1258 /*------------------------------------------------------------------*/
1260 /* Name - mono_arch_cpu_optimizazions */
1262 /* Function - Returns the optimizations supported on this CPU */
1264 /*------------------------------------------------------------------*/
1267 mono_arch_cpu_optimizazions (guint32 *exclude_mask)
1271 /*----------------------------------------------------------*/
1272 /* No s390-specific optimizations yet */
1273 /*----------------------------------------------------------*/
1274 *exclude_mask = MONO_OPT_INLINE|MONO_OPT_LINEARS;
1275 // *exclude_mask = MONO_OPT_INLINE;
1279 /*========================= End of Function ========================*/
1281 /*------------------------------------------------------------------*/
1287 /*------------------------------------------------------------------*/
1290 is_regsize_var (MonoType *t) {
1293 switch (mono_type_get_underlying_type (t)->type) {
1301 case MONO_TYPE_FNPTR:
1303 case MONO_TYPE_OBJECT:
1304 case MONO_TYPE_STRING:
1305 case MONO_TYPE_CLASS:
1306 case MONO_TYPE_SZARRAY:
1307 case MONO_TYPE_ARRAY:
1309 case MONO_TYPE_VALUETYPE:
1310 if (t->data.klass->enumtype)
1311 return is_regsize_var (mono_class_enum_basetype (t->data.klass));
1317 /*========================= End of Function ========================*/
1319 /*------------------------------------------------------------------*/
1321 /* Name - mono_arch_get_allocatable_int_vars */
1325 /*------------------------------------------------------------------*/
1328 mono_arch_get_allocatable_int_vars (MonoCompile *cfg)
1333 for (i = 0; i < cfg->num_varinfo; i++) {
1334 MonoInst *ins = cfg->varinfo [i];
1335 MonoMethodVar *vmv = MONO_VARINFO (cfg, i);
1338 if (vmv->range.first_use.abs_pos >= vmv->range.last_use.abs_pos)
1341 if (ins->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT) ||
1342 (ins->opcode != OP_LOCAL && ins->opcode != OP_ARG))
1345 /* we can only allocate 32 bit values */
1346 if (is_regsize_var (ins->inst_vtype)) {
1347 g_assert (MONO_VARINFO (cfg, i)->reg == -1);
1348 g_assert (i == vmv->idx);
1349 vars = mono_varlist_insert_sorted (cfg, vars, vmv, FALSE);
1356 /*========================= End of Function ========================*/
1358 /*------------------------------------------------------------------*/
1360 /* Name - mono_arch_global_int_regs */
1362 /* Function - Return a list of usable integer registers. */
1364 /*------------------------------------------------------------------*/
1367 mono_arch_get_global_int_regs (MonoCompile *cfg)
1370 MonoMethodHeader *header;
1373 header = cfg->header;
1374 if ((cfg->flags & MONO_CFG_HAS_ALLOCA) || header->num_clauses)
1375 cfg->frame_reg = s390_r11;
1378 /* FIXME: s390_r12 is reserved for bkchain_reg. Only reserve it if needed */
1380 for (i = 8; i < top; ++i) {
1381 if (cfg->frame_reg != i)
1382 regs = g_list_prepend (regs, GUINT_TO_POINTER (i));
1388 /*========================= End of Function ========================*/
1390 /*------------------------------------------------------------------*/
1392 /* Name - mono_arch_flush_icache */
1394 /* Function - Flush the CPU icache. */
1396 /*------------------------------------------------------------------*/
1399 mono_arch_flush_icache (guint8 *code, gint size)
1403 /*========================= End of Function ========================*/
1405 /*------------------------------------------------------------------*/
1407 /* Name - add_general */
1409 /* Function - Determine code and stack size incremements for a */
1412 /*------------------------------------------------------------------*/
1415 add_general (guint *gr, size_data *sz, ArgInfo *ainfo)
1417 if (*gr > S390_LAST_ARG_REG) {
1418 sz->stack_size = S390_ALIGN(sz->stack_size, sizeof(long));
1419 ainfo->offset = sz->stack_size;
1420 ainfo->reg = STK_BASE;
1421 ainfo->regtype = RegTypeBase;
1422 sz->stack_size += sizeof(long);
1423 sz->local_size += sizeof(long);
1424 sz->offStruct += sizeof(long);
1425 sz->code_size += 12;
1433 /*========================= End of Function ========================*/
1435 /*------------------------------------------------------------------*/
1437 /* Name - add_stackParm */
1439 /* Function - Determine code and stack size incremements for a */
1442 /*------------------------------------------------------------------*/
1445 add_stackParm (guint *gr, size_data *sz, ArgInfo *ainfo, gint size)
1447 if (*gr > S390_LAST_ARG_REG) {
1448 sz->stack_size = S390_ALIGN(sz->stack_size, sizeof(long));
1449 ainfo->reg = STK_BASE;
1450 ainfo->offset = sz->stack_size;
1451 ainfo->regtype = RegTypeStructByAddrOnStack;
1452 sz->stack_size += sizeof (gpointer);
1453 sz->parm_size += sizeof(gpointer);
1454 sz->offStruct += sizeof(gpointer);
1457 ainfo->offset = sz->stack_size;
1458 ainfo->regtype = RegTypeStructByAddr;
1461 ainfo->offparm = sz->offset;
1462 sz->offset = S390_ALIGN(sz->offset+size, sizeof(long));
1464 ainfo->vtsize = size;
1465 sz->parm_size += size;
1468 /*========================= End of Function ========================*/
1470 /*------------------------------------------------------------------*/
1472 /* Name - add_float */
1474 /* Function - Determine code and stack size incremements for a */
1475 /* float parameter. */
1477 /*------------------------------------------------------------------*/
1480 add_float (guint *fr, size_data *sz, ArgInfo *ainfo)
1482 if ((*fr) <= S390_LAST_FPARG_REG) {
1483 ainfo->regtype = RegTypeFP;
1489 ainfo->offset = sz->stack_size;
1490 ainfo->reg = STK_BASE;
1491 ainfo->regtype = RegTypeBase;
1493 sz->stack_size += sizeof(double);
1494 sz->local_size += sizeof(double);
1495 sz->offStruct += sizeof(double);
1499 /*========================= End of Function ========================*/
1501 /*------------------------------------------------------------------*/
1503 /* Name - get_call_info */
1505 /* Function - Determine the amount of space required for code */
1506 /* and stack. In addition determine starting points */
1507 /* for stack-based parameters, and area for struct- */
1508 /* ures being returned on the stack. */
1510 /*------------------------------------------------------------------*/
1513 get_call_info (MonoCompile *cfg, MonoMemPool *mp, MonoMethodSignature *sig, gboolean is_pinvoke)
1515 guint i, fr, gr, size, pstart;
1516 int nParm = sig->hasthis + sig->param_count;
1518 guint32 simpletype, align;
1521 MonoGenericSharingContext *gsctx = cfg ? cfg->generic_sharing_context : NULL;
1524 cinfo = mono_mempool_alloc0 (mp, sizeof (CallInfo) + sizeof (ArgInfo) * nParm);
1526 cinfo = g_malloc0 (sizeof (CallInfo) + sizeof (ArgInfo) * nParm);
1531 cinfo->struct_ret = 0;
1535 sz->offStruct = S390_MINIMAL_STACK_SIZE;
1536 sz->stack_size = S390_MINIMAL_STACK_SIZE;
1543 /*----------------------------------------------------------*/
1544 /* We determine the size of the return code/stack in case we*/
1545 /* need to reserve a register to be used to address a stack */
1546 /* area that the callee will use. */
1547 /*----------------------------------------------------------*/
1549 ret_type = mono_type_get_underlying_type (sig->ret);
1550 ret_type = mini_get_basic_type_from_generic (gsctx, ret_type);
1551 simpletype = ret_type->type;
1553 switch (simpletype) {
1554 case MONO_TYPE_BOOLEAN:
1559 case MONO_TYPE_CHAR:
1564 case MONO_TYPE_CLASS:
1565 case MONO_TYPE_OBJECT:
1566 case MONO_TYPE_SZARRAY:
1567 case MONO_TYPE_ARRAY:
1569 case MONO_TYPE_FNPTR:
1570 case MONO_TYPE_STRING:
1571 cinfo->ret.reg = s390_r2;
1576 cinfo->ret.reg = s390_f0;
1581 cinfo->ret.reg = s390_r2;
1584 case MONO_TYPE_GENERICINST:
1585 if (!mono_type_generic_inst_is_valuetype (sig->ret)) {
1586 cinfo->ret.reg = s390_r2;
1591 case MONO_TYPE_VALUETYPE: {
1592 MonoClass *klass = mono_class_from_mono_type (sig->ret);
1593 if (klass->enumtype) {
1594 simpletype = mono_class_enum_basetype (klass)->type;
1598 size = mono_class_native_size (klass, &align);
1600 size = mono_class_value_size (klass, &align);
1602 cinfo->struct_ret = 1;
1603 cinfo->ret.size = size;
1604 cinfo->ret.vtsize = size;
1607 case MONO_TYPE_TYPEDBYREF:
1608 size = sizeof (MonoTypedRef);
1609 cinfo->struct_ret = 1;
1610 cinfo->ret.size = size;
1611 cinfo->ret.vtsize = size;
1613 case MONO_TYPE_VOID:
1616 g_error ("Can't handle as return value 0x%x", sig->ret->type);
1622 * To simplify get_this_arg_reg () and LLVM integration, emit the vret arg after
1623 * the first argument, allowing 'this' to be always passed in the first arg reg.
1624 * Also do this if the first argument is a reference type, since virtual calls
1625 * are sometimes made using calli without sig->hasthis set, like in the delegate
1628 if (cinfo->struct_ret && !is_pinvoke &&
1630 (sig->param_count > 0 &&
1631 MONO_TYPE_IS_REFERENCE (mini_type_get_underlying_type (gsctx, sig->params [0]))))) {
1633 cinfo->args[nParm].size = sizeof (gpointer);
1634 add_general (&gr, sz, cinfo->args + nParm);
1636 cinfo->args[nParm].size = sizeof (gpointer);
1637 add_general (&gr, sz, &cinfo->args [sig->hasthis + nParm]);
1641 cinfo->vret_arg_index = 1;
1642 cinfo->ret.reg = gr;
1647 cinfo->args[nParm].size = sizeof (gpointer);
1648 add_general (&gr, sz, cinfo->args + nParm);
1652 if (cinfo->struct_ret) {
1653 cinfo->ret.reg = gr;
1658 if ((sig->call_convention == MONO_CALL_VARARG) && (sig->param_count == 0)) {
1659 gr = S390_LAST_ARG_REG + 1;
1660 fr = S390_LAST_FPARG_REG + 1;
1662 /* Emit the signature cookie just before the implicit arguments */
1663 add_general (&gr, sz, &cinfo->sigCookie);
1666 /*----------------------------------------------------------*/
1667 /* We determine the size of the parameter code and stack */
1668 /* requirements by checking the types and sizes of the */
1670 /*----------------------------------------------------------*/
1672 for (i = pstart; i < sig->param_count; ++i) {
1675 /*--------------------------------------------------*/
1676 /* Handle vararg type calls. All args are put on */
1678 /*--------------------------------------------------*/
1679 if ((sig->call_convention == MONO_CALL_VARARG) &&
1680 (i == sig->sentinelpos)) {
1681 gr = S390_LAST_ARG_REG + 1;
1682 fr = S390_LAST_FPARG_REG + 1;
1683 add_general (&gr, sz, &cinfo->sigCookie);
1686 if (sig->params [i]->byref) {
1687 add_general (&gr, sz, cinfo->args+nParm);
1688 cinfo->args[nParm].size = sizeof(gpointer);
1693 ptype = mono_type_get_underlying_type (sig->params [i]);
1694 ptype = mini_get_basic_type_from_generic (gsctx, ptype);
1695 simpletype = ptype->type;
1696 cinfo->args[nParm].type = simpletype;
1697 switch (simpletype) {
1698 case MONO_TYPE_BOOLEAN:
1701 cinfo->args[nParm].size = sizeof(char);
1702 add_general (&gr, sz, cinfo->args+nParm);
1707 case MONO_TYPE_CHAR:
1708 cinfo->args[nParm].size = sizeof(short);
1709 add_general (&gr, sz, cinfo->args+nParm);
1714 cinfo->args[nParm].size = sizeof(int);
1715 add_general (&gr, sz, cinfo->args+nParm);
1721 case MONO_TYPE_FNPTR:
1722 case MONO_TYPE_CLASS:
1723 case MONO_TYPE_OBJECT:
1724 case MONO_TYPE_STRING:
1725 case MONO_TYPE_SZARRAY:
1726 case MONO_TYPE_ARRAY:
1727 cinfo->args[nParm].size = sizeof(gpointer);
1728 add_general (&gr, sz, cinfo->args+nParm);
1733 cinfo->args[nParm].size = sizeof(long long);
1734 add_general (&gr, sz, cinfo->args+nParm);
1738 cinfo->args[nParm].size = sizeof(float);
1739 add_float (&fr, sz, cinfo->args+nParm);
1743 cinfo->args[nParm].size = sizeof(double);
1744 add_float (&fr, sz, cinfo->args+nParm);
1747 case MONO_TYPE_GENERICINST:
1748 if (!mono_type_generic_inst_is_valuetype (sig->params [i])) {
1749 cinfo->args[nParm].size = sizeof(gpointer);
1750 add_general (&gr, sz, cinfo->args+nParm);
1755 case MONO_TYPE_VALUETYPE: {
1756 MonoMarshalType *info;
1757 MonoClass *klass = mono_class_from_mono_type (sig->params [i]);
1759 size = mono_class_native_size (klass, &align);
1761 size = mono_class_value_size (klass, &align);
1763 info = mono_marshal_load_type_info (klass);
1765 if ((info->native_size == sizeof(float)) &&
1766 (info->num_fields == 1) &&
1767 (info->fields[0].field->type->type == MONO_TYPE_R4)) {
1768 cinfo->args[nParm].size = sizeof(float);
1769 add_float(&fr, sz, cinfo->args+nParm);
1774 if ((info->native_size == sizeof(double)) &&
1775 (info->num_fields == 1) &&
1776 (info->fields[0].field->type->type == MONO_TYPE_R8)) {
1777 cinfo->args[nParm].size = sizeof(double);
1778 add_float(&fr, sz, cinfo->args+nParm);
1783 cinfo->args[nParm].vtsize = 0;
1784 cinfo->args[nParm].size = 0;
1787 /*----------------------------------*/
1788 /* On S/390, structures of size 1, */
1789 /* 2, 4, and 8 bytes are passed in */
1790 /* (a) register(s). */
1791 /*----------------------------------*/
1797 add_general(&gr, sz, cinfo->args+nParm);
1798 cinfo->args[nParm].size = size;
1799 cinfo->args[nParm].regtype = RegTypeStructByVal;
1801 sz->local_size += sizeof(long);
1804 add_stackParm(&gr, sz, cinfo->args+nParm, size);
1809 case MONO_TYPE_TYPEDBYREF: {
1810 int size = sizeof (MonoTypedRef);
1812 cinfo->args[nParm].vtsize = 0;
1813 cinfo->args[nParm].size = 0;
1816 /*----------------------------------*/
1817 /* On S/390, structures of size 1, */
1818 /* 2, 4, and 8 bytes are passed in */
1819 /* (a) register(s). */
1820 /*----------------------------------*/
1826 add_general(&gr, sz, cinfo->args+nParm);
1827 cinfo->args[nParm].size = size;
1828 cinfo->args[nParm].regtype = RegTypeStructByVal;
1830 sz->local_size += sizeof(long);
1833 add_stackParm(&gr, sz, cinfo->args+nParm, size);
1839 g_error ("Can't trampoline 0x%x", sig->params [i]->type);
1843 /*----------------------------------------------------------*/
1844 /* Handle the case where there are no implicit arguments */
1845 /*----------------------------------------------------------*/
1846 if ((sig->call_convention == MONO_CALL_VARARG) &&
1849 (sig->param_count == sig->sentinelpos)) {
1850 gr = S390_LAST_ARG_REG + 1;
1851 fr = S390_LAST_FPARG_REG + 1;
1852 add_general (&gr, sz, &cinfo->sigCookie);
1855 /*----------------------------------------------------------*/
1856 /* If we are passing a structure back then if it won't be */
1857 /* in a register(s) then we make room at the end of the */
1858 /* parameters that may have been placed on the stack */
1859 /*----------------------------------------------------------*/
1860 if (cinfo->struct_ret) {
1861 cinfo->ret.offset = sz->stack_size;
1862 switch (cinfo->ret.size) {
1870 sz->stack_size += S390_ALIGN(cinfo->ret.size, align);
1875 sz->stack_size = sz->stack_size + sz->local_size + sz->parm_size +
1877 sz->stack_size = S390_ALIGN(sz->stack_size, sizeof(long));
1882 /*========================= End of Function ========================*/
1884 /*------------------------------------------------------------------*/
1886 /* Name - mono_arch_allocate_vars */
1888 /* Function - Set var information according to the calling */
1889 /* convention for S/390. The local var stuff should */
1890 /* most likely be split in another method. */
1892 /* Parameter - @m - Compile unit. */
1894 /*------------------------------------------------------------------*/
1897 mono_arch_allocate_vars (MonoCompile *cfg)
1899 MonoMethodSignature *sig;
1900 MonoMethodHeader *header;
1903 int iParm, iVar, offset, align, size, curinst;
1904 int frame_reg = STK_BASE;
1907 header = cfg->header;
1909 cfg->flags |= MONO_CFG_HAS_SPILLUP;
1911 /*---------------------------------------------------------*/
1912 /* We use the frame register also for any method that has */
1913 /* filter clauses. This way, when the handlers are called, */
1914 /* the code will reference local variables using the frame */
1915 /* reg instead of the stack pointer: if we had to restore */
1916 /* the stack pointer, we'd corrupt the method frames that */
1917 /* are already on the stack (since filters get called */
1918 /* before stack unwinding happens) when the filter code */
1919 /* would call any method. */
1920 /*---------------------------------------------------------*/
1921 if ((cfg->flags & MONO_CFG_HAS_ALLOCA) || header->num_clauses)
1922 frame_reg = s390_r11;
1924 cfg->frame_reg = frame_reg;
1926 cfg->arch.bkchain_reg = -1;
1928 if (frame_reg != STK_BASE)
1929 cfg->used_int_regs |= 1 << frame_reg;
1931 sig = mono_method_signature (cfg->method);
1933 cinfo = get_call_info (cfg, cfg->mempool, sig, sig->pinvoke);
1935 if (!cinfo->struct_ret) {
1936 switch (mono_type_get_underlying_type (sig->ret)->type) {
1937 case MONO_TYPE_VOID:
1940 cfg->ret->opcode = OP_REGVAR;
1941 cfg->ret->dreg = s390_r2;
1946 /*--------------------------------------------------------------*/
1947 /* local vars are at a positive offset from the stack pointer */
1948 /* also note that if the function uses alloca, we use s390_r11 */
1949 /* to point at the local variables. */
1950 /* add parameter area size for called functions */
1951 /*--------------------------------------------------------------*/
1952 if (cfg->param_area == 0)
1953 offset = S390_MINIMAL_STACK_SIZE;
1955 offset = cfg->param_area;
1957 cfg->sig_cookie = 0;
1959 if (cinfo->struct_ret) {
1960 inst = cfg->vret_addr;
1961 offset = S390_ALIGN(offset, sizeof(gpointer));
1962 inst->inst_offset = offset;
1963 inst->opcode = OP_REGOFFSET;
1964 inst->inst_basereg = frame_reg;
1965 offset += sizeof(gpointer);
1966 if (G_UNLIKELY (cfg->verbose_level > 1)) {
1967 printf ("vret_addr =");
1968 mono_print_ins (cfg->vret_addr);
1973 inst = cfg->args [0];
1974 if (inst->opcode != OP_REGVAR) {
1975 inst->opcode = OP_REGOFFSET;
1976 inst->inst_basereg = frame_reg;
1977 offset = S390_ALIGN(offset, sizeof(gpointer));
1978 inst->inst_offset = offset;
1979 offset += sizeof (gpointer);
1986 eArg = sig->param_count + sArg;
1988 if (sig->call_convention == MONO_CALL_VARARG)
1989 cfg->sig_cookie += S390_MINIMAL_STACK_SIZE;
1991 for (iParm = sArg; iParm < eArg; ++iParm) {
1992 inst = cfg->args [curinst];
1993 if (inst->opcode != OP_REGVAR) {
1994 switch (cinfo->args[iParm].regtype) {
1995 case RegTypeStructByAddr : {
1998 size = sizeof (gpointer);
2000 inst->opcode = OP_REGOFFSET;
2001 inst->inst_basereg = frame_reg;
2002 offset = S390_ALIGN (offset, sizeof (gpointer));
2003 inst->inst_offset = offset;
2005 /* Add a level of indirection */
2006 MONO_INST_NEW (cfg, indir, 0);
2008 inst->opcode = OP_VTARG_ADDR;
2009 inst->inst_left = indir;
2012 case RegTypeStructByAddrOnStack : {
2015 size = sizeof (gpointer);
2017 /* Similar to the == STK_BASE case below */
2018 cfg->arch.bkchain_reg = s390_r12;
2019 cfg->used_int_regs |= 1 << cfg->arch.bkchain_reg;
2021 inst->opcode = OP_REGOFFSET;
2022 inst->dreg = mono_alloc_preg (cfg);
2023 inst->inst_basereg = cfg->arch.bkchain_reg;
2024 inst->inst_offset = cinfo->args [iParm].offset;
2026 /* Add a level of indirection */
2027 MONO_INST_NEW (cfg, indir, 0);
2029 inst->opcode = OP_VTARG_ADDR;
2030 inst->inst_left = indir;
2033 case RegTypeStructByVal :
2034 size = cinfo->args[iParm].size;
2035 offset = S390_ALIGN(offset, size);
2036 inst->opcode = OP_REGOFFSET;
2037 inst->inst_basereg = frame_reg;
2038 inst->inst_offset = offset;
2041 if (cinfo->args [iParm].reg == STK_BASE) {
2043 * These arguments are in the previous frame, so we can't
2044 * compute their offset from the current frame pointer right
2045 * now, since cfg->stack_offset is not yet known, so dedicate a
2046 * register holding the previous frame pointer.
2048 cfg->arch.bkchain_reg = s390_r12;
2049 cfg->used_int_regs |= 1 << cfg->arch.bkchain_reg;
2051 inst->opcode = OP_REGOFFSET;
2052 inst->inst_basereg = cfg->arch.bkchain_reg;
2053 size = (cinfo->args[iParm].size < 8
2054 ? 8 - cinfo->args[iParm].size
2056 inst->inst_offset = cinfo->args [iParm].offset + size;
2057 size = sizeof (long);
2059 inst->opcode = OP_REGOFFSET;
2060 inst->inst_basereg = frame_reg;
2061 size = (cinfo->args[iParm].size < 8
2064 offset = S390_ALIGN(offset, size);
2065 if (cfg->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE)
2066 inst->inst_offset = offset;
2068 inst->inst_offset = offset + (8 - size);
2073 if ((sig->call_convention == MONO_CALL_VARARG) &&
2074 (cinfo->args[iParm].regtype != RegTypeGeneral) &&
2075 (iParm < sig->sentinelpos))
2076 cfg->sig_cookie += size;
2077 printf("%s %4d cookine %x\n",__FUNCTION__,__LINE__,cfg->sig_cookie);
2080 offset += MAX(size, 8);
2085 curinst = cfg->locals_start;
2086 for (iVar = curinst; iVar < cfg->num_varinfo; ++iVar) {
2087 inst = cfg->varinfo [iVar];
2088 if ((inst->flags & MONO_INST_IS_DEAD) ||
2089 (inst->opcode == OP_REGVAR))
2092 /*--------------------------------------------------*/
2093 /* inst->backend.is_pinvoke indicates native sized */
2094 /* value typs this is used by the pinvoke wrappers */
2095 /* when they call functions returning structure */
2096 /*--------------------------------------------------*/
2097 if (inst->backend.is_pinvoke && MONO_TYPE_ISSTRUCT (inst->inst_vtype))
2098 size = mono_class_native_size (mono_class_from_mono_type(inst->inst_vtype),
2099 (guint32 *) &align);
2101 size = mono_type_size (inst->inst_vtype, &align);
2103 offset = S390_ALIGN(offset, align);
2104 inst->inst_offset = offset;
2105 inst->opcode = OP_REGOFFSET;
2106 inst->inst_basereg = frame_reg;
2108 DEBUG (g_print("allocating local %d to %ld, size: %d\n",
2109 iVar, inst->inst_offset, size));
2112 /*------------------------------------------------------*/
2113 /* Allow space for the trace method stack area if needed*/
2114 /*------------------------------------------------------*/
2115 if (mono_jit_trace_calls != NULL && mono_trace_eval (cfg->method))
2116 offset += S390_TRACE_STACK_SIZE;
2118 /*------------------------------------------------------*/
2119 /* Reserve space to save LMF and caller saved registers */
2120 /*------------------------------------------------------*/
2121 if (cfg->method->save_lmf)
2122 offset += sizeof (MonoLMF);
2124 /*------------------------------------------------------*/
2125 /* align the offset */
2126 /*------------------------------------------------------*/
2127 cfg->stack_offset = S390_ALIGN(offset, S390_STACK_ALIGNMENT);
2129 /*------------------------------------------------------*/
2130 /* Fix offsets for args whose value is in parent frame */
2131 /*------------------------------------------------------*/
2132 for (iParm = sArg; iParm < eArg; ++iParm) {
2133 inst = cfg->args [iParm];
2135 if (inst->opcode == OP_S390_STKARG) {
2136 inst->opcode = OP_REGOFFSET;
2137 inst->inst_offset += cfg->stack_offset;
2142 /*========================= End of Function ========================*/
2144 /*------------------------------------------------------------------*/
2146 /* Name - mono_arch_create_vars */
2148 /*------------------------------------------------------------------*/
2151 mono_arch_create_vars (MonoCompile *cfg)
2153 MonoMethodSignature *sig;
2156 sig = mono_method_signature (cfg->method);
2158 cinfo = get_call_info (cfg, cfg->mempool, sig, sig->pinvoke);
2160 if (cinfo->struct_ret) {
2161 cfg->vret_addr = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_ARG);
2162 if (G_UNLIKELY (cfg->verbose_level > 1)) {
2163 printf ("vret_addr = ");
2164 mono_print_ins (cfg->vret_addr);
2169 /*========================= End of Function ========================*/
2171 /*------------------------------------------------------------------*/
2173 /* Name - add_outarg_reg2. */
2175 /*------------------------------------------------------------------*/
2178 add_outarg_reg2 (MonoCompile *cfg, MonoCallInst *call, ArgStorage storage, int reg, MonoInst *tree)
2183 case RegTypeGeneral:
2184 MONO_INST_NEW (cfg, ins, OP_MOVE);
2185 ins->dreg = mono_alloc_ireg (cfg);
2186 ins->sreg1 = tree->dreg;
2187 MONO_ADD_INS (cfg->cbb, ins);
2188 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, reg, FALSE);
2191 MONO_INST_NEW (cfg, ins, OP_FMOVE);
2192 ins->dreg = mono_alloc_freg (cfg);
2193 ins->sreg1 = tree->dreg;
2194 MONO_ADD_INS (cfg->cbb, ins);
2195 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, reg, TRUE);
2198 MONO_INST_NEW (cfg, ins, OP_S390_SETF4RET);
2199 ins->dreg = mono_alloc_freg (cfg);
2200 ins->sreg1 = tree->dreg;
2201 MONO_ADD_INS (cfg->cbb, ins);
2202 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, reg, TRUE);
2205 g_assert_not_reached ();
2209 /*========================= End of Function ========================*/
2211 /*------------------------------------------------------------------*/
2213 /* Name - emit_sig_cookie. */
2215 /*------------------------------------------------------------------*/
2218 emit_sig_cookie (MonoCompile *cfg, MonoCallInst *call, CallInfo *cinfo)
2220 MonoMethodSignature *tmpSig;
2223 cfg->disable_aot = TRUE;
2225 /*----------------------------------------------------------*/
2226 /* mono_ArgIterator_Setup assumes the signature cookie is */
2227 /* passed first and all the arguments which were before it */
2228 /* passed on the stack after the signature. So compensate */
2229 /* by passing a different signature. */
2230 /*----------------------------------------------------------*/
2231 tmpSig = mono_metadata_signature_dup (call->signature);
2232 tmpSig->param_count -= call->signature->sentinelpos;
2233 tmpSig->sentinelpos = 0;
2234 if (tmpSig->param_count > 0)
2235 memcpy (tmpSig->params,
2236 call->signature->params + call->signature->sentinelpos,
2237 tmpSig->param_count * sizeof(MonoType *));
2239 MONO_INST_NEW (cfg, sig_arg, OP_ICONST);
2240 sig_arg->dreg = mono_alloc_ireg (cfg);
2241 sig_arg->inst_p0 = tmpSig;
2242 MONO_ADD_INS (cfg->cbb, sig_arg);
2244 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, STK_BASE,
2245 cinfo->sigCookie.offset, sig_arg->dreg);
2248 /*========================= End of Function ========================*/
2250 /*------------------------------------------------------------------*/
2252 /* Name - mono_arch_emit_call */
2254 /*------------------------------------------------------------------*/
2257 mono_arch_emit_call (MonoCompile *cfg, MonoCallInst *call)
2260 MonoMethodSignature *sig;
2262 int i, n, lParamArea;
2264 ArgInfo *ainfo = NULL;
2266 MonoMethodHeader *header;
2269 sig = call->signature;
2270 n = sig->param_count + sig->hasthis;
2271 DEBUG (g_print ("Call requires: %d parameters\n",n));
2273 cinfo = get_call_info (cfg, cfg->mempool, sig, sig->pinvoke);
2275 stackSize = cinfo->sz.stack_size + cinfo->sz.local_size +
2276 cinfo->sz.parm_size + cinfo->sz.offset;
2277 call->stack_usage = MAX(stackSize, call->stack_usage);
2278 lParamArea = MAX((call->stack_usage-S390_MINIMAL_STACK_SIZE-cinfo->sz.parm_size), 0);
2279 cfg->param_area = MAX(((signed) cfg->param_area), lParamArea);
2280 cfg->flags |= MONO_CFG_HAS_CALLS;
2282 if (cinfo->struct_ret) {
2283 MONO_INST_NEW (cfg, ins, OP_MOVE);
2284 ins->sreg1 = call->vret_var->dreg;
2285 ins->dreg = mono_alloc_preg (cfg);
2286 MONO_ADD_INS (cfg->cbb, ins);
2287 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, cinfo->ret.reg, FALSE);
2290 header = cfg->header;
2291 if ((cfg->flags & MONO_CFG_HAS_ALLOCA) || header->num_clauses)
2296 for (i = 0; i < n; ++i) {
2299 ainfo = cinfo->args + i;
2300 if (i >= sig->hasthis)
2301 t = sig->params [i - sig->hasthis];
2303 t = &mono_defaults.int_class->byval_arg;
2304 t = mono_type_get_underlying_type (t);
2306 in = call->args [i];
2308 if ((sig->call_convention == MONO_CALL_VARARG) &&
2310 (i == sig->sentinelpos)) {
2311 emit_sig_cookie (cfg, call, cinfo);
2314 switch (ainfo->regtype) {
2315 case RegTypeGeneral:
2316 add_outarg_reg2 (cfg, call, ainfo->regtype, ainfo->reg, in);
2319 if (MONO_TYPE_ISSTRUCT (t)) {
2320 /* Valuetype passed in one fp register */
2321 ainfo->regtype = RegTypeStructByValInFP;
2324 if (ainfo->size == 4)
2325 ainfo->regtype = RegTypeFPR4;
2326 add_outarg_reg2 (cfg, call, ainfo->regtype, ainfo->reg, in);
2329 case RegTypeStructByVal:
2330 case RegTypeStructByAddr:
2331 case RegTypeStructByAddrOnStack: {
2335 if (sig->params [i - sig->hasthis]->type == MONO_TYPE_TYPEDBYREF) {
2336 size = sizeof (MonoTypedRef);
2337 align = sizeof (gpointer);
2341 size = mono_type_native_stack_size (&in->klass->byval_arg, &align);
2344 * Other backends use mono_type_stack_size (), but that
2345 * aligns the size to 8, which is larger than the size of
2346 * the source, leading to reads of invalid memory if the
2347 * source is at the end of address space.
2349 size = mono_class_value_size (in->klass, &align);
2352 g_assert (in->klass);
2354 ainfo->offparm += cinfo->sz.offStruct;
2356 MONO_INST_NEW (cfg, ins, OP_OUTARG_VT);
2357 ins->sreg1 = in->dreg;
2358 ins->klass = in->klass;
2359 ins->backend.size = ainfo->size;
2360 ins->inst_p0 = call;
2361 ins->inst_p1 = mono_mempool_alloc (cfg->mempool, sizeof (ArgInfo));
2362 memcpy (ins->inst_p1, ainfo, sizeof (ArgInfo));
2364 MONO_ADD_INS (cfg->cbb, ins);
2366 if (ainfo->regtype == RegTypeStructByAddr) {
2368 * We use OP_OUTARG_VT to copy the valuetype to a stack location, then
2369 * use the normal OUTARG opcodes to pass the address of the location to
2372 int treg = mono_alloc_preg (cfg);
2373 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ADD_IMM, treg,
2374 frmReg, ainfo->offparm);
2375 mono_call_inst_add_outarg_reg (cfg, call, treg, ainfo->reg, FALSE);
2376 } else if (ainfo->regtype == RegTypeStructByAddrOnStack) {
2377 /* The address of the valuetype is passed on the stack */
2378 int treg = mono_alloc_preg (cfg);
2379 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ADD_IMM, treg,
2380 frmReg, ainfo->offparm);
2381 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG,
2382 ainfo->reg, ainfo->offset, treg);
2387 if (!t->byref && t->type == MONO_TYPE_R4) {
2388 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORER4_MEMBASE_REG,
2389 STK_BASE, ainfo->offset + 4,
2391 } else if (!t->byref && (t->type == MONO_TYPE_R8)) {
2392 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORER8_MEMBASE_REG,
2393 STK_BASE, ainfo->offset,
2396 MONO_INST_NEW (cfg, ins, OP_STORE_MEMBASE_REG);
2397 ins->inst_destbasereg = STK_BASE;
2398 ins->inst_offset = ainfo->offset;
2399 ins->sreg1 = in->dreg;
2402 /* This is needed by MonoTypedRef->value to point to the correct data */
2403 if ((sig->call_convention == MONO_CALL_VARARG) &&
2404 (i >= sig->sentinelpos)) {
2405 switch (ainfo->size) {
2407 ins->opcode = OP_STOREI1_MEMBASE_REG;
2410 ins->opcode = OP_STOREI2_MEMBASE_REG;
2413 ins->opcode = OP_STOREI4_MEMBASE_REG;
2421 MONO_ADD_INS (cfg->cbb, ins);
2425 g_assert_not_reached ();
2431 * Handle the case where there are no implicit arguments
2433 if ((sig->call_convention == MONO_CALL_VARARG) &&
2435 (i == sig->sentinelpos)) {
2436 emit_sig_cookie (cfg, call, cinfo);
2440 /*========================= End of Function ========================*/
2442 /*------------------------------------------------------------------*/
2444 /* Name - mono_arch_emit_outarg_vt */
2446 /*------------------------------------------------------------------*/
2449 mono_arch_emit_outarg_vt (MonoCompile *cfg, MonoInst *ins, MonoInst *src)
2451 MonoCallInst *call = (MonoCallInst*)ins->inst_p0;
2452 ArgInfo *ainfo = (ArgInfo*)ins->inst_p1;
2453 int size = ins->backend.size;
2455 if (ainfo->regtype == RegTypeStructByVal) {
2457 arg->ins.sreg1 = ainfo->reg;
2458 arg->ins.opcode = OP_OUTARG_VT;
2459 arg->size = ainfo->size;
2460 arg->offset = ainfo->offset;
2461 arg->offPrm = ainfo->offparm + cinfo->sz.offStruct;
2463 if (ainfo->reg != STK_BASE) {
2464 MONO_OUTPUT_VTR (cfg, size, ainfo->reg, src->dreg, 0);
2466 MONO_OUTPUT_VTS (cfg, size, ainfo->reg, ainfo->offset,
2469 } else if (ainfo->regtype == RegTypeStructByValInFP) {
2470 int dreg = mono_alloc_freg (cfg);
2472 if (ainfo->size == 4) {
2473 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADR4_MEMBASE, dreg, src->dreg, 0);
2474 MONO_EMIT_NEW_UNALU (cfg, OP_S390_SETF4RET, dreg, dreg);
2476 g_assert (ainfo->size == 8);
2478 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADR8_MEMBASE, dreg, src->dreg, 0);
2481 mono_call_inst_add_outarg_reg (cfg, call, dreg, ainfo->reg, TRUE);
2483 MonoMethodHeader *header;
2486 header = mono_method_get_header (cfg->method);
2487 if ((cfg->flags & MONO_CFG_HAS_ALLOCA) || header->num_clauses)
2492 MONO_EMIT_NEW_MOVE (cfg, srcReg, ainfo->offparm,
2493 src->dreg, 0, size);
2497 /*========================= End of Function ========================*/
2499 /*------------------------------------------------------------------*/
2501 /* Name - mono_arch_emit_setret */
2503 /*------------------------------------------------------------------*/
2506 mono_arch_emit_setret (MonoCompile *cfg, MonoMethod *method, MonoInst *val)
2508 MonoType *ret = mono_type_get_underlying_type (mono_method_signature (method)->ret);
2511 if (ret->type == MONO_TYPE_R4) {
2512 MONO_EMIT_NEW_UNALU (cfg, OP_S390_SETF4RET, s390_f0, val->dreg);
2514 } else if (ret->type == MONO_TYPE_R8) {
2515 MONO_EMIT_NEW_UNALU (cfg, OP_FMOVE, s390_f0, val->dreg);
2520 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, cfg->ret->dreg, val->dreg);
2523 /*========================= End of Function ========================*/
2525 /*------------------------------------------------------------------*/
2527 /* Name - mono_arch_instrument_mem_needs */
2529 /* Function - Allow tracing to work with this interface (with */
2530 /* an optional argument). */
2532 /*------------------------------------------------------------------*/
2535 mono_arch_instrument_mem_needs (MonoMethod *method, int *stack, int *code)
2537 /* no stack room needed now (may be needed for FASTCALL-trace support) */
2539 /* split prolog-epilog requirements? */
2540 *code = 50; /* max bytes needed: check this number */
2543 /*========================= End of Function ========================*/
2545 /*------------------------------------------------------------------*/
2547 /* Name - mono_arch_instrument_prolog */
2549 /* Function - Create an "instrumented" prolog. */
2551 /*------------------------------------------------------------------*/
2554 mono_arch_instrument_prolog (MonoCompile *cfg, void *func, void *p,
2555 gboolean enable_arguments)
2562 parmOffset = cfg->stack_usage - S390_TRACE_STACK_SIZE;
2563 if (cfg->method->save_lmf)
2564 parmOffset -= sizeof(MonoLMF);
2565 fpOffset = parmOffset + (5*sizeof(gpointer));
2566 if ((!has_ld) && (fpOffset > 4096)) {
2567 s390_lgr (code, s390_r12, STK_BASE);
2569 while (fpOffset > 4096) {
2570 s390_aghi (code, baseReg, 4096);
2578 s390_stmg (code, s390_r2, s390_r6, STK_BASE, parmOffset);
2580 s390_stdy (code, s390_f0, 0, STK_BASE, fpOffset);
2581 s390_stdy (code, s390_f2, 0, STK_BASE, fpOffset+sizeof(gdouble));
2582 s390_stdy (code, s390_f4, 0, STK_BASE, fpOffset+2*sizeof(gdouble));
2583 s390_stdy (code, s390_f6, 0, STK_BASE, fpOffset+3*sizeof(gdouble));
2585 s390_std (code, s390_f0, 0, baseReg, fpOffset);
2586 s390_std (code, s390_f2, 0, baseReg, fpOffset+sizeof(gdouble));
2587 s390_std (code, s390_f4, 0, baseReg, fpOffset+2*sizeof(gdouble));
2588 s390_std (code, s390_f6, 0, baseReg, fpOffset+3*sizeof(gdouble));
2590 s390_basr (code, s390_r13, 0);
2592 s390_llong(code, cfg->method);
2593 s390_llong(code, func);
2594 s390_lg (code, s390_r2, 0, s390_r13, 4);
2596 s390_lay (code, s390_r3, 0, STK_BASE, parmOffset);
2598 s390_la (code, s390_r3, 0, baseReg, parmOffset);
2599 s390_lgr (code, s390_r4, STK_BASE);
2600 s390_aghi (code, s390_r4, cfg->stack_usage);
2601 s390_lg (code, s390_r1, 0, s390_r13, 12);
2602 s390_basr (code, s390_r14, s390_r1);
2604 s390_ldy (code, s390_f6, 0, STK_BASE, fpOffset+3*sizeof(gdouble));
2605 s390_ldy (code, s390_f4, 0, STK_BASE, fpOffset+2*sizeof(gdouble));
2606 s390_ldy (code, s390_f2, 0, STK_BASE, fpOffset+sizeof(gdouble));
2607 s390_ldy (code, s390_f0, 0, STK_BASE, fpOffset);
2609 s390_ld (code, s390_f6, 0, baseReg, fpOffset+3*sizeof(gdouble));
2610 s390_ld (code, s390_f4, 0, baseReg, fpOffset+2*sizeof(gdouble));
2611 s390_ld (code, s390_f2, 0, baseReg, fpOffset+sizeof(gdouble));
2612 s390_ld (code, s390_f0, 0, baseReg, fpOffset);
2614 s390_lmg (code, s390_r2, s390_r6, STK_BASE, parmOffset);
2619 /*========================= End of Function ========================*/
2621 /*------------------------------------------------------------------*/
2623 /* Name - mono_arch_instrument_epilog */
2625 /* Function - Create an epilog that will handle the returned */
2626 /* values used in instrumentation. */
2628 /*------------------------------------------------------------------*/
2631 mono_arch_instrument_epilog_full (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments, gboolean preserve_argument_registers)
2634 int save_mode = SAVE_NONE,
2636 MonoMethod *method = cfg->method;
2637 int rtype = mono_type_get_underlying_type (mono_method_signature (method)->ret)->type;
2639 saveOffset = cfg->stack_usage - S390_TRACE_STACK_SIZE;
2640 if (method->save_lmf)
2641 saveOffset -= sizeof(MonoLMF);
2645 case MONO_TYPE_VOID:
2646 /* special case string .ctor icall */
2647 if (strcmp (".ctor", method->name) && method->klass == mono_defaults.string_class)
2648 save_mode = SAVE_ONE;
2650 save_mode = SAVE_NONE;
2654 save_mode = SAVE_ONE;
2657 save_mode = SAVE_R4;
2660 save_mode = SAVE_R8;
2662 case MONO_TYPE_VALUETYPE:
2663 if (mono_method_signature (method)->ret->data.klass->enumtype) {
2664 rtype = mono_class_enum_basetype (mono_method_signature (method)->ret->data.klass)->type;
2667 save_mode = SAVE_STRUCT;
2670 save_mode = SAVE_ONE;
2674 switch (save_mode) {
2676 s390_stg (code, s390_r2, 0, cfg->frame_reg, saveOffset);
2677 if (enable_arguments) {
2678 s390_lgr (code, s390_r3, s390_r2);
2682 s390_std (code, s390_f0, 0, cfg->frame_reg, saveOffset);
2683 if (enable_arguments) {
2684 s390_ldebr (code, s390_f0, s390_f0);
2688 s390_std (code, s390_f0, 0, cfg->frame_reg, saveOffset);
2691 s390_stg (code, s390_r2, 0, cfg->frame_reg, saveOffset);
2692 if (enable_arguments) {
2693 s390_lg (code, s390_r3, 0, cfg->frame_reg,
2694 S390_MINIMAL_STACK_SIZE+cfg->param_area);
2702 s390_basr (code, s390_r13, 0);
2704 s390_llong(code, cfg->method);
2705 s390_llong(code, func);
2706 s390_lg (code, s390_r2, 0, s390_r13, 4);
2707 s390_lg (code, s390_r1, 0, s390_r13, 12);
2708 s390_basr (code, s390_r14, s390_r1);
2710 switch (save_mode) {
2712 s390_lg (code, s390_r2, 0, cfg->frame_reg, saveOffset);
2716 s390_ld (code, s390_f0, 0, cfg->frame_reg, saveOffset);
2719 s390_lg (code, s390_r2, 0, cfg->frame_reg, saveOffset);
2729 /*========================= End of Function ========================*/
2731 /*------------------------------------------------------------------*/
2733 /* Name - mono_arch_peephole_pass_1 */
2735 /* Function - Form a peephole pass at the code looking for */
2736 /* simple optimizations. */
2738 /*------------------------------------------------------------------*/
2741 mono_arch_peephole_pass_1 (MonoCompile *cfg, MonoBasicBlock *bb)
2745 /*========================= End of Function ========================*/
2747 /*------------------------------------------------------------------*/
2749 /* Name - mono_arch_peephole_pass_2 */
2751 /* Function - Form a peephole pass at the code looking for */
2752 /* simple optimizations. */
2754 /*------------------------------------------------------------------*/
2757 mono_arch_peephole_pass_2 (MonoCompile *cfg, MonoBasicBlock *bb)
2761 MONO_BB_FOR_EACH_INS_SAFE (bb, n, ins) {
2762 mono_peephole_ins (bb, ins);
2766 /*========================= End of Function ========================*/
2768 /*------------------------------------------------------------------*/
2770 /* Name - mono_arch_lowering_pass. */
2772 /*------------------------------------------------------------------*/
2775 mono_arch_lowering_pass (MonoCompile *cfg, MonoBasicBlock *bb)
2777 MonoInst *ins, *next;
2779 MONO_BB_FOR_EACH_INS_SAFE (bb, next, ins) {
2780 switch (ins->opcode) {
2785 case OP_IDIV_UN_IMM:
2786 case OP_IREM_UN_IMM:
2790 case OP_LOCALLOC_IMM:
2791 mono_decompose_op_imm (cfg, bb, ins);
2794 if (!s390_is_imm16 (ins->inst_imm))
2795 /* This is created by the memcpy code which ignores is_inst_imm */
2796 mono_decompose_op_imm (cfg, bb, ins);
2803 bb->max_vreg = cfg->next_vreg;
2806 /*========================= End of Function ========================*/
2808 /*------------------------------------------------------------------*/
2810 /* Name - emit_float_to_int */
2812 /* Function - Create instructions which will convert a floating */
2813 /* point value to integer. */
2815 /*------------------------------------------------------------------*/
2818 emit_float_to_int (MonoCompile *cfg, guchar *code, int dreg, int sreg, int size, gboolean is_signed)
2820 /* sreg is a float, dreg is an integer reg. */
2822 s390_cgdbr (code, dreg, 5, sreg);
2825 s390_lghi (code, s390_r0, 0);
2826 s390_lghi (code, s390_r13, 0xff);
2827 s390_ltgr (code, dreg, dreg);
2829 s390_lghi (code, s390_r0, 0x80);
2830 s390_ngr (code, dreg, s390_r13);
2831 s390_ogr (code, dreg, s390_r0);
2836 s390_basr (code, s390_r13, 0);
2838 s390_llong (code, 0x41e0000000000000llu);
2839 s390_llong (code, 0x41f0000000000000llu);
2840 s390_ldr (code, s390_f15, sreg);
2841 s390_cdb (code, s390_f15, 0, s390_r13, 4);
2842 s390_jl (code, 0); CODEPTR (code, o[0]);
2843 s390_sdb (code, s390_f15, 0, s390_r13, 12);
2844 s390_cfdbr (code, dreg, 7, s390_f15);
2846 PTRSLOT (code, o[0]);
2847 s390_cfdbr (code, dreg, 5, sreg);
2850 s390_lghi (code, s390_r0, 0xff);
2851 s390_ngr (code, dreg, s390_r0);
2854 s390_lghi (code, s390_r0, -1);
2855 s390_srlg (code, s390_r0, s390_r0, 0, 16);
2856 s390_ngr (code, dreg, s390_r0);
2863 /*========================= End of Function ========================*/
2865 /*------------------------------------------------------------------*/
2867 /* Name - gboolean_is_unsigned. */
2869 /* Function - Return TRUE if next opcode is checking for un- */
2872 /*------------------------------------------------------------------*/
2875 gboolean is_unsigned (MonoInst *next)
2878 (((next->opcode >= OP_IBNE_UN) &&
2879 (next->opcode <= OP_IBLT_UN)) ||
2880 ((next->opcode >= OP_LBNE_UN) &&
2881 (next->opcode <= OP_LBLT_UN)) ||
2882 ((next->opcode >= OP_COND_EXC_NE_UN) &&
2883 (next->opcode <= OP_COND_EXC_LT_UN)) ||
2884 ((next->opcode >= OP_COND_EXC_INE_UN) &&
2885 (next->opcode <= OP_COND_EXC_ILT_UN)) ||
2886 ((next->opcode == OP_CLT_UN) ||
2887 (next->opcode == OP_CGT_UN)) ||
2888 ((next->opcode == OP_ICLT_UN) ||
2889 (next->opcode == OP_ICGT_UN) ||
2890 (next->opcode == OP_LCLT_UN) ||
2891 (next->opcode == OP_LCGT_UN))))
2897 /*========================= End of Function ========================*/
2899 /*------------------------------------------------------------------*/
2901 /* Name - mono_arch_output_basic_block */
2903 /* Function - Perform the "real" work of emitting instructions */
2904 /* that will do the work of in the basic block. */
2906 /*------------------------------------------------------------------*/
2909 mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
2914 guint8 *code = cfg->native_code + cfg->code_len;
2915 guint last_offset = 0;
2916 int max_len, cpos, src2;
2918 /* we don't align basic blocks of loops on s390 */
2920 if (cfg->verbose_level > 2)
2921 g_print ("Basic block %d starting at offset 0x%x\n", bb->block_num, bb->native_offset);
2923 cpos = bb->max_offset;
2925 if (cfg->prof_options & MONO_PROFILE_COVERAGE) {
2926 //MonoCoverageInfo *cov = mono_get_coverage_info (cfg->method);
2927 //g_assert (!mono_compile_aot);
2930 // cov->data [bb->dfn].iloffset = bb->cil_code - cfg->cil_code;
2931 /* this is not thread save, but good enough */
2932 /* fixme: howto handle overflows? */
2933 //x86_inc_mem (code, &cov->data [bb->dfn].count);
2936 MONO_BB_FOR_EACH_INS (bb, ins) {
2937 offset = code - cfg->native_code;
2939 max_len = ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
2941 if (offset > (cfg->code_size - max_len - 16)) {
2942 cfg->code_size *= 2;
2943 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
2944 code = cfg->native_code + offset;
2947 mono_debug_record_line_number (cfg, ins, offset);
2949 switch (ins->opcode) {
2950 case OP_STOREI1_MEMBASE_IMM: {
2951 s390_lghi (code, s390_r0, ins->inst_imm);
2952 S390_LONG (code, stcy, stc, s390_r0, 0,
2953 ins->inst_destbasereg, ins->inst_offset);
2956 case OP_STOREI2_MEMBASE_IMM: {
2957 s390_lghi (code, s390_r0, ins->inst_imm);
2958 S390_LONG (code, sthy, sth, s390_r0, 0,
2959 ins->inst_destbasereg, ins->inst_offset);
2962 case OP_STOREI4_MEMBASE_IMM: {
2963 if (s390_is_imm16(ins->inst_imm)) {
2964 s390_lghi (code, s390_r0, ins->inst_imm);
2966 s390_basr (code, s390_r13, 0);
2968 s390_llong(code, ins->inst_imm);
2969 s390_lg (code, s390_r0, 0, s390_r13, 4);
2971 S390_LONG (code, sty, st, s390_r0, 0,
2972 ins->inst_destbasereg, ins->inst_offset);
2975 case OP_STORE_MEMBASE_IMM:
2976 case OP_STOREI8_MEMBASE_IMM: {
2977 if (s390_is_imm16(ins->inst_imm)) {
2978 s390_lghi (code, s390_r0, ins->inst_imm);
2980 s390_basr (code, s390_r13, 0);
2982 s390_llong(code, ins->inst_imm);
2983 s390_lg (code, s390_r0, 0, s390_r13, 4);
2985 S390_LONG (code, stg, stg, s390_r0, 0,
2986 ins->inst_destbasereg, ins->inst_offset);
2989 case OP_STOREI1_MEMBASE_REG: {
2990 S390_LONG (code, stcy, stc, ins->sreg1, 0,
2991 ins->inst_destbasereg, ins->inst_offset);
2994 case OP_STOREI2_MEMBASE_REG: {
2995 S390_LONG (code, sthy, sth, ins->sreg1, 0,
2996 ins->inst_destbasereg, ins->inst_offset);
2999 case OP_STOREI4_MEMBASE_REG: {
3000 S390_LONG (code, sty, st, ins->sreg1, 0,
3001 ins->inst_destbasereg, ins->inst_offset);
3004 case OP_STORE_MEMBASE_REG:
3005 case OP_STOREI8_MEMBASE_REG: {
3006 S390_LONG (code, stg, stg, ins->sreg1, 0,
3007 ins->inst_destbasereg, ins->inst_offset);
3011 g_assert_not_reached ();
3013 case OP_LOAD_MEMBASE:
3014 case OP_LOADI8_MEMBASE: {
3015 S390_LONG (code, lg, lg, ins->dreg, 0,
3016 ins->inst_basereg, ins->inst_offset);
3019 case OP_LOADI4_MEMBASE: {
3020 S390_LONG (code, lgf, lgf, ins->dreg, 0,
3021 ins->inst_basereg, ins->inst_offset);
3024 case OP_LOADU4_MEMBASE: {
3025 S390_LONG (code, llgf, llgf, ins->dreg, 0,
3026 ins->inst_basereg, ins->inst_offset);
3029 case OP_LOADU1_MEMBASE: {
3030 S390_LONG (code, llgc, llgc, ins->dreg, 0,
3031 ins->inst_basereg, ins->inst_offset);
3034 case OP_LOADI1_MEMBASE: {
3035 S390_LONG (code, lgb, lgb, ins->dreg, 0,
3036 ins->inst_basereg, ins->inst_offset);
3039 case OP_LOADU2_MEMBASE: {
3040 S390_LONG (code, llgh, llgh, ins->dreg, 0,
3041 ins->inst_basereg, ins->inst_offset);
3044 case OP_LOADI2_MEMBASE: {
3045 S390_LONG (code, lgh, lgh, ins->dreg, 0,
3046 ins->inst_basereg, ins->inst_offset);
3049 case OP_LCONV_TO_I1: {
3051 s390_lgbr (code, ins->dreg, ins->sreg1);
3053 s390_sllg (code, ins->dreg, ins->sreg1, 0, 56);
3054 s390_srag (code, ins->dreg, ins->dreg, 0, 56);
3058 case OP_LCONV_TO_I2: {
3060 s390_lghr (code, ins->dreg, ins->sreg1);
3062 s390_sllg (code, ins->dreg, ins->sreg1, 0, 48);
3063 s390_srag (code, ins->dreg, ins->dreg, 0, 48);
3067 case OP_LCONV_TO_U1: {
3069 s390_llghr (code, ins->dreg, ins->sreg1);
3071 if (ins->dreg != ins->sreg1)
3072 s390_lgr (code, ins->dreg, ins->sreg1);
3073 s390_lghi (code, s390_r0, 0xff);
3074 s390_ngr (code, ins->dreg, s390_r0);
3078 case OP_LCONV_TO_U2: {
3080 s390_llghr (code, ins->dreg, ins->sreg1);
3082 if (ins->dreg != ins->sreg1)
3083 s390_lgr (code, ins->dreg, ins->sreg1);
3084 s390_lghi (code, s390_r0, -1);
3085 s390_srlg (code, s390_r0, s390_r0, 0, 48);
3086 s390_ngr (code, ins->dreg, s390_r0);
3090 case OP_ICONV_TO_I1: {
3092 s390_lbr (code, ins->dreg, ins->sreg1);
3094 if (ins->dreg != ins->sreg1)
3095 s390_lr (code, ins->dreg, ins->sreg1);
3096 s390_sll (code, ins->dreg, 0, 24);
3097 s390_sra (code, ins->dreg, 0, 24);
3102 case OP_ICONV_TO_I2: {
3104 s390_lhr (code, ins->dreg, ins->sreg1);
3106 if (ins->dreg != ins->sreg1)
3107 s390_lr (code, ins->dreg, ins->sreg1);
3108 s390_sll (code, ins->dreg, 0, 16);
3109 s390_sra (code, ins->dreg, 0, 16);
3113 case OP_ICONV_TO_U1: {
3115 s390_llcr (code, ins->dreg, ins->sreg1);
3117 if (ins->dreg != ins->sreg1)
3118 s390_lr (code, ins->dreg, ins->sreg1);
3119 s390_lhi (code, s390_r0, 0xff);
3120 s390_nr (code, ins->dreg, s390_r0);
3124 case OP_ICONV_TO_U2: {
3126 s390_llhr (code, ins->dreg, ins->sreg1);
3128 if (ins->dreg != ins->sreg1)
3129 s390_lr (code, ins->dreg, ins->sreg1);
3130 s390_lhi (code, s390_r0, -1);
3131 s390_srl (code, s390_r0, 0, 16);
3132 s390_nr (code, ins->dreg, s390_r0);
3138 if (is_unsigned (ins->next))
3139 s390_clgr (code, ins->sreg1, ins->sreg2);
3141 s390_cgr (code, ins->sreg1, ins->sreg2);
3145 if (is_unsigned (ins->next))
3146 s390_clr (code, ins->sreg1, ins->sreg2);
3148 s390_cr (code, ins->sreg1, ins->sreg2);
3151 case OP_COMPARE_IMM:
3152 case OP_LCOMPARE_IMM: {
3153 if (s390_is_imm16 (ins->inst_imm)) {
3154 s390_lghi (code, s390_r0, ins->inst_imm);
3155 if (is_unsigned (ins->next))
3156 s390_clgr (code, ins->sreg1, s390_r0);
3158 s390_cgr (code, ins->sreg1, s390_r0);
3160 s390_basr (code, s390_r13, 0);
3162 s390_llong(code, ins->inst_imm);
3163 if (is_unsigned (ins->next))
3164 s390_clg (code, ins->sreg1, 0, s390_r13, 4);
3166 s390_cg (code, ins->sreg1, 0, s390_r13, 4);
3170 case OP_ICOMPARE_IMM: {
3171 if (s390_is_imm16 (ins->inst_imm)) {
3172 s390_lghi (code, s390_r0, ins->inst_imm);
3173 if (is_unsigned (ins->next))
3174 s390_clr (code, ins->sreg1, s390_r0);
3176 s390_cr (code, ins->sreg1, s390_r0);
3179 s390_basr (code, s390_r13, 0);
3181 s390_word (code, ins->inst_imm);
3182 if (is_unsigned (ins->next))
3183 s390_cl (code, ins->sreg1, 0, s390_r13, 4);
3185 s390_c (code, ins->sreg1, 0, s390_r13, 4);
3190 s390_basr (code, s390_r13, 0);
3192 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_ABS,
3194 s390_llong (code, mono_break);
3195 s390_lg (code, s390_r14, 0, s390_r13, 4);
3196 s390_basr (code, s390_r14, s390_r14);
3201 s390_agr (code, ins->dreg, src2);
3206 s390_agr (code, ins->dreg, src2);
3211 s390_alcgr (code, ins->dreg, src2);
3215 if (ins->dreg != ins->sreg1) {
3216 s390_lgr (code, ins->dreg, ins->sreg1);
3218 if (s390_is_imm16 (ins->inst_imm)) {
3219 s390_aghi (code, ins->dreg, ins->inst_imm);
3221 s390_basr (code, s390_r13, 0);
3223 s390_llong(code, ins->inst_imm);
3224 s390_ag (code, ins->dreg, 0, s390_r13, 4);
3229 if (ins->dreg != ins->sreg1) {
3230 s390_lgr (code, ins->dreg, ins->sreg1);
3232 g_assert (s390_is_imm16 (ins->inst_imm));
3233 s390_aghi (code, ins->dreg, ins->inst_imm);
3237 if (ins->dreg != ins->sreg1) {
3238 s390_lgr (code, ins->dreg, ins->sreg1);
3240 if (s390_is_imm16 (ins->inst_imm)) {
3241 s390_lghi (code, s390_r0, ins->inst_imm);
3242 s390_alcgr (code, ins->dreg, s390_r0);
3244 s390_basr (code, s390_r13, 0);
3246 s390_llong (code, ins->inst_imm);
3247 s390_lg (code, s390_r13, 0, s390_r13, 4);
3248 s390_alcgr (code, ins->dreg, s390_r13);
3253 case OP_S390_IADD_OVF: {
3255 s390_ar (code, ins->dreg, src2);
3256 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_OV, "OverflowException");
3257 s390_lgfr (code, ins->dreg, ins->dreg);
3260 case OP_IADD_OVF_UN:
3261 case OP_S390_IADD_OVF_UN: {
3263 s390_algr (code, ins->dreg, src2);
3264 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_CY, "OverflowException");
3265 s390_llgfr (code, ins->dreg, ins->dreg);
3268 case OP_ADD_OVF_CARRY: {
3270 s390_lghi (code, s390_r0, 0);
3271 s390_lgr (code, s390_r1, s390_r0);
3272 s390_alcgr (code, s390_r0, s390_r1);
3273 s390_agr (code, ins->dreg, src2);
3274 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_OV, "OverflowException");
3275 s390_agr (code, ins->dreg, s390_r0);
3276 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_OV, "OverflowException");
3279 case OP_ADD_OVF_UN_CARRY: {
3281 s390_alcgr (code, ins->dreg, src2);
3282 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_CY, "OverflowException");
3287 s390_sgr (code, ins->dreg, src2);
3292 s390_sgr (code, ins->dreg, src2);
3297 s390_slbgr(code, ins->dreg, src2);
3301 if (ins->dreg != ins->sreg1) {
3302 s390_lgr (code, ins->dreg, ins->sreg1);
3304 if (s390_is_imm16 (-ins->inst_imm)) {
3305 s390_lghi (code, s390_r0, ins->inst_imm);
3306 s390_slgr (code, ins->dreg, s390_r0);
3308 s390_basr (code, s390_r13, 0);
3310 s390_llong(code, ins->inst_imm);
3311 s390_slg (code, ins->dreg, 0, s390_r13, 4);
3316 if (ins->dreg != ins->sreg1) {
3317 s390_lgr (code, ins->dreg, ins->sreg1);
3319 if (s390_is_imm16 (-ins->inst_imm)) {
3320 s390_lghi (code, s390_r0, ins->inst_imm);
3321 s390_slgr (code, ins->dreg, s390_r0);
3323 s390_basr (code, s390_r13, 0);
3325 s390_llong(code, ins->inst_imm);
3326 s390_slg (code, ins->dreg, 0, s390_r13, 4);
3331 if (ins->dreg != ins->sreg1) {
3332 s390_lgr (code, ins->dreg, ins->sreg1);
3334 if (s390_is_imm16 (-ins->inst_imm)) {
3335 s390_lghi (code, s390_r0, ins->inst_imm);
3336 s390_slbgr (code, ins->dreg, s390_r0);
3338 s390_basr (code, s390_r13, 0);
3340 s390_llong(code, ins->inst_imm);
3341 s390_slbg (code, ins->dreg, 0, s390_r13, 4);
3345 case OP_SUB_OVF_CARRY: {
3347 s390_lghi (code, s390_r0, 0);
3348 s390_lgr (code, s390_r1, s390_r0);
3349 s390_slbgr (code, s390_r0, s390_r1);
3350 s390_sgr (code, ins->dreg, src2);
3351 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_OV, "OverflowException");
3352 s390_agr (code, ins->dreg, s390_r0);
3353 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_OV, "OverflowException");
3356 case OP_SUB_OVF_UN_CARRY: {
3358 s390_slbgr (code, ins->dreg, src2);
3359 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_NC, "OverflowException");
3363 if (ins->sreg1 == ins->dreg) {
3364 s390_ngr (code, ins->dreg, ins->sreg2);
3367 if (ins->sreg2 == ins->dreg) {
3368 s390_ngr (code, ins->dreg, ins->sreg1);
3371 s390_lgr (code, ins->dreg, ins->sreg1);
3372 s390_ngr (code, ins->dreg, ins->sreg2);
3378 if (ins->dreg != ins->sreg1) {
3379 s390_lgr (code, ins->dreg, ins->sreg1);
3381 if (s390_is_imm16 (ins->inst_imm)) {
3382 s390_lghi (code, s390_r0, ins->inst_imm);
3383 s390_ngr (code, ins->dreg, s390_r0);
3385 s390_basr (code, s390_r13, 0);
3387 s390_llong(code, ins->inst_imm);
3388 s390_ng (code, ins->dreg, 0, s390_r13, 4);
3393 s390_lgr (code, s390_r1, ins->sreg1);
3394 s390_dsgr (code, s390_r0, ins->sreg2);
3395 s390_lgr (code, ins->dreg, s390_r1);
3399 s390_lgr (code, s390_r1, ins->sreg1);
3400 s390_lghi (code, s390_r0, 0);
3401 s390_dlgr (code, s390_r0, ins->sreg2);
3402 s390_lgr (code, ins->dreg, s390_r1);
3406 s390_lgr (code, s390_r1, ins->sreg1);
3407 s390_dsgr (code, s390_r0, ins->sreg2);
3408 s390_lgr (code, ins->dreg, s390_r0);
3412 s390_lgr (code, s390_r1, ins->sreg1);
3413 s390_lghi (code, s390_r0, 0);
3414 s390_dlgr (code, s390_r0, ins->sreg2);
3415 s390_lgr (code, ins->dreg, s390_r0);
3419 if (ins->sreg1 == ins->dreg) {
3420 s390_ogr (code, ins->dreg, ins->sreg2);
3423 if (ins->sreg2 == ins->dreg) {
3424 s390_ogr (code, ins->dreg, ins->sreg1);
3427 s390_lgr (code, ins->dreg, ins->sreg1);
3428 s390_ogr (code, ins->dreg, ins->sreg2);
3434 if (ins->dreg != ins->sreg1) {
3435 s390_lgr (code, ins->dreg, ins->sreg1);
3437 if (s390_is_imm16 (ins->inst_imm)) {
3438 s390_lghi (code, s390_r0, ins->inst_imm);
3439 s390_ogr (code, ins->dreg, s390_r0);
3441 s390_basr (code, s390_r13, 0);
3443 s390_llong(code, ins->inst_imm);
3444 s390_og (code, ins->dreg, 0, s390_r13, 4);
3449 if (ins->sreg1 == ins->dreg) {
3450 s390_xgr (code, ins->dreg, ins->sreg2);
3453 if (ins->sreg2 == ins->dreg) {
3454 s390_xgr (code, ins->dreg, ins->sreg1);
3457 s390_lgr (code, ins->dreg, ins->sreg1);
3458 s390_xgr (code, ins->dreg, ins->sreg2);
3464 if (ins->dreg != ins->sreg1) {
3465 s390_lgr (code, ins->dreg, ins->sreg1);
3467 if (s390_is_imm16 (ins->inst_imm)) {
3468 s390_lghi (code, s390_r0, ins->inst_imm);
3469 s390_xgr (code, ins->dreg, s390_r0);
3471 s390_basr (code, s390_r13, 0);
3473 s390_llong(code, ins->inst_imm);
3474 s390_xg (code, ins->dreg, 0, s390_r13, 4);
3480 s390_sllg (code, ins->dreg, ins->dreg, src2, 0);
3485 if (ins->sreg1 != ins->dreg) {
3486 s390_lgr (code, ins->dreg, ins->sreg1);
3488 s390_sllg (code, ins->dreg, ins->dreg, 0, (ins->inst_imm & 0x3f));
3493 s390_srag (code, ins->dreg, ins->dreg, src2, 0);
3498 if (ins->sreg1 != ins->dreg) {
3499 s390_lgr (code, ins->dreg, ins->sreg1);
3501 s390_srag (code, ins->dreg, ins->dreg, 0, (ins->inst_imm & 0x3f));
3505 case OP_LSHR_UN_IMM: {
3506 if (ins->sreg1 != ins->dreg) {
3507 s390_lgr (code, ins->dreg, ins->sreg1);
3509 s390_srlg (code, ins->dreg, ins->dreg, 0, (ins->inst_imm & 0x3f));
3514 s390_srlg (code, ins->dreg, ins->dreg, src2, 0);
3518 if (ins->sreg1 != ins->dreg) {
3519 s390_lgr (code, ins->dreg, ins->sreg1);
3521 s390_lghi (code, s390_r0, -1);
3522 s390_xgr (code, ins->dreg, s390_r0);
3526 s390_lcgr (code, ins->dreg, ins->sreg1);
3531 s390_msgr (code, ins->dreg, src2);
3536 if (ins->dreg != ins->sreg1) {
3537 s390_lgr (code, ins->dreg, ins->sreg1);
3539 if (s390_is_imm16 (ins->inst_imm)) {
3540 s390_lghi (code, s390_r13, ins->inst_imm);
3542 s390_basr (code, s390_r13, 0);
3544 s390_llong(code, ins->inst_imm);
3545 s390_lg (code, s390_r13, 0, s390_r13, 4);
3547 s390_msgr (code, ins->dreg, s390_r13);
3552 s390_ltgr (code, s390_r1, ins->sreg1);
3553 s390_jz (code, 0); CODEPTR(code, o[0]);
3554 s390_ltgr (code, s390_r0, ins->sreg2);
3556 s390_lghi (code, s390_r1, 0);
3557 s390_j (code, 0); CODEPTR(code, o[1]);
3558 s390_xgr (code, s390_r0, s390_r1);
3559 s390_msgr (code, s390_r1, ins->sreg2);
3560 s390_xgr (code, s390_r0, s390_r1);
3561 s390_srlg (code, s390_r0, s390_r0, 0, 63);
3562 s390_ltgr (code, s390_r0, s390_r0);
3563 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_NZ, "OverflowException");
3564 PTRSLOT (code, o[0]);
3565 PTRSLOT (code, o[1]);
3566 s390_lgr (code, ins->dreg, s390_r1);
3569 case OP_LMUL_OVF_UN: {
3570 s390_lghi (code, s390_r0, 0);
3571 s390_lgr (code, s390_r1, ins->sreg1);
3572 s390_mlgr (code, s390_r0, ins->sreg2);
3573 s390_ltgr (code, s390_r0, s390_r0);
3574 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_NZ, "OverflowException");
3575 s390_lgr (code, ins->dreg, s390_r1);
3579 g_assert_not_reached ();
3581 s390_algr (code, ins->dreg, src2);
3586 s390_agr (code, ins->dreg, src2);
3590 g_assert_not_reached ();
3592 s390_alcgr (code, ins->dreg, src2);
3596 if (ins->dreg != ins->sreg1) {
3597 s390_lgfr (code, ins->dreg, ins->sreg1);
3599 if (s390_is_imm16 (ins->inst_imm)) {
3600 s390_aghi(code, ins->dreg, ins->inst_imm);
3602 s390_basr (code, s390_r13, 0);
3604 s390_word (code, ins->inst_imm);
3605 s390_agf (code, ins->dreg, 0, s390_r13, 4);
3610 if (ins->dreg != ins->sreg1) {
3611 s390_lgfr (code, ins->dreg, ins->sreg1);
3613 if (s390_is_imm16 (ins->inst_imm)) {
3614 s390_lghi (code, s390_r0, ins->inst_imm);
3615 s390_alcgr (code, ins->dreg, s390_r0);
3617 s390_basr (code, s390_r13, 0);
3619 s390_word (code, ins->inst_imm);
3620 s390_lgf (code, s390_r13, 0, s390_r13, 4);
3621 s390_alcgr (code, ins->dreg, s390_r13);
3626 case OP_S390_LADD_OVF: {
3628 s390_agr (code, ins->dreg, src2);
3629 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_OV, "OverflowException");
3632 case OP_LADD_OVF_UN:
3633 case OP_S390_LADD_OVF_UN: {
3635 s390_algr (code, ins->dreg, src2);
3636 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_CY, "OverflowException");
3640 CHECK_SRCDST_NCOM_I;
3641 s390_slgr (code, ins->dreg, src2);
3645 CHECK_SRCDST_NCOM_I;
3646 s390_sgr (code, ins->dreg, src2);
3650 CHECK_SRCDST_NCOM_I;
3651 s390_slbgr (code, ins->dreg, src2);
3655 if (ins->dreg != ins->sreg1) {
3656 s390_lgfr (code, ins->dreg, ins->sreg1);
3658 if (s390_is_imm16 (-ins->inst_imm)) {
3659 s390_aghi (code, ins->dreg, -ins->inst_imm);
3661 s390_basr (code, s390_r13, 0);
3663 s390_word (code, ins->inst_imm);
3664 s390_sgf (code, ins->dreg, 0, s390_r13, 4);
3669 s390_basr (code, s390_r13, 0);
3671 s390_word (code, ins->inst_imm);
3672 s390_slgf (code, ins->dreg, 0, s390_r13, 4);
3676 case OP_S390_ISUB_OVF: {
3678 s390_sr (code, ins->dreg, src2);
3679 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_OV, "OverflowException");
3680 s390_lgfr (code, ins->dreg, ins->dreg);
3683 case OP_ISUB_OVF_UN:
3684 case OP_S390_ISUB_OVF_UN: {
3686 s390_slr (code, ins->dreg, src2);
3687 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_NC, "OverflowException");
3688 s390_llgfr(code, ins->dreg, ins->dreg);
3692 case OP_S390_LSUB_OVF: {
3694 s390_sgr (code, ins->dreg, src2);
3695 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_OV, "OverflowException");
3698 case OP_LSUB_OVF_UN:
3699 case OP_S390_LSUB_OVF_UN: {
3701 s390_slgr (code, ins->dreg, src2);
3702 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_NC, "OverflowException");
3706 CHECK_SRCDST_NCOM_I;
3707 s390_ngr (code, ins->dreg, src2);
3711 if (ins->dreg != ins->sreg1) {
3712 s390_lgfr (code, ins->dreg, ins->sreg1);
3714 if (s390_is_imm16 (ins->inst_imm)) {
3715 s390_lghi (code, s390_r0, ins->inst_imm);
3716 s390_ngr (code, ins->dreg, s390_r0);
3718 s390_basr (code, s390_r13, 0);
3720 s390_llong(code, ins->inst_imm);
3721 s390_ng (code, ins->dreg, 0, s390_r13, 4);
3726 s390_lgfr (code, s390_r0, ins->sreg1);
3727 s390_srda (code, s390_r0, 0, 32);
3728 s390_dr (code, s390_r0, ins->sreg2);
3729 s390_lgfr (code, ins->dreg, s390_r1);
3733 s390_lgfr (code, s390_r0, ins->sreg1);
3734 s390_srdl (code, s390_r0, 0, 32);
3735 s390_dlr (code, s390_r0, ins->sreg2);
3736 s390_lgfr (code, ins->dreg, s390_r1);
3740 if (s390_is_imm16 (ins->inst_imm)) {
3741 s390_lghi (code, s390_r13, ins->inst_imm);
3742 s390_lgfr (code, s390_r0, ins->sreg1);
3744 s390_basr (code, s390_r13, 0);
3746 s390_word (code, ins->inst_imm);
3747 s390_lgfr (code, s390_r0, ins->sreg1);
3748 s390_lgf (code, s390_r13, 0, s390_r13, 4);
3750 s390_srda (code, s390_r0, 0, 32);
3751 s390_dr (code, s390_r0, ins->sreg2);
3752 s390_lgfr (code, ins->dreg, s390_r1);
3756 s390_lgfr (code, s390_r0, ins->sreg1);
3757 s390_srda (code, s390_r0, 0, 32);
3758 s390_dr (code, s390_r0, ins->sreg2);
3759 s390_lgfr (code, ins->dreg, s390_r0);
3762 s390_lgfr (code, s390_r0, ins->sreg1);
3763 s390_srdl (code, s390_r0, 0, 32);
3764 s390_dlr (code, s390_r0, ins->sreg2);
3765 s390_lgfr (code, ins->dreg, s390_r0);
3769 if (s390_is_imm16 (ins->inst_imm)) {
3770 s390_lghi (code, s390_r13, ins->inst_imm);
3771 s390_lgfr (code, s390_r0, ins->sreg1);
3773 s390_basr (code, s390_r13, 0);
3775 s390_word (code, ins->inst_imm);
3776 s390_lgfr (code, s390_r0, ins->sreg1);
3777 s390_lgf (code, s390_r13, 0, s390_r13, 4);
3779 s390_srda (code, s390_r0, 0, 32);
3780 s390_dr (code, s390_r0, ins->sreg2);
3781 s390_lgfr (code, ins->dreg, s390_r0);
3786 s390_ogr (code, ins->dreg, src2);
3790 if (ins->dreg != ins->sreg1) {
3791 s390_lgfr (code, ins->dreg, ins->sreg1);
3793 if (s390_is_imm16 (ins->inst_imm)) {
3794 s390_lghi (code, s390_r0, ins->inst_imm);
3795 s390_ogr (code, ins->dreg, s390_r0);
3797 s390_basr (code, s390_r13, 0);
3799 s390_llong(code, ins->inst_imm);
3800 s390_og (code, ins->dreg, 0, s390_r13, 4);
3806 s390_xgr (code, ins->dreg, src2);
3810 if (ins->dreg != ins->sreg1) {
3811 s390_lgfr (code, ins->dreg, ins->sreg1);
3813 if (s390_is_imm16 (ins->inst_imm)) {
3814 s390_lghi (code, s390_r0, ins->inst_imm);
3815 s390_xgr (code, ins->dreg, s390_r0);
3817 s390_basr (code, s390_r13, 0);
3819 s390_llong(code, ins->inst_imm);
3820 s390_xg (code, ins->dreg, 0, s390_r13, 4);
3826 s390_sll (code, ins->dreg, src2, 0);
3830 if (ins->sreg1 != ins->dreg) {
3831 s390_lgfr (code, ins->dreg, ins->sreg1);
3833 s390_sll (code, ins->dreg, 0, (ins->inst_imm & 0x1f));
3838 s390_sra (code, ins->dreg, src2, 0);
3842 if (ins->sreg1 != ins->dreg) {
3843 s390_lgfr (code, ins->dreg, ins->sreg1);
3845 s390_sra (code, ins->dreg, 0, (ins->inst_imm & 0x1f));
3848 case OP_ISHR_UN_IMM: {
3849 if (ins->sreg1 != ins->dreg) {
3850 s390_lgfr (code, ins->dreg, ins->sreg1);
3852 s390_srl (code, ins->dreg, 0, (ins->inst_imm & 0x1f));
3857 s390_srl (code, ins->dreg, src2, 0);
3861 if (ins->sreg1 != ins->dreg) {
3862 s390_lgfr (code, ins->dreg, ins->sreg1);
3864 s390_lghi (code, s390_r0, -1);
3865 s390_xgr (code, ins->dreg, s390_r0);
3869 s390_lcgr (code, ins->dreg, ins->sreg1);
3874 s390_msr (code, ins->dreg, src2);
3878 if (ins->dreg != ins->sreg1) {
3879 s390_lgfr (code, ins->dreg, ins->sreg1);
3881 if (s390_is_imm16 (ins->inst_imm)) {
3882 s390_lghi (code, s390_r13, ins->inst_imm);
3884 s390_basr (code, s390_r13, 0);
3886 s390_word (code, ins->inst_imm);
3887 s390_lgf (code, s390_r13, 0, s390_r13, 4);
3889 s390_msr (code, ins->dreg, s390_r13);
3894 s390_ltr (code, s390_r1, ins->sreg1);
3895 s390_jz (code, 0); CODEPTR(code, o[0]);
3896 s390_ltr (code, s390_r0, ins->sreg2);
3898 s390_lhi (code, s390_r1, 0);
3899 s390_j (code, 0); CODEPTR(code, o[1]);
3900 s390_xr (code, s390_r0, s390_r1);
3901 s390_msr (code, s390_r1, ins->sreg2);
3902 s390_xr (code, s390_r0, s390_r1);
3903 s390_srl (code, s390_r0, 0, 31);
3904 s390_ltr (code, s390_r0, s390_r0);
3905 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_NZ, "OverflowException");
3906 PTRSLOT (code, o[0]);
3907 PTRSLOT (code, o[1]);
3908 s390_lgfr (code, ins->dreg, s390_r1);
3911 case OP_IMUL_OVF_UN: {
3912 s390_lhi (code, s390_r0, 0);
3913 s390_lr (code, s390_r1, ins->sreg1);
3914 s390_mlr (code, s390_r0, ins->sreg2);
3915 s390_ltr (code, s390_r0, s390_r0);
3916 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_NZ, "OverflowException");
3917 s390_lgfr (code, ins->dreg, s390_r1);
3922 if (s390_is_imm16(ins->inst_c0)) {
3923 s390_lghi (code, ins->dreg, ins->inst_c0);
3925 s390_basr (code, s390_r13, 0);
3927 s390_llong(code, ins->inst_c0);
3928 s390_lg (code, ins->dreg, 0, s390_r13, 4);
3933 s390_basr (code, s390_r13, 0);
3935 mono_add_patch_info (cfg, code - cfg->native_code,
3936 (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
3937 s390_llong(code, 0);
3938 s390_lg (code,ins->dreg, 0, s390_r13, 4);
3941 case OP_JUMP_TABLE: {
3942 mono_add_patch_info (cfg, code - cfg->native_code,
3943 (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
3944 s390_basr (code, s390_r13, 0);
3946 s390_llong (code, 0);
3947 s390_lg (code, ins->dreg, 0, s390_r13, 4);
3951 if (ins->dreg != ins->sreg1) {
3952 s390_lgr (code, ins->dreg, ins->sreg1);
3956 case OP_LCONV_TO_I8:
3958 s390_lgfr (code, ins->dreg, ins->sreg1);
3960 case OP_LCONV_TO_I4:
3961 s390_lgfr (code, ins->dreg, ins->sreg1);
3964 case OP_LCONV_TO_U8:
3965 case OP_LCONV_TO_U4:
3967 s390_llgfr (code, ins->dreg, ins->sreg1);
3969 case OP_LCONV_TO_OVF_U4:
3970 s390_basr (code, s390_r13, 0);
3972 s390_llong(code, 4294967295);
3973 s390_clg (code, ins->sreg1, 0, s390_r13, 4);
3974 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_GT, "OverflowException");
3975 s390_ltgr (code, ins->sreg1, ins->sreg1);
3976 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_LT, "OverflowException");
3977 s390_llgfr(code, ins->dreg, ins->sreg1);
3979 case OP_LCONV_TO_OVF_I4_UN:
3980 s390_basr (code, s390_r13, 0);
3982 s390_llong(code, 2147483647);
3983 s390_cg (code, ins->sreg1, 0, s390_r13, 4);
3984 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_GT, "OverflowException");
3985 s390_ltgr (code, ins->sreg1, ins->sreg1);
3986 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_LT, "OverflowException");
3987 s390_lgfr (code, ins->dreg, ins->sreg1);
3990 case OP_FCONV_TO_R4: {
3991 if (ins->dreg != ins->sreg1) {
3992 s390_ldr (code, ins->dreg, ins->sreg1);
3996 case OP_S390_SETF4RET: {
3997 s390_ledbr (code, ins->dreg, ins->sreg1);
4001 if (cfg->method->save_lmf)
4002 restoreLMF(code, cfg->frame_reg, cfg->stack_usage);
4004 if (cfg->flags & MONO_CFG_HAS_TAIL) {
4005 code = emit_load_volatile_arguments (code, cfg);
4008 code = backUpStackPtr(cfg, code);
4009 s390_lg (code, s390_r14, 0, cfg->frame_reg, S390_RET_ADDR_OFFSET);
4010 mono_add_patch_info (cfg, code - cfg->native_code,
4011 MONO_PATCH_INFO_METHOD_JUMP,
4013 s390_jcl (code, S390_CC_UN, 0);
4016 case OP_CHECK_THIS: {
4017 /* ensure ins->sreg1 is not NULL */
4018 s390_lg (code, s390_r0, 0, ins->sreg1, 0);
4019 s390_ltgr (code, s390_r0, s390_r0);
4023 int offset = cfg->sig_cookie + cfg->stack_usage;
4025 if (s390_is_imm16 (offset))
4026 s390_lghi (code, s390_r0, offset);
4028 s390_basr (code, s390_r13, 0);
4030 s390_llong(code, offset);
4031 s390_lg (code, s390_r0, 0, s390_r13, 0);
4033 s390_agr (code, s390_r0, cfg->frame_reg);
4034 s390_stg (code, s390_r0, 0, ins->sreg1, 0);
4038 s390_basr (code, s390_r13, 0);
4040 call = (MonoCallInst*)ins;
4041 if (ins->flags & MONO_INST_HAS_METHOD)
4042 mono_add_patch_info (cfg, code-cfg->native_code,
4043 MONO_PATCH_INFO_METHOD,
4046 mono_add_patch_info (cfg, code-cfg->native_code,
4047 MONO_PATCH_INFO_ABS,
4049 s390_llong(code, 0);
4050 s390_lg (code, s390_r14, 0, s390_r13, 4);
4051 s390_basr (code, s390_r14, s390_r14);
4052 if (call->signature->ret->type == MONO_TYPE_R4)
4053 s390_ldebr (code, s390_f0, s390_f0);
4061 s390_basr (code, s390_r13, 0);
4063 call = (MonoCallInst*)ins;
4064 if (ins->flags & MONO_INST_HAS_METHOD)
4065 mono_add_patch_info (cfg, code-cfg->native_code,
4066 MONO_PATCH_INFO_METHOD,
4069 mono_add_patch_info (cfg, code-cfg->native_code,
4070 MONO_PATCH_INFO_ABS,
4072 s390_llong(code, 0);
4073 s390_lg (code, s390_r14, 0, s390_r13, 4);
4074 s390_basr (code, s390_r14, s390_r14);
4077 case OP_FCALL_REG: {
4078 call = (MonoCallInst*)ins;
4079 s390_lgr (code, s390_r1, ins->sreg1);
4080 s390_basr (code, s390_r14, s390_r1);
4081 if (call->signature->ret->type == MONO_TYPE_R4)
4082 s390_ldebr (code, s390_f0, s390_f0);
4088 case OP_VOIDCALL_REG:
4090 s390_lgr (code, s390_r1, ins->sreg1);
4091 s390_basr (code, s390_r14, s390_r1);
4094 case OP_FCALL_MEMBASE: {
4095 call = (MonoCallInst*)ins;
4096 s390_lg (code, s390_r1, 0, ins->sreg1, ins->inst_offset);
4097 s390_basr (code, s390_r14, s390_r1);
4098 if (call->signature->ret->type == MONO_TYPE_R4)
4099 s390_ldebr (code, s390_f0, s390_f0);
4102 case OP_LCALL_MEMBASE:
4103 case OP_VCALL_MEMBASE:
4104 case OP_VCALL2_MEMBASE:
4105 case OP_VOIDCALL_MEMBASE:
4106 case OP_CALL_MEMBASE: {
4107 s390_lg (code, s390_r1, 0, ins->sreg1, ins->inst_offset);
4108 s390_basr (code, s390_r14, s390_r1);
4115 if (cfg->param_area == 0)
4116 alloca_skip = S390_MINIMAL_STACK_SIZE;
4118 alloca_skip = cfg->param_area;
4120 area_offset = S390_ALIGN(alloca_skip, S390_STACK_ALIGNMENT);
4121 s390_lgr (code, s390_r1, ins->sreg1);
4122 if (ins->flags & MONO_INST_INIT)
4123 s390_lgr (code, s390_r0, ins->sreg1);
4124 s390_aghi (code, s390_r1, 14);
4125 s390_srlg (code, s390_r1, s390_r1, 0, 3);
4126 s390_sllg (code, s390_r1, s390_r1, 0, 3);
4127 if (cfg->method->save_lmf) {
4128 /*----------------------------------*/
4129 /* we have to adjust lmf ebp value */
4130 /*----------------------------------*/
4131 int lmfOffset = cfg->stack_usage - sizeof(MonoLMF);
4133 s390_lgr (code, s390_r13, cfg->frame_reg);
4134 if (s390_is_imm16(lmfOffset))
4135 s390_aghi (code, s390_r13, lmfOffset);
4137 s390_basr (code, s390_r14, 0);
4139 s390_word (code, lmfOffset);
4140 s390_agf (code, s390_r13, 0, s390_r14, 4);
4142 s390_lgr (code, s390_r14, STK_BASE);
4143 s390_sgr (code, s390_r14, s390_r1);
4144 s390_stg (code, s390_r14, 0, s390_r13,
4145 G_STRUCT_OFFSET(MonoLMF, ebp));
4147 s390_lg (code, s390_r13, 0, STK_BASE, 0);
4148 s390_sgr (code, STK_BASE, s390_r1);
4149 s390_stg (code, s390_r13, 0, STK_BASE, 0);
4150 s390_la (code, ins->dreg, 0, STK_BASE, area_offset);
4151 s390_srlg (code, ins->dreg, ins->dreg, 0, 3);
4152 s390_sllg (code, ins->dreg, ins->dreg, 0, 3);
4153 if (ins->flags & MONO_INST_INIT) {
4154 s390_lgr (code, s390_r1, s390_r0);
4155 s390_lgr (code, s390_r0, ins->dreg);
4156 s390_lgr (code, s390_r14, s390_r12);
4157 s390_lghi (code, s390_r13, 0);
4158 s390_mvcle(code, s390_r0, s390_r12, 0, 0);
4160 s390_lgr (code, s390_r12, s390_r14);
4165 s390_lgr (code, s390_r2, ins->sreg1);
4166 s390_basr (code, s390_r13, 0);
4168 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
4169 (gpointer) "mono_arch_throw_exception");
4170 s390_llong(code, 0);
4171 s390_lg (code, s390_r14, 0, s390_r13, 4);
4172 s390_basr (code, s390_r14, s390_r14);
4176 s390_lgr (code, s390_r2, ins->sreg1);
4177 s390_basr (code, s390_r13, 0);
4179 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
4180 (gpointer) "mono_arch_rethrow_exception");
4181 s390_llong(code, 0);
4182 s390_lg (code, s390_r14, 0, s390_r13, 4);
4183 s390_basr (code, s390_r14, s390_r14);
4186 case OP_START_HANDLER: {
4187 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
4189 S390_LONG (code, stg, stg, s390_r14, 0,
4190 spvar->inst_basereg,
4191 spvar->inst_offset);
4194 case OP_ENDFILTER: {
4195 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
4197 if (ins->sreg1 != s390_r2)
4198 s390_lgr(code, s390_r2, ins->sreg1);
4199 S390_LONG (code, lg, lg, s390_r14, 0,
4200 spvar->inst_basereg,
4201 spvar->inst_offset);
4202 s390_br (code, s390_r14);
4205 case OP_ENDFINALLY: {
4206 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
4208 S390_LONG (code, lg, lg, s390_r14, 0,
4209 spvar->inst_basereg,
4210 spvar->inst_offset);
4211 s390_br (code, s390_r14);
4214 case OP_CALL_HANDLER: {
4215 mono_add_patch_info (cfg, code-cfg->native_code,
4216 MONO_PATCH_INFO_BB, ins->inst_target_bb);
4217 s390_brasl (code, s390_r14, 0);
4218 mono_cfg_add_try_hole (cfg, ins->inst_eh_block, code, bb);
4222 ins->inst_c0 = code - cfg->native_code;
4225 case OP_RELAXED_NOP:
4228 case OP_DUMMY_STORE:
4229 case OP_NOT_REACHED:
4233 case OP_SEQ_POINT: {
4236 if (cfg->compile_aot)
4240 * Read from the single stepping trigger page. This will cause a
4241 * SIGSEGV when single stepping is enabled.
4242 * We do this _before_ the breakpoint, so single stepping after
4243 * a breakpoint is hit will step to the next IL offset.
4245 if (ins->flags & MONO_INST_SINGLE_STEP_LOC) {
4246 breakpointCode.pTrigger = ss_trigger_page;
4247 memcpy(code, (void *) &breakpointCode, BREAKPOINT_SIZE);
4248 code += BREAKPOINT_SIZE;
4251 mono_add_seq_point (cfg, bb, ins, code - cfg->native_code);
4254 * A placeholder for a possible breakpoint inserted by
4255 * mono_arch_set_breakpoint ().
4257 for (i = 0; i < (BREAKPOINT_SIZE / S390X_NOP_SIZE); ++i)
4263 EMIT_UNCOND_BRANCH(ins);
4266 s390_br (code, ins->sreg1);
4272 s390_lghi(code, ins->dreg, 1);
4274 s390_lghi(code, ins->dreg, 0);
4280 s390_lghi(code, ins->dreg, 1);
4282 s390_lghi(code, ins->dreg, 0);
4288 s390_lghi(code, ins->dreg, 1);
4290 s390_lghi(code, ins->dreg, 0);
4296 s390_lghi(code, ins->dreg, 1);
4298 s390_lghi(code, ins->dreg, 0);
4304 s390_lghi(code, ins->dreg, 1);
4306 s390_lghi(code, ins->dreg, 0);
4309 case OP_COND_EXC_EQ:
4310 case OP_COND_EXC_IEQ:
4311 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_EQ, ins->inst_p1);
4313 case OP_COND_EXC_NE_UN:
4314 case OP_COND_EXC_INE_UN:
4315 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_NE, ins->inst_p1);
4317 case OP_COND_EXC_LT:
4318 case OP_COND_EXC_ILT:
4319 case OP_COND_EXC_LT_UN:
4320 case OP_COND_EXC_ILT_UN:
4321 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_LT, ins->inst_p1);
4323 case OP_COND_EXC_GT:
4324 case OP_COND_EXC_IGT:
4325 case OP_COND_EXC_GT_UN:
4326 case OP_COND_EXC_IGT_UN:
4327 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_GT, ins->inst_p1);
4329 case OP_COND_EXC_GE:
4330 case OP_COND_EXC_IGE:
4331 case OP_COND_EXC_GE_UN:
4332 case OP_COND_EXC_IGE_UN:
4333 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_GE, ins->inst_p1);
4335 case OP_COND_EXC_LE:
4336 case OP_COND_EXC_ILE:
4337 case OP_COND_EXC_LE_UN:
4338 case OP_COND_EXC_ILE_UN:
4339 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_LE, ins->inst_p1);
4341 case OP_COND_EXC_OV:
4342 case OP_COND_EXC_IOV:
4343 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_OV, ins->inst_p1);
4345 case OP_COND_EXC_NO:
4346 case OP_COND_EXC_INO:
4347 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_NO, ins->inst_p1);
4350 case OP_COND_EXC_IC:
4351 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_CY, ins->inst_p1);
4353 case OP_COND_EXC_NC:
4354 case OP_COND_EXC_INC:
4355 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_NC, ins->inst_p1);
4359 EMIT_COND_BRANCH (ins, S390_CC_EQ);
4363 EMIT_COND_BRANCH (ins, S390_CC_NE);
4369 EMIT_COND_BRANCH (ins, S390_CC_LT);
4375 EMIT_COND_BRANCH (ins, S390_CC_GT);
4381 EMIT_COND_BRANCH (ins, S390_CC_GE);
4387 EMIT_COND_BRANCH (ins, S390_CC_LE);
4390 /* floating point opcodes */
4392 if (*((float *) ins->inst_p0) == 0) {
4393 s390_lzdr (code, ins->dreg);
4395 s390_basr (code, s390_r13, 0);
4397 s390_llong (code, ins->inst_p0);
4398 s390_lg (code, s390_r13, 0, s390_r13, 4);
4399 s390_ld (code, ins->dreg, 0, s390_r13, 0);
4404 if (*((float *) ins->inst_p0) == 0) {
4405 s390_lzdr (code, ins->dreg);
4407 s390_basr (code, s390_r13, 0);
4409 s390_llong(code, ins->inst_p0);
4410 s390_lg (code, s390_r13, 0, s390_r13, 4);
4411 s390_ldeb (code, ins->dreg, 0, s390_r13, 0);
4415 case OP_STORER8_MEMBASE_REG: {
4416 S390_LONG (code, stdy, std, ins->sreg1, 0,
4417 ins->inst_destbasereg, ins->inst_offset);
4420 case OP_LOADR8_MEMBASE: {
4421 S390_LONG (code, ldy, ld, ins->dreg, 0,
4422 ins->inst_basereg, ins->inst_offset);
4425 case OP_STORER4_MEMBASE_REG: {
4426 s390_ledbr (code, s390_f15, ins->sreg1);
4427 S390_LONG (code, stey, ste, s390_f15, 0,
4428 ins->inst_destbasereg, ins->inst_offset);
4431 case OP_LOADR4_MEMBASE: {
4432 S390_LONG (code, ldy, ld, s390_f15, 0,
4433 ins->inst_basereg, ins->inst_offset);
4434 s390_ldebr (code, ins->dreg, s390_f15);
4437 case OP_ICONV_TO_R_UN: {
4438 s390_cdfbr (code, ins->dreg, ins->sreg1);
4439 s390_ltr (code, ins->sreg1, ins->sreg1);
4440 s390_jnl (code, 12);
4441 s390_basr (code, s390_r13, 0);
4443 s390_word (code, 0x41f00000);
4444 s390_word (code, 0);
4445 s390_adb (code, ins->dreg, 0, s390_r13, 4);
4448 case OP_LCONV_TO_R_UN: {
4449 s390_cdgbr (code, ins->dreg, ins->sreg1);
4450 s390_ltgr (code, ins->sreg1, ins->sreg1);
4451 s390_jnl (code, 12);
4452 s390_basr (code, s390_r13, 0);
4454 s390_word (code, 0x41f00000);
4455 s390_word (code, 0);
4456 s390_adb (code, ins->dreg, 0, s390_r13, 4);
4459 case OP_LCONV_TO_R4:
4460 case OP_ICONV_TO_R4: {
4461 s390_cdgbr (code, ins->dreg, ins->sreg1);
4464 case OP_LCONV_TO_R8:
4465 case OP_ICONV_TO_R8: {
4466 s390_cdgbr (code, ins->dreg, ins->sreg1);
4469 case OP_FCONV_TO_I1:
4470 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 1, TRUE);
4472 case OP_FCONV_TO_U1:
4473 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 1, FALSE);
4475 case OP_FCONV_TO_I2:
4476 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 2, TRUE);
4478 case OP_FCONV_TO_U2:
4479 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 2, FALSE);
4481 case OP_FCONV_TO_I4:
4483 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 4, TRUE);
4485 case OP_FCONV_TO_U4:
4487 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 4, FALSE);
4489 case OP_FCONV_TO_I8:
4490 s390_cgdbr (code, ins->dreg, 5, ins->sreg1);
4492 case OP_LCONV_TO_OVF_I: {
4493 /* Valid ints: 0xffffffff:8000000 to 00000000:0x7f000000 */
4495 s390_ltgr (code, ins->sreg2, ins->sreg2);
4496 s390_jnl (code, 0); CODEPTR(code, o[0]);
4497 s390_ltgr (code, ins->sreg1, ins->sreg1);
4498 s390_jnl (code, 0); CODEPTR(code, o[1]);
4499 s390_lhi (code, s390_r13, -1);
4500 s390_cgr (code, ins->sreg1, s390_r13);
4501 s390_jnz (code, 0); CODEPTR(code, o[2]);
4502 if (ins->dreg != ins->sreg2)
4503 s390_lgr (code, ins->dreg, ins->sreg2);
4504 s390_j (code, 0); CODEPTR(code, o[3]);
4505 PTRSLOT(code, o[0]);
4506 s390_jz (code, 0); CODEPTR(code, o[4]);
4507 PTRSLOT(code, o[1]);
4508 PTRSLOT(code, o[2]);
4509 mono_add_patch_info (cfg, code - cfg->native_code,
4510 MONO_PATCH_INFO_EXC, "OverflowException");
4511 s390_brasl (code, s390_r14, 0);
4512 PTRSLOT(code, o[3]);
4513 PTRSLOT(code, o[4]);
4517 s390_lpdbr (code, ins->dreg, ins->sreg1);
4521 s390_sqdbr (code, ins->dreg, ins->sreg1);
4526 s390_adbr (code, ins->dreg, src2);
4530 CHECK_SRCDST_NCOM_F;
4531 s390_sdbr (code, ins->dreg, src2);
4536 s390_mdbr (code, ins->dreg, src2);
4540 CHECK_SRCDST_NCOM_F;
4541 s390_ddbr (code, ins->dreg, src2);
4545 s390_lcdbr (code, ins->dreg, ins->sreg1);
4549 CHECK_SRCDST_NCOM_F;
4550 s390_didbr (code, ins->dreg, src2, 5, s390_f15);
4554 s390_cdbr (code, ins->sreg1, ins->sreg2);
4558 s390_cdbr (code, ins->sreg1, ins->sreg2);
4559 s390_lghi (code, ins->dreg, 1);
4561 s390_lghi (code, ins->dreg, 0);
4565 s390_cdbr (code, ins->sreg1, ins->sreg2);
4566 s390_lghi (code, ins->dreg, 1);
4568 s390_lghi (code, ins->dreg, 0);
4572 s390_cdbr (code, ins->sreg1, ins->sreg2);
4573 s390_lghi (code, ins->dreg, 1);
4575 s390_lghi (code, ins->dreg, 0);
4579 s390_cdbr (code, ins->sreg1, ins->sreg2);
4580 s390_lghi (code, ins->dreg, 1);
4582 s390_lghi (code, ins->dreg, 0);
4586 s390_cdbr (code, ins->sreg1, ins->sreg2);
4587 s390_lghi (code, ins->dreg, 1);
4589 s390_lghi (code, ins->dreg, 0);
4594 s390_jo (code, 0); CODEPTR(code, o);
4595 EMIT_COND_BRANCH (ins, S390_CC_EQ);
4600 EMIT_COND_BRANCH (ins, S390_CC_NE|S390_CC_OV);
4604 s390_jo (code, 0); CODEPTR(code, o);
4605 EMIT_COND_BRANCH (ins, S390_CC_LT);
4610 EMIT_COND_BRANCH (ins, S390_CC_LT|S390_CC_OV);
4614 s390_jo (code, 0); CODEPTR(code, o);
4615 EMIT_COND_BRANCH (ins, S390_CC_GT);
4620 EMIT_COND_BRANCH (ins, S390_CC_GT|S390_CC_OV);
4624 s390_jo (code, 0); CODEPTR(code, o);
4625 EMIT_COND_BRANCH (ins, S390_CC_GE);
4630 EMIT_COND_BRANCH (ins, S390_CC_GE|S390_CC_OV);
4634 s390_jo (code, 0); CODEPTR(code, o);
4635 EMIT_COND_BRANCH (ins, S390_CC_LE);
4640 EMIT_COND_BRANCH (ins, S390_CC_LE|S390_CC_OV);
4644 s390_lhi (code, s390_r13, 0x7f);
4645 s390_tcdb (code, ins->sreg1, 0, s390_r13, 0);
4646 s390_jz (code, 0); CODEPTR(code, o);
4647 mono_add_patch_info (cfg, code - cfg->native_code,
4648 MONO_PATCH_INFO_EXC, "ArithmeticException");
4649 s390_brasl (code, s390_r14,0);
4653 case OP_S390_MOVE: {
4654 if (ins->backend.size > 0) {
4655 if (ins->backend.size <= 256) {
4656 s390_mvc (code, ins->backend.size, ins->dreg,
4657 ins->inst_offset, ins->sreg1, ins->inst_imm);
4659 s390_lgr (code, s390_r0, ins->dreg);
4660 if (ins->inst_offset > 0) {
4661 if (s390_is_imm16 (ins->inst_offset)) {
4662 s390_aghi (code, s390_r0, ins->inst_offset);
4664 s390_basr (code, s390_r13, 0);
4666 s390_llong(code, ins->inst_offset);
4667 s390_ag (code, s390_r0, 0, s390_r13, 4);
4670 s390_lgr (code, s390_r12, ins->sreg1);
4671 if (ins->inst_imm > 0) {
4672 if (s390_is_imm16 (ins->inst_imm)) {
4673 s390_aghi (code, s390_r12, ins->inst_imm);
4675 s390_basr (code, s390_r13, 0);
4677 s390_llong(code, ins->inst_imm);
4678 s390_ag (code, s390_r12, 0, s390_r13, 4);
4681 if (s390_is_imm16 (ins->backend.size)) {
4682 s390_lghi (code, s390_r1, ins->backend.size);
4684 s390_basr (code, s390_r13, 0);
4686 s390_llong(code, ins->backend.size);
4687 s390_lg (code, s390_r1, 0, s390_r13, 4);
4689 s390_lgr (code, s390_r13, s390_r1);
4690 s390_mvcle(code, s390_r0, s390_r12, 0, 0);
4696 case OP_ATOMIC_ADD_I8: {
4697 s390_lgr (code, s390_r1, ins->sreg2);
4698 s390_lg (code, s390_r0, 0, ins->inst_basereg, ins->inst_offset);
4699 s390_agr (code, s390_r1, s390_r0);
4700 s390_csg (code, s390_r0, s390_r1, ins->inst_basereg, ins->inst_offset);
4701 s390_jnz (code, -10);
4702 s390_lgr (code, ins->dreg, s390_r1);
4705 case OP_ATOMIC_ADD_NEW_I8: {
4706 s390_lgr (code, s390_r1, ins->sreg2);
4707 s390_lg (code, s390_r0, 0, ins->inst_basereg, ins->inst_offset);
4708 s390_agr (code, s390_r1, s390_r0);
4709 s390_csg (code, s390_r0, s390_r1, ins->inst_basereg, ins->inst_offset);
4710 s390_jnz (code, -10);
4711 s390_lgr (code, ins->dreg, s390_r1);
4714 case OP_ATOMIC_EXCHANGE_I8: {
4715 s390_lg (code, s390_r0, 0, ins->inst_basereg, ins->inst_offset);
4716 s390_csg (code, s390_r0, ins->sreg2, ins->inst_basereg, ins->inst_offset);
4717 s390_jnz (code, -6);
4718 s390_lgr (code, ins->dreg, s390_r0);
4721 case OP_ATOMIC_ADD_I4: {
4722 s390_lgfr(code, s390_r1, ins->sreg2);
4723 s390_lgf (code, s390_r0, 0, ins->inst_basereg, ins->inst_offset);
4724 s390_agr (code, s390_r1, s390_r0);
4725 s390_cs (code, s390_r0, s390_r1, ins->inst_basereg, ins->inst_offset);
4726 s390_jnz (code, -9);
4727 s390_lgfr(code, ins->dreg, s390_r1);
4730 case OP_ATOMIC_ADD_NEW_I4: {
4731 s390_lgfr(code, s390_r1, ins->sreg2);
4732 s390_lgf (code, s390_r0, 0, ins->inst_basereg, ins->inst_offset);
4733 s390_agr (code, s390_r1, s390_r0);
4734 s390_cs (code, s390_r0, s390_r1, ins->inst_basereg, ins->inst_offset);
4735 s390_jnz (code, -9);
4736 s390_lgfr(code, ins->dreg, s390_r1);
4739 case OP_ATOMIC_EXCHANGE_I4: {
4740 s390_l (code, s390_r0, 0, ins->inst_basereg, ins->inst_offset);
4741 s390_cs (code, s390_r0, ins->sreg2, ins->inst_basereg, ins->inst_offset);
4742 s390_jnz (code, -4);
4743 s390_lgfr(code, ins->dreg, s390_r0);
4746 case OP_S390_BKCHAIN: {
4747 s390_lgr (code, ins->dreg, ins->sreg1);
4748 if (s390_is_imm16 (cfg->stack_offset)) {
4749 s390_aghi (code, ins->dreg, cfg->stack_offset);
4751 s390_basr (code, s390_r13, 0);
4753 s390_llong(code, cfg->stack_offset);
4754 s390_ag (code, ins->dreg, 0, s390_r13, 4);
4758 case OP_MEMORY_BARRIER: {
4762 g_warning ("unknown opcode %s in %s()\n", mono_inst_name (ins->opcode), __FUNCTION__);
4763 g_assert_not_reached ();
4766 if ((cfg->opt & MONO_OPT_BRANCH) && ((code - cfg->native_code - offset) > max_len)) {
4767 g_warning ("wrong maximal instruction length of instruction %s (expected %d, got %ld)",
4768 mono_inst_name (ins->opcode), max_len, code - cfg->native_code - offset);
4769 g_assert_not_reached ();
4774 last_offset = offset;
4777 cfg->code_len = code - cfg->native_code;
4780 /*========================= End of Function ========================*/
4782 /*------------------------------------------------------------------*/
4784 /* Name - mono_arch_register_lowlevel_calls */
4786 /* Function - Register routines to help with --trace operation. */
4788 /*------------------------------------------------------------------*/
4791 mono_arch_register_lowlevel_calls (void)
4793 mono_register_jit_icall (mono_arch_get_lmf_addr, "mono_arch_get_lmf_addr", NULL, TRUE);
4796 /*========================= End of Function ========================*/
4798 /*------------------------------------------------------------------*/
4800 /* Name - mono_arch_patch_code */
4802 /* Function - Process the patch data created during the */
4803 /* instruction build process. This resolves jumps, */
4804 /* calls, variables etc. */
4806 /*------------------------------------------------------------------*/
4809 mono_arch_patch_code (MonoMethod *method, MonoDomain *domain,
4810 guint8 *code, MonoJumpInfo *ji, MonoCodeManager *dyn_code_mp, gboolean run_cctors)
4812 MonoJumpInfo *patch_info;
4814 for (patch_info = ji; patch_info; patch_info = patch_info->next) {
4815 unsigned char *ip = patch_info->ip.i + code;
4816 gconstpointer target = NULL;
4818 target = mono_resolve_patch_target (method, domain, code,
4819 patch_info, run_cctors);
4821 switch (patch_info->type) {
4822 case MONO_PATCH_INFO_IP:
4823 case MONO_PATCH_INFO_EXC_NAME:
4824 case MONO_PATCH_INFO_LDSTR:
4825 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
4826 case MONO_PATCH_INFO_LDTOKEN:
4827 case MONO_PATCH_INFO_EXC:
4828 case MONO_PATCH_INFO_ABS:
4829 case MONO_PATCH_INFO_METHOD:
4830 case MONO_PATCH_INFO_INTERNAL_METHOD:
4831 case MONO_PATCH_INFO_CLASS_INIT:
4832 s390_patch_addr (ip, (guint64) target);
4834 case MONO_PATCH_INFO_SWITCH:
4835 /*----------------------------------*/
4836 /* ip points at the basr r13,0/j +4 */
4837 /* instruction the vtable value */
4838 /* follows this (i.e. ip+6) */
4839 /*----------------------------------*/
4840 *((gconstpointer *)(ip+6)) = target;
4842 case MONO_PATCH_INFO_METHODCONST:
4843 case MONO_PATCH_INFO_CLASS:
4844 case MONO_PATCH_INFO_IMAGE:
4845 case MONO_PATCH_INFO_FIELD:
4846 case MONO_PATCH_INFO_IID:
4847 target = S390_RELATIVE(target, ip);
4848 s390_patch_rel (ip, (guint64) target);
4850 case MONO_PATCH_INFO_R4:
4851 case MONO_PATCH_INFO_R8:
4852 case MONO_PATCH_INFO_METHOD_REL:
4853 g_assert_not_reached ();
4856 target = S390_RELATIVE(target, ip);
4858 s390_patch_rel (ip, (guint64) target);
4863 /*========================= End of Function ========================*/
4865 /*------------------------------------------------------------------*/
4867 /* Name - emit_load_volatile_arguments */
4869 /* Function - Emit the instructions to reload parameter regist- */
4870 /* registers for use with "tail" operations. */
4872 /* The register loading operations performed here */
4873 /* are the mirror of the store operations performed */
4874 /* in mono_arch_emit_prolog and need to be kept in */
4875 /* synchronization with it. */
4877 /*------------------------------------------------------------------*/
4880 emit_load_volatile_arguments (guint8 *code, MonoCompile *cfg)
4883 MonoMethod *method = cfg->method;
4884 MonoMethodSignature *sig = mono_method_signature(method);
4888 cinfo = get_call_info (NULL, NULL, sig, sig->pinvoke);
4890 if (cinfo->struct_ret) {
4891 ArgInfo *ainfo = &cinfo->ret;
4892 inst = cfg->vret_addr;
4893 s390_lg (code, ainfo->reg, 0, inst->inst_basereg, inst->inst_offset);
4896 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
4897 ArgInfo *ainfo = cinfo->args + i;
4898 inst = cfg->args [pos];
4900 if (inst->opcode == OP_REGVAR) {
4901 if (ainfo->regtype == RegTypeGeneral)
4902 s390_lgr (code, ainfo->reg, inst->dreg);
4903 else if (ainfo->regtype == RegTypeFP) {
4904 if (inst->dreg != ainfo->reg) {
4905 if (ainfo->size == 4) {
4906 s390_ldebr (code, ainfo->reg, inst->dreg);
4908 s390_ldr (code, ainfo->reg, inst->dreg);
4912 else if (ainfo->regtype == RegTypeBase) {
4914 g_assert_not_reached ();
4916 if (ainfo->regtype == RegTypeGeneral) {
4917 if (!((ainfo->reg >= 2) && (ainfo->reg <= 6)))
4918 g_assert_not_reached();
4919 switch (ainfo->size) {
4921 s390_llgc (code, ainfo->reg, 0, inst->inst_basereg, inst->inst_offset);
4924 s390_lgh (code, ainfo->reg, 0, inst->inst_basereg, inst->inst_offset);
4927 s390_lgf (code, ainfo->reg, 0, inst->inst_basereg, inst->inst_offset);
4930 s390_lg (code, ainfo->reg, 0, inst->inst_basereg, inst->inst_offset);
4933 } else if (ainfo->regtype == RegTypeBase) {
4934 } else if (ainfo->regtype == RegTypeFP) {
4935 if (ainfo->size == 8)
4936 s390_ld (code, ainfo->reg, 0, inst->inst_basereg, inst->inst_offset);
4937 else if (ainfo->size == 4)
4938 s390_le (code, ainfo->reg, 0, inst->inst_basereg, inst->inst_offset);
4940 g_assert_not_reached ();
4941 } else if (ainfo->regtype == RegTypeStructByVal) {
4942 if (ainfo->reg != STK_BASE) {
4943 switch (ainfo->size) {
4945 s390_llgc (code, ainfo->reg, 0, inst->inst_basereg, inst->inst_offset);
4948 s390_lgh (code, ainfo->reg, 0, inst->inst_basereg, inst->inst_offset);
4951 s390_lgf (code, ainfo->reg, 0, inst->inst_basereg, inst->inst_offset);
4954 s390_lg (code, ainfo->reg, 0, inst->inst_basereg, inst->inst_offset);
4958 } else if (ainfo->regtype == RegTypeStructByAddr) {
4959 if (ainfo->reg != STK_BASE) {
4960 s390_lg (code, ainfo->reg, 0, inst->inst_basereg, inst->inst_offset);
4963 g_assert_not_reached ();
4971 /*========================= End of Function ========================*/
4973 /*------------------------------------------------------------------*/
4975 /* Name - mono_arch_emit_prolog */
4977 /* Function - Create the instruction sequence for a function */
4980 /*------------------------------------------------------------------*/
4983 mono_arch_emit_prolog (MonoCompile *cfg)
4985 MonoMethod *method = cfg->method;
4987 MonoMethodSignature *sig;
4989 long alloc_size, pos, max_offset, i, cfa_offset = 0;
4996 cfg->code_size = 512;
4998 if (mono_jit_trace_calls != NULL && mono_trace_eval (method)) {
5000 cfg->code_size += 256;
5003 if (method->save_lmf)
5004 cfg->code_size += 200;
5006 cfg->native_code = code = g_malloc (cfg->code_size);
5008 mono_emit_unwind_op_def_cfa (cfg, code, STK_BASE, 0);
5009 emit_unwind_regs(cfg, code, s390_r6, s390_r14, S390_REG_SAVE_OFFSET);
5010 s390_stmg (code, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
5011 mono_emit_unwind_op_offset (cfg, code, s390_r14, S390_RET_ADDR_OFFSET);
5013 if (cfg->arch.bkchain_reg != -1)
5014 s390_lgr (code, cfg->arch.bkchain_reg, STK_BASE);
5016 if (cfg->flags & MONO_CFG_HAS_ALLOCA) {
5017 cfg->used_int_regs |= 1 << 11;
5020 alloc_size = cfg->stack_offset;
5022 cfg->stack_usage = cfa_offset = alloc_size;
5023 mono_emit_unwind_op_def_cfa_offset (cfg, code, alloc_size);
5024 s390_lgr (code, s390_r11, STK_BASE);
5025 if (s390_is_imm16 (alloc_size)) {
5026 s390_aghi (code, STK_BASE, -alloc_size);
5028 int stackSize = alloc_size;
5029 while (stackSize > 32767) {
5030 s390_aghi (code, STK_BASE, -32767);
5033 s390_aghi (code, STK_BASE, -stackSize);
5035 s390_stg (code, s390_r11, 0, STK_BASE, 0);
5037 if (cfg->frame_reg != STK_BASE)
5038 s390_lgr (code, s390_r11, STK_BASE);
5040 mono_emit_unwind_op_def_cfa_reg (cfg, code, cfg->frame_reg);
5042 /* compute max_offset in order to use short forward jumps
5043 * we always do it on s390 because the immediate displacement
5044 * for jumps is too small
5047 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5049 bb->max_offset = max_offset;
5051 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
5054 MONO_BB_FOR_EACH_INS (bb, ins)
5055 max_offset += ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
5058 /* load arguments allocated to register from the stack */
5059 sig = mono_method_signature (method);
5062 cinfo = get_call_info (cfg, cfg->mempool, sig, sig->pinvoke);
5064 if (cinfo->struct_ret) {
5065 ArgInfo *ainfo = &cinfo->ret;
5066 inst = cfg->vret_addr;
5067 inst->backend.size = ainfo->vtsize;
5068 s390_stg (code, ainfo->reg, 0, inst->inst_basereg, inst->inst_offset);
5071 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
5072 ArgInfo *ainfo = cinfo->args + i;
5073 inst = cfg->args [pos];
5075 if (inst->opcode == OP_VTARG_ADDR)
5076 inst = inst->inst_left;
5078 if (inst->opcode == OP_REGVAR) {
5079 if (ainfo->regtype == RegTypeGeneral)
5080 s390_lgr (code, inst->dreg, ainfo->reg);
5081 else if (ainfo->regtype == RegTypeFP) {
5082 if (inst->dreg != ainfo->reg) {
5083 if (ainfo->size == 4) {
5084 s390_ledbr (code, inst->dreg, ainfo->reg);
5086 s390_ldr (code, inst->dreg, ainfo->reg);
5090 else if (ainfo->regtype == RegTypeBase) {
5091 s390_lgr (code, s390_r13, STK_BASE);
5092 s390_aghi (code, s390_r13, alloc_size);
5093 s390_lg (code, inst->dreg, 0, s390_r13, ainfo->offset);
5095 g_assert_not_reached ();
5097 if (cfg->verbose_level > 2)
5098 g_print ("Argument %d assigned to register %s\n",
5099 pos, mono_arch_regname (inst->dreg));
5101 if (ainfo->regtype == RegTypeGeneral) {
5102 if (!((ainfo->reg >= 2) && (ainfo->reg <= 6)))
5103 g_assert_not_reached();
5104 switch (ainfo->size) {
5106 s390_stc (code, ainfo->reg, 0, inst->inst_basereg, inst->inst_offset);
5109 s390_sth (code, ainfo->reg, 0, inst->inst_basereg, inst->inst_offset);
5112 s390_st (code, ainfo->reg, 0, inst->inst_basereg, inst->inst_offset);
5115 s390_stg (code, ainfo->reg, 0, inst->inst_basereg, inst->inst_offset);
5118 } else if (ainfo->regtype == RegTypeBase) {
5119 } else if (ainfo->regtype == RegTypeFP) {
5120 if (ainfo->size == 8)
5121 s390_std (code, ainfo->reg, 0, inst->inst_basereg, inst->inst_offset);
5122 else if (ainfo->size == 4)
5123 s390_ste (code, ainfo->reg, 0, inst->inst_basereg, inst->inst_offset);
5125 g_assert_not_reached ();
5126 } else if (ainfo->regtype == RegTypeStructByVal) {
5127 int doffset = inst->inst_offset;
5129 if (ainfo->reg != STK_BASE)
5133 s390_lgr (code, s390_r13, STK_BASE);
5134 s390_aghi (code, s390_r13, alloc_size);
5137 size = (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE
5138 ? mono_class_native_size(mono_class_from_mono_type(inst->inst_vtype), NULL)
5143 if (ainfo->reg == STK_BASE)
5144 s390_ic (code, reg, 0, s390_r13, ainfo->offset+7);
5145 s390_stc (code, reg, 0, inst->inst_basereg, doffset);
5148 if (ainfo->reg == STK_BASE)
5149 s390_lh (code, reg, 0, s390_r13, ainfo->offset+6);
5150 s390_sth (code, reg, 0, inst->inst_basereg, doffset);
5153 if (ainfo->reg == STK_BASE)
5154 s390_l (code, reg, 0, s390_r13, ainfo->offset+4);
5155 s390_st (code, reg, 0, inst->inst_basereg, doffset);
5158 if (ainfo->reg == STK_BASE)
5159 s390_lg (code, reg, 0, s390_r13, ainfo->offset);
5160 s390_stg (code, reg, 0, inst->inst_basereg, doffset);
5163 } else if (ainfo->regtype == RegTypeStructByAddr) {
5164 s390_stg (code, ainfo->reg, 0, inst->inst_basereg, inst->inst_offset);
5165 } else if (ainfo->regtype == RegTypeStructByAddrOnStack) {
5167 g_assert_not_reached ();
5172 if (method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED) {
5173 if (cfg->compile_aot)
5174 /* AOT code is only used in the root domain */
5175 s390_lghi (code, s390_r2, 0);
5177 s390_basr(code, s390_r14, 0);
5179 s390_llong(code, cfg->domain);
5180 s390_lg (code, s390_r2, 0, s390_r14, 4);
5183 s390_basr(code, s390_r14, 0);
5185 mono_add_patch_info (cfg, code - cfg->native_code,
5186 MONO_PATCH_INFO_INTERNAL_METHOD,
5187 (gpointer)"mono_jit_thread_attach");
5188 s390_llong(code, 0);
5189 s390_lg (code, s390_r1, 0, s390_r14, 4);
5190 s390_basr (code, s390_r14, s390_r1);
5193 if (method->save_lmf) {
5194 /*---------------------------------------------------------------*/
5195 /* build the MonoLMF structure on the stack - see mini-s390x.h */
5196 /*---------------------------------------------------------------*/
5197 lmfOffset = alloc_size - sizeof(MonoLMF);
5199 s390_lgr (code, s390_r13, cfg->frame_reg);
5200 s390_aghi (code, s390_r13, lmfOffset);
5202 /*---------------------------------------------------------------*/
5203 /* Preserve the parameter registers while we fix up the lmf */
5204 /*---------------------------------------------------------------*/
5205 s390_stmg (code, s390_r2, s390_r6, s390_r13,
5206 G_STRUCT_OFFSET(MonoLMF, pregs[0]));
5208 /*---------------------------------------------------------------*/
5209 /* On return from this call r2 have the address of the &lmf */
5210 /*---------------------------------------------------------------*/
5211 s390_basr(code, s390_r14, 0);
5213 mono_add_patch_info (cfg, code - cfg->native_code,
5214 MONO_PATCH_INFO_INTERNAL_METHOD,
5215 (gpointer)"mono_get_lmf_addr");
5216 s390_llong(code, 0);
5217 s390_lg (code, s390_r1, 0, s390_r14, 4);
5218 s390_basr (code, s390_r14, s390_r1);
5220 /*---------------------------------------------------------------*/
5221 /* Set lmf.lmf_addr = jit_tls->lmf */
5222 /*---------------------------------------------------------------*/
5223 s390_stg (code, s390_r2, 0, s390_r13,
5224 G_STRUCT_OFFSET(MonoLMF, lmf_addr));
5226 /*---------------------------------------------------------------*/
5227 /* Get current lmf */
5228 /*---------------------------------------------------------------*/
5229 s390_lg (code, s390_r0, 0, s390_r2, 0);
5231 /*---------------------------------------------------------------*/
5232 /* Set our lmf as the current lmf */
5233 /*---------------------------------------------------------------*/
5234 s390_stg (code, s390_r13, 0, s390_r2, 0);
5236 /*---------------------------------------------------------------*/
5237 /* Have our lmf.previous_lmf point to the last lmf */
5238 /*---------------------------------------------------------------*/
5239 s390_stg (code, s390_r0, 0, s390_r13,
5240 G_STRUCT_OFFSET(MonoLMF, previous_lmf));
5242 /*---------------------------------------------------------------*/
5243 /* save method info */
5244 /*---------------------------------------------------------------*/
5245 s390_basr (code, s390_r1, 0);
5247 s390_llong (code, method);
5248 s390_lg (code, s390_r1, 0, s390_r1, 4);
5249 s390_stg (code, s390_r1, 0, s390_r13,
5250 G_STRUCT_OFFSET(MonoLMF, method));
5252 /*---------------------------------------------------------------*/
5253 /* save the current IP */
5254 /*---------------------------------------------------------------*/
5255 s390_stg (code, STK_BASE, 0, s390_r13, G_STRUCT_OFFSET(MonoLMF, ebp));
5256 s390_basr (code, s390_r1, 0);
5257 s390_stg (code, s390_r1, 0, s390_r13, G_STRUCT_OFFSET(MonoLMF, eip));
5259 /*---------------------------------------------------------------*/
5260 /* Save general and floating point registers */
5261 /*---------------------------------------------------------------*/
5262 s390_stmg (code, s390_r2, s390_r12, s390_r13,
5263 G_STRUCT_OFFSET(MonoLMF, gregs[2]));
5264 for (i = 0; i < 16; i++) {
5265 s390_std (code, i, 0, s390_r13,
5266 G_STRUCT_OFFSET(MonoLMF, fregs[i]));
5269 /*---------------------------------------------------------------*/
5270 /* Restore the parameter registers now that we've set up the lmf */
5271 /*---------------------------------------------------------------*/
5272 s390_lmg (code, s390_r2, s390_r6, s390_r13,
5273 G_STRUCT_OFFSET(MonoLMF, pregs[0]));
5277 code = mono_arch_instrument_prolog(cfg, enter_method, code, TRUE);
5279 cfg->code_len = code - cfg->native_code;
5280 g_assert (cfg->code_len < cfg->code_size);
5285 /*========================= End of Function ========================*/
5287 /*------------------------------------------------------------------*/
5289 /* Name - mono_arch_emit_epilog */
5291 /* Function - Emit the instructions for a function epilog. */
5293 /*------------------------------------------------------------------*/
5296 mono_arch_emit_epilog (MonoCompile *cfg)
5298 MonoMethod *method = cfg->method;
5301 int max_epilog_size = 96;
5303 if (cfg->method->save_lmf)
5304 max_epilog_size += 128;
5306 if (mono_jit_trace_calls != NULL)
5307 max_epilog_size += 128;
5309 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
5310 max_epilog_size += 128;
5312 while ((cfg->code_len + max_epilog_size) > (cfg->code_size - 16)) {
5313 cfg->code_size *= 2;
5314 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
5315 cfg->stat_code_reallocs++;
5318 code = cfg->native_code + cfg->code_len;
5320 if (mono_jit_trace_calls != NULL && mono_trace_eval (method)) {
5321 code = mono_arch_instrument_epilog (cfg, leave_method, code, TRUE);
5325 if (method->save_lmf)
5326 restoreLMF(code, cfg->frame_reg, cfg->stack_usage);
5328 if (cfg->flags & MONO_CFG_HAS_ALLOCA) {
5329 // if (cfg->frame_reg != STK_BASE)
5330 // s390_lgr (code, STK_BASE, cfg->frame_reg);
5331 s390_lg (code, STK_BASE, 0, STK_BASE, 0);
5333 code = backUpStackPtr(cfg, code);
5335 s390_lmg (code, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
5336 s390_br (code, s390_r14);
5338 cfg->code_len = code - cfg->native_code;
5340 g_assert (cfg->code_len < cfg->code_size);
5344 /*========================= End of Function ========================*/
5346 /*------------------------------------------------------------------*/
5348 /* Name - mono_arch_emit_exceptions */
5350 /* Function - Emit the blocks to handle exception conditions. */
5352 /*------------------------------------------------------------------*/
5355 mono_arch_emit_exceptions (MonoCompile *cfg)
5357 MonoJumpInfo *patch_info;
5363 MonoClass *exc_classes [MAX_EXC];
5364 guint8 *exc_throw_start [MAX_EXC];
5366 for (patch_info = cfg->patch_info;
5368 patch_info = patch_info->next) {
5369 if (patch_info->type == MONO_PATCH_INFO_EXC)
5373 code_size = exc_count * 48;
5375 while ((cfg->code_len + code_size) > (cfg->code_size - 16)) {
5376 cfg->code_size *= 2;
5377 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
5378 cfg->stat_code_reallocs++;
5381 code = cfg->native_code + cfg->code_len;
5383 /*---------------------------------------------------------------------*/
5384 /* Add code to raise exceptions */
5385 /*---------------------------------------------------------------------*/
5386 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
5387 switch (patch_info->type) {
5388 case MONO_PATCH_INFO_EXC: {
5389 guint8 *ip = patch_info->ip.i + cfg->native_code;
5390 MonoClass *exc_class;
5393 /*-----------------------------------------------------*/
5394 /* Patch the branch in epilog to come here */
5395 /*-----------------------------------------------------*/
5396 s390_patch_rel (ip + 2, (guint64) S390_RELATIVE(code,ip));
5398 exc_class = mono_class_from_name (mono_defaults.corlib,
5400 patch_info->data.name);
5401 g_assert (exc_class);
5402 throw_ip = patch_info->ip.i;
5404 for (iExc = 0; iExc < nThrows; ++iExc)
5405 if (exc_classes [iExc] == exc_class)
5408 if (iExc < nThrows) {
5409 s390_jcl (code, S390_CC_UN,
5410 (guint64) exc_throw_start [iExc]);
5411 patch_info->type = MONO_PATCH_INFO_NONE;
5414 if (nThrows < MAX_EXC) {
5415 exc_classes [nThrows] = exc_class;
5416 exc_throw_start [nThrows] = code;
5419 /*---------------------------------------------*/
5420 /* Patch the parameter passed to the handler */
5421 /*---------------------------------------------*/
5422 s390_basr (code, s390_r13, 0);
5424 // s390_llong(code, patch_info->data.target);
5425 s390_llong(code, exc_class->type_token);
5426 /*---------------------------------------------*/
5427 /* Load return address & parameter register */
5428 /*---------------------------------------------*/
5429 s390_larl (code, s390_r14, (guint64)S390_RELATIVE((patch_info->ip.i +
5430 cfg->native_code + 8), code));
5431 s390_lg (code, s390_r2, 0, s390_r13, 4);
5432 /*---------------------------------------------*/
5433 /* Reuse the current patch to set the jump */
5434 /*---------------------------------------------*/
5435 s390_basr (code, s390_r13, 0);
5437 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
5438 patch_info->data.name = "mono_arch_throw_corlib_exception";
5439 patch_info->ip.i = code - cfg->native_code;
5440 s390_llong(code, 0);
5441 s390_lg (code, s390_r1, 0, s390_r13, 4);
5442 s390_br (code, s390_r1);
5452 cfg->code_len = code - cfg->native_code;
5454 g_assert (cfg->code_len < cfg->code_size);
5458 /*========================= End of Function ========================*/
5460 /*------------------------------------------------------------------*/
5462 /* Name - mono_arch_setup_jit_tls_data */
5464 /* Function - Setup the JIT's Thread Level Specific Data. */
5466 /*------------------------------------------------------------------*/
5469 mono_arch_setup_jit_tls_data (MonoJitTlsData *tls)
5471 if (!tls_offset_inited) {
5472 tls_offset_inited = TRUE;
5476 __asm__ ("\tear\t%r1,0\n"
5483 : "=r" (appdomain_tls_offset),
5484 "=r" (thread_tls_offset),
5485 "=r" (lmf_tls_offset)
5486 : "r" (&tls_appdomain),
5487 "r" (&tls_current_object),
5488 "r" (&mono_lmf_addr)
5494 if (!lmf_addr_key_inited) {
5495 lmf_addr_key_inited = TRUE;
5496 pthread_key_create (&lmf_addr_key, NULL);
5498 pthread_setspecific (lmf_addr_key, &tls->lmf);
5502 /*========================= End of Function ========================*/
5504 /*------------------------------------------------------------------*/
5506 /* Name - mono_arch_free_jit_tls_data */
5508 /* Function - Free tls data. */
5510 /*------------------------------------------------------------------*/
5513 mono_arch_free_jit_tls_data (MonoJitTlsData *tls)
5517 /*========================= End of Function ========================*/
5519 /*------------------------------------------------------------------*/
5521 /* Name - mono_arch_emit_inst_for_method */
5523 /*------------------------------------------------------------------*/
5526 mono_arch_emit_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
5531 /*========================= End of Function ========================*/
5533 /*------------------------------------------------------------------*/
5535 /* Name - mono_arch_decompose_opts */
5537 /* Function - Decompose opcode into a System z opcode. */
5539 /*------------------------------------------------------------------*/
5542 mono_arch_decompose_opts (MonoCompile *cfg, MonoInst *ins)
5545 * Have to rename these to avoid being decomposed normally, since the normal
5546 * decomposition does not work on S390.
5548 switch (ins->opcode) {
5550 ins->opcode = OP_S390_ISUB_OVF;
5552 case OP_ISUB_OVF_UN:
5553 ins->opcode = OP_S390_ISUB_OVF_UN;
5556 ins->opcode = OP_S390_IADD_OVF;
5558 case OP_IADD_OVF_UN:
5559 ins->opcode = OP_S390_IADD_OVF_UN;
5562 ins->opcode = OP_S390_LADD_OVF;
5564 case OP_LADD_OVF_UN:
5565 ins->opcode = OP_S390_LADD_OVF_UN;
5568 ins->opcode = OP_S390_LSUB_OVF;
5570 case OP_LSUB_OVF_UN:
5571 ins->opcode = OP_S390_LSUB_OVF_UN;
5578 /*========================= End of Function ========================*/
5580 /*------------------------------------------------------------------*/
5582 /* Name - mono_arch_print_tree */
5584 /* Function - Print platform-specific opcode details. */
5586 /* Returns - 1 - opcode details have been printed */
5587 /* 0 - opcode details have not been printed */
5589 /*------------------------------------------------------------------*/
5592 mono_arch_print_tree (MonoInst *tree, int arity)
5596 switch (tree->opcode) {
5597 case OP_S390_LOADARG:
5598 case OP_S390_ARGREG:
5599 case OP_S390_ARGPTR:
5600 printf ("[0x%lx(%s)]", tree->inst_offset,
5601 mono_arch_regname (tree->inst_basereg));
5604 case OP_S390_STKARG:
5605 printf ("[0x%lx(previous_frame)]",
5610 printf ("[0x%lx(%d,%s),0x%lx(%s)]",
5611 tree->inst_offset, tree->backend.size,
5612 mono_arch_regname(tree->dreg),
5614 mono_arch_regname(tree->sreg1));
5617 case OP_S390_SETF4RET:
5618 printf ("[f%s,f%s]",
5619 mono_arch_regname (tree->dreg),
5620 mono_arch_regname (tree->sreg1));
5624 printf ("[0x%lx(0x%lx,%s)]", tree->inst_offset,
5626 mono_arch_regname (tree->sreg1));
5629 case OP_S390_BKCHAIN:
5630 printf ("[previous_frame(%s)]",
5631 mono_arch_regname (tree->sreg1));
5639 /*========================= End of Function ========================*/
5641 /*------------------------------------------------------------------*/
5643 /* Name - mono_arch_regalloc_cost */
5645 /* Function - Determine the cost, in the number of memory */
5646 /* references, of the action of allocating the var- */
5647 /* iable VMV into a register during global register */
5650 /* Returns - Cost */
5652 /*------------------------------------------------------------------*/
5655 mono_arch_regalloc_cost (MonoCompile *cfg, MonoMethodVar *vmv)
5661 /*========================= End of Function ========================*/
5663 /*------------------------------------------------------------------*/
5665 /* Name - mono_arch_get_domain_intrinsic */
5671 /*------------------------------------------------------------------*/
5674 mono_arch_get_domain_intrinsic (MonoCompile* cfg)
5678 if (appdomain_tls_offset == -1)
5681 MONO_INST_NEW (cfg, ins, OP_TLS_GET);
5682 ins->inst_offset = appdomain_tls_offset;
5686 /*========================= End of Function ========================*/
5688 /*------------------------------------------------------------------*/
5690 /* Name - mono_arch_flush_register_windows */
5696 /*------------------------------------------------------------------*/
5699 mono_arch_flush_register_windows (void)
5703 /*========================= End of Function ========================*/
5705 /*------------------------------------------------------------------*/
5707 /* Name - mono_arch_get_lmf_addr */
5713 /*------------------------------------------------------------------*/
5716 mono_arch_get_lmf_addr (void)
5718 return pthread_getspecific (lmf_addr_key);
5721 /*========================= End of Function ========================*/
5723 /*------------------------------------------------------------------*/
5725 /* Name - mono_arch_is_inst_imm */
5727 /* Function - Determine if operand qualifies as an immediate */
5728 /* value. For s390 this is a value -32768-32768 */
5730 /* Returns - True|False - is [not] immediate value. */
5732 /*------------------------------------------------------------------*/
5735 mono_arch_is_inst_imm (gint64 imm)
5737 return s390_is_imm16 (imm);
5740 /*========================= End of Function ========================*/
5742 /*------------------------------------------------------------------*/
5744 /* Name - mono_arch_get_patch_offset */
5746 /* Function - Dummy entry point until s390x supports aot. */
5748 /* Returns - Offset for patch. */
5750 /*------------------------------------------------------------------*/
5753 mono_arch_get_patch_offset (guint8 *code)
5758 /*========================= End of Function ========================*/
5760 /*------------------------------------------------------------------*/
5762 /* Name - mono_arch_context_get_int_reg. */
5766 /* Returns - Return a register from the context. */
5768 /*------------------------------------------------------------------*/
5771 mono_arch_context_get_int_reg (MonoContext *ctx, int reg)
5773 return ((mgreg_t) ctx->uc_mcontext.gregs[reg]);
5776 /*========================= End of Function ========================*/
5778 /*------------------------------------------------------------------*/
5780 /* Name - mono_arch_context_set_int_reg. */
5782 /* Function - Set a value in a specified register. */
5784 /*------------------------------------------------------------------*/
5787 mono_arch_context_set_int_reg (MonoContext *ctx, int reg, mgreg_t val)
5789 ctx->uc_mcontext.gregs[reg] = val;
5792 /*========================= End of Function ========================*/
5794 /*------------------------------------------------------------------*/
5796 /* Name - mono_arch_get_this_arg_from_call. */
5800 /*------------------------------------------------------------------*/
5803 mono_arch_get_this_arg_from_call (mgreg_t *regs, guint8 *code)
5805 MonoLMF *lmf = (MonoLMF *) ((gchar *) regs - sizeof(MonoLMF));
5807 return (gpointer) lmf->gregs [s390_r2];
5810 /*========================= End of Function ========================*/
5812 /*------------------------------------------------------------------*/
5814 /* Name - get_delegate_invoke_impl. */
5818 /*------------------------------------------------------------------*/
5821 get_delegate_invoke_impl (gboolean has_target, guint32 param_count, guint32 *code_len, gboolean aot)
5823 guint8 *code, *start;
5828 start = code = mono_global_codeman_reserve (size);
5830 /* Replace the this argument with the target */
5831 s390_lg (code, s390_r1, 0, s390_r2, G_STRUCT_OFFSET(MonoDelegate, method_ptr));
5832 s390_lg (code, s390_r2, 0, s390_r2, G_STRUCT_OFFSET(MonoDelegate, target));
5833 s390_br (code, s390_r1);
5834 g_assert ((code - start) <= size);
5836 mono_arch_flush_icache (start, size);
5840 size = 32 + param_count * 8;
5841 start = code = mono_global_codeman_reserve (size);
5843 s390_lg (code, s390_r1, 0, s390_r2, G_STRUCT_OFFSET(MonoDelegate, method_ptr));
5844 /* slide down the arguments */
5845 for (i = 0; i < param_count; ++i) {
5846 s390_lgr (code, (s390_r2 + i), (s390_r2 + i + 1));
5848 s390_br (code, s390_r1);
5850 g_assert ((code - start) <= size);
5852 mono_arch_flush_icache (start, size);
5856 *code_len = code - start;
5861 /*========================= End of Function ========================*/
5863 /*------------------------------------------------------------------*/
5865 /* Name - mono_arch_get_delegate_invoke_impls. */
5869 /*------------------------------------------------------------------*/
5872 mono_arch_get_delegate_invoke_impls (void)
5879 code = get_delegate_invoke_impl (TRUE, 0, &code_len, TRUE);
5880 res = g_slist_prepend (res, mono_tramp_info_create (g_strdup ("delegate_invoke_impl_has_target"), code, code_len, NULL, NULL));
5882 for (i = 0; i < MAX_ARCH_DELEGATE_PARAMS; ++i) {
5883 code = get_delegate_invoke_impl (FALSE, i, &code_len, TRUE);
5884 res = g_slist_prepend (res, mono_tramp_info_create (g_strdup_printf ("delegate_invoke_impl_target_%d", i), code, code_len, NULL, NULL));
5890 /*========================= End of Function ========================*/
5892 /*------------------------------------------------------------------*/
5894 /* Name - mono_arch_get_delegate_invoke_impl. */
5898 /*------------------------------------------------------------------*/
5901 mono_arch_get_delegate_invoke_impl (MonoMethodSignature *sig, gboolean has_target)
5903 guint8 *code, *start;
5905 /* FIXME: Support more cases */
5906 if (MONO_TYPE_ISSTRUCT (sig->ret))
5910 static guint8* cached = NULL;
5916 start = mono_aot_get_trampoline ("delegate_invoke_impl_has_target");
5918 start = get_delegate_invoke_impl (TRUE, 0, NULL, FALSE);
5920 mono_memory_barrier ();
5924 static guint8* cache [MAX_ARCH_DELEGATE_PARAMS + 1] = {NULL};
5927 if (sig->param_count > MAX_ARCH_DELEGATE_PARAMS)
5929 for (i = 0; i < sig->param_count; ++i)
5930 if (!mono_is_regsize_var (sig->params [i]))
5934 code = cache [sig->param_count];
5938 if (mono_aot_only) {
5939 char *name = g_strdup_printf ("delegate_invoke_impl_target_%d", sig->param_count);
5940 start = mono_aot_get_trampoline (name);
5943 start = get_delegate_invoke_impl (FALSE, sig->param_count, NULL, FALSE);
5946 mono_memory_barrier ();
5948 cache [sig->param_count] = start;
5953 /*========================= End of Function ========================*/
5955 /*------------------------------------------------------------------*/
5957 /* Name - mono_arch_build_imt_thunk. */
5961 /*------------------------------------------------------------------*/
5964 mono_arch_build_imt_thunk (MonoVTable *vtable, MonoDomain *domain,
5965 MonoIMTCheckItem **imt_entries, int count,
5966 gpointer fail_tramp)
5970 guchar *code, *start;
5972 for (i = 0; i < count; ++i) {
5973 MonoIMTCheckItem *item = imt_entries [i];
5974 if (item->is_equals) {
5975 if (item->check_target_idx) {
5976 if (!item->compare_done)
5977 item->chunk_size += CMP_SIZE + JUMP_SIZE;
5978 if (item->has_target_code)
5979 item->chunk_size += BR_SIZE + JUMP_SIZE + LOADCON_SIZE;
5981 item->chunk_size += BR_SIZE + JUMP_SIZE + LOADCON_SIZE +
5985 item->chunk_size += CMP_SIZE + 2 * BR_SIZE + JUMP_SIZE +
5987 if (!item->has_target_code)
5988 item->chunk_size += LOAD_SIZE;
5990 item->chunk_size += LOADCON_SIZE + LOAD_SIZE + BR_SIZE;
5991 #if ENABLE_WRONG_METHOD_CHECK
5992 item->chunk_size += CMP_SIZE + JUMP_SIZE;
5997 item->chunk_size += CMP_SIZE + JUMP_SIZE;
5998 imt_entries [item->check_target_idx]->compare_done = TRUE;
6000 size += item->chunk_size;
6004 code = mono_method_alloc_generic_virtual_thunk (domain, size);
6006 code = mono_domain_code_reserve (domain, size);
6010 for (i = 0; i < count; ++i) {
6011 MonoIMTCheckItem *item = imt_entries [i];
6012 item->code_target = (guint8 *) code;
6013 if (item->is_equals) {
6014 if (item->check_target_idx) {
6015 if (!item->compare_done) {
6016 s390_basr (code, s390_r13, s390_r0);
6018 s390_llong(code, item->key);
6019 s390_lg (code, s390_r0, 0, s390_r13, 4);
6020 s390_cgr (code, s390_r0, MONO_ARCH_IMT_REG);
6022 item->jmp_code = (guint8*) code;
6023 s390_jcl (code, S390_CC_NE, 0);
6025 s390_basr (code, s390_r13, s390_r0);
6027 if (item->has_target_code) {
6028 s390_llong(code, item->value.target_code);
6029 s390_lg (code, s390_r1, 0, s390_r13, 4);
6031 s390_llong(code, (&(vtable->vtable [item->value.vtable_slot])));
6032 s390_lg (code, s390_r1, 0, s390_r13, 4);
6033 s390_lg (code, s390_r1, 0, s390_r1, 0);
6035 s390_br (code, s390_r1);
6040 s390_basr (code, s390_r13, s390_r0);
6042 s390_llong(code, item->key);
6043 s390_lg (code, s390_r0, 0, s390_r13, 4);
6044 s390_cgr (code, s390_r0, MONO_ARCH_IMT_REG);
6045 item->jmp_code = (guint8*) code;
6046 s390_jcl (code, S390_CC_NE, 0);
6047 s390_basr (code, s390_r13, s390_r0);
6049 if (item->has_target_code) {
6050 s390_llong(code, item->value.target_code);
6051 s390_lg (code, s390_r1, 0, s390_r13, 4);
6054 s390_llong(code, (&(vtable->vtable [item->value.vtable_slot])));
6055 s390_lg (code, s390_r1, 0, s390_r13, 4);
6056 s390_lg (code, s390_r1, 0, s390_r1, 0);
6058 s390_br (code, s390_r1);
6059 target = S390_RELATIVE(code, item->jmp_code);
6060 s390_patch_rel(item->jmp_code+2, target);
6061 s390_basr (code, s390_r13, s390_r0);
6063 s390_llong(code, fail_tramp);
6064 s390_lg (code, s390_r1, 0, s390_r13, 4);
6065 s390_br (code, s390_r1);
6066 item->jmp_code = NULL;
6068 /* enable the commented code to assert on wrong method */
6069 #if ENABLE_WRONG_METHOD_CHECK
6070 g_assert_not_reached ();
6072 s390_basr (code, s390_r13, s390_r0);
6074 s390_llong(code, (&(vtable->vtable [item->value.vtable_slot])));
6075 s390_lg (code, s390_r1, 0, s390_r13, 4);
6076 s390_lg (code, s390_r1, 0, s390_r1, 0);
6077 s390_br (code, s390_r1);
6078 #if ENABLE_WRONG_METHOD_CHECK
6079 g_assert_not_reached ();
6084 s390_basr (code, s390_r13, s390_r0);
6086 s390_llong(code, item->key);
6087 s390_lg (code, s390_r0, 0, s390_r13, 4);
6088 s390_cgr (code, MONO_ARCH_IMT_REG, s390_r0);
6089 item->jmp_code = (guint8 *) code;
6090 s390_jcl (code, S390_CC_GE, 0);
6093 /* patch the branches to get to the target items */
6094 for (i = 0; i < count; ++i) {
6095 MonoIMTCheckItem *item = imt_entries [i];
6096 if (item->jmp_code) {
6097 if (item->check_target_idx) {
6099 offset = S390_RELATIVE(imt_entries [item->check_target_idx]->code_target,
6101 s390_patch_rel ((guchar *) item->jmp_code + 2, (guint64) offset);
6106 mono_arch_flush_icache ((guint8*)start, (code - start));
6109 mono_stats.imt_thunks_size += (code - start);
6111 g_assert (code - start <= size);
6116 /*========================= End of Function ========================*/
6118 /*------------------------------------------------------------------*/
6120 /* Name - mono_arch_find_imt_method. */
6122 /* Function - Get the method address from MONO_ARCH_IMT_REG */
6123 /* found in the save area. */
6125 /*------------------------------------------------------------------*/
6128 mono_arch_find_imt_method (mgreg_t *regs, guint8 *code)
6130 MonoLMF *lmf = (MonoLMF *) ((gchar *) regs - sizeof(MonoLMF));
6132 return ((MonoMethod *) lmf->gregs [MONO_ARCH_IMT_REG]);
6135 /*========================= End of Function ========================*/
6137 #ifdef MONO_ARCH_SOFT_DEBUG_SUPPORTED
6139 /*------------------------------------------------------------------*/
6141 /* Name - mono_arch_set_breakpoint. */
6143 /* Function - Set a breakpoint at the native code corresponding */
6144 /* to JI at NATIVE_OFFSET. The location should */
6145 /* contain code emitted by OP_SEQ_POINT. */
6147 /*------------------------------------------------------------------*/
6150 mono_arch_set_breakpoint (MonoJitInfo *ji, guint8 *ip)
6154 breakpointCode.pTrigger = bp_trigger_page;
6155 memcpy(code, (void *) &breakpointCode, BREAKPOINT_SIZE);
6156 code += BREAKPOINT_SIZE;
6159 /*========================= End of Function ========================*/
6161 /*------------------------------------------------------------------*/
6163 /* Name - mono_arch_clear_breakpoint. */
6165 /* Function - Clear the breakpoint at IP. */
6167 /*------------------------------------------------------------------*/
6170 mono_arch_clear_breakpoint (MonoJitInfo *ji, guint8 *ip)
6175 for (i = 0; i < (BREAKPOINT_SIZE / S390X_NOP_SIZE); i++)
6179 /*========================= End of Function ========================*/
6181 /*------------------------------------------------------------------*/
6183 /* Name - mono_arch_is_breakpoint_event. */
6187 /*------------------------------------------------------------------*/
6190 mono_arch_is_breakpoint_event (void *info, void *sigctx)
6192 siginfo_t* sinfo = (siginfo_t*) info;
6193 /* Sometimes the address is off by 4 */
6194 if (sinfo->si_addr >= bp_trigger_page && (guint8*)sinfo->si_addr <= (guint8*)bp_trigger_page + 128)
6200 /*========================= End of Function ========================*/
6202 /*------------------------------------------------------------------*/
6204 /* Name - mono_arch_get_ip_for_breakpoint. */
6206 /* Function - Convert the IP in the CTX to the address where a */
6207 /* breakpoint was placed. */
6209 /*------------------------------------------------------------------*/
6212 mono_arch_get_ip_for_breakpoint (MonoJitInfo *ji, MonoContext *ctx)
6214 guint8 *ip = MONO_CONTEXT_GET_IP (ctx);
6216 /* ip points to the instruction causing the fault */
6217 ip -= BREAKPOINT_SIZE;
6222 /*========================= End of Function ========================*/
6224 /*------------------------------------------------------------------*/
6226 /* Name - mono_arch_skip_breakpoint. */
6228 /* Function - Modify the CTX so the IP is placed after the */
6229 /* breakpoint instruction, so when we resume, the */
6230 /* instruction is not executed again. */
6232 /*------------------------------------------------------------------*/
6235 mono_arch_skip_breakpoint (MonoContext *ctx)
6237 MONO_CONTEXT_SET_IP (ctx, (guint8*)MONO_CONTEXT_GET_IP (ctx) + BREAKPOINT_SIZE);
6240 /*========================= End of Function ========================*/
6242 /*------------------------------------------------------------------*/
6244 /* Name - mono_arch_start_single_stepping. */
6246 /* Function - Start single stepping. */
6248 /*------------------------------------------------------------------*/
6251 mono_arch_start_single_stepping (void)
6253 mono_mprotect (ss_trigger_page, mono_pagesize (), 0);
6256 /*========================= End of Function ========================*/
6258 /*------------------------------------------------------------------*/
6260 /* Name - mono_arch_stop_single_stepping. */
6262 /* Function - Stop single stepping. */
6264 /*------------------------------------------------------------------*/
6267 mono_arch_stop_single_stepping (void)
6269 mono_mprotect (ss_trigger_page, mono_pagesize (), MONO_MMAP_READ);
6272 /*========================= End of Function ========================*/
6274 /*------------------------------------------------------------------*/
6276 /* Name - mono_arch_is_single_step_event. */
6278 /* Function - Return whether the machine state in sigctx cor- */
6279 /* responds to a single step event. */
6281 /*------------------------------------------------------------------*/
6284 mono_arch_is_single_step_event (void *info, void *sigctx)
6286 siginfo_t* sinfo = (siginfo_t*) info;
6288 /* Sometimes the address is off by 4 */
6289 if (sinfo->si_addr >= ss_trigger_page && (guint8*)sinfo->si_addr <= (guint8*)ss_trigger_page + 128)
6295 /*========================= End of Function ========================*/
6297 /*------------------------------------------------------------------*/
6299 /* Name - mono_arch_get_ip_for_single_step. */
6301 /* Function - Convert the IP in ctx to the address stored in */
6304 /*------------------------------------------------------------------*/
6307 mono_arch_get_ip_for_single_step (MonoJitInfo *ji, MonoContext *ctx)
6309 guint8 *ip = MONO_CONTEXT_GET_IP (ctx);
6314 /*========================= End of Function ========================*/
6316 /*------------------------------------------------------------------*/
6318 /* Name - mono_arch_skip_single_step. */
6320 /* Function - Modify the ctx so the IP is placed after the */
6321 /* single step trigger instruction, so that the */
6322 /* instruction is not executed again. */
6324 /*------------------------------------------------------------------*/
6327 mono_arch_skip_single_step (MonoContext *ctx)
6329 MONO_CONTEXT_SET_IP (ctx, (guint8*)MONO_CONTEXT_GET_IP (ctx) + BREAKPOINT_SIZE);
6332 /*========================= End of Function ========================*/
6334 /*------------------------------------------------------------------*/
6336 /* Name - mono_arch_create_seq_point_info. */
6338 /* Function - Return a pointer to a data struction which is */
6339 /* used by the sequence point implementation in */
6342 /*------------------------------------------------------------------*/
6345 mono_arch_get_seq_point_info (MonoDomain *domain, guint8 *code)
6351 /*========================= End of Function ========================*/