aasmdata.pas 18 KB

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