PageRenderTime 56ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/mono/mini/ir-emit.h

https://bitbucket.org/danipen/mono
C Header | 897 lines | 675 code | 162 blank | 60 comment | 185 complexity | dec68b7dc02dab2af8830a4540f6ca89 MD5 | raw file
Possible License(s): Unlicense, Apache-2.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0
  1. /*
  2. * ir-emit.h: IR Creation/Emission Macros
  3. *
  4. * Author:
  5. * Zoltan Varga (vargaz@gmail.com)
  6. *
  7. * (C) 2002 Ximian, Inc.
  8. */
  9. #ifndef __MONO_IR_EMIT_H__
  10. #define __MONO_IR_EMIT_H__
  11. #include "mini.h"
  12. G_BEGIN_DECLS
  13. static inline guint32
  14. alloc_ireg (MonoCompile *cfg)
  15. {
  16. return cfg->next_vreg ++;
  17. }
  18. static inline guint32
  19. alloc_preg (MonoCompile *cfg)
  20. {
  21. return alloc_ireg (cfg);
  22. }
  23. static inline guint32
  24. alloc_lreg (MonoCompile *cfg)
  25. {
  26. #if SIZEOF_REGISTER == 8
  27. return cfg->next_vreg ++;
  28. #else
  29. /* Use a pair of consecutive vregs */
  30. guint32 res = cfg->next_vreg;
  31. cfg->next_vreg += 3;
  32. return res;
  33. #endif
  34. }
  35. static inline guint32
  36. alloc_freg (MonoCompile *cfg)
  37. {
  38. #ifdef MONO_ARCH_SOFT_FLOAT
  39. /* Allocate an lvreg so float ops can be decomposed into long ops */
  40. return alloc_lreg (cfg);
  41. #else
  42. /* Allocate these from the same pool as the int regs */
  43. return cfg->next_vreg ++;
  44. #endif
  45. }
  46. static inline guint32
  47. alloc_ireg_ref (MonoCompile *cfg)
  48. {
  49. int vreg = alloc_ireg (cfg);
  50. if (cfg->compute_gc_maps)
  51. mono_mark_vreg_as_ref (cfg, vreg);
  52. return vreg;
  53. }
  54. static inline guint32
  55. alloc_ireg_mp (MonoCompile *cfg)
  56. {
  57. int vreg = alloc_ireg (cfg);
  58. if (cfg->compute_gc_maps)
  59. mono_mark_vreg_as_mp (cfg, vreg);
  60. return vreg;
  61. }
  62. static inline guint32
  63. alloc_dreg (MonoCompile *cfg, MonoStackType stack_type)
  64. {
  65. switch (stack_type) {
  66. case STACK_I4:
  67. case STACK_PTR:
  68. return alloc_ireg (cfg);
  69. case STACK_MP:
  70. return alloc_ireg_mp (cfg);
  71. case STACK_OBJ:
  72. return alloc_ireg_ref (cfg);
  73. case STACK_R8:
  74. return alloc_freg (cfg);
  75. case STACK_I8:
  76. return alloc_lreg (cfg);
  77. case STACK_VTYPE:
  78. return alloc_ireg (cfg);
  79. default:
  80. g_warning ("Unknown stack type %x\n", stack_type);
  81. g_assert_not_reached ();
  82. return -1;
  83. }
  84. }
  85. #undef MONO_INST_NEW
  86. /*
  87. * FIXME: zeroing out some fields is not needed with the new IR, but the old
  88. * JIT code still uses the left and right fields, so it has to stay.
  89. */
  90. #define MONO_INST_NEW(cfg,dest,op) do { \
  91. (dest) = mono_mempool_alloc ((cfg)->mempool, sizeof (MonoInst)); \
  92. (dest)->inst_c0 = (dest)->inst_c1 = 0; \
  93. (dest)->next = (dest)->prev = NULL; \
  94. (dest)->opcode = (op); \
  95. (dest)->flags = 0; \
  96. (dest)->type = 0; \
  97. (dest)->dreg = -1; \
  98. MONO_INST_NULLIFY_SREGS ((dest)); \
  99. (dest)->cil_code = (cfg)->ip; \
  100. } while (0)
  101. /*
  102. * Variants which take a dest argument and don't do an emit
  103. */
  104. #define NEW_ICONST(cfg,dest,val) do { \
  105. MONO_INST_NEW ((cfg), (dest), OP_ICONST); \
  106. (dest)->inst_c0 = (val); \
  107. (dest)->type = STACK_I4; \
  108. (dest)->dreg = alloc_dreg ((cfg), STACK_I4); \
  109. } while (0)
  110. /*
  111. * Avoid using this with a non-NULL val if possible as it is not AOT
  112. * compatible. Use one of the NEW_xxxCONST variants instead.
  113. */
  114. #define NEW_PCONST(cfg,dest,val) do { \
  115. MONO_INST_NEW ((cfg), (dest), OP_PCONST); \
  116. (dest)->inst_p0 = (val); \
  117. (dest)->type = STACK_PTR; \
  118. (dest)->dreg = alloc_dreg ((cfg), STACK_PTR); \
  119. } while (0)
  120. #define NEW_I8CONST(cfg,dest,val) do { \
  121. MONO_INST_NEW ((cfg), (dest), OP_I8CONST); \
  122. (dest)->dreg = alloc_lreg ((cfg)); \
  123. (dest)->type = STACK_I8; \
  124. (dest)->inst_l = (val); \
  125. } while (0)
  126. #define NEW_STORE_MEMBASE(cfg,dest,op,base,offset,sr) do { \
  127. MONO_INST_NEW ((cfg), (dest), (op)); \
  128. (dest)->sreg1 = sr; \
  129. (dest)->inst_destbasereg = base; \
  130. (dest)->inst_offset = offset; \
  131. } while (0)
  132. #define NEW_LOAD_MEMBASE(cfg,dest,op,dr,base,offset) do { \
  133. MONO_INST_NEW ((cfg), (dest), (op)); \
  134. (dest)->dreg = (dr); \
  135. (dest)->inst_basereg = (base); \
  136. (dest)->inst_offset = (offset); \
  137. (dest)->type = STACK_I4; \
  138. } while (0)
  139. #define NEW_LOAD_MEM(cfg,dest,op,dr,mem) do { \
  140. MONO_INST_NEW ((cfg), (dest), (op)); \
  141. (dest)->dreg = (dr); \
  142. (dest)->inst_p0 = (gpointer)(gssize)(mem); \
  143. (dest)->type = STACK_I4; \
  144. } while (0)
  145. #define NEW_UNALU(cfg,dest,op,dr,sr1) do { \
  146. MONO_INST_NEW ((cfg), (dest), (op)); \
  147. (dest)->dreg = dr; \
  148. (dest)->sreg1 = sr1; \
  149. } while (0)
  150. #define NEW_BIALU(cfg,dest,op,dr,sr1,sr2) do { \
  151. MONO_INST_NEW ((cfg), (dest), (op)); \
  152. (dest)->dreg = (dr); \
  153. (dest)->sreg1 = (sr1); \
  154. (dest)->sreg2 = (sr2); \
  155. } while (0)
  156. #define NEW_BIALU_IMM(cfg,dest,op,dr,sr,imm) do { \
  157. MONO_INST_NEW ((cfg), (dest), (op)); \
  158. (dest)->dreg = dr; \
  159. (dest)->sreg1 = sr; \
  160. (dest)->inst_imm = (imm); \
  161. } while (0)
  162. #ifdef MONO_ARCH_NEED_GOT_VAR
  163. #define NEW_PATCH_INFO(cfg,dest,el1,el2) do { \
  164. MONO_INST_NEW ((cfg), (dest), OP_PATCH_INFO); \
  165. (dest)->inst_left = (gpointer)(el1); \
  166. (dest)->inst_right = (gpointer)(el2); \
  167. } while (0)
  168. /* FIXME: Add the PUSH_GOT_ENTRY optimizations */
  169. #define NEW_AOTCONST(cfg,dest,patch_type,cons) do { \
  170. MONO_INST_NEW ((cfg), (dest), cfg->compile_aot ? OP_GOT_ENTRY : OP_PCONST); \
  171. if (cfg->compile_aot) { \
  172. MonoInst *group, *got_loc; \
  173. got_loc = mono_get_got_var (cfg); \
  174. NEW_PATCH_INFO ((cfg), group, cons, patch_type); \
  175. (dest)->inst_basereg = got_loc->dreg; \
  176. (dest)->inst_p1 = group; \
  177. } else { \
  178. (dest)->inst_p0 = (cons); \
  179. (dest)->inst_i1 = (gpointer)(patch_type); \
  180. } \
  181. (dest)->type = STACK_PTR; \
  182. (dest)->dreg = alloc_dreg ((cfg), STACK_PTR); \
  183. } while (0)
  184. #define NEW_AOTCONST_TOKEN(cfg,dest,patch_type,image,token,generic_context,stack_type,stack_class) do { \
  185. MonoInst *group, *got_loc; \
  186. MONO_INST_NEW ((cfg), (dest), OP_GOT_ENTRY); \
  187. got_loc = mono_get_got_var (cfg); \
  188. NEW_PATCH_INFO ((cfg), group, NULL, patch_type); \
  189. group->inst_p0 = mono_jump_info_token_new2 ((cfg)->mempool, (image), (token), (generic_context)); \
  190. (dest)->inst_basereg = got_loc->dreg; \
  191. (dest)->inst_p1 = group; \
  192. (dest)->type = (stack_type); \
  193. (dest)->klass = (stack_class); \
  194. (dest)->dreg = alloc_dreg ((cfg), (stack_type)); \
  195. } while (0)
  196. #else
  197. #define NEW_AOTCONST(cfg,dest,patch_type,cons) do { \
  198. MONO_INST_NEW ((cfg), (dest), cfg->compile_aot ? OP_AOTCONST : OP_PCONST); \
  199. (dest)->inst_p0 = (cons); \
  200. (dest)->inst_i1 = (gpointer)(patch_type); \
  201. (dest)->type = STACK_PTR; \
  202. (dest)->dreg = alloc_dreg ((cfg), STACK_PTR); \
  203. } while (0)
  204. #define NEW_AOTCONST_TOKEN(cfg,dest,patch_type,image,token,generic_context,stack_type,stack_class) do { \
  205. MONO_INST_NEW ((cfg), (dest), OP_AOTCONST); \
  206. (dest)->inst_p0 = mono_jump_info_token_new2 ((cfg)->mempool, (image), (token), (generic_context)); \
  207. (dest)->inst_p1 = (gpointer)(patch_type); \
  208. (dest)->type = (stack_type); \
  209. (dest)->klass = (stack_class); \
  210. (dest)->dreg = alloc_dreg ((cfg), (stack_type)); \
  211. } while (0)
  212. #endif
  213. #define NEW_CLASSCONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_CLASS, (val))
  214. #define NEW_IMAGECONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_IMAGE, (val))
  215. #define NEW_FIELDCONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_FIELD, (val))
  216. #define NEW_METHODCONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_METHODCONST, (val))
  217. #define NEW_VTABLECONST(cfg,dest,vtable) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_VTABLE, cfg->compile_aot ? (gpointer)((vtable)->klass) : (vtable))
  218. #define NEW_SFLDACONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_SFLDA, (val))
  219. #define NEW_LDSTRCONST(cfg,dest,image,token) NEW_AOTCONST_TOKEN ((cfg), (dest), MONO_PATCH_INFO_LDSTR, (image), (token), NULL, STACK_OBJ, mono_defaults.string_class)
  220. #define NEW_TYPE_FROM_HANDLE_CONST(cfg,dest,image,token,generic_context) NEW_AOTCONST_TOKEN ((cfg), (dest), MONO_PATCH_INFO_TYPE_FROM_HANDLE, (image), (token), (generic_context), STACK_OBJ, mono_defaults.monotype_class)
  221. #define NEW_LDTOKENCONST(cfg,dest,image,token) NEW_AOTCONST_TOKEN ((cfg), (dest), MONO_PATCH_INFO_LDTOKEN, (image), (token), NULL, STACK_PTR, NULL)
  222. #define NEW_DECLSECCONST(cfg,dest,image,entry) do { \
  223. if (cfg->compile_aot) { \
  224. NEW_AOTCONST_TOKEN (cfg, dest, MONO_PATCH_INFO_DECLSEC, image, (entry).index, NULL, STACK_OBJ, NULL); \
  225. } else { \
  226. NEW_PCONST (cfg, args [0], (entry).blob); \
  227. } \
  228. } while (0)
  229. #define NEW_METHOD_RGCTX_CONST(cfg,dest,method) do { \
  230. if (cfg->compile_aot) { \
  231. NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_METHOD_RGCTX, (method)); \
  232. } else { \
  233. MonoMethodRuntimeGenericContext *mrgctx; \
  234. mrgctx = mono_method_lookup_rgctx (mono_class_vtable ((cfg)->domain, (method)->klass), mini_method_get_context ((method))->method_inst); \
  235. NEW_PCONST ((cfg), (dest), (mrgctx)); \
  236. } \
  237. } while (0)
  238. #define NEW_DOMAINCONST(cfg,dest) do { \
  239. if ((cfg->opt & MONO_OPT_SHARED) || cfg->compile_aot) { \
  240. /* avoid depending on undefined C behavior in sequence points */ \
  241. MonoInst* __domain_var = mono_get_domainvar (cfg); \
  242. NEW_TEMPLOAD (cfg, dest, __domain_var->inst_c0); \
  243. } else { \
  244. NEW_PCONST (cfg, dest, (cfg)->domain); \
  245. } \
  246. } while (0)
  247. #define NEW_JIT_ICALL_ADDRCONST(cfg,dest,name) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_JIT_ICALL_ADDR, (name))
  248. #define GET_VARINFO_INST(cfg,num) ((cfg)->varinfo [(num)]->inst)
  249. #define NEW_VARLOAD(cfg,dest,var,vartype) do { \
  250. MONO_INST_NEW ((cfg), (dest), OP_MOVE); \
  251. (dest)->opcode = mono_type_to_regmove ((cfg), (vartype)); \
  252. type_to_eval_stack_type ((cfg), (vartype), (dest)); \
  253. (dest)->klass = var->klass; \
  254. (dest)->sreg1 = var->dreg; \
  255. (dest)->dreg = alloc_dreg ((cfg), (dest)->type); \
  256. if ((dest)->opcode == OP_VMOVE) (dest)->klass = mono_class_from_mono_type ((vartype)); \
  257. } while (0)
  258. #ifdef MONO_ARCH_SOFT_FLOAT
  259. #define DECOMPOSE_INTO_REGPAIR(stack_type) ((stack_type) == STACK_I8 || (stack_type) == STACK_R8)
  260. #else
  261. #define DECOMPOSE_INTO_REGPAIR(stack_type) ((stack_type) == STACK_I8)
  262. #endif
  263. #define NEW_VARLOADA(cfg,dest,var,vartype) do { \
  264. MONO_INST_NEW ((cfg), (dest), OP_LDADDR); \
  265. (dest)->inst_p0 = (var); \
  266. (var)->flags |= MONO_INST_INDIRECT; \
  267. (dest)->type = STACK_MP; \
  268. (dest)->klass = (var)->klass; \
  269. (dest)->dreg = alloc_dreg ((cfg), STACK_MP); \
  270. if (SIZEOF_REGISTER == 4 && DECOMPOSE_INTO_REGPAIR ((var)->type)) { MonoInst *var1 = get_vreg_to_inst (cfg, (var)->dreg + 1); MonoInst *var2 = get_vreg_to_inst (cfg, (var)->dreg + 2); g_assert (var1); g_assert (var2); var1->flags |= MONO_INST_INDIRECT; var2->flags |= MONO_INST_INDIRECT; } \
  271. } while (0)
  272. #define NEW_VARSTORE(cfg,dest,var,vartype,inst) do { \
  273. MONO_INST_NEW ((cfg), (dest), OP_MOVE); \
  274. (dest)->opcode = mono_type_to_regmove ((cfg), (vartype)); \
  275. (dest)->klass = (var)->klass; \
  276. (dest)->sreg1 = (inst)->dreg; \
  277. (dest)->dreg = (var)->dreg; \
  278. if ((dest)->opcode == OP_VMOVE) (dest)->klass = mono_class_from_mono_type ((vartype)); \
  279. } while (0)
  280. #define NEW_TEMPLOAD(cfg,dest,num) NEW_VARLOAD ((cfg), (dest), (cfg)->varinfo [(num)], (cfg)->varinfo [(num)]->inst_vtype)
  281. #define NEW_TEMPLOADA(cfg,dest,num) NEW_VARLOADA ((cfg), (dest), cfg->varinfo [(num)], cfg->varinfo [(num)]->inst_vtype)
  282. #define NEW_TEMPSTORE(cfg,dest,num,inst) NEW_VARSTORE ((cfg), (dest), (cfg)->varinfo [(num)], (cfg)->varinfo [(num)]->inst_vtype, (inst))
  283. #define NEW_ARGLOAD(cfg,dest,num) NEW_VARLOAD ((cfg), (dest), cfg->args [(num)], cfg->arg_types [(num)])
  284. #define NEW_LOCLOAD(cfg,dest,num) NEW_VARLOAD ((cfg), (dest), cfg->locals [(num)], header->locals [(num)])
  285. #define NEW_LOCSTORE(cfg,dest,num,inst) NEW_VARSTORE ((cfg), (dest), (cfg)->locals [(num)], (cfg)->locals [(num)]->inst_vtype, (inst))
  286. #define NEW_ARGSTORE(cfg,dest,num,inst) NEW_VARSTORE ((cfg), (dest), cfg->args [(num)], cfg->arg_types [(num)], (inst))
  287. #define NEW_LOCLOADA(cfg,dest,num) NEW_VARLOADA ((cfg), (dest), (cfg)->locals [(num)], (cfg)->locals [(num)]->inst_vtype)
  288. #define NEW_RETLOADA(cfg,dest) do { \
  289. MONO_INST_NEW ((cfg), (dest), OP_MOVE); \
  290. (dest)->type = STACK_MP; \
  291. (dest)->klass = cfg->ret->klass; \
  292. (dest)->sreg1 = cfg->vret_addr->dreg; \
  293. (dest)->dreg = alloc_dreg ((cfg), (dest)->type); \
  294. } while (0)
  295. #define NEW_ARGLOADA(cfg,dest,num) NEW_VARLOADA ((cfg), (dest), arg_array [(num)], param_types [(num)])
  296. /* Promote the vreg to a variable so its address can be taken */
  297. #define NEW_VARLOADA_VREG(cfg,dest,vreg,ltype) do { \
  298. MonoInst *var = get_vreg_to_inst ((cfg), (vreg)); \
  299. if (!var) \
  300. var = mono_compile_create_var_for_vreg ((cfg), (ltype), OP_LOCAL, (vreg)); \
  301. NEW_VARLOADA ((cfg), (dest), (var), (ltype)); \
  302. } while (0)
  303. #define NEW_DUMMY_USE(cfg,dest,var) do { \
  304. MONO_INST_NEW ((cfg), (dest), OP_DUMMY_USE); \
  305. (dest)->sreg1 = var->dreg; \
  306. } while (0)
  307. /* Variants which take a type argument and handle vtypes as well */
  308. #define NEW_LOAD_MEMBASE_TYPE(cfg,dest,ltype,base,offset) do { \
  309. NEW_LOAD_MEMBASE ((cfg), (dest), mono_type_to_load_membase ((cfg), (ltype)), 0, (base), (offset)); \
  310. type_to_eval_stack_type ((cfg), (ltype), (dest)); \
  311. (dest)->dreg = alloc_dreg ((cfg), (dest)->type); \
  312. } while (0)
  313. #define NEW_STORE_MEMBASE_TYPE(cfg,dest,ltype,base,offset,sr) do { \
  314. MONO_INST_NEW ((cfg), (dest), mono_type_to_store_membase ((cfg), (ltype))); \
  315. (dest)->sreg1 = sr; \
  316. (dest)->inst_destbasereg = base; \
  317. (dest)->inst_offset = offset; \
  318. type_to_eval_stack_type ((cfg), (ltype), (dest)); \
  319. (dest)->klass = mono_class_from_mono_type (ltype); \
  320. } while (0)
  321. #define NEW_SEQ_POINT(cfg,dest,il_offset,intr_loc) do { \
  322. MONO_INST_NEW ((cfg), (dest), OP_SEQ_POINT); \
  323. (dest)->inst_imm = (il_offset); \
  324. (dest)->flags = intr_loc ? MONO_INST_SINGLE_STEP_LOC : 0; \
  325. } while (0)
  326. #define NEW_GC_PARAM_SLOT_LIVENESS_DEF(cfg,dest,offset,type) do { \
  327. MONO_INST_NEW ((cfg), (dest), OP_GC_PARAM_SLOT_LIVENESS_DEF); \
  328. (dest)->inst_offset = (offset); \
  329. (dest)->inst_vtype = (type); \
  330. } while (0)
  331. /*
  332. * Variants which do an emit as well.
  333. */
  334. #define EMIT_NEW_ICONST(cfg,dest,val) do { NEW_ICONST ((cfg), (dest), (val)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
  335. #define EMIT_NEW_PCONST(cfg,dest,val) do { NEW_PCONST ((cfg), (dest), (val)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
  336. #define EMIT_NEW_I8CONST(cfg,dest,val) do { NEW_I8CONST ((cfg), (dest), (val)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
  337. #define EMIT_NEW_AOTCONST(cfg,dest,patch_type,cons) do { NEW_AOTCONST ((cfg), (dest), (patch_type), (cons)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
  338. #define EMIT_NEW_AOTCONST_TOKEN(cfg,dest,patch_type,image,token,stack_type,stack_class) do { NEW_AOTCONST_TOKEN ((cfg), (dest), (patch_type), (image), (token), NULL, (stack_type), (stack_class)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
  339. #define EMIT_NEW_CLASSCONST(cfg,dest,val) do { NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_CLASS, (val)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
  340. #define EMIT_NEW_IMAGECONST(cfg,dest,val) do { NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_IMAGE, (val)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
  341. #define EMIT_NEW_FIELDCONST(cfg,dest,val) do { NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_FIELD, (val)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
  342. #define EMIT_NEW_METHODCONST(cfg,dest,val) do { NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_METHODCONST, (val)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
  343. #define EMIT_NEW_VTABLECONST(cfg,dest,vtable) do { NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_VTABLE, cfg->compile_aot ? (gpointer)((vtable)->klass) : (vtable)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
  344. #define EMIT_NEW_SFLDACONST(cfg,dest,val) do { NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_SFLDA, (val)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
  345. #define EMIT_NEW_LDSTRCONST(cfg,dest,image,token) do { NEW_AOTCONST_TOKEN ((cfg), (dest), MONO_PATCH_INFO_LDSTR, (image), (token), NULL, STACK_OBJ, mono_defaults.string_class); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
  346. #define EMIT_NEW_TYPE_FROM_HANDLE_CONST(cfg,dest,image,token,generic_context) do { NEW_AOTCONST_TOKEN ((cfg), (dest), MONO_PATCH_INFO_TYPE_FROM_HANDLE, (image), (token), (generic_context), STACK_OBJ, mono_defaults.monotype_class); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
  347. #define EMIT_NEW_LDTOKENCONST(cfg,dest,image,token) do { NEW_AOTCONST_TOKEN ((cfg), (dest), MONO_PATCH_INFO_LDTOKEN, (image), (token), NULL, STACK_PTR, NULL); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
  348. #define EMIT_NEW_DOMAINCONST(cfg,dest) do { NEW_DOMAINCONST ((cfg), (dest)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
  349. #define EMIT_NEW_DECLSECCONST(cfg,dest,image,entry) do { NEW_DECLSECCONST ((cfg), (dest), (image), (entry)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
  350. #define EMIT_NEW_METHOD_RGCTX_CONST(cfg,dest,method) do { NEW_METHOD_RGCTX_CONST ((cfg), (dest), (method)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
  351. #define EMIT_NEW_JIT_ICALL_ADDRCONST(cfg,dest,name) do { NEW_JIT_ICALL_ADDRCONST ((cfg), (dest), (name)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
  352. #define EMIT_NEW_VARLOAD(cfg,dest,var,vartype) do { NEW_VARLOAD ((cfg), (dest), (var), (vartype)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
  353. #define EMIT_NEW_VARSTORE(cfg,dest,var,vartype,inst) do { NEW_VARSTORE ((cfg), (dest), (var), (vartype), (inst)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
  354. #define EMIT_NEW_VARLOADA(cfg,dest,var,vartype) do { NEW_VARLOADA ((cfg), (dest), (var), (vartype)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
  355. #ifdef MONO_ARCH_SOFT_FLOAT
  356. /*
  357. * Since the IL stack (and our vregs) contain double values, we have to do a conversion
  358. * when loading/storing args/locals of type R4.
  359. */
  360. #define EMIT_NEW_VARLOAD_SFLOAT(cfg,dest,var,vartype) do { \
  361. if (COMPILE_SOFT_FLOAT ((cfg)) && !(vartype)->byref && (vartype)->type == MONO_TYPE_R4) { \
  362. MonoInst *iargs [1]; \
  363. EMIT_NEW_VARLOADA (cfg, iargs [0], (var), (vartype)); \
  364. (dest) = mono_emit_jit_icall (cfg, mono_fload_r4, iargs); \
  365. } else { \
  366. EMIT_NEW_VARLOAD ((cfg), (dest), (var), (vartype)); \
  367. } \
  368. } while (0)
  369. #define EMIT_NEW_VARSTORE_SFLOAT(cfg,dest,var,vartype,inst) do { \
  370. if (COMPILE_SOFT_FLOAT ((cfg)) && !(vartype)->byref && (vartype)->type == MONO_TYPE_R4) { \
  371. MonoInst *iargs [2]; \
  372. iargs [0] = (inst); \
  373. EMIT_NEW_VARLOADA (cfg, iargs [1], (var), (vartype)); \
  374. mono_emit_jit_icall (cfg, mono_fstore_r4, iargs); \
  375. } else { \
  376. EMIT_NEW_VARSTORE ((cfg), (dest), (var), (vartype), (inst)); \
  377. } \
  378. } while (0)
  379. #define EMIT_NEW_ARGLOAD(cfg,dest,num) EMIT_NEW_VARLOAD_SFLOAT ((cfg), (dest), cfg->args [(num)], cfg->arg_types [(num)])
  380. #define EMIT_NEW_LOCLOAD(cfg,dest,num) EMIT_NEW_VARLOAD_SFLOAT ((cfg), (dest), cfg->locals [(num)], header->locals [(num)])
  381. #define EMIT_NEW_LOCSTORE(cfg,dest,num,inst) EMIT_NEW_VARSTORE_SFLOAT ((cfg), (dest), (cfg)->locals [(num)], (cfg)->locals [(num)]->inst_vtype, (inst))
  382. #define EMIT_NEW_ARGSTORE(cfg,dest,num,inst) EMIT_NEW_VARSTORE_SFLOAT ((cfg), (dest), cfg->args [(num)], cfg->arg_types [(num)], (inst))
  383. #else
  384. #define EMIT_NEW_ARGLOAD(cfg,dest,num) do { NEW_ARGLOAD ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
  385. #define EMIT_NEW_LOCLOAD(cfg,dest,num) do { NEW_LOCLOAD ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
  386. #define EMIT_NEW_LOCSTORE(cfg,dest,num,inst) do { NEW_LOCSTORE ((cfg), (dest), (num), (inst)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
  387. #define EMIT_NEW_ARGSTORE(cfg,dest,num,inst) do { NEW_ARGSTORE ((cfg), (dest), (num), (inst)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
  388. #endif
  389. #define EMIT_NEW_TEMPLOAD(cfg,dest,num) do { NEW_TEMPLOAD ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
  390. #define EMIT_NEW_TEMPLOADA(cfg,dest,num) do { NEW_TEMPLOADA ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
  391. #define EMIT_NEW_LOCLOADA(cfg,dest,num) do { NEW_LOCLOADA ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
  392. #define EMIT_NEW_ARGLOADA(cfg,dest,num) do { NEW_ARGLOADA ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
  393. #define EMIT_NEW_RETLOADA(cfg,dest) do { NEW_RETLOADA ((cfg), (dest)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
  394. #define EMIT_NEW_TEMPSTORE(cfg,dest,num,inst) do { NEW_TEMPSTORE ((cfg), (dest), (num), (inst)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
  395. #define EMIT_NEW_VARLOADA_VREG(cfg,dest,vreg,ltype) do { NEW_VARLOADA_VREG ((cfg), (dest), (vreg), (ltype)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
  396. #define EMIT_NEW_DUMMY_USE(cfg,dest,var) do { NEW_DUMMY_USE ((cfg), (dest), (var)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
  397. #define EMIT_NEW_UNALU(cfg,dest,op,dr,sr1) do { NEW_UNALU ((cfg), (dest), (op), (dr), (sr1)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
  398. #define EMIT_NEW_BIALU(cfg,dest,op,dr,sr1,sr2) do { NEW_BIALU ((cfg), (dest), (op), (dr), (sr1), (sr2)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
  399. #define EMIT_NEW_BIALU_IMM(cfg,dest,op,dr,sr,imm) do { NEW_BIALU_IMM ((cfg), (dest), (op), (dr), (sr), (imm)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
  400. #define EMIT_NEW_LOAD_MEMBASE(cfg,dest,op,dr,base,offset) do { NEW_LOAD_MEMBASE ((cfg), (dest), (op), (dr), (base), (offset)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
  401. #define EMIT_NEW_STORE_MEMBASE(cfg,dest,op,base,offset,sr) do { NEW_STORE_MEMBASE ((cfg), (dest), (op), (base), (offset), (sr)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
  402. #define EMIT_NEW_LOAD_MEMBASE_TYPE(cfg,dest,ltype,base,offset) do { NEW_LOAD_MEMBASE_TYPE ((cfg), (dest), (ltype), (base), (offset)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
  403. #define EMIT_NEW_STORE_MEMBASE_TYPE(cfg,dest,ltype,base,offset,sr) do { NEW_STORE_MEMBASE_TYPE ((cfg), (dest), (ltype), (base), (offset), (sr)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
  404. #define EMIT_NEW_GC_PARAM_SLOT_LIVENESS_DEF(cfg,dest,offset,type) do { NEW_GC_PARAM_SLOT_LIVENESS_DEF ((cfg), (dest), (offset), (type)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
  405. /*
  406. * Variants which do not take an dest argument, but take a dreg argument.
  407. */
  408. #define MONO_EMIT_NEW_ICONST(cfg,dr,imm) do { \
  409. MonoInst *inst; \
  410. MONO_INST_NEW ((cfg), (inst), OP_ICONST); \
  411. inst->dreg = dr; \
  412. inst->inst_c0 = imm; \
  413. MONO_ADD_INS ((cfg)->cbb, inst); \
  414. } while (0)
  415. #define MONO_EMIT_NEW_PCONST(cfg,dr,val) do { \
  416. MonoInst *inst; \
  417. MONO_INST_NEW ((cfg), (inst), OP_PCONST); \
  418. inst->dreg = dr; \
  419. (inst)->inst_p0 = (val); \
  420. (inst)->type = STACK_PTR; \
  421. MONO_ADD_INS ((cfg)->cbb, inst); \
  422. } while (0)
  423. #define MONO_EMIT_NEW_I8CONST(cfg,dr,imm) do { \
  424. MonoInst *inst; \
  425. MONO_INST_NEW ((cfg), (inst), OP_I8CONST); \
  426. inst->dreg = dr; \
  427. inst->inst_l = imm; \
  428. MONO_ADD_INS ((cfg)->cbb, inst); \
  429. } while (0)
  430. #ifdef MONO_ARCH_NEED_GOT_VAR
  431. #define MONO_EMIT_NEW_AOTCONST(cfg,dr,cons,patch_type) do { \
  432. MonoInst *inst; \
  433. NEW_AOTCONST ((cfg), (inst), (patch_type), (cons)); \
  434. inst->dreg = (dr); \
  435. MONO_ADD_INS ((cfg)->cbb, inst); \
  436. } while (0)
  437. #else
  438. #define MONO_EMIT_NEW_AOTCONST(cfg,dr,imm,type) do { \
  439. MonoInst *inst; \
  440. MONO_INST_NEW ((cfg), (inst), cfg->compile_aot ? OP_AOTCONST : OP_PCONST); \
  441. inst->dreg = dr; \
  442. inst->inst_p0 = imm; \
  443. inst->inst_c1 = type; \
  444. MONO_ADD_INS ((cfg)->cbb, inst); \
  445. } while (0)
  446. #endif
  447. #define MONO_EMIT_NEW_CLASSCONST(cfg,dr,imm) MONO_EMIT_NEW_AOTCONST(cfg,dr,imm,MONO_PATCH_INFO_CLASS)
  448. #define MONO_EMIT_NEW_VTABLECONST(cfg,dest,vtable) MONO_EMIT_NEW_AOTCONST ((cfg), (dest), (cfg)->compile_aot ? (gpointer)((vtable)->klass) : (vtable), MONO_PATCH_INFO_VTABLE)
  449. #define MONO_EMIT_NEW_SIGNATURECONST(cfg,dr,sig) MONO_EMIT_NEW_AOTCONST ((cfg), (dr), (sig), MONO_PATCH_INFO_SIGNATURE)
  450. #define MONO_EMIT_NEW_VZERO(cfg,dr,kl) do { \
  451. MonoInst *inst; \
  452. MONO_INST_NEW ((cfg), (inst), MONO_CLASS_IS_SIMD (cfg, kl) ? OP_XZERO : OP_VZERO); \
  453. inst->dreg = dr; \
  454. (inst)->type = STACK_VTYPE; \
  455. (inst)->klass = (kl); \
  456. MONO_ADD_INS ((cfg)->cbb, inst); \
  457. } while (0)
  458. #define MONO_EMIT_NEW_UNALU(cfg,op,dr,sr1) do { \
  459. MonoInst *inst; \
  460. EMIT_NEW_UNALU ((cfg), (inst), (op), (dr), (sr1)); \
  461. } while (0)
  462. #define MONO_EMIT_NEW_BIALU(cfg,op,dr,sr1,sr2) do { \
  463. MonoInst *inst; \
  464. MONO_INST_NEW ((cfg), (inst), (op)); \
  465. inst->dreg = dr; \
  466. inst->sreg1 = sr1; \
  467. inst->sreg2 = sr2; \
  468. MONO_ADD_INS (cfg->cbb, inst); \
  469. } while (0)
  470. #define MONO_EMIT_NEW_BIALU_IMM(cfg,op,dr,sr,imm) do { \
  471. MonoInst *inst; \
  472. MONO_INST_NEW ((cfg), (inst), (op)); \
  473. inst->dreg = dr; \
  474. inst->sreg1 = sr; \
  475. inst->inst_imm = (mgreg_t)(imm); \
  476. MONO_ADD_INS (cfg->cbb, inst); \
  477. } while (0)
  478. #define MONO_EMIT_NEW_COMPARE_IMM(cfg,sr1,imm) do { \
  479. MonoInst *inst; \
  480. MONO_INST_NEW ((cfg), (inst), (OP_COMPARE_IMM)); \
  481. inst->sreg1 = sr1; \
  482. inst->inst_imm = (imm); \
  483. MONO_ADD_INS ((cfg)->cbb, inst); \
  484. } while (0)
  485. #define MONO_EMIT_NEW_ICOMPARE_IMM(cfg,sr1,imm) do { \
  486. MonoInst *inst; \
  487. MONO_INST_NEW ((cfg), (inst), sizeof (mgreg_t) == 8 ? OP_ICOMPARE_IMM : OP_COMPARE_IMM); \
  488. inst->sreg1 = sr1; \
  489. inst->inst_imm = (imm); \
  490. MONO_ADD_INS ((cfg)->cbb, inst); \
  491. } while (0)
  492. /* This is used on 32 bit machines too when running with LLVM */
  493. #define MONO_EMIT_NEW_LCOMPARE_IMM(cfg,sr1,imm) do { \
  494. MonoInst *inst; \
  495. MONO_INST_NEW ((cfg), (inst), (OP_LCOMPARE_IMM)); \
  496. inst->sreg1 = sr1; \
  497. if (SIZEOF_REGISTER == 4 && COMPILE_LLVM (cfg)) { \
  498. guint64 _l = (imm); \
  499. inst->inst_imm = _l & 0xffffffff; \
  500. inst->inst_offset = _l >> 32; \
  501. } else { \
  502. inst->inst_imm = (imm); \
  503. } \
  504. MONO_ADD_INS ((cfg)->cbb, inst); \
  505. } while (0)
  506. #define MONO_EMIT_NEW_LOAD_MEMBASE_OP(cfg,op,dr,base,offset) do { \
  507. MonoInst *inst; \
  508. MONO_INST_NEW ((cfg), (inst), (op)); \
  509. inst->dreg = dr; \
  510. inst->inst_basereg = base; \
  511. inst->inst_offset = offset; \
  512. MONO_ADD_INS (cfg->cbb, inst); \
  513. } while (0)
  514. #define MONO_EMIT_NEW_LOAD_MEMBASE(cfg,dr,base,offset) MONO_EMIT_NEW_LOAD_MEMBASE_OP ((cfg), (OP_LOAD_MEMBASE), (dr), (base), (offset))
  515. #define MONO_EMIT_NEW_STORE_MEMBASE(cfg,op,base,offset,sr) do { \
  516. MonoInst *inst; \
  517. MONO_INST_NEW ((cfg), (inst), (op)); \
  518. (inst)->sreg1 = sr; \
  519. (inst)->inst_destbasereg = base; \
  520. (inst)->inst_offset = offset; \
  521. MONO_ADD_INS (cfg->cbb, inst); \
  522. } while (0)
  523. #define MONO_EMIT_NEW_STORE_MEMBASE_IMM(cfg,op,base,offset,imm) do { \
  524. MonoInst *inst; \
  525. MONO_INST_NEW ((cfg), (inst), (op)); \
  526. inst->inst_destbasereg = base; \
  527. inst->inst_offset = offset; \
  528. inst->inst_imm = (mgreg_t)(imm); \
  529. MONO_ADD_INS ((cfg)->cbb, inst); \
  530. } while (0)
  531. #define MONO_EMIT_NEW_COND_EXC(cfg,cond,name) do { \
  532. MonoInst *inst; \
  533. MONO_INST_NEW ((cfg), (inst), (OP_COND_EXC_##cond)); \
  534. inst->inst_p1 = (char*)name; \
  535. MONO_ADD_INS ((cfg)->cbb, inst); \
  536. } while (0)
  537. /* Branch support */
  538. /*
  539. * Basic blocks have two numeric identifiers:
  540. * dfn: Depth First Number
  541. * block_num: unique ID assigned at bblock creation
  542. */
  543. #define NEW_BBLOCK(cfg,bblock) do { \
  544. (bblock) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoBasicBlock)); \
  545. (bblock)->block_num = cfg->num_bblocks++; \
  546. } while (0)
  547. #define ADD_BBLOCK(cfg,b) do { \
  548. if ((b)->cil_code) {\
  549. cfg->cil_offset_to_bb [(b)->cil_code - cfg->cil_start] = (b); \
  550. } \
  551. (b)->real_offset = cfg->real_offset; \
  552. } while (0)
  553. /* Emit a one-way conditional branch */
  554. /*
  555. * The inst_false_bb field of the cond branch will not be set, the JIT code should be
  556. * prepared to deal with this.
  557. */
  558. #ifdef DEBUG_EXTENDED_BBLOCKS
  559. static int ccount = 0;
  560. #define MONO_EMIT_NEW_BRANCH_BLOCK(cfg,op,truebb) do { \
  561. MonoInst *ins; \
  562. MonoBasicBlock *falsebb; \
  563. MONO_INST_NEW ((cfg), (ins), (op)); \
  564. if ((op) == OP_BR) { \
  565. NEW_BBLOCK ((cfg), falsebb); \
  566. ins->inst_target_bb = (truebb); \
  567. mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
  568. MONO_ADD_INS ((cfg)->cbb, ins); \
  569. MONO_START_BB ((cfg), falsebb); \
  570. } else { \
  571. ccount ++; \
  572. ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
  573. ins->inst_true_bb = (truebb); \
  574. ins->inst_false_bb = NULL; \
  575. mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
  576. MONO_ADD_INS ((cfg)->cbb, ins); \
  577. if (getenv ("COUNT2") && ccount == atoi (getenv ("COUNT2")) - 1) { printf ("HIT: %d\n", cfg->cbb->block_num); } \
  578. if (getenv ("COUNT2") && ccount < atoi (getenv ("COUNT2"))) { \
  579. cfg->cbb->extended = TRUE; \
  580. } else { NEW_BBLOCK ((cfg), falsebb); ins->inst_false_bb = (falsebb); mono_link_bblock ((cfg), (cfg)->cbb, (falsebb)); MONO_START_BB ((cfg), falsebb); } \
  581. } \
  582. } while (0)
  583. #else
  584. #define MONO_EMIT_NEW_BRANCH_BLOCK(cfg,op,truebb) do { \
  585. MonoInst *ins; \
  586. MonoBasicBlock *falsebb; \
  587. MONO_INST_NEW ((cfg), (ins), (op)); \
  588. if ((op) == OP_BR) { \
  589. NEW_BBLOCK ((cfg), falsebb); \
  590. ins->inst_target_bb = (truebb); \
  591. mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
  592. MONO_ADD_INS ((cfg)->cbb, ins); \
  593. MONO_START_BB ((cfg), falsebb); \
  594. } else { \
  595. ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
  596. ins->inst_true_bb = (truebb); \
  597. ins->inst_false_bb = NULL; \
  598. mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
  599. MONO_ADD_INS ((cfg)->cbb, ins); \
  600. if (!cfg->enable_extended_bblocks) { \
  601. NEW_BBLOCK ((cfg), falsebb); \
  602. ins->inst_false_bb = falsebb; \
  603. mono_link_bblock ((cfg), (cfg)->cbb, (falsebb)); \
  604. MONO_START_BB ((cfg), falsebb); \
  605. } else { \
  606. cfg->cbb->extended = TRUE; \
  607. } \
  608. } \
  609. } while (0)
  610. #endif
  611. /* Emit a two-way conditional branch */
  612. #define MONO_EMIT_NEW_BRANCH_BLOCK2(cfg,op,truebb,falsebb) do { \
  613. MonoInst *ins; \
  614. MONO_INST_NEW ((cfg), (ins), (op)); \
  615. ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
  616. ins->inst_true_bb = (truebb); \
  617. ins->inst_false_bb = (falsebb); \
  618. mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
  619. mono_link_bblock ((cfg), (cfg)->cbb, (falsebb)); \
  620. MONO_ADD_INS ((cfg)->cbb, ins); \
  621. } while (0)
  622. #define MONO_START_BB(cfg, bblock) do { \
  623. ADD_BBLOCK ((cfg), (bblock)); \
  624. if (cfg->cbb->last_ins && MONO_IS_COND_BRANCH_OP (cfg->cbb->last_ins) && !cfg->cbb->last_ins->inst_false_bb) { \
  625. cfg->cbb->last_ins->inst_false_bb = (bblock); \
  626. mono_link_bblock ((cfg), (cfg)->cbb, (bblock)); \
  627. } else if (! (cfg->cbb->last_ins && ((cfg->cbb->last_ins->opcode == OP_BR) || (cfg->cbb->last_ins->opcode == OP_BR_REG) || MONO_IS_COND_BRANCH_OP (cfg->cbb->last_ins)))) \
  628. mono_link_bblock ((cfg), (cfg)->cbb, (bblock)); \
  629. (cfg)->cbb->next_bb = (bblock); \
  630. (cfg)->cbb = (bblock); \
  631. } while (0)
  632. /* This marks a place in code where an implicit exception could be thrown */
  633. #define MONO_EMIT_NEW_IMPLICIT_EXCEPTION(cfg) do { \
  634. if (COMPILE_LLVM ((cfg))) { \
  635. MONO_EMIT_NEW_UNALU (cfg, OP_IMPLICIT_EXCEPTION, -1, -1); \
  636. } \
  637. } while (0)
  638. /* Loads/Stores which can fault are handled correctly by the LLVM mono branch */
  639. #define MONO_EMIT_NEW_IMPLICIT_EXCEPTION_LOAD_STORE(cfg) do { \
  640. if (COMPILE_LLVM (cfg) && !IS_LLVM_MONO_BRANCH) \
  641. MONO_EMIT_NEW_IMPLICIT_EXCEPTION ((cfg)); \
  642. } while (0)
  643. /* Emit an explicit null check which doesn't depend on SIGSEGV signal handling */
  644. #define MONO_EMIT_NULL_CHECK(cfg, reg) do { \
  645. if (cfg->explicit_null_checks) { \
  646. MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, (reg), 0); \
  647. MONO_EMIT_NEW_COND_EXC (cfg, EQ, "NullReferenceException"); \
  648. } else { \
  649. MONO_EMIT_NEW_IMPLICIT_EXCEPTION_LOAD_STORE (cfg); \
  650. } \
  651. } while (0)
  652. #define MONO_EMIT_NEW_CHECK_THIS(cfg, sreg) do { \
  653. cfg->flags |= MONO_CFG_HAS_CHECK_THIS; \
  654. if (cfg->explicit_null_checks) { \
  655. MONO_EMIT_NULL_CHECK (cfg, sreg); \
  656. } else { \
  657. MONO_EMIT_NEW_UNALU (cfg, OP_CHECK_THIS, -1, sreg); \
  658. MONO_EMIT_NEW_IMPLICIT_EXCEPTION_LOAD_STORE (cfg); \
  659. } \
  660. MONO_EMIT_NEW_UNALU (cfg, OP_NOT_NULL, -1, sreg); \
  661. } while (0)
  662. #define NEW_LOAD_MEMBASE_FLAGS(cfg,dest,op,dr,base,offset,ins_flags) do { \
  663. int __ins_flags = ins_flags; \
  664. if (__ins_flags & MONO_INST_FAULT) { \
  665. MONO_EMIT_NULL_CHECK ((cfg), (base)); \
  666. if (cfg->explicit_null_checks) \
  667. __ins_flags &= ~MONO_INST_FAULT; \
  668. } \
  669. NEW_LOAD_MEMBASE ((cfg), (dest), (op), (dr), (base), (offset)); \
  670. (dest)->flags = (__ins_flags); \
  671. } while (0)
  672. #define MONO_EMIT_NEW_LOAD_MEMBASE_OP_FLAGS(cfg,op,dr,base,offset,ins_flags) do { \
  673. MonoInst *inst; \
  674. int __ins_flags = ins_flags; \
  675. if (__ins_flags & MONO_INST_FAULT) { \
  676. MONO_EMIT_NULL_CHECK ((cfg), (base)); \
  677. if (cfg->explicit_null_checks) \
  678. __ins_flags &= ~MONO_INST_FAULT; \
  679. } \
  680. NEW_LOAD_MEMBASE ((cfg), (inst), (op), (dr), (base), (offset)); \
  681. inst->flags = (__ins_flags); \
  682. MONO_ADD_INS (cfg->cbb, inst); \
  683. } while (0)
  684. #define MONO_EMIT_NEW_LOAD_MEMBASE_FLAGS(cfg,dr,base,offset,ins_flags) MONO_EMIT_NEW_LOAD_MEMBASE_OP_FLAGS ((cfg), (OP_LOAD_MEMBASE), (dr), (base), (offset),(ins_flags))
  685. /* A load which can cause a nullref */
  686. #define NEW_LOAD_MEMBASE_FAULT(cfg,dest,op,dr,base,offset) NEW_LOAD_MEMBASE_FLAGS ((cfg), (dest), (op), (dr), (base), (offset), MONO_INST_FAULT)
  687. #define EMIT_NEW_LOAD_MEMBASE_FAULT(cfg,dest,op,dr,base,offset) do { \
  688. NEW_LOAD_MEMBASE_FAULT ((cfg), (dest), (op), (dr), (base), (offset)); \
  689. MONO_ADD_INS ((cfg)->cbb, (dest)); \
  690. } while (0)
  691. #define MONO_EMIT_NEW_LOAD_MEMBASE_OP_FAULT(cfg,op,dr,base,offset) MONO_EMIT_NEW_LOAD_MEMBASE_OP_FLAGS ((cfg), (op), (dr), (base), (offset), MONO_INST_FAULT)
  692. #define MONO_EMIT_NEW_LOAD_MEMBASE_FAULT(cfg,dr,base,offset) MONO_EMIT_NEW_LOAD_MEMBASE_OP_FAULT ((cfg), (OP_LOAD_MEMBASE), (dr), (base), (offset))
  693. /*Object Model related macros*/
  694. /* Default bounds check implementation for most architectures + llvm */
  695. #define MONO_EMIT_DEFAULT_BOUNDS_CHECK(cfg, array_reg, offset, index_reg, fault) do { \
  696. int _length_reg = alloc_ireg (cfg); \
  697. if (fault) \
  698. MONO_EMIT_NEW_LOAD_MEMBASE_OP_FAULT (cfg, OP_LOADI4_MEMBASE, _length_reg, array_reg, offset); \
  699. else \
  700. MONO_EMIT_NEW_LOAD_MEMBASE_OP_FLAGS (cfg, OP_LOADI4_MEMBASE, _length_reg, array_reg, offset, MONO_INST_CONSTANT_LOAD); \
  701. MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, _length_reg, index_reg); \
  702. MONO_EMIT_NEW_COND_EXC (cfg, LE_UN, "IndexOutOfRangeException"); \
  703. } while (0)
  704. #ifndef MONO_ARCH_EMIT_BOUNDS_CHECK
  705. #define MONO_ARCH_EMIT_BOUNDS_CHECK(cfg, array_reg, offset, index_reg) MONO_EMIT_DEFAULT_BOUNDS_CHECK ((cfg), (array_reg), (offset), (index_reg), TRUE)
  706. #endif
  707. /* cfg is the MonoCompile been used
  708. * array_reg is the vreg holding the array object
  709. * array_type is a struct (usually MonoArray or MonoString)
  710. * array_length_field is the field in the previous struct with the length
  711. * index_reg is the vreg holding the index
  712. */
  713. #define MONO_EMIT_BOUNDS_CHECK(cfg, array_reg, array_type, array_length_field, index_reg) do { \
  714. if (!(cfg->opt & MONO_OPT_UNSAFE)) { \
  715. if (!(cfg->opt & MONO_OPT_ABCREM)) { \
  716. MONO_EMIT_NULL_CHECK (cfg, array_reg); \
  717. if (COMPILE_LLVM (cfg)) \
  718. MONO_EMIT_DEFAULT_BOUNDS_CHECK ((cfg), (array_reg), G_STRUCT_OFFSET (array_type, array_length_field), (index_reg), TRUE); \
  719. else \
  720. MONO_ARCH_EMIT_BOUNDS_CHECK ((cfg), (array_reg), G_STRUCT_OFFSET (array_type, array_length_field), (index_reg)); \
  721. } else { \
  722. MonoInst *ins; \
  723. MONO_INST_NEW ((cfg), ins, OP_BOUNDS_CHECK); \
  724. ins->sreg1 = array_reg; \
  725. ins->sreg2 = index_reg; \
  726. ins->inst_imm = G_STRUCT_OFFSET (array_type, array_length_field); \
  727. ins->flags |= MONO_INST_FAULT; \
  728. MONO_ADD_INS ((cfg)->cbb, ins); \
  729. (cfg)->flags |= MONO_CFG_HAS_ARRAY_ACCESS; \
  730. (cfg)->cbb->has_array_access = TRUE; \
  731. } \
  732. } \
  733. } while (0)
  734. G_END_DECLS
  735. #endif