aasmbase.pas 10 KB

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