aasmbase.pas 9.9 KB

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