aasmcpu.pas 41 KB

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