aggas.pas 39 KB

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