ag386att.pas 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 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. unit ag386att;
  19. {$i defines.inc}
  20. interface
  21. uses
  22. cobjects,
  23. globals,
  24. aasm,assemble;
  25. type
  26. T386ATTAssembler=class(texternalassembler)
  27. procedure WriteTree(p:TAAsmoutput);override;
  28. procedure WriteAsmList;override;
  29. {$ifdef GDB}
  30. procedure WriteFileLineInfo(var fileinfo : tfileposinfo);
  31. procedure WriteFileEndInfo;
  32. {$endif}
  33. end;
  34. implementation
  35. uses
  36. {$ifdef Delphi}
  37. sysutils,
  38. dmisc,
  39. {$else Delphi}
  40. strings,
  41. dos,
  42. {$endif Delphi}
  43. cutils,globtype,systems,
  44. fmodule,finput,verbose,cpubase,cpuasm
  45. {$ifdef GDB}
  46. ,gdb
  47. {$endif GDB}
  48. ;
  49. const
  50. line_length = 70;
  51. var
  52. {$ifdef GDB}
  53. n_line : byte; { different types of source lines }
  54. linecount,
  55. includecount : longint;
  56. funcname : pchar;
  57. stabslastfileinfo : tfileposinfo;
  58. {$endif}
  59. lastsec : tsection; { last section type written }
  60. lastfileinfo : tfileposinfo;
  61. infile,
  62. lastinfile : tinputfile;
  63. symendcount : longint;
  64. function fixline(s:string):string;
  65. {
  66. return s with all leading and ending spaces and tabs removed
  67. }
  68. var
  69. i,j,k : longint;
  70. begin
  71. i:=length(s);
  72. while (i>0) and (s[i] in [#9,' ']) do
  73. dec(i);
  74. j:=1;
  75. while (j<i) and (s[j] in [#9,' ']) do
  76. inc(j);
  77. for k:=j to i do
  78. if s[k] in [#0..#31,#127..#255] then
  79. s[k]:='.';
  80. fixline:=Copy(s,j,i-j+1);
  81. end;
  82. function single2str(d : single) : string;
  83. var
  84. hs : string;
  85. begin
  86. str(d,hs);
  87. { replace space with + }
  88. if hs[1]=' ' then
  89. hs[1]:='+';
  90. single2str:='0d'+hs
  91. end;
  92. function double2str(d : double) : string;
  93. var
  94. hs : string;
  95. begin
  96. str(d,hs);
  97. { replace space with + }
  98. if hs[1]=' ' then
  99. hs[1]:='+';
  100. double2str:='0d'+hs
  101. end;
  102. function extended2str(e : extended) : string;
  103. var
  104. hs : string;
  105. begin
  106. str(e,hs);
  107. { replace space with + }
  108. if hs[1]=' ' then
  109. hs[1]:='+';
  110. extended2str:='0d'+hs
  111. end;
  112. type
  113. pdouble = ^double;
  114. function comp2str(d : bestreal) : string;
  115. var
  116. c : comp;
  117. dd : pdouble;
  118. begin
  119. {$ifdef FPC}
  120. c:=comp(d);
  121. {$else}
  122. c:=d;
  123. {$endif}
  124. dd:=pdouble(@c); { this makes a bitwise copy of c into a double }
  125. comp2str:=double2str(dd^);
  126. end;
  127. function getreferencestring(var ref : treference) : string;
  128. var
  129. s : string;
  130. begin
  131. if ref.is_immediate then
  132. begin
  133. internalerror(1000101);
  134. exit;
  135. end
  136. else
  137. begin
  138. with ref do
  139. begin
  140. inc(offset,offsetfixup);
  141. offsetfixup:=0;
  142. { have we a segment prefix ? }
  143. { These are probably not correctly handled under GAS }
  144. { should be replaced by coding the segment override }
  145. { directly! - DJGPP FAQ }
  146. if segment<>R_NO then
  147. s:=att_reg2str[segment]+':'
  148. else
  149. s:='';
  150. if assigned(symbol) then
  151. s:=s+symbol^.name;
  152. if offset<0 then
  153. s:=s+tostr(offset)
  154. else
  155. if (offset>0) then
  156. begin
  157. if assigned(symbol) then
  158. s:=s+'+'+tostr(offset)
  159. else
  160. s:=s+tostr(offset);
  161. end;
  162. if (index<>R_NO) and (base=R_NO) then
  163. Begin
  164. s:=s+'(,'+att_reg2str[index];
  165. if scalefactor<>0 then
  166. s:=s+','+tostr(scalefactor)+')'
  167. else
  168. s:=s+')';
  169. end
  170. else
  171. if (index=R_NO) and (base<>R_NO) then
  172. s:=s+'('+att_reg2str[base]+')'
  173. else
  174. if (index<>R_NO) and (base<>R_NO) then
  175. Begin
  176. s:=s+'('+att_reg2str[base]+','+att_reg2str[index];
  177. if scalefactor<>0 then
  178. s:=s+','+tostr(scalefactor)+')'
  179. else
  180. s := s+')';
  181. end;
  182. end;
  183. end;
  184. getreferencestring:=s;
  185. end;
  186. function getopstr(const o:toper) : string;
  187. var
  188. hs : string;
  189. begin
  190. case o.typ of
  191. top_reg :
  192. getopstr:=att_reg2str[o.reg];
  193. top_ref :
  194. getopstr:=getreferencestring(o.ref^);
  195. top_const :
  196. getopstr:='$'+tostr(o.val);
  197. top_symbol :
  198. begin
  199. if assigned(o.sym) then
  200. hs:='$'+o.sym^.name
  201. else
  202. hs:='$';
  203. if o.symofs>0 then
  204. hs:=hs+'+'+tostr(o.symofs)
  205. else
  206. if o.symofs<0 then
  207. hs:=hs+tostr(o.symofs)
  208. else
  209. if not(assigned(o.sym)) then
  210. hs:=hs+'0';
  211. getopstr:=hs;
  212. end;
  213. else
  214. internalerror(10001);
  215. end;
  216. end;
  217. function getopstr_jmp(const o:toper) : string;
  218. var
  219. hs : string;
  220. begin
  221. case o.typ of
  222. top_reg :
  223. getopstr_jmp:='*'+att_reg2str[o.reg];
  224. top_ref :
  225. getopstr_jmp:='*'+getreferencestring(o.ref^);
  226. top_const :
  227. getopstr_jmp:=tostr(o.val);
  228. top_symbol :
  229. begin
  230. hs:=o.sym^.name;
  231. if o.symofs>0 then
  232. hs:=hs+'+'+tostr(o.symofs)
  233. else
  234. if o.symofs<0 then
  235. hs:=hs+tostr(o.symofs);
  236. getopstr_jmp:=hs;
  237. end;
  238. else
  239. internalerror(10001);
  240. end;
  241. end;
  242. {****************************************************************************
  243. TI386ATTASMOUTPUT
  244. ****************************************************************************}
  245. const
  246. ait_const2str : array[ait_const_32bit..ait_const_8bit] of string[8]=
  247. (#9'.long'#9,#9'.short'#9,#9'.byte'#9);
  248. function ait_section2str(s:tsection):string;
  249. begin
  250. ait_section2str:=target_asm.secnames[s];
  251. {$ifdef GDB}
  252. { this is needed for line info in data }
  253. funcname:=nil;
  254. case s of
  255. sec_code : n_line:=n_textline;
  256. sec_data : n_line:=n_dataline;
  257. sec_bss : n_line:=n_bssline;
  258. else n_line:=n_dataline;
  259. end;
  260. {$endif GDB}
  261. LastSec:=s;
  262. end;
  263. {$ifdef GDB}
  264. procedure T386ATTAssembler.WriteFileLineInfo(var fileinfo : tfileposinfo);
  265. var
  266. curr_n : byte;
  267. begin
  268. if not ((cs_debuginfo in aktmoduleswitches) or
  269. (cs_gdb_lineinfo in aktglobalswitches)) then
  270. exit;
  271. { file changed ? (must be before line info) }
  272. if (fileinfo.fileindex<>0) and
  273. (stabslastfileinfo.fileindex<>fileinfo.fileindex) then
  274. begin
  275. infile:=current_module.sourcefiles.get_file(fileinfo.fileindex);
  276. if assigned(infile) then
  277. begin
  278. if includecount=0 then
  279. curr_n:=n_sourcefile
  280. else
  281. curr_n:=n_includefile;
  282. if (infile.path^<>'') then
  283. begin
  284. AsmWriteLn(#9'.stabs "'+lower(BsToSlash(FixPath(infile.path^,false)))+'",'+
  285. tostr(curr_n)+',0,0,'+'Ltext'+ToStr(IncludeCount));
  286. end;
  287. AsmWriteLn(#9'.stabs "'+lower(FixFileName(infile.name^))+'",'+
  288. tostr(curr_n)+',0,0,'+'Ltext'+ToStr(IncludeCount));
  289. AsmWriteLn('Ltext'+ToStr(IncludeCount)+':');
  290. inc(includecount);
  291. end;
  292. end;
  293. { line changed ? }
  294. if (stabslastfileinfo.line<>fileinfo.line) and (fileinfo.line<>0) then
  295. begin
  296. if (n_line=n_textline) and assigned(funcname) and
  297. (target_os.use_function_relative_addresses) then
  298. begin
  299. AsmWriteLn(target_asm.labelprefix+'l'+tostr(linecount)+':');
  300. AsmWrite(#9'.stabn '+tostr(n_line)+',0,'+tostr(fileinfo.line)+','+
  301. target_asm.labelprefix+'l'+tostr(linecount)+' - ');
  302. AsmWritePChar(FuncName);
  303. AsmLn;
  304. inc(linecount);
  305. end
  306. else
  307. AsmWriteLn(#9'.stabd'#9+tostr(n_line)+',0,'+tostr(fileinfo.line));
  308. end;
  309. stabslastfileinfo:=fileinfo;
  310. end;
  311. procedure T386ATTAssembler.WriteFileEndInfo;
  312. begin
  313. if not ((cs_debuginfo in aktmoduleswitches) or
  314. (cs_gdb_lineinfo in aktglobalswitches)) then
  315. exit;
  316. AsmLn;
  317. AsmWriteLn(ait_section2str(sec_code));
  318. AsmWriteLn(#9'.stabs "",'+tostr(n_sourcefile)+',0,0,Letext');
  319. AsmWriteLn('Letext:');
  320. end;
  321. {$endif GDB}
  322. procedure T386ATTAssembler.WriteTree(p:TAAsmoutput);
  323. const
  324. allocstr : array[boolean] of string[10]=(' released',' allocated');
  325. nolinetai =[ait_label,
  326. ait_regalloc,ait_tempalloc,
  327. ait_stabn,ait_stabs,ait_section,
  328. ait_cut,ait_marker,ait_align,ait_stab_function_name];
  329. type
  330. t80bitarray = array[0..9] of byte;
  331. t64bitarray = array[0..7] of byte;
  332. t32bitarray = array[0..3] of byte;
  333. var
  334. ch : char;
  335. hp : tai;
  336. consttyp : tait;
  337. s : string;
  338. found : boolean;
  339. i,pos,l : longint;
  340. InlineLevel : longint;
  341. co : comp;
  342. sin : single;
  343. d : double;
  344. e : extended;
  345. op : tasmop;
  346. calljmp,
  347. do_line : boolean;
  348. sep : char;
  349. begin
  350. if not assigned(p) then
  351. exit;
  352. InlineLevel:=0;
  353. { lineinfo is only needed for codesegment (PFV) }
  354. do_line:=(cs_asm_source in aktglobalswitches) or
  355. ((cs_lineinfo in aktmoduleswitches)
  356. and (p=codesegment));
  357. hp:=tai(p.first);
  358. while assigned(hp) do
  359. begin
  360. aktfilepos:=hp.fileinfo;
  361. if not(hp.typ in nolinetai) then
  362. begin
  363. {$ifdef GDB}
  364. { write stabs }
  365. if (cs_debuginfo in aktmoduleswitches) or
  366. (cs_gdb_lineinfo in aktglobalswitches) then
  367. WriteFileLineInfo(hp.fileinfo);
  368. {$endif GDB}
  369. if do_line then
  370. begin
  371. { load infile }
  372. if lastfileinfo.fileindex<>hp.fileinfo.fileindex then
  373. begin
  374. infile:=current_module.sourcefiles.get_file(hp.fileinfo.fileindex);
  375. if assigned(infile) then
  376. begin
  377. { open only if needed !! }
  378. if (cs_asm_source in aktglobalswitches) then
  379. infile.open;
  380. end;
  381. { avoid unnecessary reopens of the same file !! }
  382. lastfileinfo.fileindex:=hp.fileinfo.fileindex;
  383. { be sure to change line !! }
  384. lastfileinfo.line:=-1;
  385. end;
  386. { write source }
  387. if (cs_asm_source in aktglobalswitches) and
  388. assigned(infile) then
  389. begin
  390. if (infile<>lastinfile) then
  391. begin
  392. AsmWriteLn(target_asm.comment+'['+infile.name^+']');
  393. if assigned(lastinfile) then
  394. lastinfile.close;
  395. end;
  396. if (hp.fileinfo.line<>lastfileinfo.line) and
  397. ((hp.fileinfo.line<infile.maxlinebuf) or (InlineLevel>0)) then
  398. begin
  399. if (hp.fileinfo.line<>0) and
  400. ((infile.linebuf^[hp.fileinfo.line]>=0) or (InlineLevel>0)) then
  401. AsmWriteLn(target_asm.comment+'['+tostr(hp.fileinfo.line)+'] '+
  402. fixline(infile.GetLineStr(hp.fileinfo.line)));
  403. { set it to a negative value !
  404. to make that is has been read already !! PM }
  405. if (infile.linebuf^[hp.fileinfo.line]>=0) then
  406. infile.linebuf^[hp.fileinfo.line]:=-infile.linebuf^[hp.fileinfo.line]-1;
  407. end;
  408. end;
  409. lastfileinfo:=hp.fileinfo;
  410. lastinfile:=infile;
  411. end;
  412. end;
  413. case hp.typ of
  414. ait_comment :
  415. Begin
  416. AsmWrite(target_asm.comment);
  417. AsmWritePChar(tai_asm_comment(hp).str);
  418. AsmLn;
  419. End;
  420. ait_regalloc :
  421. begin
  422. if (cs_asm_regalloc in aktglobalswitches) then
  423. AsmWriteLn(target_asm.comment+'Register '+att_reg2str[tairegalloc(hp).reg]+
  424. allocstr[tairegalloc(hp).allocation]);
  425. end;
  426. ait_tempalloc :
  427. begin
  428. if (cs_asm_tempalloc in aktglobalswitches) then
  429. AsmWriteLn(target_asm.comment+'Temp '+tostr(taitempalloc(hp).temppos)+','+
  430. tostr(taitempalloc(hp).tempsize)+allocstr[taitempalloc(hp).allocation]);
  431. end;
  432. ait_align :
  433. begin
  434. AsmWrite(#9'.balign '+tostr(tai_align(hp).aligntype));
  435. if tai_align(hp).use_op then
  436. AsmWrite(','+tostr(tai_align(hp).fillop));
  437. AsmLn;
  438. end;
  439. ait_section :
  440. begin
  441. if tai_section(hp).sec<>sec_none then
  442. begin
  443. AsmLn;
  444. AsmWriteLn(ait_section2str(tai_section(hp).sec));
  445. {$ifdef GDB}
  446. lastfileinfo.line:=-1;
  447. {$endif GDB}
  448. end;
  449. end;
  450. ait_datablock :
  451. begin
  452. if tai_datablock(hp).is_global then
  453. AsmWrite(#9'.comm'#9)
  454. else
  455. AsmWrite(#9'.lcomm'#9);
  456. AsmWrite(tai_datablock(hp).sym^.name);
  457. AsmWriteLn(','+tostr(tai_datablock(hp).size));
  458. end;
  459. ait_const_32bit,
  460. ait_const_16bit,
  461. ait_const_8bit :
  462. begin
  463. AsmWrite(ait_const2str[hp.typ]+tostr(tai_const(hp).value));
  464. consttyp:=hp.typ;
  465. l:=0;
  466. repeat
  467. found:=(not (tai(hp.next)=nil)) and (tai(hp.next).typ=consttyp);
  468. if found then
  469. begin
  470. hp:=tai(hp.next);
  471. s:=','+tostr(tai_const(hp).value);
  472. AsmWrite(s);
  473. inc(l,length(s));
  474. end;
  475. until (not found) or (l>line_length);
  476. AsmLn;
  477. end;
  478. ait_const_symbol :
  479. begin
  480. AsmWrite(#9'.long'#9+tai_const_symbol(hp).sym^.name);
  481. if tai_const_symbol(hp).offset>0 then
  482. AsmWrite('+'+tostr(tai_const_symbol(hp).offset))
  483. else if tai_const_symbol(hp).offset<0 then
  484. AsmWrite(tostr(tai_const_symbol(hp).offset));
  485. AsmLn;
  486. end;
  487. ait_const_rva :
  488. AsmWriteLn(#9'.rva'#9+tai_const_symbol(hp).sym^.name);
  489. ait_real_80bit :
  490. begin
  491. if do_line then
  492. AsmWriteLn(target_asm.comment+extended2str(tai_real_80bit(hp).value));
  493. { Make sure e is a extended type, bestreal could be
  494. a different type (bestreal) !! (PFV) }
  495. e:=tai_real_80bit(hp).value;
  496. AsmWrite(#9'.byte'#9);
  497. for i:=0 to 9 do
  498. begin
  499. if i<>0 then
  500. AsmWrite(',');
  501. AsmWrite(tostr(t80bitarray(e)[i]));
  502. end;
  503. AsmLn;
  504. end;
  505. ait_real_64bit :
  506. begin
  507. if do_line then
  508. AsmWriteLn(target_asm.comment+double2str(tai_real_64bit(hp).value));
  509. d:=tai_real_64bit(hp).value;
  510. AsmWrite(#9'.byte'#9);
  511. for i:=0 to 7 do
  512. begin
  513. if i<>0 then
  514. AsmWrite(',');
  515. AsmWrite(tostr(t64bitarray(d)[i]));
  516. end;
  517. AsmLn;
  518. end;
  519. ait_real_32bit :
  520. begin
  521. if do_line then
  522. AsmWriteLn(target_asm.comment+single2str(tai_real_32bit(hp).value));
  523. sin:=tai_real_32bit(hp).value;
  524. AsmWrite(#9'.byte'#9);
  525. for i:=0 to 3 do
  526. begin
  527. if i<>0 then
  528. AsmWrite(',');
  529. AsmWrite(tostr(t32bitarray(sin)[i]));
  530. end;
  531. AsmLn;
  532. end;
  533. ait_comp_64bit :
  534. begin
  535. if do_line then
  536. AsmWriteLn(target_asm.comment+comp2str(tai_comp_64bit(hp).value));
  537. AsmWrite(#9'.byte'#9);
  538. {$ifdef FPC}
  539. co:=comp(tai_comp_64bit(hp).value);
  540. {$else}
  541. co:=tai_comp_64bit(hp).value;
  542. {$endif}
  543. for i:=0 to 7 do
  544. begin
  545. if i<>0 then
  546. AsmWrite(',');
  547. AsmWrite(tostr(t64bitarray(co)[i]));
  548. end;
  549. AsmLn;
  550. end;
  551. ait_direct :
  552. begin
  553. AsmWritePChar(tai_direct(hp).str);
  554. AsmLn;
  555. {$IfDef GDB}
  556. if strpos(tai_direct(hp).str,'.data')<>nil then
  557. n_line:=n_dataline
  558. else if strpos(tai_direct(hp).str,'.text')<>nil then
  559. n_line:=n_textline
  560. else if strpos(tai_direct(hp).str,'.bss')<>nil then
  561. n_line:=n_bssline;
  562. {$endif GDB}
  563. end;
  564. ait_string :
  565. begin
  566. pos:=0;
  567. for i:=1 to tai_string(hp).len do
  568. begin
  569. if pos=0 then
  570. begin
  571. AsmWrite(#9'.ascii'#9'"');
  572. pos:=20;
  573. end;
  574. ch:=tai_string(hp).str[i-1];
  575. case ch of
  576. #0, {This can't be done by range, because a bug in FPC}
  577. #1..#31,
  578. #128..#255 : s:='\'+tostr(ord(ch) shr 6)+tostr((ord(ch) and 63) shr 3)+tostr(ord(ch) and 7);
  579. '"' : s:='\"';
  580. '\' : s:='\\';
  581. else
  582. s:=ch;
  583. end;
  584. AsmWrite(s);
  585. inc(pos,length(s));
  586. if (pos>line_length) or (i=tai_string(hp).len) then
  587. begin
  588. AsmWriteLn('"');
  589. pos:=0;
  590. end;
  591. end;
  592. end;
  593. ait_label :
  594. begin
  595. if (tai_label(hp).l^.is_used) then
  596. begin
  597. if tai_label(hp).l^.defbind=AB_GLOBAL then
  598. begin
  599. AsmWrite('.globl'#9);
  600. AsmWriteLn(tai_label(hp).l^.name);
  601. end;
  602. AsmWrite(tai_label(hp).l^.name);
  603. AsmWriteLn(':');
  604. end;
  605. end;
  606. ait_symbol :
  607. begin
  608. if tai_symbol(hp).is_global then
  609. begin
  610. AsmWrite('.globl'#9);
  611. AsmWriteLn(tai_symbol(hp).sym^.name);
  612. end;
  613. if target_info.target=target_i386_linux then
  614. begin
  615. AsmWrite(#9'.type'#9);
  616. AsmWrite(tai_symbol(hp).sym^.name);
  617. if assigned(tai(hp.next)) and
  618. (tai(hp.next).typ in [ait_const_symbol,ait_const_rva,
  619. ait_const_32bit,ait_const_16bit,ait_const_8bit,ait_datablock,
  620. ait_real_32bit,ait_real_64bit,ait_real_80bit,ait_comp_64bit]) then
  621. AsmWriteLn(',@object')
  622. else
  623. AsmWriteLn(',@function');
  624. if tai_symbol(hp).sym^.size>0 then
  625. begin
  626. AsmWrite(#9'.size'#9);
  627. AsmWrite(tai_symbol(hp).sym^.name);
  628. AsmWrite(', ');
  629. AsmWriteLn(tostr(tai_symbol(hp).sym^.size));
  630. end;
  631. end;
  632. AsmWrite(tai_symbol(hp).sym^.name);
  633. AsmWriteLn(':');
  634. end;
  635. ait_symbol_end :
  636. begin
  637. if target_info.target=target_i386_linux then
  638. begin
  639. s:=target_asm.labelprefix+'e'+tostr(symendcount);
  640. inc(symendcount);
  641. AsmWriteLn(s+':');
  642. AsmWrite(#9'.size'#9);
  643. AsmWrite(tai_symbol(hp).sym^.name);
  644. AsmWrite(', '+s+' - ');
  645. AsmWriteLn(tai_symbol(hp).sym^.name);
  646. end;
  647. end;
  648. ait_instruction :
  649. begin
  650. taicpu(hp).SetOperandOrder(op_att);
  651. op:=taicpu(hp).opcode;
  652. calljmp:=is_calljmp(op);
  653. { call maybe not translated to call }
  654. s:=#9+att_op2str[op]+cond2str[taicpu(hp).condition];
  655. { suffix needed ? fnstsw,fldcw don't support suffixes
  656. with binutils 2.9.5 under linux }
  657. if (not calljmp) and
  658. (att_needsuffix[op]<>AttSufNONE) and
  659. (op<>A_FNSTSW) and (op<>A_FSTSW) and
  660. (op<>A_FNSTCW) and (op<>A_FSTCW) and
  661. (op<>A_FLDCW) and
  662. not(
  663. (taicpu(hp).oper[0].typ=top_reg) and
  664. (taicpu(hp).oper[0].reg in [R_ST..R_ST7])
  665. ) then
  666. s:=s+att_opsize2str[taicpu(hp).opsize];
  667. { process operands }
  668. if taicpu(hp).ops<>0 then
  669. begin
  670. { call and jmp need an extra handling }
  671. { this code is only called if jmp isn't a labeled instruction }
  672. { quick hack to overcome a problem with manglednames=255 chars }
  673. if calljmp then
  674. begin
  675. AsmWrite(s+#9);
  676. s:=getopstr_jmp(taicpu(hp).oper[0]);
  677. end
  678. else
  679. begin
  680. for i:=0 to taicpu(hp).ops-1 do
  681. begin
  682. if i=0 then
  683. sep:=#9
  684. else
  685. sep:=',';
  686. s:=s+sep+getopstr(taicpu(hp).oper[i])
  687. end;
  688. end;
  689. end;
  690. AsmWriteLn(s);
  691. end;
  692. {$ifdef GDB}
  693. ait_stabs :
  694. begin
  695. AsmWrite(#9'.stabs ');
  696. AsmWritePChar(tai_stabs(hp).str);
  697. AsmLn;
  698. end;
  699. ait_stabn :
  700. begin
  701. AsmWrite(#9'.stabn ');
  702. AsmWritePChar(tai_stabn(hp).str);
  703. AsmLn;
  704. end;
  705. ait_force_line :
  706. stabslastfileinfo.line:=0;
  707. ait_stab_function_name:
  708. funcname:=tai_stab_function_name(hp).str;
  709. {$endif GDB}
  710. ait_cut :
  711. begin
  712. if SmartAsm then
  713. begin
  714. { only reset buffer if nothing has changed }
  715. if AsmSize=AsmStartSize then
  716. AsmClear
  717. else
  718. begin
  719. AsmClose;
  720. DoAssemble;
  721. AsmCreate(tai_cut(hp).place);
  722. end;
  723. { avoid empty files }
  724. while assigned(hp.next) and (tai(hp.next).typ in [ait_cut,ait_section,ait_comment]) do
  725. begin
  726. if tai(hp.next).typ=ait_section then
  727. lastsec:=tai_section(hp.next).sec;
  728. hp:=tai(hp.next);
  729. end;
  730. {$ifdef GDB}
  731. { force write of filename }
  732. FillChar(stabslastfileinfo,sizeof(stabslastfileinfo),0);
  733. includecount:=0;
  734. funcname:=nil;
  735. WriteFileLineInfo(hp.fileinfo);
  736. {$endif GDB}
  737. if lastsec<>sec_none then
  738. AsmWriteLn(ait_section2str(lastsec));
  739. AsmStartSize:=AsmSize;
  740. end;
  741. end;
  742. ait_marker :
  743. if tai_marker(hp).kind=InlineStart then
  744. inc(InlineLevel)
  745. else if tai_marker(hp).kind=InlineEnd then
  746. dec(InlineLevel);
  747. else
  748. internalerror(10000);
  749. end;
  750. hp:=tai(hp.next);
  751. end;
  752. end;
  753. procedure T386ATTAssembler.WriteAsmList;
  754. var
  755. p:dirstr;
  756. n:namestr;
  757. e:extstr;
  758. {$ifdef GDB}
  759. fileinfo : tfileposinfo;
  760. {$endif GDB}
  761. begin
  762. {$ifdef EXTDEBUG}
  763. if assigned(current_module.mainsource) then
  764. Comment(v_info,'Start writing att-styled assembler output for '+current_module.mainsource^);
  765. {$endif}
  766. LastSec:=sec_none;
  767. {$ifdef GDB}
  768. FillChar(stabslastfileinfo,sizeof(stabslastfileinfo),0);
  769. {$endif GDB}
  770. FillChar(lastfileinfo,sizeof(lastfileinfo),0);
  771. LastInfile:=nil;
  772. if assigned(current_module.mainsource) then
  773. fsplit(current_module.mainsource^,p,n,e)
  774. else
  775. begin
  776. p:=inputdir;
  777. n:=inputfile;
  778. e:=inputextension;
  779. end;
  780. { to get symify to work }
  781. AsmWriteLn(#9'.file "'+FixFileName(n+e)+'"');
  782. {$ifdef GDB}
  783. n_line:=n_bssline;
  784. funcname:=nil;
  785. linecount:=1;
  786. includecount:=0;
  787. fileinfo.fileindex:=1;
  788. fileinfo.line:=1;
  789. { Write main file }
  790. WriteFileLineInfo(fileinfo);
  791. {$endif GDB}
  792. AsmStartSize:=AsmSize;
  793. symendcount:=0;
  794. countlabelref:=false;
  795. If (cs_debuginfo in aktmoduleswitches) then
  796. WriteTree(debuglist);
  797. WriteTree(codesegment);
  798. WriteTree(datasegment);
  799. WriteTree(consts);
  800. WriteTree(rttilist);
  801. Writetree(resourcestringlist);
  802. WriteTree(bsssegment);
  803. Writetree(importssection);
  804. { exports are written by DLLTOOL
  805. if we use it so don't insert it twice (PM) }
  806. if not UseDeffileForExport and assigned(exportssection) then
  807. Writetree(exportssection);
  808. Writetree(resourcesection);
  809. {$ifdef GDB}
  810. WriteFileEndInfo;
  811. {$ENDIF}
  812. countlabelref:=true;
  813. AsmLn;
  814. {$ifdef EXTDEBUG}
  815. if assigned(current_module.mainsource) then
  816. comment(v_info,'Done writing att-styled assembler output for '+current_module.mainsource^);
  817. {$endif EXTDEBUG}
  818. end;
  819. end.
  820. {
  821. $Log$
  822. Revision 1.4 2001-03-05 21:39:11 peter
  823. * changed to class with common TAssembler also for internal assembler
  824. Revision 1.3 2001/01/13 20:24:24 peter
  825. * fixed operand order that got mixed up for external writers after
  826. my previous assembler block valid instruction check
  827. Revision 1.2 2000/12/25 00:07:31 peter
  828. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  829. tlinkedlist objects)
  830. Revision 1.1 2000/11/30 22:18:48 florian
  831. * moved to i386
  832. Revision 1.6 2000/09/24 15:06:10 peter
  833. * use defines.inc
  834. Revision 1.5 2000/08/27 16:11:49 peter
  835. * moved some util functions from globals,cobjects to cutils
  836. * splitted files into finput,fmodule
  837. Revision 1.4 2000/08/20 17:38:21 peter
  838. * smartlinking fixed for linux (merged)
  839. Revision 1.3 2000/07/13 12:08:24 michael
  840. + patched to 1.1.0 with former 1.09patch from peter
  841. Revision 1.2 2000/07/13 11:32:28 michael
  842. + removed logs
  843. }