Merge pull request #2921 from lewurm/lower-typechecks-later
[mono.git] / mono / mini / mini.h
1 /*
2  * Copyright 2002-2003 Ximian Inc
3  * Copyright 2003-2011 Novell Inc
4  * Copyright 2011 Xamarin Inc
5  * Licensed under the MIT license. See LICENSE file in the project root for full license information.
6  */
7 #ifndef __MONO_MINI_H__
8 #define __MONO_MINI_H__
9
10 #include "config.h"
11 #include <glib.h>
12 #ifdef HAVE_SIGNAL_H
13 #include <signal.h>
14 #endif
15 #ifdef HAVE_SYS_TYPES_H
16 #include <sys/types.h>
17 #endif
18 #include <mono/metadata/loader.h>
19 #include <mono/metadata/mempool.h>
20 #include <mono/utils/monobitset.h>
21 #include <mono/metadata/class.h>
22 #include <mono/metadata/object.h>
23 #include <mono/metadata/opcodes.h>
24 #include <mono/metadata/tabledefs.h>
25 #include <mono/metadata/domain-internals.h>
26 #include "mono/metadata/class-internals.h"
27 #include "mono/metadata/object-internals.h"
28 #include <mono/metadata/profiler-private.h>
29 #include <mono/metadata/debug-helpers.h>
30 #include <mono/utils/mono-compiler.h>
31 #include <mono/utils/mono-machine.h>
32 #include <mono/utils/mono-stack-unwinding.h>
33 #include <mono/utils/mono-threads.h>
34 #include <mono/utils/mono-threads-coop.h>
35 #include <mono/utils/mono-tls.h>
36 #include <mono/utils/atomic.h>
37 #include <mono/utils/mono-conc-hashtable.h>
38 #include <mono/utils/mono-signal-handler.h>
39
40 #include "mini-arch.h"
41 #include "regalloc.h"
42 #include "mini-unwind.h"
43 #include "jit.h"
44 #include "cfgdump.h"
45
46 #include "mono/metadata/class-internals.h"
47 #include "mono/metadata/domain-internals.h"
48 #include "mono/metadata/object.h"
49 #include "mono/metadata/tabledefs.h"
50 #include "mono/metadata/marshal.h"
51 #include "mono/metadata/security-manager.h"
52 #include "mono/metadata/exception.h"
53 #include "mono/utils/mono-compiler.h"
54
55 #ifdef __native_client_codegen__
56 #include <nacl/nacl_dyncode.h>
57 #endif
58
59
60 /*
61  * The mini code should not have any compile time dependencies on the GC being used, so the same object file from mini/
62  * can be linked into both mono and mono-sgen.
63  */
64 #if defined(HAVE_BOEHM_GC) || defined(HAVE_SGEN_GC)
65 #error "The code in mini/ should not depend on these defines."
66 #endif
67
68 #ifndef __GNUC__
69 /*#define __alignof__(a) sizeof(a)*/
70 #define __alignof__(type) G_STRUCT_OFFSET(struct { char c; type x; }, x)
71 #endif
72
73 #if DISABLE_LOGGING
74 #define MINI_DEBUG(level,limit,code)
75 #else
76 #define MINI_DEBUG(level,limit,code) do {if (G_UNLIKELY ((level) >= (limit))) code} while (0)
77 #endif
78
79 #if !defined(DISABLE_TASKLETS) && defined(MONO_ARCH_SUPPORT_TASKLETS) && defined(__GNUC__)
80 #define MONO_SUPPORT_TASKLETS 1
81 #endif
82
83 #if ENABLE_LLVM
84 #define COMPILE_LLVM(cfg) ((cfg)->compile_llvm)
85 #define LLVM_ENABLED TRUE
86 #else
87 #define COMPILE_LLVM(cfg) (0)
88 #define LLVM_ENABLED FALSE
89 #endif
90
91 #ifdef MONO_ARCH_SOFT_FLOAT_FALLBACK
92 #define COMPILE_SOFT_FLOAT(cfg) (!COMPILE_LLVM ((cfg)) && mono_arch_is_soft_float ())
93 #else
94 #define COMPILE_SOFT_FLOAT(cfg) (0)
95 #endif
96
97 #define NOT_IMPLEMENTED do { g_assert_not_reached (); } while (0)
98
99 /* for 32 bit systems */
100 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
101 #define MINI_LS_WORD_IDX 0
102 #define MINI_MS_WORD_IDX 1
103 #else
104 #define MINI_LS_WORD_IDX 1
105 #define MINI_MS_WORD_IDX 0
106 #endif
107 #define MINI_LS_WORD_OFFSET (MINI_LS_WORD_IDX * 4)
108 #define MINI_MS_WORD_OFFSET (MINI_MS_WORD_IDX * 4)
109 #define inst_ls_word data.op[MINI_LS_WORD_IDX].const_val
110 #define inst_ms_word data.op[MINI_MS_WORD_IDX].const_val
111
112 #define MONO_LVREG_LS(lvreg)    ((lvreg) + 1)
113 #define MONO_LVREG_MS(lvreg)    ((lvreg) + 2)
114
115 #ifndef DISABLE_AOT
116 #define MONO_USE_AOT_COMPILER
117 #endif
118
119 /* Version number of the AOT file format */
120 #define MONO_AOT_FILE_VERSION 133
121
122 //TODO: This is x86/amd64 specific.
123 #define mono_simd_shuffle_mask(a,b,c,d) ((a) | ((b) << 2) | ((c) << 4) | ((d) << 6))
124
125 /* Remap printf to g_print (we use a mix of these in the mini code) */
126 #ifdef PLATFORM_ANDROID
127 #define printf g_print
128 #endif
129
130 #define MONO_TYPE_IS_PRIMITIVE(t) ((!(t)->byref && ((((t)->type >= MONO_TYPE_BOOLEAN && (t)->type <= MONO_TYPE_R8) || ((t)->type >= MONO_TYPE_I && (t)->type <= MONO_TYPE_U)))))
131
132 #define MONO_AOT_TRAMP_PAGE_SIZE 16384
133
134 /* Constants used to encode different types of methods in AOT */
135 enum {
136         MONO_AOT_METHODREF_MIN = 240,
137         /* Image index bigger than METHODREF_MIN */
138         MONO_AOT_METHODREF_LARGE_IMAGE_INDEX = 249,
139         /* Runtime provided methods on arrays */
140         MONO_AOT_METHODREF_ARRAY = 250,
141         MONO_AOT_METHODREF_NO_AOT_TRAMPOLINE = 251,
142         /* Wrappers */
143         MONO_AOT_METHODREF_WRAPPER = 252,
144         /* Methods on generic instances */
145         MONO_AOT_METHODREF_GINST = 253,
146         /* Methods resolve using a METHODSPEC token */
147         MONO_AOT_METHODREF_METHODSPEC = 254,
148 };
149
150 /* Constants used to encode different types of types in AOT */
151 enum {
152         /* typedef index */
153         MONO_AOT_TYPEREF_TYPEDEF_INDEX = 1,
154         /* typedef index + image index */
155         MONO_AOT_TYPEREF_TYPEDEF_INDEX_IMAGE = 2,
156         /* typespec token */
157         MONO_AOT_TYPEREF_TYPESPEC_TOKEN = 3,
158         /* generic inst */
159         MONO_AOT_TYPEREF_GINST = 4,
160         /* type/method variable */
161         MONO_AOT_TYPEREF_VAR = 5,
162         /* array */
163         MONO_AOT_TYPEREF_ARRAY = 6,
164         /* blob index of the type encoding */
165         MONO_AOT_TYPEREF_BLOB_INDEX = 7,
166         /* ptr */
167         MONO_AOT_TYPEREF_PTR = 8
168 };
169
170 /* Trampolines which we have a lot of */
171 typedef enum {
172         MONO_AOT_TRAMP_SPECIFIC = 0,
173         MONO_AOT_TRAMP_STATIC_RGCTX = 1,
174         MONO_AOT_TRAMP_IMT_THUNK = 2,
175         MONO_AOT_TRAMP_GSHAREDVT_ARG = 3,
176         MONO_AOT_TRAMP_NUM = 4
177 } MonoAotTrampoline;
178
179 typedef enum {
180         MONO_AOT_FILE_FLAG_WITH_LLVM = 1,
181         MONO_AOT_FILE_FLAG_FULL_AOT = 2,
182         MONO_AOT_FILE_FLAG_DEBUG = 4,
183         MONO_AOT_FILE_FLAG_LLVM_THUMB = 8,
184         MONO_AOT_FILE_FLAG_LLVM_ONLY = 16,
185         MONO_AOT_FILE_FLAG_SAFEPOINTS = 32,
186         MONO_AOT_FILE_FLAG_SEPARATE_DATA = 64,
187 } MonoAotFileFlags;
188
189 typedef enum {
190         MONO_AOT_TABLE_BLOB,
191         MONO_AOT_TABLE_IMAGE_TABLE,
192         MONO_AOT_TABLE_CLASS_NAME,
193         MONO_AOT_TABLE_METHOD_INFO_OFFSETS,
194         MONO_AOT_TABLE_EX_INFO_OFFSETS,
195         MONO_AOT_TABLE_CLASS_INFO_OFFSETS,
196         MONO_AOT_TABLE_GOT_INFO_OFFSETS,
197         MONO_AOT_TABLE_LLVM_GOT_INFO_OFFSETS,
198         MONO_AOT_TABLE_EXTRA_METHOD_INFO_OFFSETS,
199         MONO_AOT_TABLE_EXTRA_METHOD_TABLE,
200         MONO_AOT_TABLE_NUM
201 } MonoAotFileTable;
202
203 /* This structure is stored in the AOT file */
204 typedef struct MonoAotFileInfo
205 {
206         /* The version number of the AOT file format, should match MONO_AOT_FILE_VERSION */
207         guint32 version;
208         /* For alignment */
209         guint32 dummy;
210
211         /* All the pointers should be at the start to avoid alignment problems */
212         /* Symbols */
213 #define MONO_AOT_FILE_INFO_FIRST_SYMBOL jit_got
214         /* Global Offset Table for JITted code */
215         gpointer jit_got;
216         /* Global Offset Table for LLVM code */
217         gpointer llvm_got;
218         /* Mono EH Frame created by llc when using LLVM */
219         gpointer mono_eh_frame;
220         /* Points to the get_method () function in the LLVM image or NULL */
221         gpointer llvm_get_method;
222         /* Points to the get_unbox_tramp () function in the LLVM image or NULL */
223         gpointer llvm_get_unbox_tramp;
224         gpointer jit_code_start;
225         gpointer jit_code_end;
226         gpointer method_addresses;
227         /* Data blob */
228         gpointer blob;
229         gpointer class_name_table;
230         gpointer class_info_offsets;
231         gpointer method_info_offsets;
232         gpointer ex_info_offsets;
233         gpointer extra_method_info_offsets;
234         gpointer extra_method_table;
235         gpointer got_info_offsets;
236         gpointer llvm_got_info_offsets;
237         gpointer image_table;
238         gpointer mem_end;
239         /* The GUID of the assembly which the AOT image was generated from */
240         gpointer assembly_guid;
241         /*
242          * The runtime version string for AOT images generated using 'bind-to-runtime-version',
243          * NULL otherwise.
244          */
245         gpointer runtime_version;
246         /* Blocks of various kinds of trampolines */
247         gpointer specific_trampolines;
248         gpointer static_rgctx_trampolines;
249         gpointer imt_thunks;
250         gpointer gsharedvt_arg_trampolines;
251         /* In static mode, points to a table of global symbols for trampolines etc */
252         gpointer globals;
253         /* Points to a string containing the assembly name*/
254         gpointer assembly_name;
255         /* Start of Mono's Program Linkage Table */
256         gpointer plt;
257         /* End of Mono's Program Linkage Table */
258         gpointer plt_end;
259         gpointer unwind_info;
260         /* Points to a table mapping methods to their unbox trampolines */
261         gpointer unbox_trampolines;
262         /* Points to the end of the previous table */
263         gpointer unbox_trampolines_end;
264         /* Points to a table of unbox trampoline addresses/offsets */
265         gpointer unbox_trampoline_addresses;
266 #define MONO_AOT_FILE_INFO_LAST_SYMBOL unbox_trampoline_addresses
267
268         /* Scalars */
269         /* The index of the first GOT slot used by the PLT */
270         guint32 plt_got_offset_base;
271         /* Number of entries in the GOT */
272         guint32 got_size;
273         /* Number of entries in the PLT */
274         guint32 plt_size;
275         /* Number of methods */
276         guint32 nmethods;
277         /* A union of MonoAotFileFlags */
278         guint32 flags;
279         /* Optimization flags used to compile the module */
280         guint32 opts;
281         /* SIMD flags used to compile the module */
282         guint32 simd_opts;
283         /* Index of the blob entry holding the GC used by this module */
284         gint32 gc_name_index;
285         guint32 num_rgctx_fetch_trampolines;
286         /* These are used for sanity checking object layout problems when cross-compiling */
287         guint32 double_align, long_align, generic_tramp_num;
288         /* The page size used by trampoline pages */
289         guint32 tramp_page_size;
290         /*
291          * The number of GOT entries which need to be preinitialized when the
292          * module is loaded.
293          */
294         guint32 nshared_got_entries;
295         /* The size of the data file, if MONO_AOT_FILE_FLAG_SEPARATE_DATA is set */
296         guint32 datafile_size;
297
298         /* Arrays */
299         /* Offsets for tables inside the data file if MONO_AOT_FILE_FLAG_SEPARATE_DATA is set */
300         // FIXME: Sync with AOT
301         guint32 table_offsets [MONO_AOT_TABLE_NUM];
302         /* Number of trampolines */
303         guint32 num_trampolines [MONO_AOT_TRAMP_NUM];
304         /* The indexes of the first GOT slots used by the trampolines */
305         guint32 trampoline_got_offset_base [MONO_AOT_TRAMP_NUM];
306         /* The size of one trampoline */
307         guint32 trampoline_size [MONO_AOT_TRAMP_NUM];
308         /* The offset where the trampolines begin on a trampoline page */
309         guint32 tramp_page_code_offsets [MONO_AOT_TRAMP_NUM];
310 } MonoAotFileInfo;
311
312 /* Number of symbols in the MonoAotFileInfo structure */
313 #define MONO_AOT_FILE_INFO_NUM_SYMBOLS (((G_STRUCT_OFFSET (MonoAotFileInfo, MONO_AOT_FILE_INFO_LAST_SYMBOL) - G_STRUCT_OFFSET (MonoAotFileInfo, MONO_AOT_FILE_INFO_FIRST_SYMBOL)) / sizeof (gpointer)) + 1)
314
315 typedef struct
316 {
317         MonoClass *klass;
318         MonoMethod *method;
319 } MonoClassMethodPair;
320
321 typedef struct
322 {
323         MonoClass *klass;
324         MonoMethod *method;
325         gboolean is_virtual;
326 } MonoDelegateClassMethodPair;
327
328 /* Per-domain information maintained by the JIT */
329 typedef struct
330 {
331         /* Maps MonoMethod's to a GSList of GOT slot addresses pointing to its code */
332         GHashTable *jump_target_got_slot_hash;
333         GHashTable *jump_target_hash;
334         /* Maps methods/klasses to the address of the given type of trampoline */
335         GHashTable *class_init_trampoline_hash;
336         GHashTable *jump_trampoline_hash;
337         GHashTable *jit_trampoline_hash;
338         GHashTable *delegate_trampoline_hash;
339         /* Maps ClassMethodPair -> MonoDelegateTrampInfo */
340         GHashTable *static_rgctx_trampoline_hash;
341         GHashTable *llvm_vcall_trampoline_hash;
342         /* maps MonoMethod -> MonoJitDynamicMethodInfo */
343         GHashTable *dynamic_code_hash;
344         GHashTable *method_code_hash;
345         /* Maps methods to a RuntimeInvokeInfo structure, protected by the associated MonoDomain lock */
346         MonoConcurrentHashTable *runtime_invoke_hash;
347         /* Maps MonoMethod to a GPtrArray containing sequence point locations */
348         /* Protected by the domain lock */
349         GHashTable *seq_points;
350         /* Debugger agent data */
351         gpointer agent_info;
352         /* Maps MonoMethod to an arch-specific structure */
353         GHashTable *arch_seq_points;
354         /* Maps a GSharedVtTrampInfo structure to a trampoline address */
355         GHashTable *gsharedvt_arg_tramp_hash;
356         /* memcpy/bzero methods specialized for small constant sizes */
357         gpointer *memcpy_addr [17];
358         gpointer *bzero_addr [17];
359         gpointer llvm_module;
360         /* Maps MonoMethod -> GSlist of addresses */
361         GHashTable *llvm_jit_callees;
362 } MonoJitDomainInfo;
363
364 typedef struct {
365         MonoJitInfo *ji;
366         MonoCodeManager *code_mp;
367 } MonoJitDynamicMethodInfo;
368
369 /* An extension of MonoGenericParamFull used in generic sharing */
370 typedef struct {
371         MonoGenericParamFull param;
372         MonoGenericParam *parent;
373 } MonoGSharedGenericParam;
374
375 #define domain_jit_info(domain) ((MonoJitDomainInfo*)((domain)->runtime_info))
376
377 /* Contains a list of ips which needs to be patched when a method is compiled */
378 typedef struct {
379         GSList *list;
380 } MonoJumpList;
381
382 /* Arch-specific */
383 typedef struct {
384         int dummy;
385 } MonoDynCallInfo;
386
387 /*
388  * Information about a stack frame.
389  * FIXME This typedef exists only to avoid tons of code rewriting
390  */
391 typedef MonoStackFrameInfo StackFrameInfo;
392
393 #if 0
394 #define mono_bitset_foreach_bit(set,b,n) \
395         for (b = 0; b < n; b++)\
396                 if (mono_bitset_test_fast(set,b))
397 #else
398 #define mono_bitset_foreach_bit(set,b,n) \
399         for (b = mono_bitset_find_start (set); b < n && b >= 0; b = mono_bitset_find_first (set, b))
400 #endif
401
402 /*
403  * Pull the list of opcodes
404  */
405 #define OPDEF(a,b,c,d,e,f,g,h,i,j) \
406         a = i,
407
408 enum {
409 #include "mono/cil/opcode.def"
410         CEE_LASTOP
411 };
412 #undef OPDEF
413
414 #define MONO_VARINFO(cfg,varnum) (&(cfg)->vars [varnum])
415
416 #define MONO_INST_NULLIFY_SREGS(dest) do {                              \
417                 (dest)->sreg1 = (dest)->sreg2 = (dest)->sreg3 = -1;     \
418         } while (0)
419
420 #define MONO_INST_NEW(cfg,dest,op) do { \
421                 (dest) = (MonoInst *)mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst));   \
422                 (dest)->opcode = (op);  \
423                 (dest)->dreg = -1;                          \
424                 MONO_INST_NULLIFY_SREGS ((dest));           \
425         (dest)->cil_code = (cfg)->ip;  \
426         } while (0)
427
428 #define MONO_INST_NEW_CALL(cfg,dest,op) do {    \
429                 (dest) = (MonoCallInst *)mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoCallInst));   \
430                 (dest)->inst.opcode = (op);     \
431                 (dest)->inst.dreg = -1;                                 \
432                 MONO_INST_NULLIFY_SREGS (&(dest)->inst);                \
433         (dest)->inst.cil_code = (cfg)->ip;  \
434         } while (0)
435
436 #define MONO_ADD_INS(b,inst) do {       \
437                 if ((b)->last_ins) {    \
438                         (b)->last_ins->next = (inst);   \
439             (inst)->prev = (b)->last_ins;   \
440                         (b)->last_ins = (inst); \
441                 } else {        \
442                         (b)->code = (b)->last_ins = (inst);     \
443                 }       \
444         } while (0)
445
446 #define NULLIFY_INS(ins) do { \
447         (ins)->opcode = OP_NOP; \
448         (ins)->dreg = -1;                               \
449         MONO_INST_NULLIFY_SREGS ((ins));                \
450     } while (0)
451
452 /* Remove INS from BB */
453 #define MONO_REMOVE_INS(bb,ins) do { \
454         if ((ins)->prev) \
455             (ins)->prev->next = (ins)->next; \
456         if ((ins)->next) \
457             (ins)->next->prev = (ins)->prev; \
458         if ((bb)->code == (ins)) \
459             (bb)->code = (ins)->next; \
460         if ((bb)->last_ins == (ins)) \
461             (bb)->last_ins = (ins)->prev; \
462     } while (0)
463
464 /* Remove INS from BB and nullify it */
465 #define MONO_DELETE_INS(bb,ins) do { \
466         MONO_REMOVE_INS ((bb), (ins)); \
467         NULLIFY_INS ((ins)); \
468     } while (0)
469
470 /* 
471  * this is used to determine when some branch optimizations are possible: we exclude FP compares
472  * because they have weird semantics with NaNs.
473  */
474 #define MONO_IS_COND_BRANCH_OP(ins) (((ins)->opcode >= OP_LBEQ && (ins)->opcode <= OP_LBLT_UN) || ((ins)->opcode >= OP_FBEQ && (ins)->opcode <= OP_FBLT_UN) || ((ins)->opcode >= OP_IBEQ && (ins)->opcode <= OP_IBLT_UN))
475 #define MONO_IS_COND_BRANCH_NOFP(ins) (MONO_IS_COND_BRANCH_OP(ins) && !(((ins)->opcode >= OP_FBEQ) && ((ins)->opcode <= OP_FBLT_UN)) && (!(ins)->inst_left || (ins)->inst_left->inst_left->type != STACK_R8))
476
477 #define MONO_IS_BRANCH_OP(ins) (MONO_IS_COND_BRANCH_OP(ins) || ((ins)->opcode == OP_BR) || ((ins)->opcode == OP_BR_REG) || ((ins)->opcode == OP_SWITCH))
478
479 #define MONO_IS_COND_EXC(ins) ((((ins)->opcode >= OP_COND_EXC_EQ) && ((ins)->opcode <= OP_COND_EXC_LT_UN)) || (((ins)->opcode >= OP_COND_EXC_IEQ) && ((ins)->opcode <= OP_COND_EXC_ILT_UN)))
480
481 #define MONO_IS_SETCC(ins) ((((ins)->opcode >= OP_CEQ) && ((ins)->opcode <= OP_CLT_UN)) || (((ins)->opcode >= OP_ICEQ) && ((ins)->opcode <= OP_ICLT_UN)) || (((ins)->opcode >= OP_LCEQ) && ((ins)->opcode <= OP_LCLT_UN)) || (((ins)->opcode >= OP_FCEQ) && ((ins)->opcode <= OP_FCLT_UN)))
482
483
484 #define MONO_IS_LOAD_MEMBASE(ins) (((ins)->opcode >= OP_LOAD_MEMBASE && (ins)->opcode <= OP_LOADV_MEMBASE) || ((ins)->opcode >= OP_ATOMIC_LOAD_I1 && (ins)->opcode <= OP_ATOMIC_LOAD_R8))
485 #define MONO_IS_STORE_MEMBASE(ins) (((ins)->opcode >= OP_STORE_MEMBASE_REG && (ins)->opcode <= OP_STOREV_MEMBASE) || ((ins)->opcode >= OP_ATOMIC_STORE_I1 && (ins)->opcode <= OP_ATOMIC_STORE_R8))
486 #define MONO_IS_STORE_MEMINDEX(ins) (((ins)->opcode >= OP_STORE_MEMINDEX) && ((ins)->opcode <= OP_STORER8_MEMINDEX))
487
488 // OP_DYN_CALL is not a MonoCallInst
489 #define MONO_IS_CALL(ins) (((ins->opcode >= OP_VOIDCALL) && (ins->opcode <= OP_VCALL2_MEMBASE)) || (ins->opcode == OP_TAILCALL))
490
491 #define MONO_IS_JUMP_TABLE(ins) (((ins)->opcode == OP_JUMP_TABLE) ? TRUE : ((((ins)->opcode == OP_AOTCONST) && (ins->inst_i1 == (gpointer)MONO_PATCH_INFO_SWITCH)) ? TRUE : ((ins)->opcode == OP_SWITCH) ? TRUE : ((((ins)->opcode == OP_GOT_ENTRY) && ((ins)->inst_right->inst_i1 == (gpointer)MONO_PATCH_INFO_SWITCH)) ? TRUE : FALSE)))
492
493 #define MONO_JUMP_TABLE_FROM_INS(ins) (((ins)->opcode == OP_JUMP_TABLE) ? (ins)->inst_p0 : (((ins)->opcode == OP_AOTCONST) && (ins->inst_i1 == (gpointer)MONO_PATCH_INFO_SWITCH) ? (ins)->inst_p0 : (((ins)->opcode == OP_SWITCH) ? (ins)->inst_p0 : ((((ins)->opcode == OP_GOT_ENTRY) && ((ins)->inst_right->inst_i1 == (gpointer)MONO_PATCH_INFO_SWITCH)) ? (ins)->inst_right->inst_p0 : NULL))))
494
495 /* FIXME: Add more instructions */
496 /* INEG sets the condition codes, and the OP_LNEG decomposition depends on this on x86 */
497 #define MONO_INS_HAS_NO_SIDE_EFFECT(ins) (MONO_IS_MOVE (ins) || (ins->opcode == OP_ICONST) || (ins->opcode == OP_I8CONST) || MONO_IS_ZERO (ins) || (ins->opcode == OP_ADD_IMM) || (ins->opcode == OP_R8CONST) || (ins->opcode == OP_LADD_IMM) || (ins->opcode == OP_ISUB_IMM) || (ins->opcode == OP_IADD_IMM) || (ins->opcode == OP_LNEG) || (ins->opcode == OP_ISUB) || (ins->opcode == OP_CMOV_IGE) || (ins->opcode == OP_ISHL_IMM) || (ins->opcode == OP_ISHR_IMM) || (ins->opcode == OP_ISHR_UN_IMM) || (ins->opcode == OP_IAND_IMM) || (ins->opcode == OP_ICONV_TO_U1) || (ins->opcode == OP_ICONV_TO_I1) || (ins->opcode == OP_SEXT_I4) || (ins->opcode == OP_LCONV_TO_U1) || (ins->opcode == OP_ICONV_TO_U2) || (ins->opcode == OP_ICONV_TO_I2) || (ins->opcode == OP_LCONV_TO_I2) || (ins->opcode == OP_LDADDR) || (ins->opcode == OP_PHI) || (ins->opcode == OP_NOP) || (ins->opcode == OP_ZEXT_I4) || (ins->opcode == OP_NOT_NULL) || (ins->opcode == OP_IL_SEQ_POINT))
498
499 #define MONO_METHOD_IS_FINAL(m) (((m)->flags & METHOD_ATTRIBUTE_FINAL) || ((m)->klass && ((m)->klass->flags & TYPE_ATTRIBUTE_SEALED)))
500
501
502 #ifdef MONO_ARCH_SIMD_INTRINSICS
503
504 #define MONO_IS_PHI(ins) (((ins)->opcode == OP_PHI) || ((ins)->opcode == OP_FPHI) || ((ins)->opcode == OP_VPHI)  || ((ins)->opcode == OP_XPHI))
505 #define MONO_IS_MOVE(ins) (((ins)->opcode == OP_MOVE) || ((ins)->opcode == OP_FMOVE) || ((ins)->opcode == OP_VMOVE) || ((ins)->opcode == OP_XMOVE) || ((ins)->opcode == OP_RMOVE))
506 #define MONO_IS_NON_FP_MOVE(ins) (((ins)->opcode == OP_MOVE) || ((ins)->opcode == OP_VMOVE) || ((ins)->opcode == OP_XMOVE))
507 #define MONO_IS_REAL_MOVE(ins) (((ins)->opcode == OP_MOVE) || ((ins)->opcode == OP_FMOVE) || ((ins)->opcode == OP_XMOVE) || ((ins)->opcode == OP_RMOVE))
508 #define MONO_IS_ZERO(ins) (((ins)->opcode == OP_VZERO) || ((ins)->opcode == OP_XZERO))
509
510 #define MONO_CLASS_IS_SIMD(cfg, klass) (((cfg)->opt & MONO_OPT_SIMD) && (klass)->simd_type)
511
512 #else
513
514 #define MONO_IS_PHI(ins) (((ins)->opcode == OP_PHI) || ((ins)->opcode == OP_FPHI) || ((ins)->opcode == OP_VPHI))
515 #define MONO_IS_MOVE(ins) (((ins)->opcode == OP_MOVE) || ((ins)->opcode == OP_FMOVE) || ((ins)->opcode == OP_VMOVE) || ((ins)->opcode == OP_RMOVE))
516 #define MONO_IS_NON_FP_MOVE(ins) (((ins)->opcode == OP_MOVE) || ((ins)->opcode == OP_VMOVE))
517 /*A real MOVE is one that isn't decomposed such as a VMOVE or LMOVE*/
518 #define MONO_IS_REAL_MOVE(ins) (((ins)->opcode == OP_MOVE) || ((ins)->opcode == OP_FMOVE) || ((ins)->opcode == OP_RMOVE))
519 #define MONO_IS_ZERO(ins) ((ins)->opcode == OP_VZERO)
520
521 #define MONO_CLASS_IS_SIMD(cfg, klass) (0)
522
523 #endif
524
525 typedef struct MonoInstList MonoInstList;
526 typedef struct MonoInst MonoInst;
527 typedef struct MonoCallInst MonoCallInst;
528 typedef struct MonoCallArgParm MonoCallArgParm;
529 typedef struct MonoMethodVar MonoMethodVar;
530 typedef struct MonoBasicBlock MonoBasicBlock;
531 typedef struct MonoLMF MonoLMF;
532 typedef struct MonoSpillInfo MonoSpillInfo;
533 typedef struct MonoTraceSpec MonoTraceSpec;
534
535 extern MonoNativeTlsKey mono_jit_tls_id;
536 extern MonoTraceSpec *mono_jit_trace_calls;
537 extern gboolean mono_break_on_exc;
538 extern int mono_exc_esp_offset;
539 extern gboolean mono_compile_aot;
540 extern gboolean mono_aot_only;
541 extern gboolean mono_llvm_only;
542 extern MonoAotMode mono_aot_mode;
543 extern MonoMethodDesc *mono_inject_async_exc_method;
544 extern int mono_inject_async_exc_pos;
545 extern MonoMethodDesc *mono_break_at_bb_method;
546 extern int mono_break_at_bb_bb_num;
547 extern gboolean mono_verify_all;
548 extern gboolean mono_do_x86_stack_align;
549 extern const char *mono_build_date;
550 extern gboolean mono_do_signal_chaining;
551 extern gboolean mono_do_crash_chaining;
552 extern MONO_API gboolean mono_use_llvm;
553 extern gboolean mono_do_single_method_regression;
554 extern guint32 mono_single_method_regression_opt;
555 extern MonoMethod *mono_current_single_method;
556 extern GSList *mono_single_method_list;
557 extern GHashTable *mono_single_method_hash;
558 extern gboolean mono_using_xdebug;
559 extern int mini_verbose;
560 extern int valgrind_register;
561
562 #define INS_INFO(opcode) (&ins_info [((opcode) - OP_START - 1) * 4])
563
564 extern const char ins_info[];
565 extern const gint8 ins_sreg_counts [];
566
567 #ifndef DISABLE_JIT
568 #define mono_inst_get_num_src_registers(ins) (ins_sreg_counts [(ins)->opcode - OP_START - 1])
569 #else
570 #define mono_inst_get_num_src_registers(ins) 0
571 #endif
572
573 #define mono_inst_get_src_registers(ins, regs) (((regs) [0] = (ins)->sreg1), ((regs) [1] = (ins)->sreg2), ((regs) [2] = (ins)->sreg3), mono_inst_get_num_src_registers ((ins)))
574
575 #define MONO_BB_FOR_EACH_INS(bb, ins) for ((ins) = (bb)->code; (ins); (ins) = (ins)->next)
576
577 #define MONO_BB_FOR_EACH_INS_SAFE(bb, n, ins) for ((ins) = (bb)->code, n = (ins) ? (ins)->next : NULL; (ins); (ins) = (n), (n) = (ins) ? (ins)->next : NULL)
578
579 #define MONO_BB_FOR_EACH_INS_REVERSE_SAFE(bb, p, ins) for ((ins) = (bb)->last_ins, p = (ins) ? (ins)->prev : NULL; (ins); (ins) = (p), (p) = (ins) ? (ins)->prev : NULL)
580
581 #define mono_bb_first_ins(bb) (bb)->code
582
583 struct MonoSpillInfo {
584         int offset;
585 };
586
587 /*
588  * Information about a call site for the GC map creation code
589  */
590 typedef struct {
591         /* The next offset after the call instruction */
592         int pc_offset;
593         /* The basic block containing the call site */
594         MonoBasicBlock *bb;
595         /* 
596          * The set of variables live at the call site.
597          * Has length cfg->num_varinfo in bits.
598          */
599         guint8 *liveness;
600         /*
601          * List of OP_GC_PARAM_SLOT_LIVENESS_DEF instructions defining the param slots
602          * used by this call.
603          */
604         GSList *param_slots;
605 } GCCallSite;
606
607 /*
608  * The IR-level extended basic block.  
609  *
610  * A basic block can have multiple exits just fine, as long as the point of
611  * 'departure' is the last instruction in the basic block. Extended basic
612  * blocks, on the other hand, may have instructions that leave the block
613  * midstream. The important thing is that they cannot be _entered_
614  * midstream, ie, execution of a basic block (or extened bb) always start
615  * at the beginning of the block, never in the middle.
616  */
617 struct MonoBasicBlock {
618         MonoInst *last_ins;
619
620         /* the next basic block in the order it appears in IL */
621         MonoBasicBlock *next_bb;
622
623         /*
624          * Before instruction selection it is the first tree in the
625          * forest and the first item in the list of trees. After
626          * instruction selection it is the first instruction and the
627          * first item in the list of instructions.
628          */
629         MonoInst *code;
630
631         /* unique block number identification */
632         gint32 block_num;
633         
634         gint32 dfn;
635
636         /* Basic blocks: incoming and outgoing counts and pointers */
637         /* Each bb should only appear once in each array */
638         gint16 out_count, in_count;
639         MonoBasicBlock **in_bb;
640         MonoBasicBlock **out_bb;
641
642         /* Points to the start of the CIL code that initiated this BB */
643         unsigned char* cil_code;
644
645         /* Length of the CIL block */
646         gint32 cil_length;
647
648         /* The offset of the generated code, used for fixups */
649         int native_offset;
650         /* The length of the generated code, doesn't include alignment padding */
651         int native_length;
652         /* The real native offset, which includes alignment padding too */
653         int real_native_offset;
654         int max_offset;
655         int max_length;
656
657         /* Visited and reachable flags */
658         guint32 flags;
659
660         /*
661          * SSA and loop based flags
662          */
663         MonoBitSet *dominators;
664         MonoBitSet *dfrontier;
665         MonoBasicBlock *idom;
666         GSList *dominated;
667         /* fast dominator algorithm */
668         MonoBasicBlock *df_parent, *ancestor, *child, *label;
669         int size, sdom, idomn;
670         
671         /* loop nesting and recognition */
672         GList *loop_blocks;
673         gint8  nesting;
674         gint8  loop_body_start;
675
676         /* 
677          * Whenever the bblock is rarely executed so it should be emitted after
678          * the function epilog.
679          */
680         guint out_of_line : 1;
681         /* Caches the result of uselessness calculation during optimize_branches */
682         guint not_useless : 1;
683         /* Whenever the decompose_array_access_opts () pass needs to process this bblock */
684         guint has_array_access : 1;
685         /* Whenever this bblock is extended, ie. it has branches inside it */
686         guint extended : 1;
687         /* Whenever this bblock contains a OP_JUMP_TABLE instruction */
688         guint has_jump_table : 1;
689         /* Whenever this bblock contains an OP_CALL_HANDLER instruction */
690         guint has_call_handler : 1;
691         /* Whenever this bblock starts a try block */
692         guint try_start : 1;
693
694 #ifdef ENABLE_LLVM
695         /* The offset of the CIL instruction in this bblock which ends a try block */
696         intptr_t try_end;
697 #endif
698
699         /*
700          * If this is set, extend the try range started by this bblock by an arch specific
701          * number of bytes to encompass the end of the previous bblock (e.g. a Monitor.Enter
702          * call).
703          */
704         guint extend_try_block : 1;
705         
706         /* use for liveness analysis */
707         MonoBitSet *gen_set;
708         MonoBitSet *kill_set;
709         MonoBitSet *live_in_set;
710         MonoBitSet *live_out_set;
711
712         /* fields to deal with non-empty stack slots at bb boundary */
713         guint16 out_scount, in_scount;
714         MonoInst **out_stack;
715         MonoInst **in_stack;
716
717         /* we use that to prevent merging of bblocks covered by different clauses*/
718         guint real_offset;
719
720         GSList *seq_points;
721         MonoInst *last_seq_point;
722
723         GSList *spill_slot_defs;
724
725         /* List of call sites in this bblock sorted by pc_offset */
726         GSList *gc_callsites;
727
728         /*
729          * The region encodes whether the basic block is inside
730          * a finally, catch, filter or none of these.
731          *
732          * If the value is -1, then it is neither finally, catch nor filter
733          *
734          * Otherwise the format is:
735          *
736          *  Bits: |     0-3      |       4-7      |     8-31
737          *        |              |                |
738          *        | clause-flags |   MONO_REGION  | clause-index 
739          *
740          */
741         guint region;
742
743         /* The current symbolic register number, used in local register allocation. */
744         guint32 max_vreg;
745 };
746
747 /* BBlock flags */
748 enum {
749         BB_VISITED              = 1 << 0,
750         BB_REACHABLE            = 1 << 1,
751         BB_EXCEPTION_DEAD_OBJ   = 1 << 2,
752         BB_EXCEPTION_UNSAFE     = 1 << 3,
753         BB_EXCEPTION_HANDLER    = 1 << 4,
754         /* for Native Client, mark the blocks that can be jumped to indirectly */
755         BB_INDIRECT_JUMP_TARGET = 1 << 5 
756 };
757
758 typedef struct MonoMemcpyArgs {
759         int size, align;
760 } MonoMemcpyArgs;
761
762 typedef enum {
763         LLVMArgNone,
764         /* Scalar argument passed by value */
765         LLVMArgNormal,
766         /* Only in ainfo->pair_storage */
767         LLVMArgInIReg,
768         /* Only in ainfo->pair_storage */
769         LLVMArgInFPReg,
770         /* Valuetype passed in 1-2 consecutive register */
771         LLVMArgVtypeInReg,
772         LLVMArgVtypeByVal,
773         LLVMArgVtypeRetAddr, /* On on cinfo->ret */
774         LLVMArgGSharedVt,
775         /* Fixed size argument passed to/returned from gsharedvt method by ref */
776         LLVMArgGsharedvtFixed,
777         /* Fixed size vtype argument passed to/returned from gsharedvt method by ref */
778         LLVMArgGsharedvtFixedVtype,
779         /* Variable sized argument passed to/returned from gsharedvt method by ref */
780         LLVMArgGsharedvtVariable,
781         /* Vtype passed/returned as one int array argument */
782         LLVMArgAsIArgs,
783         /* Vtype passed as a set of fp arguments */
784         LLVMArgAsFpArgs,
785         /*
786          * Only for returns, a structure which
787          * consists of floats/doubles.
788          */
789         LLVMArgFpStruct,
790         LLVMArgVtypeByRef,
791         /* Vtype returned as an int */
792         LLVMArgVtypeAsScalar
793 } LLVMArgStorage;
794
795 typedef struct {
796         LLVMArgStorage storage;
797
798         /*
799          * Only if storage == ArgValuetypeInReg/LLVMArgAsFpArgs.
800          * This contains how the parts of the vtype are passed.
801          */
802         LLVMArgStorage pair_storage [8];
803         /*
804          * Only if storage == LLVMArgAsIArgs/LLVMArgAsFpArgs/LLVMArgFpStruct.
805          * If storage == LLVMArgAsFpArgs, this is the number of arguments
806          * used to pass the value.
807          * If storage == LLVMArgFpStruct, this is the number of fields
808          * in the structure.
809          */
810         int nslots;
811         /* Only if storage == LLVMArgAsFpArgs/LLVMArgFpStruct (4/8) */
812         int esize;
813         /* Parameter index in the LLVM signature */
814         int pindex;
815         MonoType *type;
816         /* Only if storage == LLVMArgAsFpArgs. Dummy fp args to insert before this arg */
817         int ndummy_fpargs;
818 } LLVMArgInfo;
819
820 typedef struct {
821         LLVMArgInfo ret;
822         /* Whenever there is an rgctx argument */
823         gboolean rgctx_arg;
824         /* Whenever there is an IMT argument */
825         gboolean imt_arg;
826         /* 
827          * The position of the vret arg in the argument list.
828          * Only if ret->storage == ArgVtypeRetAddr.
829          * Should be 0 or 1.
830          */
831         int vret_arg_index;
832         /* The indexes of various special arguments in the LLVM signature */
833         int vret_arg_pindex, this_arg_pindex, rgctx_arg_pindex, imt_arg_pindex;
834
835         /* Inline array of argument info */
836         /* args [0] is for the this argument if it exists */
837         LLVMArgInfo args [1];
838 } LLVMCallInfo;
839
840 #define MONO_MAX_SRC_REGS       3
841
842 struct MonoInst {
843         guint16 opcode;
844         guint8  type; /* stack type */
845         guint8  flags;
846         
847         /* used by the register allocator */
848         gint32 dreg, sreg1, sreg2, sreg3;
849
850         MonoInst *next, *prev;
851
852         union {
853                 union {
854                         MonoInst *src;
855                         MonoMethodVar *var;
856                         mgreg_t const_val;
857 #if (SIZEOF_REGISTER > SIZEOF_VOID_P) && (G_BYTE_ORDER == G_BIG_ENDIAN)
858                         struct {
859                                 gpointer p[SIZEOF_REGISTER/SIZEOF_VOID_P];
860                         } pdata;
861 #else
862                         gpointer p;
863 #endif
864                         MonoMethod *method;
865                         MonoMethodSignature *signature;
866                         MonoBasicBlock **many_blocks;
867                         MonoBasicBlock *target_block;
868                         MonoInst **args;
869                         MonoType *vtype;
870                         MonoClass *klass;
871                         int *phi_args;
872                         MonoCallInst *call_inst;
873                         MonoExceptionClause *exception_clause;
874                 } op [2];
875                 gint64 i8const;
876                 double r8const;
877         } data;
878
879         const unsigned char* cil_code; /* for debugging and bblock splitting */
880
881         /* used mostly by the backend to store additional info it may need */
882         union {
883                 gint32 reg3;
884                 gint32 arg_info;
885                 gint32 size;
886                 MonoMemcpyArgs *memcpy_args; /* in OP_MEMSET and OP_MEMCPY */
887                 gpointer data;
888                 gint shift_amount;
889                 gboolean is_pinvoke; /* for variables in the unmanaged marshal format */
890                 gboolean record_cast_details; /* For CEE_CASTCLASS */
891                 MonoInst *spill_var; /* for OP_MOVE_I4_TO_F/F_TO_I4 and OP_FCONV_TO_R8_X */
892                 guint16 source_opcode; /*OP_XCONV_R8_TO_I4 needs to know which op was used to do proper widening*/
893                 int pc_offset; /* OP_GC_LIVERANGE_START/END */
894
895                 /*
896                  * memory_barrier: MONO_MEMORY_BARRIER_{ACQ,REL,SEQ}
897                  * atomic_load_*: MONO_MEMORY_BARRIER_{ACQ,SEQ}
898                  * atomic_store_*: MONO_MEMORY_BARRIER_{REL,SEQ}
899                  */
900                 int memory_barrier_kind;
901         } backend;
902         
903         MonoClass *klass;
904 };
905         
906 struct MonoCallInst {
907         MonoInst inst;
908         MonoMethodSignature *signature;
909         MonoMethod *method;
910         MonoInst **args;
911         MonoInst *out_args;
912         MonoInst *vret_var;
913         gconstpointer fptr;
914         guint stack_usage;
915         guint stack_align_amount;
916         guint is_virtual : 1;
917         guint tail_call : 1;
918         /* If this is TRUE, 'fptr' points to a MonoJumpInfo instead of an address. */
919         guint fptr_is_patch : 1;
920         /*
921          * If this is true, then the call returns a vtype in a register using the same 
922          * calling convention as OP_CALL.
923          */
924         guint vret_in_reg : 1;
925         /* Whenever vret_in_reg returns fp values */
926         guint vret_in_reg_fp : 1;
927         /* Whenever there is an IMT argument and it is dynamic */
928         guint dynamic_imt_arg : 1;
929         /* Whenever there is an RGCTX argument */
930         guint32 rgctx_reg : 1;
931         /* Whenever the call will need an unbox trampoline */
932         guint need_unbox_trampoline : 1;
933         regmask_t used_iregs;
934         regmask_t used_fregs;
935         GSList *out_ireg_args;
936         GSList *out_freg_args;
937         GSList *outarg_vts;
938         gpointer call_info;
939 #ifdef ENABLE_LLVM
940         LLVMCallInfo *cinfo;
941         int rgctx_arg_reg, imt_arg_reg;
942 #endif
943 #ifdef TARGET_ARM
944         /* See the comment in mini-arm.c!mono_arch_emit_call for RegTypeFP. */
945         GSList *float_args;
946 #endif
947 };
948
949 struct MonoCallArgParm {
950         MonoInst ins;
951         gint32 size;
952         gint32 offset;
953         gint32 offPrm;
954 };
955
956 /* 
957  * flags for MonoInst
958  * Note: some of the values overlap, because they can't appear
959  * in the same MonoInst.
960  */
961 enum {
962         MONO_INST_HAS_METHOD = 1,
963         MONO_INST_INIT       = 1, /* in localloc */
964         MONO_INST_SINGLE_STEP_LOC = 1, /* in SEQ_POINT */
965         MONO_INST_IS_DEAD    = 2,
966         MONO_INST_TAILCALL   = 4,
967         MONO_INST_VOLATILE   = 4,
968         MONO_INST_NOTYPECHECK    = 4,
969         MONO_INST_NONEMPTY_STACK = 4, /* in SEQ_POINT */
970         MONO_INST_UNALIGNED  = 8,
971     MONO_INST_CFOLD_TAKEN = 8, /* On branches */
972     MONO_INST_CFOLD_NOT_TAKEN = 16, /* On branches */
973         MONO_INST_DEFINITION_HAS_SIDE_EFFECTS = 8,
974         /* the address of the variable has been taken */
975         MONO_INST_INDIRECT   = 16,
976         MONO_INST_NORANGECHECK   = 16,
977         /* On loads, the source address can be null */
978         MONO_INST_FAULT = 32,
979         /* 
980          * On variables, identifies LMF variables. These variables have a dummy type (int), but
981          * require stack space for a MonoLMF struct.
982          */
983         MONO_INST_LMF = 32,
984         /* On loads, the source address points to a constant value */
985         MONO_INST_INVARIANT_LOAD = 64,
986         /* On variables, the variable needs GC tracking */
987         MONO_INST_GC_TRACK = 128,
988         /*
989          * Set on instructions during code emission which make calls, i.e. OP_CALL, OP_THROW.
990          * backend.pc_offset will be set to the pc offset at the end of the native call instructions.
991          */
992         MONO_INST_GC_CALLSITE = 128,
993         /* On comparisons, mark the branch following the condition as likely to be taken */
994         MONO_INST_LIKELY = 128,
995 };
996
997 #define inst_c0 data.op[0].const_val
998 #define inst_c1 data.op[1].const_val
999 #define inst_i0 data.op[0].src
1000 #define inst_i1 data.op[1].src
1001 #if (SIZEOF_REGISTER > SIZEOF_VOID_P) && (G_BYTE_ORDER == G_BIG_ENDIAN)
1002 #define inst_p0 data.op[0].pdata.p[SIZEOF_REGISTER/SIZEOF_VOID_P - 1]
1003 #define inst_p1 data.op[1].pdata.p[SIZEOF_REGISTER/SIZEOF_VOID_P - 1]
1004 #else
1005 #define inst_p0 data.op[0].p
1006 #define inst_p1 data.op[1].p
1007 #endif
1008 #define inst_l  data.i8const
1009 #define inst_r  data.r8const
1010 #define inst_left  data.op[0].src
1011 #define inst_right data.op[1].src
1012
1013 #define inst_newa_len   data.op[0].src
1014 #define inst_newa_class data.op[1].klass
1015
1016 #define inst_var    data.op[0].var
1017 #define inst_vtype  data.op[1].vtype
1018 /* in branch instructions */
1019 #define inst_many_bb   data.op[1].many_blocks
1020 #define inst_target_bb data.op[0].target_block
1021 #define inst_true_bb   data.op[1].many_blocks[0]
1022 #define inst_false_bb  data.op[1].many_blocks[1]
1023
1024 #define inst_basereg sreg1
1025 #define inst_indexreg sreg2
1026 #define inst_destbasereg dreg
1027 #define inst_offset data.op[0].const_val
1028 #define inst_imm    data.op[1].const_val
1029 #define inst_call   data.op[1].call_inst
1030
1031 #define inst_phi_args   data.op[1].phi_args
1032 #define inst_eh_block    data.op[1].exception_clause
1033
1034 static inline void
1035 mono_inst_set_src_registers (MonoInst *ins, int *regs)
1036 {
1037         ins->sreg1 = regs [0];
1038         ins->sreg2 = regs [1];
1039         ins->sreg3 = regs [2];
1040 }
1041
1042 /* instruction description for use in regalloc/scheduling */
1043 enum {
1044         MONO_INST_DEST,
1045         MONO_INST_SRC1,         /* we depend on the SRCs to be consecutive */
1046         MONO_INST_SRC2,
1047         MONO_INST_SRC3,
1048         MONO_INST_LEN,
1049         MONO_INST_CLOB,
1050         /* Unused, commented out to reduce the size of the mdesc tables
1051         MONO_INST_FLAGS,
1052         MONO_INST_COST,
1053         MONO_INST_DELAY,
1054         MONO_INST_RES,
1055         */
1056         MONO_INST_MAX
1057 };
1058
1059 typedef union {
1060         struct {
1061                 guint16 tid; /* tree number */
1062                 guint16 bid; /* block number */
1063         } pos ;
1064         guint32 abs_pos; 
1065 } MonoPosition;
1066
1067 typedef struct {
1068         MonoPosition first_use, last_use;
1069 } MonoLiveRange;
1070
1071 typedef struct MonoLiveRange2 MonoLiveRange2;
1072
1073 struct MonoLiveRange2 {
1074         int from, to;
1075         MonoLiveRange2 *next;
1076 };
1077
1078 typedef struct {
1079         /* List of live ranges sorted by 'from' */
1080         MonoLiveRange2 *range;
1081         MonoLiveRange2 *last_range;
1082 } MonoLiveInterval;
1083
1084 /*
1085  * Additional information about a variable
1086  */
1087 struct MonoMethodVar {
1088         guint           idx; /* inside cfg->varinfo, cfg->vars */
1089         MonoLiveRange   range; /* generated by liveness analysis */
1090         MonoLiveInterval *interval; /* generated by liveness analysis */
1091         int             reg; /* != -1 if allocated into a register */
1092         int             spill_costs;
1093         MonoBitSet     *def_in; /* used by SSA */
1094         MonoInst       *def;    /* used by SSA */
1095         MonoBasicBlock *def_bb; /* used by SSA */
1096         GList          *uses;   /* used by SSA */
1097         char            cpstate;  /* used by SSA conditional  constant propagation */
1098         /* The native offsets corresponding to the live range of the variable */
1099         gint32         live_range_start, live_range_end;
1100         /* 
1101          * cfg->varinfo [idx]->dreg could be replaced for OP_REGVAR, this contains the 
1102          * original vreg.
1103          */
1104         gint32         vreg;
1105 };
1106
1107 /*
1108  * Stores state need to resume exception handling when using LLVM
1109  */
1110 typedef struct {
1111         MonoJitInfo *ji;
1112         int clause_index;
1113         MonoContext ctx, new_ctx;
1114         /* FIXME: GC */
1115         gpointer        ex_obj;
1116         MonoLMF *lmf;
1117         int first_filter_idx, filter_idx;
1118 } ResumeState;
1119
1120 typedef struct {
1121         gpointer          end_of_stack;
1122         guint32           stack_size;
1123         /* !defined(HAVE_KW_THREAD) || !defined(MONO_ARCH_ENABLE_MONO_LMF_VAR) */
1124         MonoLMF          *lmf;
1125         MonoLMF          *first_lmf;
1126         gpointer         restore_stack_prot;
1127         guint32          handling_stack_ovf;
1128         gpointer         signal_stack;
1129         guint32          signal_stack_size;
1130         gpointer         stack_ovf_guard_base;
1131         guint32          stack_ovf_guard_size;
1132         guint            stack_ovf_valloced : 1;
1133         void            (*abort_func) (MonoObject *object);
1134         /* Used to implement --debug=casts */
1135         MonoClass       *class_cast_from, *class_cast_to;
1136
1137         /* Stores state needed by handler block with a guard */
1138         MonoContext     ex_ctx;
1139         ResumeState resume_state;
1140
1141         /*Variabled use to implement handler blocks (finally/catch/etc) guards during interruption*/
1142         /* handler block return address */
1143         gpointer handler_block_return_address;
1144
1145         /* handler block been guarded. It's safe to store this even for dynamic methods since there
1146         is an activation on stack making sure it will remain alive.*/
1147         MonoJitExceptionInfo *handler_block;
1148
1149         /* context to be used by the guard trampoline when resuming interruption.*/
1150         MonoContext handler_block_context;
1151         /* 
1152          * Stores the state at the exception throw site to be used by mono_stack_walk ()
1153          * when it is called from profiler functions during exception handling.
1154          */
1155         MonoContext orig_ex_ctx;
1156         gboolean orig_ex_ctx_set;
1157
1158         /* 
1159          * Stores if we need to run a chained exception in Windows.
1160          */
1161         gboolean mono_win_chained_exception_needs_run;
1162
1163         /* 
1164          * The current exception in flight
1165          */
1166         guint32 thrown_exc;
1167
1168         /*
1169          * The calling assembly in llvmonly mode.
1170          */
1171         MonoImage *calling_image;
1172 } MonoJitTlsData;
1173
1174 /*
1175  * This structure is an extension of MonoLMF and contains extra information.
1176  */
1177 typedef struct {
1178         struct MonoLMF lmf;
1179         gboolean debugger_invoke;
1180         MonoContext ctx; /* if debugger_invoke is TRUE */
1181 } MonoLMFExt;
1182
1183 /* Generic sharing */
1184
1185 /*
1186  * Flags for which contexts were used in inflating a generic.
1187  */
1188 enum {
1189         MONO_GENERIC_CONTEXT_USED_CLASS = 1,
1190         MONO_GENERIC_CONTEXT_USED_METHOD = 2
1191 };
1192
1193 enum {
1194         /* Cannot be 0 since this is stored in rgctx slots, and 0 means an unitialized rgctx slot */
1195         MONO_GSHAREDVT_BOX_TYPE_VTYPE = 1,
1196         MONO_GSHAREDVT_BOX_TYPE_REF = 2,
1197         MONO_GSHAREDVT_BOX_TYPE_NULLABLE = 3
1198 };
1199
1200 typedef enum {
1201         MONO_RGCTX_INFO_STATIC_DATA,
1202         MONO_RGCTX_INFO_KLASS,
1203         MONO_RGCTX_INFO_ELEMENT_KLASS,
1204         MONO_RGCTX_INFO_VTABLE,
1205         MONO_RGCTX_INFO_TYPE,
1206         MONO_RGCTX_INFO_REFLECTION_TYPE,
1207         MONO_RGCTX_INFO_METHOD,
1208         /* In llvmonly mode, this is a function descriptor */
1209         MONO_RGCTX_INFO_GENERIC_METHOD_CODE,
1210         MONO_RGCTX_INFO_GSHAREDVT_OUT_WRAPPER,
1211         MONO_RGCTX_INFO_CLASS_FIELD,
1212         MONO_RGCTX_INFO_METHOD_RGCTX,
1213         MONO_RGCTX_INFO_METHOD_CONTEXT,
1214         MONO_RGCTX_INFO_REMOTING_INVOKE_WITH_CHECK,
1215         MONO_RGCTX_INFO_METHOD_DELEGATE_CODE,
1216         MONO_RGCTX_INFO_CAST_CACHE,
1217         MONO_RGCTX_INFO_ARRAY_ELEMENT_SIZE,
1218         MONO_RGCTX_INFO_VALUE_SIZE,
1219         /* +1 to avoid zero values in rgctx slots */
1220         MONO_RGCTX_INFO_FIELD_OFFSET,
1221         /* Either the code for a gsharedvt method, or the address for a gsharedvt-out trampoline for the method */
1222         /* In llvmonly mode, this is a function descriptor */
1223         MONO_RGCTX_INFO_METHOD_GSHAREDVT_OUT_TRAMPOLINE,
1224         /* Same for virtual calls */
1225         /* In llvmonly mode, this is a function descriptor */
1226         MONO_RGCTX_INFO_METHOD_GSHAREDVT_OUT_TRAMPOLINE_VIRT,
1227         /* Same for calli, associated with a signature */
1228         MONO_RGCTX_INFO_SIG_GSHAREDVT_OUT_TRAMPOLINE_CALLI,
1229         MONO_RGCTX_INFO_SIG_GSHAREDVT_IN_TRAMPOLINE_CALLI,
1230         /* One of MONO_GSHAREDVT_BOX_TYPE */
1231         MONO_RGCTX_INFO_CLASS_BOX_TYPE,
1232         /* Resolves to a MonoGSharedVtMethodRuntimeInfo */
1233         MONO_RGCTX_INFO_METHOD_GSHAREDVT_INFO,
1234         MONO_RGCTX_INFO_LOCAL_OFFSET,
1235         MONO_RGCTX_INFO_MEMCPY,
1236         MONO_RGCTX_INFO_BZERO,
1237         /* The address of Nullable<T>.Box () */
1238         /* In llvmonly mode, this is a function descriptor */
1239         MONO_RGCTX_INFO_NULLABLE_CLASS_BOX,
1240         MONO_RGCTX_INFO_NULLABLE_CLASS_UNBOX,
1241         /* MONO_PATCH_INFO_VCALL_METHOD */
1242         MONO_RGCTX_INFO_VIRT_METHOD_CODE,
1243         /*
1244          * MONO_PATCH_INFO_VCALL_METHOD
1245          * Same as MONO_RGCTX_INFO_CLASS_BOX_TYPE, but for the class
1246          * which implements the method.
1247          */
1248         MONO_RGCTX_INFO_VIRT_METHOD_BOX_TYPE,
1249 } MonoRgctxInfoType;
1250
1251 typedef struct _MonoRuntimeGenericContextInfoTemplate {
1252         MonoRgctxInfoType info_type;
1253         gpointer data;
1254         struct _MonoRuntimeGenericContextInfoTemplate *next;
1255 } MonoRuntimeGenericContextInfoTemplate;
1256
1257 typedef struct {
1258         MonoClass *next_subclass;
1259         MonoRuntimeGenericContextInfoTemplate *infos;
1260         GSList *method_templates;
1261 } MonoRuntimeGenericContextTemplate;
1262
1263 typedef struct {
1264         MonoVTable *class_vtable; /* must be the first element */
1265         MonoGenericInst *method_inst;
1266         gpointer infos [MONO_ZERO_LEN_ARRAY];
1267 } MonoMethodRuntimeGenericContext;
1268
1269 #define MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT (sizeof (MonoMethodRuntimeGenericContext) - MONO_ZERO_LEN_ARRAY * SIZEOF_VOID_P)
1270
1271 #define MONO_RGCTX_SLOT_MAKE_RGCTX(i)   (i)
1272 #define MONO_RGCTX_SLOT_MAKE_MRGCTX(i)  ((i) | 0x80000000)
1273 #define MONO_RGCTX_SLOT_INDEX(s)        ((s) & 0x7fffffff)
1274 #define MONO_RGCTX_SLOT_IS_MRGCTX(s)    (((s) & 0x80000000) ? TRUE : FALSE)
1275
1276 #define MONO_GSHAREDVT_DEL_INVOKE_VT_OFFSET -2
1277
1278 typedef struct {
1279         MonoMethod *method;
1280         MonoRuntimeGenericContextInfoTemplate *entries;
1281         int num_entries, count_entries;
1282 } MonoGSharedVtMethodInfo;
1283
1284 /* This is used by gsharedvt methods to allocate locals and compute local offsets */
1285 typedef struct {
1286         int locals_size;
1287         /*
1288          * The results of resolving the entries in MOonGSharedVtMethodInfo->entries.
1289          * We use this instead of rgctx slots since these can be loaded using a load instead
1290          * of a call to an rgctx fetch trampoline.
1291          */
1292         gpointer entries [MONO_ZERO_LEN_ARRAY];
1293 } MonoGSharedVtMethodRuntimeInfo;
1294
1295 typedef struct
1296 {
1297         MonoMethod *invoke;
1298         MonoMethod *method;
1299         MonoMethodSignature *invoke_sig;
1300         MonoMethodSignature *sig;
1301         gpointer method_ptr;
1302         gpointer invoke_impl;
1303         gpointer impl_this;
1304         gpointer impl_nothis;
1305         gboolean need_rgctx_tramp;
1306 } MonoDelegateTrampInfo;
1307
1308 /*
1309  * A function descriptor, which is a function address + argument pair.
1310  * In llvm-only mode, these are used instead of trampolines to pass
1311  * extra arguments to runtime functions/methods.
1312  */
1313 typedef struct
1314 {
1315         gpointer addr;
1316         gpointer arg;
1317 } MonoFtnDesc;
1318
1319 typedef enum {
1320 #define PATCH_INFO(a,b) MONO_PATCH_INFO_ ## a,
1321 #include "patch-info.h"
1322 #undef PATCH_INFO
1323         MONO_PATCH_INFO_NUM
1324 } MonoJumpInfoType;
1325
1326 /*
1327  * We need to store the image which the token refers to along with the token,
1328  * since the image might not be the same as the image of the method which
1329  * contains the relocation, because of inlining.
1330  */
1331 typedef struct MonoJumpInfoToken {
1332         MonoImage *image;
1333         guint32 token;
1334         gboolean has_context;
1335         MonoGenericContext context;
1336 } MonoJumpInfoToken;
1337
1338 typedef struct MonoJumpInfoBBTable {
1339         MonoBasicBlock **table;
1340         int table_size;
1341 } MonoJumpInfoBBTable;
1342
1343 typedef struct MonoJumpInfoRgctxEntry MonoJumpInfoRgctxEntry;
1344
1345 /* Contains information describing an LLVM IMT trampoline */
1346 typedef struct MonoJumpInfoImtTramp {
1347         MonoMethod *method;
1348         int vt_offset;
1349 } MonoJumpInfoImtTramp;
1350
1351 typedef struct MonoJumpInfoGSharedVtCall MonoJumpInfoGSharedVtCall;
1352
1353 /*
1354  * Represents the method which is called when a virtual call is made to METHOD
1355  * on a receiver of type KLASS.
1356  */
1357 typedef struct {
1358         /* Receiver class */
1359         MonoClass *klass;
1360         /* Virtual method */
1361         MonoMethod *method;
1362 } MonoJumpInfoVirtMethod;
1363
1364 typedef struct MonoJumpInfo MonoJumpInfo;
1365 struct MonoJumpInfo {
1366         MonoJumpInfo *next;
1367         /* Relocation type for patching */
1368         int relocation;
1369         union {
1370                 int i;
1371                 guint8 *p;
1372                 MonoInst *label;
1373         } ip;
1374
1375         MonoJumpInfoType type;
1376         union {
1377                 gconstpointer   target;
1378 #if SIZEOF_VOID_P == 8
1379                 gint64          offset;
1380 #else
1381                 int             offset;
1382 #endif
1383                 int index;
1384                 MonoBasicBlock *bb;
1385                 MonoInst       *inst;
1386                 MonoMethod     *method;
1387                 MonoClass      *klass;
1388                 MonoClassField *field;
1389                 MonoImage      *image;
1390                 MonoVTable     *vtable;
1391                 const char     *name;
1392                 MonoJumpInfoToken  *token;
1393                 MonoJumpInfoBBTable *table;
1394                 MonoJumpInfoRgctxEntry *rgctx_entry;
1395                 MonoJumpInfoImtTramp *imt_tramp;
1396                 MonoJumpInfoGSharedVtCall *gsharedvt;
1397                 MonoGSharedVtMethodInfo *gsharedvt_method;
1398                 MonoMethodSignature *sig;
1399                 MonoDelegateClassMethodPair *del_tramp;
1400                 /* MONO_PATCH_INFO_VIRT_METHOD */
1401                 MonoJumpInfoVirtMethod *virt_method;
1402         } data;
1403 };
1404  
1405 /*
1406  * Contains information for computing the
1407  * property given by INFO_TYPE of the runtime
1408  * object described by DATA.
1409  */
1410 struct MonoJumpInfoRgctxEntry {
1411         MonoMethod *method;
1412         gboolean in_mrgctx;
1413         MonoJumpInfo *data; /* describes the data to be loaded */
1414         MonoRgctxInfoType info_type;
1415 };
1416
1417 /* Contains information about a gsharedvt call */
1418 struct MonoJumpInfoGSharedVtCall {
1419         /* The original signature of the call */
1420         MonoMethodSignature *sig;
1421         /* The method which is called */
1422         MonoMethod *method;
1423 };
1424
1425 typedef enum {
1426         MONO_TRAMPOLINE_JIT,
1427         MONO_TRAMPOLINE_JUMP,
1428         MONO_TRAMPOLINE_RGCTX_LAZY_FETCH,
1429         MONO_TRAMPOLINE_AOT,
1430         MONO_TRAMPOLINE_AOT_PLT,
1431         MONO_TRAMPOLINE_DELEGATE,
1432         MONO_TRAMPOLINE_RESTORE_STACK_PROT,
1433         MONO_TRAMPOLINE_GENERIC_VIRTUAL_REMOTING,
1434         MONO_TRAMPOLINE_VCALL,
1435         MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD,
1436         MONO_TRAMPOLINE_NUM
1437 } MonoTrampolineType;
1438
1439 /* These trampolines return normally to their caller */
1440 #define MONO_TRAMPOLINE_TYPE_MUST_RETURN(t)             \
1441         ((t) == MONO_TRAMPOLINE_RESTORE_STACK_PROT ||   \
1442          (t) == MONO_TRAMPOLINE_RGCTX_LAZY_FETCH)
1443
1444 /* These trampolines receive an argument directly in a register */
1445 #define MONO_TRAMPOLINE_TYPE_HAS_ARG(t)         \
1446         ((t) == MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD)
1447
1448 /* optimization flags */
1449 #define OPTFLAG(id,shift,name,descr) MONO_OPT_ ## id = 1 << shift,
1450 enum {
1451 #include "optflags-def.h"
1452         MONO_OPT_LAST
1453 };
1454
1455 /*
1456  * This structure represents a JIT backend.
1457  */
1458 typedef struct {
1459         guint            have_card_table_wb : 1;
1460         guint            have_op_generic_class_init : 1;
1461         guint            emulate_mul_div : 1;
1462         guint            emulate_div : 1;
1463         guint            emulate_long_shift_opts : 1;
1464         guint            have_objc_get_selector : 1;
1465         guint            have_generalized_imt_thunk : 1;
1466         guint            have_tls_get : 1;
1467         guint            have_tls_get_reg : 1;
1468         guint            have_liverange_ops: 1;
1469         guint            have_op_tail_call : 1;
1470         guint            have_dummy_init : 1;
1471         guint            gshared_supported : 1;
1472         guint            use_fpstack : 1;
1473         guint            ilp32 : 1;
1474         guint            need_got_var : 1;
1475         guint            need_div_check : 1;
1476         guint            no_unaligned_access : 1;
1477         int              monitor_enter_adjustment;
1478         int              dyn_call_param_area;
1479 } MonoBackend;
1480
1481 /* Flags for mini_method_compile () */
1482 typedef enum {
1483         /* Whenever to run cctors during JITting */
1484         JIT_FLAG_RUN_CCTORS = (1 << 0),
1485         /* Whenever this is an AOT compilation */
1486         JIT_FLAG_AOT = (1 << 1),
1487         /* Whenever this is a full AOT compilation */
1488         JIT_FLAG_FULL_AOT = (1 << 2),
1489         /* Whenever to compile with LLVM */
1490         JIT_FLAG_LLVM = (1 << 3),
1491         /* Whenever to disable direct calls to direct calls to icall functions */
1492         JIT_FLAG_NO_DIRECT_ICALLS = (1 << 4),
1493         /* Emit explicit null checks */
1494         JIT_FLAG_EXPLICIT_NULL_CHECKS = (1 << 5),
1495         /* Whenever to compile in llvm-only mode */
1496         JIT_FLAG_LLVM_ONLY = (1 << 6),
1497 } JitFlags;
1498
1499 /* Bit-fields in the MonoBasicBlock.region */
1500 #define MONO_REGION_TRY       0
1501 #define MONO_REGION_FINALLY  16
1502 #define MONO_REGION_CATCH    32
1503 #define MONO_REGION_FAULT    64         /* Currently unused */
1504 #define MONO_REGION_FILTER  128
1505
1506 #define MONO_BBLOCK_IS_IN_REGION(bblock, regtype) (((bblock)->region & (0xf << 4)) == (regtype))
1507
1508 #define MONO_REGION_FLAGS(region) ((region) & 0x7)
1509 #define MONO_REGION_CLAUSE_INDEX(region) (((region) >> 8) - 1)
1510
1511 #define get_vreg_to_inst(cfg, vreg) ((vreg) < (cfg)->vreg_to_inst_len ? (cfg)->vreg_to_inst [(vreg)] : NULL)
1512
1513 #define vreg_is_volatile(cfg, vreg) (G_UNLIKELY (get_vreg_to_inst ((cfg), (vreg)) && (get_vreg_to_inst ((cfg), (vreg))->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))))
1514
1515 #define vreg_is_ref(cfg, vreg) ((vreg) < (cfg)->vreg_is_ref_len ? (cfg)->vreg_is_ref [(vreg)] : 0)
1516 #define vreg_is_mp(cfg, vreg) ((vreg) < (cfg)->vreg_is_mp_len ? (cfg)->vreg_is_mp [(vreg)] : 0)
1517
1518 /*
1519  * Control Flow Graph and compilation unit information
1520  */
1521 typedef struct {
1522         MonoMethod      *method;
1523         MonoMethodHeader *header;
1524         MonoMemPool     *mempool;
1525         MonoInst       **varinfo;
1526         MonoMethodVar   *vars;
1527         MonoInst        *ret;
1528         MonoBasicBlock  *bb_entry;
1529         MonoBasicBlock  *bb_exit;
1530         MonoBasicBlock  *bb_init;
1531         MonoBasicBlock **bblocks;
1532         MonoBasicBlock **cil_offset_to_bb;
1533         MonoMemPool     *state_pool; /* used by instruction selection */
1534         MonoBasicBlock  *cbb;        /* used by instruction selection */
1535         MonoInst        *prev_ins;   /* in decompose */
1536         MonoJumpInfo    *patch_info;
1537         MonoJitInfo     *jit_info;
1538         MonoJitDynamicMethodInfo *dynamic_info;
1539         guint            num_bblocks, max_block_num;
1540         guint            locals_start;
1541         guint            num_varinfo; /* used items in varinfo */
1542         guint            varinfo_count; /* total storage in varinfo */
1543         gint             stack_offset;
1544         gint             max_ireg;
1545         gint             cil_offset_to_bb_len;
1546         MonoRegState    *rs;
1547         MonoSpillInfo   *spill_info [16]; /* machine register spills */
1548         gint             spill_count;
1549         gint             spill_info_len [16];
1550         /* unsigned char   *cil_code; */
1551         MonoMethod      *inlined_method; /* the method which is currently inlined */
1552         MonoInst        *domainvar; /* a cache for the current domain */
1553         MonoInst        *got_var; /* Global Offset Table variable */
1554         MonoInst        **locals;
1555         MonoInst        *rgctx_var; /* Runtime generic context variable (for static generic methods) */
1556         MonoInst        **args;
1557         MonoType        **arg_types;
1558         MonoMethod      *current_method; /* The method currently processed by method_to_ir () */
1559         MonoMethod      *method_to_register; /* The method to register in JIT info tables */
1560         MonoGenericContext *generic_context;
1561
1562         MonoBackend *backend;
1563
1564         /* 
1565          * This variable represents the hidden argument holding the vtype
1566          * return address. If the method returns something other than a vtype, or
1567          * the vtype is returned in registers this is NULL.
1568          */
1569         MonoInst        *vret_addr;
1570
1571         /*
1572          * This is used to initialize the cil_code field of MonoInst's.
1573          */
1574         const unsigned char *ip;
1575         
1576         struct MonoAliasingInformation *aliasing_info;
1577
1578         /* A hashtable of region ID-> SP var mappings */
1579         /* An SP var is a place to store the stack pointer (used by handlers)*/
1580         GHashTable      *spvars;
1581
1582         /* A hashtable of region ID -> EX var mappings */
1583         /* An EX var stores the exception object passed to catch/filter blocks */
1584         GHashTable      *exvars;
1585
1586         GList           *ldstr_list; /* used by AOT */
1587         
1588         MonoDomain      *domain;
1589
1590         guint            real_offset;
1591         GHashTable      *cbb_hash;
1592
1593         /* The current virtual register number */
1594         guint32 next_vreg;
1595
1596         MonoGenericSharingContext gsctx;
1597         MonoGenericContext *gsctx_context;
1598
1599         MonoGSharedVtMethodInfo *gsharedvt_info;
1600
1601         /* Points to the gsharedvt locals area at runtime */
1602         MonoInst *gsharedvt_locals_var;
1603
1604         /* The localloc instruction used to initialize gsharedvt_locals_var */
1605         MonoInst *gsharedvt_locals_var_ins;
1606
1607         /* Points to a MonoGSharedVtMethodRuntimeInfo at runtime */
1608         MonoInst *gsharedvt_info_var;
1609
1610         /* For native-to-managed wrappers, CEE_MONO_JIT_(AT|DE)TACH opcodes */
1611         MonoInst *orig_domain_var;
1612
1613         MonoInst *lmf_var;
1614         MonoInst *lmf_addr_var;
1615
1616         MonoInst *stack_inbalance_var;
1617
1618         unsigned char   *cil_start;
1619 #ifdef __native_client_codegen__
1620         /* this alloc is not aligned, native_code */
1621         /* is the 32-byte aligned version of this */
1622         unsigned char   *native_code_alloc;
1623 #endif
1624         unsigned char   *native_code;
1625         guint            code_size;
1626         guint            code_len;
1627         guint            prolog_end;
1628         guint            epilog_begin;
1629         guint            epilog_end;
1630         regmask_t        used_int_regs;
1631         guint32          opt;
1632         guint32          prof_options;
1633         guint32          flags;
1634         guint32          comp_done;
1635         guint32          verbose_level;
1636         guint32          stack_usage;
1637         guint32          param_area;
1638         guint32          frame_reg;
1639         gint32           sig_cookie;
1640         guint            disable_aot : 1;
1641         guint            disable_ssa : 1;
1642         guint            disable_llvm : 1;
1643         guint            enable_extended_bblocks : 1;
1644         guint            run_cctors : 1;
1645         guint            need_lmf_area : 1;
1646         guint            compile_aot : 1;
1647         guint            full_aot : 1;
1648         guint            compile_llvm : 1;
1649         guint            got_var_allocated : 1;
1650         guint            ret_var_is_local : 1;
1651         guint            ret_var_set : 1;
1652         guint            unverifiable : 1;
1653         guint            skip_visibility : 1;
1654         guint            disable_reuse_registers : 1;
1655         guint            disable_reuse_stack_slots : 1;
1656         guint            disable_reuse_ref_stack_slots : 1;
1657         guint            disable_ref_noref_stack_slot_share : 1;
1658         guint            disable_initlocals_opt : 1;
1659         guint            disable_initlocals_opt_refs : 1;
1660         guint            disable_omit_fp : 1;
1661         guint            disable_vreg_to_lvreg : 1;
1662         guint            disable_deadce_vars : 1;
1663         guint            disable_out_of_line_bblocks : 1;
1664         guint            disable_direct_icalls : 1;
1665         guint            disable_gc_safe_points : 1;
1666         guint            create_lmf_var : 1;
1667         /*
1668          * When this is set, the code to push/pop the LMF from the LMF stack is generated as IR
1669          * instead of being generated in emit_prolog ()/emit_epilog ().
1670          */
1671         guint            lmf_ir : 1;
1672         /*
1673          * Whenever to use the mono_lmf TLS variable instead of indirection through the
1674          * mono_lmf_addr TLS variable.
1675          */
1676         guint            lmf_ir_mono_lmf : 1;
1677         guint            gen_write_barriers : 1;
1678         guint            init_ref_vars : 1;
1679         guint            extend_live_ranges : 1;
1680         guint            compute_precise_live_ranges : 1;
1681         guint            has_got_slots : 1;
1682         guint            uses_rgctx_reg : 1;
1683         guint            uses_vtable_reg : 1;
1684         guint            uses_simd_intrinsics : 1;
1685         guint            keep_cil_nops : 1;
1686         guint            gen_seq_points : 1;
1687         /* Generate seq points for use by the debugger */
1688         guint            gen_sdb_seq_points : 1;
1689         guint            explicit_null_checks : 1;
1690         guint            compute_gc_maps : 1;
1691         guint            soft_breakpoints : 1;
1692         guint            arch_eh_jit_info : 1;
1693         guint            has_emulated_ops : 1;
1694         guint            has_indirection : 1;
1695         guint            has_atomic_add_i4 : 1;
1696         guint            has_atomic_exchange_i4 : 1;
1697         guint            has_atomic_cas_i4 : 1;
1698         guint            check_pinvoke_callconv : 1;
1699         guint            has_unwind_info_for_epilog : 1;
1700         guint            disable_inline : 1;
1701         /* Disable inlining into caller */
1702         guint            no_inline : 1;
1703         guint            gshared : 1;
1704         guint            gsharedvt : 1;
1705         guint            r4fp : 1;
1706         guint            llvm_only : 1;
1707         int              r4_stack_type;
1708         gpointer         debug_info;
1709         guint32          lmf_offset;
1710     guint16          *intvars;
1711         MonoProfileCoverageInfo *coverage_info;
1712         GHashTable       *token_info_hash;
1713         MonoCompileArch  arch;
1714         guint32          inline_depth;
1715         /* Size of memory reserved for thunks */
1716         int              thunk_area;
1717         /* Thunks */
1718         guint8          *thunks;
1719         /* Offset between the start of code and the thunks area */
1720         int              thunks_offset;
1721         guint32          exception_type;        /* MONO_EXCEPTION_* */
1722         guint32          exception_data;
1723         char*            exception_message;
1724         gpointer         exception_ptr;
1725
1726         guint8 *         encoded_unwind_ops;
1727         guint32          encoded_unwind_ops_len;
1728         GSList*          unwind_ops;
1729
1730         GList*           dont_inline;
1731
1732         /* Fields used by the local reg allocator */
1733         void*            reginfo;
1734         int              reginfo_len;
1735
1736         /* Maps vregs to their associated MonoInst's */
1737         /* vregs with an associated MonoInst are 'global' while others are 'local' */
1738         MonoInst **vreg_to_inst;
1739
1740         /* Size of above array */
1741         guint32 vreg_to_inst_len;
1742
1743         /* Marks vregs which hold a GC ref */
1744         /* FIXME: Use a bitmap */
1745         gboolean *vreg_is_ref;
1746
1747         /* Size of above array */
1748         guint32 vreg_is_ref_len;
1749
1750         /* Marks vregs which hold a managed pointer */
1751         /* FIXME: Use a bitmap */
1752         gboolean *vreg_is_mp;
1753
1754         /* Size of above array */
1755         guint32 vreg_is_mp_len;
1756
1757         /* 
1758          * The original method to compile, differs from 'method' when doing generic
1759          * sharing.
1760          */
1761         MonoMethod *orig_method;
1762
1763         /* Patches which describe absolute addresses embedded into the native code */
1764         GHashTable *abs_patches;
1765
1766         /* If the arch passes valuetypes by address, then for methods
1767            which use JMP the arch code should use these local
1768            variables to store the addresses of incoming valuetypes.
1769            The addresses should be stored in mono_arch_emit_prolog()
1770            and can be used when emitting code for OP_JMP.  See
1771            mini-ppc.c. */
1772         MonoInst **tailcall_valuetype_addrs;
1773
1774         /* Used to implement move_i4_to_f on archs that can't do raw
1775         copy between an ireg and a freg. This is an int32 var.*/
1776         MonoInst *iconv_raw_var;
1777
1778         /* Used to implement fconv_to_r8_x. This is a double (8 bytes) var.*/
1779         MonoInst *fconv_to_r8_x_var;
1780
1781         /*Use to implement simd constructors. This is a vector (16 bytes) var.*/
1782         MonoInst *simd_ctor_var;
1783
1784         /* Used to implement dyn_call */
1785         MonoInst *dyn_call_var;
1786
1787         /*
1788          * List of sequence points represented as IL offset+native offset pairs.
1789          * Allocated using glib.
1790          * IL offset can be -1 or 0xffffff to refer to the sequence points
1791          * inside the prolog and epilog used to implement method entry/exit events.
1792          */
1793         GPtrArray *seq_points;
1794
1795         /* The encoded sequence point info */
1796         struct MonoSeqPointInfo *seq_point_info;
1797
1798         /* Method headers which need to be freed after compilation */
1799         GSList *headers_to_free;
1800
1801         /* Used by AOT */
1802         guint32 got_offset, ex_info_offset, method_info_offset, method_index;
1803         /* Symbol used to refer to this method in generated assembly */
1804         char *asm_symbol;
1805         char *asm_debug_symbol;
1806         char *llvm_method_name;
1807         int castclass_cache_index;
1808
1809         MonoJitExceptionInfo *llvm_ex_info;
1810         guint32 llvm_ex_info_len;
1811         int llvm_this_reg, llvm_this_offset;
1812
1813         GSList *try_block_holes;
1814
1815         /* DWARF location list for 'this' */
1816         GSList *this_loclist;
1817
1818         /* DWARF location list for 'rgctx_var' */
1819         GSList *rgctx_loclist;
1820
1821         int *gsharedvt_vreg_to_idx;
1822
1823         GSList *signatures;
1824
1825         /* GC Maps */
1826    
1827         /* The offsets of the locals area relative to the frame pointer */
1828         gint locals_min_stack_offset, locals_max_stack_offset;
1829
1830         /* The current CFA rule */
1831         int cur_cfa_reg, cur_cfa_offset;
1832
1833         /* The final CFA rule at the end of the prolog */
1834         int cfa_reg, cfa_offset;
1835
1836         /* Points to a MonoCompileGC */
1837         gpointer gc_info;
1838
1839         /*
1840          * The encoded GC map along with its size. This contains binary data so it can be saved in an AOT
1841          * image etc, but it requires a 4 byte alignment.
1842          */
1843         guint8 *gc_map;
1844         guint32 gc_map_size;
1845
1846         /* Error handling */
1847         MonoError error;
1848
1849         /* pointer to context datastructure used for graph dumping */
1850         MonoGraphDumper *gdump_ctx;
1851
1852         /* Stats */
1853         int stat_allocate_var;
1854         int stat_locals_stack_size;
1855         int stat_basic_blocks;
1856         int stat_cil_code_size;
1857         int stat_n_regvars;
1858         int stat_inlineable_methods;
1859         int stat_inlined_methods;
1860         int stat_code_reallocs;
1861 } MonoCompile;
1862
1863 typedef enum {
1864         MONO_CFG_HAS_ALLOCA = 1 << 0,
1865         MONO_CFG_HAS_CALLS  = 1 << 1,
1866         MONO_CFG_HAS_LDELEMA  = 1 << 2,
1867         MONO_CFG_HAS_VARARGS  = 1 << 3,
1868         MONO_CFG_HAS_TAIL     = 1 << 4,
1869         MONO_CFG_HAS_FPOUT    = 1 << 5, /* there are fp values passed in int registers */
1870         MONO_CFG_HAS_SPILLUP  = 1 << 6, /* spill var slots are allocated from bottom to top */
1871         MONO_CFG_HAS_CHECK_THIS  = 1 << 7,
1872         MONO_CFG_HAS_ARRAY_ACCESS = 1 << 8,
1873         MONO_CFG_HAS_TYPE_CHECK = 1 << 9
1874 } MonoCompileFlags;
1875
1876 typedef struct {
1877         gint32 methods_compiled;
1878         gint32 methods_aot;
1879         gint32 methods_lookups;
1880         gint32 allocate_var;
1881         gint32 cil_code_size;
1882         gint32 native_code_size;
1883         gint32 code_reallocs;
1884         gint32 max_code_size_ratio;
1885         gint32 biggest_method_size;
1886         gint32 allocated_code_size;
1887         gint32 allocated_seq_points_size;
1888         gint32 inlineable_methods;
1889         gint32 inlined_methods;
1890         gint32 basic_blocks;
1891         gint32 max_basic_blocks;
1892         gint32 locals_stack_size;
1893         gint32 regvars;
1894         gint32 generic_virtual_invocations;
1895         gint32 alias_found;
1896         gint32 alias_removed;
1897         gint32 loads_eliminated;
1898         gint32 stores_eliminated;
1899         int methods_with_llvm;
1900         int methods_without_llvm;
1901         char *max_ratio_method;
1902         char *biggest_method;
1903         double jit_method_to_ir;
1904         double jit_liveness_handle_exception_clauses;
1905         double jit_handle_out_of_line_bblock;
1906         double jit_decompose_long_opts;
1907         double jit_decompose_typechecks;
1908         double jit_local_cprop;
1909         double jit_local_emulate_ops;
1910         double jit_optimize_branches;
1911         double jit_handle_global_vregs;
1912         double jit_local_deadce;
1913         double jit_local_alias_analysis;
1914         double jit_if_conversion;
1915         double jit_bb_ordering;
1916         double jit_compile_dominator_info;
1917         double jit_compute_natural_loops;
1918         double jit_insert_safepoints;
1919         double jit_ssa_compute;
1920         double jit_ssa_cprop;
1921         double jit_ssa_deadce;
1922         double jit_perform_abc_removal;
1923         double jit_ssa_remove;
1924         double jit_local_cprop2;
1925         double jit_handle_global_vregs2;
1926         double jit_local_deadce2;
1927         double jit_optimize_branches2;
1928         double jit_decompose_vtype_opts;
1929         double jit_decompose_array_access_opts;
1930         double jit_liveness_handle_exception_clauses2;
1931         double jit_analyze_liveness;
1932         double jit_linear_scan;
1933         double jit_arch_allocate_vars;
1934         double jit_spill_global_vars;
1935         double jit_local_cprop3;
1936         double jit_local_deadce3;
1937         double jit_codegen;
1938         double jit_create_jit_info;
1939         double jit_gc_create_gc_map;
1940         double jit_save_seq_point_info;
1941         double jit_time;
1942         gboolean enabled;
1943 } MonoJitStats;
1944
1945 extern MonoJitStats mono_jit_stats;
1946
1947 /* opcodes: value assigned after all the CIL opcodes */
1948 #ifdef MINI_OP
1949 #undef MINI_OP
1950 #endif
1951 #ifdef MINI_OP3
1952 #undef MINI_OP3
1953 #endif
1954 #define MINI_OP(a,b,dest,src1,src2) a,
1955 #define MINI_OP3(a,b,dest,src1,src2,src3) a,
1956 enum {
1957         OP_START = MONO_CEE_LAST - 1,
1958 #include "mini-ops.h"
1959         OP_LAST
1960 };
1961 #undef MINI_OP
1962 #undef MINI_OP3
1963
1964 #if SIZEOF_VOID_P == 8
1965 #define OP_PCONST OP_I8CONST
1966 #define OP_DUMMY_PCONST OP_DUMMY_I8CONST
1967 #define OP_PADD OP_LADD
1968 #define OP_PADD_IMM OP_LADD_IMM
1969 #define OP_PSUB_IMM OP_LSUB_IMM
1970 #define OP_PAND_IMM OP_LAND_IMM
1971 #define OP_PXOR_IMM OP_LXOR_IMM
1972 #define OP_PSUB OP_LSUB
1973 #define OP_PMUL OP_LMUL
1974 #define OP_PMUL_IMM OP_LMUL_IMM
1975 #define OP_PNEG OP_LNEG
1976 #define OP_PCONV_TO_I1 OP_LCONV_TO_I1
1977 #define OP_PCONV_TO_U1 OP_LCONV_TO_U1
1978 #define OP_PCONV_TO_I2 OP_LCONV_TO_I2
1979 #define OP_PCONV_TO_U2 OP_LCONV_TO_U2
1980 #define OP_PCONV_TO_OVF_I1_UN OP_LCONV_TO_OVF_I1_UN
1981 #define OP_PCONV_TO_OVF_I1 OP_LCONV_TO_OVF_I1
1982 #define OP_PBEQ OP_LBEQ
1983 #define OP_PCEQ OP_LCEQ
1984 #define OP_PBNE_UN OP_LBNE_UN
1985 #define OP_PBGE_UN OP_LBGE_UN
1986 #define OP_PBLT_UN OP_LBLT_UN
1987 #define OP_PBGE OP_LBGE
1988 #define OP_STOREP_MEMBASE_REG OP_STOREI8_MEMBASE_REG
1989 #define OP_STOREP_MEMBASE_IMM OP_STOREI8_MEMBASE_IMM
1990 #else
1991 #define OP_PCONST OP_ICONST
1992 #define OP_DUMMY_PCONST OP_DUMMY_ICONST
1993 #define OP_PADD OP_IADD
1994 #define OP_PADD_IMM OP_IADD_IMM
1995 #define OP_PSUB_IMM OP_ISUB_IMM
1996 #define OP_PAND_IMM OP_IAND_IMM
1997 #define OP_PXOR_IMM OP_IXOR_IMM
1998 #define OP_PSUB OP_ISUB
1999 #define OP_PMUL OP_IMUL
2000 #define OP_PMUL_IMM OP_IMUL_IMM
2001 #define OP_PNEG OP_INEG
2002 #define OP_PCONV_TO_I1 OP_ICONV_TO_I1
2003 #define OP_PCONV_TO_U1 OP_ICONV_TO_U1
2004 #define OP_PCONV_TO_I2 OP_ICONV_TO_I2
2005 #define OP_PCONV_TO_U2 OP_ICONV_TO_U2
2006 #define OP_PCONV_TO_OVF_I1_UN OP_ICONV_TO_OVF_I1_UN
2007 #define OP_PCONV_TO_OVF_I1 OP_ICONV_TO_OVF_I1
2008 #define OP_PBEQ OP_IBEQ
2009 #define OP_PCEQ OP_ICEQ
2010 #define OP_PBNE_UN OP_IBNE_UN
2011 #define OP_PBGE_UN OP_IBGE_UN
2012 #define OP_PBLT_UN OP_IBLT_UN
2013 #define OP_PBGE OP_IBGE
2014 #define OP_STOREP_MEMBASE_REG OP_STOREI4_MEMBASE_REG
2015 #define OP_STOREP_MEMBASE_IMM OP_STOREI4_MEMBASE_IMM
2016 #endif
2017
2018 /* Opcodes to load/store regsize quantities */
2019 #if defined (__mono_ilp32__)
2020 #define OP_LOADR_MEMBASE OP_LOADI8_MEMBASE
2021 #define OP_STORER_MEMBASE_REG OP_STOREI8_MEMBASE_REG
2022 #else
2023 #define OP_LOADR_MEMBASE OP_LOAD_MEMBASE
2024 #define OP_STORER_MEMBASE_REG OP_STORE_MEMBASE_REG
2025 #endif
2026
2027 typedef enum {
2028         STACK_INV,
2029         STACK_I4,
2030         STACK_I8,
2031         STACK_PTR,
2032         STACK_R8,
2033         STACK_MP,
2034         STACK_OBJ,
2035         STACK_VTYPE,
2036         STACK_R4,
2037         STACK_MAX
2038 } MonoStackType;
2039
2040 typedef struct {
2041         union {
2042                 double   r8;
2043                 gint32   i4;
2044                 gint64   i8;
2045                 gpointer p;
2046                 MonoClass *klass;
2047         } data;
2048         int type;
2049 } StackSlot;
2050
2051 #if HAVE_ARRAY_ELEM_INIT
2052 extern const guint8 mono_burg_arity [];
2053 #else
2054 extern guint8 mono_burg_arity [];
2055 #endif
2056
2057 extern const char MONO_ARCH_CPU_SPEC [];
2058 #define MONO_ARCH_CPU_SPEC_IDX_COMBINE(a) a ## _idx
2059 #define MONO_ARCH_CPU_SPEC_IDX(a) MONO_ARCH_CPU_SPEC_IDX_COMBINE(a)
2060 extern const guint16 MONO_ARCH_CPU_SPEC_IDX(MONO_ARCH_CPU_SPEC) [];
2061 #define ins_get_spec(op) ((const char*)&MONO_ARCH_CPU_SPEC + MONO_ARCH_CPU_SPEC_IDX(MONO_ARCH_CPU_SPEC)[(op) - OP_LOAD])
2062
2063 enum {
2064         MONO_COMP_DOM = 1,
2065         MONO_COMP_IDOM = 2,
2066         MONO_COMP_DFRONTIER = 4,
2067         MONO_COMP_DOM_REV = 8,
2068         MONO_COMP_LIVENESS = 16,
2069         MONO_COMP_SSA = 32,
2070         MONO_COMP_SSA_DEF_USE = 64,
2071         MONO_COMP_REACHABILITY = 128,
2072         MONO_COMP_LOOPS = 256
2073 };
2074
2075 typedef enum {
2076         MONO_GRAPH_CFG = 1,
2077         MONO_GRAPH_DTREE = 2,
2078         MONO_GRAPH_CFG_CODE = 4,
2079         MONO_GRAPH_CFG_SSA = 8,
2080         MONO_GRAPH_CFG_OPTCODE = 16
2081 } MonoGraphOptions;
2082
2083 typedef struct {
2084         guint16 size;
2085         guint16 offset;
2086         guint8  pad;
2087 } MonoJitArgumentInfo;
2088
2089 typedef struct {
2090         gboolean handle_sigint;
2091         gboolean keep_delegates;
2092         gboolean reverse_pinvoke_exceptions;
2093         gboolean collect_pagefault_stats;
2094         gboolean break_on_unverified;
2095         gboolean better_cast_details;
2096         gboolean mdb_optimizations;
2097         gboolean no_gdb_backtrace;
2098         gboolean suspend_on_sigsegv;
2099         gboolean suspend_on_exception;
2100         gboolean suspend_on_unhandled;
2101         gboolean dyn_runtime_invoke;
2102         gboolean gdb;
2103         gboolean arm_use_fallback_tls;
2104         /*
2105          * Whenever data such as next sequence points and flags is required.
2106          * Next sequence points and flags are required by the debugger agent.
2107          */
2108         gboolean gen_sdb_seq_points;
2109         gboolean gen_seq_points_compact_data;
2110         /*
2111          * Setting single_imm_size should guarantee that each time managed code is compiled
2112          * the same instructions and registers are used, regardless of the size of used values.
2113          */
2114         gboolean single_imm_size;
2115         gboolean explicit_null_checks;
2116         /*
2117          * Fill stack frames with 0x2a in method prologs. This helps with the
2118          * debugging of the stack marking code in the GC.
2119          */
2120         gboolean init_stacks;
2121
2122         /*
2123          * Whenever to implement single stepping and breakpoints without signals in the
2124          * soft debugger. This is useful on platforms without signals, like the ps3, or during
2125          * runtime debugging, since it avoids SIGSEGVs when a single step location or breakpoint
2126          * is hit.
2127          */
2128         gboolean soft_breakpoints;
2129         /*
2130          * Whenever to break in the debugger using G_BREAKPOINT on unhandled exceptions.
2131          */
2132         gboolean break_on_exc;
2133         /*
2134          * Load AOT JIT info eagerly.
2135          */
2136         gboolean load_aot_jit_info_eagerly;
2137         /*
2138          * Check for pinvoke calling convention mismatches.
2139          */
2140         gboolean check_pinvoke_callconv;
2141         /*
2142          * Translate Debugger.Break () into a native breakpoint signal
2143          */
2144         gboolean native_debugger_break;
2145         /*
2146          * Disabling the frame pointer emit optimization can allow debuggers to more easily
2147          * identify the stack on some platforms
2148          */
2149         gboolean disable_omit_fp;
2150 } MonoDebugOptions;
2151
2152 enum {
2153         BRANCH_NOT_TAKEN,
2154         BRANCH_TAKEN,
2155         BRANCH_UNDEF
2156 };
2157
2158 typedef enum {
2159         CMP_EQ,
2160         CMP_NE,
2161         CMP_LE,
2162         CMP_GE,
2163         CMP_LT,
2164         CMP_GT,
2165         CMP_LE_UN,
2166         CMP_GE_UN,
2167         CMP_LT_UN,
2168         CMP_GT_UN
2169 } CompRelation;
2170
2171 typedef enum {
2172         CMP_TYPE_L,
2173         CMP_TYPE_I,
2174         CMP_TYPE_F
2175 } CompType;
2176
2177 /* Implicit exceptions */
2178 enum {
2179         MONO_EXC_INDEX_OUT_OF_RANGE,
2180         MONO_EXC_OVERFLOW,
2181         MONO_EXC_ARITHMETIC,
2182         MONO_EXC_DIVIDE_BY_ZERO,
2183         MONO_EXC_INVALID_CAST,
2184         MONO_EXC_NULL_REF,
2185         MONO_EXC_ARRAY_TYPE_MISMATCH,
2186         MONO_EXC_ARGUMENT,
2187         MONO_EXC_INTRINS_NUM
2188 };
2189
2190 enum {
2191         MINI_TOKEN_SOURCE_CLASS,
2192         MINI_TOKEN_SOURCE_METHOD,
2193         MINI_TOKEN_SOURCE_FIELD
2194 };
2195
2196  /* 
2197   * Information about a trampoline function.
2198   */
2199  typedef struct
2200  {
2201         /* 
2202          * The native code of the trampoline. Not owned by this structure.
2203          */
2204         guint8 *code;
2205         guint32 code_size;
2206         /*
2207          * The name of the trampoline which can be used in AOT/xdebug. Owned by this
2208          * structure.
2209          */
2210         char *name;
2211         /* 
2212          * Patches required by the trampoline when aot-ing. Owned by this structure.
2213          */
2214         MonoJumpInfo *ji;
2215         /*
2216          * Unwind information. Owned by this structure.
2217          */
2218         GSList *unwind_ops;
2219
2220          /*
2221           * Encoded unwind info loaded from AOT images
2222           */
2223          guint8 *uw_info;
2224          guint32 uw_info_len;
2225 } MonoTrampInfo;
2226
2227 typedef void (*MonoInstFunc) (MonoInst *tree, gpointer data);
2228
2229 enum {
2230         FILTER_IL_SEQ_POINT = 1 << 0,
2231         FILTER_NOP          = 1 << 1,
2232 };
2233
2234 static inline gboolean
2235 mono_inst_filter (MonoInst *ins, int filter)
2236 {
2237         if (!ins || !filter)
2238                 return FALSE;
2239
2240         if ((filter & FILTER_IL_SEQ_POINT) && ins->opcode == OP_IL_SEQ_POINT)
2241                 return TRUE;
2242
2243         if ((filter & FILTER_NOP) && ins->opcode == OP_NOP)
2244                 return TRUE;
2245
2246         return FALSE;
2247 }
2248
2249 static inline MonoInst*
2250 mono_inst_next (MonoInst *ins, int filter)
2251 {
2252         do {
2253                 ins = ins->next;
2254         } while (mono_inst_filter (ins, filter));
2255
2256         return ins;
2257 }
2258
2259 static inline MonoInst*
2260 mono_inst_prev (MonoInst *ins, int filter)
2261 {
2262         do {
2263                 ins = ins->prev;
2264         } while (mono_inst_filter (ins, filter));
2265
2266         return ins;
2267 }
2268
2269 static inline MonoInst*
2270 mono_bb_first_inst (MonoBasicBlock *bb, int filter)
2271 {
2272         MonoInst *ins = bb->code;
2273         if (mono_inst_filter (ins, filter))
2274                 ins = mono_inst_next (ins, filter);
2275
2276         return ins;
2277 }
2278
2279 static inline MonoInst*
2280 mono_bb_last_inst (MonoBasicBlock *bb, int filter)
2281 {
2282         MonoInst *ins = bb->last_ins;
2283         if (mono_inst_filter (ins, filter))
2284                 ins = mono_inst_prev (ins, filter);
2285
2286         return ins;
2287 }
2288
2289 /* main function */
2290 MONO_API int         mono_main                      (int argc, char* argv[]);
2291 MONO_API void        mono_set_defaults              (int verbose_level, guint32 opts);
2292 MONO_API void        mono_parse_env_options         (int *ref_argc, char **ref_argv []);
2293 MONO_API char       *mono_parse_options_from        (const char *options, int *ref_argc, char **ref_argv []);
2294
2295 MonoDomain* mini_init                      (const char *filename, const char *runtime_version);
2296 void        mini_cleanup                   (MonoDomain *domain);
2297 MONO_API MonoDebugOptions *mini_get_debug_options   (void);
2298 MONO_API gboolean    mini_parse_debug_option (const char *option);
2299
2300 /* graph dumping */
2301 void mono_cfg_dump_create_context (MonoCompile *cfg);
2302 void mono_cfg_dump_begin_group (MonoCompile *cfg);
2303 void mono_cfg_dump_close_group (MonoCompile *cfg);
2304 void mono_cfg_dump_ir (MonoCompile *cfg, const char *phase_name);
2305
2306 /* helper methods */
2307 void      mini_jit_init                    (void);
2308 void      mini_jit_cleanup                 (void);
2309 void      mono_disable_optimizations       (guint32 opts);
2310 void      mono_set_optimizations           (guint32 opts);
2311 void      mono_set_bisect_methods          (guint32 opt, const char *method_list_filename);
2312 guint32   mono_get_optimizations_for_method (MonoMethod *method, guint32 default_opt);
2313 char*     mono_opt_descr                   (guint32 flags);
2314 void      mono_set_verbose_level           (guint32 level);
2315 MonoJumpInfoToken* mono_jump_info_token_new (MonoMemPool *mp, MonoImage *image, guint32 token);
2316 MonoJumpInfoToken* mono_jump_info_token_new2 (MonoMemPool *mp, MonoImage *image, guint32 token, MonoGenericContext *context);
2317 MonoInst* mono_find_spvar_for_region        (MonoCompile *cfg, int region);
2318 MonoInst* mono_find_exvar_for_offset        (MonoCompile *cfg, int offset);
2319 int       mono_get_block_region_notry       (MonoCompile *cfg, int region) MONO_LLVM_INTERNAL;
2320
2321 void      mono_precompile_assemblies        (void);
2322 MONO_API int       mono_parse_default_optimizations  (const char* p);
2323 void      mono_bblock_add_inst              (MonoBasicBlock *bb, MonoInst *inst) MONO_LLVM_INTERNAL;
2324 void      mono_bblock_insert_after_ins      (MonoBasicBlock *bb, MonoInst *ins, MonoInst *ins_to_insert);
2325 void      mono_bblock_insert_before_ins     (MonoBasicBlock *bb, MonoInst *ins, MonoInst *ins_to_insert);
2326 void      mono_verify_bblock                (MonoBasicBlock *bb);
2327 void      mono_verify_cfg                   (MonoCompile *cfg);
2328 void      mono_constant_fold                (MonoCompile *cfg);
2329 MonoInst* mono_constant_fold_ins            (MonoCompile *cfg, MonoInst *ins, MonoInst *arg1, MonoInst *arg2, gboolean overwrite);
2330 int       mono_eval_cond_branch             (MonoInst *branch);
2331 int       mono_is_power_of_two              (guint32 val) MONO_LLVM_INTERNAL;
2332 void      mono_cprop_local                  (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **acp, int acp_size);
2333 MonoInst* mono_compile_create_var           (MonoCompile *cfg, MonoType *type, int opcode);
2334 MonoInst* mono_compile_create_var_for_vreg  (MonoCompile *cfg, MonoType *type, int opcode, int vreg);
2335 void      mono_compile_make_var_load        (MonoCompile *cfg, MonoInst *dest, gssize var_index);
2336 MonoInst* mini_get_int_to_float_spill_area  (MonoCompile *cfg);
2337 MonoType* mono_type_from_stack_type         (MonoInst *ins);
2338 guint32   mono_alloc_ireg                   (MonoCompile *cfg) MONO_LLVM_INTERNAL;
2339 guint32   mono_alloc_lreg                   (MonoCompile *cfg) MONO_LLVM_INTERNAL;
2340 guint32   mono_alloc_freg                   (MonoCompile *cfg) MONO_LLVM_INTERNAL;
2341 guint32   mono_alloc_preg                   (MonoCompile *cfg) MONO_LLVM_INTERNAL;
2342 guint32   mono_alloc_dreg                   (MonoCompile *cfg, MonoStackType stack_type);
2343 guint32   mono_alloc_ireg_ref               (MonoCompile *cfg) MONO_LLVM_INTERNAL;
2344 guint32   mono_alloc_ireg_mp                (MonoCompile *cfg) MONO_LLVM_INTERNAL;
2345 guint32   mono_alloc_ireg_copy              (MonoCompile *cfg, guint32 vreg) MONO_LLVM_INTERNAL;
2346 void      mono_mark_vreg_as_ref             (MonoCompile *cfg, int vreg);
2347 void      mono_mark_vreg_as_mp              (MonoCompile *cfg, int vreg);
2348
2349 void      mono_link_bblock                  (MonoCompile *cfg, MonoBasicBlock *from, MonoBasicBlock* to);
2350 void      mono_unlink_bblock                (MonoCompile *cfg, MonoBasicBlock *from, MonoBasicBlock* to);
2351 gboolean  mono_bblocks_linked               (MonoBasicBlock *bb1, MonoBasicBlock *bb2);
2352 void      mono_remove_bblock                (MonoCompile *cfg, MonoBasicBlock *bb);
2353 void      mono_nullify_basic_block          (MonoBasicBlock *bb);
2354 void      mono_merge_basic_blocks           (MonoCompile *cfg, MonoBasicBlock *bb, MonoBasicBlock *bbn);
2355 void      mono_optimize_branches            (MonoCompile *cfg);
2356
2357 void      mono_blockset_print               (MonoCompile *cfg, MonoBitSet *set, const char *name, guint idom);
2358 const char*mono_ji_type_to_string           (MonoJumpInfoType type);
2359 void      mono_print_ji                     (const MonoJumpInfo *ji);
2360 void      mono_print_ins_index              (int i, MonoInst *ins);
2361 GString  *mono_print_ins_index_strbuf       (int i, MonoInst *ins);
2362 void      mono_print_ins                    (MonoInst *ins);
2363 void      mono_print_bb                     (MonoBasicBlock *bb, const char *msg);
2364 void      mono_print_code                   (MonoCompile *cfg, const char *msg);
2365 MONO_API void      mono_print_method_from_ip         (void *ip);
2366 MONO_API char     *mono_pmip                         (void *ip);
2367 gboolean  mono_debug_count                  (void);
2368 MONO_API const char* mono_inst_name                  (int op);
2369 int       mono_op_to_op_imm                 (int opcode);
2370 int       mono_op_imm_to_op                 (int opcode);
2371 int       mono_load_membase_to_load_mem     (int opcode);
2372 guint     mono_type_to_load_membase         (MonoCompile *cfg, MonoType *type);
2373 guint     mono_type_to_store_membase        (MonoCompile *cfg, MonoType *type);
2374 guint     mini_type_to_stind                (MonoCompile* cfg, MonoType *type);
2375 MonoJitInfo* mini_lookup_method             (MonoDomain *domain, MonoMethod *method, MonoMethod *shared);
2376 guint32   mono_reverse_branch_op            (guint32 opcode);
2377 void      mono_disassemble_code             (MonoCompile *cfg, guint8 *code, int size, char *id);
2378 void      mono_add_patch_info               (MonoCompile *cfg, int ip, MonoJumpInfoType type, gconstpointer target) MONO_LLVM_INTERNAL;
2379 void      mono_add_patch_info_rel           (MonoCompile *cfg, int ip, MonoJumpInfoType type, gconstpointer target, int relocation) MONO_LLVM_INTERNAL;
2380 void      mono_remove_patch_info            (MonoCompile *cfg, int ip);
2381 MonoJumpInfo* mono_patch_info_dup_mp        (MonoMemPool *mp, MonoJumpInfo *patch_info);
2382 guint     mono_patch_info_hash (gconstpointer data);
2383 gint      mono_patch_info_equal (gconstpointer ka, gconstpointer kb);
2384 MonoJumpInfo *mono_patch_info_list_prepend  (MonoJumpInfo *list, int ip, MonoJumpInfoType type, gconstpointer target);
2385 gpointer  mono_resolve_patch_target         (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *patch_info, gboolean run_cctors, MonoError *error) MONO_LLVM_INTERNAL;
2386 gpointer  mono_jit_find_compiled_method_with_jit_info (MonoDomain *domain, MonoMethod *method, MonoJitInfo **ji);
2387 gpointer  mono_jit_find_compiled_method     (MonoDomain *domain, MonoMethod *method);
2388 gpointer  mono_jit_compile_method           (MonoMethod *method, MonoError *error);
2389 gpointer  mono_jit_compile_method_inner     (MonoMethod *method, MonoDomain *target_domain, int opt, MonoError *error);
2390 MonoLMF * mono_get_lmf                      (void);
2391 MonoLMF** mono_get_lmf_addr                 (void);
2392 void      mono_set_lmf                      (MonoLMF *lmf);
2393 MonoJitTlsData* mono_get_jit_tls            (void);
2394 MONO_API MonoDomain* mono_jit_thread_attach (MonoDomain *domain);
2395 MONO_API void      mono_jit_set_domain      (MonoDomain *domain);
2396 gint32    mono_get_jit_tls_offset           (void);
2397 gint32    mono_get_lmf_tls_offset           (void);
2398 gint32    mono_get_lmf_addr_tls_offset      (void);
2399 int       mini_get_tls_offset               (MonoTlsKey key);
2400 gboolean  mini_tls_get_supported            (MonoCompile *cfg, MonoTlsKey key);
2401 MonoInst* mono_create_tls_get               (MonoCompile *cfg, MonoTlsKey key);
2402 MonoInst* mono_get_jit_tls_intrinsic        (MonoCompile *cfg);
2403 MonoInst* mono_get_domain_intrinsic         (MonoCompile* cfg);
2404 MonoInst* mono_get_thread_intrinsic         (MonoCompile* cfg);
2405 MonoInst* mono_get_lmf_intrinsic            (MonoCompile* cfg);
2406 MonoInst* mono_get_lmf_addr_intrinsic       (MonoCompile* cfg);
2407 GList    *mono_varlist_insert_sorted        (MonoCompile *cfg, GList *list, MonoMethodVar *mv, int sort_type);
2408 GList    *mono_varlist_sort                 (MonoCompile *cfg, GList *list, int sort_type);
2409 void      mono_analyze_liveness             (MonoCompile *cfg);
2410 void      mono_analyze_liveness_gc          (MonoCompile *cfg);
2411 void      mono_linear_scan                  (MonoCompile *cfg, GList *vars, GList *regs, regmask_t *used_mask);
2412 void      mono_global_regalloc              (MonoCompile *cfg);
2413 void      mono_create_jump_table            (MonoCompile *cfg, MonoInst *label, MonoBasicBlock **bbs, int num_blocks);
2414 MonoCompile *mini_method_compile            (MonoMethod *method, guint32 opts, MonoDomain *domain, JitFlags flags, int parts, int aot_method_index);
2415 void      mono_destroy_compile              (MonoCompile *cfg);
2416 MonoJitICallInfo *mono_find_jit_opcode_emulation (int opcode);
2417 void      mono_print_ins_index (int i, MonoInst *ins);
2418 void      mono_print_ins (MonoInst *ins);
2419 gboolean  mini_assembly_can_skip_verification (MonoDomain *domain, MonoMethod *method);
2420 gboolean mono_compile_is_broken (MonoCompile *cfg, MonoMethod *method, gboolean fail_compile);
2421 MonoInst *mono_get_got_var (MonoCompile *cfg);
2422 void      mono_add_seq_point (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *ins, int native_offset);
2423 void      mono_add_var_location (MonoCompile *cfg, MonoInst *var, gboolean is_reg, int reg, int offset, int from, int to);
2424 MonoInst* mono_emit_jit_icall (MonoCompile *cfg, gconstpointer func, MonoInst **args);
2425 MonoInst* mono_emit_jit_icall_by_info (MonoCompile *cfg, int il_offset, MonoJitICallInfo *info, MonoInst **args);
2426 MonoInst* mono_emit_method_call (MonoCompile *cfg, MonoMethod *method, MonoInst **args, MonoInst *this_ins);
2427 void      mono_create_helper_signatures (void);
2428
2429 gboolean  mini_class_is_system_array (MonoClass *klass);
2430 MonoMethodSignature *mono_get_element_address_signature (int arity);
2431 MonoJitICallInfo    *mono_get_element_address_icall (int rank);
2432 MonoJitICallInfo    *mono_get_array_new_va_icall (int rank);
2433
2434 void      mono_linterval_add_range          (MonoCompile *cfg, MonoLiveInterval *interval, int from, int to);
2435 void      mono_linterval_print              (MonoLiveInterval *interval);
2436 void      mono_linterval_print_nl (MonoLiveInterval *interval);
2437 gboolean  mono_linterval_covers             (MonoLiveInterval *interval, int pos);
2438 gint32    mono_linterval_get_intersect_pos  (MonoLiveInterval *i1, MonoLiveInterval *i2);
2439 void      mono_linterval_split              (MonoCompile *cfg, MonoLiveInterval *interval, MonoLiveInterval **i1, MonoLiveInterval **i2, int pos);
2440 void      mono_liveness_handle_exception_clauses (MonoCompile *cfg);
2441
2442 /* Native Client functions */
2443 gpointer mono_realloc_native_code(MonoCompile *cfg);
2444
2445 #if defined(__native_client__) || defined(__native_client_codegen__)
2446 extern volatile int __nacl_thread_suspension_needed;
2447 void __nacl_suspend_thread_if_needed(void);
2448 void mono_nacl_gc(void);
2449 #endif
2450
2451 extern MonoDebugOptions debug_options;
2452
2453 static inline MonoMethod*
2454 jinfo_get_method (MonoJitInfo *ji)
2455 {
2456         return mono_jit_info_get_method (ji);
2457 }
2458
2459 /* AOT */
2460 void      mono_aot_init                     (void);
2461 void      mono_aot_cleanup                  (void);
2462 gpointer  mono_aot_get_method_checked       (MonoDomain *domain,
2463                                                                                          MonoMethod *method, MonoError *error);
2464 gpointer  mono_aot_get_method_from_token    (MonoDomain *domain, MonoImage *image, guint32 token, MonoError *error);
2465 gboolean  mono_aot_is_got_entry             (guint8 *code, guint8 *addr);
2466 guint8*   mono_aot_get_plt_entry            (guint8 *code);
2467 guint32   mono_aot_get_plt_info_offset      (mgreg_t *regs, guint8 *code);
2468 gboolean  mono_aot_get_cached_class_info    (MonoClass *klass, MonoCachedClassInfo *res);
2469 gboolean  mono_aot_get_class_from_name      (MonoImage *image, const char *name_space, const char *name, MonoClass **klass);
2470 MonoJitInfo* mono_aot_find_jit_info         (MonoDomain *domain, MonoImage *image, gpointer addr);
2471 gpointer mono_aot_plt_resolve               (gpointer aot_module, guint32 plt_info_offset, guint8 *code, MonoError *error);
2472 void     mono_aot_patch_plt_entry           (guint8 *code, guint8 *plt_entry, gpointer *got, mgreg_t *regs, guint8 *addr);
2473 gpointer mono_aot_get_method_from_vt_slot   (MonoDomain *domain, MonoVTable *vtable, int slot, MonoError *error);
2474 gpointer mono_aot_create_specific_trampoline   (MonoImage *image, gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len);
2475 gpointer mono_aot_get_trampoline            (const char *name);
2476 gpointer mono_aot_get_trampoline_full       (const char *name, MonoTrampInfo **out_tinfo);
2477 gpointer mono_aot_get_unbox_trampoline      (MonoMethod *method);
2478 gpointer mono_aot_get_lazy_fetch_trampoline (guint32 slot);
2479 gpointer mono_aot_get_static_rgctx_trampoline (gpointer ctx, gpointer addr);
2480 gpointer mono_aot_get_imt_thunk             (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckItem **imt_entries, int count, gpointer fail_tramp);
2481 gpointer mono_aot_get_gsharedvt_arg_trampoline(gpointer arg, gpointer addr);
2482 guint8*  mono_aot_get_unwind_info           (MonoJitInfo *ji, guint32 *unwind_info_len);
2483 guint32  mono_aot_method_hash               (MonoMethod *method);
2484 MonoMethod* mono_aot_get_array_helper_from_wrapper (MonoMethod *method);
2485 void     mono_aot_set_make_unreadable       (gboolean unreadable);
2486 gboolean mono_aot_is_pagefault              (void *ptr);
2487 void     mono_aot_handle_pagefault          (void *ptr);
2488 void     mono_aot_register_jit_icall        (const char *name, gpointer addr);
2489 guint32  mono_aot_find_method_index         (MonoMethod *method);
2490 void     mono_aot_init_llvm_method          (gpointer aot_module, guint32 method_index);
2491 void     mono_aot_init_gshared_method_this  (gpointer aot_module, guint32 method_index, MonoObject *this_ins);
2492 void     mono_aot_init_gshared_method_mrgctx  (gpointer aot_module, guint32 method_index, MonoMethodRuntimeGenericContext *rgctx);
2493 void     mono_aot_init_gshared_method_vtable  (gpointer aot_module, guint32 method_index, MonoVTable *vtable);
2494
2495 /* This is an exported function */
2496 MONO_API void     mono_aot_register_module           (gpointer *aot_info);
2497
2498 /* These are used to load the AOT data for aot images compiled with MONO_AOT_FILE_FLAG_SEPARATE_DATA */
2499 /*
2500  * Return the AOT data for ASSEMBLY. SIZE is the size of the data. OUT_HANDLE should be set to a handle which is later
2501  * passed to the free function.
2502  */
2503 typedef unsigned char* (*MonoLoadAotDataFunc)          (MonoAssembly *assembly, int size, gpointer user_data, void **out_handle);
2504 /* Not yet used */
2505 typedef void  (*MonoFreeAotDataFunc)          (MonoAssembly *assembly, int size, gpointer user_data, void *handle);
2506 MONO_API void mono_install_load_aot_data_hook (MonoLoadAotDataFunc load_func, MonoFreeAotDataFunc free_func, gpointer user_data);
2507
2508 void     mono_xdebug_init                   (const char *xdebug_opts);
2509 void     mono_save_xdebug_info              (MonoCompile *cfg);
2510 void     mono_save_trampoline_xdebug_info   (MonoTrampInfo *info);
2511 /* This is an exported function */
2512 void     mono_xdebug_flush                  (void);
2513
2514 gboolean  mono_method_blittable             (MonoMethod *method);
2515 gboolean  mono_method_same_domain           (MonoJitInfo *caller, MonoJitInfo *callee);
2516
2517 void      mono_register_opcode_emulation    (int opcode, const char* name, const char *sigstr, gpointer func, gboolean no_throw);
2518 void      mono_draw_graph                   (MonoCompile *cfg, MonoGraphOptions draw_options);
2519 void      mono_add_ins_to_end               (MonoBasicBlock *bb, MonoInst *inst);
2520 gpointer  mono_create_ftnptr                (MonoDomain *domain, gpointer addr);
2521
2522 MONO_API void      mono_replace_ins                  (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *ins, MonoInst **prev, MonoBasicBlock *first_bb, MonoBasicBlock *last_bb);
2523
2524 int               mono_find_method_opcode      (MonoMethod *method);
2525 MonoJitICallInfo *mono_register_jit_icall      (gconstpointer func, const char *name, MonoMethodSignature *sig, gboolean is_save);
2526 gconstpointer     mono_icall_get_wrapper       (MonoJitICallInfo* callinfo) MONO_LLVM_INTERNAL;
2527 gconstpointer     mono_icall_get_wrapper_full  (MonoJitICallInfo* callinfo, gboolean do_compile);
2528 void              mini_register_opcode_emulation (int opcode, const char *name, const char *sigstr, gpointer func, const char *symbol, gboolean no_throw);
2529
2530 void              mono_trampolines_init (void);
2531 void              mono_trampolines_cleanup (void);
2532 guint8 *          mono_get_trampoline_code (MonoTrampolineType tramp_type);
2533 gpointer          mono_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len);
2534 gpointer          mono_create_jump_trampoline (MonoDomain *domain, 
2535                                                                                            MonoMethod *method, 
2536                                                                                            gboolean add_sync_wrapper,
2537                                                                                            MonoError *error);
2538 gpointer          mono_create_class_init_trampoline (MonoVTable *vtable);
2539 gpointer          mono_create_jit_trampoline (MonoDomain *domain, MonoMethod *method, MonoError *error) MONO_LLVM_INTERNAL;
2540 gpointer          mono_create_jit_trampoline_from_token (MonoImage *image, guint32 token);
2541 gpointer          mono_create_delegate_trampoline (MonoDomain *domain, MonoClass *klass);
2542 MonoDelegateTrampInfo* mono_create_delegate_trampoline_info (MonoDomain *domain, MonoClass *klass, MonoMethod *method);
2543 gpointer          mono_create_delegate_virtual_trampoline (MonoDomain *domain, MonoClass *klass, MonoMethod *method);
2544 gpointer          mono_create_rgctx_lazy_fetch_trampoline (guint32 offset);
2545 gpointer          mono_create_monitor_enter_trampoline (void);
2546 gpointer          mono_create_monitor_enter_v4_trampoline (void);
2547 gpointer          mono_create_monitor_exit_trampoline (void);
2548 gpointer          mono_create_static_rgctx_trampoline (MonoMethod *m, gpointer addr);
2549 MonoVTable*       mono_find_class_init_trampoline_by_addr (gconstpointer addr);
2550 guint32           mono_find_rgctx_lazy_fetch_trampoline_by_addr (gconstpointer addr);
2551 gpointer          mono_magic_trampoline (mgreg_t *regs, guint8 *code, gpointer arg, guint8* tramp);
2552 #ifndef DISABLE_REMOTING
2553 gpointer          mono_generic_virtual_remoting_trampoline (mgreg_t *regs, guint8 *code, MonoMethod *m, guint8 *tramp);
2554 #endif
2555 gpointer          mono_delegate_trampoline (mgreg_t *regs, guint8 *code, gpointer *tramp_data, guint8* tramp);
2556 gpointer          mono_aot_trampoline (mgreg_t *regs, guint8 *code, guint8 *token_info, 
2557                                                                            guint8* tramp);
2558 gpointer          mono_aot_plt_trampoline (mgreg_t *regs, guint8 *code, guint8 *token_info, 
2559                                                                                    guint8* tramp);
2560 void              mono_class_init_trampoline (mgreg_t *regs, guint8 *code, MonoVTable *vtable, guint8 *tramp);
2561 void              mono_generic_class_init_trampoline (mgreg_t *regs, guint8 *code, MonoVTable *vtable, guint8 *tramp);
2562 void              mono_monitor_enter_trampoline (mgreg_t *regs, guint8 *code, MonoObject *obj, guint8 *tramp);
2563 void              mono_monitor_enter_v4_trampoline (mgreg_t *regs, guint8 *code, MonoObject *obj, guint8 *tramp);
2564 void              mono_monitor_exit_trampoline (mgreg_t *regs, guint8 *code, MonoObject *obj, guint8 *tramp);
2565 gconstpointer     mono_get_trampoline_func (MonoTrampolineType tramp_type);
2566 gpointer          mini_get_vtable_trampoline (MonoVTable *vt, int slot_index);
2567 const char*       mono_get_generic_trampoline_simple_name (MonoTrampolineType tramp_type);
2568 char*             mono_get_generic_trampoline_name (MonoTrampolineType tramp_type);
2569 char*             mono_get_rgctx_fetch_trampoline_name (int slot);
2570 gpointer          mini_get_nullified_class_init_trampoline (void);
2571 gpointer          mini_get_single_step_trampoline (void);
2572 gpointer          mini_get_breakpoint_trampoline (void);
2573 gpointer          mini_add_method_trampoline (MonoMethod *m, gpointer compiled_method, gboolean add_static_rgctx_tramp, gboolean add_unbox_tramp);
2574 gpointer          mini_add_method_wrappers_llvmonly (MonoMethod *m, gpointer compiled_method, gboolean caller_gsharedvt, gboolean add_unbox_tramp, gpointer *out_arg);
2575 gboolean          mini_jit_info_is_gsharedvt (MonoJitInfo *ji);
2576 gpointer*         mini_resolve_imt_method (MonoVTable *vt, gpointer *vtable_slot, MonoMethod *imt_method, MonoMethod **impl_method, gpointer *out_aot_addr,
2577                                            gboolean *out_need_rgctx_tramp, MonoMethod **variant_iface,
2578                                            MonoError *error);
2579 MonoFtnDesc      *mini_create_llvmonly_ftndesc (MonoDomain *domain, gpointer addr, gpointer arg);
2580
2581 gboolean          mono_running_on_valgrind (void);
2582 void*             mono_global_codeman_reserve (int size);
2583 const char       *mono_regname_full (int reg, int bank);
2584 gint32*           mono_allocate_stack_slots (MonoCompile *cfg, gboolean backward, guint32 *stack_size, guint32 *stack_align);
2585 void              mono_local_regalloc (MonoCompile *cfg, MonoBasicBlock *bb);
2586 MonoInst         *mono_branch_optimize_exception_target (MonoCompile *cfg, MonoBasicBlock *bb, const char * exname);
2587 void              mono_remove_critical_edges (MonoCompile *cfg);
2588 gboolean          mono_is_regsize_var (MonoType *t);
2589 void              mini_emit_memcpy (MonoCompile *cfg, int destreg, int doffset, int srcreg, int soffset, int size, int align);
2590 void              mini_emit_stobj (MonoCompile *cfg, MonoInst *dest, MonoInst *src, MonoClass *klass, gboolean native);
2591 void              mini_emit_initobj (MonoCompile *cfg, MonoInst *dest, const guchar *ip, MonoClass *klass);
2592 CompRelation      mono_opcode_to_cond (int opcode) MONO_LLVM_INTERNAL;
2593 CompType          mono_opcode_to_type (int opcode, int cmp_opcode);
2594 CompRelation      mono_negate_cond (CompRelation cond);
2595 int               mono_op_imm_to_op (int opcode);
2596 void              mono_decompose_op_imm (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *ins);
2597 void              mono_peephole_ins (MonoBasicBlock *bb, MonoInst *ins);
2598 MonoUnwindOp     *mono_create_unwind_op (int when, 
2599                                                                                  int tag, int reg, 
2600                                                                                  int val);
2601 void              mono_emit_unwind_op (MonoCompile *cfg, int when, 
2602                                                                            int tag, int reg, 
2603                                                                            int val);
2604 MonoTrampInfo*    mono_tramp_info_create (const char *name, guint8 *code, guint32 code_size, MonoJumpInfo *ji, GSList *unwind_ops);
2605 void              mono_tramp_info_free (MonoTrampInfo *info);
2606 void              mono_tramp_info_register (MonoTrampInfo *info, MonoDomain *domain);
2607 int               mini_exception_id_by_name (const char *name);
2608 gboolean          mini_type_is_hfa (MonoType *t, int *out_nfields, int *out_esize) MONO_LLVM_INTERNAL;
2609
2610 int               mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock, 
2611                                                                          MonoInst *return_var, MonoInst **inline_args,
2612                                                                          guint inline_offset, gboolean is_virtual_call);
2613
2614 MonoInst         *mono_decompose_opcode (MonoCompile *cfg, MonoInst *ins);
2615 void              mono_decompose_long_opts (MonoCompile *cfg);
2616 void              mono_decompose_vtype_opts (MonoCompile *cfg);
2617 void              mono_decompose_array_access_opts (MonoCompile *cfg);
2618 void              mono_decompose_soft_float (MonoCompile *cfg);
2619 void              mono_local_emulate_ops (MonoCompile *cfg);
2620 void              mono_handle_global_vregs (MonoCompile *cfg);
2621 void              mono_spill_global_vars (MonoCompile *cfg, gboolean *need_local_opts);
2622 void              mono_allocate_gsharedvt_vars (MonoCompile *cfg);
2623 void              mono_if_conversion (MonoCompile *cfg);
2624
2625 /* Delegates */
2626 gpointer          mini_get_delegate_arg (MonoMethod *method, gpointer method_ptr);
2627 void              mini_init_delegate (MonoDelegate *del);
2628 gpointer          mono_get_delegate_virtual_invoke_impl  (MonoMethodSignature *sig, MonoMethod *method);
2629
2630 /* methods that must be provided by the arch-specific port */
2631 void      mono_arch_init                        (void);
2632 void      mono_arch_finish_init                 (void);
2633 void      mono_arch_cleanup                     (void);
2634 void      mono_arch_cpu_init                    (void);
2635 guint32   mono_arch_cpu_optimizations           (guint32 *exclude_mask);
2636 void      mono_arch_instrument_mem_needs        (MonoMethod *method, int *stack, int *code);
2637 void     *mono_arch_instrument_prolog           (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments);
2638 void     *mono_arch_instrument_epilog           (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments);
2639 void     *mono_arch_instrument_epilog_full     (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments, gboolean preserve_argument_registers);
2640 void      mono_codegen                          (MonoCompile *cfg);
2641 void      mono_call_inst_add_outarg_reg         (MonoCompile *cfg, MonoCallInst *call, int vreg, int hreg, int bank) MONO_LLVM_INTERNAL;
2642 void      mono_call_inst_add_outarg_vt          (MonoCompile *cfg, MonoCallInst *call, MonoInst *outarg_vt);
2643 const char *mono_arch_regname                   (int reg);
2644 const char *mono_arch_fregname                  (int reg);
2645 void      mono_arch_exceptions_init             (void);
2646 guchar*   mono_arch_create_generic_trampoline   (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot);
2647 gpointer  mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot);
2648 gpointer  mono_arch_create_general_rgctx_lazy_fetch_trampoline (MonoTrampInfo **info, gboolean aot);
2649 gpointer  mono_arch_get_nullified_class_init_trampoline (MonoTrampInfo **info);
2650 guint8*   mono_arch_create_sdb_trampoline (gboolean single_step, MonoTrampInfo **info, gboolean aot);
2651 gpointer  mono_arch_create_monitor_enter_trampoline (MonoTrampInfo **info, gboolean is_v4, gboolean aot);
2652 gpointer  mono_arch_create_monitor_exit_trampoline (MonoTrampInfo **info, gboolean aot);
2653 guint8   *mono_arch_create_llvm_native_thunk     (MonoDomain *domain, guint8* addr) MONO_LLVM_INTERNAL;
2654 GList    *mono_arch_get_allocatable_int_vars    (MonoCompile *cfg);
2655 GList    *mono_arch_get_global_int_regs         (MonoCompile *cfg);
2656 GList    *mono_arch_get_global_fp_regs          (MonoCompile *cfg);
2657 GList    *mono_arch_get_iregs_clobbered_by_call (MonoCallInst *call);
2658 GList    *mono_arch_get_fregs_clobbered_by_call (MonoCallInst *call);
2659 guint32   mono_arch_regalloc_cost               (MonoCompile *cfg, MonoMethodVar *vmv);
2660 void      mono_arch_patch_code                  (MonoCompile *cfg, MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji, gboolean run_cctors, MonoError *error);
2661 void      mono_arch_patch_code_new              (MonoCompile *cfg, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji, gpointer target);
2662 void      mono_arch_flush_icache                (guint8 *code, gint size);
2663 int       mono_arch_max_epilog_size             (MonoCompile *cfg);
2664 guint8   *mono_arch_emit_prolog                 (MonoCompile *cfg);
2665 void      mono_arch_emit_epilog                 (MonoCompile *cfg);
2666 void      mono_arch_emit_exceptions             (MonoCompile *cfg);
2667 void      mono_arch_lowering_pass               (MonoCompile *cfg, MonoBasicBlock *bb);
2668 void      mono_arch_peephole_pass_1             (MonoCompile *cfg, MonoBasicBlock *bb);
2669 void      mono_arch_peephole_pass_2             (MonoCompile *cfg, MonoBasicBlock *bb);
2670 void      mono_arch_output_basic_block          (MonoCompile *cfg, MonoBasicBlock *bb);
2671 void      mono_arch_free_jit_tls_data           (MonoJitTlsData *tls);
2672 void      mono_arch_fill_argument_info          (MonoCompile *cfg);
2673 void      mono_arch_allocate_vars               (MonoCompile *m);
2674 int       mono_arch_get_argument_info           (MonoMethodSignature *csig, int param_count, MonoJitArgumentInfo *arg_info);
2675 gboolean  mono_arch_print_tree                          (MonoInst *tree, int arity);
2676 void      mono_arch_emit_call                   (MonoCompile *cfg, MonoCallInst *call);
2677 void      mono_arch_emit_outarg_vt              (MonoCompile *cfg, MonoInst *ins, MonoInst *src);
2678 void      mono_arch_emit_setret                 (MonoCompile *cfg, MonoMethod *method, MonoInst *val);
2679 MonoDynCallInfo *mono_arch_dyn_call_prepare     (MonoMethodSignature *sig);
2680 void      mono_arch_dyn_call_free               (MonoDynCallInfo *info);
2681 void      mono_arch_start_dyn_call              (MonoDynCallInfo *info, gpointer **args, guint8 *ret, guint8 *buf, int buf_len);
2682 void      mono_arch_finish_dyn_call             (MonoDynCallInfo *info, guint8 *buf);
2683 MonoInst *mono_arch_emit_inst_for_method        (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args);
2684 void      mono_arch_decompose_opts              (MonoCompile *cfg, MonoInst *ins);
2685 void      mono_arch_decompose_long_opts         (MonoCompile *cfg, MonoInst *ins);
2686 GSList*   mono_arch_get_delegate_invoke_impls   (void);
2687 LLVMCallInfo* mono_arch_get_llvm_call_info      (MonoCompile *cfg, MonoMethodSignature *sig) MONO_LLVM_INTERNAL;
2688 guint8*   mono_arch_emit_load_got_addr          (guint8 *start, guint8 *code, MonoCompile *cfg, MonoJumpInfo **ji);
2689 guint8*   mono_arch_emit_load_aotconst          (guint8 *start, guint8 *code, MonoJumpInfo **ji, MonoJumpInfoType tramp_type, gconstpointer target);
2690 GSList*   mono_arch_get_cie_program             (void);
2691 void      mono_arch_set_target                  (char *mtriple);
2692 gboolean  mono_arch_gsharedvt_sig_supported     (MonoMethodSignature *sig);
2693 gpointer  mono_arch_get_gsharedvt_trampoline    (MonoTrampInfo **info, gboolean aot);
2694 gpointer  mono_arch_get_gsharedvt_call_info     (gpointer addr, MonoMethodSignature *normal_sig, MonoMethodSignature *gsharedvt_sig, gboolean gsharedvt_in, gint32 vcall_offset, gboolean calli);
2695 gboolean  mono_arch_opcode_needs_emulation      (MonoCompile *cfg, int opcode);
2696 gboolean  mono_arch_tail_call_supported         (MonoCompile *cfg, MonoMethodSignature *caller_sig, MonoMethodSignature *callee_sig);
2697 int       mono_arch_translate_tls_offset        (int offset);
2698 gboolean  mono_arch_opcode_supported            (int opcode);
2699 void     mono_arch_setup_resume_sighandler_ctx  (MonoContext *ctx, gpointer func);
2700
2701 #ifdef MONO_ARCH_SOFT_FLOAT_FALLBACK
2702 gboolean  mono_arch_is_soft_float               (void);
2703 #else
2704 static inline MONO_ALWAYS_INLINE gboolean
2705 mono_arch_is_soft_float (void)
2706 {
2707         return FALSE;
2708 }
2709 #endif
2710
2711 /* Soft Debug support */
2712 #ifdef MONO_ARCH_SOFT_DEBUG_SUPPORTED
2713 void      mono_arch_set_breakpoint              (MonoJitInfo *ji, guint8 *ip);
2714 void      mono_arch_clear_breakpoint            (MonoJitInfo *ji, guint8 *ip);
2715 void      mono_arch_start_single_stepping       (void);
2716 void      mono_arch_stop_single_stepping        (void);
2717 gboolean  mono_arch_is_single_step_event        (void *info, void *sigctx);
2718 gboolean  mono_arch_is_breakpoint_event         (void *info, void *sigctx);
2719 void     mono_arch_skip_breakpoint              (MonoContext *ctx, MonoJitInfo *ji);
2720 void     mono_arch_skip_single_step             (MonoContext *ctx);
2721 gpointer mono_arch_get_seq_point_info           (MonoDomain *domain, guint8 *code);
2722 void     mono_arch_init_lmf_ext                 (MonoLMFExt *ext, gpointer prev_lmf);
2723 #endif
2724
2725 gboolean
2726 mono_arch_unwind_frame (MonoDomain *domain, MonoJitTlsData *jit_tls, 
2727                                                 MonoJitInfo *ji, MonoContext *ctx, 
2728                                                 MonoContext *new_ctx, MonoLMF **lmf,
2729                                                 mgreg_t **save_locations,
2730                                                 StackFrameInfo *frame_info);
2731 gpointer  mono_arch_get_throw_exception_by_name (void);
2732 gpointer mono_arch_get_call_filter              (MonoTrampInfo **info, gboolean aot);
2733 gpointer mono_arch_get_restore_context          (MonoTrampInfo **info, gboolean aot);
2734 gpointer  mono_arch_get_throw_exception         (MonoTrampInfo **info, gboolean aot);
2735 gpointer  mono_arch_get_rethrow_exception       (MonoTrampInfo **info, gboolean aot);
2736 gpointer  mono_arch_get_throw_corlib_exception  (MonoTrampInfo **info, gboolean aot);
2737 gpointer  mono_arch_get_throw_pending_exception (MonoTrampInfo **info, gboolean aot);
2738 gboolean mono_arch_handle_exception             (void *sigctx, gpointer obj);
2739 void     mono_arch_handle_altstack_exception    (void *sigctx, MONO_SIG_HANDLER_INFO_TYPE *siginfo, gpointer fault_addr, gboolean stack_ovf);
2740 gboolean mono_handle_soft_stack_ovf             (MonoJitTlsData *jit_tls, MonoJitInfo *ji, void *ctx, MONO_SIG_HANDLER_INFO_TYPE *siginfo, guint8* fault_addr);
2741 void     mono_handle_hard_stack_ovf             (MonoJitTlsData *jit_tls, MonoJitInfo *ji, void *ctx, guint8* fault_addr);
2742 gpointer mono_arch_ip_from_context              (void *sigctx);
2743 mgreg_t mono_arch_context_get_int_reg               (MonoContext *ctx, int reg);
2744 void     mono_arch_context_set_int_reg              (MonoContext *ctx, int reg, mgreg_t val);
2745 void     mono_arch_flush_register_windows       (void);
2746 gboolean mono_arch_is_inst_imm                  (gint64 imm);
2747 gboolean mono_arch_is_int_overflow              (void *sigctx, void *info);
2748 void     mono_arch_invalidate_method            (MonoJitInfo *ji, void *func, gpointer func_arg);
2749 guint32  mono_arch_get_patch_offset             (guint8 *code);
2750 gpointer*mono_arch_get_delegate_method_ptr_addr (guint8* code, mgreg_t *regs);
2751 void     mono_arch_create_vars                  (MonoCompile *cfg) MONO_LLVM_INTERNAL;
2752 void     mono_arch_save_unwind_info             (MonoCompile *cfg);
2753 void     mono_arch_register_lowlevel_calls      (void);
2754 gpointer mono_arch_get_unbox_trampoline         (MonoMethod *m, gpointer addr);
2755 gpointer mono_arch_get_static_rgctx_trampoline  (MonoMethod *m, MonoMethodRuntimeGenericContext *mrgctx, gpointer addr);
2756 gpointer  mono_arch_get_llvm_imt_trampoline     (MonoDomain *domain, MonoMethod *method, int vt_offset);
2757 gpointer mono_arch_get_gsharedvt_arg_trampoline (MonoDomain *domain, gpointer arg, gpointer addr);
2758 void     mono_arch_patch_callsite               (guint8 *method_start, guint8 *code, guint8 *addr);
2759 void     mono_arch_patch_plt_entry              (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *addr);
2760 void     mono_arch_nullify_class_init_trampoline(guint8 *code, mgreg_t *regs);
2761 int      mono_arch_get_this_arg_reg             (guint8 *code);
2762 gpointer mono_arch_get_this_arg_from_call       (mgreg_t *regs, guint8 *code);
2763 gpointer mono_arch_get_delegate_invoke_impl     (MonoMethodSignature *sig, gboolean has_target);
2764 gpointer mono_arch_get_delegate_virtual_invoke_impl (MonoMethodSignature *sig, MonoMethod *method, int offset, gboolean load_imt_reg);
2765 gpointer mono_arch_create_specific_trampoline   (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len);
2766 MonoMethod* mono_arch_find_imt_method           (mgreg_t *regs, guint8 *code);
2767 MonoVTable* mono_arch_find_static_call_vtable   (mgreg_t *regs, guint8 *code);
2768 gpointer    mono_arch_build_imt_thunk           (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckItem **imt_entries, int count, gpointer fail_tramp);
2769 void    mono_arch_notify_pending_exc            (MonoThreadInfo *info);
2770 guint8* mono_arch_get_call_target               (guint8 *code);
2771 guint32 mono_arch_get_plt_info_offset           (guint8 *plt_entry, mgreg_t *regs, guint8 *code);
2772 GSList *mono_arch_get_trampolines               (gboolean aot);
2773
2774 /* Handle block guard */
2775 gpointer mono_arch_install_handler_block_guard (MonoJitInfo *ji, MonoJitExceptionInfo *clause, MonoContext *ctx, gpointer new_value);
2776 gpointer mono_arch_create_handler_block_trampoline (MonoTrampInfo **info, gboolean aot);
2777 gpointer mono_create_handler_block_trampoline (void);
2778 gboolean mono_install_handler_block_guard (MonoThreadUnwindState *ctx);
2779
2780 /*New interruption machinery */
2781 void
2782 mono_setup_async_callback (MonoContext *ctx, void (*async_cb)(void *fun), gpointer user_data);
2783
2784 void
2785 mono_arch_setup_async_callback (MonoContext *ctx, void (*async_cb)(void *fun), gpointer user_data);
2786
2787 gboolean
2788 mono_thread_state_init_from_handle (MonoThreadUnwindState *tctx, MonoThreadInfo *info);
2789
2790
2791 /* Exception handling */
2792 typedef gboolean (*MonoJitStackWalk)            (StackFrameInfo *frame, MonoContext *ctx, gpointer data);
2793
2794 void     mono_exceptions_init                   (void);
2795 gboolean mono_handle_exception                  (MonoContext *ctx, MonoObject *obj);
2796 void     mono_handle_native_sigsegv             (int signal, void *sigctx, MONO_SIG_HANDLER_INFO_TYPE *siginfo);
2797 MONO_API void     mono_print_thread_dump                 (void *sigctx);
2798 MONO_API void     mono_print_thread_dump_from_ctx        (MonoContext *ctx);
2799 void     mono_walk_stack_with_ctx               (MonoJitStackWalk func, MonoContext *start_ctx, MonoUnwindOptions unwind_options, void *user_data);
2800 void     mono_walk_stack_with_state             (MonoJitStackWalk func, MonoThreadUnwindState *state, MonoUnwindOptions unwind_options, void *user_data);
2801 void     mono_walk_stack                        (MonoJitStackWalk func, MonoUnwindOptions options, void *user_data);
2802 gboolean mono_thread_state_init_from_sigctx     (MonoThreadUnwindState *ctx, void *sigctx);
2803 void     mono_thread_state_init                 (MonoThreadUnwindState *ctx);
2804 gboolean mono_thread_state_init_from_current    (MonoThreadUnwindState *ctx);
2805 gboolean mono_thread_state_init_from_monoctx    (MonoThreadUnwindState *ctx, MonoContext *mctx);
2806
2807 void     mono_setup_altstack                    (MonoJitTlsData *tls);
2808 void     mono_free_altstack                     (MonoJitTlsData *tls);
2809 gpointer mono_altstack_restore_prot             (mgreg_t *regs, guint8 *code, gpointer *tramp_data, guint8* tramp);
2810 MonoJitInfo* mini_jit_info_table_find           (MonoDomain *domain, char *addr, MonoDomain **out_domain);
2811 MonoJitInfo* mini_jit_info_table_find_ext       (MonoDomain *domain, char *addr, gboolean allow_trampolines, MonoDomain **out_domain);
2812 void     mono_resume_unwind                     (MonoContext *ctx) MONO_LLVM_INTERNAL;
2813
2814 MonoJitInfo * mono_find_jit_info                (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInfo *res, MonoJitInfo *prev_ji, MonoContext *ctx, MonoContext *new_ctx, char **trace, MonoLMF **lmf, int *native_offset, gboolean *managed);
2815
2816 typedef gboolean (*MonoExceptionFrameWalk)      (MonoMethod *method, gpointer ip, size_t native_offset, gboolean managed, gpointer user_data);
2817 MONO_API gboolean mono_exception_walk_trace     (MonoException *ex, MonoExceptionFrameWalk func, gpointer user_data);
2818 void mono_restore_context                       (MonoContext *ctx);
2819 guint8* mono_jinfo_get_unwind_info              (MonoJitInfo *ji, guint32 *unwind_info_len);
2820 int  mono_jinfo_get_epilog_size                 (MonoJitInfo *ji);
2821 void     mono_llvm_rethrow_exception            (MonoObject *ex);
2822 void     mono_llvm_throw_exception              (MonoObject *ex);
2823 void     mono_llvm_throw_corlib_exception       (guint32 ex_token_index);
2824 void     mono_llvm_resume_exception             (void);
2825 void     mono_llvm_clear_exception              (void);
2826 MonoObject *mono_llvm_load_exception            (void);
2827 void     mono_llvm_reset_exception              (void);
2828 void     mono_llvm_raise_exception              (MonoException *e);
2829 gint32 mono_llvm_match_exception                (MonoJitInfo *jinfo, guint32 region_start, guint32 region_end, gpointer rgctx, MonoObject *this_obj);
2830
2831 gboolean
2832 mono_find_jit_info_ext (MonoDomain *domain, MonoJitTlsData *jit_tls, 
2833                                                 MonoJitInfo *prev_ji, MonoContext *ctx,
2834                                                 MonoContext *new_ctx, char **trace, MonoLMF **lmf,
2835                                                 mgreg_t **save_locations,
2836                                                 StackFrameInfo *frame);
2837
2838 gpointer mono_get_throw_exception               (void);
2839 gpointer mono_get_rethrow_exception             (void);
2840 gpointer mono_get_call_filter                   (void);
2841 gpointer mono_get_restore_context               (void);
2842 gpointer mono_get_throw_exception_by_name       (void);
2843 gpointer mono_get_throw_corlib_exception        (void);
2844 gpointer mono_get_throw_exception_addr          (void);
2845 MonoArray *ves_icall_get_trace                  (MonoException *exc, gint32 skip, MonoBoolean need_file_info);
2846 MonoBoolean ves_icall_get_frame_info            (gint32 skip, MonoBoolean need_file_info, 
2847                                                  MonoReflectionMethod **method, 
2848                                                  gint32 *iloffset, gint32 *native_offset,
2849                                                  MonoString **file, gint32 *line, gint32 *column);
2850 void mono_set_cast_details                      (MonoClass *from, MonoClass *to);
2851
2852 /* Installs a function which is called when the runtime encounters an unhandled exception.
2853  * This hook isn't expected to return.
2854  * If no hook has been installed, the runtime will print a message before aborting.
2855  */
2856 typedef void  (*MonoUnhandledExceptionFunc)         (MonoObject *exc, gpointer user_data);
2857 MONO_API void mono_install_unhandled_exception_hook (MonoUnhandledExceptionFunc func, gpointer user_data);
2858 void          mono_invoke_unhandled_exception_hook  (MonoObject *exc);
2859
2860 void mono_decompose_typechecks (MonoCompile *cfg);
2861 /* Dominator/SSA methods */
2862 void        mono_compile_dominator_info         (MonoCompile *cfg, int dom_flags);
2863 void        mono_compute_natural_loops          (MonoCompile *cfg);
2864 MonoBitSet* mono_compile_iterated_dfrontier     (MonoCompile *cfg, MonoBitSet *set);
2865 void        mono_ssa_compute                    (MonoCompile *cfg);
2866 void        mono_ssa_remove                     (MonoCompile *cfg);
2867 void        mono_ssa_remove_gsharedvt           (MonoCompile *cfg);
2868 void        mono_ssa_cprop                      (MonoCompile *cfg);
2869 void        mono_ssa_deadce                     (MonoCompile *cfg);
2870 void        mono_ssa_strength_reduction         (MonoCompile *cfg);
2871 void        mono_free_loop_info                 (MonoCompile *cfg);
2872 void        mono_ssa_loop_invariant_code_motion (MonoCompile *cfg);
2873
2874 void        mono_ssa_compute2                   (MonoCompile *cfg);
2875 void        mono_ssa_remove2                    (MonoCompile *cfg);
2876 void        mono_ssa_cprop2                     (MonoCompile *cfg);
2877 void        mono_ssa_deadce2                    (MonoCompile *cfg);
2878
2879 /* debugging support */
2880 void      mono_debug_init_method                (MonoCompile *cfg, MonoBasicBlock *start_block,
2881                                                  guint32 breakpoint_id);
2882 void      mono_debug_open_method                (MonoCompile *cfg);
2883 void      mono_debug_close_method               (MonoCompile *cfg);
2884 void      mono_debug_free_method                (MonoCompile *cfg);
2885 void      mono_debug_open_block                 (MonoCompile *cfg, MonoBasicBlock *bb, guint32 address);
2886 void      mono_debug_record_line_number         (MonoCompile *cfg, MonoInst *ins, guint32 address);
2887 void      mono_debug_serialize_debug_info       (MonoCompile *cfg, guint8 **out_buf, guint32 *buf_len);
2888 void      mono_debug_add_aot_method             (MonoDomain *domain,
2889                                                  MonoMethod *method, guint8 *code_start, 
2890                                                  guint8 *debug_info, guint32 debug_info_len);
2891 MONO_API void      mono_debug_print_vars                 (gpointer ip, gboolean only_arguments);
2892 MONO_API void      mono_debugger_run_finally             (MonoContext *start_ctx);
2893
2894 MONO_API gboolean mono_breakpoint_clean_code (guint8 *method_start, guint8 *code, int offset, guint8 *buf, int size);
2895
2896 /* Tracing */
2897 MonoTraceSpec *mono_trace_parse_options         (const char *options);
2898 void           mono_trace_set_assembly          (MonoAssembly *assembly);
2899 gboolean       mono_trace_eval                  (MonoMethod *method);
2900
2901 extern void
2902 mono_perform_abc_removal (MonoCompile *cfg);
2903 extern void
2904 mono_perform_abc_removal (MonoCompile *cfg);
2905 extern void
2906 mono_perform_ssapre (MonoCompile *cfg);
2907 extern void
2908 mono_local_cprop (MonoCompile *cfg);
2909 extern void
2910 mono_local_cprop (MonoCompile *cfg);
2911 extern void
2912 mono_local_deadce (MonoCompile *cfg);
2913 void
2914 mono_local_alias_analysis (MonoCompile *cfg);
2915
2916 /* Generic sharing */
2917
2918 void
2919 mono_set_generic_sharing_supported (gboolean supported);
2920
2921 void
2922 mono_set_generic_sharing_vt_supported (gboolean supported);
2923
2924 void
2925 mono_set_partial_sharing_supported (gboolean supported);
2926
2927 gboolean
2928 mono_class_generic_sharing_enabled (MonoClass *klass);
2929
2930 gpointer
2931 mono_class_fill_runtime_generic_context (MonoVTable *class_vtable, guint32 slot, MonoError *error);
2932
2933 gpointer
2934 mono_method_fill_runtime_generic_context (MonoMethodRuntimeGenericContext *mrgctx, guint32 slot, MonoError *error);
2935
2936 MonoMethodRuntimeGenericContext*
2937 mono_method_lookup_rgctx (MonoVTable *class_vtable, MonoGenericInst *method_inst);
2938
2939 const char*
2940 mono_rgctx_info_type_to_str (MonoRgctxInfoType type);
2941
2942 MonoJumpInfoType
2943 mini_rgctx_info_type_to_patch_info_type (MonoRgctxInfoType info_type);
2944
2945 gboolean
2946 mono_method_needs_static_rgctx_invoke (MonoMethod *method, gboolean allow_type_vars);
2947
2948 int
2949 mono_class_rgctx_get_array_size (int n, gboolean mrgctx);
2950
2951 guint32
2952 mono_method_lookup_or_register_info (MonoMethod *method, gboolean in_mrgctx, gpointer data,
2953         MonoRgctxInfoType info_type, MonoGenericContext *generic_context);
2954
2955 MonoGenericContext
2956 mono_method_construct_object_context (MonoMethod *method);
2957
2958 MonoMethod*
2959 mono_method_get_declaring_generic_method (MonoMethod *method);
2960
2961 int
2962 mono_generic_context_check_used (MonoGenericContext *context);
2963
2964 int
2965 mono_class_check_context_used (MonoClass *klass);
2966
2967 gboolean
2968 mono_generic_context_is_sharable (MonoGenericContext *context, gboolean allow_type_vars);
2969
2970 gboolean
2971 mono_generic_context_is_sharable_full (MonoGenericContext *context, gboolean allow_type_vars, gboolean allow_partial);
2972
2973 gboolean
2974 mono_method_is_generic_impl (MonoMethod *method);
2975
2976 gboolean
2977 mono_method_is_generic_sharable (MonoMethod *method, gboolean allow_type_vars);
2978
2979 gboolean
2980 mono_method_is_generic_sharable_full (MonoMethod *method, gboolean allow_type_vars, gboolean allow_partial, gboolean allow_gsharedvt);
2981
2982 gboolean
2983 mini_class_is_generic_sharable (MonoClass *klass);
2984
2985
2986 gboolean
2987 mini_generic_inst_is_sharable (MonoGenericInst *inst, gboolean allow_type_vars, gboolean allow_partial);
2988
2989 gboolean
2990 mono_is_partially_sharable_inst (MonoGenericInst *inst);
2991
2992 gboolean
2993 mini_is_gsharedvt_gparam (MonoType *t);
2994
2995 MonoGenericContext* mini_method_get_context (MonoMethod *method);
2996
2997 int mono_method_check_context_used (MonoMethod *method);
2998
2999 gboolean mono_generic_context_equal_deep (MonoGenericContext *context1, MonoGenericContext *context2);
3000
3001 gpointer mono_helper_get_rgctx_other_ptr (MonoClass *caller_class, MonoVTable *vtable,
3002                                           guint32 token, guint32 token_source, guint32 rgctx_type,
3003                                           gint32 rgctx_index);
3004
3005 void mono_generic_sharing_init (void);
3006 void mono_generic_sharing_cleanup (void);
3007
3008 MonoClass* mini_class_get_container_class (MonoClass *klass);
3009 MonoGenericContext* mini_class_get_context (MonoClass *klass);
3010
3011 MonoType* mini_get_underlying_type (MonoType *type) MONO_LLVM_INTERNAL;
3012 MonoType* mini_type_get_underlying_type (MonoType *type);
3013 MonoMethod* mini_get_shared_method (MonoMethod *method);
3014 MonoMethod* mini_get_shared_method_to_register (MonoMethod *method);
3015 MonoMethod* mini_get_shared_method_full (MonoMethod *method, gboolean all_vt, gboolean is_gsharedvt);
3016 MonoType* mini_get_shared_gparam (MonoType *t, MonoType *constraint);
3017 int mini_get_rgctx_entry_slot (MonoJumpInfoRgctxEntry *entry);
3018
3019 int mini_type_stack_size (MonoType *t, int *align);
3020 int mini_type_stack_size_full (MonoType *t, guint32 *align, gboolean pinvoke);
3021 void type_to_eval_stack_type (MonoCompile *cfg, MonoType *type, MonoInst *inst);
3022 guint mono_type_to_regmove (MonoCompile *cfg, MonoType *type) MONO_LLVM_INTERNAL;
3023
3024 void mono_cfg_add_try_hole (MonoCompile *cfg, MonoExceptionClause *clause, guint8 *start, MonoBasicBlock *bb);
3025
3026 void mono_cfg_set_exception (MonoCompile *cfg, int type);
3027 void mono_cfg_set_exception_invalid_program (MonoCompile *cfg, char *msg);
3028
3029 #define MONO_TIME_TRACK(a, phase) \
3030         { \
3031                 GTimer *timer = mono_time_track_start (); \
3032                 (phase) ; \
3033                 mono_time_track_end (&(a), timer); \
3034         }
3035
3036 GTimer *mono_time_track_start (void);
3037 void mono_time_track_end (double *time, GTimer *timer);
3038
3039 void mono_update_jit_stats (MonoCompile *cfg);
3040
3041 gboolean mini_type_is_reference (MonoType *type);
3042 gboolean mini_type_is_vtype (MonoType *t) MONO_LLVM_INTERNAL;
3043 gboolean mini_type_var_is_vt (MonoType *type) MONO_LLVM_INTERNAL;
3044 gboolean mini_is_gsharedvt_type (MonoType *t);
3045 gboolean mini_is_gsharedvt_klass (MonoClass *klass) MONO_LLVM_INTERNAL;
3046 gboolean mini_is_gsharedvt_signature (MonoMethodSignature *sig);
3047 gboolean mini_is_gsharedvt_variable_type (MonoType *t) MONO_LLVM_INTERNAL;
3048 gboolean mini_is_gsharedvt_variable_klass (MonoClass *klass) MONO_LLVM_INTERNAL;
3049 gboolean mini_is_gsharedvt_sharable_method (MonoMethod *method);
3050 gboolean mini_is_gsharedvt_variable_signature (MonoMethodSignature *sig);
3051 gboolean mini_is_gsharedvt_sharable_inst (MonoGenericInst *inst);
3052 gpointer mini_method_get_rgctx (MonoMethod *m);
3053 void mini_init_gsctx (MonoDomain *domain, MonoMemPool *mp, MonoGenericContext *context, MonoGenericSharingContext *gsctx);
3054
3055 gpointer mini_get_gsharedvt_wrapper (gboolean gsharedvt_in, gpointer addr, MonoMethodSignature *normal_sig, MonoMethodSignature *gsharedvt_sig,
3056                                                                          gint32 vcall_offset, gboolean calli);
3057 MonoMethod* mini_get_gsharedvt_in_sig_wrapper (MonoMethodSignature *sig);
3058 MonoMethod* mini_get_gsharedvt_out_sig_wrapper (MonoMethodSignature *sig);
3059 MonoMethodSignature* mini_get_gsharedvt_out_sig_wrapper_signature (gboolean has_this, gboolean has_ret, int param_count);
3060 gboolean mini_gsharedvt_runtime_invoke_supported (MonoMethodSignature *sig);
3061
3062 /* SIMD support */
3063
3064 /*
3065 This enum MUST be kept in sync with its managed mirror Mono.Simd.AccelMode.
3066  */
3067 enum {
3068         SIMD_VERSION_SSE1       = 1 << 0,
3069         SIMD_VERSION_SSE2       = 1 << 1,
3070         SIMD_VERSION_SSE3       = 1 << 2,
3071         SIMD_VERSION_SSSE3      = 1 << 3,
3072         SIMD_VERSION_SSE41      = 1 << 4,
3073         SIMD_VERSION_SSE42      = 1 << 5,
3074         SIMD_VERSION_SSE4a      = 1 << 6,
3075         SIMD_VERSION_ALL        = SIMD_VERSION_SSE1 | SIMD_VERSION_SSE2 |
3076                           SIMD_VERSION_SSE3 | SIMD_VERSION_SSSE3 |
3077                           SIMD_VERSION_SSE41 | SIMD_VERSION_SSE42 |
3078                           SIMD_VERSION_SSE4a,
3079
3080         /* this value marks the end of the bit indexes used in 
3081          * this emum.
3082          */
3083         SIMD_VERSION_INDEX_END = 6 
3084 };
3085
3086 enum {
3087         SIMD_COMP_EQ,
3088         SIMD_COMP_LT,
3089         SIMD_COMP_LE,
3090         SIMD_COMP_UNORD,
3091         SIMD_COMP_NEQ,
3092         SIMD_COMP_NLT,
3093         SIMD_COMP_NLE,
3094         SIMD_COMP_ORD
3095 };
3096
3097 enum {
3098         SIMD_PREFETCH_MODE_NTA,
3099         SIMD_PREFETCH_MODE_0,
3100         SIMD_PREFETCH_MODE_1,
3101         SIMD_PREFETCH_MODE_2,
3102 };
3103
3104 const char *mono_arch_xregname (int reg);
3105 void        mono_simd_simplify_indirection (MonoCompile *cfg);
3106 MonoInst*   mono_emit_simd_intrinsics (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args);
3107 guint32     mono_arch_cpu_enumerate_simd_versions (void);
3108 void        mono_simd_intrinsics_init (void);
3109
3110 MonoInst*   mono_emit_native_types_intrinsics (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args);
3111 MonoType*   mini_native_type_replace_type (MonoType *type) MONO_LLVM_INTERNAL;
3112
3113 #ifdef __linux__
3114 #define XDEBUG_ENABLED 1
3115 #endif
3116
3117 #ifdef __linux__
3118 /* maybe enable also for other systems? */
3119 #define ENABLE_JIT_MAP 1
3120 void mono_enable_jit_map (void);
3121 void mono_emit_jit_map   (MonoJitInfo *jinfo);
3122 void mono_emit_jit_tramp (void *start, int size, const char *desc);
3123 gboolean mono_jit_map_is_enabled (void);
3124 #else
3125 #define mono_enable_jit_map()
3126 #define mono_emit_jit_map(ji)
3127 #define mono_emit_jit_tramp(s,z,d)
3128 #define mono_jit_map_is_enabled() (0)
3129 #endif
3130
3131 /*
3132  * Per-OS implementation functions.
3133  */
3134 void mono_runtime_install_handlers (void);
3135 void mono_runtime_cleanup_handlers (void);
3136 void mono_runtime_setup_stat_profiler (void);
3137 void mono_runtime_shutdown_stat_profiler (void);
3138 void mono_runtime_posix_install_handlers (void);
3139 pid_t mono_runtime_syscall_fork (void);
3140 void mono_gdb_render_native_backtraces (pid_t crashed_pid);
3141
3142 void mono_cross_helpers_run (void);
3143
3144 /*
3145  * Signal handling
3146  */
3147
3148 #if defined(DISABLE_HW_TRAPS) || defined(MONO_ARCH_DISABLE_HW_TRAPS)
3149  // Signal handlers not available
3150 #define MONO_ARCH_NEED_DIV_CHECK 1
3151 #endif
3152
3153 void MONO_SIG_HANDLER_SIGNATURE (mono_sigfpe_signal_handler) ;
3154 void MONO_SIG_HANDLER_SIGNATURE (mono_sigill_signal_handler) ;
3155 void MONO_SIG_HANDLER_SIGNATURE (mono_sigsegv_signal_handler);
3156 void MONO_SIG_HANDLER_SIGNATURE (mono_sigint_signal_handler) ;
3157 gboolean MONO_SIG_HANDLER_SIGNATURE (mono_chain_signal);
3158
3159 #ifndef MONO_ARCH_HAVE_TLS_GET
3160 #define MONO_ARCH_HAVE_TLS_GET 0
3161 #endif
3162
3163 #ifdef MONO_ARCH_VARARG_ICALLS
3164 #define ARCH_VARARG_ICALLS 1
3165 #else
3166 #define ARCH_VARARG_ICALLS 0
3167 #endif
3168
3169 /*
3170  * Native unwinder integration
3171  */
3172 void mono_exception_native_unwind (void *ctx, MONO_SIG_HANDLER_INFO_TYPE *info);
3173
3174 /*
3175  * Coop support for trampolines
3176  */
3177 void mono_interruption_checkpoint_from_trampoline (void);
3178
3179 #endif /* __MONO_MINI_H__ */