aasmdata.pas 15 KB

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