agllvmmc.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. var
  52. i : integer;
  53. def : tdef;
  54. proc : tprocdef;
  55. list : TAsmList;
  56. cur_unit: tused_unit;
  57. begin
  58. for i:=0 to current_module.deflist.Count-1 do
  59. begin
  60. def:=tdef(current_module.deflist[i]);
  61. { since commit 48986 deflist might have NIL entries }
  62. if assigned(def) and (def.typ=procdef) then
  63. begin
  64. proc := tprocdef(def);
  65. if (po_external in proc.procoptions) and assigned(proc.import_dll) then
  66. begin
  67. //WriteProcDef(proc);
  68. list:=TAsmList.Create;
  69. thlcgwasm(hlcg).g_procdef(list,proc);
  70. WriteTree(list);
  71. list.free;
  72. writer.AsmWrite(#9'.import_module'#9);
  73. writer.AsmWrite(proc.mangledname);
  74. writer.AsmWrite(', ');
  75. writer.AsmWriteLn(proc.import_dll^);
  76. writer.AsmWrite(#9'.import_name'#9);
  77. writer.AsmWrite(proc.mangledname);
  78. writer.AsmWrite(', ');
  79. writer.AsmWriteLn(proc.import_name^);
  80. end;
  81. end;
  82. end;
  83. list:=TAsmList.Create;
  84. cur_unit:=tused_unit(usedunits.First);
  85. while assigned(cur_unit) do
  86. begin
  87. if (cur_unit.u.moduleflags * [mf_init,mf_finalize])<>[] then
  88. begin
  89. if mf_init in cur_unit.u.moduleflags then
  90. list.Concat(tai_functype.create(make_mangledname('INIT$',cur_unit.u.globalsymtable,''),TWasmFuncType.Create([],[])));
  91. if mf_finalize in cur_unit.u.moduleflags then
  92. list.Concat(tai_functype.create(make_mangledname('FINALIZE$',cur_unit.u.globalsymtable,''),TWasmFuncType.Create([],[])));
  93. end;
  94. for i:=0 to cur_unit.u.deflist.Count-1 do
  95. begin
  96. def:=tdef(cur_unit.u.deflist[i]);
  97. if assigned(def) and (tdef(def).typ = procdef) then
  98. begin
  99. proc := tprocdef(def);
  100. if (not proc.owner.iscurrentunit or (po_external in proc.procoptions)) and
  101. ((proc.paras.Count=0) or (proc.has_paraloc_info in [callerside,callbothsides])) then
  102. thlcgwasm(hlcg).g_procdef(list,proc);
  103. end;
  104. end;
  105. cur_unit:=tused_unit(cur_unit.Next);
  106. end;
  107. WriteTree(list);
  108. list.free;
  109. end;
  110. function TLLVMMachineCodePlaygroundAssembler.sectionname(atype: TAsmSectiontype; const aname: string; aorder: TAsmSectionOrder): string;
  111. begin
  112. if (atype=sec_fpc) or (atype=sec_threadvar) then
  113. atype:=sec_data;
  114. Result:=inherited sectionname(atype, aname, aorder)+',"",@';
  115. end;
  116. constructor TLLVMMachineCodePlaygroundAssembler.CreateWithWriter(info: pasminfo; wr: TExternalAssemblerOutputFile; freewriter, smart: boolean);
  117. begin
  118. inherited;
  119. InstrWriter:=TWASM32InstrWriter.create(self);
  120. end;
  121. procedure TLLVMMachineCodePlaygroundAssembler.WriteAsmList;
  122. begin
  123. inherited;
  124. { print all global procedures/functions }
  125. writer.AsmWriteLn(#9'.globaltype'#9+STACK_POINTER_SYM+', i32');
  126. WriteImports;
  127. end;
  128. { TWASM32InstrWriter }
  129. procedure TWASM32InstrWriter.WriteInstruction(hp: tai);
  130. function getreferencestring(var ref : treference) : ansistring;
  131. begin
  132. if assigned(ref.symbol) then
  133. begin
  134. // global symbol or field -> full type and name
  135. // ref.base can be <> NR_NO in case an instance field is loaded.
  136. // This register is not part of this instruction, it will have
  137. // been placed on the stack by the previous one.
  138. result:=ref.symbol.name;
  139. if ref.base<>NR_NO then
  140. result:=result+'+'+std_regname(ref.base);
  141. if ref.index<>NR_NO then
  142. result:=result+'+'+std_regname(ref.index);
  143. if ref.offset>0 then
  144. result:=result+'+'+tostr(ref.offset)
  145. else if ref.offset<0 then
  146. result:=result+tostr(ref.offset);
  147. end
  148. else
  149. begin
  150. // local symbol -> stack slot, stored in offset
  151. result:='';
  152. if (ref.base<>NR_STACK_POINTER_REG) and (ref.base<>NR_NO) then
  153. result:=std_regname(ref.base);
  154. if ref.index<>NR_NO then
  155. if result<>'' then
  156. result:=result+'+'+std_regname(ref.index)
  157. else
  158. result:=std_regname(ref.index);
  159. if ref.offset>0 then
  160. begin
  161. if result<>'' then
  162. result:=result+'+'+tostr(ref.offset)
  163. else
  164. result:=tostr(ref.offset);
  165. end
  166. else if ref.offset<0 then
  167. result:=result+tostr(ref.offset);
  168. if result='' then
  169. result:='0';
  170. end;
  171. end;
  172. function constfloat(rawfloat: int64; fraction_bits, exponent_bits, exponent_bias: Integer): ansistring;
  173. var
  174. sign: boolean;
  175. fraction: int64;
  176. exponent, fraction_hexdigits: integer;
  177. begin
  178. fraction_hexdigits := (fraction_bits + 3) div 4;
  179. sign:=(rawfloat shr (fraction_bits+exponent_bits))<>0;
  180. fraction:=rawfloat and ((int64(1) shl fraction_bits)-1);
  181. exponent:=(rawfloat shr fraction_bits) and ((1 shl exponent_bits)-1);
  182. if sign then
  183. result:='-'
  184. else
  185. result:='';
  186. if (exponent=(1 shl exponent_bits)-1) then
  187. begin
  188. if fraction=0 then
  189. result:=result+'infinity'
  190. else
  191. begin
  192. result:=result+'nan';
  193. if fraction<>(int64(1) shl (fraction_bits-1)) then
  194. result:=result+':0x'+HexStr(fraction,fraction_hexdigits);
  195. end;
  196. end
  197. else
  198. result:=result+'0x1.'+HexStr(fraction,fraction_hexdigits)+'p'+tostr(exponent-exponent_bias);
  199. end;
  200. function constsingle(s: single): ansistring;
  201. type
  202. tsingleval = record
  203. case byte of
  204. 1: (s: single);
  205. 2: (i: longword);
  206. end;
  207. begin
  208. result:=constfloat(tsingleval(s).i,23,8,127);
  209. end;
  210. function constdouble(d: double): ansistring;
  211. type
  212. tdoubleval = record
  213. case byte of
  214. 1: (d: double);
  215. 2: (i: int64);
  216. end;
  217. begin
  218. result:=constfloat(tdoubleval(d).i,52,11,1023);
  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.ops<>0 then
  267. begin
  268. for i:=0 to cpu.ops-1 do
  269. begin
  270. writer.AsmWrite(#9);
  271. if cpu.oper[i]^.typ=top_functype then
  272. owner.WriteFuncType(cpu.oper[i]^.functype)
  273. else
  274. writer.AsmWrite(getopstr(cpu.oper[i]^));
  275. end;
  276. end;
  277. writer.AsmLn;
  278. end;
  279. const
  280. as_wasm32_llvm_mc_info : tasminfo =
  281. (
  282. id : as_wasm32_llvm_mc;
  283. idtxt : 'LLVM-MC';
  284. asmbin : 'llvm-mc';
  285. asmcmd : '--assemble --arch=wasm32 -mattr=+sign-ext --filetype=obj -o $OBJ $EXTRAOPT $ASM';
  286. supported_targets : [system_wasm32_embedded,system_wasm32_wasi];
  287. flags : [af_smartlink_sections];
  288. labelprefix : '.L';
  289. labelmaxlen : -1;
  290. comment : '# ';
  291. dollarsign : '$';
  292. );
  293. initialization
  294. RegisterAssembler(as_wasm32_llvm_mc_info,TLLVMMachineCodePlaygroundAssembler);
  295. end.