aggas.pas 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977
  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. { $define DEBUG_AGGAS}
  23. interface
  24. uses
  25. globtype,globals,
  26. aasmbase,aasmtai,aasmdata,aasmcfi,
  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(atype:TAsmSectiontype):string;virtual;
  38. function sectionattrs_coff(atype:TAsmSectiontype):string;virtual;
  39. function sectionalignment_aix(atype:TAsmSectiontype;secalign: longint):string;
  40. procedure WriteSection(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder;secalign:longint;
  41. secflags:TSectionFlags=SF_None;secprogbits:TSectionProgbits=SPB_None);virtual;
  42. procedure WriteExtraHeader;virtual;
  43. procedure WriteExtraFooter;virtual;
  44. procedure WriteInstruction(hp: tai);
  45. procedure WriteWeakSymbolRef(s: tasmsymbol); virtual;
  46. procedure WriteHiddenSymbol(sym: TAsmSymbol);
  47. procedure WriteAixStringConst(hp: tai_string);
  48. procedure WriteAixIntConst(hp: tai_const);
  49. procedure WriteUnalignedIntConst(hp: tai_const);
  50. procedure WriteDirectiveName(dir: TAsmDirective); virtual;
  51. public
  52. function MakeCmdLine: TCmdStr; override;
  53. procedure WriteTree(p:TAsmList);override;
  54. procedure WriteAsmList;override;
  55. destructor destroy; override;
  56. private
  57. setcount: longint;
  58. procedure WriteDecodedSleb128(a: int64);
  59. procedure WriteDecodedUleb128(a: qword);
  60. procedure WriteCFI(hp: tai_cfi_base);
  61. function NextSetLabel: string;
  62. protected
  63. InstrWriter: TCPUInstrWriter;
  64. end;
  65. {# This is the base class for writing instructions.
  66. The WriteInstruction() method must be overridden
  67. to write a single instruction to the assembler
  68. file.
  69. }
  70. TCPUInstrWriter = class
  71. constructor create(_owner: TGNUAssembler);
  72. procedure WriteInstruction(hp : tai); virtual; abstract;
  73. protected
  74. owner: TGNUAssembler;
  75. end;
  76. { TAppleGNUAssembler }
  77. TAppleGNUAssembler=class(TGNUAssembler)
  78. protected
  79. function sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;override;
  80. procedure WriteWeakSymbolRef(s: tasmsymbol); override;
  81. procedure WriteDirectiveName(dir: TAsmDirective); override;
  82. end;
  83. TAoutGNUAssembler=class(TGNUAssembler)
  84. function sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;override;
  85. end;
  86. implementation
  87. uses
  88. SysUtils,
  89. cutils,cfileutl,systems,
  90. fmodule,verbose,
  91. {$ifndef DISABLE_WIN64_SEH}
  92. itcpugas,
  93. {$endif DISABLE_WIN64_SEH}
  94. {$ifdef m68k}
  95. cpuinfo,aasmcpu,
  96. {$endif m68k}
  97. cpubase,objcasm;
  98. const
  99. line_length = 70;
  100. var
  101. symendcount : longint;
  102. {****************************************************************************}
  103. { Support routines }
  104. {****************************************************************************}
  105. const
  106. ait_const2str : array[aitconst_128bit..aitconst_64bit_unaligned] of string[20]=(
  107. #9'.fixme128'#9,#9'.quad'#9,#9'.long'#9,#9'.short'#9,#9'.byte'#9,
  108. #9'.sleb128'#9,#9'.uleb128'#9,
  109. #9'.rva'#9,#9'.secrel32'#9,#9'.quad'#9,#9'.long'#9,#9'.short'#9,#9'.short'#9,
  110. #9'.short'#9,#9'.long'#9,#9'.quad'#9
  111. );
  112. ait_solaris_const2str : array[aitconst_128bit..aitconst_64bit_unaligned] of string[20]=(
  113. #9'.fixme128'#9,#9'.8byte'#9,#9'.4byte'#9,#9'.2byte'#9,#9'.byte'#9,
  114. #9'.sleb128'#9,#9'.uleb128'#9,
  115. #9'.rva'#9,#9'.secrel32'#9,#9'.8byte'#9,#9'.4byte'#9,#9'.2byte'#9,#9'.2byte'#9,
  116. #9'.2byte'#9,#9'.4byte'#9,#9'.8byte'#9
  117. );
  118. ait_unaligned_consts = [aitconst_16bit_unaligned..aitconst_64bit_unaligned];
  119. { Sparc type of unaligned pseudo-instructions }
  120. use_ua_sparc_systems = [system_sparc_linux];
  121. ait_ua_sparc_const2str : array[aitconst_16bit_unaligned..aitconst_64bit_unaligned]
  122. of string[20]=(
  123. #9'.uahalf'#9,#9'.uaword'#9,#9'.uaxword'#9
  124. );
  125. { Generic unaligned pseudo-instructions, seems ELF specific }
  126. use_ua_elf_systems = [system_mipsel_linux,system_mipseb_linux,system_mipsel_android,system_mipsel_embedded,system_mipseb_embedded];
  127. ait_ua_elf_const2str : array[aitconst_16bit_unaligned..aitconst_64bit_unaligned]
  128. of string[20]=(
  129. #9'.2byte'#9,#9'.4byte'#9,#9'.8byte'#9
  130. );
  131. {****************************************************************************}
  132. { GNU Assembler writer }
  133. {****************************************************************************}
  134. destructor TGNUAssembler.Destroy;
  135. begin
  136. InstrWriter.free;
  137. inherited destroy;
  138. end;
  139. function TGNUAssembler.MakeCmdLine: TCmdStr;
  140. begin
  141. result := inherited MakeCmdLine;
  142. // MWE: disabled again. It generates dwarf info for the generated .s
  143. // files as well. This conflicts with the info we generate
  144. // if target_dbg.id = dbg_dwarf then
  145. // result := result + ' --gdwarf-2';
  146. end;
  147. function TGNUAssembler.NextSetLabel: string;
  148. begin
  149. inc(setcount);
  150. result := asminfo^.labelprefix+'$set$'+tostr(setcount);
  151. end;
  152. function is_smart_section(atype:TAsmSectiontype):boolean;
  153. begin
  154. { For bss we need to set some flags that are target dependent,
  155. it is easier to disable it for smartlinking. It doesn't take up
  156. filespace }
  157. result:=not(target_info.system in systems_darwin) and
  158. create_smartlink_sections and
  159. (atype<>sec_toc) and
  160. (atype<>sec_user) and
  161. { on embedded systems every byte counts, so smartlink bss too }
  162. ((atype<>sec_bss) or (target_info.system in systems_embedded));
  163. end;
  164. function TGNUAssembler.sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;
  165. const
  166. secnames : array[TAsmSectiontype] of string[length('__DATA, __datacoal_nt,coalesced')] = ('','',
  167. '.text',
  168. '.data',
  169. { why doesn't .rodata work? (FK) }
  170. { sometimes we have to create a data.rel.ro instead of .rodata, e.g. for }
  171. { vtables (and anything else containing relocations), otherwise those are }
  172. { not relocated properly on e.g. linux/ppc64. g++ generates there for a }
  173. { vtable for a class called Window: }
  174. { .section .data.rel.ro._ZTV6Window,"awG",@progbits,_ZTV6Window,comdat }
  175. { TODO: .data.ro not yet working}
  176. {$if defined(arm) or defined(riscv64) or defined(powerpc)}
  177. '.rodata',
  178. {$else arm}
  179. '.data',
  180. {$endif arm}
  181. '.rodata',
  182. '.bss',
  183. '.threadvar',
  184. '.pdata',
  185. '', { stubs }
  186. '__DATA,__nl_symbol_ptr',
  187. '__DATA,__la_symbol_ptr',
  188. '__DATA,__mod_init_func',
  189. '__DATA,__mod_term_func',
  190. '.stab',
  191. '.stabstr',
  192. '.idata$2','.idata$4','.idata$5','.idata$6','.idata$7','.edata',
  193. '.eh_frame',
  194. '.debug_frame','.debug_info','.debug_line','.debug_abbrev','.debug_aranges','.debug_ranges',
  195. '.fpc',
  196. '.toc',
  197. '.init',
  198. '.fini',
  199. '.objc_class',
  200. '.objc_meta_class',
  201. '.objc_cat_cls_meth',
  202. '.objc_cat_inst_meth',
  203. '.objc_protocol',
  204. '.objc_string_object',
  205. '.objc_cls_meth',
  206. '.objc_inst_meth',
  207. '.objc_cls_refs',
  208. '.objc_message_refs',
  209. '.objc_symbols',
  210. '.objc_category',
  211. '.objc_class_vars',
  212. '.objc_instance_vars',
  213. '.objc_module_info',
  214. '.objc_class_names',
  215. '.objc_meth_var_types',
  216. '.objc_meth_var_names',
  217. '.objc_selector_strs',
  218. '.objc_protocol_ext',
  219. '.objc_class_ext',
  220. '.objc_property',
  221. '.objc_image_info',
  222. '.objc_cstring_object',
  223. '.objc_sel_fixup',
  224. '__DATA,__objc_data',
  225. '__DATA,__objc_const',
  226. '.objc_superrefs',
  227. '__DATA, __datacoal_nt,coalesced',
  228. '.objc_classlist',
  229. '.objc_nlclasslist',
  230. '.objc_catlist',
  231. '.obcj_nlcatlist',
  232. '.objc_protolist',
  233. '.stack',
  234. '.heap',
  235. '.gcc_except_table'
  236. );
  237. secnames_pic : array[TAsmSectiontype] of string[length('__DATA, __datacoal_nt,coalesced')] = ('','',
  238. '.text',
  239. '.data.rel',
  240. '.data.rel',
  241. '.data.rel',
  242. '.bss',
  243. '.threadvar',
  244. '.pdata',
  245. '', { stubs }
  246. '__DATA,__nl_symbol_ptr',
  247. '__DATA,__la_symbol_ptr',
  248. '__DATA,__mod_init_func',
  249. '__DATA,__mod_term_func',
  250. '.stab',
  251. '.stabstr',
  252. '.idata$2','.idata$4','.idata$5','.idata$6','.idata$7','.edata',
  253. '.eh_frame',
  254. '.debug_frame','.debug_info','.debug_line','.debug_abbrev','.debug_aranges','.debug_ranges',
  255. '.fpc',
  256. '.toc',
  257. '.init',
  258. '.fini',
  259. '.objc_class',
  260. '.objc_meta_class',
  261. '.objc_cat_cls_meth',
  262. '.objc_cat_inst_meth',
  263. '.objc_protocol',
  264. '.objc_string_object',
  265. '.objc_cls_meth',
  266. '.objc_inst_meth',
  267. '.objc_cls_refs',
  268. '.objc_message_refs',
  269. '.objc_symbols',
  270. '.objc_category',
  271. '.objc_class_vars',
  272. '.objc_instance_vars',
  273. '.objc_module_info',
  274. '.objc_class_names',
  275. '.objc_meth_var_types',
  276. '.objc_meth_var_names',
  277. '.objc_selector_strs',
  278. '.objc_protocol_ext',
  279. '.objc_class_ext',
  280. '.objc_property',
  281. '.objc_image_info',
  282. '.objc_cstring_object',
  283. '.objc_sel_fixup',
  284. '__DATA, __objc_data',
  285. '__DATA, __objc_const',
  286. '.objc_superrefs',
  287. '__DATA, __datacoal_nt,coalesced',
  288. '.objc_classlist',
  289. '.objc_nlclasslist',
  290. '.objc_catlist',
  291. '.obcj_nlcatlist',
  292. '.objc_protolist',
  293. '.stack',
  294. '.heap',
  295. '.gcc_except_table'
  296. );
  297. var
  298. sep : string[3];
  299. secname : string;
  300. begin
  301. if (cs_create_pic in current_settings.moduleswitches) and
  302. not(target_info.system in systems_darwin) then
  303. secname:=secnames_pic[atype]
  304. else
  305. secname:=secnames[atype];
  306. if (atype=sec_fpc) and (Copy(aname,1,3)='res') then
  307. begin
  308. result:=secname+'.'+aname;
  309. exit;
  310. end;
  311. if atype=sec_threadvar then
  312. begin
  313. if (target_info.system in (systems_windows+systems_wince)) then
  314. secname:='.tls'
  315. else if (target_info.system in systems_linux) then
  316. secname:='.tbss';
  317. end;
  318. { go32v2 stub only loads .text and .data sections, and allocates space for .bss.
  319. Thus, data which normally goes into .rodata and .rodata_norel sections must
  320. end up in .data section }
  321. if (atype in [sec_rodata,sec_rodata_norel]) and
  322. (target_info.system in [system_i386_go32v2,system_m68k_palmos]) then
  323. secname:='.data';
  324. { Windows correctly handles reallocations in readonly sections }
  325. if (atype=sec_rodata) and
  326. (target_info.system in systems_all_windows+systems_nativent-[system_i8086_win16]) then
  327. secname:='.rodata';
  328. { Use .rodata and .data.rel.ro for Android with PIC }
  329. if (target_info.system in systems_android) and (cs_create_pic in current_settings.moduleswitches) then
  330. begin
  331. case atype of
  332. sec_rodata:
  333. secname:='.data.rel.ro';
  334. sec_rodata_norel:
  335. secname:='.rodata';
  336. else
  337. ;
  338. end;
  339. end;
  340. { section type user gives the user full controll on the section name }
  341. if atype=sec_user then
  342. secname:=aname;
  343. if is_smart_section(atype) and (aname<>'') then
  344. begin
  345. case aorder of
  346. secorder_begin :
  347. sep:='.b_';
  348. secorder_end :
  349. sep:='.z_';
  350. else
  351. sep:='.n_';
  352. end;
  353. result:=secname+sep+aname
  354. end
  355. else
  356. result:=secname;
  357. end;
  358. function TGNUAssembler.sectionattrs(atype:TAsmSectiontype):string;
  359. begin
  360. result:='';
  361. if (target_info.system in [system_i386_win32,system_x86_64_win64]) then
  362. begin
  363. result:=sectionattrs_coff(atype);
  364. end;
  365. end;
  366. function TGNUAssembler.sectionattrs_coff(atype:TAsmSectiontype):string;
  367. begin
  368. case atype of
  369. sec_code, sec_init, sec_fini, sec_stub:
  370. result:='x';
  371. { TODO: must be individual for each section }
  372. sec_user:
  373. result:='d';
  374. sec_data, sec_data_lazy, sec_data_nonlazy, sec_fpc,
  375. sec_idata2, sec_idata4, sec_idata5, sec_idata6, sec_idata7:
  376. result:='d';
  377. { TODO: these need a fix to become read-only }
  378. sec_rodata, sec_rodata_norel:
  379. result:='d';
  380. sec_bss:
  381. result:='b';
  382. { TODO: Somewhat questionable. FPC does not allow initialized threadvars,
  383. so no sense to mark it as containing data. But Windows allows it to
  384. contain data, and Linux even has .tdata and .tbss }
  385. sec_threadvar:
  386. result:='b';
  387. sec_pdata, sec_edata, sec_eh_frame, sec_toc:
  388. result:='r';
  389. sec_stab,sec_stabstr,
  390. sec_debug_frame,sec_debug_info,sec_debug_line,sec_debug_abbrev,sec_debug_aranges,sec_debug_ranges:
  391. result:='n';
  392. else
  393. result:=''; { defaults to data+load }
  394. end;
  395. end;
  396. function TGNUAssembler.sectionalignment_aix(atype:TAsmSectiontype;secalign: longint): string;
  397. var
  398. l: longint;
  399. begin
  400. if (secalign=0) or
  401. not(atype in [sec_code,sec_bss,sec_rodata_norel,sec_rodata,sec_data]) then
  402. begin
  403. result:='';
  404. exit;
  405. end;
  406. if not ispowerof2(secalign,l) then
  407. internalerror(2012022201);
  408. result:=tostr(l);
  409. end;
  410. procedure TGNUAssembler.WriteSection(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder;secalign:longint;secflags:TSectionFlags=SF_None;secprogbits:TSectionProgbits=SPB_None);
  411. var
  412. s : string;
  413. begin
  414. writer.AsmLn;
  415. case target_info.system of
  416. system_i386_OS2,
  417. system_i386_EMX: ;
  418. system_m68k_atari, { atari tos/mint GNU AS also doesn't seem to like .section (KB) }
  419. system_m68k_amiga: { amiga has old GNU AS (2.14), which blews up from .section (KB) }
  420. begin
  421. { ... but vasm is GAS compatible on amiga/atari, and supports named sections }
  422. if create_smartlink_sections then
  423. writer.AsmWrite('.section ');
  424. end;
  425. system_powerpc_darwin,
  426. system_i386_darwin,
  427. system_i386_iphonesim,
  428. system_powerpc64_darwin,
  429. system_x86_64_darwin,
  430. system_arm_darwin,
  431. system_aarch64_darwin,
  432. system_x86_64_iphonesim,
  433. system_powerpc_aix,
  434. system_powerpc64_aix:
  435. begin
  436. if (atype in [sec_stub]) then
  437. writer.AsmWrite('.section ');
  438. end
  439. else
  440. writer.AsmWrite('.section ');
  441. end;
  442. s:=sectionname(atype,aname,aorder);
  443. writer.AsmWrite(s);
  444. { flags explicitly defined? }
  445. if (secflags<>SF_None) or (secprogbits<>SPB_None) then
  446. begin
  447. case secflags of
  448. SF_A:
  449. writer.AsmWrite(',"a"');
  450. SF_W:
  451. writer.AsmWrite(',"w"');
  452. SF_X:
  453. writer.AsmWrite(',"x"');
  454. SF_None:
  455. writer.AsmWrite(',""');
  456. end;
  457. case secprogbits of
  458. SPB_PROGBITS:
  459. writer.AsmWrite(',%progbits');
  460. SPB_NOBITS:
  461. writer.AsmWrite(',%nobits');
  462. SPB_None:
  463. ;
  464. end;
  465. end
  466. else
  467. case atype of
  468. sec_fpc :
  469. if aname = 'resptrs' then
  470. writer.AsmWrite(', "a", @progbits');
  471. sec_stub :
  472. begin
  473. case target_info.system of
  474. { there are processor-independent shortcuts available }
  475. { for this, namely .symbol_stub and .picsymbol_stub, but }
  476. { they don't work and gcc doesn't use them either... }
  477. system_powerpc_darwin,
  478. system_powerpc64_darwin:
  479. if (cs_create_pic in current_settings.moduleswitches) then
  480. writer.AsmWriteln('__TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32')
  481. else
  482. writer.AsmWriteln('__TEXT,__symbol_stub1,symbol_stubs,pure_instructions,16');
  483. system_i386_darwin,
  484. system_i386_iphonesim:
  485. writer.AsmWriteln('__IMPORT,__jump_table,symbol_stubs,self_modifying_code+pure_instructions,5');
  486. system_arm_darwin:
  487. if (cs_create_pic in current_settings.moduleswitches) then
  488. writer.AsmWriteln('__TEXT,__picsymbolstub4,symbol_stubs,none,16')
  489. else
  490. writer.AsmWriteln('__TEXT,__symbol_stub4,symbol_stubs,none,12')
  491. { darwin/(x86-64/AArch64) uses PC-based GOT addressing, no
  492. explicit symbol stubs }
  493. else
  494. internalerror(2006031101);
  495. end;
  496. end;
  497. else
  498. { GNU AS won't recognize '.text.n_something' section name as belonging
  499. to '.text' and assigns default attributes to it, which is not
  500. always correct. We have to fix it.
  501. TODO: This likely applies to all systems which smartlink without
  502. creating libraries }
  503. begin
  504. if is_smart_section(atype) and (aname<>'') then
  505. begin
  506. s:=sectionattrs(atype);
  507. if (s<>'') then
  508. writer.AsmWrite(',"'+s+'"');
  509. end;
  510. if target_info.system in systems_aix then
  511. begin
  512. s:=sectionalignment_aix(atype,secalign);
  513. if s<>'' then
  514. writer.AsmWrite(','+s);
  515. end;
  516. end;
  517. end;
  518. writer.AsmLn;
  519. LastSecType:=atype;
  520. end;
  521. procedure TGNUAssembler.WriteDecodedUleb128(a: qword);
  522. var
  523. i,len : longint;
  524. buf : array[0..63] of byte;
  525. begin
  526. len:=EncodeUleb128(a,buf,0);
  527. for i:=0 to len-1 do
  528. begin
  529. if (i > 0) then
  530. writer.AsmWrite(',');
  531. writer.AsmWrite(tostr(buf[i]));
  532. end;
  533. end;
  534. procedure TGNUAssembler.WriteCFI(hp: tai_cfi_base);
  535. begin
  536. writer.AsmWrite(cfi2str[hp.cfityp]);
  537. case hp.cfityp of
  538. cfi_startproc,
  539. cfi_endproc:
  540. ;
  541. cfi_undefined,
  542. cfi_restore,
  543. cfi_def_cfa_register:
  544. begin
  545. writer.AsmWrite(' ');
  546. writer.AsmWrite(gas_regname(tai_cfi_op_reg(hp).reg1));
  547. end;
  548. cfi_def_cfa_offset:
  549. begin
  550. writer.AsmWrite(' ');
  551. writer.AsmWrite(tostr(tai_cfi_op_val(hp).val1));
  552. end;
  553. cfi_offset:
  554. begin
  555. writer.AsmWrite(' ');
  556. writer.AsmWrite(gas_regname(tai_cfi_op_reg_val(hp).reg1));
  557. writer.AsmWrite(',');
  558. writer.AsmWrite(tostr(tai_cfi_op_reg_val(hp).val));
  559. end;
  560. else
  561. internalerror(2019030203);
  562. end;
  563. writer.AsmLn;
  564. end;
  565. procedure TGNUAssembler.WriteDecodedSleb128(a: int64);
  566. var
  567. i,len : longint;
  568. buf : array[0..255] of byte;
  569. begin
  570. len:=EncodeSleb128(a,buf,0);
  571. for i:=0 to len-1 do
  572. begin
  573. if (i > 0) then
  574. writer.AsmWrite(',');
  575. writer.AsmWrite(tostr(buf[i]));
  576. end;
  577. end;
  578. procedure TGNUAssembler.WriteTree(p:TAsmList);
  579. function needsObject(hp : tai_symbol) : boolean;
  580. begin
  581. needsObject :=
  582. (
  583. assigned(hp.next) and
  584. (tai(hp.next).typ in [ait_const,ait_datablock,ait_realconst])
  585. ) or
  586. (hp.sym.typ in [AT_DATA,AT_METADATA]);
  587. end;
  588. procedure doalign(alignment: byte; use_op: boolean; fillop: byte; maxbytes: byte; out last_align: longint;lasthp:tai);
  589. var
  590. i: longint;
  591. alignment64 : int64;
  592. {$ifdef m68k}
  593. instr : string;
  594. {$endif}
  595. begin
  596. last_align:=alignment;
  597. if alignment>1 then
  598. begin
  599. if not(target_info.system in (systems_darwin+systems_aix)) then
  600. begin
  601. {$ifdef m68k}
  602. if not use_op and (lastsectype=sec_code) then
  603. begin
  604. if not ispowerof2(alignment,i) then
  605. internalerror(2014022201);
  606. { the Coldfire manual suggests the TBF instruction for
  607. alignments, but somehow QEMU does not interpret that
  608. correctly... }
  609. {if current_settings.cputype in cpu_coldfire then
  610. instr:='0x51fc'
  611. else}
  612. instr:='0x4e71';
  613. writer.AsmWrite(#9'.balignw '+tostr(alignment)+','+instr);
  614. end
  615. else
  616. begin
  617. {$endif m68k}
  618. alignment64:=alignment;
  619. if (maxbytes<>alignment) and ispowerof2(alignment64,i) then
  620. begin
  621. if use_op then
  622. begin
  623. writer.AsmWrite(#9'.p2align '+tostr(i)+','+tostr(fillop)+','+tostr(maxbytes));
  624. writer.AsmLn;
  625. writer.AsmWrite(#9'.p2align '+tostr(i-1)+','+tostr(fillop));
  626. end
  627. else
  628. begin
  629. writer.AsmWrite(#9'.p2align '+tostr(i)+',,'+tostr(maxbytes));
  630. writer.AsmLn;
  631. writer.AsmWrite(#9'.p2align '+tostr(i-1));
  632. end
  633. end
  634. else
  635. begin
  636. writer.AsmWrite(#9'.balign '+tostr(alignment));
  637. if use_op then
  638. writer.AsmWrite(','+tostr(fillop))
  639. {$ifdef x86}
  640. { force NOP as alignment op code }
  641. else if (LastSecType=sec_code) and (asminfo^.id<>as_solaris_as) then
  642. writer.AsmWrite(',0x90');
  643. {$endif x86}
  644. end;
  645. {$ifdef m68k}
  646. end;
  647. {$endif m68k}
  648. end
  649. else
  650. begin
  651. { darwin and aix as only support .align }
  652. if not ispowerof2(alignment,i) then
  653. internalerror(2003010305);
  654. writer.AsmWrite(#9'.align '+tostr(i));
  655. last_align:=i;
  656. end;
  657. writer.AsmLn;
  658. end;
  659. end;
  660. var
  661. ch : char;
  662. lasthp,
  663. hp : tai;
  664. constdef : taiconst_type;
  665. s,t : string;
  666. i,pos,l : longint;
  667. InlineLevel : cardinal;
  668. last_align : longint;
  669. do_line : boolean;
  670. sepChar : char;
  671. replaceforbidden: boolean;
  672. begin
  673. if not assigned(p) then
  674. exit;
  675. replaceforbidden:=asminfo^.dollarsign<>'$';
  676. last_align := 2;
  677. InlineLevel:=0;
  678. { lineinfo is only needed for al_procedures (PFV) }
  679. do_line:=(cs_asm_source in current_settings.globalswitches) or
  680. ((cs_lineinfo in current_settings.moduleswitches)
  681. and (p=current_asmdata.asmlists[al_procedures]));
  682. lasthp:=nil;
  683. hp:=tai(p.first);
  684. while assigned(hp) do
  685. begin
  686. prefetch(pointer(hp.next)^);
  687. if not(hp.typ in SkipLineInfo) then
  688. begin
  689. current_filepos:=tailineinfo(hp).fileinfo;
  690. { no line info for inlined code }
  691. if do_line and (inlinelevel=0) then
  692. WriteSourceLine(hp as tailineinfo);
  693. end;
  694. case hp.typ of
  695. ait_comment :
  696. Begin
  697. writer.AsmWrite(asminfo^.comment);
  698. writer.AsmWritePChar(tai_comment(hp).str);
  699. writer.AsmLn;
  700. End;
  701. ait_regalloc :
  702. begin
  703. if (cs_asm_regalloc in current_settings.globalswitches) then
  704. begin
  705. writer.AsmWrite(#9+asminfo^.comment+'Register ');
  706. repeat
  707. writer.AsmWrite(std_regname(Tai_regalloc(hp).reg));
  708. if (hp.next=nil) or
  709. (tai(hp.next).typ<>ait_regalloc) or
  710. (tai_regalloc(hp.next).ratype<>tai_regalloc(hp).ratype) then
  711. break;
  712. hp:=tai(hp.next);
  713. writer.AsmWrite(',');
  714. until false;
  715. writer.AsmWrite(' ');
  716. writer.AsmWriteLn(regallocstr[tai_regalloc(hp).ratype]);
  717. end;
  718. end;
  719. ait_tempalloc :
  720. begin
  721. if (cs_asm_tempalloc in current_settings.globalswitches) then
  722. WriteTempalloc(tai_tempalloc(hp));
  723. end;
  724. ait_align :
  725. begin
  726. doalign(tai_align_abstract(hp).aligntype,tai_align_abstract(hp).use_op,tai_align_abstract(hp).fillop,tai_align_abstract(hp).maxbytes,last_align,lasthp);
  727. end;
  728. ait_section :
  729. begin
  730. if tai_section(hp).sectype<>sec_none then
  731. if replaceforbidden then
  732. WriteSection(tai_section(hp).sectype,ReplaceForbiddenAsmSymbolChars(tai_section(hp).name^),tai_section(hp).secorder,
  733. tai_section(hp).secalign,tai_section(hp).secflags,tai_section(hp).secprogbits)
  734. else
  735. WriteSection(tai_section(hp).sectype,tai_section(hp).name^,tai_section(hp).secorder,
  736. tai_section(hp).secalign,tai_section(hp).secflags,tai_section(hp).secprogbits)
  737. else
  738. begin
  739. {$ifdef EXTDEBUG}
  740. writer.AsmWrite(asminfo^.comment);
  741. writer.AsmWriteln(' sec_none');
  742. {$endif EXTDEBUG}
  743. end;
  744. end;
  745. ait_datablock :
  746. begin
  747. if (target_info.system in systems_darwin) then
  748. begin
  749. { On Mac OS X you can't have common symbols in a shared library
  750. since those are in the TEXT section and the text section is
  751. read-only in shared libraries (so it can be shared among different
  752. processes). The alternate code creates some kind of common symbols
  753. in the data segment.
  754. }
  755. if tai_datablock(hp).is_global then
  756. begin
  757. if tai_datablock(hp).sym.bind=AB_PRIVATE_EXTERN then
  758. WriteHiddenSymbol(tai_datablock(hp).sym);
  759. writer.AsmWrite('.globl ');
  760. writer.AsmWriteln(tai_datablock(hp).sym.name);
  761. writer.AsmWriteln('.data');
  762. writer.AsmWrite('.zerofill __DATA, __common, ');
  763. writer.AsmWrite(tai_datablock(hp).sym.name);
  764. writer.AsmWriteln(', '+tostr(tai_datablock(hp).size)+','+tostr(last_align));
  765. if not(LastSecType in [sec_data,sec_none]) then
  766. writesection(LastSecType,'',secorder_default,1 shl last_align);
  767. end
  768. else
  769. begin
  770. writer.AsmWrite(#9'.lcomm'#9);
  771. writer.AsmWrite(tai_datablock(hp).sym.name);
  772. writer.AsmWrite(','+tostr(tai_datablock(hp).size));
  773. writer.AsmWrite(','+tostr(last_align));
  774. writer.AsmLn;
  775. end;
  776. end
  777. else if target_info.system in systems_aix then
  778. begin
  779. if tai_datablock(hp).is_global then
  780. begin
  781. writer.AsmWrite(#9'.globl ');
  782. writer.AsmWriteln(ReplaceForbiddenAsmSymbolChars(tai_datablock(hp).sym.name));
  783. writer.AsmWrite(ReplaceForbiddenAsmSymbolChars(tai_datablock(hp).sym.name));
  784. writer.AsmWriteln(':');
  785. writer.AsmWrite(#9'.space ');
  786. writer.AsmWriteln(tostr(tai_datablock(hp).size));
  787. if not(LastSecType in [sec_data,sec_none]) then
  788. writesection(LastSecType,'',secorder_default,1 shl last_align);
  789. end
  790. else
  791. begin
  792. writer.AsmWrite(#9'.lcomm ');
  793. writer.AsmWrite(ReplaceForbiddenAsmSymbolChars(tai_datablock(hp).sym.name));
  794. writer.AsmWrite(',');
  795. writer.AsmWrite(tostr(tai_datablock(hp).size)+',');
  796. writer.AsmWrite('_data.bss_,');
  797. writer.AsmWriteln(tostr(last_align));
  798. end;
  799. end
  800. else
  801. begin
  802. {$ifdef USE_COMM_IN_BSS}
  803. if writingpackages then
  804. begin
  805. { The .comm is required for COMMON symbols. These are used
  806. in the shared library loading. All the symbols declared in
  807. the .so file need to resolve to the data allocated in the main
  808. program (PFV) }
  809. if tai_datablock(hp).is_global then
  810. begin
  811. writer.AsmWrite(#9'.comm'#9);
  812. if replaceforbidden then
  813. writer.AsmWrite(ReplaceForbiddenAsmSymbolChars(tai_datablock(hp).sym.name))
  814. else
  815. writer.AsmWrite(tai_datablock(hp).sym.name);
  816. writer.AsmWrite(','+tostr(tai_datablock(hp).size));
  817. writer.AsmWrite(','+tostr(last_align));
  818. writer.AsmLn;
  819. end
  820. else
  821. begin
  822. writer.AsmWrite(#9'.lcomm'#9);
  823. if replaceforbidden then
  824. writer.AsmWrite(ReplaceForbiddenAsmSymbolChars(tai_datablock(hp).sym.name));
  825. else
  826. writer.AsmWrite(tai_datablock(hp).sym.name);
  827. writer.AsmWrite(','+tostr(tai_datablock(hp).size));
  828. writer.AsmWrite(','+tostr(last_align));
  829. writer.AsmLn;
  830. end
  831. end
  832. else
  833. {$endif USE_COMM_IN_BSS}
  834. begin
  835. if Tai_datablock(hp).is_global then
  836. begin
  837. if (tai_datablock(hp).sym.bind=AB_PRIVATE_EXTERN) then
  838. WriteHiddenSymbol(tai_datablock(hp).sym);
  839. writer.AsmWrite(#9'.globl ');
  840. if replaceforbidden then
  841. writer.AsmWriteln(ReplaceForbiddenAsmSymbolChars(Tai_datablock(hp).sym.name))
  842. else
  843. writer.AsmWriteln(Tai_datablock(hp).sym.name);
  844. end;
  845. if ((target_info.system <> system_arm_linux) and (target_info.system <> system_arm_android)) then
  846. sepChar := '@'
  847. else
  848. sepChar := '%';
  849. if replaceforbidden then
  850. begin
  851. if (tf_needs_symbol_type in target_info.flags) then
  852. writer.AsmWriteln(#9'.type '+ReplaceForbiddenAsmSymbolChars(Tai_datablock(hp).sym.name)+','+sepChar+'object');
  853. if (tf_needs_symbol_size in target_info.flags) and (tai_datablock(hp).size > 0) then
  854. writer.AsmWriteln(#9'.size '+ReplaceForbiddenAsmSymbolChars(Tai_datablock(hp).sym.name)+','+tostr(Tai_datablock(hp).size));
  855. writer.AsmWrite(ReplaceForbiddenAsmSymbolChars(Tai_datablock(hp).sym.name))
  856. end
  857. else
  858. begin
  859. if (tf_needs_symbol_type in target_info.flags) then
  860. writer.AsmWriteln(#9'.type '+Tai_datablock(hp).sym.name+','+sepChar+'object');
  861. if (tf_needs_symbol_size in target_info.flags) and (tai_datablock(hp).size > 0) then
  862. writer.AsmWriteln(#9'.size '+Tai_datablock(hp).sym.name+','+tostr(Tai_datablock(hp).size));
  863. writer.AsmWrite(Tai_datablock(hp).sym.name);
  864. end;
  865. writer.AsmWriteln(':');
  866. writer.AsmWriteln(#9'.zero '+tostr(Tai_datablock(hp).size));
  867. end;
  868. end;
  869. end;
  870. ait_const:
  871. begin
  872. constdef:=tai_const(hp).consttype;
  873. case constdef of
  874. {$ifndef cpu64bitaddr}
  875. aitconst_128bit :
  876. begin
  877. internalerror(200404291);
  878. end;
  879. aitconst_64bit :
  880. begin
  881. if assigned(tai_const(hp).sym) then
  882. internalerror(200404292);
  883. if not(target_info.system in systems_aix) then
  884. begin
  885. writer.AsmWrite(ait_const2str[aitconst_32bit]);
  886. if target_info.endian = endian_little then
  887. begin
  888. writer.AsmWrite(tostr(longint(lo(tai_const(hp).value))));
  889. writer.AsmWrite(',');
  890. writer.AsmWrite(tostr(longint(hi(tai_const(hp).value))));
  891. end
  892. else
  893. begin
  894. writer.AsmWrite(tostr(longint(hi(tai_const(hp).value))));
  895. writer.AsmWrite(',');
  896. writer.AsmWrite(tostr(longint(lo(tai_const(hp).value))));
  897. end;
  898. end
  899. else
  900. WriteAixIntConst(tai_const(hp));
  901. writer.AsmLn;
  902. end;
  903. aitconst_gottpoff:
  904. begin
  905. writer.AsmWrite(#9'.word'#9+tai_const(hp).sym.name+'(gottpoff)+(.-'+tai_const(hp).endsym.name+tostr_with_plus(tai_const(hp).symofs)+')');
  906. writer.Asmln;
  907. end;
  908. {$endif cpu64bitaddr}
  909. aitconst_got:
  910. begin
  911. if tai_const(hp).symofs<>0 then
  912. InternalError(2015091401); // No symbol offset is allowed for GOT.
  913. writer.AsmWrite(#9'.word'#9+tai_const(hp).sym.name+'(GOT)');
  914. writer.AsmLn;
  915. end;
  916. aitconst_gotoff_symbol:
  917. begin
  918. if (tai_const(hp).sym=nil) then
  919. InternalError(2014022601);
  920. case target_info.cpu of
  921. cpu_mipseb,cpu_mipsel:
  922. begin
  923. writer.AsmWrite(#9'.gpword'#9);
  924. writer.AsmWrite(tai_const(hp).sym.name);
  925. end;
  926. cpu_i386:
  927. begin
  928. writer.AsmWrite(ait_const2str[aitconst_32bit]);
  929. writer.AsmWrite(tai_const(hp).sym.name+'-_GLOBAL_OFFSET_TABLE_');
  930. end;
  931. else
  932. InternalError(2014022602);
  933. end;
  934. if (tai_const(hp).value<>0) then
  935. writer.AsmWrite(tostr_with_plus(tai_const(hp).value));
  936. writer.AsmLn;
  937. end;
  938. aitconst_uleb128bit,
  939. aitconst_sleb128bit,
  940. {$ifdef cpu64bitaddr}
  941. aitconst_128bit,
  942. aitconst_64bit,
  943. {$endif cpu64bitaddr}
  944. aitconst_32bit,
  945. aitconst_16bit,
  946. aitconst_8bit,
  947. aitconst_rva_symbol,
  948. aitconst_secrel32_symbol,
  949. aitconst_darwin_dwarf_delta32,
  950. aitconst_darwin_dwarf_delta64,
  951. aitconst_half16bit,
  952. aitconst_gs,
  953. aitconst_16bit_unaligned,
  954. aitconst_32bit_unaligned,
  955. aitconst_64bit_unaligned:
  956. begin
  957. { the AIX assembler (and for compatibility, the GNU
  958. assembler when targeting AIX) automatically aligns
  959. .short/.long/.llong to a multiple of 2/4/8 bytes. We
  960. don't want that, since this may be data inside a packed
  961. record -> use .vbyte instead (byte stream of fixed
  962. length) }
  963. if (target_info.system in systems_aix) and
  964. (constdef in [aitconst_128bit,aitconst_64bit,aitconst_32bit,aitconst_16bit]) and
  965. not assigned(tai_const(hp).sym) then
  966. begin
  967. WriteAixIntConst(tai_const(hp));
  968. end
  969. else if (target_info.system in systems_darwin) and
  970. (constdef in [aitconst_uleb128bit,aitconst_sleb128bit]) then
  971. begin
  972. writer.AsmWrite(ait_const2str[aitconst_8bit]);
  973. case tai_const(hp).consttype of
  974. aitconst_uleb128bit:
  975. WriteDecodedUleb128(qword(tai_const(hp).value));
  976. aitconst_sleb128bit:
  977. WriteDecodedSleb128(int64(tai_const(hp).value));
  978. else
  979. ;
  980. end
  981. end
  982. else
  983. begin
  984. if (constdef in ait_unaligned_consts) and
  985. (target_info.system in use_ua_sparc_systems) then
  986. writer.AsmWrite(ait_ua_sparc_const2str[constdef])
  987. else if (constdef in ait_unaligned_consts) and
  988. (target_info.system in use_ua_elf_systems) then
  989. writer.AsmWrite(ait_ua_elf_const2str[constdef])
  990. { we can also have unaligned pointers in packed record
  991. constants, which don't get translated into
  992. unaligned tai -> always use vbyte }
  993. else if target_info.system in systems_aix then
  994. writer.AsmWrite(#9'.vbyte'#9+tostr(tai_const(hp).size)+',')
  995. else if (asminfo^.id=as_solaris_as) then
  996. writer.AsmWrite(ait_solaris_const2str[constdef])
  997. else
  998. writer.AsmWrite(ait_const2str[constdef]);
  999. l:=0;
  1000. t := '';
  1001. repeat
  1002. if assigned(tai_const(hp).sym) then
  1003. begin
  1004. if assigned(tai_const(hp).endsym) then
  1005. begin
  1006. if (constdef in [aitconst_darwin_dwarf_delta32,aitconst_darwin_dwarf_delta64]) then
  1007. begin
  1008. s := NextSetLabel;
  1009. t := #9'.set '+s+','+tai_const(hp).endsym.name+'-'+tai_const(hp).sym.name;
  1010. end
  1011. else
  1012. s:=tai_const(hp).endsym.name+'-'+tai_const(hp).sym.name
  1013. end
  1014. else
  1015. s:=tai_const(hp).sym.name;
  1016. if replaceforbidden then
  1017. s:=ReplaceForbiddenAsmSymbolChars(s);
  1018. if tai_const(hp).value<>0 then
  1019. s:=s+tostr_with_plus(tai_const(hp).value);
  1020. end
  1021. else
  1022. {$ifdef cpu64bitaddr}
  1023. s:=tostr(tai_const(hp).value);
  1024. {$else cpu64bitaddr}
  1025. { 64 bit constants are already handled above in this case }
  1026. s:=tostr(longint(tai_const(hp).value));
  1027. {$endif cpu64bitaddr}
  1028. if constdef = aitconst_half16bit then
  1029. s:='('+s+')/2';
  1030. if constdef = aitconst_gs then
  1031. s:='gs('+s+')';
  1032. writer.AsmWrite(s);
  1033. inc(l,length(s));
  1034. { Values with symbols are written on a single line to improve
  1035. reading of the .s file (PFV) }
  1036. if assigned(tai_const(hp).sym) or
  1037. not(LastSecType in [sec_data,sec_rodata,sec_rodata_norel]) or
  1038. (l>line_length) or
  1039. (hp.next=nil) or
  1040. (tai(hp.next).typ<>ait_const) or
  1041. (tai_const(hp.next).consttype<>constdef) or
  1042. assigned(tai_const(hp.next).sym) then
  1043. break;
  1044. hp:=tai(hp.next);
  1045. writer.AsmWrite(',');
  1046. until false;
  1047. if (t <> '') then
  1048. begin
  1049. writer.AsmLn;
  1050. writer.AsmWrite(t);
  1051. end;
  1052. end;
  1053. writer.AsmLn;
  1054. end;
  1055. else
  1056. internalerror(200704251);
  1057. end;
  1058. end;
  1059. ait_realconst :
  1060. begin
  1061. WriteRealConstAsBytes(tai_realconst(hp),#9'.byte'#9,do_line);
  1062. end;
  1063. ait_string :
  1064. begin
  1065. pos:=0;
  1066. if not(target_info.system in systems_aix) then
  1067. begin
  1068. for i:=1 to tai_string(hp).len do
  1069. begin
  1070. if pos=0 then
  1071. begin
  1072. writer.AsmWrite(#9'.ascii'#9'"');
  1073. pos:=20;
  1074. end;
  1075. ch:=tai_string(hp).str[i-1];
  1076. case ch of
  1077. #0, {This can't be done by range, because a bug in FPC}
  1078. #1..#31,
  1079. #128..#255 : s:='\'+tostr(ord(ch) shr 6)+tostr((ord(ch) and 63) shr 3)+tostr(ord(ch) and 7);
  1080. '"' : s:='\"';
  1081. '\' : s:='\\';
  1082. else
  1083. s:=ch;
  1084. end;
  1085. writer.AsmWrite(s);
  1086. inc(pos,length(s));
  1087. if (pos>line_length) or (i=tai_string(hp).len) then
  1088. begin
  1089. writer.AsmWriteLn('"');
  1090. pos:=0;
  1091. end;
  1092. end;
  1093. end
  1094. else
  1095. WriteAixStringConst(tai_string(hp));
  1096. end;
  1097. ait_label :
  1098. begin
  1099. if (tai_label(hp).labsym.is_used) then
  1100. begin
  1101. if (tai_label(hp).labsym.bind=AB_PRIVATE_EXTERN) then
  1102. begin
  1103. writer.AsmWrite(#9'.private_extern ');
  1104. writer.AsmWriteln(tai_label(hp).labsym.name);
  1105. end;
  1106. if tai_label(hp).labsym.bind in [AB_GLOBAL,AB_PRIVATE_EXTERN] then
  1107. begin
  1108. {$ifdef arm}
  1109. { do no change arm mode accidently, .globl seems to reset the mode }
  1110. if GenerateThumbCode or GenerateThumb2Code then
  1111. writer.AsmWriteln(#9'.thumb_func'#9);
  1112. {$endif arm}
  1113. writer.AsmWrite('.globl'#9);
  1114. if replaceforbidden then
  1115. writer.AsmWriteLn(ReplaceForbiddenAsmSymbolChars(tai_label(hp).labsym.name))
  1116. else
  1117. writer.AsmWriteLn(tai_label(hp).labsym.name);
  1118. end;
  1119. if replaceforbidden then
  1120. writer.AsmWrite(ReplaceForbiddenAsmSymbolChars(tai_label(hp).labsym.name))
  1121. else
  1122. writer.AsmWrite(tai_label(hp).labsym.name);
  1123. writer.AsmWriteLn(':');
  1124. end;
  1125. end;
  1126. ait_symbol :
  1127. begin
  1128. if (target_info.system=system_powerpc64_linux) and
  1129. (tai_symbol(hp).sym.typ=AT_FUNCTION) and
  1130. (cs_profile in current_settings.moduleswitches) then
  1131. writer.AsmWriteLn('.globl _mcount');
  1132. if tai_symbol(hp).is_global then
  1133. begin
  1134. writer.AsmWrite('.globl'#9);
  1135. if replaceforbidden then
  1136. writer.AsmWriteln(ReplaceForbiddenAsmSymbolChars(tai_symbol(hp).sym.name))
  1137. else
  1138. writer.AsmWriteln(tai_symbol(hp).sym.name);
  1139. if (tai_symbol(hp).sym.bind=AB_PRIVATE_EXTERN) then
  1140. WriteHiddenSymbol(tai_symbol(hp).sym);
  1141. end;
  1142. if (target_info.system=system_powerpc64_linux) and
  1143. use_dotted_functions and
  1144. (tai_symbol(hp).sym.typ=AT_FUNCTION) then
  1145. begin
  1146. writer.AsmWriteLn('.section ".opd", "aw"');
  1147. writer.AsmWriteLn('.align 3');
  1148. writer.AsmWriteLn(tai_symbol(hp).sym.name + ':');
  1149. writer.AsmWriteLn('.quad .' + tai_symbol(hp).sym.name + ', .TOC.@tocbase, 0');
  1150. writer.AsmWriteLn('.previous');
  1151. writer.AsmWriteLn('.size ' + tai_symbol(hp).sym.name + ', 24');
  1152. if (tai_symbol(hp).is_global) then
  1153. writer.AsmWriteLn('.globl .' + tai_symbol(hp).sym.name);
  1154. writer.AsmWriteLn('.type .' + tai_symbol(hp).sym.name + ', @function');
  1155. { the dotted name is the name of the actual function entry }
  1156. writer.AsmWrite('.');
  1157. end
  1158. else if (target_info.system in systems_aix) and
  1159. (tai_symbol(hp).sym.typ = AT_FUNCTION) then
  1160. begin
  1161. if target_info.system=system_powerpc_aix then
  1162. begin
  1163. s:=#9'.long .';
  1164. ch:='2';
  1165. end
  1166. else
  1167. begin
  1168. s:=#9'.llong .';
  1169. ch:='3';
  1170. end;
  1171. writer.AsmWriteLn(#9'.csect '+ReplaceForbiddenAsmSymbolChars(tai_symbol(hp).sym.name)+'[DS],'+ch);
  1172. writer.AsmWriteLn(ReplaceForbiddenAsmSymbolChars(tai_symbol(hp).sym.name)+':');
  1173. writer.AsmWriteln(s+ReplaceForbiddenAsmSymbolChars(tai_symbol(hp).sym.name)+', TOC[tc0], 0');
  1174. writer.AsmWriteln(#9'.csect .text[PR]');
  1175. if (tai_symbol(hp).is_global) then
  1176. writer.AsmWriteLn('.globl .'+ReplaceForbiddenAsmSymbolChars(tai_symbol(hp).sym.name))
  1177. else
  1178. writer.AsmWriteLn('.lglobl .'+ReplaceForbiddenAsmSymbolChars(tai_symbol(hp).sym.name));
  1179. { the dotted name is the name of the actual function entry }
  1180. writer.AsmWrite('.');
  1181. end
  1182. else
  1183. begin
  1184. if ((target_info.system <> system_arm_linux) and (target_info.system <> system_arm_android)) then
  1185. sepChar := '@'
  1186. else
  1187. sepChar := '#';
  1188. if (tf_needs_symbol_type in target_info.flags) then
  1189. begin
  1190. writer.AsmWrite(#9'.type'#9 + tai_symbol(hp).sym.name);
  1191. if (needsObject(tai_symbol(hp))) then
  1192. writer.AsmWriteLn(',' + sepChar + 'object')
  1193. else
  1194. writer.AsmWriteLn(',' + sepChar + 'function');
  1195. end;
  1196. end;
  1197. if replaceforbidden then
  1198. if not(tai_symbol(hp).has_value) then
  1199. writer.AsmWriteLn(ReplaceForbiddenAsmSymbolChars(tai_symbol(hp).sym.name + ':'))
  1200. else
  1201. writer.AsmWriteLn(ReplaceForbiddenAsmSymbolChars(tai_symbol(hp).sym.name + '=' + tostr(tai_symbol(hp).value)))
  1202. else if not(tai_symbol(hp).has_value) then
  1203. writer.AsmWriteLn(tai_symbol(hp).sym.name + ':')
  1204. else
  1205. writer.AsmWriteLn(tai_symbol(hp).sym.name + '=' + tostr(tai_symbol(hp).value));
  1206. end;
  1207. ait_symbolpair:
  1208. begin
  1209. writer.AsmWrite(#9);
  1210. writer.AsmWrite(symbolpairkindstr[tai_symbolpair(hp).kind]);
  1211. writer.AsmWrite(' ');
  1212. if tai_symbolpair(hp).kind<>spk_localentry then
  1213. s:=', '
  1214. else
  1215. { the .localentry directive has to specify the size from the
  1216. start till here of the non-local entry code as second argument }
  1217. s:=', .-';
  1218. if replaceforbidden then
  1219. begin
  1220. { avoid string truncation }
  1221. writer.AsmWrite(ReplaceForbiddenAsmSymbolChars(tai_symbolpair(hp).sym^));
  1222. writer.AsmWrite(s);
  1223. writer.AsmWriteLn(ReplaceForbiddenAsmSymbolChars(tai_symbolpair(hp).value^));
  1224. if tai_symbolpair(hp).kind=spk_set_global then
  1225. begin
  1226. writer.AsmWrite(#9'.globl ');
  1227. writer.AsmWriteLn(ReplaceForbiddenAsmSymbolChars(tai_symbolpair(hp).sym^));
  1228. end;
  1229. end
  1230. else
  1231. begin
  1232. { avoid string truncation }
  1233. writer.AsmWrite(tai_symbolpair(hp).sym^);
  1234. writer.AsmWrite(s);
  1235. writer.AsmWriteLn(tai_symbolpair(hp).value^);
  1236. if tai_symbolpair(hp).kind=spk_set_global then
  1237. begin
  1238. writer.AsmWrite(#9'.globl ');
  1239. writer.AsmWriteLn(tai_symbolpair(hp).sym^);
  1240. end;
  1241. end;
  1242. end;
  1243. ait_symbol_end :
  1244. begin
  1245. if tf_needs_symbol_size in target_info.flags then
  1246. begin
  1247. s:=asminfo^.labelprefix+'e'+tostr(symendcount);
  1248. inc(symendcount);
  1249. writer.AsmWriteLn(s+':');
  1250. writer.AsmWrite(#9'.size'#9);
  1251. if (target_info.system=system_powerpc64_linux) and
  1252. use_dotted_functions and
  1253. (tai_symbol_end(hp).sym.typ=AT_FUNCTION) then
  1254. writer.AsmWrite('.');
  1255. if replaceforbidden then
  1256. writer.AsmWrite(ReplaceForbiddenAsmSymbolChars(tai_symbol_end(hp).sym.name))
  1257. else
  1258. writer.AsmWrite(tai_symbol_end(hp).sym.name);
  1259. writer.AsmWrite(', '+s+' - ');
  1260. if (target_info.system=system_powerpc64_linux) and
  1261. use_dotted_functions and
  1262. (tai_symbol_end(hp).sym.typ=AT_FUNCTION) then
  1263. writer.AsmWrite('.');
  1264. if replaceforbidden then
  1265. writer.AsmWriteLn(ReplaceForbiddenAsmSymbolChars(tai_symbol_end(hp).sym.name))
  1266. else
  1267. writer.AsmWriteLn(tai_symbol_end(hp).sym.name);
  1268. end;
  1269. end;
  1270. ait_instruction :
  1271. begin
  1272. WriteInstruction(hp);
  1273. end;
  1274. ait_stab :
  1275. begin
  1276. if assigned(tai_stab(hp).str) then
  1277. begin
  1278. writer.AsmWrite(#9'.'+stabtypestr[tai_stab(hp).stabtype]+' ');
  1279. writer.AsmWritePChar(tai_stab(hp).str);
  1280. writer.AsmLn;
  1281. end;
  1282. end;
  1283. ait_force_line,
  1284. ait_function_name :
  1285. begin
  1286. {$ifdef DEBUG_AGGAS}
  1287. WriteStr(s,hp.typ);
  1288. writer.AsmWriteLn('# '+s);
  1289. {$endif DEBUG_AGGAS}
  1290. end;
  1291. ait_cutobject :
  1292. begin
  1293. {$ifdef DEBUG_AGGAS}
  1294. writer.AsmWriteLn('# ait_cutobject');
  1295. {$endif DEBUG_AGGAS}
  1296. if SmartAsm then
  1297. begin
  1298. { only reset buffer if nothing has changed }
  1299. if not(writer.ClearIfEmpty) then
  1300. begin
  1301. writer.AsmClose;
  1302. DoAssemble;
  1303. writer.AsmCreate(tai_cutobject(hp).place);
  1304. end;
  1305. { avoid empty files }
  1306. while assigned(hp.next) and (tai(hp.next).typ in [ait_cutobject,ait_section,ait_comment]) do
  1307. begin
  1308. if tai(hp.next).typ=ait_section then
  1309. LastSecType:=tai_section(hp.next).sectype;
  1310. hp:=tai(hp.next);
  1311. end;
  1312. if LastSecType<>sec_none then
  1313. WriteSection(LastSecType,'',secorder_default,last_align);
  1314. writer.MarkEmpty;
  1315. end;
  1316. end;
  1317. ait_marker :
  1318. begin
  1319. {$ifdef DEBUG_AGGAS}
  1320. WriteStr(s,tai_marker(hp).Kind);
  1321. writer.AsmWriteLn('# ait_marker, kind: '+s);
  1322. {$endif DEBUG_AGGAS}
  1323. if tai_marker(hp).kind=mark_NoLineInfoStart then
  1324. inc(InlineLevel)
  1325. else if tai_marker(hp).kind=mark_NoLineInfoEnd then
  1326. dec(InlineLevel);
  1327. end;
  1328. ait_directive :
  1329. begin
  1330. WriteDirectiveName(tai_directive(hp).directive);
  1331. if tai_directive(hp).name <>'' then
  1332. begin
  1333. if replaceforbidden then
  1334. writer.AsmWrite(ReplaceForbiddenAsmSymbolChars(tai_directive(hp).name))
  1335. else
  1336. writer.AsmWrite(tai_directive(hp).name);
  1337. end;
  1338. writer.AsmLn;
  1339. end;
  1340. ait_seh_directive :
  1341. begin
  1342. {$ifndef DISABLE_WIN64_SEH}
  1343. writer.AsmWrite(sehdirectivestr[tai_seh_directive(hp).kind]);
  1344. case tai_seh_directive(hp).datatype of
  1345. sd_none:;
  1346. sd_string:
  1347. begin
  1348. writer.AsmWrite(' '+tai_seh_directive(hp).data.name^);
  1349. if (tai_seh_directive(hp).data.flags and 1)<>0 then
  1350. writer.AsmWrite(',@except');
  1351. if (tai_seh_directive(hp).data.flags and 2)<>0 then
  1352. writer.AsmWrite(',@unwind');
  1353. end;
  1354. sd_reg:
  1355. writer.AsmWrite(' '+gas_regname(tai_seh_directive(hp).data.reg));
  1356. sd_offset:
  1357. writer.AsmWrite(' '+tostr(tai_seh_directive(hp).data.offset));
  1358. sd_regoffset:
  1359. writer.AsmWrite(' '+gas_regname(tai_seh_directive(hp).data.reg)+', '+
  1360. tostr(tai_seh_directive(hp).data.offset));
  1361. end;
  1362. writer.AsmLn;
  1363. {$endif DISABLE_WIN64_SEH}
  1364. end;
  1365. ait_varloc:
  1366. begin
  1367. if tai_varloc(hp).newlocationhi<>NR_NO then
  1368. writer.AsmWrite(strpnew('Var '+tai_varloc(hp).varsym.realname+' located in register '+
  1369. std_regname(tai_varloc(hp).newlocationhi)+':'+std_regname(tai_varloc(hp).newlocation)))
  1370. else
  1371. writer.AsmWrite(strpnew('Var '+tai_varloc(hp).varsym.realname+' located in register '+
  1372. std_regname(tai_varloc(hp).newlocation)));
  1373. writer.AsmLn;
  1374. end;
  1375. ait_cfi:
  1376. begin
  1377. WriteCFI(tai_cfi_base(hp));
  1378. end;
  1379. else
  1380. internalerror(2006012201);
  1381. end;
  1382. lasthp:=hp;
  1383. hp:=tai(hp.next);
  1384. end;
  1385. end;
  1386. procedure TGNUAssembler.WriteExtraHeader;
  1387. begin
  1388. end;
  1389. procedure TGNUAssembler.WriteExtraFooter;
  1390. begin
  1391. end;
  1392. procedure TGNUAssembler.WriteInstruction(hp: tai);
  1393. begin
  1394. InstrWriter.WriteInstruction(hp);
  1395. end;
  1396. procedure TGNUAssembler.WriteWeakSymbolRef(s: tasmsymbol);
  1397. begin
  1398. writer.AsmWrite(#9'.weak ');
  1399. if asminfo^.dollarsign='$' then
  1400. writer.AsmWriteLn(s.name)
  1401. else
  1402. writer.AsmWriteLn(ReplaceForbiddenAsmSymbolChars(s.name))
  1403. end;
  1404. procedure TGNUAssembler.WriteHiddenSymbol(sym: TAsmSymbol);
  1405. begin
  1406. { on Windows/(PE)COFF, global symbols are hidden by default: global
  1407. symbols that are not explicitly exported from an executable/library,
  1408. become hidden }
  1409. if target_info.system in systems_windows then
  1410. exit;
  1411. if target_info.system in systems_darwin then
  1412. writer.AsmWrite(#9'.private_extern ')
  1413. else
  1414. writer.AsmWrite(#9'.hidden ');
  1415. if asminfo^.dollarsign='$' then
  1416. writer.AsmWriteLn(sym.name)
  1417. else
  1418. writer.AsmWriteLn(ReplaceForbiddenAsmSymbolChars(sym.name))
  1419. end;
  1420. procedure TGNUAssembler.WriteAixStringConst(hp: tai_string);
  1421. type
  1422. tterminationkind = (term_none,term_string,term_nostring);
  1423. var
  1424. i: longint;
  1425. pos: longint;
  1426. s: string;
  1427. ch: char;
  1428. instring: boolean;
  1429. procedure newstatement(terminationkind: tterminationkind);
  1430. begin
  1431. case terminationkind of
  1432. term_none: ;
  1433. term_string:
  1434. writer.AsmWriteLn('"');
  1435. term_nostring:
  1436. writer.AsmLn;
  1437. end;
  1438. writer.AsmWrite(#9'.byte'#9);
  1439. pos:=20;
  1440. instring:=false;
  1441. end;
  1442. begin
  1443. pos:=0;
  1444. instring:=false;
  1445. for i:=1 to hp.len do
  1446. begin
  1447. if pos=0 then
  1448. newstatement(term_none);
  1449. ch:=hp.str[i-1];
  1450. case ch of
  1451. #0..#31,
  1452. #127..#255 :
  1453. begin
  1454. if instring then
  1455. newstatement(term_string);
  1456. if pos=20 then
  1457. s:=tostr(ord(ch))
  1458. else
  1459. s:=', '+tostr(ord(ch))
  1460. end;
  1461. '"' :
  1462. if instring then
  1463. s:='""'
  1464. else
  1465. begin
  1466. if pos<>20 then
  1467. newstatement(term_nostring);
  1468. s:='"""';
  1469. instring:=true;
  1470. end;
  1471. else
  1472. if not instring then
  1473. begin
  1474. if (pos<>20) then
  1475. newstatement(term_nostring);
  1476. s:='"'+ch;
  1477. instring:=true;
  1478. end
  1479. else
  1480. s:=ch;
  1481. end;
  1482. writer.AsmWrite(s);
  1483. inc(pos,length(s));
  1484. if (pos>line_length) or (i=tai_string(hp).len) then
  1485. begin
  1486. if instring then
  1487. writer.AsmWriteLn('"')
  1488. else
  1489. writer.AsmLn;
  1490. pos:=0;
  1491. end;
  1492. end;
  1493. end;
  1494. procedure TGNUAssembler.WriteAixIntConst(hp: tai_const);
  1495. var
  1496. pos, size: longint;
  1497. begin
  1498. { only big endian AIX supported for now }
  1499. if target_info.endian<>endian_big then
  1500. internalerror(2012010401);
  1501. { limitation: can only write 4 bytes at a time }
  1502. pos:=0;
  1503. size:=tai_const(hp).size;
  1504. while pos<(size-4) do
  1505. begin
  1506. writer.AsmWrite(#9'.vbyte'#9'4, ');
  1507. writer.AsmWriteln(tostr(longint(tai_const(hp).value shr ((size-pos-4)*8))));
  1508. inc(pos,4);
  1509. end;
  1510. writer.AsmWrite(#9'.vbyte'#9);
  1511. writer.AsmWrite(tostr(size-pos));
  1512. writer.AsmWrite(', ');
  1513. case size-pos of
  1514. 1: writer.AsmWrite(tostr(byte(tai_const(hp).value)));
  1515. 2: writer.AsmWrite(tostr(word(tai_const(hp).value)));
  1516. 4: writer.AsmWrite(tostr(longint(tai_const(hp).value)));
  1517. else
  1518. internalerror(2012010402);
  1519. end;
  1520. end;
  1521. procedure TGNUAssembler.WriteUnalignedIntConst(hp: tai_const);
  1522. var
  1523. pos, size: longint;
  1524. begin
  1525. size:=tai_const(hp).size;
  1526. writer.AsmWrite(#9'.byte'#9);
  1527. if target_info.endian=endian_big then
  1528. begin
  1529. pos:=size-1;
  1530. while pos>=0 do
  1531. begin
  1532. writer.AsmWrite(tostr((tai_const(hp).value shr (pos*8)) and $ff));
  1533. dec(pos);
  1534. if pos>=0 then
  1535. writer.AsmWrite(', ')
  1536. else
  1537. writer.AsmLn;
  1538. end;
  1539. end
  1540. else
  1541. begin
  1542. pos:=0;
  1543. while pos<size do
  1544. begin
  1545. writer.AsmWriteln(tostr((tai_const(hp).value shr (pos*8)) and $ff));
  1546. inc(pos);
  1547. if pos<=size then
  1548. writer.AsmWrite(', ')
  1549. else
  1550. writer.AsmLn;
  1551. end;
  1552. end;
  1553. writer.AsmLn;
  1554. end;
  1555. procedure TGNUAssembler.WriteDirectiveName(dir: TAsmDirective);
  1556. begin
  1557. { TODO: implement asd_cpu for GAS => usually .arch or .cpu, but the CPU
  1558. name has to be translated as well }
  1559. if dir=asd_cpu then
  1560. writer.AsmWrite(asminfo^.comment+' CPU ')
  1561. else
  1562. writer.AsmWrite('.'+directivestr[dir]+' ');
  1563. end;
  1564. procedure TGNUAssembler.WriteAsmList;
  1565. var
  1566. n : string;
  1567. hal : tasmlisttype;
  1568. i: longint;
  1569. begin
  1570. {$ifdef EXTDEBUG}
  1571. if current_module.mainsource<>'' then
  1572. Comment(V_Debug,'Start writing gas-styled assembler output for '+current_module.mainsource);
  1573. {$endif}
  1574. if current_module.mainsource<>'' then
  1575. n:=ExtractFileName(current_module.mainsource)
  1576. else
  1577. n:=InputFileName;
  1578. { gcc does not add it either for Darwin. Grep for
  1579. TARGET_ASM_FILE_START_FILE_DIRECTIVE in gcc/config/*.h
  1580. }
  1581. if not(target_info.system in systems_darwin) then
  1582. writer.AsmWriteLn(#9'.file "'+FixFileName(n)+'"');
  1583. WriteExtraHeader;
  1584. writer.MarkEmpty;
  1585. symendcount:=0;
  1586. for hal:=low(TasmlistType) to high(TasmlistType) do
  1587. begin
  1588. if not (current_asmdata.asmlists[hal].empty) then
  1589. begin
  1590. writer.AsmWriteLn(asminfo^.comment+'Begin asmlist '+AsmlistTypeStr[hal]);
  1591. writetree(current_asmdata.asmlists[hal]);
  1592. writer.AsmWriteLn(asminfo^.comment+'End asmlist '+AsmlistTypeStr[hal]);
  1593. end;
  1594. end;
  1595. { add weak symbol markers }
  1596. for i:=0 to current_asmdata.asmsymboldict.count-1 do
  1597. if (tasmsymbol(current_asmdata.asmsymboldict[i]).bind=AB_WEAK_EXTERNAL) then
  1598. WriteWeakSymbolRef(tasmsymbol(current_asmdata.asmsymboldict[i]));
  1599. if create_smartlink_sections and
  1600. (target_info.system in systems_darwin) then
  1601. writer.AsmWriteLn(#9'.subsections_via_symbols');
  1602. { "no executable stack" marker }
  1603. { TODO: used by OpenBSD/NetBSD as well? }
  1604. if (target_info.system in (systems_linux + systems_android + systems_freebsd + systems_dragonfly)) and
  1605. not(cs_executable_stack in current_settings.moduleswitches) then
  1606. begin
  1607. writer.AsmWriteLn('.section .note.GNU-stack,"",%progbits');
  1608. end;
  1609. writer.AsmLn;
  1610. WriteExtraFooter;
  1611. {$ifdef EXTDEBUG}
  1612. if current_module.mainsource<>'' then
  1613. Comment(V_Debug,'Done writing gas-styled assembler output for '+current_module.mainsource);
  1614. {$endif EXTDEBUG}
  1615. end;
  1616. {****************************************************************************}
  1617. { Apple/GNU Assembler writer }
  1618. {****************************************************************************}
  1619. function TAppleGNUAssembler.sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;
  1620. begin
  1621. if (target_info.system in systems_darwin) then
  1622. case atype of
  1623. sec_user:
  1624. begin
  1625. result:='.section '+aname;
  1626. exit;
  1627. end;
  1628. sec_bss:
  1629. { all bss (lcomm) symbols are automatically put in the right }
  1630. { place by using the lcomm assembler directive }
  1631. atype := sec_none;
  1632. sec_debug_frame,
  1633. sec_eh_frame:
  1634. begin
  1635. result := '.section __DWARF,__debug_info,regular,debug';
  1636. exit;
  1637. end;
  1638. sec_debug_line:
  1639. begin
  1640. result := '.section __DWARF,__debug_line,regular,debug';
  1641. exit;
  1642. end;
  1643. sec_debug_info:
  1644. begin
  1645. result := '.section __DWARF,__debug_info,regular,debug';
  1646. exit;
  1647. end;
  1648. sec_debug_abbrev:
  1649. begin
  1650. result := '.section __DWARF,__debug_abbrev,regular,debug';
  1651. exit;
  1652. end;
  1653. sec_debug_aranges:
  1654. begin
  1655. result := '.section __DWARF,__debug_aranges,regular,debug';
  1656. exit;
  1657. end;
  1658. sec_debug_ranges:
  1659. begin
  1660. result := '.section __DWARF,__debug_ranges,regular,debug';
  1661. exit;
  1662. end;
  1663. sec_rodata:
  1664. begin
  1665. result := '.const_data';
  1666. exit;
  1667. end;
  1668. sec_rodata_norel:
  1669. begin
  1670. result := '.const';
  1671. exit;
  1672. end;
  1673. sec_fpc:
  1674. begin
  1675. result := '.section __TEXT, .fpc, regular, no_dead_strip';
  1676. exit;
  1677. end;
  1678. sec_code:
  1679. begin
  1680. if (aname='fpc_geteipasebx') or
  1681. (aname='fpc_geteipasecx') then
  1682. begin
  1683. result:='.section __TEXT,__textcoal_nt,coalesced,pure_instructions'#10'.weak_definition '+aname+
  1684. #10'.private_extern '+aname;
  1685. exit;
  1686. end;
  1687. end;
  1688. sec_data_nonlazy:
  1689. begin
  1690. result:='.section __DATA, __nl_symbol_ptr,non_lazy_symbol_pointers';
  1691. exit;
  1692. end;
  1693. sec_data_lazy:
  1694. begin
  1695. result:='.section __DATA, __la_symbol_ptr,lazy_symbol_pointers';
  1696. exit;
  1697. end;
  1698. sec_init_func:
  1699. begin
  1700. result:='.section __DATA, __mod_init_func, mod_init_funcs';
  1701. exit;
  1702. end;
  1703. sec_term_func:
  1704. begin
  1705. result:='.section __DATA, __mod_term_func, mod_term_funcs';
  1706. exit;
  1707. end;
  1708. low(TObjCAsmSectionType)..high(TObjCAsmSectionType):
  1709. begin
  1710. result:='.section '+objc_section_name(atype);
  1711. exit
  1712. end;
  1713. else
  1714. ;
  1715. end;
  1716. result := inherited sectionname(atype,aname,aorder);
  1717. end;
  1718. procedure TAppleGNUAssembler.WriteWeakSymbolRef(s: tasmsymbol);
  1719. begin
  1720. writer.AsmWriteLn(#9'.weak_reference '+s.name);
  1721. end;
  1722. procedure TAppleGNUAssembler.WriteDirectiveName(dir: TAsmDirective);
  1723. begin
  1724. case dir of
  1725. asd_weak_reference:
  1726. writer.AsmWrite('.weak_reference ');
  1727. asd_weak_definition:
  1728. writer.AsmWrite('.weak_definition ');
  1729. else
  1730. inherited;
  1731. end;
  1732. end;
  1733. {****************************************************************************}
  1734. { a.out/GNU Assembler writer }
  1735. {****************************************************************************}
  1736. function TAoutGNUAssembler.sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;
  1737. const
  1738. (* Translation table - replace unsupported section types with basic ones. *)
  1739. SecXTable: array[TAsmSectionType] of TAsmSectionType = (
  1740. sec_none,
  1741. sec_none,
  1742. sec_code,
  1743. sec_data,
  1744. sec_data (* sec_rodata *),
  1745. sec_data (* sec_rodata_norel *),
  1746. sec_bss,
  1747. sec_data (* sec_threadvar *),
  1748. { used for wince exception handling }
  1749. sec_code (* sec_pdata *),
  1750. { used for darwin import stubs }
  1751. sec_code (* sec_stub *),
  1752. sec_data,(* sec_data_nonlazy *)
  1753. sec_data,(* sec_data_lazy *)
  1754. sec_data,(* sec_init_func *)
  1755. sec_data,(* sec_term_func *)
  1756. { stabs }
  1757. sec_stab,sec_stabstr,
  1758. { win32 }
  1759. sec_data (* sec_idata2 *),
  1760. sec_data (* sec_idata4 *),
  1761. sec_data (* sec_idata5 *),
  1762. sec_data (* sec_idata6 *),
  1763. sec_data (* sec_idata7 *),
  1764. sec_data (* sec_edata *),
  1765. { C++ exception handling unwinding (uses dwarf) }
  1766. sec_eh_frame,
  1767. { dwarf }
  1768. sec_debug_frame,
  1769. sec_debug_info,
  1770. sec_debug_line,
  1771. sec_debug_abbrev,
  1772. sec_debug_aranges,
  1773. sec_debug_ranges,
  1774. { ELF resources (+ references to stabs debug information sections) }
  1775. sec_code (* sec_fpc *),
  1776. { Table of contents section }
  1777. sec_code (* sec_toc *),
  1778. sec_code (* sec_init *),
  1779. sec_code (* sec_fini *),
  1780. sec_none (* sec_objc_class *),
  1781. sec_none (* sec_objc_meta_class *),
  1782. sec_none (* sec_objc_cat_cls_meth *),
  1783. sec_none (* sec_objc_cat_inst_meth *),
  1784. sec_none (* sec_objc_protocol *),
  1785. sec_none (* sec_objc_string_object *),
  1786. sec_none (* sec_objc_cls_meth *),
  1787. sec_none (* sec_objc_inst_meth *),
  1788. sec_none (* sec_objc_cls_refs *),
  1789. sec_none (* sec_objc_message_refs *),
  1790. sec_none (* sec_objc_symbols *),
  1791. sec_none (* sec_objc_category *),
  1792. sec_none (* sec_objc_class_vars *),
  1793. sec_none (* sec_objc_instance_vars *),
  1794. sec_none (* sec_objc_module_info *),
  1795. sec_none (* sec_objc_class_names *),
  1796. sec_none (* sec_objc_meth_var_types *),
  1797. sec_none (* sec_objc_meth_var_names *),
  1798. sec_none (* sec_objc_selector_strs *),
  1799. sec_none (* sec_objc_protocol_ext *),
  1800. sec_none (* sec_objc_class_ext *),
  1801. sec_none (* sec_objc_property *),
  1802. sec_none (* sec_objc_image_info *),
  1803. sec_none (* sec_objc_cstring_object *),
  1804. sec_none (* sec_objc_sel_fixup *),
  1805. sec_none (* sec_objc_data *),
  1806. sec_none (* sec_objc_const *),
  1807. sec_none (* sec_objc_sup_refs *),
  1808. sec_none (* sec_data_coalesced *),
  1809. sec_none (* sec_objc_classlist *),
  1810. sec_none (* sec_objc_nlclasslist *),
  1811. sec_none (* sec_objc_catlist *),
  1812. sec_none (* sec_objc_nlcatlist *),
  1813. sec_none (* sec_objc_protlist *),
  1814. sec_none (* sec_stack *),
  1815. sec_none (* sec_heap *),
  1816. sec_none (* gcc_except_table *)
  1817. );
  1818. begin
  1819. Result := inherited SectionName (SecXTable [AType], AName, AOrder);
  1820. end;
  1821. {****************************************************************************}
  1822. { Abstract Instruction Writer }
  1823. {****************************************************************************}
  1824. constructor TCPUInstrWriter.create(_owner: TGNUAssembler);
  1825. begin
  1826. inherited create;
  1827. owner := _owner;
  1828. end;
  1829. end.