aasmdata.pas 18 KB

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