aasmdata.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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. { Type of constant 'pools'. Currently contains only string types,
  59. but may be extended with reals, sets, etc. }
  60. TConstPoolType = (
  61. sp_invalid,
  62. sp_conststr,
  63. sp_shortstr,
  64. sp_longstr,
  65. sp_ansistr,
  66. sp_widestr,
  67. sp_unicodestr
  68. );
  69. const
  70. AsmListTypeStr : array[TAsmListType] of string[24] =(
  71. 'al_begin',
  72. 'al_stabs',
  73. 'al_procedures',
  74. 'al_globals',
  75. 'al_const',
  76. 'al_typedconsts',
  77. 'al_rotypedconsts',
  78. 'al_threadvars',
  79. 'al_imports',
  80. 'al_exports',
  81. 'al_resources',
  82. 'al_rtti',
  83. 'al_dwarf',
  84. 'al_dwarf_info',
  85. 'al_dwarf_abbrev',
  86. 'al_dwarf_line',
  87. 'al_picdata',
  88. 'al_resourcestrings',
  89. 'al_end'
  90. );
  91. type
  92. TAsmList = class(tlinkedlist)
  93. constructor create;
  94. function empty : boolean;
  95. function getlasttaifilepos : pfileposinfo;
  96. end;
  97. TAsmCFI=class
  98. public
  99. constructor create;virtual;
  100. destructor destroy;override;
  101. procedure generate_code(list:TAsmList);virtual;
  102. procedure start_frame(list:TAsmList);virtual;
  103. procedure end_frame(list:TAsmList);virtual;
  104. procedure cfa_offset(list:TAsmList;reg:tregister;ofs:longint);virtual;
  105. procedure cfa_restore(list:TAsmList;reg:tregister);virtual;
  106. procedure cfa_def_cfa_register(list:TAsmList;reg:tregister);virtual;
  107. procedure cfa_def_cfa_offset(list:TAsmList;ofs:longint);virtual;
  108. end;
  109. TAsmCFIClass=class of TAsmCFI;
  110. TAsmData = class
  111. private
  112. { Symbols }
  113. FAsmSymbolDict : TFPHashObjectList;
  114. FAltSymbolList : TFPObjectList;
  115. FNextAltNr : longint;
  116. FNextLabelNr : array[TAsmLabeltype] of longint;
  117. { Call Frame Information for stack unwinding}
  118. FAsmCFI : TAsmCFI;
  119. public
  120. name,
  121. realname : string[80];
  122. NextVTEntryNr : longint;
  123. { Assembler lists }
  124. AsmLists : array[TAsmListType] of TAsmList;
  125. CurrAsmList : TAsmList;
  126. { hash tables for reusing constant storage }
  127. ConstPools : array[TConstPoolType] of THashSet;
  128. constructor create(const n:string);
  129. destructor destroy;override;
  130. { asmsymbol }
  131. function DefineAsmSymbol(const s : string;_bind:TAsmSymBind;_typ:Tasmsymtype) : TAsmSymbol;
  132. function RefAsmSymbol(const s : string) : TAsmSymbol;
  133. function GetAsmSymbol(const s : string) : TAsmSymbol;
  134. { create new assembler label }
  135. procedure getlabel(out l : TAsmLabel;alt:TAsmLabeltype);
  136. procedure getjumplabel(out l : TAsmLabel);
  137. procedure getglobaljumplabel(out l : TAsmLabel);
  138. procedure getaddrlabel(out l : TAsmLabel);
  139. procedure getdatalabel(out l : TAsmLabel);
  140. { generate an alternative (duplicate) symbol }
  141. procedure GenerateAltSymbol(p:TAsmSymbol);
  142. procedure ResetAltSymbols;
  143. property AsmSymbolDict:TFPHashObjectList read FAsmSymbolDict;
  144. property AsmCFI:TAsmCFI read FAsmCFI;
  145. end;
  146. var
  147. CAsmCFI : TAsmCFIClass;
  148. current_asmdata : TAsmData;
  149. implementation
  150. uses
  151. verbose,
  152. aasmtai;
  153. {$ifdef MEMDEBUG}
  154. var
  155. memasmsymbols,
  156. memasmcfi,
  157. memasmlists : TMemDebug;
  158. {$endif MEMDEBUG}
  159. {*****************************************************************************
  160. TAsmCFI
  161. *****************************************************************************}
  162. constructor TAsmCFI.create;
  163. begin
  164. end;
  165. destructor TAsmCFI.destroy;
  166. begin
  167. end;
  168. procedure TAsmCFI.generate_code(list:TAsmList);
  169. begin
  170. end;
  171. procedure TAsmCFI.start_frame(list:TAsmList);
  172. begin
  173. end;
  174. procedure TAsmCFI.end_frame(list:TAsmList);
  175. begin
  176. end;
  177. procedure TAsmCFI.cfa_offset(list:TAsmList;reg:tregister;ofs:longint);
  178. begin
  179. end;
  180. procedure TAsmCFI.cfa_restore(list:TAsmList;reg:tregister);
  181. begin
  182. end;
  183. procedure TAsmCFI.cfa_def_cfa_register(list:TAsmList;reg:tregister);
  184. begin
  185. end;
  186. procedure TAsmCFI.cfa_def_cfa_offset(list:TAsmList;ofs:longint);
  187. begin
  188. end;
  189. {*****************************************************************************
  190. TAsmList
  191. *****************************************************************************}
  192. constructor TAsmList.create;
  193. begin
  194. inherited create;
  195. { make sure the optimizer won't remove the first tai of this list}
  196. insert(tai_marker.create(mark_BlockStart));
  197. end;
  198. function TAsmList.empty : boolean;
  199. begin
  200. { there is always a mark_BlockStart available,
  201. see TAsmList.create }
  202. result:=(count<=1);
  203. end;
  204. function TAsmList.getlasttaifilepos : pfileposinfo;
  205. var
  206. hp : tlinkedlistitem;
  207. begin
  208. getlasttaifilepos := nil;
  209. if assigned(last) then
  210. begin
  211. { find the last file information record }
  212. if not (tai(last).typ in SkipLineInfo) then
  213. getlasttaifilepos:=@tailineinfo(last).fileinfo
  214. else
  215. { go through list backwards to find the first entry
  216. with line information
  217. }
  218. begin
  219. hp:=tai(last);
  220. while assigned(hp) and (tai(hp).typ in SkipLineInfo) do
  221. hp:=hp.Previous;
  222. { found entry }
  223. if assigned(hp) then
  224. getlasttaifilepos:=@tailineinfo(hp).fileinfo
  225. end;
  226. end;
  227. end;
  228. {****************************************************************************
  229. TAsmData
  230. ****************************************************************************}
  231. constructor TAsmData.create(const n:string);
  232. var
  233. alt : TAsmLabelType;
  234. hal : TAsmListType;
  235. begin
  236. inherited create;
  237. realname:=n;
  238. name:=upper(n);
  239. { symbols }
  240. FAsmSymbolDict:=TFPHashObjectList.create(true);
  241. FAltSymbolList:=TFPObjectList.Create(false);
  242. { labels }
  243. FNextAltNr:=1;
  244. for alt:=low(TAsmLabelType) to high(TAsmLabelType) do
  245. FNextLabelNr[alt]:=1;
  246. { AsmLists }
  247. CurrAsmList:=TAsmList.create;
  248. for hal:=low(TAsmListType) to high(TAsmListType) do
  249. AsmLists[hal]:=TAsmList.create;
  250. { PIC data }
  251. if (target_info.system in [system_powerpc_darwin,system_powerpc64_darwin,system_i386_darwin]) then
  252. AsmLists[al_picdata].concat(tai_directive.create(asd_non_lazy_symbol_pointer,''));
  253. { CFI }
  254. FAsmCFI:=CAsmCFI.Create;
  255. end;
  256. destructor TAsmData.destroy;
  257. var
  258. hal : TAsmListType;
  259. hp : TConstPoolType;
  260. begin
  261. { Symbols }
  262. {$ifdef MEMDEBUG}
  263. memasmsymbols.start;
  264. {$endif}
  265. FAltSymbolList.free;
  266. FAsmSymbolDict.free;
  267. {$ifdef MEMDEBUG}
  268. memasmsymbols.stop;
  269. {$endif}
  270. { CFI }
  271. {$ifdef MEMDEBUG}
  272. memasmcfi.start;
  273. {$endif}
  274. FAsmCFI.free;
  275. {$ifdef MEMDEBUG}
  276. memasmcfi.stop;
  277. {$endif}
  278. { Lists }
  279. {$ifdef MEMDEBUG}
  280. memasmlists.start;
  281. {$endif}
  282. for hal:=low(TAsmListType) to high(TAsmListType) do
  283. AsmLists[hal].free;
  284. CurrAsmList.free;
  285. {$ifdef MEMDEBUG}
  286. memasmlists.stop;
  287. {$endif}
  288. for hp := low(TConstPoolType) to high(TConstPoolType) do
  289. ConstPools[hp].Free;
  290. end;
  291. function TAsmData.DefineAsmSymbol(const s : string;_bind:TAsmSymBind;_typ:Tasmsymtype) : TAsmSymbol;
  292. var
  293. hp : TAsmSymbol;
  294. begin
  295. hp:=TAsmSymbol(FAsmSymbolDict.Find(s));
  296. if assigned(hp) then
  297. begin
  298. { Redefine is allowed, but the types must be the same. The redefine
  299. is needed for Darwin where the labels are first allocated }
  300. if (hp.bind<>AB_EXTERNAL) then
  301. begin
  302. if (hp.bind<>_bind) and
  303. (hp.typ<>_typ) then
  304. internalerror(200603261);
  305. end;
  306. hp.typ:=_typ;
  307. hp.bind:=_bind;
  308. end
  309. else
  310. begin
  311. { Not found, insert it. }
  312. hp:=TAsmSymbol.create(AsmSymbolDict,s,_bind,_typ);
  313. end;
  314. result:=hp;
  315. end;
  316. function TAsmData.RefAsmSymbol(const s : string) : TAsmSymbol;
  317. begin
  318. result:=TAsmSymbol(FAsmSymbolDict.Find(s));
  319. if not assigned(result) then
  320. result:=TAsmSymbol.create(AsmSymbolDict,s,AB_EXTERNAL,AT_NONE);
  321. end;
  322. function TAsmData.GetAsmSymbol(const s : string) : TAsmSymbol;
  323. begin
  324. result:=TAsmSymbol(FAsmSymbolDict.Find(s));
  325. end;
  326. procedure TAsmData.GenerateAltSymbol(p:TAsmSymbol);
  327. begin
  328. if not assigned(p.altsymbol) then
  329. begin
  330. p.altsymbol:=p.getaltcopy(AsmSymbolDict,FNextAltNr);
  331. FAltSymbolList.Add(p);
  332. end;
  333. end;
  334. procedure TAsmData.ResetAltSymbols;
  335. var
  336. i : longint;
  337. begin
  338. for i:=0 to FAltSymbolList.Count-1 do
  339. TAsmSymbol(FAltSymbolList[i]).altsymbol:=nil;
  340. FAltSymbolList.Clear;
  341. end;
  342. procedure TAsmData.getlabel(out l : TAsmLabel;alt:TAsmLabeltype);
  343. begin
  344. l:=TAsmLabel.createlocal(AsmSymbolDict,FNextLabelNr[alt],alt);
  345. inc(FNextLabelNr[alt]);
  346. end;
  347. procedure TAsmData.getjumplabel(out l : TAsmLabel);
  348. begin
  349. l:=TAsmLabel.createlocal(AsmSymbolDict,FNextLabelNr[alt_jump],alt_jump);
  350. inc(FNextLabelNr[alt_jump]);
  351. end;
  352. procedure TAsmData.getglobaljumplabel(out l : TAsmLabel);
  353. begin
  354. l:=TAsmLabel.createglobal(AsmSymbolDict,name,FNextLabelNr[alt_jump],alt_jump);
  355. inc(FNextLabelNr[alt_jump]);
  356. end;
  357. procedure TAsmData.getdatalabel(out l : TAsmLabel);
  358. begin
  359. l:=TAsmLabel.createglobal(AsmSymbolDict,name,FNextLabelNr[alt_data],alt_data);
  360. inc(FNextLabelNr[alt_data]);
  361. end;
  362. procedure TAsmData.getaddrlabel(out l : TAsmLabel);
  363. begin
  364. l:=TAsmLabel.createlocal(AsmSymbolDict,FNextLabelNr[alt_addr],alt_addr);
  365. inc(FNextLabelNr[alt_addr]);
  366. end;
  367. initialization
  368. {$ifdef MEMDEBUG}
  369. memasmsymbols:=TMemDebug.create('AsmSymbols');
  370. memasmsymbols.stop;
  371. memasmcfi:=TMemDebug.create('AsmCFI');
  372. memasmcfi.stop;
  373. memasmlists:=TMemDebug.create('AsmLists');
  374. memasmlists.stop;
  375. {$endif MEMDEBUG}
  376. CAsmCFI:=TAsmCFI;
  377. finalization
  378. {$ifdef MEMDEBUG}
  379. memasmsymbols.free;
  380. memasmcfi.free;
  381. memasmlists.free;
  382. {$endif MEMDEBUG}
  383. end.