objcgutl.pas 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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;
  25. procedure objcfinishstringrefpoolentry(entry: phashsetitem; stringpool: tconstpooltype; refsec, stringsec: tasmsectiontype);
  26. procedure MaybeGenerateObjectiveCImageInfo(globalst, localst: tsymtable);
  27. implementation
  28. uses
  29. globtype,globals,
  30. systems,
  31. aasmtai,
  32. cgbase,cgutils,
  33. objcutil,
  34. symconst,symtype,symsym,symdef,symtable,
  35. verbose;
  36. {******************************************************************
  37. String section helpers
  38. *******************************************************************}
  39. function objcreatestringpoolentryintern(p: pchar; len: longint; pooltype: tconstpooltype; stringsec: tasmsectiontype): TAsmSymbol;
  40. var
  41. entry : PHashSetItem;
  42. strlab : tasmlabel;
  43. pc : pchar;
  44. pool : THashSet;
  45. begin
  46. if current_asmdata.ConstPools[pooltype]=nil then
  47. current_asmdata.ConstPools[pooltype]:=THashSet.Create(64, True, False);
  48. pool := current_asmdata.constpools[pooltype];
  49. entry:=pool.FindOrAdd(p,len);
  50. if not assigned(entry^.data) then
  51. begin
  52. { create new entry }
  53. current_asmdata.getlabel(strlab,alt_data);
  54. entry^.Data:=strlab;
  55. getmem(pc,entry^.keylength+1);
  56. move(entry^.key^,pc^,entry^.keylength);
  57. pc[entry^.keylength]:=#0;
  58. { add the string to the approriate section }
  59. new_section(current_asmdata.asmlists[al_objc_pools],stringsec,strlab.name,sizeof(pint));
  60. current_asmdata.asmlists[al_objc_pools].concat(Tai_label.Create(strlab));
  61. current_asmdata.asmlists[al_objc_pools].concat(Tai_string.Create_pchar(pc,entry^.keylength+1));
  62. Result := strlab;
  63. end
  64. else
  65. Result := TAsmLabel(Entry^.Data);
  66. end;
  67. procedure objcfinishstringrefpoolentry(entry: phashsetitem; stringpool: tconstpooltype; refsec, stringsec: tasmsectiontype);
  68. var
  69. reflab : tasmlabel;
  70. strlab : tasmsymbol;
  71. classname: string;
  72. begin
  73. { have we already generated a reference for this string entry? }
  74. if not assigned(entry^.Data) then
  75. begin
  76. { no, add the string to the associated strings section }
  77. strlab:=objcreatestringpoolentryintern(pchar(entry^.key),entry^.keylength,stringpool,stringsec);
  78. { and now finish the reference }
  79. current_asmdata.getlabel(reflab,alt_data);
  80. entry^.Data:=reflab;
  81. { add a pointer to the string in the string references section }
  82. new_section(current_asmdata.asmlists[al_objc_pools],refsec,reflab.name,sizeof(pint));
  83. current_asmdata.asmlists[al_objc_pools].concat(Tai_label.Create(reflab));
  84. current_asmdata.asmlists[al_objc_pools].concat(Tai_const.Create_sym(strlab));
  85. { in case of a class reference, also add a lazy symbol reference for
  86. the class (the linker requires this). }
  87. if (refsec=sec_objc_cls_refs) then
  88. begin
  89. setlength(classname,entry^.keylength);
  90. move(entry^.key^,classname[1],entry^.keylength);
  91. current_asmdata.asmlists[al_objc_pools].concat(tai_directive.Create(asd_lazy_reference,'.objc_class_name_'+classname));
  92. end;
  93. end;
  94. end;
  95. function objcreatestringpoolentry(const s: string; pooltype: tconstpooltype; stringsec: tasmsectiontype): TAsmSymbol;
  96. begin
  97. result:=objcreatestringpoolentryintern(@s[1],length(s),pooltype,stringsec);
  98. end;
  99. {******************************************************************
  100. RTTI generation
  101. *******************************************************************}
  102. { generate a method list, either of class methods or of instance methods,
  103. and both for obj-c classes and categories. }
  104. procedure gen_objc1_methods(list: tasmlist; objccls: tobjectdef; out methodslabel: tasmlabel; classmethods, iscategory: Boolean);
  105. const
  106. clsSectType : array [Boolean] of tasmsectiontype = (sec_objc_inst_meth, sec_objc_cls_meth);
  107. clsSectName : array [Boolean] of string = ('_OBJC_INST_METH','_OBJC_CLS_METH');
  108. catSectType : array [Boolean] of tasmsectiontype = (sec_objc_cat_inst_meth, sec_objc_cat_cls_meth);
  109. catSectName : array [Boolean] of string = ('_OBJC_CAT_INST_METH','_OBJC_CAT_CLS_METH');
  110. type
  111. method_data = record
  112. def : tprocdef;
  113. selsym : TAsmSymbol;
  114. encsym : TAsmSymbol;
  115. end;
  116. var
  117. i : Integer;
  118. def : tprocdef;
  119. defs : array of method_data;
  120. mcnt : integer;
  121. sym : tasmsymbol;
  122. begin
  123. methodslabel:=nil;
  124. mcnt:=0;
  125. { collect all instance/class methods }
  126. SetLength(defs,objccls.vmtentries.count);
  127. for i:=0 to objccls.vmtentries.count-1 do
  128. begin
  129. def:=pvmtentry(objccls.vmtentries[i])^.procdef;
  130. if Assigned(def.procstarttai) and
  131. (classmethods = (po_classmethod in def.procoptions)) then
  132. begin
  133. defs[mcnt].def:=def;
  134. defs[mcnt].selsym:=objcreatestringpoolentry(def.messageinf.str^,sp_objcvarnames,sec_objc_meth_var_names);
  135. defs[mcnt].encsym:=objcreatestringpoolentry(objcencodemethod(def),sp_objcvartypes,sec_objc_meth_var_types);
  136. inc(mcnt);
  137. end;
  138. end;
  139. if mcnt=0 then
  140. exit;
  141. if iscategory then
  142. new_section(list,clsSectType[classmethods],clsSectName[classmethods],4)
  143. else
  144. new_section(list,catSectType[classmethods],catSectName[classmethods],4);
  145. current_asmdata.getlabel(methodslabel,alt_data);
  146. list.Concat(tai_label.Create(methodslabel));
  147. { not used, always zero }
  148. list.Concat(tai_const.Create_32bit(0));
  149. { number of objc_method entries in the method_list array }
  150. list.Concat(tai_const.Create_32bit(mcnt));
  151. for i := 0 to mcnt - 1 do
  152. begin
  153. { reference to the selector name }
  154. list.Concat(tai_const.Create_sym(defs[i].selsym));
  155. { reference to the obj-c encoded function parameters (signature) }
  156. list.Concat(tai_const.Create_sym(defs[i].encsym));
  157. { mangled name of the method }
  158. sym:=current_asmdata.GetAsmSymbol(defs[i].def.mangledname);
  159. if not assigned(sym) then
  160. internalerror(2009091601);
  161. list.Concat(tai_const.Create_sym(sym));
  162. end;
  163. end;
  164. { generate an instance variables list for an obj-c class. }
  165. procedure gen_objc1_ivars(list: TAsmList; objccls: tobjectdef; out ivarslabel: TAsmLabel);
  166. type
  167. ivar_data = record
  168. vf : tfieldvarsym;
  169. namesym : TAsmSymbol;
  170. typesym : TAsmSymbol;
  171. end;
  172. var
  173. i : integer;
  174. vf : tfieldvarsym;
  175. vars : array of ivar_data;
  176. vcnt : Integer;
  177. enctype : ansistring;
  178. encerr : tdef;
  179. begin
  180. ivarslabel:=nil;
  181. vcnt:=0;
  182. setLength(vars,objccls.symtable.SymList.Count);
  183. for i:=0 to objccls.symtable.SymList.Count-1 do
  184. if tsym(objccls.symtable.SymList[i]).typ=fieldvarsym then
  185. begin
  186. vf:=tfieldvarsym(objccls.symtable.SymList[i]);
  187. if objctryencodetype(vf.vardef,enctype,encerr) then
  188. begin
  189. vars[vcnt].vf:=vf;
  190. vars[vcnt].namesym:=objcreatestringpoolentry(vf.RealName,sp_objcvarnames,sec_objc_meth_var_names);
  191. vars[vcnt].typesym:=objcreatestringpoolentry(enctype,sp_objcvartypes,sec_objc_meth_var_types);
  192. inc(vcnt);
  193. end
  194. else
  195. { must be caught during parsing }
  196. internalerror(2009090601);
  197. end;
  198. if vcnt=0 then
  199. exit;
  200. new_section(list,sec_objc_instance_vars,'_OBJC_INSTANCE_VARS',sizeof(pint));
  201. current_asmdata.getlabel(ivarslabel,alt_data);
  202. list.Concat(tai_label.Create(ivarslabel));
  203. { objc_ivar_list: first the number of elements }
  204. list.Concat(tai_const.Create_32bit(vcnt));
  205. for i:=0 to vcnt-1 do
  206. begin
  207. { reference to the instance variable name }
  208. list.Concat(tai_const.Create_sym(vars[i].namesym));
  209. { reference to the encoded type }
  210. list.Concat(tai_const.Create_sym(vars[i].typesym));
  211. { and the offset of the field }
  212. list.Concat(tai_const.Create_32bit(vars[i].vf.fieldoffset));
  213. end;
  214. end;
  215. (*
  216. From Clang:
  217. struct _objc_class {
  218. Class isa;
  219. Class super_class;
  220. const char *name;
  221. long version;
  222. long info;
  223. long instance_size;
  224. struct _objc_ivar_list *ivars;
  225. struct _objc_method_list *methods;
  226. struct _objc_cache *cache;
  227. struct _objc_protocol_list *protocols;
  228. // Objective-C 1.0 extensions (<rdr://4585769>) -- for garbage collection
  229. const char *ivar_layout;
  230. struct _objc_class_ext *ext;
  231. };
  232. *)
  233. { Generate rtti for an Objective-C class and its meta-class. }
  234. procedure gen_objc1_classes_sections(list:TAsmList; objclss: tobjectdef; out classlabel: TAsmSymbol);
  235. const
  236. CLS_CLASS = 1;
  237. CLS_META = 2;
  238. META_INST_SIZE = 40+8; // sizeof(objc_class) + 8
  239. var
  240. root : tobjectdef;
  241. superStrSym,
  242. classStrSym,
  243. metaisaStrSym,
  244. metasym,
  245. clssym : TAsmSymbol;
  246. mthdlist,
  247. ivarslist : TAsmLabel;
  248. begin
  249. { generate the class methods list }
  250. gen_objc1_methods(list,objclss,mthdlist,true,false);
  251. { register necessary names }
  252. { 1) the superclass }
  253. if assigned(objclss.childof) then
  254. superStrSym:=objcreatestringpoolentry(objclss.childof.objextname^,sp_objcclassnames,sec_objc_class_names)
  255. else
  256. { not empty string, but nil! }
  257. superStrSym:=nil;
  258. { 2) the current class }
  259. classStrSym:=objcreatestringpoolentry(objclss.objextname^,sp_objcclassnames,sec_objc_class_names);
  260. { 3) the isa }
  261. { From Clang: The isa for the meta-class is the root of the hierarchy. }
  262. root:=objclss;
  263. while assigned(root.childof) do
  264. root:=root.childof;
  265. metaisaStrSym:=objcreatestringpoolentry(root.objextname^,sp_objcclassnames,sec_objc_class_names);
  266. { class declaration section }
  267. new_section(list,sec_objc_meta_class,'_OBJC_META_CLASS',sizeof(pint));
  268. { 1) meta-class declaration (warning: if name changed, also change tclassrefdef.rtti_mangledname) }
  269. metasym:=current_asmdata.DefineAsmSymbol(target_asm.labelprefix+'_OBJC_METACLASS_'+objclss.objextname^,AB_LOCAL,AT_DATA);
  270. list.Concat(tai_symbol.Create(metasym,0));
  271. list.Concat(Tai_const.Create_sym(metaisaStrSym));
  272. { pointer to the superclass name if any, otherwise nil }
  273. if assigned(superstrsym) then
  274. list.Concat(Tai_const.Create_sym(superStrSym))
  275. else
  276. list.concat(tai_const.create_32bit(0));
  277. { pointer to the class name }
  278. list.Concat(Tai_const.Create_sym(classStrSym));
  279. { version is always 0 currently }
  280. list.Concat(Tai_const.Create_32bit(0));
  281. { CLS_META for meta-classes }
  282. list.Concat(Tai_const.Create_32bit(CLS_META));
  283. { size of the meta-class instance: sizeof(objc_class) + 8 bytes }
  284. list.Concat(Tai_const.Create_32bit(META_INST_SIZE) );
  285. { meta-classes don't have ivars list (=0) }
  286. list.Concat(Tai_const.Create_32bit(0));
  287. { class methods list (stored in "__cls_meth" section) }
  288. if Assigned(mthdlist) then
  289. list.Concat(Tai_const.Create_sym(mthdlist))
  290. else
  291. list.Concat(Tai_const.Create_32bit(0));
  292. { From Clang: cache is always nil }
  293. list.Concat(Tai_const.Create_32bit(0));
  294. { TODO: protocols }
  295. list.Concat(Tai_const.Create_32bit(0));
  296. { From Clang: ivar_layout for meta-class is always NULL. }
  297. list.Concat(Tai_const.Create_32bit(0));
  298. { From Clang: The class extension is always unused for meta-classes. }
  299. list.Concat(Tai_const.Create_32bit(0));
  300. { 2) regular class declaration }
  301. { generate the instance methods list }
  302. gen_objc1_methods(list,objclss,mthdlist,false,false);
  303. { generate the instance variables list }
  304. gen_objc1_ivars(list,objclss,ivarslist);
  305. new_section(list,sec_objc_class,'_OBJC_CLASS',sizeof(pint));
  306. clssym:=current_asmdata.DefineAsmSymbol(objclss.rtti_mangledname(fullrtti),AB_LOCAL,AT_DATA);
  307. list.Concat(tai_symbol.Create(clssym,0));
  308. { for class declaration: the is points to the meta-class declaration }
  309. list.Concat(Tai_const.Create_sym(metasym));
  310. { pointer to the super_class name if any, nil otherwise }
  311. if assigned(superStrSym) then
  312. list.Concat(Tai_const.Create_sym(superStrSym))
  313. else
  314. list.Concat(Tai_const.Create_32bit(0));
  315. { pointer to the class name }
  316. list.Concat(Tai_const.Create_sym(classStrSym));
  317. { version is always 0 currently }
  318. list.Concat(Tai_const.Create_32bit(0));
  319. { CLS_CLASS for classes }
  320. list.Concat(Tai_const.Create_32bit(CLS_CLASS));
  321. { size of instance: total size of instance variables }
  322. list.Concat(Tai_const.Create_32bit(tobjectsymtable(objclss.symtable).datasize));
  323. { objc_ivar_list (stored in "__instance_vars" section) }
  324. if assigned(ivarslist) then
  325. list.Concat(Tai_const.Create_sym(ivarslist))
  326. else
  327. list.Concat(tai_const.create_32bit(0));
  328. { instance methods list (stored in "__inst_meth" section) }
  329. if Assigned(mthdlist) then
  330. list.Concat(Tai_const.Create_sym(mthdlist))
  331. else
  332. list.Concat(Tai_const.Create_32bit(0));
  333. { From Clang: cache is always NULL }
  334. list.Concat(Tai_const.Create_32bit(0));
  335. { TODO: protocols }
  336. list.Concat(Tai_const.Create_32bit(0));
  337. { TODO: From Clang: strong ivar_layout, necessary for garbage collection support }
  338. list.Concat(Tai_const.Create_32bit(0));
  339. { TODO: From Clang: weak ivar_layout, necessary for garbage collection support }
  340. list.Concat(Tai_const.Create_32bit(0));
  341. classlabel:=clssym;
  342. end;
  343. { Generate the rtti sections for all obj-c classes defined in st, and return
  344. these classes in the classes list. }
  345. procedure gen_objc1_rtti_sections(list:TAsmList; st:TSymtable; var classsyms, classdefs: tfpobjectlist);
  346. var
  347. i: longint;
  348. def: tdef;
  349. sym : TAsmSymbol;
  350. begin
  351. if not Assigned(st) then
  352. exit;
  353. for i:=0 to st.DefList.Count-1 do
  354. begin
  355. def:=tdef(st.DefList[i]);
  356. if is_objcclass(def) and
  357. not(oo_is_external in tobjectdef(def).objectoptions) then
  358. begin
  359. gen_objc1_classes_sections(list,tobjectdef(def),sym);
  360. classsyms.add(sym);
  361. classdefs.add(def);
  362. end;
  363. end;
  364. end;
  365. { Generate the global information sections (objc_symbols and objc_module_info)
  366. for this module. }
  367. procedure gen_objc1_info_sections(list: tasmlist; classsyms,classdefs: tfpobjectlist);
  368. var
  369. i: longint;
  370. sym : TAsmSymbol;
  371. parent: tobjectdef;
  372. superclasses: tfpobjectlist;
  373. begin
  374. if (classsyms.count<>0) then
  375. begin
  376. new_section(list,sec_objc_symbols,'_OBJC_SYMBOLS',sizeof(pint));
  377. sym := current_asmdata.RefAsmSymbol(target_asm.labelprefix+'_OBJC_SYMBOLS');
  378. { symbol to refer to this information }
  379. list.Concat(tai_symbol.Create(sym,0));
  380. { ??? (always 0 in Clang) }
  381. list.Concat(Tai_const.Create_pint(0));
  382. { ??? (From Clang: always 0, pointer to some selector) }
  383. list.Concat(Tai_const.Create_pint(0));
  384. { From Clang: number of defined classes }
  385. list.Concat(Tai_const.Create_16bit(classsyms.count));
  386. { From Clang: number of defined categories }
  387. list.Concat(Tai_const.Create_16bit(0));
  388. { first all classes }
  389. for i:=0 to classsyms.count-1 do
  390. list.Concat(Tai_const.Create_sym(tasmsymbol(classsyms[i])));
  391. { then all categories }
  392. end
  393. else
  394. sym:=nil;
  395. new_section(list,sec_objc_module_info,'_OBJC_MODULE_INFO',4);
  396. { version number = 7 (always, both for gcc and clang, regardless of objc-1 or 2 }
  397. list.Concat(Tai_const.Create_pint(7));
  398. { sizeof(objc_module): 4 pointer-size entities }
  399. list.Concat(Tai_const.Create_pint(sizeof(pint)*4));
  400. { used to be file name, now unused (points to empty string) }
  401. list.Concat(Tai_const.Create_sym(objcreatestringpoolentry('',sp_objcclassnames,sec_objc_class_names)));
  402. { pointer to classes/categories list declared in this module }
  403. if assigned(sym) then
  404. list.Concat(Tai_const.Create_sym(sym))
  405. else
  406. list.concat(tai_const.create_pint(0));
  407. { Add lazy references to parent classes of all classes defined in this unit }
  408. superclasses:=tfpobjectlist.create(false);
  409. for i:=0 to classdefs.count-1 do
  410. begin
  411. parent:=tobjectdef(classdefs[i]).childof;
  412. if assigned(parent) and
  413. (superclasses.indexof(parent)=-1) then
  414. begin
  415. list.concat(tai_directive.create(asd_lazy_reference,'.objc_class_name_'+parent.objextname^));
  416. superclasses.add(parent);
  417. end;
  418. end;
  419. superclasses.free;
  420. { reference symbols for all classes declaredin this unit }
  421. for i:=0 to classdefs.count-1 do
  422. list.concat(tai_symbol.Createname_global_value('.objc_class_name_'+tobjectdef(classdefs[i]).objextname^,AT_DATA,0,0));
  423. end;
  424. procedure MaybeGenerateObjectiveCImageInfo(globalst, localst: tsymtable);
  425. var
  426. classsyms,
  427. classdefs: tfpobjectlist;
  428. begin
  429. if (m_objectivec1 in current_settings.modeswitches) then
  430. begin
  431. { first 4 bytes contain version information about this section (currently version 0),
  432. next 4 bytes contain flags (currently only regarding whether the code in the object
  433. file supports or requires garbage collection)
  434. }
  435. new_section(current_asmdata.asmlists[al_objc_data],sec_objc_image_info,'_OBJC_IMAGE_INFO',sizeof(pint));
  436. current_asmdata.asmlists[al_objc_data].concat(Tai_symbol.Createname(target_asm.labelprefix+'_OBJC_IMAGE_INFO',AT_LABEL,sizeof(pint)));
  437. current_asmdata.asmlists[al_objc_data].concat(Tai_const.Create_64bit(0));
  438. { generate rtti for all obj-c classes, protocols (todo) and categories (todo)
  439. defined in this module. }
  440. classsyms:=tfpobjectlist.create(false);
  441. classdefs:=tfpobjectlist.create(false);
  442. gen_objc1_rtti_sections(current_asmdata.asmlists[al_objc_data],globalst,classsyms,classdefs);
  443. gen_objc1_rtti_sections(current_asmdata.asmlists[al_objc_data],localst,classsyms,classdefs);
  444. gen_objc1_info_sections(current_asmdata.asmlists[al_objc_data],classsyms,classdefs);
  445. classsyms.free;
  446. classdefs.free;
  447. end;
  448. end;
  449. end.