aggas.pas 82 KB

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