aggas.pas 83 KB

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