aggas.pas 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002
  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 "'+BsToSlash(FixPath(infile.path^,false))+'",'+
  200. tostr(curr_n)+',0,0,'+target_asm.labelprefix+'text'+ToStr(IncludeCount));
  201. end;
  202. AsmWriteLn(#9'.stabs "'+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. case target_info.system of
  267. system_powerpc_darwin, system_i386_OS2, system_i386_EMX: ;
  268. else
  269. AsmWrite('.section ');
  270. end;
  271. s:=sectionname(atype,aname);
  272. AsmWrite(s);
  273. if copy(s,1,4)='.gnu' then
  274. begin
  275. case atype of
  276. sec_data :
  277. AsmWrite(',""');
  278. sec_code :
  279. AsmWrite(',"x"');
  280. end;
  281. end;
  282. AsmLn;
  283. {$ifdef GDB}
  284. { this is needed for line info in data }
  285. funcname:=nil;
  286. case atype of
  287. sec_code :
  288. n_line:=n_textline;
  289. sec_data :
  290. n_line:=n_dataline;
  291. sec_bss :
  292. n_line:=n_bssline;
  293. else
  294. n_line:=n_dataline;
  295. end;
  296. {$endif GDB}
  297. LasTSecType:=atype;
  298. end;
  299. procedure TGNUAssembler.WriteTree(p:TAAsmoutput);
  300. const
  301. regallocstr : array[tregalloctype] of string[10]=(' allocated',' released',' sync',' resized');
  302. tempallocstr : array[boolean] of string[10]=(' released',' allocated');
  303. var
  304. ch : char;
  305. hp : tai;
  306. hp1 : tailineinfo;
  307. consttyp : taitype;
  308. s : string;
  309. i,pos,l : longint;
  310. InlineLevel : longint;
  311. last_align : longint;
  312. co : comp;
  313. sin : single;
  314. d : double;
  315. {$ifdef cpuextended}
  316. e : extended;
  317. {$endif cpuextended}
  318. do_line : boolean;
  319. begin
  320. if not assigned(p) then
  321. exit;
  322. last_align := 2;
  323. InlineLevel:=0;
  324. { lineinfo is only needed for codesegment (PFV) }
  325. do_line:=(cs_asm_source in aktglobalswitches) or
  326. ((cs_lineinfo in aktmoduleswitches)
  327. and (p=codesegment));
  328. hp:=tai(p.first);
  329. while assigned(hp) do
  330. begin
  331. if not(hp.typ in SkipLineInfo) then
  332. begin
  333. hp1 := hp as tailineinfo;
  334. aktfilepos:=hp1.fileinfo;
  335. {$ifdef GDB}
  336. { write stabs }
  337. if (cs_debuginfo in aktmoduleswitches) or
  338. (cs_gdb_lineinfo in aktglobalswitches) then
  339. WriteFileLineInfo(hp1.fileinfo);
  340. {$endif GDB}
  341. { no line info for inlined code }
  342. if do_line and (inlinelevel=0) then
  343. begin
  344. { load infile }
  345. if lastfileinfo.fileindex<>hp1.fileinfo.fileindex then
  346. begin
  347. infile:=current_module.sourcefiles.get_file(hp1.fileinfo.fileindex);
  348. if assigned(infile) then
  349. begin
  350. { open only if needed !! }
  351. if (cs_asm_source in aktglobalswitches) then
  352. infile.open;
  353. end;
  354. { avoid unnecessary reopens of the same file !! }
  355. lastfileinfo.fileindex:=hp1.fileinfo.fileindex;
  356. { be sure to change line !! }
  357. lastfileinfo.line:=-1;
  358. end;
  359. { write source }
  360. if (cs_asm_source in aktglobalswitches) and
  361. assigned(infile) then
  362. begin
  363. if (infile<>lastinfile) then
  364. begin
  365. AsmWriteLn(target_asm.comment+'['+infile.name^+']');
  366. if assigned(lastinfile) then
  367. lastinfile.close;
  368. end;
  369. if (hp1.fileinfo.line<>lastfileinfo.line) and
  370. ((hp1.fileinfo.line<infile.maxlinebuf) or (InlineLevel>0)) then
  371. begin
  372. if (hp1.fileinfo.line<>0) and
  373. ((infile.linebuf^[hp1.fileinfo.line]>=0) or (InlineLevel>0)) then
  374. AsmWriteLn(target_asm.comment+'['+tostr(hp1.fileinfo.line)+'] '+
  375. fixline(infile.GetLineStr(hp1.fileinfo.line)));
  376. { set it to a negative value !
  377. to make that is has been read already !! PM }
  378. if (infile.linebuf^[hp1.fileinfo.line]>=0) then
  379. infile.linebuf^[hp1.fileinfo.line]:=-infile.linebuf^[hp1.fileinfo.line]-1;
  380. end;
  381. end;
  382. lastfileinfo:=hp1.fileinfo;
  383. lastinfile:=infile;
  384. end;
  385. end;
  386. case hp.typ of
  387. ait_comment :
  388. Begin
  389. AsmWrite(target_asm.comment);
  390. AsmWritePChar(tai_comment(hp).str);
  391. AsmLn;
  392. End;
  393. ait_regalloc :
  394. begin
  395. if (cs_asm_regalloc in aktglobalswitches) then
  396. begin
  397. AsmWrite(#9+target_asm.comment+'Register ');
  398. repeat
  399. AsmWrite(gas_regname(Tai_regalloc(hp).reg));
  400. if (hp.next=nil) or
  401. (tai(hp.next).typ<>ait_regalloc) or
  402. (tai_regalloc(hp.next).ratype<>tai_regalloc(hp).ratype) then
  403. break;
  404. hp:=tai(hp.next);
  405. AsmWrite(',');
  406. until false;
  407. AsmWriteLn(regallocstr[tai_regalloc(hp).ratype]);
  408. end;
  409. end;
  410. ait_tempalloc :
  411. begin
  412. if (cs_asm_tempalloc in aktglobalswitches) then
  413. begin
  414. {$ifdef EXTDEBUG}
  415. if assigned(tai_tempalloc(hp).problem) then
  416. AsmWriteLn(target_asm.comment+'Temp '+tostr(tai_tempalloc(hp).temppos)+','+
  417. tostr(tai_tempalloc(hp).tempsize)+' '+tai_tempalloc(hp).problem^)
  418. else
  419. {$endif EXTDEBUG}
  420. AsmWriteLn(target_asm.comment+'Temp '+tostr(tai_tempalloc(hp).temppos)+','+
  421. tostr(tai_tempalloc(hp).tempsize)+tempallocstr[tai_tempalloc(hp).allocation]);
  422. end;
  423. end;
  424. ait_align :
  425. begin
  426. if tai_align(hp).aligntype>1 then
  427. begin
  428. if target_info.system <> system_powerpc_darwin then
  429. begin
  430. AsmWrite(#9'.balign '+tostr(tai_align(hp).aligntype));
  431. if tai_align(hp).use_op then
  432. AsmWrite(','+tostr(tai_align(hp).fillop))
  433. end
  434. else
  435. begin
  436. { darwin as only supports .align }
  437. if not ispowerof2(tai_align(hp).aligntype,i) then
  438. internalerror(2003010305);
  439. AsmWrite(#9'.align '+tostr(i));
  440. last_align := i;
  441. end;
  442. AsmLn;
  443. end;
  444. end;
  445. ait_section :
  446. begin
  447. if tai_section(hp).sectype<>sec_none then
  448. begin
  449. WriteSection(tai_section(hp).sectype,tai_section(hp).name^);
  450. {$ifdef GDB}
  451. lastfileinfo.line:=-1;
  452. {$endif GDB}
  453. end
  454. else
  455. begin
  456. {$ifdef EXTDEBUG}
  457. AsmWrite(target_asm.comment);
  458. AsmWriteln(' sec_none');
  459. {$endif EXTDEBUG}
  460. end;
  461. end;
  462. ait_datablock :
  463. begin
  464. if tai_datablock(hp).is_global then
  465. AsmWrite(#9'.comm'#9)
  466. else
  467. AsmWrite(#9'.lcomm'#9);
  468. AsmWrite(tai_datablock(hp).sym.name);
  469. AsmWrite(','+tostr(tai_datablock(hp).size));
  470. if (target_info.system = system_powerpc_darwin) and
  471. not(tai_datablock(hp).is_global) then
  472. AsmWrite(','+tostr(last_align));
  473. AsmWriteln('');
  474. end;
  475. {$ifndef cpu64bit}
  476. ait_const_128bit :
  477. begin
  478. internalerror(200404291);
  479. end;
  480. ait_const_64bit :
  481. begin
  482. if assigned(tai_const(hp).sym) then
  483. internalerror(200404292);
  484. AsmWrite(ait_const2str[ait_const_32bit]);
  485. if target_info.endian = endian_little then
  486. begin
  487. AsmWrite(tostr(longint(lo(tai_const(hp).value))));
  488. AsmWrite(',');
  489. AsmWrite(tostr(longint(hi(tai_const(hp).value))));
  490. end
  491. else
  492. begin
  493. AsmWrite(tostr(longint(hi(tai_const(hp).value))));
  494. AsmWrite(',');
  495. AsmWrite(tostr(longint(lo(tai_const(hp).value))));
  496. end;
  497. AsmLn;
  498. end;
  499. {$endif cpu64bit}
  500. ait_const_uleb128bit,
  501. ait_const_sleb128bit,
  502. {$ifdef cpu64bit}
  503. ait_const_128bit,
  504. ait_const_64bit,
  505. {$endif cpu64bit}
  506. ait_const_32bit,
  507. ait_const_16bit,
  508. ait_const_8bit,
  509. ait_const_rva_symbol,
  510. ait_const_indirect_symbol :
  511. begin
  512. AsmWrite(ait_const2str[hp.typ]);
  513. consttyp:=hp.typ;
  514. l:=0;
  515. repeat
  516. if assigned(tai_const(hp).sym) then
  517. begin
  518. if assigned(tai_const(hp).endsym) then
  519. s:=tai_const(hp).endsym.name+'-'+tai_const(hp).sym.name
  520. else
  521. s:=tai_const(hp).sym.name;
  522. if tai_const(hp).value<>0 then
  523. s:=s+tostr_with_plus(tai_const(hp).value);
  524. end
  525. else
  526. s:=tostr(tai_const(hp).value);
  527. AsmWrite(s);
  528. inc(l,length(s));
  529. if (LasTSecType<>sec_data) or
  530. (l>line_length) or
  531. (hp.next=nil) or
  532. (tai(hp.next).typ<>consttyp) then
  533. break;
  534. hp:=tai(hp.next);
  535. AsmWrite(',');
  536. until false;
  537. AsmLn;
  538. end;
  539. {$ifdef cpuextended}
  540. ait_real_80bit :
  541. begin
  542. if do_line then
  543. AsmWriteLn(target_asm.comment+'value: '+extended2str(tai_real_80bit(hp).value));
  544. { Make sure e is a extended type, bestreal could be
  545. a different type (bestreal) !! (PFV) }
  546. e:=tai_real_80bit(hp).value;
  547. AsmWrite(#9'.byte'#9);
  548. for i:=0 to 9 do
  549. begin
  550. if i<>0 then
  551. AsmWrite(',');
  552. AsmWrite(tostr(t80bitarray(e)[i]));
  553. end;
  554. AsmLn;
  555. end;
  556. {$endif cpuextended}
  557. ait_real_64bit :
  558. begin
  559. if do_line then
  560. AsmWriteLn(target_asm.comment+'value: '+double2str(tai_real_64bit(hp).value));
  561. d:=tai_real_64bit(hp).value;
  562. { swap the values to correct endian if required }
  563. if source_info.endian <> target_info.endian then
  564. swap64bitarray(t64bitarray(d));
  565. AsmWrite(#9'.byte'#9);
  566. {$ifdef arm}
  567. { on a real arm cpu, it's already hi/lo swapped }
  568. {$ifndef cpuarm}
  569. if tai_real_64bit(hp).formatoptions=fo_hiloswapped then
  570. begin
  571. for i:=4 to 7 do
  572. begin
  573. if i<>4 then
  574. AsmWrite(',');
  575. AsmWrite(tostr(t64bitarray(d)[i]));
  576. end;
  577. for i:=0 to 3 do
  578. begin
  579. AsmWrite(',');
  580. AsmWrite(tostr(t64bitarray(d)[i]));
  581. end;
  582. end
  583. else
  584. {$endif cpuarm}
  585. {$endif arm}
  586. begin
  587. for i:=0 to 7 do
  588. begin
  589. if i<>0 then
  590. AsmWrite(',');
  591. AsmWrite(tostr(t64bitarray(d)[i]));
  592. end;
  593. end;
  594. AsmLn;
  595. end;
  596. ait_real_32bit :
  597. begin
  598. if do_line then
  599. AsmWriteLn(target_asm.comment+'value: '+single2str(tai_real_32bit(hp).value));
  600. sin:=tai_real_32bit(hp).value;
  601. { swap the values to correct endian if required }
  602. if source_info.endian <> target_info.endian then
  603. swap32bitarray(t32bitarray(sin));
  604. AsmWrite(#9'.byte'#9);
  605. for i:=0 to 3 do
  606. begin
  607. if i<>0 then
  608. AsmWrite(',');
  609. AsmWrite(tostr(t32bitarray(sin)[i]));
  610. end;
  611. AsmLn;
  612. end;
  613. ait_comp_64bit :
  614. begin
  615. if do_line then
  616. AsmWriteLn(target_asm.comment+'value: '+extended2str(tai_comp_64bit(hp).value));
  617. AsmWrite(#9'.byte'#9);
  618. {$ifdef FPC}
  619. co:=comp(tai_comp_64bit(hp).value);
  620. {$else}
  621. co:=tai_comp_64bit(hp).value;
  622. {$endif}
  623. { swap the values to correct endian if required }
  624. if source_info.endian <> target_info.endian then
  625. swap64bitarray(t64bitarray(co));
  626. for i:=0 to 7 do
  627. begin
  628. if i<>0 then
  629. AsmWrite(',');
  630. AsmWrite(tostr(t64bitarray(co)[i]));
  631. end;
  632. AsmLn;
  633. end;
  634. ait_direct :
  635. begin
  636. AsmWritePChar(tai_direct(hp).str);
  637. AsmLn;
  638. {$IfDef GDB}
  639. if strpos(tai_direct(hp).str,'.data')<>nil then
  640. n_line:=n_dataline
  641. else if strpos(tai_direct(hp).str,'.text')<>nil then
  642. n_line:=n_textline
  643. else if strpos(tai_direct(hp).str,'.bss')<>nil then
  644. n_line:=n_bssline;
  645. {$endif GDB}
  646. end;
  647. ait_string :
  648. begin
  649. pos:=0;
  650. for i:=1 to tai_string(hp).len do
  651. begin
  652. if pos=0 then
  653. begin
  654. AsmWrite(#9'.ascii'#9'"');
  655. pos:=20;
  656. end;
  657. ch:=tai_string(hp).str[i-1];
  658. case ch of
  659. #0, {This can't be done by range, because a bug in FPC}
  660. #1..#31,
  661. #128..#255 : s:='\'+tostr(ord(ch) shr 6)+tostr((ord(ch) and 63) shr 3)+tostr(ord(ch) and 7);
  662. '"' : s:='\"';
  663. '\' : s:='\\';
  664. else
  665. s:=ch;
  666. end;
  667. AsmWrite(s);
  668. inc(pos,length(s));
  669. if (pos>line_length) or (i=tai_string(hp).len) then
  670. begin
  671. AsmWriteLn('"');
  672. pos:=0;
  673. end;
  674. end;
  675. end;
  676. ait_label :
  677. begin
  678. if (tai_label(hp).l.is_used) then
  679. begin
  680. if tai_label(hp).l.defbind=AB_GLOBAL then
  681. begin
  682. AsmWrite('.globl'#9);
  683. AsmWriteLn(tai_label(hp).l.name);
  684. end;
  685. AsmWrite(tai_label(hp).l.name);
  686. AsmWriteLn(':');
  687. end;
  688. end;
  689. ait_symbol :
  690. begin
  691. if tai_symbol(hp).is_global then
  692. begin
  693. AsmWrite('.globl'#9);
  694. AsmWriteLn(tai_symbol(hp).sym.name);
  695. end;
  696. if target_info.system in [system_i386_linux,system_i386_beos,
  697. system_powerpc_linux,system_m68k_linux,
  698. system_sparc_linux,system_alpha_linux,
  699. system_x86_64_linux,system_arm_linux] then
  700. begin
  701. AsmWrite(#9'.type'#9);
  702. AsmWrite(tai_symbol(hp).sym.name);
  703. if assigned(tai(hp.next)) and
  704. (tai(hp.next).typ in [ait_const_rva_symbol,
  705. ait_const_32bit,ait_const_16bit,ait_const_8bit,ait_datablock,
  706. ait_real_32bit,ait_real_64bit,ait_real_80bit,ait_comp_64bit]) then
  707. begin
  708. if target_info.system = system_arm_linux then
  709. AsmWriteLn(',#object')
  710. else
  711. AsmWriteLn(',@object')
  712. end
  713. else
  714. begin
  715. if target_info.system = system_arm_linux then
  716. AsmWriteLn(',#function')
  717. else
  718. AsmWriteLn(',@function');
  719. end;
  720. if tai_symbol(hp).sym.size>0 then
  721. begin
  722. AsmWrite(#9'.size'#9);
  723. AsmWrite(tai_symbol(hp).sym.name);
  724. AsmWrite(', ');
  725. AsmWriteLn(tostr(tai_symbol(hp).sym.size));
  726. end;
  727. end;
  728. AsmWrite(tai_symbol(hp).sym.name);
  729. AsmWriteLn(':');
  730. end;
  731. ait_symbol_end :
  732. begin
  733. if tf_needs_symbol_size in target_info.flags then
  734. begin
  735. s:=target_asm.labelprefix+'e'+tostr(symendcount);
  736. inc(symendcount);
  737. AsmWriteLn(s+':');
  738. AsmWrite(#9'.size'#9);
  739. AsmWrite(tai_symbol_end(hp).sym.name);
  740. AsmWrite(', '+s+' - ');
  741. AsmWriteLn(tai_symbol_end(hp).sym.name);
  742. end;
  743. end;
  744. ait_instruction :
  745. begin
  746. WriteInstruction(hp);
  747. end;
  748. {$ifdef GDB}
  749. ait_stabs :
  750. begin
  751. if assigned(tai_stabs(hp).str) then
  752. begin
  753. AsmWrite(#9'.stabs ');
  754. AsmWritePChar(tai_stabs(hp).str);
  755. AsmLn;
  756. end;
  757. end;
  758. ait_stabn :
  759. begin
  760. if assigned(tai_stabn(hp).str) then
  761. begin
  762. AsmWrite(#9'.stabn ');
  763. AsmWritePChar(tai_stabn(hp).str);
  764. AsmLn;
  765. end;
  766. end;
  767. ait_force_line :
  768. stabslastfileinfo.line:=0;
  769. ait_stab_function_name:
  770. funcname:=tai_stab_function_name(hp).str;
  771. {$endif GDB}
  772. ait_cutobject :
  773. begin
  774. if SmartAsm then
  775. begin
  776. { only reset buffer if nothing has changed }
  777. if AsmSize=AsmStartSize then
  778. AsmClear
  779. else
  780. begin
  781. AsmClose;
  782. DoAssemble;
  783. AsmCreate(tai_cutobject(hp).place);
  784. end;
  785. { avoid empty files }
  786. while assigned(hp.next) and (tai(hp.next).typ in [ait_cutobject,ait_section,ait_comment]) do
  787. begin
  788. if tai(hp.next).typ=ait_section then
  789. lasTSectype:=tai_section(hp.next).sectype;
  790. hp:=tai(hp.next);
  791. end;
  792. {$ifdef GDB}
  793. { force write of filename }
  794. FillChar(stabslastfileinfo,sizeof(stabslastfileinfo),0);
  795. includecount:=0;
  796. funcname:=nil;
  797. WriteFileLineInfo(aktfilepos);
  798. {$endif GDB}
  799. if lasTSectype<>sec_none then
  800. WriteSection(lasTSectype,'');
  801. AsmStartSize:=AsmSize;
  802. end;
  803. end;
  804. ait_marker :
  805. if tai_marker(hp).kind=InlineStart then
  806. inc(InlineLevel)
  807. else if tai_marker(hp).kind=InlineEnd then
  808. dec(InlineLevel);
  809. ait_non_lazy_symbol_pointer:
  810. AsmWriteLn('.non_lazy_symbol_pointer');
  811. else
  812. internalerror(10000);
  813. end;
  814. hp:=tai(hp.next);
  815. end;
  816. end;
  817. procedure TGNUAssembler.WriteExtraHeader;
  818. begin
  819. end;
  820. procedure TGNUAssembler.WriteAsmList;
  821. var
  822. p:dirstr;
  823. n:namestr;
  824. e:extstr;
  825. {$ifdef GDB}
  826. fileinfo : tfileposinfo;
  827. {$endif GDB}
  828. begin
  829. {$ifdef EXTDEBUG}
  830. if assigned(current_module.mainsource) then
  831. Comment(V_Debug,'Start writing gas-styled assembler output for '+current_module.mainsource^);
  832. {$endif}
  833. LasTSectype:=sec_none;
  834. {$ifdef GDB}
  835. FillChar(stabslastfileinfo,sizeof(stabslastfileinfo),0);
  836. {$endif GDB}
  837. FillChar(lastfileinfo,sizeof(lastfileinfo),0);
  838. LastInfile:=nil;
  839. if assigned(current_module.mainsource) then
  840. fsplit(current_module.mainsource^,p,n,e)
  841. else
  842. begin
  843. p:=inputdir;
  844. n:=inputfile;
  845. e:=inputextension;
  846. end;
  847. { to get symify to work }
  848. AsmWriteLn(#9'.file "'+FixFileName(n+e)+'"');
  849. WriteExtraHeader;
  850. {$ifdef GDB}
  851. n_line:=n_bssline;
  852. funcname:=nil;
  853. linecount:=1;
  854. includecount:=0;
  855. fileinfo.fileindex:=1;
  856. fileinfo.line:=1;
  857. { Write main file }
  858. WriteFileLineInfo(fileinfo);
  859. {$endif GDB}
  860. AsmStartSize:=AsmSize;
  861. symendcount:=0;
  862. If (cs_debuginfo in aktmoduleswitches) then
  863. WriteTree(debuglist);
  864. WriteTree(codesegment);
  865. WriteTree(datasegment);
  866. WriteTree(consts);
  867. WriteTree(rttilist);
  868. WriteTree(picdata);
  869. Writetree(resourcestringlist);
  870. WriteTree(bsssegment);
  871. Writetree(importssection);
  872. { exports are written by DLLTOOL
  873. if we use it so don't insert it twice (PM) }
  874. if not UseDeffileForExports and assigned(exportssection) then
  875. Writetree(exportssection);
  876. Writetree(resourcesection);
  877. Writetree(dwarflist);
  878. {$ifdef GDB}
  879. WriteFileEndInfo;
  880. {$ENDIF}
  881. AsmLn;
  882. {$ifdef EXTDEBUG}
  883. if assigned(current_module.mainsource) then
  884. Comment(V_Debug,'Done writing gas-styled assembler output for '+current_module.mainsource^);
  885. {$endif EXTDEBUG}
  886. end;
  887. end.
  888. {
  889. $Log$
  890. Revision 1.59 2004-09-26 17:45:29 peter
  891. * simple regvar support, not yet finished
  892. Revision 1.58 2004/08/27 20:53:52 peter
  893. don't lowercase filenames in stabs
  894. Revision 1.57 2004/07/18 22:04:55 hajny
  895. * fix for OS/2 and EMX - .section not supported by as.exe
  896. Revision 1.56 2004/07/01 18:16:10 jonas
  897. * Darwin fixes
  898. Revision 1.55 2004/06/20 08:55:28 florian
  899. * logs truncated
  900. Revision 1.54 2004/06/16 20:07:06 florian
  901. * dwarf branch merged
  902. Revision 1.53 2004/05/28 21:13:08 peter
  903. * fix wrong regalloc comments
  904. Revision 1.52 2004/05/22 23:34:27 peter
  905. tai_regalloc.allocation changed to ratype to notify rgobj of register size changes
  906. Revision 1.51 2004/04/27 13:38:24 florian
  907. * fixed wrong commit from yesterday
  908. Revision 1.50 2004/04/25 21:26:16 florian
  909. * some m68k stuff fixed
  910. }