aasmdata.pas 19 KB

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