chiark / gitweb /
Add a new class slot `align', holding the instance layout alignment.
[sod] / doc / structures.tex
1 %%% -*-latex-*-
2 %%%
3 %%% In-depth exploration of the generated structures
4 %%%
5 %%% (c) 2015 Straylight/Edgeware
6 %%%
7
8 %%%----- Licensing notice ---------------------------------------------------
9 %%%
10 %%% This file is part of the Simple Object Definition system.
11 %%%
12 %%% SOD is free software; you can redistribute it and/or modify
13 %%% it under the terms of the GNU General Public License as published by
14 %%% the Free Software Foundation; either version 2 of the License, or
15 %%% (at your option) any later version.
16 %%%
17 %%% SOD is distributed in the hope that it will be useful,
18 %%% but WITHOUT ANY WARRANTY; without even the implied warranty of
19 %%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 %%% GNU General Public License for more details.
21 %%%
22 %%% You should have received a copy of the GNU General Public License
23 %%% along with SOD; if not, write to the Free Software Foundation,
24 %%% Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25
26 \chapter{Object structures} \label{ch:structures}
27
28 This chapter describes the structure and layout of standard Sod objects,
29 classes and associated metadata.  Note that Sod's object system is very
30 flexible and it's possible for an extension to define a new root class which
31 works very differently from the standard @|SodObject| described here.
32
33 The concrete types described in \xref{sec:structures.common} and
34 \ref{sec:structures.root} are declared by the header file @|<sod/sod.h>|.
35 The definitions described in sections \ref{sec:structures.layout} are defined
36 in the header file generated by the containing module.
37
38 %%%--------------------------------------------------------------------------
39 \section{Common instance structure} \label{sec:structures.common}
40
41 As described below, a pointer to an instance actually points to an
42 \emph{instance chain} structure within the instances overall layout
43 structure.
44
45 Instance chains contain slots and vtable pointers, as described below.  All
46 instances have the basic structure of a @|struct sod_instance|.
47
48 \begin{describe}[struct sod_instance]{type}
49     {struct sod_instance \{ \\ \ind
50        const struct sod_vtable *_vt; \- \\
51      \};}
52
53   The basic structure of all instances.  Members are as follows.
54   \begin{description} \let\makelabel\code
55   \item[_vt] A pointer to a \emph{vtable}, which has the basic structure of a
56     @|struct sod_vtable|, described below.
57   \end{description}
58 \end{describe}
59
60 \begin{describe}[struct sod_vtable]{type}
61     {struct sod_vtable \{ \\ \ind
62        const SodClass *_class; \\
63        size_t _base; \- \\
64      \};}
65
66   A vtable contains static metadata needed for efficient conversions and
67   message dispatch, and pointers to the instance's class.  Each chain points
68   to a different vtable.  All vtables have the basic structure of a @|struct
69   sod_vtable|, which has the following members.
70   \begin{description} \let\makelabel\code
71   \item[_class] A pointer to the instance's class object.
72   \item[_base] The offset of this chain structure above the start of the
73     overall instance layout, in bytes.  Subtracting @|_base| from the
74     instance chain pointer finds the layout base address.
75   \end{description}
76 \end{describe}
77
78 %%%--------------------------------------------------------------------------
79 \section{Built-in root objects} \label{sec:structures.root}
80
81 This section describes the built-in classes @|SodObject| and @|SodClass|,
82 which are the standard roots of the inheritance and metaclass graphs
83 respectively.  Specifically, @|SodObject| has no direct superclasses, and
84 @|SodClass| is its own metaclass.  It is not possible to define root classes
85 in module files because of circularities: @|SodObject| has @|SodClass| as its
86 metaclass, and @|SodClass| is a subclass of @|SodObject|.  Extensions can
87 define additional root classes, but this is tricky, and not really to be
88 recommended.
89
90
91 \subsection{The SodObject class} \label{sec:structures.root.sodobject}
92
93 \begin{figure}[tbp]
94   \begin{tabular}{p{10pt}p{10pt}}
95     \begin{prog}
96       struct SodObject__ilayout \{ \\ \ind
97         union \{ \\ \ind
98           struct SodObject__ichain_obj \{ \\ \ind
99             const struct SodObject__vt_obj *_vt; \- \\
100           \} obj; \- \\
101         \} obj; \- \\
102       \};
103     \end{prog}
104     &
105     \begin{prog}
106       struct SodObject__vt_obj \{ \\ \ind
107         const SodClass *_class; \\
108         size_t _base; \- \\
109       \};
110     \end{prog} \\
111   \end{tabular}
112   \caption{Instance and vtable layout of @|SodObject|}
113   \label{fig:structures.root.sodobject}
114 \end{figure}
115
116 \begin{describe}[SodObject]{cls}
117     {[nick = obj, metaclass = SodClass, lisp_metaclass = sod_class] \\
118      class SodObject \{ \}}
119
120   The @|SodObject| class defines no slots or messages.  Because @|SodObject|
121   has no direct superclasses, there is only one chain, and no inherited
122   slots or messages, so the single chain contains only a vtable pointer.
123
124   Since there are no messages, and @|SodClass| also has only one chain, the
125   vtable contains only the standard class pointer and offset-to-base members.
126   In a direct instance of @|SodObject| (why would you want one?)  the class
127   pointer contains the address of @|SodObject__class| and the offset is zero.
128
129   The instance and vtable layout of @|SodObject| is shown in
130   \xref{fig:structures.root.sodobject}.
131 \end{describe}
132
133
134 \subsection{The SodClass class} \label{sec:structures.root.sodclass}
135
136 \begin{describe}[SodClass]{cls}
137     {[nick = cls, link = SodObject] \\
138      class SodClass : SodObject \{ \\ \ind
139        const char *name; \\
140        const char *nick; \\
141        size_t initsz; \\
142        size_t align; \\
143        void *(*imprint)(void *@<p>); \\
144        void *(*init)(void *@<p>); \\
145        size_t n_supers; \\
146        const SodClass *const *supers; \\
147        size_t n_cpl; \\
148        const SodClass *const *cpl; \\
149        const SodClass *link; \\
150        const SodClass *head; \\
151        size_t level; \\
152        size_t n_chains; \\
153        const struct sod_chain *chains; \\
154        size_t off_islots; \\
155        size_t islotsz; \- \\
156      \}}
157
158   The @|SodClass| class defines no messages, but there are a number of slots.
159   Its only direct superclass is @|SodObject| and so (like its superclass) its
160   vtable is trivial.
161
162   The slots defined are as follows.
163   \begin{description} \let\makelabel\code
164
165   \item[name] A pointer to the class's name.
166
167   \item[nick] A pointer to the class's nickname.
168
169   \item[initsz] The size in bytes required to store an instance of the class.
170
171   \item[align] A sufficient alignment for the class's instance storage.
172
173   \item[imprint] A pointer to a function: given a pointer @<p> to at least
174     @<initsz> bytes of appropriately aligned memory, `imprint' this memory it
175     so that it becomes a minimally functional instance of the class: all of
176     the vtable and class pointers are properly initialized, but the slots are
177     left untouched.  The function returns its argument @<p>.
178
179   \item[init] A pointer to a function: given a pointer @<p> to at least
180     @<initsz> bytes of appropriately aligned memory, initialize an instance
181     of the class in it: all of the vtable and class pointers are initialized,
182     as are slots for which initializers are defined.  Other slots are left
183     untouched.  The function returns its argument @<p>.
184
185   \item[n_supers] The number of direct superclasses.  (This is zero exactly
186     in the case of @|SodObject|.)
187
188   \item[supers] A pointer to an array of @<n_supers> pointers to class
189     objects listing the class's direct superclasses, in the order in which
190     they were listed in the class definition.  If @<n_supers> is zero, then
191     this pointer is null.
192
193   \item[n_cpl] The number of superclasses in the class's class precedence
194     list.
195
196   \item[cpl] A pointer to an array of pointers to class objects listing all
197     of the class's superclasses, from most- to least-specific, starting with
198     the class itself, so $@|$c$@->cls.cpl[0]| = c$ for all class objects
199     $c$.
200
201   \item[link] If the class is a chain head, then this is a null pointer;
202     otherwise it points to the class's distinguished link superclass (which
203     might or might not be a direct superclass).
204
205   \item[head] A pointer to the least-specific class in this class's chain; so
206     @|$c$@->cls.head@->cls.link| is always null, and either @|$c$@->cls.link|
207     is null (in which case $@|$c$@->cls.head| = c$) or $@|$c$@->cls.head| =
208     @|$c$@->cls.link@->cls.head|$.
209
210   \item[level] The number of less specific superclasses in this class's
211     chain.  If @|$c$@->cls.link| is null then @|$c$@->cls.level| is zero;
212     otherwise $@|$c$@->cls.level| = @|$c$@->cls.link@->cls.level| + 1$.
213
214   \item[n_chains] The number of chains formed by the class's superclasses.
215
216   \item[chains] A pointer to an array of @|struct sod_chain| structures (see
217     below) describing the class's superclass chains, in decreasing order of
218     specificity of their most specific classes.  It is always the case that
219     $@|$c$@->cls.chains[0].classes[$c$@->cls.level]| = c$.
220
221   \item[off_islots] The offset of the class's @|islots| structure relative to
222     its containing @|ichain| structure.  The class doesn't define any slots
223     if and only if this is zero.  (The offset can't be zero because the
224     vtable pointer is at offset zero.)
225
226   \item[islotsz] The size required to store the class's direct slots, i.e.,
227     the size of its @|islots| structure.  The class doesn't define any slots
228     if and only if this is zero.
229
230   \end{description}
231 \end{describe}
232
233 \begin{describe}[struct sod_chain]{type}
234     {struct sod_chain \{ \\ \ind
235        size_t n_classes; \\
236        const SodClass *const *classes; \\
237        size_t off_ichain; \\
238        const struct sod_vtable *vt; \\
239        size_t ichainsz; \- \\
240      \};}
241
242   The @|struct sod_chain| structure describes an individual chain of
243   superclasses.  It has the following members.
244   \begin{description} \let\makelabel\code
245
246   \item[n_classes] The number of classes in the chain.  This is always at
247     least one.
248
249   \item[classes] A pointer to an array of class pointers listing the classes
250     in the chain from least- to most-specific.  So
251     $@|@<classes>[$i$]@->cls.head| = @|@<classes>[0]|$ for all $0 \le i <
252     @<n_classes>$, @|@<classes>[0]@->cls.link| is always null, and
253     $@|@<classes>[$i$]@->cls.link| = @|@<classes>[$i - 1$]|$ if $1 \le i <
254     @<n_classes>$.
255
256   \item[off_ichain] The size of the @|ichain| structure for this chain.
257
258   \item[vt] The vtable for this chain.  (It is possible, therefore, to
259     partially duplicate the behaviour of the @<imprint> function by walking
260     the chain structure.\footnote{%
261       There isn't enough information readily available to fill in the class
262       pointers correctly.} %
263     The @<imprint> function is much faster, though.)
264
265   \item[ichainsz] The size of the @|ichain| structure for this chain.
266
267   \end{description}
268 \end{describe}
269
270 %%%--------------------------------------------------------------------------
271 \section{Class and vtable layout} \label{sec:structures.layout}
272
273 The layout algorithms for Sod instances and vtables are nontrivial.  They are
274 defined here in full detail, since they're effectively fixed by Sod's ABI
275 compatibility guarantees, so they might as well be documented for the sake of
276 interoperating programs.
277
278 Unfortunately, the descriptions are rather complicated, and, for the most
279 part not necessary to a working understanding of Sod.  The skeleton structure
280 definitions shown should be more than enough for readers attempting to make
281 sense of the generated headers and tables.
282
283 In the description that follows, uppercase letters vary over class names,
284 while the corresponding lowercase letters indicate the class nicknames.
285 Throughout, we consider a class $C$ (therefore with nickname $c$).
286
287
288 \subsection{Generic instance structure}
289 \label{sec:structures.layout.instance}
290
291 The entire state of an instance of $C$ is contained in a single structure of
292 type @|struct $C$__ilayout|.
293
294 \begin{prog}
295   struct $C$__ilayout \{ \\ \ind
296     union $C$__ichainu_$h$ \{ \\ \ind
297       struct $C$__ichain_$h$ \{ \\ \ind
298         const struct $C$__vt_$h$ *_vt; \\
299         struct $H$__islots $h$; \\
300         \quad$\vdots$ \\
301         struct $C$__islots \{ \\ \ind
302           @<type>_1 @<slot>_1; \\
303           \quad$\vdots$ \\
304           @<type>_n @<slot>_n; \- \\
305         \} $c$; \- \\
306       \} $c$; \\
307       struct $H$__ichain_$h$ $h$; \\
308       \quad$\vdots$ \- \\
309     \} $h$; \\
310     union $B$__ichainu_$i$ $i$; \\
311     \quad$\vdots$ \- \\
312   \};
313   \\[\bigskipamount]
314   typedef struct $C$__ichain_$h$ $C$;
315 \end{prog}
316
317 The set of superclasses of $C$, including itself, can be partitioned into
318 chains by following their distinguished superclass links.  (Formally, the
319 chains are the equivalence classes determined by the reflexive, symmetric,
320 transitive closure of the `links to' relation.)  Chains are identified by
321 naming their least specific classes; the least specific class in a chain is
322 called the \emph{chain head}.  Suppose that the chain head of the chain
323 containing $C$ itself is named $H$ (though keep in mind that it's possible
324 that .$H$ is in fact $C$ itself.)
325
326 \subsubsection{The ilayout structure}
327 The @|ilayout| structure contains one member for each of $C$'s superclass
328 chains.  The first such member is
329 \begin{prog}
330   union $C$__ichainu_$h$ $h$;
331 \end{prog}
332 described below; this is followed by members
333 \begin{prog}
334   union $B$__ichainu_$i$ $i$;
335 \end{prog}
336 for each other chain, where $I$ is the head and $B$ the tail (most-specific)
337 class of the chain.  The members are in decreasing order of the specificity
338 of the chains' most-specific classes.  (Note that all but the first of these
339 unions has already been defined as part of the definition of the
340 corresponding $B$.)
341
342 \subsubsection{The ichainu union}
343 The @|ichainu| union contains a member for each class in the chain.  The
344 first is
345 \begin{prog}
346   struct $C$__ichain_$h$ $c$;
347 \end{prog}
348 and this is followed by corresponding members
349 \begin{prog}
350   struct $A$__ichain_$h$ $a$;
351 \end{prog}
352 for each of $C$'s superclasses $A$ in the same chain in some (unimportant)
353 order.
354
355 \subsubsection{The ichain structure}
356 The
357 @|ichain|
358 structure contains (in order), a pointer
359 \begin{prog}
360   const struct $C$__vt_$h$ *_vt;
361 \end{prog}
362 followed by a structure
363 \begin{prog}
364   struct $A$__islots $a$;
365 \end{prog}
366 for each superclass $A$ of $C$ in the same chain which defines slots, from
367 least- to most-specific; if $C$ defines any slots, then the last member is
368 \begin{prog}
369   struct $C$__islots $c$;
370 \end{prog}
371 A `pointer to $C$' is always assumed (and, indeed, defined in C's
372 type system) to be a pointer to the @|struct $C$__ichain_$h$|.
373
374 \subsubsection{The islots structure}
375 Finally, the @|islots| structure simply contains one member for each slot
376 defined by $C$ in the order they appear in the class definition.
377
378
379 \subsection{Generic vtable structure} \label{sec:structures.layout.vtable}
380
381 As described above, each @|ichain| structure of an instance's storage has a
382 vtable pointer
383 \begin{prog}
384   const struct $C$__vt_$h$ *_vt;
385 \end{prog}
386 In general, the vtables for the different chains will have \emph{different}
387 structures.
388
389 The instance layout split neatly into disjoint chains.  This is necessary
390 because each @|ichain| must have as a prefix the @|ichain| for each
391 superclass in the same chain, and each slot must be stored in exactly one
392 place.  The layout of vtables doesn't have this second requirement: it
393 doesn't matter that there are multiple method entry pointers for the same
394 effective method as long as they all work correctly.  Indeed, it's essential
395 that they do, because each chain's method entry function will need to apply a
396 different offset to the receiver pointer before invoking the effective
397 method.
398
399 A vtable for a class $C$ with chain head $H$ has the following general
400 structure.
401 \begin{prog}
402   union $C$__vtu_$h$ \{ \\ \ind
403     struct $C$__vt_$h$ \{ \\ \ind
404       const $P$ *_class; \\
405       size_t _base; \\
406       \quad$\vdots$ \\
407       const $Q$ *_cls_$j$; \\
408       \quad$\vdots$ \\
409       ptrdiff_t _off_$i$; \\
410       \quad$\vdots$ \\
411       struct $C$__vtmsgs_$a$ \{ \\ \ind
412         @<type> (*@<msg>)($C$ *, $\dots$); \\
413         \quad$\vdots$ \- \\
414       \} $a$; \\
415       \quad$\vdots$ \- \\
416     \} $c$; \- \\
417   \};
418   \\[\bigskipamount]
419   extern const union $C$__vtu_$h$ $C$__vtable_$h$;
420 \end{prog}
421
422 \subsubsection{The vtu union}
423 The outer layer is a @|union $C$__vtu_$h$| containing a member
424 \begin{prog}
425   struct $A$__vt_$h$ $a$;
426 \end{prog}
427 for each of $C$'s superclasses $A$ in the same chain, with $C$ itself listed
428 first.
429
430 This is mostly an irrelevant detail,
431 whose purpose is to defend against malicious compilers:
432 pointers are always to one of the inner
433 @|vt|
434 structures.
435 It's important only because it's the outer
436 @|vtu|
437 union which is exported by name.
438 Specifically, for each chain of
439 $C$'s
440 superclasses
441 there is an external object
442 \begin{prog}
443   const union $A$__vtu_$i$ $C$__vtable_$i$;
444 \end{prog}
445 where $A$ and $I$ are respectively the most and least specific classes in the
446 chain.
447
448 \subsubsection{The vt structure}
449 The first member in the @|vt| structure is the \emph{root class pointer}
450 \begin{prog}
451   const $P$ *_class;
452 \end{prog}
453 Among the superclasses of $C$ there must be exactly one class $O$ which
454 itself has no direct superclasses; this is the \emph{root superclass} of $C$.
455 (This is a rule enforced by the Sod translator.)  The metaclass $R$ of $O$ is
456 then the \emph{root metaclass} of $C$.  The @|_class| member points to the
457 @|ichain| structure of most specific superclass $P$ of $M$ in the same chain
458 as $R$.
459
460 This is followed by the \emph{base offset}
461 \begin{prog}
462   size_t _base;
463 \end{prog}
464 which is simply the offset of the @|ichain| structure from the instance base.
465
466 The rest of the vtable structure is populated by walking the superclass chain
467 containing $C$ as follows.  For each such superclass $B$, in increasing order
468 of specificity, walk the class precedence list of $B$, again starting with
469 its least-specific superclass.  (This complex procedure guarantees that the
470 vtable structure for a class is a prefix of the vtable structure for any of
471 its subclasses in the same chain.)
472
473 So, let $A$ be some superclass of $C$ which has been encountered during this
474 traversal.
475
476 \begin{itemize}
477
478 \item Let $N$ be the metaclass of $A$.  Examine the superclass chains of $N$
479   in order of decreasing specificity of their most-specific classes.  Let $J$
480   be the chain head of such a chain, and let $Q$ be the most specific
481   superclass of $M$ in the same chain as $J$.  Then, if there is currently no
482   class pointer of type $Q$, then add a member
483   \begin{prog}
484     const $Q$ *_cls_$j$;
485   \end{prog}
486   to the vtable pointing to the appropriate @|islots| structure within $M$'s
487   class object.
488
489 \item Examine the superclass chains of $A$ in order of decreasing specificity
490   of their most-specific classes.  Let $I$ be the chain head of such a chain.
491   If there is currently no member @|_off_$i$| then add a member
492   \begin{prog}
493     ptrdiff_t _off_$i$;
494   \end{prog}
495   to the vtable, containing the (signed) offset from the @|ichain| structure
496   of the chain headed by $h$ to that of the chain headed by $i$ within the
497   instance's layout.
498
499 \item If class $A$ defines any messages, and there is currently no member
500   $a$, then add a member
501   \begin{prog}
502     struct $C$__vtmsgs_$a$ $a$;
503   \end{prog}
504   to the vtable.  See below.
505
506 \end{itemize}
507
508 \subsubsection{The vtmsgs structure}
509 Finally, the @|vtmsgs| structures contain pointers to the effective method
510 entry functions for the messages defined by a superclass.  There may be more
511 than one method entry for a message, but all of the entry pointers for a
512 message appear together, and entry pointers for separate messages appear in
513 the order in which the messages are defined.  If the receiver class has no
514 applicable primary method for a message then it's usual for the method entry
515 pointer to be null (though, as with a lot of things in Sod, extensions may do
516 something different).
517
518 For a standard message which takes a fixed number of arguments, defined as
519 \begin{prog}
520   @<type>_0 $m$(@<type>_1 @<arg>_1, $\ldots$, @<type>_n @<arg>_n);
521 \end{prog}
522 there is always a `main' entry point,
523 \begin{prog}
524   @<type>_0 $m$($C$ *me, @<type>_1 @<arg>_1, $\ldots$, @<type>_n @<arg>_n);
525 \end{prog}
526
527 For a standard message which takes a variable number of arguments,
528 defined as
529 \begin{prog}
530   @<type>_0 $m$(@<type>_1 @<arg>_1, $\ldots$, @<type>_n @<arg>_n, \dots);
531 \end{prog}
532 two entry points are defined: the usual `main' entry point which accepts a
533 variable number of arguments, and a `valist' entry point which accepts an
534 argument of type @|va_list| in place of the variable portion of the argument
535 list.
536 \begin{prog}
537   @<type>_0 $m$($C$ *me, @<type>_1 @<arg>_1, $\ldots$,
538                 @<type>_n @<arg>_n, \dots); \\
539   @<type>_0 $m$__v($C$ *me, @<type>_1 @<arg>_1, $\ldots$,
540                    @<type>_n @<arg>_n, va_list sod__ap);
541 \end{prog}
542
543
544 \subsection{Additional definitions} \label{sec:structures.layout.additional}
545
546 In addition to the instance and vtable structures described above, the
547 following definitions are made for each class $C$.
548
549 For each message $m$ directly defined by $C$ there is a macro definition
550 \begin{prog}
551   \#define $C$_$m$(@<me>, $\ldots$) @<me>@->_vt@->$c$.$m$(@<me>, $\ldots$)
552 \end{prog}
553 which makes sending the message $m$ to an instance of (any subclass of) $C$
554 somewhat less ugly.
555
556 If $m$ takes a variable number of arguments, the macro is more complicated
557 and is only available in compilers advertising C99 support, but the effect is
558 the same.  For each variable-argument message, there is also an additional
559 macro for calling the `valist' entry point.
560 \begin{prog}
561   \#define $C$_$m$__v(@<me>, $\ldots$, @<sod__ap>)
562     @<me>@->_vt@->$c$.$m$__v(@<me>, $\ldots$, @<sod__ap>)
563 \end{prog}
564
565 For each proper superclass $A$ of $C$, there is a macro defined
566 \begin{prog}
567   $A$ *$C$__CONV_$a$($C$ *_obj);
568 \end{prog}
569 (named in \emph{upper case}) which converts a (static-type) pointer to $C$ to
570 a pointer to the same actual instance, but statically typed as a pointer to
571 $A$.  This is most useful when $A$ is not in the same chain as $C$ since
572 in-chain upcasts are both trivial and rarely needed, but the full set is
573 defined for the sake of completeness.
574
575 Finally, the class object is defined as
576 \begin{prog}
577   extern const struct $R$__ilayout $C$__classobj; \\
578   \#define $C$__class (\&$C$__classobj.$j$.$r$)
579 \end{prog}
580 The exported symbol @|$C$__classobj| contains the entire class instance.
581 This is usually rather unwieldy.  The macro @|$C$__class| is usable as a
582 pointer of type @|const $R$~*|, where $R$ is the root metaclass of $C$, i.e.,
583 the metaclass of the least specific superclass of $C$; usually this is
584 @|const SodClass~*|.
585
586 %%%----- That's all, folks --------------------------------------------------
587
588 %%% Local variables:
589 %%% mode: LaTeX
590 %%% TeX-master: "sod.tex"
591 %%% TeX-PDF-mode: t
592 %%% End: