aggas.pas 32 KB

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