agllvmmc.pas 10.0 KB

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