agllvmmc.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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. FLLVMMajorVersion: Integer;
  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. end;
  36. { TLLVMMachineCodePlaygroundAssemblerV10 }
  37. TLLVMMachineCodePlaygroundAssemblerV10=class(TLLVMMachineCodePlaygroundAssembler)
  38. constructor CreateWithWriter(info: pasminfo; wr: TExternalAssemblerOutputFile; freewriter, smart: boolean); override;
  39. end;
  40. { TLLVMMachineCodePlaygroundAssemblerV11 }
  41. TLLVMMachineCodePlaygroundAssemblerV11=class(TLLVMMachineCodePlaygroundAssembler)
  42. constructor CreateWithWriter(info: pasminfo; wr: TExternalAssemblerOutputFile; freewriter, smart: boolean); override;
  43. end;
  44. { TLLVMMachineCodePlaygroundAssemblerV12 }
  45. TLLVMMachineCodePlaygroundAssemblerV12=class(TLLVMMachineCodePlaygroundAssembler)
  46. constructor CreateWithWriter(info: pasminfo; wr: TExternalAssemblerOutputFile; freewriter, smart: boolean); override;
  47. end;
  48. { TLLVMMachineCodePlaygroundAssemblerV13 }
  49. TLLVMMachineCodePlaygroundAssemblerV13=class(TLLVMMachineCodePlaygroundAssembler)
  50. constructor CreateWithWriter(info: pasminfo; wr: TExternalAssemblerOutputFile; freewriter, smart: boolean); override;
  51. end;
  52. { TLLVMMachineCodePlaygroundAssemblerV14 }
  53. TLLVMMachineCodePlaygroundAssemblerV14=class(TLLVMMachineCodePlaygroundAssembler)
  54. constructor CreateWithWriter(info: pasminfo; wr: TExternalAssemblerOutputFile; freewriter, smart: boolean); override;
  55. end;
  56. { TWASM32InstrWriter }
  57. TWASM32InstrWriter = class(TCPUInstrWriter)
  58. protected
  59. FLLVMMajorVersion: Integer;
  60. public
  61. procedure WriteInstruction(hp : tai);override;
  62. end;
  63. implementation
  64. uses
  65. cutils,
  66. fmodule,finput,
  67. itcpugas,
  68. cpubase,
  69. hlcgobj,hlcgcpu,
  70. verbose;
  71. { TLLVMMachineCodePlaygroundAssemblerV10 }
  72. constructor TLLVMMachineCodePlaygroundAssemblerV10.CreateWithWriter(info: pasminfo; wr: TExternalAssemblerOutputFile; freewriter, smart: boolean);
  73. begin
  74. FLLVMMajorVersion:=10;
  75. inherited CreateWithWriter(info, wr, freewriter, smart);
  76. end;
  77. { TLLVMMachineCodePlaygroundAssemblerV11 }
  78. constructor TLLVMMachineCodePlaygroundAssemblerV11.CreateWithWriter(info: pasminfo; wr: TExternalAssemblerOutputFile; freewriter, smart: boolean);
  79. begin
  80. FLLVMMajorVersion:=11;
  81. inherited CreateWithWriter(info, wr, freewriter, smart);
  82. end;
  83. { TLLVMMachineCodePlaygroundAssemblerV12 }
  84. constructor TLLVMMachineCodePlaygroundAssemblerV12.CreateWithWriter(info: pasminfo; wr: TExternalAssemblerOutputFile; freewriter, smart: boolean);
  85. begin
  86. FLLVMMajorVersion:=12;
  87. inherited CreateWithWriter(info, wr, freewriter, smart);
  88. end;
  89. { TLLVMMachineCodePlaygroundAssemblerV13 }
  90. constructor TLLVMMachineCodePlaygroundAssemblerV13.CreateWithWriter(info: pasminfo; wr: TExternalAssemblerOutputFile; freewriter, smart: boolean);
  91. begin
  92. FLLVMMajorVersion:=13;
  93. inherited CreateWithWriter(info, wr, freewriter, smart);
  94. end;
  95. { TLLVMMachineCodePlaygroundAssemblerV14 }
  96. constructor TLLVMMachineCodePlaygroundAssemblerV14.CreateWithWriter(info: pasminfo; wr: TExternalAssemblerOutputFile; freewriter, smart: boolean);
  97. begin
  98. FLLVMMajorVersion:=14;
  99. inherited CreateWithWriter(info, wr, freewriter, smart);
  100. end;
  101. { TLLVMMachineCodePlaygroundAssembler }
  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. TWASM32InstrWriter(InstrWriter).FLLVMMajorVersion:=FLLVMMajorVersion;
  113. end;
  114. { TWASM32InstrWriter }
  115. procedure TWASM32InstrWriter.WriteInstruction(hp: tai);
  116. function getreferencestring(var ref : treference) : ansistring;
  117. begin
  118. if assigned(ref.symbol) then
  119. begin
  120. // global symbol or field -> full type and name
  121. // ref.base can be <> NR_NO in case an instance field is loaded.
  122. // This register is not part of this instruction, it will have
  123. // been placed on the stack by the previous one.
  124. result:=ref.symbol.name;
  125. if ref.base<>NR_NO then
  126. result:=result+'+'+std_regname(ref.base);
  127. if ref.index<>NR_NO then
  128. result:=result+'+'+std_regname(ref.index);
  129. if ref.offset>0 then
  130. result:=result+'+'+tostr(ref.offset)
  131. else if ref.offset<0 then
  132. result:=result+tostr(ref.offset);
  133. end
  134. else
  135. begin
  136. // local symbol -> stack slot, stored in offset
  137. result:='';
  138. if (ref.base<>NR_STACK_POINTER_REG) and (ref.base<>NR_NO) then
  139. result:=std_regname(ref.base);
  140. if ref.index<>NR_NO then
  141. if result<>'' then
  142. result:=result+'+'+std_regname(ref.index)
  143. else
  144. result:=std_regname(ref.index);
  145. if ref.offset>0 then
  146. begin
  147. if result<>'' then
  148. result:=result+'+'+tostr(ref.offset)
  149. else
  150. result:=tostr(ref.offset);
  151. end
  152. else if ref.offset<0 then
  153. result:=result+tostr(ref.offset);
  154. if result='' then
  155. result:='0';
  156. end;
  157. end;
  158. function constfloat(rawfloat: int64; fraction_bits, exponent_bits, exponent_bias: Integer): ansistring;
  159. var
  160. sign: boolean;
  161. fraction: int64;
  162. exponent, fraction_hexdigits: integer;
  163. begin
  164. fraction_hexdigits := (fraction_bits + 3) div 4;
  165. sign:=(rawfloat shr (fraction_bits+exponent_bits))<>0;
  166. fraction:=rawfloat and ((int64(1) shl fraction_bits)-1);
  167. exponent:=(rawfloat shr fraction_bits) and ((1 shl exponent_bits)-1);
  168. if sign then
  169. result:='-'
  170. else
  171. result:='';
  172. if (exponent=(1 shl exponent_bits)-1) then
  173. begin
  174. if fraction=0 then
  175. result:=result+'infinity'
  176. else
  177. begin
  178. result:=result+'nan';
  179. if fraction<>(int64(1) shl (fraction_bits-1)) then
  180. result:=result+'(0x'+HexStr(fraction,fraction_hexdigits)+')';
  181. end;
  182. end
  183. else if (exponent=0) then
  184. begin
  185. if (fraction=0) then
  186. result:=result+'0x0.0p'+tostr(-exponent_bias)
  187. else
  188. result:=result+'0x0.'+HexStr(fraction shl (fraction_hexdigits*4-fraction_bits),fraction_hexdigits)+'p'+tostr(-exponent_bias+1)
  189. end
  190. else
  191. result:=result+'0x1.'+HexStr(fraction shl (fraction_hexdigits*4-fraction_bits),fraction_hexdigits)+'p'+tostr(exponent-exponent_bias);
  192. end;
  193. function constsingle(s: single): ansistring;
  194. type
  195. tsingleval = record
  196. case byte of
  197. 1: (s: single);
  198. 2: (i: longword);
  199. end;
  200. begin
  201. result:=constfloat(tsingleval(s).i,23,8,127);
  202. end;
  203. function constdouble(d: double): ansistring;
  204. type
  205. tdoubleval = record
  206. case byte of
  207. 1: (d: double);
  208. 2: (i: int64);
  209. end;
  210. begin
  211. result:=constfloat(tdoubleval(d).i,52,11,1023);
  212. end;
  213. function getopstr(const o:toper) : ansistring;
  214. var
  215. d: double;
  216. s: single;
  217. begin
  218. case o.typ of
  219. top_reg:
  220. // should have been translated into a memory location by the
  221. // register allocator)
  222. if (cs_no_regalloc in current_settings.globalswitches) then
  223. getopstr:=std_regname(o.reg)
  224. else
  225. internalerror(2010122803);
  226. top_const:
  227. str(o.val,result);
  228. top_ref:
  229. getopstr:=getreferencestring(o.ref^);
  230. top_single:
  231. begin
  232. result:=constsingle(o.sval);
  233. end;
  234. top_double:
  235. begin
  236. result:=constdouble(o.dval);
  237. end;
  238. {top_string:
  239. begin
  240. result:=constastr(o.pcval,o.pcvallen);
  241. end;
  242. top_wstring:
  243. begin
  244. result:=constwstr(o.pwstrval^.data,getlengthwidestring(o.pwstrval));
  245. end}
  246. else
  247. internalerror(2010122802);
  248. end;
  249. end;
  250. var
  251. cpu : taicpu;
  252. i : integer;
  253. writer: TExternalAssemblerOutputFile;
  254. begin
  255. writer:=owner.writer;
  256. cpu := taicpu(hp);
  257. writer.AsmWrite(#9#9);
  258. if FLLVMMajorVersion<=11 then
  259. case cpu.opcode of
  260. a_memory_atomic_wait32:
  261. writer.AsmWrite('i32.atomic.wait');
  262. a_memory_atomic_wait64:
  263. writer.AsmWrite('i64.atomic.wait');
  264. a_memory_atomic_notify:
  265. writer.AsmWrite('atomic.notify');
  266. else
  267. writer.AsmWrite(gas_op2str[cpu.opcode]);
  268. end
  269. else
  270. writer.AsmWrite(gas_op2str[cpu.opcode]);
  271. if cpu.ops<>0 then
  272. begin
  273. for i:=0 to cpu.ops-1 do
  274. begin
  275. writer.AsmWrite(#9);
  276. if cpu.oper[i]^.typ=top_functype then
  277. owner.WriteFuncType(cpu.oper[i]^.functype)
  278. else
  279. writer.AsmWrite(getopstr(cpu.oper[i]^));
  280. end;
  281. end;
  282. writer.AsmLn;
  283. end;
  284. const
  285. as_wasm32_llvm_mc_v10_info : tasminfo =
  286. (
  287. id : as_wasm32_llvm_mc_v10;
  288. idtxt : 'LLVM-MC-10';
  289. asmbin : 'llvm-mc-10';
  290. asmcmd : '--assemble --arch=wasm32 -mattr=+sign-ext,+exception-handling,+bulk-memory,+atomics --filetype=obj -o $OBJ $EXTRAOPT $ASM';
  291. supported_targets : [system_wasm32_embedded,system_wasm32_wasi];
  292. flags : [af_smartlink_sections];
  293. labelprefix : '.L';
  294. labelmaxlen : -1;
  295. comment : '# ';
  296. dollarsign : '$';
  297. );
  298. as_wasm32_llvm_mc_v11_info : tasminfo =
  299. (
  300. id : as_wasm32_llvm_mc_v11;
  301. idtxt : 'LLVM-MC-11';
  302. asmbin : 'llvm-mc-11';
  303. asmcmd : '--assemble --arch=wasm32 -mattr=+sign-ext,+exception-handling,+bulk-memory,+atomics --filetype=obj -o $OBJ $EXTRAOPT $ASM';
  304. supported_targets : [system_wasm32_embedded,system_wasm32_wasi];
  305. flags : [af_smartlink_sections];
  306. labelprefix : '.L';
  307. labelmaxlen : -1;
  308. comment : '# ';
  309. dollarsign : '$';
  310. );
  311. as_wasm32_llvm_mc_v12_info : tasminfo =
  312. (
  313. id : as_wasm32_llvm_mc_v12;
  314. idtxt : 'LLVM-MC-12';
  315. asmbin : 'llvm-mc-12';
  316. asmcmd : '--assemble --arch=wasm32 -mattr=+sign-ext,+exception-handling,+bulk-memory,+atomics --filetype=obj -o $OBJ $EXTRAOPT $ASM';
  317. supported_targets : [system_wasm32_embedded,system_wasm32_wasi];
  318. flags : [af_smartlink_sections];
  319. labelprefix : '.L';
  320. labelmaxlen : -1;
  321. comment : '# ';
  322. dollarsign : '$';
  323. );
  324. as_wasm32_llvm_mc_v13_info : tasminfo =
  325. (
  326. id : as_wasm32_llvm_mc_v13;
  327. idtxt : 'LLVM-MC-13';
  328. asmbin : 'llvm-mc-13';
  329. asmcmd : '--assemble --arch=wasm32 -mattr=+sign-ext,+exception-handling,+bulk-memory,+atomics --filetype=obj -o $OBJ $EXTRAOPT $ASM';
  330. supported_targets : [system_wasm32_embedded,system_wasm32_wasi];
  331. flags : [af_smartlink_sections];
  332. labelprefix : '.L';
  333. labelmaxlen : -1;
  334. comment : '# ';
  335. dollarsign : '$';
  336. );
  337. as_wasm32_llvm_mc_v14_info : tasminfo =
  338. (
  339. id : as_wasm32_llvm_mc;
  340. idtxt : 'LLVM-MC';
  341. asmbin : 'llvm-mc';
  342. asmcmd : '--assemble --arch=wasm32 -mattr=+sign-ext,+exception-handling,+bulk-memory,+atomics --filetype=obj -o $OBJ $EXTRAOPT $ASM';
  343. supported_targets : [system_wasm32_embedded,system_wasm32_wasi];
  344. flags : [af_smartlink_sections];
  345. labelprefix : '.L';
  346. labelmaxlen : -1;
  347. comment : '# ';
  348. dollarsign : '$';
  349. );
  350. initialization
  351. RegisterAssembler(as_wasm32_llvm_mc_v10_info,TLLVMMachineCodePlaygroundAssemblerV10);
  352. RegisterAssembler(as_wasm32_llvm_mc_v11_info,TLLVMMachineCodePlaygroundAssemblerV11);
  353. RegisterAssembler(as_wasm32_llvm_mc_v12_info,TLLVMMachineCodePlaygroundAssemblerV12);
  354. RegisterAssembler(as_wasm32_llvm_mc_v13_info,TLLVMMachineCodePlaygroundAssemblerV13);
  355. RegisterAssembler(as_wasm32_llvm_mc_v14_info,TLLVMMachineCodePlaygroundAssemblerV14);
  356. end.