agx86att.pas 14 KB

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