aasmdata.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. {
  2. Copyright (c) 1998-2006 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 aasmdata;
  23. {$i fpcdefs.inc}
  24. interface
  25. uses
  26. cutils,cclasses,
  27. globtype,globals,systems,
  28. cpuinfo,cpubase,
  29. cgbase,cgutils,
  30. symtype,
  31. aasmbase,ogbase;
  32. type
  33. { Type of AsmLists. The order is important for the layout of the
  34. information in the .o file. The stabs for the types must be defined
  35. before they can be referenced and therefor they need to be written
  36. first (PFV) }
  37. TAsmListType=(
  38. al_start,
  39. al_stabs,
  40. al_procedures,
  41. al_globals,
  42. al_const,
  43. al_typedconsts,
  44. al_rotypedconsts,
  45. al_threadvars,
  46. al_imports,
  47. al_exports,
  48. al_resources,
  49. al_rtti,
  50. al_dwarf,
  51. al_dwarf_info,
  52. al_dwarf_abbrev,
  53. al_dwarf_line,
  54. al_picdata,
  55. al_resourcestrings,
  56. al_end
  57. );
  58. const
  59. AsmListTypeStr : array[TAsmListType] of string[24] =(
  60. 'al_begin',
  61. 'al_stabs',
  62. 'al_procedures',
  63. 'al_globals',
  64. 'al_const',
  65. 'al_typedconsts',
  66. 'al_rotypedconsts',
  67. 'al_threadvars',
  68. 'al_imports',
  69. 'al_exports',
  70. 'al_resources',
  71. 'al_rtti',
  72. 'al_dwarf',
  73. 'al_dwarf_info',
  74. 'al_dwarf_abbrev',
  75. 'al_dwarf_line',
  76. 'al_picdata',
  77. 'al_resourcestrings',
  78. 'al_end'
  79. );
  80. type
  81. TAsmList = class(tlinkedlist)
  82. constructor create;
  83. function empty : boolean;
  84. function getlasttaifilepos : pfileposinfo;
  85. end;
  86. TAsmCFI=class
  87. public
  88. constructor create;virtual;
  89. destructor destroy;override;
  90. procedure generate_code(list:TAsmList);virtual;
  91. procedure start_frame(list:TAsmList);virtual;
  92. procedure end_frame(list:TAsmList);virtual;
  93. procedure cfa_offset(list:TAsmList;reg:tregister;ofs:longint);virtual;
  94. procedure cfa_restore(list:TAsmList;reg:tregister);virtual;
  95. procedure cfa_def_cfa_register(list:TAsmList;reg:tregister);virtual;
  96. procedure cfa_def_cfa_offset(list:TAsmList;ofs:longint);virtual;
  97. end;
  98. TAsmCFIClass=class of TAsmCFI;
  99. TAsmData = class
  100. private
  101. { Symbols }
  102. FAsmSymbolDict : TDictionary;
  103. FAltSymbolList : TFPObjectList;
  104. FNextAltNr : longint;
  105. FNextLabelNr : array[Tasmlabeltype] of longint;
  106. { Call Frame Information for stack unwinding}
  107. FAsmCFI : TAsmCFI;
  108. public
  109. name,
  110. realname : string[80];
  111. NextVTEntryNr : longint;
  112. { Assembler lists }
  113. AsmLists : array[TAsmListType] of TAsmList;
  114. CurrAsmList : TAsmList;
  115. constructor create(const n:string);
  116. destructor destroy;override;
  117. { asmsymbol }
  118. function DefineAsmSymbol(const s : string;_bind:TAsmSymBind;_typ:Tasmsymtype) : tasmsymbol;
  119. function RefAsmSymbol(const s : string) : tasmsymbol;
  120. function getasmsymbol(const s : string) : tasmsymbol;
  121. { create new assembler label }
  122. procedure getlabel(var l : tasmlabel;alt:tasmlabeltype);
  123. procedure getjumplabel(var l : tasmlabel);
  124. procedure getaddrlabel(var l : tasmlabel);
  125. procedure getdatalabel(var l : tasmlabel);
  126. { generate an alternative (duplicate) symbol }
  127. procedure GenerateAltSymbol(p:tasmsymbol);
  128. procedure ResetAltSymbols;
  129. property AsmSymbolDict:TDictionary read FAsmSymbolDict;
  130. property AsmCFI:TAsmCFI read FAsmCFI;
  131. end;
  132. var
  133. CAsmCFI : TAsmCFIClass;
  134. current_asmdata : TAsmData;
  135. implementation
  136. uses
  137. verbose,
  138. aasmtai;
  139. {*****************************************************************************
  140. TAsmCFI
  141. *****************************************************************************}
  142. constructor TAsmCFI.create;
  143. begin
  144. end;
  145. destructor TAsmCFI.destroy;
  146. begin
  147. end;
  148. procedure TAsmCFI.generate_code(list:TAsmList);
  149. begin
  150. end;
  151. procedure TAsmCFI.start_frame(list:TAsmList);
  152. begin
  153. end;
  154. procedure TAsmCFI.end_frame(list:TAsmList);
  155. begin
  156. end;
  157. procedure TAsmCFI.cfa_offset(list:TAsmList;reg:tregister;ofs:longint);
  158. begin
  159. end;
  160. procedure TAsmCFI.cfa_restore(list:TAsmList;reg:tregister);
  161. begin
  162. end;
  163. procedure TAsmCFI.cfa_def_cfa_register(list:TAsmList;reg:tregister);
  164. begin
  165. end;
  166. procedure TAsmCFI.cfa_def_cfa_offset(list:TAsmList;ofs:longint);
  167. begin
  168. end;
  169. {*****************************************************************************
  170. TAsmList
  171. *****************************************************************************}
  172. constructor TAsmList.create;
  173. begin
  174. inherited create;
  175. { make sure the optimizer won't remove the first tai of this list}
  176. insert(tai_marker.create(mark_BlockStart));
  177. end;
  178. function TAsmList.empty : boolean;
  179. begin
  180. { there is always a mark_BlockStart available,
  181. see TAsmList.create }
  182. result:=(count<=1);
  183. end;
  184. function TAsmList.getlasttaifilepos : pfileposinfo;
  185. var
  186. hp : tlinkedlistitem;
  187. begin
  188. getlasttaifilepos := nil;
  189. if assigned(last) then
  190. begin
  191. { find the last file information record }
  192. if not (tai(last).typ in SkipLineInfo) then
  193. getlasttaifilepos:=@tailineinfo(last).fileinfo
  194. else
  195. { go through list backwards to find the first entry
  196. with line information
  197. }
  198. begin
  199. hp:=tai(last);
  200. while assigned(hp) and (tai(hp).typ in SkipLineInfo) do
  201. hp:=hp.Previous;
  202. { found entry }
  203. if assigned(hp) then
  204. getlasttaifilepos:=@tailineinfo(hp).fileinfo
  205. end;
  206. end;
  207. end;
  208. {****************************************************************************
  209. TAsmData
  210. ****************************************************************************}
  211. constructor TAsmData.create(const n:string);
  212. var
  213. alt : TAsmLabelType;
  214. hal : TAsmListType;
  215. begin
  216. inherited create;
  217. realname:=n;
  218. name:=upper(n);
  219. { symbols }
  220. FAsmSymbolDict:=TDictionary.create;
  221. FAsmSymbolDict.usehash;
  222. FAltSymbolList:=TFPObjectList.Create(false);
  223. { labels }
  224. FNextAltNr:=1;
  225. for alt:=low(TAsmLabelType) to high(TAsmLabelType) do
  226. FNextLabelNr[alt]:=1;
  227. { AsmLists }
  228. CurrAsmList:=TAsmList.create;
  229. for hal:=low(TAsmListType) to high(TAsmListType) do
  230. AsmLists[hal]:=TAsmList.create;
  231. { PIC data }
  232. if (target_info.system in [system_powerpc_darwin,system_i386_darwin]) then
  233. AsmLists[al_picdata].concat(tai_directive.create(asd_non_lazy_symbol_pointer,''));
  234. { CFI }
  235. FAsmCFI:=CAsmCFI.Create;
  236. end;
  237. destructor TAsmData.destroy;
  238. var
  239. {$ifdef MEMDEBUG}
  240. d : tmemdebug;
  241. {$endif}
  242. hal : TAsmListType;
  243. begin
  244. {$ifdef MEMDEBUG}
  245. d:=tmemdebug.create(name+' - AsmSymbols');
  246. {$endif}
  247. FAltSymbolList.free;
  248. FAsmSymbolDict.free;
  249. {$ifdef MEMDEBUG}
  250. d.free;
  251. {$endif}
  252. {$ifdef MEMDEBUG}
  253. d:=tmemdebug.create(name+' - AsmCFI');
  254. {$endif}
  255. FAsmCFI.free;
  256. {$ifdef MEMDEBUG}
  257. d.free;
  258. {$endif}
  259. {$ifdef MEMDEBUG}
  260. d:=tmemdebug.create(name+' - AsmLists');
  261. {$endif}
  262. for hal:=low(TAsmListType) to high(TAsmListType) do
  263. AsmLists[hal].free;
  264. {$ifdef MEMDEBUG}
  265. d.free;
  266. {$endif}
  267. end;
  268. function TAsmData.DefineAsmSymbol(const s : string;_bind:TAsmSymBind;_typ:Tasmsymtype) : tasmsymbol;
  269. var
  270. hp : tasmsymbol;
  271. begin
  272. hp:=tasmsymbol(FAsmSymbolDict.search(s));
  273. if assigned(hp) then
  274. begin
  275. { Redefine is allowed, but the types must be the same. The redefine
  276. is needed for Darwin where the labels are first allocated }
  277. if (hp.bind<>AB_EXTERNAL) then
  278. begin
  279. if (hp.bind<>_bind) and
  280. (hp.typ<>_typ) then
  281. internalerror(200603261);
  282. end;
  283. hp.typ:=_typ;
  284. hp.bind:=_bind;
  285. end
  286. else
  287. begin
  288. { Not found, insert it. }
  289. hp:=tasmsymbol.create(s,_bind,_typ);
  290. FAsmSymbolDict.insert(hp);
  291. end;
  292. result:=hp;
  293. end;
  294. function TAsmData.RefAsmSymbol(const s : string) : tasmsymbol;
  295. var
  296. hp : tasmsymbol;
  297. begin
  298. hp:=tasmsymbol(FAsmSymbolDict.search(s));
  299. if not assigned(hp) then
  300. begin
  301. { Not found, insert it. }
  302. hp:=tasmsymbol.create(s,AB_EXTERNAL,AT_NONE);
  303. FAsmSymbolDict.insert(hp);
  304. end;
  305. result:=hp;
  306. end;
  307. function TAsmData.getasmsymbol(const s : string) : tasmsymbol;
  308. begin
  309. getasmsymbol:=tasmsymbol(FAsmSymbolDict.search(s));
  310. end;
  311. procedure TAsmData.GenerateAltSymbol(p:tasmsymbol);
  312. begin
  313. if not assigned(p.altsymbol) then
  314. begin
  315. p.altsymbol:=tasmsymbol.create(p.name+'_'+tostr(FNextAltNr),p.bind,p.typ);
  316. FAsmSymbolDict.insert(p.altsymbol);
  317. FAltSymbolList.Add(p);
  318. end;
  319. end;
  320. procedure TAsmData.ResetAltSymbols;
  321. var
  322. i : longint;
  323. begin
  324. for i:=0 to FAltSymbolList.Count-1 do
  325. tasmsymbol(FAltSymbolList[i]).altsymbol:=nil;
  326. FAltSymbolList.Clear;
  327. end;
  328. procedure TAsmData.getlabel(var l : tasmlabel;alt:tasmlabeltype);
  329. begin
  330. l:=tasmlabel.createlocal(FNextLabelNr[alt],alt);
  331. inc(FNextLabelNr[alt]);
  332. FAsmSymbolDict.insert(l);
  333. end;
  334. procedure TAsmData.getjumplabel(var l : tasmlabel);
  335. begin
  336. l:=tasmlabel.createlocal(FNextLabelNr[alt_jump],alt_jump);
  337. inc(FNextLabelNr[alt_jump]);
  338. FAsmSymbolDict.insert(l);
  339. end;
  340. procedure TAsmData.getdatalabel(var l : tasmlabel);
  341. begin
  342. l:=tasmlabel.createglobal(name,FNextLabelNr[alt_data],alt_data);
  343. inc(FNextLabelNr[alt_data]);
  344. FAsmSymbolDict.insert(l);
  345. end;
  346. procedure TAsmData.getaddrlabel(var l : tasmlabel);
  347. begin
  348. l:=tasmlabel.createlocal(FNextLabelNr[alt_addr],alt_addr);
  349. inc(FNextLabelNr[alt_addr]);
  350. FAsmSymbolDict.insert(l);
  351. end;
  352. begin
  353. CAsmCFI:=TAsmCFI;
  354. end.