aasmbase.pas 12 KB

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