aggas.pas 82 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174
  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. if (tai_label(hp).labsym.is_used) then
  1211. begin
  1212. {$ifdef DEBUG_LABEL}
  1213. writer.AsmWrite(asminfo^.comment);
  1214. writer.AsmWriteLn('References = ' + tostr(tai_label(hp).labsym.getrefs));
  1215. {$endif DEBUG_LABEL}
  1216. if tai_label(hp).labsym.bind in [AB_GLOBAL,AB_PRIVATE_EXTERN] then
  1217. begin
  1218. if (tai_label(hp).labsym.bind=AB_PRIVATE_EXTERN) then
  1219. begin
  1220. writer.AsmWrite(#9'.private_extern ');
  1221. writer.AsmWriteln(tai_label(hp).labsym.name);
  1222. end;
  1223. {$ifdef arm}
  1224. { do no change arm mode accidently, .globl seems to reset the mode }
  1225. if GenerateThumbCode or GenerateThumb2Code then
  1226. writer.AsmWriteln(#9'.thumb_func'#9);
  1227. {$endif arm}
  1228. writer.AsmWrite('.globl'#9);
  1229. if replaceforbidden then
  1230. writer.AsmWriteLn(ApplyAsmSymbolRestrictions(tai_label(hp).labsym.name))
  1231. else
  1232. writer.AsmWriteLn(tai_label(hp).labsym.name);
  1233. end;
  1234. if replaceforbidden then
  1235. writer.AsmWrite(ApplyAsmSymbolRestrictions(tai_label(hp).labsym.name))
  1236. else
  1237. writer.AsmWrite(tai_label(hp).labsym.name);
  1238. writer.AsmWriteLn(':');
  1239. end;
  1240. end;
  1241. ait_symbol :
  1242. begin
  1243. if (target_info.system=system_powerpc64_linux) and
  1244. (tai_symbol(hp).sym.typ=AT_FUNCTION) and
  1245. (cs_profile in current_settings.moduleswitches) then
  1246. writer.AsmWriteLn('.globl _mcount');
  1247. if tai_symbol(hp).is_global then
  1248. begin
  1249. writer.AsmWrite('.globl'#9);
  1250. if replaceforbidden then
  1251. writer.AsmWriteln(ApplyAsmSymbolRestrictions(tai_symbol(hp).sym.name))
  1252. else
  1253. writer.AsmWriteln(tai_symbol(hp).sym.name);
  1254. if (tai_symbol(hp).sym.bind=AB_PRIVATE_EXTERN) then
  1255. WriteHiddenSymbol(tai_symbol(hp).sym);
  1256. end;
  1257. if (target_info.system=system_powerpc64_linux) and
  1258. use_dotted_functions and
  1259. (tai_symbol(hp).sym.typ=AT_FUNCTION) then
  1260. begin
  1261. writer.AsmWriteLn('.section ".opd", "aw"');
  1262. writer.AsmWriteLn('.align 3');
  1263. writer.AsmWriteLn(tai_symbol(hp).sym.name + ':');
  1264. writer.AsmWriteLn('.quad .' + tai_symbol(hp).sym.name + ', .TOC.@tocbase, 0');
  1265. writer.AsmWriteLn('.previous');
  1266. writer.AsmWriteLn('.size ' + tai_symbol(hp).sym.name + ', 24');
  1267. if (tai_symbol(hp).is_global) then
  1268. writer.AsmWriteLn('.globl .' + tai_symbol(hp).sym.name);
  1269. writer.AsmWriteLn('.type .' + tai_symbol(hp).sym.name + ', @function');
  1270. { the dotted name is the name of the actual function entry }
  1271. writer.AsmWrite('.');
  1272. end
  1273. else if (target_info.system in systems_aix) and
  1274. (tai_symbol(hp).sym.typ = AT_FUNCTION) then
  1275. begin
  1276. if target_info.system=system_powerpc_aix then
  1277. begin
  1278. s:=#9'.long .';
  1279. ch:='2';
  1280. end
  1281. else
  1282. begin
  1283. s:=#9'.llong .';
  1284. ch:='3';
  1285. end;
  1286. writer.AsmWriteLn(#9'.csect '+ApplyAsmSymbolRestrictions(tai_symbol(hp).sym.name)+'[DS],'+ch);
  1287. writer.AsmWriteLn(ApplyAsmSymbolRestrictions(tai_symbol(hp).sym.name)+':');
  1288. writer.AsmWriteln(s+ApplyAsmSymbolRestrictions(tai_symbol(hp).sym.name)+', TOC[tc0], 0');
  1289. writer.AsmWriteln(#9'.csect .text[PR]');
  1290. if (tai_symbol(hp).is_global) then
  1291. writer.AsmWriteLn('.globl .'+ApplyAsmSymbolRestrictions(tai_symbol(hp).sym.name))
  1292. else
  1293. writer.AsmWriteLn('.lglobl .'+ApplyAsmSymbolRestrictions(tai_symbol(hp).sym.name));
  1294. { the dotted name is the name of the actual function entry }
  1295. writer.AsmWrite('.');
  1296. end
  1297. else if tai_symbol(hp).sym.typ=AT_WASM_EXCEPTION_TAG then
  1298. begin
  1299. { nothing here, to ensure we don' write the .type directive for exception tags }
  1300. end
  1301. else
  1302. begin
  1303. if ((target_info.system <> system_arm_linux) and (target_info.system <> system_arm_android)) or
  1304. (target_asm.id=as_arm_vasm) then
  1305. sepChar := '@'
  1306. else
  1307. sepChar := '#';
  1308. if (tf_needs_symbol_type in target_info.flags) then
  1309. begin
  1310. writer.AsmWrite(#9'.type'#9 + tai_symbol(hp).sym.name);
  1311. if (needsObject(tai_symbol(hp))) then
  1312. writer.AsmWriteLn(',' + sepChar + 'object')
  1313. else
  1314. writer.AsmWriteLn(',' + sepChar + 'function');
  1315. end;
  1316. end;
  1317. if replaceforbidden then
  1318. if not(tai_symbol(hp).has_value) then
  1319. writer.AsmWriteLn(ApplyAsmSymbolRestrictions(tai_symbol(hp).sym.name + ':'))
  1320. else
  1321. writer.AsmWriteLn(ApplyAsmSymbolRestrictions(tai_symbol(hp).sym.name + '=' + tostr(tai_symbol(hp).value)))
  1322. else if not(tai_symbol(hp).has_value) then
  1323. writer.AsmWriteLn(tai_symbol(hp).sym.name + ':')
  1324. else
  1325. writer.AsmWriteLn(tai_symbol(hp).sym.name + '=' + tostr(tai_symbol(hp).value));
  1326. end;
  1327. ait_symbolpair:
  1328. begin
  1329. writer.AsmWrite(#9);
  1330. writer.AsmWrite(symbolpairkindstr[tai_symbolpair(hp).kind]);
  1331. writer.AsmWrite(' ');
  1332. if tai_symbolpair(hp).kind<>spk_localentry then
  1333. s:=', '
  1334. else
  1335. { the .localentry directive has to specify the size from the
  1336. start till here of the non-local entry code as second argument }
  1337. s:=', .-';
  1338. if ((target_info.system <> system_arm_linux) and (target_info.system <> system_arm_android)) then
  1339. sepChar := '@'
  1340. else
  1341. sepChar := '#';
  1342. if replaceforbidden then
  1343. begin
  1344. { avoid string truncation }
  1345. writer.AsmWrite(ApplyAsmSymbolRestrictions(tai_symbolpair(hp).sym^));
  1346. writer.AsmWrite(s);
  1347. writer.AsmWriteLn(ApplyAsmSymbolRestrictions(tai_symbolpair(hp).value^));
  1348. if tai_symbolpair(hp).kind=spk_set_global then
  1349. begin
  1350. writer.AsmWrite(#9'.globl ');
  1351. writer.AsmWriteLn(ApplyAsmSymbolRestrictions(tai_symbolpair(hp).sym^));
  1352. end;
  1353. if (tf_needs_symbol_type in target_info.flags) then
  1354. begin
  1355. writer.AsmWrite(#9'.type'#9 + ApplyAsmSymbolRestrictions(tai_symbolpair(hp).sym^));
  1356. writer.AsmWriteLn(',' + sepChar + 'function');
  1357. end;
  1358. end
  1359. else
  1360. begin
  1361. { avoid string truncation }
  1362. writer.AsmWrite(tai_symbolpair(hp).sym^);
  1363. writer.AsmWrite(s);
  1364. writer.AsmWriteLn(tai_symbolpair(hp).value^);
  1365. if tai_symbolpair(hp).kind=spk_set_global then
  1366. begin
  1367. writer.AsmWrite(#9'.globl ');
  1368. writer.AsmWriteLn(tai_symbolpair(hp).sym^);
  1369. end;
  1370. if (tf_needs_symbol_type in target_info.flags) then
  1371. begin
  1372. writer.AsmWrite(#9'.type'#9 + tai_symbolpair(hp).sym^);
  1373. writer.AsmWriteLn(',' + sepChar + 'function');
  1374. end;
  1375. end;
  1376. end;
  1377. ait_symbol_end :
  1378. begin
  1379. if tf_needs_symbol_size in target_info.flags then
  1380. begin
  1381. s:=asminfo^.labelprefix+'e'+tostr(symendcount);
  1382. inc(symendcount);
  1383. writer.AsmWriteLn(s+':');
  1384. writer.AsmWrite(#9'.size'#9);
  1385. if (target_info.system=system_powerpc64_linux) and
  1386. use_dotted_functions and
  1387. (tai_symbol_end(hp).sym.typ=AT_FUNCTION) then
  1388. writer.AsmWrite('.');
  1389. if replaceforbidden then
  1390. writer.AsmWrite(ApplyAsmSymbolRestrictions(tai_symbol_end(hp).sym.name))
  1391. else
  1392. writer.AsmWrite(tai_symbol_end(hp).sym.name);
  1393. writer.AsmWrite(', '+s+' - ');
  1394. if (target_info.system=system_powerpc64_linux) and
  1395. use_dotted_functions and
  1396. (tai_symbol_end(hp).sym.typ=AT_FUNCTION) then
  1397. writer.AsmWrite('.');
  1398. if replaceforbidden then
  1399. writer.AsmWriteLn(ApplyAsmSymbolRestrictions(tai_symbol_end(hp).sym.name))
  1400. else
  1401. writer.AsmWriteLn(tai_symbol_end(hp).sym.name);
  1402. end;
  1403. end;
  1404. ait_instruction :
  1405. begin
  1406. WriteInstruction(hp);
  1407. end;
  1408. ait_stab :
  1409. begin
  1410. if assigned(tai_stab(hp).str) then
  1411. begin
  1412. writer.AsmWrite(#9'.'+stabtypestr[tai_stab(hp).stabtype]+' ');
  1413. writer.AsmWritePChar(tai_stab(hp).str);
  1414. writer.AsmLn;
  1415. end;
  1416. end;
  1417. ait_force_line,
  1418. ait_function_name :
  1419. begin
  1420. {$ifdef DEBUG_AGGAS}
  1421. WriteStr(s,hp.typ);
  1422. writer.AsmWriteLn('# '+s);
  1423. {$endif DEBUG_AGGAS}
  1424. end;
  1425. ait_cutobject :
  1426. begin
  1427. {$ifdef DEBUG_AGGAS}
  1428. writer.AsmWriteLn('# ait_cutobject');
  1429. {$endif DEBUG_AGGAS}
  1430. if SmartAsm then
  1431. begin
  1432. { only reset buffer if nothing has changed }
  1433. if not(writer.ClearIfEmpty) then
  1434. begin
  1435. writer.AsmClose;
  1436. DoAssemble;
  1437. writer.AsmCreate(tai_cutobject(hp).place);
  1438. end;
  1439. { avoid empty files }
  1440. while assigned(hp.next) and (tai(hp.next).typ in [ait_cutobject,ait_section,ait_comment]) do
  1441. begin
  1442. if tai(hp.next).typ=ait_section then
  1443. LastSecType:=tai_section(hp.next).sectype;
  1444. hp:=tai(hp.next);
  1445. end;
  1446. if LastSecType<>sec_none then
  1447. WriteSection(LastSecType,'',secorder_default,last_align);
  1448. writer.MarkEmpty;
  1449. end;
  1450. end;
  1451. ait_marker :
  1452. begin
  1453. {$ifdef DEBUG_AGGAS}
  1454. WriteStr(s,tai_marker(hp).Kind);
  1455. writer.AsmWriteLn('# ait_marker, kind: '+s);
  1456. {$endif DEBUG_AGGAS}
  1457. if tai_marker(hp).kind=mark_NoLineInfoStart then
  1458. inc(InlineLevel)
  1459. else if tai_marker(hp).kind=mark_NoLineInfoEnd then
  1460. dec(InlineLevel);
  1461. end;
  1462. ait_directive :
  1463. begin
  1464. WriteDirectiveName(tai_directive(hp).directive);
  1465. if tai_directive(hp).name <>'' then
  1466. begin
  1467. if replaceforbidden then
  1468. writer.AsmWrite(ApplyAsmSymbolRestrictions(tai_directive(hp).name))
  1469. else
  1470. writer.AsmWrite(tai_directive(hp).name);
  1471. end;
  1472. writer.AsmLn;
  1473. end;
  1474. ait_seh_directive :
  1475. begin
  1476. {$ifndef DISABLE_WIN64_SEH}
  1477. writer.AsmWrite(sehdirectivestr[tai_seh_directive(hp).kind]);
  1478. case tai_seh_directive(hp).datatype of
  1479. sd_none:;
  1480. sd_string:
  1481. begin
  1482. writer.AsmWrite(' '+tai_seh_directive(hp).data.name^);
  1483. if (tai_seh_directive(hp).data.flags and 1)<>0 then
  1484. writer.AsmWrite(',@except');
  1485. if (tai_seh_directive(hp).data.flags and 2)<>0 then
  1486. writer.AsmWrite(',@unwind');
  1487. end;
  1488. sd_reg:
  1489. writer.AsmWrite(' '+gas_regname(tai_seh_directive(hp).data.reg));
  1490. sd_offset:
  1491. writer.AsmWrite(' '+tostr(tai_seh_directive(hp).data.offset));
  1492. sd_regoffset:
  1493. writer.AsmWrite(' '+gas_regname(tai_seh_directive(hp).data.reg)+', '+
  1494. tostr(tai_seh_directive(hp).data.offset));
  1495. end;
  1496. writer.AsmLn;
  1497. {$endif DISABLE_WIN64_SEH}
  1498. end;
  1499. ait_cfi:
  1500. begin
  1501. WriteCFI(tai_cfi_base(hp));
  1502. end;
  1503. ait_eabi_attribute:
  1504. begin
  1505. { as of today, vasm does not support the eabi directives }
  1506. if target_asm.id<>as_arm_vasm then
  1507. begin
  1508. case tai_eabi_attribute(hp).eattr_typ of
  1509. eattrtype_dword:
  1510. writer.AsmWrite(#9'.eabi_attribute '+tostr(tai_eabi_attribute(hp).tag)+','+tostr(tai_eabi_attribute(hp).value));
  1511. eattrtype_ntbs:
  1512. begin
  1513. if assigned(tai_eabi_attribute(hp).valuestr) then
  1514. writer.AsmWrite(#9'.eabi_attribute '+tostr(tai_eabi_attribute(hp).tag)+',"'+tai_eabi_attribute(hp).valuestr^+'"')
  1515. else
  1516. writer.AsmWrite(#9'.eabi_attribute '+tostr(tai_eabi_attribute(hp).tag)+',""');
  1517. end
  1518. else
  1519. Internalerror(2019100601);
  1520. end;
  1521. writer.AsmLn;
  1522. end;
  1523. end;
  1524. {$ifdef WASM}
  1525. ait_local:
  1526. begin
  1527. if tai_local(hp).first then
  1528. writer.AsmWrite(#9'.local'#9)
  1529. else
  1530. writer.AsmWrite(', ');
  1531. writer.AsmWrite(gas_wasm_basic_type_str[tai_local(hp).bastyp]);
  1532. if tai_local(hp).last then
  1533. writer.AsmLn;
  1534. end;
  1535. ait_globaltype:
  1536. begin
  1537. writer.AsmWrite(#9'.globaltype'#9);
  1538. writer.AsmWrite(tai_globaltype(hp).globalname);
  1539. writer.AsmWrite(', ');
  1540. writer.AsmWrite(gas_wasm_basic_type_str[tai_globaltype(hp).gtype]);
  1541. if tai_globaltype(hp).immutable then
  1542. writer.AsmWrite(', immutable');
  1543. writer.AsmLn;
  1544. end;
  1545. ait_functype:
  1546. WriteFuncTypeDirective(tai_functype(hp));
  1547. ait_export_name:
  1548. begin
  1549. writer.AsmWrite(#9'.export_name'#9);
  1550. writer.AsmWrite(tai_export_name(hp).intname);
  1551. writer.AsmWrite(', ');
  1552. writer.AsmWriteLn(tai_export_name(hp).extname);
  1553. end;
  1554. ait_tagtype:
  1555. WriteTagType(tai_tagtype(hp));
  1556. ait_import_module:
  1557. begin
  1558. writer.AsmWrite(#9'.import_module'#9);
  1559. writer.AsmWrite(tai_import_module(hp).symname);
  1560. writer.AsmWrite(', ');
  1561. writer.AsmWriteLn(tai_import_module(hp).importmodule);
  1562. end;
  1563. ait_import_name:
  1564. begin
  1565. writer.AsmWrite(#9'.import_name'#9);
  1566. writer.AsmWrite(tai_import_name(hp).symname);
  1567. writer.AsmWrite(', ');
  1568. writer.AsmWriteLn(tai_import_name(hp).importname);
  1569. end;
  1570. {$endif WASM}
  1571. else
  1572. if not WriteComments(hp) then
  1573. internalerror(2006012201);
  1574. end;
  1575. lasthp:=hp;
  1576. hp:=tai(hp.next);
  1577. end;
  1578. end;
  1579. procedure TGNUAssembler.WriteExtraHeader;
  1580. begin
  1581. end;
  1582. procedure TGNUAssembler.WriteExtraFooter;
  1583. begin
  1584. end;
  1585. procedure TGNUAssembler.WriteInstruction(hp: tai);
  1586. begin
  1587. InstrWriter.WriteInstruction(hp);
  1588. end;
  1589. procedure TGNUAssembler.WriteWeakSymbolRef(s: tasmsymbol);
  1590. begin
  1591. writer.AsmWrite(#9'.weak ');
  1592. if asminfo^.dollarsign='$' then
  1593. writer.AsmWriteLn(s.name)
  1594. else
  1595. writer.AsmWriteLn(ApplyAsmSymbolRestrictions(s.name))
  1596. end;
  1597. procedure TGNUAssembler.WriteHiddenSymbol(sym: TAsmSymbol);
  1598. begin
  1599. { on Windows/(PE)COFF, global symbols are hidden by default: global
  1600. symbols that are not explicitly exported from an executable/library,
  1601. become hidden }
  1602. if (target_info.system in (systems_windows+systems_wince)) then
  1603. exit;
  1604. if target_info.system in systems_darwin then
  1605. writer.AsmWrite(#9'.private_extern ')
  1606. else
  1607. writer.AsmWrite(#9'.hidden ');
  1608. if asminfo^.dollarsign='$' then
  1609. writer.AsmWriteLn(sym.name)
  1610. else
  1611. writer.AsmWriteLn(ApplyAsmSymbolRestrictions(sym.name))
  1612. end;
  1613. procedure TGNUAssembler.WriteAixStringConst(hp: tai_string);
  1614. type
  1615. tterminationkind = (term_none,term_string,term_nostring);
  1616. var
  1617. i: longint;
  1618. pos: longint;
  1619. s: string;
  1620. ch: char;
  1621. instring: boolean;
  1622. procedure newstatement(terminationkind: tterminationkind);
  1623. begin
  1624. case terminationkind of
  1625. term_none: ;
  1626. term_string:
  1627. writer.AsmWriteLn('"');
  1628. term_nostring:
  1629. writer.AsmLn;
  1630. end;
  1631. writer.AsmWrite(#9'.byte'#9);
  1632. pos:=20;
  1633. instring:=false;
  1634. end;
  1635. begin
  1636. pos:=0;
  1637. instring:=false;
  1638. for i:=1 to hp.len do
  1639. begin
  1640. if pos=0 then
  1641. newstatement(term_none);
  1642. ch:=hp.str[i-1];
  1643. case ch of
  1644. #0..#31,
  1645. #127..#255 :
  1646. begin
  1647. if instring then
  1648. newstatement(term_string);
  1649. if pos=20 then
  1650. s:=tostr(ord(ch))
  1651. else
  1652. s:=', '+tostr(ord(ch))
  1653. end;
  1654. '"' :
  1655. if instring then
  1656. s:='""'
  1657. else
  1658. begin
  1659. if pos<>20 then
  1660. newstatement(term_nostring);
  1661. s:='"""';
  1662. instring:=true;
  1663. end;
  1664. else
  1665. if not instring then
  1666. begin
  1667. if (pos<>20) then
  1668. newstatement(term_nostring);
  1669. s:='"'+ch;
  1670. instring:=true;
  1671. end
  1672. else
  1673. s:=ch;
  1674. end;
  1675. writer.AsmWrite(s);
  1676. inc(pos,length(s));
  1677. if (pos>line_length) or (i=tai_string(hp).len) then
  1678. begin
  1679. if instring then
  1680. writer.AsmWriteLn('"')
  1681. else
  1682. writer.AsmLn;
  1683. pos:=0;
  1684. end;
  1685. end;
  1686. end;
  1687. procedure TGNUAssembler.WriteAixIntConst(hp: tai_const);
  1688. var
  1689. pos, size: longint;
  1690. begin
  1691. { only big endian AIX supported for now }
  1692. if target_info.endian<>endian_big then
  1693. internalerror(2012010401);
  1694. { limitation: can only write 4 bytes at a time }
  1695. pos:=0;
  1696. size:=tai_const(hp).size;
  1697. while pos<(size-4) do
  1698. begin
  1699. writer.AsmWrite(#9'.vbyte'#9'4, ');
  1700. writer.AsmWriteln(tostr(longint(tai_const(hp).value shr ((size-pos-4)*8))));
  1701. inc(pos,4);
  1702. end;
  1703. writer.AsmWrite(#9'.vbyte'#9);
  1704. writer.AsmWrite(tostr(size-pos));
  1705. writer.AsmWrite(', ');
  1706. case size-pos of
  1707. 1: writer.AsmWrite(tostr(byte(tai_const(hp).value)));
  1708. 2: writer.AsmWrite(tostr(word(tai_const(hp).value)));
  1709. 4: writer.AsmWrite(tostr(longint(tai_const(hp).value)));
  1710. else
  1711. internalerror(2012010402);
  1712. end;
  1713. end;
  1714. procedure TGNUAssembler.WriteUnalignedIntConst(hp: tai_const);
  1715. var
  1716. pos, size: longint;
  1717. begin
  1718. size:=tai_const(hp).size;
  1719. writer.AsmWrite(#9'.byte'#9);
  1720. if target_info.endian=endian_big then
  1721. begin
  1722. pos:=size-1;
  1723. while pos>=0 do
  1724. begin
  1725. writer.AsmWrite(tostr((tai_const(hp).value shr (pos*8)) and $ff));
  1726. dec(pos);
  1727. if pos>=0 then
  1728. writer.AsmWrite(', ')
  1729. else
  1730. writer.AsmLn;
  1731. end;
  1732. end
  1733. else
  1734. begin
  1735. pos:=0;
  1736. while pos<size do
  1737. begin
  1738. writer.AsmWriteln(tostr((tai_const(hp).value shr (pos*8)) and $ff));
  1739. inc(pos);
  1740. if pos<=size then
  1741. writer.AsmWrite(', ')
  1742. else
  1743. writer.AsmLn;
  1744. end;
  1745. end;
  1746. writer.AsmLn;
  1747. end;
  1748. procedure TGNUAssembler.WriteDirectiveName(dir: TAsmDirective);
  1749. begin
  1750. { TODO: implement asd_cpu for GAS => usually .arch or .cpu, but the CPU
  1751. name has to be translated as well }
  1752. if dir=asd_cpu then
  1753. writer.AsmWrite(asminfo^.comment+' CPU ')
  1754. else
  1755. writer.AsmWrite('.'+directivestr[dir]+' ');
  1756. end;
  1757. procedure TGNUAssembler.WriteAsmList;
  1758. var
  1759. n : string;
  1760. hal : tasmlisttype;
  1761. i: longint;
  1762. begin
  1763. {$ifdef EXTDEBUG}
  1764. if current_module.mainsource<>'' then
  1765. Comment(V_Debug,'Start writing gas-styled assembler output for '+current_module.mainsource);
  1766. {$endif}
  1767. if current_module.mainsource<>'' then
  1768. n:=ExtractFileName(current_module.mainsource)
  1769. else
  1770. n:=InputFileName;
  1771. { gcc does not add it either for Darwin. Grep for
  1772. TARGET_ASM_FILE_START_FILE_DIRECTIVE in gcc/config/*.h
  1773. }
  1774. if not(target_info.system in systems_darwin) then
  1775. writer.AsmWriteLn(#9'.file "'+FixFileName(n)+'"');
  1776. WriteExtraHeader;
  1777. writer.MarkEmpty;
  1778. symendcount:=0;
  1779. for hal:=low(TasmlistType) to high(TasmlistType) do
  1780. begin
  1781. if not (current_asmdata.asmlists[hal].empty) then
  1782. begin
  1783. writer.AsmWriteLn(asminfo^.comment+'Begin asmlist '+AsmlistTypeStr[hal]);
  1784. writetree(current_asmdata.asmlists[hal]);
  1785. writer.AsmWriteLn(asminfo^.comment+'End asmlist '+AsmlistTypeStr[hal]);
  1786. end;
  1787. end;
  1788. { add weak symbol markers }
  1789. for i:=0 to current_asmdata.asmsymboldict.count-1 do
  1790. if (tasmsymbol(current_asmdata.asmsymboldict[i]).bind=AB_WEAK_EXTERNAL) then
  1791. WriteWeakSymbolRef(tasmsymbol(current_asmdata.asmsymboldict[i]));
  1792. if create_smartlink_sections and
  1793. (target_info.system in systems_darwin) then
  1794. writer.AsmWriteLn(#9'.subsections_via_symbols');
  1795. { "no executable stack" marker }
  1796. { TODO: used by OpenBSD/NetBSD as well? }
  1797. if (target_info.system in (systems_linux + systems_android + systems_freebsd + systems_dragonfly)) and
  1798. not(cs_executable_stack in current_settings.moduleswitches) then
  1799. begin
  1800. writer.AsmWriteLn('.section .note.GNU-stack,"",%progbits');
  1801. end;
  1802. writer.AsmLn;
  1803. WriteExtraFooter;
  1804. {$ifdef EXTDEBUG}
  1805. if current_module.mainsource<>'' then
  1806. Comment(V_Debug,'Done writing gas-styled assembler output for '+current_module.mainsource);
  1807. {$endif EXTDEBUG}
  1808. end;
  1809. {****************************************************************************}
  1810. { Apple/GNU Assembler writer }
  1811. {****************************************************************************}
  1812. function TAppleGNUAssembler.sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;
  1813. begin
  1814. if (target_info.system in systems_darwin) then
  1815. case atype of
  1816. sec_user:
  1817. begin
  1818. result:='.section '+aname;
  1819. exit;
  1820. end;
  1821. sec_bss:
  1822. { all bss (lcomm) symbols are automatically put in the right }
  1823. { place by using the lcomm assembler directive }
  1824. atype := sec_none;
  1825. sec_debug_frame,
  1826. sec_eh_frame:
  1827. begin
  1828. result := '.section __DWARF,__debug_info,regular,debug';
  1829. exit;
  1830. end;
  1831. sec_debug_line:
  1832. begin
  1833. result := '.section __DWARF,__debug_line,regular,debug';
  1834. exit;
  1835. end;
  1836. sec_debug_info:
  1837. begin
  1838. result := '.section __DWARF,__debug_info,regular,debug';
  1839. exit;
  1840. end;
  1841. sec_debug_abbrev:
  1842. begin
  1843. result := '.section __DWARF,__debug_abbrev,regular,debug';
  1844. exit;
  1845. end;
  1846. sec_debug_aranges:
  1847. begin
  1848. result := '.section __DWARF,__debug_aranges,regular,debug';
  1849. exit;
  1850. end;
  1851. sec_debug_ranges:
  1852. begin
  1853. result := '.section __DWARF,__debug_ranges,regular,debug';
  1854. exit;
  1855. end;
  1856. sec_rodata:
  1857. begin
  1858. result := '.const_data';
  1859. exit;
  1860. end;
  1861. sec_rodata_norel:
  1862. begin
  1863. result := '.const';
  1864. exit;
  1865. end;
  1866. sec_fpc:
  1867. begin
  1868. result := '.section __TEXT, .fpc, regular, no_dead_strip';
  1869. exit;
  1870. end;
  1871. sec_code:
  1872. begin
  1873. if (aname='fpc_geteipasebx') or
  1874. (aname='fpc_geteipasecx') then
  1875. begin
  1876. result:='.section __TEXT,__textcoal_nt,coalesced,pure_instructions'#10'.weak_definition '+aname+
  1877. #10'.private_extern '+aname;
  1878. exit;
  1879. end;
  1880. end;
  1881. sec_data_nonlazy:
  1882. begin
  1883. result:='.section __DATA, __nl_symbol_ptr,non_lazy_symbol_pointers';
  1884. exit;
  1885. end;
  1886. sec_data_lazy:
  1887. begin
  1888. result:='.section __DATA, __la_symbol_ptr,lazy_symbol_pointers';
  1889. exit;
  1890. end;
  1891. sec_init_func:
  1892. begin
  1893. result:='.section __DATA, __mod_init_func, mod_init_funcs';
  1894. exit;
  1895. end;
  1896. sec_term_func:
  1897. begin
  1898. result:='.section __DATA, __mod_term_func, mod_term_funcs';
  1899. exit;
  1900. end;
  1901. low(TObjCAsmSectionType)..high(TObjCAsmSectionType):
  1902. begin
  1903. result:='.section '+objc_section_name(atype);
  1904. exit
  1905. end;
  1906. else
  1907. ;
  1908. end;
  1909. result := inherited sectionname(atype,aname,aorder);
  1910. end;
  1911. procedure TAppleGNUAssembler.WriteWeakSymbolRef(s: tasmsymbol);
  1912. begin
  1913. writer.AsmWriteLn(#9'.weak_reference '+s.name);
  1914. end;
  1915. procedure TAppleGNUAssembler.WriteDirectiveName(dir: TAsmDirective);
  1916. begin
  1917. case dir of
  1918. asd_weak_reference:
  1919. writer.AsmWrite('.weak_reference ');
  1920. asd_weak_definition:
  1921. writer.AsmWrite('.weak_definition ');
  1922. else
  1923. inherited;
  1924. end;
  1925. end;
  1926. {****************************************************************************}
  1927. { a.out/GNU Assembler writer }
  1928. {****************************************************************************}
  1929. function TAoutGNUAssembler.sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;
  1930. const
  1931. (* Translation table - replace unsupported section types with basic ones. *)
  1932. SecXTable: array[TAsmSectionType] of TAsmSectionType = (
  1933. sec_none,
  1934. sec_none,
  1935. sec_code,
  1936. sec_data,
  1937. sec_data (* sec_rodata *),
  1938. sec_data (* sec_rodata_norel *),
  1939. sec_bss,
  1940. sec_data (* sec_threadvar *),
  1941. { used for wince exception handling }
  1942. sec_code (* sec_pdata *),
  1943. { used for darwin import stubs }
  1944. sec_code (* sec_stub *),
  1945. sec_data,(* sec_data_nonlazy *)
  1946. sec_data,(* sec_data_lazy *)
  1947. sec_data,(* sec_init_func *)
  1948. sec_data,(* sec_term_func *)
  1949. { stabs }
  1950. sec_stab,sec_stabstr,
  1951. { win32 }
  1952. sec_data (* sec_idata2 *),
  1953. sec_data (* sec_idata4 *),
  1954. sec_data (* sec_idata5 *),
  1955. sec_data (* sec_idata6 *),
  1956. sec_data (* sec_idata7 *),
  1957. sec_data (* sec_edata *),
  1958. { C++ exception handling unwinding (uses dwarf) }
  1959. sec_eh_frame,
  1960. { dwarf }
  1961. sec_debug_frame,
  1962. sec_debug_info,
  1963. sec_debug_line,
  1964. sec_debug_abbrev,
  1965. sec_debug_aranges,
  1966. sec_debug_ranges,
  1967. { ELF resources (+ references to stabs debug information sections) }
  1968. sec_code (* sec_fpc *),
  1969. { Table of contents section }
  1970. sec_code (* sec_toc *),
  1971. sec_code (* sec_init *),
  1972. sec_code (* sec_fini *),
  1973. sec_none (* sec_objc_class *),
  1974. sec_none (* sec_objc_meta_class *),
  1975. sec_none (* sec_objc_cat_cls_meth *),
  1976. sec_none (* sec_objc_cat_inst_meth *),
  1977. sec_none (* sec_objc_protocol *),
  1978. sec_none (* sec_objc_string_object *),
  1979. sec_none (* sec_objc_cls_meth *),
  1980. sec_none (* sec_objc_inst_meth *),
  1981. sec_none (* sec_objc_cls_refs *),
  1982. sec_none (* sec_objc_message_refs *),
  1983. sec_none (* sec_objc_symbols *),
  1984. sec_none (* sec_objc_category *),
  1985. sec_none (* sec_objc_class_vars *),
  1986. sec_none (* sec_objc_instance_vars *),
  1987. sec_none (* sec_objc_module_info *),
  1988. sec_none (* sec_objc_class_names *),
  1989. sec_none (* sec_objc_meth_var_types *),
  1990. sec_none (* sec_objc_meth_var_names *),
  1991. sec_none (* sec_objc_selector_strs *),
  1992. sec_none (* sec_objc_protocol_ext *),
  1993. sec_none (* sec_objc_class_ext *),
  1994. sec_none (* sec_objc_property *),
  1995. sec_none (* sec_objc_image_info *),
  1996. sec_none (* sec_objc_cstring_object *),
  1997. sec_none (* sec_objc_sel_fixup *),
  1998. sec_none (* sec_objc_data *),
  1999. sec_none (* sec_objc_const *),
  2000. sec_none (* sec_objc_sup_refs *),
  2001. sec_none (* sec_data_coalesced *),
  2002. sec_none (* sec_objc_classlist *),
  2003. sec_none (* sec_objc_nlclasslist *),
  2004. sec_none (* sec_objc_catlist *),
  2005. sec_none (* sec_objc_nlcatlist *),
  2006. sec_none (* sec_objc_protlist *),
  2007. sec_none (* sec_stack *),
  2008. sec_none (* sec_heap *),
  2009. sec_none (* gcc_except_table *),
  2010. sec_none (* sec_arm_attribute *)
  2011. );
  2012. begin
  2013. Result := inherited SectionName (SecXTable [AType], AName, AOrder);
  2014. end;
  2015. {****************************************************************************}
  2016. { Abstract Instruction Writer }
  2017. {****************************************************************************}
  2018. constructor TCPUInstrWriter.create(_owner: TGNUAssembler);
  2019. begin
  2020. inherited create;
  2021. owner := _owner;
  2022. end;
  2023. end.