aasmdata.pas 13 KB

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