-
Notifications
You must be signed in to change notification settings - Fork 43
/
array.c
1211 lines (1082 loc) · 43.2 KB
/
array.c
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 "gwion_util.h"
#include "gwion_ast.h"
#include "gwion_env.h"
#include "vm.h"
#include "gwion.h"
#include "instr.h"
#include "object.h"
#include "array.h"
#include "emit.h"
#include "operator.h"
#include "import.h"
#include "traverse.h"
#include "parse.h"
#include "gwi.h"
#include "emit.h"
#include "looper.h"
static DTOR(array_dtor) {
if (*(void **)(o->data + SZ_INT)) xfree(*(void **)(o->data + SZ_INT));
struct M_Vector_ *a = ARRAY(o);
m_vector_release(a);
}
static DTOR(array_dtor_obj) {
struct M_Vector_ *a = ARRAY(o);
for (m_uint i = 0; i < ARRAY_LEN(a); ++i)
release(*(M_Object *)(ARRAY_PTR(a) + i * SZ_INT), shred);
}
static DTOR(array_dtor_struct) {
struct M_Vector_ *a = ARRAY(o);
for (m_uint i = 0; i < ARRAY_LEN(a); ++i)
struct_release(shred, array_base(o->type_ref),
&*(m_bit *)(ARRAY_PTR(a) + i * SZ_INT));
}
static DTOR(array_dtor_union) {
struct M_Vector_ *a = ARRAY(o);
for (m_uint i = 0; i < ARRAY_LEN(a); ++i)
union_release(shred, array_base(o->type_ref),
&*(m_bit *)(ARRAY_PTR(a) + i * SZ_INT));
}
ANN M_Object new_array(MemPool p, const Type t, const m_uint length) {
const M_Object a = new_object(p, t);
const m_uint depth =
!tflag(t, tflag_typedef) ? t->array_depth : t->info->parent->array_depth;
// const m_uint size = depth > 1 ? SZ_INT : array_base(t)->size;
const m_uint size = depth > 1 ? SZ_INT : array_base(t)->actual_size ?: array_base(t)->size;
//ARRAY(a) = new_m_vector(p, size, length);
m_vector_init(ARRAY(a), size, length);
return a;
}
static MFUN(vm_vector_rem) {
const m_int index = *(m_int *)(shred->mem + SZ_INT);
const M_Vector v = ARRAY(o);
if (index < 0 || (m_uint)index >= ARRAY_LEN(v)) return;
m_vector_rem(v, (vtype)index);
}
static MFUN(vm_vector_rem_obj) {
const m_int index = *(m_int *)(shred->mem + SZ_INT);
const M_Vector v = ARRAY(o);
if (index < 0 || (m_uint)index >= ARRAY_LEN(v)) return;
release(*(M_Object *)(ARRAY_PTR(v) + index * ARRAY_SIZE(v)), shred);
m_vector_rem(v, (vtype)index);
}
static MFUN(vm_vector_rem_struct) {
const m_int index = *(m_int *)(shred->mem + SZ_INT);
const M_Vector v = ARRAY(o);
if (index < 0 || (m_uint)index >= ARRAY_LEN(v)) return;
const Type t = o->type_ref;
struct_release(shred, array_base(t), ARRAY_PTR(v) + index * ARRAY_SIZE(v));
m_vector_rem(v, (vtype)index);
}
static MFUN(vm_vector_rem_union) {
const m_int index = *(m_int *)(shred->mem + SZ_INT);
const M_Vector v = ARRAY(o);
if (index < 0 || (m_uint)index >= ARRAY_LEN(v)) return;
const Type t = o->type_ref;
union_release(shred, array_base(t), ARRAY_PTR(v) + index * ARRAY_SIZE(v));
m_vector_rem(v, (vtype)index);
}
static MFUN(vm_vector_insert) {
const m_int index = *(m_int *)(shred->mem + SZ_INT);
const M_Vector v = ARRAY(o);
if (index < 0 || (m_uint)index > ARRAY_LEN(v)) return;
m_vector_insert(v, index, shred->mem + SZ_INT * 2);
}
static MFUN(vm_vector_insert_obj) {
const m_int index = *(m_int *)(shred->mem + SZ_INT);
const M_Vector v = ARRAY(o);
if (index < 0 || (m_uint)index > ARRAY_LEN(v)) return;
m_vector_insert(v, index, shred->mem + SZ_INT * 2);
++(*(M_Object *)(shred->mem + SZ_INT * 2))->ref;
}
static MFUN(vm_vector_insert_struct) {
const m_int index = *(m_int *)(shred->mem + SZ_INT);
const M_Vector v = ARRAY(o);
if (index < 0 || (m_uint)index > ARRAY_LEN(v)) return;
m_vector_insert(v, index, shred->mem + SZ_INT * 2);
struct_addref(array_base(o->type_ref), shred->mem + SZ_INT * 2);
}
static MFUN(vm_vector_insert_union) {
const m_int index = *(m_int *)(shred->mem + SZ_INT);
const M_Vector v = ARRAY(o);
if (index < 0 || (m_uint)index > ARRAY_LEN(v)) return;
m_vector_insert(v, index, shred->mem + SZ_INT * 2);
union_addref(array_base(o->type_ref), shred->mem + SZ_INT * 2);
}
static MFUN(vm_vector_size) { *(m_uint *)RETURN = ARRAY_LEN(ARRAY(o)); }
static MFUN(vm_vector_depth) { *(m_uint *)RETURN = o->type_ref->array_depth; }
static MFUN(vm_vector_cap) { *(m_uint *)RETURN = ARRAY_CAP(ARRAY(o)); }
static MFUN(vm_vector_random) {
const M_Vector array = ARRAY(o);
const m_uint sz = ARRAY_LEN(array);
const m_uint idx =
(m_int)(sz) * (gw_rand(shred->info->vm->rand) / (UINT32_MAX + 1.0));
m_vector_get(array, idx, (void *)RETURN);
}
#define ARRAY_OPCK(a, b, loc) \
const Type l = array_base(a->type); \
const Type r = array_base(b->type); \
if (!isa(r, l)) ERR_N(loc, _("array types do not match."));
static OP_CHECK(opck_array_at) {
const Exp_Binary *bin = (Exp_Binary *)data;
CHECK_NN(opck_const_rhs(env, data));
if (bin->lhs->type != env->gwion->type[et_error]) {
ARRAY_OPCK(bin->lhs, bin->rhs, exp_self(bin)->loc)
if (bin->lhs->type->array_depth != bin->rhs->type->array_depth)
ERR_N(exp_self(bin)->loc, _("array depths do not match."));
}
if (bin->rhs->exp_type == ae_exp_decl) {
Type_Decl *td = bin->rhs->d.exp_decl.var.td;
if (td->array && td->array->exp)
ERR_N(exp_self(bin)->loc,
_("do not provide array for 'xxx => declaration'."));
SET_FLAG(bin->rhs->d.exp_decl.var.vd.value, late);
}
bin->rhs->ref = bin->lhs;
// bin->rhs->data = bin->lhs;
exp_setvar(bin->rhs, 1);
return bin->rhs->type;
}
ANN static inline bool shift_match(const Type base, const Type more) {
return get_depth(base) == get_depth(more);
}
ANN static Type check_array_shift(const Env env, Exp* a, Exp* b,
const m_str str, const loc_t loc) {
/* if(a->type == env->gwion->type[et_error] &&
b->type->array_depth > 1)
return a->type;*/
ARRAY_OPCK(a, b, loc)
const m_int diff = get_depth(a->type) - get_depth(b->type);
if (diff >= 0 && diff <= 1)
return a->type;
ERR_N(loc, "array depths do not match for '%s'.", str);
}
static OP_CHECK(opck_array_sl) {
const Exp_Binary *bin = (Exp_Binary *)data;
return check_array_shift(env, bin->lhs, bin->rhs, "<<", exp_self(bin)->loc);
}
static OP_CHECK(opck_array_sr) {
const Exp_Binary *bin = (Exp_Binary *)data;
return check_array_shift(env, bin->rhs, bin->lhs, ">>", exp_self(bin)->loc);
}
ANN static inline bool emit_array_shift(const Emitter emit,
const f_instr exec) {
emit_regmove(emit, -SZ_INT);
(void)emit_add_instr(emit, exec);
return true;
}
static INSTR(ArrayAppendFront) {
const M_Object o = *(M_Object *)(shred->reg);
const M_Vector a = ARRAY(o);
m_vector_add_front(a, shred->reg - ARRAY_SIZE(a));
}
static INSTR(ArrayConcatLeft) {
const M_Object obase = *(M_Object *)(shred->reg - SZ_INT);
const M_Object omore = *(M_Object *)(shred->reg);
const M_Vector base = ARRAY(obase);
const M_Vector more = ARRAY(omore);
const m_uint len = ARRAY_LEN(base);
const m_uint sz = ARRAY_SIZE(base);
if ((ARRAY_LEN(base) += ARRAY_LEN(more)) >= ARRAY_CAP(base)) {
ARRAY_CAP(base) += ARRAY_CAP(more);
m_bit *ptr =
(m_bit *)xrealloc(base->ptr, ARRAY_OFFSET + ARRAY_CAP(base) * sz);
base->ptr = ptr;
}
m_bit *data = more->ptr + ARRAY_OFFSET;
memmove(ARRAY_PTR(base) + len * sz, data, sz);
}
static INSTR(ArrayConcatRight) {
const M_Object obase = *(M_Object *)(shred->reg);
const M_Object omore = *(M_Object *)(shred->reg - SZ_INT);
const M_Vector base = ARRAY(obase);
const M_Vector more = ARRAY(omore);
const m_uint len = ARRAY_LEN(base);
const m_uint sz = ARRAY_SIZE(base);
if ((ARRAY_LEN(base) += ARRAY_LEN(more)) >= ARRAY_CAP(base)) {
ARRAY_CAP(base) += ARRAY_CAP(more);
m_bit *ptr =
(m_bit *)xrealloc(base->ptr, ARRAY_OFFSET + ARRAY_CAP(base) * sz);
base->ptr = ptr;
}
memmove(ARRAY_PTR(base) + (ARRAY_LEN(more) + len - 1) * sz, ARRAY_PTR(base),
len * sz);
memmove(ARRAY_PTR(base), ARRAY_PTR(more), ARRAY_LEN(more) * sz);
}
static OP_EMIT(opem_array_sr) {
const Exp_Binary *bin = (Exp_Binary *)data;
if (shift_match(bin->lhs->type, bin->rhs->type))
return emit_array_shift(emit, ArrayConcatRight);
emit_regmove(emit, -SZ_INT);
if (tflag(bin->lhs->type, tflag_compound))
emit_compound_addref(emit, bin->lhs->type, -SZ_INT - bin->lhs->type->size, false);
(void)emit_add_instr(emit, ArrayAppendFront);
return true;
}
static OP_EMIT(opem_array_sl) {
const Exp_Binary *bin = (Exp_Binary *)data;
if (shift_match(bin->rhs->type, bin->lhs->type))
return emit_array_shift(emit, ArrayConcatLeft);
if (tflag(bin->rhs->type, tflag_compound))
emit_compound_addref(emit, bin->rhs->type, -bin->rhs->type->size, false);
emit_regmove(emit, -bin->rhs->type->size);
emit_add_instr(emit, ArrayAppend);
return true;
}
// check me. use common ancestor maybe
static OP_CHECK(opck_array_cast) {
const Exp_Cast *cast = (Exp_Cast *)data;
const Type l = array_base(cast->exp->type);
const Type t = known_type(env, cast->td);
const Type r = array_base(t);
if (get_depth(cast->exp->type) != get_depth(t))
return NULL;
if(isa(l, r)) return l;
Type parent = t;
while(parent) {
if (tflag(parent, tflag_cdef) && parent->info->cdef->base.ext && parent->info->cdef->base.ext->array) {
ERR_N(cast->td->tag.loc, "can only cast to simple array types");
}
parent = parent->info->parent;
}
Exp e = { .type = l, .loc = cast->exp->loc };
CHECK_ON(check_implicit(env, &e, r));
return t;
}
ANN static void cast_start(const Emitter emit, const m_uint depth) {
for(m_uint i = 0; i < depth; i++) {
const m_uint offset = emit_local(emit, emit->gwion->type[et_int]); // idx
emit_local(emit, emit->gwion->type[et_int]); // store base
emit_regtomem(emit, offset, -SZ_INT);
emit_memsetimm(emit, offset + SZ_INT, 0);
emit_regmove(emit, -SZ_INT);
const Instr loop = emit_add_instr(emit, ArrayCastLoop);
loop->m_val2 = offset;
}
}
ANN static void cast_end(const Emitter emit, const Type base, const m_uint depth, const m_uint start) {
for(m_uint i = 0; i < depth; i++) {
const m_uint pc = start + (depth-i) * 4 - 1;
const Instr top = emit_add_instr(emit, Goto);
top->m_val = pc;
const Instr loop = (Instr)vector_at(&emit->code->instr, pc);
loop->m_val = emit_code_size(emit);
const Instr end = emit_add_instr(emit, ArrayInit);
const Type t = array_type(emit->env, base, i + 1, base->info->cdef->base.tag.loc);
end->m_val = (m_uint)array_type(emit->env, base, i + 1, base->info->cdef->base.tag.loc);
end->m_val2 = t->actual_size ?: t->size;
}
}
static OP_EMIT(opem_array_cast) {
const Exp_Cast *cast = (Exp_Cast *)data;
const Env env = emit->env;
const Type l = array_base(cast->exp->type);
const Type t = known_type(env, cast->td);
const Type r = array_base(t);
if(!isa(l, r)) {
const m_uint depth = get_depth(t);
const m_uint start = emit_code_size(emit);
cast_start(emit, depth);
if(r->actual_size) emit_regmove(emit, r->size - r->actual_size);
// we need a correct exp to pass
struct Op_Import opi = {.op = insert_symbol("$"),
.lhs = l,
.rhs = r,
.data = (uintptr_t)cast}; // no pos ?
(void)op_emit(emit, &opi);
cast_end(emit, r, depth, start);
const m_uint ret_offset = emit_local(emit, t);
emit_regtomem(emit, ret_offset, -SZ_INT);
}
return true;
}
static OP_CHECK(opck_array_slice) {
Exp* e = (Exp*)data;
exp_setmeta(exp_self(e), 1);
return e->d.exp_slice.base->type;
}
static inline bool bounds(const M_Vector v, const m_int i) {
CHECK_B(i);
return (m_uint)i < ARRAY_LEN(v);
}
static INSTR(ArraySlice) {
shred->reg -= SZ_INT * 2;
const M_Object array = *(M_Object *)REG(-SZ_INT);
const M_Vector in = ARRAY(array);
const m_int start = *(m_uint *)REG(0);
m_int end = *(m_uint *)REG(SZ_INT);
if (end < 0) end = ARRAY_LEN(in) + end;
const m_int op = start < end ? 1 : -1;
const m_uint sz = op > 0 ? end - start : start - end;
if (!bounds(in, start) || !bounds(in, end)) {
handle(shred, "OutOfBoundsArraySliceException");
return;
}
const M_Object out = new_array(shred->info->mp, array->type_ref, sz);
for (m_int i = start, j = 0; i != end; i += op, ++j) {
m_bit buf[ARRAY_SIZE(in)];
m_vector_get(in, i, &buf);
m_vector_set(ARRAY(out), j, buf);
}
*(M_Object *)REG(-SZ_INT) = out;
}
static OP_EMIT(opem_array_slice) {
emit_add_instr(emit, ArraySlice);
return true;
}
static FREEARG(freearg_array) {
ArrayInfo *info = (ArrayInfo *)instr->m_val;
vector_release(&info->type);
mp_free(((Gwion)gwion)->mp, ArrayInfo, info);
}
ANN Type check_array_access(const Env env, const Array_Sub array);
ANN static inline Type get_array_type(const Type type) {
const Type t = !tflag(type, tflag_ref) ? type : (Type)vector_front(&type->info->tuple->contains);
return t->array_depth ? t : typedef_base(t);
}
static OP_CHECK(opck_array) {
const Array_Sub array = (Array_Sub)data;
const Type t_int = env->gwion->type[et_int];
Exp* e = array->exp;
do CHECK_ON(check_implicit(env, e, t_int));
while ((e = e->next));
const Type t = get_array_type(array->type);
if (t->array_depth >= array->depth)
return array_type(env, array_base(t), t->array_depth - array->depth, array->exp->loc);
Exp* curr = take_exp(array->exp, t->array_depth);
struct Array_Sub_ next = {curr->next, array_base(t),
array->depth - t->array_depth};
return check_array_access(env, &next) ?: env->gwion->type[et_error];
}
ANN static inline bool array_do(const Emitter emit, const Array_Sub array,
const bool is_var) {
CHECK_B(emit_exp(emit, array->exp));
const m_uint depth = array->depth;
const m_uint offset = is_var ? SZ_INT : array->type->size;
emit_regmove(emit, -(depth+1) * SZ_INT + offset);
assert(depth);
const Type t = array->type;
Instr access = NULL;
for (m_uint i = 0; i < depth; ++i) {
access = emit_add_instr(emit, ArrayAccess);
access->m_val = (i+1) * SZ_INT - offset;
access->udata.one = offset;
if(i < get_depth(t) || is_object(emit->gwion, array_base(t))) {
const Instr ex = emit_add_instr(emit, GWOP_EXCEPT);
ex->m_val = -SZ_INT;
}
}
assert(access);
access->udata.two = is_var;
return true;
}
ANN bool get_emit_var(const Emitter emit, const Type t, bool is_var) {
const Env env = emit->env;
bool vars[2] = { is_var };
struct Op_Import opi = {.op = insert_symbol("@array_init"),
.lhs = t,
.data = (uintptr_t)vars};
if(!op_emit(emit, &opi))
return false;
return vars[1];
}
ANN static inline Exp* emit_n_exp(const Emitter emit,
struct ArrayAccessInfo *const info) {
Exp* e = take_exp(info->array.exp, info->array.depth);
Exp* next = e->next;
e->next = NULL;
struct Array_Sub_ partial = {info->array.exp, info->array.type,
info->array.depth};
const bool is_var = get_emit_var(emit, array_base(info->array.type), info->is_var);
const bool ret = array_do(emit, &partial, is_var);
e->next = next;
return ret ? next : NULL;
}
ANN static Type emit_get_array_type(const Emitter emit, const Type t) {
if(!tflag(t, tflag_ref)) return t;
const Instr instr = emit_add_instr(emit, Reg2RegDeref);
instr->m_val = -SZ_INT;
instr->m_val2 = -SZ_INT;
return (Type)vector_front(&t->info->tuple->contains);
}
static OP_EMIT(opem_array_access) {
struct ArrayAccessInfo *const info = (struct ArrayAccessInfo *)data;
const Type t = emit_get_array_type(emit, info->array.type);
if (t->array_depth >= info->array.depth) {
struct Array_Sub_ next = {
.exp = info->array.exp, .type = info->type, .depth = info->array.depth};
return array_do(emit, &next, info->is_var);
}
struct Array_Sub_ partial = {info->array.exp, t,
t->array_depth};
struct Array_Sub_ next = {info->array.exp, array_base(t),
info->array.depth - t->array_depth};
info->array = partial;
Exp* exp = emit_n_exp(emit, info);
next.exp = exp;
info->array = next;
if(exp)
return emit_array_access(emit, info);
return false;
}
static m_bit map_byte[BYTECODE_SZ * 5];
static const struct VM_Code_ map_run_code = {.name = "map_run_code",
.bytecode = map_byte};
static m_bit compactmap_byte[BYTECODE_SZ * 5];
static const struct VM_Code_ compactmap_run_code = {
.name = "compactmap_run_code", .bytecode = compactmap_byte};
static m_bit filter_byte[BYTECODE_SZ * 5];
static const struct VM_Code_ filter_run_code = {.name = "filter_run_code",
.bytecode = filter_byte};
static m_bit count_byte[BYTECODE_SZ * 5];
static const struct VM_Code_ count_run_code = {.name = "count_run_code",
.bytecode = count_byte};
static m_bit foldl_byte[BYTECODE_SZ * 5];
static const struct VM_Code_ foldl_run_code = {.name = "foldl_run_code",
.bytecode = foldl_byte};
static m_bit foldr_byte[BYTECODE_SZ * 5];
static const struct VM_Code_ foldr_run_code = {.name = "foldr_run_code",
.bytecode = foldr_byte};
static m_bit new_byte[BYTECODE_SZ * 5];
static const struct VM_Code_ new_run_code = {.name = "new_run_code",
.bytecode = new_byte};
typedef struct FunctionalFrame {
VM_Code code;
M_Object o;
uint16_t pc;
uint16_t offset;
uint16_t index;
uint16_t ret_size;
} FunctionalFrame;
ANN static inline void _init(const VM_Shred shred, const struct VM_Code_ *code, const M_Object o,
const m_uint offset, const m_uint start) {
FunctionalFrame *frame = &*(FunctionalFrame *)MEM(SZ_INT * 2 + start);
frame->pc = shred->pc;
frame->code = shred->code;
frame->offset = offset;
frame->index = 0;
*(m_uint *)REG(SZ_INT) = offset;
shred->code = (VM_Code)code;
shred->pc = 0;
shredule(shred->tick->shreduler, shred, 0);
if(!(*(VM_Code *)REG(0) = *(VM_Code*)o->data))
handle(shred, "MissingCodeException");
}
ANN static inline void _next(const VM_Shred shred, const m_uint offset) {
shred->pc = 0;
*(m_uint *)REG(0) = offset;
POP_REG(shred, SZ_INT);
}
ANN static inline void _return(const VM_Shred shred,
const FunctionalFrame *frame) {
shred->pc = frame->pc;
shred->code = frame->code;
}
ANN static inline void _finish(const VM_Shred shred,
const FunctionalFrame *frame) {
POP_MEM(shred, frame->offset);
shredule(shred->tick->shreduler, shred, 0);
}
#define MAP_CODE_OFFSET (sizeof(FunctionalFrame) + sizeof(struct frame_t))
static INSTR(map_run_ini) {
const VM_Code code = *(VM_Code*)REG(0);
const m_uint offset = *(m_uint *)REG(SZ_INT);
if (offset) PUSH_MEM(shred, offset);
PUSH_REG(shred, SZ_INT);
const M_Object self = *(M_Object *)MEM(0);
const M_Vector array = ARRAY(self);
FunctionalFrame *frame = &*(FunctionalFrame *)MEM(SZ_INT * 3);
frame->ret_size = code->ret_type->size;
shred->pc++;
shred->mem += MAP_CODE_OFFSET + SZ_INT; // work in a safe memory space
m_vector_get(array, frame->index, &*(m_bit **)(shred->mem + SZ_INT * 2 + frame->offset + frame->code->stack_depth));
}
static INSTR(map_run_end) {
shred->mem -= MAP_CODE_OFFSET + SZ_INT;
const M_Object ret_obj = *(M_Object *)MEM(SZ_INT * 2);
const M_Vector array = ARRAY(*(M_Object *)MEM(0));
FunctionalFrame *const frame = &*(FunctionalFrame *)MEM(SZ_INT * 3);
POP_REG(shred, frame->ret_size);
m_vector_set(ARRAY(ret_obj), frame->index, shred->reg);
if (++frame->index == ARRAY_LEN(array)) {
_return(shred, frame);
*(M_Object *)(REG(-SZ_INT)) = ret_obj;
} else
_next(shred, frame->offset);
_finish(shred, frame);
}
static INSTR(compactmap_run_end) {
shred->mem -= MAP_CODE_OFFSET + SZ_INT;
const M_Object self = *(M_Object *)MEM(0);
const M_Vector self_array = ARRAY(self);
const M_Object ret_obj = *(M_Object *)MEM(SZ_INT * 2);
const M_Vector ret_array = ARRAY(ret_obj);
FunctionalFrame *const frame = &*(FunctionalFrame *)MEM(SZ_INT * 3);
POP_REG(shred, frame->ret_size);
const m_uint size = m_vector_size(self_array);
const M_Object obj = *(M_Object *)REG(0);
if (*(m_uint *)obj->data)
m_vector_add(ret_array, &*(m_bit *)(obj->data + SZ_INT));
if (++frame->index == size) {
_return(shred, frame);
*(M_Object *)(REG(-SZ_INT)) = ret_obj;
} else
_next(shred, frame->offset);
_finish(shred, frame);
}
static INSTR(filter_run_end) {
shred->mem -= MAP_CODE_OFFSET + SZ_INT;
POP_REG(shred, SZ_INT);
const M_Object self = *(M_Object *)MEM(0);
const M_Object ret_obj = *(M_Object *)MEM(SZ_INT * 2);
const M_Vector array = ARRAY(ret_obj);
FunctionalFrame *const frame = &*(FunctionalFrame *)MEM(SZ_INT * 3);
if (*(m_uint *)(shred->reg))
m_vector_add(array,
ARRAY_PTR(ARRAY(self)) + frame->index * ARRAY_SIZE(array));
if (++frame->index == ARRAY_LEN(ARRAY(self))) {
_return(shred, frame);
*(M_Object *)(REG(-SZ_INT)) = ret_obj;
} else
_next(shred, frame->offset);
_finish(shred, frame);
}
static INSTR(count_run_end) {
shred->mem -= MAP_CODE_OFFSET + SZ_INT;
const M_Object self = *(M_Object *)MEM(0);
POP_REG(shred, SZ_INT);
FunctionalFrame *const frame = &*(FunctionalFrame *)MEM(SZ_INT * 3);
if (*(m_uint *)(shred->reg)) (*(m_uint *)MEM(SZ_INT * 2))++;
if (++frame->index == ARRAY_LEN(ARRAY(self))) {
_return(shred, frame);
*(m_uint *)(REG(-SZ_INT)) = *(m_uint *)MEM(SZ_INT * 2);
} else
_next(shred, frame->offset);
_finish(shred, frame);
}
static MFUN(vm_vector_map) {
const m_uint offset = *(m_uint *)REG(SZ_INT * 3);
const M_Object ret =
new_array(shred->info->mp, o->type_ref, ARRAY_LEN(ARRAY(o)));
if (ARRAY_LEN(ARRAY(o))) {
*(M_Object *)MEM(SZ_INT * 2) = ret;
_init(shred, &map_run_code, *(M_Object*)MEM(SZ_INT*1), offset, SZ_INT);
} else
*(M_Object *)RETURN = ret;
}
static MFUN(vm_vector_compactmap) {
const VM_Code code = *(VM_Code *)REG(SZ_INT * 2);
const m_uint offset = *(m_uint *)REG(SZ_INT * 3);
const M_Object ret = new_array(shred->info->mp, code->ret_type, 0);
if (ARRAY_LEN(ARRAY(o))) {
_init(shred, &compactmap_run_code, *(M_Object*)MEM(SZ_INT*1), offset, SZ_INT);
*(M_Object *)MEM(SZ_INT * 2) = ret;
} else
*(M_Object *)RETURN = ret;
}
static MFUN(vm_vector_filter) {
const m_uint offset = *(m_uint *)REG(SZ_INT * 3);
const M_Object ret = new_array(shred->info->mp, o->type_ref, 0);
if (ARRAY_LEN(ARRAY(o))) {
_init(shred, &filter_run_code, *(M_Object*)MEM(SZ_INT*1), offset, SZ_INT);
*(M_Object *)MEM(SZ_INT * 2) = ret;
} else
*(M_Object *)RETURN = ret;
}
static MFUN(vm_vector_count) {
const m_uint offset = *(m_uint *)REG(SZ_INT * 3);
if (ARRAY_LEN(ARRAY(o))) {
_init(shred, &count_run_code, *(M_Object*)MEM(SZ_INT*1), offset, SZ_INT);
*(m_uint *)MEM(SZ_INT * 2) = 0;
} else
*(m_uint *)RETURN = 0;
}
static INSTR(foldl_run_ini) {
const VM_Code code = *(VM_Code*)REG(0);
const m_uint offset = *(m_uint *)REG(SZ_INT);
if (offset) PUSH_MEM(shred, offset);
const M_Object self = *(M_Object *)MEM(0);
*(m_uint *)(shred->reg + SZ_INT) = 0;
PUSH_REG(shred, SZ_INT);
shred->pc++;
FunctionalFrame *const frame = &*(FunctionalFrame *)MEM(SZ_INT * 3);
frame->ret_size = code->ret_type->size;
shred->mem += MAP_CODE_OFFSET + SZ_INT; // work in a safe memory space
m_vector_get(ARRAY(self), frame->index,
&*(m_bit **)(shred->mem + SZ_INT * 2 + frame->code->stack_depth));
}
static INSTR(foldr_run_ini) {
const VM_Code code = *(VM_Code*)REG(0);
const m_uint offset = *(m_uint *)REG(SZ_INT);
if (offset) PUSH_MEM(shred, offset);
const M_Object self = *(M_Object *)MEM(0);
*(m_uint *)(shred->reg + SZ_INT) = 0;
PUSH_REG(shred, SZ_INT);
shred->pc++;
FunctionalFrame *const frame = &*(FunctionalFrame *)MEM(SZ_INT * 3);
frame->ret_size = code->ret_type->size;
shred->mem += MAP_CODE_OFFSET + SZ_INT; // work in a safe memory space
const M_Vector array = ARRAY(self);
m_vector_get(array, ARRAY_LEN(array) - frame->index - 1,
&*(m_bit **)(shred->mem + SZ_INT * 2 + frame->code->stack_depth));
}
static INSTR(fold_run_end) {
shred->mem -= MAP_CODE_OFFSET + SZ_INT;
FunctionalFrame *const frame = &*(FunctionalFrame *)MEM(SZ_INT * 3);
const M_Object self = *(M_Object *)MEM(0);
const VM_Code code = *(VM_Code *)(*(M_Object*)MEM(SZ_INT))->data;
const m_uint sz = code->stack_depth - ARRAY_SIZE(ARRAY(self));
const m_uint base_sz = code->stack_depth - sz;
POP_REG(shred, base_sz); // ret_sz?
if (++frame->index == ARRAY_LEN(ARRAY(self))) {
POP_REG(shred, SZ_INT - base_sz);
shred->pc = frame->pc;
shred->code = frame->code;
memcpy(REG(-sz), REG(0), base_sz);
} else {
memcpy(shred->mem + MAP_CODE_OFFSET + SZ_INT * 3 + sz, shred->reg, base_sz);
_next(shred, frame->offset);
}
_finish(shred, frame);
}
static INSTR(new_run_ini) {
const m_uint offset = *(m_uint *)REG(SZ_INT);
if (offset) PUSH_MEM(shred, offset);
const M_Object arg = *(M_Object *)MEM(SZ_INT);
const VM_Code code = *(VM_Code*)arg->data;
*(VM_Code*)REG(0) = code;
PUSH_REG(shred, SZ_INT);
FunctionalFrame *frame = &*(FunctionalFrame *)MEM(SZ_INT * 3);
shred->pc++;
shred->mem += MAP_CODE_OFFSET + SZ_INT; // work in a safe memory space
*(m_uint*)MEM(SZ_INT*2+offset) = frame->index;
}
static INSTR(new_run_end) {
shred->mem -= MAP_CODE_OFFSET + SZ_INT;
FunctionalFrame *const frame = &*(FunctionalFrame *)MEM(SZ_INT * 3);
const M_Object self = *(M_Object *)MEM(0);
const M_Vector array = ARRAY(self);
const m_uint base_sz = ARRAY_SIZE(array);
m_vector_set(array, frame->index, REG(-base_sz));
POP_REG(shred, base_sz);
if (++frame->index == ARRAY_LEN(ARRAY(self))) {
shred->pc = frame->pc;
shred->code = frame->code;
*(M_Object*)REG(-SZ_INT) = self;
} else _next(shred, frame->offset);
_finish(shred, frame);
}
static MFUN(vm_vector_foldl) {
const m_bit *byte = shred->code->bytecode + (shred->pc - 1) * BYTECODE_SZ;
const m_uint acc_sz = *(m_uint *)(byte + SZ_INT);
const m_uint offset = *(m_uint *)REG(SZ_INT * 3 + acc_sz);
if (ARRAY_LEN(ARRAY(o))) {
_init(shred, &foldl_run_code, *(M_Object*)MEM(SZ_INT*1), offset, SZ_INT);
memcpy(shred->mem + MAP_CODE_OFFSET + SZ_INT * 3 + acc_sz, MEM(SZ_INT * 2),
acc_sz);
} else
memcpy((m_bit *)RETURN, MEM(SZ_INT * 2), acc_sz);
}
static MFUN(vm_vector_foldr) {
const m_bit *byte = shred->code->bytecode + (shred->pc - 1) * BYTECODE_SZ;
const m_uint acc_sz = *(m_uint *)(byte + SZ_INT);
const m_uint offset = *(m_uint *)REG(SZ_INT * 3 + acc_sz);
if (ARRAY_LEN(ARRAY(o))) {
_init(shred, &foldr_run_code, *(M_Object*)MEM(SZ_INT*1), offset, SZ_INT);
memcpy(shred->mem + MAP_CODE_OFFSET + SZ_INT * 3 + acc_sz, MEM(SZ_INT * 2),
acc_sz);
} else
memcpy((m_bit *)RETURN, MEM(SZ_INT * 2), acc_sz);
}
static MFUN(vm_vector_new) {
if (ARRAY_LEN(ARRAY(o))) {
const m_uint offset = *(m_uint *)REG(SZ_INT * 3);
_init(shred, &new_run_code, *(M_Object*)MEM(SZ_INT*1), offset, SZ_INT);
}
*(M_Object *)RETURN = o;
}
static void array_func(const Env env, const Type t, const m_str name, f_xfun fun) {
const Value v = nspc_lookup_value0(t->nspc, insert_symbol(name));
builtin_func(env->gwion, v->d.func_ref, fun);
}
ANN static f_xfun get_rem(const Type t) {
if(!tflag(t, tflag_release)) return vm_vector_rem;
if(!tflag(t, tflag_struct)) return vm_vector_rem_obj;
if(!tflag(t, tflag_union)) return vm_vector_rem_struct;
return vm_vector_rem_union;
}
ANN static f_xfun get_insert(const Type t) {
if(!tflag(t, tflag_release)) return vm_vector_insert;
if(!tflag(t, tflag_struct)) return vm_vector_insert_obj;
if(!tflag(t, tflag_union)) return vm_vector_insert_struct;
return vm_vector_insert_union;
}
ANN static f_xfun get_dtor(const Type t) {
if(!tflag(t, tflag_struct)) return array_dtor_obj;
if(!tflag(t, tflag_union)) return array_dtor_struct;
return array_dtor_union;
}
static OP_CHECK(opck_array_scan) {
struct TemplateScan *ts = (struct TemplateScan *)data;
const Type t_array = env->gwion->type[et_array];
const Class_Def c = t_array->info->cdef;
if (ts->t == t_array && !ts->td->types)
ERR_N(ts->td->tag.loc, "Array needs template arguments");
DECL_ON(const Type, base,
= ts->t != t_array ? ts->t : known_type(env, mp_vector_at(ts->td->types, TmplArg, 0)->d.td));
if (base->size == 0) {
gwlog_error("Can't use type of size 0 as array base", NULL,
env->name, ts->td->tag.loc, 0);
env_set_error(env, true);
return env->gwion->type[et_error];
}
if (tflag(base, tflag_ref)) {
gwlog_error("Can't use ref types as array base", NULL,
env->name, ts->td->tag.loc, 0);
env_set_error(env, true);
return env->gwion->type[et_error];
}
const Symbol sym = array_sym(env, array_base_simple(base), base->array_depth + 1);
const Type type = nspc_lookup_type1(base->info->value->from->owner, sym);
if (type) return type;
const Class_Def cdef = cpy_class_def(env->gwion->mp, c);
cdef->base.ext = type2td(env->gwion, t_array, t_array->info->value->from->loc);
cdef->base.tag.sym = sym;
cdef->base.tmpl->call = new_mp_vector(env->gwion->mp, TmplArg, 1);
TmplArg arg = {.type = tmplarg_td, .d = {.td = type2td(env->gwion, base, base->info->value->from->loc)} };
mp_vector_set(cdef->base.tmpl->call, TmplArg, 0, arg);
struct EnvSet es = {
.env = env,
.data = env,
.scope = env->scope->depth
};
envset_pushv(&es, base->info->value);
CHECK_ON(scan0_class_def(env, cdef));
const Type t = cdef->base.type;
if (GET_FLAG(base, abstract) && !tflag(base, tflag_union))
SET_FLAG(t, abstract);
else
UNSET_FLAG(t, abstract);
const bool ret = traverse_cdef(env, t);
UNSET_FLAG(t, abstract);
envset_popv(&es, base->info->value);
if (!ret) return NULL;
set_tflag(t, tflag_emit);
t->array_depth = base->array_depth + 1;
t->info->base_type = array_base(base);
set_tflag(t, tflag_cdef | tflag_tmpl);
builtin_func(env->gwion, (Func)vector_at(&t->nspc->vtable, 0), get_rem(t));
array_func(env, t, "insert", get_insert(t));
array_func(env, t, "size", vm_vector_size);
array_func(env, t, "depth", vm_vector_depth);
array_func(env, t, "cap", vm_vector_cap);
array_func(env, t, "random", vm_vector_random);
array_func(env, t, "map", vm_vector_map);
array_func(env, t, "compactMap", vm_vector_compactmap);
array_func(env, t, "filter", vm_vector_filter);
array_func(env, t, "count", vm_vector_count);
array_func(env, t, "foldl", vm_vector_foldl);
array_func(env, t, "foldr", vm_vector_foldr);
if (tflag(base, tflag_compound)) {
t->nspc->dtor = new_vmcode(env->gwion->mp, NULL, NULL,
"array component dtor", SZ_INT, true, false);
set_tflag(t, tflag_dtor);
t->nspc->dtor->native_func = (m_uint)get_dtor(base);
}
return t;
}
static OP_CHECK(opck_array_implicit) {
const struct Implicit *imp = (struct Implicit *)data;
if (imp->t->array_depth != imp->e->type->array_depth)
return env->gwion->type[et_error];
if (!isa(array_base(imp->e->type), array_base(imp->t)))
return env->gwion->type[et_error];
return imp->t;
}
static OP_EMIT(opem_array_each_init) {
Looper *loop = (Looper *)data;
const Instr instr = emit_add_instr(emit, AutoUnrollInit);
instr->m_val = loop->offset;
return true;
}
ANN static inline Type foreach_type(const Env env, Exp* exp) {
const Type et = exp->type;
DECL_O(Type, base, = typedef_base(et));
DECL_O(const Type, t, = array_base_simple(base));
if(!tflag(base, tflag_ref)) {
const m_uint depth = base->array_depth - 1;
return depth ? array_type(env, t, depth, exp->loc) : t;
}
const Type inner = (Type)vector_front(&base->info->tuple->contains);
const Type refbase = array_base_simple(inner);
const m_uint depth = inner->array_depth - 1;
return depth ? array_type(env, refbase, depth, exp->loc) : refbase;
}
// rewrite me
static OP_CHECK(opck_array_each_val) {
Exp* exp = (Exp*) data;
DECL_ON(const Type, base, = foreach_type(env, exp));
CHECK_ON(ensure_traverse(env, base));
return ref_type(env->gwion, base, exp->loc);
}
static OP_EMIT(opem_array_each) {
Looper *loop = (Looper *)data;
const Instr instr = emit_add_instr(emit, AutoLoop);
if(!loop->n) {
instr->m_val2 = loop->offset + SZ_INT;
loop->instr = instr;
} else {
instr->m_val2 = loop->offset + SZ_INT*2;
vector_add(&loop->unroll_v, (m_uint)instr);
}
return true;
}
ANN static void prepare_run(m_bit *const byte, const f_instr ini,
const f_instr end) {
*(unsigned *)(byte) = eOP_MAX;
*(f_instr *)(byte+ SZ_INT * 2) = ini;
*(unsigned *)(byte + BYTECODE_SZ) = eSetCode;
*(uint16_t *)(byte + BYTECODE_SZ + SZ_INT * 2) = 3;
*(unsigned *)(byte + BYTECODE_SZ * 2) = eOverflow;
*(unsigned *)(byte + BYTECODE_SZ * 3) = eOP_MAX;
*(f_instr *)(byte + BYTECODE_SZ * 3 + SZ_INT * 2) = end;
*(unsigned *)(byte + BYTECODE_SZ * 4) = eEOC;
}
ANN static void prepare_map_run(m_bit *const byte, const f_instr end) {
prepare_run(byte, map_run_ini, end);
vm_prepare(NULL, byte);
}
ANN static void prepare_fold_run(m_bit *const byte, const f_instr ini) {
prepare_run(byte, ini, fold_run_end);
vm_prepare(NULL, byte);
}
GWION_IMPORT(array) {
prepare_map_run(map_byte, map_run_end);
prepare_map_run(compactmap_byte, compactmap_run_end);
prepare_map_run(filter_byte, filter_run_end);
prepare_map_run(count_byte, count_run_end);
prepare_fold_run(foldl_byte, foldl_run_ini);
prepare_fold_run(foldr_byte, foldr_run_ini);
prepare_run(new_byte, new_run_ini, new_run_end);
vm_prepare(NULL, new_byte);
const Type t_array = gwi_class_ini(gwi, "Array:[T]", "Object");
set_tflag(t_array, tflag_infer);
gwi->gwion->type[et_array] = t_array;
gwi_class_xtor(gwi, NULL, array_dtor);
t_array->nspc->offset += SZ_INT*2;
GWI_B(gwi_fptr_ini(gwi, "A", "map_t:[A]"))
GWI_B(gwi_func_arg(gwi, "T", "elem"))
GWI_B(gwi_fptr_end(gwi, ae_flag_static))
GWI_B(gwi_fptr_ini(gwi, "Option:[A]", "compactmap_t:[A]"))
GWI_B(gwi_func_arg(gwi, "T", "elem"))
GWI_B(gwi_fptr_end(gwi, ae_flag_static))
GWI_B(gwi_fptr_ini(gwi, "A", "fold_t:[A]"))
GWI_B(gwi_func_arg(gwi, "T", "elem"))
GWI_B(gwi_func_arg(gwi, "A", "acc"))
GWI_B(gwi_fptr_end(gwi, ae_flag_static))
GWI_B(gwi_fptr_ini(gwi, "bool", "filter_t"))
GWI_B(gwi_func_arg(gwi, "T", "elem"))
GWI_B(gwi_fptr_end(gwi, ae_flag_static))
GWI_B(gwi_fptr_ini(gwi, "T", "new_t"))
GWI_B(gwi_func_arg(gwi, "int", "idx"))
GWI_B(gwi_fptr_end(gwi, ae_flag_static))
// put functions using T first
GWI_B(gwi_func_ini(gwi, "bool", "remove"))
GWI_B(gwi_func_arg(gwi, "int", "index"))
GWI_B(gwi_func_end(gwi, vm_vector_rem, ae_flag_none))
GWI_B(gwi_func_ini(gwi, "bool", "insert"))
GWI_B(gwi_func_arg(gwi, "int", "index"))
GWI_B(gwi_func_arg(gwi, "T", "data"))
GWI_B(gwi_func_end(gwi, vm_vector_insert, ae_flag_none))
GWI_B(gwi_func_ini(gwi, "int", "size"))
GWI_B(gwi_func_end(gwi, vm_vector_size, ae_flag_none))
GWI_B(gwi_func_ini(gwi, "int", "depth"))
GWI_B(gwi_func_end(gwi, vm_vector_depth, ae_flag_none))
GWI_B(gwi_func_ini(gwi, "int", "cap"))
GWI_B(gwi_func_end(gwi, vm_vector_cap, ae_flag_none))