aasmbase.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. This unit implements an abstract asmoutput class for all processor types
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. { @abstract(This unit implements an abstract asm output class for all processor types)
  18. This unit implements an abstract assembler output class for all processors, these
  19. are then overridden for each assembler writer to actually write the data in these
  20. classes to an assembler file.
  21. }
  22. unit aasmbase;
  23. {$i fpcdefs.inc}
  24. interface
  25. uses
  26. cutils,cclasses,
  27. globtype,globals,systems
  28. ;
  29. type
  30. TAsmsymbind=(
  31. AB_NONE,AB_EXTERNAL,AB_COMMON,AB_LOCAL,AB_GLOBAL,AB_WEAK_EXTERNAL,
  32. { global in the current program/library, but not visible outside it
  33. (= "hidden" in ELF) }
  34. AB_PRIVATE_EXTERN,
  35. AB_LAZY,AB_IMPORT,
  36. { a symbol that's internal to the compiler and used as a temp }
  37. AB_TEMP,
  38. { a global symbol that points to another global symbol and is only used
  39. to allow indirect loading in case of packages and indirect imports }
  40. AB_INDIRECT,AB_EXTERNAL_INDIRECT);
  41. TAsmsymtype=(
  42. AT_NONE,AT_FUNCTION,AT_DATA,AT_SECTION,AT_LABEL,
  43. {
  44. the address of this code label is taken somewhere in the code
  45. so it must be taken care of it when creating pic
  46. }
  47. AT_ADDR,
  48. { Label for debug or other non-program information }
  49. AT_METADATA,
  50. { label for data that must always be accessed indirectly, because it
  51. is handled explcitely in the system unit or (e.g. RTTI and threadvar
  52. tables) -- never seen in an assembler/assembler writer, always
  53. changed to AT_DATA }
  54. AT_DATA_FORCEINDIRECT,
  55. { don't generate an implicit indirect symbol as that might be provided
  56. by other means (e.g. the typed const builder) to ensure a correct
  57. section name }
  58. AT_DATA_NOINDIRECT,
  59. { Thread-local symbol (ELF targets) }
  60. AT_TLS,
  61. { GNU indirect function (ELF targets) }
  62. AT_GNU_IFUNC
  63. );
  64. { is the label only there for getting an DataOffset (e.g. for i/o
  65. checks -> alt_addr) or is it a jump target (alt_jump), for debug
  66. info alt_dbgline and alt_dbgfile, etc. }
  67. TAsmLabelType = (alt_jump,alt_addr,alt_data,alt_dbgline,alt_dbgfile,alt_dbgtype,alt_dbgframe,alt_eh_begin,alt_eh_end);
  68. const
  69. asmlabeltypeprefix : array[TAsmLabeltype] of string[2] = ('j','a','d','l','f','t','c','eb','ee');
  70. asmsymbindname : array[TAsmsymbind] of string[23] = ('none', 'external','common',
  71. 'local','global','weak external','private external','lazy','import','internal temp',
  72. 'indirect','external indirect');
  73. asmsymbindindirect = [AB_INDIRECT,AB_EXTERNAL_INDIRECT];
  74. type
  75. TAsmSectiontype=(sec_none,
  76. { this section type allows to define a user named section }
  77. sec_user,
  78. sec_code,
  79. sec_data,
  80. { read-only, but may contain relocations }
  81. sec_rodata,
  82. { read-only and cannot contain relocations }
  83. sec_rodata_norel,
  84. sec_bss,
  85. sec_threadvar,
  86. { used for wince exception handling }
  87. sec_pdata,
  88. { used for darwin import stubs }
  89. sec_stub,
  90. sec_data_nonlazy,
  91. sec_data_lazy,
  92. sec_init_func,
  93. sec_term_func,
  94. { stabs }
  95. sec_stab,sec_stabstr,
  96. { win32 }
  97. sec_idata2,sec_idata4,sec_idata5,sec_idata6,sec_idata7,sec_edata,
  98. { C++ exception handling unwinding (uses dwarf) }
  99. sec_eh_frame,
  100. { dwarf }
  101. sec_debug_frame,
  102. sec_debug_info,
  103. sec_debug_line,
  104. sec_debug_abbrev,
  105. sec_debug_aranges,
  106. sec_debug_ranges,
  107. { Yury: "sec_fpc is intended for storing fpc specific data
  108. which must be recognized and processed specially by linker.
  109. Currently fpc version string, dummy links to stab sections
  110. and elf resources are stored in .fpc sections."
  111. "If special .fpc section cannot be used on some target,
  112. .text can be used instead." }
  113. sec_fpc,
  114. { Table of contents section }
  115. sec_toc,
  116. sec_init,
  117. sec_fini,
  118. {Objective-C common and fragile ABI }
  119. sec_objc_class,
  120. sec_objc_meta_class,
  121. sec_objc_cat_cls_meth,
  122. sec_objc_cat_inst_meth,
  123. sec_objc_protocol,
  124. sec_objc_string_object,
  125. sec_objc_cls_meth,
  126. sec_objc_inst_meth,
  127. sec_objc_cls_refs,
  128. sec_objc_message_refs,
  129. sec_objc_symbols,
  130. sec_objc_category,
  131. sec_objc_class_vars,
  132. sec_objc_instance_vars,
  133. sec_objc_module_info,
  134. sec_objc_class_names,
  135. sec_objc_meth_var_types,
  136. sec_objc_meth_var_names,
  137. sec_objc_selector_strs,
  138. sec_objc_protocol_ext,
  139. sec_objc_class_ext,
  140. sec_objc_property,
  141. sec_objc_image_info,
  142. sec_objc_cstring_object,
  143. sec_objc_sel_fixup,
  144. { Objective-C non-fragile ABI }
  145. sec_objc_data,
  146. sec_objc_const,
  147. sec_objc_sup_refs,
  148. sec_data_coalesced,
  149. sec_objc_classlist,
  150. sec_objc_nlclasslist,
  151. sec_objc_catlist,
  152. sec_objc_nlcatlist,
  153. sec_objc_protolist,
  154. { stack segment for 16-bit DOS }
  155. sec_stack,
  156. { initial heap segment for 16-bit DOS }
  157. sec_heap,
  158. { dwarf based/gcc style exception handling }
  159. sec_gcc_except_table,
  160. sec_arm_attribute
  161. );
  162. TObjCAsmSectionType = sec_objc_class..sec_objc_protolist;
  163. TAsmSectionOrder = (secorder_begin,secorder_default,secorder_end);
  164. TSectionFlag = (SF_A,SF_W,SF_X);
  165. TSectionFlags = set of TSectionFlag;
  166. TSectionProgbits = (SPB_None,SPB_PROGBITS,SPB_NOBITS,SPB_NOTE,SPB_ARM_ATTRIBUTES);
  167. TAsmSymbol = class(TFPHashObject)
  168. private
  169. { this need to be incremented with every symbol loading into the
  170. TAsmList with loadsym/loadref/const_symbol (PFV) }
  171. refs : longint;
  172. public
  173. { on avr the compiler needs to replace cond. jumps with too large offsets
  174. so we have to store an offset somewhere to calculate jump distances }
  175. {$ifdef AVR}
  176. offset : longint;
  177. {$endif AVR}
  178. bind : TAsmsymbind;
  179. typ : TAsmsymtype;
  180. {$ifdef llvm}
  181. { have we generated a declaration for this symbol? }
  182. declared : boolean;
  183. {$endif llvm}
  184. { Alternate symbol which can be used for 'renaming' needed for
  185. asm inlining. Also used for external and common solving during linking }
  186. altsymbol : TAsmSymbol;
  187. { Cached objsymbol }
  188. cachedobjsymbol : TObject;
  189. constructor Create(AList:TFPHashObjectList;const s:TSymStr;_bind:TAsmsymbind;_typ:Tasmsymtype);
  190. function getaltcopy(AList:TFPHashObjectList;altnr: longint): TAsmSymbol; virtual;
  191. function is_used:boolean;
  192. procedure increfs;
  193. procedure decrefs;
  194. function getrefs: longint;
  195. end;
  196. TAsmSymbolClass = class of TAsmSymbol;
  197. TAsmLabel = class(TAsmSymbol)
  198. protected
  199. function getname:TSymStr;override;
  200. {$push}{$warnings off}
  201. { new visibility section to let "warnings off" take effect }
  202. protected
  203. { this constructor is only supposed to be used internally by
  204. createstatoc/createlocal -> disable warning that constructors should
  205. be public }
  206. constructor create_non_global(AList: TFPHashObjectList; nr: longint; ltyp: TAsmLabelType; const prefix: TSymStr);
  207. public
  208. {$pop}
  209. labelnr : longint;
  210. labeltype : TAsmLabelType;
  211. is_set : boolean;
  212. is_public : boolean;
  213. defined_in_asmstatement : boolean;
  214. constructor Createlocal(AList: TFPHashObjectList; nr: longint; ltyp: TAsmLabelType);
  215. constructor Createstatic(AList: TFPHashObjectList; nr: longint; ltyp: TAsmLabelType);
  216. constructor Createglobal(AList: TFPHashObjectList; const modulename: TSymStr; nr: longint; ltyp: TAsmLabelType);
  217. function getaltcopy(AList:TFPHashObjectList;altnr: longint): TAsmSymbol; override;
  218. end;
  219. function create_smartlink_sections:boolean;inline;
  220. function create_smartlink_library:boolean;inline;
  221. function create_smartlink:boolean;inline;
  222. function ReplaceForbiddenAsmSymbolChars(const s: ansistring): ansistring;
  223. { dummy default noop callback }
  224. procedure default_global_used;
  225. type
  226. { Procedure variable to allow for special handling of
  227. the occurence of use of a global variable,
  228. used by PIC code generation to request GOT loading }
  229. TGlobalUsedProcedure = procedure;
  230. const
  231. global_used : TGlobalUsedProcedure = @default_global_used;
  232. implementation
  233. uses
  234. verbose;
  235. function create_smartlink_sections:boolean;inline;
  236. begin
  237. result:=(af_smartlink_sections in target_asm.flags) and
  238. (tf_smartlink_sections in target_info.flags);
  239. end;
  240. function create_smartlink_library:boolean;inline;
  241. begin
  242. result:=(cs_Create_smart in current_settings.moduleswitches) and
  243. (tf_smartlink_library in target_info.flags) and
  244. not create_smartlink_sections;
  245. end;
  246. function create_smartlink:boolean;inline;
  247. begin
  248. result:=(
  249. (af_smartlink_sections in target_asm.flags) and
  250. (tf_smartlink_sections in target_info.flags)
  251. ) or
  252. (
  253. (cs_Create_smart in current_settings.moduleswitches) and
  254. (tf_smartlink_library in target_info.flags)
  255. );
  256. end;
  257. function ReplaceForbiddenAsmSymbolChars(const s: ansistring): ansistring;
  258. var
  259. i : longint;
  260. rchar: char;
  261. begin
  262. Result:=s;
  263. rchar:=target_asm.dollarsign;
  264. for i:=1 to Length(Result) do
  265. if Result[i]='$' then
  266. Result[i]:=rchar;
  267. end;
  268. {*****************************************************************************
  269. TAsmSymbol
  270. *****************************************************************************}
  271. constructor TAsmSymbol.Create(AList:TFPHashObjectList;const s:TSymStr;_bind:TAsmsymbind;_typ:Tasmsymtype);
  272. begin;
  273. inherited Create(AList,s);
  274. bind:=_bind;
  275. typ:=_typ;
  276. { used to remove unused labels from the al_procedures }
  277. refs:=0;
  278. end;
  279. function TAsmSymbol.getaltcopy(AList:TFPHashObjectList;altnr: longint): TAsmSymbol;
  280. begin
  281. result := TAsmSymbol(TAsmSymbolClass(classtype).Create(AList,name+'_'+tostr(altnr),bind,typ));
  282. end;
  283. function TAsmSymbol.is_used:boolean;
  284. begin
  285. is_used:=(refs>0);
  286. end;
  287. procedure TAsmSymbol.increfs;
  288. begin
  289. inc(refs);
  290. end;
  291. procedure TAsmSymbol.decrefs;
  292. begin
  293. dec(refs);
  294. if refs<0 then
  295. internalerror(200211121);
  296. end;
  297. function TAsmSymbol.getrefs: longint;
  298. begin
  299. getrefs := refs;
  300. end;
  301. {*****************************************************************************
  302. TAsmLabel
  303. *****************************************************************************}
  304. constructor TAsmLabel.Createlocal(AList: TFPHashObjectList; nr: longint; ltyp: TAsmLabelType);
  305. begin
  306. create_non_global(AList,nr,ltyp,target_asm.labelprefix);
  307. end;
  308. constructor TAsmLabel.Createstatic(AList:TFPHashObjectList;nr:longint;ltyp:TAsmLabelType);
  309. begin
  310. create_non_global(AList,nr,ltyp,'_$$fpclocal$_l');
  311. end;
  312. constructor TAsmLabel.Createglobal(AList:TFPHashObjectList;const modulename:TSymStr;nr:longint;ltyp:TAsmLabelType);
  313. begin
  314. inherited Create(AList,'_$'+modulename+'$_L'+asmlabeltypeprefix[ltyp]+tostr(nr),AB_GLOBAL,AT_DATA);
  315. labelnr:=nr;
  316. labeltype:=ltyp;
  317. is_set:=false;
  318. { write it always }
  319. increfs;
  320. global_used;
  321. end;
  322. function TAsmLabel.getaltcopy(AList:TFPHashObjectList;altnr: longint): TAsmSymbol;
  323. begin;
  324. result := inherited getaltcopy(AList,altnr);
  325. TAsmLabel(result).labelnr:=labelnr;
  326. TAsmLabel(result).labeltype:=labeltype;
  327. TAsmLabel(result).is_set:=false;
  328. case bind of
  329. AB_GLOBAL,
  330. AB_PRIVATE_EXTERN:
  331. result.increfs;
  332. AB_LOCAL:
  333. ;
  334. else
  335. internalerror(2006053101);
  336. end;
  337. end;
  338. function TAsmLabel.getname:TSymStr;
  339. begin
  340. getname:=inherited getname;
  341. increfs;
  342. end;
  343. constructor TAsmLabel.create_non_global(AList: TFPHashObjectList; nr: longint; ltyp: TAsmLabelType; const prefix: TSymStr);
  344. var
  345. asmtyp: TAsmsymtype;
  346. begin
  347. case ltyp of
  348. alt_addr:
  349. asmtyp:=AT_ADDR;
  350. alt_data:
  351. asmtyp:=AT_DATA;
  352. else
  353. asmtyp:=AT_LABEL;
  354. end;
  355. inherited Create(AList,prefix+asmlabeltypeprefix[ltyp]+tostr(nr),AB_LOCAL,asmtyp);
  356. labelnr:=nr;
  357. labeltype:=ltyp;
  358. is_set:=false;
  359. end;
  360. procedure default_global_used;
  361. begin
  362. end;
  363. end.