aggas.pas 85 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239
  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_wasm32_wasi,
  501. system_wasm32_embedded:
  502. begin
  503. writer.AsmWrite('.section ');
  504. end
  505. else
  506. begin
  507. writer.AsmWrite('.section ');
  508. { sectionname may rename those sections, so we do not write flags/progbits for them,
  509. the assembler will ignore them/spite out a warning anyways }
  510. if not(atype in [sec_data,sec_rodata,sec_rodata_norel]) and
  511. not(asminfo^.id=as_solaris_as) then
  512. begin
  513. usesectionflags:=true;
  514. usesectionprogbits:=true;
  515. end;
  516. end
  517. end;
  518. s:=sectionname(atype,aname,aorder);
  519. writer.AsmWrite(s);
  520. { flags explicitly defined? }
  521. if (usesectionflags or usesectionprogbits) and
  522. ((secflags<>[]) or
  523. (secprogbits<>SPB_None)) then
  524. begin
  525. if usesectionflags then
  526. begin
  527. s:=',"'+sectionflags(secflags);
  528. writer.AsmWrite(s+'"');
  529. end;
  530. if usesectionprogbits then
  531. begin
  532. case secprogbits of
  533. SPB_PROGBITS:
  534. writer.AsmWrite(',%progbits');
  535. SPB_NOBITS:
  536. writer.AsmWrite(',%nobits');
  537. SPB_NOTE:
  538. writer.AsmWrite(',%note');
  539. SPB_None:
  540. ;
  541. else
  542. InternalError(2019100801);
  543. end;
  544. end;
  545. end
  546. else
  547. case atype of
  548. sec_fpc :
  549. if aname = 'resptrs' then
  550. writer.AsmWrite(', "a", @progbits');
  551. sec_stub :
  552. begin
  553. case target_info.system of
  554. { there are processor-independent shortcuts available }
  555. { for this, namely .symbol_stub and .picsymbol_stub, but }
  556. { they don't work and gcc doesn't use them either... }
  557. system_powerpc_darwin,
  558. system_powerpc64_darwin:
  559. if (cs_create_pic in current_settings.moduleswitches) then
  560. writer.AsmWriteln('__TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32')
  561. else
  562. writer.AsmWriteln('__TEXT,__symbol_stub1,symbol_stubs,pure_instructions,16');
  563. system_i386_darwin,
  564. system_i386_iphonesim:
  565. writer.AsmWriteln('__IMPORT,__jump_table,symbol_stubs,self_modifying_code+pure_instructions,5');
  566. system_arm_ios:
  567. if (cs_create_pic in current_settings.moduleswitches) then
  568. writer.AsmWriteln('__TEXT,__picsymbolstub4,symbol_stubs,none,16')
  569. else
  570. writer.AsmWriteln('__TEXT,__symbol_stub4,symbol_stubs,none,12')
  571. { darwin/(x86-64/AArch64) uses PC-based GOT addressing, no
  572. explicit symbol stubs }
  573. else
  574. internalerror(2006031101);
  575. end;
  576. end;
  577. else
  578. { GNU AS won't recognize '.text.n_something' section name as belonging
  579. to '.text' and assigns default attributes to it, which is not
  580. always correct. We have to fix it.
  581. TODO: This likely applies to all systems which smartlink without
  582. creating libraries }
  583. begin
  584. if is_smart_section(atype) and (aname<>'') then
  585. begin
  586. s:=sectionattrs(atype);
  587. if (s<>'') then
  588. writer.AsmWrite(',"'+s+'"');
  589. end;
  590. if target_info.system in systems_aix then
  591. begin
  592. s:=sectionalignment_aix(atype,secalign);
  593. if s<>'' then
  594. writer.AsmWrite(','+s);
  595. end;
  596. end;
  597. end;
  598. writer.AsmLn;
  599. LastSecType:=atype;
  600. end;
  601. procedure TGNUAssembler.WriteCFI(hp: tai_cfi_base);
  602. begin
  603. writer.AsmWrite(cfi2str[hp.cfityp]);
  604. case hp.cfityp of
  605. cfi_startproc,
  606. cfi_endproc:
  607. ;
  608. cfi_undefined,
  609. cfi_restore,
  610. cfi_def_cfa_register:
  611. begin
  612. writer.AsmWrite(' ');
  613. writer.AsmWrite(gas_regname(tai_cfi_op_reg(hp).reg1));
  614. end;
  615. cfi_def_cfa_offset:
  616. begin
  617. writer.AsmWrite(' ');
  618. writer.AsmWrite(tostr(tai_cfi_op_val(hp).val1));
  619. end;
  620. cfi_offset:
  621. begin
  622. writer.AsmWrite(' ');
  623. writer.AsmWrite(gas_regname(tai_cfi_op_reg_val(hp).reg1));
  624. writer.AsmWrite(',');
  625. writer.AsmWrite(tostr(tai_cfi_op_reg_val(hp).val));
  626. end;
  627. else
  628. internalerror(2019030203);
  629. end;
  630. writer.AsmLn;
  631. end;
  632. {$ifdef WASM}
  633. procedure TGNUAssembler.WriteFuncType(functype: TWasmFuncType);
  634. var
  635. wasm_basic_typ: TWasmBasicType;
  636. first: boolean;
  637. begin
  638. writer.AsmWrite('(');
  639. first:=true;
  640. for wasm_basic_typ in functype.params do
  641. begin
  642. if first then
  643. first:=false
  644. else
  645. writer.AsmWrite(',');
  646. writer.AsmWrite(gas_wasm_basic_type_str[wasm_basic_typ]);
  647. end;
  648. writer.AsmWrite(') -> (');
  649. first:=true;
  650. for wasm_basic_typ in functype.results do
  651. begin
  652. if first then
  653. first:=false
  654. else
  655. writer.AsmWrite(',');
  656. writer.AsmWrite(gas_wasm_basic_type_str[wasm_basic_typ]);
  657. end;
  658. writer.AsmWrite(')');
  659. end;
  660. {$endif WASM}
  661. procedure TGNUAssembler.WriteTree(p:TAsmList);
  662. function needsObject(hp : tai_symbol) : boolean;
  663. begin
  664. needsObject :=
  665. (
  666. assigned(hp.next) and
  667. (tai(hp.next).typ in [ait_const,ait_datablock,ait_realconst])
  668. ) or
  669. (hp.sym.typ in [AT_DATA,AT_METADATA]);
  670. end;
  671. procedure doalign(alignment: byte; use_op: boolean; fillop: byte; maxbytes: byte; out last_align: longint;lasthp:tai);
  672. var
  673. i: longint;
  674. alignment64 : int64;
  675. {$ifdef m68k}
  676. instr : string;
  677. {$endif}
  678. begin
  679. last_align:=alignment;
  680. if alignment>1 then
  681. begin
  682. if not(target_info.system in (systems_darwin+systems_aix)) then
  683. begin
  684. {$ifdef m68k}
  685. if not use_op and (lastsectype=sec_code) then
  686. begin
  687. if not ispowerof2(alignment,i) then
  688. internalerror(2014022201);
  689. { the Coldfire manual suggests the TBF instruction for
  690. alignments, but somehow QEMU does not interpret that
  691. correctly... }
  692. {if current_settings.cputype in cpu_coldfire then
  693. instr:='0x51fc'
  694. else}
  695. instr:='0x4e71';
  696. writer.AsmWrite(#9'.balignw '+tostr(alignment)+','+instr);
  697. end
  698. else
  699. begin
  700. {$endif m68k}
  701. alignment64:=alignment;
  702. if (maxbytes<>alignment) and ispowerof2(alignment64,i) then
  703. begin
  704. if use_op then
  705. begin
  706. writer.AsmWrite(#9'.p2align '+tostr(i)+','+tostr(fillop)+','+tostr(maxbytes));
  707. writer.AsmLn;
  708. writer.AsmWrite(#9'.p2align '+tostr(i-1)+','+tostr(fillop));
  709. end
  710. else
  711. begin
  712. writer.AsmWrite(#9'.p2align '+tostr(i)+',,'+tostr(maxbytes));
  713. writer.AsmLn;
  714. writer.AsmWrite(#9'.p2align '+tostr(i-1));
  715. end
  716. end
  717. else
  718. begin
  719. writer.AsmWrite(#9'.balign '+tostr(alignment));
  720. if use_op then
  721. writer.AsmWrite(','+tostr(fillop))
  722. {$ifdef x86}
  723. { force NOP as alignment op code }
  724. else if (LastSecType=sec_code) and (asminfo^.id<>as_solaris_as) then
  725. writer.AsmWrite(',0x90');
  726. {$endif x86}
  727. end;
  728. {$ifdef m68k}
  729. end;
  730. {$endif m68k}
  731. end
  732. else
  733. begin
  734. { darwin and aix as only support .align }
  735. if not ispowerof2(alignment,i) then
  736. internalerror(2003010305);
  737. writer.AsmWrite(#9'.align '+tostr(i));
  738. last_align:=i;
  739. end;
  740. writer.AsmLn;
  741. end;
  742. end;
  743. {$ifdef WASM}
  744. procedure WriteTagType(hp: tai_tagtype);
  745. var
  746. wasm_basic_typ: TWasmBasicType;
  747. first: boolean;
  748. begin
  749. writer.AsmWrite(#9'.tagtype'#9);
  750. writer.AsmWrite(hp.tagname);
  751. first:=true;
  752. for wasm_basic_typ in hp.params do
  753. begin
  754. if first then
  755. begin
  756. first:=false;
  757. writer.AsmWrite(' ');
  758. end
  759. else
  760. writer.AsmWrite(',');
  761. writer.AsmWrite(gas_wasm_basic_type_str[wasm_basic_typ]);
  762. end;
  763. writer.AsmLn;
  764. end;
  765. {$endif WASM}
  766. var
  767. ch : char;
  768. lasthp,
  769. hp : tai;
  770. constdef : taiconst_type;
  771. s,t : string;
  772. i,pos,l : longint;
  773. InlineLevel : cardinal;
  774. last_align : longint;
  775. do_line : boolean;
  776. sepChar : char;
  777. replaceforbidden: boolean;
  778. begin
  779. if not assigned(p) then
  780. exit;
  781. replaceforbidden:=asminfo^.dollarsign<>'$';
  782. last_align := 2;
  783. InlineLevel:=0;
  784. { lineinfo is only needed for al_procedures (PFV) }
  785. do_line:=(cs_asm_source in current_settings.globalswitches) or
  786. ((cs_lineinfo in current_settings.moduleswitches)
  787. and (p=current_asmdata.asmlists[al_procedures]));
  788. lasthp:=nil;
  789. hp:=tai(p.first);
  790. while assigned(hp) do
  791. begin
  792. prefetch(pointer(hp.next)^);
  793. if not(hp.typ in SkipLineInfo) then
  794. begin
  795. current_filepos:=tailineinfo(hp).fileinfo;
  796. { no line info for inlined code }
  797. if do_line and (inlinelevel=0) then
  798. WriteSourceLine(hp as tailineinfo);
  799. end;
  800. case hp.typ of
  801. ait_align :
  802. begin
  803. 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);
  804. end;
  805. ait_section :
  806. begin
  807. ResetSourceLines;
  808. if tai_section(hp).sectype<>sec_none then
  809. if replaceforbidden then
  810. WriteSection(tai_section(hp).sectype,ApplyAsmSymbolRestrictions(tai_section(hp).name^),tai_section(hp).secorder,
  811. tai_section(hp).secalign,tai_section(hp).secflags,tai_section(hp).secprogbits)
  812. else
  813. WriteSection(tai_section(hp).sectype,tai_section(hp).name^,tai_section(hp).secorder,
  814. tai_section(hp).secalign,tai_section(hp).secflags,tai_section(hp).secprogbits)
  815. else
  816. begin
  817. {$ifdef EXTDEBUG}
  818. writer.AsmWrite(asminfo^.comment);
  819. writer.AsmWriteln(' sec_none');
  820. {$endif EXTDEBUG}
  821. end;
  822. end;
  823. ait_datablock :
  824. begin
  825. if (target_info.system in systems_darwin) then
  826. begin
  827. { On Mac OS X you can't have common symbols in a shared library
  828. since those are in the TEXT section and the text section is
  829. read-only in shared libraries (so it can be shared among different
  830. processes). The alternate code creates some kind of common symbols
  831. in the data segment.
  832. }
  833. if tai_datablock(hp).is_global then
  834. begin
  835. if tai_datablock(hp).sym.bind=AB_PRIVATE_EXTERN then
  836. WriteHiddenSymbol(tai_datablock(hp).sym);
  837. writer.AsmWrite('.globl ');
  838. writer.AsmWriteln(tai_datablock(hp).sym.name);
  839. writer.AsmWriteln('.data');
  840. writer.AsmWrite('.zerofill __DATA, __common, ');
  841. writer.AsmWrite(tai_datablock(hp).sym.name);
  842. writer.AsmWriteln(', '+tostr(tai_datablock(hp).size)+','+tostr(last_align));
  843. if not(LastSecType in [sec_data,sec_none]) then
  844. writesection(LastSecType,'',secorder_default,1 shl last_align);
  845. end
  846. else
  847. begin
  848. writer.AsmWrite(#9'.lcomm'#9);
  849. writer.AsmWrite(tai_datablock(hp).sym.name);
  850. writer.AsmWrite(','+tostr(tai_datablock(hp).size));
  851. writer.AsmWrite(','+tostr(last_align));
  852. writer.AsmLn;
  853. end;
  854. end
  855. else if target_info.system in systems_aix then
  856. begin
  857. if tai_datablock(hp).is_global then
  858. begin
  859. writer.AsmWrite(#9'.globl ');
  860. writer.AsmWriteln(ApplyAsmSymbolRestrictions(tai_datablock(hp).sym.name));
  861. writer.AsmWrite(ApplyAsmSymbolRestrictions(tai_datablock(hp).sym.name));
  862. writer.AsmWriteln(':');
  863. writer.AsmWrite(#9'.space ');
  864. writer.AsmWriteln(tostr(tai_datablock(hp).size));
  865. if not(LastSecType in [sec_data,sec_none]) then
  866. writesection(LastSecType,'',secorder_default,1 shl last_align);
  867. end
  868. else
  869. begin
  870. writer.AsmWrite(#9'.lcomm ');
  871. writer.AsmWrite(ApplyAsmSymbolRestrictions(tai_datablock(hp).sym.name));
  872. writer.AsmWrite(',');
  873. writer.AsmWrite(tostr(tai_datablock(hp).size)+',');
  874. writer.AsmWrite('_data.bss_,');
  875. writer.AsmWriteln(tostr(last_align));
  876. end;
  877. end
  878. else
  879. begin
  880. {$ifdef USE_COMM_IN_BSS}
  881. if writingpackages then
  882. begin
  883. { The .comm is required for COMMON symbols. These are used
  884. in the shared library loading. All the symbols declared in
  885. the .so file need to resolve to the data allocated in the main
  886. program (PFV) }
  887. if tai_datablock(hp).is_global then
  888. begin
  889. writer.AsmWrite(#9'.comm'#9);
  890. if replaceforbidden then
  891. writer.AsmWrite(ApplyAsmSymbolRestrictions(tai_datablock(hp).sym.name))
  892. else
  893. writer.AsmWrite(tai_datablock(hp).sym.name);
  894. writer.AsmWrite(','+tostr(tai_datablock(hp).size));
  895. writer.AsmWrite(','+tostr(last_align));
  896. writer.AsmLn;
  897. end
  898. else
  899. begin
  900. writer.AsmWrite(#9'.lcomm'#9);
  901. if replaceforbidden then
  902. writer.AsmWrite(ApplyAsmSymbolRestrictions(tai_datablock(hp).sym.name));
  903. else
  904. writer.AsmWrite(tai_datablock(hp).sym.name);
  905. writer.AsmWrite(','+tostr(tai_datablock(hp).size));
  906. writer.AsmWrite(','+tostr(last_align));
  907. writer.AsmLn;
  908. end
  909. end
  910. else
  911. {$endif USE_COMM_IN_BSS}
  912. begin
  913. if Tai_datablock(hp).is_global then
  914. begin
  915. if (tai_datablock(hp).sym.bind=AB_PRIVATE_EXTERN) then
  916. WriteHiddenSymbol(tai_datablock(hp).sym);
  917. writer.AsmWrite(#9'.globl ');
  918. if replaceforbidden then
  919. writer.AsmWriteln(ApplyAsmSymbolRestrictions(Tai_datablock(hp).sym.name))
  920. else
  921. writer.AsmWriteln(Tai_datablock(hp).sym.name);
  922. end;
  923. if ((target_info.system <> system_arm_linux) and (target_info.system <> system_arm_android)) then
  924. sepChar := '@'
  925. else
  926. sepChar := '%';
  927. if replaceforbidden then
  928. begin
  929. if (tf_needs_symbol_type in target_info.flags) then
  930. writer.AsmWriteln(#9'.type '+ApplyAsmSymbolRestrictions(Tai_datablock(hp).sym.name)+','+sepChar+'object');
  931. if (tf_needs_symbol_size in target_info.flags) and (tai_datablock(hp).size > 0) then
  932. writer.AsmWriteln(#9'.size '+ApplyAsmSymbolRestrictions(Tai_datablock(hp).sym.name)+','+tostr(Tai_datablock(hp).size));
  933. writer.AsmWrite(ApplyAsmSymbolRestrictions(Tai_datablock(hp).sym.name))
  934. end
  935. else
  936. begin
  937. if (tf_needs_symbol_type in target_info.flags) then
  938. writer.AsmWriteln(#9'.type '+Tai_datablock(hp).sym.name+','+sepChar+'object');
  939. if (tf_needs_symbol_size in target_info.flags) and (tai_datablock(hp).size > 0) then
  940. writer.AsmWriteln(#9'.size '+Tai_datablock(hp).sym.name+','+tostr(Tai_datablock(hp).size));
  941. writer.AsmWrite(Tai_datablock(hp).sym.name);
  942. end;
  943. writer.AsmWriteln(':');
  944. writer.AsmWriteln(#9'.zero '+tostr(Tai_datablock(hp).size));
  945. end;
  946. end;
  947. end;
  948. ait_const:
  949. begin
  950. constdef:=tai_const(hp).consttype;
  951. case constdef of
  952. {$ifndef cpu64bitaddr}
  953. aitconst_128bit :
  954. begin
  955. internalerror(200404291);
  956. end;
  957. aitconst_64bit :
  958. begin
  959. if assigned(tai_const(hp).sym) then
  960. internalerror(200404292);
  961. if not(target_info.system in systems_aix) then
  962. begin
  963. if (target_info.system in use_ua_elf_systems) then
  964. writer.AsmWrite(ait_ua_elf_const2str[aitconst_32bit])
  965. else
  966. writer.AsmWrite(ait_const2str[aitconst_32bit]);
  967. if target_info.endian = endian_little then
  968. begin
  969. writer.AsmWrite(tostr(longint(lo(tai_const(hp).value))));
  970. writer.AsmWrite(',');
  971. writer.AsmWrite(tostr(longint(hi(tai_const(hp).value))));
  972. end
  973. else
  974. begin
  975. writer.AsmWrite(tostr(longint(hi(tai_const(hp).value))));
  976. writer.AsmWrite(',');
  977. writer.AsmWrite(tostr(longint(lo(tai_const(hp).value))));
  978. end;
  979. end
  980. else
  981. WriteAixIntConst(tai_const(hp));
  982. writer.AsmLn;
  983. end;
  984. aitconst_gottpoff:
  985. begin
  986. writer.AsmWrite(#9'.word'#9+tai_const(hp).sym.name+'(gottpoff)+(.-'+tai_const(hp).endsym.name+tostr_with_plus(tai_const(hp).symofs)+')');
  987. writer.Asmln;
  988. end;
  989. aitconst_tlsgd:
  990. begin
  991. writer.AsmWrite(#9'.word'#9+tai_const(hp).sym.name+'(tlsgd)+(.-'+tai_const(hp).endsym.name+tostr_with_plus(tai_const(hp).symofs)+')');
  992. writer.Asmln;
  993. end;
  994. aitconst_tlsdesc:
  995. begin
  996. writer.AsmWrite(#9'.word'#9+tai_const(hp).sym.name+'(tlsdesc)+(.-'+tai_const(hp).endsym.name+tostr_with_plus(tai_const(hp).symofs)+')');
  997. writer.Asmln;
  998. end;
  999. aitconst_tpoff:
  1000. begin
  1001. if assigned(tai_const(hp).endsym) or (tai_const(hp).symofs<>0) then
  1002. Internalerror(2019092805);
  1003. writer.AsmWrite(#9'.word'#9+tai_const(hp).sym.name+'(tpoff)');
  1004. writer.Asmln;
  1005. end;
  1006. {$endif cpu64bitaddr}
  1007. aitconst_dtpoff:
  1008. begin
  1009. {$ifdef arm}
  1010. writer.AsmWrite(#9'.word'#9+tai_const(hp).sym.name+'(tlsldo)');
  1011. writer.Asmln;
  1012. {$endif arm}
  1013. {$ifdef x86_64}
  1014. writer.AsmWrite(#9'.long'#9+tai_const(hp).sym.name+'@dtpoff');
  1015. writer.Asmln;
  1016. {$endif x86_64}
  1017. {$ifdef i386}
  1018. writer.AsmWrite(#9'.word'#9+tai_const(hp).sym.name+'@tdpoff');
  1019. writer.Asmln;
  1020. {$endif i386}
  1021. end;
  1022. aitconst_got:
  1023. begin
  1024. if tai_const(hp).symofs<>0 then
  1025. InternalError(2015091401); // No symbol offset is allowed for GOT.
  1026. writer.AsmWrite(#9'.word'#9+tai_const(hp).sym.name+'(GOT)');
  1027. writer.AsmLn;
  1028. end;
  1029. aitconst_gotoff_symbol:
  1030. begin
  1031. if (tai_const(hp).sym=nil) then
  1032. InternalError(2014022601);
  1033. case target_info.cpu of
  1034. cpu_mipseb,cpu_mipsel:
  1035. begin
  1036. writer.AsmWrite(#9'.gpword'#9);
  1037. writer.AsmWrite(tai_const(hp).sym.name);
  1038. end;
  1039. cpu_i386:
  1040. begin
  1041. writer.AsmWrite(ait_const2str[aitconst_32bit]);
  1042. writer.AsmWrite(tai_const(hp).sym.name+'-_GLOBAL_OFFSET_TABLE_');
  1043. end;
  1044. else
  1045. InternalError(2014022602);
  1046. end;
  1047. if (tai_const(hp).value<>0) then
  1048. writer.AsmWrite(tostr_with_plus(tai_const(hp).value));
  1049. writer.AsmLn;
  1050. end;
  1051. aitconst_uleb128bit,
  1052. aitconst_sleb128bit,
  1053. {$ifdef cpu64bitaddr}
  1054. aitconst_128bit,
  1055. aitconst_64bit,
  1056. {$endif cpu64bitaddr}
  1057. aitconst_32bit,
  1058. aitconst_16bit,
  1059. aitconst_8bit,
  1060. aitconst_rva_symbol,
  1061. aitconst_secrel32_symbol,
  1062. aitconst_darwin_dwarf_delta32,
  1063. aitconst_darwin_dwarf_delta64,
  1064. aitconst_half16bit,
  1065. aitconst_gs,
  1066. aitconst_16bit_unaligned,
  1067. aitconst_32bit_unaligned,
  1068. aitconst_64bit_unaligned:
  1069. begin
  1070. { the AIX assembler (and for compatibility, the GNU
  1071. assembler when targeting AIX) automatically aligns
  1072. .short/.long/.llong to a multiple of 2/4/8 bytes. We
  1073. don't want that, since this may be data inside a packed
  1074. record -> use .vbyte instead (byte stream of fixed
  1075. length) }
  1076. if (target_info.system in systems_aix) and
  1077. (constdef in [aitconst_128bit,aitconst_64bit,aitconst_32bit,aitconst_16bit]) and
  1078. not assigned(tai_const(hp).sym) then
  1079. begin
  1080. WriteAixIntConst(tai_const(hp));
  1081. end
  1082. else if (target_info.system in systems_darwin) and
  1083. (constdef in [aitconst_uleb128bit,aitconst_sleb128bit]) then
  1084. begin
  1085. writer.AsmWrite(ait_const2str[aitconst_8bit]);
  1086. case tai_const(hp).consttype of
  1087. aitconst_uleb128bit:
  1088. writer.AsmWrite(uleb128tostr(qword(tai_const(hp).value)));
  1089. aitconst_sleb128bit:
  1090. writer.AsmWrite(sleb128tostr(tai_const(hp).value));
  1091. else
  1092. ;
  1093. end
  1094. end
  1095. else
  1096. begin
  1097. if (constdef in ait_unaligned_consts) and
  1098. (target_info.system in use_ua_sparc_systems) then
  1099. writer.AsmWrite(ait_ua_sparc_const2str[constdef])
  1100. else if (target_info.system in use_ua_elf_systems) then
  1101. writer.AsmWrite(ait_ua_elf_const2str[constdef])
  1102. { we can also have unaligned pointers in packed record
  1103. constants, which don't get translated into
  1104. unaligned tai -> always use vbyte }
  1105. else if target_info.system in systems_aix then
  1106. writer.AsmWrite(#9'.vbyte'#9+tostr(tai_const(hp).size)+',')
  1107. else if (asminfo^.id=as_solaris_as) then
  1108. writer.AsmWrite(ait_solaris_const2str[constdef])
  1109. else
  1110. writer.AsmWrite(ait_const2str[constdef]);
  1111. l:=0;
  1112. t := '';
  1113. repeat
  1114. if assigned(tai_const(hp).sym) then
  1115. begin
  1116. if assigned(tai_const(hp).endsym) then
  1117. begin
  1118. if (constdef in [aitconst_darwin_dwarf_delta32,aitconst_darwin_dwarf_delta64]) then
  1119. begin
  1120. s := NextSetLabel;
  1121. t := #9'.set '+s+','+tai_const(hp).endsym.name+'-'+tai_const(hp).sym.name;
  1122. end
  1123. else
  1124. s:=tai_const(hp).endsym.name+'-'+tai_const(hp).sym.name
  1125. end
  1126. else
  1127. s:=tai_const(hp).sym.name;
  1128. if replaceforbidden then
  1129. s:=ApplyAsmSymbolRestrictions(s);
  1130. if tai_const(hp).value<>0 then
  1131. s:=s+tostr_with_plus(tai_const(hp).value);
  1132. end
  1133. else
  1134. {$ifdef cpu64bitaddr}
  1135. s:=tostr(tai_const(hp).value);
  1136. {$else cpu64bitaddr}
  1137. { 64 bit constants are already handled above in this case }
  1138. s:=tostr(longint(tai_const(hp).value));
  1139. {$endif cpu64bitaddr}
  1140. if constdef = aitconst_half16bit then
  1141. s:='('+s+')/2';
  1142. if constdef = aitconst_gs then
  1143. s:='gs('+s+')';
  1144. writer.AsmWrite(s);
  1145. inc(l,length(s));
  1146. { Values with symbols are written on a single line to improve
  1147. reading of the .s file (PFV) }
  1148. if assigned(tai_const(hp).sym) or
  1149. not(LastSecType in [sec_data,sec_rodata,sec_rodata_norel]) or
  1150. (l>line_length) or
  1151. (hp.next=nil) or
  1152. (tai(hp.next).typ<>ait_const) or
  1153. (tai_const(hp.next).consttype<>constdef) or
  1154. assigned(tai_const(hp.next).sym) then
  1155. break;
  1156. hp:=tai(hp.next);
  1157. writer.AsmWrite(',');
  1158. until false;
  1159. if (t <> '') then
  1160. begin
  1161. writer.AsmLn;
  1162. writer.AsmWrite(t);
  1163. end;
  1164. end;
  1165. writer.AsmLn;
  1166. end;
  1167. else
  1168. internalerror(200704251);
  1169. end;
  1170. end;
  1171. ait_realconst :
  1172. begin
  1173. WriteRealConstAsBytes(tai_realconst(hp),#9'.byte'#9,do_line);
  1174. end;
  1175. ait_string :
  1176. begin
  1177. pos:=0;
  1178. if not(target_info.system in systems_aix) then
  1179. begin
  1180. for i:=1 to tai_string(hp).len do
  1181. begin
  1182. if pos=0 then
  1183. begin
  1184. writer.AsmWrite(#9'.ascii'#9'"');
  1185. pos:=20;
  1186. end;
  1187. ch:=tai_string(hp).str[i-1];
  1188. case ch of
  1189. #0, {This can't be done by range, because a bug in FPC}
  1190. #1..#31,
  1191. #128..#255 : s:='\'+tostr(ord(ch) shr 6)+tostr((ord(ch) and 63) shr 3)+tostr(ord(ch) and 7);
  1192. '"' : s:='\"';
  1193. '\' : s:='\\';
  1194. else
  1195. s:=ch;
  1196. end;
  1197. writer.AsmWrite(s);
  1198. inc(pos,length(s));
  1199. if (pos>line_length) or (i=tai_string(hp).len) then
  1200. begin
  1201. writer.AsmWriteLn('"');
  1202. pos:=0;
  1203. end;
  1204. end;
  1205. end
  1206. else
  1207. WriteAixStringConst(tai_string(hp));
  1208. end;
  1209. ait_label :
  1210. begin
  1211. {$ifdef DEBUG_LABEL}
  1212. writer.AsmWrite(asminfo^.comment);
  1213. writer.AsmWriteLn('References = ' + tostr(tai_label(hp).labsym.getrefs));
  1214. if tai_label(hp).labsym.getrefs=0 then
  1215. writer.AsmWriteln(asminfo^.comment+'Optimized out label '+tai_label(hp).labsym.name);
  1216. {$endif DEBUG_LABEL}
  1217. if (tai_label(hp).labsym.is_used) then
  1218. begin
  1219. if tai_label(hp).labsym.bind in [AB_GLOBAL,AB_PRIVATE_EXTERN] then
  1220. begin
  1221. if (tai_label(hp).labsym.bind=AB_PRIVATE_EXTERN) then
  1222. begin
  1223. writer.AsmWrite(#9'.private_extern ');
  1224. writer.AsmWriteln(tai_label(hp).labsym.name);
  1225. end;
  1226. {$ifdef arm}
  1227. { do no change arm mode accidently, .globl seems to reset the mode }
  1228. if GenerateThumbCode or GenerateThumb2Code then
  1229. writer.AsmWriteln(#9'.thumb_func'#9);
  1230. {$endif arm}
  1231. writer.AsmWrite('.globl'#9);
  1232. if replaceforbidden then
  1233. writer.AsmWriteLn(ApplyAsmSymbolRestrictions(tai_label(hp).labsym.name))
  1234. else
  1235. writer.AsmWriteLn(tai_label(hp).labsym.name);
  1236. end;
  1237. if replaceforbidden then
  1238. writer.AsmWrite(ApplyAsmSymbolRestrictions(tai_label(hp).labsym.name))
  1239. else
  1240. writer.AsmWrite(tai_label(hp).labsym.name);
  1241. writer.AsmWriteLn(':');
  1242. end;
  1243. end;
  1244. ait_symbol :
  1245. begin
  1246. if (target_info.system=system_powerpc64_linux) and
  1247. (tai_symbol(hp).sym.typ=AT_FUNCTION) and
  1248. (cs_profile in current_settings.moduleswitches) then
  1249. writer.AsmWriteLn('.globl _mcount');
  1250. if tai_symbol(hp).is_global then
  1251. begin
  1252. writer.AsmWrite('.globl'#9);
  1253. if replaceforbidden then
  1254. writer.AsmWriteln(ApplyAsmSymbolRestrictions(tai_symbol(hp).sym.name))
  1255. else
  1256. writer.AsmWriteln(tai_symbol(hp).sym.name);
  1257. if (tai_symbol(hp).sym.bind=AB_PRIVATE_EXTERN) then
  1258. WriteHiddenSymbol(tai_symbol(hp).sym);
  1259. end;
  1260. if (target_info.system=system_powerpc64_linux) and
  1261. use_dotted_functions and
  1262. (tai_symbol(hp).sym.typ=AT_FUNCTION) then
  1263. begin
  1264. writer.AsmWriteLn('.section ".opd", "aw"');
  1265. writer.AsmWriteLn('.align 3');
  1266. writer.AsmWriteLn(tai_symbol(hp).sym.name + ':');
  1267. writer.AsmWriteLn('.quad .' + tai_symbol(hp).sym.name + ', .TOC.@tocbase, 0');
  1268. writer.AsmWriteLn('.previous');
  1269. writer.AsmWriteLn('.size ' + tai_symbol(hp).sym.name + ', 24');
  1270. if (tai_symbol(hp).is_global) then
  1271. writer.AsmWriteLn('.globl .' + tai_symbol(hp).sym.name);
  1272. writer.AsmWriteLn('.type .' + tai_symbol(hp).sym.name + ', @function');
  1273. { the dotted name is the name of the actual function entry }
  1274. writer.AsmWrite('.');
  1275. end
  1276. else if (target_info.system in systems_aix) and
  1277. (tai_symbol(hp).sym.typ = AT_FUNCTION) then
  1278. begin
  1279. if target_info.system=system_powerpc_aix then
  1280. begin
  1281. s:=#9'.long .';
  1282. ch:='2';
  1283. end
  1284. else
  1285. begin
  1286. s:=#9'.llong .';
  1287. ch:='3';
  1288. end;
  1289. writer.AsmWriteLn(#9'.csect '+ApplyAsmSymbolRestrictions(tai_symbol(hp).sym.name)+'[DS],'+ch);
  1290. writer.AsmWriteLn(ApplyAsmSymbolRestrictions(tai_symbol(hp).sym.name)+':');
  1291. writer.AsmWriteln(s+ApplyAsmSymbolRestrictions(tai_symbol(hp).sym.name)+', TOC[tc0], 0');
  1292. writer.AsmWriteln(#9'.csect .text[PR]');
  1293. if (tai_symbol(hp).is_global) then
  1294. writer.AsmWriteLn('.globl .'+ApplyAsmSymbolRestrictions(tai_symbol(hp).sym.name))
  1295. else
  1296. writer.AsmWriteLn('.lglobl .'+ApplyAsmSymbolRestrictions(tai_symbol(hp).sym.name));
  1297. { the dotted name is the name of the actual function entry }
  1298. writer.AsmWrite('.');
  1299. end
  1300. else if tai_symbol(hp).sym.typ=AT_WASM_EXCEPTION_TAG then
  1301. begin
  1302. { nothing here, to ensure we don' write the .type directive for exception tags }
  1303. end
  1304. else
  1305. begin
  1306. if ((target_info.system <> system_arm_linux) and (target_info.system <> system_arm_android)) or
  1307. (target_asm.id=as_arm_vasm) then
  1308. sepChar := '@'
  1309. else
  1310. sepChar := '#';
  1311. if (tf_needs_symbol_type in target_info.flags) then
  1312. begin
  1313. writer.AsmWrite(#9'.type'#9 + tai_symbol(hp).sym.name);
  1314. if (needsObject(tai_symbol(hp))) then
  1315. writer.AsmWriteLn(',' + sepChar + 'object')
  1316. else
  1317. writer.AsmWriteLn(',' + sepChar + 'function');
  1318. end;
  1319. end;
  1320. if replaceforbidden then
  1321. if not(tai_symbol(hp).has_value) then
  1322. writer.AsmWriteLn(ApplyAsmSymbolRestrictions(tai_symbol(hp).sym.name + ':'))
  1323. else
  1324. writer.AsmWriteLn(ApplyAsmSymbolRestrictions(tai_symbol(hp).sym.name + '=' + tostr(tai_symbol(hp).value)))
  1325. else if not(tai_symbol(hp).has_value) then
  1326. writer.AsmWriteLn(tai_symbol(hp).sym.name + ':')
  1327. else
  1328. writer.AsmWriteLn(tai_symbol(hp).sym.name + '=' + tostr(tai_symbol(hp).value));
  1329. end;
  1330. ait_symbolpair:
  1331. begin
  1332. writer.AsmWrite(#9);
  1333. writer.AsmWrite(symbolpairkindstr[tai_symbolpair(hp).kind]);
  1334. writer.AsmWrite(' ');
  1335. if tai_symbolpair(hp).kind<>spk_localentry then
  1336. s:=', '
  1337. else
  1338. { the .localentry directive has to specify the size from the
  1339. start till here of the non-local entry code as second argument }
  1340. s:=', .-';
  1341. if ((target_info.system <> system_arm_linux) and (target_info.system <> system_arm_android)) then
  1342. sepChar := '@'
  1343. else
  1344. sepChar := '#';
  1345. if replaceforbidden then
  1346. begin
  1347. { avoid string truncation }
  1348. writer.AsmWrite(ApplyAsmSymbolRestrictions(tai_symbolpair(hp).sym^));
  1349. writer.AsmWrite(s);
  1350. writer.AsmWriteLn(ApplyAsmSymbolRestrictions(tai_symbolpair(hp).value^));
  1351. if tai_symbolpair(hp).kind=spk_set_global then
  1352. begin
  1353. writer.AsmWrite(#9'.globl ');
  1354. writer.AsmWriteLn(ApplyAsmSymbolRestrictions(tai_symbolpair(hp).sym^));
  1355. end;
  1356. if (tf_needs_symbol_type in target_info.flags) then
  1357. begin
  1358. writer.AsmWrite(#9'.type'#9 + ApplyAsmSymbolRestrictions(tai_symbolpair(hp).sym^));
  1359. writer.AsmWriteLn(',' + sepChar + 'function');
  1360. end;
  1361. end
  1362. else
  1363. begin
  1364. { avoid string truncation }
  1365. writer.AsmWrite(tai_symbolpair(hp).sym^);
  1366. writer.AsmWrite(s);
  1367. writer.AsmWriteLn(tai_symbolpair(hp).value^);
  1368. if tai_symbolpair(hp).kind=spk_set_global then
  1369. begin
  1370. writer.AsmWrite(#9'.globl ');
  1371. writer.AsmWriteLn(tai_symbolpair(hp).sym^);
  1372. end;
  1373. if (tf_needs_symbol_type in target_info.flags) then
  1374. begin
  1375. writer.AsmWrite(#9'.type'#9 + tai_symbolpair(hp).sym^);
  1376. writer.AsmWriteLn(',' + sepChar + 'function');
  1377. end;
  1378. end;
  1379. end;
  1380. ait_symbol_end :
  1381. begin
  1382. if tf_needs_symbol_size in target_info.flags then
  1383. begin
  1384. s:=asminfo^.labelprefix+'e'+tostr(symendcount);
  1385. inc(symendcount);
  1386. writer.AsmWriteLn(s+':');
  1387. writer.AsmWrite(#9'.size'#9);
  1388. if (target_info.system=system_powerpc64_linux) and
  1389. use_dotted_functions and
  1390. (tai_symbol_end(hp).sym.typ=AT_FUNCTION) then
  1391. writer.AsmWrite('.');
  1392. if replaceforbidden then
  1393. writer.AsmWrite(ApplyAsmSymbolRestrictions(tai_symbol_end(hp).sym.name))
  1394. else
  1395. writer.AsmWrite(tai_symbol_end(hp).sym.name);
  1396. writer.AsmWrite(', '+s+' - ');
  1397. if (target_info.system=system_powerpc64_linux) and
  1398. use_dotted_functions and
  1399. (tai_symbol_end(hp).sym.typ=AT_FUNCTION) then
  1400. writer.AsmWrite('.');
  1401. if replaceforbidden then
  1402. writer.AsmWriteLn(ApplyAsmSymbolRestrictions(tai_symbol_end(hp).sym.name))
  1403. else
  1404. writer.AsmWriteLn(tai_symbol_end(hp).sym.name);
  1405. end;
  1406. end;
  1407. ait_instruction :
  1408. begin
  1409. WriteInstruction(hp);
  1410. end;
  1411. ait_stab :
  1412. begin
  1413. if assigned(tai_stab(hp).str) then
  1414. begin
  1415. writer.AsmWrite(#9'.'+stabtypestr[tai_stab(hp).stabtype]+' ');
  1416. writer.AsmWritePChar(tai_stab(hp).str);
  1417. writer.AsmLn;
  1418. end;
  1419. end;
  1420. ait_force_line,
  1421. ait_function_name :
  1422. begin
  1423. {$ifdef DEBUG_AGGAS}
  1424. WriteStr(s,hp.typ);
  1425. writer.AsmWriteLn('# '+s);
  1426. {$endif DEBUG_AGGAS}
  1427. end;
  1428. ait_cutobject :
  1429. begin
  1430. {$ifdef DEBUG_AGGAS}
  1431. writer.AsmWriteLn('# ait_cutobject');
  1432. {$endif DEBUG_AGGAS}
  1433. if SmartAsm then
  1434. begin
  1435. { only reset buffer if nothing has changed }
  1436. if not(writer.ClearIfEmpty) then
  1437. begin
  1438. writer.AsmClose;
  1439. DoAssemble;
  1440. writer.AsmCreate(tai_cutobject(hp).place);
  1441. end;
  1442. { avoid empty files }
  1443. while assigned(hp.next) and (tai(hp.next).typ in [ait_cutobject,ait_section,ait_comment]) do
  1444. begin
  1445. if tai(hp.next).typ=ait_section then
  1446. LastSecType:=tai_section(hp.next).sectype;
  1447. hp:=tai(hp.next);
  1448. end;
  1449. if LastSecType<>sec_none then
  1450. WriteSection(LastSecType,'',secorder_default,last_align);
  1451. writer.MarkEmpty;
  1452. end;
  1453. end;
  1454. ait_marker :
  1455. begin
  1456. {$ifdef DEBUG_AGGAS}
  1457. WriteStr(s,tai_marker(hp).Kind);
  1458. writer.AsmWriteLn('# ait_marker, kind: '+s);
  1459. {$endif DEBUG_AGGAS}
  1460. if tai_marker(hp).kind=mark_NoLineInfoStart then
  1461. inc(InlineLevel)
  1462. else if tai_marker(hp).kind=mark_NoLineInfoEnd then
  1463. dec(InlineLevel);
  1464. end;
  1465. ait_directive :
  1466. begin
  1467. WriteDirectiveName(tai_directive(hp).directive);
  1468. if tai_directive(hp).name <>'' then
  1469. begin
  1470. if replaceforbidden then
  1471. writer.AsmWrite(ApplyAsmSymbolRestrictions(tai_directive(hp).name))
  1472. else
  1473. writer.AsmWrite(tai_directive(hp).name);
  1474. end;
  1475. writer.AsmLn;
  1476. end;
  1477. ait_seh_directive :
  1478. begin
  1479. {$ifndef DISABLE_WIN64_SEH}
  1480. writer.AsmWrite(sehdirectivestr[tai_seh_directive(hp).kind]);
  1481. case tai_seh_directive(hp).datatype of
  1482. sd_none:;
  1483. sd_string:
  1484. begin
  1485. writer.AsmWrite(' '+tai_seh_directive(hp).data.name^);
  1486. if (tai_seh_directive(hp).data.flags and 1)<>0 then
  1487. writer.AsmWrite(',@except');
  1488. if (tai_seh_directive(hp).data.flags and 2)<>0 then
  1489. writer.AsmWrite(',@unwind');
  1490. end;
  1491. sd_reg:
  1492. writer.AsmWrite(' '+gas_regname(tai_seh_directive(hp).data.reg));
  1493. sd_offset:
  1494. writer.AsmWrite(' '+tostr(tai_seh_directive(hp).data.offset));
  1495. sd_regoffset:
  1496. writer.AsmWrite(' '+gas_regname(tai_seh_directive(hp).data.reg)+', '+
  1497. tostr(tai_seh_directive(hp).data.offset));
  1498. end;
  1499. writer.AsmLn;
  1500. {$endif DISABLE_WIN64_SEH}
  1501. end;
  1502. ait_cfi:
  1503. begin
  1504. WriteCFI(tai_cfi_base(hp));
  1505. end;
  1506. ait_eabi_attribute:
  1507. begin
  1508. { as of today, vasm does not support the eabi directives }
  1509. if target_asm.id<>as_arm_vasm then
  1510. begin
  1511. case tai_eabi_attribute(hp).eattr_typ of
  1512. eattrtype_dword:
  1513. writer.AsmWrite(#9'.eabi_attribute '+tostr(tai_eabi_attribute(hp).tag)+','+tostr(tai_eabi_attribute(hp).value));
  1514. eattrtype_ntbs:
  1515. begin
  1516. if assigned(tai_eabi_attribute(hp).valuestr) then
  1517. writer.AsmWrite(#9'.eabi_attribute '+tostr(tai_eabi_attribute(hp).tag)+',"'+tai_eabi_attribute(hp).valuestr^+'"')
  1518. else
  1519. writer.AsmWrite(#9'.eabi_attribute '+tostr(tai_eabi_attribute(hp).tag)+',""');
  1520. end
  1521. else
  1522. Internalerror(2019100601);
  1523. end;
  1524. writer.AsmLn;
  1525. end;
  1526. end;
  1527. {$ifdef WASM}
  1528. ait_local:
  1529. begin
  1530. if tai_local(hp).first then
  1531. writer.AsmWrite(#9'.local'#9)
  1532. else
  1533. writer.AsmWrite(', ');
  1534. writer.AsmWrite(gas_wasm_basic_type_str[tai_local(hp).bastyp]);
  1535. if tai_local(hp).last then
  1536. writer.AsmLn;
  1537. end;
  1538. ait_globaltype:
  1539. begin
  1540. writer.AsmWrite(#9'.globaltype'#9);
  1541. writer.AsmWrite(tai_globaltype(hp).globalname);
  1542. writer.AsmWrite(', ');
  1543. writer.AsmWrite(gas_wasm_basic_type_str[tai_globaltype(hp).gtype]);
  1544. if tai_globaltype(hp).immutable then
  1545. writer.AsmWrite(', immutable');
  1546. writer.AsmLn;
  1547. if tai_globaltype(hp).is_global then
  1548. begin
  1549. writer.AsmWrite(#9'.globl ');
  1550. writer.AsmWriteLn(tai_globaltype(hp).globalname);
  1551. end;
  1552. if not tai_globaltype(hp).is_external then
  1553. begin
  1554. writer.AsmWrite(tai_globaltype(hp).globalname);
  1555. writer.AsmWriteLn(':');
  1556. end;
  1557. end;
  1558. ait_functype:
  1559. WriteFuncTypeDirective(tai_functype(hp));
  1560. ait_export_name:
  1561. begin
  1562. writer.AsmWrite(#9'.export_name'#9);
  1563. writer.AsmWrite(tai_export_name(hp).intname);
  1564. writer.AsmWrite(', ');
  1565. writer.AsmWriteLn(tai_export_name(hp).extname);
  1566. end;
  1567. ait_tagtype:
  1568. WriteTagType(tai_tagtype(hp));
  1569. ait_import_module:
  1570. begin
  1571. writer.AsmWrite(#9'.import_module'#9);
  1572. writer.AsmWrite(tai_import_module(hp).symname);
  1573. writer.AsmWrite(', ');
  1574. writer.AsmWriteLn(tai_import_module(hp).importmodule);
  1575. end;
  1576. ait_import_name:
  1577. begin
  1578. writer.AsmWrite(#9'.import_name'#9);
  1579. writer.AsmWrite(tai_import_name(hp).symname);
  1580. writer.AsmWrite(', ');
  1581. writer.AsmWriteLn(tai_import_name(hp).importname);
  1582. end;
  1583. ait_wasm_structured_instruction:
  1584. begin
  1585. { What we output for these is not valid llvm-mc output
  1586. and we only print it for compiler debug purposes.
  1587. These shouldn't be present in the final asmlist. }
  1588. if hp is tai_wasmstruc_block then
  1589. begin
  1590. writer.AsmWriteLn('.err block {');
  1591. WriteTree(tai_wasmstruc_block(hp).inner_asmlist);
  1592. writer.AsmWriteLn('.err } end block');
  1593. end
  1594. else if hp is tai_wasmstruc_loop then
  1595. begin
  1596. writer.AsmWriteLn('.err loop {');
  1597. WriteTree(tai_wasmstruc_loop(hp).inner_asmlist);
  1598. writer.AsmWriteLn('.err } end loop');
  1599. end
  1600. else if hp is tai_wasmstruc_if then
  1601. begin
  1602. writer.AsmWriteLn('.err if {');
  1603. WriteTree(tai_wasmstruc_if(hp).then_asmlist);
  1604. writer.AsmWriteLn('.err } else {');
  1605. WriteTree(tai_wasmstruc_if(hp).else_asmlist);
  1606. writer.AsmWriteLn('.err } endif');
  1607. end
  1608. else if hp is tai_wasmstruc_try then
  1609. begin
  1610. writer.AsmWriteLn('.err try {');
  1611. WriteTree(tai_wasmstruc_try(hp).try_asmlist);
  1612. if hp is tai_wasmstruc_try_catch then
  1613. with tai_wasmstruc_try_catch(hp) do
  1614. begin
  1615. for i:=low(catch_list) to high(catch_list) do
  1616. begin
  1617. writer.AsmWriteLn('.err catch');
  1618. WriteTree(catch_list[i].asmlist);
  1619. end;
  1620. if assigned(catch_all_asmlist) then
  1621. begin
  1622. writer.AsmWriteLn('.err catch_all');
  1623. WriteTree(catch_all_asmlist);
  1624. end;
  1625. writer.AsmWriteLn('.err } end try');
  1626. end
  1627. else if hp is tai_wasmstruc_try_delegate then
  1628. writer.AsmWriteLn('.err } delegate')
  1629. else
  1630. writer.AsmWriteLn('.err unknown try structured instruction: ' + hp.ClassType.ClassName);
  1631. end
  1632. else
  1633. writer.AsmWriteLn('.err structured instruction: ' + hp.ClassType.ClassName);
  1634. end;
  1635. {$endif WASM}
  1636. else
  1637. if not WriteComments(hp) then
  1638. internalerror(2006012201);
  1639. end;
  1640. lasthp:=hp;
  1641. hp:=tai(hp.next);
  1642. end;
  1643. end;
  1644. procedure TGNUAssembler.WriteExtraHeader;
  1645. begin
  1646. end;
  1647. procedure TGNUAssembler.WriteExtraFooter;
  1648. begin
  1649. end;
  1650. procedure TGNUAssembler.WriteInstruction(hp: tai);
  1651. begin
  1652. InstrWriter.WriteInstruction(hp);
  1653. end;
  1654. procedure TGNUAssembler.WriteWeakSymbolRef(s: tasmsymbol);
  1655. begin
  1656. writer.AsmWrite(#9'.weak ');
  1657. if asminfo^.dollarsign='$' then
  1658. writer.AsmWriteLn(s.name)
  1659. else
  1660. writer.AsmWriteLn(ApplyAsmSymbolRestrictions(s.name))
  1661. end;
  1662. procedure TGNUAssembler.WriteHiddenSymbol(sym: TAsmSymbol);
  1663. begin
  1664. { on Windows/(PE)COFF, global symbols are hidden by default: global
  1665. symbols that are not explicitly exported from an executable/library,
  1666. become hidden }
  1667. if (target_info.system in (systems_windows+systems_wince+systems_nativent)) then
  1668. exit;
  1669. if target_info.system in systems_darwin then
  1670. writer.AsmWrite(#9'.private_extern ')
  1671. else
  1672. writer.AsmWrite(#9'.hidden ');
  1673. if asminfo^.dollarsign='$' then
  1674. writer.AsmWriteLn(sym.name)
  1675. else
  1676. writer.AsmWriteLn(ApplyAsmSymbolRestrictions(sym.name))
  1677. end;
  1678. procedure TGNUAssembler.WriteAixStringConst(hp: tai_string);
  1679. type
  1680. tterminationkind = (term_none,term_string,term_nostring);
  1681. var
  1682. i: longint;
  1683. pos: longint;
  1684. s: string;
  1685. ch: char;
  1686. instring: boolean;
  1687. procedure newstatement(terminationkind: tterminationkind);
  1688. begin
  1689. case terminationkind of
  1690. term_none: ;
  1691. term_string:
  1692. writer.AsmWriteLn('"');
  1693. term_nostring:
  1694. writer.AsmLn;
  1695. end;
  1696. writer.AsmWrite(#9'.byte'#9);
  1697. pos:=20;
  1698. instring:=false;
  1699. end;
  1700. begin
  1701. pos:=0;
  1702. instring:=false;
  1703. for i:=1 to hp.len do
  1704. begin
  1705. if pos=0 then
  1706. newstatement(term_none);
  1707. ch:=hp.str[i-1];
  1708. case ch of
  1709. #0..#31,
  1710. #127..#255 :
  1711. begin
  1712. if instring then
  1713. newstatement(term_string);
  1714. if pos=20 then
  1715. s:=tostr(ord(ch))
  1716. else
  1717. s:=', '+tostr(ord(ch))
  1718. end;
  1719. '"' :
  1720. if instring then
  1721. s:='""'
  1722. else
  1723. begin
  1724. if pos<>20 then
  1725. newstatement(term_nostring);
  1726. s:='"""';
  1727. instring:=true;
  1728. end;
  1729. else
  1730. if not instring then
  1731. begin
  1732. if (pos<>20) then
  1733. newstatement(term_nostring);
  1734. s:='"'+ch;
  1735. instring:=true;
  1736. end
  1737. else
  1738. s:=ch;
  1739. end;
  1740. writer.AsmWrite(s);
  1741. inc(pos,length(s));
  1742. if (pos>line_length) or (i=tai_string(hp).len) then
  1743. begin
  1744. if instring then
  1745. writer.AsmWriteLn('"')
  1746. else
  1747. writer.AsmLn;
  1748. pos:=0;
  1749. end;
  1750. end;
  1751. end;
  1752. procedure TGNUAssembler.WriteAixIntConst(hp: tai_const);
  1753. var
  1754. pos, size: longint;
  1755. begin
  1756. { only big endian AIX supported for now }
  1757. if target_info.endian<>endian_big then
  1758. internalerror(2012010401);
  1759. { limitation: can only write 4 bytes at a time }
  1760. pos:=0;
  1761. size:=tai_const(hp).size;
  1762. while pos<(size-4) do
  1763. begin
  1764. writer.AsmWrite(#9'.vbyte'#9'4, ');
  1765. writer.AsmWriteln(tostr(longint(tai_const(hp).value shr ((size-pos-4)*8))));
  1766. inc(pos,4);
  1767. end;
  1768. writer.AsmWrite(#9'.vbyte'#9);
  1769. writer.AsmWrite(tostr(size-pos));
  1770. writer.AsmWrite(', ');
  1771. case size-pos of
  1772. 1: writer.AsmWrite(tostr(byte(tai_const(hp).value)));
  1773. 2: writer.AsmWrite(tostr(word(tai_const(hp).value)));
  1774. 4: writer.AsmWrite(tostr(longint(tai_const(hp).value)));
  1775. else
  1776. internalerror(2012010402);
  1777. end;
  1778. end;
  1779. procedure TGNUAssembler.WriteUnalignedIntConst(hp: tai_const);
  1780. var
  1781. pos, size: longint;
  1782. begin
  1783. size:=tai_const(hp).size;
  1784. writer.AsmWrite(#9'.byte'#9);
  1785. if target_info.endian=endian_big then
  1786. begin
  1787. pos:=size-1;
  1788. while pos>=0 do
  1789. begin
  1790. writer.AsmWrite(tostr((tai_const(hp).value shr (pos*8)) and $ff));
  1791. dec(pos);
  1792. if pos>=0 then
  1793. writer.AsmWrite(', ')
  1794. else
  1795. writer.AsmLn;
  1796. end;
  1797. end
  1798. else
  1799. begin
  1800. pos:=0;
  1801. while pos<size do
  1802. begin
  1803. writer.AsmWriteln(tostr((tai_const(hp).value shr (pos*8)) and $ff));
  1804. inc(pos);
  1805. if pos<=size then
  1806. writer.AsmWrite(', ')
  1807. else
  1808. writer.AsmLn;
  1809. end;
  1810. end;
  1811. writer.AsmLn;
  1812. end;
  1813. procedure TGNUAssembler.WriteDirectiveName(dir: TAsmDirective);
  1814. begin
  1815. { TODO: implement asd_cpu for GAS => usually .arch or .cpu, but the CPU
  1816. name has to be translated as well }
  1817. if dir=asd_cpu then
  1818. writer.AsmWrite(asminfo^.comment+' CPU ')
  1819. else
  1820. writer.AsmWrite('.'+directivestr[dir]+' ');
  1821. end;
  1822. procedure TGNUAssembler.WriteAsmList;
  1823. var
  1824. n : string;
  1825. hal : tasmlisttype;
  1826. i: longint;
  1827. begin
  1828. {$ifdef EXTDEBUG}
  1829. if current_module.mainsource<>'' then
  1830. Comment(V_Debug,'Start writing gas-styled assembler output for '+current_module.mainsource);
  1831. {$endif}
  1832. if current_module.mainsource<>'' then
  1833. n:=ExtractFileName(current_module.mainsource)
  1834. else
  1835. n:=InputFileName;
  1836. { gcc does not add it either for Darwin. Grep for
  1837. TARGET_ASM_FILE_START_FILE_DIRECTIVE in gcc/config/*.h
  1838. }
  1839. if not(target_info.system in systems_darwin) then
  1840. writer.AsmWriteLn(#9'.file "'+FixFileName(n)+'"');
  1841. WriteExtraHeader;
  1842. writer.MarkEmpty;
  1843. symendcount:=0;
  1844. for hal:=low(TasmlistType) to high(TasmlistType) do
  1845. begin
  1846. if not (current_asmdata.asmlists[hal].empty) then
  1847. begin
  1848. writer.AsmWriteLn(asminfo^.comment+'Begin asmlist '+AsmlistTypeStr[hal]);
  1849. writetree(current_asmdata.asmlists[hal]);
  1850. writer.AsmWriteLn(asminfo^.comment+'End asmlist '+AsmlistTypeStr[hal]);
  1851. end;
  1852. end;
  1853. { add weak symbol markers }
  1854. for i:=0 to current_asmdata.asmsymboldict.count-1 do
  1855. if (tasmsymbol(current_asmdata.asmsymboldict[i]).bind=AB_WEAK_EXTERNAL) then
  1856. WriteWeakSymbolRef(tasmsymbol(current_asmdata.asmsymboldict[i]));
  1857. if create_smartlink_sections and
  1858. (target_info.system in systems_darwin) then
  1859. writer.AsmWriteLn(#9'.subsections_via_symbols');
  1860. { "no executable stack" marker }
  1861. { TODO: used by OpenBSD/NetBSD as well? }
  1862. if (target_info.system in (systems_linux + systems_android + systems_freebsd + systems_dragonfly)) and
  1863. not(cs_executable_stack in current_settings.moduleswitches) then
  1864. begin
  1865. writer.AsmWriteLn('.section .note.GNU-stack,"",%progbits');
  1866. end;
  1867. writer.AsmLn;
  1868. WriteExtraFooter;
  1869. {$ifdef EXTDEBUG}
  1870. if current_module.mainsource<>'' then
  1871. Comment(V_Debug,'Done writing gas-styled assembler output for '+current_module.mainsource);
  1872. {$endif EXTDEBUG}
  1873. end;
  1874. {****************************************************************************}
  1875. { Apple/GNU Assembler writer }
  1876. {****************************************************************************}
  1877. function TAppleGNUAssembler.sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;
  1878. begin
  1879. if (target_info.system in systems_darwin) then
  1880. case atype of
  1881. sec_user:
  1882. begin
  1883. result:='.section '+aname;
  1884. exit;
  1885. end;
  1886. sec_bss:
  1887. { all bss (lcomm) symbols are automatically put in the right }
  1888. { place by using the lcomm assembler directive }
  1889. atype := sec_none;
  1890. sec_debug_frame,
  1891. sec_eh_frame:
  1892. begin
  1893. result := '.section __DWARF,__debug_info,regular,debug';
  1894. exit;
  1895. end;
  1896. sec_debug_line:
  1897. begin
  1898. result := '.section __DWARF,__debug_line,regular,debug';
  1899. exit;
  1900. end;
  1901. sec_debug_info:
  1902. begin
  1903. result := '.section __DWARF,__debug_info,regular,debug';
  1904. exit;
  1905. end;
  1906. sec_debug_abbrev:
  1907. begin
  1908. result := '.section __DWARF,__debug_abbrev,regular,debug';
  1909. exit;
  1910. end;
  1911. sec_debug_aranges:
  1912. begin
  1913. result := '.section __DWARF,__debug_aranges,regular,debug';
  1914. exit;
  1915. end;
  1916. sec_debug_ranges:
  1917. begin
  1918. result := '.section __DWARF,__debug_ranges,regular,debug';
  1919. exit;
  1920. end;
  1921. sec_rodata:
  1922. begin
  1923. result := '.const_data';
  1924. exit;
  1925. end;
  1926. sec_rodata_norel:
  1927. begin
  1928. result := '.const';
  1929. exit;
  1930. end;
  1931. sec_fpc:
  1932. begin
  1933. result := '.section __TEXT, .fpc, regular, no_dead_strip';
  1934. exit;
  1935. end;
  1936. sec_code:
  1937. begin
  1938. if (aname='fpc_geteipasebx') or
  1939. (aname='fpc_geteipasecx') then
  1940. begin
  1941. result:='.section __TEXT,__textcoal_nt,coalesced,pure_instructions'#10'.weak_definition '+aname+
  1942. #10'.private_extern '+aname;
  1943. exit;
  1944. end;
  1945. end;
  1946. sec_data_nonlazy:
  1947. begin
  1948. result:='.section __DATA, __nl_symbol_ptr,non_lazy_symbol_pointers';
  1949. exit;
  1950. end;
  1951. sec_data_lazy:
  1952. begin
  1953. result:='.section __DATA, __la_symbol_ptr,lazy_symbol_pointers';
  1954. exit;
  1955. end;
  1956. sec_init_func:
  1957. begin
  1958. result:='.section __DATA, __mod_init_func, mod_init_funcs';
  1959. exit;
  1960. end;
  1961. sec_term_func:
  1962. begin
  1963. result:='.section __DATA, __mod_term_func, mod_term_funcs';
  1964. exit;
  1965. end;
  1966. low(TObjCAsmSectionType)..high(TObjCAsmSectionType):
  1967. begin
  1968. result:='.section '+objc_section_name(atype);
  1969. exit
  1970. end;
  1971. else
  1972. ;
  1973. end;
  1974. result := inherited sectionname(atype,aname,aorder);
  1975. end;
  1976. procedure TAppleGNUAssembler.WriteWeakSymbolRef(s: tasmsymbol);
  1977. begin
  1978. writer.AsmWriteLn(#9'.weak_reference '+s.name);
  1979. end;
  1980. procedure TAppleGNUAssembler.WriteDirectiveName(dir: TAsmDirective);
  1981. begin
  1982. case dir of
  1983. asd_weak_reference:
  1984. writer.AsmWrite('.weak_reference ');
  1985. asd_weak_definition:
  1986. writer.AsmWrite('.weak_definition ');
  1987. else
  1988. inherited;
  1989. end;
  1990. end;
  1991. {****************************************************************************}
  1992. { a.out/GNU Assembler writer }
  1993. {****************************************************************************}
  1994. function TAoutGNUAssembler.sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;
  1995. const
  1996. (* Translation table - replace unsupported section types with basic ones. *)
  1997. SecXTable: array[TAsmSectionType] of TAsmSectionType = (
  1998. sec_none,
  1999. sec_none,
  2000. sec_code,
  2001. sec_data,
  2002. sec_data (* sec_rodata *),
  2003. sec_data (* sec_rodata_norel *),
  2004. sec_bss,
  2005. sec_data (* sec_threadvar *),
  2006. { used for wince exception handling }
  2007. sec_code (* sec_pdata *),
  2008. { used for darwin import stubs }
  2009. sec_code (* sec_stub *),
  2010. sec_data,(* sec_data_nonlazy *)
  2011. sec_data,(* sec_data_lazy *)
  2012. sec_data,(* sec_init_func *)
  2013. sec_data,(* sec_term_func *)
  2014. { stabs }
  2015. sec_stab,sec_stabstr,
  2016. { win32 }
  2017. sec_data (* sec_idata2 *),
  2018. sec_data (* sec_idata4 *),
  2019. sec_data (* sec_idata5 *),
  2020. sec_data (* sec_idata6 *),
  2021. sec_data (* sec_idata7 *),
  2022. sec_data (* sec_edata *),
  2023. { C++ exception handling unwinding (uses dwarf) }
  2024. sec_eh_frame,
  2025. { dwarf }
  2026. sec_debug_frame,
  2027. sec_debug_info,
  2028. sec_debug_line,
  2029. sec_debug_abbrev,
  2030. sec_debug_aranges,
  2031. sec_debug_ranges,
  2032. sec_debug_loc,
  2033. sec_debug_loclists,
  2034. { ELF resources (+ references to stabs debug information sections) }
  2035. sec_code (* sec_fpc *),
  2036. { Table of contents section }
  2037. sec_code (* sec_toc *),
  2038. sec_code (* sec_init *),
  2039. sec_code (* sec_fini *),
  2040. sec_none (* sec_objc_class *),
  2041. sec_none (* sec_objc_meta_class *),
  2042. sec_none (* sec_objc_cat_cls_meth *),
  2043. sec_none (* sec_objc_cat_inst_meth *),
  2044. sec_none (* sec_objc_protocol *),
  2045. sec_none (* sec_objc_string_object *),
  2046. sec_none (* sec_objc_cls_meth *),
  2047. sec_none (* sec_objc_inst_meth *),
  2048. sec_none (* sec_objc_cls_refs *),
  2049. sec_none (* sec_objc_message_refs *),
  2050. sec_none (* sec_objc_symbols *),
  2051. sec_none (* sec_objc_category *),
  2052. sec_none (* sec_objc_class_vars *),
  2053. sec_none (* sec_objc_instance_vars *),
  2054. sec_none (* sec_objc_module_info *),
  2055. sec_none (* sec_objc_class_names *),
  2056. sec_none (* sec_objc_meth_var_types *),
  2057. sec_none (* sec_objc_meth_var_names *),
  2058. sec_none (* sec_objc_selector_strs *),
  2059. sec_none (* sec_objc_protocol_ext *),
  2060. sec_none (* sec_objc_class_ext *),
  2061. sec_none (* sec_objc_property *),
  2062. sec_none (* sec_objc_image_info *),
  2063. sec_none (* sec_objc_cstring_object *),
  2064. sec_none (* sec_objc_sel_fixup *),
  2065. sec_none (* sec_objc_data *),
  2066. sec_none (* sec_objc_const *),
  2067. sec_none (* sec_objc_sup_refs *),
  2068. sec_none (* sec_data_coalesced *),
  2069. sec_none (* sec_objc_classlist *),
  2070. sec_none (* sec_objc_nlclasslist *),
  2071. sec_none (* sec_objc_catlist *),
  2072. sec_none (* sec_objc_nlcatlist *),
  2073. sec_none (* sec_objc_protlist *),
  2074. sec_none (* sec_stack *),
  2075. sec_none (* sec_heap *),
  2076. sec_none (* gcc_except_table *),
  2077. sec_none (* sec_arm_attribute *)
  2078. );
  2079. begin
  2080. Result := inherited SectionName (SecXTable [AType], AName, AOrder);
  2081. end;
  2082. {****************************************************************************}
  2083. { Abstract Instruction Writer }
  2084. {****************************************************************************}
  2085. constructor TCPUInstrWriter.create(_owner: TGNUAssembler);
  2086. begin
  2087. inherited create;
  2088. owner := _owner;
  2089. end;
  2090. end.