aasmdata.pas 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  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. protected
  146. function DefineAsmSymbolByClassBase(symclass: TAsmSymbolClass; const s : TSymStr;_bind:TAsmSymBind;_typ:Tasmsymtype; def: tdef; out wasdefined: boolean) : TAsmSymbol;
  147. public
  148. name : pshortstring; { owned by tmodule }
  149. NextVTEntryNr : longint;
  150. { Assembler lists }
  151. AsmLists : array[TAsmListType] of TAsmList;
  152. CurrAsmList : TAsmList;
  153. WideInits : TLinkedList;
  154. ResStrInits : TLinkedList;
  155. constructor create(n: pshortstring);
  156. destructor destroy;override;
  157. { asmsymbol }
  158. function DefineAsmSymbolByClass(symclass: TAsmSymbolClass; const s : TSymStr;_bind:TAsmSymBind;_typ:Tasmsymtype; def: tdef) : TAsmSymbol; virtual;
  159. function DefineAsmSymbol(const s : TSymStr;_bind:TAsmSymBind;_typ:Tasmsymtype; def: tdef) : TAsmSymbol;
  160. function WeakRefAsmSymbol(const s : TSymStr;_typ:Tasmsymtype=AT_NONE) : TAsmSymbol;
  161. function RefAsmSymbol(const s : TSymStr;_typ:Tasmsymtype=AT_NONE;indirect:boolean=false) : TAsmSymbol;
  162. function GetAsmSymbol(const s : TSymStr) : TAsmSymbol;
  163. { create new assembler label }
  164. procedure getlabel(out l : TAsmLabel;alt:TAsmLabeltype);
  165. procedure getjumplabel(out l : TAsmLabel);
  166. procedure getglobaljumplabel(out l : TAsmLabel);
  167. procedure getaddrlabel(out l : TAsmLabel);
  168. { visible from outside current object }
  169. procedure getglobaldatalabel(out l : TAsmLabel);
  170. { visible only inside current object, but doesn't start with
  171. target_asm.label_prefix (treated the Darwin linker as the start of a
  172. dead-strippable data block) }
  173. procedure getstaticdatalabel(out l : TAsmLabel);
  174. { visible only inside the current object and does start with
  175. target_asm.label_prefix (not treated by the Darwin linker as the start
  176. of a dead-strippable data block, and references to such labels are
  177. also ignored to determine whether a data block should be live) }
  178. procedure getlocaldatalabel(out l : TAsmLabel);
  179. { generate an alternative (duplicate) symbol }
  180. procedure GenerateAltSymbol(p:TAsmSymbol);
  181. procedure ResetAltSymbols;
  182. property AsmSymbolDict:TFPHashObjectList read FAsmSymbolDict;
  183. property AsmCFI:TAsmCFI read FAsmCFI;
  184. { hash tables for reusing constant storage }
  185. property ConstPools[APoolType:TConstPoolType]: THashSet read GetConstPools;
  186. end;
  187. TAsmDataClass = class of TAsmData;
  188. TTCInitItem = class(TLinkedListItem)
  189. sym: tsym;
  190. offset: aint;
  191. datalabel: TAsmSymbol;
  192. constructor Create(asym: tsym; aoffset: aint; alabel: TAsmSymbol);
  193. end;
  194. const
  195. casmdata: TAsmDataClass = TAsmData;
  196. var
  197. CAsmCFI : TAsmCFIClass;
  198. current_asmdata : TAsmData;
  199. implementation
  200. uses
  201. verbose,
  202. symconst,
  203. aasmtai;
  204. {$ifdef MEMDEBUG}
  205. var
  206. memasmsymbols,
  207. memasmcfi,
  208. memasmlists : TMemDebug;
  209. {$endif MEMDEBUG}
  210. {*****************************************************************************
  211. TAsmCFI
  212. *****************************************************************************}
  213. constructor TAsmCFI.create;
  214. begin
  215. end;
  216. destructor TAsmCFI.destroy;
  217. begin
  218. end;
  219. procedure TAsmCFI.generate_code(list:TAsmList);
  220. begin
  221. end;
  222. procedure TAsmCFI.start_frame(list:TAsmList);
  223. begin
  224. end;
  225. procedure TAsmCFI.end_frame(list:TAsmList);
  226. begin
  227. end;
  228. procedure TAsmCFI.cfa_offset(list:TAsmList;reg:tregister;ofs:longint);
  229. begin
  230. end;
  231. procedure TAsmCFI.cfa_restore(list:TAsmList;reg:tregister);
  232. begin
  233. end;
  234. procedure TAsmCFI.cfa_def_cfa_register(list:TAsmList;reg:tregister);
  235. begin
  236. end;
  237. procedure TAsmCFI.cfa_def_cfa_offset(list:TAsmList;ofs:longint);
  238. begin
  239. end;
  240. {*****************************************************************************
  241. TTCInitItem
  242. *****************************************************************************}
  243. constructor TTCInitItem.Create(asym: tsym; aoffset: aint; alabel: TAsmSymbol);
  244. begin
  245. inherited Create;
  246. sym:=asym;
  247. offset:=aoffset;
  248. datalabel:=alabel;
  249. end;
  250. {*****************************************************************************
  251. TAsmList
  252. *****************************************************************************}
  253. constructor TAsmList.create;
  254. begin
  255. inherited create;
  256. end;
  257. function TAsmList.getlasttaifilepos : pfileposinfo;
  258. var
  259. hp : tlinkedlistitem;
  260. begin
  261. getlasttaifilepos := nil;
  262. if assigned(last) then
  263. begin
  264. { find the last file information record }
  265. if not (tai(last).typ in SkipLineInfo) then
  266. getlasttaifilepos:=@tailineinfo(last).fileinfo
  267. else
  268. { go through list backwards to find the first entry
  269. with line information
  270. }
  271. begin
  272. hp:=tai(last);
  273. while assigned(hp) and (tai(hp).typ in SkipLineInfo) do
  274. hp:=hp.Previous;
  275. { found entry }
  276. if assigned(hp) then
  277. getlasttaifilepos:=@tailineinfo(hp).fileinfo
  278. end;
  279. end;
  280. end;
  281. {****************************************************************************
  282. TAsmData
  283. ****************************************************************************}
  284. function TAsmData.GetConstPools(APoolType: TConstPoolType): THashSet;
  285. begin
  286. if FConstPools[APoolType] = nil then
  287. case APoolType of
  288. sp_ansistr: FConstPools[APoolType] := TTagHashSet.Create(64, True, False);
  289. else
  290. FConstPools[APoolType] := THashSet.Create(64, True, False);
  291. end;
  292. Result := FConstPools[APoolType];
  293. end;
  294. function TAsmData.DefineAsmSymbolByClassBase(symclass: TAsmSymbolClass; const s: TSymStr; _bind: TAsmSymBind; _typ: Tasmsymtype; def: tdef; out wasdefined: boolean): TAsmSymbol;
  295. var
  296. hp : TAsmSymbol;
  297. namestr : TSymStr;
  298. begin
  299. namestr:=s;
  300. if _bind in asmsymbindindirect then
  301. namestr:=namestr+suffix_indirect;
  302. hp:=TAsmSymbol(FAsmSymbolDict.Find(namestr));
  303. if assigned(hp) then
  304. begin
  305. { Redefine is allowed, but the types must be the same. The redefine
  306. is needed for Darwin where the labels are first allocated }
  307. wasdefined:=not(hp.bind in [AB_EXTERNAL,AB_WEAK_EXTERNAL]);
  308. if wasdefined then
  309. begin
  310. if (hp.bind<>_bind) and
  311. (hp.typ<>_typ) then
  312. internalerror(200603261);
  313. end;
  314. hp.typ:=_typ;
  315. { Changing bind from AB_GLOBAL to AB_LOCAL is wrong
  316. if bind is already AB_GLOBAL or AB_EXTERNAL,
  317. GOT might have been used, so change might be harmful. }
  318. if (_bind<>hp.bind) and (hp.getrefs>0) then
  319. begin
  320. {$ifdef extdebug}
  321. { the changes that matter must become internalerrors, the rest
  322. should be ignored; a used cannot change anything about this,
  323. so printing a warning/hint is not useful }
  324. if (_bind=AB_LOCAL) then
  325. Message3(asmw_w_changing_bind_type,namestr,asmsymbindname[hp.bind],asmsymbindname[_bind])
  326. else
  327. Message3(asmw_h_changing_bind_type,namestr,asmsymbindname[hp.bind],asmsymbindname[_bind]);
  328. {$endif extdebug}
  329. end;
  330. hp.bind:=_bind;
  331. end
  332. else
  333. begin
  334. wasdefined:=false;
  335. { Not found, insert it. }
  336. hp:=symclass.create(AsmSymbolDict,namestr,_bind,_typ);
  337. end;
  338. result:=hp;
  339. end;
  340. constructor TAsmData.create(n:pshortstring);
  341. var
  342. alt : TAsmLabelType;
  343. hal : TAsmListType;
  344. begin
  345. inherited create;
  346. name:=n;
  347. { symbols }
  348. FAsmSymbolDict:=TFPHashObjectList.create(true);
  349. FAltSymbolList:=TFPObjectList.Create(false);
  350. { labels }
  351. FNextAltNr:=1;
  352. for alt:=low(TAsmLabelType) to high(TAsmLabelType) do
  353. FNextLabelNr[alt]:=1;
  354. { AsmLists }
  355. CurrAsmList:=TAsmList.create;
  356. for hal:=low(TAsmListType) to high(TAsmListType) do
  357. AsmLists[hal]:=TAsmList.create;
  358. WideInits :=TLinkedList.create;
  359. ResStrInits:=TLinkedList.create;
  360. { CFI }
  361. FAsmCFI:=CAsmCFI.Create;
  362. end;
  363. destructor TAsmData.destroy;
  364. var
  365. hal : TAsmListType;
  366. hp : TConstPoolType;
  367. begin
  368. { Symbols }
  369. {$ifdef MEMDEBUG}
  370. memasmsymbols.start;
  371. {$endif}
  372. FAltSymbolList.free;
  373. FAsmSymbolDict.free;
  374. {$ifdef MEMDEBUG}
  375. memasmsymbols.stop;
  376. {$endif}
  377. { CFI }
  378. {$ifdef MEMDEBUG}
  379. memasmcfi.start;
  380. {$endif}
  381. FAsmCFI.free;
  382. {$ifdef MEMDEBUG}
  383. memasmcfi.stop;
  384. {$endif}
  385. { Lists }
  386. {$ifdef MEMDEBUG}
  387. memasmlists.start;
  388. {$endif}
  389. ResStrInits.free;
  390. WideInits.free;
  391. for hal:=low(TAsmListType) to high(TAsmListType) do
  392. AsmLists[hal].free;
  393. CurrAsmList.free;
  394. {$ifdef MEMDEBUG}
  395. memasmlists.stop;
  396. {$endif}
  397. for hp := low(TConstPoolType) to high(TConstPoolType) do
  398. FConstPools[hp].Free;
  399. end;
  400. function TAsmData.DefineAsmSymbolByClass(symclass: TAsmSymbolClass; const s: TSymStr; _bind: TAsmSymBind; _typ: Tasmsymtype; def: tdef): TAsmSymbol;
  401. var
  402. wasdefined: boolean;
  403. begin
  404. result:=DefineAsmSymbolByClassBase(symclass,s,_bind,_typ,def,wasdefined);
  405. end;
  406. function TAsmData.DefineAsmSymbol(const s: TSymStr; _bind: TAsmSymBind; _typ: Tasmsymtype; def: tdef): TAsmSymbol;
  407. begin
  408. result:=DefineAsmSymbolByClass(TAsmSymbol,s,_bind,_typ,def);
  409. end;
  410. function TAsmData.RefAsmSymbol(const s : TSymStr;_typ:Tasmsymtype;indirect:boolean) : TAsmSymbol;
  411. var
  412. namestr : TSymStr;
  413. bind : tasmsymbind;
  414. begin
  415. namestr:=s;
  416. if indirect then
  417. begin
  418. namestr:=namestr+suffix_indirect;
  419. bind:=AB_EXTERNAL_INDIRECT;
  420. end
  421. else
  422. begin
  423. bind:=AB_EXTERNAL;
  424. end;
  425. result:=TAsmSymbol(FAsmSymbolDict.Find(namestr));
  426. if not assigned(result) then
  427. result:=TAsmSymbol.create(AsmSymbolDict,namestr,bind,_typ)
  428. { one normal reference removes the "weak" character of a symbol }
  429. else if (result.bind=AB_WEAK_EXTERNAL) then
  430. result.bind:=bind;
  431. end;
  432. function TAsmData.WeakRefAsmSymbol(const s : TSymStr;_typ:Tasmsymtype=AT_NONE) : TAsmSymbol;
  433. begin
  434. result:=TAsmSymbol(FAsmSymbolDict.Find(s));
  435. if not assigned(result) then
  436. result:=TAsmSymbol.create(AsmSymbolDict,s,AB_WEAK_EXTERNAL,_typ);
  437. end;
  438. function TAsmData.GetAsmSymbol(const s : TSymStr) : TAsmSymbol;
  439. begin
  440. result:=TAsmSymbol(FAsmSymbolDict.Find(s));
  441. end;
  442. procedure TAsmData.GenerateAltSymbol(p:TAsmSymbol);
  443. begin
  444. if not assigned(p.altsymbol) then
  445. begin
  446. p.altsymbol:=p.getaltcopy(AsmSymbolDict,FNextAltNr);
  447. FAltSymbolList.Add(p);
  448. end;
  449. end;
  450. procedure TAsmData.ResetAltSymbols;
  451. var
  452. i : longint;
  453. begin
  454. for i:=0 to FAltSymbolList.Count-1 do
  455. TAsmSymbol(FAltSymbolList[i]).altsymbol:=nil;
  456. FAltSymbolList.Clear;
  457. end;
  458. procedure TAsmData.getlabel(out l : TAsmLabel;alt:TAsmLabeltype);
  459. begin
  460. if (target_info.system in (systems_linux + systems_bsd + systems_android)) and
  461. { the next condition was
  462. (cs_create_smart in current_settings.moduleswitches) and
  463. but if we create_smartlink_sections, this is useless }
  464. (create_smartlink_library) and
  465. (alt = alt_dbgline) then
  466. l:=TAsmLabel.createglobal(AsmSymbolDict,name^,FNextLabelNr[alt],alt)
  467. else
  468. l:=TAsmLabel.createlocal(AsmSymbolDict,FNextLabelNr[alt],alt);
  469. inc(FNextLabelNr[alt]);
  470. end;
  471. procedure TAsmData.getjumplabel(out l : TAsmLabel);
  472. begin
  473. l:=TAsmLabel.createlocal(AsmSymbolDict,FNextLabelNr[alt_jump],alt_jump);
  474. inc(FNextLabelNr[alt_jump]);
  475. end;
  476. procedure TAsmData.getglobaljumplabel(out l : TAsmLabel);
  477. begin
  478. l:=TAsmLabel.createglobal(AsmSymbolDict,name^,FNextLabelNr[alt_jump],alt_jump);
  479. inc(FNextLabelNr[alt_jump]);
  480. end;
  481. procedure TAsmData.getglobaldatalabel(out l : TAsmLabel);
  482. begin
  483. l:=TAsmLabel.createglobal(AsmSymbolDict,name^,FNextLabelNr[alt_data],alt_data);
  484. inc(FNextLabelNr[alt_data]);
  485. end;
  486. procedure TAsmData.getstaticdatalabel(out l : TAsmLabel);
  487. begin
  488. l:=TAsmLabel.createstatic(AsmSymbolDict,FNextLabelNr[alt_data],alt_data);
  489. inc(FNextLabelNr[alt_data]);
  490. end;
  491. procedure TAsmData.getlocaldatalabel(out l: TAsmLabel);
  492. begin
  493. l:=TAsmLabel.createlocal(AsmSymbolDict,FNextLabelNr[alt_data],alt_data);
  494. inc(FNextLabelNr[alt_data]);
  495. end;
  496. procedure TAsmData.getaddrlabel(out l : TAsmLabel);
  497. begin
  498. l:=TAsmLabel.createlocal(AsmSymbolDict,FNextLabelNr[alt_addr],alt_addr);
  499. inc(FNextLabelNr[alt_addr]);
  500. end;
  501. initialization
  502. {$ifdef MEMDEBUG}
  503. memasmsymbols:=TMemDebug.create('AsmSymbols');
  504. memasmsymbols.stop;
  505. memasmcfi:=TMemDebug.create('AsmCFI');
  506. memasmcfi.stop;
  507. memasmlists:=TMemDebug.create('AsmLists');
  508. memasmlists.stop;
  509. {$endif MEMDEBUG}
  510. CAsmCFI:=TAsmCFI;
  511. finalization
  512. {$ifdef MEMDEBUG}
  513. memasmsymbols.free;
  514. memasmcfi.free;
  515. memasmlists.free;
  516. {$endif MEMDEBUG}
  517. end.