objcgutl.pas 68 KB

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