agllvmmc.pas 8.2 KB

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