-
Notifications
You must be signed in to change notification settings - Fork 6
/
spec_execution.py
1989 lines (1758 loc) · 54.8 KB
/
spec_execution.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
"""
PyWebAssembly - Implmentation of WebAssembly, and some tools.
Copyright (C) 2018-2019 Paul Dworzanski
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import spec_structure as structure
import spec_validation as validation
import math
import struct
verbose = 0
###############
###############
# 4 EXECUTION #
###############
###############
#Chapter 4 defines execution semantics over the abstract syntax.
##############
# 4.3 NUMERICS
##############
def spec_trunc(q):
if verbose>=1: print("spec_trunc(",q,")")
# round towards zero
# q can be float or rational as tuple (numerator,denominator)
if type(q)==tuple: #rational
result = q[0]//q[1] #rounds towards negative infinity
if result < 0 and q[1]*result != q[0]:
return result+1
else:
return result
elif type(q)==float:
#using ftrunc instead
return int(q)
# 4.3.1 REPRESENTATIONS
# bits are string of 1s and 0s
# bytes are bytearray (maybe can also read from memoryview)
def spec_bitst(t,c):
if verbose>=1: print("spec_bitst(",t,c,")")
N = int(t[1:3])
if t[0]=='i':
return spec_bitsiN(N,c)
elif t[0]=='f':
return spec_bitsfN(N,c)
def spec_bitst_inv(t,bits):
if verbose>=1: print("spec_bitst_inv(",t,bits,")")
N = int(t[1:3])
if t[0]=='i':
return spec_bitsiN_inv(N,bits)
elif t[0]=='f':
return spec_bitsfN_inv(N,bits)
def spec_bitsiN(N,i):
if verbose>=1: print("spec_bitsiN(",N,i,")")
return spec_ibitsN(N,i)
def spec_bitsiN_inv(N,bits):
if verbose>=1: print("spec_bitsiN_inv(",N,bits,")")
return spec_ibitsN_inv(N,bits)
def spec_bitsfN(N,z):
if verbose>=1: print("spec_bitsfN(",N,z,")")
return spec_fbitsN(N,z)
def spec_bitsfN_inv(N,bits):
if verbose>=1: print("spec_bitsfN_inv(",N,bits,")")
return spec_fbitsN_inv(N,bits)
# Integers
def spec_ibitsN(N,i):
if verbose>=1: print("spec_ibitsN(",N,i,")")
#print(bin(i)[2:].zfill(N))
return bin(i)[2:].zfill(N)
def spec_ibitsN_inv(N,bits):
if verbose>=1: print("spec_ibitsN_inv(",N,bits,")")
return int(bits,2)
# Floating-Point
def spec_fbitsN(N,z):
if verbose>=1: print("spec_fbitsN(",N,z,")")
if N==32:
z_bytes = struct.pack('>f',z)
elif N==64:
z_bytes = struct.pack('>d',z)
#stryct.pack() gave us bytes, need bits
bits = ''
for byte in z_bytes:
bits += bin( int(byte) ).lstrip('0b').zfill(8)
return bits
def spec_fbitsN_inv(N,bits):
# this is used by reinterpret
if verbose>=1: print("spec_fbitsN_inv(",N,bits,")")
#will use struct.unpack() so need bytearray
bytes_ = bytearray()
for i in range(len(bits)//8):
bytes_ += bytearray( [int(bits[8*i:8*(i+1)],2)] )
if N==32:
z = struct.unpack('>f',bytes_)[0]
elif N==64:
z = struct.unpack('>d',bytes_)[0]
return z
def spec_fsign(z):
bytes_ = spec_bytest("f"+str(64),z)
#print("fsign bytes_",bytes_, [bin(byte).lstrip('0b').zfill(8) for byte in bytes_])
sign = bytes_[-1] & 0b10000000 #-1 since littleendian
#print("spec_fsign(",z,")",sign)
if sign: return 1
else: return 0
#z_bytes = struct.pack('d',z)
#if bin(z_bytes[0]).replace('0b','')[0] == '1':
# return 1
#else:
# return 0
# decided to just use struct.pack() and struct.unpack()
# other options to represent floating point numbers:
# float which is 64-bit, for 32-bit, can truncate significand and exponent after each operation
# ctypes.c_float and ctypes.c_double
# numpy.float32 and numpy.float64
# Storage
def spec_bytest(t,i):
if verbose>=1: print("spec_bytest(",t,i,")")
if t[0]=='i':
bits = spec_bitsiN(int(t[1:3]),i)
elif t[0]=='f':
bits = spec_bitsfN(int(t[1:3]),i)
return spec_littleendian(bits)
def spec_bytest_inv(t,bytes_):
if verbose>=1: print("spec_bytest_inv(",t,bytes_,")")
bits = spec_littleendian_inv(bytes_)
if t[0]=='i':
return spec_bitsiN_inv(int(t[1:3]),bits)
elif t[0]=='f':
return spec_bitsfN_inv(int(t[1:3]),bits)
def spec_bytesiN(N,i):
if verbose>=1: print("spec_bytesiN(",N,i,")")
bits = spec_bitsiN(N,i)
#convert bits to bytes
bytes_ = bytearray()
for byteIdx in range(0,len(bits),8):
bytes_ += bytearray([int(bits[byteIdx:byteIdx+8],2)])
return bytes_
def spec_bytesiN_inv(N,bytes_):
if verbose>=1: print("spec_bytesiN_inv(",N,bytes_,")")
bits=""
for byte in bytes_:
bits += spec_ibitsN(8,byte)
return spec_ibitsN_inv(N,bits)
# TODO: these are unused, but might use when refactor floats to pass NaN significand tests
def spec_bytesfN(N,z):
if verbose>=1: print("spec_bytesfN(",N,z,")")
if N==32:
z_bytes = struct.pack('>f',z)
elif N==64:
z_bytes = struct.pack('>d',z)
return z_bytes
def spec_bytesfN_inv(N,bytes_):
if verbose>=1: print("spec_bytesfN_inv(",N,bytes_,")")
if N==32:
z = struct.unpack('>f',bytes_)[0]
elif N==64:
z = struct.unpack('>d',bytes_)[0]
return z
def spec_littleendian(d):
if verbose>=1: print("spec_littleendian(",d,")")
#same behavior for both 32 and 64-bit values
#this assumes len(d) is divisible by 8
if len(d)==0: return bytearray()
d18 = d[:8]
d2Nm8 = d[8:]
d18_as_int = spec_ibitsN_inv(8,d18)
return spec_littleendian(d2Nm8) + bytearray([d18_as_int])
#return bytearray([d18_as_int]) + spec_littleendian(d2Nm8)
def spec_littleendian_inv(bytes_):
if verbose>=1: print("spec_littleendian_inv(",bytes_,")")
#same behavior for both 32 and 64-bit values
#this assumes len(d) is divisible by 8
#this converts bytes to bits
if len(bytes_)==0: return ''
bits = bin( int(bytes_[-1]) ).lstrip('0b').zfill(8)
return bits + spec_littleendian_inv( bytes_[:-1] )
#bits = bin( int(bytes_[0]) ).lstrip('0b').zfill(8)
#return spec_littleendian_inv( bytes_[1:] ) + bits
# 4.3.2 INTEGER OPERATIONS
#two's comlement
def spec_signediN(N,i):
if verbose>=1: print("spec_signediN(",N,i,")")
if 0<=i<2**(N-1):
return i
elif 2**(N-1)<=i<2**N:
return i-2**N
#alternative 2's comlement
# return i - int((i << 1) & 2**N) #https://stackoverflow.com/a/36338336
def spec_signediN_inv(N,i):
if verbose>=1: print("spec_signediN_inv(",N,i,")")
if 0<=i<2**(N-1):
return i
elif -1*(2**(N-1))<=i<0:
return i+2**N
else:
return None
def spec_iaddN(N,i1,i2):
if verbose>=1: print("spec_iaddN(",N,i1,i2,")")
return (i1+i2) % 2**N
def spec_isubN(N,i1,i2):
if verbose>=1: print("spec_isubN(",N,i1,i2,")")
return (i1-i2+2**N) % 2**N
def spec_imulN(N,i1,i2):
if verbose>=1: print("spec_imulN(",N,i1,i2,")")
return (i1*i2) % 2**N
def spec_idiv_uN(N,i1,i2):
if verbose>=1: print("spec_idiv_uN(",N,i1,i2,")")
if i2==0: raise Exception("trap")
return spec_trunc((i1,i2))
def spec_idiv_sN(N,i1,i2):
if verbose>=1: print("spec_idiv_sN(",N,i1,i2,")")
j1 = spec_signediN(N,i1)
j2 = spec_signediN(N,i2)
if j2==0: raise Exception("trap")
#assuming j1 and j2 are N-bit
if j1//j2 == 2**(N-1): raise Exception("trap")
return spec_signediN_inv(N,spec_trunc((j1,j2)))
def spec_irem_uN(N,i1,i2):
if verbose>=1: print("spec_irem_uN(",i1,i2,")")
if i2==0:raise Exception("trap")
return i1-i2*spec_trunc((i1,i2))
def spec_irem_sN(N,i1,i2):
if verbose>=1: print("spec_irem_sN(",N,i1,i2,")")
j1 = spec_signediN(N,i1)
j2 = spec_signediN(N,i2)
if i2==0: raise Exception("trap")
#print(j1,j2,spec_trunc((j1,j2)))
return spec_signediN_inv(N,j1-j2*spec_trunc((j1,j2)))
def spec_iandN(N,i1,i2):
if verbose>=1: print("spec_iandN(",N,i1,i2,")")
return i1 & i2
def spec_iorN(N,i1,i2):
if verbose>=1: print("spec_iorN(",N,i1,i2,")")
return i1 | i2
def spec_ixorN(N,i1,i2):
if verbose>=1: print("spec_ixorN(",N,i1,i2,")")
return i1 ^ i2
def spec_ishlN(N,i1,i2):
if verbose>=1: print("spec_ishlN(",N,i1,i2,")")
k = i2 % N
return (i1 << k) % (2**N)
def spec_ishr_uN(N,i1,i2):
if verbose>=1: print("spec_ishr_uN(",N,i1,i2,")")
j2 = i2 % N
return i1 >> j2
def spec_ishr_sN(N,i1,i2):
if verbose>=1: print("spec_ishr_sN(",N,i1,i2,")")
#print("spec_ishr_sN(",N,i1,i2,")")
k = i2 % N
#print(k)
d0d1Nmkm1d2k = spec_ibitsN(N,i1)
d0 = d0d1Nmkm1d2k[0]
d1Nmkm1 = d0d1Nmkm1d2k[1:N-k]
#print(d0*k)
#print(d0*(k+1) + d1Nmkm1)
return spec_ibitsN_inv(N, d0*(k+1) + d1Nmkm1 )
def spec_irotlN(N,i1,i2):
if verbose>=1: print("spec_irotlN(",N,i1,i2,")")
k = i2 % N
d1kd2Nmk = spec_ibitsN(N,i1)
d2Nmk = d1kd2Nmk[k:]
d1k = d1kd2Nmk[:k]
return spec_ibitsN_inv(N, d2Nmk+d1k )
def spec_irotrN(N,i1,i2):
if verbose>=1: print("spec_irotrN(",N,i1,i2,")")
k = i2 % N
d1Nmkd2k = spec_ibitsN(N,i1)
d1Nmk = d1Nmkd2k[:N-k]
d2k = d1Nmkd2k[N-k:]
return spec_ibitsN_inv(N, d2k+d1Nmk )
def spec_iclzN(N,i):
if verbose>=1: print("spec_iclzN(",N,i,")")
k = 0
for b in spec_ibitsN(N,i):
if b=='0':
k+=1
else:
break
return k
def spec_ictzN(N,i):
if verbose>=1: print("spec_ictzN(",N,i,")")
k = 0
for b in reversed(spec_ibitsN(N,i)):
if b=='0':
k+=1
else:
break
return k
def spec_ipopcntN(N,i):
if verbose>=1: print("spec_ipopcntN(",N,i,")")
k = 0
for b in spec_ibitsN(N,i):
if b=='1':
k+=1
return k
def spec_ieqzN(N,i):
if verbose>=1: print("spec_ieqzN(",N,i,")")
if i==0:
return 1
else:
return 0
def spec_ieqN(N,i1,i2):
if verbose>=1: print("spec_ieqN(",N,i1,i2,")")
if i1==i2:
return 1
else:
return 0
def spec_ineN(N,i1,i2):
if verbose>=1: print("spec_ineN(",N,i1,i2,")")
if i1!=i2:
return 1
else:
return 0
def spec_ilt_uN(N,i1,i2):
if verbose>=1: print("spec_ilt_uN(",N,i1,i2,")")
if i1<i2:
return 1
else:
return 0
def spec_ilt_sN(N,i1,i2):
if verbose>=1: print("spec_ilt_sN(",N,i1,i2,")")
j1 = spec_signediN(N,i1)
j2 = spec_signediN(N,i2)
if j1<j2:
return 1
else:
return 0
def spec_igt_uN(N,i1,i2):
if verbose>=1: print("spec_igt_uN(",N,i1,i2,")")
if i1>i2:
return 1
else:
return 0
def spec_igt_sN(N,i1,i2):
if verbose>=1: print("spec_igt_sN(",N,i1,i2,")")
j1 = spec_signediN(N,i1)
j2 = spec_signediN(N,i2)
if j1>j2:
return 1
else:
return 0
def spec_ile_uN(N,i1,i2):
if verbose>=1: print("spec_ile_uN(",N,i2,i1,")")
if i1<=i2:
return 1
else:
return 0
def spec_ile_sN(N,i1,i2):
if verbose>=1: print("spec_ile_sN(",N,i1,i2,")")
j1 = spec_signediN(N,i1)
j2 = spec_signediN(N,i2)
if j1<=j2:
return 1
else:
return 0
def spec_ige_uN(N,i1,i2):
if verbose>=1: print("spec_ige_uN(",N,i1,i2,")")
if i1>=i2:
return 1
else:
return 0
def spec_ige_sN(N,i1,i2):
if verbose>=1: print("spec_ige_sN(",N,i1,i2,")")
j1 = spec_signediN(N,i1)
j2 = spec_signediN(N,i2)
if j1>=j2:
return 1
else:
return 0
# 4.3.3 FLOATING-POINT OPERATIONS
def spec_fabsN(N,z):
if verbose>=1: print("spec_fabsN(",N,z,")")
#print("spec_fabsN(",N,z,")")
sign = spec_fsign(z)
#print(sign)
if sign == 0:
return z
else:
return spec_fnegN(N,z)
def spec_fnegN(N,z):
if verbose>=1: print("spec_fnegN(",N,z,")")
#get bytes and sign
bytes_ = spec_bytest("f64",z) #64 since errors if z too bit for 32
sign = spec_fsign(z)
if sign == 0:
bytes_[-1] |= 0b10000000 #-1 since littleendian
else:
bytes_[-1] &= 0b01111111 #-1 since littleendian
z = spec_bytest_inv("f64",bytes_) #64 since errors if z too bit for 32
return z
def spec_fceilN(N,z):
if verbose>=1: print("spec_fceilN(",N,z,")")
if math.isnan(z):
return z
elif math.isinf(z):
return z
elif z==0:
return z
elif -1.0<z<0.0:
return -0.0
else:
return float(math.ceil(z))
def spec_ffloorN(N,z):
if verbose>=1: print("spec_ffloorN(",N,z,")")
if math.isnan(z):
return z
elif math.isinf(z):
return z
elif z==0:
return z
elif 0.0<z<1.0:
return 0.0
else:
return float(math.floor(z))
def spec_ftruncN(N,z):
if verbose>=1: print("spec_ftruncN(",N,z,")")
if math.isnan(z):
return z
elif math.isinf(z):
return z
elif z==0:
return z
elif 0.0<z<1.0:
return 0.0
elif -1.0<z<0.0:
return -0.0
else:
magnitude = spec_fabsN(N,z)
floormagnitude = spec_ffloorN(N,magnitude)
return floormagnitude * (-1 if spec_fsign(z) else 1) #math.floor(z)) + spec_fsign(z)
def spec_fnearestN(N,z):
if verbose>=1: print("spec_fnearestN(",N,z,")")
if math.isnan(z):
return z
elif math.isinf(z):
return z
elif z==0:
return z
elif 0.0 < z <= 0.5:
return 0.0
elif -0.5 <= z < 0.0:
return -0.0
else:
return float(round(z))
def spec_fsqrtN(N,z):
if verbose>=1: print("spec_fsqrtN(",N,z,")")
if math.isnan(z) or (spec_fsign(z)==1 and z!=0):
return float('nan')
else:
return math.sqrt(z)
def spec_faddN(N,z1,z2):
if verbose>=1: print("spec_faddN(",N,z1,z2,")")
res = z1+z2
if N==32:
res = spec_demoteMN(64,32,res)
return res
def spec_fsubN(N,z1,z2):
if verbose>=1: print("spec_fsubN(",N,z1,z2,")")
res = z1-z2
#print("z1-z2:",z1-z2)
if N==32:
res = spec_demoteMN(64,32,res)
#print("demoted z1-z2:",res)
return res
def spec_fmulN(N,z1,z2):
if verbose>=1: print("spec_fmulN(",N,z1,z2,")")
res = z1*z2
if N==32:
res = spec_demoteMN(64,32,res)
return res
def spec_fdivN(N,z1,z2):
if verbose>=1: print("spec_fdivN(",N,z1,z2,")")
if math.isnan(z1):
return z1
elif math.isnan(z2):
return z2
elif math.isinf(z1) and math.isinf(z2):
return float('nan')
elif z1==0.0 and z2==0.0:
return float('nan')
elif z1==0.0 and z2==0.0:
return float('nan')
elif math.isinf(z1):
if spec_fsign(z1) == spec_fsign(z2):
return float('inf')
else:
return -float('inf')
elif math.isinf(z2):
if spec_fsign(z1) == spec_fsign(z2):
return 0.0
else:
return -0.0
elif z1==0:
if spec_fsign(z1) == spec_fsign(z2):
return 0.0
else:
return -0.0
elif z2==0:
if spec_fsign(z1) == spec_fsign(z2):
return float('inf')
else:
return -float('inf')
else:
res = z1/z2
if N==32:
res = spec_demoteMN(64,32,res)
return res
def spec_fminN(N,z1,z2):
if verbose>=1: print("spec_fminN(",N,z1,z2,")")
if math.isnan(z1):
return z1
elif math.isnan(z2):
return z2
elif z1==-float('inf') or z2==-float('inf'):
return -float('inf')
elif z1 == float('inf'):
return z2
elif z2 == float('inf'):
return z1
elif z1==z2==0.0:
if spec_fsign(z1) != spec_fsign(z2):
return -0.0
else:
return z1
elif z1 <= z2:
return z1
else:
return z2
def spec_fmaxN(N,z1,z2):
if verbose>=1: print("spec_fmaxN(",N,z1,z2,")")
if math.isnan(z1):
return z1
elif math.isnan(z2):
return z2
elif z1==float('inf') or z2==float('inf') :
return float('inf')
elif z1 == -float('inf'):
return z2
elif z2 == -float('inf'):
return z1
elif z1==z2==0.0:
if spec_fsign(z1) != spec_fsign(z2):
return 0.0
else:
return z1
elif z1 <= z2:
return z2
else:
return z1
def spec_fcopysignN(N,z1,z2):
if verbose>=1: print("spec_fcopysignN(",N,z1,z2,")")
z1sign = spec_fsign(z1)
z2sign = spec_fsign(z2)
if z1sign == z2sign:
return z1
else:
z1bytes = spec_bytest("f"+str(N),z1)
if z1sign == 0:
z1bytes[-1] |= 0b10000000 #-1 since littleendian
else:
z1bytes[-1] &= 0b01111111 #-1 since littleendian
z1 = spec_bytest_inv("f"+str(N),z1bytes)
return z1
def spec_feqN(N,z1,z2):
if verbose>=1: print("spec_feqN(",N,z1,z2,")")
if z1==z2:
return 1
else:
return 0
def spec_fneN(N,z1,z2):
if verbose>=1: print("spec_fneN(",N,z1,z2,")")
if z1 != z2:
return 1
else:
return 0
def spec_fltN(N,z1,z2):
if verbose>=1: print("spec_fltN(",N,z1,z2,")")
if math.isnan(z1):
return 0
elif math.isnan(z2):
return 0
elif spec_bitsfN(N,z1)==spec_bitsfN(N,z2):
return 0
elif z1==float('inf'):
return 0
elif z1==-float('inf'):
return 1
elif z2==float('inf'):
return 1
elif z2==-float('inf'):
return 0
elif z1==z2==0:
return 0
elif z1 < z2:
return 1
else:
return 0
def spec_fgtN(N,z1,z2):
if verbose>=1: print("spec_fgtN(",N,z1,z2,")")
if math.isnan(z1):
return 0
elif math.isnan(z2):
return 0
elif spec_bitsfN(N,z1)==spec_bitsfN(N,z2):
return 0
elif z1==float('inf'):
return 1
elif z1==-float('inf'):
return 0
elif z2==float('inf'):
return 0
elif z2==-float('inf'):
return 1
elif z1==z2==0:
return 0
elif z1 > z2:
return 1
else:
return 0
def spec_fleN(N,z1,z2):
if verbose>=1: print("spec_fleN(",N,z1,z2,")")
if math.isnan(z1):
return 0
elif math.isnan(z2):
return 0
elif spec_bitsfN(N,z1)==spec_bitsfN(N,z2):
return 1
elif z1==float('inf'):
return 0
elif z1==-float('inf'):
return 1
elif z2==float('inf'):
return 1
elif z2==-float('inf'):
return 0
elif z1==z2==0:
return 1
elif z1 <= z2:
return 1
else:
return 0
def spec_fgeN(N,z1,z2):
if verbose>=1: print("spec_fgeN(",N,z1,z2,")")
if math.isnan(z1):
return 0
elif math.isnan(z2):
return 0
elif spec_bitsfN(N,z1)==spec_bitsfN(N,z2):
return 1
elif z1==float('inf'):
return 1
elif z1==-float('inf'):
return 0
elif z2==float('inf'):
return 0
elif z2==-float('inf'):
return 1
elif z1==z2==0:
return 1
elif z1 >= z2:
return 1
else:
return 0
# 4.3.4 CONVERSIONS
def spec_extend_uMN(M,N,i):
if verbose>=1: print("spec_extend_uMN(",i,")")
return i
def spec_extend_sMN(M,N,i):
if verbose>=1: print("spec_extend_sMN(",M,N,i,")")
#print("spec_extend_sMN(",M,N,i,")")
j = spec_signediN(M,i)
return spec_signediN_inv(N,j)
def spec_wrapMN(M,N,i):
if verbose>=1: print("spec_wrapMN(",M,N,i,")")
#print("spec_wrapMN(",M,N,i,")")
return i % (2**N)
def spec_trunc_uMN(M,N,z):
if verbose>=1: print("spec_trunc_uMN(",M,N,z,")")
if math.isnan(z) or math.isinf(z): raise Exception("trap")
ztrunc = spec_ftruncN(M,z)
if -1 < ztrunc < 2**N:
return int(ztrunc)
else: raise Exception("trap")
def spec_trunc_sMN(M,N,z):
if verbose>=1: print("spec_trunc_sMN(",M,N,z,")")
if math.isnan(z) or math.isinf(z): raise Exception("trap")
ztrunc = spec_ftruncN(M,z)
if -(2**(N-1))-1 < ztrunc < 2**(N-1):
iztrunc = int(ztrunc)
if iztrunc < 0:
return spec_signediN_inv(N,iztrunc)
else:
return iztrunc
else:
raise Exception("trap")
def spec_promoteMN(M,N,z):
if verbose>=1: print("spec_promoteMN(",M,N,z,")")
return z
def spec_demoteMN(M,N,z):
if verbose>=1: print("spec_demoteMN(",M,N,z,")")
absz = spec_fabsN(N,z)
#limitN = 2**(2**(structure.spec_expon(N)-1))
limitN = 2**128 * (1 - 2**-25) #this FLT_MAX is slightly different than the Wasm spec's 2**127
if absz >= limitN:
signz = spec_fsign(z)
if signz:
return -float('inf')
else:
return float('inf')
bytes_ = spec_bytest('f32',z)
z32 = spec_bytest_inv('f32',bytes_)
return z32
def spec_convert_uMN(M,N,i):
if verbose>=1: print("spec_convert_uMN(",M,N,i,")")
limitN = 2**(2**(structure.spec_expon(N)-1))
if i >= limitN:
return float('inf')
return float(i)
def spec_convert_sMN(M,N,i):
if verbose>=1: print("spec_convert_sMN(",M,N,i,")")
limitN = 2**(2**(structure.spec_expon(N)-1))
#print("limitN",limitN)
if i >= limitN:
return float('inf')
if i <= -1*limitN:
return -float('inf')
i = spec_signediN(M,i)
return float(i)
def spec_reinterprett1t2(t1,t2,c):
if verbose>=1: print("spec_reinterprett1t2(",t1,t2,c,")")
#print("spec_reinterprett1t2(",t1,t2,c,")")
bits = spec_bitst(t1,c)
#print(bits)
return spec_bitst_inv(t2,bits)
##################
# 4.4 INSTRUCTIONS
##################
# S is the store
# 4.4.1 NUMERIC INSTRUCTIONS
def spec_tconst(config):
if verbose>=1: print("spec_tconst(",")")
S = config["S"]
c = config["instrstar"][config["idx"]][1]
if verbose>=1: print("spec_tconst(",c,")")
config["operand_stack"] += [c]
config["idx"] += 1
def spec_tunop(config): # t is in {'i32','i64','f32','f64'}
if verbose>=1: print("spec_tunop(",")")
S = config["S"]
instr = config["instrstar"][config["idx"]][0]
t = instr[0:3]
op = opcode2exec[instr][1]
c1 = config["operand_stack"].pop()
c = op(int(t[1:3]),c1)
if c == "trap": return c
config["operand_stack"].append(c)
config["idx"] += 1
def spec_tbinop(config):
if verbose>=1: print("spec_tbinop(",")")
S = config["S"]
instr = config["instrstar"][config["idx"]][0]
t = instr[0:3]
op = opcode2exec[instr][1]
c2 = config["operand_stack"].pop()
c1 = config["operand_stack"].pop()
c = op(int(t[1:3]),c1,c2)
if c == "trap": return c
config["operand_stack"].append(c)
config["idx"] += 1
def spec_ttestop(config):
if verbose>=1: print("spec_ttestop(",")")
S = config["S"]
instr = config["instrstar"][config["idx"]][0]
t = instr[0:3]
op = opcode2exec[instr][1]
c1 = config["operand_stack"].pop()
c = op(int(t[1:3]),c1)
if c == "trap": return c
config["operand_stack"].append(c)
config["idx"] += 1
def spec_trelop(config):
if verbose>=1: print("spec_trelop(",")")
S = config["S"]
instr = config["instrstar"][config["idx"]][0]
t = instr[0:3]
op = opcode2exec[instr][1]
c2 = config["operand_stack"].pop()
c1 = config["operand_stack"].pop()
c = op(int(t[1:3]),c1,c2)
if c == "trap": return c
config["operand_stack"].append(c)
config["idx"] += 1
def spec_t2cvtopt1(config):
if verbose>=1: print("spec_t2crtopt1(",")")
S = config["S"]
instr = config["instrstar"][config["idx"]][0]
t2 = instr[0:3]
t1 = instr[-3:]
op = opcode2exec[instr][1]
c1 = config["operand_stack"].pop()
if instr[4:15] == "reinterpret":
c2 = op(t1,t2,c1)
else:
c2 = op(int(t1[1:]),int(t2[1:]),c1)
if c2 == "trap": return c2
config["operand_stack"].append(c2)
config["idx"] += 1
# 4.4.2 PARAMETRIC INSTRUCTIONS
def spec_drop(config):
if verbose>=1: print("spec_drop(",")")
S = config["S"]
config["operand_stack"].pop()
config["idx"] += 1
def spec_select(config):
if verbose>=1: print("spec_select(",")")
S = config["S"]
operand_stack = config["operand_stack"]
c = operand_stack.pop()
val1 = operand_stack.pop()
val2 = operand_stack.pop()
if not c:
operand_stack.append(val1)
else:
operand_stack.append(val2)
config["idx"] += 1
# 4.4.3 VARIABLE INSTRUCTIONS
def spec_get_local(config):
if verbose>=1: print("spec_get_local(",")")
S = config["S"]
F = config["F"]
x = config["instrstar"][config["idx"]][1]
#print(F)
#print(F[-1])
#print(F[-1]["locals"])
#print(x)
val = F[-1]["locals"][x]
config["operand_stack"].append(val)
config["idx"] += 1
def spec_set_local(config):
if verbose>=1: print("spec_set_local(",")")
S = config["S"]
F = config["F"]
x = config["instrstar"][config["idx"]][1]
val = config["operand_stack"].pop()
F[-1]["locals"][x] = val
config["idx"] += 1
def spec_tee_local(config):
if verbose>=1: print("spec_tee_local(",")")
S = config["S"]
x = config["instrstar"][config["idx"]][1]
operand_stack = config["operand_stack"]
val = operand_stack.pop()
operand_stack.append(val)
operand_stack.append(val)
spec_set_local(config)
#config["idx"] += 1
def spec_get_global(config):
if verbose>=1: print("spec_get_global(",")")
S = config["S"]
F = config["F"]
#print("F[-1]",F[-1])
x = config["instrstar"][config["idx"]][1]
a = F[-1]["module"]["globaladdrs"][x]
glob = S["globals"][a]