aasmdata.pas 19 KB

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