aasmcpu.pas 30 KB

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