aasmbase.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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. TAsmSectiontype=(sec_none,
  33. sec_code,
  34. sec_data,
  35. sec_rodata,
  36. sec_bss,
  37. sec_threadvar,
  38. { used for darwin import stubs }
  39. sec_stub,
  40. { stabs }
  41. sec_stab,sec_stabstr,
  42. { win32 }
  43. sec_idata2,sec_idata4,sec_idata5,sec_idata6,sec_idata7,sec_edata,
  44. { C++ exception handling unwinding (uses dwarf) }
  45. sec_eh_frame,
  46. { dwarf }
  47. sec_debug_frame,
  48. sec_debug_info,
  49. sec_debug_line,
  50. sec_debug_abbrev,
  51. { ELF resources }
  52. sec_fpc,
  53. { Table of contents section }
  54. sec_toc
  55. );
  56. TAsmSymbol = class(TNamedIndexItem)
  57. private
  58. { this need to be incremented with every symbol loading into the
  59. taasmoutput with loadsym/loadref/const_symbol (PFV) }
  60. refs : longint;
  61. public
  62. bind : TAsmsymbind;
  63. typ : TAsmsymtype;
  64. { Alternate symbol which can be used for 'renaming' needed for
  65. asm inlining. Also used for external and common solving during linking }
  66. altsymbol : TAsmSymbol;
  67. { Cached objsymbol }
  68. cachedobjsymbol : TObject;
  69. constructor create(const s:string;_bind:TAsmsymbind;_typ:Tasmsymtype);
  70. function is_used:boolean;
  71. procedure increfs;
  72. procedure decrefs;
  73. function getrefs: longint;
  74. end;
  75. { is the label only there for getting an DataOffset (e.g. for i/o
  76. checks -> alt_addr) or is it a jump target (alt_jump), for debug
  77. info alt_dbgline and alt_dbgfile }
  78. TAsmLabelType = (alt_jump,alt_addr,alt_data,alt_dbgline,alt_dbgfile,alt_dbgtype,alt_dbgframe);
  79. TAsmLabel = class(TAsmSymbol)
  80. labelnr : longint;
  81. labeltype : TAsmLabelType;
  82. is_set : boolean;
  83. constructor createlocal(nr:longint;ltyp:TAsmLabelType);
  84. constructor createglobal(const modulename:string;nr:longint;ltyp:TAsmLabelType);
  85. function getname:string;override;
  86. end;
  87. tasmsymbolidxarr = array[0..($7fffffff div sizeof(pointer))-1] of tasmsymbol;
  88. pasmsymbolidxarr = ^tasmsymbolidxarr;
  89. TObjLibraryData = class(TLinkedListItem)
  90. private
  91. nextaltnr : longint;
  92. nextlabelnr : array[Tasmlabeltype] of longint;
  93. public
  94. name,
  95. realname : string[80];
  96. symbolsearch : tdictionary; { contains ALL assembler symbols }
  97. AltSymbollist : tlist;
  98. constructor create(const n:string);
  99. destructor destroy;override;
  100. { asmsymbol }
  101. function newasmsymbol(const s : string;_bind:TAsmSymBind;_typ:TAsmsymtype) : tasmsymbol;
  102. function getasmsymbol(const s : string) : tasmsymbol;
  103. function newasmlabel(nr:longint;alt:tasmlabeltype;is_global:boolean) : tasmlabel;
  104. {# create a new assembler label }
  105. procedure getlabel(var l : tasmlabel;alt:tasmlabeltype);
  106. {# create a new assembler label for jumps }
  107. procedure getjumplabel(var l : tasmlabel);
  108. { make l as a new label and flag is_addr }
  109. procedure getaddrlabel(var l : tasmlabel);
  110. { make l as a new label and flag is_data }
  111. procedure getdatalabel(var l : tasmlabel);
  112. {# return a label number }
  113. { generate an alternative (duplicate) symbol }
  114. procedure GenerateAltSymbol(p:tasmsymbol);
  115. procedure ResetAltSymbols;
  116. end;
  117. function LengthUleb128(a: aword) : byte;
  118. function LengthSleb128(a: aint) : byte;
  119. const
  120. { alt_jump,alt_addr,alt_data,alt_dbgline,alt_dbgfile }
  121. asmlabeltypeprefix : array[tasmlabeltype] of char = ('j','a','d','l','f','t','c');
  122. var
  123. objectlibrary : TObjLibraryData;
  124. implementation
  125. uses
  126. strings,
  127. verbose;
  128. const
  129. sectsgrow = 100;
  130. symbolsgrow = 100;
  131. function LengthUleb128(a: aword) : byte;
  132. var
  133. b: byte;
  134. begin
  135. result:=0;
  136. repeat
  137. b := a and $7f;
  138. a := a shr 7;
  139. if a<>0 then
  140. b := b or $80;
  141. inc(result);
  142. if a=0 then
  143. break;
  144. until false;
  145. end;
  146. function LengthSleb128(a: aint) : byte;
  147. var
  148. b, size: byte;
  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. a := a or -(1 shl (size - 7));
  160. if (((a = 0) and
  161. (b and $40 = 0)) or
  162. ((a = -1) and
  163. (b and $40 <> 0))) then
  164. more := false
  165. else
  166. b := b or $80;
  167. inc(result);
  168. if not(more) then
  169. break;
  170. until false;
  171. end;
  172. {*****************************************************************************
  173. TAsmSymbol
  174. *****************************************************************************}
  175. constructor tasmsymbol.create(const s:string;_bind:TAsmsymbind;_typ:Tasmsymtype);
  176. begin;
  177. inherited createname(s);
  178. bind:=_bind;
  179. typ:=_typ;
  180. { used to remove unused labels from the al_procedures }
  181. refs:=0;
  182. end;
  183. function tasmsymbol.is_used:boolean;
  184. begin
  185. is_used:=(refs>0);
  186. end;
  187. procedure tasmsymbol.increfs;
  188. begin
  189. inc(refs);
  190. end;
  191. procedure tasmsymbol.decrefs;
  192. begin
  193. dec(refs);
  194. if refs<0 then
  195. internalerror(200211121);
  196. end;
  197. function tasmsymbol.getrefs: longint;
  198. begin
  199. getrefs := refs;
  200. end;
  201. {*****************************************************************************
  202. TAsmLabel
  203. *****************************************************************************}
  204. constructor tasmlabel.createlocal(nr:longint;ltyp:TAsmLabelType);
  205. begin;
  206. inherited create(target_asm.labelprefix+asmlabeltypeprefix[ltyp]+tostr(nr),AB_LOCAL,AT_LABEL);
  207. labelnr:=nr;
  208. labeltype:=ltyp;
  209. is_set:=false;
  210. end;
  211. constructor tasmlabel.createglobal(const modulename:string;nr:longint;ltyp:TAsmLabelType);
  212. begin;
  213. inherited create('_$'+modulename+'$_L'+asmlabeltypeprefix[ltyp]+tostr(nr),AB_GLOBAL,AT_DATA);
  214. labelnr:=nr;
  215. labeltype:=ltyp;
  216. is_set:=false;
  217. { write it always }
  218. increfs;
  219. end;
  220. function tasmlabel.getname:string;
  221. begin
  222. getname:=inherited getname;
  223. increfs;
  224. end;
  225. {****************************************************************************
  226. TObjLibraryData
  227. ****************************************************************************}
  228. constructor TObjLibraryData.create(const n:string);
  229. var
  230. alt : TAsmLabelType;
  231. begin
  232. inherited create;
  233. realname:=n;
  234. name:=upper(n);
  235. { symbols }
  236. symbolsearch:=tdictionary.create;
  237. symbolsearch.usehash;
  238. AltSymbollist:=TList.Create;
  239. { labels }
  240. nextaltnr:=1;
  241. for alt:=low(TAsmLabelType) to high(TAsmLabelType) do
  242. nextlabelnr[alt]:=1;
  243. end;
  244. destructor TObjLibraryData.destroy;
  245. begin
  246. AltSymbollist.free;
  247. symbolsearch.free;
  248. end;
  249. function TObjLibraryData.newasmsymbol(const s : string;_bind:TAsmSymBind;_typ:Tasmsymtype) : tasmsymbol;
  250. var
  251. hp : tasmsymbol;
  252. begin
  253. hp:=tasmsymbol(symbolsearch.search(s));
  254. if assigned(hp) then
  255. begin
  256. {$IFDEF EXTDEBUG}
  257. if (_typ <> AT_NONE) and
  258. (hp.typ <> _typ) and
  259. not(cs_compilesystem in aktmoduleswitches) and
  260. (target_info.system <> system_powerpc_darwin) then
  261. begin
  262. //Writeln('Error symbol '+hp.name+' type is ',Ord(_typ),', should be ',Ord(hp.typ));
  263. InternalError(2004031501);
  264. end;
  265. {$ENDIF}
  266. if (_bind<>AB_EXTERNAL) then
  267. hp.bind:=_bind
  268. end
  269. else
  270. begin
  271. { Not found, insert it. }
  272. hp:=tasmsymbol.create(s,_bind,_typ);
  273. symbolsearch.insert(hp);
  274. end;
  275. newasmsymbol:=hp;
  276. end;
  277. function TObjLibraryData.getasmsymbol(const s : string) : tasmsymbol;
  278. begin
  279. getasmsymbol:=tasmsymbol(symbolsearch.search(s));
  280. end;
  281. procedure TObjLibraryData.GenerateAltSymbol(p:tasmsymbol);
  282. begin
  283. if not assigned(p.altsymbol) then
  284. begin
  285. p.altsymbol:=tasmsymbol.create(p.name+'_'+tostr(nextaltnr),p.bind,p.typ);
  286. symbolsearch.insert(p.altsymbol);
  287. AltSymbollist.Add(p);
  288. end;
  289. end;
  290. procedure TObjLibraryData.ResetAltSymbols;
  291. var
  292. i : longint;
  293. begin
  294. for i:=0 to AltSymbollist.Count-1 do
  295. tasmsymbol(AltSymbollist[i]).altsymbol:=nil;
  296. AltSymbollist.Clear;
  297. end;
  298. function TObjLibraryData.newasmlabel(nr:longint;alt:tasmlabeltype;is_global:boolean) : tasmlabel;
  299. var
  300. hp : tasmlabel;
  301. begin
  302. if is_global then
  303. hp:=tasmlabel.createglobal(name,nr,alt)
  304. else
  305. hp:=tasmlabel.createlocal(nr,alt);
  306. symbolsearch.insert(hp);
  307. newasmlabel:=hp;
  308. end;
  309. procedure TObjLibraryData.getlabel(var l : tasmlabel;alt:tasmlabeltype);
  310. begin
  311. l:=tasmlabel.createlocal(nextlabelnr[alt],alt);
  312. inc(nextlabelnr[alt]);
  313. symbolsearch.insert(l);
  314. end;
  315. procedure TObjLibraryData.getjumplabel(var l : tasmlabel);
  316. begin
  317. l:=tasmlabel.createlocal(nextlabelnr[alt_jump],alt_jump);
  318. inc(nextlabelnr[alt_jump]);
  319. symbolsearch.insert(l);
  320. end;
  321. procedure TObjLibraryData.getdatalabel(var l : tasmlabel);
  322. begin
  323. l:=tasmlabel.createglobal(name,nextlabelnr[alt_data],alt_data);
  324. inc(nextlabelnr[alt_data]);
  325. symbolsearch.insert(l);
  326. end;
  327. procedure TObjLibraryData.getaddrlabel(var l : tasmlabel);
  328. begin
  329. l:=tasmlabel.createlocal(nextlabelnr[alt_addr],alt_addr);
  330. inc(nextlabelnr[alt_addr]);
  331. symbolsearch.insert(l);
  332. end;
  333. end.