aggas.pas 79 KB

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