agllvmmc.pas 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. {
  2. Copyright (c) 1998-2020 by the Free Pascal team
  3. This unit implements the llvm-mc ("llvm machine code playground")
  4. assembler writer for WebAssembly
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit agllvmmc;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. systems,cgutils,
  23. globtype,globals,
  24. symbase,symdef,symtype,symconst,symcpu,
  25. aasmbase,aasmtai,aasmdata,aasmcpu,
  26. assemble,aggas;
  27. type
  28. { TLLVMMachineCodePlaygroundAssembler }
  29. TLLVMMachineCodePlaygroundAssembler=class(TGNUassembler)
  30. protected
  31. procedure WriteProcDef(pd: tprocdef);
  32. procedure WriteSymtableProcdefs(st: TSymtable);
  33. function sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;override;
  34. public
  35. constructor CreateWithWriter(info: pasminfo; wr: TExternalAssemblerOutputFile; freewriter, smart: boolean); override;
  36. procedure WriteAsmList;override;
  37. end;
  38. { TWASM32InstrWriter }
  39. TWASM32InstrWriter = class(TCPUInstrWriter)
  40. procedure WriteInstruction(hp : tai);override;
  41. end;
  42. implementation
  43. uses
  44. cutils,
  45. fmodule,finput,
  46. itcpugas,
  47. cpubase,
  48. verbose;
  49. { TLLVMMachineCodePlaygroundAssembler }
  50. procedure TLLVMMachineCodePlaygroundAssembler.WriteProcDef(pd: tprocdef);
  51. begin
  52. if not assigned(tcpuprocdef(pd).exprasmlist) and
  53. not(po_abstractmethod in pd.procoptions) and
  54. (pd.proctypeoption in [potype_unitinit,potype_unitfinalize]) then
  55. exit;
  56. writer.AsmWriteLn(asminfo^.comment+'WriteProcDef('+pd.mangledname+')');
  57. WriteTree(tcpuprocdef(pd).exprasmlist);
  58. writer.AsmWriteLn(asminfo^.comment+'WriteProcDef('+pd.mangledname+') done');
  59. end;
  60. procedure TLLVMMachineCodePlaygroundAssembler.WriteSymtableProcdefs(st: TSymtable);
  61. var
  62. i : longint;
  63. def : tdef;
  64. begin
  65. if not assigned(st) then
  66. exit;
  67. for i:=0 to st.DefList.Count-1 do
  68. begin
  69. def:=tdef(st.DefList[i]);
  70. case def.typ of
  71. procdef :
  72. begin
  73. { methods are also in the static/globalsymtable of the unit
  74. -> make sure they are only written for the objectdefs that
  75. own them }
  76. if (not(st.symtabletype in [staticsymtable,globalsymtable]) or
  77. (def.owner=st)) and
  78. not(df_generic in def.defoptions) then
  79. begin
  80. WriteProcDef(tprocdef(def));
  81. if assigned(tprocdef(def).localst) then
  82. WriteSymtableProcdefs(tprocdef(def).localst);
  83. end;
  84. end;
  85. else
  86. ;
  87. end;
  88. end;
  89. end;
  90. function TLLVMMachineCodePlaygroundAssembler.sectionname(atype: TAsmSectiontype; const aname: string; aorder: TAsmSectionOrder): string;
  91. begin
  92. Result:=inherited sectionname(atype, aname, aorder)+',"",@';
  93. end;
  94. constructor TLLVMMachineCodePlaygroundAssembler.CreateWithWriter(info: pasminfo; wr: TExternalAssemblerOutputFile; freewriter, smart: boolean);
  95. begin
  96. inherited;
  97. InstrWriter:=TWASM32InstrWriter.create(self);
  98. end;
  99. procedure TLLVMMachineCodePlaygroundAssembler.WriteAsmList;
  100. begin
  101. inherited;
  102. { print all global procedures/functions }
  103. WriteSymtableProcdefs(current_module.globalsymtable);
  104. WriteSymtableProcdefs(current_module.localsymtable);
  105. end;
  106. { TWASM32InstrWriter }
  107. procedure TWASM32InstrWriter.WriteInstruction(hp: tai);
  108. function getreferencestring(var ref : treference) : ansistring;
  109. begin
  110. if (ref.index<>NR_NO) then
  111. internalerror(2010122809);
  112. if assigned(ref.symbol) then
  113. begin
  114. // global symbol or field -> full type and name
  115. // ref.base can be <> NR_NO in case an instance field is loaded.
  116. // This register is not part of this instruction, it will have
  117. // been placed on the stack by the previous one.
  118. result:=ref.symbol.name;
  119. end
  120. else
  121. begin
  122. // local symbol -> stack slot, stored in offset
  123. if ref.base<>NR_STACK_POINTER_REG then
  124. internalerror(2010122810);
  125. result:=tostr(ref.offset);
  126. end;
  127. end;
  128. function constsingle(s: single): ansistring;
  129. begin
  130. // wat2wasm is using strtof() internally
  131. str(s, result); //'0x'+hexstr(longint(t32bitarray(s)),8);
  132. end;
  133. function constdouble(d: double): ansistring;
  134. begin
  135. // force interpretation as double (since we write it out as an
  136. // integer, we never have to swap the endianess). We have to
  137. // include the sign separately because of the way Java parses
  138. // hex numbers (0x8000000000000000 is not a valid long)
  139. //result:=hexstr(abs(int64(t64bitarray(d))),16);
  140. //if int64(t64bitarray(d))<0 then
  141. // result:='-'+result;
  142. //result:='0dx'+result;
  143. str(d, result);
  144. end;
  145. function getopstr(const o:toper) : ansistring;
  146. var
  147. d: double;
  148. s: single;
  149. begin
  150. case o.typ of
  151. top_reg:
  152. // should have been translated into a memory location by the
  153. // register allocator)
  154. if (cs_no_regalloc in current_settings.globalswitches) then
  155. getopstr:=std_regname(o.reg)
  156. else
  157. internalerror(2010122803);
  158. top_const:
  159. str(o.val,result);
  160. top_ref:
  161. getopstr:=getreferencestring(o.ref^);
  162. top_single:
  163. begin
  164. result:=constsingle(o.sval);
  165. end;
  166. top_double:
  167. begin
  168. result:=constdouble(o.dval);
  169. end;
  170. {top_string:
  171. begin
  172. result:=constastr(o.pcval,o.pcvallen);
  173. end;
  174. top_wstring:
  175. begin
  176. result:=constwstr(o.pwstrval^.data,getlengthwidestring(o.pwstrval));
  177. end}
  178. else
  179. internalerror(2010122802);
  180. end;
  181. end;
  182. var
  183. cpu : taicpu;
  184. i : integer;
  185. isprm : boolean;
  186. writer: TExternalAssemblerOutputFile;
  187. begin
  188. writer:=owner.writer;
  189. cpu := taicpu(hp);
  190. writer.AsmWrite(#9#9);
  191. writer.AsmWrite(gas_op2str[cpu.opcode]);
  192. if (cpu.opcode = a_call_indirect) then begin
  193. // special wat2wasm syntax "call_indirect (type x)"
  194. writer.AsmWrite(#9);
  195. isprm := true;
  196. for i:=1 to length(cpu.typecode) do
  197. if cpu.typecode[i]=':' then
  198. isprm:=false
  199. else begin
  200. if isprm then writer.AsmWrite('(param ')
  201. else writer.AsmWrite('(result ');
  202. case cpu.typecode[i] of
  203. 'i': writer.AsmWrite('i32');
  204. 'I': writer.AsmWrite('i64');
  205. 'f': writer.AsmWrite('f32');
  206. 'F': writer.AsmWrite('f64');
  207. end;
  208. writer.AsmWrite(')');
  209. end;
  210. writer.AsmLn;
  211. exit;
  212. end;
  213. if (cpu.opcode = a_if) then
  214. writer.AsmWrite(' (result i32)') //todo: this is a hardcode, but shouldn't
  215. else
  216. cpu := taicpu(hp);
  217. if cpu.ops<>0 then
  218. begin
  219. for i:=0 to cpu.ops-1 do
  220. begin
  221. writer.AsmWrite(#9);
  222. if (cpu.opcode in AsmOp_LoadStore) and (cpu.oper[i]^.typ = top_ref) then
  223. writer.AsmWrite('offset='+tostr( cpu.oper[i]^.ref^.offset))
  224. else
  225. writer.AsmWrite(getopstr(cpu.oper[i]^));
  226. end;
  227. end;
  228. if (cpu.opcode = a_call_indirect) then
  229. // special wat2wasm syntax "call_indirect (type x)"
  230. writer.AsmWrite(')');
  231. writer.AsmLn;
  232. end;
  233. const
  234. as_wasm32_llvm_mc_info : tasminfo =
  235. (
  236. id : as_wasm32_llvm_mc;
  237. idtxt : 'LLVM-MC';
  238. asmbin : 'llvm-mc';
  239. asmcmd : '--assemble --arch=wasm32 --filetype=obj -o $OBJ $EXTRAOPT $ASM';
  240. supported_targets : [system_wasm32_wasm,system_wasm32_wasi];
  241. flags : [];
  242. labelprefix : '.L';
  243. labelmaxlen : -1;
  244. comment : '# ';
  245. dollarsign : '$';
  246. );
  247. initialization
  248. RegisterAssembler(as_wasm32_llvm_mc_info,TLLVMMachineCodePlaygroundAssembler);
  249. end.