aasmdata.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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. { Assembler lists }
  112. AsmLists : array[TAsmListType] of TAsmList;
  113. CurrAsmList : TAsmList;
  114. constructor create(const n:string);
  115. destructor destroy;override;
  116. { asmsymbol }
  117. function DefineAsmSymbol(const s : string;_bind:TAsmSymBind;_typ:Tasmsymtype) : tasmsymbol;
  118. function RefAsmSymbol(const s : string) : tasmsymbol;
  119. function getasmsymbol(const s : string) : tasmsymbol;
  120. { create new assembler label }
  121. procedure getlabel(var l : tasmlabel;alt:tasmlabeltype);
  122. procedure getjumplabel(var l : tasmlabel);
  123. procedure getaddrlabel(var l : tasmlabel);
  124. procedure getdatalabel(var l : tasmlabel);
  125. { generate an alternative (duplicate) symbol }
  126. procedure GenerateAltSymbol(p:tasmsymbol);
  127. procedure ResetAltSymbols;
  128. property AsmSymbolDict:TDictionary read FAsmSymbolDict;
  129. property AsmCFI:TAsmCFI read FAsmCFI;
  130. end;
  131. var
  132. CAsmCFI : TAsmCFIClass;
  133. current_asmdata : TAsmData;
  134. implementation
  135. uses
  136. verbose,
  137. aasmtai;
  138. {*****************************************************************************
  139. TAsmCFI
  140. *****************************************************************************}
  141. constructor TAsmCFI.create;
  142. begin
  143. end;
  144. destructor TAsmCFI.destroy;
  145. begin
  146. end;
  147. procedure TAsmCFI.generate_code(list:TAsmList);
  148. begin
  149. end;
  150. procedure TAsmCFI.start_frame(list:TAsmList);
  151. begin
  152. end;
  153. procedure TAsmCFI.end_frame(list:TAsmList);
  154. begin
  155. end;
  156. procedure TAsmCFI.cfa_offset(list:TAsmList;reg:tregister;ofs:longint);
  157. begin
  158. end;
  159. procedure TAsmCFI.cfa_restore(list:TAsmList;reg:tregister);
  160. begin
  161. end;
  162. procedure TAsmCFI.cfa_def_cfa_register(list:TAsmList;reg:tregister);
  163. begin
  164. end;
  165. procedure TAsmCFI.cfa_def_cfa_offset(list:TAsmList;ofs:longint);
  166. begin
  167. end;
  168. {*****************************************************************************
  169. TAsmList
  170. *****************************************************************************}
  171. constructor TAsmList.create;
  172. begin
  173. inherited create;
  174. { make sure the optimizer won't remove the first tai of this list}
  175. insert(tai_marker.create(mark_BlockStart));
  176. end;
  177. function TAsmList.empty : boolean;
  178. begin
  179. { there is always a mark_BlockStart available,
  180. see TAsmList.create }
  181. result:=(count<=1);
  182. end;
  183. function TAsmList.getlasttaifilepos : pfileposinfo;
  184. var
  185. hp : tlinkedlistitem;
  186. begin
  187. getlasttaifilepos := nil;
  188. if assigned(last) then
  189. begin
  190. { find the last file information record }
  191. if not (tai(last).typ in SkipLineInfo) then
  192. getlasttaifilepos:=@tailineinfo(last).fileinfo
  193. else
  194. { go through list backwards to find the first entry
  195. with line information
  196. }
  197. begin
  198. hp:=tai(last);
  199. while assigned(hp) and (tai(hp).typ in SkipLineInfo) do
  200. hp:=hp.Previous;
  201. { found entry }
  202. if assigned(hp) then
  203. getlasttaifilepos:=@tailineinfo(hp).fileinfo
  204. end;
  205. end;
  206. end;
  207. {****************************************************************************
  208. TAsmData
  209. ****************************************************************************}
  210. constructor TAsmData.create(const n:string);
  211. var
  212. alt : TAsmLabelType;
  213. hal : TAsmListType;
  214. begin
  215. inherited create;
  216. realname:=n;
  217. name:=upper(n);
  218. { symbols }
  219. FAsmSymbolDict:=TDictionary.create;
  220. FAsmSymbolDict.usehash;
  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.search(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(s,_bind,_typ);
  289. FAsmSymbolDict.insert(hp);
  290. end;
  291. result:=hp;
  292. end;
  293. function TAsmData.RefAsmSymbol(const s : string) : tasmsymbol;
  294. var
  295. hp : tasmsymbol;
  296. begin
  297. hp:=tasmsymbol(FAsmSymbolDict.search(s));
  298. if not assigned(hp) then
  299. begin
  300. { Not found, insert it. }
  301. hp:=tasmsymbol.create(s,AB_EXTERNAL,AT_NONE);
  302. FAsmSymbolDict.insert(hp);
  303. end;
  304. result:=hp;
  305. end;
  306. function TAsmData.getasmsymbol(const s : string) : tasmsymbol;
  307. begin
  308. getasmsymbol:=tasmsymbol(FAsmSymbolDict.search(s));
  309. end;
  310. procedure TAsmData.GenerateAltSymbol(p:tasmsymbol);
  311. begin
  312. if not assigned(p.altsymbol) then
  313. begin
  314. p.altsymbol:=tasmsymbol.create(p.name+'_'+tostr(FNextAltNr),p.bind,p.typ);
  315. FAsmSymbolDict.insert(p.altsymbol);
  316. FAltSymbolList.Add(p);
  317. end;
  318. end;
  319. procedure TAsmData.ResetAltSymbols;
  320. var
  321. i : longint;
  322. begin
  323. for i:=0 to FAltSymbolList.Count-1 do
  324. tasmsymbol(FAltSymbolList[i]).altsymbol:=nil;
  325. FAltSymbolList.Clear;
  326. end;
  327. procedure TAsmData.getlabel(var l : tasmlabel;alt:tasmlabeltype);
  328. begin
  329. l:=tasmlabel.createlocal(FNextLabelNr[alt],alt);
  330. inc(FNextLabelNr[alt]);
  331. FAsmSymbolDict.insert(l);
  332. end;
  333. procedure TAsmData.getjumplabel(var l : tasmlabel);
  334. begin
  335. l:=tasmlabel.createlocal(FNextLabelNr[alt_jump],alt_jump);
  336. inc(FNextLabelNr[alt_jump]);
  337. FAsmSymbolDict.insert(l);
  338. end;
  339. procedure TAsmData.getdatalabel(var l : tasmlabel);
  340. begin
  341. l:=tasmlabel.createglobal(name,FNextLabelNr[alt_data],alt_data);
  342. inc(FNextLabelNr[alt_data]);
  343. FAsmSymbolDict.insert(l);
  344. end;
  345. procedure TAsmData.getaddrlabel(var l : tasmlabel);
  346. begin
  347. l:=tasmlabel.createlocal(FNextLabelNr[alt_addr],alt_addr);
  348. inc(FNextLabelNr[alt_addr]);
  349. FAsmSymbolDict.insert(l);
  350. end;
  351. begin
  352. CAsmCFI:=TAsmCFI;
  353. end.