aasmbase.pas 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. implementation
  95. uses
  96. strings,
  97. verbose;
  98. function use_smartlink_section:boolean;
  99. begin
  100. result:=(af_smartlink_sections in target_asm.flags) and
  101. (tf_smartlink_sections in target_info.flags);
  102. end;
  103. function maybe_smartlink_symbol:boolean;
  104. begin
  105. result:=(cs_create_smart in aktmoduleswitches) or
  106. use_smartlink_section;
  107. end;
  108. function LengthUleb128(a: aword) : byte;
  109. var
  110. b: byte;
  111. begin
  112. result:=0;
  113. repeat
  114. b := a and $7f;
  115. a := a shr 7;
  116. if a<>0 then
  117. b := b or $80;
  118. inc(result);
  119. if a=0 then
  120. break;
  121. until false;
  122. end;
  123. function LengthSleb128(a: aint) : byte;
  124. var
  125. b, size: byte;
  126. neg, more: boolean;
  127. begin
  128. more := true;
  129. neg := a < 0;
  130. size := sizeof(a)*8;
  131. result:=0;
  132. repeat
  133. b := a and $7f;
  134. a := a shr 7;
  135. if neg then
  136. a := a or -(1 shl (size - 7));
  137. if (((a = 0) and
  138. (b and $40 = 0)) or
  139. ((a = -1) and
  140. (b and $40 <> 0))) then
  141. more := false
  142. else
  143. b := b or $80;
  144. inc(result);
  145. if not(more) then
  146. break;
  147. until false;
  148. end;
  149. {*****************************************************************************
  150. TAsmSymbol
  151. *****************************************************************************}
  152. constructor tasmsymbol.create(const s:string;_bind:TAsmsymbind;_typ:Tasmsymtype);
  153. begin;
  154. inherited createname(s);
  155. bind:=_bind;
  156. typ:=_typ;
  157. { used to remove unused labels from the al_procedures }
  158. refs:=0;
  159. end;
  160. function tasmsymbol.is_used:boolean;
  161. begin
  162. is_used:=(refs>0);
  163. end;
  164. procedure tasmsymbol.increfs;
  165. begin
  166. inc(refs);
  167. end;
  168. procedure tasmsymbol.decrefs;
  169. begin
  170. dec(refs);
  171. if refs<0 then
  172. internalerror(200211121);
  173. end;
  174. function tasmsymbol.getrefs: longint;
  175. begin
  176. getrefs := refs;
  177. end;
  178. {*****************************************************************************
  179. TAsmLabel
  180. *****************************************************************************}
  181. constructor tasmlabel.createlocal(nr:longint;ltyp:TAsmLabelType);
  182. begin;
  183. inherited create(target_asm.labelprefix+asmlabeltypeprefix[ltyp]+tostr(nr),AB_LOCAL,AT_LABEL);
  184. labelnr:=nr;
  185. labeltype:=ltyp;
  186. is_set:=false;
  187. end;
  188. constructor tasmlabel.createglobal(const modulename:string;nr:longint;ltyp:TAsmLabelType);
  189. begin;
  190. inherited create('_$'+modulename+'$_L'+asmlabeltypeprefix[ltyp]+tostr(nr),AB_GLOBAL,AT_DATA);
  191. labelnr:=nr;
  192. labeltype:=ltyp;
  193. is_set:=false;
  194. { write it always }
  195. increfs;
  196. end;
  197. function tasmlabel.getname:string;
  198. begin
  199. getname:=inherited getname;
  200. increfs;
  201. end;
  202. end.