aasmbase.pas 14 KB

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