aggas.pas 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206
  1. {
  2. Copyright (c) 1998-2006 by the Free Pascal team
  3. This unit implements the generic part of the GNU assembler
  4. (v2.8 or later) writer
  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. globtype,globals,
  26. aasmbase,aasmtai,aasmdata,aasmcpu,
  27. assemble;
  28. type
  29. TCPUInstrWriter = class;
  30. {# This is a derived class which is used to write
  31. GAS styled assembler.
  32. }
  33. TGNUAssembler=class(texternalassembler)
  34. protected
  35. function sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;virtual;
  36. procedure WriteSection(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder);
  37. procedure WriteExtraHeader;virtual;
  38. procedure WriteInstruction(hp: tai);
  39. public
  40. function MakeCmdLine: TCmdStr; override;
  41. procedure WriteTree(p:TAsmList);override;
  42. procedure WriteAsmList;override;
  43. destructor destroy; override;
  44. private
  45. setcount: longint;
  46. procedure WriteDecodedSleb128(a: int64);
  47. procedure WriteDecodedUleb128(a: qword);
  48. function NextSetLabel: string;
  49. protected
  50. InstrWriter: TCPUInstrWriter;
  51. end;
  52. {# This is the base class for writing instructions.
  53. The WriteInstruction() method must be overriden
  54. to write a single instruction to the assembler
  55. file.
  56. }
  57. TCPUInstrWriter = class
  58. constructor create(_owner: TGNUAssembler);
  59. procedure WriteInstruction(hp : tai); virtual; abstract;
  60. protected
  61. owner: TGNUAssembler;
  62. end;
  63. TAppleGNUAssembler=class(TGNUAssembler)
  64. function sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;override;
  65. private
  66. debugframecount: aint;
  67. end;
  68. TAoutGNUAssembler=class(TGNUAssembler)
  69. function sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;override;
  70. end;
  71. implementation
  72. uses
  73. SysUtils,
  74. cutils,cfileutl,systems,
  75. fmodule,finput,verbose,
  76. itcpugas,cpubase
  77. ;
  78. const
  79. line_length = 70;
  80. var
  81. CurrSecType : TAsmSectiontype; { last section type written }
  82. lastfileinfo : tfileposinfo;
  83. infile,
  84. lastinfile : tinputfile;
  85. symendcount : longint;
  86. type
  87. {$ifdef cpuextended}
  88. t80bitarray = array[0..9] of byte;
  89. {$endif cpuextended}
  90. t64bitarray = array[0..7] of byte;
  91. t32bitarray = array[0..3] of byte;
  92. {****************************************************************************}
  93. { Support routines }
  94. {****************************************************************************}
  95. function fixline(s:string):string;
  96. {
  97. return s with all leading and ending spaces and tabs removed
  98. }
  99. var
  100. i,j,k : integer;
  101. begin
  102. i:=length(s);
  103. while (i>0) and (s[i] in [#9,' ']) do
  104. dec(i);
  105. j:=1;
  106. while (j<i) and (s[j] in [#9,' ']) do
  107. inc(j);
  108. for k:=j to i do
  109. if s[k] in [#0..#31,#127..#255] then
  110. s[k]:='.';
  111. fixline:=Copy(s,j,i-j+1);
  112. end;
  113. function single2str(d : single) : string;
  114. var
  115. hs : string;
  116. begin
  117. str(d,hs);
  118. { replace space with + }
  119. if hs[1]=' ' then
  120. hs[1]:='+';
  121. single2str:='0d'+hs
  122. end;
  123. function double2str(d : double) : string;
  124. var
  125. hs : string;
  126. begin
  127. str(d,hs);
  128. { replace space with + }
  129. if hs[1]=' ' then
  130. hs[1]:='+';
  131. double2str:='0d'+hs
  132. end;
  133. function extended2str(e : extended) : string;
  134. var
  135. hs : string;
  136. begin
  137. str(e,hs);
  138. { replace space with + }
  139. if hs[1]=' ' then
  140. hs[1]:='+';
  141. extended2str:='0d'+hs
  142. end;
  143. { convert floating point values }
  144. { to correct endian }
  145. procedure swap64bitarray(var t: t64bitarray);
  146. var
  147. b: byte;
  148. begin
  149. b:= t[7];
  150. t[7] := t[0];
  151. t[0] := b;
  152. b := t[6];
  153. t[6] := t[1];
  154. t[1] := b;
  155. b:= t[5];
  156. t[5] := t[2];
  157. t[2] := b;
  158. b:= t[4];
  159. t[4] := t[3];
  160. t[3] := b;
  161. end;
  162. procedure swap32bitarray(var t: t32bitarray);
  163. var
  164. b: byte;
  165. begin
  166. b:= t[1];
  167. t[1]:= t[2];
  168. t[2]:= b;
  169. b:= t[0];
  170. t[0]:= t[3];
  171. t[3]:= b;
  172. end;
  173. const
  174. ait_const2str : array[aitconst_128bit..aitconst_indirect_symbol] of string[20]=(
  175. #9'.fixme128'#9,#9'.quad'#9,#9'.long'#9,#9'.short'#9,#9'.byte'#9,
  176. #9'.sleb128'#9,#9'.uleb128'#9,
  177. #9'.rva'#9,#9'.secrel32'#9,#9'.indirect_symbol'#9
  178. );
  179. {****************************************************************************}
  180. { GNU Assembler writer }
  181. {****************************************************************************}
  182. destructor TGNUAssembler.Destroy;
  183. begin
  184. InstrWriter.free;
  185. inherited destroy;
  186. end;
  187. function TGNUAssembler.MakeCmdLine: TCmdStr;
  188. begin
  189. result := inherited MakeCmdLine;
  190. // MWE: disabled again. It generates dwarf info for the generated .s
  191. // files as well. This conflicts with the info we generate
  192. // if target_dbg.id = dbg_dwarf then
  193. // result := result + ' --gdwarf-2';
  194. end;
  195. function TGNUAssembler.NextSetLabel: string;
  196. begin
  197. inc(setcount);
  198. result := target_asm.labelprefix+'$set$'+tostr(setcount);
  199. end;
  200. function TGNUAssembler.sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;
  201. const
  202. secnames : array[TAsmSectiontype] of string[17] = ('',
  203. '.text',
  204. '.data',
  205. { why doesn't .rodata work? (FK) }
  206. { sometimes we have to create a data.rel.ro instead of .rodata, e.g. for }
  207. { vtables (and anything else containing relocations), otherwise those are }
  208. { not relocated properly on e.g. linux/ppc64. g++ generates there for a }
  209. { vtable for a class called Window: }
  210. { .section .data.rel.ro._ZTV6Window,"awG",@progbits,_ZTV6Window,comdat }
  211. {$warning TODO .data.ro not yet working}
  212. {$if defined(arm) or defined(powerpc)}
  213. '.rodata',
  214. {$else arm}
  215. '.data',
  216. {$endif arm}
  217. '.rodata',
  218. '.bss',
  219. '.threadvar',
  220. '.pdata',
  221. '', { stubs }
  222. '.stab',
  223. '.stabstr',
  224. '.idata$2','.idata$4','.idata$5','.idata$6','.idata$7','.edata',
  225. '.eh_frame',
  226. '.debug_frame','.debug_info','.debug_line','.debug_abbrev',
  227. '.fpc',
  228. '.toc',
  229. '.init',
  230. '.fini'
  231. );
  232. secnames_pic : array[TAsmSectiontype] of string[17] = ('',
  233. '.text',
  234. '.data.rel',
  235. '.data.rel',
  236. '.data.rel',
  237. '.bss',
  238. '.threadvar',
  239. '.pdata',
  240. '', { stubs }
  241. '.stab',
  242. '.stabstr',
  243. '.idata$2','.idata$4','.idata$5','.idata$6','.idata$7','.edata',
  244. '.eh_frame',
  245. '.debug_frame','.debug_info','.debug_line','.debug_abbrev',
  246. '.fpc',
  247. '.toc',
  248. '.init',
  249. '.fini'
  250. );
  251. var
  252. sep : string[3];
  253. secname : string;
  254. begin
  255. if (cs_create_pic in current_settings.moduleswitches) and
  256. not(target_info.system in systems_darwin) then
  257. secname:=secnames_pic[atype]
  258. else
  259. secname:=secnames[atype];
  260. {$ifdef m68k}
  261. { old Amiga GNU AS doesn't support .section .fpc }
  262. if (atype=sec_fpc) and (target_info.system = system_m68k_amiga) then
  263. secname:=secnames[sec_data];
  264. {$endif}
  265. if (atype=sec_fpc) and (Copy(aname,1,3)='res') then
  266. begin
  267. result:=secname+'.'+aname;
  268. exit;
  269. end;
  270. if (atype=sec_threadvar) and
  271. (target_info.system=system_i386_win32) then
  272. secname:='.tls';
  273. { For bss we need to set some flags that are target dependent,
  274. it is easier to disable it for smartlinking. It doesn't take up
  275. filespace }
  276. if not(target_info.system in systems_darwin) and
  277. create_smartlink_sections and
  278. (aname<>'') and
  279. (atype <> sec_toc) and
  280. (atype<>sec_bss) then
  281. begin
  282. case aorder of
  283. secorder_begin :
  284. sep:='.b_';
  285. secorder_end :
  286. sep:='.z_';
  287. else
  288. sep:='.n_';
  289. end;
  290. result:=secname+sep+aname
  291. end
  292. else
  293. result:=secname;
  294. end;
  295. procedure TGNUAssembler.WriteSection(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder);
  296. var
  297. s : string;
  298. begin
  299. AsmLn;
  300. case target_info.system of
  301. system_i386_OS2,
  302. system_i386_EMX,
  303. system_m68k_amiga, { amiga has old GNU AS (2.14), which blews up from .section (KB) }
  304. system_m68k_linux: ;
  305. system_powerpc_darwin,
  306. system_i386_darwin,
  307. system_powerpc64_darwin,
  308. system_x86_64_darwin:
  309. begin
  310. if (atype = sec_stub) then
  311. AsmWrite('.section ');
  312. end
  313. else
  314. AsmWrite('.section ');
  315. end;
  316. s:=sectionname(atype,aname,aorder);
  317. AsmWrite(s);
  318. case atype of
  319. sec_fpc :
  320. if aname = 'resptrs' then
  321. AsmWrite(', "a", @progbits');
  322. sec_stub :
  323. begin
  324. case target_info.system of
  325. { there are processor-independent shortcuts available }
  326. { for this, namely .symbol_stub and .picsymbol_stub, but }
  327. { they don't work and gcc doesn't use them either... }
  328. system_powerpc_darwin,
  329. system_powerpc64_darwin:
  330. if (cs_create_pic in current_settings.moduleswitches) then
  331. AsmWriteln('__TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32')
  332. else
  333. AsmWriteln('__TEXT,__symbol_stub1,symbol_stubs,pure_instructions,16');
  334. system_i386_darwin:
  335. AsmWriteln('__IMPORT,__jump_table,symbol_stubs,self_modifying_code+pure_instructions,5');
  336. { darwin/x86-64 uses RIP-based GOT addressing }
  337. else
  338. internalerror(2006031101);
  339. end;
  340. end;
  341. end;
  342. AsmLn;
  343. CurrSecType:=atype;
  344. end;
  345. procedure TGNUAssembler.WriteDecodedUleb128(a: qword);
  346. var
  347. i,len : longint;
  348. buf : array[0..63] of byte;
  349. begin
  350. len:=EncodeUleb128(a,buf);
  351. for i:=0 to len-1 do
  352. begin
  353. if (i > 0) then
  354. AsmWrite(',');
  355. AsmWrite(tostr(buf[i]));
  356. end;
  357. end;
  358. procedure TGNUAssembler.WriteDecodedSleb128(a: int64);
  359. var
  360. i,len : longint;
  361. buf : array[0..255] of byte;
  362. begin
  363. len:=EncodeSleb128(a,buf);
  364. for i:=0 to len-1 do
  365. begin
  366. if (i > 0) then
  367. AsmWrite(',');
  368. AsmWrite(tostr(buf[i]));
  369. end;
  370. end;
  371. procedure TGNUAssembler.WriteTree(p:TAsmList);
  372. function needsObject(hp : tai_symbol) : boolean;
  373. begin
  374. needsObject :=
  375. (
  376. assigned(hp.next) and
  377. (tai(hp.next).typ in [ait_const,ait_datablock,
  378. ait_real_32bit,ait_real_64bit,ait_real_80bit,ait_comp_64bit])
  379. ) or
  380. (hp.sym.typ=AT_DATA);
  381. end;
  382. var
  383. ch : char;
  384. hp : tai;
  385. hp1 : tailineinfo;
  386. constdef : taiconst_type;
  387. s,t : string;
  388. i,pos,l : longint;
  389. InlineLevel : longint;
  390. last_align : longint;
  391. co : comp;
  392. sin : single;
  393. d : double;
  394. {$ifdef cpuextended}
  395. e : extended;
  396. {$endif cpuextended}
  397. do_line : boolean;
  398. sepChar : char;
  399. begin
  400. if not assigned(p) then
  401. exit;
  402. last_align := 2;
  403. InlineLevel:=0;
  404. { lineinfo is only needed for al_procedures (PFV) }
  405. do_line:=(cs_asm_source in current_settings.globalswitches) or
  406. ((cs_lineinfo in current_settings.moduleswitches)
  407. and (p=current_asmdata.asmlists[al_procedures]));
  408. hp:=tai(p.first);
  409. while assigned(hp) do
  410. begin
  411. if not(hp.typ in SkipLineInfo) then
  412. begin
  413. hp1 := hp as tailineinfo;
  414. current_filepos:=hp1.fileinfo;
  415. { no line info for inlined code }
  416. if do_line and (inlinelevel=0) then
  417. begin
  418. { load infile }
  419. if lastfileinfo.fileindex<>hp1.fileinfo.fileindex then
  420. begin
  421. infile:=current_module.sourcefiles.get_file(hp1.fileinfo.fileindex);
  422. if assigned(infile) then
  423. begin
  424. { open only if needed !! }
  425. if (cs_asm_source in current_settings.globalswitches) then
  426. infile.open;
  427. end;
  428. { avoid unnecessary reopens of the same file !! }
  429. lastfileinfo.fileindex:=hp1.fileinfo.fileindex;
  430. { be sure to change line !! }
  431. lastfileinfo.line:=-1;
  432. end;
  433. { write source }
  434. if (cs_asm_source in current_settings.globalswitches) and
  435. assigned(infile) then
  436. begin
  437. if (infile<>lastinfile) then
  438. begin
  439. AsmWriteLn(target_asm.comment+'['+infile.name^+']');
  440. if assigned(lastinfile) then
  441. lastinfile.close;
  442. end;
  443. if (hp1.fileinfo.line<>lastfileinfo.line) and
  444. ((hp1.fileinfo.line<infile.maxlinebuf) or (InlineLevel>0)) then
  445. begin
  446. if (hp1.fileinfo.line<>0) and
  447. ((infile.linebuf^[hp1.fileinfo.line]>=0) or (InlineLevel>0)) then
  448. AsmWriteLn(target_asm.comment+'['+tostr(hp1.fileinfo.line)+'] '+
  449. fixline(infile.GetLineStr(hp1.fileinfo.line)));
  450. { set it to a negative value !
  451. to make that is has been read already !! PM }
  452. if (infile.linebuf^[hp1.fileinfo.line]>=0) then
  453. infile.linebuf^[hp1.fileinfo.line]:=-infile.linebuf^[hp1.fileinfo.line]-1;
  454. end;
  455. end;
  456. lastfileinfo:=hp1.fileinfo;
  457. lastinfile:=infile;
  458. end;
  459. end;
  460. case hp.typ of
  461. ait_comment :
  462. Begin
  463. AsmWrite(target_asm.comment);
  464. AsmWritePChar(tai_comment(hp).str);
  465. AsmLn;
  466. End;
  467. ait_regalloc :
  468. begin
  469. if (cs_asm_regalloc in current_settings.globalswitches) then
  470. begin
  471. AsmWrite(#9+target_asm.comment+'Register ');
  472. repeat
  473. AsmWrite(std_regname(Tai_regalloc(hp).reg));
  474. if (hp.next=nil) or
  475. (tai(hp.next).typ<>ait_regalloc) or
  476. (tai_regalloc(hp.next).ratype<>tai_regalloc(hp).ratype) then
  477. break;
  478. hp:=tai(hp.next);
  479. AsmWrite(',');
  480. until false;
  481. AsmWrite(' ');
  482. AsmWriteLn(regallocstr[tai_regalloc(hp).ratype]);
  483. end;
  484. end;
  485. ait_tempalloc :
  486. begin
  487. if (cs_asm_tempalloc in current_settings.globalswitches) then
  488. begin
  489. {$ifdef EXTDEBUG}
  490. if assigned(tai_tempalloc(hp).problem) then
  491. AsmWriteLn(target_asm.comment+'Temp '+tostr(tai_tempalloc(hp).temppos)+','+
  492. tostr(tai_tempalloc(hp).tempsize)+' '+tai_tempalloc(hp).problem^)
  493. else
  494. {$endif EXTDEBUG}
  495. AsmWriteLn(target_asm.comment+'Temp '+tostr(tai_tempalloc(hp).temppos)+','+
  496. tostr(tai_tempalloc(hp).tempsize)+' '+tempallocstr[tai_tempalloc(hp).allocation]);
  497. end;
  498. end;
  499. ait_align :
  500. begin
  501. if tai_align_abstract(hp).aligntype>1 then
  502. begin
  503. if not(target_info.system in systems_darwin) then
  504. begin
  505. AsmWrite(#9'.balign '+tostr(tai_align_abstract(hp).aligntype));
  506. if tai_align_abstract(hp).use_op then
  507. AsmWrite(','+tostr(tai_align_abstract(hp).fillop))
  508. {$ifdef x86}
  509. { force NOP as alignment op code }
  510. else if CurrSecType=sec_code then
  511. AsmWrite(',0x90');
  512. {$endif x86}
  513. end
  514. else
  515. begin
  516. { darwin as only supports .align }
  517. if not ispowerof2(tai_align_abstract(hp).aligntype,i) then
  518. internalerror(2003010305);
  519. AsmWrite(#9'.align '+tostr(i));
  520. last_align := i;
  521. end;
  522. AsmLn;
  523. end;
  524. end;
  525. ait_section :
  526. begin
  527. if tai_section(hp).sectype<>sec_none then
  528. WriteSection(tai_section(hp).sectype,tai_section(hp).name^,tai_section(hp).secorder)
  529. else
  530. begin
  531. {$ifdef EXTDEBUG}
  532. AsmWrite(target_asm.comment);
  533. AsmWriteln(' sec_none');
  534. {$endif EXTDEBUG}
  535. end;
  536. end;
  537. ait_datablock :
  538. begin
  539. if (target_info.system in systems_darwin) then
  540. begin
  541. { On Mac OS X you can't have common symbols in a shared library
  542. since those are in the TEXT section and the text section is
  543. read-only in shared libraries (so it can be shared among different
  544. processes). The alternate code creates some kind of common symbols
  545. in the data segment.
  546. }
  547. if tai_datablock(hp).is_global then
  548. begin
  549. asmwrite('.globl ');
  550. asmwriteln(tai_datablock(hp).sym.name);
  551. asmwriteln('.data');
  552. asmwrite('.zerofill __DATA, __common, ');
  553. asmwrite(tai_datablock(hp).sym.name);
  554. asmwriteln(', '+tostr(tai_datablock(hp).size)+','+tostr(last_align));
  555. if not(CurrSecType in [sec_data,sec_none]) then
  556. writesection(CurrSecType,'',secorder_default);
  557. end
  558. else
  559. begin
  560. asmwrite(#9'.lcomm'#9);
  561. asmwrite(tai_datablock(hp).sym.name);
  562. asmwrite(','+tostr(tai_datablock(hp).size));
  563. asmwrite(','+tostr(last_align));
  564. asmln;
  565. end
  566. end
  567. else
  568. begin
  569. { The .comm is required for COMMON symbols. These are used
  570. in the shared library loading. All the symbols declared in
  571. the .so file need to resolve to the data allocated in the main
  572. program (PFV) }
  573. if Tai_datablock(hp).is_global then
  574. begin
  575. asmwrite(#9'.comm'#9);
  576. asmwrite(tai_datablock(hp).sym.name);
  577. asmwrite(','+tostr(tai_datablock(hp).size));
  578. asmln;
  579. end
  580. else
  581. begin
  582. asmwrite(#9'.lcomm'#9);
  583. asmwrite(tai_datablock(hp).sym.name);
  584. asmwrite(','+tostr(tai_datablock(hp).size));
  585. asmln;
  586. end;
  587. end;
  588. end;
  589. ait_const:
  590. begin
  591. constdef:=tai_const(hp).consttype;
  592. case constdef of
  593. {$ifndef cpu64bit}
  594. aitconst_128bit :
  595. begin
  596. internalerror(200404291);
  597. end;
  598. aitconst_64bit :
  599. begin
  600. if assigned(tai_const(hp).sym) then
  601. internalerror(200404292);
  602. AsmWrite(ait_const2str[aitconst_32bit]);
  603. if target_info.endian = endian_little then
  604. begin
  605. AsmWrite(tostr(longint(lo(tai_const(hp).value))));
  606. AsmWrite(',');
  607. AsmWrite(tostr(longint(hi(tai_const(hp).value))));
  608. end
  609. else
  610. begin
  611. AsmWrite(tostr(longint(hi(tai_const(hp).value))));
  612. AsmWrite(',');
  613. AsmWrite(tostr(longint(lo(tai_const(hp).value))));
  614. end;
  615. AsmLn;
  616. end;
  617. {$endif cpu64bit}
  618. aitconst_uleb128bit,
  619. aitconst_sleb128bit,
  620. {$ifdef cpu64bit}
  621. aitconst_128bit,
  622. aitconst_64bit,
  623. {$endif cpu64bit}
  624. aitconst_32bit,
  625. aitconst_16bit,
  626. aitconst_8bit,
  627. aitconst_rva_symbol,
  628. aitconst_secrel32_symbol,
  629. aitconst_indirect_symbol :
  630. begin
  631. if (target_info.system in systems_darwin) and
  632. (tai_const(hp).consttype in [aitconst_uleb128bit,aitconst_sleb128bit]) then
  633. begin
  634. AsmWrite(ait_const2str[aitconst_8bit]);
  635. case tai_const(hp).consttype of
  636. aitconst_uleb128bit:
  637. WriteDecodedUleb128(qword(tai_const(hp).value));
  638. aitconst_sleb128bit:
  639. WriteDecodedSleb128(int64(tai_const(hp).value));
  640. end
  641. end
  642. else
  643. begin
  644. AsmWrite(ait_const2str[tai_const(hp).consttype]);
  645. l:=0;
  646. t := '';
  647. repeat
  648. if assigned(tai_const(hp).sym) then
  649. begin
  650. if assigned(tai_const(hp).endsym) then
  651. begin
  652. if (target_info.system in systems_darwin) then
  653. begin
  654. s := NextSetLabel;
  655. t := #9'.set '+s+','+tai_const(hp).endsym.name+'-'+tai_const(hp).sym.name;
  656. end
  657. else
  658. s:=tai_const(hp).endsym.name+'-'+tai_const(hp).sym.name
  659. end
  660. else
  661. s:=tai_const(hp).sym.name;
  662. if tai_const(hp).value<>0 then
  663. s:=s+tostr_with_plus(tai_const(hp).value);
  664. end
  665. else
  666. s:=tostr(tai_const(hp).value);
  667. AsmWrite(s);
  668. inc(l,length(s));
  669. { Values with symbols are written on a single line to improve
  670. reading of the .s file (PFV) }
  671. if assigned(tai_const(hp).sym) or
  672. not(CurrSecType in [sec_data,sec_rodata,sec_rodata_norel]) or
  673. (l>line_length) or
  674. (hp.next=nil) or
  675. (tai(hp.next).typ<>ait_const) or
  676. (tai_const(hp.next).consttype<>constdef) or
  677. assigned(tai_const(hp.next).sym) then
  678. break;
  679. hp:=tai(hp.next);
  680. AsmWrite(',');
  681. until false;
  682. if (t <> '') then
  683. begin
  684. AsmLn;
  685. AsmWrite(t);
  686. end;
  687. end;
  688. AsmLn;
  689. end;
  690. else
  691. internalerror(200704251);
  692. end;
  693. end;
  694. { the "and defined(FPC_HAS_TYPE_EXTENDED)" isn't optimal but currently the only solution
  695. it prevents proper cross compilation to i386 though
  696. }
  697. {$if defined(cpuextended) and defined(FPC_HAS_TYPE_EXTENDED)}
  698. ait_real_80bit :
  699. begin
  700. if do_line then
  701. AsmWriteLn(target_asm.comment+'value: '+extended2str(tai_real_80bit(hp).value));
  702. { Make sure e is a extended type, bestreal could be
  703. a different type (bestreal) !! (PFV) }
  704. e:=tai_real_80bit(hp).value;
  705. AsmWrite(#9'.byte'#9);
  706. for i:=0 to 9 do
  707. begin
  708. if i<>0 then
  709. AsmWrite(',');
  710. AsmWrite(tostr(t80bitarray(e)[i]));
  711. end;
  712. AsmLn;
  713. end;
  714. {$endif cpuextended}
  715. ait_real_64bit :
  716. begin
  717. if do_line then
  718. AsmWriteLn(target_asm.comment+'value: '+double2str(tai_real_64bit(hp).value));
  719. d:=tai_real_64bit(hp).value;
  720. { swap the values to correct endian if required }
  721. if source_info.endian <> target_info.endian then
  722. swap64bitarray(t64bitarray(d));
  723. AsmWrite(#9'.byte'#9);
  724. {$ifdef arm}
  725. if tai_real_64bit(hp).formatoptions=fo_hiloswapped then
  726. begin
  727. for i:=4 to 7 do
  728. begin
  729. if i<>4 then
  730. AsmWrite(',');
  731. AsmWrite(tostr(t64bitarray(d)[i]));
  732. end;
  733. for i:=0 to 3 do
  734. begin
  735. AsmWrite(',');
  736. AsmWrite(tostr(t64bitarray(d)[i]));
  737. end;
  738. end
  739. else
  740. {$endif arm}
  741. begin
  742. for i:=0 to 7 do
  743. begin
  744. if i<>0 then
  745. AsmWrite(',');
  746. AsmWrite(tostr(t64bitarray(d)[i]));
  747. end;
  748. end;
  749. AsmLn;
  750. end;
  751. ait_real_32bit :
  752. begin
  753. if do_line then
  754. AsmWriteLn(target_asm.comment+'value: '+single2str(tai_real_32bit(hp).value));
  755. sin:=tai_real_32bit(hp).value;
  756. { swap the values to correct endian if required }
  757. if source_info.endian <> target_info.endian then
  758. swap32bitarray(t32bitarray(sin));
  759. AsmWrite(#9'.byte'#9);
  760. for i:=0 to 3 do
  761. begin
  762. if i<>0 then
  763. AsmWrite(',');
  764. AsmWrite(tostr(t32bitarray(sin)[i]));
  765. end;
  766. AsmLn;
  767. end;
  768. ait_comp_64bit :
  769. begin
  770. if do_line then
  771. AsmWriteLn(target_asm.comment+'value: '+extended2str(tai_comp_64bit(hp).value));
  772. AsmWrite(#9'.byte'#9);
  773. co:=comp(tai_comp_64bit(hp).value);
  774. { swap the values to correct endian if required }
  775. if source_info.endian <> target_info.endian then
  776. swap64bitarray(t64bitarray(co));
  777. for i:=0 to 7 do
  778. begin
  779. if i<>0 then
  780. AsmWrite(',');
  781. AsmWrite(tostr(t64bitarray(co)[i]));
  782. end;
  783. AsmLn;
  784. end;
  785. ait_string :
  786. begin
  787. pos:=0;
  788. for i:=1 to tai_string(hp).len do
  789. begin
  790. if pos=0 then
  791. begin
  792. AsmWrite(#9'.ascii'#9'"');
  793. pos:=20;
  794. end;
  795. ch:=tai_string(hp).str[i-1];
  796. case ch of
  797. #0, {This can't be done by range, because a bug in FPC}
  798. #1..#31,
  799. #128..#255 : s:='\'+tostr(ord(ch) shr 6)+tostr((ord(ch) and 63) shr 3)+tostr(ord(ch) and 7);
  800. '"' : s:='\"';
  801. '\' : s:='\\';
  802. else
  803. s:=ch;
  804. end;
  805. AsmWrite(s);
  806. inc(pos,length(s));
  807. if (pos>line_length) or (i=tai_string(hp).len) then
  808. begin
  809. AsmWriteLn('"');
  810. pos:=0;
  811. end;
  812. end;
  813. end;
  814. ait_label :
  815. begin
  816. if (tai_label(hp).labsym.is_used) then
  817. begin
  818. if tai_label(hp).labsym.bind=AB_GLOBAL then
  819. begin
  820. AsmWrite('.globl'#9);
  821. AsmWriteLn(tai_label(hp).labsym.name);
  822. end;
  823. AsmWrite(tai_label(hp).labsym.name);
  824. AsmWriteLn(':');
  825. end;
  826. end;
  827. ait_symbol :
  828. begin
  829. if (target_info.system = system_powerpc64_linux) and
  830. (tai_symbol(hp).sym.typ = AT_FUNCTION) and (cs_profile in current_settings.moduleswitches) then
  831. begin
  832. AsmWriteLn('.globl _mcount');
  833. end;
  834. if tai_symbol(hp).is_global then
  835. begin
  836. AsmWrite('.globl'#9);
  837. AsmWriteLn(tai_symbol(hp).sym.name);
  838. end;
  839. if (target_info.system = system_powerpc64_linux) and
  840. (tai_symbol(hp).sym.typ = AT_FUNCTION) then
  841. begin
  842. AsmWriteLn('.section ".opd", "aw"');
  843. AsmWriteLn('.align 3');
  844. AsmWriteLn(tai_symbol(hp).sym.name + ':');
  845. AsmWriteLn('.quad .' + tai_symbol(hp).sym.name + ', .TOC.@tocbase, 0');
  846. AsmWriteLn('.previous');
  847. AsmWriteLn('.size ' + tai_symbol(hp).sym.name + ', 24');
  848. if (tai_symbol(hp).is_global) then
  849. AsmWriteLn('.globl .' + tai_symbol(hp).sym.name);
  850. AsmWriteLn('.type .' + tai_symbol(hp).sym.name + ', @function');
  851. { the dotted name is the name of the actual function entry }
  852. AsmWrite('.');
  853. end
  854. else
  855. begin
  856. if (target_info.system <> system_arm_linux) then
  857. sepChar := '@'
  858. else
  859. sepChar := '#';
  860. if (tf_needs_symbol_type in target_info.flags) then
  861. begin
  862. AsmWrite(#9'.type'#9 + tai_symbol(hp).sym.name);
  863. if (needsObject(tai_symbol(hp))) then
  864. AsmWriteLn(',' + sepChar + 'object')
  865. else
  866. AsmWriteLn(',' + sepChar + 'function');
  867. end;
  868. end;
  869. AsmWriteLn(tai_symbol(hp).sym.name + ':');
  870. end;
  871. ait_symbol_end :
  872. begin
  873. if tf_needs_symbol_size in target_info.flags then
  874. begin
  875. s:=target_asm.labelprefix+'e'+tostr(symendcount);
  876. inc(symendcount);
  877. AsmWriteLn(s+':');
  878. AsmWrite(#9'.size'#9);
  879. if (target_info.system = system_powerpc64_linux) and (tai_symbol_end(hp).sym.typ = AT_FUNCTION) then
  880. AsmWrite('.');
  881. AsmWrite(tai_symbol_end(hp).sym.name);
  882. AsmWrite(', '+s+' - ');
  883. if (target_info.system = system_powerpc64_linux) and (tai_symbol_end(hp).sym.typ = AT_FUNCTION) then
  884. AsmWrite('.');
  885. AsmWriteLn(tai_symbol_end(hp).sym.name);
  886. end;
  887. end;
  888. ait_instruction :
  889. begin
  890. WriteInstruction(hp);
  891. end;
  892. ait_stab :
  893. begin
  894. if assigned(tai_stab(hp).str) then
  895. begin
  896. AsmWrite(#9'.'+stabtypestr[tai_stab(hp).stabtype]+' ');
  897. AsmWritePChar(tai_stab(hp).str);
  898. AsmLn;
  899. end;
  900. end;
  901. ait_force_line,
  902. ait_function_name : ;
  903. ait_cutobject :
  904. begin
  905. if SmartAsm then
  906. begin
  907. { only reset buffer if nothing has changed }
  908. if AsmSize=AsmStartSize then
  909. AsmClear
  910. else
  911. begin
  912. AsmClose;
  913. DoAssemble;
  914. AsmCreate(tai_cutobject(hp).place);
  915. end;
  916. { avoid empty files }
  917. while assigned(hp.next) and (tai(hp.next).typ in [ait_cutobject,ait_section,ait_comment]) do
  918. begin
  919. if tai(hp.next).typ=ait_section then
  920. CurrSecType:=tai_section(hp.next).sectype;
  921. hp:=tai(hp.next);
  922. end;
  923. if CurrSecType<>sec_none then
  924. WriteSection(CurrSecType,'',secorder_default);
  925. AsmStartSize:=AsmSize;
  926. end;
  927. end;
  928. ait_marker :
  929. if tai_marker(hp).kind=mark_InlineStart then
  930. inc(InlineLevel)
  931. else if tai_marker(hp).kind=mark_InlineEnd then
  932. dec(InlineLevel);
  933. ait_directive :
  934. begin
  935. AsmWrite('.'+directivestr[tai_directive(hp).directive]+' ');
  936. if assigned(tai_directive(hp).name) then
  937. AsmWrite(tai_directive(hp).name^);
  938. AsmLn;
  939. end;
  940. else
  941. internalerror(2006012201);
  942. end;
  943. hp:=tai(hp.next);
  944. end;
  945. end;
  946. procedure TGNUAssembler.WriteExtraHeader;
  947. begin
  948. end;
  949. procedure TGNUAssembler.WriteInstruction(hp: tai);
  950. begin
  951. InstrWriter.WriteInstruction(hp);
  952. end;
  953. procedure TGNUAssembler.WriteAsmList;
  954. var
  955. n : string;
  956. hal : tasmlisttype;
  957. begin
  958. {$ifdef EXTDEBUG}
  959. if assigned(current_module.mainsource) then
  960. Comment(V_Debug,'Start writing gas-styled assembler output for '+current_module.mainsource^);
  961. {$endif}
  962. CurrSecType:=sec_none;
  963. FillChar(lastfileinfo,sizeof(lastfileinfo),0);
  964. LastInfile:=nil;
  965. if assigned(current_module.mainsource) then
  966. n:=ExtractFileName(current_module.mainsource^)
  967. else
  968. n:=InputFileName;
  969. AsmWriteLn(#9'.file "'+FixFileName(n)+'"');
  970. WriteExtraHeader;
  971. AsmStartSize:=AsmSize;
  972. symendcount:=0;
  973. for hal:=low(TasmlistType) to high(TasmlistType) do
  974. begin
  975. AsmWriteLn(target_asm.comment+'Begin asmlist '+AsmlistTypeStr[hal]);
  976. writetree(current_asmdata.asmlists[hal]);
  977. AsmWriteLn(target_asm.comment+'End asmlist '+AsmlistTypeStr[hal]);
  978. end;
  979. if create_smartlink_sections and
  980. (target_info.system in systems_darwin) then
  981. AsmWriteLn(#9'.subsections_via_symbols');
  982. AsmLn;
  983. {$ifdef EXTDEBUG}
  984. if assigned(current_module.mainsource) then
  985. Comment(V_Debug,'Done writing gas-styled assembler output for '+current_module.mainsource^);
  986. {$endif EXTDEBUG}
  987. end;
  988. {****************************************************************************}
  989. { Apple/GNU Assembler writer }
  990. {****************************************************************************}
  991. function TAppleGNUAssembler.sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;
  992. begin
  993. if (target_info.system in systems_darwin) then
  994. case atype of
  995. sec_bss:
  996. { all bss (lcomm) symbols are automatically put in the right }
  997. { place by using the lcomm assembler directive }
  998. atype := sec_none;
  999. sec_debug_frame,
  1000. sec_eh_frame:
  1001. begin
  1002. result := '.section __DWARFA,__debug_frame,coalesced,no_toc+strip_static_syms'#10'EH_frame'+tostr(debugframecount)+':';
  1003. inc(debugframecount);
  1004. exit;
  1005. end;
  1006. sec_debug_line:
  1007. begin
  1008. result := '.section __DWARF,__debug_line,regular,debug';
  1009. exit;
  1010. end;
  1011. sec_debug_info:
  1012. begin
  1013. result := '.section __DWARF,__debug_info,regular,debug';
  1014. exit;
  1015. end;
  1016. sec_debug_abbrev:
  1017. begin
  1018. result := '.section __DWARF,__debug_abbrev,regular,debug';
  1019. exit;
  1020. end;
  1021. sec_rodata:
  1022. begin
  1023. result := '.const_data';
  1024. exit;
  1025. end;
  1026. sec_rodata_norel:
  1027. begin
  1028. result := '.const';
  1029. exit;
  1030. end;
  1031. sec_fpc:
  1032. begin
  1033. result := '.section __TEXT, .fpc, regular, no_dead_strip';
  1034. exit;
  1035. end;
  1036. sec_code:
  1037. begin
  1038. if (aname='fpc_geteipasebx') or
  1039. (aname='fpc_geteipasecx') then
  1040. begin
  1041. result:='.section __TEXT,__textcoal_nt,coalesced,pure_instructions'#10'.weak_definition '+aname+
  1042. #10'.private_extern '+aname;
  1043. exit;
  1044. end;
  1045. end;
  1046. end;
  1047. result := inherited sectionname(atype,aname,aorder);
  1048. end;
  1049. {****************************************************************************}
  1050. { a.out/GNU Assembler writer }
  1051. {****************************************************************************}
  1052. function TAoutGNUAssembler.sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;
  1053. const
  1054. (* Translation table - replace unsupported section types with basic ones. *)
  1055. SecXTable: array[TAsmSectionType] of TAsmSectionType = (
  1056. sec_none,
  1057. sec_code,
  1058. sec_data,
  1059. sec_data (* sec_rodata *),
  1060. sec_data (* sec_rodata_norel *),
  1061. sec_bss,
  1062. sec_data (* sec_threadvar *),
  1063. { used for wince exception handling }
  1064. sec_code (* sec_pdata *),
  1065. { used for darwin import stubs }
  1066. sec_code (* sec_stub *),
  1067. { stabs }
  1068. sec_stab,sec_stabstr,
  1069. { win32 }
  1070. sec_data (* sec_idata2 *),
  1071. sec_data (* sec_idata4 *),
  1072. sec_data (* sec_idata5 *),
  1073. sec_data (* sec_idata6 *),
  1074. sec_data (* sec_idata7 *),
  1075. sec_data (* sec_edata *),
  1076. { C++ exception handling unwinding (uses dwarf) }
  1077. sec_eh_frame,
  1078. { dwarf }
  1079. sec_debug_frame,
  1080. sec_debug_info,
  1081. sec_debug_line,
  1082. sec_debug_abbrev,
  1083. { ELF resources (+ references to stabs debug information sections) }
  1084. sec_code (* sec_fpc *),
  1085. { Table of contents section }
  1086. sec_code (* sec_toc *),
  1087. sec_code (* sec_init *),
  1088. sec_code (* sec_fini *)
  1089. );
  1090. begin
  1091. Result := inherited SectionName (SecXTable [AType], AName, AOrder);
  1092. end;
  1093. {****************************************************************************}
  1094. { Abstract Instruction Writer }
  1095. {****************************************************************************}
  1096. constructor TCPUInstrWriter.create(_owner: TGNUAssembler);
  1097. begin
  1098. inherited create;
  1099. owner := _owner;
  1100. end;
  1101. end.