aggas.pas 81 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163
  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(riscv64) or defined(powerpc)}
  184. '.rodata',
  185. {$else defined(arm) or defined(riscv64) or defined(powerpc)}
  186. '.data',
  187. {$endif defined(arm) or defined(riscv64) or defined(powerpc)}
  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 WriteImportExport(hp:tai_impexp);
  747. var
  748. symstypestr: string;
  749. begin
  750. Str(hp.symstype,symstypestr);
  751. writer.AsmWriteLn(asminfo^.comment+'ait_importexport(extname='''+hp.extname+''', intname='''+hp.intname+''', symstype='+symstypestr+')');
  752. writer.AsmWriteLn(#9'.export_name '+hp.intname+', '+hp.extname);
  753. end;
  754. procedure WriteTagType(hp: tai_tagtype);
  755. var
  756. wasm_basic_typ: TWasmBasicType;
  757. first: boolean;
  758. begin
  759. writer.AsmWrite(#9'.tagtype'#9);
  760. writer.AsmWrite(hp.tagname);
  761. first:=true;
  762. for wasm_basic_typ in hp.params do
  763. begin
  764. if first then
  765. begin
  766. first:=false;
  767. writer.AsmWrite(' ');
  768. end
  769. else
  770. writer.AsmWrite(',');
  771. writer.AsmWrite(gas_wasm_basic_type_str[wasm_basic_typ]);
  772. end;
  773. writer.AsmLn;
  774. end;
  775. {$endif WASM}
  776. var
  777. ch : char;
  778. lasthp,
  779. hp : tai;
  780. constdef : taiconst_type;
  781. s,t : string;
  782. i,pos,l : longint;
  783. InlineLevel : cardinal;
  784. last_align : longint;
  785. do_line : boolean;
  786. sepChar : char;
  787. replaceforbidden: boolean;
  788. begin
  789. if not assigned(p) then
  790. exit;
  791. replaceforbidden:=asminfo^.dollarsign<>'$';
  792. last_align := 2;
  793. InlineLevel:=0;
  794. { lineinfo is only needed for al_procedures (PFV) }
  795. do_line:=(cs_asm_source in current_settings.globalswitches) or
  796. ((cs_lineinfo in current_settings.moduleswitches)
  797. and (p=current_asmdata.asmlists[al_procedures]));
  798. lasthp:=nil;
  799. hp:=tai(p.first);
  800. while assigned(hp) do
  801. begin
  802. prefetch(pointer(hp.next)^);
  803. if not(hp.typ in SkipLineInfo) then
  804. begin
  805. current_filepos:=tailineinfo(hp).fileinfo;
  806. { no line info for inlined code }
  807. if do_line and (inlinelevel=0) then
  808. WriteSourceLine(hp as tailineinfo);
  809. end;
  810. case hp.typ of
  811. ait_align :
  812. begin
  813. 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);
  814. end;
  815. ait_section :
  816. begin
  817. if tai_section(hp).sectype<>sec_none then
  818. if replaceforbidden then
  819. WriteSection(tai_section(hp).sectype,ApplyAsmSymbolRestrictions(tai_section(hp).name^),tai_section(hp).secorder,
  820. tai_section(hp).secalign,tai_section(hp).secflags,tai_section(hp).secprogbits)
  821. else
  822. WriteSection(tai_section(hp).sectype,tai_section(hp).name^,tai_section(hp).secorder,
  823. tai_section(hp).secalign,tai_section(hp).secflags,tai_section(hp).secprogbits)
  824. else
  825. begin
  826. {$ifdef EXTDEBUG}
  827. writer.AsmWrite(asminfo^.comment);
  828. writer.AsmWriteln(' sec_none');
  829. {$endif EXTDEBUG}
  830. end;
  831. end;
  832. ait_datablock :
  833. begin
  834. if (target_info.system in systems_darwin) then
  835. begin
  836. { On Mac OS X you can't have common symbols in a shared library
  837. since those are in the TEXT section and the text section is
  838. read-only in shared libraries (so it can be shared among different
  839. processes). The alternate code creates some kind of common symbols
  840. in the data segment.
  841. }
  842. if tai_datablock(hp).is_global then
  843. begin
  844. if tai_datablock(hp).sym.bind=AB_PRIVATE_EXTERN then
  845. WriteHiddenSymbol(tai_datablock(hp).sym);
  846. writer.AsmWrite('.globl ');
  847. writer.AsmWriteln(tai_datablock(hp).sym.name);
  848. writer.AsmWriteln('.data');
  849. writer.AsmWrite('.zerofill __DATA, __common, ');
  850. writer.AsmWrite(tai_datablock(hp).sym.name);
  851. writer.AsmWriteln(', '+tostr(tai_datablock(hp).size)+','+tostr(last_align));
  852. if not(LastSecType in [sec_data,sec_none]) then
  853. writesection(LastSecType,'',secorder_default,1 shl last_align);
  854. end
  855. else
  856. begin
  857. writer.AsmWrite(#9'.lcomm'#9);
  858. writer.AsmWrite(tai_datablock(hp).sym.name);
  859. writer.AsmWrite(','+tostr(tai_datablock(hp).size));
  860. writer.AsmWrite(','+tostr(last_align));
  861. writer.AsmLn;
  862. end;
  863. end
  864. else if target_info.system in systems_aix then
  865. begin
  866. if tai_datablock(hp).is_global then
  867. begin
  868. writer.AsmWrite(#9'.globl ');
  869. writer.AsmWriteln(ApplyAsmSymbolRestrictions(tai_datablock(hp).sym.name));
  870. writer.AsmWrite(ApplyAsmSymbolRestrictions(tai_datablock(hp).sym.name));
  871. writer.AsmWriteln(':');
  872. writer.AsmWrite(#9'.space ');
  873. writer.AsmWriteln(tostr(tai_datablock(hp).size));
  874. if not(LastSecType in [sec_data,sec_none]) then
  875. writesection(LastSecType,'',secorder_default,1 shl last_align);
  876. end
  877. else
  878. begin
  879. writer.AsmWrite(#9'.lcomm ');
  880. writer.AsmWrite(ApplyAsmSymbolRestrictions(tai_datablock(hp).sym.name));
  881. writer.AsmWrite(',');
  882. writer.AsmWrite(tostr(tai_datablock(hp).size)+',');
  883. writer.AsmWrite('_data.bss_,');
  884. writer.AsmWriteln(tostr(last_align));
  885. end;
  886. end
  887. else
  888. begin
  889. {$ifdef USE_COMM_IN_BSS}
  890. if writingpackages then
  891. begin
  892. { The .comm is required for COMMON symbols. These are used
  893. in the shared library loading. All the symbols declared in
  894. the .so file need to resolve to the data allocated in the main
  895. program (PFV) }
  896. if tai_datablock(hp).is_global then
  897. begin
  898. writer.AsmWrite(#9'.comm'#9);
  899. if replaceforbidden then
  900. writer.AsmWrite(ApplyAsmSymbolRestrictions(tai_datablock(hp).sym.name))
  901. else
  902. writer.AsmWrite(tai_datablock(hp).sym.name);
  903. writer.AsmWrite(','+tostr(tai_datablock(hp).size));
  904. writer.AsmWrite(','+tostr(last_align));
  905. writer.AsmLn;
  906. end
  907. else
  908. begin
  909. writer.AsmWrite(#9'.lcomm'#9);
  910. if replaceforbidden then
  911. writer.AsmWrite(ApplyAsmSymbolRestrictions(tai_datablock(hp).sym.name));
  912. else
  913. writer.AsmWrite(tai_datablock(hp).sym.name);
  914. writer.AsmWrite(','+tostr(tai_datablock(hp).size));
  915. writer.AsmWrite(','+tostr(last_align));
  916. writer.AsmLn;
  917. end
  918. end
  919. else
  920. {$endif USE_COMM_IN_BSS}
  921. begin
  922. if Tai_datablock(hp).is_global then
  923. begin
  924. if (tai_datablock(hp).sym.bind=AB_PRIVATE_EXTERN) then
  925. WriteHiddenSymbol(tai_datablock(hp).sym);
  926. writer.AsmWrite(#9'.globl ');
  927. if replaceforbidden then
  928. writer.AsmWriteln(ApplyAsmSymbolRestrictions(Tai_datablock(hp).sym.name))
  929. else
  930. writer.AsmWriteln(Tai_datablock(hp).sym.name);
  931. end;
  932. if ((target_info.system <> system_arm_linux) and (target_info.system <> system_arm_android)) then
  933. sepChar := '@'
  934. else
  935. sepChar := '%';
  936. if replaceforbidden then
  937. begin
  938. if (tf_needs_symbol_type in target_info.flags) then
  939. writer.AsmWriteln(#9'.type '+ApplyAsmSymbolRestrictions(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 '+ApplyAsmSymbolRestrictions(Tai_datablock(hp).sym.name)+','+tostr(Tai_datablock(hp).size));
  942. writer.AsmWrite(ApplyAsmSymbolRestrictions(Tai_datablock(hp).sym.name))
  943. end
  944. else
  945. begin
  946. if (tf_needs_symbol_type in target_info.flags) then
  947. writer.AsmWriteln(#9'.type '+Tai_datablock(hp).sym.name+','+sepChar+'object');
  948. if (tf_needs_symbol_size in target_info.flags) and (tai_datablock(hp).size > 0) then
  949. writer.AsmWriteln(#9'.size '+Tai_datablock(hp).sym.name+','+tostr(Tai_datablock(hp).size));
  950. writer.AsmWrite(Tai_datablock(hp).sym.name);
  951. end;
  952. writer.AsmWriteln(':');
  953. writer.AsmWriteln(#9'.zero '+tostr(Tai_datablock(hp).size));
  954. end;
  955. end;
  956. end;
  957. ait_const:
  958. begin
  959. constdef:=tai_const(hp).consttype;
  960. case constdef of
  961. {$ifndef cpu64bitaddr}
  962. aitconst_128bit :
  963. begin
  964. internalerror(200404291);
  965. end;
  966. aitconst_64bit :
  967. begin
  968. if assigned(tai_const(hp).sym) then
  969. internalerror(200404292);
  970. if not(target_info.system in systems_aix) then
  971. begin
  972. writer.AsmWrite(ait_const2str[aitconst_32bit]);
  973. if target_info.endian = endian_little then
  974. begin
  975. writer.AsmWrite(tostr(longint(lo(tai_const(hp).value))));
  976. writer.AsmWrite(',');
  977. writer.AsmWrite(tostr(longint(hi(tai_const(hp).value))));
  978. end
  979. else
  980. begin
  981. writer.AsmWrite(tostr(longint(hi(tai_const(hp).value))));
  982. writer.AsmWrite(',');
  983. writer.AsmWrite(tostr(longint(lo(tai_const(hp).value))));
  984. end;
  985. end
  986. else
  987. WriteAixIntConst(tai_const(hp));
  988. writer.AsmLn;
  989. end;
  990. aitconst_gottpoff:
  991. begin
  992. writer.AsmWrite(#9'.word'#9+tai_const(hp).sym.name+'(gottpoff)+(.-'+tai_const(hp).endsym.name+tostr_with_plus(tai_const(hp).symofs)+')');
  993. writer.Asmln;
  994. end;
  995. aitconst_tlsgd:
  996. begin
  997. writer.AsmWrite(#9'.word'#9+tai_const(hp).sym.name+'(tlsgd)+(.-'+tai_const(hp).endsym.name+tostr_with_plus(tai_const(hp).symofs)+')');
  998. writer.Asmln;
  999. end;
  1000. aitconst_tlsdesc:
  1001. begin
  1002. writer.AsmWrite(#9'.word'#9+tai_const(hp).sym.name+'(tlsdesc)+(.-'+tai_const(hp).endsym.name+tostr_with_plus(tai_const(hp).symofs)+')');
  1003. writer.Asmln;
  1004. end;
  1005. aitconst_tpoff:
  1006. begin
  1007. if assigned(tai_const(hp).endsym) or (tai_const(hp).symofs<>0) then
  1008. Internalerror(2019092805);
  1009. writer.AsmWrite(#9'.word'#9+tai_const(hp).sym.name+'(tpoff)');
  1010. writer.Asmln;
  1011. end;
  1012. {$endif cpu64bitaddr}
  1013. aitconst_dtpoff:
  1014. begin
  1015. {$ifdef arm}
  1016. writer.AsmWrite(#9'.word'#9+tai_const(hp).sym.name+'(tlsldo)');
  1017. writer.Asmln;
  1018. {$endif arm}
  1019. {$ifdef x86_64}
  1020. writer.AsmWrite(#9'.long'#9+tai_const(hp).sym.name+'@dtpoff');
  1021. writer.Asmln;
  1022. {$endif x86_64}
  1023. {$ifdef i386}
  1024. writer.AsmWrite(#9'.word'#9+tai_const(hp).sym.name+'@tdpoff');
  1025. writer.Asmln;
  1026. {$endif i386}
  1027. end;
  1028. aitconst_got:
  1029. begin
  1030. if tai_const(hp).symofs<>0 then
  1031. InternalError(2015091401); // No symbol offset is allowed for GOT.
  1032. writer.AsmWrite(#9'.word'#9+tai_const(hp).sym.name+'(GOT)');
  1033. writer.AsmLn;
  1034. end;
  1035. aitconst_gotoff_symbol:
  1036. begin
  1037. if (tai_const(hp).sym=nil) then
  1038. InternalError(2014022601);
  1039. case target_info.cpu of
  1040. cpu_mipseb,cpu_mipsel:
  1041. begin
  1042. writer.AsmWrite(#9'.gpword'#9);
  1043. writer.AsmWrite(tai_const(hp).sym.name);
  1044. end;
  1045. cpu_i386:
  1046. begin
  1047. writer.AsmWrite(ait_const2str[aitconst_32bit]);
  1048. writer.AsmWrite(tai_const(hp).sym.name+'-_GLOBAL_OFFSET_TABLE_');
  1049. end;
  1050. else
  1051. InternalError(2014022602);
  1052. end;
  1053. if (tai_const(hp).value<>0) then
  1054. writer.AsmWrite(tostr_with_plus(tai_const(hp).value));
  1055. writer.AsmLn;
  1056. end;
  1057. aitconst_uleb128bit,
  1058. aitconst_sleb128bit,
  1059. {$ifdef cpu64bitaddr}
  1060. aitconst_128bit,
  1061. aitconst_64bit,
  1062. {$endif cpu64bitaddr}
  1063. aitconst_32bit,
  1064. aitconst_16bit,
  1065. aitconst_8bit,
  1066. aitconst_rva_symbol,
  1067. aitconst_secrel32_symbol,
  1068. aitconst_darwin_dwarf_delta32,
  1069. aitconst_darwin_dwarf_delta64,
  1070. aitconst_half16bit,
  1071. aitconst_gs,
  1072. aitconst_16bit_unaligned,
  1073. aitconst_32bit_unaligned,
  1074. aitconst_64bit_unaligned:
  1075. begin
  1076. { the AIX assembler (and for compatibility, the GNU
  1077. assembler when targeting AIX) automatically aligns
  1078. .short/.long/.llong to a multiple of 2/4/8 bytes. We
  1079. don't want that, since this may be data inside a packed
  1080. record -> use .vbyte instead (byte stream of fixed
  1081. length) }
  1082. if (target_info.system in systems_aix) and
  1083. (constdef in [aitconst_128bit,aitconst_64bit,aitconst_32bit,aitconst_16bit]) and
  1084. not assigned(tai_const(hp).sym) then
  1085. begin
  1086. WriteAixIntConst(tai_const(hp));
  1087. end
  1088. else if (target_info.system in systems_darwin) and
  1089. (constdef in [aitconst_uleb128bit,aitconst_sleb128bit]) then
  1090. begin
  1091. writer.AsmWrite(ait_const2str[aitconst_8bit]);
  1092. case tai_const(hp).consttype of
  1093. aitconst_uleb128bit:
  1094. writer.AsmWrite(uleb128tostr(qword(tai_const(hp).value)));
  1095. aitconst_sleb128bit:
  1096. writer.AsmWrite(sleb128tostr(tai_const(hp).value));
  1097. else
  1098. ;
  1099. end
  1100. end
  1101. else
  1102. begin
  1103. if (constdef in ait_unaligned_consts) and
  1104. (target_info.system in use_ua_sparc_systems) then
  1105. writer.AsmWrite(ait_ua_sparc_const2str[constdef])
  1106. else if (target_info.system in use_ua_elf_systems) then
  1107. writer.AsmWrite(ait_ua_elf_const2str[constdef])
  1108. { we can also have unaligned pointers in packed record
  1109. constants, which don't get translated into
  1110. unaligned tai -> always use vbyte }
  1111. else if target_info.system in systems_aix then
  1112. writer.AsmWrite(#9'.vbyte'#9+tostr(tai_const(hp).size)+',')
  1113. else if (asminfo^.id=as_solaris_as) then
  1114. writer.AsmWrite(ait_solaris_const2str[constdef])
  1115. else
  1116. writer.AsmWrite(ait_const2str[constdef]);
  1117. l:=0;
  1118. t := '';
  1119. repeat
  1120. if assigned(tai_const(hp).sym) then
  1121. begin
  1122. if assigned(tai_const(hp).endsym) then
  1123. begin
  1124. if (constdef in [aitconst_darwin_dwarf_delta32,aitconst_darwin_dwarf_delta64]) then
  1125. begin
  1126. s := NextSetLabel;
  1127. t := #9'.set '+s+','+tai_const(hp).endsym.name+'-'+tai_const(hp).sym.name;
  1128. end
  1129. else
  1130. s:=tai_const(hp).endsym.name+'-'+tai_const(hp).sym.name
  1131. end
  1132. else
  1133. s:=tai_const(hp).sym.name;
  1134. if replaceforbidden then
  1135. s:=ApplyAsmSymbolRestrictions(s);
  1136. if tai_const(hp).value<>0 then
  1137. s:=s+tostr_with_plus(tai_const(hp).value);
  1138. end
  1139. else
  1140. {$ifdef cpu64bitaddr}
  1141. s:=tostr(tai_const(hp).value);
  1142. {$else cpu64bitaddr}
  1143. { 64 bit constants are already handled above in this case }
  1144. s:=tostr(longint(tai_const(hp).value));
  1145. {$endif cpu64bitaddr}
  1146. if constdef = aitconst_half16bit then
  1147. s:='('+s+')/2';
  1148. if constdef = aitconst_gs then
  1149. s:='gs('+s+')';
  1150. writer.AsmWrite(s);
  1151. inc(l,length(s));
  1152. { Values with symbols are written on a single line to improve
  1153. reading of the .s file (PFV) }
  1154. if assigned(tai_const(hp).sym) or
  1155. not(LastSecType in [sec_data,sec_rodata,sec_rodata_norel]) or
  1156. (l>line_length) or
  1157. (hp.next=nil) or
  1158. (tai(hp.next).typ<>ait_const) or
  1159. (tai_const(hp.next).consttype<>constdef) or
  1160. assigned(tai_const(hp.next).sym) then
  1161. break;
  1162. hp:=tai(hp.next);
  1163. writer.AsmWrite(',');
  1164. until false;
  1165. if (t <> '') then
  1166. begin
  1167. writer.AsmLn;
  1168. writer.AsmWrite(t);
  1169. end;
  1170. end;
  1171. writer.AsmLn;
  1172. end;
  1173. else
  1174. internalerror(200704251);
  1175. end;
  1176. end;
  1177. ait_realconst :
  1178. begin
  1179. WriteRealConstAsBytes(tai_realconst(hp),#9'.byte'#9,do_line);
  1180. end;
  1181. ait_string :
  1182. begin
  1183. pos:=0;
  1184. if not(target_info.system in systems_aix) then
  1185. begin
  1186. for i:=1 to tai_string(hp).len do
  1187. begin
  1188. if pos=0 then
  1189. begin
  1190. writer.AsmWrite(#9'.ascii'#9'"');
  1191. pos:=20;
  1192. end;
  1193. ch:=tai_string(hp).str[i-1];
  1194. case ch of
  1195. #0, {This can't be done by range, because a bug in FPC}
  1196. #1..#31,
  1197. #128..#255 : s:='\'+tostr(ord(ch) shr 6)+tostr((ord(ch) and 63) shr 3)+tostr(ord(ch) and 7);
  1198. '"' : s:='\"';
  1199. '\' : s:='\\';
  1200. else
  1201. s:=ch;
  1202. end;
  1203. writer.AsmWrite(s);
  1204. inc(pos,length(s));
  1205. if (pos>line_length) or (i=tai_string(hp).len) then
  1206. begin
  1207. writer.AsmWriteLn('"');
  1208. pos:=0;
  1209. end;
  1210. end;
  1211. end
  1212. else
  1213. WriteAixStringConst(tai_string(hp));
  1214. end;
  1215. ait_label :
  1216. begin
  1217. if (tai_label(hp).labsym.is_used) then
  1218. begin
  1219. {$ifdef DEBUG_LABEL}
  1220. writer.AsmWrite(asminfo^.comment);
  1221. writer.AsmWriteLn('References = ' + tostr(tai_label(hp).labsym.getrefs));
  1222. {$endif DEBUG_LABEL}
  1223. if tai_label(hp).labsym.bind in [AB_GLOBAL,AB_PRIVATE_EXTERN] then
  1224. begin
  1225. if (tai_label(hp).labsym.bind=AB_PRIVATE_EXTERN) then
  1226. begin
  1227. writer.AsmWrite(#9'.private_extern ');
  1228. writer.AsmWriteln(tai_label(hp).labsym.name);
  1229. end;
  1230. {$ifdef arm}
  1231. { do no change arm mode accidently, .globl seems to reset the mode }
  1232. if GenerateThumbCode or GenerateThumb2Code then
  1233. writer.AsmWriteln(#9'.thumb_func'#9);
  1234. {$endif arm}
  1235. writer.AsmWrite('.globl'#9);
  1236. if replaceforbidden then
  1237. writer.AsmWriteLn(ApplyAsmSymbolRestrictions(tai_label(hp).labsym.name))
  1238. else
  1239. writer.AsmWriteLn(tai_label(hp).labsym.name);
  1240. end;
  1241. if replaceforbidden then
  1242. writer.AsmWrite(ApplyAsmSymbolRestrictions(tai_label(hp).labsym.name))
  1243. else
  1244. writer.AsmWrite(tai_label(hp).labsym.name);
  1245. writer.AsmWriteLn(':');
  1246. end;
  1247. end;
  1248. ait_symbol :
  1249. begin
  1250. if (target_info.system=system_powerpc64_linux) and
  1251. (tai_symbol(hp).sym.typ=AT_FUNCTION) and
  1252. (cs_profile in current_settings.moduleswitches) then
  1253. writer.AsmWriteLn('.globl _mcount');
  1254. if tai_symbol(hp).is_global then
  1255. begin
  1256. writer.AsmWrite('.globl'#9);
  1257. if replaceforbidden then
  1258. writer.AsmWriteln(ApplyAsmSymbolRestrictions(tai_symbol(hp).sym.name))
  1259. else
  1260. writer.AsmWriteln(tai_symbol(hp).sym.name);
  1261. if (tai_symbol(hp).sym.bind=AB_PRIVATE_EXTERN) then
  1262. WriteHiddenSymbol(tai_symbol(hp).sym);
  1263. end;
  1264. if (target_info.system=system_powerpc64_linux) and
  1265. use_dotted_functions and
  1266. (tai_symbol(hp).sym.typ=AT_FUNCTION) then
  1267. begin
  1268. writer.AsmWriteLn('.section ".opd", "aw"');
  1269. writer.AsmWriteLn('.align 3');
  1270. writer.AsmWriteLn(tai_symbol(hp).sym.name + ':');
  1271. writer.AsmWriteLn('.quad .' + tai_symbol(hp).sym.name + ', .TOC.@tocbase, 0');
  1272. writer.AsmWriteLn('.previous');
  1273. writer.AsmWriteLn('.size ' + tai_symbol(hp).sym.name + ', 24');
  1274. if (tai_symbol(hp).is_global) then
  1275. writer.AsmWriteLn('.globl .' + tai_symbol(hp).sym.name);
  1276. writer.AsmWriteLn('.type .' + tai_symbol(hp).sym.name + ', @function');
  1277. { the dotted name is the name of the actual function entry }
  1278. writer.AsmWrite('.');
  1279. end
  1280. else if (target_info.system in systems_aix) and
  1281. (tai_symbol(hp).sym.typ = AT_FUNCTION) then
  1282. begin
  1283. if target_info.system=system_powerpc_aix then
  1284. begin
  1285. s:=#9'.long .';
  1286. ch:='2';
  1287. end
  1288. else
  1289. begin
  1290. s:=#9'.llong .';
  1291. ch:='3';
  1292. end;
  1293. writer.AsmWriteLn(#9'.csect '+ApplyAsmSymbolRestrictions(tai_symbol(hp).sym.name)+'[DS],'+ch);
  1294. writer.AsmWriteLn(ApplyAsmSymbolRestrictions(tai_symbol(hp).sym.name)+':');
  1295. writer.AsmWriteln(s+ApplyAsmSymbolRestrictions(tai_symbol(hp).sym.name)+', TOC[tc0], 0');
  1296. writer.AsmWriteln(#9'.csect .text[PR]');
  1297. if (tai_symbol(hp).is_global) then
  1298. writer.AsmWriteLn('.globl .'+ApplyAsmSymbolRestrictions(tai_symbol(hp).sym.name))
  1299. else
  1300. writer.AsmWriteLn('.lglobl .'+ApplyAsmSymbolRestrictions(tai_symbol(hp).sym.name));
  1301. { the dotted name is the name of the actual function entry }
  1302. writer.AsmWrite('.');
  1303. end
  1304. else
  1305. begin
  1306. if ((target_info.system <> system_arm_linux) and (target_info.system <> system_arm_android)) or
  1307. (target_asm.id=as_arm_vasm) then
  1308. sepChar := '@'
  1309. else
  1310. sepChar := '#';
  1311. if (tf_needs_symbol_type in target_info.flags) then
  1312. begin
  1313. writer.AsmWrite(#9'.type'#9 + tai_symbol(hp).sym.name);
  1314. if (needsObject(tai_symbol(hp))) then
  1315. writer.AsmWriteLn(',' + sepChar + 'object')
  1316. else
  1317. writer.AsmWriteLn(',' + sepChar + 'function');
  1318. end;
  1319. end;
  1320. if replaceforbidden then
  1321. if not(tai_symbol(hp).has_value) then
  1322. writer.AsmWriteLn(ApplyAsmSymbolRestrictions(tai_symbol(hp).sym.name + ':'))
  1323. else
  1324. writer.AsmWriteLn(ApplyAsmSymbolRestrictions(tai_symbol(hp).sym.name + '=' + tostr(tai_symbol(hp).value)))
  1325. else if not(tai_symbol(hp).has_value) then
  1326. writer.AsmWriteLn(tai_symbol(hp).sym.name + ':')
  1327. else
  1328. writer.AsmWriteLn(tai_symbol(hp).sym.name + '=' + tostr(tai_symbol(hp).value));
  1329. end;
  1330. ait_symbolpair:
  1331. begin
  1332. writer.AsmWrite(#9);
  1333. writer.AsmWrite(symbolpairkindstr[tai_symbolpair(hp).kind]);
  1334. writer.AsmWrite(' ');
  1335. if tai_symbolpair(hp).kind<>spk_localentry then
  1336. s:=', '
  1337. else
  1338. { the .localentry directive has to specify the size from the
  1339. start till here of the non-local entry code as second argument }
  1340. s:=', .-';
  1341. if ((target_info.system <> system_arm_linux) and (target_info.system <> system_arm_android)) then
  1342. sepChar := '@'
  1343. else
  1344. sepChar := '#';
  1345. if replaceforbidden then
  1346. begin
  1347. { avoid string truncation }
  1348. writer.AsmWrite(ApplyAsmSymbolRestrictions(tai_symbolpair(hp).sym^));
  1349. writer.AsmWrite(s);
  1350. writer.AsmWriteLn(ApplyAsmSymbolRestrictions(tai_symbolpair(hp).value^));
  1351. if tai_symbolpair(hp).kind=spk_set_global then
  1352. begin
  1353. writer.AsmWrite(#9'.globl ');
  1354. writer.AsmWriteLn(ApplyAsmSymbolRestrictions(tai_symbolpair(hp).sym^));
  1355. end;
  1356. if (tf_needs_symbol_type in target_info.flags) then
  1357. begin
  1358. writer.AsmWrite(#9'.type'#9 + ApplyAsmSymbolRestrictions(tai_symbolpair(hp).sym^));
  1359. writer.AsmWriteLn(',' + sepChar + 'function');
  1360. end;
  1361. end
  1362. else
  1363. begin
  1364. { avoid string truncation }
  1365. writer.AsmWrite(tai_symbolpair(hp).sym^);
  1366. writer.AsmWrite(s);
  1367. writer.AsmWriteLn(tai_symbolpair(hp).value^);
  1368. if tai_symbolpair(hp).kind=spk_set_global then
  1369. begin
  1370. writer.AsmWrite(#9'.globl ');
  1371. writer.AsmWriteLn(tai_symbolpair(hp).sym^);
  1372. end;
  1373. if (tf_needs_symbol_type in target_info.flags) then
  1374. begin
  1375. writer.AsmWrite(#9'.type'#9 + tai_symbolpair(hp).sym^);
  1376. writer.AsmWriteLn(',' + sepChar + 'function');
  1377. end;
  1378. end;
  1379. end;
  1380. ait_symbol_end :
  1381. begin
  1382. if tf_needs_symbol_size in target_info.flags then
  1383. begin
  1384. s:=asminfo^.labelprefix+'e'+tostr(symendcount);
  1385. inc(symendcount);
  1386. writer.AsmWriteLn(s+':');
  1387. writer.AsmWrite(#9'.size'#9);
  1388. if (target_info.system=system_powerpc64_linux) and
  1389. use_dotted_functions and
  1390. (tai_symbol_end(hp).sym.typ=AT_FUNCTION) then
  1391. writer.AsmWrite('.');
  1392. if replaceforbidden then
  1393. writer.AsmWrite(ApplyAsmSymbolRestrictions(tai_symbol_end(hp).sym.name))
  1394. else
  1395. writer.AsmWrite(tai_symbol_end(hp).sym.name);
  1396. writer.AsmWrite(', '+s+' - ');
  1397. if (target_info.system=system_powerpc64_linux) and
  1398. use_dotted_functions and
  1399. (tai_symbol_end(hp).sym.typ=AT_FUNCTION) then
  1400. writer.AsmWrite('.');
  1401. if replaceforbidden then
  1402. writer.AsmWriteLn(ApplyAsmSymbolRestrictions(tai_symbol_end(hp).sym.name))
  1403. else
  1404. writer.AsmWriteLn(tai_symbol_end(hp).sym.name);
  1405. end;
  1406. end;
  1407. ait_instruction :
  1408. begin
  1409. WriteInstruction(hp);
  1410. end;
  1411. ait_stab :
  1412. begin
  1413. if assigned(tai_stab(hp).str) then
  1414. begin
  1415. writer.AsmWrite(#9'.'+stabtypestr[tai_stab(hp).stabtype]+' ');
  1416. writer.AsmWritePChar(tai_stab(hp).str);
  1417. writer.AsmLn;
  1418. end;
  1419. end;
  1420. ait_force_line,
  1421. ait_function_name :
  1422. begin
  1423. {$ifdef DEBUG_AGGAS}
  1424. WriteStr(s,hp.typ);
  1425. writer.AsmWriteLn('# '+s);
  1426. {$endif DEBUG_AGGAS}
  1427. end;
  1428. ait_cutobject :
  1429. begin
  1430. {$ifdef DEBUG_AGGAS}
  1431. writer.AsmWriteLn('# ait_cutobject');
  1432. {$endif DEBUG_AGGAS}
  1433. if SmartAsm then
  1434. begin
  1435. { only reset buffer if nothing has changed }
  1436. if not(writer.ClearIfEmpty) then
  1437. begin
  1438. writer.AsmClose;
  1439. DoAssemble;
  1440. writer.AsmCreate(tai_cutobject(hp).place);
  1441. end;
  1442. { avoid empty files }
  1443. while assigned(hp.next) and (tai(hp.next).typ in [ait_cutobject,ait_section,ait_comment]) do
  1444. begin
  1445. if tai(hp.next).typ=ait_section then
  1446. LastSecType:=tai_section(hp.next).sectype;
  1447. hp:=tai(hp.next);
  1448. end;
  1449. if LastSecType<>sec_none then
  1450. WriteSection(LastSecType,'',secorder_default,last_align);
  1451. writer.MarkEmpty;
  1452. end;
  1453. end;
  1454. ait_marker :
  1455. begin
  1456. {$ifdef DEBUG_AGGAS}
  1457. WriteStr(s,tai_marker(hp).Kind);
  1458. writer.AsmWriteLn('# ait_marker, kind: '+s);
  1459. {$endif DEBUG_AGGAS}
  1460. if tai_marker(hp).kind=mark_NoLineInfoStart then
  1461. inc(InlineLevel)
  1462. else if tai_marker(hp).kind=mark_NoLineInfoEnd then
  1463. dec(InlineLevel);
  1464. end;
  1465. ait_directive :
  1466. begin
  1467. WriteDirectiveName(tai_directive(hp).directive);
  1468. if tai_directive(hp).name <>'' then
  1469. begin
  1470. if replaceforbidden then
  1471. writer.AsmWrite(ApplyAsmSymbolRestrictions(tai_directive(hp).name))
  1472. else
  1473. writer.AsmWrite(tai_directive(hp).name);
  1474. end;
  1475. writer.AsmLn;
  1476. end;
  1477. ait_seh_directive :
  1478. begin
  1479. {$ifndef DISABLE_WIN64_SEH}
  1480. writer.AsmWrite(sehdirectivestr[tai_seh_directive(hp).kind]);
  1481. case tai_seh_directive(hp).datatype of
  1482. sd_none:;
  1483. sd_string:
  1484. begin
  1485. writer.AsmWrite(' '+tai_seh_directive(hp).data.name^);
  1486. if (tai_seh_directive(hp).data.flags and 1)<>0 then
  1487. writer.AsmWrite(',@except');
  1488. if (tai_seh_directive(hp).data.flags and 2)<>0 then
  1489. writer.AsmWrite(',@unwind');
  1490. end;
  1491. sd_reg:
  1492. writer.AsmWrite(' '+gas_regname(tai_seh_directive(hp).data.reg));
  1493. sd_offset:
  1494. writer.AsmWrite(' '+tostr(tai_seh_directive(hp).data.offset));
  1495. sd_regoffset:
  1496. writer.AsmWrite(' '+gas_regname(tai_seh_directive(hp).data.reg)+', '+
  1497. tostr(tai_seh_directive(hp).data.offset));
  1498. end;
  1499. writer.AsmLn;
  1500. {$endif DISABLE_WIN64_SEH}
  1501. end;
  1502. ait_cfi:
  1503. begin
  1504. WriteCFI(tai_cfi_base(hp));
  1505. end;
  1506. ait_eabi_attribute:
  1507. begin
  1508. { as of today, vasm does not support the eabi directives }
  1509. if target_asm.id<>as_arm_vasm then
  1510. begin
  1511. case tai_eabi_attribute(hp).eattr_typ of
  1512. eattrtype_dword:
  1513. writer.AsmWrite(#9'.eabi_attribute '+tostr(tai_eabi_attribute(hp).tag)+','+tostr(tai_eabi_attribute(hp).value));
  1514. eattrtype_ntbs:
  1515. begin
  1516. if assigned(tai_eabi_attribute(hp).valuestr) then
  1517. writer.AsmWrite(#9'.eabi_attribute '+tostr(tai_eabi_attribute(hp).tag)+',"'+tai_eabi_attribute(hp).valuestr^+'"')
  1518. else
  1519. writer.AsmWrite(#9'.eabi_attribute '+tostr(tai_eabi_attribute(hp).tag)+',""');
  1520. end
  1521. else
  1522. Internalerror(2019100601);
  1523. end;
  1524. writer.AsmLn;
  1525. end;
  1526. end;
  1527. {$ifdef WASM}
  1528. ait_local:
  1529. begin
  1530. if tai_local(hp).first then
  1531. writer.AsmWrite(#9'.local'#9)
  1532. else
  1533. writer.AsmWrite(', ');
  1534. writer.AsmWrite(gas_wasm_basic_type_str[tai_local(hp).bastyp]);
  1535. if tai_local(hp).last then
  1536. writer.AsmLn;
  1537. end;
  1538. ait_functype:
  1539. WriteFuncTypeDirective(tai_functype(hp));
  1540. ait_importexport:
  1541. WriteImportExport(tai_impexp(hp));
  1542. ait_tagtype:
  1543. WriteTagType(tai_tagtype(hp));
  1544. ait_import_module:
  1545. begin
  1546. writer.AsmWrite(#9'.import_module'#9);
  1547. writer.AsmWrite(tai_import_module(hp).symname);
  1548. writer.AsmWrite(', ');
  1549. writer.AsmWriteLn(tai_import_module(hp).importmodule);
  1550. end;
  1551. ait_import_name:
  1552. begin
  1553. writer.AsmWrite(#9'.import_name'#9);
  1554. writer.AsmWrite(tai_import_name(hp).symname);
  1555. writer.AsmWrite(', ');
  1556. writer.AsmWriteLn(tai_import_name(hp).importname);
  1557. end;
  1558. {$endif WASM}
  1559. else
  1560. if not WriteComments(hp) then
  1561. internalerror(2006012201);
  1562. end;
  1563. lasthp:=hp;
  1564. hp:=tai(hp.next);
  1565. end;
  1566. end;
  1567. procedure TGNUAssembler.WriteExtraHeader;
  1568. begin
  1569. end;
  1570. procedure TGNUAssembler.WriteExtraFooter;
  1571. begin
  1572. end;
  1573. procedure TGNUAssembler.WriteInstruction(hp: tai);
  1574. begin
  1575. InstrWriter.WriteInstruction(hp);
  1576. end;
  1577. procedure TGNUAssembler.WriteWeakSymbolRef(s: tasmsymbol);
  1578. begin
  1579. writer.AsmWrite(#9'.weak ');
  1580. if asminfo^.dollarsign='$' then
  1581. writer.AsmWriteLn(s.name)
  1582. else
  1583. writer.AsmWriteLn(ApplyAsmSymbolRestrictions(s.name))
  1584. end;
  1585. procedure TGNUAssembler.WriteHiddenSymbol(sym: TAsmSymbol);
  1586. begin
  1587. { on Windows/(PE)COFF, global symbols are hidden by default: global
  1588. symbols that are not explicitly exported from an executable/library,
  1589. become hidden }
  1590. if (target_info.system in (systems_windows+systems_wince)) then
  1591. exit;
  1592. if target_info.system in systems_darwin then
  1593. writer.AsmWrite(#9'.private_extern ')
  1594. else
  1595. writer.AsmWrite(#9'.hidden ');
  1596. if asminfo^.dollarsign='$' then
  1597. writer.AsmWriteLn(sym.name)
  1598. else
  1599. writer.AsmWriteLn(ApplyAsmSymbolRestrictions(sym.name))
  1600. end;
  1601. procedure TGNUAssembler.WriteAixStringConst(hp: tai_string);
  1602. type
  1603. tterminationkind = (term_none,term_string,term_nostring);
  1604. var
  1605. i: longint;
  1606. pos: longint;
  1607. s: string;
  1608. ch: char;
  1609. instring: boolean;
  1610. procedure newstatement(terminationkind: tterminationkind);
  1611. begin
  1612. case terminationkind of
  1613. term_none: ;
  1614. term_string:
  1615. writer.AsmWriteLn('"');
  1616. term_nostring:
  1617. writer.AsmLn;
  1618. end;
  1619. writer.AsmWrite(#9'.byte'#9);
  1620. pos:=20;
  1621. instring:=false;
  1622. end;
  1623. begin
  1624. pos:=0;
  1625. instring:=false;
  1626. for i:=1 to hp.len do
  1627. begin
  1628. if pos=0 then
  1629. newstatement(term_none);
  1630. ch:=hp.str[i-1];
  1631. case ch of
  1632. #0..#31,
  1633. #127..#255 :
  1634. begin
  1635. if instring then
  1636. newstatement(term_string);
  1637. if pos=20 then
  1638. s:=tostr(ord(ch))
  1639. else
  1640. s:=', '+tostr(ord(ch))
  1641. end;
  1642. '"' :
  1643. if instring then
  1644. s:='""'
  1645. else
  1646. begin
  1647. if pos<>20 then
  1648. newstatement(term_nostring);
  1649. s:='"""';
  1650. instring:=true;
  1651. end;
  1652. else
  1653. if not instring then
  1654. begin
  1655. if (pos<>20) then
  1656. newstatement(term_nostring);
  1657. s:='"'+ch;
  1658. instring:=true;
  1659. end
  1660. else
  1661. s:=ch;
  1662. end;
  1663. writer.AsmWrite(s);
  1664. inc(pos,length(s));
  1665. if (pos>line_length) or (i=tai_string(hp).len) then
  1666. begin
  1667. if instring then
  1668. writer.AsmWriteLn('"')
  1669. else
  1670. writer.AsmLn;
  1671. pos:=0;
  1672. end;
  1673. end;
  1674. end;
  1675. procedure TGNUAssembler.WriteAixIntConst(hp: tai_const);
  1676. var
  1677. pos, size: longint;
  1678. begin
  1679. { only big endian AIX supported for now }
  1680. if target_info.endian<>endian_big then
  1681. internalerror(2012010401);
  1682. { limitation: can only write 4 bytes at a time }
  1683. pos:=0;
  1684. size:=tai_const(hp).size;
  1685. while pos<(size-4) do
  1686. begin
  1687. writer.AsmWrite(#9'.vbyte'#9'4, ');
  1688. writer.AsmWriteln(tostr(longint(tai_const(hp).value shr ((size-pos-4)*8))));
  1689. inc(pos,4);
  1690. end;
  1691. writer.AsmWrite(#9'.vbyte'#9);
  1692. writer.AsmWrite(tostr(size-pos));
  1693. writer.AsmWrite(', ');
  1694. case size-pos of
  1695. 1: writer.AsmWrite(tostr(byte(tai_const(hp).value)));
  1696. 2: writer.AsmWrite(tostr(word(tai_const(hp).value)));
  1697. 4: writer.AsmWrite(tostr(longint(tai_const(hp).value)));
  1698. else
  1699. internalerror(2012010402);
  1700. end;
  1701. end;
  1702. procedure TGNUAssembler.WriteUnalignedIntConst(hp: tai_const);
  1703. var
  1704. pos, size: longint;
  1705. begin
  1706. size:=tai_const(hp).size;
  1707. writer.AsmWrite(#9'.byte'#9);
  1708. if target_info.endian=endian_big then
  1709. begin
  1710. pos:=size-1;
  1711. while pos>=0 do
  1712. begin
  1713. writer.AsmWrite(tostr((tai_const(hp).value shr (pos*8)) and $ff));
  1714. dec(pos);
  1715. if pos>=0 then
  1716. writer.AsmWrite(', ')
  1717. else
  1718. writer.AsmLn;
  1719. end;
  1720. end
  1721. else
  1722. begin
  1723. pos:=0;
  1724. while pos<size do
  1725. begin
  1726. writer.AsmWriteln(tostr((tai_const(hp).value shr (pos*8)) and $ff));
  1727. inc(pos);
  1728. if pos<=size then
  1729. writer.AsmWrite(', ')
  1730. else
  1731. writer.AsmLn;
  1732. end;
  1733. end;
  1734. writer.AsmLn;
  1735. end;
  1736. procedure TGNUAssembler.WriteDirectiveName(dir: TAsmDirective);
  1737. begin
  1738. { TODO: implement asd_cpu for GAS => usually .arch or .cpu, but the CPU
  1739. name has to be translated as well }
  1740. if dir=asd_cpu then
  1741. writer.AsmWrite(asminfo^.comment+' CPU ')
  1742. else
  1743. writer.AsmWrite('.'+directivestr[dir]+' ');
  1744. end;
  1745. procedure TGNUAssembler.WriteAsmList;
  1746. var
  1747. n : string;
  1748. hal : tasmlisttype;
  1749. i: longint;
  1750. begin
  1751. {$ifdef EXTDEBUG}
  1752. if current_module.mainsource<>'' then
  1753. Comment(V_Debug,'Start writing gas-styled assembler output for '+current_module.mainsource);
  1754. {$endif}
  1755. if current_module.mainsource<>'' then
  1756. n:=ExtractFileName(current_module.mainsource)
  1757. else
  1758. n:=InputFileName;
  1759. { gcc does not add it either for Darwin. Grep for
  1760. TARGET_ASM_FILE_START_FILE_DIRECTIVE in gcc/config/*.h
  1761. }
  1762. if not(target_info.system in systems_darwin) then
  1763. writer.AsmWriteLn(#9'.file "'+FixFileName(n)+'"');
  1764. WriteExtraHeader;
  1765. writer.MarkEmpty;
  1766. symendcount:=0;
  1767. for hal:=low(TasmlistType) to high(TasmlistType) do
  1768. begin
  1769. if not (current_asmdata.asmlists[hal].empty) then
  1770. begin
  1771. writer.AsmWriteLn(asminfo^.comment+'Begin asmlist '+AsmlistTypeStr[hal]);
  1772. writetree(current_asmdata.asmlists[hal]);
  1773. writer.AsmWriteLn(asminfo^.comment+'End asmlist '+AsmlistTypeStr[hal]);
  1774. end;
  1775. end;
  1776. { add weak symbol markers }
  1777. for i:=0 to current_asmdata.asmsymboldict.count-1 do
  1778. if (tasmsymbol(current_asmdata.asmsymboldict[i]).bind=AB_WEAK_EXTERNAL) then
  1779. WriteWeakSymbolRef(tasmsymbol(current_asmdata.asmsymboldict[i]));
  1780. if create_smartlink_sections and
  1781. (target_info.system in systems_darwin) then
  1782. writer.AsmWriteLn(#9'.subsections_via_symbols');
  1783. { "no executable stack" marker }
  1784. { TODO: used by OpenBSD/NetBSD as well? }
  1785. if (target_info.system in (systems_linux + systems_android + systems_freebsd + systems_dragonfly)) and
  1786. not(cs_executable_stack in current_settings.moduleswitches) then
  1787. begin
  1788. writer.AsmWriteLn('.section .note.GNU-stack,"",%progbits');
  1789. end;
  1790. writer.AsmLn;
  1791. WriteExtraFooter;
  1792. {$ifdef EXTDEBUG}
  1793. if current_module.mainsource<>'' then
  1794. Comment(V_Debug,'Done writing gas-styled assembler output for '+current_module.mainsource);
  1795. {$endif EXTDEBUG}
  1796. end;
  1797. {****************************************************************************}
  1798. { Apple/GNU Assembler writer }
  1799. {****************************************************************************}
  1800. function TAppleGNUAssembler.sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;
  1801. begin
  1802. if (target_info.system in systems_darwin) then
  1803. case atype of
  1804. sec_user:
  1805. begin
  1806. result:='.section '+aname;
  1807. exit;
  1808. end;
  1809. sec_bss:
  1810. { all bss (lcomm) symbols are automatically put in the right }
  1811. { place by using the lcomm assembler directive }
  1812. atype := sec_none;
  1813. sec_debug_frame,
  1814. sec_eh_frame:
  1815. begin
  1816. result := '.section __DWARF,__debug_info,regular,debug';
  1817. exit;
  1818. end;
  1819. sec_debug_line:
  1820. begin
  1821. result := '.section __DWARF,__debug_line,regular,debug';
  1822. exit;
  1823. end;
  1824. sec_debug_info:
  1825. begin
  1826. result := '.section __DWARF,__debug_info,regular,debug';
  1827. exit;
  1828. end;
  1829. sec_debug_abbrev:
  1830. begin
  1831. result := '.section __DWARF,__debug_abbrev,regular,debug';
  1832. exit;
  1833. end;
  1834. sec_debug_aranges:
  1835. begin
  1836. result := '.section __DWARF,__debug_aranges,regular,debug';
  1837. exit;
  1838. end;
  1839. sec_debug_ranges:
  1840. begin
  1841. result := '.section __DWARF,__debug_ranges,regular,debug';
  1842. exit;
  1843. end;
  1844. sec_rodata:
  1845. begin
  1846. result := '.const_data';
  1847. exit;
  1848. end;
  1849. sec_rodata_norel:
  1850. begin
  1851. result := '.const';
  1852. exit;
  1853. end;
  1854. sec_fpc:
  1855. begin
  1856. result := '.section __TEXT, .fpc, regular, no_dead_strip';
  1857. exit;
  1858. end;
  1859. sec_code:
  1860. begin
  1861. if (aname='fpc_geteipasebx') or
  1862. (aname='fpc_geteipasecx') then
  1863. begin
  1864. result:='.section __TEXT,__textcoal_nt,coalesced,pure_instructions'#10'.weak_definition '+aname+
  1865. #10'.private_extern '+aname;
  1866. exit;
  1867. end;
  1868. end;
  1869. sec_data_nonlazy:
  1870. begin
  1871. result:='.section __DATA, __nl_symbol_ptr,non_lazy_symbol_pointers';
  1872. exit;
  1873. end;
  1874. sec_data_lazy:
  1875. begin
  1876. result:='.section __DATA, __la_symbol_ptr,lazy_symbol_pointers';
  1877. exit;
  1878. end;
  1879. sec_init_func:
  1880. begin
  1881. result:='.section __DATA, __mod_init_func, mod_init_funcs';
  1882. exit;
  1883. end;
  1884. sec_term_func:
  1885. begin
  1886. result:='.section __DATA, __mod_term_func, mod_term_funcs';
  1887. exit;
  1888. end;
  1889. low(TObjCAsmSectionType)..high(TObjCAsmSectionType):
  1890. begin
  1891. result:='.section '+objc_section_name(atype);
  1892. exit
  1893. end;
  1894. else
  1895. ;
  1896. end;
  1897. result := inherited sectionname(atype,aname,aorder);
  1898. end;
  1899. procedure TAppleGNUAssembler.WriteWeakSymbolRef(s: tasmsymbol);
  1900. begin
  1901. writer.AsmWriteLn(#9'.weak_reference '+s.name);
  1902. end;
  1903. procedure TAppleGNUAssembler.WriteDirectiveName(dir: TAsmDirective);
  1904. begin
  1905. case dir of
  1906. asd_weak_reference:
  1907. writer.AsmWrite('.weak_reference ');
  1908. asd_weak_definition:
  1909. writer.AsmWrite('.weak_definition ');
  1910. else
  1911. inherited;
  1912. end;
  1913. end;
  1914. {****************************************************************************}
  1915. { a.out/GNU Assembler writer }
  1916. {****************************************************************************}
  1917. function TAoutGNUAssembler.sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;
  1918. const
  1919. (* Translation table - replace unsupported section types with basic ones. *)
  1920. SecXTable: array[TAsmSectionType] of TAsmSectionType = (
  1921. sec_none,
  1922. sec_none,
  1923. sec_code,
  1924. sec_data,
  1925. sec_data (* sec_rodata *),
  1926. sec_data (* sec_rodata_norel *),
  1927. sec_bss,
  1928. sec_data (* sec_threadvar *),
  1929. { used for wince exception handling }
  1930. sec_code (* sec_pdata *),
  1931. { used for darwin import stubs }
  1932. sec_code (* sec_stub *),
  1933. sec_data,(* sec_data_nonlazy *)
  1934. sec_data,(* sec_data_lazy *)
  1935. sec_data,(* sec_init_func *)
  1936. sec_data,(* sec_term_func *)
  1937. { stabs }
  1938. sec_stab,sec_stabstr,
  1939. { win32 }
  1940. sec_data (* sec_idata2 *),
  1941. sec_data (* sec_idata4 *),
  1942. sec_data (* sec_idata5 *),
  1943. sec_data (* sec_idata6 *),
  1944. sec_data (* sec_idata7 *),
  1945. sec_data (* sec_edata *),
  1946. { C++ exception handling unwinding (uses dwarf) }
  1947. sec_eh_frame,
  1948. { dwarf }
  1949. sec_debug_frame,
  1950. sec_debug_info,
  1951. sec_debug_line,
  1952. sec_debug_abbrev,
  1953. sec_debug_aranges,
  1954. sec_debug_ranges,
  1955. { ELF resources (+ references to stabs debug information sections) }
  1956. sec_code (* sec_fpc *),
  1957. { Table of contents section }
  1958. sec_code (* sec_toc *),
  1959. sec_code (* sec_init *),
  1960. sec_code (* sec_fini *),
  1961. sec_none (* sec_objc_class *),
  1962. sec_none (* sec_objc_meta_class *),
  1963. sec_none (* sec_objc_cat_cls_meth *),
  1964. sec_none (* sec_objc_cat_inst_meth *),
  1965. sec_none (* sec_objc_protocol *),
  1966. sec_none (* sec_objc_string_object *),
  1967. sec_none (* sec_objc_cls_meth *),
  1968. sec_none (* sec_objc_inst_meth *),
  1969. sec_none (* sec_objc_cls_refs *),
  1970. sec_none (* sec_objc_message_refs *),
  1971. sec_none (* sec_objc_symbols *),
  1972. sec_none (* sec_objc_category *),
  1973. sec_none (* sec_objc_class_vars *),
  1974. sec_none (* sec_objc_instance_vars *),
  1975. sec_none (* sec_objc_module_info *),
  1976. sec_none (* sec_objc_class_names *),
  1977. sec_none (* sec_objc_meth_var_types *),
  1978. sec_none (* sec_objc_meth_var_names *),
  1979. sec_none (* sec_objc_selector_strs *),
  1980. sec_none (* sec_objc_protocol_ext *),
  1981. sec_none (* sec_objc_class_ext *),
  1982. sec_none (* sec_objc_property *),
  1983. sec_none (* sec_objc_image_info *),
  1984. sec_none (* sec_objc_cstring_object *),
  1985. sec_none (* sec_objc_sel_fixup *),
  1986. sec_none (* sec_objc_data *),
  1987. sec_none (* sec_objc_const *),
  1988. sec_none (* sec_objc_sup_refs *),
  1989. sec_none (* sec_data_coalesced *),
  1990. sec_none (* sec_objc_classlist *),
  1991. sec_none (* sec_objc_nlclasslist *),
  1992. sec_none (* sec_objc_catlist *),
  1993. sec_none (* sec_objc_nlcatlist *),
  1994. sec_none (* sec_objc_protlist *),
  1995. sec_none (* sec_stack *),
  1996. sec_none (* sec_heap *),
  1997. sec_none (* gcc_except_table *),
  1998. sec_none (* sec_arm_attribute *)
  1999. );
  2000. begin
  2001. Result := inherited SectionName (SecXTable [AType], AName, AOrder);
  2002. end;
  2003. {****************************************************************************}
  2004. { Abstract Instruction Writer }
  2005. {****************************************************************************}
  2006. constructor TCPUInstrWriter.create(_owner: TGNUAssembler);
  2007. begin
  2008. inherited create;
  2009. owner := _owner;
  2010. end;
  2011. end.