aggas.pas 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990
  1. {
  2. $Id$
  3. Copyright (c) 1998-2004 by the Free Pascal team
  4. This unit implements generic GNU assembler (v2.8 or later)
  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. { Base unit for writing GNU assembler output.
  19. }
  20. unit aggas;
  21. {$i fpcdefs.inc}
  22. interface
  23. uses
  24. dos,
  25. cclasses,
  26. globals,
  27. aasmbase,aasmtai,aasmcpu,
  28. assemble;
  29. type
  30. {# This is a derived class which is used to write
  31. GAS styled assembler.
  32. The WriteInstruction() method must be overriden
  33. to write a single instruction to the assembler
  34. file.
  35. }
  36. TGNUAssembler=class(texternalassembler)
  37. protected
  38. function sectionname(atype:tasmsectiontype;const aname:string):string;virtual;
  39. procedure WriteSection(atype:tasmsectiontype;const aname:string);
  40. procedure WriteExtraHeader;virtual;
  41. {$ifdef GDB}
  42. procedure WriteFileLineInfo(var fileinfo : tfileposinfo);
  43. procedure WriteFileEndInfo;
  44. {$endif}
  45. procedure WriteInstruction(hp: tai); virtual; abstract;
  46. public
  47. procedure WriteTree(p:TAAsmoutput);override;
  48. procedure WriteAsmList;override;
  49. end;
  50. const
  51. regname_count=45;
  52. regname_count_bsstart=32; { Largest power of 2 out of regname_count. }
  53. implementation
  54. uses
  55. cutils,globtype,systems,
  56. fmodule,finput,verbose,
  57. itcpugas
  58. {$ifdef GDB}
  59. {$ifdef delphi}
  60. ,sysutils
  61. {$else}
  62. ,strings
  63. {$endif}
  64. ,gdb
  65. {$endif GDB}
  66. ;
  67. const
  68. line_length = 70;
  69. var
  70. {$ifdef GDB}
  71. n_line : byte; { different types of source lines }
  72. linecount,
  73. includecount : longint;
  74. funcname : pchar;
  75. stabslastfileinfo : tfileposinfo;
  76. {$endif}
  77. lasTSecType : TAsmSectionType; { last section type written }
  78. lastfileinfo : tfileposinfo;
  79. infile,
  80. lastinfile : tinputfile;
  81. symendcount : longint;
  82. type
  83. {$ifdef cpuextended}
  84. t80bitarray = array[0..9] of byte;
  85. {$endif cpuextended}
  86. t64bitarray = array[0..7] of byte;
  87. t32bitarray = array[0..3] of byte;
  88. {****************************************************************************}
  89. { Support routines }
  90. {****************************************************************************}
  91. function fixline(s:string):string;
  92. {
  93. return s with all leading and ending spaces and tabs removed
  94. }
  95. var
  96. i,j,k : integer;
  97. begin
  98. i:=length(s);
  99. while (i>0) and (s[i] in [#9,' ']) do
  100. dec(i);
  101. j:=1;
  102. while (j<i) and (s[j] in [#9,' ']) do
  103. inc(j);
  104. for k:=j to i do
  105. if s[k] in [#0..#31,#127..#255] then
  106. s[k]:='.';
  107. fixline:=Copy(s,j,i-j+1);
  108. end;
  109. function single2str(d : single) : string;
  110. var
  111. hs : string;
  112. begin
  113. str(d,hs);
  114. { replace space with + }
  115. if hs[1]=' ' then
  116. hs[1]:='+';
  117. single2str:='0d'+hs
  118. end;
  119. function double2str(d : double) : string;
  120. var
  121. hs : string;
  122. begin
  123. str(d,hs);
  124. { replace space with + }
  125. if hs[1]=' ' then
  126. hs[1]:='+';
  127. double2str:='0d'+hs
  128. end;
  129. function extended2str(e : extended) : string;
  130. var
  131. hs : string;
  132. begin
  133. str(e,hs);
  134. { replace space with + }
  135. if hs[1]=' ' then
  136. hs[1]:='+';
  137. extended2str:='0d'+hs
  138. end;
  139. { convert floating point values }
  140. { to correct endian }
  141. procedure swap64bitarray(var t: t64bitarray);
  142. var
  143. b: byte;
  144. begin
  145. b:= t[7];
  146. t[7] := t[0];
  147. t[0] := b;
  148. b := t[6];
  149. t[6] := t[1];
  150. t[1] := b;
  151. b:= t[5];
  152. t[5] := t[2];
  153. t[2] := b;
  154. b:= t[4];
  155. t[4] := t[3];
  156. t[3] := b;
  157. end;
  158. procedure swap32bitarray(var t: t32bitarray);
  159. var
  160. b: byte;
  161. begin
  162. b:= t[1];
  163. t[1]:= t[2];
  164. t[2]:= b;
  165. b:= t[0];
  166. t[0]:= t[3];
  167. t[3]:= b;
  168. end;
  169. const
  170. ait_const2str : array[ait_const_128bit..ait_const_indirect_symbol] of string[20]=(
  171. #9'.fixme128'#9,#9'.quad'#9,#9'.long'#9,#9'.short'#9,#9'.byte'#9,
  172. #9'.sleb128'#9,#9'.uleb128'#9,
  173. #9'.rva'#9,#9'.indirect_symbol'#9
  174. );
  175. {****************************************************************************}
  176. { GNU Assembler writer }
  177. {****************************************************************************}
  178. {$ifdef GDB}
  179. procedure TGNUAssembler.WriteFileLineInfo(var fileinfo : tfileposinfo);
  180. var
  181. curr_n : byte;
  182. begin
  183. if not ((cs_debuginfo in aktmoduleswitches) or
  184. (cs_gdb_lineinfo in aktglobalswitches)) then
  185. exit;
  186. { file changed ? (must be before line info) }
  187. if (fileinfo.fileindex<>0) and
  188. (stabslastfileinfo.fileindex<>fileinfo.fileindex) then
  189. begin
  190. infile:=current_module.sourcefiles.get_file(fileinfo.fileindex);
  191. if assigned(infile) then
  192. begin
  193. if includecount=0 then
  194. curr_n:=n_sourcefile
  195. else
  196. curr_n:=n_includefile;
  197. if (infile.path^<>'') then
  198. begin
  199. AsmWriteLn(#9'.stabs "'+lower(BsToSlash(FixPath(infile.path^,false)))+'",'+
  200. tostr(curr_n)+',0,0,'+target_asm.labelprefix+'text'+ToStr(IncludeCount));
  201. end;
  202. AsmWriteLn(#9'.stabs "'+lower(FixFileName(infile.name^))+'",'+
  203. tostr(curr_n)+',0,0,'+target_asm.labelprefix+'text'+ToStr(IncludeCount));
  204. AsmWriteLn(target_asm.labelprefix+'text'+ToStr(IncludeCount)+':');
  205. inc(includecount);
  206. { force new line info }
  207. stabslastfileinfo.line:=-1;
  208. end;
  209. end;
  210. { line changed ? }
  211. if (stabslastfileinfo.line<>fileinfo.line) and (fileinfo.line<>0) then
  212. begin
  213. if (n_line=n_textline) and assigned(funcname) and
  214. (target_info.use_function_relative_addresses) then
  215. begin
  216. AsmWriteLn(target_asm.labelprefix+'l'+tostr(linecount)+':');
  217. AsmWrite(#9'.stabn '+tostr(n_line)+',0,'+tostr(fileinfo.line)+','+
  218. target_asm.labelprefix+'l'+tostr(linecount)+' - ');
  219. AsmWritePChar(FuncName);
  220. AsmLn;
  221. inc(linecount);
  222. end
  223. else
  224. AsmWriteLn(#9'.stabd'#9+tostr(n_line)+',0,'+tostr(fileinfo.line));
  225. end;
  226. stabslastfileinfo:=fileinfo;
  227. end;
  228. procedure TGNUAssembler.WriteFileEndInfo;
  229. begin
  230. if not ((cs_debuginfo in aktmoduleswitches) or
  231. (cs_gdb_lineinfo in aktglobalswitches)) then
  232. exit;
  233. WriteSection(sec_code,'');
  234. AsmWriteLn(#9'.stabs "",'+tostr(n_sourcefile)+',0,0,'+target_asm.labelprefix+'etext');
  235. AsmWriteLn(target_asm.labelprefix+'etext:');
  236. end;
  237. {$endif GDB}
  238. function TGNUAssembler.sectionname(atype:tasmsectiontype;const aname:string):string;
  239. const
  240. secnames : array[tasmsectiontype] of string[12] = ('',
  241. {$warning TODO .rodata not yet working}
  242. '.text','.data','.data','.bss',
  243. 'common',
  244. '.note',
  245. '.stab','.stabstr',
  246. '.idata$2','.idata$4','.idata$5','.idata$6','.idata$7','.edata',
  247. '.eh_frame',
  248. '.debug_frame'
  249. );
  250. begin
  251. if (target_info.system = system_powerpc_darwin) and
  252. (atype = sec_bss) then
  253. atype := sec_code;
  254. if use_smartlink_section and
  255. (atype<>sec_bss) and
  256. (aname<>'') then
  257. result:='.gnu.linkonce'+copy(secnames[atype],1,2)+'.'+aname
  258. else
  259. result:=secnames[atype];
  260. end;
  261. procedure TGNUAssembler.WriteSection(atype:tasmsectiontype;const aname:string);
  262. var
  263. s : string;
  264. begin
  265. AsmLn;
  266. if (target_info.system <> system_powerpc_darwin) then
  267. AsmWrite('.section ');
  268. s:=sectionname(atype,aname);
  269. AsmWrite(s);
  270. if copy(s,1,4)='.gnu' then
  271. begin
  272. case atype of
  273. sec_data :
  274. AsmWrite(',""');
  275. sec_code :
  276. AsmWrite(',"x"');
  277. end;
  278. end;
  279. AsmLn;
  280. {$ifdef GDB}
  281. { this is needed for line info in data }
  282. funcname:=nil;
  283. case atype of
  284. sec_code :
  285. n_line:=n_textline;
  286. sec_data :
  287. n_line:=n_dataline;
  288. sec_bss :
  289. n_line:=n_bssline;
  290. else
  291. n_line:=n_dataline;
  292. end;
  293. {$endif GDB}
  294. LasTSecType:=atype;
  295. end;
  296. procedure TGNUAssembler.WriteTree(p:TAAsmoutput);
  297. const
  298. regallocstr : array[tregalloctype] of string[10]=(' allocated',' released','resized');
  299. tempallocstr : array[boolean] of string[10]=(' released',' allocated');
  300. var
  301. ch : char;
  302. hp : tai;
  303. hp1 : tailineinfo;
  304. consttyp : taitype;
  305. s : string;
  306. i,pos,l : longint;
  307. InlineLevel : longint;
  308. last_align : longint;
  309. co : comp;
  310. sin : single;
  311. d : double;
  312. {$ifdef cpuextended}
  313. e : extended;
  314. {$endif cpuextended}
  315. do_line : boolean;
  316. begin
  317. if not assigned(p) then
  318. exit;
  319. last_align := 2;
  320. InlineLevel:=0;
  321. { lineinfo is only needed for codesegment (PFV) }
  322. do_line:=(cs_asm_source in aktglobalswitches) or
  323. ((cs_lineinfo in aktmoduleswitches)
  324. and (p=codesegment));
  325. hp:=tai(p.first);
  326. while assigned(hp) do
  327. begin
  328. if not(hp.typ in SkipLineInfo) then
  329. begin
  330. hp1 := hp as tailineinfo;
  331. aktfilepos:=hp1.fileinfo;
  332. {$ifdef GDB}
  333. { write stabs }
  334. if (cs_debuginfo in aktmoduleswitches) or
  335. (cs_gdb_lineinfo in aktglobalswitches) then
  336. WriteFileLineInfo(hp1.fileinfo);
  337. {$endif GDB}
  338. { no line info for inlined code }
  339. if do_line and (inlinelevel=0) then
  340. begin
  341. { load infile }
  342. if lastfileinfo.fileindex<>hp1.fileinfo.fileindex then
  343. begin
  344. infile:=current_module.sourcefiles.get_file(hp1.fileinfo.fileindex);
  345. if assigned(infile) then
  346. begin
  347. { open only if needed !! }
  348. if (cs_asm_source in aktglobalswitches) then
  349. infile.open;
  350. end;
  351. { avoid unnecessary reopens of the same file !! }
  352. lastfileinfo.fileindex:=hp1.fileinfo.fileindex;
  353. { be sure to change line !! }
  354. lastfileinfo.line:=-1;
  355. end;
  356. { write source }
  357. if (cs_asm_source in aktglobalswitches) and
  358. assigned(infile) then
  359. begin
  360. if (infile<>lastinfile) then
  361. begin
  362. AsmWriteLn(target_asm.comment+'['+infile.name^+']');
  363. if assigned(lastinfile) then
  364. lastinfile.close;
  365. end;
  366. if (hp1.fileinfo.line<>lastfileinfo.line) and
  367. ((hp1.fileinfo.line<infile.maxlinebuf) or (InlineLevel>0)) then
  368. begin
  369. if (hp1.fileinfo.line<>0) and
  370. ((infile.linebuf^[hp1.fileinfo.line]>=0) or (InlineLevel>0)) then
  371. AsmWriteLn(target_asm.comment+'['+tostr(hp1.fileinfo.line)+'] '+
  372. fixline(infile.GetLineStr(hp1.fileinfo.line)));
  373. { set it to a negative value !
  374. to make that is has been read already !! PM }
  375. if (infile.linebuf^[hp1.fileinfo.line]>=0) then
  376. infile.linebuf^[hp1.fileinfo.line]:=-infile.linebuf^[hp1.fileinfo.line]-1;
  377. end;
  378. end;
  379. lastfileinfo:=hp1.fileinfo;
  380. lastinfile:=infile;
  381. end;
  382. end;
  383. case hp.typ of
  384. ait_comment :
  385. Begin
  386. AsmWrite(target_asm.comment);
  387. AsmWritePChar(tai_comment(hp).str);
  388. AsmLn;
  389. End;
  390. ait_regalloc :
  391. begin
  392. if (cs_asm_regalloc in aktglobalswitches) then
  393. begin
  394. AsmWrite(#9+target_asm.comment+'Register ');
  395. repeat
  396. AsmWrite(gas_regname(Tai_regalloc(hp).reg));
  397. if (hp.next=nil) or
  398. (tai(hp.next).typ<>ait_regalloc) or
  399. (tai_regalloc(hp.next).ratype<>tai_regalloc(hp).ratype) then
  400. break;
  401. hp:=tai(hp.next);
  402. AsmWrite(',');
  403. until false;
  404. AsmWriteLn(regallocstr[tai_regalloc(hp).ratype]);
  405. end;
  406. end;
  407. ait_tempalloc :
  408. begin
  409. if (cs_asm_tempalloc in aktglobalswitches) then
  410. begin
  411. {$ifdef EXTDEBUG}
  412. if assigned(tai_tempalloc(hp).problem) then
  413. AsmWriteLn(target_asm.comment+'Temp '+tostr(tai_tempalloc(hp).temppos)+','+
  414. tostr(tai_tempalloc(hp).tempsize)+' '+tai_tempalloc(hp).problem^)
  415. else
  416. {$endif EXTDEBUG}
  417. AsmWriteLn(target_asm.comment+'Temp '+tostr(tai_tempalloc(hp).temppos)+','+
  418. tostr(tai_tempalloc(hp).tempsize)+tempallocstr[tai_tempalloc(hp).allocation]);
  419. end;
  420. end;
  421. ait_align :
  422. begin
  423. if tai_align(hp).aligntype>1 then
  424. begin
  425. if target_info.system <> system_powerpc_darwin then
  426. begin
  427. AsmWrite(#9'.balign '+tostr(tai_align(hp).aligntype));
  428. if tai_align(hp).use_op then
  429. AsmWrite(','+tostr(tai_align(hp).fillop))
  430. end
  431. else
  432. begin
  433. { darwin as only supports .align }
  434. if not ispowerof2(tai_align(hp).aligntype,i) then
  435. internalerror(2003010305);
  436. AsmWrite(#9'.align '+tostr(i));
  437. last_align := i;
  438. end;
  439. AsmLn;
  440. end;
  441. end;
  442. ait_section :
  443. begin
  444. if tai_section(hp).sectype<>sec_none then
  445. begin
  446. WriteSection(tai_section(hp).sectype,tai_section(hp).name^);
  447. {$ifdef GDB}
  448. lastfileinfo.line:=-1;
  449. {$endif GDB}
  450. end
  451. else
  452. begin
  453. {$ifdef EXTDEBUG}
  454. AsmWrite(target_asm.comment);
  455. AsmWriteln(' sec_none');
  456. {$endif EXTDEBUG}
  457. end;
  458. end;
  459. ait_datablock :
  460. begin
  461. if tai_datablock(hp).is_global then
  462. AsmWrite(#9'.comm'#9)
  463. else
  464. AsmWrite(#9'.lcomm'#9);
  465. AsmWrite(tai_datablock(hp).sym.name);
  466. AsmWrite(','+tostr(tai_datablock(hp).size));
  467. if (target_info.system = system_powerpc_darwin) and
  468. not(tai_datablock(hp).is_global) then
  469. AsmWrite(','+tostr(last_align));
  470. AsmWriteln('');
  471. end;
  472. {$ifndef cpu64bit}
  473. ait_const_128bit :
  474. begin
  475. internalerror(200404291);
  476. end;
  477. ait_const_64bit :
  478. begin
  479. if assigned(tai_const(hp).sym) then
  480. internalerror(200404292);
  481. AsmWrite(ait_const2str[ait_const_32bit]);
  482. if target_info.endian = endian_little then
  483. begin
  484. AsmWrite(tostr(longint(lo(tai_const(hp).value))));
  485. AsmWrite(',');
  486. AsmWrite(tostr(longint(hi(tai_const(hp).value))));
  487. end
  488. else
  489. begin
  490. AsmWrite(tostr(longint(hi(tai_const(hp).value))));
  491. AsmWrite(',');
  492. AsmWrite(tostr(longint(lo(tai_const(hp).value))));
  493. end;
  494. AsmLn;
  495. end;
  496. {$endif cpu64bit}
  497. ait_const_uleb128bit,
  498. ait_const_sleb128bit,
  499. {$ifdef cpu64bit}
  500. ait_const_128bit,
  501. ait_const_64bit,
  502. {$endif cpu64bit}
  503. ait_const_32bit,
  504. ait_const_16bit,
  505. ait_const_8bit,
  506. ait_const_rva_symbol,
  507. ait_const_indirect_symbol :
  508. begin
  509. AsmWrite(ait_const2str[hp.typ]);
  510. consttyp:=hp.typ;
  511. l:=0;
  512. repeat
  513. if assigned(tai_const(hp).sym) then
  514. begin
  515. if assigned(tai_const(hp).endsym) then
  516. s:=tai_const(hp).endsym.name+'-'+tai_const(hp).sym.name
  517. else
  518. s:=tai_const(hp).sym.name;
  519. if tai_const(hp).value<>0 then
  520. s:=s+tostr_with_plus(tai_const(hp).value);
  521. end
  522. else
  523. s:=tostr(tai_const(hp).value);
  524. AsmWrite(s);
  525. inc(l,length(s));
  526. if (LasTSecType<>sec_data) or
  527. (l>line_length) or
  528. (hp.next=nil) or
  529. (tai(hp.next).typ<>consttyp) then
  530. break;
  531. hp:=tai(hp.next);
  532. AsmWrite(',');
  533. until false;
  534. AsmLn;
  535. end;
  536. {$ifdef cpuextended}
  537. ait_real_80bit :
  538. begin
  539. if do_line then
  540. AsmWriteLn(target_asm.comment+'value: '+extended2str(tai_real_80bit(hp).value));
  541. { Make sure e is a extended type, bestreal could be
  542. a different type (bestreal) !! (PFV) }
  543. e:=tai_real_80bit(hp).value;
  544. AsmWrite(#9'.byte'#9);
  545. for i:=0 to 9 do
  546. begin
  547. if i<>0 then
  548. AsmWrite(',');
  549. AsmWrite(tostr(t80bitarray(e)[i]));
  550. end;
  551. AsmLn;
  552. end;
  553. {$endif cpuextended}
  554. ait_real_64bit :
  555. begin
  556. if do_line then
  557. AsmWriteLn(target_asm.comment+'value: '+double2str(tai_real_64bit(hp).value));
  558. d:=tai_real_64bit(hp).value;
  559. { swap the values to correct endian if required }
  560. if source_info.endian <> target_info.endian then
  561. swap64bitarray(t64bitarray(d));
  562. AsmWrite(#9'.byte'#9);
  563. {$ifdef arm}
  564. { on a real arm cpu, it's already hi/lo swapped }
  565. {$ifndef cpuarm}
  566. if tai_real_64bit(hp).formatoptions=fo_hiloswapped then
  567. begin
  568. for i:=4 to 7 do
  569. begin
  570. if i<>4 then
  571. AsmWrite(',');
  572. AsmWrite(tostr(t64bitarray(d)[i]));
  573. end;
  574. for i:=0 to 3 do
  575. begin
  576. AsmWrite(',');
  577. AsmWrite(tostr(t64bitarray(d)[i]));
  578. end;
  579. end
  580. else
  581. {$endif cpuarm}
  582. {$endif arm}
  583. begin
  584. for i:=0 to 7 do
  585. begin
  586. if i<>0 then
  587. AsmWrite(',');
  588. AsmWrite(tostr(t64bitarray(d)[i]));
  589. end;
  590. end;
  591. AsmLn;
  592. end;
  593. ait_real_32bit :
  594. begin
  595. if do_line then
  596. AsmWriteLn(target_asm.comment+'value: '+single2str(tai_real_32bit(hp).value));
  597. sin:=tai_real_32bit(hp).value;
  598. { swap the values to correct endian if required }
  599. if source_info.endian <> target_info.endian then
  600. swap32bitarray(t32bitarray(sin));
  601. AsmWrite(#9'.byte'#9);
  602. for i:=0 to 3 do
  603. begin
  604. if i<>0 then
  605. AsmWrite(',');
  606. AsmWrite(tostr(t32bitarray(sin)[i]));
  607. end;
  608. AsmLn;
  609. end;
  610. ait_comp_64bit :
  611. begin
  612. if do_line then
  613. AsmWriteLn(target_asm.comment+'value: '+extended2str(tai_comp_64bit(hp).value));
  614. AsmWrite(#9'.byte'#9);
  615. {$ifdef FPC}
  616. co:=comp(tai_comp_64bit(hp).value);
  617. {$else}
  618. co:=tai_comp_64bit(hp).value;
  619. {$endif}
  620. { swap the values to correct endian if required }
  621. if source_info.endian <> target_info.endian then
  622. swap64bitarray(t64bitarray(co));
  623. for i:=0 to 7 do
  624. begin
  625. if i<>0 then
  626. AsmWrite(',');
  627. AsmWrite(tostr(t64bitarray(co)[i]));
  628. end;
  629. AsmLn;
  630. end;
  631. ait_direct :
  632. begin
  633. AsmWritePChar(tai_direct(hp).str);
  634. AsmLn;
  635. {$IfDef GDB}
  636. if strpos(tai_direct(hp).str,'.data')<>nil then
  637. n_line:=n_dataline
  638. else if strpos(tai_direct(hp).str,'.text')<>nil then
  639. n_line:=n_textline
  640. else if strpos(tai_direct(hp).str,'.bss')<>nil then
  641. n_line:=n_bssline;
  642. {$endif GDB}
  643. end;
  644. ait_string :
  645. begin
  646. pos:=0;
  647. for i:=1 to tai_string(hp).len do
  648. begin
  649. if pos=0 then
  650. begin
  651. AsmWrite(#9'.ascii'#9'"');
  652. pos:=20;
  653. end;
  654. ch:=tai_string(hp).str[i-1];
  655. case ch of
  656. #0, {This can't be done by range, because a bug in FPC}
  657. #1..#31,
  658. #128..#255 : s:='\'+tostr(ord(ch) shr 6)+tostr((ord(ch) and 63) shr 3)+tostr(ord(ch) and 7);
  659. '"' : s:='\"';
  660. '\' : s:='\\';
  661. else
  662. s:=ch;
  663. end;
  664. AsmWrite(s);
  665. inc(pos,length(s));
  666. if (pos>line_length) or (i=tai_string(hp).len) then
  667. begin
  668. AsmWriteLn('"');
  669. pos:=0;
  670. end;
  671. end;
  672. end;
  673. ait_label :
  674. begin
  675. if (tai_label(hp).l.is_used) then
  676. begin
  677. if tai_label(hp).l.defbind=AB_GLOBAL then
  678. begin
  679. AsmWrite('.globl'#9);
  680. AsmWriteLn(tai_label(hp).l.name);
  681. end;
  682. AsmWrite(tai_label(hp).l.name);
  683. AsmWriteLn(':');
  684. end;
  685. end;
  686. ait_symbol :
  687. begin
  688. if tai_symbol(hp).is_global then
  689. begin
  690. AsmWrite('.globl'#9);
  691. AsmWriteLn(tai_symbol(hp).sym.name);
  692. end;
  693. if target_info.system in [system_i386_linux,system_i386_beos,
  694. system_powerpc_linux,system_m68k_linux,
  695. system_sparc_linux,system_alpha_linux,
  696. system_x86_64_linux,system_arm_linux] then
  697. begin
  698. AsmWrite(#9'.type'#9);
  699. AsmWrite(tai_symbol(hp).sym.name);
  700. if assigned(tai(hp.next)) and
  701. (tai(hp.next).typ in [ait_const_rva_symbol,
  702. ait_const_32bit,ait_const_16bit,ait_const_8bit,ait_datablock,
  703. ait_real_32bit,ait_real_64bit,ait_real_80bit,ait_comp_64bit]) then
  704. begin
  705. if target_info.system = system_arm_linux then
  706. AsmWriteLn(',#object')
  707. else
  708. AsmWriteLn(',@object')
  709. end
  710. else
  711. begin
  712. if target_info.system = system_arm_linux then
  713. AsmWriteLn(',#function')
  714. else
  715. AsmWriteLn(',@function');
  716. end;
  717. if tai_symbol(hp).sym.size>0 then
  718. begin
  719. AsmWrite(#9'.size'#9);
  720. AsmWrite(tai_symbol(hp).sym.name);
  721. AsmWrite(', ');
  722. AsmWriteLn(tostr(tai_symbol(hp).sym.size));
  723. end;
  724. end;
  725. AsmWrite(tai_symbol(hp).sym.name);
  726. AsmWriteLn(':');
  727. end;
  728. ait_symbol_end :
  729. begin
  730. if tf_needs_symbol_size in target_info.flags then
  731. begin
  732. s:=target_asm.labelprefix+'e'+tostr(symendcount);
  733. inc(symendcount);
  734. AsmWriteLn(s+':');
  735. AsmWrite(#9'.size'#9);
  736. AsmWrite(tai_symbol_end(hp).sym.name);
  737. AsmWrite(', '+s+' - ');
  738. AsmWriteLn(tai_symbol_end(hp).sym.name);
  739. end;
  740. end;
  741. ait_instruction :
  742. begin
  743. WriteInstruction(hp);
  744. end;
  745. {$ifdef GDB}
  746. ait_stabs :
  747. begin
  748. if assigned(tai_stabs(hp).str) then
  749. begin
  750. AsmWrite(#9'.stabs ');
  751. AsmWritePChar(tai_stabs(hp).str);
  752. AsmLn;
  753. end;
  754. end;
  755. ait_stabn :
  756. begin
  757. if assigned(tai_stabn(hp).str) then
  758. begin
  759. AsmWrite(#9'.stabn ');
  760. AsmWritePChar(tai_stabn(hp).str);
  761. AsmLn;
  762. end;
  763. end;
  764. ait_force_line :
  765. stabslastfileinfo.line:=0;
  766. ait_stab_function_name:
  767. funcname:=tai_stab_function_name(hp).str;
  768. {$endif GDB}
  769. ait_cutobject :
  770. begin
  771. if SmartAsm then
  772. begin
  773. { only reset buffer if nothing has changed }
  774. if AsmSize=AsmStartSize then
  775. AsmClear
  776. else
  777. begin
  778. AsmClose;
  779. DoAssemble;
  780. AsmCreate(tai_cutobject(hp).place);
  781. end;
  782. { avoid empty files }
  783. while assigned(hp.next) and (tai(hp.next).typ in [ait_cutobject,ait_section,ait_comment]) do
  784. begin
  785. if tai(hp.next).typ=ait_section then
  786. lasTSectype:=tai_section(hp.next).sectype;
  787. hp:=tai(hp.next);
  788. end;
  789. {$ifdef GDB}
  790. { force write of filename }
  791. FillChar(stabslastfileinfo,sizeof(stabslastfileinfo),0);
  792. includecount:=0;
  793. funcname:=nil;
  794. WriteFileLineInfo(aktfilepos);
  795. {$endif GDB}
  796. if lasTSectype<>sec_none then
  797. WriteSection(lasTSectype,'');
  798. AsmStartSize:=AsmSize;
  799. end;
  800. end;
  801. ait_marker :
  802. if tai_marker(hp).kind=InlineStart then
  803. inc(InlineLevel)
  804. else if tai_marker(hp).kind=InlineEnd then
  805. dec(InlineLevel);
  806. ait_non_lazy_symbol_pointer:
  807. AsmWriteLn('.non_lazy_symbol_pointer');
  808. else
  809. internalerror(10000);
  810. end;
  811. hp:=tai(hp.next);
  812. end;
  813. end;
  814. procedure TGNUAssembler.WriteExtraHeader;
  815. begin
  816. end;
  817. procedure TGNUAssembler.WriteAsmList;
  818. var
  819. p:dirstr;
  820. n:namestr;
  821. e:extstr;
  822. {$ifdef GDB}
  823. fileinfo : tfileposinfo;
  824. {$endif GDB}
  825. begin
  826. {$ifdef EXTDEBUG}
  827. if assigned(current_module.mainsource) then
  828. Comment(V_Debug,'Start writing gas-styled assembler output for '+current_module.mainsource^);
  829. {$endif}
  830. LasTSectype:=sec_none;
  831. {$ifdef GDB}
  832. FillChar(stabslastfileinfo,sizeof(stabslastfileinfo),0);
  833. {$endif GDB}
  834. FillChar(lastfileinfo,sizeof(lastfileinfo),0);
  835. LastInfile:=nil;
  836. if assigned(current_module.mainsource) then
  837. fsplit(current_module.mainsource^,p,n,e)
  838. else
  839. begin
  840. p:=inputdir;
  841. n:=inputfile;
  842. e:=inputextension;
  843. end;
  844. { to get symify to work }
  845. AsmWriteLn(#9'.file "'+FixFileName(n+e)+'"');
  846. WriteExtraHeader;
  847. {$ifdef GDB}
  848. n_line:=n_bssline;
  849. funcname:=nil;
  850. linecount:=1;
  851. includecount:=0;
  852. fileinfo.fileindex:=1;
  853. fileinfo.line:=1;
  854. { Write main file }
  855. WriteFileLineInfo(fileinfo);
  856. {$endif GDB}
  857. AsmStartSize:=AsmSize;
  858. symendcount:=0;
  859. If (cs_debuginfo in aktmoduleswitches) then
  860. WriteTree(debuglist);
  861. WriteTree(codesegment);
  862. WriteTree(datasegment);
  863. WriteTree(consts);
  864. WriteTree(rttilist);
  865. WriteTree(picdata);
  866. Writetree(resourcestringlist);
  867. WriteTree(bsssegment);
  868. Writetree(importssection);
  869. { exports are written by DLLTOOL
  870. if we use it so don't insert it twice (PM) }
  871. if not UseDeffileForExports and assigned(exportssection) then
  872. Writetree(exportssection);
  873. Writetree(resourcesection);
  874. Writetree(dwarflist);
  875. {$ifdef GDB}
  876. WriteFileEndInfo;
  877. {$ENDIF}
  878. AsmLn;
  879. {$ifdef EXTDEBUG}
  880. if assigned(current_module.mainsource) then
  881. Comment(V_Debug,'Done writing gas-styled assembler output for '+current_module.mainsource^);
  882. {$endif EXTDEBUG}
  883. end;
  884. end.
  885. {
  886. $Log$
  887. Revision 1.56 2004-07-01 18:16:10 jonas
  888. * Darwin fixes
  889. Revision 1.55 2004/06/20 08:55:28 florian
  890. * logs truncated
  891. Revision 1.54 2004/06/16 20:07:06 florian
  892. * dwarf branch merged
  893. Revision 1.53 2004/05/28 21:13:08 peter
  894. * fix wrong regalloc comments
  895. Revision 1.52 2004/05/22 23:34:27 peter
  896. tai_regalloc.allocation changed to ratype to notify rgobj of register size changes
  897. Revision 1.51 2004/04/27 13:38:24 florian
  898. * fixed wrong commit from yesterday
  899. Revision 1.50 2004/04/25 21:26:16 florian
  900. * some m68k stuff fixed
  901. }