aasmbase.pas 16 KB

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