ncgrtti.pas 50 KB

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