ncgrtti.pas 49 KB

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