aasmcpu.pas 31 KB

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