aasmdata.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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 : TFPHashObjectList;
  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(out l : TAsmLabel;alt:TAsmLabeltype);
  123. procedure getjumplabel(out l : TAsmLabel);
  124. procedure getaddrlabel(out l : TAsmLabel);
  125. procedure getdatalabel(out l : TAsmLabel);
  126. { generate an alternative (duplicate) symbol }
  127. procedure GenerateAltSymbol(p:TAsmSymbol);
  128. procedure ResetAltSymbols;
  129. property AsmSymbolDict:TFPHashObjectList 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:=TFPHashObjectList.create(true);
  221. FAltSymbolList:=TFPObjectList.Create(false);
  222. { labels }
  223. FNextAltNr:=1;
  224. for alt:=low(TAsmLabelType) to high(TAsmLabelType) do
  225. FNextLabelNr[alt]:=1;
  226. { AsmLists }
  227. CurrAsmList:=TAsmList.create;
  228. for hal:=low(TAsmListType) to high(TAsmListType) do
  229. AsmLists[hal]:=TAsmList.create;
  230. { PIC data }
  231. if (target_info.system in [system_powerpc_darwin,system_i386_darwin]) then
  232. AsmLists[al_picdata].concat(tai_directive.create(asd_non_lazy_symbol_pointer,''));
  233. { CFI }
  234. FAsmCFI:=CAsmCFI.Create;
  235. end;
  236. destructor TAsmData.destroy;
  237. var
  238. {$ifdef MEMDEBUG}
  239. d : tmemdebug;
  240. {$endif}
  241. hal : TAsmListType;
  242. begin
  243. {$ifdef MEMDEBUG}
  244. d:=tmemdebug.create(name+' - AsmSymbols');
  245. {$endif}
  246. FAltSymbolList.free;
  247. FAsmSymbolDict.free;
  248. {$ifdef MEMDEBUG}
  249. d.free;
  250. {$endif}
  251. {$ifdef MEMDEBUG}
  252. d:=tmemdebug.create(name+' - AsmCFI');
  253. {$endif}
  254. FAsmCFI.free;
  255. {$ifdef MEMDEBUG}
  256. d.free;
  257. {$endif}
  258. {$ifdef MEMDEBUG}
  259. d:=tmemdebug.create(name+' - AsmLists');
  260. {$endif}
  261. for hal:=low(TAsmListType) to high(TAsmListType) do
  262. AsmLists[hal].free;
  263. {$ifdef MEMDEBUG}
  264. d.free;
  265. {$endif}
  266. end;
  267. function TAsmData.DefineAsmSymbol(const s : string;_bind:TAsmSymBind;_typ:Tasmsymtype) : TAsmSymbol;
  268. var
  269. hp : TAsmSymbol;
  270. begin
  271. hp:=TAsmSymbol(FAsmSymbolDict.Find(s));
  272. if assigned(hp) then
  273. begin
  274. { Redefine is allowed, but the types must be the same. The redefine
  275. is needed for Darwin where the labels are first allocated }
  276. if (hp.bind<>AB_EXTERNAL) then
  277. begin
  278. if (hp.bind<>_bind) and
  279. (hp.typ<>_typ) then
  280. internalerror(200603261);
  281. end;
  282. hp.typ:=_typ;
  283. hp.bind:=_bind;
  284. end
  285. else
  286. begin
  287. { Not found, insert it. }
  288. hp:=TAsmSymbol.create(AsmSymbolDict,s,_bind,_typ);
  289. end;
  290. result:=hp;
  291. end;
  292. function TAsmData.RefAsmSymbol(const s : string) : TAsmSymbol;
  293. begin
  294. result:=TAsmSymbol(FAsmSymbolDict.Find(s));
  295. if not assigned(result) then
  296. result:=TAsmSymbol.create(AsmSymbolDict,s,AB_EXTERNAL,AT_NONE);
  297. end;
  298. function TAsmData.GetAsmSymbol(const s : string) : TAsmSymbol;
  299. begin
  300. result:=TAsmSymbol(FAsmSymbolDict.Find(s));
  301. end;
  302. procedure TAsmData.GenerateAltSymbol(p:TAsmSymbol);
  303. begin
  304. if not assigned(p.altsymbol) then
  305. begin
  306. p.altsymbol:=p.getaltcopy(AsmSymbolDict,FNextAltNr);
  307. FAltSymbolList.Add(p);
  308. end;
  309. end;
  310. procedure TAsmData.ResetAltSymbols;
  311. var
  312. i : longint;
  313. begin
  314. for i:=0 to FAltSymbolList.Count-1 do
  315. TAsmSymbol(FAltSymbolList[i]).altsymbol:=nil;
  316. FAltSymbolList.Clear;
  317. end;
  318. procedure TAsmData.getlabel(out l : TAsmLabel;alt:TAsmLabeltype);
  319. begin
  320. l:=TAsmLabel.createlocal(AsmSymbolDict,FNextLabelNr[alt],alt);
  321. inc(FNextLabelNr[alt]);
  322. end;
  323. procedure TAsmData.getjumplabel(out l : TAsmLabel);
  324. begin
  325. l:=TAsmLabel.createlocal(AsmSymbolDict,FNextLabelNr[alt_jump],alt_jump);
  326. inc(FNextLabelNr[alt_jump]);
  327. end;
  328. procedure TAsmData.getdatalabel(out l : TAsmLabel);
  329. begin
  330. l:=TAsmLabel.createglobal(AsmSymbolDict,name,FNextLabelNr[alt_data],alt_data);
  331. inc(FNextLabelNr[alt_data]);
  332. end;
  333. procedure TAsmData.getaddrlabel(out l : TAsmLabel);
  334. begin
  335. l:=TAsmLabel.createlocal(AsmSymbolDict,FNextLabelNr[alt_addr],alt_addr);
  336. inc(FNextLabelNr[alt_addr]);
  337. end;
  338. begin
  339. CAsmCFI:=TAsmCFI;
  340. end.