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