aasmbase.pas 10 KB

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