aasmcpu.pas 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393
  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_else,
  440. a_end_block,
  441. a_end_if,
  442. a_end_loop,
  443. a_end_try,
  444. a_catch_all:
  445. result:=1;
  446. a_memory_size,
  447. a_memory_grow:
  448. result:=2;
  449. a_i32_const,
  450. a_i64_const:
  451. begin
  452. if ops<>1 then
  453. internalerror(2021092001);
  454. with oper[0]^ do
  455. case typ of
  456. top_ref:
  457. begin
  458. if assigned(ref^.symbol) then
  459. result:=6
  460. else
  461. begin
  462. if assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) then
  463. internalerror(2021092018);
  464. result:=1+SlebSize(ref^.offset);
  465. end;
  466. end;
  467. top_const:
  468. result:=1+SlebSize(val);
  469. else
  470. internalerror(2021092615);
  471. end;
  472. end;
  473. a_f32_const:
  474. result:=5;
  475. a_f64_const:
  476. result:=9;
  477. a_local_get,
  478. a_local_set,
  479. a_local_tee:
  480. begin
  481. if ops<>1 then
  482. internalerror(2021092001);
  483. with oper[0]^ do
  484. case typ of
  485. top_ref:
  486. begin
  487. if assigned(ref^.symbol) then
  488. internalerror(2021092005);
  489. if ref^.base<>NR_STACK_POINTER_REG then
  490. internalerror(2021092006);
  491. if ref^.index<>NR_NO then
  492. internalerror(2021092007);
  493. result:=1+UlebSize(ref^.offset);
  494. end;
  495. else
  496. internalerror(2021092008);
  497. end;
  498. end;
  499. a_global_get,
  500. a_global_set:
  501. begin
  502. if ops<>1 then
  503. internalerror(2021092010);
  504. with oper[0]^ do
  505. case typ of
  506. top_ref:
  507. begin
  508. if not assigned(ref^.symbol) then
  509. internalerror(2021092012);
  510. if (ref^.base<>NR_NO) or (ref^.index<>NR_NO) or (ref^.offset<>0) then
  511. internalerror(2021092013);
  512. if ref^.symbol.Name<>'__stack_pointer' then
  513. internalerror(2021092014);
  514. result:=1+UlebSize(0);
  515. end;
  516. else
  517. internalerror(2021092011);
  518. end;
  519. end;
  520. a_end_function:
  521. result:=0;
  522. a_block,
  523. a_loop,
  524. a_if:
  525. begin
  526. if ops=0 then
  527. result:=2
  528. else
  529. begin
  530. if ops<>1 then
  531. internalerror(2021092015);
  532. with oper[0]^ do
  533. case typ of
  534. top_functype:
  535. begin
  536. if (length(functype.params)=0) and (length(functype.results)<=1) then
  537. result:=2
  538. else
  539. { more complex blocktypes are not yet implemented }
  540. internalerror(2021092621);
  541. end;
  542. else
  543. internalerror(2021092620);
  544. end;
  545. end;
  546. end;
  547. a_i32_load,
  548. a_i64_load,
  549. a_f32_load,
  550. a_f64_load,
  551. a_i32_load8_s,
  552. a_i32_load8_u,
  553. a_i32_load16_s,
  554. a_i32_load16_u,
  555. a_i64_load8_s,
  556. a_i64_load8_u,
  557. a_i64_load16_s,
  558. a_i64_load16_u,
  559. a_i64_load32_s,
  560. a_i64_load32_u,
  561. a_i32_store,
  562. a_i64_store,
  563. a_f32_store,
  564. a_f64_store,
  565. a_i32_store8,
  566. a_i32_store16,
  567. a_i64_store8,
  568. a_i64_store16,
  569. a_i64_store32:
  570. begin
  571. if ops<>1 then
  572. internalerror(2021092016);
  573. with oper[0]^ do
  574. case typ of
  575. top_ref:
  576. begin
  577. if assigned(ref^.symbol) then
  578. begin
  579. Result:=1+
  580. UlebSize(natural_alignment_for_load_store(opcode))+
  581. 5; { relocation, fixed size = 5 bytes }
  582. end
  583. else
  584. begin
  585. if assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) then
  586. internalerror(2021092018);
  587. Result:=1+
  588. UlebSize(natural_alignment_for_load_store(opcode))+
  589. UlebSize(ref^.offset);
  590. end;
  591. end;
  592. else
  593. internalerror(2021092017);
  594. end;
  595. end;
  596. a_call:
  597. begin
  598. if ops<>1 then
  599. internalerror(2021092021);
  600. with oper[0]^ do
  601. case typ of
  602. top_ref:
  603. begin
  604. if not assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) or (ref^.offset<>0) then
  605. internalerror(2021092023);
  606. result:=6;
  607. end;
  608. else
  609. internalerror(2021092022);
  610. end;
  611. end;
  612. a_call_indirect:
  613. begin
  614. if ops<>1 then
  615. internalerror(2021092610);
  616. with oper[0]^ do
  617. case typ of
  618. top_functype:
  619. begin
  620. TWasmObjData(objdata).AddFuncType(functype);
  621. result:=6+
  622. UlebSize(0);
  623. end;
  624. else
  625. internalerror(2021092611);
  626. end;
  627. end;
  628. else
  629. internalerror(2021092623);
  630. end;
  631. end;
  632. procedure taicpu.Pass2(objdata: TObjData);
  633. procedure WriteByte(b: byte);
  634. begin
  635. objdata.writebytes(b,1);
  636. end;
  637. {$ifdef FPC_LITTLE_ENDIAN}
  638. procedure WriteSingle(s: single);
  639. begin
  640. objdata.writebytes(s,4);
  641. end;
  642. procedure WriteDouble(d: double);
  643. begin
  644. objdata.writebytes(d,8);
  645. end;
  646. {$else FPC_LITTLE_ENDIAN}
  647. procedure WriteSingle(s: single);
  648. var
  649. l: longword;
  650. begin
  651. Move(s,l,4);
  652. l:=SwapEndian(l);
  653. objdata.writebytes(l,4);
  654. end;
  655. procedure WriteDouble(d: double);
  656. var
  657. q: qword;
  658. begin
  659. Move(d,q,8);
  660. q:=SwapEndian(q);
  661. objdata.writebytes(q,8);
  662. end;
  663. {$endif FPC_LITTLE_ENDIAN}
  664. procedure WriteSleb(v: tcgint);
  665. var
  666. b: byte;
  667. Done: Boolean=false;
  668. begin
  669. repeat
  670. b:=byte(v) and 127;
  671. v:=SarInt64(v,7);
  672. if ((v=0) and ((b and 64)=0)) or ((v=-1) and ((b and 64)<>0)) then
  673. Done:=true
  674. else
  675. b:=b or 128;
  676. objdata.writebytes(b,1);
  677. until Done;
  678. end;
  679. procedure WriteUleb(v: tcgint);
  680. var
  681. b: byte;
  682. begin
  683. repeat
  684. b:=byte(v) and 127;
  685. v:=v shr 7;
  686. if v<>0 then
  687. b:=b or 128;
  688. objdata.writebytes(b,1);
  689. until v=0;
  690. end;
  691. begin
  692. case opcode of
  693. a_unreachable:
  694. WriteByte($00);
  695. a_nop:
  696. WriteByte($01);
  697. a_return:
  698. WriteByte($0F);
  699. a_drop:
  700. WriteByte($1A);
  701. a_memory_size:
  702. begin
  703. WriteByte($3F);
  704. WriteByte($00);
  705. end;
  706. a_memory_grow:
  707. begin
  708. WriteByte($40);
  709. WriteByte($00);
  710. end;
  711. a_i32_eqz:
  712. WriteByte($45);
  713. a_i32_eq:
  714. WriteByte($46);
  715. a_i32_ne:
  716. WriteByte($47);
  717. a_i32_lt_s:
  718. WriteByte($48);
  719. a_i32_lt_u:
  720. WriteByte($49);
  721. a_i32_gt_s:
  722. WriteByte($4A);
  723. a_i32_gt_u:
  724. WriteByte($4B);
  725. a_i32_le_s:
  726. WriteByte($4C);
  727. a_i32_le_u:
  728. WriteByte($4D);
  729. a_i32_ge_s:
  730. WriteByte($4E);
  731. a_i32_ge_u:
  732. WriteByte($4F);
  733. a_i64_eqz:
  734. WriteByte($50);
  735. a_i64_eq:
  736. WriteByte($51);
  737. a_i64_ne:
  738. WriteByte($52);
  739. a_i64_lt_s:
  740. WriteByte($53);
  741. a_i64_lt_u:
  742. WriteByte($54);
  743. a_i64_gt_s:
  744. WriteByte($55);
  745. a_i64_gt_u:
  746. WriteByte($56);
  747. a_i64_le_s:
  748. WriteByte($57);
  749. a_i64_le_u:
  750. WriteByte($58);
  751. a_i64_ge_s:
  752. WriteByte($59);
  753. a_i64_ge_u:
  754. WriteByte($5A);
  755. a_f32_eq:
  756. WriteByte($5B);
  757. a_f32_ne:
  758. WriteByte($5C);
  759. a_f32_lt:
  760. WriteByte($5D);
  761. a_f32_gt:
  762. WriteByte($5E);
  763. a_f32_le:
  764. WriteByte($5F);
  765. a_f32_ge:
  766. WriteByte($60);
  767. a_f64_eq:
  768. WriteByte($61);
  769. a_f64_ne:
  770. WriteByte($62);
  771. a_f64_lt:
  772. WriteByte($63);
  773. a_f64_gt:
  774. WriteByte($64);
  775. a_f64_le:
  776. WriteByte($65);
  777. a_f64_ge:
  778. WriteByte($66);
  779. a_i32_clz:
  780. WriteByte($67);
  781. a_i32_ctz:
  782. WriteByte($68);
  783. a_i32_popcnt:
  784. WriteByte($69);
  785. a_i32_add:
  786. WriteByte($6A);
  787. a_i32_sub:
  788. WriteByte($6B);
  789. a_i32_mul:
  790. WriteByte($6C);
  791. a_i32_div_s:
  792. WriteByte($6D);
  793. a_i32_div_u:
  794. WriteByte($6E);
  795. a_i32_rem_s:
  796. WriteByte($6F);
  797. a_i32_rem_u:
  798. WriteByte($70);
  799. a_i32_and:
  800. WriteByte($71);
  801. a_i32_or:
  802. WriteByte($72);
  803. a_i32_xor:
  804. WriteByte($73);
  805. a_i32_shl:
  806. WriteByte($74);
  807. a_i32_shr_s:
  808. WriteByte($75);
  809. a_i32_shr_u:
  810. WriteByte($76);
  811. a_i32_rotl:
  812. WriteByte($77);
  813. a_i32_rotr:
  814. WriteByte($78);
  815. a_i64_clz:
  816. WriteByte($79);
  817. a_i64_ctz:
  818. WriteByte($7A);
  819. a_i64_popcnt:
  820. WriteByte($7B);
  821. a_i64_add:
  822. WriteByte($7C);
  823. a_i64_sub:
  824. WriteByte($7D);
  825. a_i64_mul:
  826. WriteByte($7E);
  827. a_i64_div_s:
  828. WriteByte($7F);
  829. a_i64_div_u:
  830. WriteByte($80);
  831. a_i64_rem_s:
  832. WriteByte($81);
  833. a_i64_rem_u:
  834. WriteByte($82);
  835. a_i64_and:
  836. WriteByte($83);
  837. a_i64_or:
  838. WriteByte($84);
  839. a_i64_xor:
  840. WriteByte($85);
  841. a_i64_shl:
  842. WriteByte($86);
  843. a_i64_shr_s:
  844. WriteByte($87);
  845. a_i64_shr_u:
  846. WriteByte($88);
  847. a_i64_rotl:
  848. WriteByte($89);
  849. a_i64_rotr:
  850. WriteByte($8A);
  851. a_f32_abs:
  852. WriteByte($8B);
  853. a_f32_neg:
  854. WriteByte($8C);
  855. a_f32_ceil:
  856. WriteByte($8D);
  857. a_f32_floor:
  858. WriteByte($8E);
  859. a_f32_trunc:
  860. WriteByte($8F);
  861. a_f32_nearest:
  862. WriteByte($90);
  863. a_f32_sqrt:
  864. WriteByte($91);
  865. a_f32_add:
  866. WriteByte($92);
  867. a_f32_sub:
  868. WriteByte($93);
  869. a_f32_mul:
  870. WriteByte($94);
  871. a_f32_div:
  872. WriteByte($95);
  873. a_f32_min:
  874. WriteByte($96);
  875. a_f32_max:
  876. WriteByte($97);
  877. a_f32_copysign:
  878. WriteByte($98);
  879. a_f64_abs:
  880. WriteByte($99);
  881. a_f64_neg:
  882. WriteByte($9A);
  883. a_f64_ceil:
  884. WriteByte($9B);
  885. a_f64_floor:
  886. WriteByte($9C);
  887. a_f64_trunc:
  888. WriteByte($9D);
  889. a_f64_nearest:
  890. WriteByte($9E);
  891. a_f64_sqrt:
  892. WriteByte($9F);
  893. a_f64_add:
  894. WriteByte($A0);
  895. a_f64_sub:
  896. WriteByte($A1);
  897. a_f64_mul:
  898. WriteByte($A2);
  899. a_f64_div:
  900. WriteByte($A3);
  901. a_f64_min:
  902. WriteByte($A4);
  903. a_f64_max:
  904. WriteByte($A5);
  905. a_f64_copysign:
  906. WriteByte($A6);
  907. a_i32_wrap_i64:
  908. WriteByte($A7);
  909. a_i32_trunc_f32_s:
  910. WriteByte($A8);
  911. a_i32_trunc_f32_u:
  912. WriteByte($A9);
  913. a_i32_trunc_f64_s:
  914. WriteByte($AA);
  915. a_i32_trunc_f64_u:
  916. WriteByte($AB);
  917. a_i64_extend_i32_s:
  918. WriteByte($AC);
  919. a_i64_extend_i32_u:
  920. WriteByte($AD);
  921. a_i64_trunc_f32_s:
  922. WriteByte($AE);
  923. a_i64_trunc_f32_u:
  924. WriteByte($AF);
  925. a_i64_trunc_f64_s:
  926. WriteByte($B0);
  927. a_i64_trunc_f64_u:
  928. WriteByte($B1);
  929. a_f32_convert_i32_s:
  930. WriteByte($B2);
  931. a_f32_convert_i32_u:
  932. WriteByte($B3);
  933. a_f32_convert_i64_s:
  934. WriteByte($B4);
  935. a_f32_convert_i64_u:
  936. WriteByte($B5);
  937. a_f32_demote_f64:
  938. WriteByte($B6);
  939. a_f64_convert_i32_s:
  940. WriteByte($B7);
  941. a_f64_convert_i32_u:
  942. WriteByte($B8);
  943. a_f64_convert_i64_s:
  944. WriteByte($B9);
  945. a_f64_convert_i64_u:
  946. WriteByte($BA);
  947. a_f64_promote_f32:
  948. WriteByte($BB);
  949. a_i32_reinterpret_f32:
  950. WriteByte($BC);
  951. a_i64_reinterpret_f64:
  952. WriteByte($BD);
  953. a_f32_reinterpret_i32:
  954. WriteByte($BE);
  955. a_f64_reinterpret_i64:
  956. WriteByte($BF);
  957. a_i32_extend8_s:
  958. WriteByte($C0);
  959. a_i32_extend16_s:
  960. WriteByte($C1);
  961. a_i64_extend8_s:
  962. WriteByte($C2);
  963. a_i64_extend16_s:
  964. WriteByte($C3);
  965. a_i64_extend32_s:
  966. WriteByte($C4);
  967. a_end_block,
  968. a_end_if,
  969. a_end_loop,
  970. a_end_try:
  971. WriteByte($0B);
  972. a_catch_all:
  973. WriteByte($19);
  974. a_i32_const,
  975. a_i64_const:
  976. begin
  977. case opcode of
  978. a_i32_const:
  979. WriteByte($41);
  980. a_i64_const:
  981. WriteByte($42);
  982. else
  983. internalerror(2021092002);
  984. end;
  985. if ops<>1 then
  986. internalerror(2021092001);
  987. with oper[0]^ do
  988. case typ of
  989. top_ref:
  990. begin
  991. if assigned(ref^.symbol) then
  992. objdata.writeReloc(ref^.offset,5,ObjData.symbolref(ref^.symbol),RELOC_MEMORY_ADDR_OR_TABLE_INDEX_SLEB)
  993. else
  994. begin
  995. if assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) then
  996. internalerror(2021092018);
  997. WriteSleb(ref^.offset);
  998. end;
  999. end;
  1000. top_const:
  1001. WriteSleb(val);
  1002. else
  1003. internalerror(2021092615);
  1004. end;
  1005. end;
  1006. a_f32_const:
  1007. begin
  1008. if ops<>1 then
  1009. internalerror(2021092619);
  1010. WriteByte($44);
  1011. with oper[0]^ do
  1012. case typ of
  1013. top_single:
  1014. WriteSingle(sval);
  1015. else
  1016. internalerror(2021092618);
  1017. end;
  1018. end;
  1019. a_f64_const:
  1020. begin
  1021. if ops<>1 then
  1022. internalerror(2021092616);
  1023. WriteByte($44);
  1024. with oper[0]^ do
  1025. case typ of
  1026. top_double:
  1027. WriteDouble(dval);
  1028. else
  1029. internalerror(2021092617);
  1030. end;
  1031. end;
  1032. a_local_get,
  1033. a_local_set,
  1034. a_local_tee:
  1035. begin
  1036. case opcode of
  1037. a_local_get:
  1038. WriteByte($20);
  1039. a_local_set:
  1040. WriteByte($21);
  1041. a_local_tee:
  1042. WriteByte($22);
  1043. else
  1044. internalerror(2021092003);
  1045. end;
  1046. if ops<>1 then
  1047. internalerror(2021092004);
  1048. with oper[0]^ do
  1049. case typ of
  1050. top_ref:
  1051. begin
  1052. if assigned(ref^.symbol) then
  1053. internalerror(2021092005);
  1054. if ref^.base<>NR_STACK_POINTER_REG then
  1055. internalerror(2021092006);
  1056. if ref^.index<>NR_NO then
  1057. internalerror(2021092007);
  1058. WriteUleb(ref^.offset);
  1059. end;
  1060. else
  1061. internalerror(2021092008);
  1062. end;
  1063. end;
  1064. a_global_get,
  1065. a_global_set:
  1066. begin
  1067. case opcode of
  1068. a_global_get:
  1069. WriteByte($23);
  1070. a_global_set:
  1071. WriteByte($24);
  1072. else
  1073. internalerror(2021092009);
  1074. end;
  1075. if ops<>1 then
  1076. internalerror(2021092010);
  1077. with oper[0]^ do
  1078. case typ of
  1079. top_ref:
  1080. begin
  1081. if not assigned(ref^.symbol) then
  1082. internalerror(2021092012);
  1083. if (ref^.base<>NR_NO) or (ref^.index<>NR_NO) or (ref^.offset<>0) then
  1084. internalerror(2021092013);
  1085. if ref^.symbol.Name<>'__stack_pointer' then
  1086. internalerror(2021092014);
  1087. WriteUleb(0);
  1088. end;
  1089. else
  1090. internalerror(2021092011);
  1091. end;
  1092. end;
  1093. a_end_function:
  1094. ;
  1095. a_block,
  1096. a_loop,
  1097. a_if:
  1098. begin
  1099. case opcode of
  1100. a_block:
  1101. WriteByte($02);
  1102. a_loop:
  1103. WriteByte($03);
  1104. a_if:
  1105. WriteByte($04);
  1106. end;
  1107. if ops=0 then
  1108. WriteByte($40)
  1109. else
  1110. begin
  1111. if ops<>1 then
  1112. internalerror(2021092015);
  1113. with oper[0]^ do
  1114. case typ of
  1115. top_functype:
  1116. begin
  1117. if (length(functype.params)=0) and (length(functype.results)<=1) then
  1118. begin
  1119. if length(functype.results)=1 then
  1120. WriteByte(encode_wasm_basic_type(functype.results[0]))
  1121. else
  1122. WriteByte($40);
  1123. end
  1124. else
  1125. { more complex blocktypes are not yet implemented }
  1126. internalerror(2021092621);
  1127. end;
  1128. else
  1129. internalerror(2021092620);
  1130. end;
  1131. end;
  1132. end;
  1133. a_else:
  1134. WriteByte($05);
  1135. a_i32_load,
  1136. a_i64_load,
  1137. a_f32_load,
  1138. a_f64_load,
  1139. a_i32_load8_s,
  1140. a_i32_load8_u,
  1141. a_i32_load16_s,
  1142. a_i32_load16_u,
  1143. a_i64_load8_s,
  1144. a_i64_load8_u,
  1145. a_i64_load16_s,
  1146. a_i64_load16_u,
  1147. a_i64_load32_s,
  1148. a_i64_load32_u,
  1149. a_i32_store,
  1150. a_i64_store,
  1151. a_f32_store,
  1152. a_f64_store,
  1153. a_i32_store8,
  1154. a_i32_store16,
  1155. a_i64_store8,
  1156. a_i64_store16,
  1157. a_i64_store32:
  1158. begin
  1159. case opcode of
  1160. a_i32_load:
  1161. WriteByte($28);
  1162. a_i64_load:
  1163. WriteByte($29);
  1164. a_f32_load:
  1165. WriteByte($2A);
  1166. a_f64_load:
  1167. WriteByte($2B);
  1168. a_i32_load8_s:
  1169. WriteByte($2C);
  1170. a_i32_load8_u:
  1171. WriteByte($2D);
  1172. a_i32_load16_s:
  1173. WriteByte($2E);
  1174. a_i32_load16_u:
  1175. WriteByte($2F);
  1176. a_i64_load8_s:
  1177. WriteByte($30);
  1178. a_i64_load8_u:
  1179. WriteByte($31);
  1180. a_i64_load16_s:
  1181. WriteByte($32);
  1182. a_i64_load16_u:
  1183. WriteByte($33);
  1184. a_i64_load32_s:
  1185. WriteByte($34);
  1186. a_i64_load32_u:
  1187. WriteByte($35);
  1188. a_i32_store:
  1189. WriteByte($36);
  1190. a_i64_store:
  1191. WriteByte($37);
  1192. a_f32_store:
  1193. WriteByte($38);
  1194. a_f64_store:
  1195. WriteByte($39);
  1196. a_i32_store8:
  1197. WriteByte($3A);
  1198. a_i32_store16:
  1199. WriteByte($3B);
  1200. a_i64_store8:
  1201. WriteByte($3C);
  1202. a_i64_store16:
  1203. WriteByte($3D);
  1204. a_i64_store32:
  1205. WriteByte($3E);
  1206. else
  1207. internalerror(2021092019);
  1208. end;
  1209. if ops<>1 then
  1210. internalerror(2021092016);
  1211. with oper[0]^ do
  1212. case typ of
  1213. top_ref:
  1214. begin
  1215. if assigned(ref^.symbol) then
  1216. begin
  1217. WriteUleb(natural_alignment_for_load_store(opcode));
  1218. objdata.writeReloc(ref^.offset,5,ObjData.symbolref(ref^.symbol),RELOC_MEMORY_ADDR_LEB);
  1219. end
  1220. else
  1221. begin
  1222. if assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) then
  1223. internalerror(2021092018);
  1224. WriteUleb(natural_alignment_for_load_store(opcode));
  1225. WriteUleb(ref^.offset);
  1226. end;
  1227. end;
  1228. else
  1229. internalerror(2021092017);
  1230. end;
  1231. end;
  1232. a_call:
  1233. begin
  1234. if ops<>1 then
  1235. internalerror(2021092021);
  1236. with oper[0]^ do
  1237. case typ of
  1238. top_ref:
  1239. begin
  1240. if not assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) or (ref^.offset<>0) then
  1241. internalerror(2021092023);
  1242. WriteByte($10);
  1243. objdata.writeReloc(0,5,ObjData.symbolref(ref^.symbol),RELOC_FUNCTION_INDEX_LEB);
  1244. end;
  1245. else
  1246. internalerror(2021092022);
  1247. end;
  1248. end;
  1249. a_call_indirect:
  1250. begin
  1251. if ops<>1 then
  1252. internalerror(2021092610);
  1253. with oper[0]^ do
  1254. case typ of
  1255. top_functype:
  1256. begin
  1257. WriteByte($11);
  1258. objdata.writeReloc(TWasmObjData(objdata).AddFuncType(functype),5,nil,RELOC_TYPE_INDEX_LEB);
  1259. WriteUleb(0);
  1260. end;
  1261. else
  1262. internalerror(2021092611);
  1263. end;
  1264. end;
  1265. else
  1266. internalerror(2021092624);
  1267. end;
  1268. end;
  1269. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  1270. begin
  1271. internalerror(2010122614);
  1272. result:=nil;
  1273. end;
  1274. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  1275. begin
  1276. internalerror(2010122615);
  1277. result:=nil;
  1278. end;
  1279. procedure InitAsm;
  1280. begin
  1281. end;
  1282. procedure DoneAsm;
  1283. begin
  1284. end;
  1285. initialization
  1286. cai_cpu:=taicpu;
  1287. cai_align:=tai_align;
  1288. casmdata:=TAsmData;
  1289. end.