~ubuntu-branches/ubuntu/vivid/nqp/vivid-proposed

« back to all changes in this revision

Viewing changes to src/vm/jvm/runtime/org/perl6/nqp/sixmodel/KnowHOWBootstrapper.java

  • Committer: Package Import Robot
  • Author(s): Alessandro Ghedini
  • Date: 2013-11-01 12:09:18 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20131101120918-kx51sl0sxl3exsxi
Tags: 2013.10-1
* New upstream release
* Bump versioned (Build-)Depends on parrot
* Update patches
* Install new README.pod
* Fix vcs-field-not-canonical
* Do not install rubyish examples
* Do not Depends on parrot-devel anymore
* Add 07_disable-serialization-tests.patch

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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;
 
7
 
 
8
public class KnowHOWBootstrapper {
 
9
    public static void bootstrap(ThreadContext tc)
 
10
    {
 
11
        CompilationUnit knowhowUnit = new KnowHOWMethods();
 
12
        knowhowUnit.initializeCompilationUnit(tc);
 
13
        bootstrapKnowHOW(tc, knowhowUnit);
 
14
        bootstrapKnowHOWAttribute(tc, knowhowUnit);
 
15
        
 
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");
 
28
        
 
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;
 
35
        
 
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);
 
39
        
 
40
        tc.gc.MultiCache = bootType(tc, "MultiCache", "MultiCache");
 
41
 
 
42
        tc.gc.Continuation = bootType(tc, "Continuation", "Continuation");
 
43
        tc.gc.Lexotic = bootType(tc, "Lexotic", "Lexotic");
 
44
        tc.gc.BOOTJava = bootType(tc, "BOOTJavaObject", "JavaWrap");
 
45
        
 
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);
 
55
    }
 
56
 
 
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
 
59
         * pass in NULL. */
 
60
        REPR REPR = REPRRegistry.getByName("KnowHOWREPR");
 
61
        SixModelObject knowhow = REPR.type_object_for(tc, null);
 
62
 
 
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);
 
67
        st.WHAT = knowhow;
 
68
        KnowHOWREPRInstance knowhow_how = (KnowHOWREPRInstance)REPR.allocate(tc, st);
 
69
        st.HOW = knowhow_how;
 
70
        knowhow_how.st = st;
 
71
        
 
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"));
 
80
        
 
81
        /* Set name KnowHOW for the KnowHOW's HOW. */
 
82
        knowhow_how.name = "KnowHOW";
 
83
 
 
84
        /* Set this built up HOW as the KnowHOW's HOW. */
 
85
        knowhow.st.HOW = knowhow_how;
 
86
        
 
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;
 
93
        
 
94
        /* Associate the created objects with the initial core serialization
 
95
         * context. */
 
96
        SerializationContext sc = new SerializationContext("__6MODEL_CORE__");
 
97
        tc.gc.scs.put("__6MODEL_CORE__", sc);
 
98
        sc.root_objects.add(knowhow);
 
99
        knowhow.sc = sc;
 
100
        sc.root_objects.add(knowhow_how);
 
101
        knowhow_how.sc = sc;
 
102
        sc.root_stables.add(knowhow.st);
 
103
        knowhow.st.sc = sc;
 
104
        sc.root_stables.add(knowhow_how.st);
 
105
        knowhow_how.st.sc = sc;
 
106
 
 
107
        /* Stash the created KnowHOW. */
 
108
        tc.gc.KnowHOW = knowhow;
 
109
    }
 
110
 
 
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);
 
115
        
 
116
        /* Add methods. */
 
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"));
 
122
        
 
123
        /* Set name. */
 
124
        meta_obj.name = "KnowHOWAttribute";
 
125
        
 
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);
 
129
        
 
130
        /* Set up method dispatch cache. */
 
131
        type_obj.st.MethodCache = meta_obj.methods;
 
132
        type_obj.st.ModeFlags = STable.METHOD_CACHE_AUTHORITATIVE;
 
133
        
 
134
        /* Associate the created object with the intial core serialization
 
135
         * context. */
 
136
        SerializationContext sc =  tc.gc.scs.get("__6MODEL_CORE__");
 
137
        sc.root_objects.add(type_obj);
 
138
        type_obj.sc = sc;
 
139
        sc.root_stables.add(type_obj.st);
 
140
        type_obj.st.sc = sc;
 
141
        
 
142
        /* Stash the created type object. */
 
143
        tc.gc.KnowHOWAttribute = type_obj;
 
144
    }
 
145
    
 
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;
 
154
        
 
155
        SerializationContext sc = tc.gc.scs.get("__6MODEL_CORE__");
 
156
        sc.root_objects.add(type_obj);
 
157
        type_obj.sc = sc;
 
158
        sc.root_objects.add(type_obj.st.HOW);
 
159
        type_obj.st.HOW.sc = sc;
 
160
        sc.root_stables.add(type_obj.st);
 
161
        type_obj.st.sc = sc;
 
162
        
 
163
        return type_obj;
 
164
    }
 
165
 
 
166
    private static SixModelObject bootTypedArray(ThreadContext tc, String name, SixModelObject type) {
 
167
        SixModelObject booted = bootType(tc, name, "VMArray");
 
168
        
 
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);
 
175
        
 
176
        return booted;
 
177
    }
 
178
}