aggas.pas 82 KB

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