aggas.pas 87 KB

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