aasmdata.pas 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  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) : TAsmSymbol;
  161. function RefAsmSymbol(const s : TSymStr;_typ:Tasmsymtype;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. var
  195. casmdata: TAsmDataClass;
  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. { this difference is only necessary to determine whether we always need
  300. indirect references or not }
  301. if _typ=AT_DATA_FORCEINDIRECT then
  302. _typ:=AT_DATA;
  303. namestr:=s;
  304. if _bind in asmsymbindindirect then
  305. namestr:=namestr+suffix_indirect;
  306. hp:=TAsmSymbol(FAsmSymbolDict.Find(namestr));
  307. if assigned(hp) then
  308. begin
  309. { Redefine is allowed, but the types must be the same. The redefine
  310. is needed for Darwin where the labels are first allocated }
  311. wasdefined:=not(hp.bind in [AB_EXTERNAL,AB_WEAK_EXTERNAL]);
  312. if wasdefined then
  313. begin
  314. if (hp.bind<>_bind) and
  315. (hp.typ<>_typ) then
  316. internalerror(200603261);
  317. end;
  318. hp.typ:=_typ;
  319. { Changing bind from AB_GLOBAL to AB_LOCAL is wrong
  320. if bind is already AB_GLOBAL or AB_EXTERNAL,
  321. GOT might have been used, so change might be harmful. }
  322. if (_bind<>hp.bind) and (hp.getrefs>0) then
  323. begin
  324. {$ifdef extdebug}
  325. { the changes that matter must become internalerrors, the rest
  326. should be ignored; a used cannot change anything about this,
  327. so printing a warning/hint is not useful }
  328. if (_bind=AB_LOCAL) then
  329. Message3(asmw_w_changing_bind_type,namestr,asmsymbindname[hp.bind],asmsymbindname[_bind])
  330. else
  331. Message3(asmw_h_changing_bind_type,namestr,asmsymbindname[hp.bind],asmsymbindname[_bind]);
  332. {$endif extdebug}
  333. end;
  334. hp.bind:=_bind;
  335. end
  336. else
  337. begin
  338. wasdefined:=false;
  339. { Not found, insert it. }
  340. hp:=symclass.create(AsmSymbolDict,namestr,_bind,_typ);
  341. end;
  342. result:=hp;
  343. end;
  344. constructor TAsmData.create(n:pshortstring);
  345. var
  346. alt : TAsmLabelType;
  347. hal : TAsmListType;
  348. begin
  349. inherited create;
  350. name:=n;
  351. { symbols }
  352. FAsmSymbolDict:=TFPHashObjectList.create(true);
  353. FAltSymbolList:=TFPObjectList.Create(false);
  354. { labels }
  355. FNextAltNr:=1;
  356. for alt:=low(TAsmLabelType) to high(TAsmLabelType) do
  357. FNextLabelNr[alt]:=1;
  358. { AsmLists }
  359. CurrAsmList:=TAsmList.create;
  360. for hal:=low(TAsmListType) to high(TAsmListType) do
  361. AsmLists[hal]:=TAsmList.create;
  362. WideInits :=TLinkedList.create;
  363. ResStrInits:=TLinkedList.create;
  364. { CFI }
  365. FAsmCFI:=CAsmCFI.Create;
  366. end;
  367. destructor TAsmData.destroy;
  368. var
  369. hal : TAsmListType;
  370. hp : TConstPoolType;
  371. begin
  372. { Symbols }
  373. {$ifdef MEMDEBUG}
  374. memasmsymbols.start;
  375. {$endif}
  376. FAltSymbolList.free;
  377. FAsmSymbolDict.free;
  378. {$ifdef MEMDEBUG}
  379. memasmsymbols.stop;
  380. {$endif}
  381. { CFI }
  382. {$ifdef MEMDEBUG}
  383. memasmcfi.start;
  384. {$endif}
  385. FAsmCFI.free;
  386. {$ifdef MEMDEBUG}
  387. memasmcfi.stop;
  388. {$endif}
  389. { Lists }
  390. {$ifdef MEMDEBUG}
  391. memasmlists.start;
  392. {$endif}
  393. ResStrInits.free;
  394. WideInits.free;
  395. for hal:=low(TAsmListType) to high(TAsmListType) do
  396. AsmLists[hal].free;
  397. CurrAsmList.free;
  398. {$ifdef MEMDEBUG}
  399. memasmlists.stop;
  400. {$endif}
  401. for hp := low(TConstPoolType) to high(TConstPoolType) do
  402. FConstPools[hp].Free;
  403. end;
  404. function TAsmData.DefineAsmSymbolByClass(symclass: TAsmSymbolClass; const s: TSymStr; _bind: TAsmSymBind; _typ: Tasmsymtype; def: tdef): TAsmSymbol;
  405. var
  406. wasdefined: boolean;
  407. begin
  408. result:=DefineAsmSymbolByClassBase(symclass,s,_bind,_typ,def,wasdefined);
  409. end;
  410. function TAsmData.DefineAsmSymbol(const s: TSymStr; _bind: TAsmSymBind; _typ: Tasmsymtype; def: tdef): TAsmSymbol;
  411. begin
  412. result:=DefineAsmSymbolByClass(TAsmSymbol,s,_bind,_typ,def);
  413. end;
  414. function TAsmData.RefAsmSymbol(const s : TSymStr;_typ:Tasmsymtype;indirect:boolean) : TAsmSymbol;
  415. var
  416. namestr : TSymStr;
  417. bind : tasmsymbind;
  418. begin
  419. namestr:=s;
  420. if indirect then
  421. begin
  422. namestr:=namestr+suffix_indirect;
  423. bind:=AB_EXTERNAL_INDIRECT;
  424. end
  425. else
  426. begin
  427. bind:=AB_EXTERNAL;
  428. end;
  429. result:=TAsmSymbol(FAsmSymbolDict.Find(namestr));
  430. if not assigned(result) then
  431. result:=TAsmSymbol.create(AsmSymbolDict,namestr,bind,_typ)
  432. { one normal reference removes the "weak" character of a symbol }
  433. else if (result.bind=AB_WEAK_EXTERNAL) then
  434. result.bind:=bind;
  435. end;
  436. function TAsmData.WeakRefAsmSymbol(const s : TSymStr;_typ:Tasmsymtype) : TAsmSymbol;
  437. begin
  438. result:=TAsmSymbol(FAsmSymbolDict.Find(s));
  439. if not assigned(result) then
  440. result:=TAsmSymbol.create(AsmSymbolDict,s,AB_WEAK_EXTERNAL,_typ);
  441. end;
  442. function TAsmData.GetAsmSymbol(const s : TSymStr) : TAsmSymbol;
  443. begin
  444. result:=TAsmSymbol(FAsmSymbolDict.Find(s));
  445. end;
  446. procedure TAsmData.GenerateAltSymbol(p:TAsmSymbol);
  447. begin
  448. if not assigned(p.altsymbol) then
  449. begin
  450. p.altsymbol:=p.getaltcopy(AsmSymbolDict,FNextAltNr);
  451. FAltSymbolList.Add(p);
  452. end;
  453. end;
  454. procedure TAsmData.ResetAltSymbols;
  455. var
  456. i : longint;
  457. begin
  458. for i:=0 to FAltSymbolList.Count-1 do
  459. TAsmSymbol(FAltSymbolList[i]).altsymbol:=nil;
  460. FAltSymbolList.Clear;
  461. end;
  462. procedure TAsmData.getlabel(out l : TAsmLabel;alt:TAsmLabeltype);
  463. begin
  464. if (target_info.system in (systems_linux + systems_bsd + systems_android)) and
  465. { the next condition was
  466. (cs_create_smart in current_settings.moduleswitches) and
  467. but if we create_smartlink_sections, this is useless }
  468. (create_smartlink_library) and
  469. (alt = alt_dbgline) then
  470. l:=TAsmLabel.createglobal(AsmSymbolDict,name^,FNextLabelNr[alt],alt)
  471. else
  472. l:=TAsmLabel.createlocal(AsmSymbolDict,FNextLabelNr[alt],alt);
  473. inc(FNextLabelNr[alt]);
  474. end;
  475. procedure TAsmData.getjumplabel(out l : TAsmLabel);
  476. begin
  477. l:=TAsmLabel.createlocal(AsmSymbolDict,FNextLabelNr[alt_jump],alt_jump);
  478. inc(FNextLabelNr[alt_jump]);
  479. end;
  480. procedure TAsmData.getglobaljumplabel(out l : TAsmLabel);
  481. begin
  482. l:=TAsmLabel.createglobal(AsmSymbolDict,name^,FNextLabelNr[alt_jump],alt_jump);
  483. inc(FNextLabelNr[alt_jump]);
  484. end;
  485. procedure TAsmData.getglobaldatalabel(out l : TAsmLabel);
  486. begin
  487. l:=TAsmLabel.createglobal(AsmSymbolDict,name^,FNextLabelNr[alt_data],alt_data);
  488. inc(FNextLabelNr[alt_data]);
  489. end;
  490. procedure TAsmData.getstaticdatalabel(out l : TAsmLabel);
  491. begin
  492. l:=TAsmLabel.createstatic(AsmSymbolDict,FNextLabelNr[alt_data],alt_data);
  493. inc(FNextLabelNr[alt_data]);
  494. end;
  495. procedure TAsmData.getlocaldatalabel(out l: TAsmLabel);
  496. begin
  497. l:=TAsmLabel.createlocal(AsmSymbolDict,FNextLabelNr[alt_data],alt_data);
  498. inc(FNextLabelNr[alt_data]);
  499. end;
  500. procedure TAsmData.getaddrlabel(out l : TAsmLabel);
  501. begin
  502. l:=TAsmLabel.createlocal(AsmSymbolDict,FNextLabelNr[alt_addr],alt_addr);
  503. inc(FNextLabelNr[alt_addr]);
  504. end;
  505. initialization
  506. {$ifdef MEMDEBUG}
  507. memasmsymbols:=TMemDebug.create('AsmSymbols');
  508. memasmsymbols.stop;
  509. memasmcfi:=TMemDebug.create('AsmCFI');
  510. memasmcfi.stop;
  511. memasmlists:=TMemDebug.create('AsmLists');
  512. memasmlists.stop;
  513. {$endif MEMDEBUG}
  514. CAsmCFI:=TAsmCFI;
  515. finalization
  516. {$ifdef MEMDEBUG}
  517. memasmsymbols.free;
  518. memasmcfi.free;
  519. memasmlists.free;
  520. {$endif MEMDEBUG}
  521. end.