cpugas.pas 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. {
  2. Copyright (c) 1999-2003 by Florian Klaempfl
  3. This unit implements an asmoutput class for SPARC AT&T syntax
  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. unit cpugas;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cpubase,
  22. aasmtai,aasmdata,aasmcpu,assemble,aggas,
  23. cgutils,globtype;
  24. type
  25. TGasSPARC=class(TGnuAssembler)
  26. constructor create(smart: boolean); override;
  27. {# Constructs the command line for calling the assembler }
  28. function MakeCmdLine: TCmdStr; override;
  29. end;
  30. TSPARCInstrWriter=class(TCPUInstrWriter)
  31. procedure WriteInstruction(hp:Tai);override;
  32. function GetReferenceString(var ref : TReference):string;
  33. function getopstr(const Oper:TOper):string;
  34. end;
  35. implementation
  36. uses
  37. cutils,systems,globals,cpuinfo,procinfo,
  38. verbose,itcpugas,cgbase;
  39. {****************************************************************************}
  40. { GNU PPC Assembler writer }
  41. {****************************************************************************}
  42. constructor TGasSPARC.create(smart: boolean);
  43. begin
  44. inherited create(smart);
  45. InstrWriter := TSPARCInstrWriter.create(self);
  46. end;
  47. function TGasSPARC.MakeCmdLine: TCmdStr;
  48. begin
  49. result := Inherited MakeCmdLine;
  50. { ARCH selection }
  51. // Note for casual readers: gas (GNU as) uses -Av7, -Av8, -Av9 etc. on SPARC,
  52. // rather than variants of the -m option used by most other CPUs. Solaris as
  53. // uses -xarch=v7, -xarch=v8 etc., that form is not supported here since there
  54. // are probably other incompatibilties between the GNU and Solaris binutils
  55. // that need to be reviewed.
  56. //
  57. // v9 is required as the default since the RTL started using membar at 2.2.2.
  58. case current_settings.cputype of
  59. cpu_SPARC_V7: Replace(result,'$ARCH','-Av7');
  60. cpu_SPARC_V8: Replace(result,'$ARCH','-Av8')
  61. else
  62. Replace(result,'$ARCH','-Av9')
  63. end
  64. end;
  65. function TSPARCInstrWriter.GetReferenceString(var ref:TReference):string;
  66. begin
  67. result:='';
  68. if assigned(ref.symbol) then
  69. result:=ref.symbol.name;
  70. if (ref.offset<0) then
  71. result:=result+tostr(ref.offset)
  72. else if (ref.offset>0) then
  73. begin
  74. if assigned(ref.symbol) then
  75. result:=result+'+';
  76. result:=result+tostr(ref.offset);
  77. end
  78. { asmreader appears to treat literal numbers as references }
  79. else if (ref.symbol=nil) and (ref.base=NR_NO) and (ref.index=NR_NO) then
  80. result:='0';
  81. case ref.refaddr of
  82. addr_high:
  83. result:='%hi('+result+')';
  84. addr_low:
  85. result:='%lo('+result+')';
  86. end;
  87. if assigned(ref.symbol) or (ref.offset<>0) then
  88. begin
  89. if (ref.base<>NR_NO) then
  90. begin
  91. if (ref.index<>NR_NO) then
  92. InternalError(2013013001);
  93. if (result[1]='-') then
  94. result:=gas_regname(ref.base)+result
  95. else
  96. result:=gas_regname(ref.base)+'+'+result;
  97. end
  98. else if (ref.index<>NR_NO) then
  99. InternalError(2013122501);
  100. end
  101. else
  102. begin
  103. if (ref.base<>NR_NO) then
  104. begin
  105. result:=gas_regname(ref.base);
  106. if (ref.index<>NR_NO) then
  107. result:=result+'+'+gas_regname(ref.index);
  108. end
  109. else if (ref.index<>NR_NO) then
  110. result:=gas_regname(ref.index);
  111. end;
  112. end;
  113. function TSPARCInstrWriter.getopstr(const Oper:TOper):string;
  114. begin
  115. with Oper do
  116. case typ of
  117. top_reg:
  118. getopstr:=gas_regname(reg);
  119. top_const:
  120. getopstr:=tostr(longint(val));
  121. top_ref:
  122. if (oper.ref^.refaddr in [addr_no,addr_pic]) or
  123. ((oper.ref^.refaddr=addr_low) and ((oper.ref^.base<>NR_NO) or
  124. (oper.ref^.index<>NR_NO))) then
  125. getopstr:='['+getreferencestring(ref^)+']'
  126. else
  127. getopstr:=getreferencestring(ref^);
  128. else
  129. internalerror(10001);
  130. end;
  131. end;
  132. procedure TSPARCInstrWriter.WriteInstruction(hp:Tai);
  133. procedure writePseudoInstruction(opc: TAsmOp);
  134. begin
  135. if (taicpu(hp).ops<>2) or
  136. (taicpu(hp).oper[0]^.typ<>top_reg) or
  137. (taicpu(hp).oper[1]^.typ<>top_reg) then
  138. internalerror(200401045);
  139. { Fxxxs %f<even>,%f<even> }
  140. owner.AsmWriteln(#9+std_op2str[opc]+#9+getopstr(taicpu(hp).oper[0]^)+','+getopstr(taicpu(hp).oper[1]^));
  141. { FMOVs %f<odd>,%f<odd> }
  142. inc(taicpu(hp).oper[0]^.reg);
  143. inc(taicpu(hp).oper[1]^.reg);
  144. owner.AsmWriteln(#9+std_op2str[A_FMOVs]+#9+getopstr(taicpu(hp).oper[0]^)+','+getopstr(taicpu(hp).oper[1]^));
  145. dec(taicpu(hp).oper[0]^.reg);
  146. dec(taicpu(hp).oper[1]^.reg);
  147. end;
  148. var
  149. Op:TAsmOp;
  150. s:String;
  151. i:Integer;
  152. begin
  153. if hp.typ<>ait_instruction then
  154. exit;
  155. op:=taicpu(hp).opcode;
  156. if (op=A_Bxx) and (taicpu(hp).condition in floatAsmConds) then
  157. op:=A_FBxx;
  158. { translate pseudoops, this should be move to a separate pass later, so it's done before
  159. peephole optimization }
  160. case op of
  161. A_FABSd:
  162. writePseudoInstruction(A_FABSs);
  163. A_FMOVd:
  164. writePseudoInstruction(A_FMOVs);
  165. A_FNEGd:
  166. writePseudoInstruction(A_FNEGs);
  167. else
  168. begin
  169. { call maybe not translated to call }
  170. s:=#9+std_op2str[op]+cond2str[taicpu(hp).condition];
  171. if taicpu(hp).delayslot_annulled then
  172. s:=s+',a';
  173. if taicpu(hp).ops>0 then
  174. begin
  175. s:=s+#9+getopstr(taicpu(hp).oper[0]^);
  176. for i:=1 to taicpu(hp).ops-1 do
  177. s:=s+','+getopstr(taicpu(hp).oper[i]^);
  178. end;
  179. owner.AsmWriteLn(s);
  180. end;
  181. end;
  182. end;
  183. const
  184. as_sparc_as_info : tasminfo =
  185. (
  186. id : as_gas;
  187. idtxt : 'AS';
  188. asmbin : 'as';
  189. {$ifdef FPC_SPARC_V8_ONLY}
  190. asmcmd : '$PIC -o $OBJ $ASM';
  191. {$else}
  192. asmcmd : '$ARCH $PIC -o $OBJ $ASM';
  193. {$endif}
  194. supported_targets : [system_sparc_solaris,system_sparc_linux,system_sparc_embedded];
  195. flags : [af_needar,af_smartlink_sections];
  196. labelprefix : '.L';
  197. comment : '# ';
  198. dollarsign: '$';
  199. );
  200. as_sparc_gas_info : tasminfo =
  201. (
  202. id : as_ggas;
  203. idtxt : 'GAS';
  204. asmbin : 'gas';
  205. asmcmd : '$ARCH $PIC -o $OBJ $ASM';
  206. supported_targets : [system_sparc_solaris,system_sparc_linux,system_sparc_embedded];
  207. flags : [af_needar,af_smartlink_sections];
  208. labelprefix : '.L';
  209. comment : '# ';
  210. dollarsign: '$';
  211. );
  212. begin
  213. RegisterAssembler(as_SPARC_as_info,TGasSPARC);
  214. RegisterAssembler(as_SPARC_gas_info,TGasSPARC);
  215. end.