aasmbase.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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. { on avr the compiler needs to replace cond. jumps with too large offsets
  181. so we have to store an offset somewhere to calculate jump distances }
  182. {$ifdef AVR}
  183. offset : longint;
  184. {$endif AVR}
  185. bind : TAsmsymbind;
  186. typ : TAsmsymtype;
  187. {$ifdef llvm}
  188. { have we generated a declaration for this symbol? }
  189. declared : boolean;
  190. {$endif llvm}
  191. { Alternate symbol which can be used for 'renaming' needed for
  192. asm inlining. Also used for external and common solving during linking }
  193. altsymbol : TAsmSymbol;
  194. { Cached objsymbol }
  195. cachedobjsymbol : TObject;
  196. constructor Create(AList:TFPHashObjectList;const s:TSymStr;_bind:TAsmsymbind;_typ:Tasmsymtype);
  197. function getaltcopy(AList:TFPHashObjectList;altnr: longint): TAsmSymbol; virtual;
  198. function is_used:boolean;
  199. procedure increfs;
  200. procedure decrefs;
  201. function getrefs: longint;
  202. end;
  203. TAsmSymbolClass = class of TAsmSymbol;
  204. TAsmLabel = class(TAsmSymbol)
  205. protected
  206. function getname:TSymStr;override;
  207. {$push}{$warnings off}
  208. { new visibility section to let "warnings off" take effect }
  209. protected
  210. { this constructor is only supposed to be used internally by
  211. createstatoc/createlocal -> disable warning that constructors should
  212. be public }
  213. constructor create_non_global(AList: TFPHashObjectList; nr: longint; ltyp: TAsmLabelType; const prefix: TSymStr);
  214. public
  215. {$pop}
  216. labelnr : longint;
  217. labeltype : TAsmLabelType;
  218. is_set : boolean;
  219. is_public : boolean;
  220. defined_in_asmstatement : boolean;
  221. constructor Createlocal(AList: TFPHashObjectList; nr: longint; ltyp: TAsmLabelType);
  222. constructor Createstatic(AList: TFPHashObjectList; nr: longint; ltyp: TAsmLabelType);
  223. constructor Createglobal(AList: TFPHashObjectList; const modulename: TSymStr; nr: longint; ltyp: TAsmLabelType);
  224. function getaltcopy(AList:TFPHashObjectList;altnr: longint): TAsmSymbol; override;
  225. end;
  226. function create_smartlink_sections:boolean;inline;
  227. function create_smartlink_library:boolean;inline;
  228. function create_smartlink:boolean;inline;
  229. function ApplyAsmSymbolRestrictions(const s: ansistring): ansistring;
  230. { dummy default noop callback }
  231. procedure default_global_used;
  232. type
  233. { Procedure variable to allow for special handling of
  234. the occurence of use of a global variable,
  235. used by PIC code generation to request GOT loading }
  236. TGlobalUsedProcedure = procedure;
  237. const
  238. global_used : TGlobalUsedProcedure = @default_global_used;
  239. implementation
  240. uses
  241. verbose,fpccrc;
  242. function create_smartlink_sections:boolean;inline;
  243. begin
  244. result:=(af_smartlink_sections in target_asm.flags) and
  245. (tf_smartlink_sections in target_info.flags);
  246. end;
  247. function create_smartlink_library:boolean;inline;
  248. begin
  249. result:=(cs_Create_smart in current_settings.moduleswitches) and
  250. (tf_smartlink_library in target_info.flags) and
  251. not create_smartlink_sections;
  252. end;
  253. function create_smartlink:boolean;inline;
  254. begin
  255. result:=(
  256. (af_smartlink_sections in target_asm.flags) and
  257. (tf_smartlink_sections in target_info.flags)
  258. ) or
  259. (
  260. (cs_Create_smart in current_settings.moduleswitches) and
  261. (tf_smartlink_library in target_info.flags)
  262. );
  263. end;
  264. function ApplyAsmSymbolRestrictions(const s: ansistring): ansistring;
  265. var
  266. i : longint;
  267. rchar, ochar: char;
  268. crc: Cardinal;
  269. charstoremove: integer;
  270. begin
  271. Result:=s;
  272. rchar:=target_asm.dollarsign;
  273. if target_asm.id=as_i386_wasm then
  274. ochar:='.'
  275. else
  276. ochar:='$';
  277. if (ochar<>rchar) then
  278. for i:=1 to Length(Result) do
  279. if Result[i]=ochar then
  280. Result[i]:=rchar;
  281. if (target_asm.labelmaxlen<>-1) and (Length(Result)>target_asm.labelmaxlen) then
  282. begin
  283. crc:=0;
  284. crc:=UpdateCrc32(crc,Result[1],Length(Result));
  285. charstoremove:=Length(Result)-target_asm.labelmaxlen+13;
  286. Delete(Result,(Length(Result)-charstoremove) div 2,charstoremove);
  287. Result:='_'+target_asm.dollarsign+'CRC'+hexstr(crc,8)+Result;
  288. if Length(Result)>target_asm.labelmaxlen then
  289. Internalerror(2020042502);
  290. end;
  291. end;
  292. {*****************************************************************************
  293. TAsmSymbol
  294. *****************************************************************************}
  295. constructor TAsmSymbol.Create(AList:TFPHashObjectList;const s:TSymStr;_bind:TAsmsymbind;_typ:Tasmsymtype);
  296. begin;
  297. inherited Create(AList,s);
  298. bind:=_bind;
  299. typ:=_typ;
  300. { used to remove unused labels from the al_procedures }
  301. refs:=0;
  302. end;
  303. function TAsmSymbol.getaltcopy(AList:TFPHashObjectList;altnr: longint): TAsmSymbol;
  304. begin
  305. result := TAsmSymbol(TAsmSymbolClass(classtype).Create(AList,name+'_'+tostr(altnr),bind,typ));
  306. end;
  307. function TAsmSymbol.is_used:boolean;
  308. begin
  309. is_used:=(refs>0);
  310. end;
  311. procedure TAsmSymbol.increfs;
  312. begin
  313. inc(refs);
  314. end;
  315. procedure TAsmSymbol.decrefs;
  316. begin
  317. dec(refs);
  318. if refs<0 then
  319. internalerror(200211121);
  320. end;
  321. function TAsmSymbol.getrefs: longint;
  322. begin
  323. getrefs := refs;
  324. end;
  325. {*****************************************************************************
  326. TAsmLabel
  327. *****************************************************************************}
  328. constructor TAsmLabel.Createlocal(AList: TFPHashObjectList; nr: longint; ltyp: TAsmLabelType);
  329. begin
  330. create_non_global(AList,nr,ltyp,target_asm.labelprefix);
  331. end;
  332. constructor TAsmLabel.Createstatic(AList:TFPHashObjectList;nr:longint;ltyp:TAsmLabelType);
  333. begin
  334. create_non_global(AList,nr,ltyp,'_$$fpclocal$_l');
  335. end;
  336. constructor TAsmLabel.Createglobal(AList:TFPHashObjectList;const modulename:TSymStr;nr:longint;ltyp:TAsmLabelType);
  337. begin
  338. inherited Create(AList,'_$'+modulename+'$_L'+asmlabeltypeprefix[ltyp]+tostr(nr),AB_GLOBAL,AT_DATA);
  339. labelnr:=nr;
  340. labeltype:=ltyp;
  341. is_set:=false;
  342. { write it always }
  343. increfs;
  344. global_used;
  345. end;
  346. function TAsmLabel.getaltcopy(AList:TFPHashObjectList;altnr: longint): TAsmSymbol;
  347. begin;
  348. result := inherited getaltcopy(AList,altnr);
  349. TAsmLabel(result).labelnr:=labelnr;
  350. TAsmLabel(result).labeltype:=labeltype;
  351. TAsmLabel(result).is_set:=false;
  352. case bind of
  353. AB_GLOBAL,
  354. AB_PRIVATE_EXTERN:
  355. result.increfs;
  356. AB_LOCAL:
  357. ;
  358. else
  359. internalerror(2006053101);
  360. end;
  361. end;
  362. function TAsmLabel.getname:TSymStr;
  363. begin
  364. getname:=inherited getname;
  365. increfs;
  366. end;
  367. constructor TAsmLabel.create_non_global(AList: TFPHashObjectList; nr: longint; ltyp: TAsmLabelType; const prefix: TSymStr);
  368. var
  369. asmtyp: TAsmsymtype;
  370. begin
  371. case ltyp of
  372. alt_addr:
  373. asmtyp:=AT_ADDR;
  374. alt_data:
  375. asmtyp:=AT_DATA;
  376. else
  377. asmtyp:=AT_LABEL;
  378. end;
  379. inherited Create(AList,prefix+asmlabeltypeprefix[ltyp]+tostr(nr),AB_LOCAL,asmtyp);
  380. labelnr:=nr;
  381. labeltype:=ltyp;
  382. is_set:=false;
  383. end;
  384. procedure default_global_used;
  385. begin
  386. end;
  387. end.