forked from perl11/potion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompile.c
1124 lines (1058 loc) · 37.6 KB
/
compile.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
/** \file compile.c
* ast to bytecode.
*
* implement PNSource (AST) and PNProto (closure) methods,
* special signature handling (parsed extra) and compile, bytecode load and dump methods.
* Some special control methods are handled here and not in the parser. We do not need
* lexed keywords.
*/
// (c) 2008 why the lucky stiff, the freelance professor
//
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include "p2.h"
#include "internal.h"
#include "ast.h"
#include "opcodes.h"
#include "asm.h"
PN potion_proto_load(Potion *P, PN up, u8 pn, u8 **ptr);
#define PN_ASM1(ins, _a) f->asmb = (PN)potion_asm_op(P, (PNAsm *)f->asmb, (u8)ins, (int)_a, 0)
#define PN_ASM2(ins, _a, _b) f->asmb = (PN)potion_asm_op(P, (PNAsm *)f->asmb, (u8)ins, (int)_a, (int)_b)
const struct {
const char *name;
const u8 args;
} potion_ops[] = {
{"noop", 0}, {"move", 2}, {"loadk", 2}, {"loadpn", 2}, {"self", 1},
{"newtuple", 2}, {"settuple", 2}, {"getlocal", 2}, {"setlocal", 2},
{"getupval", 2}, {"setupval", 2}, {"global", 2}, {"gettable", 2},
{"settable", 2}, {"newlick", 2}, {"getpath", 2}, {"setpath", 2},
{"add", 2}, {"sub", 2}, {"mult", 2}, {"div", 2}, {"mod", 2}, {"pow", 2},
{"not", 1}, {"cmp", 2}, {"eq", 2}, {"neq", 2}, {"lt", 2}, {"lte", 2},
{"gt", 2}, {"gte", 2}, {"bitn", 2}, {"bitl", 2}, {"bitr", 2}, {"def", 2},
{"bind", 2}, {"msg", 2}, {"jump", 1}, {"test", 2}, {"testjmp", 2},
{"notjmp", 2}, {"named", 2}, {"call", 2}, {"callset", 2}, {"tailcall", 2},
{"return", 1}, {"proto", 2}, {"class", 2}, {"debug", 3}
};
///\memberof PNProto
/// tree method of PNProto
///\return the original PNSource AST for the closure
PN potion_proto_tree(Potion *P, PN cl, PN self) {
return PN_PROTO(self)->tree;
}
///\memberof PNProto
/// call method of PNProto. i.e. apply - bind args to a closure.
///\param args
PN potion_proto_call(Potion *P, PN cl, PN self, PN args) {
return potion_vm(P, self, P->lobby, args, 0, NULL);
}
///\return string of the sig tuple. "arg1=o,arg2=o"
PN potion_sig_string(Potion *P, PN cl, PN sig) {
PN out = potion_byte_str(P, "");
if (PN_IS_TUPLE(sig)) {
int nextdef = 0;
struct PNTuple * volatile t = ((struct PNTuple *)potion_fwd(sig));
if (t->len != 0) {
PN_SIZE i, comma=0;
for (i = 0; i < t->len; i++) {
PN v = (PN)t->set[i];
if (PN_IS_NUM(v)) {
// currently types are still encoded as NUM, TODO: support VTABLE also
int c = PN_INT(v); comma=0;
if (c == '.') // is end
pn_printf(P, out, ".");
else if (c == '|') // is optional
pn_printf(P, out, "|");
else if (c == ':') { nextdef = 1;
pn_printf(P, out, ":"); // is default
}
else {
if (comma++) pn_printf(P, out, ",");
if (nextdef) { nextdef = 0;
pn_printf(P, out, "=");
potion_bytes_obj_string(P, out, v);
} else
pn_printf(P, out, "=%c", c);
}
} else {
if (comma++) pn_printf(P, out, ",");
if (nextdef)
{ nextdef = 0; pn_printf(P, out, "="); }
potion_bytes_obj_string(P, out, v);
}}}}
return PN_STR_B(out);
}
///\memberof PNProto
/// string method of PNProto. ascii dump of a function definition
PN potion_proto_string(Potion *P, PN cl, PN self) {
vPN(Proto) t = (struct PNProto *)self;
int x = 0;
PN_SIZE num = 1;
PN_SIZE numcols;
PN out = potion_byte_str(P, "; function definition");
#ifdef JIT_DEBUG
pn_printf(P, out, ": %p; %u bytes\n", t, PN_FLEX_SIZE(t->asmb));
#else
pn_printf(P, out, ": %u bytes\n", PN_FLEX_SIZE(t->asmb));
#endif
pn_printf(P, out, "; (");
potion_bytes_obj_string(P, out, potion_sig_string(P,cl,t->sig));
pn_printf(P, out, ") %ld registers\n", PN_INT(t->stack));
PN_TUPLE_EACH(t->paths, i, v, {
pn_printf(P, out, ".path /");
v = PN_TUPLE_AT(t->values, PN_INT(v));
potion_bytes_obj_string(P, out, v);
pn_printf(P, out, " ; %u\n", i);
});
PN_TUPLE_EACH(t->locals, i, v, {
pn_printf(P, out, ".local ");
potion_bytes_obj_string(P, out, v);
pn_printf(P, out, " ; %u\n", i);
});
PN_TUPLE_EACH(t->upvals, i, v, {
pn_printf(P, out, ".upval ");
potion_bytes_obj_string(P, out, v);
pn_printf(P, out, " ; %u\n", i);
});
PN_TUPLE_EACH(t->values, i, v, {
pn_printf(P, out, ".value ");
potion_bytes_obj_string(P, out, v);
pn_printf(P, out, " ; %u\n", i);
});
PN_TUPLE_EACH(t->protos, i, v, {
potion_bytes_obj_string(P, out, v);
});
numcols = (int)ceil(log10(PN_FLEX_SIZE(t->asmb) / sizeof(PN_OP)));
for (x = 0; x < PN_FLEX_SIZE(t->asmb) / sizeof(PN_OP); x++) {
const int commentoffset = 20;
int width = pn_printf(P, out, "[%*u] %-8s %d",
numcols, num, potion_ops[PN_OP_AT(t->asmb, x).code].name, PN_OP_AT(t->asmb, x).a);
if (potion_ops[PN_OP_AT(t->asmb, x).code].args > 1)
width += pn_printf(P, out, " %d", PN_OP_AT(t->asmb, x).b);
if (width < commentoffset)
pn_printf(P, out, "%*s", commentoffset - width, "");
else
pn_printf(P, out, " ");
// TODO: Byte code listing: instead of using tabs, pad with spaces to make everything line up
switch (PN_OP_AT(t->asmb, x).code) {
case OP_JMP:
pn_printf(P, out, "; to %d", num + PN_OP_AT(t->asmb, x).a + 1);
break;
case OP_NOTJMP:
case OP_TESTJMP:
pn_printf(P, out, "; to %d", num + PN_OP_AT(t->asmb, x).b + 1);
break;
case OP_LOADPN:
pn_printf(P, out, "; ");
potion_bytes_obj_string(P, out, PN_OP_AT(t->asmb, x).b);
break;
case OP_LOADK:
pn_printf(P, out, "; ");
potion_bytes_obj_string(P, out, PN_TUPLE_AT(t->values, PN_OP_AT(t->asmb, x).b));
break;
case OP_SETLOCAL:
case OP_GETLOCAL:
pn_printf(P, out, "; ");
potion_bytes_obj_string(P, out, PN_TUPLE_AT(t->locals, PN_OP_AT(t->asmb, x).b));
break;
}
pn_printf(P, out, "\n");
num++;
}
//pn_printf(P, out, "\n");
pn_printf(P, out, "; function end\n");
return PN_STR_B(out);
}
#define PN_REG(f, reg) \
if (reg >= PN_INT(f->stack)) \
f->stack = PN_NUM(reg + 1)
#ifdef P2
// uses global PN_SRC(t)
#define PN_ARG(n, reg) \
if (PN_PART(t->a[n]) == AST_LIST && PN_PART(PN_TUPLE_AT(PN_S(t->a[n],0), 0)) == AST_EXPR) { \
PN test = PN_S(PN_TUPLE_AT(PN_S(t->a[n],0), 0), 0); \
if (!PN_IS_NIL(test)) { \
PN_TUPLE_EACH(test, i, v, { \
potion_source_asmb(P, f, loop, 0, PN_SRC(v), reg); }); \
} \
} else { \
potion_source_asmb(P, f, loop, 0, t->a[n], reg); \
}
#else
#define PN_ARG(n, reg) \
if (PN_PART(t->a[n]) == AST_EXPR && PN_PART(PN_TUPLE_AT(PN_S(t->a[n],0), 0)) == AST_LIST) { \
PN test = PN_S(PN_TUPLE_AT(PN_S(t->a[n],0), 0), 0); \
if (!PN_IS_NIL(test)) { \
PN_TUPLE_EACH(test, i, v, { \
potion_source_asmb(P, f, loop, 0, PN_SRC(v), reg); }); \
} \
} else { \
potion_source_asmb(P, f, loop, 0, t->a[n], reg); \
}
#endif
#define PN_BLOCK(reg, blk, sig) do { \
PN block = potion_send(blk, PN_compile, (PN)f, sig); \
PN_SIZE num = PN_PUT(f->protos, block); \
PN_ASM2(OP_PROTO, reg, num); \
PN_TUPLE_EACH(((struct PNProto *)block)->upvals, i, v, { \
PN_SIZE numup = PN_GET(f->upvals, v); \
if (numup != PN_NONE) PN_ASM2(OP_GETUPVAL, reg, numup); \
else PN_ASM2(OP_GETLOCAL, reg, PN_GET(f->locals, v)); \
}); \
} while(0);
#define PN_UPVAL(name) potion_upval(P, f, name)
inline PN_SIZE potion_upval(Potion *P, struct PNProto * volatile f, PN name) {
PN_SIZE numl = PN_GET(f->locals, name);
PN_SIZE numup = PN_NONE;
if (numl == PN_NONE) {
numup = PN_GET(f->upvals, name);
if (numup == PN_NONE) {
vPN(Proto) up = f;
int depth = 1;
while (PN_IS_PROTO(up->source)) {
up = (struct PNProto *)up->source;
if (PN_NONE != (numup = PN_GET(up->locals, name))) break;
depth++;
}
if (numup != PN_NONE) {
up = f;
while (depth--) {
up->upvals = PN_PUSH(up->upvals, name);
up = (struct PNProto *)up->source;
}
}
numup = PN_GET(f->upvals, name);
}
}
return numup;
}
#define PN_ARG_TABLE(args, reg, inc) potion_arg_asmb(P, f, loop, args, ®, inc)
#define SRC_TUPLE_AT(src,i) PN_SRC((PN_TUPLE_AT(PN_S(src,0), i)))
#define MAX_JUMPS 1024
/// jump table for loops, for the 2 args b and c
/// Note: only statically allocated (max 1024)
struct PNLoop {
int bjmps[MAX_JUMPS];
int cjmps[MAX_JUMPS];
int bjmpc; ///< count of b jumps (abc regs)
int cjmpc; ///< count of c jumps (abc regs)
};
void potion_source_asmb(Potion *, struct PNProto * volatile, struct PNLoop *, PN_SIZE, struct PNSource * volatile, u8);
void potion_arg_asmb(Potion *P, struct PNProto * volatile f, struct PNLoop *loop, PN args, u8 *reg, int inc) {
if (args != PN_NIL) {
if (PN_PART(args) == AST_LIST) {
args = PN_S(args,0);
if (!PN_IS_NIL(args)) {
u8 freg = *reg, sreg = *reg + PN_TUPLE_LEN(args) + 1;
DBG_c("ARGLIST %d %d\n", freg, sreg);
PN_TUPLE_EACH(args, i, v, {
if (inc) {
(*reg)++;
if (PN_PART(v) == AST_ASSIGN) {
vPN(Source) lhs = PN_SRC(PN_S(v,0));
potion_source_asmb(P, f, loop, 0, PN_SRC(v)->a[1], sreg + 1);
if (lhs->part == AST_EXPR && PN_TUPLE_LEN(PN_S(lhs,0)) == 1)
{
lhs = SRC_TUPLE_AT(lhs, 0);
if (lhs->part == AST_MSG || lhs->part == AST_VALUE) {
PN_OP op; op.a = PN_S(lhs,0); //12 bit!
if (!PN_IS_PTR(PN_S(lhs,0)) && PN_S(lhs,0) == op.a) {
PN_ASM2(OP_LOADPN, sreg, PN_S(lhs,0));
} else {
PN_SIZE num = PN_PUT(f->values, PN_S(lhs,0));
PN_ASM2(OP_LOADK, sreg, num);
}
lhs = NULL;
}
}
if (lhs != NULL)
potion_source_asmb(P, f, loop, 0, lhs, sreg);
PN_ASM2(OP_NAMED, freg - 1, sreg + 1);
PN_REG(f, sreg + 1);
} else
potion_source_asmb(P, f, loop, 0, PN_SRC(v), *reg);
} else
potion_source_asmb(P, f, loop, 0, PN_SRC(v), *reg);
});
}
} else {
if (inc) (*reg)++;
potion_source_asmb(P, f, loop, 0, PN_SRC(args), *reg);
}
} else {
if (inc) (*reg)++;
PN_ASM2(OP_LOADPN, *reg, args);
}
}
void potion_source_asmb(Potion *P, struct PNProto * volatile f, struct PNLoop *loop, PN_SIZE count,
struct PNSource * volatile t, u8 reg) {
PN_REG(f, reg);
if (P->flags & EXEC_DEBUG) {
// debug eol ast's are not pushed nicely at statement/expression borders
// but can be embedded anywhere. so unpack ori from (debug ori line file) at first.
if (t->a[0]->part == AST_DEBUG) {
struct PNSource * volatile v = t->a[0];
DBG_c("debug %ld %s\n", PN_INT(PN_S(v,1)), PN_STR_PTR(PN_S(v,2)));
PN_ASM2(OP_DEBUG, PN_S(v,1), PN_S(v,2));
return potion_source_asmb(P, f, loop, 0, v->a[0], reg);
}
}
switch (t->part) {
case AST_CODE:
case AST_BLOCK:
if (PN_S(t,0) != PN_NIL) {
DBG_c("%s [%u] %u\n", t->part == AST_CODE?"code":"block", PN_TUPLE_LEN(PN_S(t,0)), reg);
PN_TUPLE_EACH(PN_S(t,0), i, v, {
potion_source_asmb(P, f, loop, 0, PN_SRC(v), reg);
});
}
break;
case AST_EXPR:
if (PN_S(t,0) != PN_NIL) {
DBG_c("expr %d\n", PN_TUPLE_LEN(PN_S(t,0)));
PN_TUPLE_EACH(PN_S(t,0), i, v, {
potion_source_asmb(P, f, loop, i, PN_SRC(v), reg);
});
}
break;
case AST_PROTO:
PN_BLOCK(reg, PN_S(t,1), PN_S(t,0));
break;
case AST_VALUE: {
PN_OP op; op.a = PN_S(t,0);
if (!PN_IS_PTR(PN_S(t,0)) && PN_S(t,0) == (PN)op.a) {
PN_ASM2(OP_LOADPN, reg, PN_S(t,0));
} else {
PN_SIZE num = PN_PUT(f->values, PN_S(t,0));
PN_ASM2(OP_LOADK, reg, num);
}
if (PN_S(t,1) != PN_NIL) {
u8 breg = reg;
PN_ASM1(OP_SELF, ++breg);
PN_ARG_TABLE(PN_S(t,1), breg, 1);
if (PN_S(t,2) != PN_NIL) {
breg++;
PN_BLOCK(breg, PN_S(t,2), PN_NIL);
}
DBG_c("; call %d %d VALUE\n", reg, breg);
PN_ASM2(OP_CALL, reg, breg);
}
}
break;
case AST_ASSIGN: {
vPN(Source) lhs = t->a[0];
PN_SIZE num = PN_NONE, c = count;
u8 opcode = OP_GETUPVAL, breg = reg;
if (lhs->part == AST_EXPR) {
unsigned long i = 0;
c = PN_TUPLE_LEN(PN_S(lhs,0)) - 1;
DBG_c("assign expr [%lu]\n", (_PN)c);
for (i = 0; i < c; i++) {
potion_source_asmb(P, f, loop, i, SRC_TUPLE_AT(lhs, i), reg);
};
lhs = SRC_TUPLE_AT(lhs, c);
}
if (lhs->part == AST_MSG || lhs->part == AST_QUERY) {
char first_letter = PN_STR_PTR(PN_S(lhs,0))[0];
DBG_c("assign %s '%s'\n", lhs->part == AST_MSG?"msg":"query",
PN_STR_PTR(PN_S(lhs,0)));
if ((first_letter & 0x80) == 0 && isupper((unsigned char)first_letter)) {
num = PN_PUT(f->values, PN_S(lhs,0));
PN_ASM2(OP_LOADK, breg, num);
opcode = OP_GLOBAL;
num = ++breg;
} else if (c == 0) {
num = PN_UPVAL(PN_S(lhs,0));
if (num == PN_NONE) {
num = PN_PUT(f->locals, PN_S(lhs,0));
opcode = OP_GETLOCAL;
}
} else {
num = PN_PUT(f->values, PN_S(lhs,0));
PN_ASM2(OP_LOADK, ++breg, num);
opcode = OP_DEF;
num = ++breg;
}
} else if (lhs->part == AST_PATH || lhs->part == AST_PATHQ) {
DBG_c("assign %s\n", lhs->part == AST_PATH?"path":"pathq");
num = PN_PUT(f->values, PN_S(lhs,0));
if (c == 0) {
PN_PUT(f->paths, PN_NUM(num));
PN_ASM1(OP_SELF, reg);
}
PN_ASM2(OP_LOADK, ++breg, num);
opcode = OP_GETPATH;
num = ++breg;
}
if (lhs->a[1] != PN_NIL) {
breg = reg;
PN_ASM2(opcode, ++breg, num);
DBG_c("; callset %d %d ASSIGN\n", reg, breg);
PN_ASM2(OP_CALLSET, reg, breg);
PN_ARG_TABLE(PN_S(lhs,1), breg, 1);
// TODO: no block allowed here?
potion_source_asmb(P, f, loop, 0, t->a[1], ++breg);
DBG_c("; call %d %d ASSIGN\n", reg, breg);
PN_ASM2(OP_CALL, reg, breg);
} else {
potion_source_asmb(P, f, loop, 0, t->a[1], breg);
if (opcode == OP_GETUPVAL) {
if (lhs->part == AST_QUERY) {
PN_ASM2(OP_GETUPVAL, breg, num);
PN_ASM2(OP_TESTJMP, breg, 1);
}
PN_ASM2(OP_SETUPVAL, reg, num);
} else if (opcode == OP_GETLOCAL) {
if (lhs->part == AST_QUERY) {
PN_ASM2(OP_GETLOCAL, breg, num);
PN_ASM2(OP_TESTJMP, breg, 1);
}
PN_ASM2(OP_SETLOCAL, reg, num);
} else if (opcode == OP_GETPATH) {
if (lhs->part == AST_PATHQ) {
PN_ASM2(OP_GETPATH, reg, num);
PN_ASM2(OP_TESTJMP, reg, 1);
}
PN_ASM2(OP_SETPATH, reg, num);
} else {
PN_ASM2(opcode, reg, num);
}
}
PN_REG(f, breg);
}
break;
case AST_INC: {
u8 breg = reg;
vPN(Source) lhs = t->a[0];
PN_SIZE num = PN_UPVAL(PN_S(lhs,0));
u8 opcode = OP_SETUPVAL;
if (num == PN_NONE) {
num = PN_PUT(f->locals, PN_S(lhs,0));
opcode = OP_SETLOCAL;
}
if (opcode == OP_SETUPVAL)
PN_ASM2(OP_GETUPVAL, reg, num);
else if (opcode == OP_SETLOCAL)
PN_ASM2(OP_GETLOCAL, reg, num);
if (PN_IS_NUM(PN_S(t,1))) {
breg++;
PN_ASM2(OP_MOVE, breg, reg);
}
PN_ASM2(OP_LOADPN, breg + 1, (PN_S(t,1) | PN_FNUMBER));
PN_ASM2(OP_ADD, breg, breg + 1);
PN_ASM2(opcode, breg, num);
PN_REG(f, breg + 1);
}
break;
case AST_CMP: case AST_EQ: case AST_NEQ:
case AST_GT: case AST_GTE: case AST_LT: case AST_LTE:
case AST_PLUS: case AST_MINUS: case AST_TIMES: case AST_DIV:
case AST_REM: case AST_POW: case AST_BITL: case AST_BITR: {
PN_ARG(0, reg);
PN_ARG(1, reg + 1);
switch (t->part) {
case AST_CMP: PN_ASM2(OP_CMP, reg, reg + 1); break;
case AST_EQ: PN_ASM2(OP_EQ, reg, reg + 1); break;
case AST_NEQ: PN_ASM2(OP_NEQ, reg, reg + 1); break;
case AST_GTE: PN_ASM2(OP_GTE, reg, reg + 1); break;
case AST_GT: PN_ASM2(OP_GT, reg, reg + 1); break;
case AST_LT: PN_ASM2(OP_LT, reg, reg + 1); break;
case AST_LTE: PN_ASM2(OP_LTE, reg, reg + 1); break;
case AST_PLUS: PN_ASM2(OP_ADD, reg, reg + 1); break;
case AST_MINUS: PN_ASM2(OP_SUB, reg, reg + 1); break;
case AST_TIMES: PN_ASM2(OP_MULT, reg, reg + 1); break;
case AST_DIV: PN_ASM2(OP_DIV, reg, reg + 1); break;
case AST_REM: PN_ASM2(OP_REM, reg, reg + 1); break;
case AST_POW: PN_ASM2(OP_POW, reg, reg + 1); break;
case AST_BITL: PN_ASM2(OP_BITL, reg, reg + 1); break;
case AST_BITR: PN_ASM2(OP_BITR, reg, reg + 1); break;
}
}
break;
case AST_NOT: case AST_WAVY:
PN_ARG(0, reg);
PN_ASM2(t->part == AST_WAVY ? OP_BITN : OP_NOT, reg, reg);
break;
case AST_AND: case AST_OR: {
int jmp;
PN_ARG(0, reg);
jmp = PN_OP_LEN(f->asmb);
PN_ASM2(t->part == AST_AND ? OP_NOTJMP : OP_TESTJMP, reg, 0);
PN_ARG(1, reg);
PN_OP_AT(f->asmb, jmp).b = (PN_OP_LEN(f->asmb) - jmp) - 1;
}
break;
// TODO: this stuff is ugly and repetitive
case AST_MSG:
case AST_QUERY: {
u8 breg = reg;
int arg = (PN_S(t,1) != PN_NIL);
int call = (PN_S(t,2) != PN_NIL || arg);
PN ifconst = -1;
if (t->part == AST_MSG && PN_S(t,0) == PN_if) {
int jmp; breg++;
if (t->a[1]->part == AST_VALUE && !t->a[1]->a[1]) {
ifconst = PN_S(t->a[1], 0);
if (PN_TEST(ifconst)) {
DBG_c("if (true) {block} => block\n");
potion_source_asmb(P, f, loop, 0, t->a[2], reg);
} else {
DBG_c("if (false) {block} => \n");
}
} else {
ifconst = -1;
PN_ARG_TABLE(PN_S(t,1), breg, 0);
jmp = PN_OP_LEN(f->asmb);
PN_ASM2(OP_NOTJMP, breg, 0);
potion_source_asmb(P, f, loop, 0, t->a[2], reg);
PN_OP_AT(f->asmb, jmp).b = (PN_OP_LEN(f->asmb) - jmp) - 1;
}
} else if (t->part == AST_MSG && PN_S(t,0) == PN_elsif) {
int jmp1 = PN_OP_LEN(f->asmb), jmp2; breg++;
// true ifconst: ignore. use only 1st if
if (ifconst != -1 && PN_TEST(ifconst)) {
DBG_c("elsif (...) {block} => [false]\n");
} else if (t->a[1]->part == AST_VALUE && !t->a[1]->a[1]) {
ifconst = PN_S(t->a[1], 0);
if (!PN_TEST(ifconst)) {
DBG_c("elsif (true) {block} => block\n");
potion_source_asmb(P, f, loop, 0, t->a[2], reg);
} else {
DBG_c("elsif (false) {block} =>\n");
}
}
else {
PN_ASM2(OP_TESTJMP, breg, 0);
PN_ARG_TABLE(PN_S(t,1), breg, 0);
jmp2 = PN_OP_LEN(f->asmb);
PN_ASM2(OP_NOTJMP, breg, 0);
potion_source_asmb(P, f, loop, 0, t->a[2], reg);
PN_OP_AT(f->asmb, jmp1).b = (PN_OP_LEN(f->asmb) - jmp1) - 1;
PN_OP_AT(f->asmb, jmp2).b = (PN_OP_LEN(f->asmb) - jmp2) - 1;
}
} else if (t->part == AST_MSG && PN_S(t,0) == PN_else) {
int jmp = PN_OP_LEN(f->asmb); breg++;
if (ifconst != -1) {
if (!PN_TEST(ifconst)) {
DBG_c("else {block} => block [false]\n");
potion_source_asmb(P, f, loop, 0, t->a[2], reg);
} else {
DBG_c("else {block} => [true]\n");
}
}
else {
PN_ASM2(OP_TESTJMP, breg, 0);
potion_source_asmb(P, f, loop, 0, t->a[2], reg);
PN_OP_AT(f->asmb, jmp).b = (PN_OP_LEN(f->asmb) - jmp) - 1;
}
} else if (t->part == AST_MSG && PN_S(t,0) == PN_class) {
u8 breg = reg;
if (count == 0)
PN_ASM1(OP_SELF, reg);
if (PN_S(t,2) != PN_NIL) {
// TODO: a hack to make sure constructors always return self
if (PN_S(PN_S(t,2), 0) == PN_NIL)
(t->a[2])->a[0] = PN_SRC(PN_AST(CODE, PN_NIL));
PN ctor = PN_S(PN_S(t,2), 0);
PN_PUSH(ctor, PN_AST(EXPR, PN_TUP(PN_AST(MSG, potion_str(P, "self")))));
breg++;
PN_BLOCK(breg, PN_S(t,2), PN_S(t,1));
}
PN_ASM2(OP_CLASS, reg, breg);
} else if (t->part == AST_MSG && (PN_S(t,0) == PN_while || PN_S(t,0) == PN_loop)) {
int jmp1 = 0, jmp2 = PN_OP_LEN(f->asmb); breg++;
struct PNLoop l; l.bjmpc = 0; l.cjmpc = 0;
int i;
if (PN_S(t,0) == PN_while) {
// TODO: error if args to `loop`?
PN_ARG_TABLE(PN_S(t,1), breg, 0);
jmp1 = PN_OP_LEN(f->asmb);
PN_ASM2(OP_NOTJMP, breg, 0);
}
potion_source_asmb(P, f, &l, 0, t->a[2], reg);
PN_ASM1(OP_JMP, (jmp2 - PN_OP_LEN(f->asmb)) - 1);
if (PN_S(t,0) == PN_while) {
PN_OP_AT(f->asmb, jmp1).b = (PN_OP_LEN(f->asmb) - jmp1) - 1;
}
for (i = 0; i < l.bjmpc; i++) {
PN_OP_AT(f->asmb, l.bjmps[i]).a = (PN_OP_LEN(f->asmb) - l.bjmps[i]) - 1;
}
for (i = 0; i < l.cjmpc; i++) {
PN_OP_AT(f->asmb, l.cjmps[i]).a = (jmp2 - l.cjmps[i]) - 1;
}
} else if (t->part == AST_MSG && PN_S(t,0) == PN_return) {
PN_ARG_TABLE(PN_S(t,1), reg, 0);
PN_ASM1(OP_RETURN, reg);
} else if (t->part == AST_MSG && PN_S(t,0) == PN_break) {
if (loop != NULL) {
loop->bjmps[loop->bjmpc++] = PN_OP_LEN(f->asmb);
PN_ASM1(OP_JMP, 0);
} else {
// TODO: Report error: 'break' outside of loop.
}
} else if (t->part == AST_MSG && PN_S(t,0) == PN_continue) {
if (loop != NULL) {
loop->cjmps[loop->cjmpc++] = PN_OP_LEN(f->asmb);
PN_ASM1(OP_JMP, 0);
} else {
// TODO: Report error: 'continue' outside of loop.
}
} else if (t->part == AST_MSG && PN_S(t,0) == PN_self) {
PN_ASM1(OP_SELF, reg);
} else {
u8 opcode = OP_GETUPVAL;
PN_SIZE num = PN_NONE;
if (count == 0 && t->part == AST_MSG) {
num = PN_UPVAL(PN_S(t,0));
if (num == PN_NONE) {
num = PN_GET(f->locals, PN_S(t,0));
opcode = OP_GETLOCAL;
}
}
if (num == PN_NONE && PN_S(t,0) != PN_NIL) {
u8 oreg = ++breg;
int jmp = 0;
num = PN_PUT(f->values, PN_S(t,0));
if (count == 0) {
PN_ASM1(OP_SELF, oreg);
} else {
PN_ASM2(OP_MOVE, oreg, reg);
}
PN_ASM2(OP_LOADK, reg, num);
PN_ASM2(PN_S(t,1) != PN_NIL || PN_S(t,2) != PN_NIL ? OP_MSG : OP_BIND, reg, breg);
if (t->part == AST_QUERY && PN_S(t,1) != PN_NIL) {
jmp = PN_OP_LEN(f->asmb);
PN_ASM2(OP_NOTJMP, reg, 0);
}
#define LOAD_ARG() PN_ARG_TABLE(PN_S(t,1), breg, 1)
if (arg) {
u8 part1 = PN_PART(PN_S(t,1));
if (part1 == AST_VALUE || (part1 == AST_LIST && PN_S(t,2) == PN_NIL)) {
LOAD_ARG();
}
}
if (PN_S(t,2) != PN_NIL && (PN_PART(PN_S(t,2)) == AST_PROTO)) {
vPN(Source) t2 = PN_SRC(PN_S(t,2));
breg++;
PN_BLOCK(breg, PN_S(t2,1), PN_S(t2,0));
} else {
if (PN_S(t,1) == PN_NIL && PN_S(t,2) == PN_NIL)
LOAD_ARG();
if (PN_S(t,2) != PN_NIL && PN_PART(PN_S(t,2)) == AST_BLOCK) {
breg++;
PN_BLOCK(breg, PN_S(t,2), PN_S(t,1));
}
}
#undef LOAD_ARG
if (t->part == AST_MSG) {
DBG_c("; call %d %d MSG\n", reg, breg);
PN_ASM2(OP_CALL, reg, breg);
} else
if (PN_S(t,1) != PN_NIL) {
DBG_c("; call %d %d !MSG\n", reg, breg);
PN_ASM2(OP_CALL, reg, breg);
PN_OP_AT(f->asmb, jmp).b = (PN_OP_LEN(f->asmb) - jmp) - 1;
} else {
PN_ASM2(OP_TEST, reg, breg);
}
} else {
if (num != PN_NONE)
PN_ASM2(opcode, reg, num);
if (call) {
PN_ASM1(OP_SELF, ++breg);
PN_ARG_TABLE(PN_S(t,1), breg, 1);
if (PN_S(t,2) != PN_NIL) {
breg++;
PN_BLOCK(breg, PN_S(t,2), PN_NIL);
}
DBG_c("; call %d %d\n", reg, breg);
PN_ASM2(OP_CALL, reg, breg);
}
}
}
PN_REG(f, breg);
}
break;
case AST_PATH:
case AST_PATHQ: {
PN_SIZE num = PN_PUT(f->values, PN_S(t,0));
if (count == 0) {
PN_PUT(f->paths, PN_NUM(num));
PN_ASM1(OP_SELF, reg);
}
PN_ASM2(OP_LOADK, reg + 1, num);
PN_ASM2(OP_GETPATH, reg, reg + 1);
if (t->part == AST_PATHQ)
PN_ASM2(OP_TEST, reg, reg);
PN_REG(f, reg + 1);
}
break;
case AST_LICK: {
u8 breg = reg;
PN_SIZE num = PN_PUT(f->values, PN_S(t,0));
PN_ASM2(OP_LOADK, reg, num);
if (PN_S(t,1) != PN_NIL)
potion_source_asmb(P, f, loop, 0, t->a[1], ++breg);
else if (PN_S(t,2) != PN_NIL)
PN_ASM2(OP_LOADPN, ++breg, PN_NIL);
if (PN_S(t,2) != PN_NIL)
potion_source_asmb(P, f, loop, 0, t->a[2], ++breg);
PN_ASM2(OP_NEWLICK, reg, breg);
PN_REG(f, breg);
}
break;
case AST_LIST:
PN_ASM1(OP_NEWTUPLE, reg);
if (PN_S(t,0) != PN_NIL) {
PN_TUPLE_EACH(PN_S(t,0), i, v, {
if (PN_PART(v) == AST_ASSIGN) {
vPN(Source) lhs = PN_SRC(PN_S(v,0));
if (lhs->part == AST_EXPR && PN_TUPLE_LEN(PN_S(lhs,0)) == 1)
{
lhs = SRC_TUPLE_AT(lhs, 0);
if (lhs->part == AST_MSG) {
PN_SIZE num = PN_PUT(f->values, PN_S(lhs,0));
PN_ASM2(OP_LOADK, reg + 1, num);
lhs = NULL;
}
}
if (lhs != NULL)
potion_source_asmb(P, f, loop, 0, lhs, reg + 1);
potion_source_asmb(P, f, loop, 0, PN_SRC(v)->a[1], reg + 2);
PN_ASM2(OP_SETTABLE, reg, reg + 2);
PN_REG(f, reg + 2);
} else {
potion_source_asmb(P, f, loop, 0, PN_SRC(v), reg + 1);
PN_ASM2(OP_SETTUPLE, reg, reg + 1);
PN_REG(f, reg + 1);
}
});
}
break;
case AST_DEBUG:
if (P->flags & EXEC_DEBUG) {
PN_ASM2(OP_DEBUG, PN_S(t,1), PN_S(t,2));
}
}
}
#define SIG_EXPR_MSG(name,expr) \
if (expr->part == AST_EXPR) { \
vPN(Source) t = SRC_TUPLE_AT(expr, 0); \
if (t->part == AST_MSG) { \
name = PN_S(t,0); \
PN_PUT(f->locals, name); \
sig = PN_PUSH(sig, name); \
} \
} else { name = PN_NIL; }
/** Extract locals, apply type defaults and do type and param checks.
sigs are also ambigious, the parser usually compiles it down to
expression trees, not to a sig tuple. Convert assign,pipe,value.
Name=Type, '|' optional '.' end ':='default
type: 'o' (= PN object)
Encode to AST_CODE: (name type|modifier default)
\param f the PNProto closure to store locals
\param src PNSource signature tree, parsed via yy_sig()
\return PNProto a closure
(x=n,y:=1) =>
\code (list (assign (expr (msg (x)) expr (msg (n ))),
assign (expr (msg (y)) value (1 ))) \endcode */
PN potion_sig_compile(Potion *P, vPN(Proto) f, PN src) {
PN sig = PN_TUP0();
vPN(Source) t = PN_SRC(src);
if (t->part == AST_LIST && PN_S(t,0) != PN_NIL) {
//PN_TUPLE_EACH(PN_S(t,0), i, v, {
struct PNTuple * volatile __tv = ((struct PNTuple *)potion_fwd(PN_S(t,0)));
if (__tv->len != 0) {
DBG_c("--- sig compile ---\n");
PN_SIZE i;
for (i = 0; i < __tv->len; i++) {
struct PNSource *v = PN_SRC(__tv->set[i]);
{
vPN(Source) expr = PN_SRC(v);
PN name = 0;
SIG_EXPR_MSG(name, expr)
if (expr->part == AST_VALUE) {
vPN(Source) lhs = expr->a[0];
PN v = PN_S(lhs,0);
if (PN_IS_STR(v)) {
DBG_c("sig: lhs value, computed name %s\n", AS_STR(v));
PN_PUT(f->locals, v);
sig = PN_PUSH(sig, v);
} else {
potion_syntax_error(P, "in signature: value %s as argument name", AS_STR(v));
}
} else if (expr->part == AST_PIPE) { //x|y => (pipe (expm x) (expm y))
vPN(Source) lhs = expr->a[0];
vPN(Source) rhs = expr->a[1];
PN name2 = 0;
SIG_EXPR_MSG(name, lhs);
sig = PN_PUSH(sig, PN_NUM('|'));
SIG_EXPR_MSG(name2, rhs);
DBG_c("; (%s | %s)\n", AS_STR(name), AS_STR(name2));
} else if (expr->part == AST_ASSIGN) { //x=o => (assign (expm x) (expm o))
vPN(Source) lhs = expr->a[0];
vPN(Source) rhs = expr->a[1];
if (lhs->part == AST_EXPR && PN_TUPLE_LEN(PN_S(lhs,0)) == 1) {
SIG_EXPR_MSG(name, lhs);
lhs = SRC_TUPLE_AT(lhs, 0);
DBG_c("; (%s ", AS_STR(name));
}
else if (lhs->part == AST_PIPE) {
//x|y=o => (assign (pipe (expm x) (expm y)) (expm o))
SIG_EXPR_MSG(name, lhs->a[0]);
sig = PN_PUSH(sig, PN_NUM('|'));
rhs = lhs->a[1];
DBG_c("; (%s | ", AS_STR(name));
} else {
potion_syntax_error(P, "in signature: unexpected AST %s", AS_STR(lhs));
}
if (rhs->part == AST_EXPR && PN_TUPLE_LEN(rhs->a[0]) == 1) {
rhs = SRC_TUPLE_AT(rhs, 0);
if (rhs->part == AST_MSG) {
PN v = PN_NUM(PN_STR_PTR(PN_S(rhs,0))[0]); // = type. TODO: one-char => VTABLE
DBG_c("%s)\n", AS_STR(v));
sig = PN_PUSH(sig, v);
}
} else if (rhs->part == AST_VALUE) { // :=default
PN v = PN_S(rhs,0);
DBG_c(": %s)\n", AS_STR(v));
sig = PN_PUSH(PN_PUSH(sig, PN_NUM(':')), v);
} else {
potion_syntax_error(P, "in signature: unexpected AST %s", AS_STR(expr));
}
}
}}}
}
return sig;
}
#undef SRC_TUPLE_AT
#undef SIG_EXPR_MSG
///\memberof PNSource
/// "compile" method for PNSource, an AST fragment. Typically to add a function definition,
/// but also objects, blocks, ... (Almost everything is a PNClosure)
///\param source PNSource AST source tree
///\param sig PNSource signature tree or PN_NIL, parsed via yy_sig(), compiled with potion_sig_compile()
///\return PNProto a closure
PN potion_source_compile(Potion *P, PN cl, PN self, PN source, PN sig) {
vPN(Proto) f;
vPN(Source) t = (struct PNSource *)self;
switch (t->part) {
case AST_CODE:
case AST_BLOCK: break;
default: return PN_NIL; // TODO: error
}
f = PN_ALLOC(PN_TPROTO, struct PNProto);
f->source = source;
f->stack = PN_NUM(1);
f->protos = PN_TUP0();
f->paths = PN_TUP0();
f->locals = PN_TUP0();
f->upvals = PN_TUP0();
f->values = PN_TUP0();
f->tree = self;
DBG_c("-- compile --\n");
f->sig = (sig == PN_NIL ? PN_TUP0() : potion_sig_compile(P, f, sig));
f->asmb = (PN)potion_asm_new(P);
//DBG_c("--- compile ---\n");
potion_source_asmb(P, f, NULL, 0, t, 0);
PN_ASM1(OP_RETURN, 0);
f->localsize = PN_TUPLE_LEN(f->locals);
f->upvalsize = PN_TUPLE_LEN(f->upvals);
f->pathsize = PN_TUPLE_LEN(f->paths);
return (PN)f;
}
#define READ_U8(ptr) potion_read_u8(ptr)
inline u8 potion_read_u8(u8 * ptr){
u8 rpu = *ptr; ptr += sizeof(u8); return rpu;
}
#define READ_PN(pn, ptr) potion_read_pn(pn, (PN *)ptr)
inline PN potion_read_pn(PN pn, PN * ptr) {
PN rpn = *ptr; ptr += pn; return rpn;
}
#define READ_CONST(pn, ptr) potion_read_const(P, pn, ptr)
inline PN potion_read_const(Potion *P, PN pn, u8 *ptr) {
PN val = READ_PN(pn, ptr);
if (PN_IS_PTR(val)) {
if (val & 2) {
size_t len = ((val ^ 2) >> 4) - 1;
val = potion_decimal(P, (char *)ptr, len);
ptr += len;
} else {
size_t len = (val >> 4) - 1;
val = PN_STRN((char *)ptr, len);
ptr += len;
}
}
return val;
}
#define READ_TUPLE(ptr) \
long i = 0, count = READ_U8(ptr); \
PN tup = potion_tuple_with_size(P, (PN_SIZE)count); \
for (; i < count; i++)
#define READ_VALUES(pn, ptr) potion_read_values(P, pn, ptr)
inline PN potion_read_values(Potion *P, u8 pn, u8 *ptr) {
READ_TUPLE(ptr) PN_TUPLE_AT(tup, i) = READ_CONST(pn, ptr);
return tup;
}
#define READ_PROTOS(pn, ptr) potion_read_protos(P, f, pn, ptr)
inline PN potion_read_protos(Potion *P, struct PNProto * volatile f, u8 pn, u8 *ptr) {
READ_TUPLE(ptr) PN_TUPLE_AT(tup, i) = potion_proto_load(P, (PN)f, pn, &(ptr));
return tup;
}
// TODO: this byte string is volatile, need to avoid using ptr
PN potion_proto_load(Potion *P, PN up, u8 pn, u8 **ptr) {
PN len = 0;
PNAsm * volatile asmb = NULL;
vPN(Proto) f = PN_ALLOC(PN_TPROTO, struct PNProto);
f->source = READ_CONST(pn, *ptr);
if (f->source == PN_NIL) f->source = up;
f->sig = READ_VALUES(pn, *ptr);
f->stack = READ_CONST(pn, *ptr);
f->values = READ_VALUES(pn, *ptr);
f->paths = READ_VALUES(pn, *ptr);
f->locals = READ_VALUES(pn, *ptr);
f->upvals = READ_VALUES(pn, *ptr);
f->protos = READ_PROTOS(pn, *ptr);
len = READ_PN(pn, *ptr);
PN_FLEX_NEW(asmb, PN_TBYTES, PNAsm, len);
PN_MEMCPY_N(asmb->ptr, *ptr, u8, len);
asmb->len = len;
f->asmb = (PN)asmb;
f->localsize = PN_TUPLE_LEN(f->locals);
f->upvalsize = PN_TUPLE_LEN(f->upvals);
f->pathsize = PN_TUPLE_LEN(f->paths);
*ptr += len;
return (PN)f;
}
///\memberof PNSource
// TODO: "load" from a stream
PN potion_source_load(Potion *P, PN cl, PN buf) {
u8 *ptr;
vPN(BHeader) h = (struct PNBHeader *)PN_STR_PTR(buf);
// check for compiled binary first
if ((size_t)PN_STR_LEN(buf) <= sizeof(struct PNBHeader) ||
strncmp((char *)h->sig, POTION_SIG, 4) != 0)
return PN_NIL;
ptr = h->proto;
return potion_proto_load(P, PN_NIL, h->pn, &ptr);
}
// TODO: switch to dump methods
#define WRITE_U8(un, ptr) (*ptr = (u8)un, ptr += sizeof(u8))