agrvgas.pas 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. end;
  122. end;
  123. getreferencestring:=s;
  124. end;
  125. function getopstr(asminfo: pasminfo; const o:toper) : string;
  126. var
  127. hs : string;
  128. begin
  129. case o.typ of
  130. top_reg:
  131. getopstr:=gas_regname(o.reg);
  132. top_const:
  133. getopstr:=tostr(longint(o.val));
  134. top_ref:
  135. if o.ref^.refaddr=addr_full then
  136. begin
  137. hs:=o.ref^.symbol.name;
  138. if asminfo^.dollarsign<>'$' then
  139. hs:=ReplaceForbiddenAsmSymbolChars(hs);
  140. if o.ref^.offset>0 then
  141. hs:=hs+'+'+tostr(o.ref^.offset)
  142. else
  143. if o.ref^.offset<0 then
  144. hs:=hs+tostr(o.ref^.offset);
  145. getopstr:=hs;
  146. end
  147. else
  148. getopstr:=getreferencestring(asminfo,o.ref^);
  149. top_fenceflags:
  150. begin
  151. getopstr:='';
  152. if ffI in o.fenceflags then getopstr:=getopstr+'i';
  153. if ffO in o.fenceflags then getopstr:=getopstr+'o';
  154. if ffR in o.fenceflags then getopstr:=getopstr+'r';
  155. if ffW in o.fenceflags then getopstr:=getopstr+'w';
  156. end;
  157. top_roundingmode:
  158. getopstr:=roundingmode2str[o.roundingmode];
  159. else
  160. internalerror(2002070604);
  161. end;
  162. end;
  163. Procedure TRVInstrWriter.WriteInstruction(hp : tai);
  164. var op: TAsmOp;
  165. s: string;
  166. i: byte;
  167. sep: string[3];
  168. begin
  169. s:=#9+gas_op2str[taicpu(hp).opcode];
  170. if taicpu(hp).condition<>C_None then
  171. s:=s+cond2str[taicpu(hp).condition];
  172. if taicpu(hp).memoryordering<>[] then
  173. begin
  174. s:=s+'.';
  175. if moAq in taicpu(hp).memoryordering then s:=s+'aq';
  176. if moRl in taicpu(hp).memoryordering then s:=s+'rl';
  177. end;
  178. if taicpu(hp).ops<>0 then
  179. begin
  180. sep:=#9;
  181. for i:=0 to taicpu(hp).ops-1 do
  182. begin
  183. s:=s+sep+getopstr(owner.asminfo,taicpu(hp).oper[i]^);
  184. sep:=',';
  185. end;
  186. end;
  187. owner.writer.AsmWriteLn(s);
  188. end;
  189. {****************************************************************************}
  190. { GNU RiscV Assembler writer }
  191. {****************************************************************************}
  192. constructor TRVGNUAssembler.CreateWithWriter(info: pasminfo; wr: TExternalAssemblerOutputFile; freewriter, smart: boolean);
  193. begin
  194. inherited;
  195. InstrWriter := TRVInstrWriter.create(self);
  196. end;
  197. function TRVGNUAssembler.MakeCmdLine: TCmdStr;
  198. begin
  199. result := inherited MakeCmdLine;
  200. Replace(result,'$ARCH',lower(cputypestr[current_settings.cputype]));
  201. end;
  202. const
  203. as_riscv_gas_info : tasminfo =
  204. (
  205. id : as_gas;
  206. idtxt : 'AS';
  207. asmbin : 'as';
  208. asmcmd : '-o $OBJ $EXTRAOPT -march=$ARCH $ASM';
  209. supported_targets : [system_riscv32_linux,system_riscv64_linux];
  210. flags : [af_needar,af_smartlink_sections];
  211. labelprefix : '.L';
  212. comment : '# ';
  213. dollarsign: '$';
  214. );
  215. begin
  216. RegisterAssembler(as_riscv_gas_info,TRVGNUAssembler);
  217. end.