aasmdata.pas 17 KB

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