Merge pull request #615 from nealef/master
[mono.git] / mono / mini / mini-s390x.c
index 67fbe4b44a81d677838d440aefe18a5aabf3f47f..b32953ca3e883bc3dc86edc96accdadb17c0ebc6 100644 (file)
@@ -246,6 +246,9 @@ if (ins->inst_target_bb->native_offset) {                                   \
 #define JUMP_SIZE      6
 #define ENABLE_WRONG_METHOD_CHECK 0
 
+#define mono_mini_arch_lock() EnterCriticalSection (&mini_arch_mutex)
+#define mono_mini_arch_unlock() LeaveCriticalSection (&mini_arch_mutex)
+
 /*========================= End of Defines =========================*/
 
 /*------------------------------------------------------------------*/
@@ -365,7 +368,7 @@ static gboolean is_regsize_var (MonoType *);
 static inline void add_general (guint *, size_data *, ArgInfo *);
 static inline void add_stackParm (guint *, size_data *, ArgInfo *, gint);
 static inline void add_float (guint *, size_data *, ArgInfo *);
-static CallInfo * get_call_info (MonoCompile *, MonoMemPool *, MonoMethodSignature *, gboolean);
+static CallInfo * get_call_info (MonoCompile *, MonoMemPool *, MonoMethodSignature *);
 static guchar * emit_float_to_int (MonoCompile *, guchar *, int, int, int, gboolean);
 static guint8 * emit_load_volatile_arguments (guint8 *, MonoCompile *);
 static void catch_SIGILL(int, siginfo_t *, void *);
@@ -414,6 +417,11 @@ static gpointer bp_trigger_page;
 
 breakpoint_t breakpointCode;
 
+/* 
+ * This mutex protects architecture specific caches 
+ */
+static CRITICAL_SECTION mini_arch_mutex;
+
 /*====================== End of Global Variables ===================*/
 
 /*------------------------------------------------------------------*/
@@ -485,7 +493,7 @@ mono_arch_fregname (int reg) {
 /*------------------------------------------------------------------*/
 
 int
-mono_arch_get_argument_info (MonoMethodSignature *csig, 
+mono_arch_get_argument_info (MonoGenericSharingContext *gsctx, MonoMethodSignature *csig, 
                             int param_count, 
                             MonoJitArgumentInfo *arg_info)
 {
@@ -916,7 +924,7 @@ enter_method (MonoMethod *method, RegParm *rParm, char *sp)
        
        sig = mono_method_signature (method);
        
-       cinfo = get_call_info (NULL, NULL, sig, sig->pinvoke);
+       cinfo = get_call_info (NULL, NULL, sig);
 
        if (cinfo->struct_ret) {
                printf ("[STRUCTRET:%p], ", (gpointer) rParm->gr[0]);
@@ -1355,14 +1363,14 @@ mono_arch_cleanup (void)
 
 /*------------------------------------------------------------------*/
 /*                                                                  */
-/* Name                - mono_arch_cpu_optimizazions                       */
+/* Name                - mono_arch_cpu_optimizations                       */
 /*                                                                  */
 /* Function    - Returns the optimizations supported on this CPU   */
 /*                                                                 */
 /*------------------------------------------------------------------*/
 
 guint32
-mono_arch_cpu_optimizazions (guint32 *exclude_mask)
+mono_arch_cpu_optimizations (guint32 *exclude_mask)
 {
        guint32 opts = 0;
 
@@ -1376,6 +1384,21 @@ mono_arch_cpu_optimizazions (guint32 *exclude_mask)
 
 /*========================= End of Function ========================*/
 
+/*------------------------------------------------------------------*/
+/*                                                                  */
+/* Name         - mono_arch_cpu_enumerate_simd_versions             */
+/*                                                                  */
+/* Function     - Returns the SIMD instruction sets on this CPU     */
+/*                                                                  */
+/*------------------------------------------------------------------*/
+guint32
+mono_arch_cpu_enumerate_simd_versions (void)
+{
+       /* SIMD is currently unimplemented */
+       return 0;
+}
+/*========================= End of Function ========================*/
+
 /*------------------------------------------------------------------*/
 /*                                                                  */
 /* Name                -                                                   */
@@ -1608,12 +1631,13 @@ add_float (guint *fr,  size_data *sz, ArgInfo *ainfo)
 /*------------------------------------------------------------------*/
 
 static CallInfo *
-get_call_info (MonoCompile *cfg, MonoMemPool *mp, MonoMethodSignature *sig, gboolean is_pinvoke)
+get_call_info (MonoCompile *cfg, MonoMemPool *mp, MonoMethodSignature *sig)
 {
        guint i, fr, gr, size, pstart;
        int nParm = sig->hasthis + sig->param_count;
        MonoType *ret_type;
-       guint32 simpletype, align;
+       guint32 simpleType, align;
+       gboolean is_pinvoke = sig->pinvoke;
        CallInfo *cinfo;
        size_data *sz;
        MonoGenericSharingContext *gsctx = cfg ? cfg->generic_sharing_context : NULL;
@@ -1644,11 +1668,11 @@ get_call_info (MonoCompile *cfg, MonoMemPool *mp, MonoMethodSignature *sig, gboo
        /* area that the callee will use.                           */
        /*----------------------------------------------------------*/
 
-       ret_type = mono_type_get_underlying_type (sig->ret);
+       ret_type = mini_type_get_underlying_type (gsctx, sig->ret);
        ret_type = mini_get_basic_type_from_generic (gsctx, ret_type);
-       simpletype = ret_type->type;
+       simpleType = ret_type->type;
 enum_retvalue:
-       switch (simpletype) {
+       switch (simpleType) {
                case MONO_TYPE_BOOLEAN:
                case MONO_TYPE_I1:
                case MONO_TYPE_U1:
@@ -1689,13 +1713,10 @@ enum_retvalue:
                case MONO_TYPE_VALUETYPE: {
                        MonoClass *klass = mono_class_from_mono_type (sig->ret);
                        if (klass->enumtype) {
-                               simpletype = mono_class_enum_basetype (klass)->type;
+                               simpleType = mono_class_enum_basetype (klass)->type;
                                goto enum_retvalue;
                        }
-                       if (sig->pinvoke)
-                               size = mono_class_native_size (klass, &align);
-                       else
-                               size = mono_class_value_size (klass, &align);
+                       size = mini_type_stack_size_full (gsctx, &klass->byval_arg, NULL, sig->pinvoke);
        
                        cinfo->struct_ret = 1;
                        cinfo->ret.size   = size;
@@ -1788,11 +1809,11 @@ enum_retvalue:
                        continue;
                }
 
-               ptype = mono_type_get_underlying_type (sig->params [i]);
-               ptype = mini_get_basic_type_from_generic (gsctx, ptype);
-               simpletype = ptype->type;
-               cinfo->args[nParm].type = simpletype;
-               switch (simpletype) {
+               ptype = mini_type_get_underlying_type (gsctx, sig->params [i]);
+//             ptype = mini_get_basic_type_from_generic (gsctx, ptype);
+               simpleType = ptype->type;
+               cinfo->args[nParm].type = simpleType;
+               switch (simpleType) {
                case MONO_TYPE_BOOLEAN:
                case MONO_TYPE_I1:
                case MONO_TYPE_U1:
@@ -1843,7 +1864,7 @@ enum_retvalue:
                        nParm++;
                        break;
                case MONO_TYPE_GENERICINST:
-                       if (!mono_type_generic_inst_is_valuetype (sig->params [i])) {
+                       if (!mono_type_generic_inst_is_valuetype (ptype)) {
                                cinfo->args[nParm].size = sizeof(gpointer);
                                add_general (&gr, sz, cinfo->args+nParm);
                                nParm++;
@@ -1852,30 +1873,29 @@ enum_retvalue:
                        /* Fall through */
                case MONO_TYPE_VALUETYPE: {
                        MonoMarshalType *info;
-                       MonoClass *klass = mono_class_from_mono_type (sig->params [i]);
-                       if (sig->pinvoke)
-                               size = mono_class_native_size (klass, &align);
-                       else
-                               size = mono_class_value_size (klass, &align);
-       
-                       info = mono_marshal_load_type_info (klass);
+                       MonoClass *klass = mono_class_from_mono_type (ptype);
 
-                       if ((info->native_size == sizeof(float)) &&
-                           (info->num_fields  == 1) &&
-                           (info->fields[0].field->type->type == MONO_TYPE_R4)) {
-                               cinfo->args[nParm].size = sizeof(float);
-                               add_float(&fr, sz, cinfo->args+nParm);
-                               nParm ++;
-                               break;
-                       }
+                       size = mini_type_stack_size_full(gsctx, &klass->byval_arg, NULL, sig->pinvoke);
+                       if (simpleType != MONO_TYPE_GENERICINST) {
+                               info = mono_marshal_load_type_info(klass);
 
-                       if ((info->native_size == sizeof(double)) &&
-                           (info->num_fields  == 1) &&
-                           (info->fields[0].field->type->type == MONO_TYPE_R8)) {
-                               cinfo->args[nParm].size = sizeof(double);
-                               add_float(&fr, sz, cinfo->args+nParm);
-                               nParm ++;
-                               break;
+                               if ((info->native_size == sizeof(float)) &&
+                                   (info->num_fields  == 1) &&
+                                   (info->fields[0].field->type->type == MONO_TYPE_R4)) {
+                                       cinfo->args[nParm].size = sizeof(float);
+                                       add_float(&fr, sz, cinfo->args+nParm);
+                                       nParm ++;
+                                       break;
+                               }
+
+                               if ((info->native_size == sizeof(double)) &&
+                                   (info->num_fields  == 1) &&
+                                   (info->fields[0].field->type->type == MONO_TYPE_R8)) {
+                                       cinfo->args[nParm].size = sizeof(double);
+                                       add_float(&fr, sz, cinfo->args+nParm);
+                                       nParm ++;
+                                       break;
+                               }
                        }
 
                        cinfo->args[nParm].vtsize  = 0;
@@ -1934,7 +1954,7 @@ enum_retvalue:
                }
                        break;
                default:
-                       g_error ("Can't trampoline 0x%x", sig->params [i]->type);
+                       g_error ("Can't trampoline 0x%x", ptype);
                }
        }
 
@@ -2028,7 +2048,7 @@ mono_arch_allocate_vars (MonoCompile *cfg)
 
        sig     = mono_method_signature (cfg->method);
        
-       cinfo   = get_call_info (cfg, cfg->mempool, sig, sig->pinvoke);
+       cinfo   = get_call_info (cfg, cfg->mempool, sig);
 
        if (!cinfo->struct_ret) {
                switch (mono_type_get_underlying_type (sig->ret)->type) {
@@ -2257,7 +2277,7 @@ mono_arch_create_vars (MonoCompile *cfg)
 
        sig = mono_method_signature (cfg->method);
 
-       cinfo = get_call_info (cfg, cfg->mempool, sig, sig->pinvoke);
+       cinfo = get_call_info (cfg, cfg->mempool, sig);
 
        if (cinfo->struct_ret) {
                cfg->vret_addr = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_ARG);
@@ -2372,7 +2392,7 @@ mono_arch_emit_call (MonoCompile *cfg, MonoCallInst *call)
        n = sig->param_count + sig->hasthis;
        DEBUG (g_print ("Call requires: %d parameters\n",n));
        
-       cinfo = get_call_info (cfg, cfg->mempool, sig, sig->pinvoke);
+       cinfo = get_call_info (cfg, cfg->mempool, sig);
 
        stackSize         = cinfo->sz.stack_size + cinfo->sz.local_size + 
                            cinfo->sz.parm_size + cinfo->sz.offset;
@@ -4146,6 +4166,7 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        /* ensure ins->sreg1 is not NULL */
                        s390_lg   (code, s390_r0, 0, ins->sreg1, 0);
                        s390_ltgr (code, s390_r0, s390_r0);
+                       EMIT_COND_SYSTEM_EXCEPTION (S390_CC_ZR, "NullReferenceException");
                }
                        break;
                case OP_ARGLIST: {
@@ -4518,7 +4539,7 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
 
                /* floating point opcodes */
                case OP_R8CONST: {
-                       if (*((float *) ins->inst_p0) == 0) {
+                       if (*((double *) ins->inst_p0) == 0) {
                                s390_lzdr (code, ins->dreg);
                        } else {
                                s390_basr  (code, s390_r13, 0);
@@ -4964,8 +4985,11 @@ mono_arch_patch_code (MonoMethod *method, MonoDomain *domain,
                        case MONO_PATCH_INFO_EXC:
                        case MONO_PATCH_INFO_ABS:
                        case MONO_PATCH_INFO_METHOD:
+                       case MONO_PATCH_INFO_RGCTX_FETCH:
                        case MONO_PATCH_INFO_INTERNAL_METHOD:
                        case MONO_PATCH_INFO_CLASS_INIT:
+                       case MONO_PATCH_INFO_JIT_ICALL_ADDR:
+                       case MONO_PATCH_INFO_GENERIC_CLASS_INIT:
                                s390_patch_addr (ip, (guint64) target);
                                continue;
                        case MONO_PATCH_INFO_SWITCH: 
@@ -5022,7 +5046,7 @@ emit_load_volatile_arguments (guint8 *code, MonoCompile *cfg)
        int pos = 0, i;
        CallInfo *cinfo;
 
-       cinfo = get_call_info (NULL, NULL, sig, sig->pinvoke);
+       cinfo = get_call_info (NULL, NULL, sig);
 
        if (cinfo->struct_ret) {
                ArgInfo *ainfo = &cinfo->ret;
@@ -5176,6 +5200,15 @@ mono_arch_emit_prolog (MonoCompile *cfg)
 
        mono_emit_unwind_op_def_cfa_reg (cfg, code, cfg->frame_reg);
 
+       /* store runtime generic context */
+       if (cfg->rgctx_var) {
+               g_assert (cfg->rgctx_var->opcode == OP_REGOFFSET);
+
+               s390_stg  (code, MONO_ARCH_RGCTX_REG, 0, 
+                          cfg->rgctx_var->inst_basereg, 
+                          cfg->rgctx_var->inst_offset);
+       }
+
         /* compute max_offset in order to use short forward jumps
         * we always do it on s390 because the immediate displacement
         * for jumps is too small 
@@ -5196,7 +5229,7 @@ mono_arch_emit_prolog (MonoCompile *cfg)
        sig = mono_method_signature (method);
        pos = 0;
 
-       cinfo = get_call_info (cfg, cfg->mempool, sig, sig->pinvoke);
+       cinfo = get_call_info (cfg, cfg->mempool, sig);
 
        if (cinfo->struct_ret) {
                ArgInfo *ainfo     = &cinfo->ret;
@@ -6216,6 +6249,44 @@ mono_arch_find_imt_method (mgreg_t *regs, guint8 *code)
 
 /*========================= End of Function ========================*/
 
+/*------------------------------------------------------------------*/
+/*                                                                  */
+/* Name                - mono_arch_find_static_call_vtable                 */
+/*                                                                  */
+/* Function    - Find the static call vtable.                      */
+/*                                                                 */
+/*------------------------------------------------------------------*/
+
+MonoVTable*
+mono_arch_find_static_call_vtable (mgreg_t *regs, guint8 *code)
+{
+       mgreg_t *r = (mgreg_t*)regs;
+
+       return (MonoVTable*)(gsize) r [MONO_ARCH_RGCTX_REG];
+}
+
+/*========================= End of Function ========================*/
+
+/*------------------------------------------------------------------*/
+/*                                                                  */
+/* Name                - mono_arch_get_cie_program                         */
+/*                                                                  */
+/* Function    - Find the static call vtable.                      */
+/*                                                                 */
+/*------------------------------------------------------------------*/
+
+GSList*
+mono_arch_get_cie_program (void)
+{
+       GSList *l = NULL;
+
+       mono_add_unwind_op_def_cfa (l, NULL, NULL, STK_BASE, 0);
+
+       return(l);
+}
+
+/*========================= End of Function ========================*/
+
 #ifdef MONO_ARCH_SOFT_DEBUG_SUPPORTED
 
 /*------------------------------------------------------------------*/