agllvmmc.pas 14 KB

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