aasmbase.pas 13 KB

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