agllvmmc.pas 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. function sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;override;
  32. public
  33. constructor CreateWithWriter(info: pasminfo; wr: TExternalAssemblerOutputFile; freewriter, smart: boolean); override;
  34. end;
  35. { TWASM32InstrWriter }
  36. TWASM32InstrWriter = class(TCPUInstrWriter)
  37. procedure WriteInstruction(hp : tai);override;
  38. end;
  39. implementation
  40. uses
  41. cutils,
  42. fmodule,finput,
  43. itcpugas,
  44. cpubase,
  45. hlcgobj,hlcgcpu,
  46. verbose;
  47. { TLLVMMachineCodePlaygroundAssembler }
  48. function TLLVMMachineCodePlaygroundAssembler.sectionname(atype: TAsmSectiontype; const aname: string; aorder: TAsmSectionOrder): string;
  49. begin
  50. if (atype=sec_fpc) or (atype=sec_threadvar) then
  51. atype:=sec_data;
  52. Result:=inherited sectionname(atype, aname, aorder)+',"",@';
  53. end;
  54. constructor TLLVMMachineCodePlaygroundAssembler.CreateWithWriter(info: pasminfo; wr: TExternalAssemblerOutputFile; freewriter, smart: boolean);
  55. begin
  56. inherited;
  57. InstrWriter:=TWASM32InstrWriter.create(self);
  58. end;
  59. { TWASM32InstrWriter }
  60. procedure TWASM32InstrWriter.WriteInstruction(hp: tai);
  61. function getreferencestring(var ref : treference) : ansistring;
  62. begin
  63. if assigned(ref.symbol) then
  64. begin
  65. // global symbol or field -> full type and name
  66. // ref.base can be <> NR_NO in case an instance field is loaded.
  67. // This register is not part of this instruction, it will have
  68. // been placed on the stack by the previous one.
  69. result:=ref.symbol.name;
  70. if ref.base<>NR_NO then
  71. result:=result+'+'+std_regname(ref.base);
  72. if ref.index<>NR_NO then
  73. result:=result+'+'+std_regname(ref.index);
  74. if ref.offset>0 then
  75. result:=result+'+'+tostr(ref.offset)
  76. else if ref.offset<0 then
  77. result:=result+tostr(ref.offset);
  78. end
  79. else
  80. begin
  81. // local symbol -> stack slot, stored in offset
  82. result:='';
  83. if (ref.base<>NR_STACK_POINTER_REG) and (ref.base<>NR_NO) then
  84. result:=std_regname(ref.base);
  85. if ref.index<>NR_NO then
  86. if result<>'' then
  87. result:=result+'+'+std_regname(ref.index)
  88. else
  89. result:=std_regname(ref.index);
  90. if ref.offset>0 then
  91. begin
  92. if result<>'' then
  93. result:=result+'+'+tostr(ref.offset)
  94. else
  95. result:=tostr(ref.offset);
  96. end
  97. else if ref.offset<0 then
  98. result:=result+tostr(ref.offset);
  99. if result='' then
  100. result:='0';
  101. end;
  102. end;
  103. function constfloat(rawfloat: int64; fraction_bits, exponent_bits, exponent_bias: Integer): ansistring;
  104. var
  105. sign: boolean;
  106. fraction: int64;
  107. exponent, fraction_hexdigits: integer;
  108. begin
  109. fraction_hexdigits := (fraction_bits + 3) div 4;
  110. sign:=(rawfloat shr (fraction_bits+exponent_bits))<>0;
  111. fraction:=rawfloat and ((int64(1) shl fraction_bits)-1);
  112. exponent:=(rawfloat shr fraction_bits) and ((1 shl exponent_bits)-1);
  113. if sign then
  114. result:='-'
  115. else
  116. result:='';
  117. if (exponent=(1 shl exponent_bits)-1) then
  118. begin
  119. if fraction=0 then
  120. result:=result+'infinity'
  121. else
  122. begin
  123. result:=result+'nan';
  124. if fraction<>(int64(1) shl (fraction_bits-1)) then
  125. result:=result+':0x'+HexStr(fraction,fraction_hexdigits);
  126. end;
  127. end
  128. else
  129. result:=result+'0x1.'+HexStr(fraction shl (fraction_hexdigits*4-fraction_bits),fraction_hexdigits)+'p'+tostr(exponent-exponent_bias);
  130. end;
  131. function constsingle(s: single): ansistring;
  132. type
  133. tsingleval = record
  134. case byte of
  135. 1: (s: single);
  136. 2: (i: longword);
  137. end;
  138. begin
  139. result:=constfloat(tsingleval(s).i,23,8,127);
  140. end;
  141. function constdouble(d: double): ansistring;
  142. type
  143. tdoubleval = record
  144. case byte of
  145. 1: (d: double);
  146. 2: (i: int64);
  147. end;
  148. begin
  149. result:=constfloat(tdoubleval(d).i,52,11,1023);
  150. end;
  151. function getopstr(const o:toper) : ansistring;
  152. var
  153. d: double;
  154. s: single;
  155. begin
  156. case o.typ of
  157. top_reg:
  158. // should have been translated into a memory location by the
  159. // register allocator)
  160. if (cs_no_regalloc in current_settings.globalswitches) then
  161. getopstr:=std_regname(o.reg)
  162. else
  163. internalerror(2010122803);
  164. top_const:
  165. str(o.val,result);
  166. top_ref:
  167. getopstr:=getreferencestring(o.ref^);
  168. top_single:
  169. begin
  170. result:=constsingle(o.sval);
  171. end;
  172. top_double:
  173. begin
  174. result:=constdouble(o.dval);
  175. end;
  176. {top_string:
  177. begin
  178. result:=constastr(o.pcval,o.pcvallen);
  179. end;
  180. top_wstring:
  181. begin
  182. result:=constwstr(o.pwstrval^.data,getlengthwidestring(o.pwstrval));
  183. end}
  184. else
  185. internalerror(2010122802);
  186. end;
  187. end;
  188. var
  189. cpu : taicpu;
  190. i : integer;
  191. writer: TExternalAssemblerOutputFile;
  192. begin
  193. writer:=owner.writer;
  194. cpu := taicpu(hp);
  195. writer.AsmWrite(#9#9);
  196. writer.AsmWrite(gas_op2str[cpu.opcode]);
  197. if cpu.ops<>0 then
  198. begin
  199. for i:=0 to cpu.ops-1 do
  200. begin
  201. writer.AsmWrite(#9);
  202. if cpu.oper[i]^.typ=top_functype then
  203. owner.WriteFuncType(cpu.oper[i]^.functype)
  204. else
  205. writer.AsmWrite(getopstr(cpu.oper[i]^));
  206. end;
  207. end;
  208. writer.AsmLn;
  209. end;
  210. const
  211. as_wasm32_llvm_mc_info : tasminfo =
  212. (
  213. id : as_wasm32_llvm_mc;
  214. idtxt : 'LLVM-MC';
  215. asmbin : 'llvm-mc';
  216. asmcmd : '--assemble --arch=wasm32 -mattr=+sign-ext,+exception-handling,+bulk-memory --filetype=obj -o $OBJ $EXTRAOPT $ASM';
  217. supported_targets : [system_wasm32_embedded,system_wasm32_wasi];
  218. flags : [af_smartlink_sections];
  219. labelprefix : '.L';
  220. labelmaxlen : -1;
  221. comment : '# ';
  222. dollarsign : '$';
  223. );
  224. initialization
  225. RegisterAssembler(as_wasm32_llvm_mc_info,TLLVMMachineCodePlaygroundAssembler);
  226. end.