-
Notifications
You must be signed in to change notification settings - Fork 20
/
compilation_ctx.cpp
1838 lines (1532 loc) · 84.3 KB
/
compilation_ctx.cpp
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
#include "compilation_ctx.h"
#include "common.h"
using namespace Trinity;
namespace // static/local this module
{
static constexpr bool traceCompile{false};
}
compilation_ctx::phrase *compilation_ctx::register_phrase(const Trinity::phrase *p) {
auto ptr = static_cast<phrase *>(allocator.Alloc(sizeof(phrase) + sizeof(exec_term_id_t) * p->size));
ptr->size = p->size;
for (size_t i{0}; i != p->size; ++i) {
if (const auto id = resolve_query_term(p->terms[i].token))
ptr->termIDs[i] = id;
else
return nullptr;
}
return ptr;
}
uint8_t compilation_ctx::phrase::intersection(const termsrun *const tr, exec_term_id_t *const out) const noexcept {
uint16_t n{0};
for (size_t i{0}; i != size; ++i) {
if (const auto id = termIDs[i]; tr->is_set(id))
out[n++] = id;
}
return n;
}
uint8_t compilation_ctx::phrase::disjoint_union(const termsrun *const tr, exec_term_id_t *const out) const noexcept {
uint16_t n{0};
const auto cnt = tr->size;
for (size_t i{0}; i != cnt; ++i) {
if (const auto id = tr->terms[i]; !is_set(id))
out[n++] = id;
}
return n;
}
bool compilation_ctx::phrase::intersected_by(const termsrun *const tr) const noexcept {
if (tr->size >= size) {
for (size_t i{0}; i != size; ++i) {
if (!tr->is_set(termIDs[i]))
return false;
}
return true;
} else
return false;
}
bool compilation_ctx::phrase::operator==(const phrase &o) const noexcept {
if (size == o.size) {
for (size_t i{0}; i != size; ++i) {
if (termIDs[i] != o.termIDs[i])
return false;
}
return true;
} else
return false;
}
bool compilation_ctx::phrase::is_set(const exec_term_id_t *const l, const uint8_t n) const noexcept {
if (n <= size) {
const auto upto = size - n;
for (size_t i{0}; i != upto; ++i) {
uint32_t k;
for (k = 0; k != n && l[k] == termIDs[i + k]; ++k)
continue;
if (k == n)
return true;
}
}
return false;
}
bool compilation_ctx::phrase::is_set(const exec_term_id_t id) const noexcept {
for (size_t i{0}; i != size; ++i) {
if (termIDs[i] == id)
return true;
}
return false;
}
bool compilation_ctx::termsrun::operator==(const termsrun &o) const noexcept {
if (size == o.size) {
for (size_t i{0}; i != size; ++i) {
if (terms[i] != o.terms[i])
return false;
}
return true;
} else
return false;
}
bool compilation_ctx::termsrun::is_set(const exec_term_id_t id) const noexcept {
for (size_t i{0}; i != size; ++i) {
if (terms[i] == id)
return true;
}
return false;
}
bool compilation_ctx::termsrun::erase(const exec_term_id_t id) noexcept {
for (size_t i{0}; i != size; ++i) {
if (terms[i] == id) {
memmove(terms + i, terms + i + 1, (--size - i) * sizeof(terms[0]));
return true;
}
}
return false;
}
bool compilation_ctx::termsrun::erase(const termsrun &o) {
// Fast scalar intersection scheme designed by N.Kurz.
// lemire/SIMDCompressionAndIntersection.cpp
const auto *A = terms, *B = o.terms;
const auto endA = terms + size;
const auto endB = o.terms + o.size;
auto out{terms};
for (;;) {
while (*A < *B) {
l1:
*out++ = *A;
if (++A == endA) {
l2:
if (const auto n = out - terms; n == size)
return false;
else {
size = n;
return true;
}
}
}
while (*A > *B) {
if (++B == endB) {
while (A != endA)
*out++ = *A++;
goto l2;
}
}
if (*A == *B) {
if (++A == endA || ++B == endB) {
while (A != endA)
*out++ = *A++;
goto l2;
}
} else
goto l1;
}
}
static const exec_node *stronger(const exec_node &a, const exec_node &b) noexcept {
if (a.fp == ENT::matchallphrases || a.fp == ENT::matchphrase)
return &a;
else
return &b;
}
static bool same(const exec_node &a, const exec_node &b) noexcept {
if (a.fp == ENT::matchallterms && b.fp == a.fp) {
const auto *const __restrict__ runa = static_cast<compilation_ctx::termsrun *>(a.ptr);
const auto *const __restrict__ runb = static_cast<compilation_ctx::termsrun *>(b.ptr);
return *runa == *runb;
} else if (a.fp == ENT::matchterm && b.fp == a.fp)
return a.u16 == b.u16;
else if (a.fp == ENT::matchphrase && b.fp == ENT::matchphrase) {
const auto *const __restrict__ pa = static_cast<compilation_ctx::phrase *>(a.ptr);
const auto *const __restrict__ pb = static_cast<compilation_ctx::phrase *>(b.ptr);
return *pa == *pb;
}
return false;
}
static exec_node compile_node(const ast_node *const n, compilation_ctx &cctx, simple_allocator &a) {
exec_node res;
require(n);
switch (n->type) {
case ast_node::Type::Dummy:
std::abort();
case ast_node::Type::Token:
res.u16 = cctx.register_token(n->p);
if (res.u16)
res.fp = ENT::matchterm;
else
res.fp = ENT::constfalse;
break;
case ast_node::Type::Phrase:
if (n->p->size == 1) {
res.u16 = cctx.register_token(n->p);
if (res.u16)
res.fp = ENT::matchterm;
else
res.fp = ENT::constfalse;
} else {
res.ptr = cctx.register_phrase(n->p);
if (res.ptr)
res.fp = ENT::matchphrase;
else
res.fp = ENT::constfalse;
}
break;
case ast_node::Type::BinOp: {
auto ctx = cctx.register_binop(compile_node(n->binop.lhs, cctx, a), compile_node(n->binop.rhs, cctx, a));
res.ptr = ctx;
switch (n->binop.op) {
case Operator::AND:
case Operator::STRICT_AND:
res.fp = ENT::logicaland;
break;
case Operator::OR:
res.fp = ENT::logicalor;
break;
case Operator::NOT:
res.fp = ENT::logicalnot;
break;
case Operator::NONE:
std::abort();
break;
}
} break;
case ast_node::Type::ConstFalse:
res.fp = ENT::constfalse;
break;
case ast_node::Type::MatchSome:
res.fp = ENT::matchsome;
{
const auto size = sizeof(compilation_ctx::partial_match_ctx) + sizeof(exec_node) * n->match_some.size;
auto pm = static_cast<compilation_ctx::partial_match_ctx *>(cctx.allocate(size));
pm->min = n->match_some.min;
pm->size = n->match_some.size;
for (size_t i{0}; i != n->match_some.size; ++i)
pm->nodes[i] = compile_node(n->match_some.nodes[i], cctx, a);
res.ptr = pm;
}
break;
case ast_node::Type::UnaryOp:
switch (n->unaryop.op) {
case Operator::AND:
case Operator::STRICT_AND:
res.fp = ENT::unaryand;
break;
case Operator::NOT:
res.fp = ENT::unarynot;
break;
default:
std::abort();
}
res.ptr = cctx.register_unaryop(compile_node(n->unaryop.expr, cctx, a));
break;
case ast_node::Type::ConstTrueExpr:
// use register_unaryop() for this as well
// no need for another register_x()
res.ptr = cctx.register_unaryop(compile_node(n->expr, cctx, a));
if (static_cast<compilation_ctx::unaryop_ctx *>(res.ptr)->expr.fp == ENT::constfalse)
res.fp = ENT::dummyop;
else
res.fp = ENT::consttrueexpr;
break;
}
return res;
}
struct execnodes_collection final {
uint16_t size;
exec_node a, b;
static execnodes_collection *make(simple_allocator &allocator, const exec_node a, const exec_node b) {
auto ptr = allocator.Alloc<execnodes_collection>();
ptr->a = a;
ptr->b = b;
return ptr;
}
};
template <typename T, typename T2>
static execnodes_collection *try_collect_impl(const exec_node lhs, const exec_node rhs, simple_allocator &a, T fp, T2 fp2) {
if ((lhs.fp == ENT::matchterm || lhs.fp == ENT::matchphrase || lhs.fp == fp || lhs.fp == fp2) && (rhs.fp == ENT::matchterm || rhs.fp == ENT::matchphrase || rhs.fp == fp || rhs.fp == fp2)) {
// collect collection, will turn this into 0+ termruns and 0+ phraseruns
return execnodes_collection::make(a, lhs, rhs);
} else
return nullptr;
}
template <typename T, typename T2>
static bool try_collect(exec_node &res, simple_allocator &a, T fp, T2 fp2) {
auto opctx = static_cast<compilation_ctx::binop_ctx *>(res.ptr);
if (auto ptr = try_collect_impl(opctx->lhs, opctx->rhs, a, fp, fp2)) {
res.fp = reinterpret_cast<decltype(res.fp)>(fp);
res.ptr = ptr;
return true;
} else
return false;
}
static void collapse_node(exec_node &n, compilation_ctx &cctx, simple_allocator &a, std::vector<exec_term_id_t> &terms, std::vector<const compilation_ctx::phrase *> &phrases, std::vector<exec_node> &stack) {
if (n.fp == ENT::consttrueexpr || n.fp == ENT::unaryand || n.fp == ENT::unarynot) {
auto ctx = static_cast<compilation_ctx::unaryop_ctx *>(n.ptr);
collapse_node(ctx->expr, cctx, a, terms, phrases, stack);
} else if (n.fp == ENT::matchsome) {
auto pm = static_cast<compilation_ctx::partial_match_ctx *>(n.ptr);
const auto size{pm->size};
for (size_t i{0}; i != size; ++i)
collapse_node(pm->nodes[i], cctx, a, terms, phrases, stack);
} else if (n.fp == ENT::matchallnodes || n.fp == ENT::matchanynodes) {
auto g = static_cast<compilation_ctx::nodes_group *>(n.ptr);
const auto size{g->size};
for (size_t i{0}; i != size; ++i)
collapse_node(g->nodes[i], cctx, a, terms, phrases, stack);
} else if (n.fp == ENT::logicaland || n.fp == ENT::logicalor || n.fp == ENT::logicalnot) {
auto ctx = static_cast<compilation_ctx::binop_ctx *>(n.ptr);
compilation_ctx::binop_ctx *otherCtx;
collapse_node(ctx->lhs, cctx, a, terms, phrases, stack);
collapse_node(ctx->rhs, cctx, a, terms, phrases, stack);
if (n.fp == ENT::logicaland) {
if (try_collect(n, a, ENT::SPECIALIMPL_COLLECTION_LOGICALAND, ENT::matchallterms))
return;
if ((ctx->lhs.fp == ENT::matchterm || ctx->lhs.fp == ENT::matchphrase || ctx->lhs.fp == ENT::SPECIALIMPL_COLLECTION_LOGICALAND) && ctx->rhs.fp == ENT::logicaland && ((otherCtx = static_cast<compilation_ctx::binop_ctx *>(ctx->rhs.ptr))->lhs.fp == ENT::matchterm || otherCtx->lhs.fp == ENT::SPECIALIMPL_COLLECTION_LOGICALAND || otherCtx->lhs.fp == ENT::matchphrase)) {
// lord AND (of AND (the AND rings))
// (lord of) AND (the AND rings)
auto collection = execnodes_collection::make(a, ctx->lhs, otherCtx->lhs);
ctx->lhs.fp = static_cast<decltype(ctx->lhs.fp)>(ENT::SPECIALIMPL_COLLECTION_LOGICALAND);
ctx->lhs.ptr = collection;
ctx->rhs = otherCtx->rhs;
return;
}
if (ctx->lhs.fp == ENT::consttrueexpr && ctx->rhs.fp == ENT::consttrueexpr) {
// [<foo> AND <bar>] => [ <foo,bar> ]
auto *const __restrict__ lhsCtx = static_cast<compilation_ctx::unaryop_ctx *>(ctx->lhs.ptr);
auto *const __restrict__ rhsCtx = static_cast<compilation_ctx::unaryop_ctx *>(ctx->rhs.ptr);
if (auto ptr = try_collect_impl(lhsCtx->expr, rhsCtx->expr, a, ENT::SPECIALIMPL_COLLECTION_LOGICALAND, ENT::matchallterms)) {
// reuse lhsCtx
lhsCtx->expr.fp = reinterpret_cast<decltype(lhsCtx->expr.fp)>(ENT::SPECIALIMPL_COLLECTION_LOGICALAND);
lhsCtx->expr.ptr = ptr;
n.ptr = lhsCtx;
n.fp = ENT::consttrueexpr;
return;
}
}
if (ctx->lhs.fp == ENT::consttrueexpr && ctx->rhs.fp == ENT::logicaland) {
// <foo> AND (<bar> AND whatever)
// <foo, bar> AND whatever
auto *const __restrict__ lhsCtx = static_cast<compilation_ctx::unaryop_ctx *>(ctx->lhs.ptr);
auto *const __restrict__ rhsCtx = static_cast<compilation_ctx::binop_ctx *>(ctx->rhs.ptr);
if (rhsCtx->lhs.fp == ENT::consttrueexpr) {
auto *const __restrict__ otherCtx = static_cast<compilation_ctx::unaryop_ctx *>(rhsCtx->lhs.ptr);
if (auto ptr = try_collect_impl(lhsCtx->expr,
otherCtx->expr, a, ENT::SPECIALIMPL_COLLECTION_LOGICALAND, ENT::matchallterms)) {
lhsCtx->expr.fp = reinterpret_cast<decltype(lhsCtx->expr.fp)>(ENT::SPECIALIMPL_COLLECTION_LOGICALAND);
lhsCtx->expr.ptr = ptr;
ctx->rhs = rhsCtx->rhs;
return;
}
}
}
} else if (n.fp == ENT::logicalor) {
if (try_collect(n, a, ENT::SPECIALIMPL_COLLECTION_LOGICALOR, ENT::matchanyterms))
return;
if ((ctx->lhs.fp == ENT::matchterm || ctx->lhs.fp == ENT::matchphrase || ctx->lhs.fp == ENT::SPECIALIMPL_COLLECTION_LOGICALOR) && ctx->rhs.fp == ENT::logicalor && ((otherCtx = static_cast<compilation_ctx::binop_ctx *>(ctx->rhs.ptr))->lhs.fp == ENT::matchterm || otherCtx->lhs.fp == ENT::SPECIALIMPL_COLLECTION_LOGICALOR || otherCtx->lhs.fp == ENT::matchphrase)) {
// lord OR (ring OR (rings OR towers))
// (lord OR ring) OR (rings OR towers)
auto collection = execnodes_collection::make(a, ctx->lhs, otherCtx->lhs);
ctx->lhs.fp = static_cast<decltype(ctx->lhs.fp)>(ENT::SPECIALIMPL_COLLECTION_LOGICALOR);
ctx->lhs.ptr = collection;
ctx->rhs = otherCtx->rhs;
return;
}
#if 0 // this is wrong, because we don't want e.g [ FOO <BAR> | <PONG> ] to [ FOO AND <FOO | PONG > ]
if (ctx->lhs.fp == ENT::consttrueexpr && ctx->rhs.fp == ENT::consttrueexpr)
{
// [<foo> OR <bar>] => [ OR<foo, bar> ]
auto *const __restrict__ lhsCtx = (compilation_ctx::unaryop_ctx *)ctx->lhs.ptr;
auto *const __restrict__ rhsCtx = (compilation_ctx::unaryop_ctx *)ctx->rhs.ptr;
if (auto ptr = try_collect_impl(lhsCtx->expr, rhsCtx->expr, a, ENT::SPECIALIMPL_COLLECTION_LOGICALOR, ENT::matchanyterms))
{
// reuse lhsCtx
lhsCtx->expr.fp = reinterpret_cast<decltype(lhsCtx->expr.fp)>(ENT::SPECIALIMPL_COLLECTION_LOGICALOR);
lhsCtx->expr.ptr = ptr;
n.ptr = lhsCtx;
n.fp = ENT::consttrueexpr;
return;
}
}
if (ctx->lhs.fp == ENT::consttrueexpr && ctx->rhs.fp == ENT::logicalor)
{
// <foo> OR (<bar> OR whatever)
// <foo, bar> OR whatever
auto *const __restrict__ lhsCtx = (compilation_ctx::unaryop_ctx *)ctx->lhs.ptr;
auto *const __restrict__ rhsCtx = (compilation_ctx::binop_ctx *)ctx->rhs.ptr;
if (rhsCtx->lhs.fp == ENT::consttrueexpr)
{
auto *const __restrict__ otherCtx = (compilation_ctx::unaryop_ctx *)rhsCtx->lhs.ptr;
if (auto ptr = try_collect_impl(lhsCtx->expr,
otherCtx->expr, a, ENT::SPECIALIMPL_COLLECTION_LOGICALOR, ENT::matchanyterms))
{
lhsCtx->expr.fp = reinterpret_cast<decltype(lhsCtx->expr.fp)>(ENT::SPECIALIMPL_COLLECTION_LOGICALOR);
lhsCtx->expr.ptr = ptr;
ctx->rhs = rhsCtx->rhs;
return;
}
}
}
#endif
}
}
}
static void trim_phrasesrun(exec_node &n, compilation_ctx::phrasesrun *pr) {
auto out = pr->phrases;
const auto size = pr->size;
uint32_t k;
for (size_t i{0}; i != size; ++i) {
auto p = pr->phrases[i];
for (k = i + 1; k != size && !(*p == *pr->phrases[k]); ++k)
continue;
if (k == size)
*out++ = p;
}
pr->size = out - pr->phrases;
if (pr->size == 1) {
n.fp = ENT::matchphrase;
n.ptr = pr->phrases[0];
} else {
std::sort(pr->phrases, pr->phrases + pr->size, [](const auto a, const auto b) {
return a->size < b->size;
});
}
}
static void expand_node(exec_node &n, compilation_ctx &cctx, simple_allocator &a, std::vector<exec_term_id_t> &terms, std::vector<const compilation_ctx::phrase *> &phrases, std::vector<exec_node> &stack) {
if (n.fp == ENT::consttrueexpr || n.fp == ENT::unaryand || n.fp == ENT::unarynot) {
auto ctx = static_cast<compilation_ctx::unaryop_ctx *>(n.ptr);
expand_node(ctx->expr, cctx, a, terms, phrases, stack);
} else if (n.fp == ENT::matchsome) {
auto ctx = static_cast<compilation_ctx::partial_match_ctx *>(n.ptr);
for (size_t i{0}; i != ctx->size; ++i)
expand_node(ctx->nodes[i], cctx, a, terms, phrases, stack);
} else if (n.fp == ENT::matchallnodes || n.fp == ENT::matchanynodes) {
auto g = static_cast<compilation_ctx::nodes_group *>(n.ptr);
for (size_t i{0}; i != g->size; ++i)
expand_node(g->nodes[i], cctx, a, terms, phrases, stack);
} else if (n.fp == ENT::logicaland || n.fp == ENT::logicalor || n.fp == ENT::logicalnot) {
auto ctx = static_cast<compilation_ctx::binop_ctx *>(n.ptr);
expand_node(ctx->lhs, cctx, a, terms, phrases, stack);
expand_node(ctx->rhs, cctx, a, terms, phrases, stack);
} else if (n.fp == ENT::SPECIALIMPL_COLLECTION_LOGICALOR || n.fp == ENT::SPECIALIMPL_COLLECTION_LOGICALAND) {
auto ctx = static_cast<execnodes_collection *>(n.ptr);
terms.clear();
phrases.clear();
stack.clear();
stack.push_back(ctx->a);
stack.push_back(ctx->b);
do {
auto en = stack.back();
stack.pop_back();
if (en.fp == ENT::matchterm) {
const auto termID = exec_term_id_t(en.u16);
terms.push_back(termID);
} else if (en.fp == ENT::matchphrase) {
const auto p = static_cast<compilation_ctx::phrase *>(en.ptr);
phrases.push_back(p);
} else if (en.fp == ENT::matchallterms || en.fp == ENT::matchanyterms) {
const auto run = static_cast<const compilation_ctx::termsrun *>(en.ptr);
terms.insert(terms.end(), run->terms, run->terms + run->size);
} else if (en.fp == ENT::SPECIALIMPL_COLLECTION_LOGICALOR || en.fp == ENT::SPECIALIMPL_COLLECTION_LOGICALAND) {
auto ctx = static_cast<execnodes_collection *>(en.ptr);
stack.push_back(ctx->a);
stack.push_back(ctx->b);
}
} while (!stack.empty());
std::sort(terms.begin(), terms.end());
terms.resize(std::unique(terms.begin(), terms.end()) - terms.begin());
if constexpr (traceCompile)
SLog("Expanded terms = ", terms.size(), ", phrases = ", phrases.size(), " ", n.fp == ENT::SPECIALIMPL_COLLECTION_LOGICALOR ? "OR" : "AND", "\n");
// We have expanded this collection into tokens and phrases
// Depending on the number of tokens and phrases we collected we will
// create:
// - (ENT::matchanyterms/ENT::matchallterms OP phrase)
// - (ENT::matchanyterms/ENT::matchallterms)
// - (ENT::matchanyterms/ENT::matchallterms OP (ENT::matchanyphrases/ENT::matchallphrases))
// - (token OP (ENT::matchanyphrases/ENT::matchallphrases))
// - (ENT::matchanyphrases/ENT::matchallphrases)
if (const auto termsCnt = terms.size(), phrasesCnt = phrases.size(); termsCnt == 1) {
exec_node termNode;
termNode.fp = ENT::matchterm;
termNode.u16 = terms.front();
if (phrasesCnt == 0)
n = termNode;
else if (phrasesCnt == 1) {
exec_node phraseMatchNode;
phraseMatchNode.fp = ENT::matchphrase;
phraseMatchNode.ptr = (void *)phrases.front();
n.fp = (n.fp == ENT::SPECIALIMPL_COLLECTION_LOGICALOR ? ENT::logicalor : ENT::logicaland);
n.ptr = cctx.register_binop(termNode, phraseMatchNode);
} else {
exec_node phrasesRunNode;
auto phrasesRun = static_cast<compilation_ctx::phrasesrun *>(cctx.allocator.Alloc(sizeof(compilation_ctx::phrasesrun) + phrasesCnt * sizeof(compilation_ctx::phrase *)));
phrasesRun->size = phrasesCnt;
memcpy(phrasesRun->phrases, phrases.data(), phrasesCnt * sizeof(compilation_ctx::phrase *));
phrasesRunNode.fp = (n.fp == ENT::SPECIALIMPL_COLLECTION_LOGICALOR ? ENT::matchanyphrases : ENT::matchallphrases);
phrasesRunNode.ptr = (void *)phrasesRun;
n.fp = (n.fp == ENT::SPECIALIMPL_COLLECTION_LOGICALOR ? ENT::logicalor : ENT::logicaland);
n.ptr = cctx.register_binop(termNode, phrasesRunNode);
trim_phrasesrun(n, phrasesRun);
}
} else if (termsCnt > 1) {
auto run = static_cast<compilation_ctx::termsrun *>(cctx.runsAllocator.Alloc(sizeof(compilation_ctx::termsrun) + sizeof(exec_term_id_t) * termsCnt));
exec_node runNode;
run->size = termsCnt;
memcpy(run->terms, terms.data(), termsCnt * sizeof(exec_term_id_t));
runNode.fp = (n.fp == ENT::SPECIALIMPL_COLLECTION_LOGICALOR ? ENT::matchanyterms : ENT::matchallterms);
runNode.ptr = run;
// see same()
// we will eventually sort those terms by evaluation cost, but for now
// it is important to sort them by id so that we can easily check for termruns eq.
std::sort(run->terms, run->terms + run->size);
if (phrasesCnt == 0) {
n = runNode;
} else if (phrasesCnt == 1) {
exec_node phraseNode;
phraseNode.fp = ENT::matchphrase;
phraseNode.ptr = (void *)(phrases.front());
n.fp = (n.fp == ENT::SPECIALIMPL_COLLECTION_LOGICALOR ? ENT::logicalor : ENT::logicaland);
n.ptr = cctx.register_binop(runNode, phraseNode);
} else {
exec_node phrasesRunNode;
auto phrasesRun = static_cast<compilation_ctx::phrasesrun *>(cctx.allocator.Alloc(sizeof(compilation_ctx::phrasesrun) + phrasesCnt * sizeof(compilation_ctx::phrase *)));
phrasesRun->size = phrasesCnt;
memcpy(phrasesRun->phrases, phrases.data(), phrasesCnt * sizeof(compilation_ctx::phrase *));
phrasesRunNode.fp = (n.fp == ENT::SPECIALIMPL_COLLECTION_LOGICALOR ? ENT::matchanyphrases : ENT::matchallphrases);
phrasesRunNode.ptr = (void *)phrasesRun;
trim_phrasesrun(phrasesRunNode, phrasesRun);
n.fp = (n.fp == ENT::SPECIALIMPL_COLLECTION_LOGICALOR ? ENT::logicalor : ENT::logicaland);
n.ptr = cctx.register_binop(runNode, phrasesRunNode);
}
} else if (termsCnt == 0) {
if (phrasesCnt == 1) {
n.fp = ENT::matchphrase;
n.ptr = (void *)phrases.front();
} else {
auto phrasesRun = static_cast<compilation_ctx::phrasesrun *>(cctx.allocator.Alloc(sizeof(compilation_ctx::phrasesrun) + phrasesCnt * sizeof(compilation_ctx::phrase *)));
phrasesRun->size = phrasesCnt;
memcpy(phrasesRun->phrases, phrases.data(), phrasesCnt * sizeof(compilation_ctx::phrase *));
n.fp = (n.fp == ENT::SPECIALIMPL_COLLECTION_LOGICALOR ? ENT::matchanyphrases : ENT::matchallphrases);
n.ptr = (void *)phrasesRun;
trim_phrasesrun(n, phrasesRun);
}
}
#if 0
// if we set_dirty() here
// fails for:
// ./T ' sf OR "san francisco" OR san-francisco pizza OR pizzaria OR tavern OR inn OR mcdonalds '
// but if we don't, it fails to optimize:
// ./T ' "world of warcraft" "mists of pandaria" pandaria '
#endif
} else if (n.fp == ENT::matchallterms || n.fp == ENT::matchanyterms) {
const auto run = static_cast<const compilation_ctx::termsrun *>(n.ptr);
if (run->size == 1) {
n.fp = ENT::matchterm;
n.u16 = run->terms[0];
}
} else if (n.fp == ENT::matchphrase) {
const auto p = static_cast<compilation_ctx::phrase *>(n.ptr);
if (p->size == 1) {
n.fp = ENT::matchterm;
n.u16 = p->termIDs[0];
}
}
}
static exec_node optimize_node(exec_node n, compilation_ctx &cctx, simple_allocator &a, std::vector<exec_term_id_t> &terms, std::vector<const compilation_ctx::phrase *> &phrases, std::vector<exec_node> &stack, bool &updates, const exec_node *const root) {
const auto saved{n};
if constexpr (traceCompile)
SLog("Before OPT:", n, "\n");
#define set_dirty() \
do { \
if constexpr (traceCompile) \
SLog("HERE from [", saved, "] to [", n, "]\n"); \
updates = true; \
} while (0)
if (n.fp == ENT::consttrueexpr) {
auto ctx = static_cast<compilation_ctx::unaryop_ctx *>(n.ptr);
ctx->expr = optimize_node(ctx->expr, cctx, a, terms, phrases, stack, updates, root);
if (ctx->expr.fp == ENT::constfalse || ctx->expr.fp == ENT::dummyop) {
n.fp = ENT::dummyop;
set_dirty();
}
} else if (n.fp == ENT::matchallnodes || n.fp == ENT::matchanynodes) {
auto g = static_cast<compilation_ctx::nodes_group *>(n.ptr);
if (0 == g->size) {
n.fp = ENT::constfalse;
set_dirty();
return n;
} else {
// if they are all tokens, switch to respective matchanyterm or matchallterms
std::size_t dummy_nodes{0}, false_nodes{0}, true_nodes{0}, term_nodes{0};
const size_t size = g->size;
for (size_t i{0}; i < size; ++i) {
if (const auto p = g->nodes[i].fp; p == ENT::matchterm)
++term_nodes;
else if (p == ENT::dummyop) {
if (n.fp == ENT::matchallnodes) {
n.fp = ENT::constfalse;
set_dirty();
return n;
} else {
++dummy_nodes;
}
} else if (p == ENT::consttrue)
++true_nodes;
else if (p == ENT::constfalse) {
if (n.fp == ENT::matchallnodes) {
n.fp = ENT::constfalse;
set_dirty();
return n;
} else {
++false_nodes;
}
} else
break;
}
if (term_nodes == size) {
exec_node new_node;
auto tr = static_cast<compilation_ctx::termsrun *>(a.Alloc(sizeof(compilation_ctx::termsrun) + sizeof(exec_term_id_t) * g->size));
tr->size = g->size;
for (size_t i = 0; i != g->size; ++i)
tr->terms[i] = g->nodes[i].u16;
new_node.fp = n.fp == ENT::matchallnodes ? ENT::matchallterms : ENT::matchanyterms;
new_node.ptr = tr;
n = new_node;
set_dirty();
return n;
} else if (dummy_nodes == size) {
n.fp = ENT::dummyop;
set_dirty();
return n;
} else if (true_nodes == size) {
n.fp = ENT::consttrue;
set_dirty();
return n;
} else if (false_nodes == size) {
n.fp = ENT::constfalse;
set_dirty();
return n;
}
}
} else if (n.fp == ENT::matchsome) {
auto ctx = static_cast<compilation_ctx::partial_match_ctx *>(n.ptr);
std::size_t true_nodes{0}, term_nodes{0};
const auto saved{ctx->size};
for (size_t i{0}; i < ctx->size; ) {
ctx->nodes[i] = optimize_node(ctx->nodes[i], cctx, a, terms, phrases, stack, updates, root);
if (ctx->nodes[i].fp == ENT::constfalse || ctx->nodes[i].fp == ENT::dummyop)
ctx->nodes[i] = ctx->nodes[--(ctx->size)];
else {
if (const auto p = ctx->nodes[i].fp; p == ENT::matchterm)
++term_nodes;
else if (p == ENT::consttrue)
++true_nodes;
++i;
}
}
const size_t size = ctx->size;
if (ctx->min > size) {
n.fp = ENT::constfalse;
set_dirty();
return n;
} else {
if (size == 1) {
n = ctx->nodes[0];
set_dirty();
return n;
} else if (size == term_nodes && (size == ctx->min || ctx->min == 1)) {
// turn this into a matchallterms or matchanyterms(if only 1 is enough to accept match)
const auto required = sizeof(compilation_ctx::termsrun) + sizeof(exec_term_id_t) * size;
auto ptr = static_cast<compilation_ctx::termsrun *>(cctx.allocate(required));
ptr->size = size;
for (size_t i{0}; i < size; ++i)
ptr->terms[i] = ctx->nodes[i].u16;
n.fp = ctx->min == 1 ? ENT::matchanyterms : ENT::matchallterms;
n.ptr = ptr;
set_dirty();
return n;
} else if (size == true_nodes) {
n.fp = ENT::consttrue;
set_dirty();
return n;
} else if (ctx->min == size) {
// transform to binary op that includes all those
auto en = ctx->nodes[0];
for (size_t i{1}; i != size; ++i) {
auto b = static_cast<compilation_ctx::binop_ctx *>(a.Alloc(sizeof(compilation_ctx::binop_ctx)));
b->lhs = en;
b->rhs = ctx->nodes[i];
en.fp = ENT::logicaland;
en.ptr = b;
}
n = en;
set_dirty();
return n;
} else if (ctx->min == 1) {
// TODO(markp): consider expanding to OR sequence
} else if (size != saved) {
set_dirty();
}
}
} else if (n.fp == ENT::unaryand) {
auto ctx = static_cast<compilation_ctx::unaryop_ctx *>(n.ptr);
ctx->expr = optimize_node(ctx->expr, cctx, a, terms, phrases, stack, updates, root);
if (ctx->expr.fp == ENT::constfalse) {
n.fp = ENT::constfalse;
set_dirty();
} else if (ctx->expr.fp == ENT::dummyop) {
n.fp = ENT::dummyop;
set_dirty();
}
} else if (n.fp == ENT::unarynot) {
auto ctx = static_cast<compilation_ctx::unaryop_ctx *>(n.ptr);
ctx->expr = optimize_node(ctx->expr, cctx, a, terms, phrases, stack, updates, root);
if (ctx->expr.fp == ENT::dummyop) {
n.fp = ENT::dummyop;
set_dirty();
}
} else if (n.fp == ENT::logicaland || n.fp == ENT::logicalor || n.fp == ENT::logicalnot) {
auto ctx = static_cast<compilation_ctx::binop_ctx *>(n.ptr);
compilation_ctx::binop_ctx *otherCtx;
ctx->lhs = optimize_node(ctx->lhs, cctx, a, terms, phrases, stack, updates, root);
ctx->rhs = optimize_node(ctx->rhs, cctx, a, terms, phrases, stack, updates, root);
if (ctx->lhs.fp == ENT::dummyop && ctx->rhs.fp == ENT::dummyop) {
n.fp = ENT::dummyop;
set_dirty();
return n;
} else if (ctx->rhs.fp == ENT::dummyop) {
n = ctx->lhs;
set_dirty();
return n;
} else if (ctx->lhs.fp == ENT::dummyop) {
n = ctx->rhs;
set_dirty();
return n;
}
if (n.fp == ENT::logicalor) {
if (ctx->lhs.fp == ENT::constfalse) {
if (ctx->rhs.fp == ENT::constfalse) {
n.fp = ENT::constfalse;
set_dirty();
} else {
n = ctx->rhs;
set_dirty();
}
return n;
} else if (ctx->rhs.fp == ENT::constfalse) {
n = ctx->lhs;
set_dirty();
return n;
} else if (same(ctx->lhs, ctx->rhs)) {
auto s = stronger(ctx->lhs, ctx->rhs);
if (s == &ctx->lhs)
n = ctx->rhs;
else
n = ctx->lhs;
set_dirty();
return n;
} else if (ctx->lhs.fp == ENT::constfalse && ctx->rhs.fp == ENT::constfalse) {
n.fp = ENT::constfalse;
set_dirty();
return n;
} else if (ctx->lhs.fp == ENT::constfalse) {
n = ctx->rhs;
set_dirty();
return n;
} else if (ctx->rhs.fp == ENT::constfalse) {
n = ctx->lhs;
set_dirty();
return n;
} else if (ctx->lhs.fp == ENT::matchphrase && ctx->rhs.fp == ENT::matchallterms) {
// ([1,2] OR ALL OF[3,1,2]) => ALL OF [3,1,2]
const auto *const __restrict__ run = static_cast<compilation_ctx::termsrun *>(ctx->rhs.ptr);
const auto *const phrase = static_cast<compilation_ctx::phrase *>(ctx->lhs.ptr);
if (phrase->intersected_by(run)) {
n = ctx->rhs;
set_dirty();
return n;
}
} else if (ctx->lhs.fp == ENT::matchallterms && ctx->rhs.fp == ENT::matchphrase) {
// ([1,2] OR ALL OF[1,3,2]) => ALL OF [1,3,2]
const auto *const __restrict__ run = static_cast<compilation_ctx::termsrun *>(ctx->lhs.ptr);
const auto *const phrase = static_cast<compilation_ctx::phrase *>(ctx->rhs.ptr);
if (phrase->intersected_by(run)) {
n = ctx->lhs;
set_dirty();
return n;
}
}
if (ctx->lhs.fp == ENT::consttrueexpr && ctx->rhs.fp == ENT::consttrueexpr) {
// We are now handling [<A> OR <B>] as <ANYNODEOF[A, B]>
exec_node new_node, group;
auto ng = static_cast<compilation_ctx::nodes_group *>(a.Alloc(sizeof(compilation_ctx::nodes_group) + 2 * sizeof(exec_node)));
auto un = a.Alloc<compilation_ctx::unaryop_ctx>();
ng->size = 2;
ng->nodes[0] = static_cast<compilation_ctx::unaryop_ctx *>(ctx->lhs.ptr)->expr;
ng->nodes[1] = static_cast<compilation_ctx::unaryop_ctx *>(ctx->rhs.ptr)->expr;
group.fp = ENT::matchanynodes;
group.ptr = ng;
un->expr = group;
new_node.ptr = un;
new_node.fp = ENT::consttrueexpr;
if constexpr (traceCompile)
SLog("<CTRUEXPR> OR <CTRUEXPR> => ", new_node, "\n");
n = new_node;
set_dirty();
return n;
}
if (ctx->lhs.fp == ENT::consttrueexpr) {
// XXX: not sure [<FOR> OR <THE>] or [<FOR> OR THE] should become
// [FOR OR THE]
// UPDATE: this is handled above, however what about
//
// UPDATE:
// Assuming THE is a stopword and you wrap it in a ConstTrueExpr:
// For query: [THES FOOD] rewritten to [(THES OR <THE> FOOD]
// we want to match THES or whatever else it is expanded to (i.e <THE>)
// For query: [THE FOOD] rewritten to [ (<THE> OR THES) FOOD]
// we don't care if THE or whatever it expands to is matched
//
// so this is the responsibility of the application/rewrite logic.
auto c = static_cast<const compilation_ctx::unaryop_ctx *>(ctx->lhs.ptr);
if constexpr (traceCompile)
SLog("Logical OR, lhs is ENT::consttrueexpr ", c->expr, "\n");
ctx->lhs = c->expr;
set_dirty();
return n;
}
if (ctx->rhs.fp == ENT::consttrueexpr) {
// UPDATE: see above
auto c = static_cast<const compilation_ctx::unaryop_ctx *>(ctx->rhs.ptr);
if constexpr (traceCompile)
SLog("Logical OR, rhs is ENT::consttrueexpr ", c->expr, "\n");
ctx->rhs = c->expr;
set_dirty();
return n;
}
} else if (n.fp == ENT::logicaland) {
if (ctx->lhs.fp == ENT::constfalse || ctx->rhs.fp == ENT::constfalse) {
n.fp = ENT::constfalse;
set_dirty();
return n;
} else if (same(ctx->lhs, ctx->rhs)) {
n = *stronger(ctx->lhs, ctx->rhs);
set_dirty();
return n;
} else if (ctx->lhs.fp == ENT::logicalnot && same(static_cast<compilation_ctx::binop_ctx *>(ctx->lhs.ptr)->rhs, ctx->rhs)) {
// ((1 NOT 2) AND 2)
n.fp = ENT::constfalse;
set_dirty();