aasmdata.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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,globals,systems,
  28. cpuinfo,cpubase,
  29. cgbase,cgutils,
  30. symtype,
  31. aasmbase,ogbase;
  32. type
  33. { Type of AsmLists. The order is important for the layout of the
  34. information in the .o file. The stabs for the types must be defined
  35. before they can be referenced and therefor they need to be written
  36. first (PFV) }
  37. TAsmListType=(
  38. al_start,
  39. al_stabs,
  40. al_procedures,
  41. al_globals,
  42. al_const,
  43. al_typedconsts,
  44. al_rotypedconsts,
  45. al_threadvars,
  46. al_imports,
  47. al_exports,
  48. al_resources,
  49. al_rtti,
  50. al_dwarf_frame,
  51. al_dwarf_info,
  52. al_dwarf_abbrev,
  53. al_dwarf_line,
  54. al_picdata,
  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. );
  82. const
  83. AsmListTypeStr : array[TAsmListType] of string[24] =(
  84. 'al_begin',
  85. 'al_stabs',
  86. 'al_procedures',
  87. 'al_globals',
  88. 'al_const',
  89. 'al_typedconsts',
  90. 'al_rotypedconsts',
  91. 'al_threadvars',
  92. 'al_imports',
  93. 'al_exports',
  94. 'al_resources',
  95. 'al_rtti',
  96. 'al_dwarf_frame',
  97. 'al_dwarf_info',
  98. 'al_dwarf_abbrev',
  99. 'al_dwarf_line',
  100. 'al_picdata',
  101. 'al_resourcestrings',
  102. 'al_objc_data',
  103. 'al_objc_pools',
  104. 'al_end'
  105. );
  106. type
  107. TAsmList = class(tlinkedlist)
  108. constructor create;
  109. function empty : boolean;
  110. function getlasttaifilepos : pfileposinfo;
  111. end;
  112. TAsmCFI=class
  113. public
  114. constructor create;virtual;
  115. destructor destroy;override;
  116. procedure generate_code(list:TAsmList);virtual;
  117. procedure start_frame(list:TAsmList);virtual;
  118. procedure end_frame(list:TAsmList);virtual;
  119. procedure cfa_offset(list:TAsmList;reg:tregister;ofs:longint);virtual;
  120. procedure cfa_restore(list:TAsmList;reg:tregister);virtual;
  121. procedure cfa_def_cfa_register(list:TAsmList;reg:tregister);virtual;
  122. procedure cfa_def_cfa_offset(list:TAsmList;ofs:longint);virtual;
  123. end;
  124. TAsmCFIClass=class of TAsmCFI;
  125. TAsmData = class
  126. private
  127. { Symbols }
  128. FAsmSymbolDict : TFPHashObjectList;
  129. FAltSymbolList : TFPObjectList;
  130. FNextAltNr : longint;
  131. FNextLabelNr : array[TAsmLabeltype] of longint;
  132. { Call Frame Information for stack unwinding}
  133. FAsmCFI : TAsmCFI;
  134. public
  135. name,
  136. realname : string[80];
  137. NextVTEntryNr : longint;
  138. { Assembler lists }
  139. AsmLists : array[TAsmListType] of TAsmList;
  140. CurrAsmList : TAsmList;
  141. WideInits : TLinkedList;
  142. { hash tables for reusing constant storage }
  143. ConstPools : array[TConstPoolType] of THashSet;
  144. constructor create(const n:string);
  145. destructor destroy;override;
  146. { asmsymbol }
  147. function DefineAsmSymbol(const s : string;_bind:TAsmSymBind;_typ:Tasmsymtype) : TAsmSymbol;
  148. function WeakRefAsmSymbol(const s : string) : TAsmSymbol;
  149. function RefAsmSymbol(const s : string) : TAsmSymbol;
  150. function GetAsmSymbol(const s : string) : TAsmSymbol;
  151. { create new assembler label }
  152. procedure getlabel(out l : TAsmLabel;alt:TAsmLabeltype);
  153. procedure getjumplabel(out l : TAsmLabel);
  154. procedure getglobaljumplabel(out l : TAsmLabel);
  155. procedure getaddrlabel(out l : TAsmLabel);
  156. procedure getdatalabel(out l : TAsmLabel);
  157. { generate an alternative (duplicate) symbol }
  158. procedure GenerateAltSymbol(p:TAsmSymbol);
  159. procedure ResetAltSymbols;
  160. property AsmSymbolDict:TFPHashObjectList read FAsmSymbolDict;
  161. property AsmCFI:TAsmCFI read FAsmCFI;
  162. end;
  163. TTCInitItem = class(TLinkedListItem)
  164. sym: tsym;
  165. offset: aint;
  166. datalabel: TAsmLabel;
  167. constructor Create(asym: tsym; aoffset: aint; alabel: TAsmLabel);
  168. end;
  169. var
  170. CAsmCFI : TAsmCFIClass;
  171. current_asmdata : TAsmData;
  172. implementation
  173. uses
  174. verbose,
  175. aasmtai;
  176. {$ifdef MEMDEBUG}
  177. var
  178. memasmsymbols,
  179. memasmcfi,
  180. memasmlists : TMemDebug;
  181. {$endif MEMDEBUG}
  182. {*****************************************************************************
  183. TAsmCFI
  184. *****************************************************************************}
  185. constructor TAsmCFI.create;
  186. begin
  187. end;
  188. destructor TAsmCFI.destroy;
  189. begin
  190. end;
  191. procedure TAsmCFI.generate_code(list:TAsmList);
  192. begin
  193. end;
  194. procedure TAsmCFI.start_frame(list:TAsmList);
  195. begin
  196. end;
  197. procedure TAsmCFI.end_frame(list:TAsmList);
  198. begin
  199. end;
  200. procedure TAsmCFI.cfa_offset(list:TAsmList;reg:tregister;ofs:longint);
  201. begin
  202. end;
  203. procedure TAsmCFI.cfa_restore(list:TAsmList;reg:tregister);
  204. begin
  205. end;
  206. procedure TAsmCFI.cfa_def_cfa_register(list:TAsmList;reg:tregister);
  207. begin
  208. end;
  209. procedure TAsmCFI.cfa_def_cfa_offset(list:TAsmList;ofs:longint);
  210. begin
  211. end;
  212. {*****************************************************************************
  213. TTCInitItem
  214. *****************************************************************************}
  215. constructor TTCInitItem.Create(asym: tsym; aoffset: aint; alabel: TAsmLabel);
  216. begin
  217. inherited Create;
  218. sym:=asym;
  219. offset:=aoffset;
  220. datalabel:=alabel;
  221. end;
  222. {*****************************************************************************
  223. TAsmList
  224. *****************************************************************************}
  225. constructor TAsmList.create;
  226. begin
  227. inherited create;
  228. { make sure the optimizer won't remove the first tai of this list}
  229. insert(tai_marker.create(mark_BlockStart));
  230. end;
  231. function TAsmList.empty : boolean;
  232. begin
  233. { there is always a mark_BlockStart available,
  234. see TAsmList.create }
  235. result:=(count<=1);
  236. end;
  237. function TAsmList.getlasttaifilepos : pfileposinfo;
  238. var
  239. hp : tlinkedlistitem;
  240. begin
  241. getlasttaifilepos := nil;
  242. if assigned(last) then
  243. begin
  244. { find the last file information record }
  245. if not (tai(last).typ in SkipLineInfo) then
  246. getlasttaifilepos:=@tailineinfo(last).fileinfo
  247. else
  248. { go through list backwards to find the first entry
  249. with line information
  250. }
  251. begin
  252. hp:=tai(last);
  253. while assigned(hp) and (tai(hp).typ in SkipLineInfo) do
  254. hp:=hp.Previous;
  255. { found entry }
  256. if assigned(hp) then
  257. getlasttaifilepos:=@tailineinfo(hp).fileinfo
  258. end;
  259. end;
  260. end;
  261. {****************************************************************************
  262. TAsmData
  263. ****************************************************************************}
  264. constructor TAsmData.create(const n:string);
  265. var
  266. alt : TAsmLabelType;
  267. hal : TAsmListType;
  268. begin
  269. inherited create;
  270. realname:=n;
  271. name:=upper(n);
  272. { symbols }
  273. FAsmSymbolDict:=TFPHashObjectList.create(true);
  274. FAltSymbolList:=TFPObjectList.Create(false);
  275. { labels }
  276. FNextAltNr:=1;
  277. for alt:=low(TAsmLabelType) to high(TAsmLabelType) do
  278. FNextLabelNr[alt]:=1;
  279. { AsmLists }
  280. CurrAsmList:=TAsmList.create;
  281. for hal:=low(TAsmListType) to high(TAsmListType) do
  282. AsmLists[hal]:=TAsmList.create;
  283. WideInits :=TLinkedList.create;
  284. { CFI }
  285. FAsmCFI:=CAsmCFI.Create;
  286. end;
  287. destructor TAsmData.destroy;
  288. var
  289. hal : TAsmListType;
  290. hp : TConstPoolType;
  291. begin
  292. { Symbols }
  293. {$ifdef MEMDEBUG}
  294. memasmsymbols.start;
  295. {$endif}
  296. FAltSymbolList.free;
  297. FAsmSymbolDict.free;
  298. {$ifdef MEMDEBUG}
  299. memasmsymbols.stop;
  300. {$endif}
  301. { CFI }
  302. {$ifdef MEMDEBUG}
  303. memasmcfi.start;
  304. {$endif}
  305. FAsmCFI.free;
  306. {$ifdef MEMDEBUG}
  307. memasmcfi.stop;
  308. {$endif}
  309. { Lists }
  310. {$ifdef MEMDEBUG}
  311. memasmlists.start;
  312. {$endif}
  313. WideInits.free;
  314. for hal:=low(TAsmListType) to high(TAsmListType) do
  315. AsmLists[hal].free;
  316. CurrAsmList.free;
  317. {$ifdef MEMDEBUG}
  318. memasmlists.stop;
  319. {$endif}
  320. for hp := low(TConstPoolType) to high(TConstPoolType) do
  321. ConstPools[hp].Free;
  322. end;
  323. function TAsmData.DefineAsmSymbol(const s : string;_bind:TAsmSymBind;_typ:Tasmsymtype) : TAsmSymbol;
  324. var
  325. hp : TAsmSymbol;
  326. begin
  327. hp:=TAsmSymbol(FAsmSymbolDict.Find(s));
  328. if assigned(hp) then
  329. begin
  330. { Redefine is allowed, but the types must be the same. The redefine
  331. is needed for Darwin where the labels are first allocated }
  332. if (hp.bind<>AB_EXTERNAL) then
  333. begin
  334. if (hp.bind<>_bind) and
  335. (hp.typ<>_typ) then
  336. internalerror(200603261);
  337. end;
  338. hp.typ:=_typ;
  339. hp.bind:=_bind;
  340. end
  341. else
  342. begin
  343. { Not found, insert it. }
  344. hp:=TAsmSymbol.create(AsmSymbolDict,s,_bind,_typ);
  345. end;
  346. result:=hp;
  347. end;
  348. function TAsmData.RefAsmSymbol(const s : string) : TAsmSymbol;
  349. begin
  350. result:=TAsmSymbol(FAsmSymbolDict.Find(s));
  351. if not assigned(result) then
  352. result:=TAsmSymbol.create(AsmSymbolDict,s,AB_EXTERNAL,AT_NONE)
  353. { one normal reference removes the "weak" character of a symbol }
  354. else if (result.bind=AB_WEAK_EXTERNAL) then
  355. result.bind:=AB_EXTERNAL;
  356. end;
  357. function TAsmData.WeakRefAsmSymbol(const s : string) : TAsmSymbol;
  358. begin
  359. result:=TAsmSymbol(FAsmSymbolDict.Find(s));
  360. if not assigned(result) then
  361. result:=TAsmSymbol.create(AsmSymbolDict,s,AB_WEAK_EXTERNAL,AT_NONE);
  362. end;
  363. function TAsmData.GetAsmSymbol(const s : string) : TAsmSymbol;
  364. begin
  365. result:=TAsmSymbol(FAsmSymbolDict.Find(s));
  366. end;
  367. procedure TAsmData.GenerateAltSymbol(p:TAsmSymbol);
  368. begin
  369. if not assigned(p.altsymbol) then
  370. begin
  371. p.altsymbol:=p.getaltcopy(AsmSymbolDict,FNextAltNr);
  372. FAltSymbolList.Add(p);
  373. end;
  374. end;
  375. procedure TAsmData.ResetAltSymbols;
  376. var
  377. i : longint;
  378. begin
  379. for i:=0 to FAltSymbolList.Count-1 do
  380. TAsmSymbol(FAltSymbolList[i]).altsymbol:=nil;
  381. FAltSymbolList.Clear;
  382. end;
  383. procedure TAsmData.getlabel(out l : TAsmLabel;alt:TAsmLabeltype);
  384. begin
  385. if (target_info.system in systems_linux) and
  386. (cs_link_smart in current_settings.globalswitches) and
  387. (alt = alt_dbgline) then
  388. l:=TAsmLabel.createglobal(AsmSymbolDict,name,FNextLabelNr[alt],alt)
  389. else
  390. l:=TAsmLabel.createlocal(AsmSymbolDict,FNextLabelNr[alt],alt);
  391. inc(FNextLabelNr[alt]);
  392. end;
  393. procedure TAsmData.getjumplabel(out l : TAsmLabel);
  394. begin
  395. l:=TAsmLabel.createlocal(AsmSymbolDict,FNextLabelNr[alt_jump],alt_jump);
  396. inc(FNextLabelNr[alt_jump]);
  397. end;
  398. procedure TAsmData.getglobaljumplabel(out l : TAsmLabel);
  399. begin
  400. l:=TAsmLabel.createglobal(AsmSymbolDict,name,FNextLabelNr[alt_jump],alt_jump);
  401. inc(FNextLabelNr[alt_jump]);
  402. end;
  403. procedure TAsmData.getdatalabel(out l : TAsmLabel);
  404. begin
  405. l:=TAsmLabel.createglobal(AsmSymbolDict,name,FNextLabelNr[alt_data],alt_data);
  406. inc(FNextLabelNr[alt_data]);
  407. end;
  408. procedure TAsmData.getaddrlabel(out l : TAsmLabel);
  409. begin
  410. l:=TAsmLabel.createlocal(AsmSymbolDict,FNextLabelNr[alt_addr],alt_addr);
  411. inc(FNextLabelNr[alt_addr]);
  412. end;
  413. initialization
  414. {$ifdef MEMDEBUG}
  415. memasmsymbols:=TMemDebug.create('AsmSymbols');
  416. memasmsymbols.stop;
  417. memasmcfi:=TMemDebug.create('AsmCFI');
  418. memasmcfi.stop;
  419. memasmlists:=TMemDebug.create('AsmLists');
  420. memasmlists.stop;
  421. {$endif MEMDEBUG}
  422. CAsmCFI:=TAsmCFI;
  423. finalization
  424. {$ifdef MEMDEBUG}
  425. memasmsymbols.free;
  426. memasmcfi.free;
  427. memasmlists.free;
  428. {$endif MEMDEBUG}
  429. end.