objcgutl.pas 57 KB

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