agwat.pas 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  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. public
  49. constructor CreateWithWriter(info: pasminfo; wr: TExternalAssemblerOutputFile; freewriter, smart: boolean); override;
  50. procedure WriteTree(p:TAsmList);override;
  51. procedure WriteAsmList;override;
  52. end;
  53. {# This is the base class for writing instructions.
  54. The WriteInstruction() method must be overridden
  55. to write a single instruction to the assembler
  56. file.
  57. }
  58. { TBinaryenInstrWriter }
  59. { TWatInstrWriter }
  60. TWatInstrWriter = class
  61. constructor create(_owner: TWabtTextAssembler);
  62. procedure WriteInstruction(hp : tai); virtual;
  63. protected
  64. owner: TWabtTextAssembler;
  65. end;
  66. implementation
  67. type
  68. t64bitarray = array[0..7] of byte;
  69. t32bitarray = array[0..3] of byte;
  70. const
  71. line_length = 70;
  72. {****************************************************************************}
  73. { Support routines }
  74. {****************************************************************************}
  75. function fixline(s:string):string;
  76. {
  77. return s with all leading and ending spaces and tabs removed
  78. }
  79. var
  80. i,j,k : integer;
  81. begin
  82. i:=length(s);
  83. while (i>0) and (s[i] in [#9,' ']) do
  84. dec(i);
  85. j:=1;
  86. while (j<i) and (s[j] in [#9,' ']) do
  87. inc(j);
  88. for k:=j to i do
  89. if s[k] in [#0..#31,#127..#255] then
  90. s[k]:='.';
  91. fixline:=Copy(s,j,i-j+1);
  92. end;
  93. function GetWasmName(const st: TSymStr): ansistring;
  94. begin
  95. Result := '$'+st;
  96. Replace(Result, '(','');
  97. Replace(Result, ')','');
  98. end;
  99. function getreferencestring(var ref : treference) : ansistring;
  100. begin
  101. if (ref.index<>NR_NO) then
  102. internalerror(2010122809);
  103. if assigned(ref.symbol) then
  104. begin
  105. // global symbol or field -> full type and name
  106. // ref.base can be <> NR_NO in case an instance field is loaded.
  107. // This register is not part of this instruction, it will have
  108. // been placed on the stack by the previous one.
  109. result:=GetWasmName(ref.symbol.name);
  110. end
  111. else
  112. begin
  113. // local symbol -> stack slot, stored in offset
  114. if ref.base<>NR_STACK_POINTER_REG then
  115. internalerror(2010122810);
  116. result:=tostr(ref.offset);
  117. end;
  118. end;
  119. function constsingle(s: single): ansistring;
  120. begin
  121. result:='0x'+hexstr(longint(t32bitarray(s)),8);
  122. end;
  123. function constdouble(d: double): ansistring;
  124. begin
  125. // force interpretation as double (since we write it out as an
  126. // integer, we never have to swap the endianess). We have to
  127. // include the sign separately because of the way Java parses
  128. // hex numbers (0x8000000000000000 is not a valid long)
  129. result:=hexstr(abs(int64(t64bitarray(d))),16);
  130. if int64(t64bitarray(d))<0 then
  131. result:='-'+result;
  132. result:='0dx'+result;
  133. end;
  134. function getopstr(const o:toper) : ansistring;
  135. var
  136. d: double;
  137. s: single;
  138. begin
  139. case o.typ of
  140. top_reg:
  141. // should have been translated into a memory location by the
  142. // register allocator)
  143. if (cs_no_regalloc in current_settings.globalswitches) then
  144. getopstr:=std_regname(o.reg)
  145. else
  146. internalerror(2010122803);
  147. top_const:
  148. str(o.val,result);
  149. top_ref:
  150. getopstr:=getreferencestring(o.ref^);
  151. top_single:
  152. begin
  153. result:=constsingle(o.sval);
  154. end;
  155. top_double:
  156. begin
  157. result:=constdouble(o.dval);
  158. end;
  159. {top_string:
  160. begin
  161. result:=constastr(o.pcval,o.pcvallen);
  162. end;
  163. top_wstring:
  164. begin
  165. result:=constwstr(o.pwstrval^.data,getlengthwidestring(o.pwstrval));
  166. end}
  167. else
  168. internalerror(2010122802);
  169. end;
  170. end;
  171. { TWabtTextAssembler }
  172. procedure TWabtTextAssembler.WriteInstruction(hp: tai);
  173. var
  174. cpu : taicpu;
  175. i : integer;
  176. begin
  177. //writer.AsmWriteLn('instr');
  178. cpu := taicpu(hp);
  179. writer.AsmWrite(#9);
  180. writer.AsmWrite(wasm_op2str[cpu.opcode] );
  181. if (cpu.opcode = a_if) then
  182. writer.AsmWrite(' (result i32)'); //todo: this is a hardcode, but shouldn't
  183. cpu := taicpu(hp);
  184. if cpu.ops<>0 then
  185. begin
  186. for i:=0 to cpu.ops-1 do
  187. begin
  188. writer.AsmWrite(#9);
  189. if (cpu.opcode in AsmOp_LoadStore) and (cpu.oper[i]^.typ = top_ref) then
  190. writer.AsmWrite('offset='+tostr( cpu.oper[i]^.ref^.offset))
  191. else
  192. writer.AsmWrite(getopstr(cpu.oper[i]^));
  193. end;
  194. end;
  195. writer.AsmLn;
  196. end;
  197. procedure TWabtTextAssembler.WriteProcDef(pd: tprocdef);
  198. var
  199. i : integer;
  200. begin
  201. if not assigned(tcpuprocdef(pd).exprasmlist) and
  202. not(po_abstractmethod in pd.procoptions) and
  203. (not is_javainterface(pd.struct) or
  204. (pd.proctypeoption in [potype_unitinit,potype_unitfinalize])) then
  205. begin
  206. exit;
  207. end;
  208. writer.AsmWrite('(func ');
  209. writer.AsmWrite( GetWasmName( pd.mangledname ));
  210. //procsym.RealName ));
  211. //writer.AsmWriteln(MethodDefinition(pd));
  212. {if jvmtypeneedssignature(pd) then
  213. begin
  214. writer.AsmWrite('.signature "');
  215. writer.AsmWrite(tcpuprocdef(pd).jvmmangledbasename(true));
  216. writer.AsmWriteln('"');
  217. end;}
  218. writer.AsmLn;
  219. WriteProcParams(tcpuprocdef(pd));
  220. WriteProcResult(tcpuprocdef(pd));
  221. WriteTempAlloc(tcpuprocdef(pd).exprasmlist);
  222. WriteTree(tcpuprocdef(pd).exprasmlist);
  223. writer.AsmWriteln(')');
  224. writer.AsmLn;
  225. end;
  226. procedure TWabtTextAssembler.WriteProcParams(pd: tprocdef);
  227. var
  228. i : integer;
  229. prm : tcpuparavarsym;
  230. begin
  231. if not Assigned(pd) or
  232. not Assigned(pd.paras) or
  233. (pd.paras.Count=0) then
  234. exit;
  235. for i:=0 to pd.paras.Count-1 do
  236. begin
  237. prm := tcpuparavarsym(pd.paras[i]);
  238. writer.AsmWrite(#9'(param'#9);
  239. case prm.getsize of
  240. 1..4: writer.AsmWrite('i32');
  241. 8: writer.AsmWrite('i64');
  242. end;
  243. writer.AsmWrite(')');
  244. writer.AsmLn;
  245. end;
  246. end;
  247. procedure TWabtTextAssembler.WriteProcResult(pd: tprocdef);
  248. begin
  249. if not assigned(pd) or
  250. not Assigned(pd.returndef) or
  251. (pd.returndef.size = 0)
  252. then exit;
  253. writer.AsmWrite(#9'(result'#9);
  254. case pd.returndef.size of
  255. 1..4: writer.AsmWrite('i32');
  256. 8: writer.AsmWrite('i64');
  257. end;
  258. writer.AsmWrite(')');
  259. writer.AsmLn;
  260. end;
  261. procedure TWabtTextAssembler.WriteTree(p: TAsmList);
  262. var
  263. ch : char;
  264. hp : tai;
  265. hp1 : tailineinfo;
  266. s : ansistring;
  267. i,pos : longint;
  268. InlineLevel : longint;
  269. do_line : boolean;
  270. begin
  271. if not assigned(p) then
  272. exit;
  273. InlineLevel:=0;
  274. { lineinfo is only needed for al_procedures (PFV) }
  275. do_line:=(cs_asm_source in current_settings.globalswitches);
  276. hp:=tai(p.first);
  277. while assigned(hp) do
  278. begin
  279. prefetch(pointer(hp.next)^);
  280. if not(hp.typ in SkipLineInfo) then
  281. begin
  282. hp1 := hp as tailineinfo;
  283. current_filepos:=hp1.fileinfo;
  284. { no line info for inlined code }
  285. if do_line and (inlinelevel=0) then
  286. begin
  287. { load infile }
  288. if lastfileinfo.fileindex<>hp1.fileinfo.fileindex then
  289. begin
  290. infile:=current_module.sourcefiles.get_file(hp1.fileinfo.fileindex);
  291. if assigned(infile) then
  292. begin
  293. { open only if needed !! }
  294. if (cs_asm_source in current_settings.globalswitches) then
  295. infile.open;
  296. end;
  297. { avoid unnecessary reopens of the same file !! }
  298. lastfileinfo.fileindex:=hp1.fileinfo.fileindex;
  299. { be sure to change line !! }
  300. lastfileinfo.line:=-1;
  301. end;
  302. { write source }
  303. if (cs_asm_source in current_settings.globalswitches) and
  304. assigned(infile) then
  305. begin
  306. if (infile<>lastinfile) then
  307. begin
  308. writer.AsmWriteLn(asminfo^.comment+'['+infile.name+']');
  309. if assigned(lastinfile) then
  310. lastinfile.close;
  311. end;
  312. if (hp1.fileinfo.line<>lastfileinfo.line) and
  313. ((hp1.fileinfo.line<infile.maxlinebuf) or (InlineLevel>0)) then
  314. begin
  315. if (hp1.fileinfo.line<>0) and
  316. ((infile.linebuf^[hp1.fileinfo.line]>=0) or (InlineLevel>0)) then
  317. writer.AsmWriteLn(asminfo^.comment+'['+tostr(hp1.fileinfo.line)+'] '+
  318. fixline(infile.GetLineStr(hp1.fileinfo.line)));
  319. { set it to a negative value !
  320. to make that is has been read already !! PM }
  321. if (infile.linebuf^[hp1.fileinfo.line]>=0) then
  322. infile.linebuf^[hp1.fileinfo.line]:=-infile.linebuf^[hp1.fileinfo.line]-1;
  323. end;
  324. end;
  325. lastfileinfo:=hp1.fileinfo;
  326. lastinfile:=infile;
  327. end;
  328. end;
  329. case hp.typ of
  330. ait_comment :
  331. Begin
  332. writer.AsmWrite(asminfo^.comment);
  333. writer.AsmWritePChar(tai_comment(hp).str);
  334. writer.AsmLn;
  335. End;
  336. ait_regalloc :
  337. begin
  338. if (cs_asm_regalloc in current_settings.globalswitches) then
  339. begin
  340. writer.AsmWrite(#9+asminfo^.comment+'Register ');
  341. repeat
  342. writer.AsmWrite(std_regname(Tai_regalloc(hp).reg));
  343. if (hp.next=nil) or
  344. (tai(hp.next).typ<>ait_regalloc) or
  345. (tai_regalloc(hp.next).ratype<>tai_regalloc(hp).ratype) then
  346. break;
  347. hp:=tai(hp.next);
  348. writer.AsmWrite(',');
  349. until false;
  350. writer.AsmWrite(' ');
  351. writer.AsmWriteLn(regallocstr[tai_regalloc(hp).ratype]);
  352. end;
  353. end;
  354. ait_tempalloc :
  355. begin
  356. if (cs_asm_tempalloc in current_settings.globalswitches) then
  357. begin
  358. {$ifdef EXTDEBUG}
  359. if assigned(tai_tempalloc(hp).problem) then
  360. writer.AsmWriteLn(asminfo^.comment+'Temp '+tostr(tai_tempalloc(hp).temppos)+','+
  361. tostr(tai_tempalloc(hp).tempsize)+' '+tai_tempalloc(hp).problem^)
  362. else
  363. {$endif EXTDEBUG}
  364. end;
  365. end;
  366. ait_align :
  367. begin
  368. end;
  369. ait_section :
  370. begin
  371. end;
  372. ait_datablock :
  373. begin
  374. // internalerror(2010122701);
  375. end;
  376. ait_const:
  377. begin
  378. writer.AsmWriteln('constant');
  379. // internalerror(2010122702);
  380. end;
  381. ait_realconst :
  382. begin
  383. internalerror(2010122703);
  384. end;
  385. ait_string :
  386. begin
  387. pos:=0;
  388. for i:=1 to tai_string(hp).len do
  389. begin
  390. if pos=0 then
  391. begin
  392. writer.AsmWrite(#9'strconst: '#9'"');
  393. pos:=20;
  394. end;
  395. ch:=tai_string(hp).str[i-1];
  396. case ch of
  397. #0, {This can't be done by range, because a bug in FPC}
  398. #1..#31,
  399. #128..#255 : s:='\'+tostr(ord(ch) shr 6)+tostr((ord(ch) and 63) shr 3)+tostr(ord(ch) and 7);
  400. '"' : s:='\"';
  401. '\' : s:='\\';
  402. else
  403. s:=ch;
  404. end;
  405. writer.AsmWrite(s);
  406. inc(pos,length(s));
  407. if (pos>line_length) or (i=tai_string(hp).len) then
  408. begin
  409. writer.AsmWriteLn('"');
  410. pos:=0;
  411. end;
  412. end;
  413. end;
  414. ait_label :
  415. begin
  416. if (tai_label(hp).labsym.is_used) then
  417. begin
  418. writer.AsmWrite(tai_label(hp).labsym.name);
  419. writer.AsmWriteLn(':');
  420. end;
  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. else
  465. internalerror(2010122707);
  466. end;
  467. hp:=tai(hp.next);
  468. end;
  469. end;
  470. procedure TWabtTextAssembler.WriteAsmList;
  471. var
  472. hal : tasmlisttype;
  473. begin
  474. if current_module.is_unit then begin
  475. writer.AsmWriteLn('(module)');
  476. exit;
  477. end;
  478. writer.MarkEmpty;
  479. writer.AsmWriteLn('(module ');
  480. writer.AsmWriteLn('(memory 32768) ;; todo: this should be imported or based on the directives ');
  481. { print all global variables }
  482. //current_asmdata.AsmSymbolDict
  483. WriteSymtableVarSyms(current_module.globalsymtable);
  484. WriteSymtableVarSyms(current_module.localsymtable);
  485. //writer.AsmLn;
  486. { print all global procedures/functions }
  487. WriteSymtableProcdefs(current_module.globalsymtable);
  488. WriteSymtableProcdefs(current_module.localsymtable);
  489. WriteExports(current_asmdata.asmlists[al_exports]);
  490. //WriteSymtableStructDefs(current_module.globalsymtable);
  491. //WriteSymtableStructDefs(current_module.localsymtable);
  492. //writer.decorator.LinePrefix := '';
  493. writer.AsmWriteLn(')');
  494. writer.AsmLn;
  495. end;
  496. constructor TWabtTextAssembler.CreateWithWriter(info: pasminfo;
  497. wr: TExternalAssemblerOutputFile; freewriter, smart: boolean);
  498. begin
  499. inherited CreateWithWriter(info, wr, freewriter, smart);
  500. end;
  501. procedure TWabtTextAssembler.WriteSymtableProcdefs(st: TSymtable);
  502. var
  503. i : longint;
  504. def : tdef;
  505. begin
  506. if not assigned(st) then
  507. exit;
  508. for i:=0 to st.DefList.Count-1 do
  509. begin
  510. def:=tdef(st.DefList[i]);
  511. case def.typ of
  512. procdef :
  513. begin
  514. { methods are also in the static/globalsymtable of the unit
  515. -> make sure they are only written for the objectdefs that
  516. own them }
  517. if (not(st.symtabletype in [staticsymtable,globalsymtable]) or
  518. (def.owner=st)) and
  519. not(df_generic in def.defoptions) then
  520. begin
  521. WriteProcDef(tprocdef(def));
  522. if assigned(tprocdef(def).localst) then
  523. WriteSymtableProcdefs(tprocdef(def).localst);
  524. end;
  525. end;
  526. else
  527. ;
  528. end;
  529. end;
  530. end;
  531. procedure TWabtTextAssembler.WriteSymtableVarSyms(st: TSymtable);
  532. var
  533. i : integer;
  534. sym : tsym;
  535. sz : integer;
  536. begin
  537. if not assigned(st) then
  538. exit;
  539. sz := 0;
  540. for i:=0 to st.SymList.Count-1 do
  541. begin
  542. sym:=tsym(st.SymList[i]);
  543. case sym.typ of
  544. staticvarsym:
  545. begin
  546. //WriteFieldSym(tabstractvarsym(sym));
  547. //if (sym.typ=staticvarsym) and
  548. // assigned(tstaticvarsym(sym).defaultconstsym) then
  549. // WriteFieldSym(tabstractvarsym(tstaticvarsym(sym).defaultconstsym));
  550. writer.AsmWrite(#9);
  551. writer.AsmWrite('(global $');
  552. writer.AsmWrite(tcpustaticvarsym(sym).mangledname);
  553. writer.AsmWrite(' i32 (i32.const ');
  554. writer.AsmWrite( tostr(sz));
  555. writer.AsmWrite(')');
  556. writer.AsmWrite(') ');
  557. writer.AsmWriteLn(';; static or field');
  558. inc(sz, tcpustaticvarsym(sym).getsize);
  559. end;
  560. fieldvarsym:
  561. begin
  562. writer.AsmWriteLn(';; field');
  563. end;
  564. constsym:
  565. begin
  566. //if (sym.typ=staticvarsym) and
  567. // assigned(tstaticvarsym(sym).defaultconstsym) then
  568. // WriteFieldSym(tabstractvarsym(tstaticvarsym(sym).defaultconstsym));
  569. //{ multiple procedures can have constants with the same name }
  570. //if not assigned(sym.owner.defowner) or
  571. // (tdef(sym.owner.defowner).typ<>procdef) then
  572. // WriteConstSym(tconstsym(sym));
  573. writer.AsmWriteLn(';; constant');
  574. end;
  575. {procsym:
  576. begin
  577. for j:=0 to tprocsym(sym).procdeflist.count-1 do
  578. if not(df_generic in tprocdef(tprocsym(sym).procdeflist[j]).defoptions) then
  579. WriteSymtableVarSyms(tprocdef(tprocsym(sym).procdeflist[j]).localst);
  580. end;}
  581. else
  582. ;
  583. end;
  584. end;
  585. end;
  586. procedure TWabtTextAssembler.WriteTempAlloc(p: TAsmList);
  587. var
  588. hp: tai;
  589. tmp: array of tai_tempalloc;
  590. mx : integer;
  591. i : integer;
  592. begin
  593. if not assigned(p) then
  594. exit;
  595. mx := -1;
  596. hp:=tai(p.first);
  597. while assigned(hp) do
  598. begin
  599. //prefetch(pointer(hp.next)^);
  600. if (hp.typ = ait_tempalloc) and
  601. tai_tempalloc(hp).allocation and
  602. (tai_tempalloc(hp).temppos >= mx) then
  603. mx := tai_tempalloc(hp).temppos+1;
  604. hp:=tai(hp.next);
  605. end;
  606. if (mx <= 0) then exit; // no temp allocations used
  607. SetLength(tmp, mx);
  608. hp:=tai(p.first);
  609. while assigned(hp) do
  610. begin
  611. //prefetch(pointer(hp.next)^);
  612. if (hp.typ = ait_tempalloc) and
  613. (tai_tempalloc(hp).allocation) and
  614. (tmp[ tai_tempalloc(hp).temppos ] = nil) then
  615. begin
  616. tmp[ tai_tempalloc(hp).temppos ] := tai_tempalloc(hp);
  617. dec(mx);
  618. if mx = 0 then break;
  619. end;
  620. hp:=tai(hp.next);
  621. end;
  622. for i:=0 to length(tmp)-1 do
  623. begin
  624. if tmp[i] = nil then continue;
  625. writer.AsmWrite(#9'(local'#9);
  626. if tmp[i].tempsize<=4 then writer.AsmWrite('i32')
  627. else if tmp[i].tempsize = 8 then writer.AsmWrite('i64');
  628. writer.AsmWrite(')');
  629. writer.AsmWrite(#9+asminfo^.comment+'Temp '+tostr(tmp[i].temppos)+','+
  630. tostr(tmp[i].tempsize)+' '+tempallocstr[tmp[i].allocation]);
  631. writer.AsmLn;
  632. end;
  633. end;
  634. procedure TWabtTextAssembler.WriteExports(p: TAsmList);
  635. var
  636. hp: tai;
  637. x: tai_impexp;
  638. begin
  639. if not Assigned(p) then Exit;
  640. hp:=tai(p.First);
  641. while Assigned(hp) do begin
  642. case hp.typ of
  643. ait_importexport:
  644. begin
  645. x:=tai_impexp(hp);
  646. writer.AsmWrite('(export "');
  647. writer.AsmWrite(x.extname);
  648. writer.AsmWrite('" (');
  649. case x.symstype of
  650. ie_Func: writer.AsmWrite('func');
  651. end;
  652. writer.AsmWrite(' ');
  653. writer.AsmWrite(GetWasmName(x.intname));
  654. writer.AsmWrite('))');
  655. writer.AsmLn;
  656. end;
  657. end;
  658. hp := tai_impexp(hp.Next);
  659. end;
  660. end;
  661. { TWatInstrWriter }
  662. constructor TWatInstrWriter.create(_owner: TWabtTextAssembler);
  663. begin
  664. inherited create;
  665. owner := _owner;
  666. end;
  667. procedure TWatInstrWriter.WriteInstruction(hp: tai);
  668. begin
  669. end;
  670. const
  671. as_wasm_wabt_info : tasminfo =
  672. (
  673. id : as_wasm_wabt;
  674. idtxt : 'Wabt';
  675. asmbin : 'wat2wasm';
  676. asmcmd : '$ASM $EXTRAOPT';
  677. supported_targets : [system_wasm_wasm32];
  678. flags : [];
  679. labelprefix : 'L';
  680. comment : ';; ';
  681. dollarsign : '$';
  682. );
  683. initialization
  684. RegisterAssembler(as_wasm_wabt_info, TWabtTextAssembler);
  685. end.