aasmbase.pas 9.3 KB

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