agllvmmc.pas 11 KB

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