2
0

aggas.pas 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550
  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 }
  34. TGNUAssembler=class(texternalassembler)
  35. protected
  36. function sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;virtual;
  37. procedure WriteSection(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder);
  38. procedure WriteExtraHeader;virtual;
  39. procedure WriteInstruction(hp: tai);
  40. procedure WriteWeakSymbolDef(s: tasmsymbol); virtual;
  41. public
  42. function MakeCmdLine: TCmdStr; override;
  43. procedure WriteTree(p:TAsmList);override;
  44. procedure WriteAsmList;override;
  45. destructor destroy; override;
  46. private
  47. setcount: longint;
  48. procedure WriteDecodedSleb128(a: int64);
  49. procedure WriteDecodedUleb128(a: qword);
  50. function NextSetLabel: string;
  51. protected
  52. InstrWriter: TCPUInstrWriter;
  53. end;
  54. {# This is the base class for writing instructions.
  55. The WriteInstruction() method must be overriden
  56. to write a single instruction to the assembler
  57. file.
  58. }
  59. TCPUInstrWriter = class
  60. constructor create(_owner: TGNUAssembler);
  61. procedure WriteInstruction(hp : tai); virtual; abstract;
  62. protected
  63. owner: TGNUAssembler;
  64. end;
  65. { TAppleGNUAssembler }
  66. TAppleGNUAssembler=class(TGNUAssembler)
  67. protected
  68. function sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;override;
  69. procedure WriteWeakSymbolDef(s: tasmsymbol); override;
  70. end;
  71. TAoutGNUAssembler=class(TGNUAssembler)
  72. function sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;override;
  73. end;
  74. implementation
  75. uses
  76. SysUtils,
  77. cutils,cfileutl,systems,
  78. fmodule,finput,verbose,
  79. itcpugas,cpubase
  80. ;
  81. const
  82. line_length = 70;
  83. var
  84. symendcount : longint;
  85. type
  86. {$ifdef cpuextended}
  87. t80bitarray = array[0..9] of byte;
  88. {$endif cpuextended}
  89. t64bitarray = array[0..7] of byte;
  90. t32bitarray = array[0..3] of byte;
  91. {****************************************************************************}
  92. { Support routines }
  93. {****************************************************************************}
  94. function fixline(s:string):string;
  95. {
  96. return s with all leading and ending spaces and tabs removed
  97. }
  98. var
  99. i,j,k : integer;
  100. begin
  101. i:=length(s);
  102. while (i>0) and (s[i] in [#9,' ']) do
  103. dec(i);
  104. j:=1;
  105. while (j<i) and (s[j] in [#9,' ']) do
  106. inc(j);
  107. for k:=j to i do
  108. if s[k] in [#0..#31,#127..#255] then
  109. s[k]:='.';
  110. fixline:=Copy(s,j,i-j+1);
  111. end;
  112. function single2str(d : single) : string;
  113. var
  114. hs : string;
  115. begin
  116. str(d,hs);
  117. { replace space with + }
  118. if hs[1]=' ' then
  119. hs[1]:='+';
  120. single2str:='0d'+hs
  121. end;
  122. function double2str(d : double) : string;
  123. var
  124. hs : string;
  125. begin
  126. str(d,hs);
  127. { replace space with + }
  128. if hs[1]=' ' then
  129. hs[1]:='+';
  130. double2str:='0d'+hs
  131. end;
  132. function extended2str(e : extended) : string;
  133. var
  134. hs : string;
  135. begin
  136. str(e,hs);
  137. { replace space with + }
  138. if hs[1]=' ' then
  139. hs[1]:='+';
  140. extended2str:='0d'+hs
  141. end;
  142. { convert floating point values }
  143. { to correct endian }
  144. procedure swap64bitarray(var t: t64bitarray);
  145. var
  146. b: byte;
  147. begin
  148. b:= t[7];
  149. t[7] := t[0];
  150. t[0] := b;
  151. b := t[6];
  152. t[6] := t[1];
  153. t[1] := b;
  154. b:= t[5];
  155. t[5] := t[2];
  156. t[2] := b;
  157. b:= t[4];
  158. t[4] := t[3];
  159. t[3] := b;
  160. end;
  161. procedure swap32bitarray(var t: t32bitarray);
  162. var
  163. b: byte;
  164. begin
  165. b:= t[1];
  166. t[1]:= t[2];
  167. t[2]:= b;
  168. b:= t[0];
  169. t[0]:= t[3];
  170. t[3]:= b;
  171. end;
  172. const
  173. ait_const2str : array[aitconst_128bit..aitconst_darwin_dwarf_delta32] of string[20]=(
  174. #9'.fixme128'#9,#9'.quad'#9,#9'.long'#9,#9'.short'#9,#9'.byte'#9,
  175. #9'.sleb128'#9,#9'.uleb128'#9,
  176. #9'.rva'#9,#9'.secrel32'#9,#9'.quad'#9,#9'.long'#9
  177. );
  178. {****************************************************************************}
  179. { GNU Assembler writer }
  180. {****************************************************************************}
  181. destructor TGNUAssembler.Destroy;
  182. begin
  183. InstrWriter.free;
  184. inherited destroy;
  185. end;
  186. function TGNUAssembler.MakeCmdLine: TCmdStr;
  187. begin
  188. result := inherited MakeCmdLine;
  189. // MWE: disabled again. It generates dwarf info for the generated .s
  190. // files as well. This conflicts with the info we generate
  191. // if target_dbg.id = dbg_dwarf then
  192. // result := result + ' --gdwarf-2';
  193. end;
  194. function TGNUAssembler.NextSetLabel: string;
  195. begin
  196. inc(setcount);
  197. result := target_asm.labelprefix+'$set$'+tostr(setcount);
  198. end;
  199. function TGNUAssembler.sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;
  200. const
  201. secnames : array[TAsmSectiontype] of string[length('__DATA, __datacoal_nt,coalesced')] = ('',
  202. '.text',
  203. '.data',
  204. { why doesn't .rodata work? (FK) }
  205. { sometimes we have to create a data.rel.ro instead of .rodata, e.g. for }
  206. { vtables (and anything else containing relocations), otherwise those are }
  207. { not relocated properly on e.g. linux/ppc64. g++ generates there for a }
  208. { vtable for a class called Window: }
  209. { .section .data.rel.ro._ZTV6Window,"awG",@progbits,_ZTV6Window,comdat }
  210. { TODO: .data.ro not yet working}
  211. {$if defined(arm) or defined(powerpc)}
  212. '.rodata',
  213. {$else arm}
  214. '.data',
  215. {$endif arm}
  216. {$if defined(m68k)} { Amiga/m68k GNU AS doesn't seem to like .rodata (KB) }
  217. '.data',
  218. {$else}
  219. '.rodata',
  220. {$endif}
  221. '.bss',
  222. '.threadvar',
  223. '.pdata',
  224. '', { stubs }
  225. '__DATA,__nl_symbol_ptr',
  226. '__DATA,__la_symbol_ptr',
  227. '__DATA,__mod_init_func',
  228. '__DATA,__mod_term_func',
  229. '.stab',
  230. '.stabstr',
  231. '.idata$2','.idata$4','.idata$5','.idata$6','.idata$7','.edata',
  232. '.eh_frame',
  233. '.debug_frame','.debug_info','.debug_line','.debug_abbrev',
  234. '.fpc',
  235. '.toc',
  236. '.init',
  237. '.fini',
  238. '.objc_class',
  239. '.objc_meta_class',
  240. '.objc_cat_cls_meth',
  241. '.objc_cat_inst_meth',
  242. '.objc_protocol',
  243. '.objc_string_object',
  244. '.objc_cls_meth',
  245. '.objc_inst_meth',
  246. '.objc_cls_refs',
  247. '.objc_message_refs',
  248. '.objc_symbols',
  249. '.objc_category',
  250. '.objc_class_vars',
  251. '.objc_instance_vars',
  252. '.objc_module_info',
  253. '.objc_class_names',
  254. '.objc_meth_var_types',
  255. '.objc_meth_var_names',
  256. '.objc_selector_strs',
  257. '.objc_protocol_ext',
  258. '.objc_class_ext',
  259. '.objc_property',
  260. '.objc_image_info',
  261. '.objc_cstring_object',
  262. '.objc_sel_fixup',
  263. '__DATA,__objc_data',
  264. '__DATA,__objc_const',
  265. '.objc_superrefs',
  266. '__DATA, __datacoal_nt,coalesced',
  267. '.objc_classlist',
  268. '.objc_nlclasslist',
  269. '.objc_catlist',
  270. '.obcj_nlcatlist',
  271. '.objc_protolist'
  272. );
  273. secnames_pic : array[TAsmSectiontype] of string[length('__DATA, __datacoal_nt,coalesced')] = ('',
  274. '.text',
  275. '.data.rel',
  276. '.data.rel',
  277. '.data.rel',
  278. '.bss',
  279. '.threadvar',
  280. '.pdata',
  281. '', { stubs }
  282. '__DATA,__nl_symbol_ptr',
  283. '__DATA,__la_symbol_ptr',
  284. '__DATA,__mod_init_func',
  285. '__DATA,__mod_term_func',
  286. '.stab',
  287. '.stabstr',
  288. '.idata$2','.idata$4','.idata$5','.idata$6','.idata$7','.edata',
  289. '.eh_frame',
  290. '.debug_frame','.debug_info','.debug_line','.debug_abbrev',
  291. '.fpc',
  292. '.toc',
  293. '.init',
  294. '.fini',
  295. '.objc_class',
  296. '.objc_meta_class',
  297. '.objc_cat_cls_meth',
  298. '.objc_cat_inst_meth',
  299. '.objc_protocol',
  300. '.objc_string_object',
  301. '.objc_cls_meth',
  302. '.objc_inst_meth',
  303. '.objc_cls_refs',
  304. '.objc_message_refs',
  305. '.objc_symbols',
  306. '.objc_category',
  307. '.objc_class_vars',
  308. '.objc_instance_vars',
  309. '.objc_module_info',
  310. '.objc_class_names',
  311. '.objc_meth_var_types',
  312. '.objc_meth_var_names',
  313. '.objc_selector_strs',
  314. '.objc_protocol_ext',
  315. '.objc_class_ext',
  316. '.objc_property',
  317. '.objc_image_info',
  318. '.objc_cstring_object',
  319. '.objc_sel_fixup',
  320. '__DATA, __objc_data',
  321. '__DATA, __objc_const',
  322. '.objc_superrefs',
  323. '__DATA, __datacoal_nt,coalesced',
  324. '.objc_classlist',
  325. '.objc_nlclasslist',
  326. '.objc_catlist',
  327. '.obcj_nlcatlist',
  328. '.objc_protolist'
  329. );
  330. var
  331. sep : string[3];
  332. secname : string;
  333. begin
  334. if (cs_create_pic in current_settings.moduleswitches) and
  335. not(target_info.system in systems_darwin) then
  336. secname:=secnames_pic[atype]
  337. else
  338. secname:=secnames[atype];
  339. {$ifdef m68k}
  340. { old Amiga GNU AS doesn't support .section .fpc }
  341. if (atype=sec_fpc) and (target_info.system = system_m68k_amiga) then
  342. secname:=secnames[sec_data];
  343. {$endif}
  344. if (atype=sec_fpc) and (Copy(aname,1,3)='res') then
  345. begin
  346. result:=secname+'.'+aname;
  347. exit;
  348. end;
  349. if (atype=sec_threadvar) and
  350. (target_info.system=system_i386_win32) then
  351. secname:='.tls';
  352. { go32v2 stub only loads .text and .data sections, and allocates space for .bss.
  353. Thus, data which normally goes into .rodata and .rodata_norel sections must
  354. end up in .data section }
  355. if (atype in [sec_rodata,sec_rodata_norel]) and
  356. (target_info.system=system_i386_go32v2) then
  357. secname:='.data';
  358. { For bss we need to set some flags that are target dependent,
  359. it is easier to disable it for smartlinking. It doesn't take up
  360. filespace }
  361. if not(target_info.system in systems_darwin) and
  362. create_smartlink_sections and
  363. (aname<>'') and
  364. (atype<>sec_toc) and
  365. { on embedded systems every byte counts, so smartlink bss too }
  366. ((atype<>sec_bss) or (target_info.system in systems_embedded)) then
  367. begin
  368. case aorder of
  369. secorder_begin :
  370. sep:='.b_';
  371. secorder_end :
  372. sep:='.z_';
  373. else
  374. sep:='.n_';
  375. end;
  376. result:=secname+sep+aname
  377. end
  378. else
  379. result:=secname;
  380. end;
  381. procedure TGNUAssembler.WriteSection(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder);
  382. var
  383. s : string;
  384. begin
  385. AsmLn;
  386. case target_info.system of
  387. system_i386_OS2,
  388. system_i386_EMX,
  389. system_m68k_amiga, { amiga has old GNU AS (2.14), which blews up from .section (KB) }
  390. system_m68k_linux: ;
  391. system_powerpc_darwin,
  392. system_i386_darwin,
  393. system_powerpc64_darwin,
  394. system_x86_64_darwin,
  395. system_arm_darwin:
  396. begin
  397. if (atype in [sec_stub,sec_objc_data,sec_objc_const,sec_data_coalesced]) then
  398. AsmWrite('.section ');
  399. end
  400. else
  401. AsmWrite('.section ');
  402. end;
  403. s:=sectionname(atype,aname,aorder);
  404. AsmWrite(s);
  405. case atype of
  406. sec_fpc :
  407. if aname = 'resptrs' then
  408. AsmWrite(', "a", @progbits');
  409. sec_stub :
  410. begin
  411. case target_info.system of
  412. { there are processor-independent shortcuts available }
  413. { for this, namely .symbol_stub and .picsymbol_stub, but }
  414. { they don't work and gcc doesn't use them either... }
  415. system_powerpc_darwin,
  416. system_powerpc64_darwin:
  417. if (cs_create_pic in current_settings.moduleswitches) then
  418. AsmWriteln('__TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32')
  419. else
  420. AsmWriteln('__TEXT,__symbol_stub1,symbol_stubs,pure_instructions,16');
  421. system_i386_darwin:
  422. AsmWriteln('__IMPORT,__jump_table,symbol_stubs,self_modifying_code+pure_instructions,5');
  423. system_arm_darwin:
  424. if (cs_create_pic in current_settings.moduleswitches) then
  425. AsmWriteln('.section __TEXT,__picsymbolstub4,symbol_stubs,none,16')
  426. else
  427. AsmWriteln('.section __TEXT,__symbol_stub4,symbol_stubs,none,12')
  428. { darwin/x86-64 uses RIP-based GOT addressing, no symbol stubs }
  429. else
  430. internalerror(2006031101);
  431. end;
  432. end;
  433. end;
  434. AsmLn;
  435. LastSecType:=atype;
  436. end;
  437. procedure TGNUAssembler.WriteDecodedUleb128(a: qword);
  438. var
  439. i,len : longint;
  440. buf : array[0..63] of byte;
  441. begin
  442. len:=EncodeUleb128(a,buf);
  443. for i:=0 to len-1 do
  444. begin
  445. if (i > 0) then
  446. AsmWrite(',');
  447. AsmWrite(tostr(buf[i]));
  448. end;
  449. end;
  450. procedure TGNUAssembler.WriteDecodedSleb128(a: int64);
  451. var
  452. i,len : longint;
  453. buf : array[0..255] of byte;
  454. begin
  455. len:=EncodeSleb128(a,buf);
  456. for i:=0 to len-1 do
  457. begin
  458. if (i > 0) then
  459. AsmWrite(',');
  460. AsmWrite(tostr(buf[i]));
  461. end;
  462. end;
  463. procedure TGNUAssembler.WriteTree(p:TAsmList);
  464. function needsObject(hp : tai_symbol) : boolean;
  465. begin
  466. needsObject :=
  467. (
  468. assigned(hp.next) and
  469. (tai(hp.next).typ in [ait_const,ait_datablock,
  470. ait_real_32bit,ait_real_64bit,ait_real_80bit,ait_comp_64bit])
  471. ) or
  472. (hp.sym.typ=AT_DATA);
  473. end;
  474. procedure doalign(alignment: byte; use_op: boolean; fillop: byte; out last_align: longint);
  475. var
  476. i: longint;
  477. begin
  478. last_align:=alignment;
  479. if alignment>1 then
  480. begin
  481. if not(target_info.system in systems_darwin) then
  482. begin
  483. AsmWrite(#9'.balign '+tostr(alignment));
  484. if use_op then
  485. AsmWrite(','+tostr(fillop))
  486. {$ifdef x86}
  487. { force NOP as alignment op code }
  488. else if LastSecType=sec_code then
  489. AsmWrite(',0x90');
  490. {$endif x86}
  491. end
  492. else
  493. begin
  494. { darwin as only supports .align }
  495. if not ispowerof2(alignment,i) then
  496. internalerror(2003010305);
  497. AsmWrite(#9'.align '+tostr(i));
  498. last_align:=i;
  499. end;
  500. AsmLn;
  501. end;
  502. end;
  503. var
  504. ch : char;
  505. hp : tai;
  506. hp1 : tailineinfo;
  507. constdef : taiconst_type;
  508. s,t : string;
  509. i,pos,l : longint;
  510. InlineLevel : longint;
  511. last_align : longint;
  512. co : comp;
  513. sin : single;
  514. d : double;
  515. {$ifdef cpuextended}
  516. e : extended;
  517. {$endif cpuextended}
  518. do_line : boolean;
  519. sepChar : char;
  520. begin
  521. if not assigned(p) then
  522. exit;
  523. last_align := 2;
  524. InlineLevel:=0;
  525. { lineinfo is only needed for al_procedures (PFV) }
  526. do_line:=(cs_asm_source in current_settings.globalswitches) or
  527. ((cs_lineinfo in current_settings.moduleswitches)
  528. and (p=current_asmdata.asmlists[al_procedures]));
  529. hp:=tai(p.first);
  530. while assigned(hp) do
  531. begin
  532. prefetch(pointer(hp.next)^);
  533. if not(hp.typ in SkipLineInfo) then
  534. begin
  535. hp1 := hp as tailineinfo;
  536. current_filepos:=hp1.fileinfo;
  537. { no line info for inlined code }
  538. if do_line and (inlinelevel=0) then
  539. begin
  540. { load infile }
  541. if lastfileinfo.fileindex<>hp1.fileinfo.fileindex then
  542. begin
  543. infile:=current_module.sourcefiles.get_file(hp1.fileinfo.fileindex);
  544. if assigned(infile) then
  545. begin
  546. { open only if needed !! }
  547. if (cs_asm_source in current_settings.globalswitches) then
  548. infile.open;
  549. end;
  550. { avoid unnecessary reopens of the same file !! }
  551. lastfileinfo.fileindex:=hp1.fileinfo.fileindex;
  552. { be sure to change line !! }
  553. lastfileinfo.line:=-1;
  554. end;
  555. { write source }
  556. if (cs_asm_source in current_settings.globalswitches) and
  557. assigned(infile) then
  558. begin
  559. if (infile<>lastinfile) then
  560. begin
  561. AsmWriteLn(target_asm.comment+'['+infile.name^+']');
  562. if assigned(lastinfile) then
  563. lastinfile.close;
  564. end;
  565. if (hp1.fileinfo.line<>lastfileinfo.line) and
  566. ((hp1.fileinfo.line<infile.maxlinebuf) or (InlineLevel>0)) then
  567. begin
  568. if (hp1.fileinfo.line<>0) and
  569. ((infile.linebuf^[hp1.fileinfo.line]>=0) or (InlineLevel>0)) then
  570. AsmWriteLn(target_asm.comment+'['+tostr(hp1.fileinfo.line)+'] '+
  571. fixline(infile.GetLineStr(hp1.fileinfo.line)));
  572. { set it to a negative value !
  573. to make that is has been read already !! PM }
  574. if (infile.linebuf^[hp1.fileinfo.line]>=0) then
  575. infile.linebuf^[hp1.fileinfo.line]:=-infile.linebuf^[hp1.fileinfo.line]-1;
  576. end;
  577. end;
  578. lastfileinfo:=hp1.fileinfo;
  579. lastinfile:=infile;
  580. end;
  581. end;
  582. case hp.typ of
  583. ait_comment :
  584. Begin
  585. AsmWrite(target_asm.comment);
  586. AsmWritePChar(tai_comment(hp).str);
  587. AsmLn;
  588. End;
  589. ait_regalloc :
  590. begin
  591. if (cs_asm_regalloc in current_settings.globalswitches) then
  592. begin
  593. AsmWrite(#9+target_asm.comment+'Register ');
  594. repeat
  595. AsmWrite(std_regname(Tai_regalloc(hp).reg));
  596. if (hp.next=nil) or
  597. (tai(hp.next).typ<>ait_regalloc) or
  598. (tai_regalloc(hp.next).ratype<>tai_regalloc(hp).ratype) then
  599. break;
  600. hp:=tai(hp.next);
  601. AsmWrite(',');
  602. until false;
  603. AsmWrite(' ');
  604. AsmWriteLn(regallocstr[tai_regalloc(hp).ratype]);
  605. end;
  606. end;
  607. ait_tempalloc :
  608. begin
  609. if (cs_asm_tempalloc in current_settings.globalswitches) then
  610. begin
  611. {$ifdef EXTDEBUG}
  612. if assigned(tai_tempalloc(hp).problem) then
  613. AsmWriteLn(target_asm.comment+'Temp '+tostr(tai_tempalloc(hp).temppos)+','+
  614. tostr(tai_tempalloc(hp).tempsize)+' '+tai_tempalloc(hp).problem^)
  615. else
  616. {$endif EXTDEBUG}
  617. AsmWriteLn(target_asm.comment+'Temp '+tostr(tai_tempalloc(hp).temppos)+','+
  618. tostr(tai_tempalloc(hp).tempsize)+' '+tempallocstr[tai_tempalloc(hp).allocation]);
  619. end;
  620. end;
  621. ait_align :
  622. begin
  623. doalign(tai_align_abstract(hp).aligntype,tai_align_abstract(hp).use_op,tai_align_abstract(hp).fillop,last_align);
  624. end;
  625. ait_section :
  626. begin
  627. if tai_section(hp).sectype<>sec_none then
  628. WriteSection(tai_section(hp).sectype,tai_section(hp).name^,tai_section(hp).secorder)
  629. else
  630. begin
  631. {$ifdef EXTDEBUG}
  632. AsmWrite(target_asm.comment);
  633. AsmWriteln(' sec_none');
  634. {$endif EXTDEBUG}
  635. end;
  636. doalign(tai_section(hp).secalign,false,0,last_align);
  637. end;
  638. ait_datablock :
  639. begin
  640. if (target_info.system in systems_darwin) then
  641. begin
  642. { On Mac OS X you can't have common symbols in a shared library
  643. since those are in the TEXT section and the text section is
  644. read-only in shared libraries (so it can be shared among different
  645. processes). The alternate code creates some kind of common symbols
  646. in the data segment.
  647. }
  648. if tai_datablock(hp).is_global then
  649. begin
  650. asmwrite('.globl ');
  651. asmwriteln(tai_datablock(hp).sym.name);
  652. asmwriteln('.data');
  653. asmwrite('.zerofill __DATA, __common, ');
  654. asmwrite(tai_datablock(hp).sym.name);
  655. asmwriteln(', '+tostr(tai_datablock(hp).size)+','+tostr(last_align));
  656. if not(LastSecType in [sec_data,sec_none]) then
  657. writesection(LastSecType,'',secorder_default);
  658. end
  659. else
  660. begin
  661. asmwrite(#9'.lcomm'#9);
  662. asmwrite(tai_datablock(hp).sym.name);
  663. asmwrite(','+tostr(tai_datablock(hp).size));
  664. asmwrite(','+tostr(last_align));
  665. asmln;
  666. end;
  667. end
  668. else
  669. begin
  670. {$ifdef USE_COMM_IN_BSS}
  671. if writingpackages then
  672. begin
  673. { The .comm is required for COMMON symbols. These are used
  674. in the shared library loading. All the symbols declared in
  675. the .so file need to resolve to the data allocated in the main
  676. program (PFV) }
  677. if tai_datablock(hp).is_global then
  678. begin
  679. asmwrite(#9'.comm'#9);
  680. asmwrite(tai_datablock(hp).sym.name);
  681. asmwrite(','+tostr(tai_datablock(hp).size));
  682. asmwrite(','+tostr(last_align));
  683. asmln;
  684. end
  685. else
  686. begin
  687. asmwrite(#9'.lcomm'#9);
  688. asmwrite(tai_datablock(hp).sym.name);
  689. asmwrite(','+tostr(tai_datablock(hp).size));
  690. asmwrite(','+tostr(last_align));
  691. asmln;
  692. end
  693. end
  694. else
  695. {$endif USE_COMM_IN_BSS}
  696. begin
  697. if Tai_datablock(hp).is_global then
  698. begin
  699. asmwrite(#9'.globl ');
  700. asmwriteln(Tai_datablock(hp).sym.name);
  701. end;
  702. if (target_info.system <> system_arm_linux) then
  703. sepChar := '@'
  704. else
  705. sepChar := '%';
  706. if (tf_needs_symbol_type in target_info.flags) then
  707. asmwriteln(#9'.type '+Tai_datablock(hp).sym.name+','+sepChar+'object');
  708. if (tf_needs_symbol_size in target_info.flags) and (tai_datablock(hp).size > 0) then
  709. asmwriteln(#9'.size '+Tai_datablock(hp).sym.name+','+tostr(Tai_datablock(hp).size));
  710. asmwrite(Tai_datablock(hp).sym.name);
  711. asmwriteln(':');
  712. asmwriteln(#9'.zero '+tostr(Tai_datablock(hp).size));
  713. end;
  714. end;
  715. end;
  716. ait_const:
  717. begin
  718. constdef:=tai_const(hp).consttype;
  719. case constdef of
  720. {$ifndef cpu64bitaddr}
  721. aitconst_128bit :
  722. begin
  723. internalerror(200404291);
  724. end;
  725. aitconst_64bit :
  726. begin
  727. if assigned(tai_const(hp).sym) then
  728. internalerror(200404292);
  729. AsmWrite(ait_const2str[aitconst_32bit]);
  730. if target_info.endian = endian_little then
  731. begin
  732. AsmWrite(tostr(longint(lo(tai_const(hp).value))));
  733. AsmWrite(',');
  734. AsmWrite(tostr(longint(hi(tai_const(hp).value))));
  735. end
  736. else
  737. begin
  738. AsmWrite(tostr(longint(hi(tai_const(hp).value))));
  739. AsmWrite(',');
  740. AsmWrite(tostr(longint(lo(tai_const(hp).value))));
  741. end;
  742. AsmLn;
  743. end;
  744. {$endif cpu64bitaddr}
  745. aitconst_uleb128bit,
  746. aitconst_sleb128bit,
  747. {$ifdef cpu64bitaddr}
  748. aitconst_128bit,
  749. aitconst_64bit,
  750. {$endif cpu64bitaddr}
  751. aitconst_32bit,
  752. aitconst_16bit,
  753. aitconst_8bit,
  754. aitconst_rva_symbol,
  755. aitconst_secrel32_symbol,
  756. aitconst_darwin_dwarf_delta32,
  757. aitconst_darwin_dwarf_delta64:
  758. begin
  759. if (target_info.system in systems_darwin) and
  760. (constdef in [aitconst_uleb128bit,aitconst_sleb128bit]) then
  761. begin
  762. AsmWrite(ait_const2str[aitconst_8bit]);
  763. case tai_const(hp).consttype of
  764. aitconst_uleb128bit:
  765. WriteDecodedUleb128(qword(tai_const(hp).value));
  766. aitconst_sleb128bit:
  767. WriteDecodedSleb128(int64(tai_const(hp).value));
  768. end
  769. end
  770. else
  771. begin
  772. AsmWrite(ait_const2str[constdef]);
  773. l:=0;
  774. t := '';
  775. repeat
  776. if assigned(tai_const(hp).sym) then
  777. begin
  778. if assigned(tai_const(hp).endsym) then
  779. begin
  780. if (constdef in [aitconst_darwin_dwarf_delta32,aitconst_darwin_dwarf_delta64]) then
  781. begin
  782. s := NextSetLabel;
  783. t := #9'.set '+s+','+tai_const(hp).endsym.name+'-'+tai_const(hp).sym.name;
  784. end
  785. else
  786. s:=tai_const(hp).endsym.name+'-'+tai_const(hp).sym.name
  787. end
  788. else
  789. s:=tai_const(hp).sym.name;
  790. if tai_const(hp).value<>0 then
  791. s:=s+tostr_with_plus(tai_const(hp).value);
  792. end
  793. else
  794. s:=tostr(tai_const(hp).value);
  795. AsmWrite(s);
  796. inc(l,length(s));
  797. { Values with symbols are written on a single line to improve
  798. reading of the .s file (PFV) }
  799. if assigned(tai_const(hp).sym) or
  800. not(LastSecType in [sec_data,sec_rodata,sec_rodata_norel]) or
  801. (l>line_length) or
  802. (hp.next=nil) or
  803. (tai(hp.next).typ<>ait_const) or
  804. (tai_const(hp.next).consttype<>constdef) or
  805. assigned(tai_const(hp.next).sym) then
  806. break;
  807. hp:=tai(hp.next);
  808. AsmWrite(',');
  809. until false;
  810. if (t <> '') then
  811. begin
  812. AsmLn;
  813. AsmWrite(t);
  814. end;
  815. end;
  816. AsmLn;
  817. end;
  818. else
  819. internalerror(200704251);
  820. end;
  821. end;
  822. { the "and defined(FPC_HAS_TYPE_EXTENDED)" isn't optimal but currently the only solution
  823. it prevents proper cross compilation to i386 though
  824. }
  825. {$if defined(cpuextended) and defined(FPC_HAS_TYPE_EXTENDED)}
  826. ait_real_80bit :
  827. begin
  828. if do_line then
  829. AsmWriteLn(target_asm.comment+'value: '+extended2str(tai_real_80bit(hp).value));
  830. { Make sure e is a extended type, bestreal could be
  831. a different type (bestreal) !! (PFV) }
  832. e:=tai_real_80bit(hp).value;
  833. AsmWrite(#9'.byte'#9);
  834. for i:=0 to 9 do
  835. begin
  836. if i<>0 then
  837. AsmWrite(',');
  838. AsmWrite(tostr(t80bitarray(e)[i]));
  839. end;
  840. for i:=11 to tai_real_80bit(hp).savesize do
  841. AsmWrite(',0');
  842. AsmLn;
  843. end;
  844. {$endif cpuextended}
  845. ait_real_64bit :
  846. begin
  847. if do_line then
  848. AsmWriteLn(target_asm.comment+'value: '+double2str(tai_real_64bit(hp).value));
  849. d:=tai_real_64bit(hp).value;
  850. { swap the values to correct endian if required }
  851. if source_info.endian <> target_info.endian then
  852. swap64bitarray(t64bitarray(d));
  853. AsmWrite(#9'.byte'#9);
  854. {$ifdef arm}
  855. if tai_real_64bit(hp).formatoptions=fo_hiloswapped then
  856. begin
  857. for i:=4 to 7 do
  858. begin
  859. if i<>4 then
  860. AsmWrite(',');
  861. AsmWrite(tostr(t64bitarray(d)[i]));
  862. end;
  863. for i:=0 to 3 do
  864. begin
  865. AsmWrite(',');
  866. AsmWrite(tostr(t64bitarray(d)[i]));
  867. end;
  868. end
  869. else
  870. {$endif arm}
  871. begin
  872. for i:=0 to 7 do
  873. begin
  874. if i<>0 then
  875. AsmWrite(',');
  876. AsmWrite(tostr(t64bitarray(d)[i]));
  877. end;
  878. end;
  879. AsmLn;
  880. end;
  881. ait_real_32bit :
  882. begin
  883. if do_line then
  884. AsmWriteLn(target_asm.comment+'value: '+single2str(tai_real_32bit(hp).value));
  885. sin:=tai_real_32bit(hp).value;
  886. { swap the values to correct endian if required }
  887. if source_info.endian <> target_info.endian then
  888. swap32bitarray(t32bitarray(sin));
  889. AsmWrite(#9'.byte'#9);
  890. for i:=0 to 3 do
  891. begin
  892. if i<>0 then
  893. AsmWrite(',');
  894. AsmWrite(tostr(t32bitarray(sin)[i]));
  895. end;
  896. AsmLn;
  897. end;
  898. ait_comp_64bit :
  899. begin
  900. if do_line then
  901. AsmWriteLn(target_asm.comment+'value: '+extended2str(tai_comp_64bit(hp).value));
  902. AsmWrite(#9'.byte'#9);
  903. co:=comp(tai_comp_64bit(hp).value);
  904. { swap the values to correct endian if required }
  905. if source_info.endian <> target_info.endian then
  906. swap64bitarray(t64bitarray(co));
  907. for i:=0 to 7 do
  908. begin
  909. if i<>0 then
  910. AsmWrite(',');
  911. AsmWrite(tostr(t64bitarray(co)[i]));
  912. end;
  913. AsmLn;
  914. end;
  915. ait_string :
  916. begin
  917. pos:=0;
  918. for i:=1 to tai_string(hp).len do
  919. begin
  920. if pos=0 then
  921. begin
  922. AsmWrite(#9'.ascii'#9'"');
  923. pos:=20;
  924. end;
  925. ch:=tai_string(hp).str[i-1];
  926. case ch of
  927. #0, {This can't be done by range, because a bug in FPC}
  928. #1..#31,
  929. #128..#255 : s:='\'+tostr(ord(ch) shr 6)+tostr((ord(ch) and 63) shr 3)+tostr(ord(ch) and 7);
  930. '"' : s:='\"';
  931. '\' : s:='\\';
  932. else
  933. s:=ch;
  934. end;
  935. AsmWrite(s);
  936. inc(pos,length(s));
  937. if (pos>line_length) or (i=tai_string(hp).len) then
  938. begin
  939. AsmWriteLn('"');
  940. pos:=0;
  941. end;
  942. end;
  943. end;
  944. ait_label :
  945. begin
  946. if (tai_label(hp).labsym.is_used) then
  947. begin
  948. if (tai_label(hp).labsym.bind=AB_PRIVATE_EXTERN) then
  949. begin
  950. AsmWrite(#9'.private_extern ');
  951. AsmWriteln(tai_label(hp).labsym.name);
  952. end;
  953. if tai_label(hp).labsym.bind in [AB_GLOBAL,AB_PRIVATE_EXTERN] then
  954. begin
  955. AsmWrite('.globl'#9);
  956. AsmWriteLn(tai_label(hp).labsym.name);
  957. end;
  958. AsmWrite(tai_label(hp).labsym.name);
  959. AsmWriteLn(':');
  960. end;
  961. end;
  962. ait_symbol :
  963. begin
  964. if (tai_symbol(hp).sym.bind=AB_PRIVATE_EXTERN) then
  965. begin
  966. AsmWrite(#9'.private_extern ');
  967. AsmWriteln(tai_symbol(hp).sym.name);
  968. end;
  969. if (target_info.system = system_powerpc64_linux) and
  970. (tai_symbol(hp).sym.typ = AT_FUNCTION) and (cs_profile in current_settings.moduleswitches) then
  971. begin
  972. AsmWriteLn('.globl _mcount');
  973. end;
  974. if tai_symbol(hp).is_global then
  975. begin
  976. AsmWrite('.globl'#9);
  977. AsmWriteLn(tai_symbol(hp).sym.name);
  978. end;
  979. if (target_info.system = system_powerpc64_linux) and
  980. (tai_symbol(hp).sym.typ = AT_FUNCTION) then
  981. begin
  982. AsmWriteLn('.section ".opd", "aw"');
  983. AsmWriteLn('.align 3');
  984. AsmWriteLn(tai_symbol(hp).sym.name + ':');
  985. AsmWriteLn('.quad .' + tai_symbol(hp).sym.name + ', .TOC.@tocbase, 0');
  986. AsmWriteLn('.previous');
  987. AsmWriteLn('.size ' + tai_symbol(hp).sym.name + ', 24');
  988. if (tai_symbol(hp).is_global) then
  989. AsmWriteLn('.globl .' + tai_symbol(hp).sym.name);
  990. AsmWriteLn('.type .' + tai_symbol(hp).sym.name + ', @function');
  991. { the dotted name is the name of the actual function entry }
  992. AsmWrite('.');
  993. end
  994. else
  995. begin
  996. if (target_info.system <> system_arm_linux) then
  997. sepChar := '@'
  998. else
  999. sepChar := '#';
  1000. if (tf_needs_symbol_type in target_info.flags) then
  1001. begin
  1002. AsmWrite(#9'.type'#9 + tai_symbol(hp).sym.name);
  1003. if (needsObject(tai_symbol(hp))) then
  1004. AsmWriteLn(',' + sepChar + 'object')
  1005. else
  1006. AsmWriteLn(',' + sepChar + 'function');
  1007. end;
  1008. end;
  1009. if not(tai_symbol(hp).has_value) then
  1010. AsmWriteLn(tai_symbol(hp).sym.name + ':')
  1011. else
  1012. AsmWriteLn(tai_symbol(hp).sym.name + '=' + tostr(tai_symbol(hp).value));
  1013. end;
  1014. {$ifdef arm}
  1015. ait_thumb_func:
  1016. begin
  1017. AsmWriteLn(#9'.thumb_func');
  1018. end;
  1019. {$endif arm}
  1020. ait_symbol_end :
  1021. begin
  1022. if tf_needs_symbol_size in target_info.flags then
  1023. begin
  1024. s:=target_asm.labelprefix+'e'+tostr(symendcount);
  1025. inc(symendcount);
  1026. AsmWriteLn(s+':');
  1027. AsmWrite(#9'.size'#9);
  1028. if (target_info.system = system_powerpc64_linux) and (tai_symbol_end(hp).sym.typ = AT_FUNCTION) then
  1029. AsmWrite('.');
  1030. AsmWrite(tai_symbol_end(hp).sym.name);
  1031. AsmWrite(', '+s+' - ');
  1032. if (target_info.system = system_powerpc64_linux) and (tai_symbol_end(hp).sym.typ = AT_FUNCTION) then
  1033. AsmWrite('.');
  1034. AsmWriteLn(tai_symbol_end(hp).sym.name);
  1035. end;
  1036. end;
  1037. ait_instruction :
  1038. begin
  1039. WriteInstruction(hp);
  1040. end;
  1041. ait_stab :
  1042. begin
  1043. if assigned(tai_stab(hp).str) then
  1044. begin
  1045. AsmWrite(#9'.'+stabtypestr[tai_stab(hp).stabtype]+' ');
  1046. AsmWritePChar(tai_stab(hp).str);
  1047. AsmLn;
  1048. end;
  1049. end;
  1050. ait_force_line,
  1051. ait_function_name : ;
  1052. ait_cutobject :
  1053. begin
  1054. if SmartAsm then
  1055. begin
  1056. { only reset buffer if nothing has changed }
  1057. if AsmSize=AsmStartSize then
  1058. AsmClear
  1059. else
  1060. begin
  1061. AsmClose;
  1062. DoAssemble;
  1063. AsmCreate(tai_cutobject(hp).place);
  1064. end;
  1065. { avoid empty files }
  1066. while assigned(hp.next) and (tai(hp.next).typ in [ait_cutobject,ait_section,ait_comment]) do
  1067. begin
  1068. if tai(hp.next).typ=ait_section then
  1069. LastSecType:=tai_section(hp.next).sectype;
  1070. hp:=tai(hp.next);
  1071. end;
  1072. if LastSecType<>sec_none then
  1073. WriteSection(LastSecType,'',secorder_default);
  1074. AsmStartSize:=AsmSize;
  1075. end;
  1076. end;
  1077. ait_marker :
  1078. if tai_marker(hp).kind=mark_NoLineInfoStart then
  1079. inc(InlineLevel)
  1080. else if tai_marker(hp).kind=mark_NoLineInfoEnd then
  1081. dec(InlineLevel);
  1082. ait_directive :
  1083. begin
  1084. AsmWrite('.'+directivestr[tai_directive(hp).directive]+' ');
  1085. if assigned(tai_directive(hp).name) then
  1086. AsmWrite(tai_directive(hp).name^);
  1087. AsmLn;
  1088. end;
  1089. else
  1090. internalerror(2006012201);
  1091. end;
  1092. hp:=tai(hp.next);
  1093. end;
  1094. end;
  1095. procedure TGNUAssembler.WriteExtraHeader;
  1096. begin
  1097. end;
  1098. procedure TGNUAssembler.WriteInstruction(hp: tai);
  1099. begin
  1100. InstrWriter.WriteInstruction(hp);
  1101. end;
  1102. procedure TGNUAssembler.WriteWeakSymbolDef(s: tasmsymbol);
  1103. begin
  1104. AsmWriteLn(#9'.weak '+s.name);
  1105. end;
  1106. procedure TGNUAssembler.WriteAsmList;
  1107. var
  1108. n : string;
  1109. hal : tasmlisttype;
  1110. i: longint;
  1111. begin
  1112. {$ifdef EXTDEBUG}
  1113. if assigned(current_module.mainsource) then
  1114. Comment(V_Debug,'Start writing gas-styled assembler output for '+current_module.mainsource^);
  1115. {$endif}
  1116. if assigned(current_module.mainsource) then
  1117. n:=ExtractFileName(current_module.mainsource^)
  1118. else
  1119. n:=InputFileName;
  1120. { gcc does not add it either for Darwin (and AIX). Grep for
  1121. TARGET_ASM_FILE_START_FILE_DIRECTIVE in gcc/config/*.h
  1122. }
  1123. if not(target_info.system in systems_darwin) then
  1124. AsmWriteLn(#9'.file "'+FixFileName(n)+'"');
  1125. WriteExtraHeader;
  1126. AsmStartSize:=AsmSize;
  1127. symendcount:=0;
  1128. for hal:=low(TasmlistType) to high(TasmlistType) do
  1129. begin
  1130. AsmWriteLn(target_asm.comment+'Begin asmlist '+AsmlistTypeStr[hal]);
  1131. writetree(current_asmdata.asmlists[hal]);
  1132. AsmWriteLn(target_asm.comment+'End asmlist '+AsmlistTypeStr[hal]);
  1133. end;
  1134. { add weak symbol markers }
  1135. for i:=0 to current_asmdata.asmsymboldict.count-1 do
  1136. if (tasmsymbol(current_asmdata.asmsymboldict[i]).bind=AB_WEAK_EXTERNAL) then
  1137. writeweaksymboldef(tasmsymbol(current_asmdata.asmsymboldict[i]));
  1138. if create_smartlink_sections and
  1139. (target_info.system in systems_darwin) then
  1140. AsmWriteLn(#9'.subsections_via_symbols');
  1141. { "no executable stack" marker for Linux }
  1142. if (target_info.system in systems_linux) and
  1143. not(cs_executable_stack in current_settings.moduleswitches) then
  1144. begin
  1145. AsmWriteLn('.section .note.GNU-stack,"",%progbits');
  1146. end;
  1147. AsmLn;
  1148. {$ifdef EXTDEBUG}
  1149. if assigned(current_module.mainsource) then
  1150. Comment(V_Debug,'Done writing gas-styled assembler output for '+current_module.mainsource^);
  1151. {$endif EXTDEBUG}
  1152. end;
  1153. {****************************************************************************}
  1154. { Apple/GNU Assembler writer }
  1155. {****************************************************************************}
  1156. function TAppleGNUAssembler.sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;
  1157. begin
  1158. if (target_info.system in systems_darwin) then
  1159. case atype of
  1160. sec_bss:
  1161. { all bss (lcomm) symbols are automatically put in the right }
  1162. { place by using the lcomm assembler directive }
  1163. atype := sec_none;
  1164. sec_debug_frame,
  1165. sec_eh_frame:
  1166. begin
  1167. result := '.section __DWARF,__debug_info,regular,debug';
  1168. exit;
  1169. end;
  1170. sec_debug_line:
  1171. begin
  1172. result := '.section __DWARF,__debug_line,regular,debug';
  1173. exit;
  1174. end;
  1175. sec_debug_info:
  1176. begin
  1177. result := '.section __DWARF,__debug_info,regular,debug';
  1178. exit;
  1179. end;
  1180. sec_debug_abbrev:
  1181. begin
  1182. result := '.section __DWARF,__debug_abbrev,regular,debug';
  1183. exit;
  1184. end;
  1185. sec_rodata:
  1186. begin
  1187. result := '.const_data';
  1188. exit;
  1189. end;
  1190. sec_rodata_norel:
  1191. begin
  1192. result := '.const';
  1193. exit;
  1194. end;
  1195. sec_fpc:
  1196. begin
  1197. result := '.section __TEXT, .fpc, regular, no_dead_strip';
  1198. exit;
  1199. end;
  1200. sec_code:
  1201. begin
  1202. if (aname='fpc_geteipasebx') or
  1203. (aname='fpc_geteipasecx') then
  1204. begin
  1205. result:='.section __TEXT,__textcoal_nt,coalesced,pure_instructions'#10'.weak_definition '+aname+
  1206. #10'.private_extern '+aname;
  1207. exit;
  1208. end;
  1209. end;
  1210. sec_data_nonlazy:
  1211. begin
  1212. result:='.section __DATA, __nl_symbol_ptr,non_lazy_symbol_pointers';
  1213. exit;
  1214. end;
  1215. sec_data_lazy:
  1216. begin
  1217. result:='.section __DATA, __la_symbol_ptr,lazy_symbol_pointers';
  1218. exit;
  1219. end;
  1220. sec_init_func:
  1221. begin
  1222. result:='.section __DATA, __mod_init_func, mod_init_funcs';
  1223. exit;
  1224. end;
  1225. sec_term_func:
  1226. begin
  1227. result:='.section __DATA, __mod_term_func, mod_term_funcs';
  1228. exit;
  1229. end;
  1230. sec_objc_protocol_ext:
  1231. begin
  1232. result:='.section __OBJC, __protocol_ext, regular, no_dead_strip';
  1233. exit;
  1234. end;
  1235. sec_objc_class_ext:
  1236. begin
  1237. result:='.section __OBJC, __class_ext, regular, no_dead_strip';
  1238. exit;
  1239. end;
  1240. sec_objc_property:
  1241. begin
  1242. result:='.section __OBJC, __property, regular, no_dead_strip';
  1243. exit;
  1244. end;
  1245. sec_objc_image_info:
  1246. begin
  1247. result:='.section __OBJC, __image_info, regular, no_dead_strip';
  1248. exit;
  1249. end;
  1250. sec_objc_cstring_object:
  1251. begin
  1252. result:='.section __OBJC, __cstring_object, regular, no_dead_strip';
  1253. exit;
  1254. end;
  1255. sec_objc_sel_fixup:
  1256. begin
  1257. result:='.section __OBJC, __sel_fixup, regular, no_dead_strip';
  1258. exit;
  1259. end;
  1260. sec_objc_message_refs:
  1261. begin
  1262. if (target_info.system in systems_objc_nfabi) then
  1263. begin
  1264. result:='.section __DATA, __objc_selrefs, literal_pointers, no_dead_strip';
  1265. exit;
  1266. end;
  1267. end;
  1268. sec_objc_cls_refs:
  1269. begin
  1270. if (target_info.system in systems_objc_nfabi) then
  1271. begin
  1272. result:='.section __DATA, __objc_clsrefs, regular, no_dead_strip';
  1273. exit;
  1274. end;
  1275. end;
  1276. sec_objc_meth_var_names,
  1277. sec_objc_class_names:
  1278. begin
  1279. if (target_info.system in systems_objc_nfabi) then
  1280. begin
  1281. result:='.cstring';
  1282. exit
  1283. end;
  1284. end;
  1285. sec_objc_inst_meth,
  1286. sec_objc_cls_meth,
  1287. sec_objc_cat_inst_meth,
  1288. sec_objc_cat_cls_meth:
  1289. begin
  1290. if (target_info.system in systems_objc_nfabi) then
  1291. begin
  1292. result:='.section __DATA, __objc_const';
  1293. exit;
  1294. end;
  1295. end;
  1296. sec_objc_meta_class,
  1297. sec_objc_class:
  1298. begin
  1299. if (target_info.system in systems_objc_nfabi) then
  1300. begin
  1301. result:='.section __DATA, __objc_data';
  1302. exit;
  1303. end;
  1304. end;
  1305. sec_objc_sup_refs:
  1306. begin
  1307. result:='.section __DATA, __objc_superrefs, regular, no_dead_strip';
  1308. exit
  1309. end;
  1310. sec_objc_classlist:
  1311. begin
  1312. result:='.section __DATA, __objc_classlist, regular, no_dead_strip';
  1313. exit
  1314. end;
  1315. sec_objc_nlclasslist:
  1316. begin
  1317. result:='.section __DATA, __objc_nlclasslist, regular, no_dead_strip';
  1318. exit
  1319. end;
  1320. sec_objc_catlist:
  1321. begin
  1322. result:='.section __DATA, __objc_catlist, regular, no_dead_strip';
  1323. exit
  1324. end;
  1325. sec_objc_nlcatlist:
  1326. begin
  1327. result:='.section __DATA, __objc_nlcatlist, regular, no_dead_strip';
  1328. exit
  1329. end;
  1330. sec_objc_protolist:
  1331. begin
  1332. result:='.section __DATA, __objc_protolist, coalesced, no_dead_strip';
  1333. exit;
  1334. end;
  1335. end;
  1336. result := inherited sectionname(atype,aname,aorder);
  1337. end;
  1338. procedure TAppleGNUAssembler.WriteWeakSymbolDef(s: tasmsymbol);
  1339. begin
  1340. AsmWriteLn(#9'.weak_reference '+s.name);
  1341. end;
  1342. {****************************************************************************}
  1343. { a.out/GNU Assembler writer }
  1344. {****************************************************************************}
  1345. function TAoutGNUAssembler.sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;
  1346. const
  1347. (* Translation table - replace unsupported section types with basic ones. *)
  1348. SecXTable: array[TAsmSectionType] of TAsmSectionType = (
  1349. sec_none,
  1350. sec_code,
  1351. sec_data,
  1352. sec_data (* sec_rodata *),
  1353. sec_data (* sec_rodata_norel *),
  1354. sec_bss,
  1355. sec_data (* sec_threadvar *),
  1356. { used for wince exception handling }
  1357. sec_code (* sec_pdata *),
  1358. { used for darwin import stubs }
  1359. sec_code (* sec_stub *),
  1360. sec_data,(* sec_data_nonlazy *)
  1361. sec_data,(* sec_data_lazy *)
  1362. sec_data,(* sec_init_func *)
  1363. sec_data,(* sec_term_func *)
  1364. { stabs }
  1365. sec_stab,sec_stabstr,
  1366. { win32 }
  1367. sec_data (* sec_idata2 *),
  1368. sec_data (* sec_idata4 *),
  1369. sec_data (* sec_idata5 *),
  1370. sec_data (* sec_idata6 *),
  1371. sec_data (* sec_idata7 *),
  1372. sec_data (* sec_edata *),
  1373. { C++ exception handling unwinding (uses dwarf) }
  1374. sec_eh_frame,
  1375. { dwarf }
  1376. sec_debug_frame,
  1377. sec_debug_info,
  1378. sec_debug_line,
  1379. sec_debug_abbrev,
  1380. { ELF resources (+ references to stabs debug information sections) }
  1381. sec_code (* sec_fpc *),
  1382. { Table of contents section }
  1383. sec_code (* sec_toc *),
  1384. sec_code (* sec_init *),
  1385. sec_code (* sec_fini *),
  1386. sec_none (* sec_objc_class *),
  1387. sec_none (* sec_objc_meta_class *),
  1388. sec_none (* sec_objc_cat_cls_meth *),
  1389. sec_none (* sec_objc_cat_inst_meth *),
  1390. sec_none (* sec_objc_protocol *),
  1391. sec_none (* sec_objc_string_object *),
  1392. sec_none (* sec_objc_cls_meth *),
  1393. sec_none (* sec_objc_inst_meth *),
  1394. sec_none (* sec_objc_cls_refs *),
  1395. sec_none (* sec_objc_message_refs *),
  1396. sec_none (* sec_objc_symbols *),
  1397. sec_none (* sec_objc_category *),
  1398. sec_none (* sec_objc_class_vars *),
  1399. sec_none (* sec_objc_instance_vars *),
  1400. sec_none (* sec_objc_module_info *),
  1401. sec_none (* sec_objc_class_names *),
  1402. sec_none (* sec_objc_meth_var_types *),
  1403. sec_none (* sec_objc_meth_var_names *),
  1404. sec_none (* sec_objc_selector_strs *),
  1405. sec_none (* sec_objc_protocol_ext *),
  1406. sec_none (* sec_objc_class_ext *),
  1407. sec_none (* sec_objc_property *),
  1408. sec_none (* sec_objc_image_info *),
  1409. sec_none (* sec_objc_cstring_object *),
  1410. sec_none (* sec_objc_sel_fixup *),
  1411. sec_none (* sec_objc_data *),
  1412. sec_none (* sec_objc_const *),
  1413. sec_none (* sec_objc_sup_refs *),
  1414. sec_none (* sec_data_coalesced *),
  1415. sec_none (* sec_objc_classlist *),
  1416. sec_none (* sec_objc_nlclasslist *),
  1417. sec_none (* sec_objc_catlist *),
  1418. sec_none (* sec_objc_nlcatlist *),
  1419. sec_none (* sec_objc_protlist *)
  1420. );
  1421. begin
  1422. Result := inherited SectionName (SecXTable [AType], AName, AOrder);
  1423. end;
  1424. {****************************************************************************}
  1425. { Abstract Instruction Writer }
  1426. {****************************************************************************}
  1427. constructor TCPUInstrWriter.create(_owner: TGNUAssembler);
  1428. begin
  1429. inherited create;
  1430. owner := _owner;
  1431. end;
  1432. end.