agwat.pas 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  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;
  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. ((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. const
  271. WasmBasicTypeStr : array [TWasmBasicType] of string = ('i32','i64','f32','f64');
  272. begin
  273. if not assigned(p) then
  274. exit;
  275. InlineLevel:=0;
  276. { lineinfo is only needed for al_procedures (PFV) }
  277. do_line:=(cs_asm_source in current_settings.globalswitches);
  278. hp:=tai(p.first);
  279. while assigned(hp) do
  280. begin
  281. prefetch(pointer(hp.next)^);
  282. if not(hp.typ in SkipLineInfo) then
  283. begin
  284. hp1 := hp as tailineinfo;
  285. current_filepos:=hp1.fileinfo;
  286. { no line info for inlined code }
  287. if do_line and (inlinelevel=0) then
  288. begin
  289. { load infile }
  290. if lastfileinfo.fileindex<>hp1.fileinfo.fileindex then
  291. begin
  292. infile:=current_module.sourcefiles.get_file(hp1.fileinfo.fileindex);
  293. if assigned(infile) then
  294. begin
  295. { open only if needed !! }
  296. if (cs_asm_source in current_settings.globalswitches) then
  297. infile.open;
  298. end;
  299. { avoid unnecessary reopens of the same file !! }
  300. lastfileinfo.fileindex:=hp1.fileinfo.fileindex;
  301. { be sure to change line !! }
  302. lastfileinfo.line:=-1;
  303. end;
  304. { write source }
  305. if (cs_asm_source in current_settings.globalswitches) and
  306. assigned(infile) then
  307. begin
  308. if (infile<>lastinfile) then
  309. begin
  310. writer.AsmWriteLn(asminfo^.comment+'['+infile.name+']');
  311. if assigned(lastinfile) then
  312. lastinfile.close;
  313. end;
  314. if (hp1.fileinfo.line<>lastfileinfo.line) and
  315. ((hp1.fileinfo.line<infile.maxlinebuf) or (InlineLevel>0)) then
  316. begin
  317. if (hp1.fileinfo.line<>0) and
  318. ((infile.linebuf^[hp1.fileinfo.line]>=0) or (InlineLevel>0)) then
  319. writer.AsmWriteLn(asminfo^.comment+'['+tostr(hp1.fileinfo.line)+'] '+
  320. fixline(infile.GetLineStr(hp1.fileinfo.line)));
  321. { set it to a negative value !
  322. to make that is has been read already !! PM }
  323. if (infile.linebuf^[hp1.fileinfo.line]>=0) then
  324. infile.linebuf^[hp1.fileinfo.line]:=-infile.linebuf^[hp1.fileinfo.line]-1;
  325. end;
  326. end;
  327. lastfileinfo:=hp1.fileinfo;
  328. lastinfile:=infile;
  329. end;
  330. end;
  331. case hp.typ of
  332. ait_comment :
  333. Begin
  334. writer.AsmWrite(asminfo^.comment);
  335. writer.AsmWritePChar(tai_comment(hp).str);
  336. writer.AsmLn;
  337. End;
  338. ait_regalloc :
  339. begin
  340. if (cs_asm_regalloc in current_settings.globalswitches) then
  341. begin
  342. writer.AsmWrite(#9+asminfo^.comment+'Register ');
  343. repeat
  344. writer.AsmWrite(std_regname(Tai_regalloc(hp).reg));
  345. if (hp.next=nil) or
  346. (tai(hp.next).typ<>ait_regalloc) or
  347. (tai_regalloc(hp.next).ratype<>tai_regalloc(hp).ratype) then
  348. break;
  349. hp:=tai(hp.next);
  350. writer.AsmWrite(',');
  351. until false;
  352. writer.AsmWrite(' ');
  353. writer.AsmWriteLn(regallocstr[tai_regalloc(hp).ratype]);
  354. end;
  355. end;
  356. ait_tempalloc :
  357. begin
  358. if (cs_asm_tempalloc in current_settings.globalswitches) then
  359. begin
  360. {$ifdef EXTDEBUG}
  361. if assigned(tai_tempalloc(hp).problem) then
  362. writer.AsmWriteLn(asminfo^.comment+'Temp '+tostr(tai_tempalloc(hp).temppos)+','+
  363. tostr(tai_tempalloc(hp).tempsize)+' '+tai_tempalloc(hp).problem^)
  364. else
  365. {$endif EXTDEBUG}
  366. end;
  367. end;
  368. ait_align :
  369. begin
  370. end;
  371. ait_section :
  372. begin
  373. end;
  374. ait_datablock :
  375. begin
  376. // internalerror(2010122701);
  377. end;
  378. ait_const:
  379. begin
  380. writer.AsmWriteln('constant');
  381. // internalerror(2010122702);
  382. end;
  383. ait_realconst :
  384. begin
  385. internalerror(2010122703);
  386. end;
  387. ait_string :
  388. begin
  389. pos:=0;
  390. for i:=1 to tai_string(hp).len do
  391. begin
  392. if pos=0 then
  393. begin
  394. writer.AsmWrite(#9'strconst: '#9'"');
  395. pos:=20;
  396. end;
  397. ch:=tai_string(hp).str[i-1];
  398. case ch of
  399. #0, {This can't be done by range, because a bug in FPC}
  400. #1..#31,
  401. #128..#255 : s:='\'+tostr(ord(ch) shr 6)+tostr((ord(ch) and 63) shr 3)+tostr(ord(ch) and 7);
  402. '"' : s:='\"';
  403. '\' : s:='\\';
  404. else
  405. s:=ch;
  406. end;
  407. writer.AsmWrite(s);
  408. inc(pos,length(s));
  409. if (pos>line_length) or (i=tai_string(hp).len) then
  410. begin
  411. writer.AsmWriteLn('"');
  412. pos:=0;
  413. end;
  414. end;
  415. end;
  416. ait_label :
  417. begin
  418. // don't write any labels. Wasm don't support it
  419. // labels are only allowed with the respective block structures
  420. end;
  421. ait_symbol :
  422. begin
  423. if (tai_symbol(hp).sym.typ = AT_FUNCTION) then
  424. begin
  425. end
  426. else
  427. begin
  428. writer.AsmWrite('data symbol: ');
  429. writer.AsmWriteln(tai_symbol(hp).sym.name);
  430. // internalerror(2010122706);
  431. end;
  432. end;
  433. ait_symbol_end :
  434. begin
  435. end;
  436. ait_instruction :
  437. begin
  438. WriteInstruction(hp);
  439. end;
  440. ait_force_line,
  441. ait_function_name : ;
  442. ait_cutobject :
  443. begin
  444. end;
  445. ait_marker :
  446. if tai_marker(hp).kind=mark_NoLineInfoStart then
  447. inc(InlineLevel)
  448. else if tai_marker(hp).kind=mark_NoLineInfoEnd then
  449. dec(InlineLevel);
  450. ait_directive :
  451. begin
  452. { the CPU directive is probably not supported by the JVM assembler,
  453. so it's commented out }
  454. //todo:
  455. writer.AsmWrite(asminfo^.comment);
  456. if tai_directive(hp).directive=asd_cpu then
  457. writer.AsmWrite(asminfo^.comment);
  458. writer.AsmWrite('.'+directivestr[tai_directive(hp).directive]+' ');
  459. if tai_directive(hp).name<>'' then
  460. writer.AsmWrite(tai_directive(hp).name);
  461. writer.AsmLn;
  462. end;
  463. ait_local :
  464. begin
  465. writer.AsmWrite(#9'(local ');
  466. writer.AsmWrite( WasmBasicTypeStr[ tai_local(hp).bastyp ] );
  467. writer.AsmWrite(')');
  468. writer.AsmLn;
  469. end;
  470. else
  471. internalerror(2010122707);
  472. end;
  473. hp:=tai(hp.next);
  474. end;
  475. end;
  476. procedure TWabtTextAssembler.WriteAsmList;
  477. var
  478. hal : tasmlisttype;
  479. begin
  480. if current_module.is_unit then begin
  481. writer.AsmWriteLn('(module)');
  482. exit;
  483. end;
  484. writer.MarkEmpty;
  485. writer.AsmWriteLn('(module ');
  486. writer.AsmWriteLn('(import "env" "memory" (memory 0)) ;;');
  487. WriteImports;
  488. { print all global variables }
  489. //current_asmdata.AsmSymbolDict
  490. WriteSymtableVarSyms(current_module.globalsymtable);
  491. WriteSymtableVarSyms(current_module.localsymtable);
  492. //writer.AsmLn;
  493. { print all global procedures/functions }
  494. WriteSymtableProcdefs(current_module.globalsymtable);
  495. WriteSymtableProcdefs(current_module.localsymtable);
  496. WriteExports(current_asmdata.asmlists[al_exports]);
  497. //WriteSymtableStructDefs(current_module.globalsymtable);
  498. //WriteSymtableStructDefs(current_module.localsymtable);
  499. //writer.decorator.LinePrefix := '';
  500. writer.AsmWriteLn(')');
  501. writer.AsmLn;
  502. end;
  503. constructor TWabtTextAssembler.CreateWithWriter(info: pasminfo;
  504. wr: TExternalAssemblerOutputFile; freewriter, smart: boolean);
  505. begin
  506. inherited CreateWithWriter(info, wr, freewriter, smart);
  507. end;
  508. procedure TWabtTextAssembler.WriteSymtableProcdefs(st: TSymtable);
  509. var
  510. i : longint;
  511. def : tdef;
  512. begin
  513. if not assigned(st) then
  514. exit;
  515. for i:=0 to st.DefList.Count-1 do
  516. begin
  517. def:=tdef(st.DefList[i]);
  518. case def.typ of
  519. procdef :
  520. begin
  521. { methods are also in the static/globalsymtable of the unit
  522. -> make sure they are only written for the objectdefs that
  523. own them }
  524. if (not(st.symtabletype in [staticsymtable,globalsymtable]) or
  525. (def.owner=st)) and
  526. not(df_generic in def.defoptions) and
  527. not (po_external in tprocdef(def).procoptions)
  528. then
  529. begin
  530. WriteProcDef(tprocdef(def));
  531. if assigned(tprocdef(def).localst) then
  532. WriteSymtableProcdefs(tprocdef(def).localst);
  533. end;
  534. end;
  535. else
  536. ;
  537. end;
  538. end;
  539. end;
  540. procedure TWabtTextAssembler.WriteSymtableVarSyms(st: TSymtable);
  541. var
  542. i : integer;
  543. sym : tsym;
  544. sz : integer;
  545. begin
  546. if not assigned(st) then
  547. exit;
  548. sz := 0;
  549. for i:=0 to st.SymList.Count-1 do
  550. begin
  551. sym:=tsym(st.SymList[i]);
  552. case sym.typ of
  553. staticvarsym:
  554. begin
  555. //WriteFieldSym(tabstractvarsym(sym));
  556. //if (sym.typ=staticvarsym) and
  557. // assigned(tstaticvarsym(sym).defaultconstsym) then
  558. // WriteFieldSym(tabstractvarsym(tstaticvarsym(sym).defaultconstsym));
  559. writer.AsmWrite(#9);
  560. writer.AsmWrite('(global $');
  561. writer.AsmWrite(tcpustaticvarsym(sym).mangledname);
  562. writer.AsmWrite(' (mut i32) (i32.const ');
  563. writer.AsmWrite( tostr(sz));
  564. writer.AsmWrite(')');
  565. writer.AsmWrite(') ');
  566. writer.AsmWriteLn(';; static or field');
  567. inc(sz, tcpustaticvarsym(sym).getsize);
  568. end;
  569. fieldvarsym:
  570. begin
  571. writer.AsmWriteLn(';; field');
  572. end;
  573. constsym:
  574. begin
  575. //if (sym.typ=staticvarsym) and
  576. // assigned(tstaticvarsym(sym).defaultconstsym) then
  577. // WriteFieldSym(tabstractvarsym(tstaticvarsym(sym).defaultconstsym));
  578. //{ multiple procedures can have constants with the same name }
  579. //if not assigned(sym.owner.defowner) or
  580. // (tdef(sym.owner.defowner).typ<>procdef) then
  581. // WriteConstSym(tconstsym(sym));
  582. writer.AsmWriteLn(';; constant');
  583. end;
  584. {procsym:
  585. begin
  586. for j:=0 to tprocsym(sym).procdeflist.count-1 do
  587. if not(df_generic in tprocdef(tprocsym(sym).procdeflist[j]).defoptions) then
  588. WriteSymtableVarSyms(tprocdef(tprocsym(sym).procdeflist[j]).localst);
  589. end;}
  590. else
  591. ;
  592. end;
  593. end;
  594. end;
  595. procedure TWabtTextAssembler.WriteTempAlloc(p: TAsmList);
  596. var
  597. hp: tai;
  598. tmp: array of tai_tempalloc;
  599. mx : integer;
  600. i : integer;
  601. begin
  602. if not assigned(p) then
  603. exit;
  604. mx := -1;
  605. hp:=tai(p.first);
  606. while assigned(hp) do
  607. begin
  608. //prefetch(pointer(hp.next)^);
  609. if (hp.typ = ait_tempalloc) and
  610. tai_tempalloc(hp).allocation and
  611. (tai_tempalloc(hp).temppos >= mx) then
  612. mx := tai_tempalloc(hp).temppos+1;
  613. hp:=tai(hp.next);
  614. end;
  615. if (mx <= 0) then exit; // no temp allocations used
  616. SetLength(tmp, mx);
  617. hp:=tai(p.first);
  618. while assigned(hp) do
  619. begin
  620. //prefetch(pointer(hp.next)^);
  621. if (hp.typ = ait_tempalloc) and
  622. (tai_tempalloc(hp).allocation) and
  623. (tmp[ tai_tempalloc(hp).temppos ] = nil) then
  624. begin
  625. tmp[ tai_tempalloc(hp).temppos ] := tai_tempalloc(hp);
  626. dec(mx);
  627. if mx = 0 then break;
  628. end;
  629. hp:=tai(hp.next);
  630. end;
  631. for i:=0 to length(tmp)-1 do
  632. begin
  633. if tmp[i] = nil then continue;
  634. writer.AsmWrite(#9'(local'#9);
  635. if tmp[i].tempsize<=4 then writer.AsmWrite('i32')
  636. else if tmp[i].tempsize = 8 then writer.AsmWrite('i64');
  637. writer.AsmWrite(')');
  638. writer.AsmWrite(#9+asminfo^.comment+'Temp '+tostr(tmp[i].temppos)+','+
  639. tostr(tmp[i].tempsize)+' '+tempallocstr[tmp[i].allocation]);
  640. writer.AsmLn;
  641. end;
  642. end;
  643. procedure TWabtTextAssembler.WriteExports(p: TAsmList);
  644. var
  645. hp: tai;
  646. x: tai_impexp;
  647. begin
  648. if not Assigned(p) then Exit;
  649. hp:=tai(p.First);
  650. while Assigned(hp) do begin
  651. case hp.typ of
  652. ait_importexport:
  653. begin
  654. x:=tai_impexp(hp);
  655. writer.AsmWrite('(export "');
  656. writer.AsmWrite(x.extname);
  657. writer.AsmWrite('" (');
  658. case x.symstype of
  659. ie_Func: writer.AsmWrite('func');
  660. end;
  661. writer.AsmWrite(' ');
  662. writer.AsmWrite(GetWasmName(x.intname));
  663. writer.AsmWrite('))');
  664. writer.AsmLn;
  665. end;
  666. end;
  667. hp := tai_impexp(hp.Next);
  668. end;
  669. end;
  670. procedure TWabtTextAssembler.WriteImports;
  671. var
  672. i : integer;
  673. proc : tprocdef;
  674. begin
  675. for i:=0 to current_module.deflist.Count-1 do begin
  676. //writeln('>def: ', tdef(current_module.deflist[i]).typ);
  677. if tdef(current_module.deflist[i]).typ = procdef then begin
  678. proc := tprocdef(current_module.deflist[i]);
  679. if (po_external in proc.procoptions) and assigned(proc.import_dll) then begin
  680. writer.AsmWrite('(import "');
  681. writer.AsmWrite(proc.import_dll^);
  682. writer.AsmWrite('" "');
  683. writer.AsmWrite(proc.import_name^);
  684. writer.AsmWrite('" ');
  685. WriteProcDef(proc);
  686. writer.AsmWriteLn(')');
  687. end;
  688. end;
  689. end;
  690. end;
  691. { TWatInstrWriter }
  692. constructor TWatInstrWriter.create(_owner: TWabtTextAssembler);
  693. begin
  694. inherited create;
  695. owner := _owner;
  696. end;
  697. procedure TWatInstrWriter.WriteInstruction(hp: tai);
  698. begin
  699. end;
  700. const
  701. as_wasm_wabt_info : tasminfo =
  702. (
  703. id : as_wasm_wabt;
  704. idtxt : 'Wabt';
  705. asmbin : 'wat2wasm';
  706. asmcmd : '$EXTRAOPT $ASM';
  707. supported_targets : [system_wasm_wasm32];
  708. flags : [];
  709. labelprefix : 'L';
  710. comment : ';; ';
  711. dollarsign : '$';
  712. );
  713. initialization
  714. RegisterAssembler(as_wasm_wabt_info, TWabtTextAssembler);
  715. end.