aggas.pas 85 KB

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