aasmbase.pas 15 KB

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