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