aasmbase.pas 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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);
  31. TAsmsymtype=(AT_NONE,AT_FUNCTION,AT_DATA,AT_SECTION,AT_LABEL);
  32. { is the label only there for getting an DataOffset (e.g. for i/o
  33. checks -> alt_addr) or is it a jump target (alt_jump), for debug
  34. info alt_dbgline and alt_dbgfile, etc. }
  35. TAsmLabelType = (alt_jump,alt_addr,alt_data,alt_dbgline,alt_dbgfile,alt_dbgtype,alt_dbgframe);
  36. const
  37. asmlabeltypeprefix : array[tasmlabeltype] of char = ('j','a','d','l','f','t','c');
  38. type
  39. TAsmSectiontype=(sec_none,
  40. sec_code,
  41. sec_data,
  42. sec_rodata,
  43. sec_bss,
  44. sec_threadvar,
  45. { used for wince exception handling }
  46. sec_pdata,
  47. { used for darwin import stubs }
  48. sec_stub,
  49. { stabs }
  50. sec_stab,sec_stabstr,
  51. { win32 }
  52. sec_idata2,sec_idata4,sec_idata5,sec_idata6,sec_idata7,sec_edata,
  53. { C++ exception handling unwinding (uses dwarf) }
  54. sec_eh_frame,
  55. { dwarf }
  56. sec_debug_frame,
  57. sec_debug_info,
  58. sec_debug_line,
  59. sec_debug_abbrev,
  60. { ELF resources }
  61. sec_fpc,
  62. { Table of contents section }
  63. sec_toc
  64. );
  65. TAsmSectionOrder = (secorder_begin,secorder_default,secorder_end);
  66. TAsmSymbol = class(TNamedIndexItem)
  67. private
  68. { this need to be incremented with every symbol loading into the
  69. TAsmList with loadsym/loadref/const_symbol (PFV) }
  70. refs : longint;
  71. public
  72. bind : TAsmsymbind;
  73. typ : TAsmsymtype;
  74. { Alternate symbol which can be used for 'renaming' needed for
  75. asm inlining. Also used for external and common solving during linking }
  76. altsymbol : TAsmSymbol;
  77. { Cached objsymbol }
  78. cachedobjsymbol : TObject;
  79. constructor create(const s:string;_bind:TAsmsymbind;_typ:Tasmsymtype);
  80. function getaltcopy(altnr: longint): tasmsymbol; virtual;
  81. function is_used:boolean;
  82. procedure increfs;
  83. procedure decrefs;
  84. function getrefs: longint;
  85. end;
  86. TAsmSymbolClass = class of TAsmSymbol;
  87. TAsmLabel = class(TAsmSymbol)
  88. labelnr : longint;
  89. labeltype : TAsmLabelType;
  90. is_set : boolean;
  91. constructor createlocal(nr:longint;ltyp:TAsmLabelType);
  92. constructor createglobal(const modulename:string;nr:longint;ltyp:TAsmLabelType);
  93. function getaltcopy(altnr: longint): tasmsymbol; override;
  94. function getname:string;override;
  95. end;
  96. function use_smartlink_section:boolean;
  97. function maybe_smartlink_symbol:boolean;
  98. function LengthUleb128(a: qword) : byte;
  99. function LengthSleb128(a: int64) : byte;
  100. function EncodeUleb128(a: qword;out buf) : byte;
  101. function EncodeSleb128(a: int64;out buf) : byte;
  102. implementation
  103. uses
  104. SysUtils,
  105. verbose;
  106. function use_smartlink_section:boolean;
  107. begin
  108. result:=(af_smartlink_sections in target_asm.flags) and
  109. (tf_smartlink_sections in target_info.flags);
  110. end;
  111. function maybe_smartlink_symbol:boolean;
  112. begin
  113. result:=(cs_create_smart in current_settings.moduleswitches) or
  114. use_smartlink_section;
  115. end;
  116. function LengthUleb128(a: qword) : byte;
  117. begin
  118. result:=0;
  119. repeat
  120. a := a shr 7;
  121. inc(result);
  122. if a=0 then
  123. break;
  124. until false;
  125. end;
  126. function LengthSleb128(a: int64) : byte;
  127. var
  128. b, size: byte;
  129. asign : int64;
  130. neg, more: boolean;
  131. begin
  132. more := true;
  133. neg := a < 0;
  134. size := sizeof(a)*8;
  135. result:=0;
  136. repeat
  137. b := a and $7f;
  138. a := a shr 7;
  139. if neg then
  140. begin
  141. { Use a variable to be sure that the correct or mask is generated }
  142. asign:=1;
  143. asign:=asign shl (size - 7);
  144. a := a or -asign;
  145. end;
  146. if (((a = 0) and
  147. (b and $40 = 0)) or
  148. ((a = -1) and
  149. (b and $40 <> 0))) then
  150. more := false;
  151. inc(result);
  152. if not(more) then
  153. break;
  154. until false;
  155. end;
  156. function EncodeUleb128(a: qword;out buf) : byte;
  157. var
  158. b: byte;
  159. pbuf : pbyte;
  160. begin
  161. result:=0;
  162. pbuf:=@buf;
  163. repeat
  164. b := a and $7f;
  165. a := a shr 7;
  166. if a<>0 then
  167. b := b or $80;
  168. pbuf^:=b;
  169. inc(pbuf);
  170. inc(result);
  171. if a=0 then
  172. break;
  173. until false;
  174. end;
  175. function EncodeSleb128(a: int64;out buf) : byte;
  176. var
  177. b, size: byte;
  178. asign : int64;
  179. neg, more: boolean;
  180. pbuf : pbyte;
  181. begin
  182. more := true;
  183. neg := a < 0;
  184. size := sizeof(a)*8;
  185. result:=0;
  186. pbuf:=@buf;
  187. repeat
  188. b := a and $7f;
  189. a := a shr 7;
  190. if neg then
  191. begin
  192. { Use a variable to be sure that the correct or mask is generated }
  193. asign:=1;
  194. asign:=asign shl (size - 7);
  195. a := a or -asign;
  196. end;
  197. if (((a = 0) and
  198. (b and $40 = 0)) or
  199. ((a = -1) and
  200. (b and $40 <> 0))) then
  201. more := false
  202. else
  203. b := b or $80;
  204. pbuf^:=b;
  205. inc(pbuf);
  206. inc(result);
  207. if not(more) then
  208. break;
  209. until false;
  210. end;
  211. {*****************************************************************************
  212. TAsmSymbol
  213. *****************************************************************************}
  214. constructor tasmsymbol.create(const s:string;_bind:TAsmsymbind;_typ:Tasmsymtype);
  215. begin;
  216. inherited createname(s);
  217. bind:=_bind;
  218. typ:=_typ;
  219. { used to remove unused labels from the al_procedures }
  220. refs:=0;
  221. end;
  222. function tasmsymbol.getaltcopy(altnr: longint): tasmsymbol;
  223. begin
  224. result := TAsmSymbol(TAsmSymbolClass(classtype).createname(name+'_'+tostr(altnr)));
  225. result.bind:=bind;
  226. result.typ:=typ;
  227. result.refs:=0;
  228. end;
  229. function tasmsymbol.is_used:boolean;
  230. begin
  231. is_used:=(refs>0);
  232. end;
  233. procedure tasmsymbol.increfs;
  234. begin
  235. inc(refs);
  236. end;
  237. procedure tasmsymbol.decrefs;
  238. begin
  239. dec(refs);
  240. if refs<0 then
  241. internalerror(200211121);
  242. end;
  243. function tasmsymbol.getrefs: longint;
  244. begin
  245. getrefs := refs;
  246. end;
  247. {*****************************************************************************
  248. TAsmLabel
  249. *****************************************************************************}
  250. constructor tasmlabel.createlocal(nr:longint;ltyp:TAsmLabelType);
  251. begin
  252. inherited create(target_asm.labelprefix+asmlabeltypeprefix[ltyp]+tostr(nr),AB_LOCAL,AT_LABEL);
  253. labelnr:=nr;
  254. labeltype:=ltyp;
  255. is_set:=false;
  256. end;
  257. constructor tasmlabel.createglobal(const modulename:string;nr:longint;ltyp:TAsmLabelType);
  258. begin
  259. inherited create('_$'+modulename+'$_L'+asmlabeltypeprefix[ltyp]+tostr(nr),AB_GLOBAL,AT_DATA);
  260. labelnr:=nr;
  261. labeltype:=ltyp;
  262. is_set:=false;
  263. { write it always }
  264. increfs;
  265. end;
  266. function tasmlabel.getaltcopy(altnr: longint): tasmsymbol;
  267. begin;
  268. result := inherited getaltcopy(altnr);
  269. tasmlabel(result).labelnr:=labelnr;
  270. tasmlabel(result).labeltype:=labeltype;
  271. tasmlabel(result).is_set:=false;
  272. case bind of
  273. AB_GLOBAL:
  274. result.increfs;
  275. AB_LOCAL:
  276. ;
  277. else
  278. internalerror(2006053101);
  279. end;
  280. end;
  281. function tasmlabel.getname:string;
  282. begin
  283. getname:=inherited getname;
  284. increfs;
  285. end;
  286. end.