aggas.pas 85 KB

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