jit_ir.h 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872
  1. /*
  2. * Copyright (C) 2021 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #ifndef _JIT_IR_H_
  6. #define _JIT_IR_H_
  7. #include "bh_platform.h"
  8. #include "../interpreter/wasm.h"
  9. #include "jit_utils.h"
  10. #ifdef __cplusplus
  11. extern "C" {
  12. #endif
  13. /**
  14. * Register (operand) representation of JIT IR.
  15. *
  16. * Encoding: [4-bit: kind, 28-bit register no.]
  17. *
  18. * Registers in JIT IR are classified into different kinds according
  19. * to types of values they can hold. The classification is based on
  20. * most processors' hardware register classifications, which include
  21. * various sets of integer, floating point and vector registers with
  22. * different sizes. These registers can be mapped onto corresponding
  23. * kinds of hardware registers by register allocator. Instructions
  24. * can only operate on allowed kinds of registers. For example, an
  25. * integer instruction cannot operate on floating point or vector
  26. * registers. Some encodings of these kinds of registers also
  27. * represent immediate constant values and indexes to constant tables
  28. * (see below). In that case, those registers are read-only. Writing
  29. * to them is illegal. Reading from an immediate constant value
  30. * register always returns the constant value encoded in the register
  31. * no. Reading from a constant table index register always returns
  32. * the constant value stored at the encoded index of the constant
  33. * table of the register's kind. Immediate constant values and values
  34. * indexed by constant table indexes can only be loaded into the
  35. * corresponding kinds of registers if they must be loaded into
  36. * registers. Besides these common kinds of registers, labels of
  37. * basic blocks are also treated as registers of a special kind, which
  38. * hold code addresses of basic block labels and are read-only. Each
  39. * basic block is assigned one unique label register. With this
  40. * unification, we can use the same set of load instructions to load
  41. * values either from addresses stored in normal registers or from
  42. * addresses of labels. Besides these register kinds, the void kind
  43. * is a special kind of registers to denote some error occurs when a
  44. * normal register is expected. Or it can be used as result operand
  45. * of call and invoke instructions to denote no return values. The
  46. * variable registers are classified into two sets: the hard registers
  47. * whose register numbers are less than the hard register numbers of
  48. * their kinds and the virtual registers whose register numbers are
  49. * greater than or equal to the hard register numbers. Before
  50. * register allocation is done, hard registers may appear in the IR
  51. * due to special usages of passes frontend (e.g. fp_reg and exec_env_reg)
  52. * or lower_cg. In the mean time (including during register
  53. * allocation), those hard registers are treated same as virtual
  54. * registers except that they may not be SSA and they can only be
  55. * allocated to the hard registers of themselves.
  56. *
  57. * Classification of registers:
  58. * + void register (kind == JIT_REG_KIND_VOID, no. must be 0)
  59. * + label registers (kind == JIT_REG_KIND_L32)
  60. * + value registers (kind == JIT_REG_KIND_I32/I64/F32/F64/V64/V128/V256)
  61. * | + constants (_JIT_REG_CONST_VAL_FLAG | _JIT_REG_CONST_IDX_FLAG)
  62. * | | + constant values (_JIT_REG_CONST_VAL_FLAG)
  63. * | | + constant indexes (_JIT_REG_CONST_IDX_FLAG)
  64. * | + variables (!(_JIT_REG_CONST_VAL_FLAG | _JIT_REG_CONST_IDX_FLAG))
  65. * | | + hard registers (no. < hard register number)
  66. * | | + virtual registers (no. >= hard register number)
  67. */
  68. typedef uint32 JitReg;
  69. /*
  70. * Mask and shift bits of register kind.
  71. */
  72. #define _JIT_REG_KIND_MASK 0xf0000000
  73. #define _JIT_REG_KIND_SHIFT 28
  74. /*
  75. * Mask of register no. which must be the least significant bits.
  76. */
  77. #define _JIT_REG_NO_MASK (~_JIT_REG_KIND_MASK)
  78. /*
  79. * Constant value flag (the most significant bit) of register
  80. * no. field of integer, floating point and vector registers. If this
  81. * flag is set in the register no., the rest bits of register
  82. * no. represent a signed (27-bit) integer constant value of the
  83. * corresponding type of the register and the register is read-only.
  84. */
  85. #define _JIT_REG_CONST_VAL_FLAG ((_JIT_REG_NO_MASK >> 1) + 1)
  86. /*
  87. * Constant index flag of non-constant-value (constant value flag is
  88. * not set in register no. field) integer, floating point and vector
  89. * regisers. If this flag is set, the rest bits of the register
  90. * no. represent an index to the constant value table of the
  91. * corresponding type of the register and the register is read-only.
  92. */
  93. #define _JIT_REG_CONST_IDX_FLAG (_JIT_REG_CONST_VAL_FLAG >> 1)
  94. /**
  95. * Register kinds. Don't change the order of the defined values. The
  96. * L32 kind must be after all normal kinds (see _const_val and _reg_ann
  97. * of JitCompContext).
  98. */
  99. typedef enum JitRegKind {
  100. JIT_REG_KIND_VOID = 0x00, /* void type */
  101. JIT_REG_KIND_I32 = 0x01, /* 32-bit signed or unsigned integer */
  102. JIT_REG_KIND_I64 = 0x02, /* 64-bit signed or unsigned integer */
  103. JIT_REG_KIND_F32 = 0x03, /* 32-bit floating point */
  104. JIT_REG_KIND_F64 = 0x04, /* 64-bit floating point */
  105. JIT_REG_KIND_V64 = 0x05, /* 64-bit vector */
  106. JIT_REG_KIND_V128 = 0x06, /* 128-bit vector */
  107. JIT_REG_KIND_V256 = 0x07, /* 256-bit vector */
  108. JIT_REG_KIND_L32 = 0x08, /* 32-bit label address */
  109. JIT_REG_KIND_NUM /* number of register kinds */
  110. } JitRegKind;
  111. #if UINTPTR_MAX == UINT64_MAX
  112. #define JIT_REG_KIND_PTR JIT_REG_KIND_I64
  113. #else
  114. #define JIT_REG_KIND_PTR JIT_REG_KIND_I32
  115. #endif
  116. /**
  117. * Construct a new JIT IR register from the kind and no.
  118. *
  119. * @param reg_kind register kind
  120. * @param reg_no register no.
  121. *
  122. * @return the new register with the given kind and no.
  123. */
  124. static inline JitReg
  125. jit_reg_new(unsigned reg_kind, unsigned reg_no)
  126. {
  127. return (JitReg)((reg_kind << _JIT_REG_KIND_SHIFT) | reg_no);
  128. }
  129. /**
  130. * Get the register kind of the given register.
  131. *
  132. * @param r a JIT IR register
  133. *
  134. * @return the register kind of register r
  135. */
  136. static inline int
  137. jit_reg_kind(JitReg r)
  138. {
  139. return (r & _JIT_REG_KIND_MASK) >> _JIT_REG_KIND_SHIFT;
  140. }
  141. /**
  142. * Get the register no. of the given JIT IR register.
  143. *
  144. * @param r a JIT IR register
  145. *
  146. * @return the register no. of register r
  147. */
  148. static inline int
  149. jit_reg_no(JitReg r)
  150. {
  151. return r & _JIT_REG_NO_MASK;
  152. }
  153. /**
  154. * Check whether the given register is a normal value register.
  155. *
  156. * @param r a JIT IR register
  157. *
  158. * @return true iff the register is a normal value register
  159. */
  160. static inline bool
  161. jit_reg_is_value(JitReg r)
  162. {
  163. unsigned kind = jit_reg_kind(r);
  164. return kind > JIT_REG_KIND_VOID && kind < JIT_REG_KIND_L32;
  165. }
  166. /**
  167. * Check whether the given register is a constant value.
  168. *
  169. * @param r a JIT IR register
  170. *
  171. * @return true iff register r is a constant value
  172. */
  173. static inline bool
  174. jit_reg_is_const_val(JitReg r)
  175. {
  176. return jit_reg_is_value(r) && (r & _JIT_REG_CONST_VAL_FLAG);
  177. }
  178. /**
  179. * Check whether the given register is a constant table index.
  180. *
  181. * @param r a JIT IR register
  182. *
  183. * @return true iff register r is a constant table index
  184. */
  185. static inline bool
  186. jit_reg_is_const_idx(JitReg r)
  187. {
  188. return (jit_reg_is_value(r) && !jit_reg_is_const_val(r)
  189. && (r & _JIT_REG_CONST_IDX_FLAG));
  190. }
  191. /**
  192. * Check whether the given register is a constant.
  193. *
  194. * @param r a JIT IR register
  195. *
  196. * @return true iff register r is a constant
  197. */
  198. static inline bool
  199. jit_reg_is_const(JitReg r)
  200. {
  201. return (jit_reg_is_value(r)
  202. && (r & (_JIT_REG_CONST_VAL_FLAG | _JIT_REG_CONST_IDX_FLAG)));
  203. }
  204. /**
  205. * Check whether the given register is a normal variable register.
  206. *
  207. * @param r a JIT IR register
  208. *
  209. * @return true iff the register is a normal variable register
  210. */
  211. static inline bool
  212. jit_reg_is_variable(JitReg r)
  213. {
  214. return (jit_reg_is_value(r)
  215. && !(r & (_JIT_REG_CONST_VAL_FLAG | _JIT_REG_CONST_IDX_FLAG)));
  216. }
  217. /**
  218. * Test whether the register is the given kind.
  219. *
  220. * @param KIND register kind name
  221. * @param R register
  222. *
  223. * @return true if the register is the given kind
  224. */
  225. #define jit_reg_is_kind(KIND, R) (jit_reg_kind(R) == JIT_REG_KIND_##KIND)
  226. /**
  227. * Construct a zero IR register with given the kind.
  228. *
  229. * @param kind the kind of the value
  230. *
  231. * @return a constant register of zero
  232. */
  233. static inline JitReg
  234. jit_reg_new_zero(unsigned kind)
  235. {
  236. bh_assert(kind != JIT_REG_KIND_VOID && kind < JIT_REG_KIND_L32);
  237. return jit_reg_new(kind, _JIT_REG_CONST_VAL_FLAG);
  238. }
  239. /**
  240. * Test whether the register is a zero constant value.
  241. *
  242. * @param reg an IR register
  243. *
  244. * @return true iff the register is a constant zero
  245. */
  246. static inline JitReg
  247. jit_reg_is_zero(JitReg reg)
  248. {
  249. return (jit_reg_is_value(reg)
  250. && jit_reg_no(reg) == _JIT_REG_CONST_VAL_FLAG);
  251. }
  252. /**
  253. * Operand of instructions with fixed-number register operand(s).
  254. */
  255. typedef JitReg JitOpndReg;
  256. /**
  257. * Operand of instructions with variable-number register operand(s).
  258. */
  259. typedef struct JitOpndVReg {
  260. uint32 _reg_num;
  261. JitReg _reg[1];
  262. } JitOpndVReg;
  263. /**
  264. * Operand of lookupswitch instruction.
  265. */
  266. typedef struct JitOpndLookupSwitch {
  267. /* NOTE: distance between JitReg operands must be the same (see
  268. jit_insn_opnd_regs). */
  269. JitReg value; /* the value to be compared */
  270. uint32 match_pairs_num; /* match pairs number */
  271. /* NOTE: offset between adjacent targets must be sizeof
  272. (match_pairs[0]) (see implementation of jit_basic_block_succs),
  273. so the default_target field must be here. */
  274. JitReg default_target; /* default target BB */
  275. struct {
  276. int32 value; /* match value of the match pair */
  277. JitReg target; /* target BB of the match pair */
  278. } match_pairs[1]; /* match pairs of the instruction */
  279. } JitOpndLookupSwitch;
  280. /**
  281. * Instruction of JIT IR.
  282. */
  283. typedef struct JitInsn {
  284. /* Pointers to the previous and next instructions. */
  285. struct JitInsn *prev;
  286. struct JitInsn *next;
  287. /* Opcode of the instruction. */
  288. uint16 opcode;
  289. /* Reserved field that may be used by optimizations locally. */
  290. uint8 flags_u8;
  291. /* The unique ID of the instruction. */
  292. uint16 uid;
  293. /* Operands for different kinds of instructions. */
  294. union {
  295. /* For instructions with fixed-number register operand(s). */
  296. JitOpndReg _opnd_Reg[1];
  297. /* For instructions with variable-number register operand(s). */
  298. JitOpndVReg _opnd_VReg;
  299. /* For lookupswitch instruction. */
  300. JitOpndLookupSwitch _opnd_LookupSwitch;
  301. } _opnd;
  302. } JitInsn;
  303. /**
  304. * Opcodes of IR instructions.
  305. */
  306. typedef enum JitOpcode {
  307. #define INSN(NAME, OPND_KIND, OPND_NUM, FIRST_USE) JIT_OP_##NAME,
  308. #include "jit_ir.def"
  309. #undef INSN
  310. JIT_OP_OPCODE_NUMBER
  311. } JitOpcode;
  312. /*
  313. * Helper functions for creating new instructions. Don't call them
  314. * directly. Use jit_insn_new_NAME, such as jit_insn_new_MOV instead.
  315. */
  316. JitInsn *
  317. _jit_insn_new_Reg_1(JitOpcode opc, JitReg r0);
  318. JitInsn *
  319. _jit_insn_new_Reg_2(JitOpcode opc, JitReg r0, JitReg r1);
  320. JitInsn *
  321. _jit_insn_new_Reg_3(JitOpcode opc, JitReg r0, JitReg r1, JitReg r2);
  322. JitInsn *
  323. _jit_insn_new_Reg_4(JitOpcode opc, JitReg r0, JitReg r1, JitReg r2, JitReg r3);
  324. JitInsn *
  325. _jit_insn_new_Reg_5(JitOpcode opc, JitReg r0, JitReg r1, JitReg r2, JitReg r3,
  326. JitReg r4);
  327. JitInsn *
  328. _jit_insn_new_VReg_1(JitOpcode opc, JitReg r0, int n);
  329. JitInsn *
  330. _jit_insn_new_VReg_2(JitOpcode opc, JitReg r0, JitReg r1, int n);
  331. JitInsn *
  332. _jit_insn_new_LookupSwitch_1(JitOpcode opc, JitReg value, uint32 num);
  333. /*
  334. * Instruction creation functions jit_insn_new_NAME, where NAME is the
  335. * name of the instruction defined in jit_ir.def.
  336. */
  337. #define ARG_DECL_Reg_1 JitReg r0
  338. #define ARG_LIST_Reg_1 r0
  339. #define ARG_DECL_Reg_2 JitReg r0, JitReg r1
  340. #define ARG_LIST_Reg_2 r0, r1
  341. #define ARG_DECL_Reg_3 JitReg r0, JitReg r1, JitReg r2
  342. #define ARG_LIST_Reg_3 r0, r1, r2
  343. #define ARG_DECL_Reg_4 JitReg r0, JitReg r1, JitReg r2, JitReg r3
  344. #define ARG_LIST_Reg_4 r0, r1, r2, r3
  345. #define ARG_DECL_Reg_5 JitReg r0, JitReg r1, JitReg r2, JitReg r3, JitReg r4
  346. #define ARG_LIST_Reg_5 r0, r1, r2, r3, r4
  347. #define ARG_DECL_VReg_1 JitReg r0, int n
  348. #define ARG_LIST_VReg_1 r0, n
  349. #define ARG_DECL_VReg_2 JitReg r0, JitReg r1, int n
  350. #define ARG_LIST_VReg_2 r0, r1, n
  351. #define ARG_DECL_LookupSwitch_1 JitReg value, uint32 num
  352. #define ARG_LIST_LookupSwitch_1 value, num
  353. #define INSN(NAME, OPND_KIND, OPND_NUM, FIRST_USE) \
  354. static inline JitInsn *jit_insn_new_##NAME( \
  355. ARG_DECL_##OPND_KIND##_##OPND_NUM) \
  356. { \
  357. return _jit_insn_new_##OPND_KIND##_##OPND_NUM( \
  358. JIT_OP_##NAME, ARG_LIST_##OPND_KIND##_##OPND_NUM); \
  359. }
  360. #include "jit_ir.def"
  361. #undef INSN
  362. #undef ARG_DECL_Reg_1
  363. #undef ARG_LIST_Reg_1
  364. #undef ARG_DECL_Reg_2
  365. #undef ARG_LIST_Reg_2
  366. #undef ARG_DECL_Reg_3
  367. #undef ARG_LIST_Reg_3
  368. #undef ARG_DECL_Reg_4
  369. #undef ARG_LIST_Reg_4
  370. #undef ARG_DECL_Reg_5
  371. #undef ARG_LIST_Reg_5
  372. #undef ARG_DECL_VReg_1
  373. #undef ARG_LIST_VReg_1
  374. #undef ARG_DECL_VReg_2
  375. #undef ARG_LIST_VReg_2
  376. #undef ARG_DECL_LookupSwitch_1
  377. #undef ARG_LIST_LookupSwitch_1
  378. /**
  379. * Delete an instruction
  380. *
  381. * @param insn an instruction to be deleted
  382. */
  383. static inline void
  384. jit_insn_delete(JitInsn *insn)
  385. {
  386. jit_free(insn);
  387. }
  388. /*
  389. * Runtime type check functions that check whether accessing the n-th
  390. * operand is legal. They are only used for in self-verification
  391. * mode.
  392. *
  393. * @param insn any JIT IR instruction
  394. * @param n index of the operand to access
  395. *
  396. * @return true if the access is legal
  397. */
  398. bool
  399. _jit_insn_check_opnd_access_Reg(const JitInsn *insn, unsigned n);
  400. bool
  401. _jit_insn_check_opnd_access_VReg(const JitInsn *insn, unsigned n);
  402. bool
  403. _jit_insn_check_opnd_access_LookupSwitch(const JitInsn *insn);
  404. /**
  405. * Get the pointer to the n-th register operand of the given
  406. * instruction. The instruction format must be Reg.
  407. *
  408. * @param insn a Reg format instruction
  409. * @param n index of the operand to get
  410. *
  411. * @return pointer to the n-th operand
  412. */
  413. static inline JitReg *
  414. jit_insn_opnd(JitInsn *insn, int n)
  415. {
  416. bh_assert(_jit_insn_check_opnd_access_Reg(insn, n));
  417. return &insn->_opnd._opnd_Reg[n];
  418. }
  419. /**
  420. * Get the pointer to the n-th register operand of the given
  421. * instruction. The instruction format must be VReg.
  422. *
  423. * @param insn a VReg format instruction
  424. * @param n index of the operand to get
  425. *
  426. * @return pointer to the n-th operand
  427. */
  428. static inline JitReg *
  429. jit_insn_opndv(JitInsn *insn, int n)
  430. {
  431. bh_assert(_jit_insn_check_opnd_access_VReg(insn, n));
  432. return &insn->_opnd._opnd_VReg._reg[n];
  433. }
  434. /**
  435. * Get the operand number of the given instruction. The instruction
  436. * format must be VReg.
  437. *
  438. * @param insn a VReg format instruction
  439. *
  440. * @return operand number of the instruction
  441. */
  442. static inline unsigned
  443. jit_insn_opndv_num(const JitInsn *insn)
  444. {
  445. bh_assert(_jit_insn_check_opnd_access_VReg(insn, 0));
  446. return insn->_opnd._opnd_VReg._reg_num;
  447. }
  448. /**
  449. * Get the pointer to the LookupSwitch operand of the given
  450. * instruction. The instruction format must be LookupSwitch.
  451. *
  452. * @param insn a LookupSwitch format instruction
  453. *
  454. * @return pointer to the operand
  455. */
  456. static inline JitOpndLookupSwitch *
  457. jit_insn_opndls(JitInsn *insn)
  458. {
  459. bh_assert(_jit_insn_check_opnd_access_LookupSwitch(insn));
  460. return &insn->_opnd._opnd_LookupSwitch;
  461. }
  462. /**
  463. * Insert instruction @p insn2 before instruction @p insn1.
  464. *
  465. * @param insn1 any instruction
  466. * @param insn2 any instruction
  467. */
  468. void
  469. jit_insn_insert_before(JitInsn *insn1, JitInsn *insn2);
  470. /**
  471. * Insert instruction @p insn2 after instruction @p insn1.
  472. *
  473. * @param insn1 any instruction
  474. * @param insn2 any instruction
  475. */
  476. void
  477. jit_insn_insert_after(JitInsn *insn1, JitInsn *insn2);
  478. /**
  479. * Unlink the instruction @p insn from the containing list.
  480. *
  481. * @param insn an instruction
  482. */
  483. void
  484. jit_insn_unlink(JitInsn *insn);
  485. /**
  486. * Get the hash value of the comparable instruction (pure functions
  487. * and exception check instructions).
  488. *
  489. * @param insn an instruction
  490. *
  491. * @return hash value of the instruction
  492. */
  493. unsigned
  494. jit_insn_hash(JitInsn *insn);
  495. /**
  496. * Compare whether the two comparable instructions are the same.
  497. *
  498. * @param insn1 the first instruction
  499. * @param insn2 the second instruction
  500. *
  501. * @return true if the two instructions are the same
  502. */
  503. bool
  504. jit_insn_equal(JitInsn *insn1, JitInsn *insn2);
  505. /**
  506. * Register vector for accessing predecessors and successors of a
  507. * basic block.
  508. */
  509. typedef struct JitRegVec {
  510. JitReg *_base; /* points to the first register */
  511. int32 _stride; /* stride to the next register */
  512. uint32 num; /* number of registers */
  513. } JitRegVec;
  514. /**
  515. * Get the address of the i-th register in the register vector.
  516. *
  517. * @param vec a register vector
  518. * @param i index to the register vector
  519. *
  520. * @return the address of the i-th register in the vector
  521. */
  522. static inline JitReg *
  523. jit_reg_vec_at(const JitRegVec *vec, unsigned i)
  524. {
  525. bh_assert(i < vec->num);
  526. return vec->_base + vec->_stride * i;
  527. }
  528. /**
  529. * Visit each element in a register vector.
  530. *
  531. * @param V (JitRegVec) the register vector
  532. * @param I (unsigned) index variable in the vector
  533. * @param R (JitReg *) resiger pointer variable
  534. */
  535. #define JIT_REG_VEC_FOREACH(V, I, R) \
  536. for ((I) = 0, (R) = (V)._base; (I) < (V).num; (I)++, (R) += (V)._stride)
  537. /**
  538. * Visit each register defined by an instruction.
  539. *
  540. * @param V (JitRegVec) register vector of the instruction
  541. * @param I (unsigned) index variable in the vector
  542. * @param R (JitReg *) resiger pointer variable
  543. * @param F index of the first used register
  544. */
  545. #define JIT_REG_VEC_FOREACH_DEF(V, I, R, F) \
  546. for ((I) = 0, (R) = (V)._base; (I) < (F); (I)++, (R) += (V)._stride)
  547. /**
  548. * Visit each register used by an instruction.
  549. *
  550. * @param V (JitRegVec) register vector of the instruction
  551. * @param I (unsigned) index variable in the vector
  552. * @param R (JitReg *) resiger pointer variable
  553. * @param F index of the first used register
  554. */
  555. #define JIT_REG_VEC_FOREACH_USE(V, I, R, F) \
  556. for ((I) = (F), (R) = (V)._base + (F) * (V)._stride; (I) < (V).num; \
  557. (I)++, (R) += (V)._stride)
  558. /**
  559. * Get a generic register vector that contains all register operands.
  560. * The registers defined by the instruction, if any, appear before the
  561. * registers used by the instruction.
  562. *
  563. * @param insn an instruction
  564. *
  565. * @return a register vector containing register operands
  566. */
  567. JitRegVec
  568. jit_insn_opnd_regs(JitInsn *insn);
  569. /**
  570. * Get the index of the first use register in the register vector
  571. * returned by jit_insn_opnd_regs.
  572. *
  573. * @param insn an instruction
  574. *
  575. * @return the index of the first use register in the register vector
  576. */
  577. unsigned
  578. jit_insn_opnd_first_use(JitInsn *insn);
  579. /**
  580. * Basic Block of JIT IR. It is a basic block only if the IR is not in
  581. * non-BB form. The block is represented by a special phi node, whose
  582. * result and arguments are label registers. The result label is the
  583. * containing block's label. The arguments are labels of predecessors
  584. * of the block. Successor labels are stored in the last instruction,
  585. * which must be a control flow instruction. Instructions of a block
  586. * are linked in a circular linked list with the block phi node as the
  587. * end of the list. The next and prev field of the block phi node
  588. * point to the first and last instructions of the block.
  589. */
  590. typedef JitInsn JitBasicBlock;
  591. /**
  592. * Create a new basic block instance.
  593. *
  594. * @param label the label of the new basic block
  595. * @param n number of predecessors
  596. *
  597. * @return the created new basic block instance
  598. */
  599. JitBasicBlock *
  600. jit_basic_block_new(JitReg label, int n);
  601. /**
  602. * Delete a basic block instance and all instructions init.
  603. *
  604. * @param block the basic block to be deleted
  605. */
  606. void
  607. jit_basic_block_delete(JitBasicBlock *block);
  608. /**
  609. * Get the label of the basic block.
  610. *
  611. * @param block a basic block instance
  612. *
  613. * @return the label of the basic block
  614. */
  615. static inline JitReg
  616. jit_basic_block_label(JitBasicBlock *block)
  617. {
  618. return *(jit_insn_opndv(block, 0));
  619. }
  620. /**
  621. * Get the first instruction of the basic block.
  622. *
  623. * @param block a basic block instance
  624. *
  625. * @return the first instruction of the basic block
  626. */
  627. static inline JitInsn *
  628. jit_basic_block_first_insn(JitBasicBlock *block)
  629. {
  630. return block->next;
  631. }
  632. /**
  633. * Get the last instruction of the basic block.
  634. *
  635. * @param block a basic block instance
  636. *
  637. * @return the last instruction of the basic block
  638. */
  639. static inline JitInsn *
  640. jit_basic_block_last_insn(JitBasicBlock *block)
  641. {
  642. return block->prev;
  643. }
  644. /**
  645. * Get the end of instruction list of the basic block (which is always
  646. * the block itself).
  647. *
  648. * @param block a basic block instance
  649. *
  650. * @return the end of instruction list of the basic block
  651. */
  652. static inline JitInsn *
  653. jit_basic_block_end_insn(JitBasicBlock *block)
  654. {
  655. return block;
  656. }
  657. /**
  658. * Visit each instruction in the block from the first to the last. In
  659. * the code block, the instruction pointer @p I must be a valid
  660. * pointer to an instruction in the block. That means if the
  661. * instruction may be deleted, @p I must point to the previous or next
  662. * valid instruction before the next iteration.
  663. *
  664. * @param B (JitBasicBlock *) the block
  665. * @param I (JitInsn *) instruction visited
  666. */
  667. #define JIT_FOREACH_INSN(B, I) \
  668. for (I = jit_basic_block_first_insn(B); I != jit_basic_block_end_insn(B); \
  669. I = I->next)
  670. /**
  671. * Visit each instruction in the block from the last to the first. In
  672. * the code block, the instruction pointer @p I must be a valid
  673. * pointer to an instruction in the block. That means if the
  674. * instruction may be deleted, @p I must point to the previous or next
  675. * valid instruction before the next iteration.
  676. *
  677. * @param B (JitBasicBlock *) the block
  678. * @param I (JitInsn *) instruction visited
  679. */
  680. #define JIT_FOREACH_INSN_REVERSE(B, I) \
  681. for (I = jit_basic_block_last_insn(B); I != jit_basic_block_end_insn(B); \
  682. I = I->prev)
  683. /**
  684. * Prepend an instruction in the front of the block. The position is
  685. * just after the block phi node (the block instance itself).
  686. *
  687. * @param block a block
  688. * @param insn an instruction to be prepended
  689. */
  690. static inline void
  691. jit_basic_block_prepend_insn(JitBasicBlock *block, JitInsn *insn)
  692. {
  693. jit_insn_insert_after(block, insn);
  694. }
  695. /**
  696. * Append an instruction to the end of the basic block.
  697. *
  698. * @param block a basic block
  699. * @param insn an instruction to be appended
  700. */
  701. static inline void
  702. jit_basic_block_append_insn(JitBasicBlock *block, JitInsn *insn)
  703. {
  704. jit_insn_insert_before(block, insn);
  705. }
  706. /**
  707. * Get the register vector of predecessors of a basic block.
  708. *
  709. * @param block a JIT IR block
  710. *
  711. * @return register vector of the predecessors
  712. */
  713. JitRegVec
  714. jit_basic_block_preds(JitBasicBlock *block);
  715. /**
  716. * Get the register vector of successors of a basic block.
  717. *
  718. * @param block a JIT IR basic block
  719. *
  720. * @return register vector of the successors
  721. */
  722. JitRegVec
  723. jit_basic_block_succs(JitBasicBlock *block);
  724. /**
  725. * Hard register information of one kind.
  726. */
  727. typedef struct JitHardRegInfo {
  728. struct {
  729. /* Hard register number of this kind. */
  730. uint32 num;
  731. /* Whether each register is fixed. */
  732. const uint8 *fixed;
  733. /* Whether each register is caller-saved in the native ABI. */
  734. const uint8 *caller_saved_native;
  735. /* Whether each register is caller-saved in the JITed ABI. */
  736. const uint8 *caller_saved_jitted;
  737. } info[JIT_REG_KIND_L32];
  738. /* The indexes of hard registers of frame pointer, exec_env and cmp. */
  739. uint32 fp_hreg_index;
  740. uint32 exec_env_hreg_index;
  741. uint32 cmp_hreg_index;
  742. } JitHardRegInfo;
  743. struct JitBlock;
  744. struct JitCompContext;
  745. struct JitValueSlot;
  746. /**
  747. * Value in the WASM operation stack, each stack element
  748. * is a Jit register
  749. */
  750. typedef struct JitValue {
  751. struct JitValue *next;
  752. struct JitValue *prev;
  753. struct JitValueSlot *value;
  754. /* VALUE_TYPE_I32/I64/F32/F64/VOID */
  755. uint8 type;
  756. } JitValue;
  757. /**
  758. * Value stack, represents stack elements in a WASM block
  759. */
  760. typedef struct JitValueStack {
  761. JitValue *value_list_head;
  762. JitValue *value_list_end;
  763. } JitValueStack;
  764. /* Record information of a value slot of local variable or stack
  765. during translation. */
  766. typedef struct JitValueSlot {
  767. /* The virtual register that holds the value of the slot if the
  768. value of the slot is in register. */
  769. JitReg reg;
  770. /* The dirty bit of the value slot. It's set if the value in
  771. register is newer than the value in memory. */
  772. uint32 dirty : 1;
  773. /* Whether the new value in register is a reference, which is valid
  774. only when the dirty bit is set. */
  775. uint32 ref : 1;
  776. /* Committed reference flag. 0: unknown, 1: not-reference, 2:
  777. reference. */
  778. uint32 committed_ref : 2;
  779. } JitValueSlot;
  780. typedef struct JitMemRegs {
  781. /* The following registers should be re-loaded after
  782. memory.grow, callbc and callnative */
  783. JitReg memory_data;
  784. JitReg memory_data_end;
  785. JitReg mem_bound_check_1byte;
  786. JitReg mem_bound_check_2bytes;
  787. JitReg mem_bound_check_4bytes;
  788. JitReg mem_bound_check_8bytes;
  789. JitReg mem_bound_check_16bytes;
  790. } JitMemRegs;
  791. typedef struct JitTableRegs {
  792. JitReg table_elems;
  793. /* Should be re-loaded after table.grow,
  794. callbc and callnative */
  795. JitReg table_cur_size;
  796. } JitTableRegs;
  797. /* Frame information for translation */
  798. typedef struct JitFrame {
  799. /* The current wasm module */
  800. WASMModule *cur_wasm_module;
  801. /* The current wasm function */
  802. WASMFunction *cur_wasm_func;
  803. /* The current wasm function index */
  804. uint32 cur_wasm_func_idx;
  805. /* The current compilation context */
  806. struct JitCompContext *cc;
  807. /* Max local slot number. */
  808. uint32 max_locals;
  809. /* Max operand stack slot number. */
  810. uint32 max_stacks;
  811. /* Instruction pointer */
  812. uint8 *ip;
  813. /* Stack top pointer */
  814. JitValueSlot *sp;
  815. /* Committed instruction pointer */
  816. uint8 *committed_ip;
  817. /* Committed stack top pointer */
  818. JitValueSlot *committed_sp;
  819. /* WASM module instance */
  820. JitReg module_inst_reg;
  821. /* WASM module */
  822. JitReg module_reg;
  823. /* module_inst->import_func_ptrs */
  824. JitReg import_func_ptrs_reg;
  825. /* module_inst->fast_jit_func_ptrs */
  826. JitReg fast_jit_func_ptrs_reg;
  827. /* module_inst->func_type_indexes */
  828. JitReg func_type_indexes_reg;
  829. /* Boundary of auxiliary stack */
  830. JitReg aux_stack_bound_reg;
  831. /* Bottom of auxiliary stack */
  832. JitReg aux_stack_bottom_reg;
  833. /* Data of memory instances */
  834. JitMemRegs *memory_regs;
  835. /* Data of table instances */
  836. JitTableRegs *table_regs;
  837. /* Local variables */
  838. JitValueSlot lp[1];
  839. } JitFrame;
  840. typedef struct JitIncomingInsn {
  841. struct JitIncomingInsn *next;
  842. JitInsn *insn;
  843. uint32 opnd_idx;
  844. } JitIncomingInsn, *JitIncomingInsnList;
  845. typedef struct JitBlock {
  846. struct JitBlock *next;
  847. struct JitBlock *prev;
  848. /* The current Jit Block */
  849. struct JitCompContext *cc;
  850. /* LABEL_TYPE_BLOCK/LOOP/IF/FUNCTION */
  851. uint32 label_type;
  852. /* code of else opcode of this block, if it is a IF block */
  853. uint8 *wasm_code_else;
  854. /* code of end opcode of this block */
  855. uint8 *wasm_code_end;
  856. /* JIT label points to code begin */
  857. JitBasicBlock *basic_block_entry;
  858. /* JIT label points to code else */
  859. JitBasicBlock *basic_block_else;
  860. /* JIT label points to code end */
  861. JitBasicBlock *basic_block_end;
  862. /* Incoming INSN for basic_block_else */
  863. JitInsn *incoming_insn_for_else_bb;
  864. /* Incoming INSNs for basic_block_end */
  865. JitIncomingInsnList incoming_insns_for_end_bb;
  866. /* WASM operation stack */
  867. JitValueStack value_stack;
  868. /* Param count/types/PHIs of this block */
  869. uint32 param_count;
  870. uint8 *param_types;
  871. /* Result count/types/PHIs of this block */
  872. uint32 result_count;
  873. uint8 *result_types;
  874. /* The begin frame stack pointer of this block */
  875. JitValueSlot *frame_sp_begin;
  876. } JitBlock;
  877. /**
  878. * Block stack, represents WASM block stack elements
  879. */
  880. typedef struct JitBlockStack {
  881. JitBlock *block_list_head;
  882. JitBlock *block_list_end;
  883. } JitBlockStack;
  884. /**
  885. * The JIT compilation context for one compilation process of a
  886. * compilation unit.
  887. */
  888. typedef struct JitCompContext {
  889. /* Hard register information of each kind. */
  890. const JitHardRegInfo *hreg_info;
  891. /* No. of the pass to be applied. */
  892. uint8 cur_pass_no;
  893. /* The current wasm module */
  894. WASMModule *cur_wasm_module;
  895. /* The current wasm function */
  896. WASMFunction *cur_wasm_func;
  897. /* The current wasm function index */
  898. uint32 cur_wasm_func_idx;
  899. /* The block stack */
  900. JitBlockStack block_stack;
  901. bool mem_space_unchanged;
  902. /* Entry and exit labels of the compilation unit, whose numbers must
  903. be 0 and 1 respectively (see JIT_FOREACH_BLOCK). */
  904. JitReg entry_label;
  905. JitReg exit_label;
  906. JitBasicBlock **exce_basic_blocks;
  907. JitIncomingInsnList *incoming_insns_for_exec_bbs;
  908. /* The current basic block to generate instructions */
  909. JitBasicBlock *cur_basic_block;
  910. /* Registers of frame pointer, exec_env and CMP result. */
  911. JitReg fp_reg;
  912. JitReg exec_env_reg;
  913. JitReg cmp_reg;
  914. /* WASM module instance */
  915. JitReg module_inst_reg;
  916. /* WASM module */
  917. JitReg module_reg;
  918. /* module_inst->import_func_ptrs */
  919. JitReg import_func_ptrs_reg;
  920. /* module_inst->fast_jit_func_ptrs */
  921. JitReg fast_jit_func_ptrs_reg;
  922. /* module_inst->func_type_indexes */
  923. JitReg func_type_indexes_reg;
  924. /* Boundary of auxiliary stack */
  925. JitReg aux_stack_bound_reg;
  926. /* Bottom of auxiliary stack */
  927. JitReg aux_stack_bottom_reg;
  928. /* Data of memory instances */
  929. JitMemRegs *memory_regs;
  930. /* Data of table instances */
  931. JitTableRegs *table_regs;
  932. /* Current frame information for translation */
  933. JitFrame *jit_frame;
  934. /* The total frame size of current function */
  935. uint32 total_frame_size;
  936. /* The spill cache offset to the interp frame */
  937. uint32 spill_cache_offset;
  938. /* The spill cache size */
  939. uint32 spill_cache_size;
  940. /* The offset of jitted_return_address in the frame, which is set by
  941. the pass frontend and used by the pass codegen. */
  942. uint32 jitted_return_address_offset;
  943. /* Begin and end addresses of the jitted code produced by the pass
  944. codegen and consumed by the region registration after codegen and
  945. the pass dump. */
  946. void *jitted_addr_begin;
  947. void *jitted_addr_end;
  948. char last_error[128];
  949. /* Below fields are all private. Don't access them directly. */
  950. /* Reference count of the compilation context. */
  951. uint16 _reference_count;
  952. /* Constant values. */
  953. struct {
  954. /* Number of constant values of each kind. */
  955. uint32 _num[JIT_REG_KIND_L32];
  956. /* Capacity of register annotations of each kind. */
  957. uint32 _capacity[JIT_REG_KIND_L32];
  958. /* Constant vallues of each kind. */
  959. uint8 *_value[JIT_REG_KIND_L32];
  960. /* Next element on the list of values with the same hash code. */
  961. JitReg *_next[JIT_REG_KIND_L32];
  962. /* Size of the hash table. */
  963. uint32 _hash_table_size;
  964. /* Map values to JIT register. */
  965. JitReg *_hash_table;
  966. } _const_val;
  967. /* Annotations of labels, registers and instructions. */
  968. struct {
  969. /* Number of all ever created labels. */
  970. uint32 _label_num;
  971. /* Capacity of label annotations. */
  972. uint32 _label_capacity;
  973. /* Number of all ever created instructions. */
  974. uint32 _insn_num;
  975. /* Capacity of instruction annotations. */
  976. uint32 _insn_capacity;
  977. /* Number of ever created registers of each kind. */
  978. uint32 _reg_num[JIT_REG_KIND_L32];
  979. /* Capacity of register annotations of each kind. */
  980. uint32 _reg_capacity[JIT_REG_KIND_L32];
  981. /* Storage of annotations. */
  982. #define ANN_LABEL(TYPE, NAME) TYPE *_label_##NAME;
  983. #define ANN_INSN(TYPE, NAME) TYPE *_insn_##NAME;
  984. #define ANN_REG(TYPE, NAME) TYPE *_reg_##NAME[JIT_REG_KIND_L32];
  985. #include "jit_ir.def"
  986. #undef ANN_LABEL
  987. #undef ANN_INSN
  988. #undef ANN_REG
  989. /* Flags of annotations. */
  990. #define ANN_LABEL(TYPE, NAME) uint32 _label_##NAME##_enabled : 1;
  991. #define ANN_INSN(TYPE, NAME) uint32 _insn_##NAME##_enabled : 1;
  992. #define ANN_REG(TYPE, NAME) uint32 _reg_##NAME##_enabled : 1;
  993. #include "jit_ir.def"
  994. #undef ANN_LABEL
  995. #undef ANN_INSN
  996. #undef ANN_REG
  997. } _ann;
  998. /* Instruction hash table. */
  999. struct {
  1000. /* Size of the hash table. */
  1001. uint32 _size;
  1002. /* The hash table. */
  1003. JitInsn **_table;
  1004. } _insn_hash_table;
  1005. /* indicate if the last comparision is about floating-point numbers or not
  1006. */
  1007. bool last_cmp_on_fp;
  1008. } JitCompContext;
  1009. /*
  1010. * Annotation accessing functions jit_annl_NAME, jit_anni_NAME and
  1011. * jit_annr_NAME.
  1012. */
  1013. #define ANN_LABEL(TYPE, NAME) \
  1014. static inline TYPE *jit_annl_##NAME(JitCompContext *cc, JitReg label) \
  1015. { \
  1016. unsigned idx = jit_reg_no(label); \
  1017. bh_assert(jit_reg_kind(label) == JIT_REG_KIND_L32); \
  1018. bh_assert(idx < cc->_ann._label_num); \
  1019. bh_assert(cc->_ann._label_##NAME##_enabled); \
  1020. return &cc->_ann._label_##NAME[idx]; \
  1021. }
  1022. #define ANN_INSN(TYPE, NAME) \
  1023. static inline TYPE *jit_anni_##NAME(JitCompContext *cc, JitInsn *insn) \
  1024. { \
  1025. unsigned uid = insn->uid; \
  1026. bh_assert(uid < cc->_ann._insn_num); \
  1027. bh_assert(cc->_ann._insn_##NAME##_enabled); \
  1028. return &cc->_ann._insn_##NAME[uid]; \
  1029. }
  1030. #define ANN_REG(TYPE, NAME) \
  1031. static inline TYPE *jit_annr_##NAME(JitCompContext *cc, JitReg reg) \
  1032. { \
  1033. unsigned kind = jit_reg_kind(reg); \
  1034. unsigned no = jit_reg_no(reg); \
  1035. bh_assert(kind < JIT_REG_KIND_L32); \
  1036. bh_assert(no < cc->_ann._reg_num[kind]); \
  1037. bh_assert(cc->_ann._reg_##NAME##_enabled); \
  1038. return &cc->_ann._reg_##NAME[kind][no]; \
  1039. }
  1040. #include "jit_ir.def"
  1041. #undef ANN_LABEL
  1042. #undef ANN_INSN
  1043. #undef ANN_REG
  1044. /*
  1045. * Annotation enabling functions jit_annl_enable_NAME,
  1046. * jit_anni_enable_NAME and jit_annr_enable_NAME, which allocate
  1047. * sufficient memory for the annotations.
  1048. */
  1049. #define ANN_LABEL(TYPE, NAME) bool jit_annl_enable_##NAME(JitCompContext *cc);
  1050. #define ANN_INSN(TYPE, NAME) bool jit_anni_enable_##NAME(JitCompContext *cc);
  1051. #define ANN_REG(TYPE, NAME) bool jit_annr_enable_##NAME(JitCompContext *cc);
  1052. #include "jit_ir.def"
  1053. #undef ANN_LABEL
  1054. #undef ANN_INSN
  1055. #undef ANN_REG
  1056. /*
  1057. * Annotation disabling functions jit_annl_disable_NAME,
  1058. * jit_anni_disable_NAME and jit_annr_disable_NAME, which release
  1059. * memory of the annotations. Before calling these functions,
  1060. * resources owned by the annotations must be explictely released.
  1061. */
  1062. #define ANN_LABEL(TYPE, NAME) void jit_annl_disable_##NAME(JitCompContext *cc);
  1063. #define ANN_INSN(TYPE, NAME) void jit_anni_disable_##NAME(JitCompContext *cc);
  1064. #define ANN_REG(TYPE, NAME) void jit_annr_disable_##NAME(JitCompContext *cc);
  1065. #include "jit_ir.def"
  1066. #undef ANN_LABEL
  1067. #undef ANN_INSN
  1068. #undef ANN_REG
  1069. /*
  1070. * Functions jit_annl_is_enabled_NAME, jit_anni_is_enabled_NAME and
  1071. * jit_annr_is_enabled_NAME for checking whether an annotation is
  1072. * enabled.
  1073. */
  1074. #define ANN_LABEL(TYPE, NAME) \
  1075. static inline bool jit_annl_is_enabled_##NAME(JitCompContext *cc) \
  1076. { \
  1077. return !!cc->_ann._label_##NAME##_enabled; \
  1078. }
  1079. #define ANN_INSN(TYPE, NAME) \
  1080. static inline bool jit_anni_is_enabled_##NAME(JitCompContext *cc) \
  1081. { \
  1082. return !!cc->_ann._insn_##NAME##_enabled; \
  1083. }
  1084. #define ANN_REG(TYPE, NAME) \
  1085. static inline bool jit_annr_is_enabled_##NAME(JitCompContext *cc) \
  1086. { \
  1087. return !!cc->_ann._reg_##NAME##_enabled; \
  1088. }
  1089. #include "jit_ir.def"
  1090. #undef ANN_LABEL
  1091. #undef ANN_INSN
  1092. #undef ANN_REG
  1093. /**
  1094. * Initialize a compilation context.
  1095. *
  1096. * @param cc the compilation context
  1097. * @param htab_size the initial hash table size of constant pool
  1098. *
  1099. * @return cc if succeeds, NULL otherwise
  1100. */
  1101. JitCompContext *
  1102. jit_cc_init(JitCompContext *cc, unsigned htab_size);
  1103. /**
  1104. * Release all resources of a compilation context, which doesn't
  1105. * include the compilation context itself.
  1106. *
  1107. * @param cc the compilation context
  1108. */
  1109. void
  1110. jit_cc_destroy(JitCompContext *cc);
  1111. /**
  1112. * Increase the reference count of the compilation context.
  1113. *
  1114. * @param cc the compilation context
  1115. */
  1116. static inline void
  1117. jit_cc_inc_ref(JitCompContext *cc)
  1118. {
  1119. cc->_reference_count++;
  1120. }
  1121. /**
  1122. * Decrease the reference_count and destroy and free the compilation
  1123. * context if the reference_count is decreased to zero.
  1124. *
  1125. * @param cc the compilation context
  1126. */
  1127. void
  1128. jit_cc_delete(JitCompContext *cc);
  1129. char *
  1130. jit_get_last_error(JitCompContext *cc);
  1131. void
  1132. jit_set_last_error(JitCompContext *cc, const char *error);
  1133. void
  1134. jit_set_last_error_v(JitCompContext *cc, const char *format, ...);
  1135. /**
  1136. * Create a I32 constant value with relocatable into the compilation
  1137. * context. A constant value that has relocation info cannot be
  1138. * constant-folded as normal constants because its value depends on
  1139. * runtime context and may be different in different executions.
  1140. *
  1141. * @param cc compilation context
  1142. * @param val a I32 value
  1143. * @param rel relocation information
  1144. *
  1145. * @return a constant register containing the value
  1146. */
  1147. JitReg
  1148. jit_cc_new_const_I32_rel(JitCompContext *cc, int32 val, uint32 rel);
  1149. /**
  1150. * Create a I32 constant value without relocation info (0) into the
  1151. * compilation context.
  1152. *
  1153. * @param cc compilation context
  1154. * @param val a I32 value
  1155. *
  1156. * @return a constant register containing the value
  1157. */
  1158. static inline JitReg
  1159. jit_cc_new_const_I32(JitCompContext *cc, int32 val)
  1160. {
  1161. return jit_cc_new_const_I32_rel(cc, val, 0);
  1162. }
  1163. /**
  1164. * Create a I64 constant value into the compilation context.
  1165. *
  1166. * @param cc compilation context
  1167. * @param val a I64 value
  1168. *
  1169. * @return a constant register containing the value
  1170. */
  1171. JitReg
  1172. jit_cc_new_const_I64(JitCompContext *cc, int64 val);
  1173. #if UINTPTR_MAX == UINT64_MAX
  1174. #define jit_cc_new_const_PTR jit_cc_new_const_I64
  1175. #else
  1176. #define jit_cc_new_const_PTR jit_cc_new_const_I32
  1177. #endif
  1178. /**
  1179. * Create a F32 constant value into the compilation context.
  1180. *
  1181. * @param cc compilation context
  1182. * @param val a F32 value
  1183. *
  1184. * @return a constant register containing the value
  1185. */
  1186. JitReg
  1187. jit_cc_new_const_F32(JitCompContext *cc, float val);
  1188. /**
  1189. * Create a F64 constant value into the compilation context.
  1190. *
  1191. * @param cc compilation context
  1192. * @param val a F64 value
  1193. *
  1194. * @return a constant register containing the value
  1195. */
  1196. JitReg
  1197. jit_cc_new_const_F64(JitCompContext *cc, double val);
  1198. /**
  1199. * Get the relocation info of a I32 constant register.
  1200. *
  1201. * @param cc compilation context
  1202. * @param reg constant register
  1203. *
  1204. * @return the relocation info of the constant
  1205. */
  1206. uint32
  1207. jit_cc_get_const_I32_rel(JitCompContext *cc, JitReg reg);
  1208. /**
  1209. * Get the constant value of a I32 constant register.
  1210. *
  1211. * @param cc compilation context
  1212. * @param reg constant register
  1213. *
  1214. * @return the constant value
  1215. */
  1216. int32
  1217. jit_cc_get_const_I32(JitCompContext *cc, JitReg reg);
  1218. /**
  1219. * Get the constant value of a I64 constant register.
  1220. *
  1221. * @param cc compilation context
  1222. * @param reg constant register
  1223. *
  1224. * @return the constant value
  1225. */
  1226. int64
  1227. jit_cc_get_const_I64(JitCompContext *cc, JitReg reg);
  1228. /**
  1229. * Get the constant value of a F32 constant register.
  1230. *
  1231. * @param cc compilation context
  1232. * @param reg constant register
  1233. *
  1234. * @return the constant value
  1235. */
  1236. float
  1237. jit_cc_get_const_F32(JitCompContext *cc, JitReg reg);
  1238. /**
  1239. * Get the constant value of a F64 constant register.
  1240. *
  1241. * @param cc compilation context
  1242. * @param reg constant register
  1243. *
  1244. * @return the constant value
  1245. */
  1246. double
  1247. jit_cc_get_const_F64(JitCompContext *cc, JitReg reg);
  1248. /**
  1249. * Get the number of total created labels.
  1250. *
  1251. * @param cc the compilation context
  1252. *
  1253. * @return the number of total created labels
  1254. */
  1255. static inline unsigned
  1256. jit_cc_label_num(JitCompContext *cc)
  1257. {
  1258. return cc->_ann._label_num;
  1259. }
  1260. /**
  1261. * Get the number of total created instructions.
  1262. *
  1263. * @param cc the compilation context
  1264. *
  1265. * @return the number of total created instructions
  1266. */
  1267. static inline unsigned
  1268. jit_cc_insn_num(JitCompContext *cc)
  1269. {
  1270. return cc->_ann._insn_num;
  1271. }
  1272. /**
  1273. * Get the number of total created registers.
  1274. *
  1275. * @param cc the compilation context
  1276. * @param kind the register kind
  1277. *
  1278. * @return the number of total created registers
  1279. */
  1280. static inline unsigned
  1281. jit_cc_reg_num(JitCompContext *cc, unsigned kind)
  1282. {
  1283. bh_assert(kind < JIT_REG_KIND_L32);
  1284. return cc->_ann._reg_num[kind];
  1285. }
  1286. /**
  1287. * Create a new label in the compilation context.
  1288. *
  1289. * @param cc the compilation context
  1290. *
  1291. * @return a new label in the compilation context
  1292. */
  1293. JitReg
  1294. jit_cc_new_label(JitCompContext *cc);
  1295. /**
  1296. * Create a new block with a new label in the compilation context.
  1297. *
  1298. * @param cc the compilation context
  1299. * @param n number of predecessors
  1300. *
  1301. * @return a new block with a new label in the compilation context
  1302. */
  1303. JitBasicBlock *
  1304. jit_cc_new_basic_block(JitCompContext *cc, int n);
  1305. /**
  1306. * Resize the predecessor number of a block.
  1307. *
  1308. * @param cc the containing compilation context
  1309. * @param block block to be resized
  1310. * @param n new number of predecessors
  1311. *
  1312. * @return the new block if succeeds, NULL otherwise
  1313. */
  1314. JitBasicBlock *
  1315. jit_cc_resize_basic_block(JitCompContext *cc, JitBasicBlock *block, int n);
  1316. /**
  1317. * Initialize the instruction hash table to the given size and enable
  1318. * the instruction's _hash_link annotation.
  1319. *
  1320. * @param cc the containing compilation context
  1321. * @param n size of the hash table
  1322. *
  1323. * @return true if succeeds, false otherwise
  1324. */
  1325. bool
  1326. jit_cc_enable_insn_hash(JitCompContext *cc, unsigned n);
  1327. /**
  1328. * Destroy the instruction hash table and disable the instruction's
  1329. * _hash_link annotation.
  1330. *
  1331. * @param cc the containing compilation context
  1332. */
  1333. void
  1334. jit_cc_disable_insn_hash(JitCompContext *cc);
  1335. /**
  1336. * Reset the hash table entries.
  1337. *
  1338. * @param cc the containing compilation context
  1339. */
  1340. void
  1341. jit_cc_reset_insn_hash(JitCompContext *cc);
  1342. /**
  1343. * Allocate a new instruction ID in the compilation context and set it
  1344. * to the given instruction.
  1345. *
  1346. * @param cc the compilation context
  1347. * @param insn IR instruction
  1348. *
  1349. * @return the insn with uid being set
  1350. */
  1351. JitInsn *
  1352. jit_cc_set_insn_uid(JitCompContext *cc, JitInsn *insn);
  1353. /*
  1354. * Similar to jit_cc_set_insn_uid except that if setting uid failed,
  1355. * delete the insn. Only used by jit_cc_new_insn
  1356. */
  1357. JitInsn *
  1358. _jit_cc_set_insn_uid_for_new_insn(JitCompContext *cc, JitInsn *insn);
  1359. /**
  1360. * Create a new instruction in the compilation context.
  1361. *
  1362. * @param cc the compilationo context
  1363. * @param NAME instruction name
  1364. *
  1365. * @return a new instruction in the compilation context
  1366. */
  1367. #define jit_cc_new_insn(cc, NAME, ...) \
  1368. _jit_cc_set_insn_uid_for_new_insn(cc, jit_insn_new_##NAME(__VA_ARGS__))
  1369. /*
  1370. * Helper function for jit_cc_new_insn_norm.
  1371. */
  1372. JitInsn *
  1373. _jit_cc_new_insn_norm(JitCompContext *cc, JitReg *result, JitInsn *insn);
  1374. /**
  1375. * Create a new instruction in the compilation context and normalize
  1376. * the instruction (constant folding and simplification etc.). If the
  1377. * instruction hashing is enabled (anni__hash_link is enabled), try to
  1378. * find the existing equivalent insruction first before adding a new
  1379. * one to the compilation contest.
  1380. *
  1381. * @param cc the compilationo context
  1382. * @param result returned result of the instruction. If the value is
  1383. * non-zero, it is the result of the constant-folding or an exsiting
  1384. * equivalent instruction, in which case no instruction is added into
  1385. * the compilation context. Otherwise, a new normalized instruction
  1386. * has been added into the compilation context.
  1387. * @param NAME instruction name
  1388. *
  1389. * @return a new or existing instruction in the compilation context
  1390. */
  1391. #define jit_cc_new_insn_norm(cc, result, NAME, ...) \
  1392. _jit_cc_new_insn_norm(cc, result, jit_insn_new_##NAME(__VA_ARGS__))
  1393. /**
  1394. * Helper function for GEN_INSN
  1395. *
  1396. * @param cc compilation context
  1397. * @param block the current block
  1398. * @param insn the new instruction
  1399. *
  1400. * @return the new instruction if inserted, NULL otherwise
  1401. */
  1402. static inline JitInsn *
  1403. _gen_insn(JitCompContext *cc, JitInsn *insn)
  1404. {
  1405. if (insn)
  1406. jit_basic_block_append_insn(cc->cur_basic_block, insn);
  1407. else
  1408. jit_set_last_error(cc, "generate insn failed");
  1409. return insn;
  1410. }
  1411. /**
  1412. * Generate and append an instruction to the current block.
  1413. */
  1414. #define GEN_INSN(...) _gen_insn(cc, jit_cc_new_insn(cc, __VA_ARGS__))
  1415. /**
  1416. * Create a constant register without relocation info.
  1417. *
  1418. * @param Type type of the register
  1419. * @param val the constant value
  1420. *
  1421. * @return the constant register if succeeds, 0 otherwise
  1422. */
  1423. #define NEW_CONST(Type, val) jit_cc_new_const_##Type(cc, val)
  1424. /**
  1425. * Create a new virtual register in the compilation context.
  1426. *
  1427. * @param cc the compilation context
  1428. * @param kind kind of the register
  1429. *
  1430. * @return a new label in the compilation context
  1431. */
  1432. JitReg
  1433. jit_cc_new_reg(JitCompContext *cc, unsigned kind);
  1434. /*
  1435. * Create virtual registers with specific types in the compilation
  1436. * context. They are more convenient than the above one.
  1437. */
  1438. static inline JitReg
  1439. jit_cc_new_reg_I32(JitCompContext *cc)
  1440. {
  1441. return jit_cc_new_reg(cc, JIT_REG_KIND_I32);
  1442. }
  1443. static inline JitReg
  1444. jit_cc_new_reg_I64(JitCompContext *cc)
  1445. {
  1446. return jit_cc_new_reg(cc, JIT_REG_KIND_I64);
  1447. }
  1448. #if UINTPTR_MAX == UINT64_MAX
  1449. #define jit_cc_new_reg_ptr jit_cc_new_reg_I64
  1450. #else
  1451. #define jit_cc_new_reg_ptr jit_cc_new_reg_I32
  1452. #endif
  1453. static inline JitReg
  1454. jit_cc_new_reg_F32(JitCompContext *cc)
  1455. {
  1456. return jit_cc_new_reg(cc, JIT_REG_KIND_F32);
  1457. }
  1458. static inline JitReg
  1459. jit_cc_new_reg_F64(JitCompContext *cc)
  1460. {
  1461. return jit_cc_new_reg(cc, JIT_REG_KIND_F64);
  1462. }
  1463. static inline JitReg
  1464. jit_cc_new_reg_V64(JitCompContext *cc)
  1465. {
  1466. return jit_cc_new_reg(cc, JIT_REG_KIND_V64);
  1467. }
  1468. static inline JitReg
  1469. jit_cc_new_reg_V128(JitCompContext *cc)
  1470. {
  1471. return jit_cc_new_reg(cc, JIT_REG_KIND_V128);
  1472. }
  1473. static inline JitReg
  1474. jit_cc_new_reg_V256(JitCompContext *cc)
  1475. {
  1476. return jit_cc_new_reg(cc, JIT_REG_KIND_V256);
  1477. }
  1478. /**
  1479. * Get the hard register numbe of the given kind
  1480. *
  1481. * @param cc the compilation context
  1482. * @param kind the register kind
  1483. *
  1484. * @return number of hard registers of the given kind
  1485. */
  1486. static inline unsigned
  1487. jit_cc_hreg_num(JitCompContext *cc, unsigned kind)
  1488. {
  1489. bh_assert(kind < JIT_REG_KIND_L32);
  1490. return cc->hreg_info->info[kind].num;
  1491. }
  1492. /**
  1493. * Check whether a given register is a hard register.
  1494. *
  1495. * @param cc the compilation context
  1496. * @param reg the register which must be a variable
  1497. *
  1498. * @return true if the register is a hard register
  1499. */
  1500. static inline bool
  1501. jit_cc_is_hreg(JitCompContext *cc, JitReg reg)
  1502. {
  1503. unsigned kind = jit_reg_kind(reg);
  1504. unsigned no = jit_reg_no(reg);
  1505. bh_assert(jit_reg_is_variable(reg));
  1506. bh_assert(kind < JIT_REG_KIND_L32);
  1507. return no < cc->hreg_info->info[kind].num;
  1508. }
  1509. /**
  1510. * Check whether the given hard register is fixed.
  1511. *
  1512. * @param cc the compilation context
  1513. * @param reg the hard register
  1514. *
  1515. * @return true if the hard register is fixed
  1516. */
  1517. static inline bool
  1518. jit_cc_is_hreg_fixed(JitCompContext *cc, JitReg reg)
  1519. {
  1520. unsigned kind = jit_reg_kind(reg);
  1521. unsigned no = jit_reg_no(reg);
  1522. bh_assert(jit_cc_is_hreg(cc, reg));
  1523. bh_assert(kind < JIT_REG_KIND_L32);
  1524. return !!cc->hreg_info->info[kind].fixed[no];
  1525. }
  1526. /**
  1527. * Check whether the given hard register is caller-saved-native.
  1528. *
  1529. * @param cc the compilation context
  1530. * @param reg the hard register
  1531. *
  1532. * @return true if the hard register is caller-saved-native
  1533. */
  1534. static inline bool
  1535. jit_cc_is_hreg_caller_saved_native(JitCompContext *cc, JitReg reg)
  1536. {
  1537. unsigned kind = jit_reg_kind(reg);
  1538. unsigned no = jit_reg_no(reg);
  1539. bh_assert(jit_cc_is_hreg(cc, reg));
  1540. bh_assert(kind < JIT_REG_KIND_L32);
  1541. return !!cc->hreg_info->info[kind].caller_saved_native[no];
  1542. }
  1543. /**
  1544. * Check whether the given hard register is caller-saved-jitted.
  1545. *
  1546. * @param cc the compilation context
  1547. * @param reg the hard register
  1548. *
  1549. * @return true if the hard register is caller-saved-jitted
  1550. */
  1551. static inline bool
  1552. jit_cc_is_hreg_caller_saved_jitted(JitCompContext *cc, JitReg reg)
  1553. {
  1554. unsigned kind = jit_reg_kind(reg);
  1555. unsigned no = jit_reg_no(reg);
  1556. bh_assert(jit_cc_is_hreg(cc, reg));
  1557. bh_assert(kind < JIT_REG_KIND_L32);
  1558. return !!cc->hreg_info->info[kind].caller_saved_jitted[no];
  1559. }
  1560. /**
  1561. * Return the entry block of the compilation context.
  1562. *
  1563. * @param cc the compilation context
  1564. *
  1565. * @return the entry block of the compilation context
  1566. */
  1567. static inline JitBasicBlock *
  1568. jit_cc_entry_basic_block(JitCompContext *cc)
  1569. {
  1570. return *(jit_annl_basic_block(cc, cc->entry_label));
  1571. }
  1572. /**
  1573. * Return the exit block of the compilation context.
  1574. *
  1575. * @param cc the compilation context
  1576. *
  1577. * @return the exit block of the compilation context
  1578. */
  1579. static inline JitBasicBlock *
  1580. jit_cc_exit_basic_block(JitCompContext *cc)
  1581. {
  1582. return *(jit_annl_basic_block(cc, cc->exit_label));
  1583. }
  1584. void
  1585. jit_value_stack_push(JitValueStack *stack, JitValue *value);
  1586. JitValue *
  1587. jit_value_stack_pop(JitValueStack *stack);
  1588. void
  1589. jit_value_stack_destroy(JitValueStack *stack);
  1590. JitBlock *
  1591. jit_block_stack_top(JitBlockStack *stack);
  1592. void
  1593. jit_block_stack_push(JitBlockStack *stack, JitBlock *block);
  1594. JitBlock *
  1595. jit_block_stack_pop(JitBlockStack *stack);
  1596. void
  1597. jit_block_stack_destroy(JitBlockStack *stack);
  1598. bool
  1599. jit_block_add_incoming_insn(JitBlock *block, JitInsn *insn, uint32 opnd_idx);
  1600. void
  1601. jit_block_destroy(JitBlock *block);
  1602. bool
  1603. jit_cc_push_value(JitCompContext *cc, uint8 type, JitReg value);
  1604. bool
  1605. jit_cc_pop_value(JitCompContext *cc, uint8 type, JitReg *p_value);
  1606. bool
  1607. jit_lock_reg_in_insn(JitCompContext *cc, JitInsn *the_insn, JitReg reg_to_lock);
  1608. /**
  1609. * Update the control flow graph after successors of blocks are
  1610. * changed so that the predecessor vector of each block represents the
  1611. * updated status. The predecessors may not be required by all
  1612. * passes, so we don't need to keep them always being updated.
  1613. *
  1614. * @param cc the compilation context
  1615. *
  1616. * @return true if succeeds, false otherwise
  1617. */
  1618. bool
  1619. jit_cc_update_cfg(JitCompContext *cc);
  1620. /**
  1621. * Visit each normal block (which is not entry nor exit block) in a
  1622. * compilation context. New blocks can be added in the loop body, but
  1623. * they won't be visited. Blocks can also be removed safely (by
  1624. * setting the label's block annotation to NULL) in the loop body.
  1625. *
  1626. * @param CC (JitCompContext *) the compilation context
  1627. * @param I (unsigned) index variable of the block (label no)
  1628. * @param E (unsigned) end index variable of block (last index + 1)
  1629. * @param B (JitBasicBlock *) block pointer variable
  1630. */
  1631. #define JIT_FOREACH_BLOCK(CC, I, E, B) \
  1632. for ((I) = 2, (E) = (CC)->_ann._label_num; (I) < (E); (I)++) \
  1633. if (((B) = (CC)->_ann._label_basic_block[(I)]))
  1634. /**
  1635. * The version that includes entry and exit block.
  1636. */
  1637. #define JIT_FOREACH_BLOCK_ENTRY_EXIT(CC, I, E, B) \
  1638. for ((I) = 0, (E) = (CC)->_ann._label_num; (I) < (E); (I)++) \
  1639. if (((B) = (CC)->_ann._label_basic_block[(I)]))
  1640. /**
  1641. * Visit each normal block (which is not entry nor exit block) in a
  1642. * compilation context in reverse order. New blocks can be added in
  1643. * the loop body, but they won't be visited. Blocks can also be
  1644. * removed safely (by setting the label's block annotation to NULL) in
  1645. * the loop body.
  1646. *
  1647. * @param CC (JitCompContext *) the compilation context
  1648. * @param I (unsigned) index of the block (label no)
  1649. * @param B (JitBasicBlock *) block pointer
  1650. */
  1651. #define JIT_FOREACH_BLOCK_REVERSE(CC, I, B) \
  1652. for ((I) = (CC)->_ann._label_num; (I) > 2; (I)--) \
  1653. if (((B) = (CC)->_ann._label_basic_block[(I)-1]))
  1654. /**
  1655. * The version that includes entry and exit block.
  1656. */
  1657. #define JIT_FOREACH_BLOCK_REVERSE_ENTRY_EXIT(CC, I, B) \
  1658. for ((I) = (CC)->_ann._label_num; (I) > 0; (I)--) \
  1659. if (((B) = (CC)->_ann._label_basic_block[(I)-1]))
  1660. #ifdef __cplusplus
  1661. }
  1662. #endif
  1663. #endif /* end of _JIT_IR_H_ */