aasmbase.pas 15 KB

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