aggas.pas 87 KB

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