agx86att.pas 14 KB

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