aggas.pas 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549
  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. end;
  637. ait_datablock :
  638. begin
  639. if (target_info.system in systems_darwin) then
  640. begin
  641. { On Mac OS X you can't have common symbols in a shared library
  642. since those are in the TEXT section and the text section is
  643. read-only in shared libraries (so it can be shared among different
  644. processes). The alternate code creates some kind of common symbols
  645. in the data segment.
  646. }
  647. if tai_datablock(hp).is_global then
  648. begin
  649. asmwrite('.globl ');
  650. asmwriteln(tai_datablock(hp).sym.name);
  651. asmwriteln('.data');
  652. asmwrite('.zerofill __DATA, __common, ');
  653. asmwrite(tai_datablock(hp).sym.name);
  654. asmwriteln(', '+tostr(tai_datablock(hp).size)+','+tostr(last_align));
  655. if not(LastSecType in [sec_data,sec_none]) then
  656. writesection(LastSecType,'',secorder_default);
  657. end
  658. else
  659. begin
  660. asmwrite(#9'.lcomm'#9);
  661. asmwrite(tai_datablock(hp).sym.name);
  662. asmwrite(','+tostr(tai_datablock(hp).size));
  663. asmwrite(','+tostr(last_align));
  664. asmln;
  665. end;
  666. end
  667. else
  668. begin
  669. {$ifdef USE_COMM_IN_BSS}
  670. if writingpackages then
  671. begin
  672. { The .comm is required for COMMON symbols. These are used
  673. in the shared library loading. All the symbols declared in
  674. the .so file need to resolve to the data allocated in the main
  675. program (PFV) }
  676. if tai_datablock(hp).is_global then
  677. begin
  678. asmwrite(#9'.comm'#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. else
  685. begin
  686. asmwrite(#9'.lcomm'#9);
  687. asmwrite(tai_datablock(hp).sym.name);
  688. asmwrite(','+tostr(tai_datablock(hp).size));
  689. asmwrite(','+tostr(last_align));
  690. asmln;
  691. end
  692. end
  693. else
  694. {$endif USE_COMM_IN_BSS}
  695. begin
  696. if Tai_datablock(hp).is_global then
  697. begin
  698. asmwrite(#9'.globl ');
  699. asmwriteln(Tai_datablock(hp).sym.name);
  700. end;
  701. if (target_info.system <> system_arm_linux) then
  702. sepChar := '@'
  703. else
  704. sepChar := '%';
  705. if (tf_needs_symbol_type in target_info.flags) then
  706. asmwriteln(#9'.type '+Tai_datablock(hp).sym.name+','+sepChar+'object');
  707. if (tf_needs_symbol_size in target_info.flags) and (tai_datablock(hp).size > 0) then
  708. asmwriteln(#9'.size '+Tai_datablock(hp).sym.name+','+tostr(Tai_datablock(hp).size));
  709. asmwrite(Tai_datablock(hp).sym.name);
  710. asmwriteln(':');
  711. asmwriteln(#9'.zero '+tostr(Tai_datablock(hp).size));
  712. end;
  713. end;
  714. end;
  715. ait_const:
  716. begin
  717. constdef:=tai_const(hp).consttype;
  718. case constdef of
  719. {$ifndef cpu64bitaddr}
  720. aitconst_128bit :
  721. begin
  722. internalerror(200404291);
  723. end;
  724. aitconst_64bit :
  725. begin
  726. if assigned(tai_const(hp).sym) then
  727. internalerror(200404292);
  728. AsmWrite(ait_const2str[aitconst_32bit]);
  729. if target_info.endian = endian_little then
  730. begin
  731. AsmWrite(tostr(longint(lo(tai_const(hp).value))));
  732. AsmWrite(',');
  733. AsmWrite(tostr(longint(hi(tai_const(hp).value))));
  734. end
  735. else
  736. begin
  737. AsmWrite(tostr(longint(hi(tai_const(hp).value))));
  738. AsmWrite(',');
  739. AsmWrite(tostr(longint(lo(tai_const(hp).value))));
  740. end;
  741. AsmLn;
  742. end;
  743. {$endif cpu64bitaddr}
  744. aitconst_uleb128bit,
  745. aitconst_sleb128bit,
  746. {$ifdef cpu64bitaddr}
  747. aitconst_128bit,
  748. aitconst_64bit,
  749. {$endif cpu64bitaddr}
  750. aitconst_32bit,
  751. aitconst_16bit,
  752. aitconst_8bit,
  753. aitconst_rva_symbol,
  754. aitconst_secrel32_symbol,
  755. aitconst_darwin_dwarf_delta32,
  756. aitconst_darwin_dwarf_delta64:
  757. begin
  758. if (target_info.system in systems_darwin) and
  759. (constdef in [aitconst_uleb128bit,aitconst_sleb128bit]) then
  760. begin
  761. AsmWrite(ait_const2str[aitconst_8bit]);
  762. case tai_const(hp).consttype of
  763. aitconst_uleb128bit:
  764. WriteDecodedUleb128(qword(tai_const(hp).value));
  765. aitconst_sleb128bit:
  766. WriteDecodedSleb128(int64(tai_const(hp).value));
  767. end
  768. end
  769. else
  770. begin
  771. AsmWrite(ait_const2str[constdef]);
  772. l:=0;
  773. t := '';
  774. repeat
  775. if assigned(tai_const(hp).sym) then
  776. begin
  777. if assigned(tai_const(hp).endsym) then
  778. begin
  779. if (constdef in [aitconst_darwin_dwarf_delta32,aitconst_darwin_dwarf_delta64]) then
  780. begin
  781. s := NextSetLabel;
  782. t := #9'.set '+s+','+tai_const(hp).endsym.name+'-'+tai_const(hp).sym.name;
  783. end
  784. else
  785. s:=tai_const(hp).endsym.name+'-'+tai_const(hp).sym.name
  786. end
  787. else
  788. s:=tai_const(hp).sym.name;
  789. if tai_const(hp).value<>0 then
  790. s:=s+tostr_with_plus(tai_const(hp).value);
  791. end
  792. else
  793. s:=tostr(tai_const(hp).value);
  794. AsmWrite(s);
  795. inc(l,length(s));
  796. { Values with symbols are written on a single line to improve
  797. reading of the .s file (PFV) }
  798. if assigned(tai_const(hp).sym) or
  799. not(LastSecType in [sec_data,sec_rodata,sec_rodata_norel]) or
  800. (l>line_length) or
  801. (hp.next=nil) or
  802. (tai(hp.next).typ<>ait_const) or
  803. (tai_const(hp.next).consttype<>constdef) or
  804. assigned(tai_const(hp.next).sym) then
  805. break;
  806. hp:=tai(hp.next);
  807. AsmWrite(',');
  808. until false;
  809. if (t <> '') then
  810. begin
  811. AsmLn;
  812. AsmWrite(t);
  813. end;
  814. end;
  815. AsmLn;
  816. end;
  817. else
  818. internalerror(200704251);
  819. end;
  820. end;
  821. { the "and defined(FPC_HAS_TYPE_EXTENDED)" isn't optimal but currently the only solution
  822. it prevents proper cross compilation to i386 though
  823. }
  824. {$if defined(cpuextended) and defined(FPC_HAS_TYPE_EXTENDED)}
  825. ait_real_80bit :
  826. begin
  827. if do_line then
  828. AsmWriteLn(target_asm.comment+'value: '+extended2str(tai_real_80bit(hp).value));
  829. { Make sure e is a extended type, bestreal could be
  830. a different type (bestreal) !! (PFV) }
  831. e:=tai_real_80bit(hp).value;
  832. AsmWrite(#9'.byte'#9);
  833. for i:=0 to 9 do
  834. begin
  835. if i<>0 then
  836. AsmWrite(',');
  837. AsmWrite(tostr(t80bitarray(e)[i]));
  838. end;
  839. for i:=11 to tai_real_80bit(hp).savesize do
  840. AsmWrite(',0');
  841. AsmLn;
  842. end;
  843. {$endif cpuextended}
  844. ait_real_64bit :
  845. begin
  846. if do_line then
  847. AsmWriteLn(target_asm.comment+'value: '+double2str(tai_real_64bit(hp).value));
  848. d:=tai_real_64bit(hp).value;
  849. { swap the values to correct endian if required }
  850. if source_info.endian <> target_info.endian then
  851. swap64bitarray(t64bitarray(d));
  852. AsmWrite(#9'.byte'#9);
  853. {$ifdef arm}
  854. if tai_real_64bit(hp).formatoptions=fo_hiloswapped then
  855. begin
  856. for i:=4 to 7 do
  857. begin
  858. if i<>4 then
  859. AsmWrite(',');
  860. AsmWrite(tostr(t64bitarray(d)[i]));
  861. end;
  862. for i:=0 to 3 do
  863. begin
  864. AsmWrite(',');
  865. AsmWrite(tostr(t64bitarray(d)[i]));
  866. end;
  867. end
  868. else
  869. {$endif arm}
  870. begin
  871. for i:=0 to 7 do
  872. begin
  873. if i<>0 then
  874. AsmWrite(',');
  875. AsmWrite(tostr(t64bitarray(d)[i]));
  876. end;
  877. end;
  878. AsmLn;
  879. end;
  880. ait_real_32bit :
  881. begin
  882. if do_line then
  883. AsmWriteLn(target_asm.comment+'value: '+single2str(tai_real_32bit(hp).value));
  884. sin:=tai_real_32bit(hp).value;
  885. { swap the values to correct endian if required }
  886. if source_info.endian <> target_info.endian then
  887. swap32bitarray(t32bitarray(sin));
  888. AsmWrite(#9'.byte'#9);
  889. for i:=0 to 3 do
  890. begin
  891. if i<>0 then
  892. AsmWrite(',');
  893. AsmWrite(tostr(t32bitarray(sin)[i]));
  894. end;
  895. AsmLn;
  896. end;
  897. ait_comp_64bit :
  898. begin
  899. if do_line then
  900. AsmWriteLn(target_asm.comment+'value: '+extended2str(tai_comp_64bit(hp).value));
  901. AsmWrite(#9'.byte'#9);
  902. co:=comp(tai_comp_64bit(hp).value);
  903. { swap the values to correct endian if required }
  904. if source_info.endian <> target_info.endian then
  905. swap64bitarray(t64bitarray(co));
  906. for i:=0 to 7 do
  907. begin
  908. if i<>0 then
  909. AsmWrite(',');
  910. AsmWrite(tostr(t64bitarray(co)[i]));
  911. end;
  912. AsmLn;
  913. end;
  914. ait_string :
  915. begin
  916. pos:=0;
  917. for i:=1 to tai_string(hp).len do
  918. begin
  919. if pos=0 then
  920. begin
  921. AsmWrite(#9'.ascii'#9'"');
  922. pos:=20;
  923. end;
  924. ch:=tai_string(hp).str[i-1];
  925. case ch of
  926. #0, {This can't be done by range, because a bug in FPC}
  927. #1..#31,
  928. #128..#255 : s:='\'+tostr(ord(ch) shr 6)+tostr((ord(ch) and 63) shr 3)+tostr(ord(ch) and 7);
  929. '"' : s:='\"';
  930. '\' : s:='\\';
  931. else
  932. s:=ch;
  933. end;
  934. AsmWrite(s);
  935. inc(pos,length(s));
  936. if (pos>line_length) or (i=tai_string(hp).len) then
  937. begin
  938. AsmWriteLn('"');
  939. pos:=0;
  940. end;
  941. end;
  942. end;
  943. ait_label :
  944. begin
  945. if (tai_label(hp).labsym.is_used) then
  946. begin
  947. if (tai_label(hp).labsym.bind=AB_PRIVATE_EXTERN) then
  948. begin
  949. AsmWrite(#9'.private_extern ');
  950. AsmWriteln(tai_label(hp).labsym.name);
  951. end;
  952. if tai_label(hp).labsym.bind in [AB_GLOBAL,AB_PRIVATE_EXTERN] then
  953. begin
  954. AsmWrite('.globl'#9);
  955. AsmWriteLn(tai_label(hp).labsym.name);
  956. end;
  957. AsmWrite(tai_label(hp).labsym.name);
  958. AsmWriteLn(':');
  959. end;
  960. end;
  961. ait_symbol :
  962. begin
  963. if (tai_symbol(hp).sym.bind=AB_PRIVATE_EXTERN) then
  964. begin
  965. AsmWrite(#9'.private_extern ');
  966. AsmWriteln(tai_symbol(hp).sym.name);
  967. end;
  968. if (target_info.system = system_powerpc64_linux) and
  969. (tai_symbol(hp).sym.typ = AT_FUNCTION) and (cs_profile in current_settings.moduleswitches) then
  970. begin
  971. AsmWriteLn('.globl _mcount');
  972. end;
  973. if tai_symbol(hp).is_global then
  974. begin
  975. AsmWrite('.globl'#9);
  976. AsmWriteLn(tai_symbol(hp).sym.name);
  977. end;
  978. if (target_info.system = system_powerpc64_linux) and
  979. (tai_symbol(hp).sym.typ = AT_FUNCTION) then
  980. begin
  981. AsmWriteLn('.section ".opd", "aw"');
  982. AsmWriteLn('.align 3');
  983. AsmWriteLn(tai_symbol(hp).sym.name + ':');
  984. AsmWriteLn('.quad .' + tai_symbol(hp).sym.name + ', .TOC.@tocbase, 0');
  985. AsmWriteLn('.previous');
  986. AsmWriteLn('.size ' + tai_symbol(hp).sym.name + ', 24');
  987. if (tai_symbol(hp).is_global) then
  988. AsmWriteLn('.globl .' + tai_symbol(hp).sym.name);
  989. AsmWriteLn('.type .' + tai_symbol(hp).sym.name + ', @function');
  990. { the dotted name is the name of the actual function entry }
  991. AsmWrite('.');
  992. end
  993. else
  994. begin
  995. if (target_info.system <> system_arm_linux) then
  996. sepChar := '@'
  997. else
  998. sepChar := '#';
  999. if (tf_needs_symbol_type in target_info.flags) then
  1000. begin
  1001. AsmWrite(#9'.type'#9 + tai_symbol(hp).sym.name);
  1002. if (needsObject(tai_symbol(hp))) then
  1003. AsmWriteLn(',' + sepChar + 'object')
  1004. else
  1005. AsmWriteLn(',' + sepChar + 'function');
  1006. end;
  1007. end;
  1008. if not(tai_symbol(hp).has_value) then
  1009. AsmWriteLn(tai_symbol(hp).sym.name + ':')
  1010. else
  1011. AsmWriteLn(tai_symbol(hp).sym.name + '=' + tostr(tai_symbol(hp).value));
  1012. end;
  1013. {$ifdef arm}
  1014. ait_thumb_func:
  1015. begin
  1016. AsmWriteLn(#9'.thumb_func');
  1017. end;
  1018. {$endif arm}
  1019. ait_symbol_end :
  1020. begin
  1021. if tf_needs_symbol_size in target_info.flags then
  1022. begin
  1023. s:=target_asm.labelprefix+'e'+tostr(symendcount);
  1024. inc(symendcount);
  1025. AsmWriteLn(s+':');
  1026. AsmWrite(#9'.size'#9);
  1027. if (target_info.system = system_powerpc64_linux) and (tai_symbol_end(hp).sym.typ = AT_FUNCTION) then
  1028. AsmWrite('.');
  1029. AsmWrite(tai_symbol_end(hp).sym.name);
  1030. AsmWrite(', '+s+' - ');
  1031. if (target_info.system = system_powerpc64_linux) and (tai_symbol_end(hp).sym.typ = AT_FUNCTION) then
  1032. AsmWrite('.');
  1033. AsmWriteLn(tai_symbol_end(hp).sym.name);
  1034. end;
  1035. end;
  1036. ait_instruction :
  1037. begin
  1038. WriteInstruction(hp);
  1039. end;
  1040. ait_stab :
  1041. begin
  1042. if assigned(tai_stab(hp).str) then
  1043. begin
  1044. AsmWrite(#9'.'+stabtypestr[tai_stab(hp).stabtype]+' ');
  1045. AsmWritePChar(tai_stab(hp).str);
  1046. AsmLn;
  1047. end;
  1048. end;
  1049. ait_force_line,
  1050. ait_function_name : ;
  1051. ait_cutobject :
  1052. begin
  1053. if SmartAsm then
  1054. begin
  1055. { only reset buffer if nothing has changed }
  1056. if AsmSize=AsmStartSize then
  1057. AsmClear
  1058. else
  1059. begin
  1060. AsmClose;
  1061. DoAssemble;
  1062. AsmCreate(tai_cutobject(hp).place);
  1063. end;
  1064. { avoid empty files }
  1065. while assigned(hp.next) and (tai(hp.next).typ in [ait_cutobject,ait_section,ait_comment]) do
  1066. begin
  1067. if tai(hp.next).typ=ait_section then
  1068. LastSecType:=tai_section(hp.next).sectype;
  1069. hp:=tai(hp.next);
  1070. end;
  1071. if LastSecType<>sec_none then
  1072. WriteSection(LastSecType,'',secorder_default);
  1073. AsmStartSize:=AsmSize;
  1074. end;
  1075. end;
  1076. ait_marker :
  1077. if tai_marker(hp).kind=mark_NoLineInfoStart then
  1078. inc(InlineLevel)
  1079. else if tai_marker(hp).kind=mark_NoLineInfoEnd then
  1080. dec(InlineLevel);
  1081. ait_directive :
  1082. begin
  1083. AsmWrite('.'+directivestr[tai_directive(hp).directive]+' ');
  1084. if assigned(tai_directive(hp).name) then
  1085. AsmWrite(tai_directive(hp).name^);
  1086. AsmLn;
  1087. end;
  1088. else
  1089. internalerror(2006012201);
  1090. end;
  1091. hp:=tai(hp.next);
  1092. end;
  1093. end;
  1094. procedure TGNUAssembler.WriteExtraHeader;
  1095. begin
  1096. end;
  1097. procedure TGNUAssembler.WriteInstruction(hp: tai);
  1098. begin
  1099. InstrWriter.WriteInstruction(hp);
  1100. end;
  1101. procedure TGNUAssembler.WriteWeakSymbolDef(s: tasmsymbol);
  1102. begin
  1103. AsmWriteLn(#9'.weak '+s.name);
  1104. end;
  1105. procedure TGNUAssembler.WriteAsmList;
  1106. var
  1107. n : string;
  1108. hal : tasmlisttype;
  1109. i: longint;
  1110. begin
  1111. {$ifdef EXTDEBUG}
  1112. if assigned(current_module.mainsource) then
  1113. Comment(V_Debug,'Start writing gas-styled assembler output for '+current_module.mainsource^);
  1114. {$endif}
  1115. if assigned(current_module.mainsource) then
  1116. n:=ExtractFileName(current_module.mainsource^)
  1117. else
  1118. n:=InputFileName;
  1119. { gcc does not add it either for Darwin (and AIX). Grep for
  1120. TARGET_ASM_FILE_START_FILE_DIRECTIVE in gcc/config/*.h
  1121. }
  1122. if not(target_info.system in systems_darwin) then
  1123. AsmWriteLn(#9'.file "'+FixFileName(n)+'"');
  1124. WriteExtraHeader;
  1125. AsmStartSize:=AsmSize;
  1126. symendcount:=0;
  1127. for hal:=low(TasmlistType) to high(TasmlistType) do
  1128. begin
  1129. AsmWriteLn(target_asm.comment+'Begin asmlist '+AsmlistTypeStr[hal]);
  1130. writetree(current_asmdata.asmlists[hal]);
  1131. AsmWriteLn(target_asm.comment+'End asmlist '+AsmlistTypeStr[hal]);
  1132. end;
  1133. { add weak symbol markers }
  1134. for i:=0 to current_asmdata.asmsymboldict.count-1 do
  1135. if (tasmsymbol(current_asmdata.asmsymboldict[i]).bind=AB_WEAK_EXTERNAL) then
  1136. writeweaksymboldef(tasmsymbol(current_asmdata.asmsymboldict[i]));
  1137. if create_smartlink_sections and
  1138. (target_info.system in systems_darwin) then
  1139. AsmWriteLn(#9'.subsections_via_symbols');
  1140. { "no executable stack" marker for Linux }
  1141. if (target_info.system in systems_linux) and
  1142. not(cs_executable_stack in current_settings.moduleswitches) then
  1143. begin
  1144. AsmWriteLn('.section .note.GNU-stack,"",%progbits');
  1145. end;
  1146. AsmLn;
  1147. {$ifdef EXTDEBUG}
  1148. if assigned(current_module.mainsource) then
  1149. Comment(V_Debug,'Done writing gas-styled assembler output for '+current_module.mainsource^);
  1150. {$endif EXTDEBUG}
  1151. end;
  1152. {****************************************************************************}
  1153. { Apple/GNU Assembler writer }
  1154. {****************************************************************************}
  1155. function TAppleGNUAssembler.sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;
  1156. begin
  1157. if (target_info.system in systems_darwin) then
  1158. case atype of
  1159. sec_bss:
  1160. { all bss (lcomm) symbols are automatically put in the right }
  1161. { place by using the lcomm assembler directive }
  1162. atype := sec_none;
  1163. sec_debug_frame,
  1164. sec_eh_frame:
  1165. begin
  1166. result := '.section __DWARF,__debug_info,regular,debug';
  1167. exit;
  1168. end;
  1169. sec_debug_line:
  1170. begin
  1171. result := '.section __DWARF,__debug_line,regular,debug';
  1172. exit;
  1173. end;
  1174. sec_debug_info:
  1175. begin
  1176. result := '.section __DWARF,__debug_info,regular,debug';
  1177. exit;
  1178. end;
  1179. sec_debug_abbrev:
  1180. begin
  1181. result := '.section __DWARF,__debug_abbrev,regular,debug';
  1182. exit;
  1183. end;
  1184. sec_rodata:
  1185. begin
  1186. result := '.const_data';
  1187. exit;
  1188. end;
  1189. sec_rodata_norel:
  1190. begin
  1191. result := '.const';
  1192. exit;
  1193. end;
  1194. sec_fpc:
  1195. begin
  1196. result := '.section __TEXT, .fpc, regular, no_dead_strip';
  1197. exit;
  1198. end;
  1199. sec_code:
  1200. begin
  1201. if (aname='fpc_geteipasebx') or
  1202. (aname='fpc_geteipasecx') then
  1203. begin
  1204. result:='.section __TEXT,__textcoal_nt,coalesced,pure_instructions'#10'.weak_definition '+aname+
  1205. #10'.private_extern '+aname;
  1206. exit;
  1207. end;
  1208. end;
  1209. sec_data_nonlazy:
  1210. begin
  1211. result:='.section __DATA, __nl_symbol_ptr,non_lazy_symbol_pointers';
  1212. exit;
  1213. end;
  1214. sec_data_lazy:
  1215. begin
  1216. result:='.section __DATA, __la_symbol_ptr,lazy_symbol_pointers';
  1217. exit;
  1218. end;
  1219. sec_init_func:
  1220. begin
  1221. result:='.section __DATA, __mod_init_func, mod_init_funcs';
  1222. exit;
  1223. end;
  1224. sec_term_func:
  1225. begin
  1226. result:='.section __DATA, __mod_term_func, mod_term_funcs';
  1227. exit;
  1228. end;
  1229. sec_objc_protocol_ext:
  1230. begin
  1231. result:='.section __OBJC, __protocol_ext, regular, no_dead_strip';
  1232. exit;
  1233. end;
  1234. sec_objc_class_ext:
  1235. begin
  1236. result:='.section __OBJC, __class_ext, regular, no_dead_strip';
  1237. exit;
  1238. end;
  1239. sec_objc_property:
  1240. begin
  1241. result:='.section __OBJC, __property, regular, no_dead_strip';
  1242. exit;
  1243. end;
  1244. sec_objc_image_info:
  1245. begin
  1246. result:='.section __OBJC, __image_info, regular, no_dead_strip';
  1247. exit;
  1248. end;
  1249. sec_objc_cstring_object:
  1250. begin
  1251. result:='.section __OBJC, __cstring_object, regular, no_dead_strip';
  1252. exit;
  1253. end;
  1254. sec_objc_sel_fixup:
  1255. begin
  1256. result:='.section __OBJC, __sel_fixup, regular, no_dead_strip';
  1257. exit;
  1258. end;
  1259. sec_objc_message_refs:
  1260. begin
  1261. if (target_info.system in systems_objc_nfabi) then
  1262. begin
  1263. result:='.section __DATA, __objc_selrefs, literal_pointers, no_dead_strip';
  1264. exit;
  1265. end;
  1266. end;
  1267. sec_objc_cls_refs:
  1268. begin
  1269. if (target_info.system in systems_objc_nfabi) then
  1270. begin
  1271. result:='.section __DATA, __objc_clsrefs, regular, no_dead_strip';
  1272. exit;
  1273. end;
  1274. end;
  1275. sec_objc_meth_var_names,
  1276. sec_objc_class_names:
  1277. begin
  1278. if (target_info.system in systems_objc_nfabi) then
  1279. begin
  1280. result:='.cstring';
  1281. exit
  1282. end;
  1283. end;
  1284. sec_objc_inst_meth,
  1285. sec_objc_cls_meth,
  1286. sec_objc_cat_inst_meth,
  1287. sec_objc_cat_cls_meth:
  1288. begin
  1289. if (target_info.system in systems_objc_nfabi) then
  1290. begin
  1291. result:='.section __DATA, __objc_const';
  1292. exit;
  1293. end;
  1294. end;
  1295. sec_objc_meta_class,
  1296. sec_objc_class:
  1297. begin
  1298. if (target_info.system in systems_objc_nfabi) then
  1299. begin
  1300. result:='.section __DATA, __objc_data';
  1301. exit;
  1302. end;
  1303. end;
  1304. sec_objc_sup_refs:
  1305. begin
  1306. result:='.section __DATA, __objc_superrefs, regular, no_dead_strip';
  1307. exit
  1308. end;
  1309. sec_objc_classlist:
  1310. begin
  1311. result:='.section __DATA, __objc_classlist, regular, no_dead_strip';
  1312. exit
  1313. end;
  1314. sec_objc_nlclasslist:
  1315. begin
  1316. result:='.section __DATA, __objc_nlclasslist, regular, no_dead_strip';
  1317. exit
  1318. end;
  1319. sec_objc_catlist:
  1320. begin
  1321. result:='.section __DATA, __objc_catlist, regular, no_dead_strip';
  1322. exit
  1323. end;
  1324. sec_objc_nlcatlist:
  1325. begin
  1326. result:='.section __DATA, __objc_nlcatlist, regular, no_dead_strip';
  1327. exit
  1328. end;
  1329. sec_objc_protolist:
  1330. begin
  1331. result:='.section __DATA, __objc_protolist, coalesced, no_dead_strip';
  1332. exit;
  1333. end;
  1334. end;
  1335. result := inherited sectionname(atype,aname,aorder);
  1336. end;
  1337. procedure TAppleGNUAssembler.WriteWeakSymbolDef(s: tasmsymbol);
  1338. begin
  1339. AsmWriteLn(#9'.weak_reference '+s.name);
  1340. end;
  1341. {****************************************************************************}
  1342. { a.out/GNU Assembler writer }
  1343. {****************************************************************************}
  1344. function TAoutGNUAssembler.sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;
  1345. const
  1346. (* Translation table - replace unsupported section types with basic ones. *)
  1347. SecXTable: array[TAsmSectionType] of TAsmSectionType = (
  1348. sec_none,
  1349. sec_code,
  1350. sec_data,
  1351. sec_data (* sec_rodata *),
  1352. sec_data (* sec_rodata_norel *),
  1353. sec_bss,
  1354. sec_data (* sec_threadvar *),
  1355. { used for wince exception handling }
  1356. sec_code (* sec_pdata *),
  1357. { used for darwin import stubs }
  1358. sec_code (* sec_stub *),
  1359. sec_data,(* sec_data_nonlazy *)
  1360. sec_data,(* sec_data_lazy *)
  1361. sec_data,(* sec_init_func *)
  1362. sec_data,(* sec_term_func *)
  1363. { stabs }
  1364. sec_stab,sec_stabstr,
  1365. { win32 }
  1366. sec_data (* sec_idata2 *),
  1367. sec_data (* sec_idata4 *),
  1368. sec_data (* sec_idata5 *),
  1369. sec_data (* sec_idata6 *),
  1370. sec_data (* sec_idata7 *),
  1371. sec_data (* sec_edata *),
  1372. { C++ exception handling unwinding (uses dwarf) }
  1373. sec_eh_frame,
  1374. { dwarf }
  1375. sec_debug_frame,
  1376. sec_debug_info,
  1377. sec_debug_line,
  1378. sec_debug_abbrev,
  1379. { ELF resources (+ references to stabs debug information sections) }
  1380. sec_code (* sec_fpc *),
  1381. { Table of contents section }
  1382. sec_code (* sec_toc *),
  1383. sec_code (* sec_init *),
  1384. sec_code (* sec_fini *),
  1385. sec_none (* sec_objc_class *),
  1386. sec_none (* sec_objc_meta_class *),
  1387. sec_none (* sec_objc_cat_cls_meth *),
  1388. sec_none (* sec_objc_cat_inst_meth *),
  1389. sec_none (* sec_objc_protocol *),
  1390. sec_none (* sec_objc_string_object *),
  1391. sec_none (* sec_objc_cls_meth *),
  1392. sec_none (* sec_objc_inst_meth *),
  1393. sec_none (* sec_objc_cls_refs *),
  1394. sec_none (* sec_objc_message_refs *),
  1395. sec_none (* sec_objc_symbols *),
  1396. sec_none (* sec_objc_category *),
  1397. sec_none (* sec_objc_class_vars *),
  1398. sec_none (* sec_objc_instance_vars *),
  1399. sec_none (* sec_objc_module_info *),
  1400. sec_none (* sec_objc_class_names *),
  1401. sec_none (* sec_objc_meth_var_types *),
  1402. sec_none (* sec_objc_meth_var_names *),
  1403. sec_none (* sec_objc_selector_strs *),
  1404. sec_none (* sec_objc_protocol_ext *),
  1405. sec_none (* sec_objc_class_ext *),
  1406. sec_none (* sec_objc_property *),
  1407. sec_none (* sec_objc_image_info *),
  1408. sec_none (* sec_objc_cstring_object *),
  1409. sec_none (* sec_objc_sel_fixup *),
  1410. sec_none (* sec_objc_data *),
  1411. sec_none (* sec_objc_const *),
  1412. sec_none (* sec_objc_sup_refs *),
  1413. sec_none (* sec_data_coalesced *),
  1414. sec_none (* sec_objc_classlist *),
  1415. sec_none (* sec_objc_nlclasslist *),
  1416. sec_none (* sec_objc_catlist *),
  1417. sec_none (* sec_objc_nlcatlist *),
  1418. sec_none (* sec_objc_protlist *)
  1419. );
  1420. begin
  1421. Result := inherited SectionName (SecXTable [AType], AName, AOrder);
  1422. end;
  1423. {****************************************************************************}
  1424. { Abstract Instruction Writer }
  1425. {****************************************************************************}
  1426. constructor TCPUInstrWriter.create(_owner: TGNUAssembler);
  1427. begin
  1428. inherited create;
  1429. owner := _owner;
  1430. end;
  1431. end.