1
package org.perl6.nqp.sixmodel;
2
import org.perl6.nqp.runtime.CompilationUnit;
3
import org.perl6.nqp.runtime.HLLConfig;
4
import org.perl6.nqp.runtime.Ops;
5
import org.perl6.nqp.runtime.ThreadContext;
6
import org.perl6.nqp.sixmodel.reprs.KnowHOWREPRInstance;
8
public class KnowHOWBootstrapper {
9
public static void bootstrap(ThreadContext tc)
11
CompilationUnit knowhowUnit = new KnowHOWMethods();
12
knowhowUnit.initializeCompilationUnit(tc);
13
bootstrapKnowHOW(tc, knowhowUnit);
14
bootstrapKnowHOWAttribute(tc, knowhowUnit);
16
tc.gc.BOOTArray = bootType(tc, "BOOTArray", "VMArray");
17
tc.gc.BOOTHash = bootType(tc, "BOOTHash", "VMHash");
18
tc.gc.BOOTIter = bootType(tc, "BOOTIter", "VMIter");
19
tc.gc.BOOTInt = bootType(tc, "BOOTInt", "P6int");
20
tc.gc.BOOTNum = bootType(tc, "BOOTNum", "P6num");
21
tc.gc.BOOTStr = bootType(tc, "BOOTStr", "P6str");
22
tc.gc.BOOTCode = bootType(tc, "BOOTCode", "CodeRef");
23
tc.gc.SCRef = bootType(tc, "SCRef", "SCRef");
24
tc.gc.ContextRef = bootType(tc, "ContextRef", "ContextRef");
25
tc.gc.CallCapture = bootType(tc, "CallCapture", "CallCapture");
26
tc.gc.BOOTException = bootType(tc, "BOOTException", "VMException");
27
tc.gc.BOOTIO = bootType(tc, "BOOTIO", "IOHandle");
29
tc.gc.BOOTArray.st.hllRole = HLLConfig.ROLE_ARRAY;
30
tc.gc.BOOTHash.st.hllRole = HLLConfig.ROLE_HASH;
31
tc.gc.BOOTInt.st.hllRole = HLLConfig.ROLE_INT;
32
tc.gc.BOOTNum.st.hllRole = HLLConfig.ROLE_NUM;
33
tc.gc.BOOTStr.st.hllRole = HLLConfig.ROLE_STR;
34
tc.gc.BOOTCode.st.hllRole = HLLConfig.ROLE_CODE;
36
tc.gc.BOOTIntArray = bootTypedArray(tc, "BOOTIntArray", tc.gc.BOOTInt);
37
tc.gc.BOOTNumArray = bootTypedArray(tc, "BOOTNumArray", tc.gc.BOOTNum);
38
tc.gc.BOOTStrArray = bootTypedArray(tc, "BOOTStrArray", tc.gc.BOOTStr);
40
tc.gc.MultiCache = bootType(tc, "MultiCache", "MultiCache");
42
tc.gc.Continuation = bootType(tc, "Continuation", "Continuation");
43
tc.gc.Lexotic = bootType(tc, "Lexotic", "Lexotic");
44
tc.gc.BOOTJava = bootType(tc, "BOOTJavaObject", "JavaWrap");
46
Ops.setboolspec(tc.gc.BOOTIter, BoolificationSpec.MODE_ITER, null, tc);
47
Ops.setboolspec(tc.gc.BOOTInt, BoolificationSpec.MODE_UNBOX_INT, null, tc);
48
Ops.setboolspec(tc.gc.BOOTNum, BoolificationSpec.MODE_UNBOX_NUM, null, tc);
49
Ops.setboolspec(tc.gc.BOOTStr, BoolificationSpec.MODE_UNBOX_STR_NOT_EMPTY_OR_ZERO, null, tc);
50
Ops.setboolspec(tc.gc.BOOTArray, BoolificationSpec.MODE_HAS_ELEMS, null, tc);
51
Ops.setboolspec(tc.gc.BOOTIntArray, BoolificationSpec.MODE_HAS_ELEMS, null, tc);
52
Ops.setboolspec(tc.gc.BOOTNumArray, BoolificationSpec.MODE_HAS_ELEMS, null, tc);
53
Ops.setboolspec(tc.gc.BOOTStrArray, BoolificationSpec.MODE_HAS_ELEMS, null, tc);
54
Ops.setboolspec(tc.gc.BOOTHash, BoolificationSpec.MODE_HAS_ELEMS, null, tc);
57
private static void bootstrapKnowHOW(ThreadContext tc, CompilationUnit knowhowUnit) {
58
/* Create our KnowHOW type object. Note we don't have a HOW just yet, so
60
REPR REPR = REPRRegistry.getByName("KnowHOWREPR");
61
SixModelObject knowhow = REPR.type_object_for(tc, null);
63
/* We create a KnowHOW instance that can describe itself. This means
64
* (once we tie the knot) that .HOW.HOW.HOW.HOW etc will always return
65
* that, which closes the model up. */
66
STable st = new STable(REPR, null);
68
KnowHOWREPRInstance knowhow_how = (KnowHOWREPRInstance)REPR.allocate(tc, st);
72
/* Add various methods to the KnowHOW's HOW. */
73
knowhow_how.methods.put("new_type", knowhowUnit.lookupCodeRef("new_type"));
74
knowhow_how.methods.put("add_method", knowhowUnit.lookupCodeRef("add_method"));
75
knowhow_how.methods.put("add_attribute", knowhowUnit.lookupCodeRef("add_attribute"));
76
knowhow_how.methods.put("compose", knowhowUnit.lookupCodeRef("compose"));
77
knowhow_how.methods.put("attributes", knowhowUnit.lookupCodeRef("attributes"));
78
knowhow_how.methods.put("methods", knowhowUnit.lookupCodeRef("methods"));
79
knowhow_how.methods.put("name", knowhowUnit.lookupCodeRef("name"));
81
/* Set name KnowHOW for the KnowHOW's HOW. */
82
knowhow_how.name = "KnowHOW";
84
/* Set this built up HOW as the KnowHOW's HOW. */
85
knowhow.st.HOW = knowhow_how;
87
/* Give it an authoritative method cache; this in turn will make the
88
* method dispatch bottom out. */
89
knowhow.st.MethodCache = knowhow_how.methods;
90
knowhow.st.ModeFlags = STable.METHOD_CACHE_AUTHORITATIVE;
91
knowhow_how.st.MethodCache = knowhow_how.methods;
92
knowhow_how.st.ModeFlags = STable.METHOD_CACHE_AUTHORITATIVE;
94
/* Associate the created objects with the initial core serialization
96
SerializationContext sc = new SerializationContext("__6MODEL_CORE__");
97
tc.gc.scs.put("__6MODEL_CORE__", sc);
98
sc.root_objects.add(knowhow);
100
sc.root_objects.add(knowhow_how);
102
sc.root_stables.add(knowhow.st);
104
sc.root_stables.add(knowhow_how.st);
105
knowhow_how.st.sc = sc;
107
/* Stash the created KnowHOW. */
108
tc.gc.KnowHOW = knowhow;
111
private static void bootstrapKnowHOWAttribute(ThreadContext tc, CompilationUnit knowhowUnit) {
112
/* Create meta-object. */
113
SixModelObject knowhow_how = tc.gc.KnowHOW.st.HOW;
114
KnowHOWREPRInstance meta_obj = (KnowHOWREPRInstance)knowhow_how.st.REPR.allocate(tc, knowhow_how.st);
117
meta_obj.methods.put("new", knowhowUnit.lookupCodeRef("attr_new"));
118
meta_obj.methods.put("compose", knowhowUnit.lookupCodeRef("attr_compose"));
119
meta_obj.methods.put("name", knowhowUnit.lookupCodeRef("attr_name"));
120
meta_obj.methods.put("type", knowhowUnit.lookupCodeRef("attr_type"));
121
meta_obj.methods.put("box_target", knowhowUnit.lookupCodeRef("attr_box_target"));
124
meta_obj.name = "KnowHOWAttribute";
126
/* Create a new type object with the correct REPR. */
127
REPR repr = REPRRegistry.getByName("KnowHOWAttribute");
128
SixModelObject type_obj = repr.type_object_for(tc, meta_obj);
130
/* Set up method dispatch cache. */
131
type_obj.st.MethodCache = meta_obj.methods;
132
type_obj.st.ModeFlags = STable.METHOD_CACHE_AUTHORITATIVE;
134
/* Associate the created object with the intial core serialization
136
SerializationContext sc = tc.gc.scs.get("__6MODEL_CORE__");
137
sc.root_objects.add(type_obj);
139
sc.root_stables.add(type_obj.st);
142
/* Stash the created type object. */
143
tc.gc.KnowHOWAttribute = type_obj;
146
private static SixModelObject bootType(ThreadContext tc, String typeName, String reprName) {
147
SixModelObject knowhow_how = tc.gc.KnowHOW.st.HOW;
148
KnowHOWREPRInstance meta_obj = (KnowHOWREPRInstance)knowhow_how.st.REPR.allocate(tc, knowhow_how.st);
149
meta_obj.name = typeName;
150
REPR repr = REPRRegistry.getByName(reprName);
151
SixModelObject type_obj = repr.type_object_for(tc, meta_obj);
152
type_obj.st.MethodCache = meta_obj.methods;
153
type_obj.st.ModeFlags = STable.METHOD_CACHE_AUTHORITATIVE;
155
SerializationContext sc = tc.gc.scs.get("__6MODEL_CORE__");
156
sc.root_objects.add(type_obj);
158
sc.root_objects.add(type_obj.st.HOW);
159
type_obj.st.HOW.sc = sc;
160
sc.root_stables.add(type_obj.st);
166
private static SixModelObject bootTypedArray(ThreadContext tc, String name, SixModelObject type) {
167
SixModelObject booted = bootType(tc, name, "VMArray");
169
SixModelObject BOOTHash = tc.gc.BOOTHash;
170
SixModelObject repr_info = BOOTHash.st.REPR.allocate(tc, BOOTHash.st);
171
SixModelObject repr_array_info = BOOTHash.st.REPR.allocate(tc, BOOTHash.st);
172
repr_array_info.bind_key_boxed(tc, "type", type);
173
repr_info.bind_key_boxed(tc, "array", repr_array_info);
174
booted.st.REPR.compose(tc, booted.st, repr_info);