2
0

agllvmmc.pas 10 KB

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