/
Builtins.py
2890 lines (2151 loc) · 82.9 KB
/
Builtins.py
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
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright (c) 2011 King's College London, created by Laurence Tratt
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
from pypy.rlib import debug, jit, objectmodel, rarithmetic, rweakref
from pypy.rpython.lltypesystem import lltype, rffi
NUM_BUILTINS = 41
from Core import *
import Bytecode, Target, VM
BUILTIN_NULL_OBJ = 0
BUILTIN_FAIL_OBJ = 1
# Core atom defs
BUILTIN_ATOM_DEF_OBJECT = 2
BUILTIN_SLOTS_ATOM_DEF_OBJECT = 3
BUILTIN_CLASS_ATOM_DEF_OBJECT = 4
BUILTIN_VM_ATOM_DEF_OBJECT = 5
BUILTIN_THREAD_ATOM_DEF_OBJECT = 6
BUILTIN_FUNC_ATOM_DEF_OBJECT = 7
BUILTIN_STRING_ATOM_DEF_OBJECT = 8
BUILTIN_CON_STACK_ATOM_DEF_OBJECT = 9
BUILTIN_LIST_ATOM_DEF_OBJECT = 10
BUILTIN_DICT_ATOM_DEF_OBJECT = 11
BUILTIN_MODULE_ATOM_DEF_OBJECT = 12
BUILTIN_INT_ATOM_DEF_OBJECT = 13
BUILTIN_UNIQUE_ATOM_DEF_OBJECT = 14
BUILTIN_CLOSURE_ATOM_DEF_OBJECT = 15
BUILTIN_PARTIAL_APPLICATION_ATOM_DEF_OBJECT = 16
BUILTIN_EXCEPTION_ATOM_DEF_OBJECT = 17
BUILTIN_SET_ATOM_DEF_OBJECT = 18
# Core classes
BUILTIN_OBJECT_CLASS = 19
BUILTIN_CLASS_CLASS = 20
BUILTIN_VM_CLASS = 21
BUILTIN_THREAD_CLASS = 22
BUILTIN_FUNC_CLASS = 23
BUILTIN_STRING_CLASS = 24
BUILTIN_CON_STACK_CLASS = 25
BUILTIN_LIST_CLASS = 26
BUILTIN_DICT_CLASS = 27
BUILTIN_MODULE_CLASS = 28
BUILTIN_INT_CLASS = 29
BUILTIN_CLOSURE_CLASS = 30
BUILTIN_PARTIAL_APPLICATION_CLASS = 31
BUILTIN_EXCEPTION_CLASS = 32
BUILTIN_SET_CLASS = 33
BUILTIN_NUMBER_CLASS = 34
BUILTIN_BUILTINS_MODULE = 35
BUILTIN_C_FILE_MODULE = 36
BUILTIN_EXCEPTIONS_MODULE = 37
BUILTIN_SYS_MODULE = 38
# Floats
BUILTIN_FLOAT_ATOM_DEF_OBJECT = 39
BUILTIN_FLOAT_CLASS = 40
################################################################################
# Con_Object
#
class Con_Object(Con_Thingy):
__slots__ = ()
class Version(object):
pass
# This map class is inspired by:
# http://morepypy.blogspot.com/2011/03/controlling-tracing-of-interpreter-with_21.html
class _Con_Map(object):
__slots__ = ("index_map", "other_maps")
_immutable_fields_ = ("index_map", "other_maps")
def __init__(self):
self.index_map = {}
self.other_maps = {}
@jit.elidable
def find(self, n):
return self.index_map.get(n, -1)
@jit.elidable
def extend(self, n):
if n not in self.other_maps:
nm = _Con_Map()
nm.index_map.update(self.index_map)
nm.index_map[n] = len(self.index_map)
self.other_maps[n] = nm
return self.other_maps[n]
@jit.elidable
def size(self):
return len(self.index_map)
_EMPTY_MAP = _Con_Map()
BASIC_SLOTS_SIZE = 4
class Con_Boxed_Object(Con_Object):
__slots__ = ("instance_of", "slots_map", "s1", "s2", "s3", "s4", "extra_slots")
def __init__(self, vm, instance_of=None):
if instance_of is None:
self.instance_of = vm.get_builtin(BUILTIN_OBJECT_CLASS)
else:
self.instance_of = instance_of
self.slots_map = _EMPTY_MAP
self.s1 = self.s2 = self.s3 = self.s4 = None
self.extra_slots = None
def has_slot(self, vm, n):
m = jit.promote(self.slots_map)
i = m.find(n)
if i != -1:
return True
return False
def find_slot(self, vm, n):
o = None
m = jit.promote(self.slots_map)
i = m.find(n)
if i != -1:
if i == 0:
o = self.s1
elif i == 1:
o = self.s2
elif i == 2:
o = self.s3
elif i == 3:
o = self.s4
else:
o = self.extra_slots[i - BASIC_SLOTS_SIZE]
if o is None:
o = self.instance_of.find_field(vm, n)
if o is None:
if n == "instance_of":
o = self.instance_of
if o is None:
return o
if isinstance(o, Con_Func) and o.is_bound:
return Con_Partial_Application(vm, o, [self])
return o
def get_slot(self, vm, n):
o = None
m = jit.promote(self.slots_map)
i = m.find(n)
if i != -1:
if i == 0:
o = self.s1
elif i == 1:
o = self.s2
elif i == 2:
o = self.s3
elif i == 3:
o = self.s4
else:
o = self.extra_slots[i - BASIC_SLOTS_SIZE]
if o is None:
o = self.instance_of.find_field(vm, n)
if o is None:
if n == "instance_of":
o = self.instance_of
if o is None:
vm.raise_helper("Slot_Exception", [Con_String(vm, n), self])
if isinstance(o, Con_Func) and o.is_bound:
return Con_Partial_Application(vm, o, [self])
return o
def set_slot(self, vm, n, o):
assert o is not None
m = jit.promote(self.slots_map)
i = m.find(n)
if i == -1:
self.slots_map = m.extend(n)
i = self.slots_map.size() - 1
if i == BASIC_SLOTS_SIZE:
self.extra_slots = [o]
elif i > BASIC_SLOTS_SIZE:
self.extra_slots.append(o)
return
assert i != -1
if i == 0:
self.s1 = o
elif i == 1:
self.s2 = o
elif i == 2:
self.s3 = o
elif i == 3:
self.s4 = o
else:
self.extra_slots[i - BASIC_SLOTS_SIZE] = o
def is_(self, o):
return self is o
def add(self, vm, o):
return vm.get_slot_apply(self, "+", [o])
def subtract(self, vm, o):
return vm.get_slot_apply(self, "-", [o])
def eq(self, vm, o):
if vm.get_slot_apply(self, "==", [o], allow_fail=True):
return True
else:
return False
def neq(self, vm, o):
if vm.get_slot_apply(self, "!=", [o], allow_fail=True):
return True
else:
return False
def le(self, vm, o):
if vm.get_slot_apply(self, "<", [o], allow_fail=True):
return True
else:
return False
def le_eq(self, vm, o):
if vm.get_slot_apply(self, "<=", [o], allow_fail=True):
return True
else:
return False
def gr_eq(self, vm, o):
if vm.get_slot_apply(self, ">=", [o], allow_fail=True):
return True
else:
return False
def gt(self, vm, o):
if vm.get_slot_apply(self, ">", [o], allow_fail=True):
return True
else:
return False
@con_object_proc
def _new_func_Con_Object(vm):
(c,), vargs = vm.decode_args("O", vargs=True)
o = Con_Boxed_Object(vm, c)
vm.apply(o.get_slot(vm, "init"), vargs)
return o
@con_object_proc
def _Con_Object_find_slot(vm):
(self, sn_o),_ = vm.decode_args("OS")
assert isinstance(sn_o, Con_String)
v = self.find_slot(vm, sn_o.v)
if not v:
v = vm.get_builtin(BUILTIN_FAIL_OBJ)
return v
@con_object_proc
def _Con_Object_get_slot(vm):
(self, sn_o),_ = vm.decode_args("OS")
assert isinstance(sn_o, Con_String)
return self.get_slot(vm, sn_o.v)
@con_object_proc
def _Con_Object_init(vm):
(self,), vargs = vm.decode_args("O", vargs=True)
return vm.get_builtin(BUILTIN_NULL_OBJ)
@con_object_proc
def _Con_Object_is(vm):
(self, o),_ = vm.decode_args("OO")
if self.is_(o):
return vm.get_builtin(BUILTIN_NULL_OBJ)
else:
return vm.get_builtin(BUILTIN_FAIL_OBJ)
@con_object_proc
def _Con_Object_to_str(vm):
(self,),_ = vm.decode_args("O")
return Con_String(vm, "<Object@%x>" % objectmodel.current_object_addr_as_int(self))
def bootstrap_con_object(vm):
# This is where the hardcore bootstrapping stuff happens. Many things here are done in a very
# specific order - changing something can easily cause lots of later things to break.
object_class_nm = Con_String(vm, "Object")
object_class = Con_Class(vm, object_class_nm, [], None)
vm.set_builtin(BUILTIN_OBJECT_CLASS, object_class)
class_class_nm = Con_String(vm, "Class")
class_class = Con_Class(vm, class_class_nm, [object_class], None)
vm.set_builtin(BUILTIN_CLASS_CLASS, class_class)
object_class.instance_of = class_class
class_class.instance_of = class_class
string_class_nm = Con_String(vm, "String")
string_class = Con_Class(vm, string_class_nm, [object_class], None)
vm.set_builtin(BUILTIN_STRING_CLASS, string_class)
object_class_nm.instance_of = string_class
class_class_nm.instance_of = string_class
string_class_nm.instance_of = string_class
vm.set_builtin(BUILTIN_NULL_OBJ, Con_Boxed_Object(vm))
vm.set_builtin(BUILTIN_FAIL_OBJ, Con_Boxed_Object(vm))
module_class = Con_Class(vm, Con_String(vm, "Module"), [object_class], None)
vm.set_builtin(BUILTIN_MODULE_CLASS, module_class)
# In order that later objects can refer to the Builtins module, we have to create it now.
builtins_module = new_c_con_module(vm, "Builtins", "Builtins", __file__, None, \
["Object", "Class", "Func", "Partial_Application", "String", "Module", "Number", "Int",
"Float", "List", "Set", "Dict", "Exception"])
# We effectively initialize the Builtins module through the bootstrapping process, so it doesn't
# need a separate initialization function.
builtins_module.initialized = True
vm.set_builtin(BUILTIN_BUILTINS_MODULE, builtins_module)
vm.set_mod(builtins_module)
object_class.set_slot(vm, "container", builtins_module)
class_class.set_slot(vm, "container", builtins_module)
string_class.set_slot(vm, "container", builtins_module)
module_class.set_slot(vm, "container", builtins_module)
builtins_module.set_defn(vm, "Object", object_class)
builtins_module.set_defn(vm, "Class", class_class)
builtins_module.set_defn(vm, "String", string_class)
builtins_module.set_defn(vm, "Module", module_class)
func_class = Con_Class(vm, Con_String(vm, "Func"), [object_class], builtins_module)
vm.set_builtin(BUILTIN_FUNC_CLASS, func_class)
builtins_module.set_defn(vm, "Func", func_class)
partial_application_class = Con_Class(vm, Con_String(vm, "Partial_Application"), \
[object_class], builtins_module)
vm.set_builtin(BUILTIN_PARTIAL_APPLICATION_CLASS, partial_application_class)
builtins_module.set_defn(vm, "Partial_Application", partial_application_class)
number_class = Con_Class(vm, Con_String(vm, "Number"), [object_class], builtins_module)
vm.set_builtin(BUILTIN_NUMBER_CLASS, number_class)
builtins_module.set_defn(vm, "Number", number_class)
int_class = Con_Class(vm, Con_String(vm, "Int"), [object_class], builtins_module)
vm.set_builtin(BUILTIN_INT_CLASS, int_class)
builtins_module.set_defn(vm, "Int", int_class)
float_class = Con_Class(vm, Con_String(vm, "Float"), [object_class], builtins_module)
vm.set_builtin(BUILTIN_FLOAT_CLASS, float_class)
builtins_module.set_defn(vm, "Float", float_class)
list_class = Con_Class(vm, Con_String(vm, "List"), [object_class], builtins_module)
vm.set_builtin(BUILTIN_LIST_CLASS, list_class)
builtins_module.set_defn(vm, "List", list_class)
set_class = Con_Class(vm, Con_String(vm, "Set"), [object_class], builtins_module)
vm.set_builtin(BUILTIN_SET_CLASS, set_class)
builtins_module.set_defn(vm, "Set", set_class)
dict_class = Con_Class(vm, Con_String(vm, "Dict"), [object_class], builtins_module)
vm.set_builtin(BUILTIN_DICT_CLASS, dict_class)
builtins_module.set_defn(vm, "Dict", dict_class)
exception_class = Con_Class(vm, Con_String(vm, "Exception"), [object_class], builtins_module)
vm.set_builtin(BUILTIN_EXCEPTION_CLASS, exception_class)
builtins_module.set_defn(vm, "Exception", exception_class)
object_class.new_func = \
new_c_con_func(vm, Con_String(vm, "new_Object"), False, _new_func_Con_Object, \
builtins_module)
new_c_con_func_for_class(vm, "find_slot", _Con_Object_find_slot, object_class)
new_c_con_func_for_class(vm, "get_slot", _Con_Object_get_slot, object_class)
new_c_con_func_for_class(vm, "init", _Con_Object_init, object_class)
new_c_con_func_for_class(vm, "is", _Con_Object_is, object_class)
new_c_con_func_for_class(vm, "to_str", _Con_Object_to_str, object_class)
################################################################################
# Con_Class
#
class Con_Class(Con_Boxed_Object):
__slots__ = ("supers", "fields_map", "fields", "new_func", "version", "dependents")
_immutable_fields = ("supers", "fields", "dependents")
def __init__(self, vm, name, supers, container, instance_of=None, new_func=None):
if instance_of is None:
instance_of = vm.get_builtin(BUILTIN_CLASS_CLASS)
Con_Boxed_Object.__init__(self, vm, instance_of)
if new_func is None:
# A new object function hasn't been supplied so we need to search for one.
# See http://tratt.net/laurie/tech_articles/articles/more_meta_matters for
# more details about this algorithm.
for sc in supers:
assert isinstance(sc, Con_Class)
if new_func is None:
new_func = sc.new_func
elif new_func is not sc.new_func:
new_func = sc.new_func
object_class = vm.get_builtin(BUILTIN_OBJECT_CLASS)
assert isinstance(object_class, Con_Class)
object_class.new_func
if new_func is object_class.new_func:
new_func = sc.new_func
else:
# There's a clash between superclass's metaclasses.
raise Exception("XXX")
self.new_func = new_func
self.supers = supers
self.fields_map = _EMPTY_MAP
self.fields = []
# To optimise slot lookups, we need to be a little more cunning. We make a (reasonable)
# assumption that classes rarely change their fields (most classes never change their fields
# after their initial creation at all), so that in general we can simply elide field
# lookups entirely. When a field is changed, all subsequent field lookups on that class and
# its subclasses need to be regenerated. To force this, every class a "version" which is
# incremented whenever its fields are changed. As well as changing the class itself, all
# subclasses must be changed too. We maintain a list of all subclasses (even indirect ones!)
# to do this.
self.version = Version()
self.dependents = []
sc_stack = supers[:]
while len(sc_stack) > 0:
sc = type_check_class(vm, sc_stack.pop())
sc.dependents.append(rweakref.ref(sc))
sc_stack.extend(sc.supers)
self.set_slot(vm, "name", name)
if container:
self.set_slot(vm, "container", container)
@jit.elidable_promote("0")
def _get_field_i(self, vm, n, version):
m = jit.promote(self.fields_map)
i = m.find(n)
if i != -1:
return self.fields[i]
for s in self.supers:
assert isinstance(s, Con_Class)
o = s.find_field(vm, n)
if o is not None:
return o
return None
def find_field(self, vm, n):
return self._get_field_i(vm, n, jit.promote(self.version))
def get_field(self, vm, n):
o = self._get_field_i(vm, n, jit.promote(self.version))
if o is None:
vm.raise_helper("Field_Exception", [Con_String(vm, n), self])
return o
def set_field(self, vm, n, o):
assert o is not None
m = jit.promote(self.fields_map)
i = m.find(n)
if i == -1:
self.fields_map = m.extend(n)
self.fields.append(o)
else:
self.fields[i] = o
self.version = Version()
j = 0
while j < len(self.dependents):
dep = self.dependents[j]()
if dep is None:
del self.dependents[j]
continue
dep.version = Version()
j += 1
@con_object_proc
def _new_func_Con_Class(vm):
(c, name, supers, container), vargs = vm.decode_args("CSLO", vargs=True)
assert isinstance(c, Con_Class)
assert isinstance(name, Con_String)
assert isinstance(supers, Con_List)
o = Con_Class(vm, name, supers.l[:], container, c)
vm.apply(o.get_slot(vm, "init"), vargs)
return o
@con_object_proc
def _Con_Class_new(vm):
_, v = vm.decode_args(vargs=True)
c = type_check_class(vm, v[0])
if c.new_func is None:
p = type_check_string(vm, vm.get_slot_apply(c, "path")).v
msg = "Instance of %s has no new_func." % p
vm.raise_helper("VM_Exception", [Con_String(vm, msg)])
return vm.apply(c.new_func, v)
@con_object_proc
def _Con_Class_get_field(vm):
(self, n),_ = vm.decode_args("CS")
assert isinstance(self, Con_Class)
assert isinstance(n, Con_String)
o = self.find_field(vm, n.v)
if o is None:
vm.raise_helper("Field_Exception", [n, self])
return o
@con_object_proc
def _Con_Class_set_field(vm):
(self, n, o),_ = vm.decode_args("CSO")
assert isinstance(self, Con_Class)
assert isinstance(n, Con_String)
self.set_field(vm, n.v, o)
return vm.get_builtin(BUILTIN_NULL_OBJ)
@con_object_proc
def _Con_Class_path(vm):
(self, stop_at),_ = vm.decode_args("C", opt="o")
assert isinstance(self, Con_Class)
name = type_check_string(vm, self.get_slot(vm, "name"))
if name.v == "":
name = Con_String(vm, "<anon>")
container = self.get_slot(vm, "container")
if container is vm.get_builtin(BUILTIN_NULL_OBJ) or container is stop_at:
return name
else:
if stop_at is None:
stop_at = vm.get_builtin(BUILTIN_NULL_OBJ)
rtn = type_check_string(vm, vm.get_slot_apply(container, "path", [stop_at]))
if isinstance(container, Con_Module):
sep = "::"
else:
sep = "."
return Con_String(vm, "%s%s%s" % (rtn.v, sep, name.v))
@con_object_proc
def _Con_Class_to_str(vm):
(self,),_ = vm.decode_args("C")
assert isinstance(self, Con_Class)
nm = type_check_string(vm, self.get_slot(vm, "name"))
return Con_String(vm, "<Class %s>" % nm.v)
@con_object_proc
def _Con_Class_conformed_by(vm):
(self, o),_ = vm.decode_args("CO")
assert isinstance(self, Con_Class)
assert isinstance(o, Con_Boxed_Object)
if o.instance_of is self:
# We optimise the easy case.
return vm.get_builtin(BUILTIN_NULL_OBJ)
else:
stack = [self]
while len(stack) > 0:
cnd = stack.pop()
assert isinstance(cnd, Con_Class)
for f in cnd.fields_map.index_map.keys():
if not o.has_slot(vm, f):
return vm.get_builtin(BUILTIN_FAIL_OBJ)
stack.extend(cnd.supers)
return vm.get_builtin(BUILTIN_NULL_OBJ)
@con_object_proc
def _Con_Class_instantiated(vm):
(self, o),_ = vm.decode_args("CO")
assert isinstance(self, Con_Class)
assert isinstance(o, Con_Boxed_Object)
if o.instance_of is self:
# We optimise the easy case.
return vm.get_builtin(BUILTIN_NULL_OBJ)
else:
# What we do now is to put 'instance_of' onto a stack; if the current class on the stack
# does not match 'self', we push all the class's superclasses onto the stack.
#
# If we run off the end of the stack then there is no match.
stack = [o.instance_of]
while len(stack) > 0:
cnd = stack.pop()
assert isinstance(cnd, Con_Class)
if cnd is self:
return vm.get_builtin(BUILTIN_NULL_OBJ)
stack.extend(cnd.supers)
return vm.get_builtin(BUILTIN_FAIL_OBJ)
def bootstrap_con_class(vm):
class_class = vm.get_builtin(BUILTIN_CLASS_CLASS)
assert isinstance(class_class, Con_Class)
class_class.new_func = \
new_c_con_func(vm, Con_String(vm, "new_Class"), False, _new_func_Con_Class, \
vm.get_builtin(BUILTIN_BUILTINS_MODULE))
new_c_con_func_for_class(vm, "conformed_by", _Con_Class_conformed_by, class_class)
new_c_con_func_for_class(vm, "instantiated", _Con_Class_instantiated, class_class)
new_c_con_func_for_class(vm, "new", _Con_Class_new, class_class)
new_c_con_func_for_class(vm, "get_field", _Con_Class_get_field, class_class)
new_c_con_func_for_class(vm, "set_field", _Con_Class_set_field, class_class)
new_c_con_func_for_class(vm, "path", _Con_Class_path, class_class)
new_c_con_func_for_class(vm, "to_str", _Con_Class_to_str, class_class)
################################################################################
# Con_Module
#
class Con_Module(Con_Boxed_Object):
__slots__ = ("is_bc", "bc", "id_", "src_path", "imps", "tlvars_map", "consts",
"init_func", "values", "closure", "initialized")
_immutable_fields_ = ("is_bc", "bc", "name", "id_", "src_path", "imps", "tlvars_map",
"init_func", "consts")
def __init__(self, vm, is_bc, bc, name, id_, src_path, imps, tlvars_map, num_consts, init_func, \
instance_of=None):
if instance_of is None:
instance_of = vm.get_builtin(BUILTIN_MODULE_CLASS)
Con_Boxed_Object.__init__(self, vm, instance_of)
self.is_bc = is_bc # True for bytecode modules; False for RPython modules
self.bc = bc
self.id_ = id_
self.src_path = src_path
self.imps = imps
self.tlvars_map = tlvars_map
self.consts = [None] * num_consts
debug.make_sure_not_resized(self.consts)
self.init_func = init_func
self.values = []
if is_bc:
self.closure = None
else:
self.closure = VM.Closure(None, len(tlvars_map))
self.set_slot(vm, "name", name)
self.set_slot(vm, "src_path", Con_String(vm, src_path))
self.set_slot(vm, "mod_id", Con_String(vm, id_))
self.set_slot(vm, "container", vm.get_builtin(BUILTIN_NULL_OBJ))
self.initialized = False
def import_(self, vm):
if self.initialized:
return
if self.is_bc:
# Bytecode modules use the old "push a Con_Int onto the stack to signify how many
# parameters are being passed" hack. To add insult injury, they simply pop this object
# off without using it. So we pass null as a 'magic' parameter (since it will be popped
# first, it's actually the second parameter), knowing that it won't actually be used for
# anything.
v, self.closure = vm.apply_closure(self.init_func, \
[self, vm.get_builtin(BUILTIN_NULL_OBJ)])
else:
vm.apply(self.init_func, [self])
self.initialized = True
return
@jit.elidable_promote("0")
def get_closure_i(self, vm, n):
return self.tlvars_map.get(n, -1)
def get_defn(self, vm, n):
i = self.get_closure_i(vm, n)
if i == -1:
name = type_check_string(vm, self.get_slot(vm, "name")).v
vm.raise_helper("Mod_Defn_Exception", \
[Builtins.Con_String(vm, "No such definition '%s' in '%s'." % (n, name))])
o = self.closure.vars[i]
if o is None:
name = type_check_string(vm, self.get_slot(vm, "name")).v
vm.raise_helper("Mod_Defn_Exception", \
[Builtins.Con_String(vm, "Definition '%s' unassigned in '%s'." % (n, name))])
return o
@jit.elidable_promote("0")
def has_defn(self, vm, n):
if self.get_closure_i(vm, n) == -1:
return False
return True
def set_defn(self, vm, n, o):
i = self.get_closure_i(vm, n)
if i == -1:
name = type_check_string(vm, self.get_slot(vm, "name")).v
vm.raise_helper("Mod_Defn_Exception", \
[Builtins.Con_String(vm, "No such definition '%s' in '%s'." % (n, name))])
self.closure.vars[i] = o
@jit.elidable_promote("0")
def get_const(self, vm, i):
v = self.consts[i]
if v is not None:
return v
consts_offs = Target.read_word(self.bc, Target.BC_MOD_CONSTANTS_OFFSETS)
const_off = Target.read_word(self.bc, Target.BC_MOD_CONSTANTS) \
+ Target.read_word(self.bc, consts_offs + i * Target.INTSIZE)
type = Target.read_word(self.bc, const_off)
if type == Target.CONST_INT:
v = Con_Int(vm, Target.read_word(self.bc, const_off + Target.INTSIZE))
elif type == Target.CONST_FLOAT:
v = Con_Float(vm, Target.read_float(self.bc, const_off + Target.INTSIZE))
else:
assert type == Target.CONST_STRING
s = Target.extract_str(self.bc,
const_off + Target.INTSIZE + Target.INTSIZE,
Target.read_word(self.bc, const_off + Target.INTSIZE))
v = Con_String(vm, s)
self.consts[i] = v
return v
def bc_off_to_src_infos(self, vm, bc_off):
bc = self.bc
cur_bc_off = Target.read_word(bc, Target.BC_MOD_INSTRUCTIONS)
instr_i = 0
while cur_bc_off < bc_off:
instr = Target.read_word(bc, cur_bc_off)
it = Target.get_instr(instr)
if it == Target.CON_INSTR_EXBI:
start, size = Target.unpack_exbi(instr)
cur_bc_off += Target.align(start + size)
elif it == Target.CON_INSTR_IS_ASSIGNED:
cur_bc_off += Target.INTSIZE + Target.INTSIZE
elif it == Target.CON_INSTR_SLOT_LOOKUP or it == Target.CON_INSTR_PRE_SLOT_LOOKUP_APPLY:
start, size = Target.unpack_slot_lookup(instr)
cur_bc_off += Target.align(start + size)
elif it == Target.CON_INSTR_ASSIGN_SLOT:
start, size = Target.unpack_assign_slot(instr)
cur_bc_off += Target.align(start + size)
elif it == Target.CON_INSTR_UNPACK_ARGS:
num_args, has_vargs = Target.unpack_unpack_args(instr)
cur_bc_off += Target.INTSIZE + num_args * Target.INTSIZE
if has_vargs:
cur_bc_off += Target.INTSIZE
elif it == Target.CON_INSTR_MODULE_LOOKUP:
start, size = Target.unpack_mod_lookup(instr)
cur_bc_off += Target.align(start + size)
elif it == Target.CON_INSTR_VAR_LOOKUP \
or it == Target.CON_INSTR_VAR_ASSIGN \
or it == Target.CON_INSTR_ADD_FAILURE_FRAME \
or it == Target.CON_INSTR_ADD_FAIL_UP_FRAME \
or it == Target.CON_INSTR_REMOVE_FAILURE_FRAME \
or it == Target.CON_INSTR_IS \
or it == Target.CON_INSTR_FAIL_NOW \
or it == Target.CON_INSTR_POP \
or it == Target.CON_INSTR_IMPORT \
or it == Target.CON_INSTR_LIST \
or it == Target.CON_INSTR_APPLY \
or it == Target.CON_INSTR_FUNC_DEFN \
or it == Target.CON_INSTR_RETURN \
or it == Target.CON_INSTR_BRANCH \
or it == Target.CON_INSTR_YIELD \
or it == Target.CON_INSTR_DICT \
or it == Target.CON_INSTR_DUP \
or it == Target.CON_INSTR_PULL \
or it == Target.CON_INSTR_BUILTIN_LOOKUP \
or it == Target.CON_INSTR_EYIELD \
or it == Target.CON_INSTR_ADD_EXCEPTION_FRAME \
or it == Target.CON_INSTR_REMOVE_EXCEPTION_FRAME \
or it == Target.CON_INSTR_RAISE \
or it == Target.CON_INSTR_SET \
or it == Target.CON_INSTR_BRANCH_IF_NOT_FAIL \
or it == Target.CON_INSTR_BRANCH_IF_FAIL \
or it == Target.CON_INSTR_CONST_GET \
or it == Target.CON_INSTR_UNPACK_ASSIGN \
or it == Target.CON_INSTR_EQ \
or it == Target.CON_INSTR_NEQ \
or it == Target.CON_INSTR_GT \
or it == Target.CON_INSTR_LE \
or it == Target.CON_INSTR_LE_EQ \
or it == Target.CON_INSTR_GR_EQ \
or it == Target.CON_INSTR_ADD \
or it == Target.CON_INSTR_SUBTRACT:
cur_bc_off += Target.INTSIZE
else:
print it
raise Exception("XXX")
instr_i += 1
assert cur_bc_off == bc_off
src_info_pos = src_info_num = 0
src_infos_off = Target.read_word(bc, Target.BC_MOD_SRC_POSITIONS)
while 1:
src_info1 = Target.read_uint32_word(bc, src_infos_off + src_info_pos * 4)
if src_info_num + (src_info1 & ((1 << 4) - 1)) > instr_i:
break
src_info_num += src_info1 & ((1 << 4) - 1)
while src_info1 & (1 << 4):
src_info_pos += 2
src_info1 = Target.read_uint32_word(bc, src_infos_off + src_info_pos * 4)
src_info_pos += 2
src_infos = []
while 1:
src_info1 = Target.read_uint32_word(bc, src_infos_off + src_info_pos * 4)
src_info2 = Target.read_uint32_word(bc, src_infos_off + (src_info_pos + 1) * 4)
if src_info2 & ((1 << 12) - 1) == ((1 << 12) - 1):
mod_id = self.id_
else:
mod_id = self.imps[src_info2 & ((1 << 12) - 1)]
src_off = (src_info1 >> 5) & ((1 << (31 - 5)) - 1)
src_len = src_info2 >> 12
src_info = Con_List(vm, \
[Con_String(vm, mod_id), Con_Int(vm, src_off), Con_Int(vm, src_len)])
src_infos.append(src_info)
if not (src_info1 & (1 << 4)):
break
src_info_pos += 2
return Con_List(vm, src_infos)
@con_object_proc
def _new_func_Con_Module(vm):
(class_, bc_o), vargs = vm.decode_args("CS", vargs=True)
assert isinstance(bc_o, Con_String)
bc = rffi.str2charp(bc_o.v)
mod = Bytecode.mk_mod(vm, bc, 0)
return mod
@con_object_proc
def _Con_Module_get_defn(vm):
(self, n),_ = vm.decode_args("MS")
assert isinstance(self, Con_Module)
assert isinstance(n, Con_String)
return self.get_defn(vm, n.v)
@con_object_proc
def _Con_Module_has_defn(vm):
(self, n),_ = vm.decode_args("MS")
assert isinstance(self, Con_Module)
assert isinstance(n, Con_String)
if self.has_defn(vm, n.v):
r_o = vm.get_builtin(BUILTIN_NULL_OBJ)
else:
r_o = vm.get_builtin(BUILTIN_FAIL_OBJ)
return r_o
@con_object_proc
def _Con_Module_set_defn(vm):
(self, n, o),_ = vm.decode_args("MSO")
assert isinstance(self, Con_Module)
assert isinstance(n, Con_String)
self.set_defn(vm, n.v, o)
return vm.get_builtin(BUILTIN_NULL_OBJ)
@con_object_gen
def _Con_Module_iter_defns(vm):
(self,),_ = vm.decode_args("M")
assert isinstance(self, Con_Module)
for d in self.tlvars_map.keys():
yield Con_List(vm, [Con_String(vm, d), self.get_defn(vm, d)])
@con_object_gen
def _Con_Module_iter_newlines(vm):
(self,),_ = vm.decode_args("M")
assert isinstance(self, Con_Module)
bc = self.bc
newlines_off = Target.read_word(bc, Target.BC_MOD_NEWLINES)
for i in range(Target.read_word(bc, Target.BC_MOD_NUM_NEWLINES)):
yield Con_Int(vm, Target.read_word(bc, newlines_off + i * Target.INTSIZE))
@con_object_proc
def _Con_Module_path(vm):
(self, stop_at),_ = vm.decode_args("M", opt="o")
assert isinstance(self, Con_Module)
if self is stop_at:
return Con_String(vm, "")
name = type_check_string(vm, self.get_slot(vm, "name"))
container = self.get_slot(vm, "container")
if container is vm.get_builtin(BUILTIN_NULL_OBJ) or container is stop_at:
return name
else:
if stop_at is None:
stop_at = vm.get_builtin(BUILTIN_NULL_OBJ)
rtn = type_check_string(vm, vm.get_slot_apply(container, "path", [stop_at]))
if isinstance(container, Con_Module):
sep = "::"
else:
sep = "."
return Con_String(vm, "%s%s%s" % (rtn.v, sep, name.v))
def bootstrap_con_module(vm):
module_class = vm.get_builtin(BUILTIN_MODULE_CLASS)
assert isinstance(module_class, Con_Class)