aasmdata.pas 13 KB

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