aasmdata.pas 19 KB

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