aasmdata.pas 16 KB

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