aggas.pas 31 KB

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