ag386att.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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. unit ag386att;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. cclasses,cpubase,
  23. globals,
  24. aasmbase,aasmtai,aasmcpu,assemble,aggas;
  25. type
  26. T386ATTAssembler=class(TGNUassembler)
  27. public
  28. procedure WriteInstruction(hp: tai); override;
  29. end;
  30. TAttSuffix = (AttSufNONE,AttSufINT,AttSufFPU,AttSufFPUint);
  31. const
  32. gas_op2str:op2strtable={$i i386att.inc}
  33. gas_needsuffix:array[tasmop] of TAttSuffix={$i i386atts.inc}
  34. gas_reg2str : reg2strtable = ('',
  35. '%eax','%ecx','%edx','%ebx','%esp','%ebp','%esi','%edi',
  36. '%ax','%cx','%dx','%bx','%sp','%bp','%si','%di',
  37. '%al','%cl','%dl','%bl','%ah','%ch','%bh','%dh',
  38. '%cs','%ds','%es','%ss','%fs','%gs',
  39. '%st','%st(0)','%st(1)','%st(2)','%st(3)','%st(4)','%st(5)','%st(6)','%st(7)',
  40. '%dr0','%dr1','%dr2','%dr3','%dr6','%dr7',
  41. '%cr0','%cr2','%cr3','%cr4',
  42. '%tr3','%tr4','%tr5','%tr6','%tr7',
  43. '%mm0','%mm1','%mm2','%mm3','%mm4','%mm5','%mm6','%mm7',
  44. '%xmm0','%xmm1','%xmm2','%xmm3','%xmm4','%xmm5','%xmm6','%xmm7'
  45. );
  46. gas_opsize2str : array[topsize] of string[2] = ('',
  47. 'b','w','l','bw','bl','wl',
  48. 's','l','q',
  49. 's','l','t','d','q','v',
  50. '','',''
  51. );
  52. implementation
  53. uses
  54. cutils,systems,
  55. verbose;
  56. function getreferencestring(var ref : treference) : string;
  57. var
  58. s : string;
  59. begin
  60. with ref do
  61. begin
  62. inc(offset,offsetfixup);
  63. offsetfixup:=0;
  64. { have we a segment prefix ? }
  65. { These are probably not correctly handled under GAS }
  66. { should be replaced by coding the segment override }
  67. { directly! - DJGPP FAQ }
  68. if segment<>R_NO then
  69. s:=gas_reg2str[segment]+':'
  70. else
  71. s:='';
  72. if assigned(symbol) then
  73. s:=s+symbol.name;
  74. if offset<0 then
  75. s:=s+tostr(offset)
  76. else
  77. if (offset>0) then
  78. begin
  79. if assigned(symbol) then
  80. s:=s+'+'+tostr(offset)
  81. else
  82. s:=s+tostr(offset);
  83. end
  84. else if (index=R_NO) and (base=R_NO) and not assigned(symbol) then
  85. s:=s+'0';
  86. if (index<>R_NO) and (base=R_NO) then
  87. begin
  88. s:=s+'(,'+gas_reg2str[index];
  89. if scalefactor<>0 then
  90. s:=s+','+tostr(scalefactor)+')'
  91. else
  92. s:=s+')';
  93. end
  94. else
  95. if (index=R_NO) and (base<>R_NO) then
  96. s:=s+'('+gas_reg2str[base]+')'
  97. else
  98. if (index<>R_NO) and (base<>R_NO) then
  99. begin
  100. s:=s+'('+gas_reg2str[base]+','+gas_reg2str[index];
  101. if scalefactor<>0 then
  102. s:=s+','+tostr(scalefactor)+')'
  103. else
  104. s := s+')';
  105. end;
  106. end;
  107. getreferencestring:=s;
  108. end;
  109. function getopstr(const o:toper) : string;
  110. var
  111. hs : string;
  112. begin
  113. case o.typ of
  114. top_reg :
  115. getopstr:=gas_reg2str[o.reg];
  116. top_ref :
  117. getopstr:=getreferencestring(o.ref^);
  118. top_const :
  119. getopstr:='$'+tostr(longint(o.val));
  120. top_symbol :
  121. begin
  122. if assigned(o.sym) then
  123. hs:='$'+o.sym.name
  124. else
  125. hs:='$';
  126. if o.symofs>0 then
  127. hs:=hs+'+'+tostr(o.symofs)
  128. else
  129. if o.symofs<0 then
  130. hs:=hs+tostr(o.symofs)
  131. else
  132. if not(assigned(o.sym)) then
  133. hs:=hs+'0';
  134. getopstr:=hs;
  135. end;
  136. else
  137. internalerror(10001);
  138. end;
  139. end;
  140. function getopstr_jmp(const o:toper) : string;
  141. var
  142. hs : string;
  143. begin
  144. case o.typ of
  145. top_reg :
  146. getopstr_jmp:='*'+gas_reg2str[o.reg];
  147. top_ref :
  148. getopstr_jmp:='*'+getreferencestring(o.ref^);
  149. top_const :
  150. getopstr_jmp:=tostr(longint(o.val));
  151. top_symbol :
  152. begin
  153. hs:=o.sym.name;
  154. if o.symofs>0 then
  155. hs:=hs+'+'+tostr(o.symofs)
  156. else
  157. if o.symofs<0 then
  158. hs:=hs+tostr(o.symofs);
  159. getopstr_jmp:=hs;
  160. end;
  161. else
  162. internalerror(10001);
  163. end;
  164. end;
  165. {****************************************************************************
  166. TI386ATTASMOUTPUT
  167. ****************************************************************************}
  168. procedure T386AttAssembler. WriteInstruction(hp: tai);
  169. var
  170. op : tasmop;
  171. s : string;
  172. sep : char;
  173. calljmp : boolean;
  174. i : integer;
  175. begin
  176. if hp.typ <> ait_instruction then exit;
  177. taicpu(hp).SetOperandOrder(op_att);
  178. op:=taicpu(hp).opcode;
  179. calljmp:=is_calljmp(op);
  180. { call maybe not translated to call }
  181. s:=#9+gas_op2str[op]+cond2str[taicpu(hp).condition];
  182. { suffix needed ? fnstsw,fldcw don't support suffixes
  183. with binutils 2.9.5 under linux }
  184. if (not calljmp) and
  185. (gas_needsuffix[op]<>AttSufNONE) and
  186. (op<>A_FNSTSW) and (op<>A_FSTSW) and
  187. (op<>A_FNSTCW) and (op<>A_FSTCW) and
  188. (op<>A_FLDCW) and not(
  189. (taicpu(hp).oper[0].typ=top_reg) and
  190. (taicpu(hp).oper[0].reg in [R_ST..R_ST7])
  191. ) then
  192. s:=s+gas_opsize2str[taicpu(hp).opsize];
  193. { process operands }
  194. if taicpu(hp).ops<>0 then
  195. begin
  196. { call and jmp need an extra handling }
  197. { this code is only called if jmp isn't a labeled instruction }
  198. { quick hack to overcome a problem with manglednames=255 chars }
  199. if calljmp then
  200. begin
  201. AsmWrite(s+#9);
  202. s:=getopstr_jmp(taicpu(hp).oper[0]);
  203. end
  204. else
  205. begin
  206. for i:=0 to taicpu(hp).ops-1 do
  207. begin
  208. if i=0 then
  209. sep:=#9
  210. else
  211. sep:=',';
  212. s:=s+sep+getopstr(taicpu(hp).oper[i])
  213. end;
  214. end;
  215. end;
  216. AsmWriteLn(s);
  217. end;
  218. {*****************************************************************************
  219. Initialize
  220. *****************************************************************************}
  221. const
  222. as_i386_as_info : tasminfo =
  223. (
  224. id : as_i386_as;
  225. idtxt : 'AS';
  226. asmbin : 'as';
  227. asmcmd : '-o $OBJ $ASM';
  228. supported_target : target_any;
  229. outputbinary: false;
  230. allowdirect : true;
  231. externals : false;
  232. needar : true;
  233. labelprefix_only_inside_procedure : false;
  234. labelprefix : '.L';
  235. comment : '# ';
  236. secnames : ('',
  237. '.text','.data','.bss',
  238. '','','','','','',
  239. '.stab','.stabstr','COMMON')
  240. );
  241. as_i386_as_aout_info : tasminfo =
  242. (
  243. id : as_i386_as_aout;
  244. idtxt : 'AS_AOUT';
  245. asmbin : 'as';
  246. asmcmd : '-o $OBJ $ASM';
  247. supported_target : target_i386_os2;
  248. outputbinary: false;
  249. allowdirect : true;
  250. externals : false;
  251. needar : true;
  252. labelprefix_only_inside_procedure : false;
  253. labelprefix : 'L';
  254. comment : '# ';
  255. secnames : ('',
  256. '.text','.data','.bss',
  257. '','','','','','',
  258. '.stab','.stabstr','COMMON')
  259. );
  260. as_i386_asw_info : tasminfo =
  261. (
  262. id : as_i386_asw;
  263. idtxt : 'ASW';
  264. asmbin : 'asw';
  265. asmcmd : '-o $OBJ $ASM';
  266. supported_target : target_i386_win32;
  267. outputbinary: false;
  268. allowdirect : true;
  269. externals : false;
  270. needar : true;
  271. labelprefix_only_inside_procedure : false;
  272. labelprefix : '.L';
  273. comment : '# ';
  274. secnames : ('',
  275. '.text','.data','.section .bss',
  276. '.section .idata$2','.section .idata$4','.section .idata$5',
  277. '.section .idata$6','.section .idata$7','.section .edata',
  278. '.stab','.stabstr','COMMON')
  279. );
  280. as_i386_aswwdosx_info : tasminfo =
  281. (
  282. id : as_i386_aswdosx;
  283. idtxt : 'ASWDOSX';
  284. asmbin : 'asw';
  285. asmcmd : '-o $OBJ $ASM';
  286. supported_target : target_i386_wdosx;
  287. outputbinary: false;
  288. allowdirect : true;
  289. externals : false;
  290. needar : true;
  291. labelprefix_only_inside_procedure : false;
  292. labelprefix : '.L';
  293. comment : '# ';
  294. secnames : ('',
  295. '.text','.data','.section .bss',
  296. '.section .idata$2','.section .idata$4','.section .idata$5',
  297. '.section .idata$6','.section .idata$7','.section .edata',
  298. '.stab','.stabstr','COMMON')
  299. );
  300. initialization
  301. RegisterAssembler(as_i386_as_info,T386ATTAssembler);
  302. RegisterAssembler(as_i386_as_aout_info,T386ATTAssembler);
  303. RegisterAssembler(as_i386_asw_info,T386ATTAssembler);
  304. RegisterAssembler(as_i386_aswwdosx_info,T386ATTAssembler);
  305. end.
  306. {
  307. $Log$
  308. Revision 1.23 2002-07-01 18:46:29 peter
  309. * internal linker
  310. * reorganized aasm layer
  311. Revision 1.22 2002/05/18 13:34:21 peter
  312. * readded missing revisions
  313. Revision 1.21 2002/05/16 19:46:49 carl
  314. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  315. + try to fix temp allocation (still in ifdef)
  316. + generic constructor calls
  317. + start of tassembler / tmodulebase class cleanup
  318. Revision 1.19 2002/05/12 16:53:16 peter
  319. * moved entry and exitcode to ncgutil and cgobj
  320. * foreach gets extra argument for passing local data to the
  321. iterator function
  322. * -CR checks also class typecasts at runtime by changing them
  323. into as
  324. * fixed compiler to cycle with the -CR option
  325. * fixed stabs with elf writer, finally the global variables can
  326. be watched
  327. * removed a lot of routines from cga unit and replaced them by
  328. calls to cgobj
  329. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  330. u32bit then the other is typecasted also to u32bit without giving
  331. a rangecheck warning/error.
  332. * fixed pascal calling method with reversing also the high tree in
  333. the parast, detected by tcalcst3 test
  334. Revision 1.18 2002/04/15 19:12:10 carl
  335. + target_info.size_of_pointer -> pointer_size
  336. + some cleanup of unused types/variables
  337. * move several constants from cpubase to their specific units
  338. (where they are used)
  339. + att_Reg2str -> gas_reg2str
  340. + int_reg2str -> std_reg2str
  341. Revision 1.17 2002/04/14 16:58:04 carl
  342. + move into aggas most of the stuff non-processor specific
  343. Revision 1.16 2002/04/10 08:07:55 jonas
  344. * fix for the ie9999 under Linux (patch from Peter)
  345. Revision 1.15 2002/04/04 19:06:06 peter
  346. * removed unused units
  347. * use tlocation.size in cg.a_*loc*() routines
  348. Revision 1.14 2002/04/04 18:26:55 carl
  349. + added wdosx patch from Pavel
  350. Revision 1.13 2002/04/02 17:11:33 peter
  351. * tlocation,treference update
  352. * LOC_CONSTANT added for better constant handling
  353. * secondadd splitted in multiple routines
  354. * location_force_reg added for loading a location to a register
  355. of a specified size
  356. * secondassignment parses now first the right and then the left node
  357. (this is compatible with Kylix). This saves a lot of push/pop especially
  358. with string operations
  359. * adapted some routines to use the new cg methods
  360. }