aasmdata.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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_frame,
  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_frame',
  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 WeakRefAsmSymbol(const s : string) : TAsmSymbol;
  133. function RefAsmSymbol(const s : string) : TAsmSymbol;
  134. function GetAsmSymbol(const s : string) : TAsmSymbol;
  135. { create new assembler label }
  136. procedure getlabel(out l : TAsmLabel;alt:TAsmLabeltype);
  137. procedure getjumplabel(out l : TAsmLabel);
  138. procedure getglobaljumplabel(out l : TAsmLabel);
  139. procedure getaddrlabel(out l : TAsmLabel);
  140. procedure getdatalabel(out l : TAsmLabel);
  141. { generate an alternative (duplicate) symbol }
  142. procedure GenerateAltSymbol(p:TAsmSymbol);
  143. procedure ResetAltSymbols;
  144. property AsmSymbolDict:TFPHashObjectList read FAsmSymbolDict;
  145. property AsmCFI:TAsmCFI read FAsmCFI;
  146. end;
  147. var
  148. CAsmCFI : TAsmCFIClass;
  149. current_asmdata : TAsmData;
  150. implementation
  151. uses
  152. verbose,
  153. aasmtai;
  154. {$ifdef MEMDEBUG}
  155. var
  156. memasmsymbols,
  157. memasmcfi,
  158. memasmlists : TMemDebug;
  159. {$endif MEMDEBUG}
  160. {*****************************************************************************
  161. TAsmCFI
  162. *****************************************************************************}
  163. constructor TAsmCFI.create;
  164. begin
  165. end;
  166. destructor TAsmCFI.destroy;
  167. begin
  168. end;
  169. procedure TAsmCFI.generate_code(list:TAsmList);
  170. begin
  171. end;
  172. procedure TAsmCFI.start_frame(list:TAsmList);
  173. begin
  174. end;
  175. procedure TAsmCFI.end_frame(list:TAsmList);
  176. begin
  177. end;
  178. procedure TAsmCFI.cfa_offset(list:TAsmList;reg:tregister;ofs:longint);
  179. begin
  180. end;
  181. procedure TAsmCFI.cfa_restore(list:TAsmList;reg:tregister);
  182. begin
  183. end;
  184. procedure TAsmCFI.cfa_def_cfa_register(list:TAsmList;reg:tregister);
  185. begin
  186. end;
  187. procedure TAsmCFI.cfa_def_cfa_offset(list:TAsmList;ofs:longint);
  188. begin
  189. end;
  190. {*****************************************************************************
  191. TAsmList
  192. *****************************************************************************}
  193. constructor TAsmList.create;
  194. begin
  195. inherited create;
  196. { make sure the optimizer won't remove the first tai of this list}
  197. insert(tai_marker.create(mark_BlockStart));
  198. end;
  199. function TAsmList.empty : boolean;
  200. begin
  201. { there is always a mark_BlockStart available,
  202. see TAsmList.create }
  203. result:=(count<=1);
  204. end;
  205. function TAsmList.getlasttaifilepos : pfileposinfo;
  206. var
  207. hp : tlinkedlistitem;
  208. begin
  209. getlasttaifilepos := nil;
  210. if assigned(last) then
  211. begin
  212. { find the last file information record }
  213. if not (tai(last).typ in SkipLineInfo) then
  214. getlasttaifilepos:=@tailineinfo(last).fileinfo
  215. else
  216. { go through list backwards to find the first entry
  217. with line information
  218. }
  219. begin
  220. hp:=tai(last);
  221. while assigned(hp) and (tai(hp).typ in SkipLineInfo) do
  222. hp:=hp.Previous;
  223. { found entry }
  224. if assigned(hp) then
  225. getlasttaifilepos:=@tailineinfo(hp).fileinfo
  226. end;
  227. end;
  228. end;
  229. {****************************************************************************
  230. TAsmData
  231. ****************************************************************************}
  232. constructor TAsmData.create(const n:string);
  233. var
  234. alt : TAsmLabelType;
  235. hal : TAsmListType;
  236. begin
  237. inherited create;
  238. realname:=n;
  239. name:=upper(n);
  240. { symbols }
  241. FAsmSymbolDict:=TFPHashObjectList.create(true);
  242. FAltSymbolList:=TFPObjectList.Create(false);
  243. { labels }
  244. FNextAltNr:=1;
  245. for alt:=low(TAsmLabelType) to high(TAsmLabelType) do
  246. FNextLabelNr[alt]:=1;
  247. { AsmLists }
  248. CurrAsmList:=TAsmList.create;
  249. for hal:=low(TAsmListType) to high(TAsmListType) do
  250. AsmLists[hal]:=TAsmList.create;
  251. { PIC data }
  252. if (target_info.system in [system_powerpc_darwin,system_powerpc64_darwin,system_i386_darwin,system_arm_darwin]) then
  253. AsmLists[al_picdata].concat(tai_directive.create(asd_non_lazy_symbol_pointer,''));
  254. { CFI }
  255. FAsmCFI:=CAsmCFI.Create;
  256. end;
  257. destructor TAsmData.destroy;
  258. var
  259. hal : TAsmListType;
  260. hp : TConstPoolType;
  261. begin
  262. { Symbols }
  263. {$ifdef MEMDEBUG}
  264. memasmsymbols.start;
  265. {$endif}
  266. FAltSymbolList.free;
  267. FAsmSymbolDict.free;
  268. {$ifdef MEMDEBUG}
  269. memasmsymbols.stop;
  270. {$endif}
  271. { CFI }
  272. {$ifdef MEMDEBUG}
  273. memasmcfi.start;
  274. {$endif}
  275. FAsmCFI.free;
  276. {$ifdef MEMDEBUG}
  277. memasmcfi.stop;
  278. {$endif}
  279. { Lists }
  280. {$ifdef MEMDEBUG}
  281. memasmlists.start;
  282. {$endif}
  283. for hal:=low(TAsmListType) to high(TAsmListType) do
  284. AsmLists[hal].free;
  285. CurrAsmList.free;
  286. {$ifdef MEMDEBUG}
  287. memasmlists.stop;
  288. {$endif}
  289. for hp := low(TConstPoolType) to high(TConstPoolType) do
  290. ConstPools[hp].Free;
  291. end;
  292. function TAsmData.DefineAsmSymbol(const s : string;_bind:TAsmSymBind;_typ:Tasmsymtype) : TAsmSymbol;
  293. var
  294. hp : TAsmSymbol;
  295. begin
  296. hp:=TAsmSymbol(FAsmSymbolDict.Find(s));
  297. if assigned(hp) then
  298. begin
  299. { Redefine is allowed, but the types must be the same. The redefine
  300. is needed for Darwin where the labels are first allocated }
  301. if (hp.bind<>AB_EXTERNAL) then
  302. begin
  303. if (hp.bind<>_bind) and
  304. (hp.typ<>_typ) then
  305. internalerror(200603261);
  306. end;
  307. hp.typ:=_typ;
  308. hp.bind:=_bind;
  309. end
  310. else
  311. begin
  312. { Not found, insert it. }
  313. hp:=TAsmSymbol.create(AsmSymbolDict,s,_bind,_typ);
  314. end;
  315. result:=hp;
  316. end;
  317. function TAsmData.RefAsmSymbol(const s : string) : TAsmSymbol;
  318. begin
  319. result:=TAsmSymbol(FAsmSymbolDict.Find(s));
  320. if not assigned(result) then
  321. result:=TAsmSymbol.create(AsmSymbolDict,s,AB_EXTERNAL,AT_NONE)
  322. { one normal reference removes the "weak" character of a symbol }
  323. else if (result.bind=AB_WEAK_EXTERNAL) then
  324. result.bind:=AB_EXTERNAL;
  325. end;
  326. function TAsmData.WeakRefAsmSymbol(const s : string) : TAsmSymbol;
  327. begin
  328. result:=TAsmSymbol(FAsmSymbolDict.Find(s));
  329. if not assigned(result) then
  330. result:=TAsmSymbol.create(AsmSymbolDict,s,AB_WEAK_EXTERNAL,AT_NONE);
  331. end;
  332. function TAsmData.GetAsmSymbol(const s : string) : TAsmSymbol;
  333. begin
  334. result:=TAsmSymbol(FAsmSymbolDict.Find(s));
  335. end;
  336. procedure TAsmData.GenerateAltSymbol(p:TAsmSymbol);
  337. begin
  338. if not assigned(p.altsymbol) then
  339. begin
  340. p.altsymbol:=p.getaltcopy(AsmSymbolDict,FNextAltNr);
  341. FAltSymbolList.Add(p);
  342. end;
  343. end;
  344. procedure TAsmData.ResetAltSymbols;
  345. var
  346. i : longint;
  347. begin
  348. for i:=0 to FAltSymbolList.Count-1 do
  349. TAsmSymbol(FAltSymbolList[i]).altsymbol:=nil;
  350. FAltSymbolList.Clear;
  351. end;
  352. procedure TAsmData.getlabel(out l : TAsmLabel;alt:TAsmLabeltype);
  353. begin
  354. l:=TAsmLabel.createlocal(AsmSymbolDict,FNextLabelNr[alt],alt);
  355. inc(FNextLabelNr[alt]);
  356. end;
  357. procedure TAsmData.getjumplabel(out l : TAsmLabel);
  358. begin
  359. l:=TAsmLabel.createlocal(AsmSymbolDict,FNextLabelNr[alt_jump],alt_jump);
  360. inc(FNextLabelNr[alt_jump]);
  361. end;
  362. procedure TAsmData.getglobaljumplabel(out l : TAsmLabel);
  363. begin
  364. l:=TAsmLabel.createglobal(AsmSymbolDict,name,FNextLabelNr[alt_jump],alt_jump);
  365. inc(FNextLabelNr[alt_jump]);
  366. end;
  367. procedure TAsmData.getdatalabel(out l : TAsmLabel);
  368. begin
  369. l:=TAsmLabel.createglobal(AsmSymbolDict,name,FNextLabelNr[alt_data],alt_data);
  370. inc(FNextLabelNr[alt_data]);
  371. end;
  372. procedure TAsmData.getaddrlabel(out l : TAsmLabel);
  373. begin
  374. l:=TAsmLabel.createlocal(AsmSymbolDict,FNextLabelNr[alt_addr],alt_addr);
  375. inc(FNextLabelNr[alt_addr]);
  376. end;
  377. initialization
  378. {$ifdef MEMDEBUG}
  379. memasmsymbols:=TMemDebug.create('AsmSymbols');
  380. memasmsymbols.stop;
  381. memasmcfi:=TMemDebug.create('AsmCFI');
  382. memasmcfi.stop;
  383. memasmlists:=TMemDebug.create('AsmLists');
  384. memasmlists.stop;
  385. {$endif MEMDEBUG}
  386. CAsmCFI:=TAsmCFI;
  387. finalization
  388. {$ifdef MEMDEBUG}
  389. memasmsymbols.free;
  390. memasmcfi.free;
  391. memasmlists.free;
  392. {$endif MEMDEBUG}
  393. end.