agwat.pas 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  1. {
  2. Copyright (c) 1998-2010 by the Free Pascal team
  3. This unit implements the WebAssembly text assembler
  4. The text is in S-expression format and should be consumable
  5. By either Binaryen or Wabt
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. ****************************************************************************
  18. }
  19. unit agwat;
  20. interface
  21. uses
  22. cclasses,systems, cgutils,
  23. globtype,globals,
  24. symconst,symbase,symdef,symsym, symtype,symcpu,
  25. aasmbase,aasmtai,aasmdata,aasmcpu,
  26. assemble
  27. ,cutils
  28. ,cpubase
  29. ,fmodule
  30. ,verbose, itcpuwasm;
  31. type
  32. TWatInstrWriter = class;
  33. {# This is a derived class which is used to write
  34. Binaryen-styled assembler.
  35. }
  36. { TWatAssembler }
  37. { TWabtTextAssembler }
  38. TWabtTextAssembler=class(texternalassembler)
  39. protected
  40. procedure WriteInstruction(hp: tai);
  41. procedure WriteProcDef(pd: tprocdef);
  42. procedure WriteProcParams(pd: tprocdef);
  43. procedure WriteProcResult(pd: tprocdef);
  44. procedure WriteSymtableProcdefs(st: TSymtable);
  45. procedure WriteSymtableVarSyms(st: TSymtable);
  46. procedure WriteTempAlloc(p:TAsmList);
  47. procedure WriteExports(p: TAsmList);
  48. procedure WriteImports(p: TAsmList);
  49. public
  50. constructor CreateWithWriter(info: pasminfo; wr: TExternalAssemblerOutputFile; freewriter, smart: boolean); override;
  51. procedure WriteTree(p:TAsmList);override;
  52. procedure WriteAsmList;override;
  53. end;
  54. {# This is the base class for writing instructions.
  55. The WriteInstruction() method must be overridden
  56. to write a single instruction to the assembler
  57. file.
  58. }
  59. { TBinaryenInstrWriter }
  60. { TWatInstrWriter }
  61. TWatInstrWriter = class
  62. constructor create(_owner: TWabtTextAssembler);
  63. procedure WriteInstruction(hp : tai); virtual;
  64. protected
  65. owner: TWabtTextAssembler;
  66. end;
  67. implementation
  68. type
  69. t64bitarray = array[0..7] of byte;
  70. t32bitarray = array[0..3] of byte;
  71. const
  72. line_length = 70;
  73. {****************************************************************************}
  74. { Support routines }
  75. {****************************************************************************}
  76. function fixline(s:string):string;
  77. {
  78. return s with all leading and ending spaces and tabs removed
  79. }
  80. var
  81. i,j,k : integer;
  82. begin
  83. i:=length(s);
  84. while (i>0) and (s[i] in [#9,' ']) do
  85. dec(i);
  86. j:=1;
  87. while (j<i) and (s[j] in [#9,' ']) do
  88. inc(j);
  89. for k:=j to i do
  90. if s[k] in [#0..#31,#127..#255] then
  91. s[k]:='.';
  92. fixline:=Copy(s,j,i-j+1);
  93. end;
  94. function GetWasmName(const st: TSymStr): ansistring;
  95. begin
  96. Result := '$'+st;
  97. Replace(Result, '(','');
  98. Replace(Result, ')','');
  99. end;
  100. function getreferencestring(var ref : treference) : ansistring;
  101. begin
  102. if (ref.index<>NR_NO) then
  103. internalerror(2010122809);
  104. if assigned(ref.symbol) then
  105. begin
  106. // global symbol or field -> full type and name
  107. // ref.base can be <> NR_NO in case an instance field is loaded.
  108. // This register is not part of this instruction, it will have
  109. // been placed on the stack by the previous one.
  110. result:=GetWasmName(ref.symbol.name);
  111. end
  112. else
  113. begin
  114. // local symbol -> stack slot, stored in offset
  115. if ref.base<>NR_STACK_POINTER_REG then
  116. internalerror(2010122810);
  117. result:=tostr(ref.offset);
  118. end;
  119. end;
  120. function constsingle(s: single): ansistring;
  121. begin
  122. result:='0x'+hexstr(longint(t32bitarray(s)),8);
  123. end;
  124. function constdouble(d: double): ansistring;
  125. begin
  126. // force interpretation as double (since we write it out as an
  127. // integer, we never have to swap the endianess). We have to
  128. // include the sign separately because of the way Java parses
  129. // hex numbers (0x8000000000000000 is not a valid long)
  130. result:=hexstr(abs(int64(t64bitarray(d))),16);
  131. if int64(t64bitarray(d))<0 then
  132. result:='-'+result;
  133. result:='0dx'+result;
  134. end;
  135. function getopstr(const o:toper) : ansistring;
  136. var
  137. d: double;
  138. s: single;
  139. begin
  140. case o.typ of
  141. top_reg:
  142. // should have been translated into a memory location by the
  143. // register allocator)
  144. if (cs_no_regalloc in current_settings.globalswitches) then
  145. getopstr:=std_regname(o.reg)
  146. else
  147. internalerror(2010122803);
  148. top_const:
  149. str(o.val,result);
  150. top_ref:
  151. getopstr:=getreferencestring(o.ref^);
  152. top_single:
  153. begin
  154. result:=constsingle(o.sval);
  155. end;
  156. top_double:
  157. begin
  158. result:=constdouble(o.dval);
  159. end;
  160. {top_string:
  161. begin
  162. result:=constastr(o.pcval,o.pcvallen);
  163. end;
  164. top_wstring:
  165. begin
  166. result:=constwstr(o.pwstrval^.data,getlengthwidestring(o.pwstrval));
  167. end}
  168. else
  169. internalerror(2010122802);
  170. end;
  171. end;
  172. { TWabtTextAssembler }
  173. procedure TWabtTextAssembler.WriteInstruction(hp: tai);
  174. var
  175. cpu : taicpu;
  176. i : integer;
  177. begin
  178. //writer.AsmWriteLn('instr');
  179. cpu := taicpu(hp);
  180. writer.AsmWrite(#9);
  181. writer.AsmWrite(wasm_op2str[cpu.opcode] );
  182. if (cpu.opcode = a_if) then
  183. writer.AsmWrite(' (result i32)'); //todo: this is a hardcode, but shouldn't
  184. cpu := taicpu(hp);
  185. if cpu.ops<>0 then
  186. begin
  187. for i:=0 to cpu.ops-1 do
  188. begin
  189. writer.AsmWrite(#9);
  190. if (cpu.opcode in AsmOp_LoadStore) and (cpu.oper[i]^.typ = top_ref) then
  191. writer.AsmWrite('offset='+tostr( cpu.oper[i]^.ref^.offset))
  192. else
  193. writer.AsmWrite(getopstr(cpu.oper[i]^));
  194. end;
  195. end;
  196. writer.AsmLn;
  197. end;
  198. procedure TWabtTextAssembler.WriteProcDef(pd: tprocdef);
  199. var
  200. i : integer;
  201. begin
  202. if not assigned(tcpuprocdef(pd).exprasmlist) and
  203. not(po_abstractmethod in pd.procoptions) and
  204. (not is_javainterface(pd.struct) or
  205. (pd.proctypeoption in [potype_unitinit,potype_unitfinalize])) then
  206. begin
  207. exit;
  208. end;
  209. writer.AsmWrite('(func ');
  210. writer.AsmWrite( GetWasmName( pd.mangledname ));
  211. //procsym.RealName ));
  212. //writer.AsmWriteln(MethodDefinition(pd));
  213. {if jvmtypeneedssignature(pd) then
  214. begin
  215. writer.AsmWrite('.signature "');
  216. writer.AsmWrite(tcpuprocdef(pd).jvmmangledbasename(true));
  217. writer.AsmWriteln('"');
  218. end;}
  219. writer.AsmLn;
  220. WriteProcParams(tcpuprocdef(pd));
  221. WriteProcResult(tcpuprocdef(pd));
  222. WriteTempAlloc(tcpuprocdef(pd).exprasmlist);
  223. WriteTree(tcpuprocdef(pd).exprasmlist);
  224. writer.AsmWriteln(')');
  225. writer.AsmLn;
  226. end;
  227. procedure TWabtTextAssembler.WriteProcParams(pd: tprocdef);
  228. var
  229. i : integer;
  230. prm : tcpuparavarsym;
  231. begin
  232. if not Assigned(pd) or
  233. not Assigned(pd.paras) or
  234. (pd.paras.Count=0) then
  235. exit;
  236. for i:=0 to pd.paras.Count-1 do
  237. begin
  238. prm := tcpuparavarsym(pd.paras[i]);
  239. writer.AsmWrite(#9'(param'#9);
  240. case prm.getsize of
  241. 1..4: writer.AsmWrite('i32');
  242. 8: writer.AsmWrite('i64');
  243. end;
  244. writer.AsmWrite(')');
  245. writer.AsmLn;
  246. end;
  247. end;
  248. procedure TWabtTextAssembler.WriteProcResult(pd: tprocdef);
  249. begin
  250. if not assigned(pd) or
  251. not Assigned(pd.returndef) or
  252. (pd.returndef.size = 0)
  253. then exit;
  254. writer.AsmWrite(#9'(result'#9);
  255. case pd.returndef.size of
  256. 1..4: writer.AsmWrite('i32');
  257. 8: writer.AsmWrite('i64');
  258. end;
  259. writer.AsmWrite(')');
  260. writer.AsmLn;
  261. end;
  262. procedure TWabtTextAssembler.WriteTree(p: TAsmList);
  263. var
  264. ch : char;
  265. hp : tai;
  266. hp1 : tailineinfo;
  267. s : ansistring;
  268. i,pos : longint;
  269. InlineLevel : longint;
  270. do_line : boolean;
  271. const
  272. WasmBasicTypeStr : array [TWasmBasicType] of string = ('i32','i64','f32','f64');
  273. begin
  274. if not assigned(p) then
  275. exit;
  276. InlineLevel:=0;
  277. { lineinfo is only needed for al_procedures (PFV) }
  278. do_line:=(cs_asm_source in current_settings.globalswitches);
  279. hp:=tai(p.first);
  280. while assigned(hp) do
  281. begin
  282. prefetch(pointer(hp.next)^);
  283. if not(hp.typ in SkipLineInfo) then
  284. begin
  285. hp1 := hp as tailineinfo;
  286. current_filepos:=hp1.fileinfo;
  287. { no line info for inlined code }
  288. if do_line and (inlinelevel=0) then
  289. begin
  290. { load infile }
  291. if lastfileinfo.fileindex<>hp1.fileinfo.fileindex then
  292. begin
  293. infile:=current_module.sourcefiles.get_file(hp1.fileinfo.fileindex);
  294. if assigned(infile) then
  295. begin
  296. { open only if needed !! }
  297. if (cs_asm_source in current_settings.globalswitches) then
  298. infile.open;
  299. end;
  300. { avoid unnecessary reopens of the same file !! }
  301. lastfileinfo.fileindex:=hp1.fileinfo.fileindex;
  302. { be sure to change line !! }
  303. lastfileinfo.line:=-1;
  304. end;
  305. { write source }
  306. if (cs_asm_source in current_settings.globalswitches) and
  307. assigned(infile) then
  308. begin
  309. if (infile<>lastinfile) then
  310. begin
  311. writer.AsmWriteLn(asminfo^.comment+'['+infile.name+']');
  312. if assigned(lastinfile) then
  313. lastinfile.close;
  314. end;
  315. if (hp1.fileinfo.line<>lastfileinfo.line) and
  316. ((hp1.fileinfo.line<infile.maxlinebuf) or (InlineLevel>0)) then
  317. begin
  318. if (hp1.fileinfo.line<>0) and
  319. ((infile.linebuf^[hp1.fileinfo.line]>=0) or (InlineLevel>0)) then
  320. writer.AsmWriteLn(asminfo^.comment+'['+tostr(hp1.fileinfo.line)+'] '+
  321. fixline(infile.GetLineStr(hp1.fileinfo.line)));
  322. { set it to a negative value !
  323. to make that is has been read already !! PM }
  324. if (infile.linebuf^[hp1.fileinfo.line]>=0) then
  325. infile.linebuf^[hp1.fileinfo.line]:=-infile.linebuf^[hp1.fileinfo.line]-1;
  326. end;
  327. end;
  328. lastfileinfo:=hp1.fileinfo;
  329. lastinfile:=infile;
  330. end;
  331. end;
  332. case hp.typ of
  333. ait_comment :
  334. Begin
  335. writer.AsmWrite(asminfo^.comment);
  336. writer.AsmWritePChar(tai_comment(hp).str);
  337. writer.AsmLn;
  338. End;
  339. ait_regalloc :
  340. begin
  341. if (cs_asm_regalloc in current_settings.globalswitches) then
  342. begin
  343. writer.AsmWrite(#9+asminfo^.comment+'Register ');
  344. repeat
  345. writer.AsmWrite(std_regname(Tai_regalloc(hp).reg));
  346. if (hp.next=nil) or
  347. (tai(hp.next).typ<>ait_regalloc) or
  348. (tai_regalloc(hp.next).ratype<>tai_regalloc(hp).ratype) then
  349. break;
  350. hp:=tai(hp.next);
  351. writer.AsmWrite(',');
  352. until false;
  353. writer.AsmWrite(' ');
  354. writer.AsmWriteLn(regallocstr[tai_regalloc(hp).ratype]);
  355. end;
  356. end;
  357. ait_tempalloc :
  358. begin
  359. if (cs_asm_tempalloc in current_settings.globalswitches) then
  360. begin
  361. {$ifdef EXTDEBUG}
  362. if assigned(tai_tempalloc(hp).problem) then
  363. writer.AsmWriteLn(asminfo^.comment+'Temp '+tostr(tai_tempalloc(hp).temppos)+','+
  364. tostr(tai_tempalloc(hp).tempsize)+' '+tai_tempalloc(hp).problem^)
  365. else
  366. {$endif EXTDEBUG}
  367. end;
  368. end;
  369. ait_align :
  370. begin
  371. end;
  372. ait_section :
  373. begin
  374. end;
  375. ait_datablock :
  376. begin
  377. // internalerror(2010122701);
  378. end;
  379. ait_const:
  380. begin
  381. writer.AsmWriteln('constant');
  382. // internalerror(2010122702);
  383. end;
  384. ait_realconst :
  385. begin
  386. internalerror(2010122703);
  387. end;
  388. ait_string :
  389. begin
  390. pos:=0;
  391. for i:=1 to tai_string(hp).len do
  392. begin
  393. if pos=0 then
  394. begin
  395. writer.AsmWrite(#9'strconst: '#9'"');
  396. pos:=20;
  397. end;
  398. ch:=tai_string(hp).str[i-1];
  399. case ch of
  400. #0, {This can't be done by range, because a bug in FPC}
  401. #1..#31,
  402. #128..#255 : s:='\'+tostr(ord(ch) shr 6)+tostr((ord(ch) and 63) shr 3)+tostr(ord(ch) and 7);
  403. '"' : s:='\"';
  404. '\' : s:='\\';
  405. else
  406. s:=ch;
  407. end;
  408. writer.AsmWrite(s);
  409. inc(pos,length(s));
  410. if (pos>line_length) or (i=tai_string(hp).len) then
  411. begin
  412. writer.AsmWriteLn('"');
  413. pos:=0;
  414. end;
  415. end;
  416. end;
  417. ait_label :
  418. begin
  419. // don't write any labels. Wasm don't support it
  420. // labels are only allowed with the respective block structures
  421. end;
  422. ait_symbol :
  423. begin
  424. if (tai_symbol(hp).sym.typ = AT_FUNCTION) then
  425. begin
  426. end
  427. else
  428. begin
  429. writer.AsmWrite('data symbol: ');
  430. writer.AsmWriteln(tai_symbol(hp).sym.name);
  431. // internalerror(2010122706);
  432. end;
  433. end;
  434. ait_symbol_end :
  435. begin
  436. end;
  437. ait_instruction :
  438. begin
  439. WriteInstruction(hp);
  440. end;
  441. ait_force_line,
  442. ait_function_name : ;
  443. ait_cutobject :
  444. begin
  445. end;
  446. ait_marker :
  447. if tai_marker(hp).kind=mark_NoLineInfoStart then
  448. inc(InlineLevel)
  449. else if tai_marker(hp).kind=mark_NoLineInfoEnd then
  450. dec(InlineLevel);
  451. ait_directive :
  452. begin
  453. { the CPU directive is probably not supported by the JVM assembler,
  454. so it's commented out }
  455. //todo:
  456. writer.AsmWrite(asminfo^.comment);
  457. if tai_directive(hp).directive=asd_cpu then
  458. writer.AsmWrite(asminfo^.comment);
  459. writer.AsmWrite('.'+directivestr[tai_directive(hp).directive]+' ');
  460. if tai_directive(hp).name<>'' then
  461. writer.AsmWrite(tai_directive(hp).name);
  462. writer.AsmLn;
  463. end;
  464. ait_local :
  465. begin
  466. writer.AsmWrite(#9'(local ');
  467. writer.AsmWrite( WasmBasicTypeStr[ tai_local(hp).bastyp ] );
  468. writer.AsmWrite(')');
  469. writer.AsmLn;
  470. end;
  471. else
  472. internalerror(2010122707);
  473. end;
  474. hp:=tai(hp.next);
  475. end;
  476. end;
  477. procedure TWabtTextAssembler.WriteAsmList;
  478. var
  479. hal : tasmlisttype;
  480. begin
  481. if current_module.is_unit then begin
  482. writer.AsmWriteLn('(module)');
  483. exit;
  484. end;
  485. writer.MarkEmpty;
  486. writer.AsmWriteLn('(module ');
  487. writer.AsmWriteLn('(import "env" "memory" (memory 0)) ;;');
  488. WriteImports(current_asmdata.asmlists[al_imports]);
  489. { print all global variables }
  490. //current_asmdata.AsmSymbolDict
  491. WriteSymtableVarSyms(current_module.globalsymtable);
  492. WriteSymtableVarSyms(current_module.localsymtable);
  493. //writer.AsmLn;
  494. { print all global procedures/functions }
  495. WriteSymtableProcdefs(current_module.globalsymtable);
  496. WriteSymtableProcdefs(current_module.localsymtable);
  497. WriteExports(current_asmdata.asmlists[al_exports]);
  498. //WriteSymtableStructDefs(current_module.globalsymtable);
  499. //WriteSymtableStructDefs(current_module.localsymtable);
  500. //writer.decorator.LinePrefix := '';
  501. writer.AsmWriteLn(')');
  502. writer.AsmLn;
  503. end;
  504. constructor TWabtTextAssembler.CreateWithWriter(info: pasminfo;
  505. wr: TExternalAssemblerOutputFile; freewriter, smart: boolean);
  506. begin
  507. inherited CreateWithWriter(info, wr, freewriter, smart);
  508. end;
  509. procedure TWabtTextAssembler.WriteSymtableProcdefs(st: TSymtable);
  510. var
  511. i : longint;
  512. def : tdef;
  513. begin
  514. if not assigned(st) then
  515. exit;
  516. for i:=0 to st.DefList.Count-1 do
  517. begin
  518. def:=tdef(st.DefList[i]);
  519. case def.typ of
  520. procdef :
  521. begin
  522. { methods are also in the static/globalsymtable of the unit
  523. -> make sure they are only written for the objectdefs that
  524. own them }
  525. if (not(st.symtabletype in [staticsymtable,globalsymtable]) or
  526. (def.owner=st)) and
  527. not(df_generic in def.defoptions) then
  528. begin
  529. WriteProcDef(tprocdef(def));
  530. if assigned(tprocdef(def).localst) then
  531. WriteSymtableProcdefs(tprocdef(def).localst);
  532. end;
  533. end;
  534. else
  535. ;
  536. end;
  537. end;
  538. end;
  539. procedure TWabtTextAssembler.WriteSymtableVarSyms(st: TSymtable);
  540. var
  541. i : integer;
  542. sym : tsym;
  543. sz : integer;
  544. begin
  545. if not assigned(st) then
  546. exit;
  547. sz := 0;
  548. for i:=0 to st.SymList.Count-1 do
  549. begin
  550. sym:=tsym(st.SymList[i]);
  551. case sym.typ of
  552. staticvarsym:
  553. begin
  554. //WriteFieldSym(tabstractvarsym(sym));
  555. //if (sym.typ=staticvarsym) and
  556. // assigned(tstaticvarsym(sym).defaultconstsym) then
  557. // WriteFieldSym(tabstractvarsym(tstaticvarsym(sym).defaultconstsym));
  558. writer.AsmWrite(#9);
  559. writer.AsmWrite('(global $');
  560. writer.AsmWrite(tcpustaticvarsym(sym).mangledname);
  561. writer.AsmWrite(' (mut i32) (i32.const ');
  562. writer.AsmWrite( tostr(sz));
  563. writer.AsmWrite(')');
  564. writer.AsmWrite(') ');
  565. writer.AsmWriteLn(';; static or field');
  566. inc(sz, tcpustaticvarsym(sym).getsize);
  567. end;
  568. fieldvarsym:
  569. begin
  570. writer.AsmWriteLn(';; field');
  571. end;
  572. constsym:
  573. begin
  574. //if (sym.typ=staticvarsym) and
  575. // assigned(tstaticvarsym(sym).defaultconstsym) then
  576. // WriteFieldSym(tabstractvarsym(tstaticvarsym(sym).defaultconstsym));
  577. //{ multiple procedures can have constants with the same name }
  578. //if not assigned(sym.owner.defowner) or
  579. // (tdef(sym.owner.defowner).typ<>procdef) then
  580. // WriteConstSym(tconstsym(sym));
  581. writer.AsmWriteLn(';; constant');
  582. end;
  583. {procsym:
  584. begin
  585. for j:=0 to tprocsym(sym).procdeflist.count-1 do
  586. if not(df_generic in tprocdef(tprocsym(sym).procdeflist[j]).defoptions) then
  587. WriteSymtableVarSyms(tprocdef(tprocsym(sym).procdeflist[j]).localst);
  588. end;}
  589. else
  590. ;
  591. end;
  592. end;
  593. end;
  594. procedure TWabtTextAssembler.WriteTempAlloc(p: TAsmList);
  595. var
  596. hp: tai;
  597. tmp: array of tai_tempalloc;
  598. mx : integer;
  599. i : integer;
  600. begin
  601. if not assigned(p) then
  602. exit;
  603. mx := -1;
  604. hp:=tai(p.first);
  605. while assigned(hp) do
  606. begin
  607. //prefetch(pointer(hp.next)^);
  608. if (hp.typ = ait_tempalloc) and
  609. tai_tempalloc(hp).allocation and
  610. (tai_tempalloc(hp).temppos >= mx) then
  611. mx := tai_tempalloc(hp).temppos+1;
  612. hp:=tai(hp.next);
  613. end;
  614. if (mx <= 0) then exit; // no temp allocations used
  615. SetLength(tmp, mx);
  616. hp:=tai(p.first);
  617. while assigned(hp) do
  618. begin
  619. //prefetch(pointer(hp.next)^);
  620. if (hp.typ = ait_tempalloc) and
  621. (tai_tempalloc(hp).allocation) and
  622. (tmp[ tai_tempalloc(hp).temppos ] = nil) then
  623. begin
  624. tmp[ tai_tempalloc(hp).temppos ] := tai_tempalloc(hp);
  625. dec(mx);
  626. if mx = 0 then break;
  627. end;
  628. hp:=tai(hp.next);
  629. end;
  630. for i:=0 to length(tmp)-1 do
  631. begin
  632. if tmp[i] = nil then continue;
  633. writer.AsmWrite(#9'(local'#9);
  634. if tmp[i].tempsize<=4 then writer.AsmWrite('i32')
  635. else if tmp[i].tempsize = 8 then writer.AsmWrite('i64');
  636. writer.AsmWrite(')');
  637. writer.AsmWrite(#9+asminfo^.comment+'Temp '+tostr(tmp[i].temppos)+','+
  638. tostr(tmp[i].tempsize)+' '+tempallocstr[tmp[i].allocation]);
  639. writer.AsmLn;
  640. end;
  641. end;
  642. procedure TWabtTextAssembler.WriteExports(p: TAsmList);
  643. var
  644. hp: tai;
  645. x: tai_impexp;
  646. begin
  647. if not Assigned(p) then Exit;
  648. hp:=tai(p.First);
  649. while Assigned(hp) do begin
  650. case hp.typ of
  651. ait_importexport:
  652. begin
  653. x:=tai_impexp(hp);
  654. writer.AsmWrite('(export "');
  655. writer.AsmWrite(x.extname);
  656. writer.AsmWrite('" (');
  657. case x.symstype of
  658. ie_Func: writer.AsmWrite('func');
  659. end;
  660. writer.AsmWrite(' ');
  661. writer.AsmWrite(GetWasmName(x.intname));
  662. writer.AsmWrite('))');
  663. writer.AsmLn;
  664. end;
  665. end;
  666. hp := tai_impexp(hp.Next);
  667. end;
  668. end;
  669. procedure TWabtTextAssembler.WriteImports(p: TAsmList);
  670. var
  671. hp: tai;
  672. x: tai_impexp;
  673. begin
  674. if not Assigned(p) then Exit;
  675. hp:=tai(p.First);
  676. while Assigned(hp) do begin
  677. case hp.typ of
  678. ait_importexport:
  679. begin
  680. x:=tai_impexp(hp);
  681. writer.AsmWrite('(import "');
  682. writer.AsmWrite(x.extmodule);
  683. writer.AsmWrite('" "');
  684. writer.AsmWrite(x.extname);
  685. writer.AsmWrite('" (');
  686. case x.symstype of
  687. ie_Func: writer.AsmWrite('func');
  688. end;
  689. writer.AsmWrite(' ');
  690. writer.AsmWrite(GetWasmName(x.intname));
  691. writer.AsmWrite('))');
  692. writer.AsmLn;
  693. end;
  694. end;
  695. hp := tai_impexp(hp.Next);
  696. end;
  697. end;
  698. { TWatInstrWriter }
  699. constructor TWatInstrWriter.create(_owner: TWabtTextAssembler);
  700. begin
  701. inherited create;
  702. owner := _owner;
  703. end;
  704. procedure TWatInstrWriter.WriteInstruction(hp: tai);
  705. begin
  706. end;
  707. const
  708. as_wasm_wabt_info : tasminfo =
  709. (
  710. id : as_wasm_wabt;
  711. idtxt : 'Wabt';
  712. asmbin : 'wat2wasm';
  713. asmcmd : '$ASM $EXTRAOPT';
  714. supported_targets : [system_wasm_wasm32];
  715. flags : [];
  716. labelprefix : 'L';
  717. comment : ';; ';
  718. dollarsign : '$';
  719. );
  720. initialization
  721. RegisterAssembler(as_wasm_wabt_info, TWabtTextAssembler);
  722. end.