forked from MoarVM/MoarVM
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbootstrap.c
More file actions
715 lines (619 loc) · 33.3 KB
/
Copy pathbootstrap.c
File metadata and controls
715 lines (619 loc) · 33.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
#include "moar.h"
/* This file implements the various steps involved in getting 6model
* bootstrapped from the ground up - that is, getting to having a
* KnowHOW meta-object type so that userland can start building up
* more interesting meta-objects. Mostly it just has to make objects
* with some "holes", and later go back and fill them out. This is
* due to the circular nature of things.
*/
/* Creates a stub VMString. Note we didn't initialize the
* representation yet, so have to do this somewhat pokily. */
static void create_stub_VMString(MVMThreadContext *tc) {
/* Need to create the REPR function table "in advance". */
const MVMREPROps *repr = MVMString_initialize(tc);
/* Now we can create a type object; note we have no HOW yet,
* though. */
tc->instance->VMString = repr->type_object_for(tc, NULL);
}
/* KnowHOW.new_type method. Creates a new type with this HOW as its meta-object. */
static void new_type(MVMThreadContext *tc, MVMCallsite *callsite, MVMRegister *args) {
MVMObject *self, *HOW, *type_object, *BOOTHash, *stash;
MVMArgInfo repr_arg, name_arg;
MVMString *repr_name, *name;
const MVMREPROps *repr_to_use;
MVMInstance *instance = tc->instance;
/* Get arguments. */
MVMArgProcContext arg_ctx;
MVM_args_proc_init(tc, &arg_ctx, callsite, args);
MVM_args_checkarity(tc, &arg_ctx, 1, 1);
self = MVM_args_get_required_pos_obj(tc, &arg_ctx, 0);
repr_arg = MVM_args_get_named_str(tc, &arg_ctx, instance->str_consts.repr, MVM_ARG_OPTIONAL);
name_arg = MVM_args_get_named_str(tc, &arg_ctx, instance->str_consts.name, MVM_ARG_OPTIONAL);
MVM_args_proc_cleanup(tc, &arg_ctx);
if (REPR(self)->ID != MVM_REPR_ID_KnowHOWREPR)
MVM_exception_throw_adhoc(tc, "KnowHOW methods must be called on object with REPR KnowHOWREPR");
/* See if we have a representation name; if not default to P6opaque. */
repr_name = repr_arg.exists ? repr_arg.arg.s : instance->str_consts.P6opaque;
repr_to_use = MVM_repr_get_by_name(tc, repr_name);
MVM_gc_root_temp_push(tc, (MVMCollectable **)&name_arg);
/* We first create a new HOW instance. */
HOW = REPR(self)->allocate(tc, STABLE(self));
MVM_gc_root_temp_push(tc, (MVMCollectable **)&HOW);
/* Create a new type object of the desired REPR. (Note that we can't
* default to KnowHOWREPR here, since it doesn't know how to actually
* store attributes, it's just for bootstrapping knowhow's. */
type_object = repr_to_use->type_object_for(tc, HOW);
MVM_gc_root_temp_push(tc, (MVMCollectable **)&type_object);
/* This may move name_arg.arg.s so do it first: */
REPR(HOW)->initialize(tc, STABLE(HOW), HOW, OBJECT_BODY(HOW));
/* See if we were given a name; put it into the meta-object if so. */
name = name_arg.exists ? name_arg.arg.s : instance->str_consts.anon;
MVM_ASSIGN_REF(tc, &(HOW->header), ((MVMKnowHOWREPR *)HOW)->body.name, name);
type_object->st->debug_name = MVM_string_utf8_encode_C_string(tc, name);
/* Set .WHO to an empty hash. */
BOOTHash = tc->instance->boot_types.BOOTHash;
stash = REPR(BOOTHash)->allocate(tc, STABLE(BOOTHash));
MVM_gc_root_temp_push(tc, (MVMCollectable **)&stash);
MVM_ASSIGN_REF(tc, &(STABLE(type_object)->header), STABLE(type_object)->WHO, stash);
/* Return the type object. */
MVM_args_set_result_obj(tc, type_object, MVM_RETURN_CURRENT_FRAME);
MVM_gc_root_temp_pop_n(tc, 4);
}
/* Adds a method. */
static void add_method(MVMThreadContext *tc, MVMCallsite *callsite, MVMRegister *args) {
MVMObject *self, *method, *method_table;
MVMString *name;
/* Get arguments. */
MVMArgProcContext arg_ctx;
MVM_args_proc_init(tc, &arg_ctx, callsite, args);
MVM_args_checkarity(tc, &arg_ctx, 4, 4);
self = MVM_args_get_required_pos_obj(tc, &arg_ctx, 0);
name = MVM_args_get_required_pos_str(tc, &arg_ctx, 2);
method = MVM_args_get_required_pos_obj(tc, &arg_ctx, 3);
MVM_args_proc_cleanup(tc, &arg_ctx);
if (!self || !IS_CONCRETE(self) || REPR(self)->ID != MVM_REPR_ID_KnowHOWREPR)
MVM_exception_throw_adhoc(tc, "KnowHOW methods must be called on object instance with REPR KnowHOWREPR");
/* Add to method table. */
method_table = ((MVMKnowHOWREPR *)self)->body.methods;
MVM_repr_bind_key_o(tc, method_table, name, method);
/* Return added method as result. */
MVM_args_set_result_obj(tc, method, MVM_RETURN_CURRENT_FRAME);
}
/* Adds an method. */
static void add_attribute(MVMThreadContext *tc, MVMCallsite *callsite, MVMRegister *args) {
MVMObject *self, *attr, *attributes;
/* Get arguments. */
MVMArgProcContext arg_ctx;
MVM_args_proc_init(tc, &arg_ctx, callsite, args);
MVM_args_checkarity(tc, &arg_ctx, 3, 3);
self = MVM_args_get_required_pos_obj(tc, &arg_ctx, 0);
attr = MVM_args_get_required_pos_obj(tc, &arg_ctx, 2);
MVM_args_proc_cleanup(tc, &arg_ctx);
/* Ensure we have the required representations. */
if (!self || !IS_CONCRETE(self) || REPR(self)->ID != MVM_REPR_ID_KnowHOWREPR)
MVM_exception_throw_adhoc(tc, "KnowHOW methods must be called on object instance with REPR KnowHOWREPR");
if (REPR(attr)->ID != MVM_REPR_ID_KnowHOWAttributeREPR)
MVM_exception_throw_adhoc(tc, "KnowHOW attributes must use KnowHOWAttributeREPR");
/* Add to method table. */
attributes = ((MVMKnowHOWREPR *)self)->body.attributes;
MVM_repr_push_o(tc, attributes, attr);
/* Return added attribute as result. */
MVM_args_set_result_obj(tc, attr, MVM_RETURN_CURRENT_FRAME);
}
/* Composes the meta-object. */
static void compose(MVMThreadContext *tc, MVMCallsite *callsite, MVMRegister *args) {
MVMObject *self, *type_obj, *method_table, *attributes, *BOOTArray, *BOOTHash,
*repr_info_hash, *repr_info, *type_info, *attr_info_list, *parent_info;
MVMuint64 num_attrs, i;
MVMInstance *instance = tc->instance;
/* Get arguments. */
MVMArgProcContext arg_ctx;
MVM_args_proc_init(tc, &arg_ctx, callsite, args);
MVM_args_checkarity(tc, &arg_ctx, 2, 2);
self = MVM_args_get_required_pos_obj(tc, &arg_ctx, 0);
type_obj = MVM_args_get_required_pos_obj(tc, &arg_ctx, 1);
MVM_args_proc_cleanup(tc, &arg_ctx);
if (!self || !IS_CONCRETE(self) || REPR(self)->ID != MVM_REPR_ID_KnowHOWREPR)
MVM_exception_throw_adhoc(tc, "KnowHOW methods must be called on object instance with REPR KnowHOWREPR");
/* Fill out STable. */
method_table = ((MVMKnowHOWREPR *)self)->body.methods;
MVM_ASSIGN_REF(tc, &(STABLE(type_obj)->header), STABLE(type_obj)->method_cache, method_table);
STABLE(type_obj)->mode_flags = MVM_METHOD_CACHE_AUTHORITATIVE;
STABLE(type_obj)->type_check_cache_length = 1;
STABLE(type_obj)->type_check_cache = MVM_malloc(sizeof(MVMObject *));
MVM_ASSIGN_REF(tc, &(STABLE(type_obj)->header), STABLE(type_obj)->type_check_cache[0], type_obj);
attributes = ((MVMKnowHOWREPR *)self)->body.attributes;
/* Next steps will allocate, so make sure we keep hold of the type
* object and ourself. */
MVM_gc_root_temp_push(tc, (MVMCollectable **)&attributes);
MVM_gc_root_temp_push(tc, (MVMCollectable **)&type_obj);
/* Use any attribute information to produce attribute protocol
* data. The protocol consists of an array... */
BOOTArray = instance->boot_types.BOOTArray;
BOOTHash = instance->boot_types.BOOTHash;
MVM_gc_root_temp_push(tc, (MVMCollectable **)&BOOTArray);
MVM_gc_root_temp_push(tc, (MVMCollectable **)&BOOTHash);
repr_info = REPR(BOOTArray)->allocate(tc, STABLE(BOOTArray));
MVM_gc_root_temp_push(tc, (MVMCollectable **)&repr_info);
/* ...which contains an array per MRO entry (just us)... */
type_info = REPR(BOOTArray)->allocate(tc, STABLE(BOOTArray));
MVM_gc_root_temp_push(tc, (MVMCollectable **)&type_info);
MVM_repr_push_o(tc, repr_info, type_info);
/* ...which in turn contains this type... */
MVM_repr_push_o(tc, type_info, type_obj);
/* ...then an array of hashes per attribute... */
attr_info_list = REPR(BOOTArray)->allocate(tc, STABLE(BOOTArray));
MVM_gc_root_temp_push(tc, (MVMCollectable **)&attr_info_list);
MVM_repr_push_o(tc, type_info, attr_info_list);
num_attrs = REPR(attributes)->elems(tc, STABLE(attributes),
attributes, OBJECT_BODY(attributes));
for (i = 0; i < num_attrs; i++) {
MVMObject *attr_info = REPR(BOOTHash)->allocate(tc, STABLE(BOOTHash));
MVMKnowHOWAttributeREPR *attribute = (MVMKnowHOWAttributeREPR *)
MVM_repr_at_pos_o(tc, attributes, i);
MVMROOT2(tc, attr_info, attribute, {
if (REPR((MVMObject *)attribute)->ID != MVM_REPR_ID_KnowHOWAttributeREPR)
MVM_exception_throw_adhoc(tc, "KnowHOW attributes must use KnowHOWAttributeREPR");
MVM_repr_init(tc, attr_info);
MVM_repr_bind_key_o(tc, attr_info, instance->str_consts.name, (MVMObject *)attribute->body.name);
MVM_repr_bind_key_o(tc, attr_info, instance->str_consts.type, attribute->body.type);
if (attribute->body.box_target) {
/* Merely having the key serves as a "yes". */
MVM_repr_bind_key_o(tc, attr_info, instance->str_consts.box_target, attr_info);
}
MVM_repr_push_o(tc, attr_info_list, attr_info);
});
}
/* ...followed by a list of parents (none). */
parent_info = REPR(BOOTArray)->allocate(tc, STABLE(BOOTArray));
MVM_gc_root_temp_push(tc, (MVMCollectable **)&parent_info);
MVM_repr_init(tc, parent_info);
MVM_repr_push_o(tc, type_info, parent_info);
/* Finally, this all goes in a hash under the key 'attribute'. */
repr_info_hash = REPR(BOOTHash)->allocate(tc, STABLE(BOOTHash));
MVM_gc_root_temp_push(tc, (MVMCollectable **)&repr_info_hash);
MVM_repr_init(tc, repr_info_hash);
MVM_repr_bind_key_o(tc, repr_info_hash, instance->str_consts.attribute, repr_info);
/* Compose the representation using it. */
MVM_repr_compose(tc, type_obj, repr_info_hash);
/* Clear temporary roots. */
MVM_gc_root_temp_pop_n(tc, 9);
/* Return type object. */
MVM_args_set_result_obj(tc, type_obj, MVM_RETURN_CURRENT_FRAME);
}
#define introspect_member(member, set_result, result) \
static void member(MVMThreadContext *tc, MVMCallsite *callsite, MVMRegister *args) { \
MVMObject *self, *type_obj, *member; \
MVMArgProcContext arg_ctx; \
MVM_args_proc_init(tc, &arg_ctx, callsite, args); \
MVM_args_checkarity(tc, &arg_ctx, 2, 2); \
self = MVM_args_get_required_pos_obj(tc, &arg_ctx, 0); \
type_obj = MVM_args_get_required_pos_obj(tc, &arg_ctx, 1); \
MVM_args_proc_cleanup(tc, &arg_ctx); \
if (!self || !IS_CONCRETE(self) || REPR(self)->ID != MVM_REPR_ID_KnowHOWREPR) \
MVM_exception_throw_adhoc(tc, "KnowHOW methods must be called on object instance with REPR KnowHOWREPR"); \
member = (MVMObject *)((MVMKnowHOWREPR *)self)->body.member; \
set_result(tc, result, MVM_RETURN_CURRENT_FRAME); \
}
/* Introspects the attributes. For now just hand back real list. */
introspect_member(attributes, MVM_args_set_result_obj, attributes)
/* Introspects the methods. */
introspect_member(methods, MVM_args_set_result_obj, methods)
/* Introspects the name. */
introspect_member(name, MVM_args_set_result_str, (MVMString *)name)
/* Adds a method into the KnowHOW.HOW method table. */
static void add_knowhow_how_method(MVMThreadContext *tc, MVMKnowHOWREPR *knowhow_how,
char *name, void (*func) (MVMThreadContext *, MVMCallsite *, MVMRegister *)) {
MVMObject *BOOTCCode, *code_obj, *method_table;
MVMString *name_str;
/* Create string for name. */
name_str = MVM_string_ascii_decode_nt(tc, tc->instance->VMString, name);
/* Allocate a BOOTCCode and put pointer in. */
BOOTCCode = tc->instance->boot_types.BOOTCCode;
code_obj = REPR(BOOTCCode)->allocate(tc, STABLE(BOOTCCode));
((MVMCFunction *)code_obj)->body.func = func;
/* Add into the table. */
method_table = knowhow_how->body.methods;
MVM_repr_bind_key_o(tc, method_table, name_str, code_obj);
}
/* Bootstraps the KnowHOW type. */
static void bootstrap_KnowHOW(MVMThreadContext *tc) {
MVMObject *VMString = tc->instance->VMString;
/* Create our KnowHOW type object. Note we don't have a HOW just yet, so
* pass in NULL. */
const MVMREPROps *REPR = MVM_repr_get_by_id(tc, MVM_REPR_ID_KnowHOWREPR);
MVMObject *knowhow = REPR->type_object_for(tc, NULL);
/* We create a KnowHOW instance that can describe itself. This means
* (once we tie the knot) that .HOW.HOW.HOW.HOW etc will always return
* that, which closes the model up. Note that the STable for it must
* be allocated first, since that holds the allocation size. */
MVMKnowHOWREPR *knowhow_how;
MVMSTable *st = MVM_gc_allocate_stable(tc, REPR, NULL);
st->WHAT = (MVMObject *)knowhow;
st->size = sizeof(MVMKnowHOWREPR);
knowhow_how = (MVMKnowHOWREPR *)REPR->allocate(tc, st);
st->HOW = (MVMObject *)knowhow_how;
knowhow_how->common.st = st;
/* Add various methods to the KnowHOW's HOW. */
REPR->initialize(tc, NULL, (MVMObject *)knowhow_how, &knowhow_how->body);
add_knowhow_how_method(tc, knowhow_how, "new_type", new_type);
add_knowhow_how_method(tc, knowhow_how, "add_method", add_method);
add_knowhow_how_method(tc, knowhow_how, "add_attribute", add_attribute);
add_knowhow_how_method(tc, knowhow_how, "compose", compose);
add_knowhow_how_method(tc, knowhow_how, "attributes", attributes);
add_knowhow_how_method(tc, knowhow_how, "methods", methods);
add_knowhow_how_method(tc, knowhow_how, "name", name);
/* Set name KnowHOW for the KnowHOW's HOW. */
knowhow_how->body.name = MVM_string_ascii_decode_nt(tc, VMString, "KnowHOW");
/* Set this built up HOW as the KnowHOW's HOW. */
STABLE(knowhow)->HOW = (MVMObject *)knowhow_how;
/* Give it an authoritative method cache; this in turn will make the
* method dispatch bottom out. */
STABLE(knowhow)->method_cache = knowhow_how->body.methods;
STABLE(knowhow)->mode_flags = MVM_METHOD_CACHE_AUTHORITATIVE;
STABLE(knowhow_how)->method_cache = knowhow_how->body.methods;
STABLE(knowhow_how)->mode_flags = MVM_METHOD_CACHE_AUTHORITATIVE;
/* Stash the created KnowHOW. */
tc->instance->KnowHOW = (MVMObject *)knowhow;
MVM_gc_root_add_permanent_desc(tc, (MVMCollectable **)&tc->instance->KnowHOW, "KnowHOW");
}
/* Takes a stub object that existed before we had bootstrapped things and
* gives it a meta-object. */
static void add_meta_object(MVMThreadContext *tc, MVMObject *type_obj, char *name) {
MVMObject *meta_obj;
MVMString *name_str;
/* Create meta-object. */
meta_obj = MVM_repr_alloc_init(tc, STABLE(tc->instance->KnowHOW)->HOW);
MVMROOT(tc, meta_obj, {
/* Put it in place. */
MVM_ASSIGN_REF(tc, &(STABLE(type_obj)->header), STABLE(type_obj)->HOW, meta_obj);
/* Set name. */
name_str = MVM_string_ascii_decode_nt(tc, tc->instance->VMString, name);
MVM_ASSIGN_REF(tc, &(meta_obj->header), ((MVMKnowHOWREPR *)meta_obj)->body.name, name_str);
type_obj->st->debug_name = strdup(name);
});
}
/* Creates a new attribute meta-object. */
static void attr_new(MVMThreadContext *tc, MVMCallsite *callsite, MVMRegister *args) {
MVMObject *self, *obj;
MVMArgInfo type_arg, name_arg, bt_arg;
const MVMREPROps *repr;
MVMInstance *instance = tc->instance;
/* Process arguments. */
MVMArgProcContext arg_ctx;
MVM_args_proc_init(tc, &arg_ctx, callsite, args);
MVM_args_checkarity(tc, &arg_ctx, 1, 1);
self = MVM_args_get_required_pos_obj(tc, &arg_ctx, 0);
name_arg = MVM_args_get_named_str(tc, &arg_ctx, instance->str_consts.name, MVM_ARG_REQUIRED);
type_arg = MVM_args_get_named_obj(tc, &arg_ctx, instance->str_consts.type, MVM_ARG_OPTIONAL);
bt_arg = MVM_args_get_named_int(tc, &arg_ctx, instance->str_consts.box_target, MVM_ARG_OPTIONAL);
MVM_args_proc_cleanup(tc, &arg_ctx);
/* Anchor all the things. */
MVM_gc_root_temp_push(tc, (MVMCollectable **)&name_arg);
MVM_gc_root_temp_push(tc, (MVMCollectable **)&type_arg);
/* Allocate attribute object. */
repr = MVM_repr_get_by_id(tc, MVM_REPR_ID_KnowHOWAttributeREPR);
obj = repr->allocate(tc, STABLE(self));
/* Populate it. */
MVM_ASSIGN_REF(tc, &(obj->header), ((MVMKnowHOWAttributeREPR *)obj)->body.name, name_arg.arg.s);
MVM_ASSIGN_REF(tc, &(obj->header), ((MVMKnowHOWAttributeREPR *)obj)->body.type, type_arg.exists ? type_arg.arg.o : tc->instance->KnowHOW);
((MVMKnowHOWAttributeREPR *)obj)->body.box_target = bt_arg.exists ? bt_arg.arg.i64 : 0;
/* Return produced object. */
MVM_gc_root_temp_pop_n(tc, 2);
MVM_args_set_result_obj(tc, obj, MVM_RETURN_CURRENT_FRAME);
}
/* Composes the attribute; actually, nothing to do really. */
static void attr_compose(MVMThreadContext *tc, MVMCallsite *callsite, MVMRegister *args) {
MVMObject *self;
MVMArgProcContext arg_ctx;
MVM_args_proc_init(tc, &arg_ctx, callsite, args);
MVM_args_checkarity(tc, &arg_ctx, 1, 1);
self = MVM_args_get_required_pos_obj(tc, &arg_ctx, 0);
MVM_args_proc_cleanup(tc, &arg_ctx);
MVM_args_set_result_obj(tc, self, MVM_RETURN_CURRENT_FRAME);
}
/* Introspects the attribute's name. */
static void attr_name(MVMThreadContext *tc, MVMCallsite *callsite, MVMRegister *args) {
MVMObject *self;
MVMString *name;
MVMArgProcContext arg_ctx;
MVM_args_proc_init(tc, &arg_ctx, callsite, args);
MVM_args_checkarity(tc, &arg_ctx, 1, 1);
self = MVM_args_get_required_pos_obj(tc, &arg_ctx, 0);
MVM_args_proc_cleanup(tc, &arg_ctx);
name = ((MVMKnowHOWAttributeREPR *)self)->body.name;
MVM_args_set_result_str(tc, name, MVM_RETURN_CURRENT_FRAME);
}
/* Introspects the attribute's type. */
static void attr_type(MVMThreadContext *tc, MVMCallsite *callsite, MVMRegister *args) {
MVMObject *self, *type;
MVMArgProcContext arg_ctx;
MVM_args_proc_init(tc, &arg_ctx, callsite, args);
MVM_args_checkarity(tc, &arg_ctx, 1, 1);
self = MVM_args_get_required_pos_obj(tc, &arg_ctx, 0);
MVM_args_proc_cleanup(tc, &arg_ctx);
type = ((MVMKnowHOWAttributeREPR *)self)->body.type;
MVM_args_set_result_obj(tc, type, MVM_RETURN_CURRENT_FRAME);
}
/* Introspects the attribute's box target flag. */
static void attr_box_target(MVMThreadContext *tc, MVMCallsite *callsite, MVMRegister *args) {
MVMObject *self;
MVMint64 box_target;
MVMArgProcContext arg_ctx;
MVM_args_proc_init(tc, &arg_ctx, callsite, args);
MVM_args_checkarity(tc, &arg_ctx, 1, 1);
self = MVM_args_get_required_pos_obj(tc, &arg_ctx, 0);
MVM_args_proc_cleanup(tc, &arg_ctx);
box_target = ((MVMKnowHOWAttributeREPR *)self)->body.box_target;
MVM_args_set_result_int(tc, box_target, MVM_RETURN_CURRENT_FRAME);
}
/* Creates and installs the KnowHOWAttribute type. */
static void create_KnowHOWAttribute(MVMThreadContext *tc) {
MVMObject *meta_obj, *type_obj;
MVMString *name_str;
const MVMREPROps *repr;
/* Create meta-object. */
meta_obj = MVM_repr_alloc_init(tc, STABLE(tc->instance->KnowHOW)->HOW);
MVMROOT(tc, meta_obj, {
/* Add methods. */
add_knowhow_how_method(tc, (MVMKnowHOWREPR *)meta_obj, "new", attr_new);
add_knowhow_how_method(tc, (MVMKnowHOWREPR *)meta_obj, "compose", attr_compose);
add_knowhow_how_method(tc, (MVMKnowHOWREPR *)meta_obj, "name", attr_name);
add_knowhow_how_method(tc, (MVMKnowHOWREPR *)meta_obj, "type", attr_type);
add_knowhow_how_method(tc, (MVMKnowHOWREPR *)meta_obj, "box_target", attr_box_target);
/* Set name. */
name_str = MVM_string_ascii_decode_nt(tc, tc->instance->VMString, "KnowHOWAttribute");
MVM_ASSIGN_REF(tc, &(meta_obj->header), ((MVMKnowHOWREPR *)meta_obj)->body.name, name_str);
/* Create a new type object with the correct REPR. */
repr = MVM_repr_get_by_id(tc, MVM_REPR_ID_KnowHOWAttributeREPR);
type_obj = repr->type_object_for(tc, meta_obj);
/* Set up method dispatch cache. */
STABLE(type_obj)->method_cache = ((MVMKnowHOWREPR *)meta_obj)->body.methods;
STABLE(type_obj)->mode_flags = MVM_METHOD_CACHE_AUTHORITATIVE;
/* Stash the created type object. */
tc->instance->KnowHOWAttribute = (MVMObject *)type_obj;
MVM_gc_root_add_permanent_desc(tc,
(MVMCollectable **)&tc->instance->KnowHOWAttribute,
"KnowHOWAttribute");
});
}
/* Bootstraps a typed array. */
static MVMObject * boot_typed_array(MVMThreadContext *tc, char *name, MVMObject *type) {
MVMBoolificationSpec *bs;
MVMObject *repr_info;
MVMInstance *instance = tc->instance;
const MVMREPROps *repr = MVM_repr_get_by_id(tc, MVM_REPR_ID_VMArray);
MVMObject *array = repr->type_object_for(tc, NULL);
MVMROOT(tc, array, {
/* Give it a meta-object. */
add_meta_object(tc, array, name);
/* Now need to compose it with the specified type. */
repr_info = MVM_repr_alloc_init(tc, tc->instance->boot_types.BOOTHash);
MVMROOT(tc, repr_info, {
MVMObject *arr_info = MVM_repr_alloc_init(tc, tc->instance->boot_types.BOOTHash);
MVM_repr_bind_key_o(tc, arr_info, instance->str_consts.type, type);
MVM_repr_bind_key_o(tc, repr_info, instance->str_consts.array, arr_info);
MVM_repr_compose(tc, array, repr_info);
});
/* Also give it a boolification spec. */
bs = MVM_malloc(sizeof(MVMBoolificationSpec));
bs->mode = MVM_BOOL_MODE_HAS_ELEMS;
bs->method = NULL;
array->st->boolification_spec = bs;
});
return array;
}
/* Sets up the core serialization context. It is marked as the SC of various
* rooted objects, which means in turn it will never be collected. */
static void setup_core_sc(MVMThreadContext *tc) {
MVMString *handle = MVM_string_ascii_decode_nt(tc,
tc->instance->VMString, "__6MODEL_CORE__");
MVMSerializationContext * const sc = (MVMSerializationContext *)MVM_sc_create(tc, handle);
MVMint32 obj_index = 0;
MVMint32 st_index = 0;
#define add_to_sc_with_st(tc, sc, variable) do { \
MVM_sc_set_object(tc, sc, obj_index++, variable); \
MVM_sc_set_obj_sc(tc, variable, sc); \
MVM_sc_set_stable(tc, sc, st_index++, STABLE(variable)); \
MVM_sc_set_stable_sc(tc, STABLE(variable), sc); \
} while (0)
#define add_to_sc_with_st_and_mo(tc, sc, variable) do { \
add_to_sc_with_st(tc, sc, variable); \
MVM_sc_set_object(tc, sc, obj_index++, STABLE(variable)->HOW); \
MVM_sc_set_obj_sc(tc, STABLE(variable)->HOW, sc); \
} while (0)
/* KnowHOW */
add_to_sc_with_st(tc, sc, tc->instance->KnowHOW);
/* KnowHOW.HOW */
add_to_sc_with_st(tc, sc, STABLE(tc->instance->KnowHOW)->HOW);
/* KnowHOWAttribute */
add_to_sc_with_st(tc, sc, tc->instance->KnowHOWAttribute);
/* BOOT* */
add_to_sc_with_st_and_mo(tc, sc, tc->instance->boot_types.BOOTArray);
add_to_sc_with_st_and_mo(tc, sc, tc->instance->boot_types.BOOTHash);
add_to_sc_with_st_and_mo(tc, sc, tc->instance->boot_types.BOOTIter);
add_to_sc_with_st_and_mo(tc, sc, tc->instance->boot_types.BOOTInt);
add_to_sc_with_st_and_mo(tc, sc, tc->instance->boot_types.BOOTNum);
add_to_sc_with_st_and_mo(tc, sc, tc->instance->boot_types.BOOTStr);
add_to_sc_with_st_and_mo(tc, sc, tc->instance->boot_types.BOOTCode);
}
/* Sets up some string constants. */
static void string_consts(MVMThreadContext *tc) {
MVMInstance * const instance = tc->instance;
/* Set up some strings. */
#define string_creator(variable, name) do { \
instance->str_consts.variable = MVM_string_ascii_decode_nt(tc, tc->instance->VMString, (name)); \
MVM_gc_root_add_permanent_desc(tc, (MVMCollectable **)&(instance->str_consts.variable), "VM string constant"); \
} while (0)
string_creator(empty, "");
string_creator(Int, "Int");
string_creator(Str, "Str");
string_creator(Num, "Num");
string_creator(integer, "integer");
string_creator(float_str, "float");
string_creator(bits, "bits");
string_creator(unsigned_str, "unsigned");
string_creator(find_method, "find_method");
string_creator(type_check, "type_check");
string_creator(accepts_type, "accepts_type");
string_creator(name, "name");
string_creator(attribute, "attribute");
string_creator(of, "of");
string_creator(rw, "rw");
string_creator(type, "type");
string_creator(typeobj, "typeobj");
string_creator(free_str, "free_str");
string_creator(callback_args, "callback_args");
string_creator(encoding, "encoding");
string_creator(inlined, "inlined");
string_creator(repr, "repr");
string_creator(anon, "<anon>");
string_creator(P6opaque, "P6opaque");
string_creator(box_target, "box_target");
string_creator(array, "array");
string_creator(positional_delegate, "positional_delegate");
string_creator(associative_delegate, "associative_delegate");
string_creator(auto_viv_container, "auto_viv_container");
string_creator(done, "done");
string_creator(error, "error");
string_creator(stdout_bytes, "stdout_bytes");
string_creator(stderr_bytes, "stderr_bytes");
string_creator(merge_bytes, "merge_bytes");
string_creator(buf_type, "buf_type");
string_creator(write, "write");
string_creator(stdin_fd, "stdin_fd");
string_creator(stdout_fd, "stdout_fd");
string_creator(stderr_fd, "stderr_fd");
string_creator(nativeref, "nativeref");
string_creator(refkind, "refkind");
string_creator(positional, "positional");
string_creator(lexical, "lexical");
string_creator(dimensions, "dimensions");
string_creator(ready, "ready");
string_creator(multidim, "multidim");
string_creator(entry_point, "entry_point");
string_creator(kind, "kind");
string_creator(instrumented, "instrumented");
string_creator(heap, "heap");
string_creator(translate_newlines, "translate_newlines");
string_creator(platform_newline, MVM_TRANSLATE_NEWLINE_OUTPUT ? "\r\n" : "\n");
string_creator(path, "path");
string_creator(config, "config");
string_creator(replacement, "replacement");
}
/* Drives the overall bootstrap process. */
void MVM_6model_bootstrap(MVMThreadContext *tc) {
/* First, we have to get the VMString type to exist; this has to
* come even before REPR registry setup because it relies on
* being able to create strings. */
create_stub_VMString(tc);
/* Set up some string constants commonly used. */
string_consts(tc);
/* Now we've enough to actually create the REPR registry. */
MVM_repr_initialize_registry(tc);
/* Create stub VMNull, BOOTInt, BOOTNum, BOOTStr, BOOTArray, BOOTHash,
* BOOTCCode, BOOTCode, BOOTThread, BOOTIter, BOOTContext, SCRef,
* CallCapture, BOOTIO, BOOTException, BOOTQueue, BOOTAsync,
* and BOOTReentrantMutex types. */
#define create_stub_boot_type(tc, reprid, slot, makeboolspec, boolspec) do { \
const MVMREPROps *repr = MVM_repr_get_by_id(tc, reprid); \
MVMObject *type = tc->instance->slot = repr->type_object_for(tc, NULL); \
if (makeboolspec) { \
MVMBoolificationSpec *bs; \
bs = MVM_malloc(sizeof(MVMBoolificationSpec)); \
bs->mode = boolspec; \
bs->method = NULL; \
type->st->boolification_spec = bs; \
} \
} while (0)
create_stub_boot_type(tc, MVM_REPR_ID_MVMNull, VMNull, 0, MVM_BOOL_MODE_NOT_TYPE_OBJECT);
create_stub_boot_type(tc, MVM_REPR_ID_P6int, boot_types.BOOTInt, 1, MVM_BOOL_MODE_UNBOX_INT);
create_stub_boot_type(tc, MVM_REPR_ID_P6num, boot_types.BOOTNum, 1, MVM_BOOL_MODE_UNBOX_NUM);
create_stub_boot_type(tc, MVM_REPR_ID_P6str, boot_types.BOOTStr, 1, MVM_BOOL_MODE_UNBOX_STR_NOT_EMPTY);
create_stub_boot_type(tc, MVM_REPR_ID_VMArray, boot_types.BOOTArray, 1, MVM_BOOL_MODE_HAS_ELEMS);
create_stub_boot_type(tc, MVM_REPR_ID_MVMHash, boot_types.BOOTHash, 1, MVM_BOOL_MODE_HAS_ELEMS);
create_stub_boot_type(tc, MVM_REPR_ID_MVMCFunction, boot_types.BOOTCCode, 0, MVM_BOOL_MODE_NOT_TYPE_OBJECT);
create_stub_boot_type(tc, MVM_REPR_ID_MVMCode, boot_types.BOOTCode, 0, MVM_BOOL_MODE_NOT_TYPE_OBJECT);
create_stub_boot_type(tc, MVM_REPR_ID_MVMThread, boot_types.BOOTThread, 0, MVM_BOOL_MODE_NOT_TYPE_OBJECT);
create_stub_boot_type(tc, MVM_REPR_ID_MVMIter, boot_types.BOOTIter, 1, MVM_BOOL_MODE_ITER);
create_stub_boot_type(tc, MVM_REPR_ID_MVMContext, boot_types.BOOTContext, 0, MVM_BOOL_MODE_NOT_TYPE_OBJECT);
create_stub_boot_type(tc, MVM_REPR_ID_SCRef, SCRef, 0, MVM_BOOL_MODE_NOT_TYPE_OBJECT);
create_stub_boot_type(tc, MVM_REPR_ID_MVMCallCapture, CallCapture, 0, MVM_BOOL_MODE_NOT_TYPE_OBJECT);
create_stub_boot_type(tc, MVM_REPR_ID_MVMOSHandle, boot_types.BOOTIO, 0, MVM_BOOL_MODE_NOT_TYPE_OBJECT);
create_stub_boot_type(tc, MVM_REPR_ID_MVMException, boot_types.BOOTException, 0, MVM_BOOL_MODE_NOT_TYPE_OBJECT);
create_stub_boot_type(tc, MVM_REPR_ID_MVMStaticFrame, boot_types.BOOTStaticFrame, 0, MVM_BOOL_MODE_NOT_TYPE_OBJECT);
create_stub_boot_type(tc, MVM_REPR_ID_MVMCompUnit, boot_types.BOOTCompUnit, 0, MVM_BOOL_MODE_NOT_TYPE_OBJECT);
create_stub_boot_type(tc, MVM_REPR_ID_MVMMultiCache, boot_types.BOOTMultiCache, 0, MVM_BOOL_MODE_NOT_TYPE_OBJECT);
create_stub_boot_type(tc, MVM_REPR_ID_MVMContinuation, boot_types.BOOTContinuation, 0, MVM_BOOL_MODE_NOT_TYPE_OBJECT);
create_stub_boot_type(tc, MVM_REPR_ID_MVMThread, Thread, 0, MVM_BOOL_MODE_NOT_TYPE_OBJECT);
create_stub_boot_type(tc, MVM_REPR_ID_ConcBlockingQueue, boot_types.BOOTQueue, 0, MVM_BOOL_MODE_NOT_TYPE_OBJECT);
create_stub_boot_type(tc, MVM_REPR_ID_MVMAsyncTask, boot_types.BOOTAsync, 0, MVM_BOOL_MODE_NOT_TYPE_OBJECT);
create_stub_boot_type(tc, MVM_REPR_ID_ReentrantMutex, boot_types.BOOTReentrantMutex, 0, MVM_BOOL_MODE_NOT_TYPE_OBJECT);
create_stub_boot_type(tc, MVM_REPR_ID_MVMSpeshLog, SpeshLog, 0, MVM_BOOL_MODE_NOT_TYPE_OBJECT);
create_stub_boot_type(tc, MVM_REPR_ID_MVMStaticFrameSpesh, StaticFrameSpesh, 0, MVM_BOOL_MODE_NOT_TYPE_OBJECT);
/* Bootstrap the KnowHOW type, giving it a meta-object. */
bootstrap_KnowHOW(tc);
/* Give stub types meta-objects. */
#define meta_objectifier(tc, slot, name) do { \
add_meta_object((tc), (tc)->instance->slot, (name)); \
MVM_gc_root_add_permanent_desc((tc), (MVMCollectable **)&(tc)->instance->slot, name); \
} while (0)
meta_objectifier(tc, VMString, "VMString");
meta_objectifier(tc, VMNull, "VMNull");
meta_objectifier(tc, boot_types.BOOTInt, "BOOTInt");
meta_objectifier(tc, boot_types.BOOTNum, "BOOTNum");
meta_objectifier(tc, boot_types.BOOTStr, "BOOTStr");
meta_objectifier(tc, boot_types.BOOTArray, "BOOTArray");
meta_objectifier(tc, boot_types.BOOTHash, "BOOTHash");
meta_objectifier(tc, boot_types.BOOTCCode, "BOOTCCode");
meta_objectifier(tc, boot_types.BOOTCode, "BOOTCode");
meta_objectifier(tc, boot_types.BOOTThread, "BOOTThread");
meta_objectifier(tc, boot_types.BOOTIter, "BOOTIter");
meta_objectifier(tc, boot_types.BOOTContext, "BOOTContext");
meta_objectifier(tc, SCRef, "SCRef");
meta_objectifier(tc, CallCapture, "CallCapture");
meta_objectifier(tc, boot_types.BOOTIO, "BOOTIO");
meta_objectifier(tc, boot_types.BOOTException, "BOOTException");
meta_objectifier(tc, boot_types.BOOTStaticFrame, "BOOTStaticFrame");
meta_objectifier(tc, boot_types.BOOTCompUnit, "BOOTCompUnit");
meta_objectifier(tc, boot_types.BOOTMultiCache, "BOOTMultiCache");
meta_objectifier(tc, boot_types.BOOTContinuation, "BOOTContinuation");
meta_objectifier(tc, Thread, "Thread");
meta_objectifier(tc, boot_types.BOOTQueue, "BOOTQueue");
meta_objectifier(tc, boot_types.BOOTAsync, "BOOTAsync");
meta_objectifier(tc, boot_types.BOOTReentrantMutex, "BOOTReentrantMutex");
/* Create the KnowHOWAttribute type. */
create_KnowHOWAttribute(tc);
/* Bootstrap typed arrays. */
tc->instance->boot_types.BOOTIntArray = boot_typed_array(tc, "BOOTIntArray",
tc->instance->boot_types.BOOTInt);
MVM_gc_root_add_permanent_desc(tc,
(MVMCollectable **)&tc->instance->boot_types.BOOTIntArray,
"BOOTIntArray");
tc->instance->boot_types.BOOTNumArray = boot_typed_array(tc, "BOOTNumArray",
tc->instance->boot_types.BOOTNum);
MVM_gc_root_add_permanent_desc(tc,
(MVMCollectable **)&tc->instance->boot_types.BOOTNumArray,
"BOOTNumArray");
tc->instance->boot_types.BOOTStrArray = boot_typed_array(tc, "BOOTStrArray",
tc->instance->boot_types.BOOTStr);
MVM_gc_root_add_permanent_desc(tc,
(MVMCollectable **)&tc->instance->boot_types.BOOTStrArray,
"BOOTStrArray");
/* Set some fields on the first TC to exist, since MVM_tc_create
* runs before the bootstrap, but tries to initialize these fields to
* VMNull regardless */
tc->last_payload = tc->instance->VMNull;
tc->plugin_guard_args = tc->instance->VMNull;
/* Set up HLL roles. */
STABLE(tc->instance->boot_types.BOOTInt)->hll_role = MVM_HLL_ROLE_INT;
STABLE(tc->instance->boot_types.BOOTNum)->hll_role = MVM_HLL_ROLE_NUM;
STABLE(tc->instance->boot_types.BOOTStr)->hll_role = MVM_HLL_ROLE_STR;
STABLE(tc->instance->boot_types.BOOTArray)->hll_role = MVM_HLL_ROLE_ARRAY;
STABLE(tc->instance->boot_types.BOOTHash)->hll_role = MVM_HLL_ROLE_HASH;
STABLE(tc->instance->boot_types.BOOTCode)->hll_role = MVM_HLL_ROLE_CODE;
/* Get initial __6MODEL_CORE__ serialization context set up. */
setup_core_sc(tc);
MVM_6model_containers_setup(tc);
MVM_intcache_for(tc, tc->instance->boot_types.BOOTInt);
}