agrvgas.pas 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. This unit the GAS asm writers for Risc-V32/Risc-V64
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. {****************************************************************************}
  18. { Helper routines for Instruction Writer }
  19. {****************************************************************************}
  20. unit agrvgas;
  21. {$i fpcdefs.inc}
  22. interface
  23. uses
  24. systems,aasmbase,
  25. aasmtai,aasmdata,
  26. aggas,
  27. assemble,
  28. cpubase,cgutils,
  29. globtype;
  30. type
  31. TRVInstrWriter=class(TCPUInstrWriter)
  32. procedure WriteInstruction(hp : tai);override;
  33. end;
  34. TRVGNUAssembler=class(TGNUassembler)
  35. constructor CreateWithWriter(info: pasminfo; wr: TExternalAssemblerOutputFile; freewriter, smart: boolean); override;
  36. function MakeCmdLine: TCmdStr; override;
  37. end;
  38. implementation
  39. uses
  40. cutils,globals,verbose,
  41. cgbase,
  42. itcpugas,cpuinfo,
  43. aasmcpu;
  44. function getreferencestring(asminfo: pasminfo; var ref : treference) : string;
  45. var
  46. s : string;
  47. begin
  48. with ref do
  49. begin
  50. if ((offset < -2048) or (offset > 2047)) and
  51. (refaddr = addr_no) then
  52. internalerror(2006052501);
  53. case refaddr of
  54. addr_no:
  55. s := '';
  56. addr_pic_no_got:
  57. internalerror(2016060501);
  58. else
  59. begin
  60. s :='';
  61. s := s+'(';
  62. if assigned(symbol) then
  63. begin
  64. if asminfo^.dollarsign<>'$' then
  65. begin
  66. s:=s+ReplaceForbiddenAsmSymbolChars(symbol.name);
  67. if assigned(relsymbol) then
  68. s:=s+'-'+ReplaceForbiddenAsmSymbolChars(relsymbol.name)
  69. end
  70. else
  71. begin
  72. s:=s+symbol.name;
  73. if assigned(relsymbol) then
  74. s:=s+'-'+relsymbol.name;
  75. end;
  76. end;
  77. end;
  78. end;
  79. if offset<0 then
  80. s:=s+tostr(offset)
  81. else
  82. if (offset>0) then
  83. begin
  84. if assigned(symbol) then
  85. s:=s+'+'+tostr(offset)
  86. else
  87. s:=s+tostr(offset);
  88. end;
  89. if not(refaddr in [addr_no,addr_pic_no_got]) then
  90. begin
  91. s := s+')';
  92. end;
  93. {$ifdef cpu64bitaddr}
  94. if (refaddr=addr_pic) then
  95. s := s + '@got';
  96. {$endif cpu64bitaddr}
  97. if (index=NR_NO) then
  98. begin
  99. if offset=0 then
  100. begin
  101. if not (assigned(symbol)) then
  102. s:=s+'0';
  103. end;
  104. if (base<>NR_NO) then
  105. s:=s+'('+gas_regname(base)+')'
  106. else if not assigned(symbol) then
  107. s:=s+'(0)';
  108. end
  109. else if (index<>NR_NO) and (base<>NR_NO) then
  110. begin
  111. if (offset=0) then
  112. s:=s+gas_regname(base)+','+gas_regname(index)
  113. else
  114. internalerror(2006052502);
  115. end;
  116. case refaddr of
  117. addr_lo12: s:='%lo'+s;
  118. addr_hi20: s:='%hi'+s;
  119. addr_pcrel_lo12: s:='%pcrel_lo'+s;
  120. addr_pcrel_hi20: s:='%pcrel_hi'+s;
  121. else
  122. ;
  123. end;
  124. end;
  125. getreferencestring:=s;
  126. end;
  127. function getopstr(asminfo: pasminfo; const o:toper) : string;
  128. var
  129. hs : string;
  130. begin
  131. case o.typ of
  132. top_reg:
  133. getopstr:=gas_regname(o.reg);
  134. top_const:
  135. getopstr:=tostr(longint(o.val));
  136. top_ref:
  137. if o.ref^.refaddr=addr_full then
  138. begin
  139. hs:=o.ref^.symbol.name;
  140. if asminfo^.dollarsign<>'$' then
  141. hs:=ReplaceForbiddenAsmSymbolChars(hs);
  142. if o.ref^.offset>0 then
  143. hs:=hs+'+'+tostr(o.ref^.offset)
  144. else
  145. if o.ref^.offset<0 then
  146. hs:=hs+tostr(o.ref^.offset);
  147. getopstr:=hs;
  148. end
  149. else
  150. getopstr:=getreferencestring(asminfo,o.ref^);
  151. top_fenceflags:
  152. begin
  153. getopstr:='';
  154. if ffI in o.fenceflags then getopstr:=getopstr+'i';
  155. if ffO in o.fenceflags then getopstr:=getopstr+'o';
  156. if ffR in o.fenceflags then getopstr:=getopstr+'r';
  157. if ffW in o.fenceflags then getopstr:=getopstr+'w';
  158. end;
  159. top_roundingmode:
  160. getopstr:=roundingmode2str[o.roundingmode];
  161. else
  162. internalerror(2002070604);
  163. end;
  164. end;
  165. Procedure TRVInstrWriter.WriteInstruction(hp : tai);
  166. var op: TAsmOp;
  167. s: string;
  168. i: byte;
  169. sep: string[3];
  170. begin
  171. s:=#9+gas_op2str[taicpu(hp).opcode];
  172. if taicpu(hp).condition<>C_None then
  173. s:=s+cond2str[taicpu(hp).condition];
  174. if taicpu(hp).memoryordering<>[] then
  175. begin
  176. s:=s+'.';
  177. if moAq in taicpu(hp).memoryordering then s:=s+'aq';
  178. if moRl in taicpu(hp).memoryordering then s:=s+'rl';
  179. end;
  180. if taicpu(hp).ops<>0 then
  181. begin
  182. sep:=#9;
  183. for i:=0 to taicpu(hp).ops-1 do
  184. begin
  185. s:=s+sep+getopstr(owner.asminfo,taicpu(hp).oper[i]^);
  186. sep:=',';
  187. end;
  188. end;
  189. owner.writer.AsmWriteLn(s);
  190. end;
  191. {****************************************************************************}
  192. { GNU RiscV Assembler writer }
  193. {****************************************************************************}
  194. constructor TRVGNUAssembler.CreateWithWriter(info: pasminfo; wr: TExternalAssemblerOutputFile; freewriter, smart: boolean);
  195. begin
  196. inherited;
  197. InstrWriter := TRVInstrWriter.create(self);
  198. end;
  199. function TRVGNUAssembler.MakeCmdLine: TCmdStr;
  200. const
  201. arch_str: array[boolean,tcputype] of string[10] = (
  202. {$ifdef RISCV32}
  203. ('','rv32ima','rv32im','rv32i'),
  204. ('','rv32imafd','rv32imfd','rv32ifd')
  205. {$endif RISCV32}
  206. {$ifdef RISCV64}
  207. ('','rv64imac','rv64ima','rv64im','rv64i'),
  208. ('','rv64imafdc','rv64imafd','rv64imfd','rv64ifd')
  209. {$endif RISCV64}
  210. );
  211. begin
  212. result := inherited MakeCmdLine;
  213. Replace(result,'$ARCH',arch_str[current_settings.fputype=fpu_fd,current_settings.cputype]);
  214. end;
  215. const
  216. as_riscv_gas_info : tasminfo =
  217. (
  218. id : as_gas;
  219. idtxt : 'AS';
  220. asmbin : 'as';
  221. asmcmd : '-o $OBJ $EXTRAOPT -march=$ARCH $ASM';
  222. supported_targets : [system_riscv32_linux,system_riscv64_linux];
  223. flags : [af_needar,af_smartlink_sections];
  224. labelprefix : '.L';
  225. comment : '# ';
  226. dollarsign: '$';
  227. );
  228. begin
  229. RegisterAssembler(as_riscv_gas_info,TRVGNUAssembler);
  230. end.