aasmdata.pas 17 KB

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