aasmdata.pas 15 KB

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