aggas.pas 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 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. cclasses,
  25. globals,
  26. aasmbase,aasmtai,aasmcpu,
  27. assemble;
  28. type
  29. {# This is a derived class which is used to write
  30. GAS styled assembler.
  31. The WriteInstruction() method must be overriden
  32. to write a single instruction to the assembler
  33. file.
  34. }
  35. TGNUAssembler=class(texternalassembler)
  36. public
  37. procedure WriteTree(p:TAAsmoutput);override;
  38. procedure WriteAsmList;override;
  39. procedure WriteExtraHeader;virtual;
  40. {$ifdef GDB}
  41. procedure WriteFileLineInfo(var fileinfo : tfileposinfo);
  42. procedure WriteFileEndInfo;
  43. {$endif}
  44. procedure WriteInstruction(hp: tai); virtual; abstract;
  45. end;
  46. const
  47. regname_count=45;
  48. regname_count_bsstart=32; {Largest power of 2 out of regname_count.}
  49. implementation
  50. uses
  51. {$ifdef Delphi}
  52. dmisc,
  53. {$else Delphi}
  54. dos,
  55. {$endif Delphi}
  56. cutils,globtype,systems,
  57. fmodule,finput,verbose,cpubase,
  58. itcpugas
  59. {$ifdef GDB}
  60. {$ifdef delphi}
  61. ,sysutils
  62. {$else}
  63. ,strings
  64. {$endif}
  65. ,gdb
  66. {$endif GDB}
  67. ;
  68. const
  69. line_length = 70;
  70. var
  71. {$ifdef GDB}
  72. n_line : byte; { different types of source lines }
  73. linecount,
  74. includecount : longint;
  75. funcname : pchar;
  76. stabslastfileinfo : tfileposinfo;
  77. {$endif}
  78. lasTSec : TSection; { last section type written }
  79. lastfileinfo : tfileposinfo;
  80. infile,
  81. lastinfile : tinputfile;
  82. symendcount : longint;
  83. type
  84. t80bitarray = array[0..9] of byte;
  85. t64bitarray = array[0..7] of byte;
  86. t32bitarray = array[0..3] of byte;
  87. {****************************************************************************}
  88. { Support routines }
  89. {****************************************************************************}
  90. function fixline(s:string):string;
  91. {
  92. return s with all leading and ending spaces and tabs removed
  93. }
  94. var
  95. i,j,k : integer;
  96. begin
  97. i:=length(s);
  98. while (i>0) and (s[i] in [#9,' ']) do
  99. dec(i);
  100. j:=1;
  101. while (j<i) and (s[j] in [#9,' ']) do
  102. inc(j);
  103. for k:=j to i do
  104. if s[k] in [#0..#31,#127..#255] then
  105. s[k]:='.';
  106. fixline:=Copy(s,j,i-j+1);
  107. end;
  108. function single2str(d : single) : string;
  109. var
  110. hs : string;
  111. begin
  112. str(d,hs);
  113. { replace space with + }
  114. if hs[1]=' ' then
  115. hs[1]:='+';
  116. single2str:='0d'+hs
  117. end;
  118. function double2str(d : double) : string;
  119. var
  120. hs : string;
  121. begin
  122. str(d,hs);
  123. { replace space with + }
  124. if hs[1]=' ' then
  125. hs[1]:='+';
  126. double2str:='0d'+hs
  127. end;
  128. function extended2str(e : extended) : string;
  129. var
  130. hs : string;
  131. begin
  132. str(e,hs);
  133. { replace space with + }
  134. if hs[1]=' ' then
  135. hs[1]:='+';
  136. extended2str:='0d'+hs
  137. end;
  138. { convert floating point values }
  139. { to correct endian }
  140. procedure swap64bitarray(var t: t64bitarray);
  141. var
  142. b: byte;
  143. begin
  144. b:= t[7];
  145. t[7] := t[0];
  146. t[0] := b;
  147. b := t[6];
  148. t[6] := t[1];
  149. t[1] := b;
  150. b:= t[5];
  151. t[5] := t[2];
  152. t[2] := b;
  153. b:= t[4];
  154. t[4] := t[3];
  155. t[3] := b;
  156. end;
  157. procedure swap32bitarray(var t: t32bitarray);
  158. var
  159. b: byte;
  160. begin
  161. b:= t[1];
  162. t[1]:= t[2];
  163. t[2]:= b;
  164. b:= t[0];
  165. t[0]:= t[3];
  166. t[3]:= b;
  167. end;
  168. const
  169. ait_const2str : array[ait_const_64bit..ait_const_8bit] of string[8]=
  170. (#9'.quad'#9,#9'.long'#9,#9'.short'#9,#9'.byte'#9);
  171. function ait_section2str(s:TSection):string;
  172. begin
  173. ait_section2str:=target_asm.secnames[s];
  174. {$ifdef GDB}
  175. { this is needed for line info in data }
  176. funcname:=nil;
  177. case s of
  178. sec_code : n_line:=n_textline;
  179. sec_data : n_line:=n_dataline;
  180. sec_bss : n_line:=n_bssline;
  181. else n_line:=n_dataline;
  182. end;
  183. {$endif GDB}
  184. LasTSec:=s;
  185. end;
  186. {****************************************************************************}
  187. { GNU Assembler writer }
  188. {****************************************************************************}
  189. {$ifdef GDB}
  190. procedure TGNUAssembler.WriteFileLineInfo(var fileinfo : tfileposinfo);
  191. var
  192. curr_n : byte;
  193. begin
  194. if not ((cs_debuginfo in aktmoduleswitches) or
  195. (cs_gdb_lineinfo in aktglobalswitches)) then
  196. exit;
  197. { file changed ? (must be before line info) }
  198. if (fileinfo.fileindex<>0) and
  199. (stabslastfileinfo.fileindex<>fileinfo.fileindex) then
  200. begin
  201. infile:=current_module.sourcefiles.get_file(fileinfo.fileindex);
  202. if assigned(infile) then
  203. begin
  204. if includecount=0 then
  205. curr_n:=n_sourcefile
  206. else
  207. curr_n:=n_includefile;
  208. if (infile.path^<>'') then
  209. begin
  210. AsmWriteLn(#9'.stabs "'+lower(BsToSlash(FixPath(infile.path^,false)))+'",'+
  211. tostr(curr_n)+',0,0,'+target_asm.labelprefix+'text'+ToStr(IncludeCount));
  212. end;
  213. AsmWriteLn(#9'.stabs "'+lower(FixFileName(infile.name^))+'",'+
  214. tostr(curr_n)+',0,0,'+target_asm.labelprefix+'text'+ToStr(IncludeCount));
  215. AsmWriteLn(target_asm.labelprefix+'text'+ToStr(IncludeCount)+':');
  216. inc(includecount);
  217. { force new line info }
  218. stabslastfileinfo.line:=-1;
  219. end;
  220. end;
  221. { line changed ? }
  222. if (stabslastfileinfo.line<>fileinfo.line) and (fileinfo.line<>0) then
  223. begin
  224. if (n_line=n_textline) and assigned(funcname) and
  225. (target_info.use_function_relative_addresses) then
  226. begin
  227. AsmWriteLn(target_asm.labelprefix+'l'+tostr(linecount)+':');
  228. AsmWrite(#9'.stabn '+tostr(n_line)+',0,'+tostr(fileinfo.line)+','+
  229. target_asm.labelprefix+'l'+tostr(linecount)+' - ');
  230. AsmWritePChar(FuncName);
  231. AsmLn;
  232. inc(linecount);
  233. end
  234. else
  235. AsmWriteLn(#9'.stabd'#9+tostr(n_line)+',0,'+tostr(fileinfo.line));
  236. end;
  237. stabslastfileinfo:=fileinfo;
  238. end;
  239. procedure TGNUAssembler.WriteFileEndInfo;
  240. begin
  241. if not ((cs_debuginfo in aktmoduleswitches) or
  242. (cs_gdb_lineinfo in aktglobalswitches)) then
  243. exit;
  244. AsmLn;
  245. AsmWriteLn(ait_section2str(sec_code));
  246. AsmWriteLn(#9'.stabs "",'+tostr(n_sourcefile)+',0,0,'+target_asm.labelprefix+'etext');
  247. AsmWriteLn(target_asm.labelprefix+'etext:');
  248. end;
  249. {$endif GDB}
  250. procedure TGNUAssembler.WriteTree(p:TAAsmoutput);
  251. const
  252. allocstr : array[boolean] of string[10]=(' released',' allocated');
  253. var
  254. ch : char;
  255. hp : tai;
  256. hp1 : tailineinfo;
  257. consttyp : taitype;
  258. s : string;
  259. found : boolean;
  260. i,pos,l : longint;
  261. InlineLevel : longint;
  262. last_align : longint;
  263. co : comp;
  264. sin : single;
  265. d : double;
  266. {$ifdef cpuextended}
  267. e : extended;
  268. {$endif cpuextended}
  269. do_line : boolean;
  270. begin
  271. if not assigned(p) then
  272. exit;
  273. last_align := 2;
  274. InlineLevel:=0;
  275. { lineinfo is only needed for codesegment (PFV) }
  276. do_line:=(cs_asm_source in aktglobalswitches) or
  277. ((cs_lineinfo in aktmoduleswitches)
  278. and (p=codesegment));
  279. hp:=tai(p.first);
  280. while assigned(hp) do
  281. begin
  282. if not(hp.typ in SkipLineInfo) then
  283. begin
  284. hp1 := hp as tailineinfo;
  285. aktfilepos:=hp1.fileinfo;
  286. {$ifdef GDB}
  287. { write stabs }
  288. if (cs_debuginfo in aktmoduleswitches) or
  289. (cs_gdb_lineinfo in aktglobalswitches) then
  290. WriteFileLineInfo(hp1.fileinfo);
  291. {$endif GDB}
  292. { no line info for inlined code }
  293. if do_line and (inlinelevel=0) then
  294. begin
  295. { load infile }
  296. if lastfileinfo.fileindex<>hp1.fileinfo.fileindex then
  297. begin
  298. infile:=current_module.sourcefiles.get_file(hp1.fileinfo.fileindex);
  299. if assigned(infile) then
  300. begin
  301. { open only if needed !! }
  302. if (cs_asm_source in aktglobalswitches) then
  303. infile.open;
  304. end;
  305. { avoid unnecessary reopens of the same file !! }
  306. lastfileinfo.fileindex:=hp1.fileinfo.fileindex;
  307. { be sure to change line !! }
  308. lastfileinfo.line:=-1;
  309. end;
  310. { write source }
  311. if (cs_asm_source in aktglobalswitches) and
  312. assigned(infile) then
  313. begin
  314. if (infile<>lastinfile) then
  315. begin
  316. AsmWriteLn(target_asm.comment+'['+infile.name^+']');
  317. if assigned(lastinfile) then
  318. lastinfile.close;
  319. end;
  320. if (hp1.fileinfo.line<>lastfileinfo.line) and
  321. ((hp1.fileinfo.line<infile.maxlinebuf) or (InlineLevel>0)) then
  322. begin
  323. if (hp1.fileinfo.line<>0) and
  324. ((infile.linebuf^[hp1.fileinfo.line]>=0) or (InlineLevel>0)) then
  325. AsmWriteLn(target_asm.comment+'['+tostr(hp1.fileinfo.line)+'] '+
  326. fixline(infile.GetLineStr(hp1.fileinfo.line)));
  327. { set it to a negative value !
  328. to make that is has been read already !! PM }
  329. if (infile.linebuf^[hp1.fileinfo.line]>=0) then
  330. infile.linebuf^[hp1.fileinfo.line]:=-infile.linebuf^[hp1.fileinfo.line]-1;
  331. end;
  332. end;
  333. lastfileinfo:=hp1.fileinfo;
  334. lastinfile:=infile;
  335. end;
  336. end;
  337. case hp.typ of
  338. ait_comment :
  339. Begin
  340. AsmWrite(target_asm.comment);
  341. AsmWritePChar(tai_comment(hp).str);
  342. AsmLn;
  343. End;
  344. ait_regalloc :
  345. begin
  346. if (cs_asm_regalloc in aktglobalswitches) then
  347. AsmWriteLn(#9+target_asm.comment+'Register '+gas_regname(Tai_regalloc(hp).reg)+
  348. allocstr[tai_regalloc(hp).allocation]);
  349. end;
  350. ait_tempalloc :
  351. begin
  352. if (cs_asm_tempalloc in aktglobalswitches) then
  353. begin
  354. {$ifdef EXTDEBUG}
  355. if assigned(tai_tempalloc(hp).problem) then
  356. AsmWriteLn(target_asm.comment+'Temp '+tostr(tai_tempalloc(hp).temppos)+','+
  357. tostr(tai_tempalloc(hp).tempsize)+' '+tai_tempalloc(hp).problem^)
  358. else
  359. {$endif EXTDEBUG}
  360. AsmWriteLn(target_asm.comment+'Temp '+tostr(tai_tempalloc(hp).temppos)+','+
  361. tostr(tai_tempalloc(hp).tempsize)+allocstr[tai_tempalloc(hp).allocation]);
  362. end;
  363. end;
  364. ait_align :
  365. begin
  366. if target_info.system <> system_powerpc_darwin then
  367. begin
  368. AsmWrite(#9'.balign '+tostr(tai_align(hp).aligntype));
  369. if tai_align(hp).use_op then
  370. AsmWrite(','+tostr(tai_align(hp).fillop))
  371. end
  372. else
  373. begin
  374. { darwin as only supports .align }
  375. if not ispowerof2(tai_align(hp).aligntype,i) then
  376. internalerror(2003010305);
  377. AsmWrite(#9'.align '+tostr(i));
  378. last_align := i;
  379. end;
  380. AsmLn;
  381. end;
  382. ait_section :
  383. begin
  384. if tai_section(hp).sec<>sec_none then
  385. begin
  386. AsmLn;
  387. AsmWriteLn(ait_section2str(tai_section(hp).sec));
  388. {$ifdef GDB}
  389. lastfileinfo.line:=-1;
  390. {$endif GDB}
  391. end
  392. else
  393. begin
  394. {$ifdef EXTDEBUG}
  395. AsmWrite(target_asm.comment);
  396. AsmWriteln(' sec_none');
  397. {$endif EXTDEBUG}
  398. end;
  399. end;
  400. ait_datablock :
  401. begin
  402. if tai_datablock(hp).is_global then
  403. AsmWrite(#9'.comm'#9)
  404. else
  405. AsmWrite(#9'.lcomm'#9);
  406. AsmWrite(tai_datablock(hp).sym.name);
  407. AsmWrite(','+tostr(tai_datablock(hp).size));
  408. if (target_info.system = system_powerpc_darwin) and
  409. not(tai_datablock(hp).is_global) then
  410. AsmWrite(','+tostr(last_align));
  411. AsmWriteln('');
  412. end;
  413. ait_const_64bit,
  414. ait_const_32bit,
  415. ait_const_16bit,
  416. ait_const_8bit :
  417. begin
  418. AsmWrite(ait_const2str[hp.typ]+tostru(tai_const(hp).value));
  419. consttyp:=hp.typ;
  420. l:=0;
  421. repeat
  422. found:=(not (tai(hp.next)=nil)) and (tai(hp.next).typ=consttyp);
  423. if found then
  424. begin
  425. hp:=tai(hp.next);
  426. s:=','+tostru(tai_const(hp).value);
  427. AsmWrite(s);
  428. inc(l,length(s));
  429. end;
  430. until (not found) or (l>line_length);
  431. AsmLn;
  432. end;
  433. ait_const_symbol :
  434. begin
  435. AsmWrite(#9'.long'#9);
  436. AsmWrite(tai_const_symbol(hp).sym.name);
  437. if tai_const_symbol(hp).offset>0 then
  438. AsmWrite('+'+tostr(tai_const_symbol(hp).offset))
  439. else if tai_const_symbol(hp).offset<0 then
  440. AsmWrite(tostr(tai_const_symbol(hp).offset));
  441. AsmLn;
  442. end;
  443. ait_indirect_symbol :
  444. begin
  445. AsmWrite(#9'.indirect_symbol'#9);
  446. AsmWrite(tai_const_symbol(hp).sym.name);
  447. AsmLn;
  448. end;
  449. ait_const_rva :
  450. begin
  451. AsmWrite(#9'.rva'#9);
  452. AsmWriteLn(tai_const_symbol(hp).sym.name);
  453. end;
  454. {$ifdef cpuextended}
  455. ait_real_80bit :
  456. begin
  457. if do_line then
  458. AsmWriteLn(target_asm.comment+'value: '+extended2str(tai_real_80bit(hp).value));
  459. { Make sure e is a extended type, bestreal could be
  460. a different type (bestreal) !! (PFV) }
  461. e:=tai_real_80bit(hp).value;
  462. AsmWrite(#9'.byte'#9);
  463. for i:=0 to 9 do
  464. begin
  465. if i<>0 then
  466. AsmWrite(',');
  467. AsmWrite(tostr(t80bitarray(e)[i]));
  468. end;
  469. AsmLn;
  470. end;
  471. {$endif cpuextended}
  472. ait_real_64bit :
  473. begin
  474. if do_line then
  475. AsmWriteLn(target_asm.comment+'value: '+double2str(tai_real_64bit(hp).value));
  476. d:=tai_real_64bit(hp).value;
  477. { swap the values to correct endian if required }
  478. if source_info.endian <> target_info.endian then
  479. swap64bitarray(t64bitarray(d));
  480. AsmWrite(#9'.byte'#9);
  481. {$ifdef arm}
  482. { on a real arm cpu, it's already hi/lo swapped }
  483. {$ifndef cpuarm}
  484. if tai_real_64bit(hp).formatoptions=fo_hiloswapped then
  485. begin
  486. for i:=4 to 7 do
  487. begin
  488. if i<>4 then
  489. AsmWrite(',');
  490. AsmWrite(tostr(t64bitarray(d)[i]));
  491. end;
  492. for i:=0 to 3 do
  493. begin
  494. AsmWrite(',');
  495. AsmWrite(tostr(t64bitarray(d)[i]));
  496. end;
  497. end
  498. else
  499. {$endif cpuarm}
  500. {$endif arm}
  501. begin
  502. for i:=0 to 7 do
  503. begin
  504. if i<>0 then
  505. AsmWrite(',');
  506. AsmWrite(tostr(t64bitarray(d)[i]));
  507. end;
  508. end;
  509. AsmLn;
  510. end;
  511. ait_real_32bit :
  512. begin
  513. if do_line then
  514. AsmWriteLn(target_asm.comment+'value: '+single2str(tai_real_32bit(hp).value));
  515. sin:=tai_real_32bit(hp).value;
  516. { swap the values to correct endian if required }
  517. if source_info.endian <> target_info.endian then
  518. swap32bitarray(t32bitarray(sin));
  519. AsmWrite(#9'.byte'#9);
  520. for i:=0 to 3 do
  521. begin
  522. if i<>0 then
  523. AsmWrite(',');
  524. AsmWrite(tostr(t32bitarray(sin)[i]));
  525. end;
  526. AsmLn;
  527. end;
  528. ait_comp_64bit :
  529. begin
  530. if do_line then
  531. AsmWriteLn(target_asm.comment+'value: '+extended2str(tai_comp_64bit(hp).value));
  532. AsmWrite(#9'.byte'#9);
  533. {$ifdef FPC}
  534. co:=comp(tai_comp_64bit(hp).value);
  535. {$else}
  536. co:=tai_comp_64bit(hp).value;
  537. {$endif}
  538. { swap the values to correct endian if required }
  539. if source_info.endian <> target_info.endian then
  540. swap64bitarray(t64bitarray(co));
  541. for i:=0 to 7 do
  542. begin
  543. if i<>0 then
  544. AsmWrite(',');
  545. AsmWrite(tostr(t64bitarray(co)[i]));
  546. end;
  547. AsmLn;
  548. end;
  549. ait_direct :
  550. begin
  551. AsmWritePChar(tai_direct(hp).str);
  552. AsmLn;
  553. {$IfDef GDB}
  554. if strpos(tai_direct(hp).str,'.data')<>nil then
  555. n_line:=n_dataline
  556. else if strpos(tai_direct(hp).str,'.text')<>nil then
  557. n_line:=n_textline
  558. else if strpos(tai_direct(hp).str,'.bss')<>nil then
  559. n_line:=n_bssline;
  560. {$endif GDB}
  561. end;
  562. ait_string :
  563. begin
  564. pos:=0;
  565. for i:=1 to tai_string(hp).len do
  566. begin
  567. if pos=0 then
  568. begin
  569. AsmWrite(#9'.ascii'#9'"');
  570. pos:=20;
  571. end;
  572. ch:=tai_string(hp).str[i-1];
  573. case ch of
  574. #0, {This can't be done by range, because a bug in FPC}
  575. #1..#31,
  576. #128..#255 : s:='\'+tostr(ord(ch) shr 6)+tostr((ord(ch) and 63) shr 3)+tostr(ord(ch) and 7);
  577. '"' : s:='\"';
  578. '\' : s:='\\';
  579. else
  580. s:=ch;
  581. end;
  582. AsmWrite(s);
  583. inc(pos,length(s));
  584. if (pos>line_length) or (i=tai_string(hp).len) then
  585. begin
  586. AsmWriteLn('"');
  587. pos:=0;
  588. end;
  589. end;
  590. end;
  591. ait_label :
  592. begin
  593. if (tai_label(hp).l.is_used) then
  594. begin
  595. if tai_label(hp).l.defbind=AB_GLOBAL then
  596. begin
  597. AsmWrite('.globl'#9);
  598. AsmWriteLn(tai_label(hp).l.name);
  599. end;
  600. AsmWrite(tai_label(hp).l.name);
  601. AsmWriteLn(':');
  602. end;
  603. end;
  604. ait_symbol :
  605. begin
  606. if tai_symbol(hp).is_global then
  607. begin
  608. AsmWrite('.globl'#9);
  609. AsmWriteLn(tai_symbol(hp).sym.name);
  610. end;
  611. if target_info.system in [system_i386_linux,system_i386_beos,
  612. system_powerpc_linux,system_m68k_linux,
  613. system_sparc_linux,system_alpha_linux,
  614. system_x86_64_linux,system_arm_linux] then
  615. begin
  616. AsmWrite(#9'.type'#9);
  617. AsmWrite(tai_symbol(hp).sym.name);
  618. if assigned(tai(hp.next)) and
  619. (tai(hp.next).typ in [ait_const_symbol,ait_const_rva,
  620. ait_const_32bit,ait_const_16bit,ait_const_8bit,ait_datablock,
  621. ait_real_32bit,ait_real_64bit,ait_real_80bit,ait_comp_64bit]) then
  622. begin
  623. if target_info.system = system_arm_linux then
  624. AsmWriteLn(',#object')
  625. else
  626. AsmWriteLn(',@object')
  627. end
  628. else
  629. begin
  630. if target_info.system = system_arm_linux then
  631. AsmWriteLn(',#function')
  632. else
  633. AsmWriteLn(',@function');
  634. end;
  635. if tai_symbol(hp).sym.size>0 then
  636. begin
  637. AsmWrite(#9'.size'#9);
  638. AsmWrite(tai_symbol(hp).sym.name);
  639. AsmWrite(', ');
  640. AsmWriteLn(tostr(tai_symbol(hp).sym.size));
  641. end;
  642. end;
  643. AsmWrite(tai_symbol(hp).sym.name);
  644. AsmWriteLn(':');
  645. end;
  646. ait_symbol_end :
  647. begin
  648. if tf_needs_symbol_size in target_info.flags then
  649. begin
  650. s:=target_asm.labelprefix+'e'+tostr(symendcount);
  651. inc(symendcount);
  652. AsmWriteLn(s+':');
  653. AsmWrite(#9'.size'#9);
  654. AsmWrite(tai_symbol_end(hp).sym.name);
  655. AsmWrite(', '+s+' - ');
  656. AsmWriteLn(tai_symbol_end(hp).sym.name);
  657. end;
  658. end;
  659. ait_instruction :
  660. begin
  661. WriteInstruction(hp);
  662. end;
  663. {$ifdef GDB}
  664. ait_stabs :
  665. begin
  666. if assigned(tai_stabs(hp).str) then
  667. begin
  668. AsmWrite(#9'.stabs ');
  669. AsmWritePChar(tai_stabs(hp).str);
  670. AsmLn;
  671. end;
  672. end;
  673. ait_stabn :
  674. begin
  675. if assigned(tai_stabn(hp).str) then
  676. begin
  677. AsmWrite(#9'.stabn ');
  678. AsmWritePChar(tai_stabn(hp).str);
  679. AsmLn;
  680. end;
  681. end;
  682. ait_force_line :
  683. stabslastfileinfo.line:=0;
  684. ait_stab_function_name:
  685. funcname:=tai_stab_function_name(hp).str;
  686. {$endif GDB}
  687. ait_cut :
  688. begin
  689. if SmartAsm then
  690. begin
  691. { only reset buffer if nothing has changed }
  692. if AsmSize=AsmStartSize then
  693. AsmClear
  694. else
  695. begin
  696. AsmClose;
  697. DoAssemble;
  698. AsmCreate(tai_cut(hp).place);
  699. end;
  700. { avoid empty files }
  701. while assigned(hp.next) and (tai(hp.next).typ in [ait_cut,ait_section,ait_comment]) do
  702. begin
  703. if tai(hp.next).typ=ait_section then
  704. lasTSec:=tai_section(hp.next).sec;
  705. hp:=tai(hp.next);
  706. end;
  707. {$ifdef GDB}
  708. { force write of filename }
  709. FillChar(stabslastfileinfo,sizeof(stabslastfileinfo),0);
  710. includecount:=0;
  711. funcname:=nil;
  712. WriteFileLineInfo(aktfilepos);
  713. {$endif GDB}
  714. if lasTSec<>sec_none then
  715. AsmWriteLn(ait_section2str(lasTSec));
  716. AsmStartSize:=AsmSize;
  717. end;
  718. end;
  719. ait_marker :
  720. if tai_marker(hp).kind=InlineStart then
  721. inc(InlineLevel)
  722. else if tai_marker(hp).kind=InlineEnd then
  723. dec(InlineLevel);
  724. ait_non_lazy_symbol_pointer:
  725. AsmWriteLn('.non_lazy_symbol_pointer');
  726. else
  727. internalerror(10000);
  728. end;
  729. hp:=tai(hp.next);
  730. end;
  731. end;
  732. procedure TGNUAssembler.WriteExtraHeader;
  733. begin
  734. end;
  735. procedure TGNUAssembler.WriteAsmList;
  736. var
  737. p:dirstr;
  738. n:namestr;
  739. e:extstr;
  740. {$ifdef GDB}
  741. fileinfo : tfileposinfo;
  742. {$endif GDB}
  743. begin
  744. {$ifdef EXTDEBUG}
  745. if assigned(current_module.mainsource) then
  746. Comment(V_Debug,'Start writing gas-styled assembler output for '+current_module.mainsource^);
  747. {$endif}
  748. LasTSec:=sec_none;
  749. {$ifdef GDB}
  750. FillChar(stabslastfileinfo,sizeof(stabslastfileinfo),0);
  751. {$endif GDB}
  752. FillChar(lastfileinfo,sizeof(lastfileinfo),0);
  753. LastInfile:=nil;
  754. if assigned(current_module.mainsource) then
  755. fsplit(current_module.mainsource^,p,n,e)
  756. else
  757. begin
  758. p:=inputdir;
  759. n:=inputfile;
  760. e:=inputextension;
  761. end;
  762. { to get symify to work }
  763. AsmWriteLn(#9'.file "'+FixFileName(n+e)+'"');
  764. WriteExtraHeader;
  765. {$ifdef GDB}
  766. n_line:=n_bssline;
  767. funcname:=nil;
  768. linecount:=1;
  769. includecount:=0;
  770. fileinfo.fileindex:=1;
  771. fileinfo.line:=1;
  772. { Write main file }
  773. WriteFileLineInfo(fileinfo);
  774. {$endif GDB}
  775. AsmStartSize:=AsmSize;
  776. symendcount:=0;
  777. If (cs_debuginfo in aktmoduleswitches) then
  778. WriteTree(debuglist);
  779. WriteTree(codesegment);
  780. WriteTree(datasegment);
  781. WriteTree(consts);
  782. WriteTree(rttilist);
  783. WriteTree(picdata);
  784. Writetree(resourcestringlist);
  785. WriteTree(bsssegment);
  786. Writetree(importssection);
  787. { exports are written by DLLTOOL
  788. if we use it so don't insert it twice (PM) }
  789. if not UseDeffileForExport and assigned(exportssection) then
  790. Writetree(exportssection);
  791. Writetree(resourcesection);
  792. {$ifdef GDB}
  793. WriteFileEndInfo;
  794. {$ENDIF}
  795. AsmLn;
  796. {$ifdef EXTDEBUG}
  797. if assigned(current_module.mainsource) then
  798. Comment(V_Debug,'Done writing gas-styled assembler output for '+current_module.mainsource^);
  799. {$endif EXTDEBUG}
  800. end;
  801. end.
  802. {
  803. $Log$
  804. Revision 1.49 2004-04-12 18:59:32 florian
  805. * small x86_64 fixes
  806. Revision 1.48 2004/03/17 22:27:41 florian
  807. * fixed handling of doubles in a native arm compiler
  808. * fixed handling of typed double constants on arm
  809. Revision 1.47 2004/03/02 17:32:12 florian
  810. * make cycle fixed
  811. + pic support for darwin
  812. + support of importing vars from shared libs on darwin implemented
  813. Revision 1.46 2004/02/22 16:51:50 peter
  814. * tf_need_symbol_size added
  815. Revision 1.45 2004/01/24 18:12:40 florian
  816. * fixed several arm floating point issues
  817. Revision 1.44 2004/01/20 21:02:54 florian
  818. * fixed symbol type writing for arm-linux
  819. * fixed assembler generation for abs
  820. Revision 1.43 2004/01/12 16:39:40 peter
  821. * sparc updates, mostly float related
  822. Revision 1.42 2004/01/07 17:40:06 jonas
  823. * darwin requires the alginment of .lcomm symbols to be specified
  824. together with the symbol itself, instead of in a .align directive
  825. Revision 1.41 2004/01/04 21:08:59 jonas
  826. * darwin only supports .align, no .balign
  827. Revision 1.40 2004/01/03 13:51:05 jonas
  828. + support exported procedures for linuxppc
  829. * refuse to compile systems/t_linux.pas if processor-specific support
  830. for exported procedures is absent
  831. + generate .type and .size info for all currently defined linux-variants
  832. in aggas.pas
  833. Revision 1.39 2003/12/14 22:42:54 peter
  834. * fixed range check error
  835. Revision 1.38 2003/12/10 17:13:22 peter
  836. * fix range error with tai_const
  837. Revision 1.37 2003/11/12 16:05:39 florian
  838. * assembler readers OOPed
  839. + typed currency constants
  840. + typed 128 bit float constants if the CPU supports it
  841. Revision 1.36 2003/10/01 20:34:48 peter
  842. * procinfo unit contains tprocinfo
  843. * cginfo renamed to cgbase
  844. * moved cgmessage to verbose
  845. * fixed ppc and sparc compiles
  846. Revision 1.35 2003/09/23 17:56:05 peter
  847. * locals and paras are allocated in the code generation
  848. * tvarsym.localloc contains the location of para/local when
  849. generating code for the current procedure
  850. Revision 1.34 2003/09/06 16:47:24 florian
  851. + support of NaN and Inf in the compiler as values of real constants
  852. Revision 1.33 2003/09/04 00:15:29 florian
  853. * first bunch of adaptions of arm compiler for new register type
  854. Revision 1.32 2003/09/03 19:35:24 peter
  855. * powerpc compiles again
  856. Revision 1.31 2003/09/03 15:55:00 peter
  857. * NEWRA branch merged
  858. Revision 1.30 2003/09/03 11:18:36 florian
  859. * fixed arm concatcopy
  860. + arm support in the common compiler sources added
  861. * moved some generic cg code around
  862. + tfputype added
  863. * ...
  864. Revision 1.29.2.2 2003/09/01 21:02:55 peter
  865. * sparc updates for new tregister
  866. Revision 1.29.2.1 2003/08/31 15:46:26 peter
  867. * more updates for tregister
  868. Revision 1.29 2003/08/19 11:53:03 daniel
  869. * Fixed PowerPC compilation
  870. Revision 1.28 2003/08/18 11:49:47 daniel
  871. * Made ATT asm writer work with -sr
  872. Revision 1.27 2003/08/17 21:11:00 daniel
  873. * Now -sr works...
  874. Revision 1.26 2003/08/17 20:47:47 daniel
  875. * Notranslation changed into -sr functionality
  876. Revision 1.25 2003/08/17 16:59:20 jonas
  877. * fixed regvars so they work with newra (at least for ppc)
  878. * fixed some volatile register bugs
  879. + -dnotranslation option for -dnewra, which causes the registers not to
  880. be translated from virtual to normal registers. Requires support in
  881. the assembler writer as well, which is only implemented in aggas/
  882. agppcgas currently
  883. Revision 1.24 2003/04/28 21:17:53 peter
  884. * write sec_none info in extdebug
  885. Revision 1.23 2003/04/25 20:59:33 peter
  886. * removed funcretn,funcretsym, function result is now in varsym
  887. and aliases for result and function name are added using absolutesym
  888. * vs_hidden parameter for funcret passed in parameter
  889. * vs_hidden fixes
  890. * writenode changed to printnode and released from extdebug
  891. * -vp option added to generate a tree.log with the nodetree
  892. * nicer printnode for statements, callnode
  893. Revision 1.22 2003/04/24 22:29:57 florian
  894. * fixed a lot of PowerPC related stuff
  895. Revision 1.21 2003/04/22 14:33:38 peter
  896. * removed some notes/hints
  897. Revision 1.20 2003/01/09 21:52:37 peter
  898. * merged some verbosity options.
  899. * V_LineInfo is a verbosity flag to include line info
  900. Revision 1.19 2003/01/08 18:43:56 daniel
  901. * Tregister changed into a record
  902. Revision 1.18 2002/12/07 14:03:25 carl
  903. - remove some duplicates and unused vars
  904. Revision 1.17 2002/12/06 17:50:39 peter
  905. * long symbol name fix merged
  906. Revision 1.16 2002/11/17 16:31:55 carl
  907. * memory optimization (3-4%) : cleanup of tai fields,
  908. cleanup of tdef and tsym fields.
  909. * make it work for m68k
  910. Revision 1.15 2002/11/15 01:58:45 peter
  911. * merged changes from 1.0.7 up to 04-11
  912. - -V option for generating bug report tracing
  913. - more tracing for option parsing
  914. - errors for cdecl and high()
  915. - win32 import stabs
  916. - win32 records<=8 are returned in eax:edx (turned off by default)
  917. - heaptrc update
  918. - more info for temp management in .s file with EXTDEBUG
  919. Revision 1.14 2002/10/30 21:01:14 peter
  920. * always include lineno after fileswitch. valgrind requires this
  921. Revision 1.13 2002/10/05 12:43:23 carl
  922. * fixes for Delphi 6 compilation
  923. (warning : Some features do not work under Delphi)
  924. Revision 1.12 2002/08/31 16:05:17 florian
  925. * write double # before float constants when -al is turned on
  926. else some gas versions interpret it as line number
  927. Revision 1.11 2002/08/20 16:55:38 peter
  928. * don't write (stabs)line info when inlining a procedure
  929. Revision 1.10 2002/08/18 22:16:14 florian
  930. + the ppc gas assembler writer adds now registers aliases
  931. to the assembler file
  932. Revision 1.9 2002/08/18 20:06:23 peter
  933. * inlining is now also allowed in interface
  934. * renamed write/load to ppuwrite/ppuload
  935. * tnode storing in ppu
  936. * nld,ncon,nbas are already updated for storing in ppu
  937. Revision 1.8 2002/07/26 21:15:37 florian
  938. * rewrote the system handling
  939. Revision 1.7 2002/07/07 09:52:32 florian
  940. * powerpc target fixed, very simple units can be compiled
  941. * some basic stuff for better callparanode handling, far from being finished
  942. Revision 1.6 2002/07/01 18:46:20 peter
  943. * internal linker
  944. * reorganized aasm layer
  945. Revision 1.5 2002/05/18 13:34:05 peter
  946. * readded missing revisions
  947. Revision 1.4 2002/05/16 19:46:34 carl
  948. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  949. + try to fix temp allocation (still in ifdef)
  950. + generic constructor calls
  951. + start of tassembler / tmodulebase class cleanup
  952. Revision 1.2 2002/04/15 18:53:48 carl
  953. + comments in register allocator uses std_Reg2str
  954. Revision 1.1 2002/04/14 16:51:54 carl
  955. + basic GNU assembler writer class
  956. }