wasmtext.pas 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. { This file is part of wasmbin - a collection of WebAssembly binary utils.
  2. Copyright (C) 2019, 2020 Dmitry Boyarintsev <[email protected]>
  3. Copyright (C) 2020 by the Free Pascal development team
  4. This source is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free
  6. Software Foundation; either version 2 of the License, or (at your option)
  7. any later version.
  8. This code is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  10. FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  11. details.
  12. A copy of the GNU General Public License is available on the World Wide Web
  13. at <http://www.gnu.org/copyleft/gpl.html>. You can also obtain it by writing
  14. to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
  15. Boston, MA 02110-1335, USA.
  16. }
  17. unit wasmtext;
  18. interface
  19. // WebAssembly Wabt text utilities
  20. {$mode objfpc}{$H+}
  21. uses
  22. wasmbincode;
  23. type
  24. TInstText = record
  25. text : string;
  26. inst : byte;
  27. end;
  28. const
  29. WasmTextToInstr : array [MIN_INST..MAX_INST] of TInstText = (
  30. (text: 'unreachable' ; inst: inst_unreachable )
  31. ,(text: 'nop' ; inst: inst_nop )
  32. ,(text: 'block' ; inst: inst_block )
  33. ,(text: 'loop' ; inst: inst_loop )
  34. ,(text: 'if' ; inst: inst_if )
  35. ,(text: 'else' ; inst: inst_else )
  36. ,(text: '' ; inst: $06)
  37. ,(text: '' ; inst: $07)
  38. ,(text: '' ; inst: $08)
  39. ,(text: '' ; inst: $09)
  40. ,(text: '' ; inst: $0a)
  41. ,(text: 'end' ; inst: inst_end )
  42. ,(text: 'br' ; inst: inst_br)
  43. ,(text: 'br_if' ; inst: inst_br_if)
  44. ,(text: 'br_table' ; inst: inst_br_table)
  45. ,(text: 'return' ; inst: inst_return)
  46. ,(text: 'call' ; inst: inst_call)
  47. ,(text: 'call_indirect' ; inst: inst_call_indirect)
  48. ,(text: '' ; inst: $12)
  49. ,(text: '' ; inst: $13)
  50. ,(text: '' ; inst: $14)
  51. ,(text: '' ; inst: $15)
  52. ,(text: '' ; inst: $16)
  53. ,(text: '' ; inst: $17)
  54. ,(text: '' ; inst: $18)
  55. ,(text: '' ; inst: $19)
  56. ,(text: 'drop' ; inst: inst_drop )
  57. ,(text: 'select' ; inst: inst_select )
  58. ,(text: '' ; inst: $1c)
  59. ,(text: '' ; inst: $1d)
  60. ,(text: '' ; inst: $1e)
  61. ,(text: '' ; inst: $1f)
  62. ,(text: 'local.get' ; inst: inst_local_get)
  63. ,(text: 'local.set' ; inst: inst_local_set)
  64. ,(text: 'local.tee' ; inst: inst_local_tee)
  65. ,(text: 'global.get' ; inst: inst_global_get)
  66. ,(text: 'global.set' ; inst: inst_global_set)
  67. ,(text: '' ; inst: $25)
  68. ,(text: '' ; inst: $26)
  69. ,(text: '' ; inst: $27)
  70. ,(text: 'i32.load' ; inst: inst_i32_load)
  71. ,(text: 'i64.load' ; inst: inst_i64_load)
  72. ,(text: 'f32.load' ; inst: inst_f32_load)
  73. ,(text: 'f64.load' ; inst: inst_f64_load)
  74. ,(text: 'i32.load8_s' ; inst: inst_i32_load8_s)
  75. ,(text: 'i32.load8_u' ; inst: inst_i32_load8_u)
  76. ,(text: 'i32.load16_s' ; inst: inst_i32_load16_s)
  77. ,(text: 'i32.load16_u' ; inst: inst_i32_load16_u)
  78. ,(text: 'i64.load8_s' ; inst: inst_i64_load8_s)
  79. ,(text: 'i64.load8_u' ; inst: inst_i64_load8_u)
  80. ,(text: 'i64.load16_s' ; inst: inst_i64_load16_s)
  81. ,(text: 'i64.load16_u' ; inst: inst_i64_load16_u)
  82. ,(text: 'i64.load32_s' ; inst: inst_i64_load32_s)
  83. ,(text: 'i64.load32_u' ; inst: inst_i64_load32_u)
  84. ,(text: 'i32.store' ; inst: inst_i32_store)
  85. ,(text: 'i64.store' ; inst: inst_i64_store)
  86. ,(text: 'f32.store' ; inst: inst_f32_store)
  87. ,(text: 'f64.store' ; inst: inst_f64_store)
  88. ,(text: 'i32.store8' ; inst: inst_i32_store8 )
  89. ,(text: 'i32.store16' ; inst: inst_i32_store16)
  90. ,(text: 'i64.store8' ; inst: inst_i64_store8 )
  91. ,(text: 'i64.store16' ; inst: inst_i64_store16)
  92. ,(text: 'i64.store32' ; inst: inst_i64_store32)
  93. ,(text: 'memory.size' ; inst: inst_memory_size)
  94. ,(text: 'memory.grow' ; inst: inst_memory_grow)
  95. ,(text: 'i32.const' ; inst: inst_i32_const)
  96. ,(text: 'i64.const' ; inst: inst_i64_const)
  97. ,(text: 'f32.const' ; inst: inst_f32_const)
  98. ,(text: 'f64.const' ; inst: inst_f64_const)
  99. ,(text: 'i32.eqz' ; inst: inst_i32_eqz)
  100. ,(text: 'i32.eq' ; inst: inst_i32_eq )
  101. ,(text: 'i32.ne' ; inst: inst_i32_ne )
  102. ,(text: 'i32.lt_s' ; inst: inst_i32_lt_s)
  103. ,(text: 'i32.lt_u' ; inst: inst_i32_lt_u)
  104. ,(text: 'i32.gt_s' ; inst: inst_i32_gt_s)
  105. ,(text: 'i32.gt_u' ; inst: inst_i32_gt_u)
  106. ,(text: 'i32.le_s' ; inst: inst_i32_le_s)
  107. ,(text: 'i32.le_u' ; inst: inst_i32_le_u)
  108. ,(text: 'i32.ge_s' ; inst: inst_i32_ge_s)
  109. ,(text: 'i32.ge_u' ; inst: inst_i32_ge_u)
  110. ,(text: 'i64.eqz' ; inst: inst_i64_eqz)
  111. ,(text: 'i64.eq' ; inst: inst_i64_eq)
  112. ,(text: 'i64.ne' ; inst: inst_i64_ne)
  113. ,(text: 'i64.lt_s' ; inst: inst_i64_lt_s)
  114. ,(text: 'i64.lt_u' ; inst: inst_i64_lt_u)
  115. ,(text: 'i64.gt_s' ; inst: inst_i64_gt_s)
  116. ,(text: 'i64.gt_u' ; inst: inst_i64_gt_u)
  117. ,(text: 'i64.le_s' ; inst: inst_i64_le_s)
  118. ,(text: 'i64.le_u' ; inst: inst_i64_le_u)
  119. ,(text: 'i64.ge_s' ; inst: inst_i64_ge_s)
  120. ,(text: 'i64.ge_u' ; inst: inst_i64_ge_u)
  121. ,(text: 'f32.eq' ; inst: inst_f32_eq)
  122. ,(text: 'f32.ne' ; inst: inst_f32_ne)
  123. ,(text: 'f32.lt' ; inst: inst_f32_lt)
  124. ,(text: 'f32.gt' ; inst: inst_f32_gt)
  125. ,(text: 'f32.le' ; inst: inst_f32_le)
  126. ,(text: 'f32.ge' ; inst: inst_f32_ge)
  127. ,(text: 'f64.eq' ; inst: inst_f64_eq)
  128. ,(text: 'f64.ne' ; inst: inst_f64_ne)
  129. ,(text: 'f64.lt' ; inst: inst_f64_lt)
  130. ,(text: 'f64.gt' ; inst: inst_f64_gt)
  131. ,(text: 'f64.le' ; inst: inst_f64_le)
  132. ,(text: 'f64.ge' ; inst: inst_f64_ge)
  133. ,(text: 'i32.clz' ; inst: inst_i32_clz)
  134. ,(text: 'i32.ctz' ; inst: inst_i32_ctz)
  135. ,(text: 'i32.popcnt' ; inst: inst_i32_popcnt)
  136. ,(text: 'i32.add' ; inst: inst_i32_add)
  137. ,(text: 'i32.sub' ; inst: inst_i32_sub)
  138. ,(text: 'i32.mul' ; inst: inst_i32_mul)
  139. ,(text: 'i32.div_s' ; inst: inst_i32_div_s)
  140. ,(text: 'i32.div_u' ; inst: inst_i32_div_u)
  141. ,(text: 'i32.rem_s' ; inst: inst_i32_rem_s)
  142. ,(text: 'i32.rem_u' ; inst: inst_i32_rem_u)
  143. ,(text: 'i32.and' ; inst: inst_i32_and)
  144. ,(text: 'i32.or' ; inst: inst_i32_or )
  145. ,(text: 'i32.xor' ; inst: inst_i32_xor)
  146. ,(text: 'i32.shl' ; inst: inst_i32_shl)
  147. ,(text: 'i32.shr_s' ; inst: inst_i32_shr_s)
  148. ,(text: 'i32.shr_u' ; inst: inst_i32_shr_u)
  149. ,(text: 'i32.rotl' ; inst: inst_i32_rotl)
  150. ,(text: 'i32.rotr' ; inst: inst_i32_rotr)
  151. ,(text: 'i64.clz' ; inst: inst_i64_clz)
  152. ,(text: 'i64.ctz' ; inst: inst_i64_ctz)
  153. ,(text: 'i64.popcnt' ; inst: inst_i64_popcnt)
  154. ,(text: 'i64.add' ; inst: inst_i64_add)
  155. ,(text: 'i64.sub' ; inst: inst_i64_sub)
  156. ,(text: 'i64.mul' ; inst: inst_i64_mul)
  157. ,(text: 'i64.div_s' ; inst: inst_i64_div_s)
  158. ,(text: 'i64.div_u' ; inst: inst_i64_div_u)
  159. ,(text: 'i64.rem_s' ; inst: inst_i64_rem_s)
  160. ,(text: 'i64.rem_u' ; inst: inst_i64_rem_u)
  161. ,(text: 'i64.and' ; inst: inst_i64_and)
  162. ,(text: 'i64.or' ; inst: inst_i64_or )
  163. ,(text: 'i64.xor' ; inst: inst_i64_xor)
  164. ,(text: 'i64.shl' ; inst: inst_i64_shl)
  165. ,(text: 'i64.shr_s' ; inst: inst_i64_shr_s)
  166. ,(text: 'i64.shr_u' ; inst: inst_i64_shr_u)
  167. ,(text: 'i64.rotl' ; inst: inst_i64_rotl)
  168. ,(text: 'i64.rotr' ; inst: inst_i64_rotr)
  169. ,(text: 'f32.abs' ; inst: inst_f32_abs )
  170. ,(text: 'f32.neg' ; inst: inst_f32_neg )
  171. ,(text: 'f32.ceil' ; inst: inst_f32_ceil)
  172. ,(text: 'f32.floor' ; inst: inst_f32_floor)
  173. ,(text: 'f32.trunc' ; inst: inst_f32_trunc)
  174. ,(text: 'f32.nearest' ; inst: inst_f32_nearest)
  175. ,(text: 'f32.sqrt' ; inst: inst_f32_sqrt)
  176. ,(text: 'f32.add' ; inst: inst_f32_add)
  177. ,(text: 'f32.sub' ; inst: inst_f32_sub)
  178. ,(text: 'f32.mul' ; inst: inst_f32_mul)
  179. ,(text: 'f32.div' ; inst: inst_f32_div)
  180. ,(text: 'f32.min' ; inst: inst_f32_min)
  181. ,(text: 'f32.max' ; inst: inst_f32_max)
  182. ,(text: 'f32.copysign' ; inst: inst_f32_copysign)
  183. ,(text: 'f64.abs' ; inst: inst_f64_abs )
  184. ,(text: 'f64.neg' ; inst: inst_f64_neg )
  185. ,(text: 'f64.ceil' ; inst: inst_f64_ceil)
  186. ,(text: 'f64.floor' ; inst: inst_f64_floor)
  187. ,(text: 'f64.trunc' ; inst: inst_f64_trunc)
  188. ,(text: 'f64.nearest' ; inst: inst_f64_nearest)
  189. ,(text: 'f64.sqrt' ; inst: inst_f64_sqrt)
  190. ,(text: 'f64.add' ; inst: inst_f64_add)
  191. ,(text: 'f64.sub' ; inst: inst_f64_sub)
  192. ,(text: 'f64.mul' ; inst: inst_f64_mul)
  193. ,(text: 'f64.div' ; inst: inst_f64_div)
  194. ,(text: 'f64.min' ; inst: inst_f64_min)
  195. ,(text: 'f64.max' ; inst: inst_f64_max)
  196. ,(text: 'f64.copysign' ; inst: inst_f64_copysign)
  197. ,(text: 'i32.wrap_i64' ; inst: inst_i32_wrap_i64)
  198. ,(text: 'i32.trunc_f32_s' ; inst: inst_i32_trunc_f32_s)
  199. ,(text: 'i32.trunc_f32_u' ; inst: inst_i32_trunc_f32_u)
  200. ,(text: 'i32.trunc_f64_s' ; inst: inst_i32_trunc_f64_s)
  201. ,(text: 'i32.trunc_f64_u' ; inst: inst_i32_trunc_f64_u)
  202. ,(text: 'i64.extend_i32_s' ; inst: inst_i64_extend_i32_s)
  203. ,(text: 'i64.extend_i32_u' ; inst: inst_i64_extend_i32_u)
  204. ,(text: 'i64.trunc_f32_s' ; inst: inst_i64_trunc_f32_s)
  205. ,(text: 'i64.trunc_f32_u' ; inst: inst_i64_trunc_f32_u)
  206. ,(text: 'i64.trunc_f64_s' ; inst: inst_i64_trunc_f64_s)
  207. ,(text: 'i64.trunc_f64_u' ; inst: inst_i64_trunc_f64_u)
  208. ,(text: 'f32.convert_i32_s' ; inst: inst_f32_convert_i32_s)
  209. ,(text: 'f32.convert_i32_u' ; inst: inst_f32_convert_i32_u)
  210. ,(text: 'f32.convert_i64_s' ; inst: inst_f32_convert_i64_s)
  211. ,(text: 'f32.convert_i64_u' ; inst: inst_f32_convert_i64_u)
  212. ,(text: 'f32.demote_f64' ; inst: inst_f32_demote_f64)
  213. ,(text: 'f64.convert_i32_s' ; inst: inst_f64_convert_i32_s)
  214. ,(text: 'f64.convert_i32_u' ; inst: inst_f64_convert_i32_u)
  215. ,(text: 'f64.convert_i64_s' ; inst: inst_f64_convert_i64_s)
  216. ,(text: 'f64.convert_i64_u' ; inst: inst_f64_convert_i64_u)
  217. ,(text: 'f64.promote_f32' ; inst: inst_f64_promote_f32)
  218. ,(text: 'i32.reinterpret_f32' ; inst: inst_i32_reinterpret_f32)
  219. ,(text: 'i64.reinterpret_f64' ; inst: inst_i64_reinterpret_f64)
  220. ,(text: 'f32.reinterpret_i32' ; inst: inst_f32_reinterpret_i32)
  221. ,(text: 'f64.reinterpret_i64' ; inst: inst_f64_reinterpret_i64)
  222. );
  223. function TextToInst(const t: string; out inst: byte): Boolean;
  224. implementation
  225. function floatTextToInst(const t: string; var inst: byte): Boolean;
  226. begin
  227. Result := length(t)>4;
  228. if not Result then Exit;
  229. if (t[2]='3') and (t[3]='2') then begin
  230. if t = 'f32.load' then inst := inst_f32_load
  231. else if t = 'f32.store' then inst := inst_f32_store
  232. else if t = 'f32.const' then inst := inst_f32_const
  233. else if t = 'f32.eq' then inst := inst_f32_eq
  234. else if t = 'f32.ne' then inst := inst_f32_ne
  235. else if t = 'f32.lt' then inst := inst_f32_lt
  236. else if t = 'f32.gt' then inst := inst_f32_gt
  237. else if t = 'f32.le' then inst := inst_f32_le
  238. else if t = 'f32.ge' then inst := inst_f32_ge
  239. else if t = 'f32.abs' then inst := inst_f32_abs
  240. else if t = 'f32.neg' then inst := inst_f32_neg
  241. else if t = 'f32.ceil' then inst := inst_f32_ceil
  242. else if t = 'f32.floor' then inst := inst_f32_floor
  243. else if t = 'f32.trunc' then inst := inst_f32_trunc
  244. else if t = 'f32.nearest' then inst := inst_f32_nearest
  245. else if t = 'f32.sqrt' then inst := inst_f32_sqrt
  246. else if t = 'f32.add' then inst := inst_f32_add
  247. else if t = 'f32.sub' then inst := inst_f32_sub
  248. else if t = 'f32.mul' then inst := inst_f32_mul
  249. else if t = 'f32.div' then inst := inst_f32_div
  250. else if t = 'f32.min' then inst := inst_f32_min
  251. else if t = 'f32.max' then inst := inst_f32_max
  252. else if t = 'f32.copysign' then inst := inst_f32_copysign
  253. else if t = 'f32.convert_i32_s' then inst := inst_f32_convert_i32_s
  254. else if t = 'f32.convert_s/i32' then inst := inst_f32_convert_i32_s
  255. else if t = 'f32.convert_i32_u' then inst := inst_f32_convert_i32_u
  256. else if t = 'f32.convert_u/i32' then inst := inst_f32_convert_i32_u
  257. else if t = 'f32.convert_i64_s' then inst := inst_f32_convert_i64_s
  258. else if t = 'f32.convert_s/i64' then inst := inst_f32_convert_i64_s
  259. else if t = 'f32.convert_i64_u' then inst := inst_f32_convert_i64_u
  260. else if t = 'f32.convert_u/i64' then inst := inst_f32_convert_i64_u
  261. else if t = 'f32.demote_f64' then inst := inst_f32_demote_f64
  262. else if t = 'f32.demote/f64' then inst := inst_f32_demote_f64
  263. else if t = 'f32.reinterpret_i32' then inst := inst_f32_reinterpret_i32
  264. else if t = 'f32.reinterpret/i32' then inst := inst_f32_reinterpret_i32
  265. else Result := false;
  266. end else if (t[2]='6') and (t[3]='4') then begin
  267. if t = 'f64.load' then inst := inst_f64_load
  268. else if t = 'f64.store' then inst := inst_f64_store
  269. else if t = 'f64.const' then inst := inst_f64_const
  270. else if t = 'f64.eq' then inst := inst_f64_eq
  271. else if t = 'f64.ne' then inst := inst_f64_ne
  272. else if t = 'f64.lt' then inst := inst_f64_lt
  273. else if t = 'f64.gt' then inst := inst_f64_gt
  274. else if t = 'f64.le' then inst := inst_f64_le
  275. else if t = 'f64.ge' then inst := inst_f64_ge
  276. else if t = 'f64.abs' then inst := inst_f64_abs
  277. else if t = 'f64.neg' then inst := inst_f64_neg
  278. else if t = 'f64.ceil' then inst := inst_f64_ceil
  279. else if t = 'f64.floor' then inst := inst_f64_floor
  280. else if t = 'f64.trunc' then inst := inst_f64_trunc
  281. else if t = 'f64.nearest' then inst := inst_f64_nearest
  282. else if t = 'f64.sqrt' then inst := inst_f64_sqrt
  283. else if t = 'f64.add' then inst := inst_f64_add
  284. else if t = 'f64.sub' then inst := inst_f64_sub
  285. else if t = 'f64.mul' then inst := inst_f64_mul
  286. else if t = 'f64.div' then inst := inst_f64_div
  287. else if t = 'f64.min' then inst := inst_f64_min
  288. else if t = 'f64.max' then inst := inst_f64_max
  289. else if t = 'f64.copysign' then inst := inst_f64_copysign
  290. else if t = 'f64.convert_i32_s' then inst := inst_f64_convert_i32_s
  291. else if t = 'f64.convert_s/i32' then inst := inst_f64_convert_i32_s
  292. else if t = 'f64.convert_i32_u' then inst := inst_f64_convert_i32_u
  293. else if t = 'f64.convert_u/i32' then inst := inst_f64_convert_i32_u
  294. else if t = 'f64.convert_i64_s' then inst := inst_f64_convert_i64_s
  295. else if t = 'f64.convert_s/i64' then inst := inst_f64_convert_i64_s
  296. else if t = 'f64.convert_i64_u' then inst := inst_f64_convert_i64_u
  297. else if t = 'f64.convert_u/i64' then inst := inst_f64_convert_i64_u
  298. else if t = 'f64.promote_f32' then inst := inst_f64_promote_f32
  299. else if t = 'f64.promote/f32' then inst := inst_f64_promote_f32
  300. else if t = 'f64.reinterpret_i64' then inst := inst_f64_reinterpret_i64
  301. else if t = 'f64.reinterpret/i64' then inst := inst_f64_reinterpret_i64
  302. else Result := false;
  303. end;
  304. end;
  305. function intTextToInst(const t: string; var inst: byte): Boolean;
  306. begin
  307. Result := length(t)>4;
  308. if not Result then Exit;
  309. if (t[2]='3') and (t[3]='2') then begin
  310. if t = 'i32.load' then inst := inst_i32_load
  311. else if t = 'i32.load8_s' then inst := inst_i32_load8_s
  312. else if t = 'i32.load8_u' then inst := inst_i32_load8_u
  313. else if t = 'i32.load16_s' then inst := inst_i32_load16_s
  314. else if t = 'i32.load16_u' then inst := inst_i32_load16_u
  315. else if t = 'i32.store' then inst := inst_i32_store
  316. else if t = 'i32.store8' then inst := inst_i32_store8
  317. else if t = 'i32.store16' then inst := inst_i32_store16
  318. else if t = 'i32.const' then inst := inst_i32_const
  319. else if t = 'i32.eqz' then inst := inst_i32_eqz
  320. else if t = 'i32.eq' then inst := inst_i32_eq
  321. else if t = 'i32.ne' then inst := inst_i32_ne
  322. else if t = 'i32.lt_s' then inst := inst_i32_lt_s
  323. else if t = 'i32.lt_u' then inst := inst_i32_lt_u
  324. else if t = 'i32.gt_s' then inst := inst_i32_gt_s
  325. else if t = 'i32.gt_u' then inst := inst_i32_gt_u
  326. else if t = 'i32.le_s' then inst := inst_i32_le_s
  327. else if t = 'i32.le_u' then inst := inst_i32_le_u
  328. else if t = 'i32.ge_s' then inst := inst_i32_ge_s
  329. else if t = 'i32.ge_u' then inst := inst_i32_ge_u
  330. else if t = 'i32.clz' then inst := inst_i32_clz
  331. else if t = 'i32.ctz' then inst := inst_i32_ctz
  332. else if t = 'i32.popcnt' then inst := inst_i32_popcnt
  333. else if t = 'i32.add' then inst := inst_i32_add
  334. else if t = 'i32.sub' then inst := inst_i32_sub
  335. else if t = 'i32.mul' then inst := inst_i32_mul
  336. else if t = 'i32.div_s' then inst := inst_i32_div_s
  337. else if t = 'i32.div_u' then inst := inst_i32_div_u
  338. else if t = 'i32.rem_s' then inst := inst_i32_rem_s
  339. else if t = 'i32.rem_u' then inst := inst_i32_rem_u
  340. else if t = 'i32.and' then inst := inst_i32_and
  341. else if t = 'i32.or' then inst := inst_i32_or
  342. else if t = 'i32.xor' then inst := inst_i32_xor
  343. else if t = 'i32.shl' then inst := inst_i32_shl
  344. else if t = 'i32.shr_s' then inst := inst_i32_shr_s
  345. else if t = 'i32.shr_u' then inst := inst_i32_shr_u
  346. else if t = 'i32.rotl' then inst := inst_i32_rotl
  347. else if t = 'i32.rotr' then inst := inst_i32_rotr
  348. else if t = 'i32.wrap_i64' then inst := inst_i32_wrap_i64
  349. else if t = 'i32.wrap/i64' then inst := inst_i32_wrap_i64
  350. else if t = 'i32.trunc_f32_s' then inst := inst_i32_trunc_f32_s
  351. else if t = 'i32.trunc_s/f32' then inst := inst_i32_trunc_f32_s
  352. else if t = 'i32.trunc_f32_u' then inst := inst_i32_trunc_f32_u
  353. else if t = 'i32.trunc_u/f32' then inst := inst_i32_trunc_f32_u
  354. else if t = 'i32.trunc_f64_s' then inst := inst_i32_trunc_f64_s
  355. else if t = 'i32.trunc_s/f64' then inst := inst_i32_trunc_f64_s
  356. else if t = 'i32.trunc_f64_u' then inst := inst_i32_trunc_f64_u
  357. else if t = 'i32.trunc_u/f64' then inst := inst_i32_trunc_f64_u
  358. else if t = 'i32.reinterpret_f32' then inst := inst_i32_reinterpret_f32
  359. else if t = 'i32.reinterpret/f32' then inst := inst_i32_reinterpret_f32
  360. else Result := false;
  361. end else if (t[2]='6') and (t[3]='4') then begin
  362. if t = 'i64.load8_s' then inst := inst_i64_load8_s
  363. else if t = 'i64.load8_u' then inst := inst_i64_load8_u
  364. else if t = 'i64.load16_s' then inst := inst_i64_load16_s
  365. else if t = 'i64.load16_u' then inst := inst_i64_load16_u
  366. else if t = 'i64.load32_s' then inst := inst_i64_load32_s
  367. else if t = 'i64.load32_u' then inst := inst_i64_load32_u
  368. else if t = 'i64.store' then inst := inst_i64_store
  369. else if t = 'i64.store8' then inst := inst_i64_store8
  370. else if t = 'i64.store16' then inst := inst_i64_store16
  371. else if t = 'i64.store32' then inst := inst_i64_store32
  372. else if t = 'i64.const' then inst := inst_i64_const
  373. else if t = 'i64.eqz' then inst := inst_i64_eqz
  374. else if t = 'i64.eq' then inst := inst_i64_eq
  375. else if t = 'i64.ne' then inst := inst_i64_ne
  376. else if t = 'i64.lt_s' then inst := inst_i64_lt_s
  377. else if t = 'i64.lt_u' then inst := inst_i64_lt_u
  378. else if t = 'i64.gt_s' then inst := inst_i64_gt_s
  379. else if t = 'i64.gt_u' then inst := inst_i64_gt_u
  380. else if t = 'i64.le_s' then inst := inst_i64_le_s
  381. else if t = 'i64.le_u' then inst := inst_i64_le_u
  382. else if t = 'i64.ge_s' then inst := inst_i64_ge_s
  383. else if t = 'i64.ge_u' then inst := inst_i64_ge_u
  384. else if t = 'i64.clz' then inst := inst_i64_clz
  385. else if t = 'i64.ctz' then inst := inst_i64_ctz
  386. else if t = 'i64.popcnt' then inst := inst_i64_popcnt
  387. else if t = 'i64.add' then inst := inst_i64_add
  388. else if t = 'i64.sub' then inst := inst_i64_sub
  389. else if t = 'i64.mul' then inst := inst_i64_mul
  390. else if t = 'i64.div_s' then inst := inst_i64_div_s
  391. else if t = 'i64.div_u' then inst := inst_i64_div_u
  392. else if t = 'i64.rem_s' then inst := inst_i64_rem_s
  393. else if t = 'i64.rem_u' then inst := inst_i64_rem_u
  394. else if t = 'i64.and' then inst := inst_i64_and
  395. else if t = 'i64.or' then inst := inst_i64_or
  396. else if t = 'i64.xor' then inst := inst_i64_xor
  397. else if t = 'i64.shl' then inst := inst_i64_shl
  398. else if t = 'i64.shr_s' then inst := inst_i64_shr_s
  399. else if t = 'i64.shr_u' then inst := inst_i64_shr_u
  400. else if t = 'i64.rotl' then inst := inst_i64_rotl
  401. else if t = 'i64.rotr' then inst := inst_i64_rotr
  402. else if t = 'i64.extend_i32_s' then inst := inst_i64_extend_i32_s
  403. else if t = 'i64.extend_s/i32' then inst := inst_i64_extend_i32_s
  404. else if t = 'i64.extend_i32_u' then inst := inst_i64_extend_i32_u
  405. else if t = 'i64.extend_u/i32' then inst := inst_i64_extend_i32_u
  406. else if t = 'i64.trunc_f32_s' then inst := inst_i64_trunc_f32_s
  407. else if t = 'i64.trunc_s/f32' then inst := inst_i64_trunc_f32_s
  408. else if t = 'i64.trunc_f32_u' then inst := inst_i64_trunc_f32_u
  409. else if t = 'i64.trunc_u/f32' then inst := inst_i64_trunc_f32_u
  410. else if t = 'i64.trunc_f64_s' then inst := inst_i64_trunc_f64_s
  411. else if t = 'i64.trunc_s/f64' then inst := inst_i64_trunc_f64_s
  412. else if t = 'i64.trunc_f64_u' then inst := inst_i64_trunc_f64_u
  413. else if t = 'i64.trunc_u/f64' then inst := inst_i64_trunc_f64_u
  414. else if t = 'i64.load' then inst := inst_i64_load
  415. else if t = 'i64.reinterpret_f64' then inst := inst_i64_reinterpret_f64
  416. else if t = 'i64.reinterpret/f64' then inst := inst_i64_reinterpret_f64
  417. else Result := false;
  418. end else
  419. Result := false;
  420. end;
  421. function TextToInst(const t: string; out inst: byte): Boolean;
  422. begin
  423. inst:=0;
  424. Result := length(t)>0;
  425. if not Result then Exit;
  426. case t[1] of
  427. 'b':
  428. if t = 'block' then inst := inst_block
  429. else if t = 'br' then inst := inst_br
  430. else if t = 'br_if' then inst := inst_br_if
  431. else if t = 'br_table' then inst := inst_br_table
  432. else Result := false;
  433. 'c':
  434. if t = 'call' then inst := inst_call
  435. else if t = 'call_indirect' then inst := inst_call_indirect
  436. else if t = 'current_memory' then inst := INST_memory_size
  437. else Result := false;
  438. 'd':
  439. if t = 'drop' then inst := inst_drop
  440. else Result := false;
  441. 'e':
  442. if t = 'else' then inst := inst_else
  443. else if t = 'end' then inst := inst_end
  444. else Result := false;
  445. 'f':
  446. Result := floatTextToInst(t, inst);
  447. 'g':
  448. if t = 'global.get' then inst := inst_global_get
  449. else if t = 'global.set' then inst := inst_global_set
  450. // wabt
  451. else if t = 'get_local' then inst := INST_local_get
  452. else if t = 'get_global' then inst := INST_global_get
  453. else if t = 'grow_memory' then inst := inst_memory_grow
  454. else Result := false;
  455. 'i':
  456. if t = 'if' then inst := inst_if
  457. else Result := intTextToInst(t, inst);
  458. 'l':
  459. if t = 'local.get' then inst := inst_local_get
  460. else if t = 'local.set' then inst := inst_local_set
  461. else if t = 'local.tee' then inst := inst_local_tee
  462. else if t = 'loop' then inst := inst_loop
  463. else Result := false;
  464. 'm':
  465. if t = 'memory.size' then inst := inst_memory_size
  466. else if t = 'memory.grow' then inst := inst_memory_grow
  467. else Result := false;
  468. 'n':
  469. if t = 'nop' then inst := inst_nop
  470. else Result := false;
  471. 'r':
  472. if t = 'return' then inst := inst_return
  473. else Result := false;
  474. 's':
  475. if t = 'select' then inst := inst_select
  476. // wabt
  477. else if t = 'set_local' then inst := INST_local_set
  478. else if t = 'set_global' then inst := INST_global_set
  479. else Result := false;
  480. 't':
  481. if t = 'tee_local' then inst := INST_local_tee;
  482. 'u':
  483. if t ='unreachable' then inst := inst_unreachable
  484. else Result := false;
  485. else
  486. Result := false;
  487. end;
  488. end;
  489. end.