agx64att.pas 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. This unit implements an asmoutput class for i386 AT&T syntax
  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. { This unit implements an asmoutput class for x86-64 AT&T syntax
  19. }
  20. unit agx64att;
  21. {$i fpcdefs.inc}
  22. interface
  23. uses
  24. cclasses,cpubase,
  25. globals,
  26. aasmbase,aasmtai,aasmcpu,assemble,aggas;
  27. type
  28. Tx86_64ATTAssembler=class(TGNUassembler)
  29. public
  30. procedure WriteInstruction(hp: tai);override;
  31. end;
  32. TAttSuffix = (AttSufNONE,AttSufINT,AttSufFPU,AttSufFPUint);
  33. const
  34. gas_op2str:op2strtable={$i x64att.inc}
  35. gas_needsuffix:array[tasmop] of TAttSuffix={$i x64atts.inc}
  36. gas_reg2str : reg2strtable = ('',
  37. '%rax','%rcx','%rdx','%rbx','%rsp','%rbp','%rsi','%rdi',
  38. '%r8','%r9','%r10','%r11','%r12','%r13','%r14','%r15','%rip',
  39. '%eax','%ecx','%edx','%ebx','%esp','%ebp','%esi','%edi',
  40. '%r8d','%r9d','%r10d','%r11d','%r12d','%r13d','%r14d','%r15d',
  41. '%ax','%cx','%dx','%bx','%sp','%bp','%si','%di',
  42. '%r8w','%r9w','%r10w','%r11w','%r12w','%r13w','%r14w','%r15w',
  43. '%al','%cl','%dl','%bl','%spl','%bpl','%sil','%dil',
  44. '%r8b','%r9b','%r10b','%r11b','%r12b','%r13b','%r14b','%r15b',
  45. '%ah','%ch','%bh','%dh',
  46. '%cs','%ds','%es','%ss','%fs','%gs',
  47. '%st','%st(0)','%st(1)','%st(2)','%st(3)','%st(4)','%st(5)','%st(6)','%st(7)',
  48. '%dr0','%dr1','%dr2','%dr3','%dr6','%dr7',
  49. '%cr0','%cr2','%cr3','%cr4',
  50. '%tr3','%tr4','%tr5','%tr6','%tr7',
  51. '%mm0','%mm1','%mm2','%mm3','%mm4','%mm5','%mm6','%mm7',
  52. '%xmm0','%xmm1','%xmm2','%xmm3','%xmm4','%xmm5','%xmm6','%xmm7',
  53. '%xmm8','%xmm9','%xmm10','%xmm11','%xmm12','%xmm13','%xmm14','%xmm15'
  54. );
  55. gas_opsize2str : array[topsize] of string[2] = ('',
  56. 'b','w','l','bw','bl','wl','bq','wq','lq',
  57. 's','l','q',
  58. 's','l','t','d','q','v','x',
  59. '','',''
  60. );
  61. implementation
  62. uses
  63. cutils,systems,
  64. verbose;
  65. function getreferencestring(var ref : treference) : string;
  66. var
  67. s : string;
  68. begin
  69. with ref do
  70. begin
  71. inc(offset,offsetfixup);
  72. offsetfixup:=0;
  73. { have we a segment prefix ? }
  74. { These are probably not correctly handled under GAS }
  75. { should be replaced by coding the segment override }
  76. { directly! - DJGPP FAQ }
  77. if segment<>R_NO then
  78. s:=gas_reg2str[segment]+':'
  79. else
  80. s:='';
  81. if assigned(symbol) then
  82. s:=s+symbol.name;
  83. if offset<0 then
  84. s:=s+tostr(offset)
  85. else
  86. if (offset>0) then
  87. begin
  88. if assigned(symbol) then
  89. s:=s+'+'+tostr(offset)
  90. else
  91. s:=s+tostr(offset);
  92. end
  93. else if (index=R_NO) and (base=R_NO) and not assigned(symbol) then
  94. s:=s+'0';
  95. if (index<>R_NO) and (base=R_NO) then
  96. begin
  97. s:=s+'(,'+gas_reg2str[index];
  98. if scalefactor<>0 then
  99. s:=s+','+tostr(scalefactor)+')'
  100. else
  101. s:=s+')';
  102. end
  103. else
  104. if (index=R_NO) and (base<>R_NO) then
  105. s:=s+'('+gas_reg2str[base]+')'
  106. else
  107. if (index<>R_NO) and (base<>R_NO) then
  108. begin
  109. s:=s+'('+gas_reg2str[base]+','+gas_reg2str[index];
  110. if scalefactor<>0 then
  111. s:=s+','+tostr(scalefactor)+')'
  112. else
  113. s := s+')';
  114. end;
  115. end;
  116. getreferencestring:=s;
  117. end;
  118. function getopstr(const o:toper) : string;
  119. var
  120. hs : string;
  121. begin
  122. case o.typ of
  123. top_reg :
  124. getopstr:=gas_reg2str[o.reg];
  125. top_ref :
  126. getopstr:=getreferencestring(o.ref^);
  127. top_const :
  128. getopstr:='$'+tostr(longint(o.val));
  129. top_symbol :
  130. begin
  131. if assigned(o.sym) then
  132. hs:='$'+o.sym.name
  133. else
  134. hs:='$';
  135. if o.symofs>0 then
  136. hs:=hs+'+'+tostr(o.symofs)
  137. else
  138. if o.symofs<0 then
  139. hs:=hs+tostr(o.symofs)
  140. else
  141. if not(assigned(o.sym)) then
  142. hs:=hs+'0';
  143. getopstr:=hs;
  144. end;
  145. else
  146. internalerror(10001);
  147. end;
  148. end;
  149. function getopstr_jmp(const o:toper) : string;
  150. var
  151. hs : string;
  152. begin
  153. case o.typ of
  154. top_reg :
  155. getopstr_jmp:='*'+gas_reg2str[o.reg];
  156. top_ref :
  157. getopstr_jmp:='*'+getreferencestring(o.ref^);
  158. top_const :
  159. getopstr_jmp:=tostr(longint(o.val));
  160. top_symbol :
  161. begin
  162. hs:=o.sym.name;
  163. if o.symofs>0 then
  164. hs:=hs+'+'+tostr(o.symofs)
  165. else
  166. if o.symofs<0 then
  167. hs:=hs+tostr(o.symofs);
  168. getopstr_jmp:=hs;
  169. end;
  170. else
  171. internalerror(10001);
  172. end;
  173. end;
  174. {****************************************************************************
  175. TI386ATTASMOUTPUT
  176. ****************************************************************************}
  177. procedure Tx86_64AttAssembler. WriteInstruction(hp: tai);
  178. var
  179. op : tasmop;
  180. s : string;
  181. sep : char;
  182. calljmp : boolean;
  183. i : integer;
  184. begin
  185. if hp.typ <> ait_instruction then exit;
  186. taicpu(hp).SetOperandOrder(op_att);
  187. op:=taicpu(hp).opcode;
  188. calljmp:=is_calljmp(op);
  189. { call maybe not translated to call }
  190. s:=#9+gas_op2str[op]+cond2str[taicpu(hp).condition];
  191. { suffix needed ? fnstsw,fldcw don't support suffixes
  192. with binutils 2.9.5 under linux }
  193. if (not calljmp) and
  194. (gas_needsuffix[op]<>AttSufNONE) and
  195. (op<>A_FNSTSW) and (op<>A_FSTSW) and
  196. (op<>A_FNSTCW) and (op<>A_FSTCW) and
  197. (op<>A_FLDCW) and not(
  198. (taicpu(hp).oper[0].typ=top_reg) and
  199. (taicpu(hp).oper[0].reg in [R_ST..R_ST7])
  200. ) then
  201. s:=s+gas_opsize2str[taicpu(hp).opsize];
  202. { process operands }
  203. if taicpu(hp).ops<>0 then
  204. begin
  205. { call and jmp need an extra handling }
  206. { this code is only called if jmp isn't a labeled instruction }
  207. { quick hack to overcome a problem with manglednames=255 chars }
  208. if calljmp then
  209. begin
  210. AsmWrite(s+#9);
  211. s:=getopstr_jmp(taicpu(hp).oper[0]);
  212. end
  213. else
  214. begin
  215. for i:=0 to taicpu(hp).ops-1 do
  216. begin
  217. if i=0 then
  218. sep:=#9
  219. else
  220. sep:=',';
  221. s:=s+sep+getopstr(taicpu(hp).oper[i])
  222. end;
  223. end;
  224. end;
  225. AsmWriteLn(s);
  226. end;
  227. {*****************************************************************************
  228. Initialize
  229. *****************************************************************************}
  230. const
  231. as_x86_64_as_info : tasminfo =
  232. (
  233. id : as_x86_64_as;
  234. idtxt : 'AS';
  235. asmbin : 'as';
  236. asmcmd : '-o $OBJ $ASM';
  237. supported_target : system_any;
  238. outputbinary: false;
  239. allowdirect : true;
  240. needar : true;
  241. labelprefix_only_inside_procedure : false;
  242. labelprefix : '.L';
  243. comment : '# ';
  244. secnames : ('',
  245. '.text','.data','.bss',
  246. '','','','','','',
  247. '.stab','.stabstr','COMMON')
  248. );
  249. initialization
  250. RegisterAssembler(as_x86_64_as_info,Tx86_64ATTAssembler);
  251. end.
  252. {
  253. $Log$
  254. Revision 1.4 2003-01-05 13:36:54 florian
  255. * x86-64 compiles
  256. + very basic support for float128 type (x86-64 only)
  257. Revision 1.3 2002/08/12 15:08:44 carl
  258. + stab register indexes for powerpc (moved from gdb to cpubase)
  259. + tprocessor enumeration moved to cpuinfo
  260. + linker in target_info is now a class
  261. * many many updates for m68k (will soon start to compile)
  262. - removed some ifdef or correct them for correct cpu
  263. Revision 1.2 2002/07/25 22:55:33 florian
  264. * several fixes, small test units can be compiled
  265. Revision 1.1 2002/07/24 22:38:15 florian
  266. + initial release of x86-64 target code
  267. }