aasmcpu.pas 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206
  1. {
  2. Copyright (c) 2019 by Free Pascal and Lazarus foundation
  3. Contains the assembler object for the WebAssembly
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit aasmcpu;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cclasses,
  22. globtype,globals,verbose,
  23. aasmbase,aasmtai,aasmdata,aasmsym,
  24. cgbase,cgutils,cpubase,cpuinfo,ogbase,
  25. widestr;
  26. { fake, there are no "mov reg,reg" instructions here }
  27. const
  28. { "mov reg,reg" source operand number }
  29. O_MOV_SOURCE = 0;
  30. { "mov reg,reg" source operand number }
  31. O_MOV_DEST = 0;
  32. type
  33. { taicpu }
  34. taicpu = class(tai_cpu_abstract_sym)
  35. constructor op_none(op : tasmop);
  36. constructor op_reg(op : tasmop;_op1 : tregister);
  37. constructor op_const(op : tasmop;_op1 : aint);
  38. constructor op_ref(op : tasmop;const _op1 : treference);
  39. constructor op_sym(op : tasmop;_op1 : tasmsymbol);
  40. constructor op_sym_const(op : tasmop;_op1 : tasmsymbol;_op2 : aint);
  41. constructor op_single(op : tasmop;_op1 : single);
  42. constructor op_double(op : tasmop;_op1 : double);
  43. constructor op_functype(op : tasmop; _op1: TWasmFuncType);
  44. procedure loadfunctype(opidx:longint;ft:TWasmFuncType);
  45. procedure loadsingle(opidx:longint;f:single);
  46. procedure loaddouble(opidx:longint;d:double);
  47. { register allocation }
  48. function is_same_reg_move(regtype: Tregistertype):boolean; override;
  49. { register spilling code }
  50. function spilling_get_operation_type(opnr: longint): topertype;override;
  51. function Pass1(objdata:TObjData):longint;override;
  52. procedure Pass2(objdata:TObjData);override;
  53. end;
  54. tai_align = class(tai_align_abstract)
  55. { nothing to add }
  56. end;
  57. TImpExpType= (
  58. ie_Func, // functions
  59. ie_Table, // tables (arrays of methods)
  60. ie_Memory, // memory reference
  61. ie_Global // global variables
  62. );
  63. // the actual use is defined by the assembly section used
  64. { timpexp_ai }
  65. tai_impexp = class(tai)
  66. extname : ansistring; // external name
  67. intname : ansistring; // internal name
  68. extmodule : ansistring; // external unit name
  69. symstype: TImpExpType;
  70. constructor create(const aextname, aintname: ansistring; asymtype: timpexptype); overload;
  71. constructor create(const aextmodule, aextname, aintname: ansistring; asymtype: timpexptype); overload;
  72. end;
  73. // local variable declaration
  74. { tai_local }
  75. tai_local = class(tai)
  76. bastyp: TWasmBasicType;
  77. name : string;
  78. first: boolean;
  79. last: boolean;
  80. constructor create(abasictype: TWasmBasicType; const aname: string = '');
  81. end;
  82. { tai_functype }
  83. tai_functype = class(tai)
  84. funcname: string;
  85. functype: TWasmFuncType;
  86. constructor create(const afuncname: string; afunctype: TWasmFuncType);
  87. destructor destroy;override;
  88. end;
  89. { tai_tagtype }
  90. tai_tagtype = class(tai)
  91. tagname: string;
  92. params: TWasmResultType;
  93. constructor create(const atagname: string; aparams: TWasmResultType);
  94. end;
  95. { tai_import_module }
  96. tai_import_module = class(tai)
  97. symname: string;
  98. importmodule: string;
  99. constructor create(const asymname, aimportmodule: string);
  100. end;
  101. { tai_import_name }
  102. tai_import_name = class(tai)
  103. symname: string;
  104. importname: string;
  105. constructor create(const asymname, aimportname: string);
  106. end;
  107. procedure InitAsm;
  108. procedure DoneAsm;
  109. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  110. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  111. implementation
  112. uses
  113. ogwasm;
  114. { tai_import_name }
  115. constructor tai_import_name.create(const asymname, aimportname: string);
  116. begin
  117. inherited Create;
  118. typ:=ait_import_name;
  119. symname:=asymname;
  120. importname:=aimportname;
  121. end;
  122. { tai_import_module }
  123. constructor tai_import_module.create(const asymname, aimportmodule: string);
  124. begin
  125. inherited Create;
  126. typ:=ait_import_module;
  127. symname:=asymname;
  128. importmodule:=aimportmodule;
  129. end;
  130. { tai_functype }
  131. constructor tai_functype.create(const afuncname: string; afunctype: TWasmFuncType);
  132. begin
  133. inherited Create;
  134. typ:=ait_functype;
  135. funcname:=afuncname;
  136. functype:=afunctype;
  137. end;
  138. destructor tai_functype.destroy;
  139. begin
  140. functype.free;
  141. inherited;
  142. end;
  143. { tai_tagtype }
  144. constructor tai_tagtype.create(const atagname: string; aparams: TWasmResultType);
  145. begin
  146. typ:=ait_tagtype;
  147. tagname:=atagname;
  148. params:=aparams;
  149. end;
  150. { tai_local }
  151. constructor tai_local.create(abasictype: TWasmBasicType; const aname: string);
  152. begin
  153. inherited Create;
  154. bastyp := abasictype;
  155. typ := ait_local;
  156. name := aname;
  157. end;
  158. { timpexp_ai }
  159. constructor tai_impexp.create(const aextname, aintname: ansistring;
  160. asymtype: timpexptype);
  161. begin
  162. create('', aextname, aintname, asymtype);;
  163. end;
  164. constructor tai_impexp.create(const aextmodule, aextname, aintname: ansistring; asymtype: timpexptype);
  165. begin
  166. inherited create;
  167. typ := ait_importexport;
  168. extmodule := aextmodule;
  169. extname := aextname;
  170. intname := aintname;
  171. symstype:= asymtype;
  172. end;
  173. {*****************************************************************************
  174. taicpu Constructors
  175. *****************************************************************************}
  176. constructor taicpu.op_none(op : tasmop);
  177. begin
  178. inherited create(op);
  179. end;
  180. constructor taicpu.op_reg(op : tasmop;_op1 : tregister);
  181. begin
  182. inherited create(op);
  183. ops:=1;
  184. {$ifdef EXTDEBUG}
  185. if getregtype(_op1)=R_INVALIDREGISTER then
  186. InternalError(2021011901);
  187. {$endif EXTDEBUG}
  188. loadreg(0,_op1);
  189. end;
  190. constructor taicpu.op_ref(op : tasmop;const _op1 : treference);
  191. begin
  192. inherited create(op);
  193. ops:=1;
  194. loadref(0,_op1);
  195. if op in [a_local_get,a_local_set,a_local_tee] then
  196. begin
  197. if (_op1.base<>NR_LOCAL_STACK_POINTER_REG) or (_op1.index<>NR_NO) then
  198. internalerror(2021010201);
  199. end
  200. else
  201. begin
  202. if (_op1.base<>NR_NO) or (_op1.index<>NR_NO) then
  203. internalerror(2021010202);
  204. end;
  205. end;
  206. constructor taicpu.op_const(op : tasmop;_op1 : aint);
  207. begin
  208. inherited create(op);
  209. ops:=1;
  210. loadconst(0,_op1);
  211. end;
  212. constructor taicpu.op_sym(op : tasmop;_op1 : tasmsymbol);
  213. begin
  214. inherited create(op);
  215. ops:=1;
  216. loadsymbol(0,_op1,0);
  217. end;
  218. constructor taicpu.op_sym_const(op: tasmop; _op1: tasmsymbol; _op2: aint);
  219. begin
  220. inherited create(op);
  221. ops:=2;
  222. loadsymbol(0,_op1,0);
  223. loadconst(1,_op2);
  224. end;
  225. constructor taicpu.op_single(op: tasmop; _op1: single);
  226. begin
  227. inherited create(op);
  228. ops:=1;
  229. loadsingle(0,_op1);
  230. end;
  231. constructor taicpu.op_double(op: tasmop; _op1: double);
  232. begin
  233. inherited create(op);
  234. ops:=1;
  235. loaddouble(0,_op1);
  236. end;
  237. constructor taicpu.op_functype(op: tasmop; _op1: TWasmFuncType);
  238. begin
  239. inherited create(op);
  240. ops:=1;
  241. loadfunctype(0,_op1);
  242. end;
  243. procedure taicpu.loadfunctype(opidx: longint; ft: TWasmFuncType);
  244. begin
  245. allocate_oper(opidx+1);
  246. with oper[opidx]^ do
  247. begin
  248. if typ<>top_functype then
  249. clearop(opidx);
  250. functype:=ft;
  251. typ:=top_functype;
  252. end;
  253. end;
  254. procedure taicpu.loadsingle(opidx:longint;f:single);
  255. begin
  256. allocate_oper(opidx+1);
  257. with oper[opidx]^ do
  258. begin
  259. if typ<>top_single then
  260. clearop(opidx);
  261. sval:=f;
  262. typ:=top_single;
  263. end;
  264. end;
  265. procedure taicpu.loaddouble(opidx: longint; d: double);
  266. begin
  267. allocate_oper(opidx+1);
  268. with oper[opidx]^ do
  269. begin
  270. if typ<>top_double then
  271. clearop(opidx);
  272. dval:=d;
  273. typ:=top_double;
  274. end;
  275. end;
  276. function taicpu.is_same_reg_move(regtype: Tregistertype):boolean;
  277. begin
  278. result:=false;
  279. end;
  280. function taicpu.spilling_get_operation_type(opnr: longint): topertype;
  281. begin
  282. if opcode in AsmOp_Store then
  283. result:=operand_write
  284. else
  285. result:=operand_read;
  286. end;
  287. function taicpu.Pass1(objdata: TObjData): longint;
  288. function SlebSize(v: tcgint): longint;
  289. begin
  290. result:=0;
  291. repeat
  292. v:=SarInt64(v,7);
  293. Inc(result);
  294. until ((v=0) and ((byte(v) and 64)=0)) or ((v=-1) and ((byte(v) and 64)<>0));
  295. end;
  296. function UlebSize(v: tcgint): longint;
  297. begin
  298. result:=0;
  299. repeat
  300. v:=v shr 7;
  301. Inc(result);
  302. until v=0;
  303. end;
  304. begin
  305. result:=0;
  306. case opcode of
  307. a_unreachable,
  308. a_nop,
  309. a_return,
  310. a_drop,
  311. a_i32_eqz,
  312. a_i32_eq,
  313. a_i32_ne,
  314. a_i32_lt_s,
  315. a_i32_lt_u,
  316. a_i32_gt_s,
  317. a_i32_gt_u,
  318. a_i32_le_s,
  319. a_i32_le_u,
  320. a_i32_ge_s,
  321. a_i32_ge_u,
  322. a_i64_eqz,
  323. a_i64_eq,
  324. a_i64_ne,
  325. a_i64_lt_s,
  326. a_i64_lt_u,
  327. a_i64_gt_s,
  328. a_i64_gt_u,
  329. a_i64_le_s,
  330. a_i64_le_u,
  331. a_i64_ge_s,
  332. a_i64_ge_u,
  333. a_f32_eq,
  334. a_f32_ne,
  335. a_f32_lt,
  336. a_f32_gt,
  337. a_f32_le,
  338. a_f32_ge,
  339. a_f64_eq,
  340. a_f64_ne,
  341. a_f64_lt,
  342. a_f64_gt,
  343. a_f64_le,
  344. a_f64_ge,
  345. a_i32_clz,
  346. a_i32_ctz,
  347. a_i32_popcnt,
  348. a_i32_add,
  349. a_i32_sub,
  350. a_i32_mul,
  351. a_i32_div_s,
  352. a_i32_div_u,
  353. a_i32_rem_s,
  354. a_i32_rem_u,
  355. a_i32_and,
  356. a_i32_or,
  357. a_i32_xor,
  358. a_i32_shl,
  359. a_i32_shr_s,
  360. a_i32_shr_u,
  361. a_i32_rotl,
  362. a_i32_rotr,
  363. a_i64_clz,
  364. a_i64_ctz,
  365. a_i64_popcnt,
  366. a_i64_add,
  367. a_i64_sub,
  368. a_i64_mul,
  369. a_i64_div_s,
  370. a_i64_div_u,
  371. a_i64_rem_s,
  372. a_i64_rem_u,
  373. a_i64_and,
  374. a_i64_or,
  375. a_i64_xor,
  376. a_i64_shl,
  377. a_i64_shr_s,
  378. a_i64_shr_u,
  379. a_i64_rotl,
  380. a_i64_rotr,
  381. a_f32_abs,
  382. a_f32_neg,
  383. a_f32_ceil,
  384. a_f32_floor,
  385. a_f32_trunc,
  386. a_f32_nearest,
  387. a_f32_sqrt,
  388. a_f32_add,
  389. a_f32_sub,
  390. a_f32_mul,
  391. a_f32_div,
  392. a_f32_min,
  393. a_f32_max,
  394. a_f32_copysign,
  395. a_f64_abs,
  396. a_f64_neg,
  397. a_f64_ceil,
  398. a_f64_floor,
  399. a_f64_trunc,
  400. a_f64_nearest,
  401. a_f64_sqrt,
  402. a_f64_add,
  403. a_f64_sub,
  404. a_f64_mul,
  405. a_f64_div,
  406. a_f64_min,
  407. a_f64_max,
  408. a_f64_copysign,
  409. a_i32_wrap_i64,
  410. a_i32_trunc_f32_s,
  411. a_i32_trunc_f32_u,
  412. a_i32_trunc_f64_s,
  413. a_i32_trunc_f64_u,
  414. a_i64_extend_i32_s,
  415. a_i64_extend_i32_u,
  416. a_i64_trunc_f32_s,
  417. a_i64_trunc_f32_u,
  418. a_i64_trunc_f64_s,
  419. a_i64_trunc_f64_u,
  420. a_f32_convert_i32_s,
  421. a_f32_convert_i32_u,
  422. a_f32_convert_i64_s,
  423. a_f32_convert_i64_u,
  424. a_f32_demote_f64,
  425. a_f64_convert_i32_s,
  426. a_f64_convert_i32_u,
  427. a_f64_convert_i64_s,
  428. a_f64_convert_i64_u,
  429. a_f64_promote_f32,
  430. a_i32_reinterpret_f32,
  431. a_i64_reinterpret_f64,
  432. a_f32_reinterpret_i32,
  433. a_f64_reinterpret_i64,
  434. a_i32_extend8_s,
  435. a_i32_extend16_s,
  436. a_i64_extend8_s,
  437. a_i64_extend16_s,
  438. a_i64_extend32_s,
  439. a_end_block,
  440. a_end_if,
  441. a_end_loop,
  442. a_end_try,
  443. a_catch_all:
  444. result:=1;
  445. a_memory_size,
  446. a_memory_grow:
  447. result:=2;
  448. a_i32_const,
  449. a_i64_const:
  450. begin
  451. if ops<>1 then
  452. internalerror(2021092001);
  453. with oper[0]^ do
  454. case typ of
  455. top_ref:
  456. begin
  457. if assigned(ref^.symbol) then
  458. result:=6
  459. else
  460. begin
  461. if assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) then
  462. internalerror(2021092018);
  463. result:=1+SlebSize(ref^.offset);
  464. end;
  465. end;
  466. top_const:
  467. result:=1+SlebSize(val);
  468. else
  469. Writeln('Warning! Not implemented opcode, pass1: ', opcode, ' ', typ);
  470. end;
  471. end;
  472. a_local_get,
  473. a_local_set,
  474. a_local_tee:
  475. begin
  476. if ops<>1 then
  477. internalerror(2021092001);
  478. with oper[0]^ do
  479. case typ of
  480. top_ref:
  481. begin
  482. if assigned(ref^.symbol) then
  483. internalerror(2021092005);
  484. if ref^.base<>NR_STACK_POINTER_REG then
  485. internalerror(2021092006);
  486. if ref^.index<>NR_NO then
  487. internalerror(2021092007);
  488. result:=1+UlebSize(ref^.offset);
  489. end;
  490. else
  491. internalerror(2021092008);
  492. end;
  493. end;
  494. a_global_get,
  495. a_global_set:
  496. begin
  497. if ops<>1 then
  498. internalerror(2021092010);
  499. with oper[0]^ do
  500. case typ of
  501. top_ref:
  502. begin
  503. if not assigned(ref^.symbol) then
  504. internalerror(2021092012);
  505. if (ref^.base<>NR_NO) or (ref^.index<>NR_NO) or (ref^.offset<>0) then
  506. internalerror(2021092013);
  507. if ref^.symbol.Name<>'__stack_pointer' then
  508. internalerror(2021092014);
  509. result:=1+UlebSize(0);
  510. end;
  511. else
  512. internalerror(2021092011);
  513. end;
  514. end;
  515. a_end_function:
  516. result:=0;
  517. a_block:
  518. begin
  519. if ops=0 then
  520. result:=2
  521. else
  522. begin
  523. if ops<>1 then
  524. internalerror(2021092015);
  525. Writeln('Warning! Not implemented opcode, pass2: ', opcode, ' ', typ);
  526. end;
  527. end;
  528. a_i32_store,
  529. a_i32_load:
  530. begin
  531. if ops<>1 then
  532. internalerror(2021092016);
  533. with oper[0]^ do
  534. case typ of
  535. top_ref:
  536. begin
  537. if assigned(ref^.symbol) then
  538. begin
  539. Result:=1+
  540. UlebSize(2)+ { alignment: 1 shl 2 }
  541. 5; { relocation, fixed size = 5 bytes }
  542. end
  543. else
  544. begin
  545. if assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) then
  546. internalerror(2021092018);
  547. Result:=1+
  548. UlebSize(2)+ { alignment: 1 shl 2 }
  549. UlebSize(ref^.offset);
  550. end;
  551. end;
  552. else
  553. internalerror(2021092017);
  554. end;
  555. end;
  556. a_call:
  557. begin
  558. if ops<>1 then
  559. internalerror(2021092021);
  560. with oper[0]^ do
  561. case typ of
  562. top_ref:
  563. begin
  564. if not assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) or (ref^.offset<>0) then
  565. internalerror(2021092023);
  566. result:=6;
  567. end;
  568. else
  569. internalerror(2021092022);
  570. end;
  571. end;
  572. a_call_indirect:
  573. begin
  574. if ops<>1 then
  575. internalerror(2021092610);
  576. with oper[0]^ do
  577. case typ of
  578. top_functype:
  579. begin
  580. TWasmObjData(objdata).AddFuncType(functype);
  581. result:=6+
  582. UlebSize(0);
  583. end;
  584. else
  585. internalerror(2021092611);
  586. end;
  587. end;
  588. else
  589. Writeln('Warning! Not implemented opcode, pass1: ', opcode);
  590. end;
  591. end;
  592. procedure taicpu.Pass2(objdata: TObjData);
  593. procedure WriteByte(b: byte);
  594. begin
  595. objdata.writebytes(b,1);
  596. end;
  597. procedure WriteSleb(v: tcgint);
  598. var
  599. b: byte;
  600. Done: Boolean=false;
  601. begin
  602. repeat
  603. b:=byte(v) and 127;
  604. v:=SarInt64(v,7);
  605. if ((v=0) and ((b and 64)=0)) or ((v=-1) and ((b and 64)<>0)) then
  606. Done:=true
  607. else
  608. b:=b or 128;
  609. objdata.writebytes(b,1);
  610. until Done;
  611. end;
  612. procedure WriteUleb(v: tcgint);
  613. var
  614. b: byte;
  615. begin
  616. repeat
  617. b:=byte(v) and 127;
  618. v:=v shr 7;
  619. if v<>0 then
  620. b:=b or 128;
  621. objdata.writebytes(b,1);
  622. until v=0;
  623. end;
  624. begin
  625. case opcode of
  626. a_unreachable:
  627. WriteByte($00);
  628. a_nop:
  629. WriteByte($01);
  630. a_return:
  631. WriteByte($0F);
  632. a_drop:
  633. WriteByte($1A);
  634. a_memory_size:
  635. begin
  636. WriteByte($3F);
  637. WriteByte($00);
  638. end;
  639. a_memory_grow:
  640. begin
  641. WriteByte($40);
  642. WriteByte($00);
  643. end;
  644. a_i32_eqz:
  645. WriteByte($45);
  646. a_i32_eq:
  647. WriteByte($46);
  648. a_i32_ne:
  649. WriteByte($47);
  650. a_i32_lt_s:
  651. WriteByte($48);
  652. a_i32_lt_u:
  653. WriteByte($49);
  654. a_i32_gt_s:
  655. WriteByte($4A);
  656. a_i32_gt_u:
  657. WriteByte($4B);
  658. a_i32_le_s:
  659. WriteByte($4C);
  660. a_i32_le_u:
  661. WriteByte($4D);
  662. a_i32_ge_s:
  663. WriteByte($4E);
  664. a_i32_ge_u:
  665. WriteByte($4F);
  666. a_i64_eqz:
  667. WriteByte($50);
  668. a_i64_eq:
  669. WriteByte($51);
  670. a_i64_ne:
  671. WriteByte($52);
  672. a_i64_lt_s:
  673. WriteByte($53);
  674. a_i64_lt_u:
  675. WriteByte($54);
  676. a_i64_gt_s:
  677. WriteByte($55);
  678. a_i64_gt_u:
  679. WriteByte($56);
  680. a_i64_le_s:
  681. WriteByte($57);
  682. a_i64_le_u:
  683. WriteByte($58);
  684. a_i64_ge_s:
  685. WriteByte($59);
  686. a_i64_ge_u:
  687. WriteByte($5A);
  688. a_f32_eq:
  689. WriteByte($5B);
  690. a_f32_ne:
  691. WriteByte($5C);
  692. a_f32_lt:
  693. WriteByte($5D);
  694. a_f32_gt:
  695. WriteByte($5E);
  696. a_f32_le:
  697. WriteByte($5F);
  698. a_f32_ge:
  699. WriteByte($60);
  700. a_f64_eq:
  701. WriteByte($61);
  702. a_f64_ne:
  703. WriteByte($62);
  704. a_f64_lt:
  705. WriteByte($63);
  706. a_f64_gt:
  707. WriteByte($64);
  708. a_f64_le:
  709. WriteByte($65);
  710. a_f64_ge:
  711. WriteByte($66);
  712. a_i32_clz:
  713. WriteByte($67);
  714. a_i32_ctz:
  715. WriteByte($68);
  716. a_i32_popcnt:
  717. WriteByte($69);
  718. a_i32_add:
  719. WriteByte($6A);
  720. a_i32_sub:
  721. WriteByte($6B);
  722. a_i32_mul:
  723. WriteByte($6C);
  724. a_i32_div_s:
  725. WriteByte($6D);
  726. a_i32_div_u:
  727. WriteByte($6E);
  728. a_i32_rem_s:
  729. WriteByte($6F);
  730. a_i32_rem_u:
  731. WriteByte($70);
  732. a_i32_and:
  733. WriteByte($71);
  734. a_i32_or:
  735. WriteByte($72);
  736. a_i32_xor:
  737. WriteByte($73);
  738. a_i32_shl:
  739. WriteByte($74);
  740. a_i32_shr_s:
  741. WriteByte($75);
  742. a_i32_shr_u:
  743. WriteByte($76);
  744. a_i32_rotl:
  745. WriteByte($77);
  746. a_i32_rotr:
  747. WriteByte($78);
  748. a_i64_clz:
  749. WriteByte($79);
  750. a_i64_ctz:
  751. WriteByte($7A);
  752. a_i64_popcnt:
  753. WriteByte($7B);
  754. a_i64_add:
  755. WriteByte($7C);
  756. a_i64_sub:
  757. WriteByte($7D);
  758. a_i64_mul:
  759. WriteByte($7E);
  760. a_i64_div_s:
  761. WriteByte($7F);
  762. a_i64_div_u:
  763. WriteByte($80);
  764. a_i64_rem_s:
  765. WriteByte($81);
  766. a_i64_rem_u:
  767. WriteByte($82);
  768. a_i64_and:
  769. WriteByte($83);
  770. a_i64_or:
  771. WriteByte($84);
  772. a_i64_xor:
  773. WriteByte($85);
  774. a_i64_shl:
  775. WriteByte($86);
  776. a_i64_shr_s:
  777. WriteByte($87);
  778. a_i64_shr_u:
  779. WriteByte($88);
  780. a_i64_rotl:
  781. WriteByte($89);
  782. a_i64_rotr:
  783. WriteByte($8A);
  784. a_f32_abs:
  785. WriteByte($8B);
  786. a_f32_neg:
  787. WriteByte($8C);
  788. a_f32_ceil:
  789. WriteByte($8D);
  790. a_f32_floor:
  791. WriteByte($8E);
  792. a_f32_trunc:
  793. WriteByte($8F);
  794. a_f32_nearest:
  795. WriteByte($90);
  796. a_f32_sqrt:
  797. WriteByte($91);
  798. a_f32_add:
  799. WriteByte($92);
  800. a_f32_sub:
  801. WriteByte($93);
  802. a_f32_mul:
  803. WriteByte($94);
  804. a_f32_div:
  805. WriteByte($95);
  806. a_f32_min:
  807. WriteByte($96);
  808. a_f32_max:
  809. WriteByte($97);
  810. a_f32_copysign:
  811. WriteByte($98);
  812. a_f64_abs:
  813. WriteByte($99);
  814. a_f64_neg:
  815. WriteByte($9A);
  816. a_f64_ceil:
  817. WriteByte($9B);
  818. a_f64_floor:
  819. WriteByte($9C);
  820. a_f64_trunc:
  821. WriteByte($9D);
  822. a_f64_nearest:
  823. WriteByte($9E);
  824. a_f64_sqrt:
  825. WriteByte($9F);
  826. a_f64_add:
  827. WriteByte($A0);
  828. a_f64_sub:
  829. WriteByte($A1);
  830. a_f64_mul:
  831. WriteByte($A2);
  832. a_f64_div:
  833. WriteByte($A3);
  834. a_f64_min:
  835. WriteByte($A4);
  836. a_f64_max:
  837. WriteByte($A5);
  838. a_f64_copysign:
  839. WriteByte($A6);
  840. a_i32_wrap_i64:
  841. WriteByte($A7);
  842. a_i32_trunc_f32_s:
  843. WriteByte($A8);
  844. a_i32_trunc_f32_u:
  845. WriteByte($A9);
  846. a_i32_trunc_f64_s:
  847. WriteByte($AA);
  848. a_i32_trunc_f64_u:
  849. WriteByte($AB);
  850. a_i64_extend_i32_s:
  851. WriteByte($AC);
  852. a_i64_extend_i32_u:
  853. WriteByte($AD);
  854. a_i64_trunc_f32_s:
  855. WriteByte($AE);
  856. a_i64_trunc_f32_u:
  857. WriteByte($AF);
  858. a_i64_trunc_f64_s:
  859. WriteByte($B0);
  860. a_i64_trunc_f64_u:
  861. WriteByte($B1);
  862. a_f32_convert_i32_s:
  863. WriteByte($B2);
  864. a_f32_convert_i32_u:
  865. WriteByte($B3);
  866. a_f32_convert_i64_s:
  867. WriteByte($B4);
  868. a_f32_convert_i64_u:
  869. WriteByte($B5);
  870. a_f32_demote_f64:
  871. WriteByte($B6);
  872. a_f64_convert_i32_s:
  873. WriteByte($B7);
  874. a_f64_convert_i32_u:
  875. WriteByte($B8);
  876. a_f64_convert_i64_s:
  877. WriteByte($B9);
  878. a_f64_convert_i64_u:
  879. WriteByte($BA);
  880. a_f64_promote_f32:
  881. WriteByte($BB);
  882. a_i32_reinterpret_f32:
  883. WriteByte($BC);
  884. a_i64_reinterpret_f64:
  885. WriteByte($BD);
  886. a_f32_reinterpret_i32:
  887. WriteByte($BE);
  888. a_f64_reinterpret_i64:
  889. WriteByte($BF);
  890. a_i32_extend8_s:
  891. WriteByte($C0);
  892. a_i32_extend16_s:
  893. WriteByte($C1);
  894. a_i64_extend8_s:
  895. WriteByte($C2);
  896. a_i64_extend16_s:
  897. WriteByte($C3);
  898. a_i64_extend32_s:
  899. WriteByte($C4);
  900. a_end_block,
  901. a_end_if,
  902. a_end_loop,
  903. a_end_try:
  904. WriteByte($0B);
  905. a_catch_all:
  906. WriteByte($19);
  907. a_i32_const,
  908. a_i64_const:
  909. begin
  910. case opcode of
  911. a_i32_const:
  912. WriteByte($41);
  913. a_i64_const:
  914. WriteByte($42);
  915. else
  916. internalerror(2021092002);
  917. end;
  918. if ops<>1 then
  919. internalerror(2021092001);
  920. with oper[0]^ do
  921. case typ of
  922. top_ref:
  923. begin
  924. if assigned(ref^.symbol) then
  925. objdata.writeReloc(ref^.offset,5,ObjData.symbolref(ref^.symbol),RELOC_MEMORY_ADDR_SLEB)
  926. else
  927. begin
  928. if assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) then
  929. internalerror(2021092018);
  930. WriteSleb(ref^.offset);
  931. end;
  932. end;
  933. top_const:
  934. WriteSleb(val);
  935. else
  936. Writeln('Warning! Not implemented opcode, pass2: ', opcode, ' ', typ);
  937. end;
  938. end;
  939. a_local_get,
  940. a_local_set,
  941. a_local_tee:
  942. begin
  943. case opcode of
  944. a_local_get:
  945. WriteByte($20);
  946. a_local_set:
  947. WriteByte($21);
  948. a_local_tee:
  949. WriteByte($22);
  950. else
  951. internalerror(2021092003);
  952. end;
  953. if ops<>1 then
  954. internalerror(2021092004);
  955. with oper[0]^ do
  956. case typ of
  957. top_ref:
  958. begin
  959. if assigned(ref^.symbol) then
  960. internalerror(2021092005);
  961. if ref^.base<>NR_STACK_POINTER_REG then
  962. internalerror(2021092006);
  963. if ref^.index<>NR_NO then
  964. internalerror(2021092007);
  965. WriteUleb(ref^.offset);
  966. end;
  967. else
  968. internalerror(2021092008);
  969. end;
  970. end;
  971. a_global_get,
  972. a_global_set:
  973. begin
  974. case opcode of
  975. a_global_get:
  976. WriteByte($23);
  977. a_global_set:
  978. WriteByte($24);
  979. else
  980. internalerror(2021092009);
  981. end;
  982. if ops<>1 then
  983. internalerror(2021092010);
  984. with oper[0]^ do
  985. case typ of
  986. top_ref:
  987. begin
  988. if not assigned(ref^.symbol) then
  989. internalerror(2021092012);
  990. if (ref^.base<>NR_NO) or (ref^.index<>NR_NO) or (ref^.offset<>0) then
  991. internalerror(2021092013);
  992. if ref^.symbol.Name<>'__stack_pointer' then
  993. internalerror(2021092014);
  994. WriteUleb(0);
  995. end;
  996. else
  997. internalerror(2021092011);
  998. end;
  999. end;
  1000. a_end_function:
  1001. ;
  1002. a_block:
  1003. begin
  1004. WriteByte($02);
  1005. if ops=0 then
  1006. WriteByte($40)
  1007. else
  1008. begin
  1009. if ops<>1 then
  1010. internalerror(2021092015);
  1011. Writeln('Warning! Not implemented opcode, pass2: ', opcode, ' ', typ);
  1012. end;
  1013. end;
  1014. a_i32_store,
  1015. a_i32_load:
  1016. begin
  1017. case opcode of
  1018. a_i32_store:
  1019. WriteByte($36);
  1020. a_i32_load:
  1021. WriteByte($28);
  1022. else
  1023. internalerror(2021092019);
  1024. end;
  1025. if ops<>1 then
  1026. internalerror(2021092016);
  1027. with oper[0]^ do
  1028. case typ of
  1029. top_ref:
  1030. begin
  1031. if assigned(ref^.symbol) then
  1032. begin
  1033. WriteUleb(2); { alignment: 1 shl 2 }
  1034. objdata.writeReloc(ref^.offset,5,ObjData.symbolref(ref^.symbol),RELOC_MEMORY_ADDR_LEB);
  1035. end
  1036. else
  1037. begin
  1038. if assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) then
  1039. internalerror(2021092018);
  1040. WriteUleb(2); { alignment: 1 shl 2 }
  1041. WriteUleb(ref^.offset);
  1042. end;
  1043. end;
  1044. else
  1045. internalerror(2021092017);
  1046. end;
  1047. end;
  1048. a_call:
  1049. begin
  1050. if ops<>1 then
  1051. internalerror(2021092021);
  1052. with oper[0]^ do
  1053. case typ of
  1054. top_ref:
  1055. begin
  1056. if not assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) or (ref^.offset<>0) then
  1057. internalerror(2021092023);
  1058. WriteByte($10);
  1059. objdata.writeReloc(0,5,ObjData.symbolref(ref^.symbol),RELOC_FUNCTION_INDEX_LEB);
  1060. end;
  1061. else
  1062. internalerror(2021092022);
  1063. end;
  1064. end;
  1065. a_call_indirect:
  1066. begin
  1067. if ops<>1 then
  1068. internalerror(2021092610);
  1069. with oper[0]^ do
  1070. case typ of
  1071. top_functype:
  1072. begin
  1073. WriteByte($11);
  1074. objdata.writeReloc(TWasmObjData(objdata).AddFuncType(functype),5,nil,RELOC_TYPE_INDEX_LEB);
  1075. WriteUleb(0);
  1076. end;
  1077. else
  1078. internalerror(2021092611);
  1079. end;
  1080. end;
  1081. else
  1082. Writeln('Warning! Not implemented opcode, pass2: ', opcode);
  1083. end;
  1084. end;
  1085. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  1086. begin
  1087. internalerror(2010122614);
  1088. result:=nil;
  1089. end;
  1090. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  1091. begin
  1092. internalerror(2010122615);
  1093. result:=nil;
  1094. end;
  1095. procedure InitAsm;
  1096. begin
  1097. end;
  1098. procedure DoneAsm;
  1099. begin
  1100. end;
  1101. initialization
  1102. cai_cpu:=taicpu;
  1103. cai_align:=tai_align;
  1104. casmdata:=TAsmData;
  1105. end.