-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathabce.h
1396 lines (1274 loc) · 32.9 KB
/
abce.h
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
#ifndef _ABCE_H_
#define _ABCE_H_
#include "abcedatatypes.h"
#ifdef __cplusplus
extern "C" {
#endif
#ifdef WITH_LUA
void mb_to_lua(lua_State *lua, const struct abce_mb *mb);
void mb_from_lua(lua_State *lua, struct abce *abce, int idx);
#endif
void *abce_std_alloc(void *old, size_t oldsz, size_t newsz, void **pbaton);
void *abce_jm_alloc(void *old, size_t oldsz, size_t newsz, void **pbaton);
static inline int is_little_endian(void)
{
double d;
memcpy(&d, "\x00\x00\x00\x00\x00\x00I@", 8);
if (d == 50.0)
{
return 1;
}
memcpy(&d, "@I\x00\x00\x00\x00\x00\x00", 8);
if (d == 50.0)
{
return 0;
}
abort(); // Not IEEE 754
}
static inline uint16_t abce_bswap16(uint16_t u16)
{
u16 =
((u16 & 0xFF00U) >> 8) |
((u16 & 0x00FFU) << 8);
return u16;
}
static inline uint32_t abce_bswap32(uint32_t u32)
{
u32 =
((u32 & 0xFF000000U) >> 24) |
((u32 & 0x00FF0000U) >> 8) |
((u32 & 0x0000FF00U) << 8) |
((u32 & 0x000000FFU) << 24);
return u32;
}
static inline void abce_put_dbl(double dbl, void *dest)
{
uint64_t u64;
memcpy(&u64, &dbl, sizeof(u64));
if (is_little_endian())
{
u64 =
((u64 & 0xFF00000000000000ULL) >> 56) |
((u64 & 0x00FF000000000000ULL) >> 40) |
((u64 & 0x0000FF0000000000ULL) >> 24) |
((u64 & 0x000000FF00000000ULL) >> 8) |
((u64 & 0x00000000FF000000ULL) << 8) |
((u64 & 0x0000000000FF0000ULL) << 24) |
((u64 & 0x000000000000FF00ULL) << 40) |
((u64 & 0x00000000000000FFULL) << 56);
}
memcpy(dest, &u64, sizeof(u64));
}
static inline int abce_set_double(struct abce *abce, size_t idx, double dbl)
{
if (idx + 8 > abce->bytecodesz)
{
return -EFAULT;
}
abce_put_dbl(dbl, &abce->bytecode[idx]);
return 0;
}
static inline int
abce_add_double_alt(void *bcode, size_t *bsz, size_t cap, double dbl)
{
unsigned char *bytecode = (unsigned char*)bcode;
if ((*bsz) + 8 > cap)
{
return -EFAULT;
}
abce_put_dbl(dbl, &bytecode[*bsz]);
(*bsz) += 8;
return 0;
}
static inline int abce_add_double(struct abce *abce, double dbl)
{
return abce_add_double_alt(
abce->bytecode, &abce->bytecodesz, abce->bytecodecap, dbl);
}
void abce_mb_gc_refdn(struct abce *abce, struct abce_mb_area *mba, enum abce_type typ);
void abce_mb_do_arearefdn(struct abce *abce, struct abce_mb_area **mbap, enum abce_type typ);
static inline void abce_mb_arearefdn(struct abce *abce, struct abce_mb_area **mbap, enum abce_type typ)
{
struct abce_mb_area *mba = *mbap;
if (mba == NULL)
{
return;
}
if (!--mba->refcnt)
{
abce_mb_do_arearefdn(abce, mbap, typ);
}
}
static inline int abce_is_dynamic_type(enum abce_type typ)
{
switch (typ)
{
case ABCE_T_T:
case ABCE_T_IOS:
case ABCE_T_A:
case ABCE_T_S:
case ABCE_T_PB:
case ABCE_T_SC:
return 1;
default:
return 0;
}
}
static inline void abce_mb_refdn_typ(struct abce *abce, struct abce_mb *mb, enum abce_type typ)
{
if (abce_is_dynamic_type(typ))
{
abce_mb_arearefdn(abce, &mb->u.area, mb->typ);
}
mb->typ = ABCE_T_N;
mb->u.d = 0.0;
mb->u.area = NULL;
}
static inline void abce_mb_refdn(struct abce *abce, struct abce_mb *mb)
{
if (abce_is_dynamic_type(mb->typ))
{
abce_mb_arearefdn(abce, &mb->u.area, mb->typ);
}
mb->typ = ABCE_T_N;
mb->u.d = 0.0;
mb->u.area = NULL;
}
void
abce_mb_refdn_noinline(struct abce *abce, struct abce_mb *mb);
struct abce_mb
abce_mb_refup_noinline(struct abce *abce, const struct abce_mb *mb);
static inline struct abce_mb
abce_mb_refup(struct abce *abce, const struct abce_mb *mb)
{
if (abce_is_dynamic_type(mb->typ))
{
mb->u.area->refcnt++;
}
return *mb;
}
static inline void
abce_mb_refreplace(struct abce *abce, struct abce_mb *mbold, const struct abce_mb *mbnew)
{
struct abce_mb mbtmp = abce_mb_refup(abce, mbnew);
/*
* Here we have to be careful. The only reason the pointer mbnew might
* be valid could be that something in mbold holds the only tracked
* reference to it. Thus, if we first refdn mbold and then refup mbnew,
* it could be mbnew has already become invalid. Now that we have
* up-referenced mbnew, after we down-reference mbold, it could be the
* only valid reference to mbnew is in mbtmp. This is not tracked by
* abce, so we can't allocate any memory after the refdn operation has
* been done, or else the garbage collector could run and become confused
* by an object nothing refers to but still has a positive reference
* count. Fortunately, we don't need to do any memory allocation after
* refdn, we can directly assign mbtmp to mbold, eliminating the dangerous
* situation.
*/
abce_mb_refdn(abce, mbold);
*mbold = mbtmp;
}
void
abce_mb_refreplace_noinline(struct abce *abce, struct abce_mb *mbold, const struct abce_mb *mbnew);
void
abce_mb_errreplace_noinline(struct abce *abce, const struct abce_mb *mbnew);
static inline int64_t abce_cache_add(struct abce *abce, const struct abce_mb *mb)
{
int64_t res;
if (abce->cachesz >= abce->cachecap)
{
return -EOVERFLOW;
}
res = abce->cachesz;
abce->cachebase[abce->cachesz++] = abce_mb_refup(abce, mb);
return res;
}
static inline int abce_cpop(struct abce *abce)
{
struct abce_mb *mb;
if (abce->csp == 0)
{
abce->err.code = ABCE_E_STACK_UNDERFLOW;
return -EOVERFLOW;
}
mb = &abce->cstackbase[--abce->csp];
abce_mb_refdn(abce, mb);
return 0;
}
static inline int abce_pop(struct abce *abce)
{
struct abce_mb *mb;
if (abce->sp == 0 || abce->sp <= abce->bp)
{
abce->err.code = ABCE_E_STACK_UNDERFLOW;
return -EOVERFLOW;
}
mb = &abce->stackbase[--abce->sp];
abce_mb_refdn(abce, mb);
return 0;
}
static inline int abce_calc_caddr(size_t *paddr, struct abce *abce, int64_t idx)
{
int64_t addr;
if (idx < 0)
{
addr = abce->csp + idx;
if (abce_unlikely(addr >= abce->csp || addr < 0))
{
abce->err.code = ABCE_E_STACK_IDX_OOB;
abce->err.mb.typ = ABCE_T_D;
abce->err.mb.u.d = idx;
return -EOVERFLOW;
}
}
else
{
addr = idx;
if (abce_unlikely(addr >= abce->csp || addr < 0))
{
abce->err.code = ABCE_E_STACK_IDX_OOB;
abce->err.mb.typ = ABCE_T_D;
abce->err.mb.u.d = idx;
return -EOVERFLOW;
}
}
*paddr = addr;
return 0;
}
static inline int abce_calc_addr(size_t *paddr, struct abce *abce, int64_t idx)
{
size_t addr;
if (idx < 0)
{
addr = abce->sp + idx;
if (abce_unlikely(addr >= abce->sp || addr < abce->bp))
{
abce->err.code = ABCE_E_STACK_IDX_OOB;
abce->err.mb.typ = ABCE_T_D;
abce->err.mb.u.d = idx;
return -EOVERFLOW;
}
}
else
{
addr = abce->bp + idx;
if (abce_unlikely(addr >= abce->sp || addr < abce->bp))
{
abce->err.code = ABCE_E_STACK_IDX_OOB;
abce->err.mb.typ = ABCE_T_D;
abce->err.mb.u.d = idx;
return -EOVERFLOW;
}
}
*paddr = addr;
return 0;
}
static inline int
abce_mb_stackreplace(struct abce *abce, int64_t idx, const struct abce_mb *mbnew)
{
size_t addr;
int ret;
ret = abce_calc_addr(&addr, abce, idx);
if (ret != 0)
{
return ret;
}
abce_mb_refreplace(abce, &abce->stackbase[addr], mbnew);
return 0;
}
static inline int
abce_mb_cstackreplace(struct abce *abce, int64_t idx, const struct abce_mb *mbnew)
{
size_t addr;
int ret;
ret = abce_calc_caddr(&addr, abce, idx);
if (ret != 0)
{
return ret;
}
abce_mb_refreplace(abce, &abce->cstackbase[addr], mbnew);
return 0;
}
static inline int abce_getboolean(int *b, struct abce *abce, int64_t idx)
{
const struct abce_mb *mb;
size_t addr;
if (abce_calc_addr(&addr, abce, idx) != 0)
{
return -EOVERFLOW;
}
mb = &abce->stackbase[addr];
if (abce_unlikely(mb->typ != ABCE_T_D && mb->typ != ABCE_T_B))
{
abce->err.code = ABCE_E_EXPECT_BOOL;
abce_mb_errreplace_noinline(abce, mb);
abce->err.val2 = idx;
return -EINVAL;
}
*b = !!mb->u.d;
return 0;
}
static inline int abce_verifymb(struct abce *abce, int64_t idx, enum abce_type typ)
{
const struct abce_mb *mb;
size_t addr;
if (abce_calc_addr(&addr, abce, idx) != 0)
{
return -EOVERFLOW;
}
mb = &abce->stackbase[addr];
if (abce_unlikely(mb->typ != typ))
{
abce->err.code = (enum abce_errcode)typ; // Same numbers valid for both
abce_mb_errreplace_noinline(abce, mb);
abce->err.val2 = idx;
return -EINVAL;
}
return 0;
}
static inline int abce_getfunaddr(int64_t *paddr, struct abce *abce, int64_t idx)
{
const struct abce_mb *mb;
size_t addr;
if (abce_calc_addr(&addr, abce, idx) != 0)
{
return -EOVERFLOW;
}
mb = &abce->stackbase[addr];
if (abce_unlikely(mb->typ != ABCE_T_F))
{
abce->err.code = ABCE_E_EXPECT_FUNC;
abce_mb_errreplace_noinline(abce, mb);
abce->err.val2 = idx;
return -EINVAL;
}
*paddr = mb->u.d;
return 0;
}
static inline int abce_getbp(struct abce *abce, int64_t idx)
{
const struct abce_mb *mb;
size_t addr;
size_t trial;
if (abce_calc_addr(&addr, abce, idx) != 0)
{
return -EOVERFLOW;
}
mb = &abce->stackbase[addr];
if (abce_unlikely(mb->typ != ABCE_T_BP))
{
abce->err.code = ABCE_E_EXPECT_BP;
abce_mb_errreplace_noinline(abce, mb);
abce->err.val2 = idx;
return -EINVAL;
}
trial = mb->u.d;
if (abce_unlikely(trial != mb->u.d))
{
abce->err.code = ABCE_E_REG_NOT_INT;
abce_mb_errreplace_noinline(abce, mb);
abce->err.val2 = idx;
return -EINVAL;
}
abce->bp = trial;
return 0;
}
static inline int abce_getip(struct abce *abce, int64_t idx)
{
const struct abce_mb *mb;
size_t addr;
int64_t trial;
if (abce_calc_addr(&addr, abce, idx) != 0)
{
return -EOVERFLOW;
}
mb = &abce->stackbase[addr];
if (abce_unlikely(mb->typ != ABCE_T_IP))
{
//printf("invalid typ: %d\n", mb->typ);
abce->err.code = ABCE_E_EXPECT_IP;
abce_mb_errreplace_noinline(abce, mb);
abce->err.val2 = idx;
return -EINVAL;
}
trial = mb->u.d;
if (abce_unlikely(trial != mb->u.d))
{
abce->err.code = ABCE_E_REG_NOT_INT;
abce_mb_errreplace_noinline(abce, mb);
abce->err.val2 = idx;
return -EINVAL;
}
abce->ip = trial;
return 0;
}
static inline int abce_getmbptr(struct abce_mb **mb, struct abce *abce, int64_t idx)
{
struct abce_mb *mbptr;
size_t addr;
if (abce_calc_addr(&addr, abce, idx) != 0)
{
return -EOVERFLOW;
}
mbptr = &abce->stackbase[addr];
*mb = mbptr;
return 0;
}
static inline int abce_getmbtypedptr(struct abce_mb **mb, struct abce *abce, int64_t idx, enum abce_type typ)
{
struct abce_mb *mbptr;
size_t addr;
if (abce_calc_addr(&addr, abce, idx) != 0)
{
return -EOVERFLOW;
}
mbptr = &abce->stackbase[addr];
if (abce_unlikely(mbptr->typ != typ))
{
abce->err.code = (enum abce_errcode)typ; // Same numbers valid for both
abce_mb_errreplace_noinline(abce, mbptr);
abce->err.val2 = idx;
return -EINVAL;
}
*mb = mbptr;
return 0;
}
static inline int abce_getmbscptr(struct abce_mb **mb, struct abce *abce, int64_t idx)
{
return abce_getmbtypedptr(mb, abce, idx, ABCE_T_SC);
}
static inline int abce_getmbarptr(struct abce_mb **mb, struct abce *abce, int64_t idx)
{
return abce_getmbtypedptr(mb, abce, idx, ABCE_T_A);
}
static inline int abce_getmbpbptr(struct abce_mb **mb, struct abce *abce, int64_t idx)
{
return abce_getmbtypedptr(mb, abce, idx, ABCE_T_PB);
}
static inline int abce_getmbstrptr(struct abce_mb **mb, struct abce *abce, int64_t idx)
{
return abce_getmbtypedptr(mb, abce, idx, ABCE_T_S);
}
static inline int abce_verifyaddr(struct abce *abce, int64_t idx)
{
size_t addr;
if (abce_calc_addr(&addr, abce, idx) != 0)
{
return -EOVERFLOW;
}
return 0;
}
static inline int abce_getdbl(double *d, struct abce *abce, int64_t idx)
{
const struct abce_mb *mb;
size_t addr;
if (abce_calc_addr(&addr, abce, idx) != 0)
{
return -EOVERFLOW;
}
//printf("addr %d\n", (int)addr);
mb = &abce->stackbase[addr];
if (abce_unlikely(mb->typ != ABCE_T_D && mb->typ != ABCE_T_B))
{
abce->err.code = ABCE_E_EXPECT_DBL;
abce_mb_errreplace_noinline(abce, mb);
abce->err.val2 = idx;
return -EINVAL;
}
*d = mb->u.d;
return 0;
}
static inline int abce_cpush_nil(struct abce *abce)
{
if (abce_unlikely(abce->csp >= abce->cstacklimit))
{
abce->err.code = ABCE_E_STACK_OVERFLOW;
abce->err.mb.typ = ABCE_T_N;
return -EOVERFLOW;
}
abce->cstackbase[abce->csp].typ = ABCE_T_N;
abce->csp++;
return 0;
}
static inline int abce_cpush_mb(struct abce *abce, const struct abce_mb *mb)
{
if (abce_unlikely(abce->csp >= abce->cstacklimit))
{
abce->err.code = ABCE_E_STACK_OVERFLOW;
abce_mb_errreplace_noinline(abce, mb);
return -EOVERFLOW;
}
abce->cstackbase[abce->csp] = abce_mb_refup(abce, mb);
abce->csp++;
return 0;
}
static inline int abce_push_mb(struct abce *abce, const struct abce_mb *mb)
{
if (abce_unlikely(abce->sp >= abce->stacklimit))
{
abce->err.code = ABCE_E_STACK_OVERFLOW;
abce_mb_errreplace_noinline(abce, mb);
return -EOVERFLOW;
}
abce->stackbase[abce->sp] = abce_mb_refup(abce, mb);
abce->sp++;
return 0;
}
static inline int abce_push_c(struct abce *abce)
{
if (abce_unlikely(abce->sp >= abce->stacklimit))
{
abce->err.code = ABCE_E_STACK_OVERFLOW;
abce->err.mb.typ = ABCE_T_N;
return -EOVERFLOW;
}
if (abce_unlikely(abce->csp == 0))
{
abce->err.code = ABCE_E_STACK_UNDERFLOW;
abce->err.mb.typ = ABCE_T_N;
return -EOVERFLOW; // FIXME UNDERFLOW
}
abce->stackbase[abce->sp] = abce_mb_refup(abce, &abce->cstackbase[abce->csp-1]);
abce->sp++;
return 0;
}
static inline int abce_push_boolean(struct abce *abce, int boolean)
{
if (abce_unlikely(abce->sp >= abce->stacklimit))
{
abce->err.code = ABCE_E_STACK_OVERFLOW;
abce->err.mb.typ = ABCE_T_B;
abce->err.mb.u.d = boolean;
return -EOVERFLOW;
}
abce->stackbase[abce->sp].typ = ABCE_T_B;
abce->stackbase[abce->sp].u.d = boolean ? 1.0 : 0.0;
abce->sp++;
return 0;
}
static inline int abce_push_nil(struct abce *abce)
{
if (abce_unlikely(abce->sp >= abce->stacklimit))
{
abce->err.code = ABCE_E_STACK_OVERFLOW;
abce->err.mb.typ = ABCE_T_N;
return -EOVERFLOW;
}
abce->stackbase[abce->sp].typ = ABCE_T_N;
abce->sp++;
return 0;
}
static inline int abce_push_ip(struct abce *abce)
{
if (abce_unlikely(abce->sp >= abce->stacklimit))
{
abce->err.code = ABCE_E_STACK_OVERFLOW;
abce->err.mb.typ = ABCE_T_IP;
abce->err.mb.u.d = abce->ip;
return -EOVERFLOW;
}
abce->stackbase[abce->sp].typ = ABCE_T_IP;
abce->stackbase[abce->sp].u.d = abce->ip;
abce->sp++;
return 0;
}
static inline int abce_push_rg(struct abce *abce)
{
if (abce_unlikely(abce->sp >= abce->stacklimit))
{
abce->err.code = ABCE_E_STACK_OVERFLOW;
abce->err.mb.typ = ABCE_T_RG;
abce->err.mb.u.d = 0;
return -EOVERFLOW;
}
abce->stackbase[abce->sp].typ = ABCE_T_RG;
abce->stackbase[abce->sp].u.d = 0;
abce->sp++;
return 0;
}
static inline int abce_push_bp(struct abce *abce)
{
if (abce_unlikely(abce->sp >= abce->stacklimit))
{
abce->err.code = ABCE_E_STACK_OVERFLOW;
abce->err.mb.typ = ABCE_T_BP;
abce->err.mb.u.d = abce->bp;
return -EOVERFLOW;
}
abce->stackbase[abce->sp].typ = ABCE_T_BP;
abce->stackbase[abce->sp].u.d = abce->bp;
abce->sp++;
return 0;
}
static inline int abce_push_double(struct abce *abce, double dbl)
{
if (abce_unlikely(abce->sp >= abce->stacklimit))
{
abce->err.code = ABCE_E_STACK_OVERFLOW;
abce->err.mb.typ = ABCE_T_D;
abce->err.mb.u.d = dbl;
return -EOVERFLOW;
}
abce->stackbase[abce->sp].typ = ABCE_T_D;
abce->stackbase[abce->sp].u.d = dbl;
abce->sp++;
return 0;
}
static inline int abce_push_fun(struct abce *abce, double fun_addr)
{
if (abce_unlikely(abce->sp >= abce->stacklimit))
{
abce->err.code = ABCE_E_STACK_OVERFLOW;
abce->err.mb.typ = ABCE_T_F;
abce->err.mb.u.d = fun_addr;
return -EOVERFLOW;
}
if (abce_unlikely((double)(int64_t)fun_addr != fun_addr))
{
abce->err.code = ABCE_E_FUNADDR_NOT_INT;
abce->err.mb.typ = ABCE_T_F;
abce->err.mb.u.d = fun_addr;
return -EINVAL;
}
abce->stackbase[abce->sp].typ = ABCE_T_F;
abce->stackbase[abce->sp].u.d = fun_addr;
abce->sp++;
return 0;
}
static inline int
abce_add_ins_alt(void *bcode, size_t *bsz, size_t cap, uint16_t ins)
{
unsigned char *bytecode = (unsigned char*)bcode;
if (ins >= 2048)
{
if ((*bsz) + 3 > cap)
{
return -EFAULT;
}
bytecode[(*bsz)++] = (ins>>12) | 0xE0;
bytecode[(*bsz)++] = ((ins>>6)&0x3F) | 0x80;
bytecode[(*bsz)++] = ((ins)&0x3F) | 0x80;
return 0;
}
else if (ins >= 128)
{
if ((*bsz) + 2 > cap)
{
return -EFAULT;
}
bytecode[(*bsz)++] = ((ins>>6)) | 0xC0;
bytecode[(*bsz)++] = ((ins)&0x3F) | 0x80;
return 0;
}
else
{
if ((*bsz) >= cap)
{
return -EFAULT;
}
bytecode[(*bsz)++] = ins;
return 0;
}
}
static inline int abce_add_ins(struct abce *abce, uint16_t ins)
{
return abce_add_ins_alt(abce->bytecode, &abce->bytecodesz, abce->bytecodecap,
ins);
}
static inline int abce_add_byte(struct abce *abce, unsigned char byte)
{
if (abce->bytecodesz >= abce->bytecodecap)
{
return -EFAULT;
}
abce->bytecode[abce->bytecodesz++] = byte;
return 0;
}
static inline struct abce_mb_area*
abce_mb_arearefup(struct abce *abce, const struct abce_mb *mb)
{
if (!abce_is_dynamic_type(mb->typ))
{
abort();
}
mb->u.area->refcnt++;
return mb->u.area;
}
static inline struct abce_mb
abce_mb_refuparea(struct abce *abce, struct abce_mb_area *mba,
enum abce_type typ)
{
struct abce_mb mb = {};
if (mba == NULL)
{
mb.typ = ABCE_T_N;
mb.u.d = 0;
return mb;
}
if (!abce_is_dynamic_type(typ))
{
abort();
}
mb.typ = typ;
mb.u.area = mba;
mba->refcnt++;
return mb;
}
struct abce_mb *abce_mb_cpush_create_string(struct abce *abce, const char *str, size_t sz);
struct abce_mb *abce_mb_cpush_create_string_to_be_filled(struct abce *abce, size_t sz);
struct abce_mb *abce_mb_cpush_concat_string(struct abce *abce, const char *str1, size_t sz1,
const char *str2, size_t sz2);
struct abce_mb *abce_mb_cpush_rep_string(struct abce *abce, const char *str1, size_t sz1,
size_t cnt);
static inline struct abce_mb *
abce_mb_cpush_create_string_nul(struct abce *abce, const char *str)
{
return abce_mb_cpush_create_string(abce, str, strlen(str));
}
static inline uint32_t abce_str_hash(const char *str)
{
size_t len = strlen(str);
return abce_murmur_buf(0x12345678U, str, len);
}
static inline uint32_t abce_str_len_hash(const struct abce_const_str_len *str_len)
{
size_t len = str_len->len;
return abce_murmur_buf(0x12345678U, str_len->str, len);
}
static inline uint32_t abce_mb_str_hash(const struct abce_mb *mb)
{
if (mb->typ != ABCE_T_S)
{
abort();
}
return abce_murmur_buf(0x12345678U, mb->u.area->u.str.buf, mb->u.area->u.str.size);
}
static inline int abce_str_cache_cmp_asymlen(const struct abce_const_str_len *str_len, struct abce_rb_tree_node *n2, void *ud)
{
struct abce_mb_string *e = ABCE_CONTAINER_OF(n2, struct abce_mb_string, node);
size_t len1 = str_len->len;
size_t len2, lenmin;
int ret;
char *str2;
len2 = e->size;
str2 = e->buf;
lenmin = (len1 < len2) ? len1 : len2;
ret = memcmp(str_len->str, str2, lenmin);
if (ret != 0)
{
return ret;
}
if (len1 > len2)
{
return 1;
}
if (len1 < len2)
{
return -1;
}
return 0;
}
static inline int abce_str_cmp_asym(const char *str, struct abce_rb_tree_node *n2, void *ud)
{
struct abce_mb_rb_entry *e = ABCE_CONTAINER_OF(n2, struct abce_mb_rb_entry, n);
size_t len1 = strlen(str);
size_t len2, lenmin;
int ret;
char *str2;
if (e->key.typ != ABCE_T_S)
{
abort();
}
len2 = e->key.u.area->u.str.size;
str2 = e->key.u.area->u.str.buf;
lenmin = (len1 < len2) ? len1 : len2;
ret = memcmp(str, str2, lenmin);
if (ret != 0)
{
return ret;
}
if (len1 > len2)
{
return 1;
}
if (len1 < len2)
{
return -1;
}
return 0;
}
static inline int abce_str_cmp_halfsym(
const struct abce_mb *key, struct abce_rb_tree_node *n2, void *ud)
{
struct abce_mb_rb_entry *e2 = ABCE_CONTAINER_OF(n2, struct abce_mb_rb_entry, n);
size_t len1, len2, lenmin;
int ret;
char *str1, *str2;
if (key->typ != ABCE_T_S || e2->key.typ != ABCE_T_S)
{
abort();
}
len1 = key->u.area->u.str.size;
str1 = key->u.area->u.str.buf;
len2 = e2->key.u.area->u.str.size;
str2 = e2->key.u.area->u.str.buf;
lenmin = (len1 < len2) ? len1 : len2;
ret = memcmp(str1, str2, lenmin);
if (ret != 0)
{
return ret;
}
if (len1 > len2)
{
return 1;
}
if (len1 < len2)
{
return -1;
}
return 0;
}
static inline int abce_str_cache_cmp_sym(
struct abce_rb_tree_node *n1, struct abce_rb_tree_node *n2, void *ud)
{
struct abce_mb_string *e1 = ABCE_CONTAINER_OF(n1, struct abce_mb_string, node);
struct abce_mb_string *e2 = ABCE_CONTAINER_OF(n2, struct abce_mb_string, node);
size_t len1, len2, lenmin;
int ret;
char *str1, *str2;
len1 = e1->size;
str1 = e1->buf;
len2 = e2->size;
str2 = e2->buf;
lenmin = (len1 < len2) ? len1 : len2;
ret = memcmp(str1, str2, lenmin);
if (ret != 0)
{
return ret;
}
if (len1 > len2)
{
return 1;
}
if (len1 < len2)
{
return -1;
}
return 0;
}
static inline int abce_str_cmp_sym_mb(
const struct abce_mb *mb1, const struct abce_mb *mb2)
{
size_t len1, len2, lenmin;
int ret;
const char *str1, *str2;
if (mb1->typ != ABCE_T_S || mb2->typ != ABCE_T_S)
{
abort();
}
len1 = mb1->u.area->u.str.size;
str1 = mb1->u.area->u.str.buf;
len2 = mb2->u.area->u.str.size;
str2 = mb2->u.area->u.str.buf;
lenmin = (len1 < len2) ? len1 : len2;
ret = memcmp(str1, str2, lenmin);
if (ret != 0)
{
return ret;
}
if (len1 > len2)
{
return 1;
}
if (len1 < len2)
{
return -1;
}
return 0;
}
static inline int abce_str_cmp_sym(
struct abce_rb_tree_node *n1, struct abce_rb_tree_node *n2, void *ud)
{
struct abce_mb_rb_entry *e1 = ABCE_CONTAINER_OF(n1, struct abce_mb_rb_entry, n);
struct abce_mb_rb_entry *e2 = ABCE_CONTAINER_OF(n2, struct abce_mb_rb_entry, n);
if (e1->key.typ != ABCE_T_S || e2->key.typ != ABCE_T_S)
{
abort();
}
return abce_str_cmp_sym_mb(&e1->key, &e2->key);
}
int64_t abce_cache_add_str(struct abce *abce, const char *str, size_t len);
static inline int64_t abce_cache_add_str_nul(struct abce *abce, const char *str)
{
return abce_cache_add_str(abce, str, strlen(str));
}
// RFE remove inlining?
static inline const struct abce_mb *abce_sc_get_myval_mb_area(
const struct abce_mb_area *mba, const struct abce_mb *key)
{
uint32_t hashval;
size_t hashloc;
struct abce_rb_tree_node *n;
if (key->typ != ABCE_T_S)
{
abort();
}
hashval = abce_mb_str_hash(key);
hashloc = hashval & (mba->u.sc.size - 1);
n = ABCE_RB_TREE_NOCMP_FIND(&mba->u.sc.heads[hashloc], abce_str_cmp_halfsym, NULL, key);
if (n == NULL)
{
return NULL;
}
return &ABCE_CONTAINER_OF(n, struct abce_mb_rb_entry, n)->val;
}
static inline const struct abce_mb *abce_sc_get_myval_mb(
const struct abce_mb *mb, const struct abce_mb *key)
{
if (mb->typ != ABCE_T_SC)
{
abort();
}
return abce_sc_get_myval_mb_area(mb->u.area, key);
}
// RFE remove inlining?
static inline const struct abce_mb *abce_sc_get_myval_str_area(
const struct abce_mb_area *mba, const char *str)
{