objcgutl.pas 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448
  1. {
  2. Copyright (c) 2009 by Jonas Maebe
  3. This unit implements some Objective-C helper routines at the code generator
  4. level.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License,or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. {$i fpcdefs.inc}
  19. unit objcgutl;
  20. interface
  21. uses
  22. cclasses,
  23. aasmbase,aasmdata,
  24. symbase,symdef;
  25. procedure objcfinishstringrefpoolentry(entry: phashsetitem; stringpool: tconstpooltype; refsec, stringsec: tasmsectiontype);
  26. procedure objcfinishclassrefnfpoolentry(entry: phashsetitem; classdef: tobjectdef);
  27. procedure MaybeGenerateObjectiveCImageInfo(globalst, localst: tsymtable);
  28. implementation
  29. uses
  30. globtype,globals,fmodule,
  31. systems,
  32. aasmtai,
  33. cgbase,
  34. objcutil,
  35. symconst,symtype,symsym,symtable,
  36. verbose;
  37. type
  38. tobjcabi = (oa_fragile, oa_nonfragile);
  39. (* tivarlayouttype = (il_weak,il_strong); *)
  40. tobjcrttiwriter = class
  41. protected
  42. fabi: tobjcabi;
  43. classdefs: tfpobjectlist;
  44. classsyms: tfpobjectlist;
  45. procedure gen_objc_methods(list: tasmlist; objccls: tobjectdef; out methodslabel: tasmlabel; classmethods, iscategory: Boolean);
  46. procedure gen_objc_protocol_list(list:TAsmList; protolist: TFPObjectList; out protolistsym: TAsmLabel);
  47. procedure gen_objc_cat_methods(list:TAsmList; items: TFPObjectList; section: tasmsectiontype;const sectname: string; out listsym: TAsmLabel);
  48. procedure gen_objc_protocol(list:TAsmList; protocol: tobjectdef; out protocollabel: TAsmSymbol);virtual;abstract;
  49. procedure gen_objc_classes_sections(list:TAsmList; objclss: tobjectdef; out classlabel: TAsmSymbol);virtual;abstract;
  50. procedure gen_objc_info_sections(list: tasmlist);virtual;abstract;
  51. public
  52. constructor create(_abi: tobjcabi);
  53. destructor destroy;override;
  54. procedure gen_objc_rtti_sections(list:TAsmList; st:TSymtable);
  55. property abi: tobjcabi read fabi;
  56. end;
  57. { Used by by PowerPC/32 and i386 }
  58. tobjcrttiwriter_fragile = class(tobjcrttiwriter)
  59. protected
  60. procedure gen_objc_ivars(list: TAsmList; objccls: tobjectdef; out ivarslabel: TAsmLabel);
  61. procedure gen_objc_protocol(list:TAsmList; protocol: tobjectdef; out protocollabel: TAsmSymbol);override;
  62. procedure gen_objc_classes_sections(list:TAsmList; objclss: tobjectdef; out classlabel: TAsmSymbol);override;
  63. procedure gen_objc_info_sections(list: tasmlist);override;
  64. public
  65. constructor create;
  66. end;
  67. { Used by PowerPC/64, ARM, and x86_64 }
  68. tobjcrttiwriter_nonfragile = class(tobjcrttiwriter)
  69. protected
  70. ObjCEmptyCacheVar,
  71. ObjCEmptyVtableVar: TAsmSymbol;
  72. procedure gen_objc_class_ro_part(list: TAsmList; objclss: tobjectdef; protolistsym: TAsmSymbol; out classrolabel: TAsmSymbol; metaclass: boolean);
  73. procedure addclasslist(list: tasmlist; section: tasmsectiontype; const symname: string; classes: tfpobjectlist);
  74. procedure gen_objc_ivars(list: TAsmList; objccls: tobjectdef; out ivarslabel: TAsmLabel);
  75. procedure gen_objc_protocol(list:TAsmList; protocol: tobjectdef; out protocollabel: TAsmSymbol);override;
  76. procedure gen_objc_classes_sections(list:TAsmList; objclss: tobjectdef; out classlabel: TAsmSymbol);override;
  77. procedure gen_objc_info_sections(list: tasmlist);override;
  78. public
  79. constructor create;
  80. end;
  81. {******************************************************************
  82. Protocol declaration helpers
  83. *******************************************************************}
  84. function objcfindprotocolentry(const p: shortstring): TAsmSymbol;
  85. var
  86. item : PHashSetItem;
  87. begin
  88. result:=nil;
  89. if not assigned(current_asmdata.ConstPools[sp_objcprotocolrefs]) then
  90. exit;
  91. item:=current_asmdata.constpools[sp_objcprotocolrefs].Find(@p[1], length(p));
  92. if not assigned(item) then
  93. exit;
  94. result:=TAsmSymbol(item^.Data);
  95. end;
  96. function objcaddprotocolentry(const p: shortstring; ref: TAsmSymbol): Boolean;
  97. var
  98. item : PHashSetItem;
  99. begin
  100. if current_asmdata.ConstPools[sp_objcprotocolrefs]=nil then
  101. current_asmdata.ConstPools[sp_objcprotocolrefs]:=THashSet.Create(64, True, False);
  102. item:=current_asmdata.constpools[sp_objcprotocolrefs].FindOrAdd(@p[1], length(p));
  103. Result:=(item^.Data=nil);
  104. if Result then
  105. item^.Data:=ref;
  106. end;
  107. {******************************************************************
  108. Pool section helpers
  109. *******************************************************************}
  110. function objcreatestringpoolentryintern(p: pchar; len: longint; pooltype: tconstpooltype; stringsec: tasmsectiontype): TAsmSymbol;
  111. var
  112. entry : PHashSetItem;
  113. strlab : tasmlabel;
  114. pc : pchar;
  115. pool : THashSet;
  116. begin
  117. if current_asmdata.ConstPools[pooltype]=nil then
  118. current_asmdata.ConstPools[pooltype]:=THashSet.Create(64, True, False);
  119. pool := current_asmdata.constpools[pooltype];
  120. entry:=pool.FindOrAdd(p,len);
  121. if not assigned(entry^.data) then
  122. begin
  123. { create new entry }
  124. current_asmdata.getlabel(strlab,alt_data);
  125. entry^.Data:=strlab;
  126. getmem(pc,entry^.keylength+1);
  127. move(entry^.key^,pc^,entry^.keylength);
  128. pc[entry^.keylength]:=#0;
  129. { add the string to the approriate section }
  130. new_section(current_asmdata.asmlists[al_objc_pools],stringsec,strlab.name,sizeof(pint));
  131. current_asmdata.asmlists[al_objc_pools].concat(Tai_label.Create(strlab));
  132. current_asmdata.asmlists[al_objc_pools].concat(Tai_string.Create_pchar(pc,entry^.keylength+1));
  133. Result := strlab;
  134. end
  135. else
  136. Result := TAsmLabel(Entry^.Data);
  137. end;
  138. procedure objcfinishstringrefpoolentry(entry: phashsetitem; stringpool: tconstpooltype; refsec, stringsec: tasmsectiontype);
  139. var
  140. reflab : tasmlabel;
  141. strlab : tasmsymbol;
  142. classname: string;
  143. begin
  144. { have we already generated a reference for this string entry? }
  145. if not assigned(entry^.Data) then
  146. begin
  147. { no, add the string to the associated strings section }
  148. strlab:=objcreatestringpoolentryintern(pchar(entry^.key),entry^.keylength,stringpool,stringsec);
  149. { and now finish the reference }
  150. current_asmdata.getlabel(reflab,alt_data);
  151. entry^.Data:=reflab;
  152. { add a pointer to the string in the string references section }
  153. new_section(current_asmdata.asmlists[al_objc_pools],refsec,reflab.name,sizeof(pint));
  154. current_asmdata.asmlists[al_objc_pools].concat(Tai_label.Create(reflab));
  155. current_asmdata.asmlists[al_objc_pools].concat(Tai_const.Create_sym(strlab));
  156. { in case of a class reference, also add a lazy symbol reference for
  157. the class (the linker requires this for the fragile ABI). }
  158. if (refsec=sec_objc_cls_refs) and
  159. not(target_info.system in system_objc_nfabi) then
  160. begin
  161. setlength(classname,entry^.keylength);
  162. move(entry^.key^,classname[1],entry^.keylength);
  163. current_asmdata.asmlists[al_objc_pools].concat(tai_directive.Create(asd_lazy_reference,'.objc_class_name_'+classname));
  164. end;
  165. end;
  166. end;
  167. function objcreatestringpoolentry(const s: string; pooltype: tconstpooltype; stringsec: tasmsectiontype): TAsmSymbol;
  168. begin
  169. result:=objcreatestringpoolentryintern(@s[1],length(s),pooltype,stringsec);
  170. end;
  171. procedure objcfinishclassrefnfpoolentry(entry: phashsetitem; classdef: tobjectdef);
  172. var
  173. reflab: TAsmLabel;
  174. classym: TasmSymbol;
  175. begin
  176. { have we already generated a reference for this class ref entry? }
  177. if not assigned(entry^.Data) then
  178. begin
  179. { no, add the classref to the sec_objc_cls_refs section }
  180. current_asmdata.getlabel(reflab,alt_data);
  181. entry^.Data:=reflab;
  182. { add a pointer to the class }
  183. new_section(current_asmdata.asmlists[al_objc_pools],sec_objc_cls_refs,reflab.name,sizeof(pint));
  184. current_asmdata.asmlists[al_objc_pools].concat(Tai_label.Create(reflab));
  185. classym:=current_asmdata.RefAsmSymbol(classdef.rtti_mangledname(objcclassrtti));
  186. current_asmdata.asmlists[al_objc_pools].concat(Tai_const.Create_sym(classym));
  187. end;
  188. end;
  189. {******************************************************************
  190. RTTI generation -- Helpers
  191. *******************************************************************}
  192. procedure ConcatSymOrNil(list: tasmlist; sym: TAsmSymbol); inline;
  193. begin
  194. if Assigned(sym) then
  195. list.Concat(tai_const.Create_sym(sym))
  196. else
  197. list.Concat(tai_const.Create_pint(0));
  198. end;
  199. {******************************************************************
  200. RTTI generation -- Common
  201. *******************************************************************}
  202. { generate a method list, either of class methods or of instance methods,
  203. and both for obj-c classes and categories. }
  204. procedure tobjcrttiwriter.gen_objc_methods(list: tasmlist; objccls: tobjectdef; out methodslabel: tasmlabel; classmethods, iscategory: Boolean);
  205. const
  206. clsSectType : array [Boolean] of tasmsectiontype = (sec_objc_inst_meth, sec_objc_cls_meth);
  207. clsSectName : array [Boolean] of string = ('_OBJC_INST_METH','_OBJC_CLS_METH');
  208. catSectType : array [Boolean] of tasmsectiontype = (sec_objc_cat_inst_meth, sec_objc_cat_cls_meth);
  209. catSectName : array [Boolean] of string = ('_OBJC_CAT_INST_METH','_OBJC_CAT_CLS_METH');
  210. type
  211. method_data = record
  212. def : tprocdef;
  213. selsym : TAsmSymbol;
  214. encsym : TAsmSymbol;
  215. end;
  216. var
  217. i : Integer;
  218. def : tprocdef;
  219. defs : array of method_data;
  220. mcnt : integer;
  221. sym : tasmsymbol;
  222. mtype : tdef;
  223. begin
  224. methodslabel:=nil;
  225. mcnt:=0;
  226. { collect all instance/class methods }
  227. SetLength(defs,objccls.vmtentries.count);
  228. for i:=0 to objccls.vmtentries.count-1 do
  229. begin
  230. def:=pvmtentry(objccls.vmtentries[i])^.procdef;
  231. if Assigned(def.procstarttai) and
  232. (classmethods = (po_classmethod in def.procoptions)) then
  233. begin
  234. defs[mcnt].def:=def;
  235. defs[mcnt].selsym:=objcreatestringpoolentry(def.messageinf.str^,sp_objcvarnames,sec_objc_meth_var_names);
  236. defs[mcnt].encsym:=objcreatestringpoolentry(objcencodemethod(def),sp_objcvartypes,sec_objc_meth_var_types);
  237. inc(mcnt);
  238. end;
  239. end;
  240. if mcnt=0 then
  241. exit;
  242. if iscategory then
  243. new_section(list,clsSectType[classmethods],clsSectName[classmethods],sizeof(ptrint))
  244. else
  245. new_section(list,catSectType[classmethods],catSectName[classmethods],sizeof(ptrint));
  246. current_asmdata.getlabel(methodslabel,alt_data);
  247. list.Concat(tai_label.Create(methodslabel));
  248. if (abi=oa_fragile) then
  249. { not used, always zero }
  250. list.Concat(tai_const.Create_32bit(0))
  251. else
  252. begin
  253. { size of each entry -- always 32 bit value }
  254. mtype:=search_named_unit_globaltype('OBJC','OBJC_METHOD').typedef;
  255. list.Concat(tai_const.Create_32bit(mtype.size));
  256. end;
  257. { number of objc_method entries in the method_list array -- always 32 bit}
  258. list.Concat(tai_const.Create_32bit(mcnt));
  259. for i:=0 to mcnt-1 do
  260. begin
  261. { reference to the selector name }
  262. list.Concat(tai_const.Create_sym(defs[i].selsym));
  263. { reference to the obj-c encoded function parameters (signature) }
  264. list.Concat(tai_const.Create_sym(defs[i].encsym));
  265. { mangled name of the method }
  266. sym:=current_asmdata.GetAsmSymbol(defs[i].def.mangledname);
  267. if not assigned(sym) then
  268. internalerror(2009091601);
  269. list.Concat(tai_const.Create_sym(sym));
  270. end;
  271. end;
  272. (*
  273. From CLang:
  274. struct objc_protocol_list
  275. {
  276. #ifdef FRAGILE_ABI
  277. struct objc_protocol_list *next;
  278. int count;
  279. #else
  280. long count;
  281. #endif
  282. Protocol *list[1];
  283. };
  284. *)
  285. procedure tobjcrttiwriter.gen_objc_protocol_list(list: tasmlist; protolist: tfpobjectlist; out protolistsym: tasmlabel);
  286. var
  287. i : Integer;
  288. protosym : TAsmSymbol;
  289. protodef : tobjectdef;
  290. begin
  291. if not Assigned(protolist) or
  292. (protolist.Count=0) then
  293. begin
  294. protolistsym:=nil;
  295. Exit;
  296. end;
  297. for i:=0 to protolist.Count-1 do
  298. begin
  299. protodef:=TImplementedInterface(protolist[i]).IntfDef;
  300. protosym:=objcfindprotocolentry(protodef.objextname^);
  301. if not assigned(protosym) then
  302. begin
  303. gen_objc_protocol(list,protodef,protosym);
  304. objcaddprotocolentry(protodef.objextname^,protosym);
  305. end;
  306. end;
  307. { protocol lists are stored in .objc_cat_cls_meth section }
  308. new_section(list,sec_objc_cat_cls_meth,'_OBJC_PROTOCOLLIST',sizeof(pint));
  309. current_asmdata.getlabel(protolistsym, alt_data);
  310. list.Concat(tai_label.Create(protolistsym));
  311. if (abi=oa_fragile) then
  312. { From Clang: next, always nil}
  313. list.Concat(tai_const.Create_pint(0));
  314. { From Clang: protocols count}
  315. list.Concat(Tai_const.Create_pint(protolist.Count));
  316. for i:=0 to protolist.Count-1 do
  317. begin
  318. protodef:=(protolist[i] as TImplementedInterface).IntfDef;
  319. protosym:=objcfindprotocolentry(protodef.objextname^);
  320. if not Assigned(protosym) then
  321. begin
  322. { For some reason protosym is not declared, though must be!
  323. Probably gen_obcj1_protocol returned wrong protosym
  324. }
  325. InternalError(2009091602);
  326. end;
  327. list.Concat(tai_const.Create_sym(protosym));
  328. end;
  329. end;
  330. { Generate rtti for an Objective-C methods (methods without implementation) }
  331. { items : TFPObjectList of Tprocdef }
  332. procedure tobjcrttiwriter.gen_objc_cat_methods(list:TAsmList; items: TFPObjectList; section: tasmsectiontype;
  333. const sectname: string; out listsym: TAsmLabel);
  334. var
  335. i : integer;
  336. m : tprocdef;
  337. mtype : tdef;
  338. begin
  339. if not assigned(items) or
  340. (items.count=0) then
  341. exit;
  342. new_section(list, section, sectname, sizeof(pint));
  343. current_asmdata.getlabel(listsym,alt_data);
  344. list.Concat(tai_label.Create(listsym));
  345. if (abi=oa_nonfragile) then
  346. begin
  347. { size of each entry -- always 32 bit value }
  348. mtype:=search_named_unit_globaltype('OBJC','OBJC_METHOD').typedef;
  349. list.Concat(tai_const.Create_32bit(mtype.size));
  350. end;
  351. list.Concat(Tai_const.Create_32bit(items.count));
  352. for i:=0 to items.Count-1 do
  353. begin
  354. m:=tprocdef(items[i]);
  355. list.Concat(Tai_const.Create_sym(
  356. objcreatestringpoolentry(m.messageinf.str^,sp_objcvarnames,sec_objc_meth_var_names)));
  357. list.Concat(Tai_const.Create_sym(
  358. objcreatestringpoolentry(objcencodemethod(m),sp_objcvartypes,sec_objc_meth_var_types)));
  359. { placeholder for address of implementation? }
  360. if (abi=oa_nonfragile) then
  361. list.Concat(Tai_const.Create_pint(0));
  362. end;
  363. end;
  364. { Generate the rtti sections for all obj-c classes defined in st, and return
  365. these classes in the classes list. }
  366. procedure tobjcrttiwriter.gen_objc_rtti_sections(list:TAsmList; st:TSymtable);
  367. var
  368. i: longint;
  369. def: tdef;
  370. sym : TAsmSymbol;
  371. begin
  372. if not Assigned(st) then
  373. exit;
  374. for i:=0 to st.DefList.Count-1 do
  375. begin
  376. def:=tdef(st.DefList[i]);
  377. if is_objcclass(def) and
  378. not(oo_is_external in tobjectdef(def).objectoptions) then
  379. begin
  380. gen_objc_classes_sections(list,tobjectdef(def),sym);
  381. classsyms.add(sym);
  382. classdefs.add(def);
  383. end;
  384. end;
  385. end;
  386. constructor tobjcrttiwriter.create(_abi: tobjcabi);
  387. begin
  388. fabi:=_abi;
  389. classdefs:=tfpobjectlist.create(false);
  390. classsyms:=tfpobjectlist.create(false);
  391. end;
  392. destructor tobjcrttiwriter.destroy;
  393. begin
  394. classdefs.free;
  395. classsyms.free;
  396. inherited destroy;
  397. end;
  398. {******************************************************************
  399. RTTI generation -- Fragile ABI
  400. *******************************************************************}
  401. { generate an instance variables list for an obj-c class. }
  402. procedure tobjcrttiwriter_fragile.gen_objc_ivars(list: TAsmList; objccls: tobjectdef; out ivarslabel: TAsmLabel);
  403. type
  404. ivar_data = record
  405. vf : tfieldvarsym;
  406. namesym : TAsmSymbol;
  407. typesym : TAsmSymbol;
  408. end;
  409. var
  410. i : integer;
  411. vf : tfieldvarsym;
  412. vars : array of ivar_data;
  413. vcnt : Integer;
  414. enctype : ansistring;
  415. encerr : tdef;
  416. begin
  417. ivarslabel:=nil;
  418. vcnt:=0;
  419. setLength(vars,objccls.symtable.SymList.Count);
  420. for i:=0 to objccls.symtable.SymList.Count-1 do
  421. if tsym(objccls.symtable.SymList[i]).typ=fieldvarsym then
  422. begin
  423. vf:=tfieldvarsym(objccls.symtable.SymList[i]);
  424. if objctryencodetype(vf.vardef,enctype,encerr) then
  425. begin
  426. vars[vcnt].vf:=vf;
  427. vars[vcnt].namesym:=objcreatestringpoolentry(vf.RealName,sp_objcvarnames,sec_objc_meth_var_names);
  428. vars[vcnt].typesym:=objcreatestringpoolentry(enctype,sp_objcvartypes,sec_objc_meth_var_types);
  429. inc(vcnt);
  430. end
  431. else
  432. { must be caught during parsing }
  433. internalerror(2009090601);
  434. end;
  435. if vcnt=0 then
  436. exit;
  437. new_section(list,sec_objc_instance_vars,'_OBJC_INSTANCE_VARS',sizeof(pint));
  438. current_asmdata.getlabel(ivarslabel,alt_data);
  439. list.Concat(tai_label.Create(ivarslabel));
  440. { objc_ivar_list: first the number of elements }
  441. list.Concat(tai_const.Create_32bit(vcnt));
  442. for i:=0 to vcnt-1 do
  443. begin
  444. { reference to the instance variable name }
  445. list.Concat(tai_const.Create_sym(vars[i].namesym));
  446. { reference to the encoded type }
  447. list.Concat(tai_const.Create_sym(vars[i].typesym));
  448. { and the offset of the field }
  449. list.Concat(tai_const.Create_32bit(vars[i].vf.fieldoffset));
  450. end;
  451. end;
  452. { Generate rtti for an Objective-C protocol }
  453. procedure tobjcrttiwriter_fragile.gen_objc_protocol(list:TAsmList; protocol: tobjectdef; out protocollabel: TAsmSymbol);
  454. var
  455. namesym : TAsmSymbol;
  456. i : Integer;
  457. protolist : TAsmLabel;
  458. proc : tprocdef;
  459. instmlist,
  460. clsmlist : TFPObjectList;
  461. instsym,
  462. clssym,
  463. lbl : TAsmLabel;
  464. begin
  465. instmlist:=TFPObjectList.Create(false);
  466. clsmlist:=TFPObjectList.Create(false);
  467. for i:=0 to protocol.vmtentries.Count-1 do
  468. begin
  469. proc:=pvmtentry(protocol.vmtentries[i])^.procdef;
  470. if (po_classmethod in proc.procoptions) then
  471. clsmlist.Add(proc)
  472. else
  473. instmlist.Add(proc);
  474. end;
  475. if instmlist.Count > 0 then
  476. gen_objc_cat_methods(list,instmlist,sec_objc_cat_inst_meth,'_OBJC_CAT_INST_METH',instsym)
  477. else
  478. instsym:=nil;
  479. if clsmlist.Count>0 then
  480. gen_objc_cat_methods(list,clsmlist,sec_objc_cat_cls_meth,'_OBJC_CAT_CLS_METH',clssym)
  481. else
  482. clssym:=nil;
  483. instmlist.Free;
  484. clsmlist.Free;
  485. gen_objc_protocol_list(list,protocol.ImplementedInterfaces,protolist);
  486. new_section(list, sec_objc_protocol,'_OBJC_PROTOCOL',sizeof(pint));
  487. current_asmdata.getlabel(lbl,alt_data);
  488. list.Concat(tai_label.Create(lbl));
  489. protocollabel:=lbl;
  490. { protocol's isa - always nil }
  491. list.Concat(Tai_const.Create_pint(0));
  492. { name }
  493. namesym:=objcreatestringpoolentry(protocol.objextname^,sp_objcclassnames,sec_objc_class_names);
  494. list.Concat(Tai_const.Create_sym(namesym));
  495. { protocol's list }
  496. ConcatSymOrNil(list,protolist);
  497. { instance methods, in __cat_inst_meth }
  498. ConcatSymOrNil(list,instsym);
  499. { class methods, in __cat_cls_meth }
  500. ConcatSymOrNil(list,clssym);
  501. end;
  502. (*
  503. From Clang:
  504. struct _objc_class {
  505. Class isa;
  506. Class super_class;
  507. const char *name;
  508. long version;
  509. long info;
  510. long instance_size;
  511. struct _objc_ivar_list *ivars;
  512. struct _objc_method_list *methods;
  513. struct _objc_cache *cache;
  514. struct _objc_protocol_list *protocols;
  515. // Objective-C 1.0 extensions (<rdr://4585769>) -- for garbage collection
  516. const char *ivar_layout;
  517. struct _objc_class_ext *ext;
  518. };
  519. *)
  520. { Generate rtti for an Objective-C class and its meta-class. }
  521. procedure tobjcrttiwriter_fragile.gen_objc_classes_sections(list:TAsmList; objclss: tobjectdef; out classlabel: TAsmSymbol);
  522. const
  523. CLS_CLASS = 1;
  524. CLS_META = 2;
  525. CLS_HIDDEN = $20000;
  526. META_INST_SIZE = 40+8; // sizeof(objc_class) + 8
  527. var
  528. root : tobjectdef;
  529. superStrSym,
  530. classStrSym,
  531. metaisaStrSym,
  532. metasym,
  533. clssym : TAsmSymbol;
  534. mthdlist,
  535. ivarslist,
  536. protolistsym : TAsmLabel;
  537. hiddenflag : cardinal;
  538. begin
  539. { generate the class methods list }
  540. gen_objc_methods(list,objclss,mthdlist,true,false);
  541. { generate implemented protocols list }
  542. gen_objc_protocol_list(list,objclss.ImplementedInterfaces,protolistsym);
  543. { register necessary names }
  544. { 1) the superclass }
  545. if assigned(objclss.childof) then
  546. superStrSym:=objcreatestringpoolentry(objclss.childof.objextname^,sp_objcclassnames,sec_objc_class_names)
  547. else
  548. { not empty string, but nil! }
  549. superStrSym:=nil;
  550. { 2) the current class }
  551. classStrSym:=objcreatestringpoolentry(objclss.objextname^,sp_objcclassnames,sec_objc_class_names);
  552. { 3) the isa }
  553. { From Clang: The isa for the meta-class is the root of the hierarchy. }
  554. root:=objclss;
  555. while assigned(root.childof) do
  556. root:=root.childof;
  557. metaisaStrSym:=objcreatestringpoolentry(root.objextname^,sp_objcclassnames,sec_objc_class_names);
  558. { 4) the flags }
  559. { consider every class declared in the implementation section of a unit
  560. as "hidden"
  561. }
  562. hiddenflag:=0;
  563. if (objclss.owner.symtabletype=staticsymtable) and
  564. current_module.is_unit then
  565. hiddenflag:=CLS_HIDDEN;
  566. { class declaration section }
  567. new_section(list,sec_objc_meta_class,'_OBJC_META_CLASS',sizeof(pint));
  568. { 1) meta-class declaration }
  569. metasym:=current_asmdata.DefineAsmSymbol(objclss.rtti_mangledname(objcmetartti),AB_LOCAL,AT_DATA);
  570. list.Concat(tai_symbol.Create(metasym,0));
  571. list.Concat(Tai_const.Create_sym(metaisaStrSym));
  572. { pointer to the superclass name if any, otherwise nil }
  573. if assigned(superstrsym) then
  574. list.Concat(Tai_const.Create_sym(superStrSym))
  575. else
  576. list.concat(tai_const.create_32bit(0));
  577. { pointer to the class name }
  578. list.Concat(Tai_const.Create_sym(classStrSym));
  579. { version is always 0 currently }
  580. list.Concat(Tai_const.Create_32bit(0));
  581. { CLS_META for meta-classes }
  582. list.Concat(Tai_const.Create_32bit(hiddenflag or CLS_META));
  583. { size of the meta-class instance: sizeof(objc_class) + 8 bytes }
  584. list.Concat(Tai_const.Create_32bit(META_INST_SIZE) );
  585. { meta-classes don't have ivars list (=0) }
  586. list.Concat(Tai_const.Create_32bit(0));
  587. { class methods list (stored in "__cls_meth" section) }
  588. if Assigned(mthdlist) then
  589. list.Concat(Tai_const.Create_sym(mthdlist))
  590. else
  591. list.Concat(Tai_const.Create_32bit(0));
  592. { From Clang: cache is always nil }
  593. list.Concat(Tai_const.Create_32bit(0));
  594. { protocols }
  595. ConcatSymOrNil(list, protolistsym);
  596. { From Clang: ivar_layout for meta-class is always NULL. }
  597. list.Concat(Tai_const.Create_32bit(0));
  598. { From Clang: The class extension is always unused for meta-classes. }
  599. list.Concat(Tai_const.Create_32bit(0));
  600. { 2) regular class declaration }
  601. { generate the instance methods list }
  602. gen_objc_methods(list,objclss,mthdlist,false,false);
  603. { generate the instance variables list }
  604. gen_objc_ivars(list,objclss,ivarslist);
  605. new_section(list,sec_objc_class,'_OBJC_CLASS',sizeof(pint));
  606. clssym:=current_asmdata.DefineAsmSymbol(objclss.rtti_mangledname(objcclassrtti),AB_LOCAL,AT_DATA);
  607. list.Concat(tai_symbol.Create(clssym,0));
  608. { for class declaration: the is points to the meta-class declaration }
  609. list.Concat(Tai_const.Create_sym(metasym));
  610. { pointer to the super_class name if any, nil otherwise }
  611. if assigned(superStrSym) then
  612. list.Concat(Tai_const.Create_sym(superStrSym))
  613. else
  614. list.Concat(Tai_const.Create_32bit(0));
  615. { pointer to the class name }
  616. list.Concat(Tai_const.Create_sym(classStrSym));
  617. { version is always 0 currently }
  618. list.Concat(Tai_const.Create_32bit(0));
  619. { CLS_CLASS for classes }
  620. list.Concat(Tai_const.Create_32bit(hiddenflag or CLS_CLASS));
  621. { size of instance: total size of instance variables }
  622. list.Concat(Tai_const.Create_32bit(tobjectsymtable(objclss.symtable).datasize));
  623. { objc_ivar_list (stored in "__instance_vars" section) }
  624. if assigned(ivarslist) then
  625. list.Concat(Tai_const.Create_sym(ivarslist))
  626. else
  627. list.Concat(tai_const.create_32bit(0));
  628. { instance methods list (stored in "__inst_meth" section) }
  629. if Assigned(mthdlist) then
  630. list.Concat(Tai_const.Create_sym(mthdlist))
  631. else
  632. list.Concat(Tai_const.Create_32bit(0));
  633. { From Clang: cache is always NULL }
  634. list.Concat(Tai_const.Create_32bit(0));
  635. { protocols, protolistsym has been created for meta-class, no need to create another one}
  636. ConcatSymOrNil(list, protolistsym);
  637. { TODO: From Clang: strong ivar_layout, necessary for garbage collection support }
  638. list.Concat(Tai_const.Create_32bit(0));
  639. { TODO: From Clang: weak ivar_layout, necessary for garbage collection support }
  640. list.Concat(Tai_const.Create_32bit(0));
  641. classlabel:=clssym;
  642. end;
  643. { Generate the global information sections (objc_symbols and objc_module_info)
  644. for this module. }
  645. procedure tobjcrttiwriter_fragile.gen_objc_info_sections(list: tasmlist);
  646. var
  647. i: longint;
  648. sym : TAsmSymbol;
  649. parent: tobjectdef;
  650. superclasses: tfpobjectlist;
  651. begin
  652. if (classsyms.count<>0) then
  653. begin
  654. new_section(list,sec_objc_symbols,'_OBJC_SYMBOLS',sizeof(pint));
  655. sym := current_asmdata.DefineAsmSymbol(target_asm.labelprefix+'_OBJC_SYMBOLS_$',AB_LOCAL,AT_DATA);
  656. { symbol to refer to this information }
  657. list.Concat(tai_symbol.Create(sym,0));
  658. { ??? (always 0 in Clang) }
  659. list.Concat(Tai_const.Create_pint(0));
  660. { ??? (From Clang: always 0, pointer to some selector) }
  661. list.Concat(Tai_const.Create_pint(0));
  662. { From Clang: number of defined classes }
  663. list.Concat(Tai_const.Create_16bit(classsyms.count));
  664. { From Clang: number of defined categories }
  665. list.Concat(Tai_const.Create_16bit(0));
  666. { first all classes }
  667. for i:=0 to classsyms.count-1 do
  668. list.Concat(Tai_const.Create_sym(tasmsymbol(classsyms[i])));
  669. { then all categories }
  670. end
  671. else
  672. sym:=nil;
  673. new_section(list,sec_objc_module_info,'_OBJC_MODULE_INFO',4);
  674. { version number = 7 (always, both for gcc and clang) }
  675. list.Concat(Tai_const.Create_pint(7));
  676. { sizeof(objc_module): 4 pointer-size entities }
  677. list.Concat(Tai_const.Create_pint(sizeof(pint)*4));
  678. { used to be file name, now unused (points to empty string) }
  679. list.Concat(Tai_const.Create_sym(objcreatestringpoolentry('',sp_objcclassnames,sec_objc_class_names)));
  680. { pointer to classes/categories list declared in this module }
  681. if assigned(sym) then
  682. list.Concat(Tai_const.Create_sym(sym))
  683. else
  684. list.concat(tai_const.create_pint(0));
  685. { Add lazy references to parent classes of all classes defined in this unit }
  686. superclasses:=tfpobjectlist.create(false);
  687. for i:=0 to classdefs.count-1 do
  688. begin
  689. parent:=tobjectdef(classdefs[i]).childof;
  690. { warning: linear search, performance hazard if large number of subclasses }
  691. if assigned(parent) and
  692. (superclasses.indexof(parent)=-1) then
  693. begin
  694. list.concat(tai_directive.create(asd_lazy_reference,'.objc_class_name_'+parent.objextname^));
  695. superclasses.add(parent);
  696. end;
  697. end;
  698. superclasses.free;
  699. { reference symbols for all classes defined in this unit }
  700. for i:=0 to classdefs.count-1 do
  701. list.concat(tai_symbol.Createname_global_value('.objc_class_name_'+tobjectdef(classdefs[i]).objextname^,AT_DATA,0,0));
  702. end;
  703. constructor tobjcrttiwriter_fragile.create;
  704. begin
  705. inherited create(oa_fragile);
  706. end;
  707. {******************************************************************
  708. RTTI generation -- Non-Fragile ABI
  709. *******************************************************************}
  710. (*
  711. From Clang:
  712. /// EmitIvarList - Emit the ivar list for the given
  713. /// implementation. The return value has type
  714. /// IvarListnfABIPtrTy.
  715. /// struct _ivar_t {
  716. /// unsigned long int *offset; // pointer to ivar offset location
  717. /// char *name;
  718. /// char *type;
  719. /// uint32_t alignment;
  720. /// uint32_t size;
  721. /// }
  722. /// struct _ivar_list_t {
  723. /// uint32 entsize; // sizeof(struct _ivar_t)
  724. /// uint32 count;
  725. /// struct _iver_t list[count];
  726. /// }
  727. ///
  728. *)
  729. procedure tobjcrttiwriter_nonfragile.gen_objc_ivars(list: tasmlist; objccls: tobjectdef; out ivarslabel: tasmlabel);
  730. type
  731. ivar_data = record
  732. vf : tfieldvarsym;
  733. namesym : TAsmSymbol;
  734. typesym : TAsmSymbol;
  735. offssym : TAsmSymbol;
  736. end;
  737. var
  738. ivtype: tdef;
  739. vf : tfieldvarsym;
  740. vars : array of ivar_data;
  741. i : integer;
  742. vcnt : integer;
  743. enctype : ansistring;
  744. encerr : tdef;
  745. prefix : shortstring;
  746. vis : TAsmsymbind;
  747. begin
  748. ivarslabel:=nil;
  749. vcnt:=0;
  750. setLength(vars,objccls.symtable.SymList.Count);
  751. for i:=0 to objccls.symtable.SymList.Count-1 do
  752. if tsym(objccls.symtable.SymList[i]).typ=fieldvarsym then
  753. begin
  754. vf:=tfieldvarsym(objccls.symtable.SymList[i]);
  755. if objctryencodetype(vf.vardef,enctype,encerr) then
  756. begin
  757. vars[vcnt].vf:=vf;
  758. vars[vcnt].namesym:=objcreatestringpoolentry(vf.RealName,sp_objcvarnames,sec_objc_meth_var_names);
  759. vars[vcnt].typesym:=objcreatestringpoolentry(enctype,sp_objcvartypes,sec_objc_meth_var_types);
  760. if (vcnt=0) then
  761. begin
  762. new_section(list,sec_objc_const,'_OBJC_IVAR_OFFSETS',sizeof(pint));
  763. prefix:=target_info.cprefix+'OBJC_IVAR_$_'+objccls.objextname^+'.';
  764. end;
  765. { This matches gcc/Clang, but is strange: I would expect private
  766. fields to be local symbols rather than private_extern (which
  767. is "package-global") (JM)
  768. }
  769. if not(vf.visibility in [vis_public,vis_protected,vis_strictprotected]) then
  770. vis:=AB_PRIVATE_EXTERN
  771. else
  772. vis:=AB_GLOBAL;
  773. vars[vcnt].offssym:=current_asmdata.DefineAsmSymbol(prefix+vf.RealName,vis,AT_DATA);
  774. list.concat(tai_symbol.Create_Global(vars[vcnt].offssym,0));
  775. list.concat(tai_const.create_pint(vf.fieldoffset));
  776. inc(vcnt);
  777. end
  778. else
  779. { must be caught during parsing }
  780. internalerror(2009092301);
  781. end;
  782. if vcnt=0 then
  783. exit;
  784. new_section(list,sec_objc_instance_vars,'_OBJC_INSTANCE_VARS',sizeof(pint));
  785. current_asmdata.getlabel(ivarslabel,alt_data);
  786. list.Concat(tai_label.Create(ivarslabel));
  787. { size of each entry -- always 32 bit value }
  788. ivtype:=search_named_unit_globaltype('OBJC','OBJC_IVAR').typedef;
  789. list.concat(tai_const.Create_32bit(ivtype.size));
  790. { number of entries -- always 32 bit value }
  791. list.Concat(tai_const.Create_32bit(vcnt));
  792. for i:=0 to vcnt-1 do
  793. begin
  794. { reference to the offset }
  795. list.Concat(tai_const.Create_sym(vars[i].offssym));
  796. { reference to the instance variable name }
  797. list.Concat(tai_const.Create_sym(vars[i].namesym));
  798. { reference to the encoded type }
  799. list.Concat(tai_const.Create_sym(vars[i].typesym));
  800. { alignment -- always 32 bit value }
  801. list.Concat(tai_const.create_32bit(vars[i].vf.vardef.alignment));
  802. { size -- always 32 bit value }
  803. list.Concat(tai_const.Create_32bit(vars[i].vf.vardef.size));
  804. end;
  805. end;
  806. (*
  807. From Clang:
  808. /// GetOrEmitProtocol - Generate the protocol meta-data:
  809. /// @code
  810. /// struct _protocol_t {
  811. /// id isa; // NULL
  812. /// const char * const protocol_name;
  813. /// const struct _protocol_list_t * protocol_list; // super protocols
  814. /// const struct method_list_t * const instance_methods;
  815. /// const struct method_list_t * const class_methods;
  816. /// const struct method_list_t *optionalInstanceMethods;
  817. /// const struct method_list_t *optionalClassMethods;
  818. /// const struct _prop_list_t * properties;
  819. /// const uint32_t size; // sizeof(struct _protocol_t)
  820. /// const uint32_t flags; // = 0
  821. /// }
  822. /// @endcode
  823. *)
  824. procedure tobjcrttiwriter_nonfragile.gen_objc_protocol(list: tasmlist; protocol: tobjectdef; out protocollabel: tasmsymbol);
  825. var
  826. lbl,
  827. namesym,
  828. listsym : TAsmSymbol;
  829. protolist : TAsmLabel;
  830. proc : tprocdef;
  831. reqinstmlist,
  832. reqclsmlist,
  833. optinstmlist,
  834. optclsmlist : TFPObjectList;
  835. reqinstsym,
  836. reqclssym,
  837. optinstsym,
  838. optclssym : TAsmLabel;
  839. prottype : tdef;
  840. i : Integer;
  841. begin
  842. reqinstmlist:=TFPObjectList.Create(false);
  843. reqclsmlist:=TFPObjectList.Create(false);
  844. optinstmlist:=TFPObjectList.Create(false);
  845. optclsmlist:=TFPObjectList.Create(false);
  846. for i:=0 to protocol.vmtentries.Count-1 do
  847. begin
  848. proc:=pvmtentry(protocol.vmtentries[i])^.procdef;
  849. if (po_classmethod in proc.procoptions) then
  850. if not(po_optional in proc.procoptions) then
  851. reqclsmlist.Add(proc)
  852. else
  853. optclsmlist.Add(proc)
  854. else if not(po_optional in proc.procoptions) then
  855. reqinstmlist.Add(proc)
  856. else
  857. optinstmlist.Add(proc);
  858. end;
  859. if reqinstmlist.Count > 0 then
  860. gen_objc_cat_methods(list,reqinstmlist,sec_objc_cat_inst_meth,'_OBJC_CAT_INST_METH',reqinstsym)
  861. else
  862. reqinstsym:=nil;
  863. if optinstmlist.Count > 0 then
  864. gen_objc_cat_methods(list,optinstmlist,sec_objc_cat_inst_meth,'_OBJC_CAT_INST_METH',optinstsym)
  865. else
  866. optinstsym:=nil;
  867. if reqclsmlist.Count>0 then
  868. gen_objc_cat_methods(list,reqclsmlist,sec_objc_cat_cls_meth,'_OBJC_CAT_CLS_METH',reqclssym)
  869. else
  870. reqclssym:=nil;
  871. if optclsmlist.Count>0 then
  872. gen_objc_cat_methods(list,optclsmlist,sec_objc_cat_cls_meth,'_OBJC_CAT_CLS_METH',optclssym)
  873. else
  874. optclssym:=nil;
  875. reqinstmlist.Free;
  876. reqclsmlist.Free;
  877. optinstmlist.Free;
  878. optclsmlist.Free;
  879. gen_objc_protocol_list(list,protocol.ImplementedInterfaces,protolist);
  880. new_section(list, sec_data_coalesced,'_OBJC_PROTOCOL',sizeof(pint));
  881. { label for the protocol needs to be
  882. a) in a coalesced section (so multiple definitions of the same protocol
  883. can be merged by the linker)
  884. b) private_extern (should only be merged within the same module)
  885. c) weakly defined (so multiple definitions don't cause errors)
  886. }
  887. lbl:=current_asmdata.DefineAsmSymbol(protocol.rtti_mangledname(objcclassrtti),AB_PRIVATE_EXTERN,AT_DATA);
  888. list.Concat(tai_symbol.Create_Global(lbl,0));
  889. protocollabel:=lbl;
  890. { protocol's isa - always nil }
  891. list.Concat(Tai_const.Create_pint(0));
  892. { name }
  893. namesym:=objcreatestringpoolentry(protocol.objextname^,sp_objcclassnames,sec_objc_class_names);
  894. list.Concat(Tai_const.Create_sym(namesym));
  895. { parent protocols list }
  896. ConcatSymOrNil(list,protolist);
  897. { required instance methods }
  898. ConcatSymOrNil(list,reqinstsym);
  899. { required class methods }
  900. ConcatSymOrNil(list,reqclssym);
  901. { optional instance methods }
  902. ConcatSymOrNil(list,optinstsym);
  903. { optional class methods }
  904. ConcatSymOrNil(list,optclssym);
  905. { TODO: properties }
  906. list.Concat(Tai_const.Create_pint(0));
  907. { size of this type }
  908. prottype:=search_named_unit_globaltype('OBJC','OBJC_PROTOCOL').typedef;
  909. list.concat(tai_const.Create_32bit(prottype.size));
  910. { flags }
  911. list.concat(tai_const.Create_32bit(0));
  912. { also add an entry to the __DATA, __objc_protolist section, required to
  913. register the protocol with the runtime }
  914. new_section(list, sec_objc_protolist,'_OBJC_PROTOLIST',sizeof(pint));
  915. listsym:=current_asmdata.DefineAsmSymbol(protocol.rtti_mangledname(objcmetartti),AB_PRIVATE_EXTERN,AT_DATA);
  916. list.Concat(tai_symbol.Create_Global(listsym,0));
  917. list.Concat(tai_const.Create_sym(lbl));
  918. list.Concat(tai_directive.Create(asd_weak_definition,listsym.name));
  919. end;
  920. (*
  921. From Clang:
  922. /// BuildIvarLayout - Builds ivar layout bitmap for the class
  923. /// implementation for the __strong or __weak case.
  924. /// The layout map displays which words in ivar list must be skipped
  925. /// and which must be scanned by GC (see below). String is built of bytes.
  926. /// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
  927. /// of words to skip and right nibble is count of words to scan. So, each
  928. /// nibble represents up to 15 workds to skip or scan. Skipping the rest is
  929. /// represented by a 0x00 byte which also ends the string.
  930. /// 1. when ForStrongLayout is true, following ivars are scanned:
  931. /// - id, Class
  932. /// - object * // note: this "object" means "Objective-C object" (JM)
  933. /// - __strong anything
  934. ///
  935. /// 2. When ForStrongLayout is false, following ivars are scanned:
  936. /// - __weak anything
  937. *)
  938. (*
  939. Only required when supporting garbage collection
  940. procedure tobjcrttiwriter_nonfragile.gen_objc_ivargc_recursive(st: tabstractrecordsymtable; ptrbset: tbitset; startoffset: puint; il: tivarlayouttype);
  941. var
  942. i: longint;
  943. fs: tfieldvarsym;
  944. includelen: longint;
  945. begin
  946. for i:=0 to st.SymList.count-1 do
  947. if (tsym(st.symlist[i]).typ=fieldvarsym) then
  948. begin
  949. fs:=tfieldvarsym(st.symlist[i]);
  950. includelen:=0;
  951. case fs.vardef.typ of
  952. pointerdef,
  953. classrefdef:
  954. if (fs.vardef=objc_idtype) or
  955. (fs.vardef=objc_metaclasstype) then
  956. includelen:=1;
  957. recorddef:
  958. TODO: bitpacking -> offset differences
  959. gen_objc_ivargc_recursive(tabstractrecordsymtable(trecorddef(fs.vardef).symtable),ptrbset,startoffset+fs.fieldoffset,il);
  960. arraydef:
  961. begin
  962. if not is_special_
  963. end;
  964. objectdef :
  965. begin
  966. case tobjectdef(fs.vardef).objecttype of
  967. odt_objcclass,
  968. odt_objcprotocol:
  969. includelen:=1;
  970. odt_object:
  971. gen_objc_ivargc_recursive(tabstractrecordsymtable(tobjectdef(fs.vardef).symtable),ptrbset,startoffset+fs.fieldoffset,il);
  972. end;
  973. end;
  974. end;
  975. end;
  976. end;
  977. function tobjcrttiwriter_nonfragile.gen_objc_ivargcstring(objclss: tobjectdef; il: tivarlayouttype): ansistring;
  978. var
  979. ptrbset: tbitset;
  980. parent: tobjectdef;
  981. size,
  982. startoffset: puint;
  983. i: longint;
  984. begin
  985. size:=tObjectSymtable(objclss.symtable).datasize;
  986. if assigned(objclss.childof) then
  987. startoffset:=tObjectSymtable(objclss.childof.symtable).datasize
  988. else
  989. startoffset:=0;
  990. size:=size-startoffset;
  991. ptrbset:=tbitset.create_bytesize((size+sizeof(ptruint)-1) div sizeof(ptruint));
  992. { has to include info for this class' fields and those of all parent
  993. classes as well
  994. }
  995. parent:=obclss;
  996. repeat
  997. gen_objc_ivargc_recursive(parent.symtable,ptrbset,0,il);
  998. parent:=parent.childof;
  999. until not assigned(parent);
  1000. { convert bits set to encoded string }
  1001. end;
  1002. *)
  1003. (*
  1004. From Clang:
  1005. /// struct _class_ro_t {
  1006. /// uint32_t const flags;
  1007. /// uint32_t const instanceStart;
  1008. /// uint32_t const instanceSize;
  1009. /// uint32_t const reserved; // only when building for 64bit targets
  1010. /// const uint8_t * const ivarLayout;
  1011. /// const char *const name;
  1012. /// const struct _method_list_t * const baseMethods;
  1013. /// const struct _protocol_list_t *const baseProtocols;
  1014. /// const struct _ivar_list_t *const ivars;
  1015. /// const uint8_t * const weakIvarLayout;
  1016. /// const struct _prop_list_t * const properties;
  1017. /// }
  1018. *)
  1019. procedure tobjcrttiwriter_nonfragile.gen_objc_class_ro_part(list: tasmlist; objclss: tobjectdef; protolistsym: TAsmSymbol; out classrolabel: tasmsymbol; metaclass: boolean);
  1020. const
  1021. CLS_CLASS = 0;
  1022. CLS_META = 1;
  1023. CLS_ROOT = 2;
  1024. OBJC2_CLS_HIDDEN = $10;
  1025. CLS_EXCEPTION = $20;
  1026. var
  1027. classStrSym,
  1028. rosym : TAsmSymbol;
  1029. methodslab,
  1030. ivarslab : TAsmLabel;
  1031. class_type : tdef;
  1032. start,
  1033. size,
  1034. flags : cardinal;
  1035. rttitype : trttitype;
  1036. firstfield : tfieldvarsym;
  1037. i : longint;
  1038. begin
  1039. { consider every class declared in the implementation section of a unit
  1040. as "hidden"
  1041. }
  1042. flags:=0;
  1043. if (objclss.owner.symtabletype=staticsymtable) and
  1044. current_module.is_unit then
  1045. flags:=OBJC2_CLS_HIDDEN;
  1046. if metaclass then
  1047. begin
  1048. flags:=flags or CLS_META;
  1049. rttitype:=objcmetarortti;
  1050. { metaclass size/start: always size of objc_object }
  1051. class_type:=search_named_unit_globaltype('OBJC','OBJC_OBJECT').typedef;
  1052. start:=class_type.size;
  1053. size:=start;
  1054. end
  1055. else
  1056. begin
  1057. flags:=flags or CLS_CLASS;
  1058. rttitype:=objcclassrortti;
  1059. size:=tObjectSymtable(objclss.symtable).datasize;
  1060. { can't simply use childof's datasize, because alignment may cause the
  1061. first field to skip a couple of bytes after the previous end }
  1062. firstfield:=nil;
  1063. for i:=0 to objclss.symtable.SymList.Count-1 do
  1064. if (tsym(objclss.symtable.SymList[i]).typ=fieldvarsym) then
  1065. begin
  1066. firstfield:=tfieldvarsym(objclss.symtable.SymList[i]);
  1067. break;
  1068. end;
  1069. if assigned(firstfield) then
  1070. start:=firstfield.fieldoffset
  1071. else
  1072. { no extra fields -> start = size }
  1073. start:=size;
  1074. end;
  1075. if not assigned(objclss.childof) then
  1076. flags:=flags or CLS_ROOT;
  1077. classStrSym:=objcreatestringpoolentry(objclss.objextname^,sp_objcclassnames,sec_objc_class_names);
  1078. { generate methods list }
  1079. gen_objc_methods(list,objclss,methodslab,metaclass,false);
  1080. { generate ivars (nil for metaclass) }
  1081. if metaclass then
  1082. ivarslab:=nil
  1083. else
  1084. gen_objc_ivars(list,objclss,ivarslab);
  1085. { class declaration section }
  1086. new_section(list,sec_objc_const,'_OBJC_META_CLASS',sizeof(pint));
  1087. rosym:=current_asmdata.DefineAsmSymbol(objclss.rtti_mangledname(rttitype),AB_LOCAL,AT_DATA);
  1088. classrolabel:=rosym;
  1089. list.Concat(tai_symbol.create(rosym,0));
  1090. list.Concat(tai_const.Create_32bit(longint(flags)));
  1091. list.Concat(tai_const.Create_32bit(longint(start)));
  1092. list.Concat(tai_const.Create_32bit(longint(size)));
  1093. {$ifdef cpu64bitaddr}
  1094. { alignment }
  1095. list.Concat(tai_const.Create_32bit(0));
  1096. {$endif}
  1097. { TODO: strong ivar layout for garbage collection }
  1098. list.concat(tai_const.Create_pint(0));
  1099. list.concat(tai_const.Create_sym(classStrSym));
  1100. ConcatSymOrNil(list,methodslab);
  1101. ConcatSymOrNil(list,protolistsym);
  1102. ConcatSymOrNil(list,ivarslab);
  1103. { TODO: weak ivar layout for garbage collection }
  1104. list.concat(tai_const.Create_pint(0));
  1105. { TODO: properties }
  1106. list.concat(tai_const.Create_pint(0));
  1107. end;
  1108. (*
  1109. From Clang:
  1110. /// struct _class_t {
  1111. /// struct _class_t *isa;
  1112. /// struct _class_t * const superclass;
  1113. /// void *cache;
  1114. /// IMP *vtable;
  1115. /// struct class_ro_t *ro;
  1116. /// }
  1117. ///
  1118. *)
  1119. { Generate rtti for an Objective-C class and its meta-class. }
  1120. procedure tobjcrttiwriter_nonfragile.gen_objc_classes_sections(list:TAsmList; objclss: tobjectdef; out classlabel: TAsmSymbol);
  1121. var
  1122. root : tobjectdef;
  1123. superSym,
  1124. superMetaSym,
  1125. metaisaSym,
  1126. metasym,
  1127. clssym,
  1128. metarosym,
  1129. rosym : TAsmSymbol;
  1130. protolistsym : TAsmLabel;
  1131. vis : TAsmsymbind;
  1132. begin
  1133. { A) Register necessary names }
  1134. { 1) the current class and metaclass }
  1135. if (objclss.owner.symtabletype=globalsymtable) then
  1136. vis:=AB_GLOBAL
  1137. else
  1138. vis:=AB_PRIVATE_EXTERN;
  1139. clssym:=current_asmdata.DefineAsmSymbol(objclss.rtti_mangledname(objcclassrtti),vis,AT_DATA);
  1140. metasym:=current_asmdata.DefineAsmSymbol(objclss.rtti_mangledname(objcmetartti),vis,AT_DATA);
  1141. { 2) the superclass and meta superclass }
  1142. if assigned(objclss.childof) then
  1143. begin
  1144. superSym:=current_asmdata.RefAsmSymbol(objclss.childof.rtti_mangledname(objcclassrtti));
  1145. superMetaSym:=current_asmdata.RefAsmSymbol(objclss.childof.rtti_mangledname(objcmetartti));
  1146. end
  1147. else
  1148. begin
  1149. superSym:=nil;
  1150. { the class itself }
  1151. superMetaSym:=clssym;
  1152. end;
  1153. { 3) the isa }
  1154. { From Clang: The isa for the meta-class is the root of the hierarchy. }
  1155. root:=objclss;
  1156. while assigned(root.childof) do
  1157. root:=root.childof;
  1158. metaisaSym:=current_asmdata.RefAsmSymbol(root.rtti_mangledname(objcmetartti));
  1159. { 4) the implemented protocols (same for metaclass and regular class) }
  1160. gen_objc_protocol_list(list,objclss.ImplementedInterfaces,protolistsym);
  1161. { 5) the read-only parts of the class definitions }
  1162. gen_objc_class_ro_part(list,objclss,protolistsym,metarosym,true);
  1163. gen_objc_class_ro_part(list,objclss,protolistsym,rosym,false);
  1164. { B) Class declaration section }
  1165. { both class and metaclass are in the objc_data section for obj-c 2 }
  1166. new_section(list,sec_objc_data,'_OBJC_CLASS',sizeof(pint));
  1167. { 1) meta-class declaration }
  1168. list.Concat(tai_symbol.Create_Global(metasym,0));
  1169. { the isa }
  1170. list.Concat(Tai_const.Create_sym(metaisaSym));
  1171. { the superclass }
  1172. list.Concat(Tai_const.Create_sym(superMetaSym));
  1173. { pointer to cache }
  1174. if not assigned(ObjCEmptyCacheVar) then
  1175. ObjCEmptyCacheVar:=current_asmdata.RefAsmSymbol(target_info.Cprefix+'_objc_empty_cache');
  1176. list.Concat(Tai_const.Create_sym(ObjCEmptyCacheVar));
  1177. { pointer to vtable }
  1178. if not assigned(ObjCEmptyVtableVar) then
  1179. ObjCEmptyVtableVar:=current_asmdata.RefAsmSymbol(target_info.Cprefix+'_objc_empty_vtable');
  1180. list.Concat(Tai_const.Create_sym(ObjCEmptyVtableVar));
  1181. { the read-only part }
  1182. list.Concat(Tai_const.Create_sym(metarosym));
  1183. { 2) regular class declaration }
  1184. list.Concat(tai_symbol.Create_Global(clssym,0));
  1185. { the isa }
  1186. list.Concat(Tai_const.Create_sym(metasym));
  1187. { the superclass }
  1188. list.Concat(Tai_const.Create_sym(superSym));
  1189. { pointer to cache }
  1190. list.Concat(Tai_const.Create_sym(ObjCEmptyCacheVar));
  1191. { pointer to vtable }
  1192. list.Concat(Tai_const.Create_sym(ObjCEmptyVtableVar));
  1193. { the read-only part }
  1194. list.Concat(Tai_const.Create_sym(rosym));
  1195. classlabel:=clssym;
  1196. end;
  1197. procedure tobjcrttiwriter_nonfragile.addclasslist(list: tasmlist; section: tasmsectiontype; const symname: string; classes: tfpobjectlist);
  1198. var
  1199. i: longint;
  1200. sym: TAsmSymbol;
  1201. begin
  1202. if classes.count=0 then
  1203. exit;
  1204. new_section(list,section,symname,sizeof(pint));
  1205. sym:=current_asmdata.DefineAsmSymbol(symname,AB_LOCAL,AT_DATA);
  1206. list.concat(tai_symbol.Create(sym,0));
  1207. for i:=0 to classes.count-1 do
  1208. list.concat(tai_const.Create_sym(current_asmdata.RefAsmSymbol(tobjectdef(classes[i]).rtti_mangledname(objcclassrtti))));
  1209. end;
  1210. procedure tobjcrttiwriter_nonfragile.gen_objc_info_sections(list: tasmlist);
  1211. var
  1212. i,j : longint;
  1213. symentry : tsym;
  1214. procdef : tprocdef;
  1215. nonlazyclasses : tfpobjectlist;
  1216. begin
  1217. if (classsyms.count=0) then
  1218. exit;
  1219. { non-lazy classes are all classes that define a class method with the
  1220. selector called "load" (simply inheriting this class method is not enough,
  1221. they have to implement it themselves)
  1222. -- TODO: this currently only works if the Pascal identifier is also 'load'! }
  1223. nonlazyclasses:=tfpobjectlist.create(false);
  1224. for i:=0 to classdefs.count-1 do
  1225. begin
  1226. symentry:=tsym(tobjectsymtable(tobjectdef(classdefs[i]).symtable).find('LOAD'));
  1227. if assigned(symentry) and
  1228. (symentry.typ=procsym) then
  1229. begin
  1230. for j:=0 to tprocsym(symentry).ProcdefList.count do
  1231. begin
  1232. procdef:=tprocdef(tprocsym(symentry).ProcdefList[0]);
  1233. if ((po_classmethod in procdef.procoptions) and
  1234. (procdef.messageinf.str^='load')) then
  1235. begin
  1236. nonlazyclasses.add(classdefs[i]);
  1237. break;
  1238. end;
  1239. end;
  1240. end;
  1241. end;
  1242. { this list has to include all classes, also the non-lazy ones }
  1243. addclasslist(list,sec_objc_classlist,target_asm.labelprefix+'_OBJC_LABEL_CLASS_$',classdefs);
  1244. addclasslist(list,sec_objc_nlclasslist,target_asm.labelprefix+'_OBJC_LABEL_NONLAZY_CLASS_$',nonlazyclasses);
  1245. { TODO: category and non-lazy category lists }
  1246. { the non-fragile abi doesn't have any module info, nor lazy references
  1247. to used classes or to parent classes }
  1248. end;
  1249. constructor tobjcrttiwriter_nonfragile.create;
  1250. begin
  1251. inherited create(oa_nonfragile);
  1252. end;
  1253. {******************************************************************
  1254. RTTI generation -- Main function
  1255. *******************************************************************}
  1256. procedure MaybeGenerateObjectiveCImageInfo(globalst, localst: tsymtable);
  1257. var
  1258. objcrttiwriter: tobjcrttiwriter;
  1259. begin
  1260. if (m_objectivec1 in current_settings.modeswitches) then
  1261. begin
  1262. { first 4 bytes contain version information about this section (currently version 0),
  1263. next 4 bytes contain flags (currently only regarding whether the code in the object
  1264. file supports or requires garbage collection)
  1265. }
  1266. new_section(current_asmdata.asmlists[al_objc_data],sec_objc_image_info,'_OBJC_IMAGE_INFO',sizeof(pint));
  1267. current_asmdata.asmlists[al_objc_data].concat(Tai_symbol.Createname(target_asm.labelprefix+'_OBJC_IMAGE_INFO',AT_LABEL,sizeof(pint)));
  1268. current_asmdata.asmlists[al_objc_data].concat(Tai_const.Create_64bit(0));
  1269. { generate rtti for all obj-c classes, protocols and categories (todo)
  1270. defined in this module. }
  1271. if not(target_info.system in system_objc_nfabi) then
  1272. objcrttiwriter:=tobjcrttiwriter_fragile.create
  1273. else
  1274. objcrttiwriter:=tobjcrttiwriter_nonfragile.create;
  1275. objcrttiwriter.gen_objc_rtti_sections(current_asmdata.asmlists[al_objc_data],globalst);
  1276. objcrttiwriter.gen_objc_rtti_sections(current_asmdata.asmlists[al_objc_data],localst);
  1277. objcrttiwriter.gen_objc_info_sections(current_asmdata.asmlists[al_objc_data]);
  1278. objcrttiwriter.free;
  1279. end;
  1280. end;
  1281. end.