objcgutl.pas 17 KB

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