aggas.pas 40 KB

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