aasmbase.pas 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 is_used:boolean;
  78. procedure increfs;
  79. procedure decrefs;
  80. function getrefs: longint;
  81. end;
  82. TAsmLabel = class(TAsmSymbol)
  83. labelnr : longint;
  84. labeltype : TAsmLabelType;
  85. is_set : boolean;
  86. constructor createlocal(nr:longint;ltyp:TAsmLabelType);
  87. constructor createglobal(const modulename:string;nr:longint;ltyp:TAsmLabelType);
  88. function getname:string;override;
  89. end;
  90. function use_smartlink_section:boolean;
  91. function maybe_smartlink_symbol:boolean;
  92. function LengthUleb128(a: aword) : byte;
  93. function LengthSleb128(a: aint) : byte;
  94. function EncodeUleb128(a: aword;out buf) : byte;
  95. function EncodeSleb128(a: aint;out buf) : byte;
  96. implementation
  97. uses
  98. strings,
  99. verbose;
  100. function use_smartlink_section:boolean;
  101. begin
  102. result:=(af_smartlink_sections in target_asm.flags) and
  103. (tf_smartlink_sections in target_info.flags);
  104. end;
  105. function maybe_smartlink_symbol:boolean;
  106. begin
  107. result:=(cs_create_smart in aktmoduleswitches) or
  108. use_smartlink_section;
  109. end;
  110. function LengthUleb128(a: aword) : byte;
  111. begin
  112. result:=0;
  113. repeat
  114. a := a shr 7;
  115. inc(result);
  116. if a=0 then
  117. break;
  118. until false;
  119. end;
  120. function LengthSleb128(a: aint) : byte;
  121. var
  122. b, size: byte;
  123. asign : aint;
  124. neg, more: boolean;
  125. begin
  126. more := true;
  127. neg := a < 0;
  128. size := sizeof(a)*8;
  129. result:=0;
  130. repeat
  131. b := a and $7f;
  132. a := a shr 7;
  133. if neg then
  134. begin
  135. { Use a variable to be sure that the correct or mask is generated }
  136. asign:=1;
  137. asign:=asign shl (size - 7);
  138. a := a or -asign;
  139. end;
  140. if (((a = 0) and
  141. (b and $40 = 0)) or
  142. ((a = -1) and
  143. (b and $40 <> 0))) then
  144. more := false;
  145. inc(result);
  146. if not(more) then
  147. break;
  148. until false;
  149. end;
  150. function EncodeUleb128(a: aword;out buf) : byte;
  151. var
  152. b: byte;
  153. pbuf : pbyte;
  154. begin
  155. result:=0;
  156. pbuf:=@buf;
  157. repeat
  158. b := a and $7f;
  159. a := a shr 7;
  160. if a<>0 then
  161. b := b or $80;
  162. pbuf^:=b;
  163. inc(pbuf);
  164. inc(result);
  165. if a=0 then
  166. break;
  167. until false;
  168. end;
  169. function EncodeSleb128(a: aint;out buf) : byte;
  170. var
  171. b, size: byte;
  172. asign : aint;
  173. neg, more: boolean;
  174. pbuf : pbyte;
  175. begin
  176. more := true;
  177. neg := a < 0;
  178. size := sizeof(a)*8;
  179. result:=0;
  180. pbuf:=@buf;
  181. repeat
  182. b := a and $7f;
  183. a := a shr 7;
  184. if neg then
  185. begin
  186. { Use a variable to be sure that the correct or mask is generated }
  187. asign:=1;
  188. asign:=asign shl (size - 7);
  189. a := a or -asign;
  190. end;
  191. if (((a = 0) and
  192. (b and $40 = 0)) or
  193. ((a = -1) and
  194. (b and $40 <> 0))) then
  195. more := false
  196. else
  197. b := b or $80;
  198. pbuf^:=b;
  199. inc(result);
  200. if not(more) then
  201. break;
  202. until false;
  203. end;
  204. {*****************************************************************************
  205. TAsmSymbol
  206. *****************************************************************************}
  207. constructor tasmsymbol.create(const s:string;_bind:TAsmsymbind;_typ:Tasmsymtype);
  208. begin;
  209. inherited createname(s);
  210. bind:=_bind;
  211. typ:=_typ;
  212. { used to remove unused labels from the al_procedures }
  213. refs:=0;
  214. end;
  215. function tasmsymbol.is_used:boolean;
  216. begin
  217. is_used:=(refs>0);
  218. end;
  219. procedure tasmsymbol.increfs;
  220. begin
  221. inc(refs);
  222. end;
  223. procedure tasmsymbol.decrefs;
  224. begin
  225. dec(refs);
  226. if refs<0 then
  227. internalerror(200211121);
  228. end;
  229. function tasmsymbol.getrefs: longint;
  230. begin
  231. getrefs := refs;
  232. end;
  233. {*****************************************************************************
  234. TAsmLabel
  235. *****************************************************************************}
  236. constructor tasmlabel.createlocal(nr:longint;ltyp:TAsmLabelType);
  237. begin;
  238. inherited create(target_asm.labelprefix+asmlabeltypeprefix[ltyp]+tostr(nr),AB_LOCAL,AT_LABEL);
  239. labelnr:=nr;
  240. labeltype:=ltyp;
  241. is_set:=false;
  242. end;
  243. constructor tasmlabel.createglobal(const modulename:string;nr:longint;ltyp:TAsmLabelType);
  244. begin;
  245. inherited create('_$'+modulename+'$_L'+asmlabeltypeprefix[ltyp]+tostr(nr),AB_GLOBAL,AT_DATA);
  246. labelnr:=nr;
  247. labeltype:=ltyp;
  248. is_set:=false;
  249. { write it always }
  250. increfs;
  251. end;
  252. function tasmlabel.getname:string;
  253. begin
  254. getname:=inherited getname;
  255. increfs;
  256. end;
  257. end.