objcgutl.pas 69 KB

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