aasmcpu.pas 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497
  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. { tai_export_name }
  64. tai_export_name = class(tai)
  65. extname : ansistring; // external name
  66. intname : ansistring; // internal name
  67. symstype: TImpExpType;
  68. constructor create(const aextname, aintname: ansistring; asymtype: timpexptype);
  69. end;
  70. // local variable declaration
  71. { tai_local }
  72. tai_local = class(tai)
  73. bastyp: TWasmBasicType;
  74. name : string;
  75. first: boolean;
  76. last: boolean;
  77. constructor create(abasictype: TWasmBasicType; const aname: string = '');
  78. end;
  79. { tai_globaltype }
  80. tai_globaltype = class(tai)
  81. globalname: string;
  82. gtype: TWasmBasicType;
  83. immutable: boolean;
  84. constructor create(const aglobalname:string; atype: TWasmBasicType; aimmutable: boolean);
  85. end;
  86. { tai_functype }
  87. tai_functype = class(tai)
  88. funcname: string;
  89. functype: TWasmFuncType;
  90. constructor create(const afuncname: string; afunctype: TWasmFuncType);
  91. destructor destroy;override;
  92. end;
  93. { tai_tagtype }
  94. tai_tagtype = class(tai)
  95. tagname: string;
  96. params: TWasmResultType;
  97. constructor create(const atagname: string; aparams: TWasmResultType);
  98. end;
  99. { tai_import_module }
  100. tai_import_module = class(tai)
  101. symname: string;
  102. importmodule: string;
  103. constructor create(const asymname, aimportmodule: string);
  104. end;
  105. { tai_import_name }
  106. tai_import_name = class(tai)
  107. symname: string;
  108. importname: string;
  109. constructor create(const asymname, aimportname: string);
  110. end;
  111. procedure InitAsm;
  112. procedure DoneAsm;
  113. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  114. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  115. implementation
  116. uses
  117. ogwasm;
  118. { tai_globaltype }
  119. constructor tai_globaltype.create(const aglobalname: string; atype: TWasmBasicType; aimmutable: boolean);
  120. begin
  121. inherited Create;
  122. typ:=ait_globaltype;
  123. globalname:=aglobalname;
  124. gtype:=atype;
  125. immutable:=aimmutable;
  126. end;
  127. { tai_import_name }
  128. constructor tai_import_name.create(const asymname, aimportname: string);
  129. begin
  130. inherited Create;
  131. typ:=ait_import_name;
  132. symname:=asymname;
  133. importname:=aimportname;
  134. end;
  135. { tai_import_module }
  136. constructor tai_import_module.create(const asymname, aimportmodule: string);
  137. begin
  138. inherited Create;
  139. typ:=ait_import_module;
  140. symname:=asymname;
  141. importmodule:=aimportmodule;
  142. end;
  143. { tai_functype }
  144. constructor tai_functype.create(const afuncname: string; afunctype: TWasmFuncType);
  145. begin
  146. inherited Create;
  147. typ:=ait_functype;
  148. funcname:=afuncname;
  149. functype:=afunctype;
  150. end;
  151. destructor tai_functype.destroy;
  152. begin
  153. functype.free;
  154. inherited;
  155. end;
  156. { tai_tagtype }
  157. constructor tai_tagtype.create(const atagname: string; aparams: TWasmResultType);
  158. begin
  159. typ:=ait_tagtype;
  160. tagname:=atagname;
  161. params:=aparams;
  162. end;
  163. { tai_local }
  164. constructor tai_local.create(abasictype: TWasmBasicType; const aname: string);
  165. begin
  166. inherited Create;
  167. bastyp := abasictype;
  168. typ := ait_local;
  169. name := aname;
  170. end;
  171. { timpexp_ai }
  172. constructor tai_export_name.create(const aextname, aintname: ansistring;
  173. asymtype: timpexptype);
  174. begin
  175. inherited create;
  176. typ := ait_export_name;
  177. extname := aextname;
  178. intname := aintname;
  179. symstype:= asymtype;
  180. end;
  181. {*****************************************************************************
  182. taicpu Constructors
  183. *****************************************************************************}
  184. constructor taicpu.op_none(op : tasmop);
  185. begin
  186. inherited create(op);
  187. end;
  188. constructor taicpu.op_reg(op : tasmop;_op1 : tregister);
  189. begin
  190. inherited create(op);
  191. ops:=1;
  192. {$ifdef EXTDEBUG}
  193. if getregtype(_op1)=R_INVALIDREGISTER then
  194. InternalError(2021011901);
  195. {$endif EXTDEBUG}
  196. loadreg(0,_op1);
  197. end;
  198. constructor taicpu.op_ref(op : tasmop;const _op1 : treference);
  199. begin
  200. inherited create(op);
  201. ops:=1;
  202. loadref(0,_op1);
  203. if op in [a_local_get,a_local_set,a_local_tee] then
  204. begin
  205. if (_op1.base<>NR_LOCAL_STACK_POINTER_REG) or (_op1.index<>NR_NO) then
  206. internalerror(2021010201);
  207. end
  208. else
  209. begin
  210. if (_op1.base<>NR_NO) or (_op1.index<>NR_NO) then
  211. internalerror(2021010202);
  212. end;
  213. end;
  214. constructor taicpu.op_const(op : tasmop;_op1 : aint);
  215. begin
  216. inherited create(op);
  217. ops:=1;
  218. loadconst(0,_op1);
  219. end;
  220. constructor taicpu.op_sym(op : tasmop;_op1 : tasmsymbol);
  221. begin
  222. inherited create(op);
  223. ops:=1;
  224. loadsymbol(0,_op1,0);
  225. end;
  226. constructor taicpu.op_sym_const(op: tasmop; _op1: tasmsymbol; _op2: aint);
  227. begin
  228. inherited create(op);
  229. ops:=2;
  230. loadsymbol(0,_op1,0);
  231. loadconst(1,_op2);
  232. end;
  233. constructor taicpu.op_single(op: tasmop; _op1: single);
  234. begin
  235. inherited create(op);
  236. ops:=1;
  237. loadsingle(0,_op1);
  238. end;
  239. constructor taicpu.op_double(op: tasmop; _op1: double);
  240. begin
  241. inherited create(op);
  242. ops:=1;
  243. loaddouble(0,_op1);
  244. end;
  245. constructor taicpu.op_functype(op: tasmop; _op1: TWasmFuncType);
  246. begin
  247. inherited create(op);
  248. ops:=1;
  249. loadfunctype(0,_op1);
  250. end;
  251. procedure taicpu.loadfunctype(opidx: longint; ft: TWasmFuncType);
  252. begin
  253. allocate_oper(opidx+1);
  254. with oper[opidx]^ do
  255. begin
  256. if typ<>top_functype then
  257. clearop(opidx);
  258. functype:=ft;
  259. typ:=top_functype;
  260. end;
  261. end;
  262. procedure taicpu.loadsingle(opidx:longint;f:single);
  263. begin
  264. allocate_oper(opidx+1);
  265. with oper[opidx]^ do
  266. begin
  267. if typ<>top_single then
  268. clearop(opidx);
  269. sval:=f;
  270. typ:=top_single;
  271. end;
  272. end;
  273. procedure taicpu.loaddouble(opidx: longint; d: double);
  274. begin
  275. allocate_oper(opidx+1);
  276. with oper[opidx]^ do
  277. begin
  278. if typ<>top_double then
  279. clearop(opidx);
  280. dval:=d;
  281. typ:=top_double;
  282. end;
  283. end;
  284. function taicpu.is_same_reg_move(regtype: Tregistertype):boolean;
  285. begin
  286. result:=false;
  287. end;
  288. function taicpu.spilling_get_operation_type(opnr: longint): topertype;
  289. begin
  290. if opcode in AsmOp_Store then
  291. result:=operand_write
  292. else
  293. result:=operand_read;
  294. end;
  295. function taicpu.Pass1(objdata: TObjData): longint;
  296. function SlebSize(v: tcgint): longint;
  297. begin
  298. result:=0;
  299. repeat
  300. v:=SarInt64(v,7);
  301. Inc(result);
  302. until ((v=0) and ((byte(v) and 64)=0)) or ((v=-1) and ((byte(v) and 64)<>0));
  303. end;
  304. function UlebSize(v: tcgint): longint;
  305. begin
  306. result:=0;
  307. repeat
  308. v:=v shr 7;
  309. Inc(result);
  310. until v=0;
  311. end;
  312. begin
  313. result:=0;
  314. case opcode of
  315. a_unreachable,
  316. a_nop,
  317. a_return,
  318. a_drop,
  319. a_i32_eqz,
  320. a_i32_eq,
  321. a_i32_ne,
  322. a_i32_lt_s,
  323. a_i32_lt_u,
  324. a_i32_gt_s,
  325. a_i32_gt_u,
  326. a_i32_le_s,
  327. a_i32_le_u,
  328. a_i32_ge_s,
  329. a_i32_ge_u,
  330. a_i64_eqz,
  331. a_i64_eq,
  332. a_i64_ne,
  333. a_i64_lt_s,
  334. a_i64_lt_u,
  335. a_i64_gt_s,
  336. a_i64_gt_u,
  337. a_i64_le_s,
  338. a_i64_le_u,
  339. a_i64_ge_s,
  340. a_i64_ge_u,
  341. a_f32_eq,
  342. a_f32_ne,
  343. a_f32_lt,
  344. a_f32_gt,
  345. a_f32_le,
  346. a_f32_ge,
  347. a_f64_eq,
  348. a_f64_ne,
  349. a_f64_lt,
  350. a_f64_gt,
  351. a_f64_le,
  352. a_f64_ge,
  353. a_i32_clz,
  354. a_i32_ctz,
  355. a_i32_popcnt,
  356. a_i32_add,
  357. a_i32_sub,
  358. a_i32_mul,
  359. a_i32_div_s,
  360. a_i32_div_u,
  361. a_i32_rem_s,
  362. a_i32_rem_u,
  363. a_i32_and,
  364. a_i32_or,
  365. a_i32_xor,
  366. a_i32_shl,
  367. a_i32_shr_s,
  368. a_i32_shr_u,
  369. a_i32_rotl,
  370. a_i32_rotr,
  371. a_i64_clz,
  372. a_i64_ctz,
  373. a_i64_popcnt,
  374. a_i64_add,
  375. a_i64_sub,
  376. a_i64_mul,
  377. a_i64_div_s,
  378. a_i64_div_u,
  379. a_i64_rem_s,
  380. a_i64_rem_u,
  381. a_i64_and,
  382. a_i64_or,
  383. a_i64_xor,
  384. a_i64_shl,
  385. a_i64_shr_s,
  386. a_i64_shr_u,
  387. a_i64_rotl,
  388. a_i64_rotr,
  389. a_f32_abs,
  390. a_f32_neg,
  391. a_f32_ceil,
  392. a_f32_floor,
  393. a_f32_trunc,
  394. a_f32_nearest,
  395. a_f32_sqrt,
  396. a_f32_add,
  397. a_f32_sub,
  398. a_f32_mul,
  399. a_f32_div,
  400. a_f32_min,
  401. a_f32_max,
  402. a_f32_copysign,
  403. a_f64_abs,
  404. a_f64_neg,
  405. a_f64_ceil,
  406. a_f64_floor,
  407. a_f64_trunc,
  408. a_f64_nearest,
  409. a_f64_sqrt,
  410. a_f64_add,
  411. a_f64_sub,
  412. a_f64_mul,
  413. a_f64_div,
  414. a_f64_min,
  415. a_f64_max,
  416. a_f64_copysign,
  417. a_i32_wrap_i64,
  418. a_i32_trunc_f32_s,
  419. a_i32_trunc_f32_u,
  420. a_i32_trunc_f64_s,
  421. a_i32_trunc_f64_u,
  422. a_i64_extend_i32_s,
  423. a_i64_extend_i32_u,
  424. a_i64_trunc_f32_s,
  425. a_i64_trunc_f32_u,
  426. a_i64_trunc_f64_s,
  427. a_i64_trunc_f64_u,
  428. a_f32_convert_i32_s,
  429. a_f32_convert_i32_u,
  430. a_f32_convert_i64_s,
  431. a_f32_convert_i64_u,
  432. a_f32_demote_f64,
  433. a_f64_convert_i32_s,
  434. a_f64_convert_i32_u,
  435. a_f64_convert_i64_s,
  436. a_f64_convert_i64_u,
  437. a_f64_promote_f32,
  438. a_i32_reinterpret_f32,
  439. a_i64_reinterpret_f64,
  440. a_f32_reinterpret_i32,
  441. a_f64_reinterpret_i64,
  442. a_i32_extend8_s,
  443. a_i32_extend16_s,
  444. a_i64_extend8_s,
  445. a_i64_extend16_s,
  446. a_i64_extend32_s,
  447. a_else,
  448. a_end_block,
  449. a_end_if,
  450. a_end_loop,
  451. a_end_try,
  452. a_catch_all:
  453. result:=1;
  454. a_memory_size,
  455. a_memory_grow:
  456. result:=2;
  457. a_i32_const:
  458. begin
  459. if ops<>1 then
  460. internalerror(2021092001);
  461. with oper[0]^ do
  462. case typ of
  463. top_ref:
  464. begin
  465. if assigned(ref^.symbol) then
  466. result:=6
  467. else
  468. begin
  469. if assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) then
  470. internalerror(2021092018);
  471. result:=1+SlebSize(longint(ref^.offset));
  472. end;
  473. end;
  474. top_const:
  475. result:=1+SlebSize(longint(val));
  476. else
  477. internalerror(2021092615);
  478. end;
  479. end;
  480. a_i64_const:
  481. begin
  482. if ops<>1 then
  483. internalerror(2021092001);
  484. with oper[0]^ do
  485. case typ of
  486. top_ref:
  487. begin
  488. if assigned(ref^.symbol) then
  489. result:=6
  490. else
  491. begin
  492. if assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) then
  493. internalerror(2021092018);
  494. result:=1+SlebSize(int64(ref^.offset));
  495. end;
  496. end;
  497. top_const:
  498. result:=1+SlebSize(int64(val));
  499. else
  500. internalerror(2021092615);
  501. end;
  502. end;
  503. a_f32_const:
  504. result:=5;
  505. a_f64_const:
  506. result:=9;
  507. a_local_get,
  508. a_local_set,
  509. a_local_tee:
  510. begin
  511. if ops<>1 then
  512. internalerror(2021092001);
  513. with oper[0]^ do
  514. case typ of
  515. top_ref:
  516. begin
  517. if assigned(ref^.symbol) then
  518. internalerror(2021092005);
  519. if ref^.base<>NR_STACK_POINTER_REG then
  520. internalerror(2021092006);
  521. if ref^.index<>NR_NO then
  522. internalerror(2021092007);
  523. result:=1+UlebSize(ref^.offset);
  524. end;
  525. else
  526. internalerror(2021092008);
  527. end;
  528. end;
  529. a_global_get,
  530. a_global_set:
  531. begin
  532. if ops<>1 then
  533. internalerror(2021092010);
  534. with oper[0]^ do
  535. case typ of
  536. top_ref:
  537. begin
  538. if not assigned(ref^.symbol) then
  539. internalerror(2021092012);
  540. if (ref^.base<>NR_NO) or (ref^.index<>NR_NO) or (ref^.offset<>0) then
  541. internalerror(2021092013);
  542. result:=6;
  543. end;
  544. else
  545. internalerror(2021092011);
  546. end;
  547. end;
  548. a_end_function:
  549. result:=0;
  550. a_block,
  551. a_loop,
  552. a_if,
  553. a_try:
  554. begin
  555. if ops=0 then
  556. result:=2
  557. else
  558. begin
  559. if ops<>1 then
  560. internalerror(2021092015);
  561. with oper[0]^ do
  562. case typ of
  563. top_functype:
  564. begin
  565. if (length(functype.params)=0) and (length(functype.results)<=1) then
  566. result:=2
  567. else
  568. { more complex blocktypes are not yet implemented }
  569. internalerror(2021092621);
  570. end;
  571. else
  572. internalerror(2021092620);
  573. end;
  574. end;
  575. end;
  576. a_i32_load,
  577. a_i64_load,
  578. a_f32_load,
  579. a_f64_load,
  580. a_i32_load8_s,
  581. a_i32_load8_u,
  582. a_i32_load16_s,
  583. a_i32_load16_u,
  584. a_i64_load8_s,
  585. a_i64_load8_u,
  586. a_i64_load16_s,
  587. a_i64_load16_u,
  588. a_i64_load32_s,
  589. a_i64_load32_u,
  590. a_i32_store,
  591. a_i64_store,
  592. a_f32_store,
  593. a_f64_store,
  594. a_i32_store8,
  595. a_i32_store16,
  596. a_i64_store8,
  597. a_i64_store16,
  598. a_i64_store32:
  599. begin
  600. if ops<>1 then
  601. internalerror(2021092016);
  602. with oper[0]^ do
  603. case typ of
  604. top_ref:
  605. begin
  606. if assigned(ref^.symbol) then
  607. begin
  608. Result:=1+
  609. UlebSize(natural_alignment_for_load_store(opcode))+
  610. 5; { relocation, fixed size = 5 bytes }
  611. end
  612. else
  613. begin
  614. if assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) then
  615. internalerror(2021092018);
  616. Result:=1+
  617. UlebSize(natural_alignment_for_load_store(opcode))+
  618. UlebSize(ref^.offset);
  619. end;
  620. end;
  621. top_const:
  622. begin
  623. Result:=1+
  624. UlebSize(natural_alignment_for_load_store(opcode))+
  625. UlebSize(val);
  626. end;
  627. else
  628. internalerror(2021092017);
  629. end;
  630. end;
  631. a_call:
  632. begin
  633. if ops<>1 then
  634. internalerror(2021092021);
  635. with oper[0]^ do
  636. case typ of
  637. top_ref:
  638. begin
  639. if not assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) or (ref^.offset<>0) then
  640. internalerror(2021092023);
  641. result:=6;
  642. end;
  643. else
  644. internalerror(2021092022);
  645. end;
  646. end;
  647. a_call_indirect:
  648. begin
  649. if ops<>1 then
  650. internalerror(2021092610);
  651. with oper[0]^ do
  652. case typ of
  653. top_functype:
  654. begin
  655. TWasmObjData(objdata).AddFuncType(functype);
  656. result:=6+
  657. UlebSize(0);
  658. end;
  659. else
  660. internalerror(2021092611);
  661. end;
  662. end;
  663. a_br,
  664. a_br_if,
  665. a_rethrow,
  666. a_delegate:
  667. begin
  668. if ops<>1 then
  669. internalerror(2021092610);
  670. with oper[0]^ do
  671. case typ of
  672. top_const:
  673. result:=1+
  674. UlebSize(val);
  675. else
  676. internalerror(2021092625);
  677. end;
  678. end;
  679. else
  680. internalerror(2021092623);
  681. end;
  682. end;
  683. procedure taicpu.Pass2(objdata: TObjData);
  684. procedure WriteByte(b: byte);
  685. begin
  686. objdata.writebytes(b,1);
  687. end;
  688. {$ifdef FPC_LITTLE_ENDIAN}
  689. procedure WriteSingle(s: single);
  690. begin
  691. objdata.writebytes(s,4);
  692. end;
  693. procedure WriteDouble(d: double);
  694. begin
  695. objdata.writebytes(d,8);
  696. end;
  697. {$else FPC_LITTLE_ENDIAN}
  698. procedure WriteSingle(s: single);
  699. var
  700. l: longword;
  701. begin
  702. Move(s,l,4);
  703. l:=SwapEndian(l);
  704. objdata.writebytes(l,4);
  705. end;
  706. procedure WriteDouble(d: double);
  707. var
  708. q: qword;
  709. begin
  710. Move(d,q,8);
  711. q:=SwapEndian(q);
  712. objdata.writebytes(q,8);
  713. end;
  714. {$endif FPC_LITTLE_ENDIAN}
  715. procedure WriteSleb(v: tcgint);
  716. var
  717. b: byte;
  718. Done: Boolean=false;
  719. begin
  720. repeat
  721. b:=byte(v) and 127;
  722. v:=SarInt64(v,7);
  723. if ((v=0) and ((b and 64)=0)) or ((v=-1) and ((b and 64)<>0)) then
  724. Done:=true
  725. else
  726. b:=b or 128;
  727. objdata.writebytes(b,1);
  728. until Done;
  729. end;
  730. procedure WriteUleb(v: tcgint);
  731. var
  732. b: byte;
  733. begin
  734. repeat
  735. b:=byte(v) and 127;
  736. v:=v shr 7;
  737. if v<>0 then
  738. b:=b or 128;
  739. objdata.writebytes(b,1);
  740. until v=0;
  741. end;
  742. begin
  743. case opcode of
  744. a_unreachable:
  745. WriteByte($00);
  746. a_nop:
  747. WriteByte($01);
  748. a_return:
  749. WriteByte($0F);
  750. a_drop:
  751. WriteByte($1A);
  752. a_memory_size:
  753. begin
  754. WriteByte($3F);
  755. WriteByte($00);
  756. end;
  757. a_memory_grow:
  758. begin
  759. WriteByte($40);
  760. WriteByte($00);
  761. end;
  762. a_i32_eqz:
  763. WriteByte($45);
  764. a_i32_eq:
  765. WriteByte($46);
  766. a_i32_ne:
  767. WriteByte($47);
  768. a_i32_lt_s:
  769. WriteByte($48);
  770. a_i32_lt_u:
  771. WriteByte($49);
  772. a_i32_gt_s:
  773. WriteByte($4A);
  774. a_i32_gt_u:
  775. WriteByte($4B);
  776. a_i32_le_s:
  777. WriteByte($4C);
  778. a_i32_le_u:
  779. WriteByte($4D);
  780. a_i32_ge_s:
  781. WriteByte($4E);
  782. a_i32_ge_u:
  783. WriteByte($4F);
  784. a_i64_eqz:
  785. WriteByte($50);
  786. a_i64_eq:
  787. WriteByte($51);
  788. a_i64_ne:
  789. WriteByte($52);
  790. a_i64_lt_s:
  791. WriteByte($53);
  792. a_i64_lt_u:
  793. WriteByte($54);
  794. a_i64_gt_s:
  795. WriteByte($55);
  796. a_i64_gt_u:
  797. WriteByte($56);
  798. a_i64_le_s:
  799. WriteByte($57);
  800. a_i64_le_u:
  801. WriteByte($58);
  802. a_i64_ge_s:
  803. WriteByte($59);
  804. a_i64_ge_u:
  805. WriteByte($5A);
  806. a_f32_eq:
  807. WriteByte($5B);
  808. a_f32_ne:
  809. WriteByte($5C);
  810. a_f32_lt:
  811. WriteByte($5D);
  812. a_f32_gt:
  813. WriteByte($5E);
  814. a_f32_le:
  815. WriteByte($5F);
  816. a_f32_ge:
  817. WriteByte($60);
  818. a_f64_eq:
  819. WriteByte($61);
  820. a_f64_ne:
  821. WriteByte($62);
  822. a_f64_lt:
  823. WriteByte($63);
  824. a_f64_gt:
  825. WriteByte($64);
  826. a_f64_le:
  827. WriteByte($65);
  828. a_f64_ge:
  829. WriteByte($66);
  830. a_i32_clz:
  831. WriteByte($67);
  832. a_i32_ctz:
  833. WriteByte($68);
  834. a_i32_popcnt:
  835. WriteByte($69);
  836. a_i32_add:
  837. WriteByte($6A);
  838. a_i32_sub:
  839. WriteByte($6B);
  840. a_i32_mul:
  841. WriteByte($6C);
  842. a_i32_div_s:
  843. WriteByte($6D);
  844. a_i32_div_u:
  845. WriteByte($6E);
  846. a_i32_rem_s:
  847. WriteByte($6F);
  848. a_i32_rem_u:
  849. WriteByte($70);
  850. a_i32_and:
  851. WriteByte($71);
  852. a_i32_or:
  853. WriteByte($72);
  854. a_i32_xor:
  855. WriteByte($73);
  856. a_i32_shl:
  857. WriteByte($74);
  858. a_i32_shr_s:
  859. WriteByte($75);
  860. a_i32_shr_u:
  861. WriteByte($76);
  862. a_i32_rotl:
  863. WriteByte($77);
  864. a_i32_rotr:
  865. WriteByte($78);
  866. a_i64_clz:
  867. WriteByte($79);
  868. a_i64_ctz:
  869. WriteByte($7A);
  870. a_i64_popcnt:
  871. WriteByte($7B);
  872. a_i64_add:
  873. WriteByte($7C);
  874. a_i64_sub:
  875. WriteByte($7D);
  876. a_i64_mul:
  877. WriteByte($7E);
  878. a_i64_div_s:
  879. WriteByte($7F);
  880. a_i64_div_u:
  881. WriteByte($80);
  882. a_i64_rem_s:
  883. WriteByte($81);
  884. a_i64_rem_u:
  885. WriteByte($82);
  886. a_i64_and:
  887. WriteByte($83);
  888. a_i64_or:
  889. WriteByte($84);
  890. a_i64_xor:
  891. WriteByte($85);
  892. a_i64_shl:
  893. WriteByte($86);
  894. a_i64_shr_s:
  895. WriteByte($87);
  896. a_i64_shr_u:
  897. WriteByte($88);
  898. a_i64_rotl:
  899. WriteByte($89);
  900. a_i64_rotr:
  901. WriteByte($8A);
  902. a_f32_abs:
  903. WriteByte($8B);
  904. a_f32_neg:
  905. WriteByte($8C);
  906. a_f32_ceil:
  907. WriteByte($8D);
  908. a_f32_floor:
  909. WriteByte($8E);
  910. a_f32_trunc:
  911. WriteByte($8F);
  912. a_f32_nearest:
  913. WriteByte($90);
  914. a_f32_sqrt:
  915. WriteByte($91);
  916. a_f32_add:
  917. WriteByte($92);
  918. a_f32_sub:
  919. WriteByte($93);
  920. a_f32_mul:
  921. WriteByte($94);
  922. a_f32_div:
  923. WriteByte($95);
  924. a_f32_min:
  925. WriteByte($96);
  926. a_f32_max:
  927. WriteByte($97);
  928. a_f32_copysign:
  929. WriteByte($98);
  930. a_f64_abs:
  931. WriteByte($99);
  932. a_f64_neg:
  933. WriteByte($9A);
  934. a_f64_ceil:
  935. WriteByte($9B);
  936. a_f64_floor:
  937. WriteByte($9C);
  938. a_f64_trunc:
  939. WriteByte($9D);
  940. a_f64_nearest:
  941. WriteByte($9E);
  942. a_f64_sqrt:
  943. WriteByte($9F);
  944. a_f64_add:
  945. WriteByte($A0);
  946. a_f64_sub:
  947. WriteByte($A1);
  948. a_f64_mul:
  949. WriteByte($A2);
  950. a_f64_div:
  951. WriteByte($A3);
  952. a_f64_min:
  953. WriteByte($A4);
  954. a_f64_max:
  955. WriteByte($A5);
  956. a_f64_copysign:
  957. WriteByte($A6);
  958. a_i32_wrap_i64:
  959. WriteByte($A7);
  960. a_i32_trunc_f32_s:
  961. WriteByte($A8);
  962. a_i32_trunc_f32_u:
  963. WriteByte($A9);
  964. a_i32_trunc_f64_s:
  965. WriteByte($AA);
  966. a_i32_trunc_f64_u:
  967. WriteByte($AB);
  968. a_i64_extend_i32_s:
  969. WriteByte($AC);
  970. a_i64_extend_i32_u:
  971. WriteByte($AD);
  972. a_i64_trunc_f32_s:
  973. WriteByte($AE);
  974. a_i64_trunc_f32_u:
  975. WriteByte($AF);
  976. a_i64_trunc_f64_s:
  977. WriteByte($B0);
  978. a_i64_trunc_f64_u:
  979. WriteByte($B1);
  980. a_f32_convert_i32_s:
  981. WriteByte($B2);
  982. a_f32_convert_i32_u:
  983. WriteByte($B3);
  984. a_f32_convert_i64_s:
  985. WriteByte($B4);
  986. a_f32_convert_i64_u:
  987. WriteByte($B5);
  988. a_f32_demote_f64:
  989. WriteByte($B6);
  990. a_f64_convert_i32_s:
  991. WriteByte($B7);
  992. a_f64_convert_i32_u:
  993. WriteByte($B8);
  994. a_f64_convert_i64_s:
  995. WriteByte($B9);
  996. a_f64_convert_i64_u:
  997. WriteByte($BA);
  998. a_f64_promote_f32:
  999. WriteByte($BB);
  1000. a_i32_reinterpret_f32:
  1001. WriteByte($BC);
  1002. a_i64_reinterpret_f64:
  1003. WriteByte($BD);
  1004. a_f32_reinterpret_i32:
  1005. WriteByte($BE);
  1006. a_f64_reinterpret_i64:
  1007. WriteByte($BF);
  1008. a_i32_extend8_s:
  1009. WriteByte($C0);
  1010. a_i32_extend16_s:
  1011. WriteByte($C1);
  1012. a_i64_extend8_s:
  1013. WriteByte($C2);
  1014. a_i64_extend16_s:
  1015. WriteByte($C3);
  1016. a_i64_extend32_s:
  1017. WriteByte($C4);
  1018. a_end_block,
  1019. a_end_if,
  1020. a_end_loop,
  1021. a_end_try:
  1022. WriteByte($0B);
  1023. a_catch_all:
  1024. WriteByte($19);
  1025. a_i32_const:
  1026. begin
  1027. WriteByte($41);
  1028. if ops<>1 then
  1029. internalerror(2021092001);
  1030. with oper[0]^ do
  1031. case typ of
  1032. top_ref:
  1033. begin
  1034. if assigned(ref^.symbol) then
  1035. objdata.writeReloc(ref^.offset,5,ObjData.symbolref(ref^.symbol),RELOC_MEMORY_ADDR_OR_TABLE_INDEX_SLEB)
  1036. else
  1037. begin
  1038. if assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) then
  1039. internalerror(2021092018);
  1040. WriteSleb(longint(ref^.offset));
  1041. end;
  1042. end;
  1043. top_const:
  1044. WriteSleb(longint(val));
  1045. else
  1046. internalerror(2021092615);
  1047. end;
  1048. end;
  1049. a_i64_const:
  1050. begin
  1051. WriteByte($42);
  1052. if ops<>1 then
  1053. internalerror(2021092001);
  1054. with oper[0]^ do
  1055. case typ of
  1056. top_ref:
  1057. begin
  1058. if assigned(ref^.symbol) then
  1059. objdata.writeReloc(ref^.offset,5,ObjData.symbolref(ref^.symbol),RELOC_MEMORY_ADDR_OR_TABLE_INDEX_SLEB)
  1060. else
  1061. begin
  1062. if assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) then
  1063. internalerror(2021092018);
  1064. WriteSleb(int64(ref^.offset));
  1065. end;
  1066. end;
  1067. top_const:
  1068. WriteSleb(int64(val));
  1069. else
  1070. internalerror(2021092615);
  1071. end;
  1072. end;
  1073. a_f32_const:
  1074. begin
  1075. if ops<>1 then
  1076. internalerror(2021092619);
  1077. WriteByte($44);
  1078. with oper[0]^ do
  1079. case typ of
  1080. top_single:
  1081. WriteSingle(sval);
  1082. else
  1083. internalerror(2021092618);
  1084. end;
  1085. end;
  1086. a_f64_const:
  1087. begin
  1088. if ops<>1 then
  1089. internalerror(2021092616);
  1090. WriteByte($44);
  1091. with oper[0]^ do
  1092. case typ of
  1093. top_double:
  1094. WriteDouble(dval);
  1095. else
  1096. internalerror(2021092617);
  1097. end;
  1098. end;
  1099. a_local_get,
  1100. a_local_set,
  1101. a_local_tee:
  1102. begin
  1103. case opcode of
  1104. a_local_get:
  1105. WriteByte($20);
  1106. a_local_set:
  1107. WriteByte($21);
  1108. a_local_tee:
  1109. WriteByte($22);
  1110. else
  1111. internalerror(2021092003);
  1112. end;
  1113. if ops<>1 then
  1114. internalerror(2021092004);
  1115. with oper[0]^ do
  1116. case typ of
  1117. top_ref:
  1118. begin
  1119. if assigned(ref^.symbol) then
  1120. internalerror(2021092005);
  1121. if ref^.base<>NR_STACK_POINTER_REG then
  1122. internalerror(2021092006);
  1123. if ref^.index<>NR_NO then
  1124. internalerror(2021092007);
  1125. WriteUleb(ref^.offset);
  1126. end;
  1127. else
  1128. internalerror(2021092008);
  1129. end;
  1130. end;
  1131. a_global_get,
  1132. a_global_set:
  1133. begin
  1134. case opcode of
  1135. a_global_get:
  1136. WriteByte($23);
  1137. a_global_set:
  1138. WriteByte($24);
  1139. else
  1140. internalerror(2021092009);
  1141. end;
  1142. if ops<>1 then
  1143. internalerror(2021092010);
  1144. with oper[0]^ do
  1145. case typ of
  1146. top_ref:
  1147. begin
  1148. if not assigned(ref^.symbol) then
  1149. internalerror(2021092012);
  1150. if (ref^.base<>NR_NO) or (ref^.index<>NR_NO) or (ref^.offset<>0) then
  1151. internalerror(2021092013);
  1152. objdata.writeReloc(0,5,TWasmObjData(ObjData).globalref(ref^.symbol),RELOC_GLOBAL_INDEX_LEB);
  1153. end;
  1154. else
  1155. internalerror(2021092011);
  1156. end;
  1157. end;
  1158. a_end_function:
  1159. ;
  1160. a_block,
  1161. a_loop,
  1162. a_if,
  1163. a_try:
  1164. begin
  1165. case opcode of
  1166. a_block:
  1167. WriteByte($02);
  1168. a_loop:
  1169. WriteByte($03);
  1170. a_if:
  1171. WriteByte($04);
  1172. a_try:
  1173. WriteByte($06);
  1174. else
  1175. internalerror(2021092626);
  1176. end;
  1177. if ops=0 then
  1178. WriteByte($40)
  1179. else
  1180. begin
  1181. if ops<>1 then
  1182. internalerror(2021092015);
  1183. with oper[0]^ do
  1184. case typ of
  1185. top_functype:
  1186. begin
  1187. if (length(functype.params)=0) and (length(functype.results)<=1) then
  1188. begin
  1189. if length(functype.results)=1 then
  1190. WriteByte(encode_wasm_basic_type(functype.results[0]))
  1191. else
  1192. WriteByte($40);
  1193. end
  1194. else
  1195. { more complex blocktypes are not yet implemented }
  1196. internalerror(2021092621);
  1197. end;
  1198. else
  1199. internalerror(2021092620);
  1200. end;
  1201. end;
  1202. end;
  1203. a_else:
  1204. WriteByte($05);
  1205. a_i32_load,
  1206. a_i64_load,
  1207. a_f32_load,
  1208. a_f64_load,
  1209. a_i32_load8_s,
  1210. a_i32_load8_u,
  1211. a_i32_load16_s,
  1212. a_i32_load16_u,
  1213. a_i64_load8_s,
  1214. a_i64_load8_u,
  1215. a_i64_load16_s,
  1216. a_i64_load16_u,
  1217. a_i64_load32_s,
  1218. a_i64_load32_u,
  1219. a_i32_store,
  1220. a_i64_store,
  1221. a_f32_store,
  1222. a_f64_store,
  1223. a_i32_store8,
  1224. a_i32_store16,
  1225. a_i64_store8,
  1226. a_i64_store16,
  1227. a_i64_store32:
  1228. begin
  1229. case opcode of
  1230. a_i32_load:
  1231. WriteByte($28);
  1232. a_i64_load:
  1233. WriteByte($29);
  1234. a_f32_load:
  1235. WriteByte($2A);
  1236. a_f64_load:
  1237. WriteByte($2B);
  1238. a_i32_load8_s:
  1239. WriteByte($2C);
  1240. a_i32_load8_u:
  1241. WriteByte($2D);
  1242. a_i32_load16_s:
  1243. WriteByte($2E);
  1244. a_i32_load16_u:
  1245. WriteByte($2F);
  1246. a_i64_load8_s:
  1247. WriteByte($30);
  1248. a_i64_load8_u:
  1249. WriteByte($31);
  1250. a_i64_load16_s:
  1251. WriteByte($32);
  1252. a_i64_load16_u:
  1253. WriteByte($33);
  1254. a_i64_load32_s:
  1255. WriteByte($34);
  1256. a_i64_load32_u:
  1257. WriteByte($35);
  1258. a_i32_store:
  1259. WriteByte($36);
  1260. a_i64_store:
  1261. WriteByte($37);
  1262. a_f32_store:
  1263. WriteByte($38);
  1264. a_f64_store:
  1265. WriteByte($39);
  1266. a_i32_store8:
  1267. WriteByte($3A);
  1268. a_i32_store16:
  1269. WriteByte($3B);
  1270. a_i64_store8:
  1271. WriteByte($3C);
  1272. a_i64_store16:
  1273. WriteByte($3D);
  1274. a_i64_store32:
  1275. WriteByte($3E);
  1276. else
  1277. internalerror(2021092019);
  1278. end;
  1279. if ops<>1 then
  1280. internalerror(2021092016);
  1281. with oper[0]^ do
  1282. case typ of
  1283. top_ref:
  1284. begin
  1285. if assigned(ref^.symbol) then
  1286. begin
  1287. WriteUleb(natural_alignment_for_load_store(opcode));
  1288. objdata.writeReloc(ref^.offset,5,ObjData.symbolref(ref^.symbol),RELOC_MEMORY_ADDR_LEB);
  1289. end
  1290. else
  1291. begin
  1292. if assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) then
  1293. internalerror(2021092018);
  1294. WriteUleb(natural_alignment_for_load_store(opcode));
  1295. WriteUleb(ref^.offset);
  1296. end;
  1297. end;
  1298. top_const:
  1299. begin
  1300. WriteUleb(natural_alignment_for_load_store(opcode));
  1301. WriteUleb(val);
  1302. end;
  1303. else
  1304. internalerror(2021092017);
  1305. end;
  1306. end;
  1307. a_call:
  1308. begin
  1309. if ops<>1 then
  1310. internalerror(2021092021);
  1311. with oper[0]^ do
  1312. case typ of
  1313. top_ref:
  1314. begin
  1315. if not assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) or (ref^.offset<>0) then
  1316. internalerror(2021092023);
  1317. WriteByte($10);
  1318. objdata.writeReloc(0,5,ObjData.symbolref(ref^.symbol),RELOC_FUNCTION_INDEX_LEB);
  1319. end;
  1320. else
  1321. internalerror(2021092022);
  1322. end;
  1323. end;
  1324. a_call_indirect:
  1325. begin
  1326. if ops<>1 then
  1327. internalerror(2021092610);
  1328. with oper[0]^ do
  1329. case typ of
  1330. top_functype:
  1331. begin
  1332. WriteByte($11);
  1333. objdata.writeReloc(TWasmObjData(objdata).AddFuncType(functype),5,nil,RELOC_TYPE_INDEX_LEB);
  1334. WriteUleb(0);
  1335. end;
  1336. else
  1337. internalerror(2021092611);
  1338. end;
  1339. end;
  1340. a_br,
  1341. a_br_if,
  1342. a_rethrow,
  1343. a_delegate:
  1344. begin
  1345. case opcode of
  1346. a_br:
  1347. WriteByte($0C);
  1348. a_br_if:
  1349. WriteByte($0D);
  1350. a_rethrow:
  1351. WriteByte($09);
  1352. a_delegate:
  1353. WriteByte($18);
  1354. else
  1355. internalerror(2021092622);
  1356. end;
  1357. if ops<>1 then
  1358. internalerror(2021092610);
  1359. with oper[0]^ do
  1360. case typ of
  1361. top_const:
  1362. WriteUleb(val);
  1363. else
  1364. internalerror(2021092625);
  1365. end;
  1366. end;
  1367. else
  1368. internalerror(2021092624);
  1369. end;
  1370. end;
  1371. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  1372. begin
  1373. internalerror(2010122614);
  1374. result:=nil;
  1375. end;
  1376. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  1377. begin
  1378. internalerror(2010122615);
  1379. result:=nil;
  1380. end;
  1381. procedure InitAsm;
  1382. begin
  1383. end;
  1384. procedure DoneAsm;
  1385. begin
  1386. end;
  1387. initialization
  1388. cai_cpu:=taicpu;
  1389. cai_align:=tai_align;
  1390. casmdata:=TAsmData;
  1391. end.