aggas.pas 40 KB

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