cpugas.pas 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. {******************************************************************************
  2. $Id$
  3. Copyright (c) 1998-2000 by Florian Klaempfl
  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. unit CpuGas;
  17. {This unit implements an asmoutput class for SPARC AT&T syntax}
  18. {$MACRO ON}{$INCLUDE fpcdefs.inc}
  19. interface
  20. uses
  21. cclasses,cpubase,
  22. globals,
  23. aasmbase,aasmtai,aasmcpu,assemble,aggas;
  24. type
  25. TGasSPARC=class(TGnuAssembler)
  26. procedure WriteInstruction(hp:Tai);override;
  27. end;
  28. implementation
  29. uses
  30. finput,
  31. cutils,systems,
  32. verbose;
  33. {$DEFINE gas_reg2str:=std_reg2str}
  34. const
  35. line_length = 70;
  36. var
  37. {$ifdef GDB}
  38. n_line:byte; { different types of source lines }
  39. linecount,
  40. includecount:longint;
  41. funcname:pchar;
  42. stabslastfileinfo:tfileposinfo;
  43. {$endif}
  44. lastsec:tsection; { last section type written }
  45. lastfileinfo:tfileposinfo;
  46. infile,
  47. lastinfile:tinputfile;
  48. symendcount:longint;
  49. function fixline(s:string):string;
  50. {return s with all leading and ending spaces and tabs removed}
  51. var
  52. i,j,k:longint;
  53. begin
  54. i:=length(s);
  55. while (i>0) and (s[i] in [#9,' ']) do
  56. dec(i);
  57. j:=1;
  58. while (j<i) and (s[j] in [#9,' ']) do
  59. inc(j);
  60. for k:=j to i do
  61. if s[k] in [#0..#31,#127..#255]
  62. then
  63. s[k]:='.';
  64. fixline:=Copy(s,j,i-j+1);
  65. end;
  66. function single2str(d:single):string;
  67. var
  68. hs:string;
  69. begin
  70. str(d,hs);
  71. {replace space with +}
  72. if hs[1]=' '
  73. then
  74. hs[1]:='+';
  75. single2str:='0d'+hs
  76. end;
  77. function double2str(d:double):string;
  78. var
  79. hs:string;
  80. begin
  81. str(d,hs);
  82. { replace space with + }
  83. if hs[1]=' '
  84. then
  85. hs[1]:='+';
  86. double2str:='0d'+hs
  87. end;
  88. function extended2str(e:extended):string;
  89. var
  90. hs:string;
  91. begin
  92. str(e,hs);
  93. { replace space with + }
  94. if hs[1]=' '
  95. then
  96. hs[1]:='+';
  97. extended2str:='0d'+hs
  98. end;
  99. function GetReferenceString(var ref:TReference):string;
  100. var
  101. s:string;
  102. begin
  103. with ref do
  104. begin
  105. inc(offset,offsetfixup);
  106. offsetfixup:=0;
  107. { have we a segment prefix ? }
  108. { These are probably not correctly handled under GAS }
  109. { should be replaced by coding the segment override }
  110. { directly! - DJGPP FAQ }
  111. if segment<>R_NONE
  112. then
  113. s:=gas_reg2str[segment]+':'
  114. else
  115. s:='';
  116. if assigned(symbol)
  117. then
  118. s:=s+symbol.name;
  119. if offset<0
  120. then
  121. s:=s+tostr(offset)
  122. else if (offset>0)
  123. then
  124. begin
  125. if assigned(symbol)
  126. then
  127. s:=s+'+'+tostr(offset)
  128. else
  129. s:=s+tostr(offset);
  130. end
  131. else if (index=R_NONE) and (base=R_NONE) and not assigned(symbol)
  132. then
  133. s:=s+'0';
  134. if (index<>R_NONE) and (base=R_NONE)
  135. then
  136. begin
  137. s:='['+gas_reg2str[index]+s;
  138. if scalefactor<>0
  139. then
  140. s:=tostr(scalefactor)+'+'+s;
  141. s:=s+']';
  142. end
  143. else if (index=R_NONE) and (base<>R_NONE)
  144. then
  145. s:='['+gas_reg2str[base]+'+'+s+']'
  146. else if (index<>R_NONE) and (base<>R_NONE)
  147. then
  148. begin
  149. s:='['+gas_reg2str[base]+'+'+gas_reg2str[index];
  150. if scalefactor<>0
  151. then
  152. s:=tostr(scalefactor)+'+'+s;
  153. s:= s+']';
  154. end;
  155. end;
  156. getreferencestring:=s;
  157. end;
  158. function getopstr(const Oper:TOper):string;
  159. var
  160. hs:string;
  161. begin
  162. with Oper do
  163. case typ of
  164. top_reg:
  165. getopstr:=gas_reg2str[reg];
  166. top_ref:
  167. getopstr:=getreferencestring(ref^);
  168. top_const:
  169. getopstr:={'$'+}tostr(longint(val));
  170. top_symbol:
  171. begin
  172. if assigned(sym) then
  173. hs:={'$'+}sym.name
  174. else
  175. hs:='$';
  176. if symofs>0 then
  177. hs:=hs+'+'+tostr(symofs)
  178. else
  179. if symofs<0 then
  180. hs:=hs+tostr(symofs)
  181. else
  182. if not(assigned(sym)) then
  183. hs:=hs+'0';
  184. getopstr:=hs;
  185. end;
  186. else
  187. internalerror(10001);
  188. end;
  189. end;
  190. function getopstr_jmp(const Oper:TOper):string;
  191. var
  192. hs:string;
  193. begin
  194. with Oper do
  195. case typ of
  196. top_reg:
  197. getopstr_jmp:=gas_reg2str[reg]+'+';
  198. top_ref:
  199. getopstr_jmp:=GetReferenceString(ref^);
  200. top_const:
  201. getopstr_jmp:=tostr(longint(val));
  202. top_symbol:
  203. begin
  204. hs:=sym.name;
  205. if symofs>0 then
  206. hs:=hs+'+'+tostr(symofs)
  207. else
  208. if symofs<0 then
  209. hs:=hs+tostr(symofs);
  210. getopstr_jmp:=hs;
  211. end;
  212. else
  213. internalerror(10001);
  214. end;
  215. end;
  216. {****************************************************************************
  217. TISPARCATTASMOUTPUT
  218. ****************************************************************************}
  219. procedure TGasSPARC.WriteInstruction(hp:Tai);
  220. var
  221. Op:TAsmOp;
  222. s:String;
  223. i:Integer;
  224. begin
  225. if hp.typ<>ait_instruction
  226. then
  227. Exit;
  228. taicpu(hp).SetOperandOrder(op_att);
  229. op:=taicpu(hp).opcode;
  230. {call maybe not translated to call}
  231. s:=#9+std_op2str[op]+cond2str[taicpu(hp).condition];
  232. {process operands}
  233. s:=#9+std_op2str[op];
  234. if taicpu(hp).ops>0
  235. then
  236. begin
  237. s+=#9+getopstr(taicpu(hp).oper[0]);
  238. for i:=1 to taicpu(hp).ops-1 do
  239. s+=','+getopstr(taicpu(hp).oper[i]);
  240. end;
  241. AsmWriteLn(s);
  242. end;
  243. {*****************************************************************************
  244. Initialize
  245. *****************************************************************************}
  246. const
  247. as_SPARC_as_info:TAsmInfo=(
  248. id:as_gas;
  249. idtxt:'AS';
  250. asmbin:'as';
  251. asmcmd:'-o $OBJ $ASM';
  252. supported_target:system_any;
  253. outputbinary:false;
  254. allowdirect:true;
  255. needar:true;
  256. labelprefix_only_inside_procedure:false;
  257. labelprefix:'.L';
  258. comment:'; ';
  259. secnames:({sec_none}'', {no section}
  260. {sec_code}'.text', {executable code}
  261. {sec_data}'.data', {initialized R/W data}
  262. {sec_bss}'.bss', {uninitialized R/W data}
  263. {sec_idata2}'.comment', {comments}
  264. {sec_idata4}'.debug', {debugging information}
  265. {sec_idata5}'.rodata', {RO data}
  266. {sec_idata6}'.line', {line numbers info for symbolic debug}
  267. {sec_idata7}'.init', {runtime intialization code}
  268. {sec_edata}'.fini', {runtime finalization code}
  269. {sec_stab}'.stab',
  270. {sec_stabstr} '.stabstr',
  271. {sec_common}'.note') {note info}
  272. );
  273. INITIALIZATION
  274. RegisterAssembler(as_SPARC_as_info,TGasSPARC);
  275. END.