aasmdata.pas 13 KB

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