aasmdata.pas 15 KB

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