aasmbase.pas 9.2 KB

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