objcgutl.pas 56 KB

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