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 MAX(a, b) ((a) > (b) ? (a) : (b))
237 * imt thunking size values
240 #define LOADCON_SIZE 20
244 #define ENABLE_WRONG_METHOD_CHECK 0
246 /*========================= End of Defines =========================*/
248 /*------------------------------------------------------------------*/
249 /* I n c l u d e s */
250 /*------------------------------------------------------------------*/
255 #include <mono/metadata/appdomain.h>
256 #include <mono/metadata/debug-helpers.h>
257 #include <mono/metadata/profiler-private.h>
258 #include <mono/utils/mono-math.h>
260 #include "mini-s390x.h"
261 #include "cpu-s390x.h"
262 #include "jit-icalls.h"
265 /*========================= End of Includes ========================*/
267 /*------------------------------------------------------------------*/
268 /* T y p e d e f s */
269 /*------------------------------------------------------------------*/
281 /*------------------------------------------------------------------*/
282 /* Used by the instrument_emit_epilog */
283 /*------------------------------------------------------------------*/
294 typedef struct InstList InstList;
308 RegTypeStructByValInFP,
310 RegTypeStructByAddrOnStack
314 gint32 offset; /* offset from caller's stack */
315 gint32 offparm; /* offset from callee's stack */
316 guint16 vtsize; /* in param area */
319 guint32 size; /* Size of structure used by RegTypeStructByVal */
320 gint32 type; /* Data type of argument */
336 gint64 gr[5]; /* R2-R6 */
337 gdouble fp[3]; /* F0-F2 */
338 } __attribute__ ((packed)) RegParm;
340 /*========================= End of Typedefs ========================*/
342 /*------------------------------------------------------------------*/
343 /* P r o t o t y p e s */
344 /*------------------------------------------------------------------*/
346 static void indent (int);
347 static guint8 * backUpStackPtr(MonoCompile *, guint8 *);
348 static void decodeParm (MonoType *, void *, int);
349 static void enter_method (MonoMethod *, RegParm *, char *);
350 static void leave_method (MonoMethod *, ...);
351 static gboolean is_regsize_var (MonoType *);
352 static inline void add_general (guint *, size_data *, ArgInfo *);
353 static inline void add_stackParm (guint *, size_data *, ArgInfo *, gint);
354 static inline void add_float (guint *, size_data *, ArgInfo *);
355 static CallInfo * get_call_info (MonoCompile *, MonoMemPool *, MonoMethodSignature *, gboolean);
356 static guchar * emit_float_to_int (MonoCompile *, guchar *, int, int, int, gboolean);
357 gpointer mono_arch_get_lmf_addr (void);
358 static guint8 * emit_load_volatile_arguments (guint8 *, MonoCompile *);
359 static void catch_SIGILL(int, siginfo_t *, void *);
360 static __inline__ void emit_unwind_regs(MonoCompile *, guint8 *, int, int, long);
362 /*========================= End of Prototypes ======================*/
364 /*------------------------------------------------------------------*/
365 /* G l o b a l V a r i a b l e s */
366 /*------------------------------------------------------------------*/
368 int mono_exc_esp_offset = 0;
370 static int indent_level = 0;
374 static gboolean tls_offset_inited = FALSE;
376 static int appdomain_tls_offset = -1,
377 thread_tls_offset = -1;
379 pthread_key_t lmf_addr_key;
381 gboolean lmf_addr_key_inited = FALSE;
387 extern __thread MonoDomain *tls_appdomain;
388 extern __thread MonoThread *tls_current_object;
389 extern __thread gpointer mono_lmf_addr;
393 /*====================== End of Global Variables ===================*/
395 /*------------------------------------------------------------------*/
397 /* Name - mono_arch_regname */
399 /* Function - Returns the name of the register specified by */
400 /* the input parameter. */
402 /*------------------------------------------------------------------*/
405 mono_arch_regname (int reg) {
406 static const char * rnames[] = {
407 "s390_r0", "s390_sp", "s390_r2", "s390_r3", "s390_r4",
408 "s390_r5", "s390_r6", "s390_r7", "s390_r8", "s390_r9",
409 "s390_r10", "s390_r11", "s390_r12", "s390_r13", "s390_r14",
413 if (reg >= 0 && reg < 16)
419 /*========================= End of Function ========================*/
421 /*------------------------------------------------------------------*/
423 /* Name - mono_arch_fregname */
425 /* Function - Returns the name of the register specified by */
426 /* the input parameter. */
428 /*------------------------------------------------------------------*/
431 mono_arch_fregname (int reg) {
432 static const char * rnames[] = {
433 "s390_f0", "s390_f1", "s390_f2", "s390_f3", "s390_f4",
434 "s390_f5", "s390_f6", "s390_f7", "s390_f8", "s390_f9",
435 "s390_f10", "s390_f11", "s390_f12", "s390_f13", "s390_f14",
439 if (reg >= 0 && reg < 16)
445 /*========================= End of Function ========================*/
447 /*------------------------------------------------------------------*/
449 /* Name - arch_get_argument_info */
451 /* Function - Gathers information on parameters such as size, */
452 /* alignment, and padding. arg_info should be large */
453 /* enough to hold param_count + 1 entries. */
455 /* Parameters - @csig - Method signature */
456 /* @param_count - No. of parameters to consider */
457 /* @arg_info - An array to store the result info */
459 /* Returns - Size of the activation frame */
461 /*------------------------------------------------------------------*/
464 mono_arch_get_argument_info (MonoMethodSignature *csig,
466 MonoJitArgumentInfo *arg_info)
468 int k, frame_size = 0;
469 int size, align, pad;
472 if (MONO_TYPE_ISSTRUCT (csig->ret)) {
473 frame_size += sizeof (gpointer);
477 arg_info [0].offset = offset;
480 frame_size += sizeof (gpointer);
484 arg_info [0].size = frame_size;
486 for (k = 0; k < param_count; k++) {
489 size = mono_type_native_stack_size (csig->params [k], (guint32 *) &align);
491 size = mini_type_stack_size (NULL, csig->params [k], &align);
493 frame_size += pad = (align - (frame_size & (align - 1))) & (align - 1);
494 arg_info [k].pad = pad;
496 arg_info [k + 1].pad = 0;
497 arg_info [k + 1].size = size;
499 arg_info [k + 1].offset = offset;
503 align = MONO_ARCH_FRAME_ALIGNMENT;
504 frame_size += pad = (align - (frame_size & (align - 1))) & (align - 1);
505 arg_info [k].pad = pad;
510 /*========================= End of Function ========================*/
512 /*------------------------------------------------------------------*/
514 /* Name - emit_unwind_regs. */
516 /* Function - Determines if a value can be returned in one or */
519 /*------------------------------------------------------------------*/
521 static void __inline__
522 emit_unwind_regs(MonoCompile *cfg, guint8 *code, int start, int end, long offset)
526 for (i = start; i < end; i++) {
527 mono_emit_unwind_op_offset (cfg, code, i, offset);
528 offset += sizeof(gulong);
532 /*========================= End of Function ========================*/
534 /*------------------------------------------------------------------*/
536 /* Name - retFitsInReg. */
538 /* Function - Determines if a value can be returned in one or */
541 /*------------------------------------------------------------------*/
543 static inline gboolean
544 retFitsInReg(guint32 size)
559 /*========================= End of Function ========================*/
561 /*------------------------------------------------------------------*/
563 /* Name - backStackPtr. */
565 /* Function - Restore Stack Pointer to previous frame. */
567 /*------------------------------------------------------------------*/
569 static inline guint8 *
570 backUpStackPtr(MonoCompile *cfg, guint8 *code)
572 int stackSize = cfg->stack_usage;
574 if (cfg->frame_reg != STK_BASE)
575 s390_lgr (code, STK_BASE, cfg->frame_reg);
577 if (s390_is_imm16 (stackSize)) {
578 s390_aghi (code, STK_BASE, stackSize);
580 while (stackSize > 32767) {
581 s390_aghi (code, STK_BASE, 32767);
584 s390_aghi (code, STK_BASE, stackSize);
589 /*========================= End of Function ========================*/
591 /*------------------------------------------------------------------*/
595 /* Function - Perform nice indenting to current level */
597 /*------------------------------------------------------------------*/
603 indent_level += diff;
610 indent_level += diff;
613 /*========================= End of Function ========================*/
615 /*------------------------------------------------------------------*/
617 /* Name - decodeParm */
619 /* Function - Decode a parameter for the trace. */
621 /*------------------------------------------------------------------*/
624 decodeParm(MonoType *type, void *curParm, int size)
629 printf("[BYREF:%p], ", *((char **) curParm));
631 simpleType = mono_type_get_underlying_type(type)->type;
633 switch (simpleType) {
635 printf ("[INTPTR:%p], ", *((int **) curParm));
638 printf ("[UINTPTR:%p], ", *((int **) curParm));
640 case MONO_TYPE_BOOLEAN :
641 printf ("[BOOL:%ld], ", *((gint64 *) curParm));
643 case MONO_TYPE_CHAR :
644 printf ("[CHAR:%c], ", *((int *) curParm));
647 printf ("[INT1:%ld], ", *((gint64 *) curParm));
650 printf ("[INT2:%ld], ", *((gint64 *) curParm));
653 printf ("[INT4:%ld], ", *((gint64 *) curParm));
656 printf ("[UINT1:%lu], ", *((guint64 *) curParm));
659 printf ("[UINT2:%lu], ", *((guint64 *) curParm));
662 printf ("[UINT4:%lu], ", *((guint64 *) curParm));
665 printf ("[UINT8:%lu], ", *((guint64 *) curParm));
667 case MONO_TYPE_STRING : {
668 MonoString *s = *((MonoString **) curParm);
670 g_assert (((MonoObject *) s)->vtable->klass == mono_defaults.string_class);
671 printf("[STRING:%p:%s], ", s, mono_string_to_utf8(s));
673 printf("[STRING:null], ");
677 case MONO_TYPE_CLASS :
678 case MONO_TYPE_OBJECT : {
679 MonoObject *obj = *((MonoObject **) curParm);
681 if ((obj) && (obj->vtable)) {
682 printf("[CLASS/OBJ:");
683 class = obj->vtable->klass;
684 printf("%p [%p] ",obj,curParm);
685 if (class == mono_defaults.string_class) {
686 printf("[STRING:%p:%s]",
687 obj, mono_string_to_utf8 ((MonoString *) obj));
688 } else if (class == mono_defaults.int32_class) {
689 printf("[INT32:%p:%d]",
690 obj, *(gint32 *)((char *)obj + sizeof (MonoObject)));
693 class->name_space, class->name, obj);
696 printf("[OBJECT:null], ");
701 printf("[PTR:%p], ", *((gpointer **) (curParm)));
703 case MONO_TYPE_FNPTR :
704 printf("[FNPTR:%p], ", *((gpointer **) (curParm)));
706 case MONO_TYPE_ARRAY :
707 printf("[ARRAY:%p], ", *((gpointer **) (curParm)));
709 case MONO_TYPE_SZARRAY :
710 printf("[SZARRAY:%p], ", *((gpointer **) (curParm)));
713 printf("[INT8:%ld], ", *((gint64 *) (curParm)));
716 printf("[FLOAT4:%g], ", *((float *) (curParm)));
719 printf("[FLOAT8:%g], ", *((double *) (curParm)));
721 case MONO_TYPE_VALUETYPE : {
723 MonoMarshalType *info;
725 if (type->data.klass->enumtype) {
726 simpleType = mono_class_enum_basetype (type->data.klass)->type;
727 printf("{VALUETYPE} - ");
731 info = mono_marshal_load_type_info (type->data.klass);
733 if ((info->native_size == sizeof(float)) &&
734 (info->num_fields == 1) &&
735 (info->fields[0].field->type->type == MONO_TYPE_R4)) {
736 printf("[FLOAT4:%f], ", *((float *) (curParm)));
740 if ((info->native_size == sizeof(double)) &&
741 (info->num_fields == 1) &&
742 (info->fields[0].field->type->type == MONO_TYPE_R8)) {
743 printf("[FLOAT8:%g], ", *((double *) (curParm)));
747 printf("[VALUETYPE:");
748 for (i = 0; i < size; i++)
749 printf("%02x,", *((guint8 *)curParm+i));
753 case MONO_TYPE_TYPEDBYREF: {
755 printf("[TYPEDBYREF:");
756 for (i = 0; i < size; i++)
757 printf("%02x,", *((guint8 *)curParm+i));
762 printf("[?? - %d], ",simpleType);
767 /*========================= End of Function ========================*/
769 /*------------------------------------------------------------------*/
771 /* Name - enter_method */
773 /* Function - Perform tracing of the entry to the current */
776 /*------------------------------------------------------------------*/
779 enter_method (MonoMethod *method, RegParm *rParm, char *sp)
781 int i, oParm = 0, iParm = 0;
784 MonoMethodSignature *sig;
791 fname = mono_method_full_name (method, TRUE);
793 printf ("ENTER: %s ", fname);
796 ip = (*(guint64 *) (sp+S390_RET_ADDR_OFFSET));
797 printf ("ip: %p sp: %p - ", (gpointer) ip, sp);
802 sig = mono_method_signature (method);
804 cinfo = get_call_info (NULL, NULL, sig, sig->pinvoke);
806 if (cinfo->struct_ret) {
807 printf ("[STRUCTRET:%p], ", (gpointer) rParm->gr[0]);
812 gpointer *this = (gpointer *) rParm->gr[iParm];
813 obj = (MonoObject *) this;
814 if (method->klass->valuetype) {
816 printf("this:[value:%p:%016lx], ",
817 this, *((guint64 *)(this+sizeof(MonoObject))));
819 printf ("this:[NULL], ");
822 class = obj->vtable->klass;
823 if (class == mono_defaults.string_class) {
824 printf ("this:[STRING:%p:%s], ",
825 obj, mono_string_to_utf8 ((MonoString *)obj));
827 printf ("this:%p[%s.%s], ",
828 obj, class->name_space, class->name);
831 printf ("this:NULL, ");
836 for (i = 0; i < sig->param_count; ++i) {
837 ainfo = &cinfo->args[i + oParm];
838 switch (ainfo->regtype) {
839 case RegTypeGeneral :
840 decodeParm(sig->params[i], &(rParm->gr[ainfo->reg-2]), ainfo->size);
843 decodeParm(sig->params[i], &(rParm->fp[ainfo->reg]), ainfo->size);
846 decodeParm(sig->params[i], sp+ainfo->offset, ainfo->size);
848 case RegTypeStructByVal :
849 if (ainfo->reg != STK_BASE) {
850 int offset = sizeof(glong) - ainfo->size;
851 curParm = &(rParm->gr[ainfo->reg-2])+offset;
854 curParm = sp+ainfo->offset;
856 if (retFitsInReg (ainfo->vtsize))
857 decodeParm(sig->params[i],
861 decodeParm(sig->params[i],
862 *((char **) curParm),
865 case RegTypeStructByAddr :
866 if (ainfo->reg != STK_BASE)
867 curParm = &(rParm->gr[ainfo->reg-2]);
869 curParm = sp+ainfo->offset;
871 decodeParm(sig->params[i],
872 *((char **) curParm),
884 /*========================= End of Function ========================*/
886 /*------------------------------------------------------------------*/
888 /* Name - leave_method */
892 /*------------------------------------------------------------------*/
895 leave_method (MonoMethod *method, ...)
902 va_start(ap, method);
904 fname = mono_method_full_name (method, TRUE);
906 printf ("LEAVE: %s", fname);
909 type = mono_method_signature (method)->ret;
912 switch (type->type) {
915 case MONO_TYPE_BOOLEAN: {
916 int val = va_arg (ap, int);
918 printf ("[TRUE:%d]", val);
924 case MONO_TYPE_CHAR: {
925 int val = va_arg (ap, int);
926 printf ("[CHAR:%d]", val);
930 int val = va_arg (ap, int);
931 printf ("[INT1:%d]", val);
935 int val = va_arg (ap, int);
936 printf ("[UINT1:%d]", val);
940 int val = va_arg (ap, int);
941 printf ("[INT2:%d]", val);
945 int val = va_arg (ap, int);
946 printf ("[UINT2:%d]", val);
950 int val = va_arg (ap, int);
951 printf ("[INT4:%d]", val);
955 int val = va_arg (ap, int);
956 printf ("[UINT4:%d]", val);
960 gint64 val = va_arg (ap, gint64);
961 printf ("[INT:%ld]", val);
966 gint64 val = va_arg (ap, gint64);
967 printf ("[UINT:%lu]", val);
971 case MONO_TYPE_STRING: {
972 MonoString *s = va_arg (ap, MonoString *);
975 g_assert (((MonoObject *)s)->vtable->klass == mono_defaults.string_class);
976 printf ("[STRING:%p:%s]", s, mono_string_to_utf8 (s));
978 printf ("[STRING:null], ");
981 case MONO_TYPE_CLASS:
982 case MONO_TYPE_OBJECT: {
983 MonoObject *o = va_arg (ap, MonoObject *);
985 if ((o) && (o->vtable)) {
986 if (o->vtable->klass == mono_defaults.boolean_class) {
987 printf ("[BOOLEAN:%p:%d]", o, *((guint8 *)o + sizeof (MonoObject)));
988 } else if (o->vtable->klass == mono_defaults.int32_class) {
989 printf ("[INT32:%p:%d]", o, *((gint32 *)((char *)o + sizeof (MonoObject))));
990 } else if (o->vtable->klass == mono_defaults.int64_class) {
991 printf ("[INT64:%p:%ld]", o, *((gint64 *)((char *)o + sizeof (MonoObject))));
993 printf ("[%s.%s:%p]", o->vtable->klass->name_space, o->vtable->klass->name, o);
995 printf ("[OBJECT:%p]", o);
1000 case MONO_TYPE_FNPTR:
1001 case MONO_TYPE_ARRAY:
1002 case MONO_TYPE_SZARRAY: {
1003 gpointer p = va_arg (ap, gpointer);
1004 printf ("[result=%p]", p);
1007 case MONO_TYPE_I8: {
1008 gint64 l = va_arg (ap, gint64);
1009 printf ("[LONG:%ld]", l);
1012 case MONO_TYPE_U8: {
1013 guint64 l = va_arg (ap, guint64);
1014 printf ("[ULONG:%lu]", l);
1017 case MONO_TYPE_R4: {
1018 double f = va_arg (ap, double);
1019 printf ("[FLOAT4:%g]\n", f);
1022 case MONO_TYPE_R8: {
1023 double f = va_arg (ap, double);
1024 printf ("[FLOAT8:%g]\n", f);
1027 case MONO_TYPE_VALUETYPE: {
1028 MonoMarshalType *info;
1029 if (type->data.klass->enumtype) {
1030 type = mono_class_enum_basetype (type->data.klass);
1035 info = mono_marshal_load_type_info (type->data.klass);
1037 if ((info->native_size == sizeof(float)) &&
1038 (info->num_fields == 1) &&
1039 (info->fields[0].field->type->type == MONO_TYPE_R4)) {
1040 double f = va_arg (ap, double);
1041 printf("[FLOAT4:%g]\n", (double) f);
1045 if ((info->native_size == sizeof(double)) &&
1046 (info->num_fields == 1) &&
1047 (info->fields[0].field->type->type == MONO_TYPE_R8)) {
1048 double f = va_arg (ap, double);
1049 printf("[FLOAT8:%g]\n", f);
1053 size = mono_type_size (type, &align);
1056 guint32 p = va_arg (ap, guint32);
1057 printf ("[%02x]\n",p);
1061 guint32 p = va_arg (ap, guint32);
1062 printf ("[%04x]\n",p);
1066 guint32 p = va_arg (ap, guint32);
1067 printf ("[%08x]\n",p);
1071 guint64 p = va_arg (ap, guint64);
1072 printf ("[%016lx]\n",p);
1076 gpointer p = va_arg (ap, gpointer);
1077 printf ("[VALUETYPE] %p\n",p);
1083 case MONO_TYPE_TYPEDBYREF: {
1084 guint8 *p = va_arg (ap, gpointer);
1086 size = mono_type_size (type, &align);
1093 for (j = 0; p && j < size; j++)
1094 printf ("%02x,", p [j]);
1098 printf ("[TYPEDBYREF]\n");
1103 printf ("(unknown return type %x)",
1104 mono_method_signature (method)->ret->type);
1107 ip = ((gint64) __builtin_return_address (0));
1108 printf (" ip: %p\n", (gpointer) ip);
1111 /*========================= End of Function ========================*/
1113 /*------------------------------------------------------------------*/
1115 /* Name - catch_SIGILL */
1117 /* Function - Catch SIGILL as a result of testing for long */
1118 /* displacement facility. */
1120 /*------------------------------------------------------------------*/
1123 catch_SIGILL(int sigNo, siginfo_t *info, void *act) {
1129 /*========================= End of Function ========================*/
1131 /*------------------------------------------------------------------*/
1133 /* Name - mono_arch_cpu_init */
1135 /* Function - Perform CPU specific initialization to execute */
1138 /*------------------------------------------------------------------*/
1141 mono_arch_cpu_init (void)
1143 struct sigaction sa,
1147 /*--------------------------------------*/
1148 /* Set default rounding mode for FP */
1149 /*--------------------------------------*/
1150 __asm__ ("SRNM\t%0\n\t"
1153 /*--------------------------------------*/
1154 /* Determine if we have long displace- */
1155 /* ment facility on this processor */
1156 /*--------------------------------------*/
1157 sa.sa_sigaction = catch_SIGILL;
1158 sigemptyset (&sa.sa_mask);
1159 sa.sa_flags = SA_SIGINFO;
1161 sigaction (SIGILL, &sa, oldSa);
1163 /*--------------------------------------*/
1164 /* We test by executing the STY inst */
1165 /*--------------------------------------*/
1166 __asm__ ("LGHI\t0,1\n\t"
1168 ".byte\t0xe3,0x00,0x10,0x00,0x00,0x50\n\t"
1169 : "=m" (has_ld) : : "0", "1");
1171 sigaction (SIGILL, oldSa, NULL);
1174 /*========================= End of Function ========================*/
1176 /*------------------------------------------------------------------*/
1178 /* Name - mono_arch_init. */
1180 /* Function - Initialize architecture specific code. */
1182 /*------------------------------------------------------------------*/
1185 mono_arch_init (void)
1189 * When we do an architectural level set at z9 or better
1190 * we can use the STFLE instruction to show us
1191 * what hardware facilities are available
1193 int lFacility = sizeof(facs) % 8;
1195 memset((char *) &facs, 0, sizeof(facs));
1197 __asm__ (" lgfr 0,%1\n"
1199 : "=m" (facs) : "r" (lFacility) : "0", "cc");
1204 /*========================= End of Function ========================*/
1206 /*------------------------------------------------------------------*/
1208 /* Name - mono_arch_cleanup. */
1210 /* Function - Cleanup architecture specific code . */
1212 /*------------------------------------------------------------------*/
1215 mono_arch_cleanup (void)
1219 /*========================= End of Function ========================*/
1221 /*------------------------------------------------------------------*/
1223 /* Name - mono_arch_cpu_optimizazions */
1225 /* Function - Returns the optimizations supported on this CPU */
1227 /*------------------------------------------------------------------*/
1230 mono_arch_cpu_optimizazions (guint32 *exclude_mask)
1234 /*----------------------------------------------------------*/
1235 /* No s390-specific optimizations yet */
1236 /*----------------------------------------------------------*/
1237 *exclude_mask = MONO_OPT_INLINE|MONO_OPT_LINEARS;
1238 // *exclude_mask = MONO_OPT_INLINE;
1242 /*========================= End of Function ========================*/
1244 /*------------------------------------------------------------------*/
1250 /*------------------------------------------------------------------*/
1253 is_regsize_var (MonoType *t) {
1256 switch (mono_type_get_underlying_type (t)->type) {
1264 case MONO_TYPE_FNPTR:
1266 case MONO_TYPE_OBJECT:
1267 case MONO_TYPE_STRING:
1268 case MONO_TYPE_CLASS:
1269 case MONO_TYPE_SZARRAY:
1270 case MONO_TYPE_ARRAY:
1272 case MONO_TYPE_VALUETYPE:
1273 if (t->data.klass->enumtype)
1274 return is_regsize_var (mono_class_enum_basetype (t->data.klass));
1280 /*========================= End of Function ========================*/
1282 /*------------------------------------------------------------------*/
1284 /* Name - mono_arch_get_allocatable_int_vars */
1288 /*------------------------------------------------------------------*/
1291 mono_arch_get_allocatable_int_vars (MonoCompile *cfg)
1296 for (i = 0; i < cfg->num_varinfo; i++) {
1297 MonoInst *ins = cfg->varinfo [i];
1298 MonoMethodVar *vmv = MONO_VARINFO (cfg, i);
1301 if (vmv->range.first_use.abs_pos >= vmv->range.last_use.abs_pos)
1304 if (ins->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT) ||
1305 (ins->opcode != OP_LOCAL && ins->opcode != OP_ARG))
1308 /* we can only allocate 32 bit values */
1309 if (is_regsize_var (ins->inst_vtype)) {
1310 g_assert (MONO_VARINFO (cfg, i)->reg == -1);
1311 g_assert (i == vmv->idx);
1312 vars = mono_varlist_insert_sorted (cfg, vars, vmv, FALSE);
1319 /*========================= End of Function ========================*/
1321 /*------------------------------------------------------------------*/
1323 /* Name - mono_arch_global_int_regs */
1325 /* Function - Return a list of usable integer registers. */
1327 /*------------------------------------------------------------------*/
1330 mono_arch_get_global_int_regs (MonoCompile *cfg)
1333 MonoMethodHeader *header;
1336 header = cfg->header;
1337 if ((cfg->flags & MONO_CFG_HAS_ALLOCA) || header->num_clauses)
1338 cfg->frame_reg = s390_r11;
1341 /* FIXME: s390_r12 is reserved for bkchain_reg. Only reserve it if needed */
1343 for (i = 8; i < top; ++i) {
1344 if (cfg->frame_reg != i)
1345 regs = g_list_prepend (regs, GUINT_TO_POINTER (i));
1351 /*========================= End of Function ========================*/
1353 /*------------------------------------------------------------------*/
1355 /* Name - mono_arch_flush_icache */
1357 /* Function - Flush the CPU icache. */
1359 /*------------------------------------------------------------------*/
1362 mono_arch_flush_icache (guint8 *code, gint size)
1366 /*========================= End of Function ========================*/
1368 /*------------------------------------------------------------------*/
1370 /* Name - add_general */
1372 /* Function - Determine code and stack size incremements for a */
1375 /*------------------------------------------------------------------*/
1378 add_general (guint *gr, size_data *sz, ArgInfo *ainfo)
1380 if (*gr > S390_LAST_ARG_REG) {
1381 sz->stack_size = S390_ALIGN(sz->stack_size, sizeof(long));
1382 ainfo->offset = sz->stack_size;
1383 ainfo->reg = STK_BASE;
1384 ainfo->regtype = RegTypeBase;
1385 sz->stack_size += sizeof(long);
1386 sz->local_size += sizeof(long);
1387 sz->offStruct += sizeof(long);
1388 sz->code_size += 12;
1396 /*========================= End of Function ========================*/
1398 /*------------------------------------------------------------------*/
1400 /* Name - add_stackParm */
1402 /* Function - Determine code and stack size incremements for a */
1405 /*------------------------------------------------------------------*/
1408 add_stackParm (guint *gr, size_data *sz, ArgInfo *ainfo, gint size)
1410 if (*gr > S390_LAST_ARG_REG) {
1411 sz->stack_size = S390_ALIGN(sz->stack_size, sizeof(long));
1412 ainfo->reg = STK_BASE;
1413 ainfo->offset = sz->stack_size;
1414 ainfo->regtype = RegTypeStructByAddrOnStack;
1415 sz->stack_size += sizeof (gpointer);
1416 sz->parm_size += sizeof(gpointer);
1417 sz->offStruct += sizeof(gpointer);
1420 ainfo->offset = sz->stack_size;
1421 ainfo->regtype = RegTypeStructByAddr;
1424 ainfo->offparm = sz->offset;
1425 sz->offset = S390_ALIGN(sz->offset+size, sizeof(long));
1427 ainfo->vtsize = size;
1428 sz->parm_size += size;
1431 /*========================= End of Function ========================*/
1433 /*------------------------------------------------------------------*/
1435 /* Name - add_float */
1437 /* Function - Determine code and stack size incremements for a */
1438 /* float parameter. */
1440 /*------------------------------------------------------------------*/
1443 add_float (guint *fr, size_data *sz, ArgInfo *ainfo)
1445 if ((*fr) <= S390_LAST_FPARG_REG) {
1446 ainfo->regtype = RegTypeFP;
1452 ainfo->offset = sz->stack_size;
1453 ainfo->reg = STK_BASE;
1454 ainfo->regtype = RegTypeBase;
1456 sz->stack_size += sizeof(double);
1457 sz->local_size += sizeof(double);
1458 sz->offStruct += sizeof(double);
1462 /*========================= End of Function ========================*/
1464 /*------------------------------------------------------------------*/
1466 /* Name - get_call_info */
1468 /* Function - Determine the amount of space required for code */
1469 /* and stack. In addition determine starting points */
1470 /* for stack-based parameters, and area for struct- */
1471 /* ures being returned on the stack. */
1473 /*------------------------------------------------------------------*/
1476 get_call_info (MonoCompile *cfg, MonoMemPool *mp, MonoMethodSignature *sig, gboolean is_pinvoke)
1478 guint i, fr, gr, size, pstart;
1479 int nParm = sig->hasthis + sig->param_count;
1481 guint32 simpletype, align;
1484 MonoGenericSharingContext *gsctx = cfg ? cfg->generic_sharing_context : NULL;
1487 cinfo = mono_mempool_alloc0 (mp, sizeof (CallInfo) + sizeof (ArgInfo) * nParm);
1489 cinfo = g_malloc0 (sizeof (CallInfo) + sizeof (ArgInfo) * nParm);
1494 cinfo->struct_ret = 0;
1498 sz->offStruct = S390_MINIMAL_STACK_SIZE;
1499 sz->stack_size = S390_MINIMAL_STACK_SIZE;
1504 /*----------------------------------------------------------*/
1505 /* We determine the size of the return code/stack in case we*/
1506 /* need to reserve a register to be used to address a stack */
1507 /* area that the callee will use. */
1508 /*----------------------------------------------------------*/
1510 ret_type = mono_type_get_underlying_type (sig->ret);
1511 ret_type = mini_get_basic_type_from_generic (gsctx, ret_type);
1512 simpletype = ret_type->type;
1514 switch (simpletype) {
1515 case MONO_TYPE_BOOLEAN:
1520 case MONO_TYPE_CHAR:
1525 case MONO_TYPE_CLASS:
1526 case MONO_TYPE_OBJECT:
1527 case MONO_TYPE_SZARRAY:
1528 case MONO_TYPE_ARRAY:
1530 case MONO_TYPE_FNPTR:
1531 case MONO_TYPE_STRING:
1532 cinfo->ret.reg = s390_r2;
1537 cinfo->ret.reg = s390_f0;
1542 cinfo->ret.reg = s390_r2;
1545 case MONO_TYPE_GENERICINST:
1546 if (!mono_type_generic_inst_is_valuetype (sig->ret)) {
1547 cinfo->ret.reg = s390_r2;
1552 case MONO_TYPE_VALUETYPE: {
1553 MonoClass *klass = mono_class_from_mono_type (sig->ret);
1554 if (klass->enumtype) {
1555 simpletype = mono_class_enum_basetype (klass)->type;
1559 size = mono_class_native_size (klass, &align);
1561 size = mono_class_value_size (klass, &align);
1563 cinfo->struct_ret = 1;
1564 cinfo->ret.size = size;
1565 cinfo->ret.vtsize = size;
1568 case MONO_TYPE_TYPEDBYREF:
1569 size = sizeof (MonoTypedRef);
1570 cinfo->struct_ret = 1;
1571 cinfo->ret.size = size;
1572 cinfo->ret.vtsize = size;
1574 case MONO_TYPE_VOID:
1577 g_error ("Can't handle as return value 0x%x", sig->ret->type);
1583 * To simplify get_this_arg_reg () and LLVM integration, emit the vret arg after
1584 * the first argument, allowing 'this' to be always passed in the first arg reg.
1585 * Also do this if the first argument is a reference type, since virtual calls
1586 * are sometimes made using calli without sig->hasthis set, like in the delegate
1589 if (cinfo->struct_ret && !is_pinvoke &&
1591 (sig->param_count > 0 &&
1592 MONO_TYPE_IS_REFERENCE (mini_type_get_underlying_type (gsctx, sig->params [0]))))) {
1594 cinfo->args[nParm].size = sizeof (gpointer);
1595 add_general (&gr, sz, cinfo->args + nParm);
1597 cinfo->args[nParm].size = sizeof (gpointer);
1598 add_general (&gr, sz, &cinfo->args [sig->hasthis + nParm]);
1602 cinfo->vret_arg_index = 1;
1603 cinfo->ret.reg = gr;
1608 cinfo->args[nParm].size = sizeof (gpointer);
1609 add_general (&gr, sz, cinfo->args + nParm);
1613 if (cinfo->struct_ret) {
1614 cinfo->ret.reg = gr;
1619 if ((sig->call_convention == MONO_CALL_VARARG) && (sig->param_count == 0)) {
1620 gr = S390_LAST_ARG_REG + 1;
1622 /* Emit the signature cookie just before the implicit arguments */
1623 add_general (&gr, sz, &cinfo->sigCookie);
1626 /*----------------------------------------------------------*/
1627 /* We determine the size of the parameter code and stack */
1628 /* requirements by checking the types and sizes of the */
1630 /*----------------------------------------------------------*/
1632 for (i = pstart; i < sig->param_count; ++i) {
1635 /*--------------------------------------------------*/
1636 /* Handle vararg type calls. All args are put on */
1638 /*--------------------------------------------------*/
1639 if ((sig->call_convention == MONO_CALL_VARARG) &&
1640 (i == sig->sentinelpos)) {
1641 gr = S390_LAST_ARG_REG + 1;
1642 add_general (&gr, sz, &cinfo->sigCookie);
1645 if (sig->params [i]->byref) {
1646 add_general (&gr, sz, cinfo->args+nParm);
1647 cinfo->args[nParm].size = sizeof(gpointer);
1652 ptype = mono_type_get_underlying_type (sig->params [i]);
1653 ptype = mini_get_basic_type_from_generic (gsctx, ptype);
1654 simpletype = ptype->type;
1655 cinfo->args[nParm].type = simpletype;
1656 switch (simpletype) {
1657 case MONO_TYPE_BOOLEAN:
1660 cinfo->args[nParm].size = sizeof(char);
1661 add_general (&gr, sz, cinfo->args+nParm);
1666 case MONO_TYPE_CHAR:
1667 cinfo->args[nParm].size = sizeof(short);
1668 add_general (&gr, sz, cinfo->args+nParm);
1673 cinfo->args[nParm].size = sizeof(int);
1674 add_general (&gr, sz, cinfo->args+nParm);
1680 case MONO_TYPE_FNPTR:
1681 case MONO_TYPE_CLASS:
1682 case MONO_TYPE_OBJECT:
1683 case MONO_TYPE_STRING:
1684 case MONO_TYPE_SZARRAY:
1685 case MONO_TYPE_ARRAY:
1686 cinfo->args[nParm].size = sizeof(gpointer);
1687 add_general (&gr, sz, cinfo->args+nParm);
1692 cinfo->args[nParm].size = sizeof(long long);
1693 add_general (&gr, sz, cinfo->args+nParm);
1697 cinfo->args[nParm].size = sizeof(float);
1698 add_float (&fr, sz, cinfo->args+nParm);
1702 cinfo->args[nParm].size = sizeof(double);
1703 add_float (&fr, sz, cinfo->args+nParm);
1706 case MONO_TYPE_GENERICINST:
1707 if (!mono_type_generic_inst_is_valuetype (sig->params [i])) {
1708 cinfo->args[nParm].size = sizeof(gpointer);
1709 add_general (&gr, sz, cinfo->args+nParm);
1714 case MONO_TYPE_VALUETYPE: {
1715 MonoMarshalType *info;
1716 MonoClass *klass = mono_class_from_mono_type (sig->params [i]);
1718 size = mono_class_native_size (klass, &align);
1720 size = mono_class_value_size (klass, &align);
1722 info = mono_marshal_load_type_info (klass);
1724 if ((info->native_size == sizeof(float)) &&
1725 (info->num_fields == 1) &&
1726 (info->fields[0].field->type->type == MONO_TYPE_R4)) {
1727 cinfo->args[nParm].size = sizeof(float);
1728 add_float(&fr, sz, cinfo->args+nParm);
1733 if ((info->native_size == sizeof(double)) &&
1734 (info->num_fields == 1) &&
1735 (info->fields[0].field->type->type == MONO_TYPE_R8)) {
1736 cinfo->args[nParm].size = sizeof(double);
1737 add_float(&fr, sz, cinfo->args+nParm);
1742 cinfo->args[nParm].vtsize = 0;
1743 cinfo->args[nParm].size = 0;
1746 /*----------------------------------*/
1747 /* On S/390, structures of size 1, */
1748 /* 2, 4, and 8 bytes are passed in */
1749 /* (a) register(s). */
1750 /*----------------------------------*/
1756 add_general(&gr, sz, cinfo->args+nParm);
1757 cinfo->args[nParm].size = size;
1758 cinfo->args[nParm].regtype = RegTypeStructByVal;
1760 sz->local_size += sizeof(long);
1763 add_stackParm(&gr, sz, cinfo->args+nParm, size);
1768 case MONO_TYPE_TYPEDBYREF: {
1769 int size = sizeof (MonoTypedRef);
1771 cinfo->args[nParm].vtsize = 0;
1772 cinfo->args[nParm].size = 0;
1775 /*----------------------------------*/
1776 /* On S/390, structures of size 1, */
1777 /* 2, 4, and 8 bytes are passed in */
1778 /* (a) register(s). */
1779 /*----------------------------------*/
1785 add_general(&gr, sz, cinfo->args+nParm);
1786 cinfo->args[nParm].size = size;
1787 cinfo->args[nParm].regtype = RegTypeStructByVal;
1789 sz->local_size += sizeof(long);
1792 add_stackParm(&gr, sz, cinfo->args+nParm, size);
1798 g_error ("Can't trampoline 0x%x", sig->params [i]->type);
1802 /*----------------------------------------------------------*/
1803 /* Handle the case where there are no implicit arguments */
1804 /*----------------------------------------------------------*/
1805 if ((sig->call_convention == MONO_CALL_VARARG) &&
1808 (sig->param_count == sig->sentinelpos)) {
1809 gr = S390_LAST_ARG_REG + 1;
1810 add_general (&gr, sz, &cinfo->sigCookie);
1813 /*----------------------------------------------------------*/
1814 /* If we are passing a structure back then if it won't be */
1815 /* in a register(s) then we make room at the end of the */
1816 /* parameters that may have been placed on the stack */
1817 /*----------------------------------------------------------*/
1818 if (cinfo->struct_ret) {
1819 cinfo->ret.offset = sz->stack_size;
1820 switch (cinfo->ret.size) {
1828 sz->stack_size += S390_ALIGN(cinfo->ret.size, align);
1833 sz->stack_size = sz->stack_size + sz->local_size + sz->parm_size +
1835 sz->stack_size = S390_ALIGN(sz->stack_size, sizeof(long));
1840 /*========================= End of Function ========================*/
1842 /*------------------------------------------------------------------*/
1844 /* Name - mono_arch_allocate_vars */
1846 /* Function - Set var information according to the calling */
1847 /* convention for S/390. The local var stuff should */
1848 /* most likely be split in another method. */
1850 /* Parameter - @m - Compile unit. */
1852 /*------------------------------------------------------------------*/
1855 mono_arch_allocate_vars (MonoCompile *cfg)
1857 MonoMethodSignature *sig;
1858 MonoMethodHeader *header;
1861 int iParm, iVar, offset, align, size, curinst;
1862 int frame_reg = STK_BASE;
1865 header = cfg->header;
1867 cfg->flags |= MONO_CFG_HAS_SPILLUP;
1869 /*---------------------------------------------------------*/
1870 /* We use the frame register also for any method that has */
1871 /* filter clauses. This way, when the handlers are called, */
1872 /* the code will reference local variables using the frame */
1873 /* reg instead of the stack pointer: if we had to restore */
1874 /* the stack pointer, we'd corrupt the method frames that */
1875 /* are already on the stack (since filters get called */
1876 /* before stack unwinding happens) when the filter code */
1877 /* would call any method. */
1878 /*---------------------------------------------------------*/
1879 if ((cfg->flags & MONO_CFG_HAS_ALLOCA) || header->num_clauses)
1880 frame_reg = s390_r11;
1882 cfg->frame_reg = frame_reg;
1884 cfg->arch.bkchain_reg = -1;
1886 if (frame_reg != STK_BASE)
1887 cfg->used_int_regs |= 1 << frame_reg;
1889 sig = mono_method_signature (cfg->method);
1891 cinfo = get_call_info (cfg, cfg->mempool, sig, sig->pinvoke);
1893 if (!cinfo->struct_ret) {
1894 switch (mono_type_get_underlying_type (sig->ret)->type) {
1895 case MONO_TYPE_VOID:
1898 cfg->ret->opcode = OP_REGVAR;
1899 cfg->ret->dreg = s390_r2;
1904 /*--------------------------------------------------------------*/
1905 /* local vars are at a positive offset from the stack pointer */
1906 /* also note that if the function uses alloca, we use s390_r11 */
1907 /* to point at the local variables. */
1908 /* add parameter area size for called functions */
1909 /*--------------------------------------------------------------*/
1910 if (cfg->param_area == 0)
1911 offset = S390_MINIMAL_STACK_SIZE;
1913 offset = cfg->param_area;
1915 cfg->sig_cookie = 0;
1917 if (cinfo->struct_ret) {
1918 inst = cfg->vret_addr;
1919 offset = S390_ALIGN(offset, sizeof(gpointer));
1920 inst->inst_offset = offset;
1921 inst->opcode = OP_REGOFFSET;
1922 inst->inst_basereg = frame_reg;
1923 offset += sizeof(gpointer);
1924 if (G_UNLIKELY (cfg->verbose_level > 1)) {
1925 printf ("vret_addr =");
1926 mono_print_ins (cfg->vret_addr);
1931 inst = cfg->args [0];
1932 if (inst->opcode != OP_REGVAR) {
1933 inst->opcode = OP_REGOFFSET;
1934 inst->inst_basereg = frame_reg;
1935 offset = S390_ALIGN(offset, sizeof(gpointer));
1936 inst->inst_offset = offset;
1937 offset += sizeof (gpointer);
1944 eArg = sig->param_count + sArg;
1946 if (sig->call_convention == MONO_CALL_VARARG)
1947 cfg->sig_cookie += S390_MINIMAL_STACK_SIZE;
1949 for (iParm = sArg; iParm < eArg; ++iParm) {
1950 inst = cfg->args [curinst];
1951 if (inst->opcode != OP_REGVAR) {
1952 switch (cinfo->args[iParm].regtype) {
1953 case RegTypeStructByAddr : {
1956 size = sizeof (gpointer);
1958 inst->opcode = OP_REGOFFSET;
1959 inst->inst_basereg = frame_reg;
1960 offset = S390_ALIGN (offset, sizeof (gpointer));
1961 inst->inst_offset = offset;
1963 /* Add a level of indirection */
1964 MONO_INST_NEW (cfg, indir, 0);
1966 inst->opcode = OP_VTARG_ADDR;
1967 inst->inst_left = indir;
1970 case RegTypeStructByAddrOnStack : {
1973 size = sizeof (gpointer);
1975 /* Similar to the == STK_BASE case below */
1976 cfg->arch.bkchain_reg = s390_r12;
1977 cfg->used_int_regs |= 1 << cfg->arch.bkchain_reg;
1979 inst->opcode = OP_REGOFFSET;
1980 inst->dreg = mono_alloc_preg (cfg);
1981 inst->inst_basereg = cfg->arch.bkchain_reg;
1982 inst->inst_offset = cinfo->args [iParm].offset;
1984 /* Add a level of indirection */
1985 MONO_INST_NEW (cfg, indir, 0);
1987 inst->opcode = OP_VTARG_ADDR;
1988 inst->inst_left = indir;
1991 case RegTypeStructByVal :
1992 size = cinfo->args[iParm].size;
1993 offset = S390_ALIGN(offset, size);
1994 inst->opcode = OP_REGOFFSET;
1995 inst->inst_basereg = frame_reg;
1996 inst->inst_offset = offset;
1999 if (cinfo->args [iParm].reg == STK_BASE) {
2001 * These arguments are in the previous frame, so we can't
2002 * compute their offset from the current frame pointer right
2003 * now, since cfg->stack_offset is not yet known, so dedicate a
2004 * register holding the previous frame pointer.
2006 cfg->arch.bkchain_reg = s390_r12;
2007 cfg->used_int_regs |= 1 << cfg->arch.bkchain_reg;
2009 inst->opcode = OP_REGOFFSET;
2010 inst->inst_basereg = cfg->arch.bkchain_reg;
2011 size = (cinfo->args[iParm].size < 8
2012 ? 8 - cinfo->args[iParm].size
2014 inst->inst_offset = cinfo->args [iParm].offset + size;
2015 size = sizeof (long);
2017 inst->opcode = OP_REGOFFSET;
2018 inst->inst_basereg = frame_reg;
2019 size = (cinfo->args[iParm].size < 8
2022 offset = S390_ALIGN(offset, size);
2023 if (cfg->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE)
2024 inst->inst_offset = offset;
2026 inst->inst_offset = offset + (8 - size);
2030 if ((sig->call_convention == MONO_CALL_VARARG) &&
2031 (cinfo->args[iParm].regtype != RegTypeGeneral) &&
2032 (iParm < sig->sentinelpos))
2033 cfg->sig_cookie += size;
2035 offset += MAX(size, 8);
2040 curinst = cfg->locals_start;
2041 for (iVar = curinst; iVar < cfg->num_varinfo; ++iVar) {
2042 inst = cfg->varinfo [iVar];
2043 if ((inst->flags & MONO_INST_IS_DEAD) ||
2044 (inst->opcode == OP_REGVAR))
2047 /*--------------------------------------------------*/
2048 /* inst->backend.is_pinvoke indicates native sized */
2049 /* value typs this is used by the pinvoke wrappers */
2050 /* when they call functions returning structure */
2051 /*--------------------------------------------------*/
2052 if (inst->backend.is_pinvoke && MONO_TYPE_ISSTRUCT (inst->inst_vtype))
2053 size = mono_class_native_size (mono_class_from_mono_type(inst->inst_vtype),
2054 (guint32 *) &align);
2056 size = mono_type_size (inst->inst_vtype, &align);
2058 offset = S390_ALIGN(offset, align);
2059 inst->inst_offset = offset;
2060 inst->opcode = OP_REGOFFSET;
2061 inst->inst_basereg = frame_reg;
2063 DEBUG (g_print("allocating local %d to %ld, size: %d\n",
2064 iVar, inst->inst_offset, size));
2067 /*------------------------------------------------------*/
2068 /* Allow space for the trace method stack area if needed*/
2069 /*------------------------------------------------------*/
2070 if (mono_jit_trace_calls != NULL && mono_trace_eval (cfg->method))
2071 offset += S390_TRACE_STACK_SIZE;
2073 /*------------------------------------------------------*/
2074 /* Reserve space to save LMF and caller saved registers */
2075 /*------------------------------------------------------*/
2076 if (cfg->method->save_lmf)
2077 offset += sizeof (MonoLMF);
2079 /*------------------------------------------------------*/
2080 /* align the offset */
2081 /*------------------------------------------------------*/
2082 cfg->stack_offset = S390_ALIGN(offset, S390_STACK_ALIGNMENT);
2084 /*------------------------------------------------------*/
2085 /* Fix offsets for args whose value is in parent frame */
2086 /*------------------------------------------------------*/
2087 for (iParm = sArg; iParm < eArg; ++iParm) {
2088 inst = cfg->args [iParm];
2090 if (inst->opcode == OP_S390_STKARG) {
2091 inst->opcode = OP_REGOFFSET;
2092 inst->inst_offset += cfg->stack_offset;
2097 /*========================= End of Function ========================*/
2099 /*------------------------------------------------------------------*/
2101 /* Name - mono_arch_create_vars */
2103 /*------------------------------------------------------------------*/
2106 mono_arch_create_vars (MonoCompile *cfg)
2108 MonoMethodSignature *sig;
2111 sig = mono_method_signature (cfg->method);
2113 cinfo = get_call_info (cfg, cfg->mempool, sig, sig->pinvoke);
2115 if (cinfo->struct_ret) {
2116 cfg->vret_addr = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_ARG);
2117 if (G_UNLIKELY (cfg->verbose_level > 1)) {
2118 printf ("vret_addr = ");
2119 mono_print_ins (cfg->vret_addr);
2124 /*========================= End of Function ========================*/
2126 /*------------------------------------------------------------------*/
2128 /* Name - add_outarg_reg2. */
2130 /*------------------------------------------------------------------*/
2133 add_outarg_reg2 (MonoCompile *cfg, MonoCallInst *call, ArgStorage storage, int reg, MonoInst *tree)
2138 case RegTypeGeneral:
2139 MONO_INST_NEW (cfg, ins, OP_MOVE);
2140 ins->dreg = mono_alloc_ireg (cfg);
2141 ins->sreg1 = tree->dreg;
2142 MONO_ADD_INS (cfg->cbb, ins);
2143 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, reg, FALSE);
2146 MONO_INST_NEW (cfg, ins, OP_FMOVE);
2147 ins->dreg = mono_alloc_freg (cfg);
2148 ins->sreg1 = tree->dreg;
2149 MONO_ADD_INS (cfg->cbb, ins);
2150 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, reg, TRUE);
2153 MONO_INST_NEW (cfg, ins, OP_S390_SETF4RET);
2154 ins->dreg = mono_alloc_freg (cfg);
2155 ins->sreg1 = tree->dreg;
2156 MONO_ADD_INS (cfg->cbb, ins);
2157 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, reg, TRUE);
2160 g_assert_not_reached ();
2164 /*========================= End of Function ========================*/
2166 /*------------------------------------------------------------------*/
2168 /* Name - emit_sig_cookie. */
2170 /*------------------------------------------------------------------*/
2173 emit_sig_cookie (MonoCompile *cfg, MonoCallInst *call, CallInfo *cinfo)
2175 MonoMethodSignature *tmpSig;
2178 cfg->disable_aot = TRUE;
2180 /*----------------------------------------------------------*/
2181 /* mono_ArgIterator_Setup assumes the signature cookie is */
2182 /* passed first and all the arguments which were before it */
2183 /* passed on the stack after the signature. So compensate */
2184 /* by passing a different signature. */
2185 /*----------------------------------------------------------*/
2186 tmpSig = mono_metadata_signature_dup (call->signature);
2187 tmpSig->param_count -= call->signature->sentinelpos;
2188 tmpSig->sentinelpos = 0;
2189 if (tmpSig->param_count > 0)
2190 memcpy (tmpSig->params,
2191 call->signature->params + call->signature->sentinelpos,
2192 tmpSig->param_count * sizeof(MonoType *));
2194 MONO_INST_NEW (cfg, sig_arg, OP_ICONST);
2195 sig_arg->dreg = mono_alloc_ireg (cfg);
2196 sig_arg->inst_p0 = tmpSig;
2197 MONO_ADD_INS (cfg->cbb, sig_arg);
2199 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, STK_BASE,
2200 cinfo->sigCookie.offset, sig_arg->dreg);
2203 /*========================= End of Function ========================*/
2205 /*------------------------------------------------------------------*/
2207 /* Name - mono_arch_emit_call */
2209 /*------------------------------------------------------------------*/
2212 mono_arch_emit_call (MonoCompile *cfg, MonoCallInst *call)
2215 MonoMethodSignature *sig;
2217 int i, n, lParamArea;
2219 ArgInfo *ainfo = NULL;
2221 MonoMethodHeader *header;
2224 sig = call->signature;
2225 n = sig->param_count + sig->hasthis;
2226 DEBUG (g_print ("Call requires: %d parameters\n",n));
2228 cinfo = get_call_info (cfg, cfg->mempool, sig, sig->pinvoke);
2230 stackSize = cinfo->sz.stack_size + cinfo->sz.local_size +
2231 cinfo->sz.parm_size + cinfo->sz.offset;
2232 call->stack_usage = MAX(stackSize, call->stack_usage);
2233 lParamArea = MAX((call->stack_usage-S390_MINIMAL_STACK_SIZE-cinfo->sz.parm_size), 0);
2234 cfg->param_area = MAX(((signed) cfg->param_area), lParamArea);
2235 cfg->flags |= MONO_CFG_HAS_CALLS;
2237 if (cinfo->struct_ret) {
2238 MONO_INST_NEW (cfg, ins, OP_MOVE);
2239 ins->sreg1 = call->vret_var->dreg;
2240 ins->dreg = mono_alloc_preg (cfg);
2241 MONO_ADD_INS (cfg->cbb, ins);
2242 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, cinfo->ret.reg, FALSE);
2245 header = cfg->header;
2246 if ((cfg->flags & MONO_CFG_HAS_ALLOCA) || header->num_clauses)
2251 for (i = 0; i < n; ++i) {
2254 ainfo = cinfo->args + i;
2255 if (i >= sig->hasthis)
2256 t = sig->params [i - sig->hasthis];
2258 t = &mono_defaults.int_class->byval_arg;
2259 t = mono_type_get_underlying_type (t);
2261 in = call->args [i];
2263 if ((sig->call_convention == MONO_CALL_VARARG) &&
2265 (i == sig->sentinelpos)) {
2266 emit_sig_cookie (cfg, call, cinfo);
2269 switch (ainfo->regtype) {
2270 case RegTypeGeneral:
2271 add_outarg_reg2 (cfg, call, ainfo->regtype, ainfo->reg, in);
2274 if (MONO_TYPE_ISSTRUCT (t)) {
2275 /* Valuetype passed in one fp register */
2276 ainfo->regtype = RegTypeStructByValInFP;
2279 if (ainfo->size == 4)
2280 ainfo->regtype = RegTypeFPR4;
2281 add_outarg_reg2 (cfg, call, ainfo->regtype, ainfo->reg, in);
2284 case RegTypeStructByVal:
2285 case RegTypeStructByAddr:
2286 case RegTypeStructByAddrOnStack: {
2290 if (sig->params [i - sig->hasthis]->type == MONO_TYPE_TYPEDBYREF) {
2291 size = sizeof (MonoTypedRef);
2292 align = sizeof (gpointer);
2296 size = mono_type_native_stack_size (&in->klass->byval_arg, &align);
2299 * Other backends use mono_type_stack_size (), but that
2300 * aligns the size to 8, which is larger than the size of
2301 * the source, leading to reads of invalid memory if the
2302 * source is at the end of address space.
2304 size = mono_class_value_size (in->klass, &align);
2307 g_assert (in->klass);
2309 ainfo->offparm += cinfo->sz.offStruct;
2311 MONO_INST_NEW (cfg, ins, OP_OUTARG_VT);
2312 ins->sreg1 = in->dreg;
2313 ins->klass = in->klass;
2314 ins->backend.size = ainfo->size;
2315 ins->inst_p0 = call;
2316 ins->inst_p1 = mono_mempool_alloc (cfg->mempool, sizeof (ArgInfo));
2317 memcpy (ins->inst_p1, ainfo, sizeof (ArgInfo));
2319 MONO_ADD_INS (cfg->cbb, ins);
2321 if (ainfo->regtype == RegTypeStructByAddr) {
2323 * We use OP_OUTARG_VT to copy the valuetype to a stack location, then
2324 * use the normal OUTARG opcodes to pass the address of the location to
2327 int treg = mono_alloc_preg (cfg);
2328 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ADD_IMM, treg,
2329 frmReg, ainfo->offparm);
2330 mono_call_inst_add_outarg_reg (cfg, call, treg, ainfo->reg, FALSE);
2331 } else if (ainfo->regtype == RegTypeStructByAddrOnStack) {
2332 /* The address of the valuetype is passed on the stack */
2333 int treg = mono_alloc_preg (cfg);
2334 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ADD_IMM, treg,
2335 frmReg, ainfo->offparm);
2336 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG,
2337 ainfo->reg, ainfo->offset, treg);
2342 if (!t->byref && t->type == MONO_TYPE_R4) {
2343 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORER4_MEMBASE_REG,
2344 STK_BASE, ainfo->offset + 4,
2346 } else if (!t->byref && (t->type == MONO_TYPE_R8)) {
2347 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORER8_MEMBASE_REG,
2348 STK_BASE, ainfo->offset,
2351 MONO_INST_NEW (cfg, ins, OP_STORE_MEMBASE_REG);
2352 ins->inst_destbasereg = STK_BASE;
2353 ins->inst_offset = ainfo->offset;
2354 ins->sreg1 = in->dreg;
2357 /* This is needed by MonoTypedRef->value to point to the correct data */
2358 if ((sig->call_convention == MONO_CALL_VARARG) &&
2359 (i >= sig->sentinelpos)) {
2360 switch (ainfo->size) {
2362 ins->opcode = OP_STOREI1_MEMBASE_REG;
2365 ins->opcode = OP_STOREI2_MEMBASE_REG;
2368 ins->opcode = OP_STOREI4_MEMBASE_REG;
2376 MONO_ADD_INS (cfg->cbb, ins);
2380 g_assert_not_reached ();
2386 * Handle the case where there are no implicit arguments
2388 if ((sig->call_convention == MONO_CALL_VARARG) &&
2390 (i == sig->sentinelpos)) {
2391 emit_sig_cookie (cfg, call, cinfo);
2395 /*========================= End of Function ========================*/
2397 /*------------------------------------------------------------------*/
2399 /* Name - mono_arch_emit_outarg_vt */
2401 /*------------------------------------------------------------------*/
2404 mono_arch_emit_outarg_vt (MonoCompile *cfg, MonoInst *ins, MonoInst *src)
2406 MonoCallInst *call = (MonoCallInst*)ins->inst_p0;
2407 ArgInfo *ainfo = (ArgInfo*)ins->inst_p1;
2408 int size = ins->backend.size;
2410 if (ainfo->regtype == RegTypeStructByVal) {
2412 arg->ins.sreg1 = ainfo->reg;
2413 arg->ins.opcode = OP_OUTARG_VT;
2414 arg->size = ainfo->size;
2415 arg->offset = ainfo->offset;
2416 arg->offPrm = ainfo->offparm + cinfo->sz.offStruct;
2418 if (ainfo->reg != STK_BASE) {
2419 MONO_OUTPUT_VTR (cfg, size, ainfo->reg, src->dreg, 0);
2421 MONO_OUTPUT_VTS (cfg, size, ainfo->reg, ainfo->offset,
2424 } else if (ainfo->regtype == RegTypeStructByValInFP) {
2425 int dreg = mono_alloc_freg (cfg);
2427 if (ainfo->size == 4) {
2428 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADR4_MEMBASE, dreg, src->dreg, 0);
2429 MONO_EMIT_NEW_UNALU (cfg, OP_S390_SETF4RET, dreg, dreg);
2431 g_assert (ainfo->size == 8);
2433 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADR8_MEMBASE, dreg, src->dreg, 0);
2436 mono_call_inst_add_outarg_reg (cfg, call, dreg, ainfo->reg, TRUE);
2438 MonoMethodHeader *header;
2441 header = mono_method_get_header (cfg->method);
2442 if ((cfg->flags & MONO_CFG_HAS_ALLOCA) || header->num_clauses)
2447 MONO_EMIT_NEW_MOVE (cfg, srcReg, ainfo->offparm,
2448 src->dreg, 0, size);
2452 /*========================= End of Function ========================*/
2454 /*------------------------------------------------------------------*/
2456 /* Name - mono_arch_emit_setret */
2458 /*------------------------------------------------------------------*/
2461 mono_arch_emit_setret (MonoCompile *cfg, MonoMethod *method, MonoInst *val)
2463 MonoType *ret = mono_type_get_underlying_type (mono_method_signature (method)->ret);
2466 if (ret->type == MONO_TYPE_R4) {
2467 MONO_EMIT_NEW_UNALU (cfg, OP_S390_SETF4RET, s390_f0, val->dreg);
2469 } else if (ret->type == MONO_TYPE_R8) {
2470 MONO_EMIT_NEW_UNALU (cfg, OP_FMOVE, s390_f0, val->dreg);
2475 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, cfg->ret->dreg, val->dreg);
2478 /*========================= End of Function ========================*/
2480 /*------------------------------------------------------------------*/
2482 /* Name - mono_arch_instrument_mem_needs */
2484 /* Function - Allow tracing to work with this interface (with */
2485 /* an optional argument). */
2487 /*------------------------------------------------------------------*/
2490 mono_arch_instrument_mem_needs (MonoMethod *method, int *stack, int *code)
2492 /* no stack room needed now (may be needed for FASTCALL-trace support) */
2494 /* split prolog-epilog requirements? */
2495 *code = 50; /* max bytes needed: check this number */
2498 /*========================= End of Function ========================*/
2500 /*------------------------------------------------------------------*/
2502 /* Name - mono_arch_instrument_prolog */
2504 /* Function - Create an "instrumented" prolog. */
2506 /*------------------------------------------------------------------*/
2509 mono_arch_instrument_prolog (MonoCompile *cfg, void *func, void *p,
2510 gboolean enable_arguments)
2517 parmOffset = cfg->stack_usage - S390_TRACE_STACK_SIZE;
2518 if (cfg->method->save_lmf)
2519 parmOffset -= sizeof(MonoLMF);
2520 fpOffset = parmOffset + (5*sizeof(gpointer));
2521 if ((!has_ld) && (fpOffset > 4096)) {
2522 s390_lgr (code, s390_r12, STK_BASE);
2524 while (fpOffset > 4096) {
2525 s390_aghi (code, baseReg, 4096);
2533 s390_stmg (code, s390_r2, s390_r6, STK_BASE, parmOffset);
2535 s390_stdy (code, s390_f0, 0, STK_BASE, fpOffset);
2536 s390_stdy (code, s390_f2, 0, STK_BASE, fpOffset+sizeof(gdouble));
2537 s390_stdy (code, s390_f4, 0, STK_BASE, fpOffset+2*sizeof(gdouble));
2538 s390_stdy (code, s390_f6, 0, STK_BASE, fpOffset+3*sizeof(gdouble));
2540 s390_std (code, s390_f0, 0, baseReg, fpOffset);
2541 s390_std (code, s390_f2, 0, baseReg, fpOffset+sizeof(gdouble));
2542 s390_std (code, s390_f4, 0, baseReg, fpOffset+2*sizeof(gdouble));
2543 s390_std (code, s390_f6, 0, baseReg, fpOffset+3*sizeof(gdouble));
2545 s390_basr (code, s390_r13, 0);
2547 s390_llong(code, cfg->method);
2548 s390_llong(code, func);
2549 s390_lg (code, s390_r2, 0, s390_r13, 4);
2551 s390_lay (code, s390_r3, 0, STK_BASE, parmOffset);
2553 s390_la (code, s390_r3, 0, baseReg, parmOffset);
2554 s390_lgr (code, s390_r4, STK_BASE);
2555 s390_aghi (code, s390_r4, cfg->stack_usage);
2556 s390_lg (code, s390_r1, 0, s390_r13, 12);
2557 s390_basr (code, s390_r14, s390_r1);
2559 s390_ldy (code, s390_f6, 0, STK_BASE, fpOffset+3*sizeof(gdouble));
2560 s390_ldy (code, s390_f4, 0, STK_BASE, fpOffset+2*sizeof(gdouble));
2561 s390_ldy (code, s390_f2, 0, STK_BASE, fpOffset+sizeof(gdouble));
2562 s390_ldy (code, s390_f0, 0, STK_BASE, fpOffset);
2564 s390_ld (code, s390_f6, 0, baseReg, fpOffset+3*sizeof(gdouble));
2565 s390_ld (code, s390_f4, 0, baseReg, fpOffset+2*sizeof(gdouble));
2566 s390_ld (code, s390_f2, 0, baseReg, fpOffset+sizeof(gdouble));
2567 s390_ld (code, s390_f0, 0, baseReg, fpOffset);
2569 s390_lmg (code, s390_r2, s390_r6, STK_BASE, parmOffset);
2574 /*========================= End of Function ========================*/
2576 /*------------------------------------------------------------------*/
2578 /* Name - mono_arch_instrument_epilog */
2580 /* Function - Create an epilog that will handle the returned */
2581 /* values used in instrumentation. */
2583 /*------------------------------------------------------------------*/
2586 mono_arch_instrument_epilog_full (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments, gboolean preserve_argument_registers)
2589 int save_mode = SAVE_NONE,
2591 MonoMethod *method = cfg->method;
2592 int rtype = mono_type_get_underlying_type (mono_method_signature (method)->ret)->type;
2594 saveOffset = cfg->stack_usage - S390_TRACE_STACK_SIZE;
2595 if (method->save_lmf)
2596 saveOffset -= sizeof(MonoLMF);
2600 case MONO_TYPE_VOID:
2601 /* special case string .ctor icall */
2602 if (strcmp (".ctor", method->name) && method->klass == mono_defaults.string_class)
2603 save_mode = SAVE_ONE;
2605 save_mode = SAVE_NONE;
2609 save_mode = SAVE_ONE;
2612 save_mode = SAVE_R4;
2615 save_mode = SAVE_R8;
2617 case MONO_TYPE_VALUETYPE:
2618 if (mono_method_signature (method)->ret->data.klass->enumtype) {
2619 rtype = mono_class_enum_basetype (mono_method_signature (method)->ret->data.klass)->type;
2622 save_mode = SAVE_STRUCT;
2625 save_mode = SAVE_ONE;
2629 switch (save_mode) {
2631 s390_stg (code, s390_r2, 0, cfg->frame_reg, saveOffset);
2632 if (enable_arguments) {
2633 s390_lgr (code, s390_r3, s390_r2);
2637 s390_std (code, s390_f0, 0, cfg->frame_reg, saveOffset);
2638 if (enable_arguments) {
2639 s390_ldebr (code, s390_f0, s390_f0);
2643 s390_std (code, s390_f0, 0, cfg->frame_reg, saveOffset);
2646 s390_stg (code, s390_r2, 0, cfg->frame_reg, saveOffset);
2647 if (enable_arguments) {
2648 s390_lg (code, s390_r3, 0, cfg->frame_reg,
2649 S390_MINIMAL_STACK_SIZE+cfg->param_area);
2657 s390_basr (code, s390_r13, 0);
2659 s390_llong(code, cfg->method);
2660 s390_llong(code, func);
2661 s390_lg (code, s390_r2, 0, s390_r13, 4);
2662 s390_lg (code, s390_r1, 0, s390_r13, 12);
2663 s390_basr (code, s390_r14, s390_r1);
2665 switch (save_mode) {
2667 s390_lg (code, s390_r2, 0, cfg->frame_reg, saveOffset);
2671 s390_ld (code, s390_f0, 0, cfg->frame_reg, saveOffset);
2674 s390_lg (code, s390_r2, 0, cfg->frame_reg, saveOffset);
2684 /*========================= End of Function ========================*/
2686 /*------------------------------------------------------------------*/
2688 /* Name - mono_arch_peephole_pass_1 */
2690 /* Function - Form a peephole pass at the code looking for */
2691 /* simple optimizations. */
2693 /*------------------------------------------------------------------*/
2696 mono_arch_peephole_pass_1 (MonoCompile *cfg, MonoBasicBlock *bb)
2700 /*========================= End of Function ========================*/
2702 /*------------------------------------------------------------------*/
2704 /* Name - mono_arch_peephole_pass_2 */
2706 /* Function - Form a peephole pass at the code looking for */
2707 /* simple optimizations. */
2709 /*------------------------------------------------------------------*/
2712 mono_arch_peephole_pass_2 (MonoCompile *cfg, MonoBasicBlock *bb)
2716 MONO_BB_FOR_EACH_INS_SAFE (bb, n, ins) {
2717 mono_peephole_ins (bb, ins);
2721 /*========================= End of Function ========================*/
2723 /*------------------------------------------------------------------*/
2725 /* Name - mono_arch_lowering_pass. */
2727 /*------------------------------------------------------------------*/
2730 mono_arch_lowering_pass (MonoCompile *cfg, MonoBasicBlock *bb)
2732 MonoInst *ins, *next;
2734 MONO_BB_FOR_EACH_INS_SAFE (bb, next, ins) {
2735 switch (ins->opcode) {
2740 case OP_IDIV_UN_IMM:
2741 case OP_IREM_UN_IMM:
2745 case OP_LOCALLOC_IMM:
2746 mono_decompose_op_imm (cfg, bb, ins);
2749 if (!s390_is_imm16 (ins->inst_imm))
2750 /* This is created by the memcpy code which ignores is_inst_imm */
2751 mono_decompose_op_imm (cfg, bb, ins);
2758 bb->max_vreg = cfg->next_vreg;
2761 /*========================= End of Function ========================*/
2763 /*------------------------------------------------------------------*/
2765 /* Name - emit_float_to_int */
2767 /* Function - Create instructions which will convert a floating */
2768 /* point value to integer. */
2770 /*------------------------------------------------------------------*/
2773 emit_float_to_int (MonoCompile *cfg, guchar *code, int dreg, int sreg, int size, gboolean is_signed)
2775 /* sreg is a float, dreg is an integer reg. */
2777 s390_cgdbr (code, dreg, 5, sreg);
2780 s390_lghi (code, s390_r0, 0);
2781 s390_lghi (code, s390_r13, 0xff);
2782 s390_ltgr (code, dreg, dreg);
2784 s390_lghi (code, s390_r0, 0x80);
2785 s390_ngr (code, dreg, s390_r13);
2786 s390_ogr (code, dreg, s390_r0);
2791 s390_basr (code, s390_r13, 0);
2793 s390_llong (code, 0x41e0000000000000llu);
2794 s390_llong (code, 0x41f0000000000000llu);
2795 s390_ldr (code, s390_f15, sreg);
2796 s390_cdb (code, s390_f15, 0, s390_r13, 4);
2797 s390_jl (code, 0); CODEPTR (code, o[0]);
2798 s390_sdb (code, s390_f15, 0, s390_r13, 12);
2799 s390_cfdbr (code, dreg, 7, s390_f15);
2801 PTRSLOT (code, o[0]);
2802 s390_cfdbr (code, dreg, 5, sreg);
2805 s390_lghi (code, s390_r0, 0xff);
2806 s390_ngr (code, dreg, s390_r0);
2809 s390_lghi (code, s390_r0, -1);
2810 s390_srlg (code, s390_r0, s390_r0, 0, 16);
2811 s390_ngr (code, dreg, s390_r0);
2818 /*========================= End of Function ========================*/
2820 /*------------------------------------------------------------------*/
2822 /* Name - gboolean_is_unsigned. */
2824 /* Function - Return TRUE if next opcode is checking for un- */
2827 /*------------------------------------------------------------------*/
2830 gboolean is_unsigned (MonoInst *next)
2833 (((next->opcode >= OP_IBNE_UN) &&
2834 (next->opcode <= OP_IBLT_UN)) ||
2835 ((next->opcode >= OP_LBNE_UN) &&
2836 (next->opcode <= OP_LBLT_UN)) ||
2837 ((next->opcode >= OP_COND_EXC_NE_UN) &&
2838 (next->opcode <= OP_COND_EXC_LT_UN)) ||
2839 ((next->opcode >= OP_COND_EXC_INE_UN) &&
2840 (next->opcode <= OP_COND_EXC_ILT_UN)) ||
2841 ((next->opcode == OP_CLT_UN) ||
2842 (next->opcode == OP_CGT_UN)) ||
2843 ((next->opcode == OP_ICLT_UN) ||
2844 (next->opcode == OP_ICGT_UN) ||
2845 (next->opcode == OP_LCLT_UN) ||
2846 (next->opcode == OP_LCGT_UN))))
2852 /*========================= End of Function ========================*/
2854 /*------------------------------------------------------------------*/
2856 /* Name - mono_arch_output_basic_block */
2858 /* Function - Perform the "real" work of emitting instructions */
2859 /* that will do the work of in the basic block. */
2861 /*------------------------------------------------------------------*/
2864 mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
2869 guint8 *code = cfg->native_code + cfg->code_len;
2870 guint last_offset = 0;
2871 int max_len, cpos, src2;
2873 /* we don't align basic blocks of loops on s390 */
2875 if (cfg->verbose_level > 2)
2876 g_print ("Basic block %d starting at offset 0x%x\n", bb->block_num, bb->native_offset);
2878 cpos = bb->max_offset;
2880 if (cfg->prof_options & MONO_PROFILE_COVERAGE) {
2881 //MonoCoverageInfo *cov = mono_get_coverage_info (cfg->method);
2882 //g_assert (!mono_compile_aot);
2885 // cov->data [bb->dfn].iloffset = bb->cil_code - cfg->cil_code;
2886 /* this is not thread save, but good enough */
2887 /* fixme: howto handle overflows? */
2888 //x86_inc_mem (code, &cov->data [bb->dfn].count);
2891 MONO_BB_FOR_EACH_INS (bb, ins) {
2892 offset = code - cfg->native_code;
2894 max_len = ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
2896 if (offset > (cfg->code_size - max_len - 16)) {
2897 cfg->code_size *= 2;
2898 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
2899 code = cfg->native_code + offset;
2902 mono_debug_record_line_number (cfg, ins, offset);
2904 switch (ins->opcode) {
2905 case OP_STOREI1_MEMBASE_IMM: {
2906 s390_lghi (code, s390_r0, ins->inst_imm);
2907 S390_LONG (code, stcy, stc, s390_r0, 0,
2908 ins->inst_destbasereg, ins->inst_offset);
2911 case OP_STOREI2_MEMBASE_IMM: {
2912 s390_lghi (code, s390_r0, ins->inst_imm);
2913 S390_LONG (code, sthy, sth, s390_r0, 0,
2914 ins->inst_destbasereg, ins->inst_offset);
2917 case OP_STOREI4_MEMBASE_IMM: {
2918 if (s390_is_imm16(ins->inst_imm)) {
2919 s390_lghi (code, s390_r0, ins->inst_imm);
2921 s390_basr (code, s390_r13, 0);
2923 s390_llong(code, ins->inst_imm);
2924 s390_lg (code, s390_r0, 0, s390_r13, 4);
2926 S390_LONG (code, sty, st, s390_r0, 0,
2927 ins->inst_destbasereg, ins->inst_offset);
2930 case OP_STORE_MEMBASE_IMM:
2931 case OP_STOREI8_MEMBASE_IMM: {
2932 if (s390_is_imm16(ins->inst_imm)) {
2933 s390_lghi (code, s390_r0, ins->inst_imm);
2935 s390_basr (code, s390_r13, 0);
2937 s390_llong(code, ins->inst_imm);
2938 s390_lg (code, s390_r0, 0, s390_r13, 4);
2940 S390_LONG (code, stg, stg, s390_r0, 0,
2941 ins->inst_destbasereg, ins->inst_offset);
2944 case OP_STOREI1_MEMBASE_REG: {
2945 S390_LONG (code, stcy, stc, ins->sreg1, 0,
2946 ins->inst_destbasereg, ins->inst_offset);
2949 case OP_STOREI2_MEMBASE_REG: {
2950 S390_LONG (code, sthy, sth, ins->sreg1, 0,
2951 ins->inst_destbasereg, ins->inst_offset);
2954 case OP_STOREI4_MEMBASE_REG: {
2955 S390_LONG (code, sty, st, ins->sreg1, 0,
2956 ins->inst_destbasereg, ins->inst_offset);
2959 case OP_STORE_MEMBASE_REG:
2960 case OP_STOREI8_MEMBASE_REG: {
2961 S390_LONG (code, stg, stg, ins->sreg1, 0,
2962 ins->inst_destbasereg, ins->inst_offset);
2966 g_assert_not_reached ();
2968 case OP_LOAD_MEMBASE:
2969 case OP_LOADI8_MEMBASE: {
2970 S390_LONG (code, lg, lg, ins->dreg, 0,
2971 ins->inst_basereg, ins->inst_offset);
2974 case OP_LOADI4_MEMBASE: {
2975 S390_LONG (code, lgf, lgf, ins->dreg, 0,
2976 ins->inst_basereg, ins->inst_offset);
2979 case OP_LOADU4_MEMBASE: {
2980 S390_LONG (code, llgf, llgf, ins->dreg, 0,
2981 ins->inst_basereg, ins->inst_offset);
2984 case OP_LOADU1_MEMBASE: {
2985 S390_LONG (code, llgc, llgc, ins->dreg, 0,
2986 ins->inst_basereg, ins->inst_offset);
2989 case OP_LOADI1_MEMBASE: {
2990 S390_LONG (code, lgb, lgb, ins->dreg, 0,
2991 ins->inst_basereg, ins->inst_offset);
2994 case OP_LOADU2_MEMBASE: {
2995 S390_LONG (code, llgh, llgh, ins->dreg, 0,
2996 ins->inst_basereg, ins->inst_offset);
2999 case OP_LOADI2_MEMBASE: {
3000 S390_LONG (code, lgh, lgh, ins->dreg, 0,
3001 ins->inst_basereg, ins->inst_offset);
3004 case OP_LCONV_TO_I1: {
3006 s390_lgbr (code, ins->dreg, ins->sreg1);
3008 s390_sllg (code, ins->dreg, ins->sreg1, 0, 56);
3009 s390_srag (code, ins->dreg, ins->dreg, 0, 56);
3013 case OP_LCONV_TO_I2: {
3015 s390_lghr (code, ins->dreg, ins->sreg1);
3017 s390_sllg (code, ins->dreg, ins->sreg1, 0, 48);
3018 s390_srag (code, ins->dreg, ins->dreg, 0, 48);
3022 case OP_LCONV_TO_U1: {
3024 s390_llghr (code, ins->dreg, ins->sreg1);
3026 if (ins->dreg != ins->sreg1)
3027 s390_lgr (code, ins->dreg, ins->sreg1);
3028 s390_lghi (code, s390_r0, 0xff);
3029 s390_ngr (code, ins->dreg, s390_r0);
3033 case OP_LCONV_TO_U2: {
3035 s390_llghr (code, ins->dreg, ins->sreg1);
3037 if (ins->dreg != ins->sreg1)
3038 s390_lgr (code, ins->dreg, ins->sreg1);
3039 s390_lghi (code, s390_r0, -1);
3040 s390_srlg (code, s390_r0, s390_r0, 0, 48);
3041 s390_ngr (code, ins->dreg, s390_r0);
3045 case OP_ICONV_TO_I1: {
3047 s390_lbr (code, ins->dreg, ins->sreg1);
3049 if (ins->dreg != ins->sreg1)
3050 s390_lr (code, ins->dreg, ins->sreg1);
3051 s390_sll (code, ins->dreg, 0, 24);
3052 s390_sra (code, ins->dreg, 0, 24);
3057 case OP_ICONV_TO_I2: {
3059 s390_lhr (code, ins->dreg, ins->sreg1);
3061 if (ins->dreg != ins->sreg1)
3062 s390_lr (code, ins->dreg, ins->sreg1);
3063 s390_sll (code, ins->dreg, 0, 16);
3064 s390_sra (code, ins->dreg, 0, 16);
3068 case OP_ICONV_TO_U1: {
3070 s390_llcr (code, ins->dreg, ins->sreg1);
3072 if (ins->dreg != ins->sreg1)
3073 s390_lr (code, ins->dreg, ins->sreg1);
3074 s390_lhi (code, s390_r0, 0xff);
3075 s390_nr (code, ins->dreg, s390_r0);
3079 case OP_ICONV_TO_U2: {
3081 s390_llhr (code, ins->dreg, ins->sreg1);
3083 if (ins->dreg != ins->sreg1)
3084 s390_lr (code, ins->dreg, ins->sreg1);
3085 s390_lhi (code, s390_r0, -1);
3086 s390_srl (code, s390_r0, 0, 16);
3087 s390_nr (code, ins->dreg, s390_r0);
3093 if (is_unsigned (ins->next))
3094 s390_clgr (code, ins->sreg1, ins->sreg2);
3096 s390_cgr (code, ins->sreg1, ins->sreg2);
3100 if (is_unsigned (ins->next))
3101 s390_clr (code, ins->sreg1, ins->sreg2);
3103 s390_cr (code, ins->sreg1, ins->sreg2);
3106 case OP_COMPARE_IMM:
3107 case OP_LCOMPARE_IMM: {
3108 if (s390_is_imm16 (ins->inst_imm)) {
3109 s390_lghi (code, s390_r0, ins->inst_imm);
3110 if (is_unsigned (ins->next))
3111 s390_clgr (code, ins->sreg1, s390_r0);
3113 s390_cgr (code, ins->sreg1, s390_r0);
3115 s390_basr (code, s390_r13, 0);
3117 s390_llong(code, ins->inst_imm);
3118 if (is_unsigned (ins->next))
3119 s390_clg (code, ins->sreg1, 0, s390_r13, 4);
3121 s390_cg (code, ins->sreg1, 0, s390_r13, 4);
3125 case OP_ICOMPARE_IMM: {
3126 if (s390_is_imm16 (ins->inst_imm)) {
3127 s390_lghi (code, s390_r0, ins->inst_imm);
3128 if (is_unsigned (ins->next))
3129 s390_clr (code, ins->sreg1, s390_r0);
3131 s390_cr (code, ins->sreg1, s390_r0);
3134 s390_basr (code, s390_r13, 0);
3136 s390_word (code, ins->inst_imm);
3137 if (is_unsigned (ins->next))
3138 s390_cl (code, ins->sreg1, 0, s390_r13, 4);
3140 s390_c (code, ins->sreg1, 0, s390_r13, 4);
3145 s390_basr (code, s390_r13, 0);
3147 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_ABS,
3149 s390_llong (code, mono_break);
3150 s390_lg (code, s390_r14, 0, s390_r13, 4);
3151 s390_basr (code, s390_r14, s390_r14);
3156 s390_agr (code, ins->dreg, src2);
3161 s390_agr (code, ins->dreg, src2);
3166 s390_alcgr (code, ins->dreg, src2);
3170 if (ins->dreg != ins->sreg1) {
3171 s390_lgr (code, ins->dreg, ins->sreg1);
3173 if (s390_is_imm16 (ins->inst_imm)) {
3174 s390_aghi (code, ins->dreg, ins->inst_imm);
3176 s390_basr (code, s390_r13, 0);
3178 s390_llong(code, ins->inst_imm);
3179 s390_ag (code, ins->dreg, 0, s390_r13, 4);
3184 if (ins->dreg != ins->sreg1) {
3185 s390_lgr (code, ins->dreg, ins->sreg1);
3187 g_assert (s390_is_imm16 (ins->inst_imm));
3188 s390_aghi (code, ins->dreg, ins->inst_imm);
3192 if (ins->dreg != ins->sreg1) {
3193 s390_lgr (code, ins->dreg, ins->sreg1);
3195 if (s390_is_imm16 (ins->inst_imm)) {
3196 s390_lghi (code, s390_r0, ins->inst_imm);
3197 s390_alcgr (code, ins->dreg, s390_r0);
3199 s390_basr (code, s390_r13, 0);
3201 s390_llong (code, ins->inst_imm);
3202 s390_lg (code, s390_r13, 0, s390_r13, 4);
3203 s390_alcgr (code, ins->dreg, s390_r13);
3208 case OP_S390_IADD_OVF: {
3210 s390_ar (code, ins->dreg, src2);
3211 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_OV, "OverflowException");
3212 s390_lgfr (code, ins->dreg, ins->dreg);
3215 case OP_IADD_OVF_UN:
3216 case OP_S390_IADD_OVF_UN: {
3218 s390_algr (code, ins->dreg, src2);
3219 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_CY, "OverflowException");
3220 s390_llgfr (code, ins->dreg, ins->dreg);
3223 case OP_ADD_OVF_CARRY: {
3225 s390_lghi (code, s390_r0, 0);
3226 s390_lgr (code, s390_r1, s390_r0);
3227 s390_alcgr (code, s390_r0, s390_r1);
3228 s390_agr (code, ins->dreg, src2);
3229 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_OV, "OverflowException");
3230 s390_agr (code, ins->dreg, s390_r0);
3231 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_OV, "OverflowException");
3234 case OP_ADD_OVF_UN_CARRY: {
3236 s390_alcgr (code, ins->dreg, src2);
3237 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_CY, "OverflowException");
3242 s390_sgr (code, ins->dreg, src2);
3247 s390_sgr (code, ins->dreg, src2);
3252 s390_slbgr(code, ins->dreg, src2);
3256 if (ins->dreg != ins->sreg1) {
3257 s390_lgr (code, ins->dreg, ins->sreg1);
3259 if (s390_is_imm16 (-ins->inst_imm)) {
3260 s390_lghi (code, s390_r0, ins->inst_imm);
3261 s390_slgr (code, ins->dreg, s390_r0);
3263 s390_basr (code, s390_r13, 0);
3265 s390_llong(code, ins->inst_imm);
3266 s390_slg (code, ins->dreg, 0, s390_r13, 4);
3271 if (ins->dreg != ins->sreg1) {
3272 s390_lgr (code, ins->dreg, ins->sreg1);
3274 if (s390_is_imm16 (-ins->inst_imm)) {
3275 s390_lghi (code, s390_r0, ins->inst_imm);
3276 s390_slgr (code, ins->dreg, s390_r0);
3278 s390_basr (code, s390_r13, 0);
3280 s390_llong(code, ins->inst_imm);
3281 s390_slg (code, ins->dreg, 0, s390_r13, 4);
3286 if (ins->dreg != ins->sreg1) {
3287 s390_lgr (code, ins->dreg, ins->sreg1);
3289 if (s390_is_imm16 (-ins->inst_imm)) {
3290 s390_lghi (code, s390_r0, ins->inst_imm);
3291 s390_slbgr (code, ins->dreg, s390_r0);
3293 s390_basr (code, s390_r13, 0);
3295 s390_llong(code, ins->inst_imm);
3296 s390_slbg (code, ins->dreg, 0, s390_r13, 4);
3300 case OP_SUB_OVF_CARRY: {
3302 s390_lghi (code, s390_r0, 0);
3303 s390_lgr (code, s390_r1, s390_r0);
3304 s390_slbgr (code, s390_r0, s390_r1);
3305 s390_sgr (code, ins->dreg, src2);
3306 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_OV, "OverflowException");
3307 s390_agr (code, ins->dreg, s390_r0);
3308 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_OV, "OverflowException");
3311 case OP_SUB_OVF_UN_CARRY: {
3313 s390_slbgr (code, ins->dreg, src2);
3314 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_NC, "OverflowException");
3318 if (ins->sreg1 == ins->dreg) {
3319 s390_ngr (code, ins->dreg, ins->sreg2);
3322 if (ins->sreg2 == ins->dreg) {
3323 s390_ngr (code, ins->dreg, ins->sreg1);
3326 s390_lgr (code, ins->dreg, ins->sreg1);
3327 s390_ngr (code, ins->dreg, ins->sreg2);
3333 if (ins->dreg != ins->sreg1) {
3334 s390_lgr (code, ins->dreg, ins->sreg1);
3336 if (s390_is_imm16 (ins->inst_imm)) {
3337 s390_lghi (code, s390_r0, ins->inst_imm);
3338 s390_ngr (code, ins->dreg, s390_r0);
3340 s390_basr (code, s390_r13, 0);
3342 s390_llong(code, ins->inst_imm);
3343 s390_ng (code, ins->dreg, 0, s390_r13, 4);
3348 s390_lgr (code, s390_r1, ins->sreg1);
3349 s390_dsgr (code, s390_r0, ins->sreg2);
3350 s390_lgr (code, ins->dreg, s390_r1);
3354 s390_lgr (code, s390_r1, ins->sreg1);
3355 s390_lghi (code, s390_r0, 0);
3356 s390_dlgr (code, s390_r0, ins->sreg2);
3357 s390_lgr (code, ins->dreg, s390_r1);
3361 s390_lgr (code, s390_r1, ins->sreg1);
3362 s390_dsgr (code, s390_r0, ins->sreg2);
3363 s390_lgr (code, ins->dreg, s390_r0);
3367 s390_lgr (code, s390_r1, ins->sreg1);
3368 s390_lghi (code, s390_r0, 0);
3369 s390_dlgr (code, s390_r0, ins->sreg2);
3370 s390_lgr (code, ins->dreg, s390_r0);
3374 if (ins->sreg1 == ins->dreg) {
3375 s390_ogr (code, ins->dreg, ins->sreg2);
3378 if (ins->sreg2 == ins->dreg) {
3379 s390_ogr (code, ins->dreg, ins->sreg1);
3382 s390_lgr (code, ins->dreg, ins->sreg1);
3383 s390_ogr (code, ins->dreg, ins->sreg2);
3389 if (ins->dreg != ins->sreg1) {
3390 s390_lgr (code, ins->dreg, ins->sreg1);
3392 if (s390_is_imm16 (ins->inst_imm)) {
3393 s390_lghi (code, s390_r0, ins->inst_imm);
3394 s390_ogr (code, ins->dreg, s390_r0);
3396 s390_basr (code, s390_r13, 0);
3398 s390_llong(code, ins->inst_imm);
3399 s390_og (code, ins->dreg, 0, s390_r13, 4);
3404 if (ins->sreg1 == ins->dreg) {
3405 s390_xgr (code, ins->dreg, ins->sreg2);
3408 if (ins->sreg2 == ins->dreg) {
3409 s390_xgr (code, ins->dreg, ins->sreg1);
3412 s390_lgr (code, ins->dreg, ins->sreg1);
3413 s390_xgr (code, ins->dreg, ins->sreg2);
3419 if (ins->dreg != ins->sreg1) {
3420 s390_lgr (code, ins->dreg, ins->sreg1);
3422 if (s390_is_imm16 (ins->inst_imm)) {
3423 s390_lghi (code, s390_r0, ins->inst_imm);
3424 s390_xgr (code, ins->dreg, s390_r0);
3426 s390_basr (code, s390_r13, 0);
3428 s390_llong(code, ins->inst_imm);
3429 s390_xg (code, ins->dreg, 0, s390_r13, 4);
3435 s390_sllg (code, ins->dreg, ins->dreg, src2, 0);
3440 if (ins->sreg1 != ins->dreg) {
3441 s390_lgr (code, ins->dreg, ins->sreg1);
3443 s390_sllg (code, ins->dreg, ins->dreg, 0, (ins->inst_imm & 0x3f));
3448 s390_srag (code, ins->dreg, ins->dreg, src2, 0);
3453 if (ins->sreg1 != ins->dreg) {
3454 s390_lgr (code, ins->dreg, ins->sreg1);
3456 s390_srag (code, ins->dreg, ins->dreg, 0, (ins->inst_imm & 0x3f));
3460 case OP_LSHR_UN_IMM: {
3461 if (ins->sreg1 != ins->dreg) {
3462 s390_lgr (code, ins->dreg, ins->sreg1);
3464 s390_srlg (code, ins->dreg, ins->dreg, 0, (ins->inst_imm & 0x3f));
3469 s390_srlg (code, ins->dreg, ins->dreg, src2, 0);
3473 if (ins->sreg1 != ins->dreg) {
3474 s390_lgr (code, ins->dreg, ins->sreg1);
3476 s390_lghi (code, s390_r0, -1);
3477 s390_xgr (code, ins->dreg, s390_r0);
3481 s390_lcgr (code, ins->dreg, ins->sreg1);
3486 s390_msgr (code, ins->dreg, src2);
3491 if (ins->dreg != ins->sreg1) {
3492 s390_lgr (code, ins->dreg, ins->sreg1);
3494 if (s390_is_imm16 (ins->inst_imm)) {
3495 s390_lghi (code, s390_r13, ins->inst_imm);
3497 s390_basr (code, s390_r13, 0);
3499 s390_llong(code, ins->inst_imm);
3500 s390_lg (code, s390_r13, 0, s390_r13, 4);
3502 s390_msgr (code, ins->dreg, s390_r13);
3507 s390_ltgr (code, s390_r1, ins->sreg1);
3508 s390_jz (code, 0); CODEPTR(code, o[0]);
3509 s390_ltgr (code, s390_r0, ins->sreg2);
3511 s390_lghi (code, s390_r1, 0);
3512 s390_j (code, 0); CODEPTR(code, o[1]);
3513 s390_xgr (code, s390_r0, s390_r1);
3514 s390_msgr (code, s390_r1, ins->sreg2);
3515 s390_xgr (code, s390_r0, s390_r1);
3516 s390_srlg (code, s390_r0, s390_r0, 0, 63);
3517 s390_ltgr (code, s390_r0, s390_r0);
3518 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_NZ, "OverflowException");
3519 PTRSLOT (code, o[0]);
3520 PTRSLOT (code, o[1]);
3521 s390_lgr (code, ins->dreg, s390_r1);
3524 case OP_LMUL_OVF_UN: {
3525 s390_lghi (code, s390_r0, 0);
3526 s390_lgr (code, s390_r1, ins->sreg1);
3527 s390_mlgr (code, s390_r0, ins->sreg2);
3528 s390_ltgr (code, s390_r0, s390_r0);
3529 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_NZ, "OverflowException");
3530 s390_lgr (code, ins->dreg, s390_r1);
3534 g_assert_not_reached ();
3536 s390_algr (code, ins->dreg, src2);
3541 s390_agr (code, ins->dreg, src2);
3545 g_assert_not_reached ();
3547 s390_alcgr (code, ins->dreg, src2);
3551 if (ins->dreg != ins->sreg1) {
3552 s390_lgfr (code, ins->dreg, ins->sreg1);
3554 if (s390_is_imm16 (ins->inst_imm)) {
3555 s390_aghi(code, ins->dreg, ins->inst_imm);
3557 s390_basr (code, s390_r13, 0);
3559 s390_word (code, ins->inst_imm);
3560 s390_agf (code, ins->dreg, 0, s390_r13, 4);
3565 if (ins->dreg != ins->sreg1) {
3566 s390_lgfr (code, ins->dreg, ins->sreg1);
3568 if (s390_is_imm16 (ins->inst_imm)) {
3569 s390_lghi (code, s390_r0, ins->inst_imm);
3570 s390_alcgr (code, ins->dreg, s390_r0);
3572 s390_basr (code, s390_r13, 0);
3574 s390_word (code, ins->inst_imm);
3575 s390_lgf (code, s390_r13, 0, s390_r13, 4);
3576 s390_alcgr (code, ins->dreg, s390_r13);
3581 case OP_S390_LADD_OVF: {
3583 s390_agr (code, ins->dreg, src2);
3584 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_OV, "OverflowException");
3587 case OP_LADD_OVF_UN:
3588 case OP_S390_LADD_OVF_UN: {
3590 s390_algr (code, ins->dreg, src2);
3591 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_CY, "OverflowException");
3595 CHECK_SRCDST_NCOM_I;
3596 s390_slgr (code, ins->dreg, src2);
3600 CHECK_SRCDST_NCOM_I;
3601 s390_sgr (code, ins->dreg, src2);
3605 CHECK_SRCDST_NCOM_I;
3606 s390_slbgr (code, ins->dreg, src2);
3610 if (ins->dreg != ins->sreg1) {
3611 s390_lgfr (code, ins->dreg, ins->sreg1);
3613 if (s390_is_imm16 (-ins->inst_imm)) {
3614 s390_aghi (code, ins->dreg, -ins->inst_imm);
3616 s390_basr (code, s390_r13, 0);
3618 s390_word (code, ins->inst_imm);
3619 s390_sgf (code, ins->dreg, 0, s390_r13, 4);
3624 s390_basr (code, s390_r13, 0);
3626 s390_word (code, ins->inst_imm);
3627 s390_slgf (code, ins->dreg, 0, s390_r13, 4);
3631 case OP_S390_ISUB_OVF: {
3633 s390_sr (code, ins->dreg, src2);
3634 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_OV, "OverflowException");
3635 s390_lgfr (code, ins->dreg, ins->dreg);
3638 case OP_ISUB_OVF_UN:
3639 case OP_S390_ISUB_OVF_UN: {
3641 s390_slr (code, ins->dreg, src2);
3642 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_NC, "OverflowException");
3643 s390_llgfr(code, ins->dreg, ins->dreg);
3647 case OP_S390_LSUB_OVF: {
3649 s390_sgr (code, ins->dreg, src2);
3650 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_OV, "OverflowException");
3653 case OP_LSUB_OVF_UN:
3654 case OP_S390_LSUB_OVF_UN: {
3656 s390_slgr (code, ins->dreg, src2);
3657 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_NC, "OverflowException");
3661 CHECK_SRCDST_NCOM_I;
3662 s390_ngr (code, ins->dreg, src2);
3666 if (ins->dreg != ins->sreg1) {
3667 s390_lgfr (code, ins->dreg, ins->sreg1);
3669 if (s390_is_imm16 (ins->inst_imm)) {
3670 s390_lghi (code, s390_r0, ins->inst_imm);
3671 s390_ngr (code, ins->dreg, s390_r0);
3673 s390_basr (code, s390_r13, 0);
3675 s390_llong(code, ins->inst_imm);
3676 s390_ng (code, ins->dreg, 0, s390_r13, 4);
3681 s390_lgfr (code, s390_r0, ins->sreg1);
3682 s390_srda (code, s390_r0, 0, 32);
3683 s390_dr (code, s390_r0, ins->sreg2);
3684 s390_lgfr (code, ins->dreg, s390_r1);
3688 s390_lgfr (code, s390_r0, ins->sreg1);
3689 s390_srdl (code, s390_r0, 0, 32);
3690 s390_dlr (code, s390_r0, ins->sreg2);
3691 s390_lgfr (code, ins->dreg, s390_r1);
3695 if (s390_is_imm16 (ins->inst_imm)) {
3696 s390_lghi (code, s390_r13, ins->inst_imm);
3697 s390_lgfr (code, s390_r0, ins->sreg1);
3699 s390_basr (code, s390_r13, 0);
3701 s390_word (code, ins->inst_imm);
3702 s390_lgfr (code, s390_r0, ins->sreg1);
3703 s390_lgf (code, s390_r13, 0, s390_r13, 4);
3705 s390_srda (code, s390_r0, 0, 32);
3706 s390_dr (code, s390_r0, ins->sreg2);
3707 s390_lgfr (code, ins->dreg, s390_r1);
3711 s390_lgfr (code, s390_r0, ins->sreg1);
3712 s390_srda (code, s390_r0, 0, 32);
3713 s390_dr (code, s390_r0, ins->sreg2);
3714 s390_lgfr (code, ins->dreg, s390_r0);
3717 s390_lgfr (code, s390_r0, ins->sreg1);
3718 s390_srdl (code, s390_r0, 0, 32);
3719 s390_dlr (code, s390_r0, ins->sreg2);
3720 s390_lgfr (code, ins->dreg, s390_r0);
3724 if (s390_is_imm16 (ins->inst_imm)) {
3725 s390_lghi (code, s390_r13, ins->inst_imm);
3726 s390_lgfr (code, s390_r0, ins->sreg1);
3728 s390_basr (code, s390_r13, 0);
3730 s390_word (code, ins->inst_imm);
3731 s390_lgfr (code, s390_r0, ins->sreg1);
3732 s390_lgf (code, s390_r13, 0, s390_r13, 4);
3734 s390_srda (code, s390_r0, 0, 32);
3735 s390_dr (code, s390_r0, ins->sreg2);
3736 s390_lgfr (code, ins->dreg, s390_r0);
3741 s390_ogr (code, ins->dreg, src2);
3745 if (ins->dreg != ins->sreg1) {
3746 s390_lgfr (code, ins->dreg, ins->sreg1);
3748 if (s390_is_imm16 (ins->inst_imm)) {
3749 s390_lghi (code, s390_r0, ins->inst_imm);
3750 s390_ogr (code, ins->dreg, s390_r0);
3752 s390_basr (code, s390_r13, 0);
3754 s390_llong(code, ins->inst_imm);
3755 s390_og (code, ins->dreg, 0, s390_r13, 4);
3761 s390_xgr (code, ins->dreg, src2);
3765 if (ins->dreg != ins->sreg1) {
3766 s390_lgfr (code, ins->dreg, ins->sreg1);
3768 if (s390_is_imm16 (ins->inst_imm)) {
3769 s390_lghi (code, s390_r0, ins->inst_imm);
3770 s390_xgr (code, ins->dreg, s390_r0);
3772 s390_basr (code, s390_r13, 0);
3774 s390_llong(code, ins->inst_imm);
3775 s390_xg (code, ins->dreg, 0, s390_r13, 4);
3781 s390_sll (code, ins->dreg, src2, 0);
3785 if (ins->sreg1 != ins->dreg) {
3786 s390_lgfr (code, ins->dreg, ins->sreg1);
3788 s390_sll (code, ins->dreg, 0, (ins->inst_imm & 0x1f));
3793 s390_sra (code, ins->dreg, src2, 0);
3797 if (ins->sreg1 != ins->dreg) {
3798 s390_lgfr (code, ins->dreg, ins->sreg1);
3800 s390_sra (code, ins->dreg, 0, (ins->inst_imm & 0x1f));
3803 case OP_ISHR_UN_IMM: {
3804 if (ins->sreg1 != ins->dreg) {
3805 s390_lgfr (code, ins->dreg, ins->sreg1);
3807 s390_srl (code, ins->dreg, 0, (ins->inst_imm & 0x1f));
3812 s390_srl (code, ins->dreg, src2, 0);
3816 if (ins->sreg1 != ins->dreg) {
3817 s390_lgfr (code, ins->dreg, ins->sreg1);
3819 s390_lghi (code, s390_r0, -1);
3820 s390_xgr (code, ins->dreg, s390_r0);
3824 s390_lcgr (code, ins->dreg, ins->sreg1);
3829 s390_msr (code, ins->dreg, src2);
3833 if (ins->dreg != ins->sreg1) {
3834 s390_lgfr (code, ins->dreg, ins->sreg1);
3836 if (s390_is_imm16 (ins->inst_imm)) {
3837 s390_lghi (code, s390_r13, ins->inst_imm);
3839 s390_basr (code, s390_r13, 0);
3841 s390_word (code, ins->inst_imm);
3842 s390_lgf (code, s390_r13, 0, s390_r13, 4);
3844 s390_msr (code, ins->dreg, s390_r13);
3849 s390_ltr (code, s390_r1, ins->sreg1);
3850 s390_jz (code, 0); CODEPTR(code, o[0]);
3851 s390_ltr (code, s390_r0, ins->sreg2);
3853 s390_lhi (code, s390_r1, 0);
3854 s390_j (code, 0); CODEPTR(code, o[1]);
3855 s390_xr (code, s390_r0, s390_r1);
3856 s390_msr (code, s390_r1, ins->sreg2);
3857 s390_xr (code, s390_r0, s390_r1);
3858 s390_srl (code, s390_r0, 0, 31);
3859 s390_ltr (code, s390_r0, s390_r0);
3860 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_NZ, "OverflowException");
3861 PTRSLOT (code, o[0]);
3862 PTRSLOT (code, o[1]);
3863 s390_lgfr (code, ins->dreg, s390_r1);
3866 case OP_IMUL_OVF_UN: {
3867 s390_lhi (code, s390_r0, 0);
3868 s390_lr (code, s390_r1, ins->sreg1);
3869 s390_mlr (code, s390_r0, ins->sreg2);
3870 s390_ltr (code, s390_r0, s390_r0);
3871 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_NZ, "OverflowException");
3872 s390_lgfr (code, ins->dreg, s390_r1);
3877 if (s390_is_imm16(ins->inst_c0)) {
3878 s390_lghi (code, ins->dreg, ins->inst_c0);
3880 s390_basr (code, s390_r13, 0);
3882 s390_llong(code, ins->inst_c0);
3883 s390_lg (code, ins->dreg, 0, s390_r13, 4);
3888 s390_basr (code, s390_r13, 0);
3890 mono_add_patch_info (cfg, code - cfg->native_code,
3891 (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
3892 s390_llong(code, 0);
3893 s390_lg (code,ins->dreg, 0, s390_r13, 4);
3896 case OP_JUMP_TABLE: {
3897 mono_add_patch_info (cfg, code - cfg->native_code,
3898 (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
3899 s390_basr (code, s390_r13, 0);
3901 s390_llong (code, 0);
3902 s390_lg (code, ins->dreg, 0, s390_r13, 4);
3906 if (ins->dreg != ins->sreg1) {
3907 s390_lgr (code, ins->dreg, ins->sreg1);
3911 case OP_LCONV_TO_I8:
3913 s390_lgfr (code, ins->dreg, ins->sreg1);
3915 case OP_LCONV_TO_I4:
3916 s390_lgfr (code, ins->dreg, ins->sreg1);
3919 case OP_LCONV_TO_U8:
3920 case OP_LCONV_TO_U4:
3922 s390_llgfr (code, ins->dreg, ins->sreg1);
3924 case OP_LCONV_TO_OVF_U4:
3925 s390_basr (code, s390_r13, 0);
3927 s390_llong(code, 4294967295);
3928 s390_clg (code, ins->sreg1, 0, s390_r13, 4);
3929 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_GT, "OverflowException");
3930 s390_ltgr (code, ins->sreg1, ins->sreg1);
3931 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_LT, "OverflowException");
3932 s390_llgfr(code, ins->dreg, ins->sreg1);
3934 case OP_LCONV_TO_OVF_I4_UN:
3935 s390_basr (code, s390_r13, 0);
3937 s390_llong(code, 2147483647);
3938 s390_cg (code, ins->sreg1, 0, s390_r13, 4);
3939 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_GT, "OverflowException");
3940 s390_ltgr (code, ins->sreg1, ins->sreg1);
3941 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_LT, "OverflowException");
3942 s390_lgfr (code, ins->dreg, ins->sreg1);
3945 case OP_FCONV_TO_R4: {
3946 if (ins->dreg != ins->sreg1) {
3947 s390_ldr (code, ins->dreg, ins->sreg1);
3951 case OP_S390_SETF4RET: {
3952 s390_ledbr (code, ins->dreg, ins->sreg1);
3956 if (cfg->method->save_lmf)
3957 restoreLMF(code, cfg->frame_reg, cfg->stack_usage);
3959 if (cfg->flags & MONO_CFG_HAS_TAIL) {
3960 code = emit_load_volatile_arguments (code, cfg);
3963 code = backUpStackPtr(cfg, code);
3964 s390_lg (code, s390_r14, 0, cfg->frame_reg, S390_RET_ADDR_OFFSET);
3965 mono_add_patch_info (cfg, code - cfg->native_code,
3966 MONO_PATCH_INFO_METHOD_JUMP,
3968 s390_jcl (code, S390_CC_UN, 0);
3971 case OP_CHECK_THIS: {
3972 /* ensure ins->sreg1 is not NULL */
3973 s390_lg (code, s390_r0, 0, ins->sreg1, 0);
3974 s390_ltgr (code, s390_r0, s390_r0);
3978 int offset = cfg->sig_cookie + cfg->stack_usage;
3980 if (s390_is_imm16 (offset))
3981 s390_lghi (code, s390_r0, offset);
3983 s390_basr (code, s390_r13, 0);
3985 s390_llong(code, offset);
3986 s390_lg (code, s390_r0, 0, s390_r13, 0);
3988 s390_agr (code, s390_r0, cfg->frame_reg);
3989 s390_stg (code, s390_r0, 0, ins->sreg1, 0);
3993 s390_basr (code, s390_r13, 0);
3995 call = (MonoCallInst*)ins;
3996 if (ins->flags & MONO_INST_HAS_METHOD)
3997 mono_add_patch_info (cfg, code-cfg->native_code,
3998 MONO_PATCH_INFO_METHOD,
4001 mono_add_patch_info (cfg, code-cfg->native_code,
4002 MONO_PATCH_INFO_ABS,
4004 s390_llong(code, 0);
4005 s390_lg (code, s390_r14, 0, s390_r13, 4);
4006 s390_basr (code, s390_r14, s390_r14);
4007 if (call->signature->ret->type == MONO_TYPE_R4)
4008 s390_ldebr (code, s390_f0, s390_f0);
4016 s390_basr (code, s390_r13, 0);
4018 call = (MonoCallInst*)ins;
4019 if (ins->flags & MONO_INST_HAS_METHOD)
4020 mono_add_patch_info (cfg, code-cfg->native_code,
4021 MONO_PATCH_INFO_METHOD,
4024 mono_add_patch_info (cfg, code-cfg->native_code,
4025 MONO_PATCH_INFO_ABS,
4027 s390_llong(code, 0);
4028 s390_lg (code, s390_r14, 0, s390_r13, 4);
4029 s390_basr (code, s390_r14, s390_r14);
4032 case OP_FCALL_REG: {
4033 call = (MonoCallInst*)ins;
4034 s390_lgr (code, s390_r1, ins->sreg1);
4035 s390_basr (code, s390_r14, s390_r1);
4036 if (call->signature->ret->type == MONO_TYPE_R4)
4037 s390_ldebr (code, s390_f0, s390_f0);
4043 case OP_VOIDCALL_REG:
4045 s390_lgr (code, s390_r1, ins->sreg1);
4046 s390_basr (code, s390_r14, s390_r1);
4049 case OP_FCALL_MEMBASE: {
4050 call = (MonoCallInst*)ins;
4051 s390_lg (code, s390_r1, 0, ins->sreg1, ins->inst_offset);
4052 s390_basr (code, s390_r14, s390_r1);
4053 if (call->signature->ret->type == MONO_TYPE_R4)
4054 s390_ldebr (code, s390_f0, s390_f0);
4057 case OP_LCALL_MEMBASE:
4058 case OP_VCALL_MEMBASE:
4059 case OP_VCALL2_MEMBASE:
4060 case OP_VOIDCALL_MEMBASE:
4061 case OP_CALL_MEMBASE: {
4062 s390_lg (code, s390_r1, 0, ins->sreg1, ins->inst_offset);
4063 s390_basr (code, s390_r14, s390_r1);
4070 if (cfg->param_area == 0)
4071 alloca_skip = S390_MINIMAL_STACK_SIZE;
4073 alloca_skip = cfg->param_area;
4075 area_offset = S390_ALIGN(alloca_skip, S390_STACK_ALIGNMENT);
4076 s390_lgr (code, s390_r1, ins->sreg1);
4077 if (ins->flags & MONO_INST_INIT)
4078 s390_lgr (code, s390_r0, ins->sreg1);
4079 s390_aghi (code, s390_r1, 14);
4080 s390_srlg (code, s390_r1, s390_r1, 0, 3);
4081 s390_sllg (code, s390_r1, s390_r1, 0, 3);
4082 if (cfg->method->save_lmf) {
4083 /*----------------------------------*/
4084 /* we have to adjust lmf ebp value */
4085 /*----------------------------------*/
4086 int lmfOffset = cfg->stack_usage - sizeof(MonoLMF);
4088 s390_lgr (code, s390_r13, cfg->frame_reg);
4089 if (s390_is_imm16(lmfOffset))
4090 s390_aghi (code, s390_r13, lmfOffset);
4092 s390_basr (code, s390_r14, 0);
4094 s390_word (code, lmfOffset);
4095 s390_agf (code, s390_r13, 0, s390_r14, 4);
4097 s390_lgr (code, s390_r14, STK_BASE);
4098 s390_sgr (code, s390_r14, s390_r1);
4099 s390_stg (code, s390_r14, 0, s390_r13,
4100 G_STRUCT_OFFSET(MonoLMF, ebp));
4102 s390_lg (code, s390_r13, 0, STK_BASE, 0);
4103 s390_sgr (code, STK_BASE, s390_r1);
4104 s390_stg (code, s390_r13, 0, STK_BASE, 0);
4105 s390_la (code, ins->dreg, 0, STK_BASE, area_offset);
4106 s390_srlg (code, ins->dreg, ins->dreg, 0, 3);
4107 s390_sllg (code, ins->dreg, ins->dreg, 0, 3);
4108 if (ins->flags & MONO_INST_INIT) {
4109 s390_lgr (code, s390_r1, s390_r0);
4110 s390_lgr (code, s390_r0, ins->dreg);
4111 s390_lgr (code, s390_r14, s390_r12);
4112 s390_lghi (code, s390_r13, 0);
4113 s390_mvcle(code, s390_r0, s390_r12, 0, 0);
4115 s390_lgr (code, s390_r12, s390_r14);
4120 s390_lgr (code, s390_r2, ins->sreg1);
4121 s390_basr (code, s390_r13, 0);
4123 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
4124 (gpointer) "mono_arch_throw_exception");
4125 s390_llong(code, 0);
4126 s390_lg (code, s390_r14, 0, s390_r13, 4);
4127 s390_basr (code, s390_r14, s390_r14);
4131 s390_lgr (code, s390_r2, ins->sreg1);
4132 s390_basr (code, s390_r13, 0);
4134 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
4135 (gpointer) "mono_arch_rethrow_exception");
4136 s390_llong(code, 0);
4137 s390_lg (code, s390_r14, 0, s390_r13, 4);
4138 s390_basr (code, s390_r14, s390_r14);
4141 case OP_START_HANDLER: {
4142 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
4144 S390_LONG (code, stg, stg, s390_r14, 0,
4145 spvar->inst_basereg,
4146 spvar->inst_offset);
4149 case OP_ENDFILTER: {
4150 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
4152 if (ins->sreg1 != s390_r2)
4153 s390_lgr(code, s390_r2, ins->sreg1);
4154 S390_LONG (code, lg, lg, s390_r14, 0,
4155 spvar->inst_basereg,
4156 spvar->inst_offset);
4157 s390_br (code, s390_r14);
4160 case OP_ENDFINALLY: {
4161 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
4163 S390_LONG (code, lg, lg, s390_r14, 0,
4164 spvar->inst_basereg,
4165 spvar->inst_offset);
4166 s390_br (code, s390_r14);
4169 case OP_CALL_HANDLER: {
4170 mono_add_patch_info (cfg, code-cfg->native_code,
4171 MONO_PATCH_INFO_BB, ins->inst_target_bb);
4172 s390_brasl (code, s390_r14, 0);
4173 mono_cfg_add_try_hole (cfg, ins->inst_eh_block, code, bb);
4177 ins->inst_c0 = code - cfg->native_code;
4180 case OP_RELAXED_NOP:
4183 case OP_DUMMY_STORE:
4184 case OP_NOT_REACHED:
4189 EMIT_UNCOND_BRANCH(ins);
4192 s390_br (code, ins->sreg1);
4198 s390_lghi(code, ins->dreg, 1);
4200 s390_lghi(code, ins->dreg, 0);
4206 s390_lghi(code, ins->dreg, 1);
4208 s390_lghi(code, ins->dreg, 0);
4214 s390_lghi(code, ins->dreg, 1);
4216 s390_lghi(code, ins->dreg, 0);
4222 s390_lghi(code, ins->dreg, 1);
4224 s390_lghi(code, ins->dreg, 0);
4230 s390_lghi(code, ins->dreg, 1);
4232 s390_lghi(code, ins->dreg, 0);
4235 case OP_COND_EXC_EQ:
4236 case OP_COND_EXC_IEQ:
4237 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_EQ, ins->inst_p1);
4239 case OP_COND_EXC_NE_UN:
4240 case OP_COND_EXC_INE_UN:
4241 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_NE, ins->inst_p1);
4243 case OP_COND_EXC_LT:
4244 case OP_COND_EXC_ILT:
4245 case OP_COND_EXC_LT_UN:
4246 case OP_COND_EXC_ILT_UN:
4247 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_LT, ins->inst_p1);
4249 case OP_COND_EXC_GT:
4250 case OP_COND_EXC_IGT:
4251 case OP_COND_EXC_GT_UN:
4252 case OP_COND_EXC_IGT_UN:
4253 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_GT, ins->inst_p1);
4255 case OP_COND_EXC_GE:
4256 case OP_COND_EXC_IGE:
4257 case OP_COND_EXC_GE_UN:
4258 case OP_COND_EXC_IGE_UN:
4259 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_GE, ins->inst_p1);
4261 case OP_COND_EXC_LE:
4262 case OP_COND_EXC_ILE:
4263 case OP_COND_EXC_LE_UN:
4264 case OP_COND_EXC_ILE_UN:
4265 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_LE, ins->inst_p1);
4267 case OP_COND_EXC_OV:
4268 case OP_COND_EXC_IOV:
4269 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_OV, ins->inst_p1);
4271 case OP_COND_EXC_NO:
4272 case OP_COND_EXC_INO:
4273 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_NO, ins->inst_p1);
4276 case OP_COND_EXC_IC:
4277 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_CY, ins->inst_p1);
4279 case OP_COND_EXC_NC:
4280 case OP_COND_EXC_INC:
4281 EMIT_COND_SYSTEM_EXCEPTION (S390_CC_NC, ins->inst_p1);
4285 EMIT_COND_BRANCH (ins, S390_CC_EQ);
4289 EMIT_COND_BRANCH (ins, S390_CC_NE);
4295 EMIT_COND_BRANCH (ins, S390_CC_LT);
4301 EMIT_COND_BRANCH (ins, S390_CC_GT);
4307 EMIT_COND_BRANCH (ins, S390_CC_GE);
4313 EMIT_COND_BRANCH (ins, S390_CC_LE);
4316 /* floating point opcodes */
4318 if (*((float *) ins->inst_p0) == 0) {
4319 s390_lzdr (code, ins->dreg);
4321 s390_basr (code, s390_r13, 0);
4323 s390_llong (code, ins->inst_p0);
4324 s390_lg (code, s390_r13, 0, s390_r13, 4);
4325 s390_ld (code, ins->dreg, 0, s390_r13, 0);
4330 if (*((float *) ins->inst_p0) == 0) {
4331 s390_lzdr (code, ins->dreg);
4333 s390_basr (code, s390_r13, 0);
4335 s390_llong(code, ins->inst_p0);
4336 s390_lg (code, s390_r13, 0, s390_r13, 4);
4337 s390_ldeb (code, ins->dreg, 0, s390_r13, 0);
4341 case OP_STORER8_MEMBASE_REG: {
4342 S390_LONG (code, stdy, std, ins->sreg1, 0,
4343 ins->inst_destbasereg, ins->inst_offset);
4346 case OP_LOADR8_MEMBASE: {
4347 S390_LONG (code, ldy, ld, ins->dreg, 0,
4348 ins->inst_basereg, ins->inst_offset);
4351 case OP_STORER4_MEMBASE_REG: {
4352 s390_ledbr (code, s390_f15, ins->sreg1);
4353 S390_LONG (code, stey, ste, s390_f15, 0,
4354 ins->inst_destbasereg, ins->inst_offset);
4357 case OP_LOADR4_MEMBASE: {
4358 S390_LONG (code, ldy, ld, s390_f15, 0,
4359 ins->inst_basereg, ins->inst_offset);
4360 s390_ldebr (code, ins->dreg, s390_f15);
4363 case OP_ICONV_TO_R_UN: {
4364 s390_cdfbr (code, ins->dreg, ins->sreg1);
4365 s390_ltr (code, ins->sreg1, ins->sreg1);
4366 s390_jnl (code, 12);
4367 s390_basr (code, s390_r13, 0);
4369 s390_word (code, 0x41f00000);
4370 s390_word (code, 0);
4371 s390_adb (code, ins->dreg, 0, s390_r13, 4);
4374 case OP_LCONV_TO_R_UN: {
4375 s390_cdgbr (code, ins->dreg, ins->sreg1);
4376 s390_ltgr (code, ins->sreg1, ins->sreg1);
4377 s390_jnl (code, 12);
4378 s390_basr (code, s390_r13, 0);
4380 s390_word (code, 0x41f00000);
4381 s390_word (code, 0);
4382 s390_adb (code, ins->dreg, 0, s390_r13, 4);
4385 case OP_LCONV_TO_R4:
4386 case OP_ICONV_TO_R4: {
4387 s390_cdgbr (code, ins->dreg, ins->sreg1);
4390 case OP_LCONV_TO_R8:
4391 case OP_ICONV_TO_R8: {
4392 s390_cdgbr (code, ins->dreg, ins->sreg1);
4395 case OP_FCONV_TO_I1:
4396 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 1, TRUE);
4398 case OP_FCONV_TO_U1:
4399 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 1, FALSE);
4401 case OP_FCONV_TO_I2:
4402 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 2, TRUE);
4404 case OP_FCONV_TO_U2:
4405 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 2, FALSE);
4407 case OP_FCONV_TO_I4:
4409 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 4, TRUE);
4411 case OP_FCONV_TO_U4:
4413 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 4, FALSE);
4415 case OP_FCONV_TO_I8:
4416 s390_cgdbr (code, ins->dreg, 5, ins->sreg1);
4418 case OP_LCONV_TO_OVF_I: {
4419 /* Valid ints: 0xffffffff:8000000 to 00000000:0x7f000000 */
4421 s390_ltgr (code, ins->sreg2, ins->sreg2);
4422 s390_jnl (code, 0); CODEPTR(code, o[0]);
4423 s390_ltgr (code, ins->sreg1, ins->sreg1);
4424 s390_jnl (code, 0); CODEPTR(code, o[1]);
4425 s390_lhi (code, s390_r13, -1);
4426 s390_cgr (code, ins->sreg1, s390_r13);
4427 s390_jnz (code, 0); CODEPTR(code, o[2]);
4428 if (ins->dreg != ins->sreg2)
4429 s390_lgr (code, ins->dreg, ins->sreg2);
4430 s390_j (code, 0); CODEPTR(code, o[3]);
4431 PTRSLOT(code, o[0]);
4432 s390_jz (code, 0); CODEPTR(code, o[4]);
4433 PTRSLOT(code, o[1]);
4434 PTRSLOT(code, o[2]);
4435 mono_add_patch_info (cfg, code - cfg->native_code,
4436 MONO_PATCH_INFO_EXC, "OverflowException");
4437 s390_brasl (code, s390_r14, 0);
4438 PTRSLOT(code, o[3]);
4439 PTRSLOT(code, o[4]);
4443 s390_lpdbr (code, ins->dreg, ins->sreg1);
4447 s390_sqdbr (code, ins->dreg, ins->sreg1);
4452 s390_adbr (code, ins->dreg, src2);
4456 CHECK_SRCDST_NCOM_F;
4457 s390_sdbr (code, ins->dreg, src2);
4462 s390_mdbr (code, ins->dreg, src2);
4466 CHECK_SRCDST_NCOM_F;
4467 s390_ddbr (code, ins->dreg, src2);
4471 s390_lcdbr (code, ins->dreg, ins->sreg1);
4475 CHECK_SRCDST_NCOM_F;
4476 s390_didbr (code, ins->dreg, src2, 5, s390_f15);
4480 s390_cdbr (code, ins->sreg1, ins->sreg2);
4484 s390_cdbr (code, ins->sreg1, ins->sreg2);
4485 s390_lghi (code, ins->dreg, 1);
4487 s390_lghi (code, ins->dreg, 0);
4491 s390_cdbr (code, ins->sreg1, ins->sreg2);
4492 s390_lghi (code, ins->dreg, 1);
4494 s390_lghi (code, ins->dreg, 0);
4498 s390_cdbr (code, ins->sreg1, ins->sreg2);
4499 s390_lghi (code, ins->dreg, 1);
4501 s390_lghi (code, ins->dreg, 0);
4505 s390_cdbr (code, ins->sreg1, ins->sreg2);
4506 s390_lghi (code, ins->dreg, 1);
4508 s390_lghi (code, ins->dreg, 0);
4512 s390_cdbr (code, ins->sreg1, ins->sreg2);
4513 s390_lghi (code, ins->dreg, 1);
4515 s390_lghi (code, ins->dreg, 0);
4520 s390_jo (code, 0); CODEPTR(code, o);
4521 EMIT_COND_BRANCH (ins, S390_CC_EQ);
4526 EMIT_COND_BRANCH (ins, S390_CC_NE|S390_CC_OV);
4530 s390_jo (code, 0); CODEPTR(code, o);
4531 EMIT_COND_BRANCH (ins, S390_CC_LT);
4536 EMIT_COND_BRANCH (ins, S390_CC_LT|S390_CC_OV);
4540 s390_jo (code, 0); CODEPTR(code, o);
4541 EMIT_COND_BRANCH (ins, S390_CC_GT);
4546 EMIT_COND_BRANCH (ins, S390_CC_GT|S390_CC_OV);
4550 s390_jo (code, 0); CODEPTR(code, o);
4551 EMIT_COND_BRANCH (ins, S390_CC_GE);
4556 EMIT_COND_BRANCH (ins, S390_CC_GE|S390_CC_OV);
4560 s390_jo (code, 0); CODEPTR(code, o);
4561 EMIT_COND_BRANCH (ins, S390_CC_LE);
4566 EMIT_COND_BRANCH (ins, S390_CC_LE|S390_CC_OV);
4570 s390_lhi (code, s390_r13, 0x7f);
4571 s390_tcdb (code, ins->sreg1, 0, s390_r13, 0);
4572 s390_jz (code, 0); CODEPTR(code, o);
4573 mono_add_patch_info (cfg, code - cfg->native_code,
4574 MONO_PATCH_INFO_EXC, "ArithmeticException");
4575 s390_brasl (code, s390_r14,0);
4579 case OP_S390_MOVE: {
4580 if (ins->backend.size > 0) {
4581 if (ins->backend.size <= 256) {
4582 s390_mvc (code, ins->backend.size, ins->dreg,
4583 ins->inst_offset, ins->sreg1, ins->inst_imm);
4585 s390_lgr (code, s390_r0, ins->dreg);
4586 if (ins->inst_offset > 0) {
4587 if (s390_is_imm16 (ins->inst_offset)) {
4588 s390_aghi (code, s390_r0, ins->inst_offset);
4590 s390_basr (code, s390_r13, 0);
4592 s390_llong(code, ins->inst_offset);
4593 s390_ag (code, s390_r0, 0, s390_r13, 4);
4596 s390_lgr (code, s390_r12, ins->sreg1);
4597 if (ins->inst_imm > 0) {
4598 if (s390_is_imm16 (ins->inst_imm)) {
4599 s390_aghi (code, s390_r12, ins->inst_imm);
4601 s390_basr (code, s390_r13, 0);
4603 s390_llong(code, ins->inst_imm);
4604 s390_ag (code, s390_r12, 0, s390_r13, 4);
4607 if (s390_is_imm16 (ins->backend.size)) {
4608 s390_lghi (code, s390_r1, ins->backend.size);
4610 s390_basr (code, s390_r13, 0);
4612 s390_llong(code, ins->backend.size);
4613 s390_lg (code, s390_r1, 0, s390_r13, 4);
4615 s390_lgr (code, s390_r13, s390_r1);
4616 s390_mvcle(code, s390_r0, s390_r12, 0, 0);
4622 case OP_ATOMIC_ADD_I8: {
4623 s390_lgr (code, s390_r1, ins->sreg2);
4624 s390_lg (code, s390_r0, 0, ins->inst_basereg, ins->inst_offset);
4625 s390_agr (code, s390_r1, s390_r0);
4626 s390_csg (code, s390_r0, s390_r1, ins->inst_basereg, ins->inst_offset);
4627 s390_jnz (code, -10);
4628 s390_lgr (code, ins->dreg, s390_r1);
4631 case OP_ATOMIC_ADD_NEW_I8: {
4632 s390_lgr (code, s390_r1, ins->sreg2);
4633 s390_lg (code, s390_r0, 0, ins->inst_basereg, ins->inst_offset);
4634 s390_agr (code, s390_r1, s390_r0);
4635 s390_csg (code, s390_r0, s390_r1, ins->inst_basereg, ins->inst_offset);
4636 s390_jnz (code, -10);
4637 s390_lgr (code, ins->dreg, s390_r1);
4640 case OP_ATOMIC_EXCHANGE_I8: {
4641 s390_lg (code, s390_r0, 0, ins->inst_basereg, ins->inst_offset);
4642 s390_csg (code, s390_r0, ins->sreg2, ins->inst_basereg, ins->inst_offset);
4643 s390_jnz (code, -6);
4644 s390_lgr (code, ins->dreg, s390_r0);
4647 case OP_ATOMIC_ADD_I4: {
4648 s390_lgfr(code, s390_r1, ins->sreg2);
4649 s390_lgf (code, s390_r0, 0, ins->inst_basereg, ins->inst_offset);
4650 s390_agr (code, s390_r1, s390_r0);
4651 s390_cs (code, s390_r0, s390_r1, ins->inst_basereg, ins->inst_offset);
4652 s390_jnz (code, -9);
4653 s390_lgfr(code, ins->dreg, s390_r1);
4656 case OP_ATOMIC_ADD_NEW_I4: {
4657 s390_lgfr(code, s390_r1, ins->sreg2);
4658 s390_lgf (code, s390_r0, 0, ins->inst_basereg, ins->inst_offset);
4659 s390_agr (code, s390_r1, s390_r0);
4660 s390_cs (code, s390_r0, s390_r1, ins->inst_basereg, ins->inst_offset);
4661 s390_jnz (code, -9);
4662 s390_lgfr(code, ins->dreg, s390_r1);
4665 case OP_ATOMIC_EXCHANGE_I4: {
4666 s390_l (code, s390_r0, 0, ins->inst_basereg, ins->inst_offset);
4667 s390_cs (code, s390_r0, ins->sreg2, ins->inst_basereg, ins->inst_offset);
4668 s390_jnz (code, -4);
4669 s390_lgfr(code, ins->dreg, s390_r0);
4672 case OP_S390_BKCHAIN: {
4673 s390_lgr (code, ins->dreg, ins->sreg1);
4674 if (s390_is_imm16 (cfg->stack_offset)) {
4675 s390_aghi (code, ins->dreg, cfg->stack_offset);
4677 s390_basr (code, s390_r13, 0);
4679 s390_llong(code, cfg->stack_offset);
4680 s390_ag (code, ins->dreg, 0, s390_r13, 4);
4684 case OP_MEMORY_BARRIER: {
4688 g_warning ("unknown opcode %s in %s()\n", mono_inst_name (ins->opcode), __FUNCTION__);
4689 g_assert_not_reached ();
4692 if ((cfg->opt & MONO_OPT_BRANCH) && ((code - cfg->native_code - offset) > max_len)) {
4693 g_warning ("wrong maximal instruction length of instruction %s (expected %d, got %ld)",
4694 mono_inst_name (ins->opcode), max_len, code - cfg->native_code - offset);
4695 g_assert_not_reached ();
4700 last_offset = offset;
4703 cfg->code_len = code - cfg->native_code;
4706 /*========================= End of Function ========================*/
4708 /*------------------------------------------------------------------*/
4710 /* Name - mono_arch_register_lowlevel_calls */
4712 /* Function - Register routines to help with --trace operation. */
4714 /*------------------------------------------------------------------*/
4717 mono_arch_register_lowlevel_calls (void)
4719 mono_register_jit_icall (mono_arch_get_lmf_addr, "mono_arch_get_lmf_addr", NULL, TRUE);
4722 /*========================= End of Function ========================*/
4724 /*------------------------------------------------------------------*/
4726 /* Name - mono_arch_patch_code */
4728 /* Function - Process the patch data created during the */
4729 /* instruction build process. This resolves jumps, */
4730 /* calls, variables etc. */
4732 /*------------------------------------------------------------------*/
4735 mono_arch_patch_code (MonoMethod *method, MonoDomain *domain,
4736 guint8 *code, MonoJumpInfo *ji, MonoCodeManager *dyn_code_mp, gboolean run_cctors)
4738 MonoJumpInfo *patch_info;
4740 for (patch_info = ji; patch_info; patch_info = patch_info->next) {
4741 unsigned char *ip = patch_info->ip.i + code;
4742 gconstpointer target = NULL;
4744 target = mono_resolve_patch_target (method, domain, code,
4745 patch_info, run_cctors);
4747 switch (patch_info->type) {
4748 case MONO_PATCH_INFO_IP:
4749 case MONO_PATCH_INFO_EXC_NAME:
4750 case MONO_PATCH_INFO_LDSTR:
4751 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
4752 case MONO_PATCH_INFO_LDTOKEN:
4753 case MONO_PATCH_INFO_EXC:
4754 case MONO_PATCH_INFO_ABS:
4755 case MONO_PATCH_INFO_METHOD:
4756 case MONO_PATCH_INFO_INTERNAL_METHOD:
4757 case MONO_PATCH_INFO_CLASS_INIT:
4758 s390_patch_addr (ip, (guint64) target);
4760 case MONO_PATCH_INFO_SWITCH:
4761 /*----------------------------------*/
4762 /* ip points at the basr r13,0/j +4 */
4763 /* instruction the vtable value */
4764 /* follows this (i.e. ip+6) */
4765 /*----------------------------------*/
4766 *((gconstpointer *)(ip+6)) = target;
4768 case MONO_PATCH_INFO_METHODCONST:
4769 case MONO_PATCH_INFO_CLASS:
4770 case MONO_PATCH_INFO_IMAGE:
4771 case MONO_PATCH_INFO_FIELD:
4772 case MONO_PATCH_INFO_IID:
4773 target = S390_RELATIVE(target, ip);
4774 s390_patch_rel (ip, (guint64) target);
4776 case MONO_PATCH_INFO_R4:
4777 case MONO_PATCH_INFO_R8:
4778 case MONO_PATCH_INFO_METHOD_REL:
4779 g_assert_not_reached ();
4782 target = S390_RELATIVE(target, ip);
4784 s390_patch_rel (ip, (guint64) target);
4789 /*========================= End of Function ========================*/
4791 /*------------------------------------------------------------------*/
4793 /* Name - emit_load_volatile_arguments */
4795 /* Function - Emit the instructions to reload parameter regist- */
4796 /* registers for use with "tail" operations. */
4798 /* The register loading operations performed here */
4799 /* are the mirror of the store operations performed */
4800 /* in mono_arch_emit_prolog and need to be kept in */
4801 /* synchronization with it. */
4803 /*------------------------------------------------------------------*/
4806 emit_load_volatile_arguments (guint8 *code, MonoCompile *cfg)
4809 MonoMethod *method = cfg->method;
4810 MonoMethodSignature *sig = mono_method_signature(method);
4814 cinfo = get_call_info (NULL, NULL, sig, sig->pinvoke);
4816 if (cinfo->struct_ret) {
4817 ArgInfo *ainfo = &cinfo->ret;
4818 inst = cfg->vret_addr;
4819 s390_lg (code, ainfo->reg, 0, inst->inst_basereg, inst->inst_offset);
4822 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
4823 ArgInfo *ainfo = cinfo->args + i;
4824 inst = cfg->args [pos];
4826 if (inst->opcode == OP_REGVAR) {
4827 if (ainfo->regtype == RegTypeGeneral)
4828 s390_lgr (code, ainfo->reg, inst->dreg);
4829 else if (ainfo->regtype == RegTypeFP) {
4830 if (inst->dreg != ainfo->reg) {
4831 if (ainfo->size == 4) {
4832 s390_ldebr (code, ainfo->reg, inst->dreg);
4834 s390_ldr (code, ainfo->reg, inst->dreg);
4838 else if (ainfo->regtype == RegTypeBase) {
4840 g_assert_not_reached ();
4842 if (ainfo->regtype == RegTypeGeneral) {
4843 if (!((ainfo->reg >= 2) && (ainfo->reg <= 6)))
4844 g_assert_not_reached();
4845 switch (ainfo->size) {
4847 s390_llgc (code, ainfo->reg, 0, inst->inst_basereg, inst->inst_offset);
4850 s390_lgh (code, ainfo->reg, 0, inst->inst_basereg, inst->inst_offset);
4853 s390_lgf (code, ainfo->reg, 0, inst->inst_basereg, inst->inst_offset);
4856 s390_lg (code, ainfo->reg, 0, inst->inst_basereg, inst->inst_offset);
4859 } else if (ainfo->regtype == RegTypeBase) {
4860 } else if (ainfo->regtype == RegTypeFP) {
4861 if (ainfo->size == 8)
4862 s390_ld (code, ainfo->reg, 0, inst->inst_basereg, inst->inst_offset);
4863 else if (ainfo->size == 4)
4864 s390_le (code, ainfo->reg, 0, inst->inst_basereg, inst->inst_offset);
4866 g_assert_not_reached ();
4867 } else if (ainfo->regtype == RegTypeStructByVal) {
4868 if (ainfo->reg != STK_BASE) {
4869 switch (ainfo->size) {
4871 s390_llgc (code, ainfo->reg, 0, inst->inst_basereg, inst->inst_offset);
4874 s390_lgh (code, ainfo->reg, 0, inst->inst_basereg, inst->inst_offset);
4877 s390_lgf (code, ainfo->reg, 0, inst->inst_basereg, inst->inst_offset);
4880 s390_lg (code, ainfo->reg, 0, inst->inst_basereg, inst->inst_offset);
4884 } else if (ainfo->regtype == RegTypeStructByAddr) {
4885 if (ainfo->reg != STK_BASE) {
4886 s390_lg (code, ainfo->reg, 0, inst->inst_basereg, inst->inst_offset);
4889 g_assert_not_reached ();
4897 /*========================= End of Function ========================*/
4899 /*------------------------------------------------------------------*/
4901 /* Name - mono_arch_emit_prolog */
4903 /* Function - Create the instruction sequence for a function */
4906 /*------------------------------------------------------------------*/
4909 mono_arch_emit_prolog (MonoCompile *cfg)
4911 MonoMethod *method = cfg->method;
4913 MonoMethodSignature *sig;
4915 long alloc_size, pos, max_offset, i, cfa_offset = 0;
4922 cfg->code_size = 512;
4924 if (mono_jit_trace_calls != NULL && mono_trace_eval (method)) {
4926 cfg->code_size += 256;
4929 if (method->save_lmf)
4930 cfg->code_size += 200;
4932 cfg->native_code = code = g_malloc (cfg->code_size);
4934 mono_emit_unwind_op_def_cfa (cfg, code, STK_BASE, 0);
4935 emit_unwind_regs(cfg, code, s390_r6, s390_r14, S390_REG_SAVE_OFFSET);
4936 s390_stmg (code, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
4937 mono_emit_unwind_op_offset (cfg, code, s390_r14, S390_RET_ADDR_OFFSET);
4939 if (cfg->arch.bkchain_reg != -1)
4940 s390_lgr (code, cfg->arch.bkchain_reg, STK_BASE);
4942 if (cfg->flags & MONO_CFG_HAS_ALLOCA) {
4943 cfg->used_int_regs |= 1 << 11;
4946 alloc_size = cfg->stack_offset;
4948 cfg->stack_usage = cfa_offset = alloc_size;
4949 mono_emit_unwind_op_def_cfa_offset (cfg, code, alloc_size);
4950 s390_lgr (code, s390_r11, STK_BASE);
4951 if (s390_is_imm16 (alloc_size)) {
4952 s390_aghi (code, STK_BASE, -alloc_size);
4954 int stackSize = alloc_size;
4955 while (stackSize > 32767) {
4956 s390_aghi (code, STK_BASE, -32767);
4959 s390_aghi (code, STK_BASE, -stackSize);
4961 s390_stg (code, s390_r11, 0, STK_BASE, 0);
4963 if (cfg->frame_reg != STK_BASE)
4964 s390_lgr (code, s390_r11, STK_BASE);
4966 mono_emit_unwind_op_def_cfa_reg (cfg, code, cfg->frame_reg);
4968 /* compute max_offset in order to use short forward jumps
4969 * we always do it on s390 because the immediate displacement
4970 * for jumps is too small
4973 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
4975 bb->max_offset = max_offset;
4977 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
4980 MONO_BB_FOR_EACH_INS (bb, ins)
4981 max_offset += ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
4984 /* load arguments allocated to register from the stack */
4985 sig = mono_method_signature (method);
4988 cinfo = get_call_info (cfg, cfg->mempool, sig, sig->pinvoke);
4990 if (cinfo->struct_ret) {
4991 ArgInfo *ainfo = &cinfo->ret;
4992 inst = cfg->vret_addr;
4993 inst->backend.size = ainfo->vtsize;
4994 s390_stg (code, ainfo->reg, 0, inst->inst_basereg, inst->inst_offset);
4997 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
4998 ArgInfo *ainfo = cinfo->args + i;
4999 inst = cfg->args [pos];
5001 if (inst->opcode == OP_VTARG_ADDR)
5002 inst = inst->inst_left;
5004 if (inst->opcode == OP_REGVAR) {
5005 if (ainfo->regtype == RegTypeGeneral)
5006 s390_lgr (code, inst->dreg, ainfo->reg);
5007 else if (ainfo->regtype == RegTypeFP) {
5008 if (inst->dreg != ainfo->reg) {
5009 if (ainfo->size == 4) {
5010 s390_ledbr (code, inst->dreg, ainfo->reg);
5012 s390_ldr (code, inst->dreg, ainfo->reg);
5016 else if (ainfo->regtype == RegTypeBase) {
5017 s390_lgr (code, s390_r13, STK_BASE);
5018 s390_aghi (code, s390_r13, alloc_size);
5019 s390_lg (code, inst->dreg, 0, s390_r13, ainfo->offset);
5021 g_assert_not_reached ();
5023 if (cfg->verbose_level > 2)
5024 g_print ("Argument %d assigned to register %s\n",
5025 pos, mono_arch_regname (inst->dreg));
5027 if (ainfo->regtype == RegTypeGeneral) {
5028 if (!((ainfo->reg >= 2) && (ainfo->reg <= 6)))
5029 g_assert_not_reached();
5030 switch (ainfo->size) {
5032 s390_stc (code, ainfo->reg, 0, inst->inst_basereg, inst->inst_offset);
5035 s390_sth (code, ainfo->reg, 0, inst->inst_basereg, inst->inst_offset);
5038 s390_st (code, ainfo->reg, 0, inst->inst_basereg, inst->inst_offset);
5041 s390_stg (code, ainfo->reg, 0, inst->inst_basereg, inst->inst_offset);
5044 } else if (ainfo->regtype == RegTypeBase) {
5045 } else if (ainfo->regtype == RegTypeFP) {
5046 if (ainfo->size == 8)
5047 s390_std (code, ainfo->reg, 0, inst->inst_basereg, inst->inst_offset);
5048 else if (ainfo->size == 4)
5049 s390_ste (code, ainfo->reg, 0, inst->inst_basereg, inst->inst_offset);
5051 g_assert_not_reached ();
5052 } else if (ainfo->regtype == RegTypeStructByVal) {
5053 int doffset = inst->inst_offset;
5055 if (ainfo->reg != STK_BASE)
5059 s390_lgr (code, s390_r13, STK_BASE);
5060 s390_aghi (code, s390_r13, alloc_size);
5063 size = (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE
5064 ? mono_class_native_size(mono_class_from_mono_type(inst->inst_vtype), NULL)
5069 if (ainfo->reg == STK_BASE)
5070 s390_ic (code, reg, 0, s390_r13, ainfo->offset+7);
5071 s390_stc (code, reg, 0, inst->inst_basereg, doffset);
5074 if (ainfo->reg == STK_BASE)
5075 s390_lh (code, reg, 0, s390_r13, ainfo->offset+6);
5076 s390_sth (code, reg, 0, inst->inst_basereg, doffset);
5079 if (ainfo->reg == STK_BASE)
5080 s390_l (code, reg, 0, s390_r13, ainfo->offset+4);
5081 s390_st (code, reg, 0, inst->inst_basereg, doffset);
5084 if (ainfo->reg == STK_BASE)
5085 s390_lg (code, reg, 0, s390_r13, ainfo->offset);
5086 s390_stg (code, reg, 0, inst->inst_basereg, doffset);
5089 } else if (ainfo->regtype == RegTypeStructByAddr) {
5090 s390_stg (code, ainfo->reg, 0, inst->inst_basereg, inst->inst_offset);
5091 } else if (ainfo->regtype == RegTypeStructByAddrOnStack) {
5093 g_assert_not_reached ();
5098 if (method->save_lmf) {
5099 /*---------------------------------------------------------------*/
5100 /* we build the MonoLMF structure on the stack - see mini-s390.h */
5101 /*---------------------------------------------------------------*/
5102 lmfOffset = alloc_size - sizeof(MonoLMF);
5104 s390_lgr (code, s390_r13, cfg->frame_reg);
5105 s390_aghi (code, s390_r13, lmfOffset);
5107 /*---------------------------------------------------------------*/
5108 /* Preserve the parameter registers while we fix up the lmf */
5109 /*---------------------------------------------------------------*/
5110 s390_stmg (code, s390_r2, s390_r6, s390_r13,
5111 G_STRUCT_OFFSET(MonoLMF, pregs[0]));
5113 /*---------------------------------------------------------------*/
5114 /* On return from this call r2 have the address of the &lmf */
5115 /*---------------------------------------------------------------*/
5116 s390_basr(code, s390_r14, 0);
5118 mono_add_patch_info (cfg, code - cfg->native_code,
5119 MONO_PATCH_INFO_INTERNAL_METHOD,
5120 (gpointer)"mono_get_lmf_addr");
5121 s390_llong(code, 0);
5122 s390_lg (code, s390_r1, 0, s390_r14, 4);
5123 s390_basr (code, s390_r14, s390_r1);
5125 /*---------------------------------------------------------------*/
5126 /* Set lmf.lmf_addr = jit_tls->lmf */
5127 /*---------------------------------------------------------------*/
5128 s390_stg (code, s390_r2, 0, s390_r13,
5129 G_STRUCT_OFFSET(MonoLMF, lmf_addr));
5131 /*---------------------------------------------------------------*/
5132 /* Get current lmf */
5133 /*---------------------------------------------------------------*/
5134 s390_lg (code, s390_r0, 0, s390_r2, 0);
5136 /*---------------------------------------------------------------*/
5137 /* Set our lmf as the current lmf */
5138 /*---------------------------------------------------------------*/
5139 s390_stg (code, s390_r13, 0, s390_r2, 0);
5141 /*---------------------------------------------------------------*/
5142 /* Have our lmf.previous_lmf point to the last lmf */
5143 /*---------------------------------------------------------------*/
5144 s390_stg (code, s390_r0, 0, s390_r13,
5145 G_STRUCT_OFFSET(MonoLMF, previous_lmf));
5147 /*---------------------------------------------------------------*/
5148 /* save method info */
5149 /*---------------------------------------------------------------*/
5150 s390_basr (code, s390_r1, 0);
5152 s390_llong (code, method);
5153 s390_lg (code, s390_r1, 0, s390_r1, 4);
5154 s390_stg (code, s390_r1, 0, s390_r13,
5155 G_STRUCT_OFFSET(MonoLMF, method));
5157 /*---------------------------------------------------------------*/
5158 /* save the current IP */
5159 /*---------------------------------------------------------------*/
5160 s390_stg (code, STK_BASE, 0, s390_r13, G_STRUCT_OFFSET(MonoLMF, ebp));
5161 s390_basr (code, s390_r1, 0);
5162 s390_stg (code, s390_r1, 0, s390_r13, G_STRUCT_OFFSET(MonoLMF, eip));
5164 /*---------------------------------------------------------------*/
5165 /* Save general and floating point registers */
5166 /*---------------------------------------------------------------*/
5167 s390_stmg (code, s390_r2, s390_r12, s390_r13,
5168 G_STRUCT_OFFSET(MonoLMF, gregs[2]));
5169 for (i = 0; i < 16; i++) {
5170 s390_std (code, i, 0, s390_r13,
5171 G_STRUCT_OFFSET(MonoLMF, fregs[i]));
5174 /*---------------------------------------------------------------*/
5175 /* Restore the parameter registers now that we've set up the lmf */
5176 /*---------------------------------------------------------------*/
5177 s390_lmg (code, s390_r2, s390_r6, s390_r13,
5178 G_STRUCT_OFFSET(MonoLMF, pregs[0]));
5182 code = mono_arch_instrument_prolog(cfg, enter_method, code, TRUE);
5184 cfg->code_len = code - cfg->native_code;
5185 g_assert (cfg->code_len < cfg->code_size);
5190 /*========================= End of Function ========================*/
5192 /*------------------------------------------------------------------*/
5194 /* Name - mono_arch_emit_epilog */
5196 /* Function - Emit the instructions for a function epilog. */
5198 /*------------------------------------------------------------------*/
5201 mono_arch_emit_epilog (MonoCompile *cfg)
5203 MonoMethod *method = cfg->method;
5206 int max_epilog_size = 96;
5208 if (cfg->method->save_lmf)
5209 max_epilog_size += 128;
5211 if (mono_jit_trace_calls != NULL)
5212 max_epilog_size += 128;
5214 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
5215 max_epilog_size += 128;
5217 while ((cfg->code_len + max_epilog_size) > (cfg->code_size - 16)) {
5218 cfg->code_size *= 2;
5219 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
5220 mono_jit_stats.code_reallocs++;
5223 code = cfg->native_code + cfg->code_len;
5225 if (mono_jit_trace_calls != NULL && mono_trace_eval (method)) {
5226 code = mono_arch_instrument_epilog (cfg, leave_method, code, TRUE);
5230 if (method->save_lmf)
5231 restoreLMF(code, cfg->frame_reg, cfg->stack_usage);
5233 if (cfg->flags & MONO_CFG_HAS_ALLOCA) {
5234 // if (cfg->frame_reg != STK_BASE)
5235 // s390_lgr (code, STK_BASE, cfg->frame_reg);
5236 s390_lg (code, STK_BASE, 0, STK_BASE, 0);
5238 code = backUpStackPtr(cfg, code);
5240 s390_lmg (code, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
5241 s390_br (code, s390_r14);
5243 cfg->code_len = code - cfg->native_code;
5245 g_assert (cfg->code_len < cfg->code_size);
5249 /*========================= End of Function ========================*/
5251 /*------------------------------------------------------------------*/
5253 /* Name - mono_arch_emit_exceptions */
5255 /* Function - Emit the blocks to handle exception conditions. */
5257 /*------------------------------------------------------------------*/
5260 mono_arch_emit_exceptions (MonoCompile *cfg)
5262 MonoJumpInfo *patch_info;
5268 MonoClass *exc_classes [MAX_EXC];
5269 guint8 *exc_throw_start [MAX_EXC];
5271 for (patch_info = cfg->patch_info;
5273 patch_info = patch_info->next) {
5274 if (patch_info->type == MONO_PATCH_INFO_EXC)
5278 code_size = exc_count * 48;
5280 while ((cfg->code_len + code_size) > (cfg->code_size - 16)) {
5281 cfg->code_size *= 2;
5282 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
5283 mono_jit_stats.code_reallocs++;
5286 code = cfg->native_code + cfg->code_len;
5288 /*---------------------------------------------------------------------*/
5289 /* Add code to raise exceptions */
5290 /*---------------------------------------------------------------------*/
5291 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
5292 switch (patch_info->type) {
5293 case MONO_PATCH_INFO_EXC: {
5294 guint8 *ip = patch_info->ip.i + cfg->native_code;
5295 MonoClass *exc_class;
5298 /*-----------------------------------------------------*/
5299 /* Patch the branch in epilog to come here */
5300 /*-----------------------------------------------------*/
5301 s390_patch_rel (ip + 2, (guint64) S390_RELATIVE(code,ip));
5303 exc_class = mono_class_from_name (mono_defaults.corlib,
5305 patch_info->data.name);
5306 g_assert (exc_class);
5307 throw_ip = patch_info->ip.i;
5309 for (iExc = 0; iExc < nThrows; ++iExc)
5310 if (exc_classes [iExc] == exc_class)
5313 if (iExc < nThrows) {
5314 s390_jcl (code, S390_CC_UN,
5315 (guint64) exc_throw_start [iExc]);
5316 patch_info->type = MONO_PATCH_INFO_NONE;
5319 if (nThrows < MAX_EXC) {
5320 exc_classes [nThrows] = exc_class;
5321 exc_throw_start [nThrows] = code;
5324 /*---------------------------------------------*/
5325 /* Patch the parameter passed to the handler */
5326 /*---------------------------------------------*/
5327 s390_basr (code, s390_r13, 0);
5329 // s390_llong(code, patch_info->data.target);
5330 s390_llong(code, exc_class->type_token);
5331 /*---------------------------------------------*/
5332 /* Load return address & parameter register */
5333 /*---------------------------------------------*/
5334 s390_larl (code, s390_r14, (guint64)S390_RELATIVE((patch_info->ip.i +
5335 cfg->native_code + 8), code));
5336 s390_lg (code, s390_r2, 0, s390_r13, 4);
5337 /*---------------------------------------------*/
5338 /* Reuse the current patch to set the jump */
5339 /*---------------------------------------------*/
5340 s390_basr (code, s390_r13, 0);
5342 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
5343 patch_info->data.name = "mono_arch_throw_corlib_exception";
5344 patch_info->ip.i = code - cfg->native_code;
5345 s390_llong(code, 0);
5346 s390_lg (code, s390_r1, 0, s390_r13, 4);
5347 s390_br (code, s390_r1);
5357 cfg->code_len = code - cfg->native_code;
5359 g_assert (cfg->code_len < cfg->code_size);
5363 /*========================= End of Function ========================*/
5365 /*------------------------------------------------------------------*/
5367 /* Name - mono_arch_setup_jit_tls_data */
5369 /* Function - Setup the JIT's Thread Level Specific Data. */
5371 /*------------------------------------------------------------------*/
5374 mono_arch_setup_jit_tls_data (MonoJitTlsData *tls)
5376 if (!tls_offset_inited) {
5377 tls_offset_inited = TRUE;
5381 __asm__ ("\tear\t%r1,0\n"
5388 : "=r" (appdomain_tls_offset),
5389 "=r" (thread_tls_offset),
5390 "=r" (lmf_tls_offset)
5391 : "r" (&tls_appdomain),
5392 "r" (&tls_current_object),
5393 "r" (&mono_lmf_addr)
5399 if (!lmf_addr_key_inited) {
5400 lmf_addr_key_inited = TRUE;
5401 pthread_key_create (&lmf_addr_key, NULL);
5403 pthread_setspecific (lmf_addr_key, &tls->lmf);
5407 /*========================= End of Function ========================*/
5409 /*------------------------------------------------------------------*/
5411 /* Name - mono_arch_free_jit_tls_data */
5413 /* Function - Free tls data. */
5415 /*------------------------------------------------------------------*/
5418 mono_arch_free_jit_tls_data (MonoJitTlsData *tls)
5422 /*========================= End of Function ========================*/
5424 /*------------------------------------------------------------------*/
5426 /* Name - mono_arch_emit_inst_for_method */
5428 /*------------------------------------------------------------------*/
5431 mono_arch_emit_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
5436 /*========================= End of Function ========================*/
5438 /*------------------------------------------------------------------*/
5440 /* Name - mono_arch_decompose_opts */
5442 /* Function - Decompose opcode into a System z opcode. */
5444 /*------------------------------------------------------------------*/
5447 mono_arch_decompose_opts (MonoCompile *cfg, MonoInst *ins)
5450 * Have to rename these to avoid being decomposed normally, since the normal
5451 * decomposition does not work on S390.
5453 switch (ins->opcode) {
5455 ins->opcode = OP_S390_ISUB_OVF;
5457 case OP_ISUB_OVF_UN:
5458 ins->opcode = OP_S390_ISUB_OVF_UN;
5461 ins->opcode = OP_S390_IADD_OVF;
5463 case OP_IADD_OVF_UN:
5464 ins->opcode = OP_S390_IADD_OVF_UN;
5467 ins->opcode = OP_S390_LADD_OVF;
5469 case OP_LADD_OVF_UN:
5470 ins->opcode = OP_S390_LADD_OVF_UN;
5473 ins->opcode = OP_S390_LSUB_OVF;
5475 case OP_LSUB_OVF_UN:
5476 ins->opcode = OP_S390_LSUB_OVF_UN;
5483 /*========================= End of Function ========================*/
5485 /*------------------------------------------------------------------*/
5487 /* Name - mono_arch_print_tree */
5489 /* Function - Print platform-specific opcode details. */
5491 /* Returns - 1 - opcode details have been printed */
5492 /* 0 - opcode details have not been printed */
5494 /*------------------------------------------------------------------*/
5497 mono_arch_print_tree (MonoInst *tree, int arity)
5501 switch (tree->opcode) {
5502 case OP_S390_LOADARG:
5503 case OP_S390_ARGREG:
5504 case OP_S390_ARGPTR:
5505 printf ("[0x%lx(%s)]", tree->inst_offset,
5506 mono_arch_regname (tree->inst_basereg));
5509 case OP_S390_STKARG:
5510 printf ("[0x%lx(previous_frame)]",
5515 printf ("[0x%lx(%d,%s),0x%lx(%s)]",
5516 tree->inst_offset, tree->backend.size,
5517 mono_arch_regname(tree->dreg),
5519 mono_arch_regname(tree->sreg1));
5522 case OP_S390_SETF4RET:
5523 printf ("[f%s,f%s]",
5524 mono_arch_regname (tree->dreg),
5525 mono_arch_regname (tree->sreg1));
5529 printf ("[0x%lx(0x%lx,%s)]", tree->inst_offset,
5531 mono_arch_regname (tree->sreg1));
5534 case OP_S390_BKCHAIN:
5535 printf ("[previous_frame(%s)]",
5536 mono_arch_regname (tree->sreg1));
5544 /*========================= End of Function ========================*/
5546 /*------------------------------------------------------------------*/
5548 /* Name - mono_arch_regalloc_cost */
5550 /* Function - Determine the cost, in the number of memory */
5551 /* references, of the action of allocating the var- */
5552 /* iable VMV into a register during global register */
5555 /* Returns - Cost */
5557 /*------------------------------------------------------------------*/
5560 mono_arch_regalloc_cost (MonoCompile *cfg, MonoMethodVar *vmv)
5566 /*========================= End of Function ========================*/
5568 /*------------------------------------------------------------------*/
5570 /* Name - mono_arch_get_domain_intrinsic */
5576 /*------------------------------------------------------------------*/
5579 mono_arch_get_domain_intrinsic (MonoCompile* cfg)
5583 if (appdomain_tls_offset == -1)
5586 MONO_INST_NEW (cfg, ins, OP_TLS_GET);
5587 ins->inst_offset = appdomain_tls_offset;
5591 /*========================= End of Function ========================*/
5593 /*------------------------------------------------------------------*/
5595 /* Name - mono_arch_flush_register_windows */
5601 /*------------------------------------------------------------------*/
5604 mono_arch_flush_register_windows (void)
5608 /*========================= End of Function ========================*/
5610 /*------------------------------------------------------------------*/
5612 /* Name - mono_arch_get_lmf_addr */
5618 /*------------------------------------------------------------------*/
5621 mono_arch_get_lmf_addr (void)
5623 return pthread_getspecific (lmf_addr_key);
5626 /*========================= End of Function ========================*/
5628 /*------------------------------------------------------------------*/
5630 /* Name - mono_arch_is_inst_imm */
5632 /* Function - Determine if operand qualifies as an immediate */
5633 /* value. For s390 this is a value -32768-32768 */
5635 /* Returns - True|False - is [not] immediate value. */
5637 /*------------------------------------------------------------------*/
5640 mono_arch_is_inst_imm (gint64 imm)
5642 return s390_is_imm16 (imm);
5645 /*========================= End of Function ========================*/
5647 /*------------------------------------------------------------------*/
5649 /* Name - mono_arch_get_patch_offset */
5651 /* Function - Dummy entry point until s390x supports aot. */
5653 /* Returns - Offset for patch. */
5655 /*------------------------------------------------------------------*/
5658 mono_arch_get_patch_offset (guint8 *code)
5663 /*========================= End of Function ========================*/
5665 /*------------------------------------------------------------------*/
5667 /* Name - mono_arch_context_get_int_reg. */
5671 /* Returns - Offset for patch. */
5673 /*------------------------------------------------------------------*/
5676 mono_arch_context_get_int_reg (MonoContext *ctx, int reg)
5678 /* FIXME: implement */
5679 g_assert_not_reached ();
5683 /*========================= End of Function ========================*/
5685 /*------------------------------------------------------------------*/
5687 /* Name - mono_arch_get_this_arg_from_call. */
5691 /*------------------------------------------------------------------*/
5694 mono_arch_get_this_arg_from_call (mgreg_t *regs, guint8 *code)
5696 MonoLMF *lmf = (MonoLMF *) ((gchar *) regs - sizeof(MonoLMF));
5698 return (gpointer) lmf->gregs [s390_r2];
5701 /*========================= End of Function ========================*/
5703 /*------------------------------------------------------------------*/
5705 /* Name - get_delegate_invoke_impl. */
5709 /*------------------------------------------------------------------*/
5712 get_delegate_invoke_impl (gboolean has_target, guint32 param_count, guint32 *code_len, gboolean aot)
5714 guint8 *code, *start;
5719 start = code = mono_global_codeman_reserve (size);
5721 /* Replace the this argument with the target */
5722 s390_lg (code, s390_r1, 0, s390_r2, G_STRUCT_OFFSET(MonoDelegate, method_ptr));
5723 s390_lg (code, s390_r2, 0, s390_r2, G_STRUCT_OFFSET(MonoDelegate, target));
5724 s390_br (code, s390_r1);
5725 g_assert ((code - start) <= size);
5727 mono_arch_flush_icache (start, size);
5731 size = 32 + param_count * 8;
5732 start = code = mono_global_codeman_reserve (size);
5734 s390_lg (code, s390_r1, 0, s390_r2, G_STRUCT_OFFSET(MonoDelegate, method_ptr));
5735 /* slide down the arguments */
5736 for (i = 0; i < param_count; ++i) {
5737 s390_lgr (code, (s390_r2 + i), (s390_r2 + i + 1));
5739 s390_br (code, s390_r1);
5741 g_assert ((code - start) <= size);
5743 mono_arch_flush_icache (start, size);
5747 *code_len = code - start;
5752 /*========================= End of Function ========================*/
5754 /*------------------------------------------------------------------*/
5756 /* Name - mono_arch_get_delegate_invoke_impls. */
5760 /*------------------------------------------------------------------*/
5763 mono_arch_get_delegate_invoke_impls (void)
5770 code = get_delegate_invoke_impl (TRUE, 0, &code_len, TRUE);
5771 res = g_slist_prepend (res, mono_tramp_info_create (g_strdup ("delegate_invoke_impl_has_target"), code, code_len, NULL, NULL));
5773 for (i = 0; i < MAX_ARCH_DELEGATE_PARAMS; ++i) {
5774 code = get_delegate_invoke_impl (FALSE, i, &code_len, TRUE);
5775 res = g_slist_prepend (res, mono_tramp_info_create (g_strdup_printf ("delegate_invoke_impl_target_%d", i), code, code_len, NULL, NULL));
5781 /*========================= End of Function ========================*/
5783 /*------------------------------------------------------------------*/
5785 /* Name - mono_arch_get_delegate_invoke_impl. */
5789 /*------------------------------------------------------------------*/
5792 mono_arch_get_delegate_invoke_impl (MonoMethodSignature *sig, gboolean has_target)
5794 guint8 *code, *start;
5796 /* FIXME: Support more cases */
5797 if (MONO_TYPE_ISSTRUCT (sig->ret))
5801 static guint8* cached = NULL;
5807 start = mono_aot_get_trampoline ("delegate_invoke_impl_has_target");
5809 start = get_delegate_invoke_impl (TRUE, 0, NULL, FALSE);
5811 mono_memory_barrier ();
5815 static guint8* cache [MAX_ARCH_DELEGATE_PARAMS + 1] = {NULL};
5818 if (sig->param_count > MAX_ARCH_DELEGATE_PARAMS)
5820 for (i = 0; i < sig->param_count; ++i)
5821 if (!mono_is_regsize_var (sig->params [i]))
5825 code = cache [sig->param_count];
5829 if (mono_aot_only) {
5830 char *name = g_strdup_printf ("delegate_invoke_impl_target_%d", sig->param_count);
5831 start = mono_aot_get_trampoline (name);
5834 start = get_delegate_invoke_impl (FALSE, sig->param_count, NULL, FALSE);
5837 mono_memory_barrier ();
5839 cache [sig->param_count] = start;
5844 /*========================= End of Function ========================*/
5846 /*------------------------------------------------------------------*/
5848 /* Name - mono_arch_build_imt_thunk. */
5852 /*------------------------------------------------------------------*/
5855 mono_arch_build_imt_thunk (MonoVTable *vtable, MonoDomain *domain,
5856 MonoIMTCheckItem **imt_entries, int count,
5857 gpointer fail_tramp)
5861 guchar *code, *start;
5863 for (i = 0; i < count; ++i) {
5864 MonoIMTCheckItem *item = imt_entries [i];
5865 if (item->is_equals) {
5866 if (item->check_target_idx) {
5867 if (!item->compare_done)
5868 item->chunk_size += CMP_SIZE + JUMP_SIZE;
5869 if (item->has_target_code)
5870 item->chunk_size += BR_SIZE + JUMP_SIZE + LOADCON_SIZE;
5872 item->chunk_size += BR_SIZE + JUMP_SIZE + LOADCON_SIZE +
5876 item->chunk_size += CMP_SIZE + 2 * BR_SIZE + JUMP_SIZE +
5878 if (!item->has_target_code)
5879 item->chunk_size += LOAD_SIZE;
5881 item->chunk_size += LOADCON_SIZE + LOAD_SIZE + BR_SIZE;
5882 #if ENABLE_WRONG_METHOD_CHECK
5883 item->chunk_size += CMP_SIZE + JUMP_SIZE;
5888 item->chunk_size += CMP_SIZE + JUMP_SIZE;
5889 imt_entries [item->check_target_idx]->compare_done = TRUE;
5891 size += item->chunk_size;
5895 code = mono_method_alloc_generic_virtual_thunk (domain, size);
5897 code = mono_domain_code_reserve (domain, size);
5901 for (i = 0; i < count; ++i) {
5902 MonoIMTCheckItem *item = imt_entries [i];
5903 item->code_target = (guint8 *) code;
5904 if (item->is_equals) {
5905 if (item->check_target_idx) {
5906 if (!item->compare_done) {
5907 s390_basr (code, s390_r13, s390_r0);
5909 s390_llong(code, item->key);
5910 s390_lg (code, s390_r0, 0, s390_r13, 4);
5911 s390_cgr (code, s390_r0, MONO_ARCH_IMT_REG);
5913 item->jmp_code = (guint8*) code;
5914 s390_jcl (code, S390_CC_NE, 0);
5916 s390_basr (code, s390_r13, s390_r0);
5918 if (item->has_target_code) {
5919 s390_llong(code, item->value.target_code);
5920 s390_lg (code, s390_r1, 0, s390_r13, 4);
5922 s390_llong(code, (&(vtable->vtable [item->value.vtable_slot])));
5923 s390_lg (code, s390_r1, 0, s390_r13, 4);
5924 s390_lg (code, s390_r1, 0, s390_r1, 0);
5926 s390_br (code, s390_r1);
5931 s390_basr (code, s390_r13, s390_r0);
5933 s390_llong(code, item->key);
5934 s390_lg (code, s390_r0, 0, s390_r13, 4);
5935 s390_cgr (code, s390_r0, MONO_ARCH_IMT_REG);
5936 item->jmp_code = (guint8*) code;
5937 s390_jcl (code, S390_CC_NE, 0);
5938 s390_basr (code, s390_r13, s390_r0);
5940 if (item->has_target_code) {
5941 s390_llong(code, item->value.target_code);
5942 s390_lg (code, s390_r1, 0, s390_r13, 4);
5945 s390_llong(code, (&(vtable->vtable [item->value.vtable_slot])));
5946 s390_lg (code, s390_r1, 0, s390_r13, 4);
5947 s390_lg (code, s390_r1, 0, s390_r1, 0);
5949 s390_br (code, s390_r1);
5950 target = S390_RELATIVE(code, item->jmp_code);
5951 s390_patch_rel(item->jmp_code+2, target);
5952 s390_basr (code, s390_r13, s390_r0);
5954 s390_llong(code, fail_tramp);
5955 s390_lg (code, s390_r1, 0, s390_r13, 4);
5956 s390_br (code, s390_r1);
5957 item->jmp_code = NULL;
5959 /* enable the commented code to assert on wrong method */
5960 #if ENABLE_WRONG_METHOD_CHECK
5961 g_assert_not_reached ();
5963 s390_basr (code, s390_r13, s390_r0);
5965 s390_llong(code, (&(vtable->vtable [item->value.vtable_slot])));
5966 s390_lg (code, s390_r1, 0, s390_r13, 4);
5967 s390_lg (code, s390_r1, 0, s390_r1, 0);
5968 s390_br (code, s390_r1);
5969 #if ENABLE_WRONG_METHOD_CHECK
5970 g_assert_not_reached ();
5975 s390_basr (code, s390_r13, s390_r0);
5977 s390_llong(code, item->key);
5978 s390_lg (code, s390_r0, 0, s390_r13, 4);
5979 s390_cgr (code, MONO_ARCH_IMT_REG, s390_r0);
5980 item->jmp_code = (guint8 *) code;
5981 s390_jcl (code, S390_CC_GE, 0);
5984 /* patch the branches to get to the target items */
5985 for (i = 0; i < count; ++i) {
5986 MonoIMTCheckItem *item = imt_entries [i];
5987 if (item->jmp_code) {
5988 if (item->check_target_idx) {
5990 offset = S390_RELATIVE(imt_entries [item->check_target_idx]->code_target,
5992 s390_patch_rel ((guchar *) item->jmp_code + 2, (guint64) offset);
5997 mono_arch_flush_icache ((guint8*)start, (code - start));
6000 mono_stats.imt_thunks_size += (code - start);
6002 g_assert (code - start <= size);
6007 /*========================= End of Function ========================*/
6009 /*------------------------------------------------------------------*/
6011 /* Name - mono_arch_find_imt_method. */
6013 /* Function - Get the method address from MONO_ARCH_IMT_REG */
6014 /* found in the save area. */
6016 /*------------------------------------------------------------------*/
6019 mono_arch_find_imt_method (mgreg_t *regs, guint8 *code)
6021 MonoLMF *lmf = (MonoLMF *) ((gchar *) regs - sizeof(MonoLMF));
6023 return ((MonoMethod *) lmf->gregs [MONO_ARCH_IMT_REG]);
6026 /*========================= End of Function ========================*/