ncgrtti.pas 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259
  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,sc80real,s64bit,s128bit);}
  491. translate : array[tfloattype] of byte =
  492. (ftSingle,ftDouble,ftExtended,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 }
  641. case def.proctypeoption of
  642. potype_constructor: methodkind:=mkConstructor;
  643. potype_destructor: methodkind:=mkDestructor;
  644. potype_procedure:
  645. if po_classmethod in def.procoptions then
  646. methodkind:=mkClassProcedure
  647. else
  648. methodkind:=mkProcedure;
  649. potype_function:
  650. if po_classmethod in def.procoptions then
  651. methodkind:=mkClassFunction
  652. else
  653. methodkind:=mkFunction;
  654. else
  655. begin
  656. if def.returndef = voidtype then
  657. methodkind:=mkProcedure
  658. else
  659. methodkind:=mkFunction;
  660. end;
  661. end;
  662. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(methodkind));
  663. { write parameter info. The parameters must be written in reverse order
  664. if this method uses right to left parameter pushing! }
  665. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(def.maxparacount));
  666. for i:=0 to def.paras.count-1 do
  667. write_para(tparavarsym(def.paras[i]));
  668. if (methodkind=mkFunction) or (methodkind=mkClassFunction) then
  669. begin
  670. { write name of result type }
  671. write_rtti_name(def.returndef);
  672. if (tf_requires_proper_alignment in target_info.flags) then
  673. current_asmdata.asmlists[al_rtti].concat(Cai_align.Create(sizeof(TConstPtrUint)));
  674. { write result typeinfo }
  675. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_sym(ref_rtti(def.returndef,fullrtti)))
  676. end;
  677. { write calling convention }
  678. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(ProcCallOptionToCallConv[def.proccalloption]));
  679. if (tf_requires_proper_alignment in target_info.flags) then
  680. current_asmdata.asmlists[al_rtti].concat(Cai_align.Create(sizeof(TConstPtrUint)));
  681. { write params typeinfo }
  682. for i:=0 to def.paras.count-1 do
  683. if not(vo_is_hidden_para in tparavarsym(def.paras[i]).varoptions) then
  684. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_sym(ref_rtti(tparavarsym(def.paras[i]).vardef,fullrtti)));
  685. end
  686. else
  687. begin
  688. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkprocvar));
  689. write_rtti_name(def);
  690. end;
  691. end;
  692. procedure objectdef_rtti(def:tobjectdef);
  693. procedure objectdef_rtti_class_init(def:tobjectdef);
  694. begin
  695. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(def.size));
  696. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(fields_count(def.symtable,rt)));
  697. fields_write_rtti_data(def.symtable,rt);
  698. end;
  699. procedure objectdef_rtti_interface_init(def:tobjectdef);
  700. begin
  701. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(def.size));
  702. end;
  703. procedure objectdef_rtti_class_full(def:tobjectdef);
  704. var
  705. propnamelist : TFPHashObjectList;
  706. begin
  707. { Collect unique property names with nameindex }
  708. propnamelist:=TFPHashObjectList.Create;
  709. collect_propnamelist(propnamelist,def);
  710. if (oo_has_vmt in def.objectoptions) then
  711. current_asmdata.asmlists[al_rtti].concat(Tai_const.Createname(def.vmt_mangledname,0))
  712. else
  713. current_asmdata.asmlists[al_rtti].concat(Tai_const.create_sym(nil));
  714. { write parent typeinfo }
  715. if assigned(def.childof) then
  716. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_sym(ref_rtti(def.childof,fullrtti)))
  717. else
  718. current_asmdata.asmlists[al_rtti].concat(Tai_const.create_sym(nil));
  719. { total number of unique properties }
  720. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_16bit(propnamelist.count));
  721. { write unit name }
  722. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(length(current_module.realmodulename^)));
  723. current_asmdata.asmlists[al_rtti].concat(Tai_string.Create(current_module.realmodulename^));
  724. if (tf_requires_proper_alignment in target_info.flags) then
  725. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  726. { write published properties for this object }
  727. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_16bit(published_properties_count(def.symtable)));
  728. if (tf_requires_proper_alignment in target_info.flags) then
  729. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  730. published_properties_write_rtti_data(propnamelist,def.symtable);
  731. propnamelist.free;
  732. end;
  733. procedure objectdef_rtti_interface_full(def:tobjectdef);
  734. var
  735. i : longint;
  736. propnamelist : TFPHashObjectList;
  737. { if changed to a set, make sure it's still a byte large, and
  738. swap appropriately when cross-compiling
  739. }
  740. IntfFlags: byte;
  741. begin
  742. { Collect unique property names with nameindex }
  743. propnamelist:=TFPHashObjectList.Create;
  744. collect_propnamelist(propnamelist,def);
  745. { write parent typeinfo }
  746. if assigned(def.childof) then
  747. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_sym(ref_rtti(def.childof,fullrtti)))
  748. else
  749. current_asmdata.asmlists[al_rtti].concat(Tai_const.create_sym(nil));
  750. { interface: write flags, iid and iidstr }
  751. IntfFlags:=0;
  752. if assigned(def.iidguid) then
  753. IntfFlags:=IntfFlags or (1 shl ord(ifHasGuid));
  754. if assigned(def.iidstr) then
  755. IntfFlags:=IntfFlags or (1 shl ord(ifHasStrGUID));
  756. if (def.objecttype=odt_dispinterface) then
  757. IntfFlags:=IntfFlags or (1 shl ord(ifDispInterface));
  758. if (target_info.endian=endian_big) then
  759. IntfFlags:=reverse_byte(IntfFlags);
  760. {
  761. ifDispatch, }
  762. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(IntfFlags));
  763. if (tf_requires_proper_alignment in target_info.flags) then
  764. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  765. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(longint(def.iidguid^.D1)));
  766. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_16bit(def.iidguid^.D2));
  767. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_16bit(def.iidguid^.D3));
  768. for i:=Low(def.iidguid^.D4) to High(def.iidguid^.D4) do
  769. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(def.iidguid^.D4[i]));
  770. { write unit name }
  771. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(length(current_module.realmodulename^)));
  772. current_asmdata.asmlists[al_rtti].concat(Tai_string.Create(current_module.realmodulename^));
  773. if (tf_requires_proper_alignment in target_info.flags) then
  774. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  775. { write iidstr }
  776. if assigned(def.iidstr) then
  777. begin
  778. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(length(def.iidstr^)));
  779. current_asmdata.asmlists[al_rtti].concat(Tai_string.Create(def.iidstr^));
  780. end
  781. else
  782. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(0));
  783. if (tf_requires_proper_alignment in target_info.flags) then
  784. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  785. { write published properties for this object }
  786. published_properties_write_rtti_data(propnamelist,def.symtable);
  787. propnamelist.free;
  788. end;
  789. begin
  790. case def.objecttype of
  791. odt_class:
  792. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkclass));
  793. odt_object:
  794. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkobject));
  795. odt_dispinterface,
  796. odt_interfacecom:
  797. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkinterface));
  798. odt_interfacecorba:
  799. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkinterfaceCorba));
  800. else
  801. internalerror(200611034);
  802. end;
  803. { generate the name }
  804. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(length(def.objrealname^)));
  805. current_asmdata.asmlists[al_rtti].concat(Tai_string.Create(def.objrealname^));
  806. if (tf_requires_proper_alignment in target_info.flags) then
  807. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  808. case rt of
  809. initrtti :
  810. begin
  811. if def.objecttype in [odt_class,odt_object] then
  812. objectdef_rtti_class_init(def)
  813. else
  814. objectdef_rtti_interface_init(def);
  815. end;
  816. fullrtti :
  817. begin
  818. if def.objecttype in [odt_class,odt_object] then
  819. objectdef_rtti_class_full(def)
  820. else
  821. objectdef_rtti_interface_full(def);
  822. end;
  823. end;
  824. end;
  825. begin
  826. case def.typ of
  827. variantdef :
  828. variantdef_rtti(tvariantdef(def));
  829. stringdef :
  830. stringdef_rtti(tstringdef(def));
  831. enumdef :
  832. enumdef_rtti(tenumdef(def));
  833. orddef :
  834. orddef_rtti(torddef(def));
  835. floatdef :
  836. floatdef_rtti(tfloatdef(def));
  837. setdef :
  838. setdef_rtti(tsetdef(def));
  839. procvardef :
  840. procvardef_rtti(tprocvardef(def));
  841. arraydef :
  842. begin
  843. if ado_IsBitPacked in tarraydef(def).arrayoptions then
  844. unknown_rtti(tstoreddef(def))
  845. else
  846. arraydef_rtti(tarraydef(def));
  847. end;
  848. recorddef :
  849. begin
  850. if trecorddef(def).is_packed then
  851. unknown_rtti(tstoreddef(def))
  852. else
  853. recorddef_rtti(trecorddef(def));
  854. end;
  855. objectdef :
  856. objectdef_rtti(tobjectdef(def));
  857. else
  858. unknown_rtti(tstoreddef(def));
  859. end;
  860. end;
  861. procedure TRTTIWriter.write_rtti_extrasyms(def:Tdef;rt:Trttitype;mainrtti:Tasmsymbol);
  862. procedure enumdef_rtti_ord2stringindex(def:Tenumdef);
  863. var rttilab:Tasmsymbol;
  864. t:Tenumsym;
  865. syms:^Tenumsym;
  866. offsets:^longint;
  867. sym_count,sym_alloc:longint;
  868. h,i,p,o,st:longint;
  869. mode:(lookup,search); {Modify with care, ordinal value of enum is written.}
  870. r:single; {Must be real type because of integer overflow risk.}
  871. begin
  872. {Random access needed, put in array.}
  873. getmem(syms,64*sizeof(Tenumsym));
  874. getmem(offsets,64*sizeof(longint));
  875. sym_count:=0;
  876. sym_alloc:=64;
  877. st:=0;
  878. t:=Tenumsym(def.firstenum);
  879. while assigned(t) do
  880. begin
  881. if sym_count>=sym_alloc then
  882. begin
  883. reallocmem(syms,2*sym_alloc*sizeof(Tenumsym));
  884. reallocmem(offsets,2*sym_alloc*sizeof(longint));
  885. sym_alloc:=sym_alloc*2;
  886. end;
  887. syms[sym_count]:=t;
  888. offsets[sym_count]:=st;
  889. inc(sym_count);
  890. st:=st+length(t.realname)+1;
  891. t:=t.nextenum;
  892. end;
  893. {Sort the syms by enum value}
  894. if sym_count>=2 then
  895. begin
  896. p:=1;
  897. while 2*p<sym_count do
  898. p:=2*p;
  899. while p<>0 do
  900. begin
  901. for h:=p to sym_count-1 do
  902. begin
  903. i:=h;
  904. t:=syms[i];
  905. o:=offsets[i];
  906. repeat
  907. if syms[i-p].value<=t.value then
  908. break;
  909. syms[i]:=syms[i-p];
  910. offsets[i]:=offsets[i-p];
  911. dec(i,p);
  912. until i<p;
  913. syms[i]:=t;
  914. offsets[i]:=o;
  915. end;
  916. p:=p shr 1;
  917. end;
  918. end;
  919. {Decide wether a lookup array is size efficient.}
  920. mode:=lookup;
  921. if sym_count>0 then
  922. begin
  923. i:=1;
  924. r:=0;
  925. h:=syms[0].value; {Next expected enum value is min.}
  926. while i<sym_count do
  927. begin
  928. {Calculate size of hole between values. Avoid integer overflows.}
  929. r:=r+(single(syms[i].value)-single(h))-1;
  930. h:=syms[i].value;
  931. inc(i);
  932. end;
  933. if r>sym_count then
  934. mode:=search; {Don't waste more than 50% space.}
  935. end;
  936. {Calculate start of string table.}
  937. st:=1;
  938. if assigned(def.typesym) then
  939. inc(st,length(def.typesym.realname)+1)
  940. else
  941. inc(st);
  942. if (tf_requires_proper_alignment in target_info.flags) then
  943. st:=align(st,sizeof(Tconstptruint));
  944. inc(st);
  945. if (tf_requires_proper_alignment in target_info.flags) then
  946. st:=align(st,sizeof(Tconstptruint));
  947. inc(st,8+sizeof(pint));
  948. { write rtti data }
  949. with current_asmdata do
  950. begin
  951. rttilab:=defineasmsymbol(Tstoreddef(def).rtti_mangledname(rt)+'_o2s',AB_GLOBAL,AT_DATA);
  952. maybe_new_object_file(asmlists[al_rtti]);
  953. new_section(asmlists[al_rtti],sec_rodata,rttilab.name,const_align(sizeof(pint)));
  954. asmlists[al_rtti].concat(Tai_symbol.create_global(rttilab,0));
  955. asmlists[al_rtti].concat(Tai_const.create_32bit(longint(mode)));
  956. if mode=lookup then
  957. begin
  958. if (tf_requires_proper_alignment in target_info.flags) then
  959. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  960. o:=syms[0].value; {Start with min value.}
  961. for i:=0 to sym_count-1 do
  962. begin
  963. while o<syms[i].value do
  964. begin
  965. asmlists[al_rtti].concat(Tai_const.create_pint(0));
  966. inc(o);
  967. end;
  968. inc(o);
  969. asmlists[al_rtti].concat(Tai_const.create_sym_offset(mainrtti,st+offsets[i]));
  970. end;
  971. end
  972. else
  973. begin
  974. asmlists[al_rtti].concat(Tai_const.create_32bit(sym_count));
  975. for i:=0 to sym_count-1 do
  976. begin
  977. if (tf_requires_proper_alignment in target_info.flags) then
  978. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(4));
  979. asmlists[al_rtti].concat(Tai_const.create_32bit(syms[i].value));
  980. if (tf_requires_proper_alignment in target_info.flags) then
  981. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  982. asmlists[al_rtti].concat(Tai_const.create_sym_offset(mainrtti,st+offsets[i]));
  983. end;
  984. end;
  985. asmlists[al_rtti].concat(Tai_symbol_end.create(rttilab));
  986. end;
  987. freemem(syms);
  988. freemem(offsets);
  989. end;
  990. procedure enumdef_rtti_string2ordindex(def:Tenumdef);
  991. var rttilab:Tasmsymbol;
  992. t:Tenumsym;
  993. syms:^Tenumsym;
  994. offsets:^longint;
  995. sym_count,sym_alloc:longint;
  996. h,i,p,o,st:longint;
  997. begin
  998. {Random access needed, put in array.}
  999. getmem(syms,64*sizeof(Tenumsym));
  1000. getmem(offsets,64*sizeof(longint));
  1001. sym_count:=0;
  1002. sym_alloc:=64;
  1003. st:=0;
  1004. t:=Tenumsym(def.firstenum);
  1005. while assigned(t) do
  1006. begin
  1007. if sym_count>=sym_alloc then
  1008. begin
  1009. reallocmem(syms,2*sym_alloc*sizeof(Tenumsym));
  1010. reallocmem(offsets,2*sym_alloc*sizeof(longint));
  1011. sym_alloc:=sym_alloc*2;
  1012. end;
  1013. syms[sym_count]:=t;
  1014. offsets[sym_count]:=st;
  1015. inc(sym_count);
  1016. st:=st+length(t.realname)+1;
  1017. t:=t.nextenum;
  1018. end;
  1019. {Sort the syms by enum name}
  1020. if sym_count>=2 then
  1021. begin
  1022. p:=1;
  1023. while 2*p<sym_count do
  1024. p:=2*p;
  1025. while p<>0 do
  1026. begin
  1027. for h:=p to sym_count-1 do
  1028. begin
  1029. i:=h;
  1030. t:=syms[i];
  1031. o:=offsets[i];
  1032. repeat
  1033. if syms[i-p].name<=t.name then
  1034. break;
  1035. syms[i]:=syms[i-p];
  1036. offsets[i]:=offsets[i-p];
  1037. dec(i,p);
  1038. until i<p;
  1039. syms[i]:=t;
  1040. offsets[i]:=o;
  1041. end;
  1042. p:=p shr 1;
  1043. end;
  1044. end;
  1045. {Calculate start of string table.}
  1046. st:=1;
  1047. if assigned(def.typesym) then
  1048. inc(st,length(def.typesym.realname)+1)
  1049. else
  1050. inc(st);
  1051. if (tf_requires_proper_alignment in target_info.flags) then
  1052. st:=align(st,sizeof(Tconstptruint));
  1053. inc(st);
  1054. if (tf_requires_proper_alignment in target_info.flags) then
  1055. st:=align(st,sizeof(Tconstptruint));
  1056. inc(st,8+sizeof(pint));
  1057. { write rtti data }
  1058. with current_asmdata do
  1059. begin
  1060. rttilab:=defineasmsymbol(Tstoreddef(def).rtti_mangledname(rt)+'_s2o',AB_GLOBAL,AT_DATA);
  1061. maybe_new_object_file(asmlists[al_rtti]);
  1062. new_section(asmlists[al_rtti],sec_rodata,rttilab.name,const_align(sizeof(pint)));
  1063. asmlists[al_rtti].concat(Tai_symbol.create_global(rttilab,0));
  1064. asmlists[al_rtti].concat(Tai_const.create_32bit(sym_count));
  1065. for i:=0 to sym_count-1 do
  1066. begin
  1067. if (tf_requires_proper_alignment in target_info.flags) then
  1068. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(4));
  1069. asmlists[al_rtti].concat(Tai_const.create_32bit(syms[i].value));
  1070. if (tf_requires_proper_alignment in target_info.flags) then
  1071. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  1072. asmlists[al_rtti].concat(Tai_const.create_sym_offset(mainrtti,st+offsets[i]));
  1073. end;
  1074. asmlists[al_rtti].concat(Tai_symbol_end.create(rttilab));
  1075. end;
  1076. freemem(syms);
  1077. freemem(offsets);
  1078. end;
  1079. begin
  1080. case def.typ of
  1081. enumdef:
  1082. if rt=fullrtti then
  1083. begin
  1084. enumdef_rtti_ord2stringindex(Tenumdef(def));
  1085. enumdef_rtti_string2ordindex(Tenumdef(def));
  1086. end;
  1087. end;
  1088. end;
  1089. procedure TRTTIWriter.write_child_rtti_data(def:tdef;rt:trttitype);
  1090. begin
  1091. case def.typ of
  1092. enumdef :
  1093. if assigned(tenumdef(def).basedef) then
  1094. write_rtti(tenumdef(def).basedef,rt);
  1095. setdef :
  1096. write_rtti(tsetdef(def).elementdef,rt);
  1097. arraydef :
  1098. write_rtti(tarraydef(def).elementdef,rt);
  1099. recorddef :
  1100. fields_write_rtti(trecorddef(def).symtable,rt);
  1101. objectdef :
  1102. begin
  1103. if assigned(tobjectdef(def).childof) then
  1104. write_rtti(tobjectdef(def).childof,rt);
  1105. if rt=initrtti then
  1106. fields_write_rtti(tobjectdef(def).symtable,rt)
  1107. else
  1108. published_write_rtti(tobjectdef(def).symtable,rt);
  1109. end;
  1110. end;
  1111. end;
  1112. function TRTTIWriter.ref_rtti(def:tdef;rt:trttitype):tasmsymbol;
  1113. begin
  1114. result:=current_asmdata.RefAsmSymbol(def.rtti_mangledname(rt));
  1115. end;
  1116. procedure TRTTIWriter.write_rtti(def:tdef;rt:trttitype);
  1117. var
  1118. rttilab : tasmsymbol;
  1119. begin
  1120. { only write rtti of definitions from the current module }
  1121. if not findunitsymtable(def.owner).iscurrentunit then
  1122. exit;
  1123. { prevent recursion }
  1124. if rttidefstate[rt] in def.defstates then
  1125. exit;
  1126. include(def.defstates,rttidefstate[rt]);
  1127. { write first all dependencies }
  1128. write_child_rtti_data(def,rt);
  1129. { write rtti data }
  1130. rttilab:=current_asmdata.DefineAsmSymbol(tstoreddef(def).rtti_mangledname(rt),AB_GLOBAL,AT_DATA);
  1131. maybe_new_object_file(current_asmdata.asmlists[al_rtti]);
  1132. new_section(current_asmdata.asmlists[al_rtti],sec_rodata,rttilab.name,const_align(sizeof(pint)));
  1133. current_asmdata.asmlists[al_rtti].concat(Tai_symbol.Create_global(rttilab,0));
  1134. write_rtti_data(def,rt);
  1135. current_asmdata.asmlists[al_rtti].concat(Tai_symbol_end.Create(rttilab));
  1136. write_rtti_extrasyms(def,rt,rttilab);
  1137. end;
  1138. function TRTTIWriter.get_rtti_label(def:tdef;rt:trttitype):tasmsymbol;
  1139. begin
  1140. result:=current_asmdata.RefAsmSymbol(def.rtti_mangledname(rt));
  1141. end;
  1142. function TRTTIWriter.get_rtti_label_ord2str(def:tdef;rt:trttitype):tasmsymbol;
  1143. begin
  1144. result:=current_asmdata.RefAsmSymbol(def.rtti_mangledname(rt)+'_o2s');
  1145. end;
  1146. function TRTTIWriter.get_rtti_label_str2ord(def:tdef;rt:trttitype):tasmsymbol;
  1147. begin
  1148. result:=current_asmdata.RefAsmSymbol(def.rtti_mangledname(rt)+'_s2o');
  1149. end;
  1150. end.