aasmdata.pas 16 KB

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