ncgrtti.pas 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Routines for the code generation of RTTI data structures
  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. unit ncgrtti;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cclasses,
  22. aasmbase,
  23. symbase,symconst,symtype,symdef;
  24. type
  25. { TRTTIWriter }
  26. TRTTIWriter=class
  27. private
  28. function fields_count(st:tsymtable;rt:trttitype):longint;
  29. procedure fields_write_rtti(st:tsymtable;rt:trttitype);
  30. procedure fields_write_rtti_data(st:tsymtable;rt:trttitype);
  31. procedure write_rtti_extrasyms(def:Tdef;rt:Trttitype;mainrtti:Tasmsymbol);
  32. procedure published_write_rtti(st:tsymtable;rt:trttitype);
  33. function published_properties_count(st:tsymtable):longint;
  34. procedure published_properties_write_rtti_data(propnamelist:TFPHashObjectList;st:tsymtable);
  35. procedure collect_propnamelist(propnamelist:TFPHashObjectList;objdef:tobjectdef);
  36. procedure write_rtti_name(def:tdef);
  37. procedure write_rtti_data(def:tdef;rt:trttitype);
  38. procedure write_child_rtti_data(def:tdef;rt:trttitype);
  39. function ref_rtti(def:tdef;rt:trttitype):tasmsymbol;
  40. public
  41. procedure write_rtti(def:tdef;rt:trttitype);
  42. function get_rtti_label(def:tdef;rt:trttitype):tasmsymbol;
  43. function get_rtti_label_ord2str(def:tdef;rt:trttitype):tasmsymbol;
  44. function get_rtti_label_str2ord(def:tdef;rt:trttitype):tasmsymbol;
  45. end;
  46. var
  47. RTTIWriter : TRTTIWriter;
  48. implementation
  49. uses
  50. cutils,
  51. globals,globtype,verbose,systems,
  52. fmodule,
  53. symsym,
  54. aasmtai,aasmdata
  55. ;
  56. const
  57. rttidefstate : array[trttitype] of tdefstate = (ds_rtti_table_written,ds_init_table_written);
  58. type
  59. TPropNameListItem = class(TFPHashObject)
  60. propindex : longint;
  61. propowner : TSymtable;
  62. end;
  63. {***************************************************************************
  64. TRTTIWriter
  65. ***************************************************************************}
  66. procedure TRTTIWriter.write_rtti_name(def:tdef);
  67. var
  68. hs : string;
  69. begin
  70. { name }
  71. if assigned(def.typesym) then
  72. begin
  73. hs:=ttypesym(def.typesym).realname;
  74. current_asmdata.asmlists[al_rtti].concat(Tai_string.Create(chr(length(hs))+hs));
  75. end
  76. else
  77. current_asmdata.asmlists[al_rtti].concat(Tai_string.Create(#0));
  78. end;
  79. function TRTTIWriter.fields_count(st:tsymtable;rt:trttitype):longint;
  80. var
  81. i : longint;
  82. sym : tsym;
  83. begin
  84. result:=0;
  85. for i:=0 to st.SymList.Count-1 do
  86. begin
  87. sym:=tsym(st.SymList[i]);
  88. if (rt=fullrtti) or
  89. (
  90. (tsym(sym).typ=fieldvarsym) and
  91. tfieldvarsym(sym).vardef.needs_inittable
  92. ) then
  93. inc(result);
  94. end;
  95. end;
  96. procedure TRTTIWriter.fields_write_rtti_data(st:tsymtable;rt:trttitype);
  97. var
  98. i : longint;
  99. sym : tsym;
  100. begin
  101. for i:=0 to st.SymList.Count-1 do
  102. begin
  103. sym:=tsym(st.SymList[i]);
  104. if (rt=fullrtti) or
  105. (
  106. (tsym(sym).typ=fieldvarsym) and
  107. tfieldvarsym(sym).vardef.needs_inittable
  108. ) then
  109. begin
  110. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_sym(ref_rtti(tfieldvarsym(sym).vardef,rt)));
  111. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(tfieldvarsym(sym).fieldoffset));
  112. end;
  113. end;
  114. end;
  115. procedure TRTTIWriter.fields_write_rtti(st:tsymtable;rt:trttitype);
  116. var
  117. i : longint;
  118. sym : tsym;
  119. begin
  120. for i:=0 to st.SymList.Count-1 do
  121. begin
  122. sym:=tsym(st.SymList[i]);
  123. if (rt=fullrtti) or
  124. (
  125. (tsym(sym).typ=fieldvarsym) and
  126. tfieldvarsym(sym).vardef.needs_inittable
  127. ) then
  128. write_rtti(tfieldvarsym(sym).vardef,rt);
  129. end;
  130. end;
  131. procedure TRTTIWriter.published_write_rtti(st:tsymtable;rt:trttitype);
  132. var
  133. i : longint;
  134. sym : tsym;
  135. begin
  136. for i:=0 to st.SymList.Count-1 do
  137. begin
  138. sym:=tsym(st.SymList[i]);
  139. if (sp_published in tsym(sym).symoptions) then
  140. begin
  141. case tsym(sym).typ of
  142. propertysym:
  143. write_rtti(tpropertysym(sym).propdef,rt);
  144. fieldvarsym:
  145. write_rtti(tfieldvarsym(sym).vardef,rt);
  146. end;
  147. end;
  148. end;
  149. end;
  150. function TRTTIWriter.published_properties_count(st:tsymtable):longint;
  151. var
  152. i : longint;
  153. sym : tsym;
  154. begin
  155. result:=0;
  156. for i:=0 to st.SymList.Count-1 do
  157. begin
  158. sym:=tsym(st.SymList[i]);
  159. if (tsym(sym).typ=propertysym) and
  160. (sp_published in tsym(sym).symoptions) then
  161. inc(result);
  162. end;
  163. end;
  164. procedure TRTTIWriter.collect_propnamelist(propnamelist:TFPHashObjectList;objdef:tobjectdef);
  165. var
  166. i : longint;
  167. sym : tsym;
  168. pn : tpropnamelistitem;
  169. begin
  170. if assigned(objdef.childof) then
  171. collect_propnamelist(propnamelist,objdef.childof);
  172. for i:=0 to objdef.symtable.SymList.Count-1 do
  173. begin
  174. sym:=tsym(objdef.symtable.SymList[i]);
  175. if (tsym(sym).typ=propertysym) and
  176. (sp_published in tsym(sym).symoptions) then
  177. begin
  178. pn:=TPropNameListItem(propnamelist.Find(tsym(sym).name));
  179. if not assigned(pn) then
  180. begin
  181. pn:=tpropnamelistitem.create(propnamelist,tsym(sym).name);
  182. pn.propindex:=propnamelist.count-1;
  183. pn.propowner:=tsym(sym).owner;
  184. end;
  185. end;
  186. end;
  187. end;
  188. procedure TRTTIWriter.published_properties_write_rtti_data(propnamelist:TFPHashObjectList;st:tsymtable);
  189. var
  190. i : longint;
  191. sym : tsym;
  192. proctypesinfo : byte;
  193. propnameitem : tpropnamelistitem;
  194. procedure writeaccessproc(pap:tpropaccesslisttypes; shiftvalue : byte; unsetvalue: byte);
  195. var
  196. typvalue : byte;
  197. hp : ppropaccesslistitem;
  198. address : longint;
  199. def : tdef;
  200. hpropsym : tpropertysym;
  201. propaccesslist : tpropaccesslist;
  202. begin
  203. hpropsym:=tpropertysym(sym);
  204. repeat
  205. propaccesslist:=hpropsym.propaccesslist[pap];
  206. if not propaccesslist.empty then
  207. break;
  208. hpropsym:=hpropsym.overridenpropsym;
  209. until not assigned(hpropsym);
  210. if not(assigned(propaccesslist) and assigned(propaccesslist.firstsym)) then
  211. begin
  212. current_asmdata.asmlists[al_rtti].concat(Tai_const.create(aitconst_ptr,unsetvalue));
  213. typvalue:=3;
  214. end
  215. else if propaccesslist.firstsym^.sym.typ=fieldvarsym then
  216. begin
  217. address:=0;
  218. hp:=propaccesslist.firstsym;
  219. def:=nil;
  220. while assigned(hp) do
  221. begin
  222. case hp^.sltype of
  223. sl_load :
  224. begin
  225. def:=tfieldvarsym(hp^.sym).vardef;
  226. inc(address,tfieldvarsym(hp^.sym).fieldoffset);
  227. end;
  228. sl_subscript :
  229. begin
  230. if not(assigned(def) and (def.typ=recorddef)) then
  231. internalerror(200402171);
  232. inc(address,tfieldvarsym(hp^.sym).fieldoffset);
  233. def:=tfieldvarsym(hp^.sym).vardef;
  234. end;
  235. sl_vec :
  236. begin
  237. if not(assigned(def) and (def.typ=arraydef)) then
  238. internalerror(200402172);
  239. def:=tarraydef(def).elementdef;
  240. inc(address,def.size*hp^.value);
  241. end;
  242. end;
  243. hp:=hp^.next;
  244. end;
  245. current_asmdata.asmlists[al_rtti].concat(Tai_const.create(aitconst_ptr,address));
  246. typvalue:=0;
  247. end
  248. else
  249. begin
  250. { When there was an error then procdef is not assigned }
  251. if not assigned(propaccesslist.procdef) then
  252. exit;
  253. if not(po_virtualmethod in tprocdef(propaccesslist.procdef).procoptions) then
  254. begin
  255. current_asmdata.asmlists[al_rtti].concat(Tai_const.createname(tprocdef(propaccesslist.procdef).mangledname,0));
  256. typvalue:=1;
  257. end
  258. else
  259. begin
  260. { virtual method, write vmt offset }
  261. current_asmdata.asmlists[al_rtti].concat(Tai_const.create(aitconst_ptr,
  262. tprocdef(propaccesslist.procdef)._class.vmtmethodoffset(tprocdef(propaccesslist.procdef).extnumber)));
  263. typvalue:=2;
  264. end;
  265. end;
  266. proctypesinfo:=proctypesinfo or (typvalue shl shiftvalue);
  267. end;
  268. begin
  269. for i:=0 to st.SymList.Count-1 do
  270. begin
  271. sym:=tsym(st.SymList[i]);
  272. if (sym.typ=propertysym) and
  273. (sp_published in sym.symoptions) then
  274. begin
  275. if ppo_indexed in tpropertysym(sym).propoptions then
  276. proctypesinfo:=$40
  277. else
  278. proctypesinfo:=0;
  279. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_sym(ref_rtti(tpropertysym(sym).propdef,fullrtti)));
  280. writeaccessproc(palt_read,0,0);
  281. writeaccessproc(palt_write,2,0);
  282. { is it stored ? }
  283. if not(ppo_stored in tpropertysym(sym).propoptions) then
  284. begin
  285. { no, so put a constant zero }
  286. current_asmdata.asmlists[al_rtti].concat(Tai_const.create(aitconst_ptr,0));
  287. proctypesinfo:=proctypesinfo or (3 shl 4);
  288. end
  289. else
  290. writeaccessproc(palt_stored,4,1); { maybe; if no procedure put a constant 1 (=true) }
  291. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(tpropertysym(sym).index));
  292. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(tpropertysym(sym).default));
  293. propnameitem:=TPropNameListItem(propnamelist.Find(tpropertysym(sym).name));
  294. if not assigned(propnameitem) then
  295. internalerror(200512201);
  296. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_16bit(propnameitem.propindex));
  297. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(proctypesinfo));
  298. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(length(tpropertysym(sym).realname)));
  299. current_asmdata.asmlists[al_rtti].concat(Tai_string.Create(tpropertysym(sym).realname));
  300. if (tf_requires_proper_alignment in target_info.flags) then
  301. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  302. end;
  303. end;
  304. end;
  305. procedure TRTTIWriter.write_rtti_data(def:tdef;rt:trttitype);
  306. procedure unknown_rtti(def:tstoreddef);
  307. begin
  308. current_asmdata.asmlists[al_rtti].concat(tai_const.create_8bit(tkUnknown));
  309. write_rtti_name(def);
  310. end;
  311. procedure variantdef_rtti(def:tvariantdef);
  312. begin
  313. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkVariant));
  314. end;
  315. procedure stringdef_rtti(def:tstringdef);
  316. begin
  317. case def.stringtype of
  318. st_ansistring:
  319. begin
  320. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkAString));
  321. write_rtti_name(def);
  322. end;
  323. st_widestring:
  324. begin
  325. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkWString));
  326. write_rtti_name(def);
  327. end;
  328. st_longstring:
  329. begin
  330. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkLString));
  331. write_rtti_name(def);
  332. end;
  333. st_shortstring:
  334. begin
  335. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkSString));
  336. write_rtti_name(def);
  337. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(def.len));
  338. if (tf_requires_proper_alignment in target_info.flags) then
  339. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  340. end;
  341. end;
  342. end;
  343. procedure enumdef_rtti(def:tenumdef);
  344. var
  345. hp : tenumsym;
  346. begin
  347. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkEnumeration));
  348. write_rtti_name(def);
  349. if (tf_requires_proper_alignment in target_info.flags) then
  350. current_asmdata.asmlists[al_rtti].concat(Cai_align.Create(sizeof(TConstPtrUInt)));
  351. case longint(def.size) of
  352. 1 :
  353. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(otUByte));
  354. 2 :
  355. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(otUWord));
  356. 4 :
  357. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(otULong));
  358. end;
  359. if (tf_requires_proper_alignment in target_info.flags) then
  360. current_asmdata.asmlists[al_rtti].concat(Cai_align.Create(sizeof(TConstPtrUInt)));
  361. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(def.min));
  362. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(def.max));
  363. if assigned(def.basedef) then
  364. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_sym(ref_rtti(def.basedef,rt)))
  365. else
  366. current_asmdata.asmlists[al_rtti].concat(Tai_const.create_sym(nil));
  367. hp:=tenumsym(def.firstenum);
  368. while assigned(hp) do
  369. begin
  370. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(length(hp.realname)));
  371. current_asmdata.asmlists[al_rtti].concat(Tai_string.Create(hp.realname));
  372. hp:=hp.nextenum;
  373. end;
  374. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(0));
  375. end;
  376. procedure orddef_rtti(def:torddef);
  377. procedure dointeger;
  378. const
  379. trans : array[tordtype] of byte =
  380. (otUByte{otNone},
  381. otUByte,otUWord,otULong,otUByte{otNone},
  382. otSByte,otSWord,otSLong,otUByte{otNone},
  383. otUByte,otUWord,otULong,otUByte,
  384. otUByte,otUWord,otUByte);
  385. begin
  386. write_rtti_name(def);
  387. if (tf_requires_proper_alignment in target_info.flags) then
  388. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  389. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(byte(trans[def.ordtype])));
  390. if (tf_requires_proper_alignment in target_info.flags) then
  391. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  392. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(longint(def.low)));
  393. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(longint(def.high)));
  394. end;
  395. begin
  396. case def.ordtype of
  397. s64bit :
  398. begin
  399. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkInt64));
  400. write_rtti_name(def);
  401. if (tf_requires_proper_alignment in target_info.flags) then
  402. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  403. { low }
  404. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_64bit(int64($80000000) shl 32));
  405. { high }
  406. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_64bit((int64($7fffffff) shl 32) or int64($ffffffff)));
  407. end;
  408. u64bit :
  409. begin
  410. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkQWord));
  411. write_rtti_name(def);
  412. if (tf_requires_proper_alignment in target_info.flags) then
  413. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  414. { low }
  415. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_64bit(0));
  416. { high }
  417. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_64bit(int64((int64($ffffffff) shl 32) or int64($ffffffff))));
  418. end;
  419. bool8bit:
  420. begin
  421. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkBool));
  422. dointeger;
  423. end;
  424. uchar:
  425. begin
  426. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkChar));
  427. dointeger;
  428. end;
  429. uwidechar:
  430. begin
  431. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkWChar));
  432. dointeger;
  433. end;
  434. else
  435. begin
  436. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkInteger));
  437. dointeger;
  438. end;
  439. end;
  440. end;
  441. procedure floatdef_rtti(def:tfloatdef);
  442. const
  443. {tfloattype = (s32real,s64real,s80real,s64bit,s128bit);}
  444. translate : array[tfloattype] of byte =
  445. (ftSingle,ftDouble,ftExtended,ftComp,ftCurr,ftFloat128);
  446. begin
  447. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkFloat));
  448. write_rtti_name(def);
  449. if (tf_requires_proper_alignment in target_info.flags) then
  450. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  451. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(translate[def.floattype]));
  452. end;
  453. procedure setdef_rtti(def:tsetdef);
  454. begin
  455. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkSet));
  456. write_rtti_name(def);
  457. if (tf_requires_proper_alignment in target_info.flags) then
  458. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  459. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(otULong));
  460. if (tf_requires_proper_alignment in target_info.flags) then
  461. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  462. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_sym(ref_rtti(def.elementdef,rt)));
  463. end;
  464. procedure arraydef_rtti(def:tarraydef);
  465. begin
  466. if ado_IsDynamicArray in def.arrayoptions then
  467. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkdynarray))
  468. else
  469. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkarray));
  470. write_rtti_name(def);
  471. if (tf_requires_proper_alignment in target_info.flags) then
  472. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  473. { size of elements }
  474. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_aint(def.elesize));
  475. if not(ado_IsDynamicArray in def.arrayoptions) then
  476. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_aint(def.elecount));
  477. { element type }
  478. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_sym(ref_rtti(def.elementdef,rt)));
  479. { variant type }
  480. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(tstoreddef(def.elementdef).getvardef));
  481. end;
  482. procedure recorddef_rtti(def:trecorddef);
  483. var
  484. fieldcnt : longint;
  485. begin
  486. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkrecord));
  487. write_rtti_name(def);
  488. if (tf_requires_proper_alignment in target_info.flags) then
  489. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  490. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(def.size));
  491. fieldcnt:=fields_count(def.symtable,rt);
  492. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(fieldcnt));
  493. fields_write_rtti_data(def.symtable,rt);
  494. end;
  495. procedure procvardef_rtti(def:tprocvardef);
  496. procedure write_para(parasym:tparavarsym);
  497. var
  498. paraspec : byte;
  499. begin
  500. { only store user visible parameters }
  501. if not(vo_is_hidden_para in parasym.varoptions) then
  502. begin
  503. case parasym.varspez of
  504. vs_value: paraspec := 0;
  505. vs_const: paraspec := pfConst;
  506. vs_var : paraspec := pfVar;
  507. vs_out : paraspec := pfOut;
  508. end;
  509. { write flags for current parameter }
  510. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(paraspec));
  511. { write name of current parameter }
  512. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(length(parasym.realname)));
  513. current_asmdata.asmlists[al_rtti].concat(Tai_string.Create(parasym.realname));
  514. { write name of type of current parameter }
  515. write_rtti_name(parasym.vardef);
  516. end;
  517. end;
  518. var
  519. methodkind : byte;
  520. i : integer;
  521. begin
  522. if po_methodpointer in def.procoptions then
  523. begin
  524. { write method id and name }
  525. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkmethod));
  526. write_rtti_name(def);
  527. if (tf_requires_proper_alignment in target_info.flags) then
  528. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  529. { write kind of method (can only be function or procedure)}
  530. if def.returndef = voidtype then
  531. methodkind := mkProcedure
  532. else
  533. methodkind := mkFunction;
  534. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(methodkind));
  535. { write parameter info. The parameters must be written in reverse order
  536. if this method uses right to left parameter pushing! }
  537. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(def.maxparacount));
  538. if def.proccalloption in pushleftright_pocalls then
  539. begin
  540. for i:=0 to def.paras.count-1 do
  541. write_para(tparavarsym(def.paras[i]));
  542. end
  543. else
  544. begin
  545. for i:=def.paras.count-1 downto 0 do
  546. write_para(tparavarsym(def.paras[i]));
  547. end;
  548. { write name of result type }
  549. write_rtti_name(def.returndef);
  550. end
  551. else
  552. begin
  553. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkprocvar));
  554. write_rtti_name(def);
  555. end;
  556. end;
  557. procedure objectdef_rtti(def:tobjectdef);
  558. procedure objectdef_rtti_class_init(def:tobjectdef);
  559. begin
  560. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(def.size));
  561. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(fields_count(def.symtable,rt)));
  562. fields_write_rtti_data(def.symtable,rt);
  563. end;
  564. procedure objectdef_rtti_interface_init(def:tobjectdef);
  565. begin
  566. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(def.size));
  567. end;
  568. procedure objectdef_rtti_class_full(def:tobjectdef);
  569. var
  570. propnamelist : TFPHashObjectList;
  571. begin
  572. { Collect unique property names with nameindex }
  573. propnamelist:=TFPHashObjectList.Create;
  574. collect_propnamelist(propnamelist,def);
  575. if (oo_has_vmt in def.objectoptions) then
  576. current_asmdata.asmlists[al_rtti].concat(Tai_const.Createname(def.vmt_mangledname,0))
  577. else
  578. current_asmdata.asmlists[al_rtti].concat(Tai_const.create_sym(nil));
  579. { write parent typeinfo }
  580. if assigned(def.childof) and
  581. (oo_can_have_published in def.childof.objectoptions) then
  582. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_sym(ref_rtti(def.childof,fullrtti)))
  583. else
  584. current_asmdata.asmlists[al_rtti].concat(Tai_const.create_sym(nil));
  585. { total number of unique properties }
  586. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_16bit(propnamelist.count));
  587. { write unit name }
  588. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(length(current_module.realmodulename^)));
  589. current_asmdata.asmlists[al_rtti].concat(Tai_string.Create(current_module.realmodulename^));
  590. if (tf_requires_proper_alignment in target_info.flags) then
  591. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  592. { write published properties for this object }
  593. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_16bit(published_properties_count(def.symtable)));
  594. if (tf_requires_proper_alignment in target_info.flags) then
  595. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  596. published_properties_write_rtti_data(propnamelist,def.symtable);
  597. propnamelist.free;
  598. end;
  599. procedure objectdef_rtti_interface_full(def:tobjectdef);
  600. var
  601. i : longint;
  602. propnamelist : TFPHashObjectList;
  603. begin
  604. { Collect unique property names with nameindex }
  605. propnamelist:=TFPHashObjectList.Create;
  606. collect_propnamelist(propnamelist,def);
  607. { write parent typeinfo }
  608. if assigned(def.childof) then
  609. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_sym(ref_rtti(def.childof,fullrtti)))
  610. else
  611. current_asmdata.asmlists[al_rtti].concat(Tai_const.create_sym(nil));
  612. { interface: write flags, iid and iidstr }
  613. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(
  614. { ugly, but working }
  615. {$ifdef USE_PACKSET1}
  616. byte([
  617. {$else USE_PACKSET1}
  618. longint([
  619. {$endif USE_PACKSET1}
  620. TCompilerIntfFlag(ord(ifHasGuid)*ord(assigned(def.iidguid))),
  621. TCompilerIntfFlag(ord(ifHasStrGUID)*ord(assigned(def.iidstr)))
  622. ])
  623. {
  624. ifDispInterface,
  625. ifDispatch, }
  626. ));
  627. if (tf_requires_proper_alignment in target_info.flags) then
  628. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  629. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(longint(def.iidguid^.D1)));
  630. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_16bit(def.iidguid^.D2));
  631. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_16bit(def.iidguid^.D3));
  632. for i:=Low(def.iidguid^.D4) to High(def.iidguid^.D4) do
  633. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(def.iidguid^.D4[i]));
  634. { write unit name }
  635. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(length(current_module.realmodulename^)));
  636. current_asmdata.asmlists[al_rtti].concat(Tai_string.Create(current_module.realmodulename^));
  637. if (tf_requires_proper_alignment in target_info.flags) then
  638. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  639. { write iidstr }
  640. if assigned(def.iidstr) then
  641. begin
  642. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(length(def.iidstr^)));
  643. current_asmdata.asmlists[al_rtti].concat(Tai_string.Create(def.iidstr^));
  644. end
  645. else
  646. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(0));
  647. if (tf_requires_proper_alignment in target_info.flags) then
  648. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  649. { write published properties for this object }
  650. published_properties_write_rtti_data(propnamelist,def.symtable);
  651. propnamelist.free;
  652. end;
  653. begin
  654. case def.objecttype of
  655. odt_class:
  656. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkclass));
  657. odt_object:
  658. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkobject));
  659. odt_interfacecom:
  660. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkinterface));
  661. odt_interfacecorba:
  662. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkinterfaceCorba));
  663. else
  664. internalerror(200611034);
  665. end;
  666. { generate the name }
  667. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(length(def.objrealname^)));
  668. current_asmdata.asmlists[al_rtti].concat(Tai_string.Create(def.objrealname^));
  669. if (tf_requires_proper_alignment in target_info.flags) then
  670. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  671. case rt of
  672. initrtti :
  673. begin
  674. if def.objecttype in [odt_class,odt_object] then
  675. objectdef_rtti_class_init(def)
  676. else
  677. objectdef_rtti_interface_init(def);
  678. end;
  679. fullrtti :
  680. begin
  681. if def.objecttype in [odt_class,odt_object] then
  682. objectdef_rtti_class_full(def)
  683. else
  684. objectdef_rtti_interface_full(def);
  685. end;
  686. end;
  687. end;
  688. begin
  689. case def.typ of
  690. variantdef :
  691. variantdef_rtti(tvariantdef(def));
  692. stringdef :
  693. stringdef_rtti(tstringdef(def));
  694. enumdef :
  695. enumdef_rtti(tenumdef(def));
  696. orddef :
  697. orddef_rtti(torddef(def));
  698. floatdef :
  699. floatdef_rtti(tfloatdef(def));
  700. setdef :
  701. setdef_rtti(tsetdef(def));
  702. procvardef :
  703. procvardef_rtti(tprocvardef(def));
  704. arraydef :
  705. begin
  706. if ado_IsBitPacked in tarraydef(def).arrayoptions then
  707. unknown_rtti(tstoreddef(def))
  708. else
  709. arraydef_rtti(tarraydef(def));
  710. end;
  711. recorddef :
  712. begin
  713. if trecorddef(def).is_packed then
  714. unknown_rtti(tstoreddef(def))
  715. else
  716. recorddef_rtti(trecorddef(def));
  717. end;
  718. objectdef :
  719. objectdef_rtti(tobjectdef(def));
  720. else
  721. unknown_rtti(tstoreddef(def));
  722. end;
  723. end;
  724. procedure TRTTIWriter.write_rtti_extrasyms(def:Tdef;rt:Trttitype;mainrtti:Tasmsymbol);
  725. procedure enumdef_rtti_ord2stringindex(def:Tenumdef);
  726. var rttilab:Tasmsymbol;
  727. t:Tenumsym;
  728. syms:^Tenumsym;
  729. offsets:^longint;
  730. sym_count,sym_alloc:longint;
  731. h,i,p,o,st:longint;
  732. mode:(lookup,search); {Modify with care, ordinal value of enum is written.}
  733. r:single; {Must be real type because of integer overflow risk.}
  734. begin
  735. {Random access needed, put in array.}
  736. getmem(syms,64*sizeof(Tenumsym));
  737. getmem(offsets,64*sizeof(longint));
  738. sym_count:=0;
  739. sym_alloc:=64;
  740. st:=0;
  741. t:=Tenumsym(def.firstenum);
  742. while assigned(t) do
  743. begin
  744. if sym_count>=sym_alloc then
  745. begin
  746. reallocmem(syms,2*sym_alloc*sizeof(Tenumsym));
  747. reallocmem(offsets,2*sym_alloc*sizeof(longint));
  748. sym_alloc:=sym_alloc*2;
  749. end;
  750. syms[sym_count]:=t;
  751. offsets[sym_count]:=st;
  752. inc(sym_count);
  753. st:=st+length(t.realname)+1;
  754. t:=t.nextenum;
  755. end;
  756. {Sort the syms by enum value}
  757. if sym_count>=2 then
  758. begin
  759. p:=1;
  760. while 2*p<sym_count do
  761. p:=2*p;
  762. while p<>0 do
  763. begin
  764. for h:=p to sym_count-1 do
  765. begin
  766. i:=h;
  767. t:=syms[i];
  768. o:=offsets[i];
  769. repeat
  770. if syms[i-p].value<=t.value then
  771. break;
  772. syms[i]:=syms[i-p];
  773. offsets[i]:=offsets[i-p];
  774. dec(i,p);
  775. until i<p;
  776. syms[i]:=t;
  777. offsets[i]:=o;
  778. end;
  779. p:=p shr 1;
  780. end;
  781. end;
  782. {Decide wether a lookup array is size efficient.}
  783. mode:=lookup;
  784. if sym_count>0 then
  785. begin
  786. i:=1;
  787. r:=0;
  788. h:=syms[0].value; {Next expected enum value is min.}
  789. while i<sym_count do
  790. begin
  791. {Calculate size of hole between values. Avoid integer overflows.}
  792. r:=r+(single(syms[i].value)-single(h))-1;
  793. h:=syms[i].value;
  794. inc(i);
  795. end;
  796. if r>sym_count then
  797. mode:=search; {Don't waste more than 50% space.}
  798. end;
  799. {Calculate start of string table.}
  800. st:=1;
  801. if assigned(def.typesym) then
  802. inc(st,length(def.typesym.realname)+1)
  803. else
  804. inc(st);
  805. if (tf_requires_proper_alignment in target_info.flags) then
  806. align(st,sizeof(Tconstptruint));
  807. inc(st);
  808. if (tf_requires_proper_alignment in target_info.flags) then
  809. align(st,sizeof(Tconstptruint));
  810. inc(st,8+sizeof(aint));
  811. { write rtti data }
  812. with current_asmdata do
  813. begin
  814. rttilab:=defineasmsymbol(Tstoreddef(def).rtti_mangledname(rt)+'_o2s',AB_GLOBAL,AT_DATA);
  815. maybe_new_object_file(asmlists[al_rtti]);
  816. new_section(asmlists[al_rtti],sec_rodata,rttilab.name,const_align(sizeof(aint)));
  817. asmlists[al_rtti].concat(Tai_symbol.create_global(rttilab,0));
  818. asmlists[al_rtti].concat(Tai_const.create_32bit(longint(mode)));
  819. if mode=lookup then
  820. begin
  821. o:=syms[0].value; {Start with min value.}
  822. for i:=0 to sym_count-1 do
  823. begin
  824. while o<syms[i].value do
  825. begin
  826. asmlists[al_rtti].concat(Tai_const.create_aint(0));
  827. inc(o);
  828. end;
  829. inc(o);
  830. asmlists[al_rtti].concat(Tai_const.create_sym_offset(mainrtti,st+offsets[i]));
  831. end;
  832. end
  833. else
  834. begin
  835. asmlists[al_rtti].concat(Tai_const.create_32bit(sym_count));
  836. for i:=0 to sym_count-1 do
  837. begin
  838. asmlists[al_rtti].concat(Tai_const.create_32bit(syms[i].value));
  839. asmlists[al_rtti].concat(Tai_const.create_sym_offset(mainrtti,st+offsets[i]));
  840. end;
  841. end;
  842. asmlists[al_rtti].concat(Tai_symbol_end.create(rttilab));
  843. end;
  844. dispose(syms);
  845. dispose(offsets);
  846. end;
  847. procedure enumdef_rtti_string2ordindex(def:Tenumdef);
  848. var rttilab:Tasmsymbol;
  849. t:Tenumsym;
  850. syms:^Tenumsym;
  851. offsets:^longint;
  852. sym_count,sym_alloc:longint;
  853. h,i,p,o,st:longint;
  854. begin
  855. {Random access needed, put in array.}
  856. getmem(syms,64*sizeof(Tenumsym));
  857. getmem(offsets,64*sizeof(longint));
  858. sym_count:=0;
  859. sym_alloc:=64;
  860. st:=0;
  861. t:=Tenumsym(def.firstenum);
  862. while assigned(t) do
  863. begin
  864. if sym_count>=sym_alloc then
  865. begin
  866. reallocmem(syms,2*sym_alloc*sizeof(Tenumsym));
  867. reallocmem(offsets,2*sym_alloc*sizeof(longint));
  868. sym_alloc:=sym_alloc*2;
  869. end;
  870. syms[sym_count]:=t;
  871. offsets[sym_count]:=st;
  872. inc(sym_count);
  873. st:=st+length(t.realname)+1;
  874. t:=t.nextenum;
  875. end;
  876. {Sort the syms by enum name}
  877. if sym_count>=2 then
  878. begin
  879. p:=1;
  880. while 2*p<sym_count do
  881. p:=2*p;
  882. while p<>0 do
  883. begin
  884. for h:=p to sym_count-1 do
  885. begin
  886. i:=h;
  887. t:=syms[i];
  888. o:=offsets[i];
  889. repeat
  890. if syms[i-p].name<=t.name then
  891. break;
  892. syms[i]:=syms[i-p];
  893. offsets[i]:=offsets[i-p];
  894. dec(i,p);
  895. until i<p;
  896. syms[i]:=t;
  897. offsets[i]:=o;
  898. end;
  899. p:=p shr 1;
  900. end;
  901. end;
  902. {Calculate start of string table.}
  903. st:=1;
  904. if assigned(def.typesym) then
  905. inc(st,length(def.typesym.realname)+1)
  906. else
  907. inc(st);
  908. if (tf_requires_proper_alignment in target_info.flags) then
  909. align(st,sizeof(Tconstptruint));
  910. inc(st);
  911. if (tf_requires_proper_alignment in target_info.flags) then
  912. align(st,sizeof(Tconstptruint));
  913. inc(st,8+sizeof(aint));
  914. { write rtti data }
  915. with current_asmdata do
  916. begin
  917. rttilab:=defineasmsymbol(Tstoreddef(def).rtti_mangledname(rt)+'_s2o',AB_GLOBAL,AT_DATA);
  918. maybe_new_object_file(asmlists[al_rtti]);
  919. new_section(asmlists[al_rtti],sec_rodata,rttilab.name,const_align(sizeof(aint)));
  920. asmlists[al_rtti].concat(Tai_symbol.create_global(rttilab,0));
  921. asmlists[al_rtti].concat(Tai_const.create_32bit(sym_count));
  922. for i:=0 to sym_count-1 do
  923. begin
  924. asmlists[al_rtti].concat(Tai_const.create_32bit(syms[i].value));
  925. asmlists[al_rtti].concat(Tai_const.create_sym_offset(mainrtti,st+offsets[i]));
  926. end;
  927. asmlists[al_rtti].concat(Tai_symbol_end.create(rttilab));
  928. end;
  929. dispose(syms);
  930. dispose(offsets);
  931. end;
  932. begin
  933. case def.typ of
  934. enumdef:
  935. if rt=fullrtti then
  936. begin
  937. enumdef_rtti_ord2stringindex(Tenumdef(def));
  938. enumdef_rtti_string2ordindex(Tenumdef(def));
  939. end;
  940. end;
  941. end;
  942. procedure TRTTIWriter.write_child_rtti_data(def:tdef;rt:trttitype);
  943. begin
  944. case def.typ of
  945. enumdef :
  946. if assigned(tenumdef(def).basedef) then
  947. write_rtti(tenumdef(def).basedef,rt);
  948. setdef :
  949. write_rtti(tsetdef(def).elementdef,rt);
  950. arraydef :
  951. write_rtti(tarraydef(def).elementdef,rt);
  952. recorddef :
  953. fields_write_rtti(trecorddef(def).symtable,rt);
  954. objectdef :
  955. begin
  956. if assigned(tobjectdef(def).childof) then
  957. write_rtti(tobjectdef(def).childof,rt);
  958. if rt=initrtti then
  959. fields_write_rtti(tobjectdef(def).symtable,rt)
  960. else
  961. published_write_rtti(tobjectdef(def).symtable,rt);
  962. end;
  963. end;
  964. end;
  965. function TRTTIWriter.ref_rtti(def:tdef;rt:trttitype):tasmsymbol;
  966. begin
  967. result:=current_asmdata.RefAsmSymbol(def.rtti_mangledname(rt));
  968. end;
  969. procedure TRTTIWriter.write_rtti(def:tdef;rt:trttitype);
  970. var
  971. rttilab : tasmsymbol;
  972. begin
  973. { only write rtti of definitions from the current module }
  974. if not findunitsymtable(def.owner).iscurrentunit then
  975. exit;
  976. { prevent recursion }
  977. if rttidefstate[rt] in def.defstates then
  978. exit;
  979. include(def.defstates,rttidefstate[rt]);
  980. { write first all dependencies }
  981. write_child_rtti_data(def,rt);
  982. { write rtti data }
  983. rttilab:=current_asmdata.DefineAsmSymbol(tstoreddef(def).rtti_mangledname(rt),AB_GLOBAL,AT_DATA);
  984. maybe_new_object_file(current_asmdata.asmlists[al_rtti]);
  985. new_section(current_asmdata.asmlists[al_rtti],sec_rodata,rttilab.name,const_align(sizeof(aint)));
  986. current_asmdata.asmlists[al_rtti].concat(Tai_symbol.Create_global(rttilab,0));
  987. write_rtti_data(def,rt);
  988. current_asmdata.asmlists[al_rtti].concat(Tai_symbol_end.Create(rttilab));
  989. write_rtti_extrasyms(def,rt,rttilab);
  990. end;
  991. function TRTTIWriter.get_rtti_label(def:tdef;rt:trttitype):tasmsymbol;
  992. begin
  993. result:=current_asmdata.RefAsmSymbol(def.rtti_mangledname(rt));
  994. end;
  995. function TRTTIWriter.get_rtti_label_ord2str(def:tdef;rt:trttitype):tasmsymbol;
  996. begin
  997. result:=current_asmdata.RefAsmSymbol(def.rtti_mangledname(rt)+'_o2s');
  998. end;
  999. function TRTTIWriter.get_rtti_label_str2ord(def:tdef;rt:trttitype):tasmsymbol;
  1000. begin
  1001. result:=current_asmdata.RefAsmSymbol(def.rtti_mangledname(rt)+'_s2o');
  1002. end;
  1003. end.