jit_ir.h 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874
  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. JitReg memory_inst;
  782. /* The following registers should be re-loaded after
  783. memory.grow, callbc and callnative */
  784. JitReg memory_data;
  785. JitReg memory_data_end;
  786. JitReg mem_bound_check_1byte;
  787. JitReg mem_bound_check_2bytes;
  788. JitReg mem_bound_check_4bytes;
  789. JitReg mem_bound_check_8bytes;
  790. JitReg mem_bound_check_16bytes;
  791. } JitMemRegs;
  792. typedef struct JitTableRegs {
  793. JitReg table_inst;
  794. JitReg table_data;
  795. /* Should be re-loaded after table.grow,
  796. callbc and callnative */
  797. JitReg table_cur_size;
  798. } JitTableRegs;
  799. /* Frame information for translation */
  800. typedef struct JitFrame {
  801. /* The current wasm module */
  802. WASMModule *cur_wasm_module;
  803. /* The current wasm function */
  804. WASMFunction *cur_wasm_func;
  805. /* The current wasm function index */
  806. uint32 cur_wasm_func_idx;
  807. /* The current compilation context */
  808. struct JitCompContext *cc;
  809. /* Max local slot number. */
  810. uint32 max_locals;
  811. /* Max operand stack slot number. */
  812. uint32 max_stacks;
  813. /* Instruction pointer */
  814. uint8 *ip;
  815. /* Stack top pointer */
  816. JitValueSlot *sp;
  817. /* Committed instruction pointer */
  818. uint8 *committed_ip;
  819. /* Committed stack top pointer */
  820. JitValueSlot *committed_sp;
  821. /* WASM module instance */
  822. JitReg module_inst_reg;
  823. /* WASM module */
  824. JitReg module_reg;
  825. /* module_inst->fast_jit_func_ptrs */
  826. JitReg fast_jit_func_ptrs_reg;
  827. /* Base address of global data */
  828. JitReg global_data_reg;
  829. /* Boundary of auxiliary stack */
  830. JitReg aux_stack_bound_reg;
  831. /* Bottom of auxiliary stack */
  832. JitReg aux_stack_bottom_reg;
  833. /* Memory instances */
  834. JitReg memories_reg;
  835. /* Data of memory instances */
  836. JitMemRegs *memory_regs;
  837. /* Table instances */
  838. JitReg tables_reg;
  839. /* Data of table instances */
  840. JitTableRegs *table_regs;
  841. /* Local variables */
  842. JitValueSlot lp[1];
  843. } JitFrame;
  844. typedef struct JitIncomingInsn {
  845. struct JitIncomingInsn *next;
  846. JitInsn *insn;
  847. uint32 opnd_idx;
  848. } JitIncomingInsn, *JitIncomingInsnList;
  849. typedef struct JitBlock {
  850. struct JitBlock *next;
  851. struct JitBlock *prev;
  852. /* The current Jit Block */
  853. struct JitCompContext *cc;
  854. /* LABEL_TYPE_BLOCK/LOOP/IF/FUNCTION */
  855. uint32 label_type;
  856. /* code of else opcode of this block, if it is a IF block */
  857. uint8 *wasm_code_else;
  858. /* code of end opcode of this block */
  859. uint8 *wasm_code_end;
  860. /* JIT label points to code begin */
  861. JitBasicBlock *basic_block_entry;
  862. /* JIT label points to code else */
  863. JitBasicBlock *basic_block_else;
  864. /* JIT label points to code end */
  865. JitBasicBlock *basic_block_end;
  866. /* Incoming INSN for basic_block_else */
  867. JitInsn *incoming_insn_for_else_bb;
  868. /* Incoming INSNs for basic_block_end */
  869. JitIncomingInsnList incoming_insns_for_end_bb;
  870. /* WASM operation stack */
  871. JitValueStack value_stack;
  872. /* Param count/types/PHIs of this block */
  873. uint32 param_count;
  874. uint8 *param_types;
  875. /* Result count/types/PHIs of this block */
  876. uint32 result_count;
  877. uint8 *result_types;
  878. /* The begin frame stack pointer of this block */
  879. JitValueSlot *frame_sp_begin;
  880. } JitBlock;
  881. /**
  882. * Block stack, represents WASM block stack elements
  883. */
  884. typedef struct JitBlockStack {
  885. JitBlock *block_list_head;
  886. JitBlock *block_list_end;
  887. } JitBlockStack;
  888. /**
  889. * The JIT compilation context for one compilation process of a
  890. * compilation unit.
  891. */
  892. typedef struct JitCompContext {
  893. /* Hard register information of each kind. */
  894. const JitHardRegInfo *hreg_info;
  895. /* No. of the pass to be applied. */
  896. uint8 cur_pass_no;
  897. /* The current wasm module */
  898. WASMModule *cur_wasm_module;
  899. /* The current wasm function */
  900. WASMFunction *cur_wasm_func;
  901. /* The current wasm function index */
  902. uint32 cur_wasm_func_idx;
  903. /* The block stack */
  904. JitBlockStack block_stack;
  905. bool mem_space_unchanged;
  906. /* Entry and exit labels of the compilation unit, whose numbers must
  907. be 0 and 1 respectively (see JIT_FOREACH_BLOCK). */
  908. JitReg entry_label;
  909. JitReg exit_label;
  910. JitBasicBlock **exce_basic_blocks;
  911. JitIncomingInsnList *incoming_insns_for_exec_bbs;
  912. /* The current basic block to generate instructions */
  913. JitBasicBlock *cur_basic_block;
  914. /* Registers of frame pointer, exec_env and CMP result. */
  915. JitReg fp_reg;
  916. JitReg exec_env_reg;
  917. JitReg cmp_reg;
  918. /* WASM module instance */
  919. JitReg module_inst_reg;
  920. /* WASM module */
  921. JitReg module_reg;
  922. /* module_inst->fast_jit_func_ptrs */
  923. JitReg fast_jit_func_ptrs_reg;
  924. /* Base address of global data */
  925. JitReg global_data_reg;
  926. /* Boundary of auxiliary stack */
  927. JitReg aux_stack_bound_reg;
  928. /* Bottom of auxiliary stack */
  929. JitReg aux_stack_bottom_reg;
  930. /* Memory instances */
  931. JitReg memories_reg;
  932. /* Data of memory instances */
  933. JitMemRegs *memory_regs;
  934. /* Table instances */
  935. JitReg tables_reg;
  936. /* Data of table instances */
  937. JitTableRegs *table_regs;
  938. /* Current frame information for translation */
  939. JitFrame *jit_frame;
  940. /* The total frame size of current function */
  941. uint32 total_frame_size;
  942. /* The spill cache offset to the interp frame */
  943. uint32 spill_cache_offset;
  944. /* The spill cache size */
  945. uint32 spill_cache_size;
  946. /* The offset of jitted_return_address in the frame, which is set by
  947. the pass frontend and used by the pass codegen. */
  948. uint32 jitted_return_address_offset;
  949. /* Begin and end addresses of the jitted code produced by the pass
  950. codegen and consumed by the region registration after codegen and
  951. the pass dump. */
  952. void *jitted_addr_begin;
  953. void *jitted_addr_end;
  954. char last_error[128];
  955. /* Below fields are all private. Don't access them directly. */
  956. /* Reference count of the compilation context. */
  957. uint16 _reference_count;
  958. /* Constant values. */
  959. struct {
  960. /* Number of constant values of each kind. */
  961. uint32 _num[JIT_REG_KIND_L32];
  962. /* Capacity of register annotations of each kind. */
  963. uint32 _capacity[JIT_REG_KIND_L32];
  964. /* Constant vallues of each kind. */
  965. uint8 *_value[JIT_REG_KIND_L32];
  966. /* Next element on the list of values with the same hash code. */
  967. JitReg *_next[JIT_REG_KIND_L32];
  968. /* Size of the hash table. */
  969. uint32 _hash_table_size;
  970. /* Map values to JIT register. */
  971. JitReg *_hash_table;
  972. } _const_val;
  973. /* Annotations of labels, registers and instructions. */
  974. struct {
  975. /* Number of all ever created labels. */
  976. uint32 _label_num;
  977. /* Capacity of label annotations. */
  978. uint32 _label_capacity;
  979. /* Number of all ever created instructions. */
  980. uint32 _insn_num;
  981. /* Capacity of instruction annotations. */
  982. uint32 _insn_capacity;
  983. /* Number of ever created registers of each kind. */
  984. uint32 _reg_num[JIT_REG_KIND_L32];
  985. /* Capacity of register annotations of each kind. */
  986. uint32 _reg_capacity[JIT_REG_KIND_L32];
  987. /* Storage of annotations. */
  988. #define ANN_LABEL(TYPE, NAME) TYPE *_label_##NAME;
  989. #define ANN_INSN(TYPE, NAME) TYPE *_insn_##NAME;
  990. #define ANN_REG(TYPE, NAME) TYPE *_reg_##NAME[JIT_REG_KIND_L32];
  991. #include "jit_ir.def"
  992. #undef ANN_LABEL
  993. #undef ANN_INSN
  994. #undef ANN_REG
  995. /* Flags of annotations. */
  996. #define ANN_LABEL(TYPE, NAME) uint32 _label_##NAME##_enabled : 1;
  997. #define ANN_INSN(TYPE, NAME) uint32 _insn_##NAME##_enabled : 1;
  998. #define ANN_REG(TYPE, NAME) uint32 _reg_##NAME##_enabled : 1;
  999. #include "jit_ir.def"
  1000. #undef ANN_LABEL
  1001. #undef ANN_INSN
  1002. #undef ANN_REG
  1003. } _ann;
  1004. /* Instruction hash table. */
  1005. struct {
  1006. /* Size of the hash table. */
  1007. uint32 _size;
  1008. /* The hash table. */
  1009. JitInsn **_table;
  1010. } _insn_hash_table;
  1011. /* indicate if the last comparision is about floating-point numbers or not
  1012. */
  1013. bool last_cmp_on_fp;
  1014. } JitCompContext;
  1015. /*
  1016. * Annotation accessing functions jit_annl_NAME, jit_anni_NAME and
  1017. * jit_annr_NAME.
  1018. */
  1019. #define ANN_LABEL(TYPE, NAME) \
  1020. static inline TYPE *jit_annl_##NAME(JitCompContext *cc, JitReg label) \
  1021. { \
  1022. unsigned idx = jit_reg_no(label); \
  1023. bh_assert(jit_reg_kind(label) == JIT_REG_KIND_L32); \
  1024. bh_assert(idx < cc->_ann._label_num); \
  1025. bh_assert(cc->_ann._label_##NAME##_enabled); \
  1026. return &cc->_ann._label_##NAME[idx]; \
  1027. }
  1028. #define ANN_INSN(TYPE, NAME) \
  1029. static inline TYPE *jit_anni_##NAME(JitCompContext *cc, JitInsn *insn) \
  1030. { \
  1031. unsigned uid = insn->uid; \
  1032. bh_assert(uid < cc->_ann._insn_num); \
  1033. bh_assert(cc->_ann._insn_##NAME##_enabled); \
  1034. return &cc->_ann._insn_##NAME[uid]; \
  1035. }
  1036. #define ANN_REG(TYPE, NAME) \
  1037. static inline TYPE *jit_annr_##NAME(JitCompContext *cc, JitReg reg) \
  1038. { \
  1039. unsigned kind = jit_reg_kind(reg); \
  1040. unsigned no = jit_reg_no(reg); \
  1041. bh_assert(kind < JIT_REG_KIND_L32); \
  1042. bh_assert(no < cc->_ann._reg_num[kind]); \
  1043. bh_assert(cc->_ann._reg_##NAME##_enabled); \
  1044. return &cc->_ann._reg_##NAME[kind][no]; \
  1045. }
  1046. #include "jit_ir.def"
  1047. #undef ANN_LABEL
  1048. #undef ANN_INSN
  1049. #undef ANN_REG
  1050. /*
  1051. * Annotation enabling functions jit_annl_enable_NAME,
  1052. * jit_anni_enable_NAME and jit_annr_enable_NAME, which allocate
  1053. * sufficient memory for the annotations.
  1054. */
  1055. #define ANN_LABEL(TYPE, NAME) bool jit_annl_enable_##NAME(JitCompContext *cc);
  1056. #define ANN_INSN(TYPE, NAME) bool jit_anni_enable_##NAME(JitCompContext *cc);
  1057. #define ANN_REG(TYPE, NAME) bool jit_annr_enable_##NAME(JitCompContext *cc);
  1058. #include "jit_ir.def"
  1059. #undef ANN_LABEL
  1060. #undef ANN_INSN
  1061. #undef ANN_REG
  1062. /*
  1063. * Annotation disabling functions jit_annl_disable_NAME,
  1064. * jit_anni_disable_NAME and jit_annr_disable_NAME, which release
  1065. * memory of the annotations. Before calling these functions,
  1066. * resources owned by the annotations must be explictely released.
  1067. */
  1068. #define ANN_LABEL(TYPE, NAME) void jit_annl_disable_##NAME(JitCompContext *cc);
  1069. #define ANN_INSN(TYPE, NAME) void jit_anni_disable_##NAME(JitCompContext *cc);
  1070. #define ANN_REG(TYPE, NAME) void jit_annr_disable_##NAME(JitCompContext *cc);
  1071. #include "jit_ir.def"
  1072. #undef ANN_LABEL
  1073. #undef ANN_INSN
  1074. #undef ANN_REG
  1075. /*
  1076. * Functions jit_annl_is_enabled_NAME, jit_anni_is_enabled_NAME and
  1077. * jit_annr_is_enabled_NAME for checking whether an annotation is
  1078. * enabled.
  1079. */
  1080. #define ANN_LABEL(TYPE, NAME) \
  1081. static inline bool jit_annl_is_enabled_##NAME(JitCompContext *cc) \
  1082. { \
  1083. return !!cc->_ann._label_##NAME##_enabled; \
  1084. }
  1085. #define ANN_INSN(TYPE, NAME) \
  1086. static inline bool jit_anni_is_enabled_##NAME(JitCompContext *cc) \
  1087. { \
  1088. return !!cc->_ann._insn_##NAME##_enabled; \
  1089. }
  1090. #define ANN_REG(TYPE, NAME) \
  1091. static inline bool jit_annr_is_enabled_##NAME(JitCompContext *cc) \
  1092. { \
  1093. return !!cc->_ann._reg_##NAME##_enabled; \
  1094. }
  1095. #include "jit_ir.def"
  1096. #undef ANN_LABEL
  1097. #undef ANN_INSN
  1098. #undef ANN_REG
  1099. /**
  1100. * Initialize a compilation context.
  1101. *
  1102. * @param cc the compilation context
  1103. * @param htab_size the initial hash table size of constant pool
  1104. *
  1105. * @return cc if succeeds, NULL otherwise
  1106. */
  1107. JitCompContext *
  1108. jit_cc_init(JitCompContext *cc, unsigned htab_size);
  1109. /**
  1110. * Release all resources of a compilation context, which doesn't
  1111. * include the compilation context itself.
  1112. *
  1113. * @param cc the compilation context
  1114. */
  1115. void
  1116. jit_cc_destroy(JitCompContext *cc);
  1117. /**
  1118. * Increase the reference count of the compilation context.
  1119. *
  1120. * @param cc the compilation context
  1121. */
  1122. static inline void
  1123. jit_cc_inc_ref(JitCompContext *cc)
  1124. {
  1125. cc->_reference_count++;
  1126. }
  1127. /**
  1128. * Decrease the reference_count and destroy and free the compilation
  1129. * context if the reference_count is decreased to zero.
  1130. *
  1131. * @param cc the compilation context
  1132. */
  1133. void
  1134. jit_cc_delete(JitCompContext *cc);
  1135. char *
  1136. jit_get_last_error(JitCompContext *cc);
  1137. void
  1138. jit_set_last_error(JitCompContext *cc, const char *error);
  1139. void
  1140. jit_set_last_error_v(JitCompContext *cc, const char *format, ...);
  1141. /**
  1142. * Create a I32 constant value with relocatable into the compilation
  1143. * context. A constant value that has relocation info cannot be
  1144. * constant-folded as normal constants because its value depends on
  1145. * runtime context and may be different in different executions.
  1146. *
  1147. * @param cc compilation context
  1148. * @param val a I32 value
  1149. * @param rel relocation information
  1150. *
  1151. * @return a constant register containing the value
  1152. */
  1153. JitReg
  1154. jit_cc_new_const_I32_rel(JitCompContext *cc, int32 val, uint32 rel);
  1155. /**
  1156. * Create a I32 constant value without relocation info (0) into the
  1157. * compilation context.
  1158. *
  1159. * @param cc compilation context
  1160. * @param val a I32 value
  1161. *
  1162. * @return a constant register containing the value
  1163. */
  1164. static inline JitReg
  1165. jit_cc_new_const_I32(JitCompContext *cc, int32 val)
  1166. {
  1167. return jit_cc_new_const_I32_rel(cc, val, 0);
  1168. }
  1169. /**
  1170. * Create a I64 constant value into the compilation context.
  1171. *
  1172. * @param cc compilation context
  1173. * @param val a I64 value
  1174. *
  1175. * @return a constant register containing the value
  1176. */
  1177. JitReg
  1178. jit_cc_new_const_I64(JitCompContext *cc, int64 val);
  1179. #if UINTPTR_MAX == UINT64_MAX
  1180. #define jit_cc_new_const_PTR jit_cc_new_const_I64
  1181. #else
  1182. #define jit_cc_new_const_PTR jit_cc_new_const_I32
  1183. #endif
  1184. /**
  1185. * Create a F32 constant value into the compilation context.
  1186. *
  1187. * @param cc compilation context
  1188. * @param val a F32 value
  1189. *
  1190. * @return a constant register containing the value
  1191. */
  1192. JitReg
  1193. jit_cc_new_const_F32(JitCompContext *cc, float val);
  1194. /**
  1195. * Create a F64 constant value into the compilation context.
  1196. *
  1197. * @param cc compilation context
  1198. * @param val a F64 value
  1199. *
  1200. * @return a constant register containing the value
  1201. */
  1202. JitReg
  1203. jit_cc_new_const_F64(JitCompContext *cc, double val);
  1204. /**
  1205. * Get the relocation info of a I32 constant register.
  1206. *
  1207. * @param cc compilation context
  1208. * @param reg constant register
  1209. *
  1210. * @return the relocation info of the constant
  1211. */
  1212. uint32
  1213. jit_cc_get_const_I32_rel(JitCompContext *cc, JitReg reg);
  1214. /**
  1215. * Get the constant value of a I32 constant register.
  1216. *
  1217. * @param cc compilation context
  1218. * @param reg constant register
  1219. *
  1220. * @return the constant value
  1221. */
  1222. int32
  1223. jit_cc_get_const_I32(JitCompContext *cc, JitReg reg);
  1224. /**
  1225. * Get the constant value of a I64 constant register.
  1226. *
  1227. * @param cc compilation context
  1228. * @param reg constant register
  1229. *
  1230. * @return the constant value
  1231. */
  1232. int64
  1233. jit_cc_get_const_I64(JitCompContext *cc, JitReg reg);
  1234. /**
  1235. * Get the constant value of a F32 constant register.
  1236. *
  1237. * @param cc compilation context
  1238. * @param reg constant register
  1239. *
  1240. * @return the constant value
  1241. */
  1242. float
  1243. jit_cc_get_const_F32(JitCompContext *cc, JitReg reg);
  1244. /**
  1245. * Get the constant value of a F64 constant register.
  1246. *
  1247. * @param cc compilation context
  1248. * @param reg constant register
  1249. *
  1250. * @return the constant value
  1251. */
  1252. double
  1253. jit_cc_get_const_F64(JitCompContext *cc, JitReg reg);
  1254. /**
  1255. * Get the number of total created labels.
  1256. *
  1257. * @param cc the compilation context
  1258. *
  1259. * @return the number of total created labels
  1260. */
  1261. static inline unsigned
  1262. jit_cc_label_num(JitCompContext *cc)
  1263. {
  1264. return cc->_ann._label_num;
  1265. }
  1266. /**
  1267. * Get the number of total created instructions.
  1268. *
  1269. * @param cc the compilation context
  1270. *
  1271. * @return the number of total created instructions
  1272. */
  1273. static inline unsigned
  1274. jit_cc_insn_num(JitCompContext *cc)
  1275. {
  1276. return cc->_ann._insn_num;
  1277. }
  1278. /**
  1279. * Get the number of total created registers.
  1280. *
  1281. * @param cc the compilation context
  1282. * @param kind the register kind
  1283. *
  1284. * @return the number of total created registers
  1285. */
  1286. static inline unsigned
  1287. jit_cc_reg_num(JitCompContext *cc, unsigned kind)
  1288. {
  1289. bh_assert(kind < JIT_REG_KIND_L32);
  1290. return cc->_ann._reg_num[kind];
  1291. }
  1292. /**
  1293. * Create a new label in the compilation context.
  1294. *
  1295. * @param cc the compilation context
  1296. *
  1297. * @return a new label in the compilation context
  1298. */
  1299. JitReg
  1300. jit_cc_new_label(JitCompContext *cc);
  1301. /**
  1302. * Create a new block with a new label in the compilation context.
  1303. *
  1304. * @param cc the compilation context
  1305. * @param n number of predecessors
  1306. *
  1307. * @return a new block with a new label in the compilation context
  1308. */
  1309. JitBasicBlock *
  1310. jit_cc_new_basic_block(JitCompContext *cc, int n);
  1311. /**
  1312. * Resize the predecessor number of a block.
  1313. *
  1314. * @param cc the containing compilation context
  1315. * @param block block to be resized
  1316. * @param n new number of predecessors
  1317. *
  1318. * @return the new block if succeeds, NULL otherwise
  1319. */
  1320. JitBasicBlock *
  1321. jit_cc_resize_basic_block(JitCompContext *cc, JitBasicBlock *block, int n);
  1322. /**
  1323. * Initialize the instruction hash table to the given size and enable
  1324. * the instruction's _hash_link annotation.
  1325. *
  1326. * @param cc the containing compilation context
  1327. * @param n size of the hash table
  1328. *
  1329. * @return true if succeeds, false otherwise
  1330. */
  1331. bool
  1332. jit_cc_enable_insn_hash(JitCompContext *cc, unsigned n);
  1333. /**
  1334. * Destroy the instruction hash table and disable the instruction's
  1335. * _hash_link annotation.
  1336. *
  1337. * @param cc the containing compilation context
  1338. */
  1339. void
  1340. jit_cc_disable_insn_hash(JitCompContext *cc);
  1341. /**
  1342. * Reset the hash table entries.
  1343. *
  1344. * @param cc the containing compilation context
  1345. */
  1346. void
  1347. jit_cc_reset_insn_hash(JitCompContext *cc);
  1348. /**
  1349. * Allocate a new instruction ID in the compilation context and set it
  1350. * to the given instruction.
  1351. *
  1352. * @param cc the compilation context
  1353. * @param insn IR instruction
  1354. *
  1355. * @return the insn with uid being set
  1356. */
  1357. JitInsn *
  1358. jit_cc_set_insn_uid(JitCompContext *cc, JitInsn *insn);
  1359. /*
  1360. * Similar to jit_cc_set_insn_uid except that if setting uid failed,
  1361. * delete the insn. Only used by jit_cc_new_insn
  1362. */
  1363. JitInsn *
  1364. _jit_cc_set_insn_uid_for_new_insn(JitCompContext *cc, JitInsn *insn);
  1365. /**
  1366. * Create a new instruction in the compilation context.
  1367. *
  1368. * @param cc the compilationo context
  1369. * @param NAME instruction name
  1370. *
  1371. * @return a new instruction in the compilation context
  1372. */
  1373. #define jit_cc_new_insn(cc, NAME, ...) \
  1374. _jit_cc_set_insn_uid_for_new_insn(cc, jit_insn_new_##NAME(__VA_ARGS__))
  1375. /*
  1376. * Helper function for jit_cc_new_insn_norm.
  1377. */
  1378. JitInsn *
  1379. _jit_cc_new_insn_norm(JitCompContext *cc, JitReg *result, JitInsn *insn);
  1380. /**
  1381. * Create a new instruction in the compilation context and normalize
  1382. * the instruction (constant folding and simplification etc.). If the
  1383. * instruction hashing is enabled (anni__hash_link is enabled), try to
  1384. * find the existing equivalent insruction first before adding a new
  1385. * one to the compilation contest.
  1386. *
  1387. * @param cc the compilationo context
  1388. * @param result returned result of the instruction. If the value is
  1389. * non-zero, it is the result of the constant-folding or an exsiting
  1390. * equivalent instruction, in which case no instruction is added into
  1391. * the compilation context. Otherwise, a new normalized instruction
  1392. * has been added into the compilation context.
  1393. * @param NAME instruction name
  1394. *
  1395. * @return a new or existing instruction in the compilation context
  1396. */
  1397. #define jit_cc_new_insn_norm(cc, result, NAME, ...) \
  1398. _jit_cc_new_insn_norm(cc, result, jit_insn_new_##NAME(__VA_ARGS__))
  1399. /**
  1400. * Helper function for GEN_INSN
  1401. *
  1402. * @param cc compilation context
  1403. * @param block the current block
  1404. * @param insn the new instruction
  1405. *
  1406. * @return the new instruction if inserted, NULL otherwise
  1407. */
  1408. static inline JitInsn *
  1409. _gen_insn(JitCompContext *cc, JitInsn *insn)
  1410. {
  1411. if (insn)
  1412. jit_basic_block_append_insn(cc->cur_basic_block, insn);
  1413. else
  1414. jit_set_last_error(cc, "generate insn failed");
  1415. return insn;
  1416. }
  1417. /**
  1418. * Generate and append an instruction to the current block.
  1419. */
  1420. #define GEN_INSN(...) _gen_insn(cc, jit_cc_new_insn(cc, __VA_ARGS__))
  1421. /**
  1422. * Create a constant register without relocation info.
  1423. *
  1424. * @param Type type of the register
  1425. * @param val the constant value
  1426. *
  1427. * @return the constant register if succeeds, 0 otherwise
  1428. */
  1429. #define NEW_CONST(Type, val) jit_cc_new_const_##Type(cc, val)
  1430. /**
  1431. * Create a new virtual register in the compilation context.
  1432. *
  1433. * @param cc the compilation context
  1434. * @param kind kind of the register
  1435. *
  1436. * @return a new label in the compilation context
  1437. */
  1438. JitReg
  1439. jit_cc_new_reg(JitCompContext *cc, unsigned kind);
  1440. /*
  1441. * Create virtual registers with specific types in the compilation
  1442. * context. They are more convenient than the above one.
  1443. */
  1444. static inline JitReg
  1445. jit_cc_new_reg_I32(JitCompContext *cc)
  1446. {
  1447. return jit_cc_new_reg(cc, JIT_REG_KIND_I32);
  1448. }
  1449. static inline JitReg
  1450. jit_cc_new_reg_I64(JitCompContext *cc)
  1451. {
  1452. return jit_cc_new_reg(cc, JIT_REG_KIND_I64);
  1453. }
  1454. #if UINTPTR_MAX == UINT64_MAX
  1455. #define jit_cc_new_reg_ptr jit_cc_new_reg_I64
  1456. #else
  1457. #define jit_cc_new_reg_ptr jit_cc_new_reg_I32
  1458. #endif
  1459. static inline JitReg
  1460. jit_cc_new_reg_F32(JitCompContext *cc)
  1461. {
  1462. return jit_cc_new_reg(cc, JIT_REG_KIND_F32);
  1463. }
  1464. static inline JitReg
  1465. jit_cc_new_reg_F64(JitCompContext *cc)
  1466. {
  1467. return jit_cc_new_reg(cc, JIT_REG_KIND_F64);
  1468. }
  1469. static inline JitReg
  1470. jit_cc_new_reg_V64(JitCompContext *cc)
  1471. {
  1472. return jit_cc_new_reg(cc, JIT_REG_KIND_V64);
  1473. }
  1474. static inline JitReg
  1475. jit_cc_new_reg_V128(JitCompContext *cc)
  1476. {
  1477. return jit_cc_new_reg(cc, JIT_REG_KIND_V128);
  1478. }
  1479. static inline JitReg
  1480. jit_cc_new_reg_V256(JitCompContext *cc)
  1481. {
  1482. return jit_cc_new_reg(cc, JIT_REG_KIND_V256);
  1483. }
  1484. /**
  1485. * Get the hard register numbe of the given kind
  1486. *
  1487. * @param cc the compilation context
  1488. * @param kind the register kind
  1489. *
  1490. * @return number of hard registers of the given kind
  1491. */
  1492. static inline unsigned
  1493. jit_cc_hreg_num(JitCompContext *cc, unsigned kind)
  1494. {
  1495. bh_assert(kind < JIT_REG_KIND_L32);
  1496. return cc->hreg_info->info[kind].num;
  1497. }
  1498. /**
  1499. * Check whether a given register is a hard register.
  1500. *
  1501. * @param cc the compilation context
  1502. * @param reg the register which must be a variable
  1503. *
  1504. * @return true if the register is a hard register
  1505. */
  1506. static inline bool
  1507. jit_cc_is_hreg(JitCompContext *cc, JitReg reg)
  1508. {
  1509. unsigned kind = jit_reg_kind(reg);
  1510. unsigned no = jit_reg_no(reg);
  1511. bh_assert(jit_reg_is_variable(reg));
  1512. return no < cc->hreg_info->info[kind].num;
  1513. }
  1514. /**
  1515. * Check whether the given hard register is fixed.
  1516. *
  1517. * @param cc the compilation context
  1518. * @param reg the hard register
  1519. *
  1520. * @return true if the hard register is fixed
  1521. */
  1522. static inline bool
  1523. jit_cc_is_hreg_fixed(JitCompContext *cc, JitReg reg)
  1524. {
  1525. unsigned kind = jit_reg_kind(reg);
  1526. unsigned no = jit_reg_no(reg);
  1527. bh_assert(jit_cc_is_hreg(cc, reg));
  1528. return !!cc->hreg_info->info[kind].fixed[no];
  1529. }
  1530. /**
  1531. * Check whether the given hard register is caller-saved-native.
  1532. *
  1533. * @param cc the compilation context
  1534. * @param reg the hard register
  1535. *
  1536. * @return true if the hard register is caller-saved-native
  1537. */
  1538. static inline bool
  1539. jit_cc_is_hreg_caller_saved_native(JitCompContext *cc, JitReg reg)
  1540. {
  1541. unsigned kind = jit_reg_kind(reg);
  1542. unsigned no = jit_reg_no(reg);
  1543. bh_assert(jit_cc_is_hreg(cc, reg));
  1544. return !!cc->hreg_info->info[kind].caller_saved_native[no];
  1545. }
  1546. /**
  1547. * Check whether the given hard register is caller-saved-jitted.
  1548. *
  1549. * @param cc the compilation context
  1550. * @param reg the hard register
  1551. *
  1552. * @return true if the hard register is caller-saved-jitted
  1553. */
  1554. static inline bool
  1555. jit_cc_is_hreg_caller_saved_jitted(JitCompContext *cc, JitReg reg)
  1556. {
  1557. unsigned kind = jit_reg_kind(reg);
  1558. unsigned no = jit_reg_no(reg);
  1559. bh_assert(jit_cc_is_hreg(cc, reg));
  1560. return !!cc->hreg_info->info[kind].caller_saved_jitted[no];
  1561. }
  1562. /**
  1563. * Return the entry block of the compilation context.
  1564. *
  1565. * @param cc the compilation context
  1566. *
  1567. * @return the entry block of the compilation context
  1568. */
  1569. static inline JitBasicBlock *
  1570. jit_cc_entry_basic_block(JitCompContext *cc)
  1571. {
  1572. return *(jit_annl_basic_block(cc, cc->entry_label));
  1573. }
  1574. /**
  1575. * Return the exit block of the compilation context.
  1576. *
  1577. * @param cc the compilation context
  1578. *
  1579. * @return the exit block of the compilation context
  1580. */
  1581. static inline JitBasicBlock *
  1582. jit_cc_exit_basic_block(JitCompContext *cc)
  1583. {
  1584. return *(jit_annl_basic_block(cc, cc->exit_label));
  1585. }
  1586. void
  1587. jit_value_stack_push(JitValueStack *stack, JitValue *value);
  1588. JitValue *
  1589. jit_value_stack_pop(JitValueStack *stack);
  1590. void
  1591. jit_value_stack_destroy(JitValueStack *stack);
  1592. JitBlock *
  1593. jit_block_stack_top(JitBlockStack *stack);
  1594. void
  1595. jit_block_stack_push(JitBlockStack *stack, JitBlock *block);
  1596. JitBlock *
  1597. jit_block_stack_pop(JitBlockStack *stack);
  1598. void
  1599. jit_block_stack_destroy(JitBlockStack *stack);
  1600. bool
  1601. jit_block_add_incoming_insn(JitBlock *block, JitInsn *insn, uint32 opnd_idx);
  1602. void
  1603. jit_block_destroy(JitBlock *block);
  1604. bool
  1605. jit_cc_push_value(JitCompContext *cc, uint8 type, JitReg value);
  1606. bool
  1607. jit_cc_pop_value(JitCompContext *cc, uint8 type, JitReg *p_value);
  1608. bool
  1609. jit_lock_reg_in_insn(JitCompContext *cc, JitInsn *the_insn, JitReg reg_to_lock);
  1610. /**
  1611. * Update the control flow graph after successors of blocks are
  1612. * changed so that the predecessor vector of each block represents the
  1613. * updated status. The predecessors may not be required by all
  1614. * passes, so we don't need to keep them always being updated.
  1615. *
  1616. * @param cc the compilation context
  1617. *
  1618. * @return true if succeeds, false otherwise
  1619. */
  1620. bool
  1621. jit_cc_update_cfg(JitCompContext *cc);
  1622. /**
  1623. * Visit each normal block (which is not entry nor exit block) in a
  1624. * compilation context. New blocks can be added in the loop body, but
  1625. * they won't be visited. Blocks can also be removed safely (by
  1626. * setting the label's block annotation to NULL) in the loop body.
  1627. *
  1628. * @param CC (JitCompContext *) the compilation context
  1629. * @param I (unsigned) index variable of the block (label no)
  1630. * @param E (unsigned) end index variable of block (last index + 1)
  1631. * @param B (JitBasicBlock *) block pointer variable
  1632. */
  1633. #define JIT_FOREACH_BLOCK(CC, I, E, B) \
  1634. for ((I) = 2, (E) = (CC)->_ann._label_num; (I) < (E); (I)++) \
  1635. if (((B) = (CC)->_ann._label_basic_block[(I)]))
  1636. /**
  1637. * The version that includes entry and exit block.
  1638. */
  1639. #define JIT_FOREACH_BLOCK_ENTRY_EXIT(CC, I, E, B) \
  1640. for ((I) = 0, (E) = (CC)->_ann._label_num; (I) < (E); (I)++) \
  1641. if (((B) = (CC)->_ann._label_basic_block[(I)]))
  1642. /**
  1643. * Visit each normal block (which is not entry nor exit block) in a
  1644. * compilation context in reverse order. New blocks can be added in
  1645. * the loop body, but they won't be visited. Blocks can also be
  1646. * removed safely (by setting the label's block annotation to NULL) in
  1647. * the loop body.
  1648. *
  1649. * @param CC (JitCompContext *) the compilation context
  1650. * @param I (unsigned) index of the block (label no)
  1651. * @param B (JitBasicBlock *) block pointer
  1652. */
  1653. #define JIT_FOREACH_BLOCK_REVERSE(CC, I, B) \
  1654. for ((I) = (CC)->_ann._label_num; (I) > 2; (I)--) \
  1655. if (((B) = (CC)->_ann._label_basic_block[(I)-1]))
  1656. /**
  1657. * The version that includes entry and exit block.
  1658. */
  1659. #define JIT_FOREACH_BLOCK_REVERSE_ENTRY_EXIT(CC, I, B) \
  1660. for ((I) = (CC)->_ann._label_num; (I) > 0; (I)--) \
  1661. if (((B) = (CC)->_ann._label_basic_block[(I)-1]))
  1662. #ifdef __cplusplus
  1663. }
  1664. #endif
  1665. #endif /* end of _JIT_IR_H_ */