agllvmmc.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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. if fraction<>(int64(1) shl (fraction_bits-1)) then
  197. result:=result+'(0x'+HexStr(fraction,fraction_hexdigits)+')';
  198. end;
  199. end
  200. else if (exponent=0) then
  201. begin
  202. if (fraction=0) then
  203. result:=result+'0x0.0p'+tostr(-exponent_bias)
  204. else
  205. result:=result+'0x0.'+HexStr(fraction shl (fraction_hexdigits*4-fraction_bits),fraction_hexdigits)+'p'+tostr(-exponent_bias+1)
  206. end
  207. else
  208. result:=result+'0x1.'+HexStr(fraction shl (fraction_hexdigits*4-fraction_bits),fraction_hexdigits)+'p'+tostr(exponent-exponent_bias);
  209. end;
  210. function constsingle(s: single): ansistring;
  211. type
  212. tsingleval = record
  213. case byte of
  214. 1: (s: single);
  215. 2: (i: longword);
  216. end;
  217. begin
  218. result:=constfloat(tsingleval(s).i,23,8,127);
  219. end;
  220. function constdouble(d: double): ansistring;
  221. type
  222. tdoubleval = record
  223. case byte of
  224. 1: (d: double);
  225. 2: (i: int64);
  226. end;
  227. begin
  228. result:=constfloat(tdoubleval(d).i,52,11,1023);
  229. end;
  230. function getopstr(const o:toper) : ansistring;
  231. var
  232. d: double;
  233. s: single;
  234. begin
  235. case o.typ of
  236. top_reg:
  237. // should have been translated into a memory location by the
  238. // register allocator)
  239. if (cs_no_regalloc in current_settings.globalswitches) then
  240. getopstr:=std_regname(o.reg)
  241. else
  242. internalerror(2010122803);
  243. top_const:
  244. str(o.val,result);
  245. top_ref:
  246. getopstr:=getreferencestring(o.ref^);
  247. top_single:
  248. begin
  249. result:=constsingle(o.sval);
  250. end;
  251. top_double:
  252. begin
  253. result:=constdouble(o.dval);
  254. end;
  255. {top_string:
  256. begin
  257. result:=constastr(o.pcval,o.pcvallen);
  258. end;
  259. top_wstring:
  260. begin
  261. result:=constwstr(o.pwstrval^.data,getlengthwidestring(o.pwstrval));
  262. end}
  263. else
  264. internalerror(2010122802);
  265. end;
  266. end;
  267. var
  268. cpu : taicpu;
  269. i : integer;
  270. writer: TExternalAssemblerOutputFile;
  271. begin
  272. writer:=owner.writer;
  273. cpu := taicpu(hp);
  274. writer.AsmWrite(#9#9);
  275. if FLLVMMajorVersion<=11 then
  276. case cpu.opcode of
  277. a_memory_atomic_wait32:
  278. writer.AsmWrite('i32.atomic.wait');
  279. a_memory_atomic_wait64:
  280. writer.AsmWrite('i64.atomic.wait');
  281. a_memory_atomic_notify:
  282. writer.AsmWrite('atomic.notify');
  283. else
  284. writer.AsmWrite(gas_op2str[cpu.opcode]);
  285. end
  286. else
  287. writer.AsmWrite(gas_op2str[cpu.opcode]);
  288. if cpu.ops<>0 then
  289. begin
  290. for i:=0 to cpu.ops-1 do
  291. begin
  292. writer.AsmWrite(#9);
  293. if cpu.oper[i]^.typ=top_functype then
  294. owner.WriteFuncType(cpu.oper[i]^.functype)
  295. else
  296. writer.AsmWrite(getopstr(cpu.oper[i]^));
  297. end;
  298. end;
  299. writer.AsmLn;
  300. end;
  301. const
  302. as_wasm32_llvm_mc_v10_info : tasminfo =
  303. (
  304. id : as_wasm32_llvm_mc_v10;
  305. idtxt : 'LLVM-MC-10';
  306. asmbin : 'llvm-mc-10';
  307. asmcmd : '--assemble --arch=wasm32 -mattr=+sign-ext,+exception-handling,+bulk-memory,+atomics --filetype=obj -o $OBJ $EXTRAOPT $ASM';
  308. supported_targets : [system_wasm32_embedded,system_wasm32_wasi];
  309. flags : [af_smartlink_sections];
  310. labelprefix : '.L';
  311. labelmaxlen : -1;
  312. comment : '# ';
  313. dollarsign : '$';
  314. );
  315. as_wasm32_llvm_mc_v11_info : tasminfo =
  316. (
  317. id : as_wasm32_llvm_mc_v11;
  318. idtxt : 'LLVM-MC-11';
  319. asmbin : 'llvm-mc-11';
  320. asmcmd : '--assemble --arch=wasm32 -mattr=+sign-ext,+exception-handling,+bulk-memory,+atomics --filetype=obj -o $OBJ $EXTRAOPT $ASM';
  321. supported_targets : [system_wasm32_embedded,system_wasm32_wasi];
  322. flags : [af_smartlink_sections];
  323. labelprefix : '.L';
  324. labelmaxlen : -1;
  325. comment : '# ';
  326. dollarsign : '$';
  327. );
  328. as_wasm32_llvm_mc_v12_info : tasminfo =
  329. (
  330. id : as_wasm32_llvm_mc_v12;
  331. idtxt : 'LLVM-MC-12';
  332. asmbin : 'llvm-mc-12';
  333. asmcmd : '--assemble --arch=wasm32 -mattr=+sign-ext,+exception-handling,+bulk-memory,+atomics --filetype=obj -o $OBJ $EXTRAOPT $ASM';
  334. supported_targets : [system_wasm32_embedded,system_wasm32_wasi];
  335. flags : [af_smartlink_sections];
  336. labelprefix : '.L';
  337. labelmaxlen : -1;
  338. comment : '# ';
  339. dollarsign : '$';
  340. );
  341. as_wasm32_llvm_mc_v13_info : tasminfo =
  342. (
  343. id : as_wasm32_llvm_mc_v13;
  344. idtxt : 'LLVM-MC-13';
  345. asmbin : 'llvm-mc-13';
  346. asmcmd : '--assemble --arch=wasm32 -mattr=+sign-ext,+exception-handling,+bulk-memory,+atomics --filetype=obj -o $OBJ $EXTRAOPT $ASM';
  347. supported_targets : [system_wasm32_embedded,system_wasm32_wasi];
  348. flags : [af_smartlink_sections];
  349. labelprefix : '.L';
  350. labelmaxlen : -1;
  351. comment : '# ';
  352. dollarsign : '$';
  353. );
  354. as_wasm32_llvm_mc_v14_info : tasminfo =
  355. (
  356. id : as_wasm32_llvm_mc;
  357. idtxt : 'LLVM-MC';
  358. asmbin : 'llvm-mc';
  359. asmcmd : '--assemble --arch=wasm32 -mattr=+sign-ext,+exception-handling,+bulk-memory,+atomics --filetype=obj -o $OBJ $EXTRAOPT $ASM';
  360. supported_targets : [system_wasm32_embedded,system_wasm32_wasi];
  361. flags : [af_smartlink_sections];
  362. labelprefix : '.L';
  363. labelmaxlen : -1;
  364. comment : '# ';
  365. dollarsign : '$';
  366. );
  367. initialization
  368. RegisterAssembler(as_wasm32_llvm_mc_v10_info,TLLVMMachineCodePlaygroundAssemblerV10);
  369. RegisterAssembler(as_wasm32_llvm_mc_v11_info,TLLVMMachineCodePlaygroundAssemblerV11);
  370. RegisterAssembler(as_wasm32_llvm_mc_v12_info,TLLVMMachineCodePlaygroundAssemblerV12);
  371. RegisterAssembler(as_wasm32_llvm_mc_v13_info,TLLVMMachineCodePlaygroundAssemblerV13);
  372. RegisterAssembler(as_wasm32_llvm_mc_v14_info,TLLVMMachineCodePlaygroundAssemblerV14);
  373. end.