-
Notifications
You must be signed in to change notification settings - Fork 39
/
ssa.go
4537 lines (4004 loc) · 117 KB
/
ssa.go
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 2023 Sneller, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package vm
import (
"encoding/binary"
"errors"
"fmt"
"io"
"math"
"math/bits"
"os"
"strconv"
"strings"
"slices"
"golang.org/x/sys/cpu"
"github.com/SnellerInc/sneller/date"
"github.com/SnellerInc/sneller/expr"
"github.com/SnellerInc/sneller/heap"
"github.com/SnellerInc/sneller/internal/stringext"
"github.com/SnellerInc/sneller/ion"
"github.com/SnellerInc/sneller/regexp2"
)
// MaxSymbolID is the largest symbol ID
// supported by the system.
const MaxSymbolID = (1 << 21) - 1
type value struct {
id int
op ssaop
args []*value
// if this value has non-standard
// not-missing-ness, then that is set here
notMissing *value
imm any
}
type hashcode [6]uint64
type sympair struct {
sym ion.Symbol
val string
}
type symid struct {
id int
val string
}
type prog struct {
values []*value // all values in program
ret *value // value actually yielded by program
// used to find common expressions
dict []string // common strings
tmpSt ion.Symtab // temporary symbol table we need to encode data for dict
exprs map[hashcode]*value // common expressions
reserved []stackslot
// symbolized records whether
// the program has been symbolized
symbolized bool
// literals records whether
// there are complex literals in
// the bytecode that may reference
// the input symbol table
literals bool
// if symbolized is set,
// resolved is the list of symbols
// and their IDs when symbolization
// happens; we use this to determine
// staleness
resolved []sympair
// similarly, resolvedAux is resolved[] but for aux bindings
resolvedAux []symid
// finalizers that must be run when this prog is GC'd
finalize []func()
}
func (p *prog) reset() {
for i := range p.finalize {
p.finalize[i]()
}
p.finalize = p.finalize[:0]
*p = prog{}
}
// ReserveSlot reserves a stack slot
// for use by the program (independently
// of any register saving and reloading
// that has to be performed).
func (p *prog) reserveSlot(slot stackslot) {
for i := range p.reserved {
if p.reserved[i] == slot {
return
}
}
p.reserved = append(p.reserved, slot)
}
// dictionary strings must be padded to
// multiples of 4 bytes so that we never
// cross a page boundary when reading past
// the end of the string
func pad(x string) string {
buf := []byte(x)
for len(buf)&3 != 0 {
buf = append(buf, 0)
}
return string(buf)[:len(x)]
}
func (p *prog) binaryDataToBits(str string) uint64 {
for i := range p.dict {
if str == p.dict[i] {
return uint64(i)
}
}
p.dict = append(p.dict, pad(str))
return uint64(len(p.dict) - 1)
}
// used to produce a consistent bit pattern
// for hashing common subexpressions
func (p *prog) tobits(imm any) uint64 {
switch v := imm.(type) {
case stackslot:
panic("Stack slot must be converted to int when storing it in value.imm")
case float64:
return math.Float64bits(v)
case float32:
return math.Float64bits(float64(v))
case int64:
return uint64(v)
case uint64:
return v
case uint16:
return uint64(v)
case uint:
return uint64(v)
case int:
return uint64(v)
case ion.Symbol:
return uint64(v)
case aggregateslot:
return uint64(v)
case string:
return p.binaryDataToBits(v)
case bool:
if v {
return 1
}
return 0
case date.Time:
var buf ion.Buffer
buf.WriteTime(v)
return p.binaryDataToBits(string(buf.Bytes()[1:]))
case ion.Datum:
buf := ion.Buffer{}
v.Encode(&buf, &p.tmpSt)
return p.binaryDataToBits(string(buf.Bytes()))
default:
panic(fmt.Sprintf("invalid immediate %+v with type %T", imm, imm))
}
}
// overwrite a value with a message
// indicating why it is invalid
func (v *value) errf(f string, args ...any) {
v.op = sinvalid
v.args = nil
v.imm = fmt.Sprintf(f, args...)
}
func (v *value) setimm(imm any) {
if v.op != sinvalid && ssainfo[v.op].immfmt == fmtnone {
v.errf("cannot assign immediate %v to op %s", imm, v.op)
return
}
v.imm = imm
}
func (v *value) geterror() error {
if v.op != sinvalid {
return nil
}
return errors.New(v.imm.(string))
}
func (p *prog) errorf(f string, args ...any) *value {
v := p.val()
v.errf(f, args...)
return v
}
func (p *prog) begin() {
p.exprs = make(map[hashcode]*value)
p.values = nil
p.ret = nil
p.dict = nil
p.tmpSt.Reset()
// op 0 is always 'init'
v := p.val()
v.op = sinit
// op 1 is always 'undef'
v = p.val()
v.op = sundef
p.symbolized = false
p.resolved = p.resolved[:0]
}
func (p *prog) val() *value {
v := new(value)
p.values = append(p.values, v)
v.id = len(p.values) - 1
return v
}
func (s ssaop) String() string {
return ssainfo[s].text
}
func (v *value) checkarg(arg *value, idx int) {
if v.op == sinvalid {
return
}
in := ssainfo[arg.op].rettype
argtype := ssainfo[v.op].argType(idx)
if arg.op == sinvalid {
v.op = sinvalid
v.args = nil
v.imm = arg.imm
return
}
// the type of this assignment should be unambiguous;
// we can specify multiple possible return and argument
// types for a given return value and argument position,
// but only one of them should be valid
//
// (the only case where this doesn't hold is if the
// input argument is an undef value)
want := argtype
if bits.OnesCount(uint(in&want)) != 1 && arg.op != sundef {
v.errf("ambiguous assignment type (%s=%s as argument of type %s to %s of type %s)",
arg.Name(), arg, in.String(), v.op, want.String())
}
}
func (p *prog) validLanes() *value {
return p.values[0]
}
// helper for simplification rules
func (p *prog) choose(yes bool) *value {
if yes {
return p.values[0]
}
return p.ssa0(skfalse)
}
func (p *prog) ssa0(op ssaop) *value {
var hc hashcode
hc[0] = uint64(op)
if v := p.exprs[hc]; v != nil {
return v
}
v := p.val()
v.op = op
p.exprs[hc] = v
return v
}
func (p *prog) ssa0imm(op ssaop, imm any) *value {
var hc hashcode
hc[0] = uint64(op)
hc[1] = p.tobits(imm)
if v := p.exprs[hc]; v != nil {
return v
}
v := p.val()
v.op = op
v.setimm(imm)
v.args = []*value{}
if v.op != sinvalid {
p.exprs[hc] = v
}
return v
}
func (p *prog) ssa1(op ssaop, arg *value) *value {
var hc hashcode
hc[0] = uint64(op)
hc[1] = uint64(arg.id)
if v := p.exprs[hc]; v != nil {
return v
}
v := p.val()
v.op = op
v.args = []*value{arg}
v.checkarg(arg, 0)
if v.op != sinvalid {
p.exprs[hc] = v
}
return v
}
func (p *prog) ssa2imm(op ssaop, arg0, arg1 *value, imm any) *value {
var hc hashcode
hc[0] = uint64(op)
hc[1] = uint64(arg0.id)
hc[2] = uint64(arg1.id)
hc[3] = p.tobits(imm)
if v := p.exprs[hc]; v != nil {
return v
}
v := p.val()
v.op = op
v.setimm(imm)
v.args = []*value{arg0, arg1}
v.checkarg(arg0, 0)
v.checkarg(arg1, 1)
if v.op != sinvalid {
p.exprs[hc] = v
}
return v
}
func (p *prog) ssa2(op ssaop, arg0 *value, arg1 *value) *value {
var hc hashcode
hc[0] = uint64(op)
hc[1] = uint64(arg0.id)
hc[2] = uint64(arg1.id)
if v := p.exprs[hc]; v != nil {
return v
}
v := p.val()
v.op = op
v.args = []*value{arg0, arg1}
v.checkarg(arg0, 0)
v.checkarg(arg1, 1)
if v.op != sinvalid {
p.exprs[hc] = v
}
return v
}
func (p *prog) ssa3(op ssaop, arg0, arg1, arg2 *value) *value {
var hc hashcode
hc[0] = uint64(op)
hc[1] = uint64(arg0.id)
hc[2] = uint64(arg1.id)
hc[3] = uint64(arg2.id)
if v := p.exprs[hc]; v != nil {
return v
}
v := p.val()
v.op = op
v.args = []*value{arg0, arg1, arg2}
v.checkarg(arg0, 0)
v.checkarg(arg1, 1)
v.checkarg(arg2, 2)
if v.op != sinvalid {
p.exprs[hc] = v
}
return v
}
func (p *prog) ssa3imm(op ssaop, arg0, arg1, arg2 *value, imm any) *value {
var hc hashcode
hc[0] = uint64(op)
hc[1] = uint64(arg0.id)
hc[2] = uint64(arg1.id)
hc[3] = uint64(arg2.id)
hc[4] = p.tobits(imm)
if v := p.exprs[hc]; v != nil {
return v
}
v := p.val()
v.op = op
v.setimm(imm)
v.args = []*value{arg0, arg1, arg2}
v.checkarg(arg0, 0)
v.checkarg(arg1, 1)
v.checkarg(arg2, 2)
return v
}
func (p *prog) ssa4(op ssaop, arg0, arg1, arg2, arg3 *value) *value {
var hc hashcode
hc[0] = uint64(op)
hc[1] = uint64(arg0.id)
hc[2] = uint64(arg1.id)
hc[3] = uint64(arg2.id)
hc[4] = uint64(arg3.id)
if v := p.exprs[hc]; v != nil {
return v
}
v := p.val()
v.op = op
v.args = []*value{arg0, arg1, arg2, arg3}
v.checkarg(arg0, 0)
v.checkarg(arg1, 1)
v.checkarg(arg2, 2)
v.checkarg(arg3, 3)
if v.op != sinvalid {
p.exprs[hc] = v
}
return v
}
func (p *prog) ssa4imm(op ssaop, arg0, arg1, arg2, arg3 *value, imm any) *value {
var hc hashcode
hc[0] = uint64(op)
hc[1] = uint64(arg0.id)
hc[2] = uint64(arg1.id)
hc[3] = uint64(arg2.id)
hc[4] = uint64(arg3.id)
hc[5] = p.tobits(imm)
if v := p.exprs[hc]; v != nil {
return v
}
v := p.val()
v.op = op
v.setimm(imm)
v.args = []*value{arg0, arg1, arg2, arg3}
v.checkarg(arg0, 0)
v.checkarg(arg1, 1)
v.checkarg(arg2, 2)
v.checkarg(arg3, 3)
return v
}
func (p *prog) ssa5(op ssaop, arg0, arg1, arg2, arg3, arg4 *value) *value {
var hc hashcode
hc[0] = uint64(op)
hc[1] = uint64(arg0.id)
hc[2] = uint64(arg1.id)
hc[3] = uint64(arg2.id)
hc[4] = uint64(arg3.id)
hc[5] = uint64(arg4.id)
if v := p.exprs[hc]; v != nil {
return v
}
v := p.val()
v.op = op
v.args = []*value{arg0, arg1, arg2, arg3, arg4}
v.checkarg(arg0, 0)
v.checkarg(arg1, 1)
v.checkarg(arg2, 2)
v.checkarg(arg3, 3)
v.checkarg(arg4, 4)
if v.op != sinvalid {
p.exprs[hc] = v
}
return v
}
// overwrite a value with new opcode + args, etc.
func (p *prog) setssa(v *value, op ssaop, imm any, args ...*value) *value {
v.op = op
v.notMissing = nil
if imm == nil {
v.imm = nil
} else {
v.setimm(imm)
}
v.args = shrink(v.args, len(args))
copy(v.args, args)
for i := range args {
v.checkarg(args[i], i)
}
return v
}
func (p *prog) ssaimm(op ssaop, imm any, args ...*value) *value {
v := p.val()
v.op = op
v.args = args
if imm != nil {
v.setimm(imm)
}
for i := range args {
v.checkarg(args[i], i)
}
if v.op == sinvalid {
panic("invalid op " + v.String())
}
return v
}
func (p *prog) ssava(op ssaop, args []*value) *value {
opInfo := &ssainfo[op]
baseArgCount := len(opInfo.argtypes)
v := p.val()
v.op = op
v.args = args
if len(opInfo.vaArgs) == 0 {
v.errf("%s doesn't support variable arguments", op)
return v
}
if len(args) < baseArgCount {
v.errf("%s requires at least %d arguments (%d given)", op, baseArgCount, len(args))
return v
}
for i := range args {
v.checkarg(args[i], i)
}
return v
}
func (p *prog) constant(imm any) *value {
v := p.val()
v.op = sliteral
v.imm = imm
return v
}
// returnValue terminates the execution of the program and optionally fills
// output registers with values that are allocated on virtual stack.
func (p *prog) returnValue(v *value) {
info := &ssainfo[v.op]
// Return only accepts operations that actually return (terminate the execution).
if info.returnOp || v.op == sinvalid {
p.ret = v
return
}
// If the input is stMem, we would wrap it in a void return - this
// program doesn't return any value, it most likely only aggregates.
if (info.rettype & stMem) != 0 {
p.ret = p.ssa1(sretm, v)
return
}
panic(fmt.Sprintf("invalid return operation %s", v.op.String()))
}
func (p *prog) returnBool(mem, pred *value) {
p.returnValue(p.ssa2(sretmk, mem, pred))
}
func (p *prog) returnScalar(mem, scalar, pred *value) {
p.returnValue(p.ssa3(sretmsk, mem, scalar, pred))
}
func (p *prog) returnBK(base, pred *value) {
p.returnValue(p.ssa2(sretbk, base, pred))
}
func (p *prog) returnBHK(base, hash, pred *value) {
p.returnValue(p.ssa3(sretbhk, base, hash, pred))
}
// initMem returns the memory token associated
// with the initial memory state.
func (p *prog) initMem() *value {
return p.ssa0(sinitmem)
}
// Store stores a value to a stack slot and
// returns the associated memory token.
// The store operation is guaranteed to happen
// after the 'mem' op.
func (p *prog) store(mem *value, v *value, slot stackslot) (*value, error) {
p.reserveSlot(slot)
if v.op == skfalse {
return p.ssa3imm(sstorev, mem, v, p.validLanes(), int(slot)), nil
}
switch v.primary() {
case stValue:
return p.ssa3imm(sstorev, mem, v, p.mask(v), int(slot)), nil
default:
return nil, fmt.Errorf("cannot store value %s", v)
}
}
func (p *prog) missing() *value {
return p.ssa0(skfalse)
}
func (p *prog) isMissing(v *value) *value {
return p.not(p.notMissing(v))
}
// notMissing walks logical expressions until
// it finds a terminal true/false value
// or an expression that computes a real return
// value that could be MISSING (i.e. mask=0)
func (p *prog) notMissing(v *value) *value {
if v.notMissing != nil {
return v.notMissing
}
nonLogical := func(v *value) bool {
info := ssainfo[v.op].argtypes
for i := range info {
if info[i] != stBool {
return true
}
}
return false
}
// non-logical instructions
// (scalar comparisons, etc.) only operate
// on non-MISSING lanes, so the mask argument
// is equivalent to NOT MISSING
if nonLogical(v) {
rt := v.ret()
switch {
case rt == stBool:
// this is a comparison; the mask arg
// is the set of lanes to compare
// (and therefore NOT MISSING)
return v.maskarg()
case rt&stBool != 0:
// the result is equivalent to NOT MISSING
return p.ssa1(snotmissing, v)
default:
// arithmetic or other op with no return mask;
// the mask argument is implicitly the NOT MISSING value
return p.mask(v)
}
}
switch v.op {
case skfalse, sinit:
return v
case sandn:
return p.and(p.notMissing(v.args[0]), v.args[1])
case sxor, sxnor:
// for xor and xnor, the result is only
// non-missing if both sides of the comparison
// are non-MISSING values
return p.and(p.notMissing(v.args[0]), p.notMissing(v.args[1]))
case sand:
// we need
// | TRUE | FALSE | MISSING
// --------+---------+-------+--------
// TRUE | TRUE | FALSE | MISSING
// FALSE | FALSE | FALSE | FALSE
// MISSING | MISSING | FALSE | MISSING
//
return p.or(v, p.or(
p.isFalse(v.args[0]),
p.isFalse(v.args[1]),
))
case sor:
// we need
// | TRUE | FALSE | MISSING
// --------+---------+----------+--------
// TRUE | TRUE | TRUE | TRUE
// FALSE | TRUE | FALSE | MISSING
// MISSING | TRUE | MISSING | MISSING
//
// so, the NOT MISSING mask is
// (A OR B) OR (A IS NOT MISSING AND B IS NOT MISSING)
return p.or(v, p.and(p.notMissing(v.args[0]), p.notMissing(v.args[1])))
default:
m := v.maskarg()
if m == nil {
return p.validLanes()
}
return p.notMissing(m)
}
}
// MergeMem merges memory tokens into a memory token.
// (This can be used to create a partial ordering
// constraint for memory operations.)
func (p *prog) mergeMem(args ...*value) *value {
if len(args) == 1 {
return args[0]
}
v := p.val()
v.op = smergemem
v.args = args
return v
}
// various tuple constructors:
// these just combine a non-mask
// register value (S, V, etc.)
// with a mask value into a single value;
// makes a V+K tuple so `p.mask(v)` returns `k`
func (p *prog) makevk(v, k *value) *value {
if v == k || p.mask(v) == k {
return v
}
return p.ssa2(smakevk, v, k)
}
// float+K tuple
func (p *prog) floatk(f, k *value) *value {
return p.ssa2(sfloatk, f, k)
}
// Dot computes <base>.col
func (p *prog) dot(col string, base *value) *value {
if base != p.values[0] {
// need to perform a conversion from
// a value pointer to an interior-of-structure pointer
base = p.ssa2(stuples, base, base)
}
return p.ssa2imm(sdot, base, base, col)
}
func (p *prog) tolist(v *value) *value {
switch v.ret() {
case stListMasked, stListAndValueMasked:
return v
case stValue, stValueMasked:
return p.ssa2(stolist, v, p.mask(v))
default:
return p.errorf("cannot convert value %s to list", v)
}
}
func (p *prog) isFalse(v *value) *value {
switch v.primary() {
case stBool:
// need to differentiate between
// the zero predicate from MISSING
// and the zero predicate from FALSE
return p.ssa2(sandn, v, p.notMissing(v))
case stValue:
return p.ssa2(sisfalse, v, p.mask(v))
default:
return p.errorf("bad argument %s to IsFalse", v)
}
}
func (p *prog) isTrue(v *value) *value {
switch v.primary() {
case stBool:
return v
case stValue:
return p.ssa2(sistrue, v, p.mask(v))
default:
return p.errorf("bad argument %s to IsTrue", v)
}
}
func (p *prog) isNotTrue(v *value) *value {
// we compute predicates as IS TRUE,
// so IS NOT TRUE is simply the complement
return p.not(v)
}
func (p *prog) isNotFalse(v *value) *value {
return p.or(p.isTrue(v), p.isMissing(v))
}
// Index evaluates v[i] for a constant index.
// The returned value is v[i] if evaluated as
// a value, or v[i+1:] when evaluated as a list.
//
// FIXME: make the multiple-return-value behavior
// here less confusing.
// NOTE: array access is linear- rather than
// constant-time, so accessing large offsets
// can be very slow.
func (p *prog) index(v *value, i int) *value {
l := p.tolist(v)
for i >= 0 {
// NOTE: CSE will take care of
// ensuring that the access of
// list[n] occurs before list[n+1]
// since computing list[n+1] implicitly
// computes list[n]!
l = p.ssa2(ssplit, l, l)
i--
}
return l
}
func (s ssatype) ordnum() int {
switch s {
case stBool:
return 0
case stValue:
return 1
case stInt:
return 2
case stFloat:
return 3
case stString:
return 4
case stTime:
return 5
default:
return 6
}
}
// Equals computes 'left == right'
func (p *prog) equals(left, right *value) *value {
if (left.op == sliteral) && (right.op == sliteral) {
// TODO: int64(1) == float64(1.0) ??
return p.constant(left.imm == right.imm)
}
// make ordering deterministic:
// if there is a constant, put it on the right-hand-side;
// otherwise pick an ordering for input argtypes and enforce it
if left.op == sliteral || left.primary().ordnum() > right.primary().ordnum() {
left, right = right, left
}
switch left.primary() {
case stBool:
// (bool) = (bool)
// is an xnor op, but additionally
// we have to check that the values
// are not MISSING
if right.op == sliteral {
b, ok := right.imm.(bool)
if !ok {
// left = <not a bool> -> nope
return p.missing()
}
if b {
// left = TRUE -> left mask
return left
}
// left = FALSE -> !left and left is not missing
return p.andn(left, p.notMissing(left))
}
if right.ret()&stBool == 0 {
return p.errorf("cannot compare bool(%s) and other(%s)", left, right)
}
// mask = value -> mask = (istrue value)
if right.primary() == stValue {
right = p.isTrue(right)
}
allok := p.and(p.notMissing(left), p.notMissing(right))
return p.and(p.xnor(left, right), allok)
case stValue:
if right.op == sliteral {
if _, ok := right.imm.(string); ok {
// only need this for string comparison
left = p.unsymbolized(left)
}
return p.ssa2imm(sequalconst, left, p.mask(left), right.imm)
}
switch right.primary() {
case stValue:
left = p.unsymbolized(left)
right = p.unsymbolized(right)
return p.ssa3(scmpeqv, left, right, p.ssa2(sand, p.mask(left), p.mask(right)))
case stInt:
lefti, k := p.coerceI64(left)
return p.ssa3(scmpeqi, lefti, right, p.and(k, p.mask(right)))
case stFloat:
leftf, k := p.coerceF64(left)
return p.ssa3(scmpeqf, leftf, right, p.and(k, p.mask(right)))
case stString:
leftstr := p.coerceStr(left)
return p.ssa3(scmpeqstr, leftstr, right, p.and(p.mask(leftstr), p.mask(right)))
case stTime:
leftts, leftk := p.coerceTimestamp(left)
return p.ssa3(scmpeqts, leftts, right, p.and(p.mask(leftk), p.mask(right)))
default:
return p.errorf("cannot compare value %s and other %s", left, right)
}
case stInt:
if right.op == sliteral {
return p.ssa2imm(scmpeqimmi, left, p.mask(left), right.imm)
}
if right.primary() == stInt {
return p.ssa3(scmpeqi, left, right, p.and(p.mask(left), p.mask(right)))
}
// falthrough to floating-point comparison
left = p.ssa2(scvti64tof64, left, p.mask(left))
fallthrough
case stFloat:
if right.op == sliteral {
return p.ssa2imm(scmpeqimmf, left, p.mask(left), right.imm)
}
switch right.primary() {
case stInt:
right = p.ssa2(scvti64tof64, right, p.mask(right))
fallthrough
case stFloat:
return p.ssa3(scmpeqf, left, right, p.and(p.mask(left), p.mask(right)))
default:
return p.missing() // FALSE/MISSING
}
case stString:
if right.op == sliteral {
return p.ssa2imm(sStrCmpEqCs, left, left, right.imm)
}
switch right.primary() {
case stString:
return p.ssa3(scmpeqstr, left, right, p.and(p.mask(left), p.mask(right)))
default:
return p.missing() // FALSE/MISSING
}
case stTime:
switch right.primary() {
case stTime:
return p.ssa3(scmpeqts, left, right, p.and(p.mask(left), p.mask(right)))
case stValue:
rv, rm := p.coerceTimestamp(right)
return p.ssa3(scmpeqts, left, rv, p.and(p.mask(left), rm))
default:
return p.missing() // FALSE/MISSING
}
default:
return p.errorf("cannot compare %s and %s", left, right)
}
}
// octetLength returns the number of bytes in v
func (p *prog) octetLength(v *value) *value {
v = p.coerceStr(v)
return p.ssa2(soctetlength, v, p.mask(v))
}
// charLength returns the number of unicode code-points in v
func (p *prog) charLength(v *value) *value {
v = p.coerceStr(v)
return p.ssa2(scharacterlength, v, p.mask(v))
}
// Substring returns a substring at the provided startIndex with length
func (p *prog) substring(v, substrOffset, substrLength *value) *value {
offsetInt, offsetMask := p.coerceI64(substrOffset)
lengthInt, lengthMask := p.coerceI64(substrLength)
mask := p.and(v, p.and(offsetMask, lengthMask))
return p.ssa4(sSubStr, v, offsetInt, lengthInt, mask)
}
// SplitPart splits string on delimiter and returns the field index. Field indexes start with 1.
func (p *prog) splitPart(v *value, delimiter byte, index *value) *value {
delimiterStr := string(delimiter)
indexInt, indexMask := p.coerceI64(index)
mask := p.and(v, indexMask)
return p.ssa3imm(sSplitPart, v, indexInt, mask, delimiterStr)
}
// is v an ion null value?
func (p *prog) isnull(v *value) *value {
if v.primary() != stValue {
return p.missing()
}
return p.ssa2(sisnull, v, p.mask(v))
}
// is v distinct from null?
// (i.e. non-missing and non-null?)
func (p *prog) isnonnull(v *value) *value {
if v.primary() != stValue {
return p.validLanes() // TRUE
}
return p.ssa2(sisnonnull, v, p.mask(v))
}
func isBoolImmediate(imm any) bool {
switch imm.(type) {