aggas.pas 78 KB

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