2
0

aasmdata.pas 19 KB

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