agllvmmc.pas 11 KB

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