agllvmmc.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. procedure WriteImports;
  34. function sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;override;
  35. public
  36. constructor CreateWithWriter(info: pasminfo; wr: TExternalAssemblerOutputFile; freewriter, smart: boolean); override;
  37. procedure WriteAsmList;override;
  38. end;
  39. { TWASM32InstrWriter }
  40. TWASM32InstrWriter = class(TCPUInstrWriter)
  41. procedure WriteInstruction(hp : tai);override;
  42. end;
  43. implementation
  44. uses
  45. cutils,
  46. fmodule,finput,
  47. itcpugas,
  48. cpubase,
  49. hlcgobj,hlcgcpu,
  50. verbose;
  51. { TLLVMMachineCodePlaygroundAssembler }
  52. procedure TLLVMMachineCodePlaygroundAssembler.WriteProcDef(pd: tprocdef);
  53. begin
  54. if not assigned(tcpuprocdef(pd).exprasmlist) and
  55. not(po_abstractmethod in pd.procoptions) and
  56. (pd.proctypeoption in [potype_unitinit,potype_unitfinalize]) then
  57. exit;
  58. writer.AsmWriteLn(asminfo^.comment+'WriteProcDef('+pd.mangledname+')');
  59. WriteTree(tcpuprocdef(pd).exprasmlist);
  60. writer.AsmWriteLn(asminfo^.comment+'WriteProcDef('+pd.mangledname+') done');
  61. end;
  62. procedure TLLVMMachineCodePlaygroundAssembler.WriteSymtableProcdefs(st: TSymtable);
  63. var
  64. i : longint;
  65. def : tdef;
  66. begin
  67. if not assigned(st) then
  68. exit;
  69. for i:=0 to st.DefList.Count-1 do
  70. begin
  71. def:=tdef(st.DefList[i]);
  72. case def.typ of
  73. procdef :
  74. begin
  75. { methods are also in the static/globalsymtable of the unit
  76. -> make sure they are only written for the objectdefs that
  77. own them }
  78. if (not(st.symtabletype in [staticsymtable,globalsymtable]) or
  79. (def.owner=st)) and
  80. not(df_generic in def.defoptions) then
  81. begin
  82. WriteProcDef(tprocdef(def));
  83. if assigned(tprocdef(def).localst) then
  84. WriteSymtableProcdefs(tprocdef(def).localst);
  85. end;
  86. end;
  87. else
  88. ;
  89. end;
  90. end;
  91. end;
  92. procedure TLLVMMachineCodePlaygroundAssembler.WriteImports;
  93. var
  94. i : integer;
  95. proc : tprocdef;
  96. list : TAsmList;
  97. cur_unit: tused_unit;
  98. begin
  99. for i:=0 to current_module.deflist.Count-1 do
  100. if tdef(current_module.deflist[i]).typ = procdef then
  101. begin
  102. proc := tprocdef(current_module.deflist[i]);
  103. if (po_external in proc.procoptions) and assigned(proc.import_dll) then
  104. begin
  105. //WriteProcDef(proc);
  106. list:=TAsmList.Create;
  107. thlcgwasm(hlcg).g_procdef(list,proc);
  108. WriteTree(list);
  109. list.free;
  110. writer.AsmWrite(#9'.import_module'#9);
  111. writer.AsmWrite(proc.mangledname);
  112. writer.AsmWrite(', ');
  113. writer.AsmWriteLn(proc.import_dll^);
  114. writer.AsmWrite(#9'.import_name'#9);
  115. writer.AsmWrite(proc.mangledname);
  116. writer.AsmWrite(', ');
  117. writer.AsmWriteLn(proc.import_name^);
  118. end;
  119. end;
  120. cur_unit:=tused_unit(usedunits.First);
  121. while assigned(cur_unit) do
  122. begin
  123. for i:=0 to cur_unit.u.deflist.Count-1 do
  124. if assigned(cur_unit.u.deflist[i]) and (tdef(cur_unit.u.deflist[i]).typ = procdef) then
  125. begin
  126. proc := tprocdef(cur_unit.u.deflist[i]);
  127. if (not proc.owner.iscurrentunit or (po_external in proc.procoptions)) and
  128. ((proc.paras.Count=0) or (proc.has_paraloc_info in [callerside,callbothsides])) then
  129. begin
  130. list:=TAsmList.Create;
  131. thlcgwasm(hlcg).g_procdef(list,proc);
  132. WriteTree(list);
  133. list.free;
  134. end;
  135. end;
  136. cur_unit:=tused_unit(cur_unit.Next);
  137. end;
  138. end;
  139. function TLLVMMachineCodePlaygroundAssembler.sectionname(atype: TAsmSectiontype; const aname: string; aorder: TAsmSectionOrder): string;
  140. begin
  141. if atype=sec_fpc then
  142. atype:=sec_data;
  143. Result:=inherited sectionname(atype, aname, aorder)+',"",@';
  144. end;
  145. constructor TLLVMMachineCodePlaygroundAssembler.CreateWithWriter(info: pasminfo; wr: TExternalAssemblerOutputFile; freewriter, smart: boolean);
  146. begin
  147. inherited;
  148. InstrWriter:=TWASM32InstrWriter.create(self);
  149. end;
  150. procedure TLLVMMachineCodePlaygroundAssembler.WriteAsmList;
  151. begin
  152. inherited;
  153. { print all global procedures/functions }
  154. WriteSymtableProcdefs(current_module.globalsymtable);
  155. WriteSymtableProcdefs(current_module.localsymtable);
  156. writer.AsmWriteLn(#9'.globaltype'#9+STACK_POINTER_SYM+', i32');
  157. WriteImports;
  158. end;
  159. { TWASM32InstrWriter }
  160. procedure TWASM32InstrWriter.WriteInstruction(hp: tai);
  161. function getreferencestring(var ref : treference) : ansistring;
  162. begin
  163. if assigned(ref.symbol) then
  164. begin
  165. // global symbol or field -> full type and name
  166. // ref.base can be <> NR_NO in case an instance field is loaded.
  167. // This register is not part of this instruction, it will have
  168. // been placed on the stack by the previous one.
  169. result:=ref.symbol.name;
  170. if ref.base<>NR_NO then
  171. result:=result+'+'+std_regname(ref.base);
  172. if ref.index<>NR_NO then
  173. result:=result+'+'+std_regname(ref.index);
  174. if ref.offset>0 then
  175. result:=result+'+'+tostr(ref.offset)
  176. else if ref.offset<0 then
  177. result:=result+tostr(ref.offset);
  178. end
  179. else
  180. begin
  181. // local symbol -> stack slot, stored in offset
  182. result:='';
  183. if (ref.base<>NR_STACK_POINTER_REG) and (ref.base<>NR_NO) then
  184. result:=std_regname(ref.base);
  185. if ref.index<>NR_NO then
  186. if result<>'' then
  187. result:=result+'+'+std_regname(ref.index)
  188. else
  189. result:=std_regname(ref.index);
  190. if ref.offset>0 then
  191. begin
  192. if result<>'' then
  193. result:=result+'+'+tostr(ref.offset)
  194. else
  195. result:=tostr(ref.offset);
  196. end
  197. else if ref.offset<0 then
  198. result:=result+tostr(ref.offset);
  199. if result='' then
  200. result:='0';
  201. end;
  202. end;
  203. function constsingle(s: single): ansistring;
  204. begin
  205. // wat2wasm is using strtof() internally
  206. str(s, result); //'0x'+hexstr(longint(t32bitarray(s)),8);
  207. end;
  208. function constdouble(d: double): ansistring;
  209. begin
  210. // force interpretation as double (since we write it out as an
  211. // integer, we never have to swap the endianess). We have to
  212. // include the sign separately because of the way Java parses
  213. // hex numbers (0x8000000000000000 is not a valid long)
  214. //result:=hexstr(abs(int64(t64bitarray(d))),16);
  215. //if int64(t64bitarray(d))<0 then
  216. // result:='-'+result;
  217. //result:='0dx'+result;
  218. str(d, result);
  219. end;
  220. function getopstr(const o:toper) : ansistring;
  221. var
  222. d: double;
  223. s: single;
  224. begin
  225. case o.typ of
  226. top_reg:
  227. // should have been translated into a memory location by the
  228. // register allocator)
  229. if (cs_no_regalloc in current_settings.globalswitches) then
  230. getopstr:=std_regname(o.reg)
  231. else
  232. internalerror(2010122803);
  233. top_const:
  234. str(o.val,result);
  235. top_ref:
  236. getopstr:=getreferencestring(o.ref^);
  237. top_single:
  238. begin
  239. result:=constsingle(o.sval);
  240. end;
  241. top_double:
  242. begin
  243. result:=constdouble(o.dval);
  244. end;
  245. {top_string:
  246. begin
  247. result:=constastr(o.pcval,o.pcvallen);
  248. end;
  249. top_wstring:
  250. begin
  251. result:=constwstr(o.pwstrval^.data,getlengthwidestring(o.pwstrval));
  252. end}
  253. else
  254. internalerror(2010122802);
  255. end;
  256. end;
  257. var
  258. cpu : taicpu;
  259. i : integer;
  260. writer: TExternalAssemblerOutputFile;
  261. begin
  262. writer:=owner.writer;
  263. cpu := taicpu(hp);
  264. writer.AsmWrite(#9#9);
  265. writer.AsmWrite(gas_op2str[cpu.opcode]);
  266. if (cpu.opcode = a_if) then
  267. writer.AsmWrite(' i32') //todo: this is a hardcode, but shouldn't
  268. else if cpu.ops<>0 then
  269. begin
  270. for i:=0 to cpu.ops-1 do
  271. begin
  272. writer.AsmWrite(#9);
  273. if cpu.oper[i]^.typ=top_functype then
  274. owner.WriteFuncType(cpu.oper[i]^.functype)
  275. else
  276. writer.AsmWrite(getopstr(cpu.oper[i]^));
  277. end;
  278. end;
  279. writer.AsmLn;
  280. end;
  281. const
  282. as_wasm32_llvm_mc_info : tasminfo =
  283. (
  284. id : as_wasm32_llvm_mc;
  285. idtxt : 'LLVM-MC';
  286. asmbin : 'llvm-mc';
  287. asmcmd : '--assemble --arch=wasm32 -mattr=+sign-ext --filetype=obj -o $OBJ $EXTRAOPT $ASM';
  288. supported_targets : [system_wasm32_wasm,system_wasm32_wasi];
  289. flags : [];
  290. labelprefix : '.L';
  291. labelmaxlen : -1;
  292. comment : '# ';
  293. dollarsign : '$';
  294. );
  295. initialization
  296. RegisterAssembler(as_wasm32_llvm_mc_info,TLLVMMachineCodePlaygroundAssembler);
  297. end.