aggas.pas 55 KB

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