agwat.pas 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  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. 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('(memory 32768) ;; todo: this should be imported or based on the directives ');
  487. { print all global variables }
  488. //current_asmdata.AsmSymbolDict
  489. WriteSymtableVarSyms(current_module.globalsymtable);
  490. WriteSymtableVarSyms(current_module.localsymtable);
  491. //writer.AsmLn;
  492. { print all global procedures/functions }
  493. WriteSymtableProcdefs(current_module.globalsymtable);
  494. WriteSymtableProcdefs(current_module.localsymtable);
  495. WriteExports(current_asmdata.asmlists[al_exports]);
  496. //WriteSymtableStructDefs(current_module.globalsymtable);
  497. //WriteSymtableStructDefs(current_module.localsymtable);
  498. //writer.decorator.LinePrefix := '';
  499. writer.AsmWriteLn(')');
  500. writer.AsmLn;
  501. end;
  502. constructor TWabtTextAssembler.CreateWithWriter(info: pasminfo;
  503. wr: TExternalAssemblerOutputFile; freewriter, smart: boolean);
  504. begin
  505. inherited CreateWithWriter(info, wr, freewriter, smart);
  506. end;
  507. procedure TWabtTextAssembler.WriteSymtableProcdefs(st: TSymtable);
  508. var
  509. i : longint;
  510. def : tdef;
  511. begin
  512. if not assigned(st) then
  513. exit;
  514. for i:=0 to st.DefList.Count-1 do
  515. begin
  516. def:=tdef(st.DefList[i]);
  517. case def.typ of
  518. procdef :
  519. begin
  520. { methods are also in the static/globalsymtable of the unit
  521. -> make sure they are only written for the objectdefs that
  522. own them }
  523. if (not(st.symtabletype in [staticsymtable,globalsymtable]) or
  524. (def.owner=st)) and
  525. not(df_generic in def.defoptions) then
  526. begin
  527. WriteProcDef(tprocdef(def));
  528. if assigned(tprocdef(def).localst) then
  529. WriteSymtableProcdefs(tprocdef(def).localst);
  530. end;
  531. end;
  532. else
  533. ;
  534. end;
  535. end;
  536. end;
  537. procedure TWabtTextAssembler.WriteSymtableVarSyms(st: TSymtable);
  538. var
  539. i : integer;
  540. sym : tsym;
  541. sz : integer;
  542. begin
  543. if not assigned(st) then
  544. exit;
  545. sz := 0;
  546. for i:=0 to st.SymList.Count-1 do
  547. begin
  548. sym:=tsym(st.SymList[i]);
  549. case sym.typ of
  550. staticvarsym:
  551. begin
  552. //WriteFieldSym(tabstractvarsym(sym));
  553. //if (sym.typ=staticvarsym) and
  554. // assigned(tstaticvarsym(sym).defaultconstsym) then
  555. // WriteFieldSym(tabstractvarsym(tstaticvarsym(sym).defaultconstsym));
  556. writer.AsmWrite(#9);
  557. writer.AsmWrite('(global $');
  558. writer.AsmWrite(tcpustaticvarsym(sym).mangledname);
  559. writer.AsmWrite(' i32 (i32.const ');
  560. writer.AsmWrite( tostr(sz));
  561. writer.AsmWrite(')');
  562. writer.AsmWrite(') ');
  563. writer.AsmWriteLn(';; static or field');
  564. inc(sz, tcpustaticvarsym(sym).getsize);
  565. end;
  566. fieldvarsym:
  567. begin
  568. writer.AsmWriteLn(';; field');
  569. end;
  570. constsym:
  571. begin
  572. //if (sym.typ=staticvarsym) and
  573. // assigned(tstaticvarsym(sym).defaultconstsym) then
  574. // WriteFieldSym(tabstractvarsym(tstaticvarsym(sym).defaultconstsym));
  575. //{ multiple procedures can have constants with the same name }
  576. //if not assigned(sym.owner.defowner) or
  577. // (tdef(sym.owner.defowner).typ<>procdef) then
  578. // WriteConstSym(tconstsym(sym));
  579. writer.AsmWriteLn(';; constant');
  580. end;
  581. {procsym:
  582. begin
  583. for j:=0 to tprocsym(sym).procdeflist.count-1 do
  584. if not(df_generic in tprocdef(tprocsym(sym).procdeflist[j]).defoptions) then
  585. WriteSymtableVarSyms(tprocdef(tprocsym(sym).procdeflist[j]).localst);
  586. end;}
  587. else
  588. ;
  589. end;
  590. end;
  591. end;
  592. procedure TWabtTextAssembler.WriteTempAlloc(p: TAsmList);
  593. var
  594. hp: tai;
  595. tmp: array of tai_tempalloc;
  596. mx : integer;
  597. i : integer;
  598. begin
  599. if not assigned(p) then
  600. exit;
  601. mx := -1;
  602. hp:=tai(p.first);
  603. while assigned(hp) do
  604. begin
  605. //prefetch(pointer(hp.next)^);
  606. if (hp.typ = ait_tempalloc) and
  607. tai_tempalloc(hp).allocation and
  608. (tai_tempalloc(hp).temppos >= mx) then
  609. mx := tai_tempalloc(hp).temppos+1;
  610. hp:=tai(hp.next);
  611. end;
  612. if (mx <= 0) then exit; // no temp allocations used
  613. SetLength(tmp, mx);
  614. hp:=tai(p.first);
  615. while assigned(hp) do
  616. begin
  617. //prefetch(pointer(hp.next)^);
  618. if (hp.typ = ait_tempalloc) and
  619. (tai_tempalloc(hp).allocation) and
  620. (tmp[ tai_tempalloc(hp).temppos ] = nil) then
  621. begin
  622. tmp[ tai_tempalloc(hp).temppos ] := tai_tempalloc(hp);
  623. dec(mx);
  624. if mx = 0 then break;
  625. end;
  626. hp:=tai(hp.next);
  627. end;
  628. for i:=0 to length(tmp)-1 do
  629. begin
  630. if tmp[i] = nil then continue;
  631. writer.AsmWrite(#9'(local'#9);
  632. if tmp[i].tempsize<=4 then writer.AsmWrite('i32')
  633. else if tmp[i].tempsize = 8 then writer.AsmWrite('i64');
  634. writer.AsmWrite(')');
  635. writer.AsmWrite(#9+asminfo^.comment+'Temp '+tostr(tmp[i].temppos)+','+
  636. tostr(tmp[i].tempsize)+' '+tempallocstr[tmp[i].allocation]);
  637. writer.AsmLn;
  638. end;
  639. end;
  640. procedure TWabtTextAssembler.WriteExports(p: TAsmList);
  641. var
  642. hp: tai;
  643. x: tai_impexp;
  644. begin
  645. if not Assigned(p) then Exit;
  646. hp:=tai(p.First);
  647. while Assigned(hp) do begin
  648. case hp.typ of
  649. ait_importexport:
  650. begin
  651. x:=tai_impexp(hp);
  652. writer.AsmWrite('(export "');
  653. writer.AsmWrite(x.extname);
  654. writer.AsmWrite('" (');
  655. case x.symstype of
  656. ie_Func: writer.AsmWrite('func');
  657. end;
  658. writer.AsmWrite(' ');
  659. writer.AsmWrite(GetWasmName(x.intname));
  660. writer.AsmWrite('))');
  661. writer.AsmLn;
  662. end;
  663. end;
  664. hp := tai_impexp(hp.Next);
  665. end;
  666. end;
  667. { TWatInstrWriter }
  668. constructor TWatInstrWriter.create(_owner: TWabtTextAssembler);
  669. begin
  670. inherited create;
  671. owner := _owner;
  672. end;
  673. procedure TWatInstrWriter.WriteInstruction(hp: tai);
  674. begin
  675. end;
  676. const
  677. as_wasm_wabt_info : tasminfo =
  678. (
  679. id : as_wasm_wabt;
  680. idtxt : 'Wabt';
  681. asmbin : 'wat2wasm';
  682. asmcmd : '$ASM $EXTRAOPT';
  683. supported_targets : [system_wasm_wasm32];
  684. flags : [];
  685. labelprefix : 'L';
  686. comment : ';; ';
  687. dollarsign : '$';
  688. );
  689. initialization
  690. RegisterAssembler(as_wasm_wabt_info, TWabtTextAssembler);
  691. end.