agx86att.pas 14 KB

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