agllvmmc.pas 15 KB

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