aggas.pas 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197
  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. use_smartlink_section 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. AsmWriteln('__TEXT,__symbol_stub1,symbol_stubs,pure_instructions,16');
  331. system_i386_darwin:
  332. AsmWriteln('__IMPORT,__jump_table,symbol_stubs,self_modifying_code+pure_instructions,5');
  333. { darwin/x86-64 uses RIP-based GOT addressing }
  334. else
  335. internalerror(2006031101);
  336. end;
  337. end;
  338. end;
  339. AsmLn;
  340. CurrSecType:=atype;
  341. end;
  342. procedure TGNUAssembler.WriteDecodedUleb128(a: qword);
  343. var
  344. i,len : longint;
  345. buf : array[0..63] of byte;
  346. begin
  347. len:=EncodeUleb128(a,buf);
  348. for i:=0 to len-1 do
  349. begin
  350. if (i > 0) then
  351. AsmWrite(',');
  352. AsmWrite(tostr(buf[i]));
  353. end;
  354. end;
  355. procedure TGNUAssembler.WriteDecodedSleb128(a: int64);
  356. var
  357. i,len : longint;
  358. buf : array[0..255] of byte;
  359. begin
  360. len:=EncodeSleb128(a,buf);
  361. for i:=0 to len-1 do
  362. begin
  363. if (i > 0) then
  364. AsmWrite(',');
  365. AsmWrite(tostr(buf[i]));
  366. end;
  367. end;
  368. procedure TGNUAssembler.WriteTree(p:TAsmList);
  369. function needsObject(hp : tai_symbol) : boolean;
  370. begin
  371. needsObject :=
  372. (
  373. assigned(hp.next) and
  374. (tai(hp.next).typ in [ait_const,ait_datablock,
  375. ait_real_32bit,ait_real_64bit,ait_real_80bit,ait_comp_64bit])
  376. ) or
  377. (hp.sym.typ=AT_DATA);
  378. end;
  379. var
  380. ch : char;
  381. hp : tai;
  382. hp1 : tailineinfo;
  383. constdef : taiconst_type;
  384. s,t : string;
  385. i,pos,l : longint;
  386. InlineLevel : longint;
  387. last_align : longint;
  388. co : comp;
  389. sin : single;
  390. d : double;
  391. {$ifdef cpuextended}
  392. e : extended;
  393. {$endif cpuextended}
  394. do_line : boolean;
  395. sepChar : char;
  396. begin
  397. if not assigned(p) then
  398. exit;
  399. last_align := 2;
  400. InlineLevel:=0;
  401. { lineinfo is only needed for al_procedures (PFV) }
  402. do_line:=(cs_asm_source in current_settings.globalswitches) or
  403. ((cs_lineinfo in current_settings.moduleswitches)
  404. and (p=current_asmdata.asmlists[al_procedures]));
  405. hp:=tai(p.first);
  406. while assigned(hp) do
  407. begin
  408. if not(hp.typ in SkipLineInfo) then
  409. begin
  410. hp1 := hp as tailineinfo;
  411. current_filepos:=hp1.fileinfo;
  412. { no line info for inlined code }
  413. if do_line and (inlinelevel=0) then
  414. begin
  415. { load infile }
  416. if lastfileinfo.fileindex<>hp1.fileinfo.fileindex then
  417. begin
  418. infile:=current_module.sourcefiles.get_file(hp1.fileinfo.fileindex);
  419. if assigned(infile) then
  420. begin
  421. { open only if needed !! }
  422. if (cs_asm_source in current_settings.globalswitches) then
  423. infile.open;
  424. end;
  425. { avoid unnecessary reopens of the same file !! }
  426. lastfileinfo.fileindex:=hp1.fileinfo.fileindex;
  427. { be sure to change line !! }
  428. lastfileinfo.line:=-1;
  429. end;
  430. { write source }
  431. if (cs_asm_source in current_settings.globalswitches) and
  432. assigned(infile) then
  433. begin
  434. if (infile<>lastinfile) then
  435. begin
  436. AsmWriteLn(target_asm.comment+'['+infile.name^+']');
  437. if assigned(lastinfile) then
  438. lastinfile.close;
  439. end;
  440. if (hp1.fileinfo.line<>lastfileinfo.line) and
  441. ((hp1.fileinfo.line<infile.maxlinebuf) or (InlineLevel>0)) then
  442. begin
  443. if (hp1.fileinfo.line<>0) and
  444. ((infile.linebuf^[hp1.fileinfo.line]>=0) or (InlineLevel>0)) then
  445. AsmWriteLn(target_asm.comment+'['+tostr(hp1.fileinfo.line)+'] '+
  446. fixline(infile.GetLineStr(hp1.fileinfo.line)));
  447. { set it to a negative value !
  448. to make that is has been read already !! PM }
  449. if (infile.linebuf^[hp1.fileinfo.line]>=0) then
  450. infile.linebuf^[hp1.fileinfo.line]:=-infile.linebuf^[hp1.fileinfo.line]-1;
  451. end;
  452. end;
  453. lastfileinfo:=hp1.fileinfo;
  454. lastinfile:=infile;
  455. end;
  456. end;
  457. case hp.typ of
  458. ait_comment :
  459. Begin
  460. AsmWrite(target_asm.comment);
  461. AsmWritePChar(tai_comment(hp).str);
  462. AsmLn;
  463. End;
  464. ait_regalloc :
  465. begin
  466. if (cs_asm_regalloc in current_settings.globalswitches) then
  467. begin
  468. AsmWrite(#9+target_asm.comment+'Register ');
  469. repeat
  470. AsmWrite(std_regname(Tai_regalloc(hp).reg));
  471. if (hp.next=nil) or
  472. (tai(hp.next).typ<>ait_regalloc) or
  473. (tai_regalloc(hp.next).ratype<>tai_regalloc(hp).ratype) then
  474. break;
  475. hp:=tai(hp.next);
  476. AsmWrite(',');
  477. until false;
  478. AsmWrite(' ');
  479. AsmWriteLn(regallocstr[tai_regalloc(hp).ratype]);
  480. end;
  481. end;
  482. ait_tempalloc :
  483. begin
  484. if (cs_asm_tempalloc in current_settings.globalswitches) then
  485. begin
  486. {$ifdef EXTDEBUG}
  487. if assigned(tai_tempalloc(hp).problem) then
  488. AsmWriteLn(target_asm.comment+'Temp '+tostr(tai_tempalloc(hp).temppos)+','+
  489. tostr(tai_tempalloc(hp).tempsize)+' '+tai_tempalloc(hp).problem^)
  490. else
  491. {$endif EXTDEBUG}
  492. AsmWriteLn(target_asm.comment+'Temp '+tostr(tai_tempalloc(hp).temppos)+','+
  493. tostr(tai_tempalloc(hp).tempsize)+' '+tempallocstr[tai_tempalloc(hp).allocation]);
  494. end;
  495. end;
  496. ait_align :
  497. begin
  498. if tai_align_abstract(hp).aligntype>1 then
  499. begin
  500. if not(target_info.system in systems_darwin) then
  501. begin
  502. AsmWrite(#9'.balign '+tostr(tai_align_abstract(hp).aligntype));
  503. if tai_align_abstract(hp).use_op then
  504. AsmWrite(','+tostr(tai_align_abstract(hp).fillop))
  505. {$ifdef x86}
  506. { force NOP as alignment op code }
  507. else if CurrSecType=sec_code then
  508. AsmWrite(',0x90');
  509. {$endif x86}
  510. end
  511. else
  512. begin
  513. { darwin as only supports .align }
  514. if not ispowerof2(tai_align_abstract(hp).aligntype,i) then
  515. internalerror(2003010305);
  516. AsmWrite(#9'.align '+tostr(i));
  517. last_align := i;
  518. end;
  519. AsmLn;
  520. end;
  521. end;
  522. ait_section :
  523. begin
  524. if tai_section(hp).sectype<>sec_none then
  525. WriteSection(tai_section(hp).sectype,tai_section(hp).name^,tai_section(hp).secorder)
  526. else
  527. begin
  528. {$ifdef EXTDEBUG}
  529. AsmWrite(target_asm.comment);
  530. AsmWriteln(' sec_none');
  531. {$endif EXTDEBUG}
  532. end;
  533. end;
  534. ait_datablock :
  535. begin
  536. if (target_info.system in systems_darwin) then
  537. begin
  538. { On Mac OS X you can't have common symbols in a shared library
  539. since those are in the TEXT section and the text section is
  540. read-only in shared libraries (so it can be shared among different
  541. processes). The alternate code creates some kind of common symbols
  542. in the data segment.
  543. }
  544. if tai_datablock(hp).is_global then
  545. begin
  546. asmwrite('.globl ');
  547. asmwriteln(tai_datablock(hp).sym.name);
  548. asmwriteln('.data');
  549. asmwrite('.zerofill __DATA, __common, ');
  550. asmwrite(tai_datablock(hp).sym.name);
  551. asmwriteln(', '+tostr(tai_datablock(hp).size)+','+tostr(last_align));
  552. if not(CurrSecType in [sec_data,sec_none]) then
  553. writesection(CurrSecType,'',secorder_default);
  554. end
  555. else
  556. begin
  557. asmwrite(#9'.lcomm'#9);
  558. asmwrite(tai_datablock(hp).sym.name);
  559. asmwrite(','+tostr(tai_datablock(hp).size));
  560. asmwrite(','+tostr(last_align));
  561. asmln;
  562. end
  563. end
  564. else
  565. begin
  566. { The .comm is required for COMMON symbols. These are used
  567. in the shared library loading. All the symbols declared in
  568. the .so file need to resolve to the data allocated in the main
  569. program (PFV) }
  570. if Tai_datablock(hp).is_global then
  571. begin
  572. asmwrite(#9'.comm'#9);
  573. asmwrite(tai_datablock(hp).sym.name);
  574. asmwrite(','+tostr(tai_datablock(hp).size));
  575. asmln;
  576. end
  577. else
  578. begin
  579. asmwrite(#9'.lcomm'#9);
  580. asmwrite(tai_datablock(hp).sym.name);
  581. asmwrite(','+tostr(tai_datablock(hp).size));
  582. asmln;
  583. end;
  584. end;
  585. end;
  586. ait_const:
  587. begin
  588. constdef:=tai_const(hp).consttype;
  589. case constdef of
  590. {$ifndef cpu64bit}
  591. aitconst_128bit :
  592. begin
  593. internalerror(200404291);
  594. end;
  595. aitconst_64bit :
  596. begin
  597. if assigned(tai_const(hp).sym) then
  598. internalerror(200404292);
  599. AsmWrite(ait_const2str[aitconst_32bit]);
  600. if target_info.endian = endian_little then
  601. begin
  602. AsmWrite(tostr(longint(lo(tai_const(hp).value))));
  603. AsmWrite(',');
  604. AsmWrite(tostr(longint(hi(tai_const(hp).value))));
  605. end
  606. else
  607. begin
  608. AsmWrite(tostr(longint(hi(tai_const(hp).value))));
  609. AsmWrite(',');
  610. AsmWrite(tostr(longint(lo(tai_const(hp).value))));
  611. end;
  612. AsmLn;
  613. end;
  614. {$endif cpu64bit}
  615. aitconst_uleb128bit,
  616. aitconst_sleb128bit,
  617. {$ifdef cpu64bit}
  618. aitconst_128bit,
  619. aitconst_64bit,
  620. {$endif cpu64bit}
  621. aitconst_32bit,
  622. aitconst_16bit,
  623. aitconst_8bit,
  624. aitconst_rva_symbol,
  625. aitconst_secrel32_symbol,
  626. aitconst_indirect_symbol :
  627. begin
  628. if (target_info.system in systems_darwin) and
  629. (tai_const(hp).consttype in [aitconst_uleb128bit,aitconst_sleb128bit]) then
  630. begin
  631. AsmWrite(ait_const2str[aitconst_8bit]);
  632. case tai_const(hp).consttype of
  633. aitconst_uleb128bit:
  634. WriteDecodedUleb128(qword(tai_const(hp).value));
  635. aitconst_sleb128bit:
  636. WriteDecodedSleb128(int64(tai_const(hp).value));
  637. end
  638. end
  639. else
  640. begin
  641. AsmWrite(ait_const2str[tai_const(hp).consttype]);
  642. l:=0;
  643. t := '';
  644. repeat
  645. if assigned(tai_const(hp).sym) then
  646. begin
  647. if assigned(tai_const(hp).endsym) then
  648. begin
  649. if (target_info.system in systems_darwin) then
  650. begin
  651. s := NextSetLabel;
  652. t := #9'.set '+s+','+tai_const(hp).endsym.name+'-'+tai_const(hp).sym.name;
  653. end
  654. else
  655. s:=tai_const(hp).endsym.name+'-'+tai_const(hp).sym.name
  656. end
  657. else
  658. s:=tai_const(hp).sym.name;
  659. if tai_const(hp).value<>0 then
  660. s:=s+tostr_with_plus(tai_const(hp).value);
  661. end
  662. else
  663. s:=tostr(tai_const(hp).value);
  664. AsmWrite(s);
  665. inc(l,length(s));
  666. { Values with symbols are written on a single line to improve
  667. reading of the .s file (PFV) }
  668. if assigned(tai_const(hp).sym) or
  669. not(CurrSecType in [sec_data,sec_rodata,sec_rodata_norel]) or
  670. (l>line_length) or
  671. (hp.next=nil) or
  672. (tai(hp.next).typ<>ait_const) or
  673. (tai_const(hp.next).consttype<>constdef) or
  674. assigned(tai_const(hp.next).sym) then
  675. break;
  676. hp:=tai(hp.next);
  677. AsmWrite(',');
  678. until false;
  679. if (t <> '') then
  680. begin
  681. AsmLn;
  682. AsmWrite(t);
  683. end;
  684. end;
  685. AsmLn;
  686. end;
  687. else
  688. internalerror(200704251);
  689. end;
  690. end;
  691. { the "and defined(FPC_HAS_TYPE_EXTENDED)" isn't optimal but currently the only solution
  692. it prevents proper cross compilation to i386 though
  693. }
  694. {$if defined(cpuextended) and defined(FPC_HAS_TYPE_EXTENDED)}
  695. ait_real_80bit :
  696. begin
  697. if do_line then
  698. AsmWriteLn(target_asm.comment+'value: '+extended2str(tai_real_80bit(hp).value));
  699. { Make sure e is a extended type, bestreal could be
  700. a different type (bestreal) !! (PFV) }
  701. e:=tai_real_80bit(hp).value;
  702. AsmWrite(#9'.byte'#9);
  703. for i:=0 to 9 do
  704. begin
  705. if i<>0 then
  706. AsmWrite(',');
  707. AsmWrite(tostr(t80bitarray(e)[i]));
  708. end;
  709. AsmLn;
  710. end;
  711. {$endif cpuextended}
  712. ait_real_64bit :
  713. begin
  714. if do_line then
  715. AsmWriteLn(target_asm.comment+'value: '+double2str(tai_real_64bit(hp).value));
  716. d:=tai_real_64bit(hp).value;
  717. { swap the values to correct endian if required }
  718. if source_info.endian <> target_info.endian then
  719. swap64bitarray(t64bitarray(d));
  720. AsmWrite(#9'.byte'#9);
  721. {$ifdef arm}
  722. if tai_real_64bit(hp).formatoptions=fo_hiloswapped then
  723. begin
  724. for i:=4 to 7 do
  725. begin
  726. if i<>4 then
  727. AsmWrite(',');
  728. AsmWrite(tostr(t64bitarray(d)[i]));
  729. end;
  730. for i:=0 to 3 do
  731. begin
  732. AsmWrite(',');
  733. AsmWrite(tostr(t64bitarray(d)[i]));
  734. end;
  735. end
  736. else
  737. {$endif arm}
  738. begin
  739. for i:=0 to 7 do
  740. begin
  741. if i<>0 then
  742. AsmWrite(',');
  743. AsmWrite(tostr(t64bitarray(d)[i]));
  744. end;
  745. end;
  746. AsmLn;
  747. end;
  748. ait_real_32bit :
  749. begin
  750. if do_line then
  751. AsmWriteLn(target_asm.comment+'value: '+single2str(tai_real_32bit(hp).value));
  752. sin:=tai_real_32bit(hp).value;
  753. { swap the values to correct endian if required }
  754. if source_info.endian <> target_info.endian then
  755. swap32bitarray(t32bitarray(sin));
  756. AsmWrite(#9'.byte'#9);
  757. for i:=0 to 3 do
  758. begin
  759. if i<>0 then
  760. AsmWrite(',');
  761. AsmWrite(tostr(t32bitarray(sin)[i]));
  762. end;
  763. AsmLn;
  764. end;
  765. ait_comp_64bit :
  766. begin
  767. if do_line then
  768. AsmWriteLn(target_asm.comment+'value: '+extended2str(tai_comp_64bit(hp).value));
  769. AsmWrite(#9'.byte'#9);
  770. co:=comp(tai_comp_64bit(hp).value);
  771. { swap the values to correct endian if required }
  772. if source_info.endian <> target_info.endian then
  773. swap64bitarray(t64bitarray(co));
  774. for i:=0 to 7 do
  775. begin
  776. if i<>0 then
  777. AsmWrite(',');
  778. AsmWrite(tostr(t64bitarray(co)[i]));
  779. end;
  780. AsmLn;
  781. end;
  782. ait_string :
  783. begin
  784. pos:=0;
  785. for i:=1 to tai_string(hp).len do
  786. begin
  787. if pos=0 then
  788. begin
  789. AsmWrite(#9'.ascii'#9'"');
  790. pos:=20;
  791. end;
  792. ch:=tai_string(hp).str[i-1];
  793. case ch of
  794. #0, {This can't be done by range, because a bug in FPC}
  795. #1..#31,
  796. #128..#255 : s:='\'+tostr(ord(ch) shr 6)+tostr((ord(ch) and 63) shr 3)+tostr(ord(ch) and 7);
  797. '"' : s:='\"';
  798. '\' : s:='\\';
  799. else
  800. s:=ch;
  801. end;
  802. AsmWrite(s);
  803. inc(pos,length(s));
  804. if (pos>line_length) or (i=tai_string(hp).len) then
  805. begin
  806. AsmWriteLn('"');
  807. pos:=0;
  808. end;
  809. end;
  810. end;
  811. ait_label :
  812. begin
  813. if (tai_label(hp).labsym.is_used) then
  814. begin
  815. if tai_label(hp).labsym.bind=AB_GLOBAL then
  816. begin
  817. AsmWrite('.globl'#9);
  818. AsmWriteLn(tai_label(hp).labsym.name);
  819. end;
  820. AsmWrite(tai_label(hp).labsym.name);
  821. AsmWriteLn(':');
  822. end;
  823. end;
  824. ait_symbol :
  825. begin
  826. if (target_info.system = system_powerpc64_linux) and
  827. (tai_symbol(hp).sym.typ = AT_FUNCTION) and (cs_profile in current_settings.moduleswitches) then
  828. begin
  829. AsmWriteLn('.globl _mcount');
  830. end;
  831. if tai_symbol(hp).is_global then
  832. begin
  833. AsmWrite('.globl'#9);
  834. AsmWriteLn(tai_symbol(hp).sym.name);
  835. end;
  836. if (target_info.system = system_powerpc64_linux) and
  837. (tai_symbol(hp).sym.typ = AT_FUNCTION) then
  838. begin
  839. AsmWriteLn('.section ".opd", "aw"');
  840. AsmWriteLn('.align 3');
  841. AsmWriteLn(tai_symbol(hp).sym.name + ':');
  842. AsmWriteLn('.quad .' + tai_symbol(hp).sym.name + ', .TOC.@tocbase, 0');
  843. AsmWriteLn('.previous');
  844. AsmWriteLn('.size ' + tai_symbol(hp).sym.name + ', 24');
  845. if (tai_symbol(hp).is_global) then
  846. AsmWriteLn('.globl .' + tai_symbol(hp).sym.name);
  847. AsmWriteLn('.type .' + tai_symbol(hp).sym.name + ', @function');
  848. { the dotted name is the name of the actual function entry }
  849. AsmWrite('.');
  850. end
  851. else
  852. begin
  853. if (target_info.system <> system_arm_linux) then
  854. sepChar := '@'
  855. else
  856. sepChar := '#';
  857. if (tf_needs_symbol_type in target_info.flags) then
  858. begin
  859. AsmWrite(#9'.type'#9 + tai_symbol(hp).sym.name);
  860. if (needsObject(tai_symbol(hp))) then
  861. AsmWriteLn(',' + sepChar + 'object')
  862. else
  863. AsmWriteLn(',' + sepChar + 'function');
  864. end;
  865. end;
  866. AsmWriteLn(tai_symbol(hp).sym.name + ':');
  867. end;
  868. ait_symbol_end :
  869. begin
  870. if tf_needs_symbol_size in target_info.flags then
  871. begin
  872. s:=target_asm.labelprefix+'e'+tostr(symendcount);
  873. inc(symendcount);
  874. AsmWriteLn(s+':');
  875. AsmWrite(#9'.size'#9);
  876. if (target_info.system = system_powerpc64_linux) and (tai_symbol_end(hp).sym.typ = AT_FUNCTION) then
  877. AsmWrite('.');
  878. AsmWrite(tai_symbol_end(hp).sym.name);
  879. AsmWrite(', '+s+' - ');
  880. if (target_info.system = system_powerpc64_linux) and (tai_symbol_end(hp).sym.typ = AT_FUNCTION) then
  881. AsmWrite('.');
  882. AsmWriteLn(tai_symbol_end(hp).sym.name);
  883. end;
  884. end;
  885. ait_instruction :
  886. begin
  887. WriteInstruction(hp);
  888. end;
  889. ait_stab :
  890. begin
  891. if assigned(tai_stab(hp).str) then
  892. begin
  893. AsmWrite(#9'.'+stabtypestr[tai_stab(hp).stabtype]+' ');
  894. AsmWritePChar(tai_stab(hp).str);
  895. AsmLn;
  896. end;
  897. end;
  898. ait_force_line,
  899. ait_function_name : ;
  900. ait_cutobject :
  901. begin
  902. if SmartAsm then
  903. begin
  904. { only reset buffer if nothing has changed }
  905. if AsmSize=AsmStartSize then
  906. AsmClear
  907. else
  908. begin
  909. AsmClose;
  910. DoAssemble;
  911. AsmCreate(tai_cutobject(hp).place);
  912. end;
  913. { avoid empty files }
  914. while assigned(hp.next) and (tai(hp.next).typ in [ait_cutobject,ait_section,ait_comment]) do
  915. begin
  916. if tai(hp.next).typ=ait_section then
  917. CurrSecType:=tai_section(hp.next).sectype;
  918. hp:=tai(hp.next);
  919. end;
  920. if CurrSecType<>sec_none then
  921. WriteSection(CurrSecType,'',secorder_default);
  922. AsmStartSize:=AsmSize;
  923. end;
  924. end;
  925. ait_marker :
  926. if tai_marker(hp).kind=mark_InlineStart then
  927. inc(InlineLevel)
  928. else if tai_marker(hp).kind=mark_InlineEnd then
  929. dec(InlineLevel);
  930. ait_directive :
  931. begin
  932. AsmWrite('.'+directivestr[tai_directive(hp).directive]+' ');
  933. if assigned(tai_directive(hp).name) then
  934. AsmWrite(tai_directive(hp).name^);
  935. AsmLn;
  936. end;
  937. else
  938. internalerror(2006012201);
  939. end;
  940. hp:=tai(hp.next);
  941. end;
  942. end;
  943. procedure TGNUAssembler.WriteExtraHeader;
  944. begin
  945. end;
  946. procedure TGNUAssembler.WriteInstruction(hp: tai);
  947. begin
  948. InstrWriter.WriteInstruction(hp);
  949. end;
  950. procedure TGNUAssembler.WriteAsmList;
  951. var
  952. n : string;
  953. hal : tasmlisttype;
  954. begin
  955. {$ifdef EXTDEBUG}
  956. if assigned(current_module.mainsource) then
  957. Comment(V_Debug,'Start writing gas-styled assembler output for '+current_module.mainsource^);
  958. {$endif}
  959. CurrSecType:=sec_none;
  960. FillChar(lastfileinfo,sizeof(lastfileinfo),0);
  961. LastInfile:=nil;
  962. if assigned(current_module.mainsource) then
  963. n:=ExtractFileName(current_module.mainsource^)
  964. else
  965. n:=InputFileName;
  966. AsmWriteLn(#9'.file "'+FixFileName(n)+'"');
  967. WriteExtraHeader;
  968. AsmStartSize:=AsmSize;
  969. symendcount:=0;
  970. for hal:=low(TasmlistType) to high(TasmlistType) do
  971. begin
  972. AsmWriteLn(target_asm.comment+'Begin asmlist '+AsmlistTypeStr[hal]);
  973. writetree(current_asmdata.asmlists[hal]);
  974. AsmWriteLn(target_asm.comment+'End asmlist '+AsmlistTypeStr[hal]);
  975. end;
  976. {
  977. Result doesn't work properly yet due to a bug in Apple's linker
  978. if (cs_create_smart in current_settings.moduleswitches) and
  979. (target_info.system in systems_darwin) then
  980. AsmWriteLn(#9'.subsections_via_symbols');
  981. }
  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. end;
  1037. result := inherited sectionname(atype,aname,aorder);
  1038. end;
  1039. {****************************************************************************}
  1040. { a.out/GNU Assembler writer }
  1041. {****************************************************************************}
  1042. function TAoutGNUAssembler.sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;
  1043. const
  1044. (* Translation table - replace unsupported section types with basic ones. *)
  1045. SecXTable: array[TAsmSectionType] of TAsmSectionType = (
  1046. sec_none,
  1047. sec_code,
  1048. sec_data,
  1049. sec_data (* sec_rodata *),
  1050. sec_data (* sec_rodata_norel *),
  1051. sec_bss,
  1052. sec_data (* sec_threadvar *),
  1053. { used for wince exception handling }
  1054. sec_code (* sec_pdata *),
  1055. { used for darwin import stubs }
  1056. sec_code (* sec_stub *),
  1057. { stabs }
  1058. sec_stab,sec_stabstr,
  1059. { win32 }
  1060. sec_data (* sec_idata2 *),
  1061. sec_data (* sec_idata4 *),
  1062. sec_data (* sec_idata5 *),
  1063. sec_data (* sec_idata6 *),
  1064. sec_data (* sec_idata7 *),
  1065. sec_data (* sec_edata *),
  1066. { C++ exception handling unwinding (uses dwarf) }
  1067. sec_eh_frame,
  1068. { dwarf }
  1069. sec_debug_frame,
  1070. sec_debug_info,
  1071. sec_debug_line,
  1072. sec_debug_abbrev,
  1073. { ELF resources (+ references to stabs debug information sections) }
  1074. sec_code (* sec_fpc *),
  1075. { Table of contents section }
  1076. sec_code (* sec_toc *),
  1077. sec_code (* sec_init *),
  1078. sec_code (* sec_fini *)
  1079. );
  1080. begin
  1081. Result := inherited SectionName (SecXTable [AType], AName, AOrder);
  1082. end;
  1083. {****************************************************************************}
  1084. { Abstract Instruction Writer }
  1085. {****************************************************************************}
  1086. constructor TCPUInstrWriter.create(_owner: TGNUAssembler);
  1087. begin
  1088. inherited create;
  1089. owner := _owner;
  1090. end;
  1091. end.