agx86att.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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 i386 AT&T syntax
  19. }
  20. unit agx86att;
  21. {$i fpcdefs.inc}
  22. interface
  23. uses
  24. cclasses,cpubase,
  25. globals,
  26. aasmbase,aasmtai,assemble,aggas;
  27. type
  28. Tx86ATTAssembler=class(TGNUassembler)
  29. private
  30. procedure WriteReference(var ref : treference);
  31. procedure WriteOper(const o:toper);
  32. procedure WriteOper_jmp(const o:toper);
  33. public
  34. procedure WriteInstruction(hp: tai);override;
  35. end;
  36. implementation
  37. uses
  38. cutils,systems,
  39. verbose,
  40. itx86att,
  41. cgbase,
  42. aasmcpu;
  43. {****************************************************************************
  44. TX86ATTASMOUTPUT
  45. ****************************************************************************}
  46. procedure Tx86AttAssembler.WriteReference(var ref : treference);
  47. begin
  48. with ref do
  49. begin
  50. { have we a segment prefix ? }
  51. { These are probably not correctly handled under GAS }
  52. { should be replaced by coding the segment override }
  53. { directly! - DJGPP FAQ }
  54. if segment<>NR_NO then
  55. AsmWrite(gas_regname(segment)+':');
  56. if assigned(symbol) then
  57. AsmWrite(symbol.name);
  58. if offset<0 then
  59. AsmWrite(tostr(offset))
  60. else
  61. if (offset>0) then
  62. begin
  63. if assigned(symbol) then
  64. AsmWrite('+'+tostr(offset))
  65. else
  66. AsmWrite(tostr(offset));
  67. end
  68. else if (index=NR_NO) and (base=NR_NO) and (not assigned(symbol)) then
  69. AsmWrite('0');
  70. if (index<>NR_NO) and (base=NR_NO) then
  71. begin
  72. AsmWrite('(,'+gas_regname(index));
  73. if scalefactor<>0 then
  74. AsmWrite(','+tostr(scalefactor)+')')
  75. else
  76. AsmWrite(')');
  77. end
  78. else
  79. if (index=NR_NO) and (base<>NR_NO) then
  80. AsmWrite('('+gas_regname(base)+')')
  81. else
  82. if (index<>NR_NO) and (base<>NR_NO) then
  83. begin
  84. AsmWrite('('+gas_regname(base)+','+gas_regname(index));
  85. if scalefactor<>0 then
  86. AsmWrite(','+tostr(scalefactor));
  87. AsmWrite(')');
  88. end;
  89. end;
  90. end;
  91. procedure Tx86AttAssembler.WriteOper(const o:toper);
  92. begin
  93. case o.typ of
  94. top_reg :
  95. AsmWrite(gas_regname(o.reg));
  96. top_ref :
  97. WriteReference(o.ref^);
  98. top_const :
  99. AsmWrite('$'+tostr(longint(o.val)));
  100. top_symbol :
  101. begin
  102. AsmWrite('$');
  103. if assigned(o.sym) then
  104. AsmWrite(o.sym.name);
  105. if o.symofs>0 then
  106. AsmWrite('+'+tostr(o.symofs))
  107. else
  108. if o.symofs<0 then
  109. AsmWrite(tostr(o.symofs))
  110. else
  111. if not(assigned(o.sym)) then
  112. AsmWrite('0');
  113. end;
  114. else
  115. internalerror(10001);
  116. end;
  117. end;
  118. procedure Tx86AttAssembler.WriteOper_jmp(const o:toper);
  119. begin
  120. case o.typ of
  121. top_reg :
  122. AsmWrite('*'+gas_regname(o.reg));
  123. top_ref :
  124. begin
  125. AsmWrite('*');
  126. WriteReference(o.ref^);
  127. end;
  128. top_const :
  129. AsmWrite(tostr(longint(o.val)));
  130. top_symbol :
  131. begin
  132. AsmWrite(o.sym.name);
  133. if o.symofs>0 then
  134. AsmWrite('+'+tostr(o.symofs))
  135. else
  136. if o.symofs<0 then
  137. AsmWrite(tostr(o.symofs));
  138. end;
  139. else
  140. internalerror(10001);
  141. end;
  142. end;
  143. procedure Tx86AttAssembler.WriteInstruction(hp: tai);
  144. var
  145. op : tasmop;
  146. calljmp : boolean;
  147. i : integer;
  148. begin
  149. if hp.typ <> ait_instruction then
  150. exit;
  151. taicpu(hp).SetOperandOrder(op_att);
  152. op:=taicpu(hp).opcode;
  153. calljmp:=is_calljmp(op);
  154. { call maybe not translated to call }
  155. AsmWrite(#9+gas_op2str[op]+cond2str[taicpu(hp).condition]);
  156. { suffix needed ? fnstsw,fldcw don't support suffixes
  157. with binutils 2.9.5 under linux }
  158. { if (Taicpu(hp).oper[0]^.typ=top_reg) and
  159. (Taicpu(hp).oper[0]^.reg.enum>lastreg) then
  160. internalerror(200301081);}
  161. if (not calljmp) and
  162. (gas_needsuffix[op]<>AttSufNONE) and
  163. (op<>A_FNSTSW) and
  164. (op<>A_FSTSW) and
  165. (op<>A_FNSTCW) and
  166. (op<>A_FSTCW) and
  167. (op<>A_FLDCW) and
  168. not(
  169. (taicpu(hp).ops<>0) and
  170. (taicpu(hp).oper[0]^.typ=top_reg) and
  171. (getregtype(taicpu(hp).oper[0]^.reg)=R_FPUREGISTER)
  172. ) then
  173. AsmWrite(gas_opsize2str[taicpu(hp).opsize]);
  174. { process operands }
  175. if taicpu(hp).ops<>0 then
  176. begin
  177. if calljmp then
  178. begin
  179. AsmWrite(#9);
  180. WriteOper_jmp(taicpu(hp).oper[0]^);
  181. end
  182. else
  183. begin
  184. for i:=0 to taicpu(hp).ops-1 do
  185. begin
  186. if i=0 then
  187. AsmWrite(#9)
  188. else
  189. AsmWrite(',');
  190. WriteOper(taicpu(hp).oper[i]^);
  191. end;
  192. end;
  193. end;
  194. AsmLn;
  195. end;
  196. {*****************************************************************************
  197. Initialize
  198. *****************************************************************************}
  199. const
  200. {$ifdef x86_64}
  201. as_x86_64_as_info : tasminfo =
  202. (
  203. id : as_x86_64_as;
  204. idtxt : 'AS';
  205. asmbin : 'as';
  206. asmcmd : '-o $OBJ $ASM';
  207. supported_target : system_any;
  208. outputbinary: false;
  209. allowdirect : true;
  210. needar : true;
  211. labelprefix_only_inside_procedure : false;
  212. labelprefix : '.L';
  213. comment : '# ';
  214. secnames : ('',
  215. '.text','.data','.section .bss',
  216. '.section .idata$2','.section .idata$4','.section .idata$5',
  217. '.section .idata$6','.section .idata$7','.section .edata',
  218. '.stab','.stabstr','COMMON')
  219. );
  220. {$else x86_64}
  221. as_i386_as_info : tasminfo =
  222. (
  223. id : as_gas;
  224. idtxt : 'AS';
  225. asmbin : 'as';
  226. asmcmd : '-o $OBJ $ASM';
  227. supported_target : system_any;
  228. outputbinary: false;
  229. allowdirect : true;
  230. needar : true;
  231. labelprefix_only_inside_procedure : false;
  232. labelprefix : '.L';
  233. comment : '# ';
  234. secnames : ('',
  235. '.text','.data','.section .bss',
  236. '.section .idata$2','.section .idata$4','.section .idata$5',
  237. '.section .idata$6','.section .idata$7','.section .edata',
  238. '.stab','.stabstr','COMMON')
  239. );
  240. as_i386_as_aout_info : tasminfo =
  241. (
  242. id : as_i386_as_aout;
  243. idtxt : 'AS_AOUT';
  244. asmbin : 'as';
  245. asmcmd : '-o $OBJ $ASM';
  246. supported_target : system_any;
  247. { supported_target : system_i386_emx;}
  248. outputbinary: false;
  249. allowdirect : true;
  250. needar : true;
  251. labelprefix_only_inside_procedure : false;
  252. labelprefix : 'L';
  253. comment : '# ';
  254. secnames : ('',
  255. '.text','.data','.bss',
  256. '','','','','','',
  257. '.stab','.stabstr','COMMON')
  258. );
  259. {$endif x86_64}
  260. initialization
  261. {$ifdef x86_64}
  262. RegisterAssembler(as_x86_64_as_info,Tx86ATTAssembler);
  263. {$else x86_64}
  264. RegisterAssembler(as_i386_as_info,Tx86ATTAssembler);
  265. RegisterAssembler(as_i386_as_aout_info,Tx86ATTAssembler);
  266. {$endif x86_64}
  267. end.
  268. {
  269. $Log$
  270. Revision 1.10 2003-10-28 18:46:49 peter
  271. * fix crash with ops=0
  272. Revision 1.9 2003/10/21 15:15:36 peter
  273. * taicpu_abstract.oper[] changed to pointers
  274. Revision 1.8 2003/10/02 21:18:06 peter
  275. * remove asw
  276. Revision 1.7 2003/10/01 20:34:50 peter
  277. * procinfo unit contains tprocinfo
  278. * cginfo renamed to cgbase
  279. * moved cgmessage to verbose
  280. * fixed ppc and sparc compiles
  281. Revision 1.6 2003/09/23 17:56:06 peter
  282. * locals and paras are allocated in the code generation
  283. * tvarsym.localloc contains the location of para/local when
  284. generating code for the current procedure
  285. Revision 1.5 2003/09/03 15:55:02 peter
  286. * NEWRA branch merged
  287. Revision 1.4.2.1 2003/08/31 15:46:26 peter
  288. * more updates for tregister
  289. Revision 1.4 2003/08/18 11:49:47 daniel
  290. * Made ATT asm writer work with -sr
  291. Revision 1.3 2003/05/28 23:18:31 florian
  292. * started to fix and clean up the sparc port
  293. Revision 1.2 2003/05/22 21:33:31 peter
  294. * removed some unit dependencies
  295. Revision 1.1 2003/04/25 12:04:31 florian
  296. * merged agx64att and ag386att to x86/agx86att
  297. Revision 1.31 2003/03/23 23:33:10 hajny
  298. + emx target added
  299. Revision 1.30 2003/02/19 22:00:15 daniel
  300. * Code generator converted to new register notation
  301. - Horribily outdated todo.txt removed
  302. Revision 1.29 2003/01/08 18:43:57 daniel
  303. * Tregister changed into a record
  304. Revision 1.28 2003/01/05 13:36:53 florian
  305. * x86-64 compiles
  306. + very basic support for float128 type (x86-64 only)
  307. Revision 1.27 2002/12/24 18:10:34 peter
  308. * Long symbol names support
  309. Revision 1.26 2002/08/12 15:08:40 carl
  310. + stab register indexes for powerpc (moved from gdb to cpubase)
  311. + tprocessor enumeration moved to cpuinfo
  312. + linker in target_info is now a class
  313. * many many updates for m68k (will soon start to compile)
  314. - removed some ifdef or correct them for correct cpu
  315. Revision 1.25 2002/07/26 21:15:42 florian
  316. * rewrote the system handling
  317. Revision 1.24 2002/07/07 09:52:33 florian
  318. * powerpc target fixed, very simple units can be compiled
  319. * some basic stuff for better callparanode handling, far from being finished
  320. Revision 1.23 2002/07/01 18:46:29 peter
  321. * internal linker
  322. * reorganized aasm layer
  323. Revision 1.22 2002/05/18 13:34:21 peter
  324. * readded missing revisions
  325. Revision 1.21 2002/05/16 19:46:49 carl
  326. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  327. + try to fix temp allocation (still in ifdef)
  328. + generic constructor calls
  329. + start of tassembler / tmodulebase class cleanup
  330. Revision 1.19 2002/05/12 16:53:16 peter
  331. * moved entry and exitcode to ncgutil and cgobj
  332. * foreach gets extra argument for passing local data to the
  333. iterator function
  334. * -CR checks also class typecasts at runtime by changing them
  335. into as
  336. * fixed compiler to cycle with the -CR option
  337. * fixed stabs with elf writer, finally the global variables can
  338. be watched
  339. * removed a lot of routines from cga unit and replaced them by
  340. calls to cgobj
  341. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  342. u32bit then the other is typecasted also to u32bit without giving
  343. a rangecheck warning/error.
  344. * fixed pascal calling method with reversing also the high tree in
  345. the parast, detected by tcalcst3 test
  346. Revision 1.18 2002/04/15 19:12:10 carl
  347. + target_info.size_of_pointer -> pointer_size
  348. + some cleanup of unused types/variables
  349. * move several constants from cpubase to their specific units
  350. (where they are used)
  351. + att_Reg2str -> gas_reg2str
  352. + int_reg2str -> std_reg2str
  353. Revision 1.17 2002/04/14 16:58:04 carl
  354. + move into aggas most of the stuff non-processor specific
  355. Revision 1.16 2002/04/10 08:07:55 jonas
  356. * fix for the ie9999 under Linux (patch from Peter)
  357. Revision 1.15 2002/04/04 19:06:06 peter
  358. * removed unused units
  359. * use tlocation.size in cg.a_*loc*() routines
  360. Revision 1.14 2002/04/04 18:26:55 carl
  361. + added wdosx patch from Pavel
  362. Revision 1.13 2002/04/02 17:11:33 peter
  363. * tlocation,treference update
  364. * LOC_CONSTANT added for better constant handling
  365. * secondadd splitted in multiple routines
  366. * location_force_reg added for loading a location to a register
  367. of a specified size
  368. * secondassignment parses now first the right and then the left node
  369. (this is compatible with Kylix). This saves a lot of push/pop especially
  370. with string operations
  371. * adapted some routines to use the new cg methods
  372. }