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