ag386att.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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 ag386att;
  21. {$i fpcdefs.inc}
  22. interface
  23. uses
  24. cclasses,cpubase,
  25. globals,
  26. aasmbase,aasmtai,aasmcpu,assemble,aggas;
  27. type
  28. T386ATTAssembler=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. TAttSuffix = (AttSufNONE,AttSufINT,AttSufFPU,AttSufFPUint);
  37. const
  38. gas_op2str:op2strtable={$i i386att.inc}
  39. gas_needsuffix:array[tasmop] of TAttSuffix={$i i386atts.inc}
  40. gas_reg2str : reg2strtable = ('',
  41. '%eax','%ecx','%edx','%ebx','%esp','%ebp','%esi','%edi',
  42. '%ax','%cx','%dx','%bx','%sp','%bp','%si','%di',
  43. '%al','%cl','%dl','%bl','%ah','%ch','%bh','%dh',
  44. '%cs','%ds','%es','%ss','%fs','%gs',
  45. '%st','%st(0)','%st(1)','%st(2)','%st(3)','%st(4)','%st(5)','%st(6)','%st(7)',
  46. '%dr0','%dr1','%dr2','%dr3','%dr6','%dr7',
  47. '%cr0','%cr2','%cr3','%cr4',
  48. '%tr3','%tr4','%tr5','%tr6','%tr7',
  49. '%mm0','%mm1','%mm2','%mm3','%mm4','%mm5','%mm6','%mm7',
  50. '%xmm0','%xmm1','%xmm2','%xmm3','%xmm4','%xmm5','%xmm6','%xmm7'
  51. );
  52. regname_count=45;
  53. regname_count_bsstart=32;
  54. gas_regname2regnum:array[0..regname_count-1] of regname2regnumrec=(
  55. (name:'%ah'; number:NR_AH),
  56. (name:'%al'; number:NR_AL),
  57. (name:'%ax'; number:NR_AX),
  58. (name:'%bh'; number:NR_BH),
  59. (name:'%bl'; number:NR_BL),
  60. (name:'%bp'; number:NR_BP),
  61. (name:'%bx'; number:NR_BX),
  62. (name:'%ch'; number:NR_CH),
  63. (name:'%cl'; number:NR_CL),
  64. (name:'%cs'; number:NR_CS),
  65. (name:'%cr0'; number:NR_CR0),
  66. (name:'%cr2'; number:NR_CR2),
  67. (name:'%cr3'; number:NR_CR3),
  68. (name:'%cr4'; number:NR_CR4),
  69. (name:'%cx'; number:NR_CX),
  70. (name:'%dh'; number:NR_DH),
  71. (name:'%dl'; number:NR_DL),
  72. (name:'%di'; number:NR_DI),
  73. (name:'%dr0'; number:NR_DR0),
  74. (name:'%dr1'; number:NR_DR1),
  75. (name:'%dr2'; number:NR_DR2),
  76. (name:'%dr3'; number:NR_DR3),
  77. (name:'%dr6'; number:NR_DR6),
  78. (name:'%dr7'; number:NR_DR7),
  79. (name:'%ds'; number:NR_DS),
  80. (name:'%dx'; number:NR_DX),
  81. (name:'%eax'; number:NR_EAX),
  82. (name:'%ebp'; number:NR_EBP),
  83. (name:'%ebx'; number:NR_EBX),
  84. (name:'%ecx'; number:NR_ECX),
  85. (name:'%edi'; number:NR_EDI),
  86. (name:'%edx'; number:NR_EDX),
  87. (name:'%es'; number:NR_ES),
  88. (name:'%esi'; number:NR_ESI),
  89. (name:'%esp'; number:NR_ESP),
  90. (name:'%fs'; number:NR_FS),
  91. (name:'%gs'; number:NR_GS),
  92. (name:'%si'; number:NR_SI),
  93. (name:'%sp'; number:NR_SP),
  94. (name:'%ss'; number:NR_SS),
  95. (name:'%tr3'; number:NR_DR0),
  96. (name:'%tr4'; number:NR_DR1),
  97. (name:'%tr5'; number:NR_DR2),
  98. (name:'%tr6'; number:NR_DR6),
  99. (name:'%tr7'; number:NR_DR7)
  100. );
  101. gas_opsize2str : array[topsize] of string[2] = ('',
  102. 'b','w','l','bw','bl','wl',
  103. 's','l','q',
  104. 's','l','t','d','q','v','',
  105. '','',''
  106. );
  107. function gas_regnum_search(const s:string):Tnewregister;
  108. implementation
  109. uses
  110. cutils,systems,
  111. verbose;
  112. {****************************************************************************
  113. TI386ATTASMOUTPUT
  114. ****************************************************************************}
  115. procedure T386AttAssembler.WriteReference(var ref : treference);
  116. begin
  117. with ref do
  118. begin
  119. inc(offset,offsetfixup);
  120. offsetfixup:=0;
  121. { have we a segment prefix ? }
  122. { These are probably not correctly handled under GAS }
  123. { should be replaced by coding the segment override }
  124. { directly! - DJGPP FAQ }
  125. if segment.enum>lastreg then
  126. internalerror(200301081);
  127. if base.enum>lastreg then
  128. internalerror(200301081);
  129. if index.enum>lastreg then
  130. internalerror(200301081);
  131. if segment.enum<>R_NO then
  132. AsmWrite(gas_reg2str[segment.enum]+':');
  133. if assigned(symbol) then
  134. AsmWrite(symbol.name);
  135. if offset<0 then
  136. AsmWrite(tostr(offset))
  137. else
  138. if (offset>0) then
  139. begin
  140. if assigned(symbol) then
  141. AsmWrite('+'+tostr(offset))
  142. else
  143. AsmWrite(tostr(offset));
  144. end
  145. else if (index.enum=R_NO) and (base.enum=R_NO) and not assigned(symbol) then
  146. AsmWrite('0');
  147. if (index.enum<>R_NO) and (base.enum=R_NO) then
  148. begin
  149. AsmWrite('(,'+gas_reg2str[index.enum]);
  150. if scalefactor<>0 then
  151. AsmWrite(','+tostr(scalefactor)+')')
  152. else
  153. AsmWrite(')');
  154. end
  155. else
  156. if (index.enum=R_NO) and (base.enum<>R_NO) then
  157. AsmWrite('('+gas_reg2str[base.enum]+')')
  158. else
  159. if (index.enum<>R_NO) and (base.enum<>R_NO) then
  160. begin
  161. AsmWrite('('+gas_reg2str[base.enum]+','+gas_reg2str[index.enum]);
  162. if scalefactor<>0 then
  163. AsmWrite(','+tostr(scalefactor));
  164. AsmWrite(')');
  165. end;
  166. end;
  167. end;
  168. procedure T386AttAssembler.WriteOper(const o:toper);
  169. begin
  170. case o.typ of
  171. top_reg :
  172. begin
  173. if o.reg.enum>lastreg then
  174. internalerror(200301081);
  175. AsmWrite(gas_reg2str[o.reg.enum]);
  176. end;
  177. top_ref :
  178. WriteReference(o.ref^);
  179. top_const :
  180. AsmWrite('$'+tostr(longint(o.val)));
  181. top_symbol :
  182. begin
  183. AsmWrite('$');
  184. if assigned(o.sym) then
  185. AsmWrite(o.sym.name);
  186. if o.symofs>0 then
  187. AsmWrite('+'+tostr(o.symofs))
  188. else
  189. if o.symofs<0 then
  190. AsmWrite(tostr(o.symofs))
  191. else
  192. if not(assigned(o.sym)) then
  193. AsmWrite('0');
  194. end;
  195. else
  196. internalerror(10001);
  197. end;
  198. end;
  199. procedure T386AttAssembler.WriteOper_jmp(const o:toper);
  200. begin
  201. case o.typ of
  202. top_reg :
  203. begin
  204. if o.reg.enum>lastreg then
  205. internalerror(200301081);
  206. AsmWrite('*'+gas_reg2str[o.reg.enum]);
  207. end;
  208. top_ref :
  209. begin
  210. AsmWrite('*');
  211. WriteReference(o.ref^);
  212. end;
  213. top_const :
  214. AsmWrite(tostr(longint(o.val)));
  215. top_symbol :
  216. begin
  217. AsmWrite(o.sym.name);
  218. if o.symofs>0 then
  219. AsmWrite('+'+tostr(o.symofs))
  220. else
  221. if o.symofs<0 then
  222. AsmWrite(tostr(o.symofs));
  223. end;
  224. else
  225. internalerror(10001);
  226. end;
  227. end;
  228. procedure T386AttAssembler.WriteInstruction(hp: tai);
  229. var
  230. op : tasmop;
  231. calljmp : boolean;
  232. i : integer;
  233. begin
  234. if hp.typ <> ait_instruction then
  235. exit;
  236. taicpu(hp).SetOperandOrder(op_att);
  237. op:=taicpu(hp).opcode;
  238. calljmp:=is_calljmp(op);
  239. { call maybe not translated to call }
  240. AsmWrite(#9+gas_op2str[op]+cond2str[taicpu(hp).condition]);
  241. { suffix needed ? fnstsw,fldcw don't support suffixes
  242. with binutils 2.9.5 under linux }
  243. if (Taicpu(hp).oper[0].typ=top_reg) and
  244. (Taicpu(hp).oper[0].reg.enum>lastreg) then
  245. internalerror(200301081);
  246. if (not calljmp) and
  247. (gas_needsuffix[op]<>AttSufNONE) and
  248. (op<>A_FNSTSW) and (op<>A_FSTSW) and
  249. (op<>A_FNSTCW) and (op<>A_FSTCW) and
  250. (op<>A_FLDCW) and not(
  251. (taicpu(hp).oper[0].typ=top_reg) and
  252. (taicpu(hp).oper[0].reg.enum in [R_ST..R_ST7])
  253. ) then
  254. AsmWrite(gas_opsize2str[taicpu(hp).opsize]);
  255. { process operands }
  256. if taicpu(hp).ops<>0 then
  257. begin
  258. if calljmp then
  259. begin
  260. AsmWrite(#9);
  261. WriteOper_jmp(taicpu(hp).oper[0]);
  262. end
  263. else
  264. begin
  265. for i:=0 to taicpu(hp).ops-1 do
  266. begin
  267. if i=0 then
  268. AsmWrite(#9)
  269. else
  270. AsmWrite(',');
  271. WriteOper(taicpu(hp).oper[i]);
  272. end;
  273. end;
  274. end;
  275. AsmLn;
  276. end;
  277. function gas_regnum_search(const s:string):Tnewregister;
  278. {Searches the register number that belongs to the register in s.
  279. s must be in uppercase!.}
  280. var i,p:byte;
  281. begin
  282. {Binary search.}
  283. p:=0;
  284. i:=regname_count_bsstart;
  285. while i<>0 do
  286. begin
  287. if (p+i<regname_count) and (upper(gas_regname2regnum[p+i].name)<=s) then
  288. p:=p+i;
  289. i:=i shr 1;
  290. end;
  291. if upper(gas_regname2regnum[p].name)=s then
  292. gas_regnum_search:=gas_regname2regnum[p].number
  293. else
  294. gas_regnum_search:=NR_NO;
  295. end;
  296. {*****************************************************************************
  297. Initialize
  298. *****************************************************************************}
  299. const
  300. as_i386_as_info : tasminfo =
  301. (
  302. id : as_gas;
  303. idtxt : 'AS';
  304. asmbin : 'as';
  305. asmcmd : '-o $OBJ $ASM';
  306. supported_target : system_any;
  307. outputbinary: false;
  308. allowdirect : true;
  309. needar : true;
  310. labelprefix_only_inside_procedure : false;
  311. labelprefix : '.L';
  312. comment : '# ';
  313. secnames : ('',
  314. '.text','.data','.bss',
  315. '','','','','','',
  316. '.stab','.stabstr','COMMON')
  317. );
  318. as_i386_as_aout_info : tasminfo =
  319. (
  320. id : as_i386_as_aout;
  321. idtxt : 'AS_AOUT';
  322. asmbin : 'as';
  323. asmcmd : '-o $OBJ $ASM';
  324. supported_target : system_i386_os2;
  325. outputbinary: false;
  326. allowdirect : true;
  327. needar : true;
  328. labelprefix_only_inside_procedure : false;
  329. labelprefix : 'L';
  330. comment : '# ';
  331. secnames : ('',
  332. '.text','.data','.bss',
  333. '','','','','','',
  334. '.stab','.stabstr','COMMON')
  335. );
  336. as_i386_asw_info : tasminfo =
  337. (
  338. id : as_i386_asw;
  339. idtxt : 'ASW';
  340. asmbin : 'asw';
  341. asmcmd : '-o $OBJ $ASM';
  342. supported_target : system_i386_win32;
  343. outputbinary: false;
  344. allowdirect : true;
  345. needar : true;
  346. labelprefix_only_inside_procedure : false;
  347. labelprefix : '.L';
  348. comment : '# ';
  349. secnames : ('',
  350. '.text','.data','.section .bss',
  351. '.section .idata$2','.section .idata$4','.section .idata$5',
  352. '.section .idata$6','.section .idata$7','.section .edata',
  353. '.stab','.stabstr','COMMON')
  354. );
  355. initialization
  356. RegisterAssembler(as_i386_as_info,T386ATTAssembler);
  357. RegisterAssembler(as_i386_as_aout_info,T386ATTAssembler);
  358. RegisterAssembler(as_i386_asw_info,T386ATTAssembler);
  359. end.
  360. {
  361. $Log$
  362. Revision 1.30 2003-02-19 22:00:15 daniel
  363. * Code generator converted to new register notation
  364. - Horribily outdated todo.txt removed
  365. Revision 1.29 2003/01/08 18:43:57 daniel
  366. * Tregister changed into a record
  367. Revision 1.28 2003/01/05 13:36:53 florian
  368. * x86-64 compiles
  369. + very basic support for float128 type (x86-64 only)
  370. Revision 1.27 2002/12/24 18:10:34 peter
  371. * Long symbol names support
  372. Revision 1.26 2002/08/12 15:08:40 carl
  373. + stab register indexes for powerpc (moved from gdb to cpubase)
  374. + tprocessor enumeration moved to cpuinfo
  375. + linker in target_info is now a class
  376. * many many updates for m68k (will soon start to compile)
  377. - removed some ifdef or correct them for correct cpu
  378. Revision 1.25 2002/07/26 21:15:42 florian
  379. * rewrote the system handling
  380. Revision 1.24 2002/07/07 09:52:33 florian
  381. * powerpc target fixed, very simple units can be compiled
  382. * some basic stuff for better callparanode handling, far from being finished
  383. Revision 1.23 2002/07/01 18:46:29 peter
  384. * internal linker
  385. * reorganized aasm layer
  386. Revision 1.22 2002/05/18 13:34:21 peter
  387. * readded missing revisions
  388. Revision 1.21 2002/05/16 19:46:49 carl
  389. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  390. + try to fix temp allocation (still in ifdef)
  391. + generic constructor calls
  392. + start of tassembler / tmodulebase class cleanup
  393. Revision 1.19 2002/05/12 16:53:16 peter
  394. * moved entry and exitcode to ncgutil and cgobj
  395. * foreach gets extra argument for passing local data to the
  396. iterator function
  397. * -CR checks also class typecasts at runtime by changing them
  398. into as
  399. * fixed compiler to cycle with the -CR option
  400. * fixed stabs with elf writer, finally the global variables can
  401. be watched
  402. * removed a lot of routines from cga unit and replaced them by
  403. calls to cgobj
  404. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  405. u32bit then the other is typecasted also to u32bit without giving
  406. a rangecheck warning/error.
  407. * fixed pascal calling method with reversing also the high tree in
  408. the parast, detected by tcalcst3 test
  409. Revision 1.18 2002/04/15 19:12:10 carl
  410. + target_info.size_of_pointer -> pointer_size
  411. + some cleanup of unused types/variables
  412. * move several constants from cpubase to their specific units
  413. (where they are used)
  414. + att_Reg2str -> gas_reg2str
  415. + int_reg2str -> std_reg2str
  416. Revision 1.17 2002/04/14 16:58:04 carl
  417. + move into aggas most of the stuff non-processor specific
  418. Revision 1.16 2002/04/10 08:07:55 jonas
  419. * fix for the ie9999 under Linux (patch from Peter)
  420. Revision 1.15 2002/04/04 19:06:06 peter
  421. * removed unused units
  422. * use tlocation.size in cg.a_*loc*() routines
  423. Revision 1.14 2002/04/04 18:26:55 carl
  424. + added wdosx patch from Pavel
  425. Revision 1.13 2002/04/02 17:11:33 peter
  426. * tlocation,treference update
  427. * LOC_CONSTANT added for better constant handling
  428. * secondadd splitted in multiple routines
  429. * location_force_reg added for loading a location to a register
  430. of a specified size
  431. * secondassignment parses now first the right and then the left node
  432. (this is compatible with Kylix). This saves a lot of push/pop especially
  433. with string operations
  434. * adapted some routines to use the new cg methods
  435. }