agx86att.pas 18 KB

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