aasmdata.pas 12 KB

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