aggas.pas 87 KB

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