-
Notifications
You must be signed in to change notification settings - Fork 0
/
bytecode.py
1396 lines (1210 loc) · 44.1 KB
/
bytecode.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
#from __future__ import print_function
import struct, sys, array, StringIO, os, math
import colorama
from ocaml_values import *
from ocaml_marshal import unmarshal, parse_executable
from rpython.rlib.objectmodel import always_inline
from rpython.rlib.rarithmetic import r_uint, intmask
from rpython.rlib.jit import JitDriver, hint, we_are_jitted, dont_look_inside, look_inside, unroll_safe, promote, elidable
def trace_call(env, args):
if dbg:
dbg('call', code_val(env), 'args:', args)
#dbg('env', env)
opcode_list = [ 'OP_ACC0', 'OP_ACC1', 'OP_ACC2', 'OP_ACC3', 'OP_ACC4', 'OP_ACC5', 'OP_ACC6', 'OP_ACC7', 'OP_ACC', 'OP_PUSH', 'OP_PUSHACC0', 'OP_PUSHACC1', 'OP_PUSHACC2', 'OP_PUSHACC3', 'OP_PUSHACC4', 'OP_PUSHACC5', 'OP_PUSHACC6', 'OP_PUSHACC7', 'OP_PUSHACC', 'OP_POP', 'OP_ASSIGN', 'OP_ENVACC1', 'OP_ENVACC2', 'OP_ENVACC3', 'OP_ENVACC4', 'OP_ENVACC', 'OP_PUSHENVACC1', 'OP_PUSHENVACC2', 'OP_PUSHENVACC3', 'OP_PUSHENVACC4', 'OP_PUSHENVACC', 'OP_PUSH_RETADDR', 'OP_APPLY', 'OP_APPLY1', 'OP_APPLY2', 'OP_APPLY3', 'OP_APPTERM', 'OP_APPTERM1', 'OP_APPTERM2', 'OP_APPTERM3', 'OP_RETURN', 'OP_RESTART', 'OP_GRAB', 'OP_CLOSURE', 'OP_CLOSUREREC', 'OP_OFFSETCLOSUREM2', 'OP_OFFSETCLOSURE0', 'OP_OFFSETCLOSURE2', 'OP_OFFSETCLOSURE', 'OP_PUSHOFFSETCLOSUREM2', 'OP_PUSHOFFSETCLOSURE0', 'OP_PUSHOFFSETCLOSURE2', 'OP_PUSHOFFSETCLOSURE', 'OP_GETGLOBAL', 'OP_PUSHGETGLOBAL', 'OP_GETGLOBALFIELD', 'OP_PUSHGETGLOBALFIELD', 'OP_SETGLOBAL', 'OP_ATOM0', 'OP_ATOM', 'OP_PUSHATOM0', 'OP_PUSHATOM', 'OP_MAKEBLOCK', 'OP_MAKEBLOCK1', 'OP_MAKEBLOCK2', 'OP_MAKEBLOCK3', 'OP_MAKEFLOATBLOCK', 'OP_GETFIELD0', 'OP_GETFIELD1', 'OP_GETFIELD2', 'OP_GETFIELD3', 'OP_GETFIELD', 'OP_GETFLOATFIELD', 'OP_SETFIELD0', 'OP_SETFIELD1', 'OP_SETFIELD2', 'OP_SETFIELD3', 'OP_SETFIELD', 'OP_SETFLOATFIELD', 'OP_VECTLENGTH', 'OP_GETVECTITEM', 'OP_SETVECTITEM', 'OP_GETBYTESCHAR', 'OP_SETBYTESCHAR', 'OP_BRANCH', 'OP_BRANCHIF', 'OP_BRANCHIFNOT', 'OP_SWITCH', 'OP_BOOLNOT', 'OP_PUSHTRAP', 'OP_POPTRAP', 'OP_RAISE', 'OP_CHECK_SIGNALS', 'OP_C_CALL1', 'OP_C_CALL2', 'OP_C_CALL3', 'OP_C_CALL4', 'OP_C_CALL5', 'OP_C_CALLN', 'OP_CONST0', 'OP_CONST1', 'OP_CONST2', 'OP_CONST3', 'OP_CONSTINT', 'OP_PUSHCONST0', 'OP_PUSHCONST1', 'OP_PUSHCONST2', 'OP_PUSHCONST3', 'OP_PUSHCONSTINT', 'OP_NEGINT', 'OP_ADDINT', 'OP_SUBINT', 'OP_MULINT', 'OP_DIVINT', 'OP_MODINT', 'OP_ANDINT', 'OP_ORINT', 'OP_XORINT', 'OP_LSLINT', 'OP_LSRINT', 'OP_ASRINT', 'OP_EQ', 'OP_NEQ', 'OP_LTINT', 'OP_LEINT', 'OP_GTINT', 'OP_GEINT', 'OP_OFFSETINT', 'OP_OFFSETREF', 'OP_ISINT', 'OP_GETMETHOD', 'OP_BEQ', 'OP_BNEQ', 'OP_BLTINT', 'OP_BLEINT', 'OP_BGTINT', 'OP_BGEINT', 'OP_ULTINT', 'OP_UGEINT', 'OP_BULTINT', 'OP_BUGEINT', 'OP_GETPUBMET', 'OP_GETDYNMET', 'OP_STOP', 'OP_EVENT', 'OP_BREAK', 'OP_RERAISE', 'OP_RAISE_NOTRACE', 'OP_GETSTRINGCHAR' ]
for _i, _opcode in enumerate(opcode_list):
globals()[_opcode] = _i
if os.environ.get('DEBUG'):
def dbg(*f):
print(' '.join(map(str, f)))
else:
dbg = None
class Prims:
_immutable_ = True
def __init__(self, names, argv):
self.argv = argv
self.names = names
bad_names = []
for name in self.names:
if name not in self.index_by_name:
bad_names.append(name)
#if bad_names:
# raise Exception('missing C calls: %s' % bad_names)
self.index_mapping = [ (self.index_by_name[name] if name in self.index_by_name else -1) for name in names ]
def caml_register_named_value(self, vname, val):
# print('named:', vname, val)
return ATOMS[0]
def caml_fresh_oo_id(self, _):
return make_int(666)
def caml_alloc_dummy(self, size):
return Block(tag=0, size=to_int(size))
def caml_alloc_dummy_function(self, size, arity):
return Block(tag=0, size=to_int(size))
def caml_int64_float_of_bits(self, _):
return make_float(0.666) # TODO
def caml_int64_shift_left(self, a, b):
assert isinstance(a, Int64)
return Int64(a.i << to_int(b))
def caml_int64_neg(self, a):
assert isinstance(a, Int64)
return Int64(-a.i)
def caml_int64_sub(self, a, b):
assert isinstance(a, Int64)
assert isinstance(b, Int64)
return Int64(a.i-b.i)
def caml_int64_add(self, a, b):
assert isinstance(a, Int64)
assert isinstance(b, Int64)
return Int64(a.i+b.i)
def caml_sys_isatty(self, channel):
return make_bool(False) # TODO
def caml_ml_open_descriptor_in(self, _):
return String('_stdin')
def caml_ml_open_descriptor_out(self, _):
return String('_stdout')
def caml_ml_out_channels_list(self, _):
return make_int(0) # TODO
def caml_ml_output_char(self, channel, ch):
# print('out', channel, chr(ch))
# TODO: buffering
os.write(1, chr(to_int(ch)))
return make_int(0)
def caml_ml_flush(self, channel):
return make_int(0)
def caml_ml_bytes_length(self, s):
assert isinstance(s, String)
return make_int(s.len())
def caml_bytes_set(self, s, index, newval):
assert isinstance(s, String)
s.set_at(to_int(index), to_int(newval))
return Val_unit
def caml_blit_string(self, s1, ofs1_, s2, ofs2_, n_):
ofs1 = to_int(ofs1_)
ofs2 = to_int(ofs2_)
n = to_int(n_)
assert isinstance(s1, String)
assert isinstance(s2, String)
assert s1 is not s2, "TODO"
#if s1 is s2 and ofs2 > ofs1:
# for i_ in range(n):
# i = n_ - 1 - i_
# s2.set_at(ofs2 + i, s1.get_at(ofs1 + i))
#else:
for i in range(n):
s2.set_at(ofs2 + i, s1.get_at(ofs1 + i))
return Val_unit
def caml_blit_bytes(self, s1, ofs1_, s2, ofs2_, n_):
return self.caml_blit_string(s1, ofs1_, s2, ofs2_, n_)
def caml_string_of_bytes(self, s):
assert isinstance(s, String)
return s.copy()
def caml_bytes_of_string(self, s):
assert isinstance(s, String)
return s.copy()
def caml_string_get(self, s, i):
assert isinstance(s, String)
return make_int(s.get_at(to_int(i)))
def caml_create_bytes(self, length):
# return make_bytes(bytearray(to_int(length)))
return make_string('\0' * to_int(length))
def caml_sys_get_argv(self, _):
return make_array([make_string("ocamlpypy"), self.caml_sys_argv(_)])
def caml_sys_argv(self, _):
return make_array([make_string('ocamlpypy')] + [
make_string(arg) for arg in self.argv
])
def caml_sys_executable_name(self, _):
return make_string("ocamlpypy")
def caml_sys_file_exists(self, name):
return make_bool(os.path.exists(to_str(name)))
def caml_sys_getenv(self, name):
return make_string(os.environ.get(to_str(name)) or '')
def caml_sys_get_config(self, _):
return make_array([
make_string('pypy'),
make_int(64), # bits
make_int(0) # little endian
])
def caml_sys_const_backend_type(self, _):
return make_int(1) # bytecode
def caml_sys_const_big_endian(self, _):
return make_int(0)
def caml_sys_const_word_size(self, _):
return make_int(64)
def caml_sys_const_int_size(self, _):
return make_int(64) # make_int(63) ??
def caml_sys_const_ostype_unix(self, _):
return make_int(1)
def caml_sys_const_ostype_win32(self, _):
return make_int(0)
def caml_sys_const_ostype_cygwin(self, _):
return make_int(0)
def caml_sys_const_max_wosize(self, _):
return make_int(2**20)
def caml_sys_exit(self, code):
os._exit(to_int(code))
return Val_unit
def caml_ml_string_length(self, s):
return make_int(len(to_str(s)))
def caml_ml_output(self, stream, data, ofs_, length_):
ofs = to_int(ofs_)
len = to_int(length_)
assert ofs >= 0
assert len >= 0
data = to_str(data)[ofs : ofs + len]
os.write(1, data)
return make_int(0)
def format_int(self, fmt, n):
fmt_s = to_str(fmt)
# TODO
if fmt_s in ('%d', '%i'):
return make_string('%d' % n)
elif fmt_s == '0x%X':
return make_string('0x%x' % n)
else:
raise Exception('unknown format %s' % fmt_s)
def caml_format_int(self, fmt, n_):
return self.format_int(fmt, to_int(n_))
def caml_format_float(self, fmt, f_):
f = to_float(f_)
fmt_s = to_str(fmt)
# TODO
if fmt_s == '%.0f':
return make_string('%f' % f)
if fmt_s == '%.9f':
return make_string('%f' % f)
else:
raise Exception('unknown format %s' % fmt_s)
def caml_int32_format(self, fmt, n_):
assert isinstance(n_, Int32)
return self.format_int(fmt, n_.i)
def caml_nativeint_shift_left(self, a, b):
assert isinstance(a, Int64), a
return Int64((a.i << to_int(b)) & 0xFFFFFFFF)
def caml_nativeint_to_int(self, n):
assert isinstance(n, Int64), n
#print('caml_nativeint_to_int', n)
return make_int(n.i)
def caml_nativeint_sub(self, a, b):
assert isinstance(a, Int64), a
assert isinstance(b, Int64), b
return Int64(intmask(a.i - b.i))
def caml_nativeint_add(self, a, b):
assert isinstance(a, Int64), a
assert isinstance(b, Int64), b
return Int64(intmask(a.i + b.i))
def caml_make_vect(self, size, init):
return make_array([ init for i in range(to_int(size)) ])
def caml_obj_block(self, tag, size):
b = make_block(to_int(tag), to_int(size))
for i in range(to_int(size)): b.set_field(i, make_int(0))
return b
def caml_obj_dup(self, o):
b = make_block(tag=o.get_tag(), size=o.len_fields())
for i in range(o.len_fields()): b.set_field(i, b.field(i))
return b
def caml_ensure_stack_capacity(self, space):
return Val_unit
def caml_fill_bytes(self, s, offset_, len_, init_):
offset = to_int(offset_)
len = to_int(offset_)
init = to_int(init_)
assert offset >= 0
assert len >= 0
assert isinstance(s, String)
for i in range(offset, offset + len):
s.set_at(i, init)
return Val_unit
def caml_string_equal(self, a, b):
return make_bool(poly_compare(a, b) == 0)
def caml_string_notequal(self, a, b):
return make_bool(poly_compare(a, b) != 0)
def int_of_string(self, n):
res = []
for ch in to_str(n):
if ch != '_':
res.append(ch)
s = ''.join(res)
return int(s, 0)
def caml_int_of_string(self, n):
return make_int(self.int_of_string(n))
def caml_int32_of_string(self, n):
return Int32(self.int_of_string(n))
def caml_int64_of_string(self, n):
return Int64(self.int_of_string(n))
def caml_int32_neg(self, n):
assert isinstance(n, Int32)
return Int32(-n.i)
def caml_int64_of_int(self, n):
return Int64(to_int(n))
def caml_int32_of_int(self, n):
return Int32(to_int(n))
def caml_int32_add(self, a, b):
assert isinstance(a, Int32)
assert isinstance(b, Int32)
return Int32(a.i + b.i)
def caml_int32_sub(self, a, b):
assert isinstance(a, Int32)
assert isinstance(b, Int32)
return Int32(a.i - b.i)
def caml_int32_mul(self, a, b):
assert isinstance(a, Int32)
assert isinstance(b, Int32)
return Int32(int32_signed_mul(a.i, b.i))
def caml_int32_div(self, a, b):
assert isinstance(a, Int32)
assert isinstance(b, Int32)
return Int32(int32_signed_div(a.i, b.i))
def caml_int32_shift_right_unsigned(self, a, b):
assert isinstance(a, Int32)
return Int32(a.i >> to_int(b))
def caml_int32_shift_right(self, a, b):
assert isinstance(a, Int32)
return Int32(int32_signed_rshift(a.i, to_int(b)))
def caml_int32_shift_left(self, a, b):
assert isinstance(a, Int32)
return Int32(a.i << to_int(b))
def caml_int32_to_int(self, n):
assert isinstance(n, Int32)
return make_int(n.i)
def caml_int32_compare(self, a, b):
assert isinstance(a, Int32)
assert isinstance(b, Int32)
return make_int(cmp_i(a.i, b.i))
def caml_int32_mod(self, a, b):
assert isinstance(a, Int32)
assert isinstance(b, Int32)
return Int32(int32_signed_mod(a.i, b.i))
def caml_int32_and(self, a, b):
assert isinstance(a, Int32)
assert isinstance(b, Int32)
return Int32(a.i & b.i)
def caml_int32_or(self, a, b):
assert isinstance(a, Int32)
assert isinstance(b, Int32)
return Int32(a.i | b.i)
def caml_int32_xor(self, a, b):
assert isinstance(a, Int32)
assert isinstance(b, Int32)
return Int32(a.i ^ b.i)
def caml_int32_of_float(self, a):
assert isinstance(a, Float)
return Int32(int(a.f))
def caml_int32_to_float(self, a):
assert isinstance(a, Int32)
return Float(a.i)
def caml_nativeint_of_int(self, n):
return Int64(to_int(n))
def caml_int_compare(self, a, b):
return make_int(cmp_i(to_int(a), to_int(b)))
def caml_string_compare(self, a, b):
assert isinstance(a, String)
assert isinstance(b, String)
return make_int(poly_compare(a, b))
def caml_compare(self, a, b):
return make_int(poly_compare(a, b))
def caml_float_compare(self, a, b):
assert isinstance(a, Float)
assert isinstance(b, Float)
return make_int(cmp_f(a.f, b.f))
def caml_ge_float(self, a, b):
assert isinstance(a, Float)
assert isinstance(b, Float)
return make_bool(a.f >= b.f)
def caml_gt_float(self, a, b):
assert isinstance(a, Float)
assert isinstance(b, Float)
return make_bool(a.f > b.f)
def caml_le_float(self, a, b):
assert isinstance(a, Float)
assert isinstance(b, Float)
return make_bool(a.f <= b.f)
def caml_lt_float(self, a, b):
assert isinstance(a, Float)
assert isinstance(b, Float)
return make_bool(a.f < b.f)
def caml_neq_float(self, a, b):
assert isinstance(a, Float)
assert isinstance(b, Float)
return make_bool(a.f != b.f)
def caml_neg_float(self, a):
return make_float(-to_float(a))
def caml_div_float(self, a, b):
return make_float(to_float(a) / to_float(b))
def caml_mul_float(self, a, b):
return make_float(to_float(a) * to_float(b))
def caml_int_of_float(self, n):
return make_int(int(to_float(n)))
def caml_float_of_int(self, n):
return make_float(float(to_int(n)))
def caml_add_float(self, a, b):
return make_float(to_float(a) + to_float(b))
def caml_sqrt_float(self, a):
return make_float(math.sqrt(to_float(a)))
def caml_array_sub(self, a, ofs_, len_):
ofs = to_int(ofs_)
len = to_int(len_)
assert ofs >= 0
assert len >= 0
return make_array(a.get_fields()[ofs : ofs+len])
def caml_make_array(self, init):
return init
def caml_hash(self, count, limit, seed, obj):
return make_int(obj.hash()) # TODO
def caml_floatarray_get(self, array, index):
return array.field(to_int(index))
def caml_floatarray_set(self, array, index, val):
array.set_field(to_int(index), val)
return Val_unit
def caml_array_get_addr(self, array, index):
return array.field(to_int(index))
def caml_array_set_addr(self, array, index, val):
array.set_field(to_int(index), val)
return Val_unit
def caml_array_get(self, array, index):
return array.field(to_int(index))
def caml_array_set(self, array, index, val):
array.set_field(to_int(index), val)
return Val_unit
def caml_weak_create(self, len):
return make_string('__weak') # TODO
def caml_lessthan(self, a, b):
return make_bool(poly_compare(a, b) < 0)
def caml_lessequal(self, a, b):
return make_bool(poly_compare(a, b) <= 0)
def caml_greaterequal(self, a, b):
return make_bool(poly_compare(a, b) >= 0)
def caml_greaterthan(self, a, b):
return make_bool(poly_compare(a, b) > 0)
def caml_equal(self, a, b):
return make_bool(poly_compare(a, b) == 0)
def caml_notequal(self, a, b):
return make_bool(not self.caml_equal(a, b))
def caml_eq_float(self, a, b):
return make_bool(to_float(a) == to_float(b))
def caml_abs_float(self, a):
return make_float(abs(to_float(a)))
def caml_gc_full_major(self, _):
return Val_unit
funcs1 = {}
funcs2 = {}
funcs3 = {}
funcs4 = {}
funcs5 = {}
index_by_name = {}
_i = 0
for _k, _v in locals().items():
if _k.startswith('caml_'):
_cnt = _v.func_code.co_argcount-1
locals()['funcs%d' % _cnt][_i] = _k
locals()['index_by_name'][_k] = _i
_i += 1
for i in range(1, 6):
exec '''
def call%d(self, bcindex, *args):
if self.index_mapping[bcindex] == -1:
raise Exception("missing C function %%s" %% self.names[bcindex])
index = promote(self.index_mapping[bcindex])
if False:
res = Val_unit
%s
else:
raise Exception("invalid c_call")
Root.check(res)
return res''' % (
i,
''.join([ 'elif index == %d: res = self.%s(*args)\n ' % (_index, _name) for _index, _name in locals()['funcs%d' % i].items() ])
)
def set_code_val(block, pc):
block.set_field(0, make_int(pc & 0xFFFFFFFF))
def code_val(block):
return to_int(block.field(0)) & 0xFFFFFFFF
def to_pc(x):
return to_int(x) & 0xFFFFFFFF
def offset_field(v, n):
return v.offset_field(n)
def get_printable_location(pc, bc):
return '%d(%s)' % (pc, opcode_list[bc[pc]])
jitdriver = JitDriver(
greens=['pc', 'bc'],
reds=['frame'],
virtualizables = ['frame'],
get_printable_location=get_printable_location)
class Frame:
_virtualizable_ = ['extra_args', 'accu', 'env', '_stack[*]', '_stack_top', 'pending_exception']
_immutable_fields_ = ['prims', 'global_data']
def __init__(self, prims, global_data,
env=make_string('rootenv'),
accu=make_int(0)):
self = hint(self, access_directly=True, fresh_virtualizable=True)
self.prims = prims
self.global_data = global_data
self.extra_args = 0
self.trap_sp = -1
self.accu = accu
self.env = env
self.pending_exception = False
self._stack = [None] * 1024
self._stack_top = r_uint(0)
def eval(self, bc, pc):
if dbg: print 'frame started at', pc, 'stack', self.len()
while True:
jitdriver.jit_merge_point(
frame=self, bc=bc, pc=pc)
self._stack_top = hint(self._stack_top, promote=True)
self.extra_args = hint(self.extra_args, promote=True)
pc = self.handle_opcode(bc, pc)
Root.check(self.accu)
if pc == -1:
break
if dbg: print 'return from call', self.accu
def push(self, v):
Root.check(v)
self._stack_top = promote(self._stack_top)
self._stack[self._stack_top] = v
self._stack_top += 1
def pop(self):
self._stack_top -= 1
self._stack_top = promote(self._stack_top)
assert self._stack_top >= 0
r = self._stack[self._stack_top]
self._stack[self._stack_top] = Val_unit
return r
def sp_swap(self, target, src):
ind = promote(self._stack_top - 1 - target)
assert ind >= 0
self._stack[ind] = self.sp(src)
def sp_set(self, k, val):
ind = promote(self._stack_top - 1 - k)
assert ind >= 0
self._stack[ind] = val
def sp(self, k):
ind = promote(self._stack_top - 1 - k)
assert ind >= 0
return self._stack[ind]
def len(self):
return intmask(self._stack_top)
@look_inside
@unroll_safe
@always_inline
def handle_raise(self, bc, pc):
if self.trap_sp == -1:
self.pending_exception = True
return -1
assert self.trap_sp <= self.len(), (self.trap_sp, self.len())
while self.len() > self.trap_sp:
self.pop()
pc = to_int(self.pop())
self.trap_sp = to_int(self.pop())
self.env = self.pop()
self.extra_args = to_int(self.pop())
return pc
@look_inside
@unroll_safe
@always_inline
def handle_opcode(self, bc, pc):
instr = bc[pc]
accu = self.accu
if dbg: print pc, 'instr', opcode_list[instr], 'stack_size', self.len()
#if dbg: print self._stack[:self._stack_top]
pc += 1
def unsupp():
raise Exception('unsupported instr')
if instr == OP_ACC0:
accu = self.sp(0)
elif instr == OP_ACC1:
accu = self.sp(1)
elif instr == OP_ACC2:
accu = self.sp(2)
elif instr == OP_ACC3:
accu = self.sp(3)
elif instr == OP_ACC4:
accu = self.sp(4)
elif instr == OP_ACC5:
accu = self.sp(5)
elif instr == OP_ACC6:
accu = self.sp(6)
elif instr == OP_ACC7:
accu = self.sp(7)
elif instr == OP_ACC:
accu = self.sp(bc[pc])
pc += 1
elif instr == OP_PUSH:
self.push(accu)
elif instr == OP_PUSHACC0:
self.push(accu)
elif instr == OP_PUSHACC1:
self.push(accu)
accu = self.sp(1)
elif instr == OP_PUSHACC2:
self.push(accu)
accu = self.sp(2)
elif instr == OP_PUSHACC3:
self.push(accu)
accu = self.sp(3)
elif instr == OP_PUSHACC4:
self.push(accu)
accu = self.sp(4)
elif instr == OP_PUSHACC5:
self.push(accu)
accu = self.sp(5)
elif instr == OP_PUSHACC6:
self.push(accu)
accu = self.sp(6)
elif instr == OP_PUSHACC7:
self.push(accu)
accu = self.sp(7)
elif instr == OP_PUSHACC:
self.push(accu)
accu = self.sp(bc[pc])
pc += 1
elif instr == OP_POP:
for i in range(bc[pc]):
self.pop()
pc += 1
elif instr == OP_ASSIGN:
self.sp_set(bc[pc], accu)
pc += 1
accu = Val_unit
elif instr == OP_ENVACC1:
accu = self.env.field(1)
elif instr == OP_ENVACC2:
accu = self.env.field(2)
elif instr == OP_ENVACC3:
accu = self.env.field(3)
elif instr == OP_ENVACC4:
accu = self.env.field(4)
elif instr == OP_ENVACC:
accu = self.env.field(bc[pc])
pc += 1
elif instr == OP_PUSHENVACC1:
self.push(accu)
accu = self.env.field(1)
elif instr == OP_PUSHENVACC2:
self.push(accu)
accu = self.env.field(2)
elif instr == OP_PUSHENVACC3:
self.push(accu)
accu = self.env.field(3)
elif instr == OP_PUSHENVACC4:
self.push(accu)
accu = self.env.field(4)
elif instr == OP_PUSHENVACC:
self.push(accu)
accu = self.env.field(bc[pc])
pc += 1
elif instr == OP_PUSH_RETADDR:
self.push(make_int(self.extra_args))
self.push(self.env)
self.push(make_int(pc + bc[pc]))
pc += 1
elif instr == OP_APPLY:
argc = bc[pc]
frame = Frame(prims=self.prims,
env=accu,
accu=accu,
global_data=self.global_data)
for i in range(argc):
frame.push(self.sp(argc - 1 - i))
for i in range(argc):
self.pop()
# from OP_PUSH_RETADDR
pc = to_pc(self.pop())
self.pop() # env
self.pop() # extra_args
new_pc = code_val(accu)
frame.extra_args = argc - 1
frame.eval(bc, new_pc)
if frame.pending_exception:
self.accu = frame.accu
return self.handle_raise(bc, pc)
accu = frame.accu
elif instr == OP_APPLY1:
arg1 = self.pop()
frame = Frame(prims=self.prims,
env=accu,
accu=accu,
global_data=self.global_data)
new_pc = code_val(accu)
frame.push(arg1)
frame.extra_args = 0
frame.eval(bc, new_pc)
if frame.pending_exception:
self.accu = frame.accu
return self.handle_raise(bc, pc)
accu = frame.accu
elif instr == OP_APPLY2:
arg1 = self.pop()
arg2 = self.pop()
frame = Frame(prims=self.prims,
env=accu,
accu=accu,
global_data=self.global_data)
new_pc = code_val(accu)
frame.push(arg2)
frame.push(arg1)
frame.extra_args = 1
frame.eval(bc, new_pc)
if frame.pending_exception:
self.accu = frame.accu
return self.handle_raise(bc, pc)
accu = frame.accu
elif instr == OP_APPLY3:
arg1 = self.pop()
arg2 = self.pop()
arg3 = self.pop()
frame = Frame(prims=self.prims,
env=accu,
accu=accu,
global_data=self.global_data)
new_pc = code_val(accu)
frame.push(arg3)
frame.push(arg2)
frame.push(arg1)
frame.extra_args = 2
frame.eval(bc, new_pc)
if frame.pending_exception:
self.accu = frame.accu
return self.handle_raise(bc, pc)
accu = frame.accu
elif instr == OP_APPTERM:
nargs = bc[pc]
pc += 1
slotsize = bc[pc]
newsp = slotsize - nargs
if dbg:
for i in range(nargs): dbg('arg', self.sp(i))
i = nargs - 1
while i >= 0:
self.sp_swap(newsp + i, i)
i -= 1
for _ in range(newsp): self.pop()
if dbg: trace_call(accu, [ self.sp(i) for i in range(nargs) ])
pc = code_val(accu)
self.env = accu
self.extra_args += nargs - 1
elif instr == OP_APPTERM1:
arg1 = self.sp(0)
trace_call(accu, [arg1])
for _ in range(bc[pc]): self.pop()
if dbg: dbg('stack', self._stack[:self._stack_top], self.extra_args)
self.push(arg1)
pc = code_val(accu)
self.env = accu
elif instr == OP_APPTERM2:
arg1 = self.sp(0)
arg2 = self.sp(1)
trace_call(accu, [arg1, arg2])
for _ in range(bc[pc]): self.pop()
if dbg: dbg('stack', self._stack[:self._stack_top], self.extra_args)
self.push(arg2)
self.push(arg1)
pc = code_val(accu)
self.env = accu
self.extra_args += 1
elif instr == OP_APPTERM3:
arg1 = self.sp(0)
arg2 = self.sp(1)
arg3 = self.sp(2)
trace_call(accu, [arg1, arg2, arg3])
for _ in range(bc[pc]): self.pop()
if dbg: dbg('stack', self._stack[:self._stack_top], self.extra_args)
self.push(arg3)
self.push(arg2)
self.push(arg1)
pc = code_val(accu)
self.env = accu
self.extra_args += 2
elif instr == OP_RETURN:
npop = bc[pc]
pc += 1
for _ in range(npop): self.pop()
if self.extra_args > 0:
self.extra_args -= 1
pc = code_val(accu)
self.env = accu
else:
if self.len() == 0:
self.accu = accu
return -1
pc = to_pc(self.pop())
self.env = self.pop()
self.extra_args = to_int(self.pop())
elif instr == OP_RESTART:
num_args = self.env.len_fields() - 2
assert num_args >= 0, self.env
for i in range(num_args):
self.push(self.env.field((num_args - i - 1) + 2))
self.env = self.env.field(1)
self.extra_args += num_args
elif instr == OP_GRAB:
required = bc[pc]
pc += 1
if self.extra_args >= required:
self.extra_args -= required
else:
num_args = 1 + self.extra_args
#print 'GRAB', num_args, ' ', self.extra_args, '->',pc - 3
accu = make_block(num_args + 2, Closure_tag)
accu.set_field(1, self.env)
for i in range(num_args):
accu.set_field(i + 2, self.pop())
set_code_val(accu, pc - 3)
if self.len() == 0:
self.accu = accu
return -1
pc = to_pc(self.pop())
self.env = self.pop()
self.extra_args = to_int(self.pop())
elif instr == OP_CLOSURE:
nvars = bc[pc]
pc += 1
if nvars > 0:
self.push(accu)
accu = make_block(1 + nvars, Closure_tag)
for i in range(nvars):
accu.set_field(i + 1, self.sp(0))
self.pop()
set_code_val(accu, pc + bc[pc])
pc += 1
elif instr == OP_CLOSUREREC:
nfuncs = bc[pc]
pc += 1
nvars = bc[pc]
pc += 1
blksize = nfuncs * 2 - 1 + nvars
if nvars > 0:
self.push(accu)
accu = make_block(blksize, Closure_tag)
for i in range(nvars):
accu.set_field(nfuncs * 2 - 1 + i, self.sp(0))
self.pop()
set_code_val(accu, pc + bc[pc])
self.push(accu)
assert isinstance(accu, Block)
for i in range(1, nfuncs):
b = InfixBlock(accu, i * 2)
b.set_field(0, make_int(pc + bc[pc + i]))
self.push(b)
accu.set_field(i * 2, b)
accu.set_field(i * 2 - 1, make_string('closureoffsettaint'))
pc += nfuncs
elif instr == OP_OFFSETCLOSUREM2:
unsupp()
elif instr == OP_OFFSETCLOSURE0:
accu = self.env
elif instr == OP_OFFSETCLOSURE2:
accu = offset_field(self.env, 2) # offset_field
elif instr == OP_OFFSETCLOSURE:
n = bc[pc]
pc += 1
accu = offset_field(self.env, n) # offset_field
elif instr == OP_PUSHOFFSETCLOSUREM2:
self.push(accu)
accu = offset_field(self.env, -2) # offset_field
elif instr == OP_PUSHOFFSETCLOSURE0:
self.push(accu)
accu = self.env
elif instr == OP_PUSHOFFSETCLOSURE2:
self.push(accu)
accu = offset_field(self.env, 2) # offset_field
elif instr == OP_PUSHOFFSETCLOSURE:
self.push(accu)
n = bc[pc]
if dbg: dbg('offsetclosure', n)
pc += 1
accu = offset_field(self.env, n) # offset_field
elif instr == OP_GETGLOBAL:
n = bc[pc]
pc += 1
accu = self.global_data[n]
elif instr == OP_PUSHGETGLOBAL:
self.push(accu)
n = bc[pc]
pc += 1
accu = self.global_data[n]
# print(n, global_data[n - 2 : n + 3])