Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 216 lines (164 sloc) 10.08 kB
74c1fc0 @jnthn [docs] Add an overview of 6model.
authored
1 =head1 6model Overview
2
3 This document discusses the scope, aims, design principles and high-level
4 architecture of 6model.
5
6
7 =head2 Project Scope
8
9 The Rakudo * release marked an important milestone in the development of
10 the Rakudo Perl 6 compiler. However, it was a point on a journey, not a
11 destination. Amongst the challenges facing Rakudo going forward were:
12
13 =over 4
14
15 =item Serious performance issues, in part because of the object model
16 implementation. This was in many places due to semantic mismatches,
17 which had been "fixed" by building a layer on top of what Parrot made
18 available.
19
20 =item No clear path to take advantage of gradual typing, both from an
21 optimization angle and a static analysis angle.
22
23 =item No way to support representation polymorphism, as required by the
24 Perl 6 specification.
25
26 =item Related to this, no obvious way forward with regard to supporting
27 natively typed attributes, including complex packed structs.
28
29 =item Insufficient meta-programming capabilities.
30
31 =item Very close coupling to the Parrot VM, which would hinder any efforts
32 to port Rakudo to other backends - something considered desirable in the
33 medium term.
34
35 =back
36
37 The 6model project was started in order to research, design and implement
38 a metamodel core that provided a way forward on all of these issues. While
39 the Perl 6 project was the immediate customer for 6model, the high level
40 of customizability that Perl 6 demands called for a design that would be
41 flexible enough to implement a wide range of object orientated and
42 type-related features.
43
44 Currently, there is interest from the Parrot VM team in adopting 6model.
b237786 @Util Fix typos.
Util authored
45 Furthermore, just as the compiler toolkit used to implement Rakudo aims
74c1fc0 @jnthn [docs] Add an overview of 6model.
authored
46 to provide general solutions for implementing compilation for a range
47 of languages, 6model is also built with a secondary aim of providing a
48 starting point for implementing the object oriented aspects of a range
49 of languages.
50
51 It's useful to distinguish 6model from the various NQP porting efforts,
52 which at the time of writing also live in the 6model repository. The term
53 "6model" was intended to only relate to the meta-model core itself. Since
54 one can't actually *do* anything with just that, and since it helped with
55 other goals, there is a parallel effort to port the NQP language to the
56 .Net CLR and the JVM. These implementations use 6model for all their OO
57 needs, but actually provide an awful lot more (that is, all the other bits
58 of compiler and runtime support that are needed to have an increasingly
59 complete implementation of the NQP language). Additionally, the Parrot NQP
60 implementation is being (or by the time you read this, maybe has been)
61 forked and re-built to use 6model rather than the Parrot object model.
62
63
64 =head2 Design Principles
65
66 The following principles guide a lot of the design decisions made with
67 regard to 6model, and are a good starting point for understanding why
68 it is structured the way it is.
69
0419a1f @jnthn [docs] Try and get something that renders nicer with GitHub's POD ren…
authored
70 =head3 Minimal, non-commital core
c0a00fa @jnthn [docs] Tssk. Next time I just write plain text. Or HTML.
authored
71
0419a1f @jnthn [docs] Try and get something that renders nicer with GitHub's POD ren…
authored
72 Out of the box, 6model provides very minimal object oriented functionality.
73 It provides one meta-object that implements objects with attributes (state)
74 and methods (behavior) - and that's about it. It doesn't enforce one
75 definition of method dispatch, inheritance, interfaces, introspection and
76 so forth. These are all built up by implementing meta-objects that specify
77 their semantics.
78
79 =head3 Representation polymorphism
c0a00fa @jnthn [docs] Tssk. Next time I just write plain text. Or HTML.
authored
80
0419a1f @jnthn [docs] Try and get something that renders nicer with GitHub's POD ren…
authored
81 Rather than committing to one view of how to lay out an object in memory,
82 6model supports "representations". Representations define how attributes are
83 actually stored and accessed, how primitive types (integers, floating point
84 values and strings) are boxed and unboxed - or perhaps both. Additionally,
85 representations are orthogonal to meta-objects, meaning it is possible to
7d4f8de @jnthn [docs] Typo spotted by wolverian++.
authored
86 define one type (e.g. a class) and use it with different storage strategies.
0419a1f @jnthn [docs] Try and get something that renders nicer with GitHub's POD ren…
authored
87 This is known as representation polymorphism.
88
89 =head3 Gradual typing support
c0a00fa @jnthn [docs] Tssk. Next time I just write plain text. Or HTML.
authored
90
0419a1f @jnthn [docs] Try and get something that renders nicer with GitHub's POD ren…
authored
91 6model tries to provide ways to pick points on the static-dynamic typing scale.
92 For languages that themselves support gradual typing, this is directly useful.
93 However, it means that one could implement object models that are completely
94 dynamic or completely static. Objects where method calls are dispatched by a
95 fast lookup in a v-table are just as possible as objects where method calls
96 dynamically build a call to a web service.
97
98 =head3 Portable between virtual machines
c0a00fa @jnthn [docs] Tssk. Next time I just write plain text. Or HTML.
authored
99
0419a1f @jnthn [docs] Try and get something that renders nicer with GitHub's POD ren…
authored
100 By using 6model, it should become easier to make a compiler that is portable
101 between VMs. Currently 6model implementations in varying states of completeness
102 exist on Parrot, the .Net CLR and the JVM.
103
104 =head3 Meta-objects are authoritative
c0a00fa @jnthn [docs] Tssk. Next time I just write plain text. Or HTML.
authored
105
0419a1f @jnthn [docs] Try and get something that renders nicer with GitHub's POD ren…
authored
106 In an ideal world, every single method dispatch we perform would be conducted
107 by delegating to the meta-object's method that implements method dispatch
108 semantics. In the real world, that's not practical from a performance point of
109 view. Thus 6model provides various mechanisms that a meta-object can "opt in"
110 to in order to allow for muchly increased performance. However, it considers
111 all of these to really just be a kind of "cache". A v-table is just an array
74c1fc0 @jnthn [docs] Add an overview of 6model.
authored
112 of invokable objects published by the meta-object, which it is responsible
113 for maintaining. Similar things apply to type-checking.
114
115
116 =head2 Data Structures
117
118 At the heart of 6model are three core types of data structure.
119
0419a1f @jnthn [docs] Try and get something that renders nicer with GitHub's POD ren…
authored
120 =head3 Objects
c0a00fa @jnthn [docs] Tssk. Next time I just write plain text. Or HTML.
authored
121
0419a1f @jnthn [docs] Try and get something that renders nicer with GitHub's POD ren…
authored
122 Other than native types, everything that the user ever interacts
74c1fc0 @jnthn [docs] Add an overview of 6model.
authored
123 with directly - that is, anything that makes it into lexpads, locals, packages,
124 attributes and other storage locations - is an object. This is the only user-facing
125 data structure in 6model. An object is a blob of memory. The only constraint is
126 that the first thing in the blob must be a pointer/reference to a Shared Table
127 data structure.
128
0419a1f @jnthn [docs] Try and get something that renders nicer with GitHub's POD ren…
authored
129 =head3 Representations
c0a00fa @jnthn [docs] Tssk. Next time I just write plain text. Or HTML.
authored
130
0419a1f @jnthn [docs] Try and get something that renders nicer with GitHub's POD ren…
authored
131 An object may in the abstract be a blob of memory that starts
74c1fc0 @jnthn [docs] Add an overview of 6model.
authored
132 with a pointer to a Shared Table, but of course something has to know what the rest
133 of it means. That's what representations do. A representation is responsible for
134 object allocation, attribute storage and access (both in terms of memory layout and
135 operation), boxing from and unboxing to native types and (depending on the VM) GC
136 interaction. Representations may be like singletons, or they may act more like
137 instances. How a representation is implemented is VM-specific; in fact, pretty much
138 everything the representation has to do is also VM-specific. While it's expected
139 that users of programming languages will (relatively) frequently engage in creating
140 or customizing meta-objects, the use cases for writing custom representations are
141 fewer.
142
0419a1f @jnthn [docs] Try and get something that renders nicer with GitHub's POD ren…
authored
143 =head3 Shared Tables
c0a00fa @jnthn [docs] Tssk. Next time I just write plain text. Or HTML.
authored
144
0419a1f @jnthn [docs] Try and get something that renders nicer with GitHub's POD ren…
authored
145 For every object, there is a meta-object defining its
74c1fc0 @jnthn [docs] Add an overview of 6model.
authored
146 semantics and a representation defining its memory layout. There are also some
147 entities that should live outside of either of them, such as the cached v-table
148 that some meta-objects may publish and a reference to the type object. However,
149 many objects share these (for example, every instance of a class with the same
150 representation). Rather than every object starting with a bunch of pointers, it
151 instead has one pointer to a shared table containing these things. Thus individual
152 objects stay small.
153
154 +--------+ +----------------+
155 | Object | +-->| Shared Table |
156 +--------+ | +----------------+
157 | STABLE |---+ | Meta-object |-----> Just another Object
158 | .... | | Representation |-----> Representation data structure
159 | .... | | V-table Cache |-----> Array
160 +--------+ | Type object |-----> Just another Object
161 | <other bits> |
162 +----------------+
163
164 Notice how meta-objects are in no way special; they are, in fact, just plain
165 old objects that implement a special API. Similarly, type objects are just
166 "empty instances" of objects. Whether an instance is empty or not is decided
167 by the representation API.
168
169
170 =head2 The relationship of representations and meta-objects
171
172 Representations and meta-objects are orthogonal and take charge of different
173 roles in an object's overall semantics. Being able to pair representations and
174 meta-objects together gives rise to representation polymorphism. However,
175 orthogonality does not mean they have no relationship at all. In particular,
176 a representation will always know the meta-object of the type that it needs
177 to provide a memory layout for.
178
179 For example, take a class definition:
180
181 class Vodka {
182 has $!flavour;
183 has $!name;
184 }
185
186 Now imagine a representation that has an object layout where attributes are
187 stored as an array of pointers, with the name being mapped to a numbered slot.
188 The representation needs to know that this class has two attributes, so that
189 it can allocate enough memory to store them.
190
191 There are representations that need not care about the meta-object. For example,
192 a representation where attributes are simply stored in a hash table keyed on
193 attribute name could just store whatever attribute name it is asked to. This
194 would suit languages where the programmer need not declare what attributes they
195 will have up front.
196
197 The other case that can come up is a representation dedicated to cheaply boxing
198 some native type, or that maps to some internal VM structure that should also
199 be available to pass around as an opaque object (the CLR and JVM implementations
200 of NQP make heavy use of this pattern). In this case, the representation doesn't
201 have the capability to store attributes at all, so would just use the meta-object
202 to check that it does not need to do so.
203
204
205 =head2 Bootstrapping and circularity
206
207 At "startup", a little bit of work is done to bootstrap the meta-model. At the
208 heart of this is the setting up of KnowHOW, the single type of meta-object that
209 is defined in the 6model core. An instance of it is created using a special
210 representation (only special in that it's not used or useful for anything other
211 than this bootstrap). This instance acts as the meta-object for all KnowHOW
212 meta-objects, and is thus self-describing. Therefore, its meta-object pointer is
213 set to point back on itself. Sound loopy? Good. That's the point.
214
215 =cut
Something went wrong with that request. Please try again.