aasmbase.pas 12 KB

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