-
Notifications
You must be signed in to change notification settings - Fork 56
/
compiler_new.c
1354 lines (1163 loc) · 40.9 KB
/
compiler_new.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 "minilisp.h"
#include "reader.h"
#include "writer.h"
#include "alloc.h"
#include "compiler_new.h"
#include "stream.h"
#include "utf8.c"
#define env_t StrMap
static env_t* global_env = NULL;
//#define CHECK_BOUNDS // enforce boundaries of array put/get
#define ARG_SPILLOVER 3 // max 4 args (0-3) via regs, rest via stack
#define LBDREG R4 // register base used for passing args to functions
static int debug_mode = 0;
env_entry* lookup_global_symbol(char* name) {
env_entry* res;
int found = sm_get(global_env, name, (void**)&res);
//printf("[lookup] %s res: %p\n",name,res);
if (!found) return NULL;
return res;
}
Cell* insert_symbol(Cell* symbol, Cell* cell, env_t** env) {
env_entry* e;
int found = sm_get(*env, symbol->addr, (void**)&e);
//printf("sm_get res: %d\r\n",found);
if (found) {
e->cell = cell;
//printf("[insert_symbol] update %s entry at %p (cell: %p value: %d)\r\n",symbol->addr,e,e->cell,e->cell->value);
return e->cell;
}
e = malloc(sizeof(env_entry));
memcpy(e->name, (char*)symbol->addr, symbol->size);
e->cell = cell;
//printf("[insert_symbol] %s entry at %p (cell: %p)\r\n",symbol->addr,e,e->cell);
sm_put(*env, e->name, e);
return e->cell;
}
Cell* insert_global_symbol(Cell* symbol, Cell* cell) {
return insert_symbol(symbol, cell, &global_env);
}
#define TMP_PRINT_BUFSZ 1024
static FILE* jit_out;
static Cell* cell_heap_start;
static int label_skip_count = 0;
static char temp_print_buffer[TMP_PRINT_BUFSZ];
static Cell* consed_type_error;
static Cell* reusable_type_error;
static Cell* reusable_nil;
#ifdef CPU_ARM
#include "jit_arm_raw.c"
#define PTRSZ 4
#endif
#ifdef CPU_X64
#include "jit_x64.c"
#define PTRSZ 8
#endif
Cell* lisp_print(Cell* arg) {
lisp_write(arg, temp_print_buffer, TMP_PRINT_BUFSZ);
printf("%s\r\n",temp_print_buffer);
return arg;
}
void load_int(int dreg, Arg arg, Frame* f) {
if (arg.type == ARGT_CONST) {
// argument is a constant like 123, "foo"
jit_movi(dreg, (jit_word_t)arg.cell->value);
}
else if (arg.type == ARGT_CELL) {
if (arg.cell == NULL) {
// not sure what this is
//if (dreg!=R0) jit_movr(dreg, R0);
if (dreg!=R1+arg.slot) {
jit_movr(dreg, R1+arg.slot); // FIXME: really true?
}
jit_ldr(dreg);
} else {
// argument is a cell pointer
jit_lea(dreg, arg.cell);
jit_ldr(dreg);
}
}
else if (arg.type == ARGT_ENV) {
// argument is an environment table entry, load e->cell->value
jit_lea(dreg, arg.env);
jit_ldr(dreg);
jit_ldr(dreg);
}
else if (arg.type == ARGT_REG) {
// argument comes from a register
jit_movr(dreg, LBDREG+arg.slot);
jit_ldr(dreg);
}
else if (arg.type == ARGT_INT) {
if (dreg!=R1+arg.slot) {
jit_movr(dreg, R1+arg.slot); // FIXME: really true?
}
}
else if (arg.type == ARGT_STACK) {
jit_ldr_stack(dreg, PTRSZ*(arg.slot+f->sp));
jit_ldr(dreg);
}
else if (arg.type == ARGT_STACK_INT) {
jit_ldr_stack(dreg, PTRSZ*(arg.slot+f->sp));
}
else {
jit_movi(dreg, 0xdeadbeef);
}
}
void load_cell(int dreg, Arg arg, Frame* f) {
if (arg.type == ARGT_CELL || arg.type == ARGT_CONST) {
if (arg.cell == NULL) {
// not sure what this is
jit_movr(dreg, R1+arg.slot); // FIXME: really true?
} else {
// argument is a cell pointer
jit_lea(dreg, arg.cell);
}
}
else if (arg.type == ARGT_ENV) {
jit_lea(dreg, arg.env);
jit_ldr(dreg);
}
else if (arg.type == ARGT_REG) {
jit_movr(dreg, LBDREG+arg.slot);
}
else if (arg.type == ARGT_STACK) {
jit_ldr_stack(dreg, PTRSZ*(arg.slot+f->sp));
}
else if (arg.type == ARGT_STACK_INT) {
// FIXME possible ARGR0 clobbering
int adjust = 0;
if (dreg!=ARGR0) {jit_push(ARGR0,ARGR0); adjust++;}
if (dreg!=R0) {jit_push(R0,R0); adjust++;}
jit_ldr_stack(ARGR0, PTRSZ*(arg.slot+f->sp+adjust));
jit_call(alloc_int, "alloc_int");
jit_movr(dreg,R0);
if (dreg!=R0) jit_pop(R0,R0);
if (dreg!=ARGR0) jit_pop(ARGR0,ARGR0);
}
else {
jit_movi(dreg, 0xdeadcafe);
}
}
int get_sym_frame_idx(char* argname, Arg* fn_frame, int ignore_regs) {
if (!fn_frame) return -1;
for (int i=0; i<MAXFRAME; i++) {
if (fn_frame[i].name) {
//printf("<< get_sym_frame_idx %i (type %d, reg = %d, looking for %s): %s\n",i,fn_frame[i].type,ARGT_REG,argname,fn_frame[i].name);
if (!((fn_frame[i].type == ARGT_REG) && ignore_regs)) {
if (!strcmp(argname, fn_frame[i].name)) {
//printf("!! get_sym_frame_idx %i (type %d): %s\n",i,fn_frame[i].type,fn_frame[i].name);
//printf("returning %d\n",i);
return i;
}
}
}
}
return -1;
}
// TODO: optimize!
int push_frame_regs(Arg* fn_frame) {
if (!fn_frame) return 0;
int pushreg=0;
int pushstack=0;
for (int i=0; i<MAXFRAME; i++) {
if (fn_frame[i].type == ARGT_REG) {
pushreg++;
}
}
//printf("pushing %d frame regs\n",pushreg);
if (pushreg) {
jit_push(LBDREG,LBDREG+pushreg-1);
}
return pushreg;
}
int pop_frame_regs(Arg* fn_frame) {
if (!fn_frame) return 0;
int pushreg=0;
int pushstack=0;
for (int i=0; i<MAXFRAME; i++) {
if (fn_frame[i].type == ARGT_REG) {
pushreg++;
}
}
//printf("popping %d frame regs\n",pushreg);
if (pushreg) {
jit_pop(LBDREG,LBDREG+pushreg-1);
}
return pushreg;
}
static char* analyze_buffer[MAXFRAME];
int analyze_fn(Cell* expr, Cell* parent, int num_lets) {
if (expr->tag == TAG_SYM) {
env_entry* op_env = lookup_global_symbol(expr->addr);
if (op_env) {
Cell* op = op_env->cell;
if (op->tag == TAG_BUILTIN) {
//printf("analyze_fn: found builtin: %s\n",expr->addr);
if (op->value == BUILTIN_LET) {
Cell* sym = car(cdr(parent));
if (sym) {
int existing = 0;
for (int i=0; i<num_lets; i++) {
if (!strcmp(analyze_buffer[i], sym->addr)) {
//printf("-- we already know local %s\r\n",sym->addr);
existing = 1;
break;
}
}
if (!existing) {
analyze_buffer[num_lets] = sym->addr;
num_lets++;
}
} else {
printf("!! analyze error: malformed let!\r\n");
}
}
}
}
}
else if (expr->tag == TAG_CONS) {
if (car(expr)) {
num_lets = analyze_fn(car(expr), expr, num_lets);
}
if (cdr(expr)) {
num_lets = analyze_fn(cdr(expr), expr, num_lets);
}
}
return num_lets;
}
int compile_expr(Cell* expr, Frame* frame, int return_type) {
if (!expr) return 0;
if (!frame) return 0;
int compiled_type = TAG_ANY;
Arg* fn_frame = frame->f;
if (expr->tag != TAG_CONS) {
if (expr->tag == TAG_SYM) {
int arg_frame_idx = get_sym_frame_idx(expr->addr, fn_frame, 0);
if (arg_frame_idx>=0) {
load_cell(R0, fn_frame[arg_frame_idx], frame);
return compiled_type;
}
env_entry* env = lookup_global_symbol(expr->addr);
if (env) {
Cell* value = env->cell;
jit_movi(R0,(jit_word_t)env);
jit_ldr(R0);
return value->tag; // FIXME TODO forbid later type change
} else {
printf("undefined symbol %s\n",expr->addr);
jit_movi(R0,0);
return 0;
}
} else {
// return the expr
jit_movi(R0,(jit_word_t)expr);
return compiled_type;
}
return 0;
}
cell_heap_start = get_cell_heap();
Cell* opsym = car(expr);
Cell* args = cdr(expr);
Cell* orig_args = args; // keep around for specials forms like DO
Cell* signature_args = NULL;
if (!opsym || opsym->tag != TAG_SYM) {
printf("[compile_expr] error: non-symbol in operator position.\n");
return 0;
}
env_entry* op_env = lookup_global_symbol(opsym->addr);
if (!op_env || !op_env->cell) {
printf("[compile_expr] error: undefined symbol %s in operator position.\n",opsym->addr);
return 0;
}
Cell* op = op_env->cell;
int is_let = 0;
//printf("op tag: %d\n",op->tag);
if (op->tag == TAG_BUILTIN) {
signature_args = op->next;
if (op->value == BUILTIN_LET) {
is_let = 1;
}
} else if (op->tag == TAG_LAMBDA) {
signature_args = car((Cell*)(op->addr));
} else {
printf("[compile-expr] error: non-lambda symbol %s in operator position.\n",opsym->addr);
return 0;
}
//printf("[op] %s\n",debug_buf);
//lisp_write(signature_args, debug_buf, sizeof(debug_buf));
//printf("[sig] %s\n",debug_buf);
if (debug_mode) {
push_frame_regs(frame->f);
char* debug_buf = malloc(256);
lisp_write(expr, debug_buf, 256);
jit_push(R0, ARGR1);
jit_lea(ARGR0, debug_buf);
jit_lea(ARGR1, frame);
jit_call(debug_handler,"dbg");
jit_pop(R0, ARGR1);
pop_frame_regs(frame->f);
}
// first, we need a signature
int argi = 0;
Arg argdefs[MAXARGS];
do {
Cell* arg = car(args);
Cell* signature_arg = car(signature_args);
char arg_name[32];
snprintf(arg_name,sizeof(arg_name),"a%d",argi+1,arg_name,10);
// 1. is the arg the required type? i.e. a pointer or a number?
if (signature_arg && signature_arg->tag == TAG_CONS) {
// named argument
snprintf(arg_name,sizeof(arg_name),car(signature_arg)->addr);
signature_arg = cdr(signature_arg);
}
if (arg && (!signature_args || signature_arg)) {
int given_tag = arg->tag;
if (is_let && argi==1) {
int type_hint = -1;
// check the symbol to see if we already have type information
int fidx = get_sym_frame_idx(argdefs[0].cell->addr, fn_frame, 1);
if (fidx>=0) {
//printf("existing type information for %s: %d\r\n", argdefs[0].cell->addr,fn_frame[fidx].type);
type_hint = fn_frame[fidx].type;
}
if (given_tag == TAG_INT || type_hint == ARGT_STACK_INT) {
//printf("INT mode of let\r\n");
// let prefers raw integers!
signature_arg->value = TAG_INT;
} else {
//printf("ANY mode of let\r\n");
// but cells are ok, too
signature_arg->value = TAG_ANY;
}
}
if (!signature_args) {
// any number of arguments allowed
argdefs[argi].cell = arg;
argdefs[argi].type = ARGT_CELL;
}
else if (signature_arg->value == TAG_LAMBDA) {
// lazy evaluation by form
argdefs[argi].cell = arg;
argdefs[argi].type = ARGT_LAMBDA;
}
else if (arg->tag == TAG_CONS) {
// eager evaluation
// nested expression
if (argi>0) {
// save registers
// FIXME RETHINK
jit_push(R1,R1+argi-1);
frame->sp+=(1+argi-1);
}
given_tag = compile_expr(arg, frame, signature_arg->value);
if (given_tag<1) return given_tag; // failure
argdefs[argi].cell = NULL; // cell is in R0 at runtime
argdefs[argi].slot = argi;
if (given_tag == TAG_INT) {
argdefs[argi].type = ARGT_INT;
jit_movr(R1+argi,ARGR0);
} else {
argdefs[argi].type = ARGT_CELL;
jit_movr(R1+argi,R0);
}
if (argi>0) {
jit_pop(R1,R1+argi-1);
frame->sp-=(1+argi-1);
}
}
else if (given_tag == TAG_SYM && signature_arg->value != TAG_SYM) {
// symbol given, lookup (indirect)
//printf("indirect symbol lookup (name: %p)\n",arg->value);
int arg_frame_idx = get_sym_frame_idx(arg->addr, fn_frame, 0);
// argument passed to function in register
if (arg_frame_idx>=0) {
argdefs[argi] = fn_frame[arg_frame_idx];
//printf("argument %s from stack frame.\n", arg->addr);
} else {
argdefs[argi].env = lookup_global_symbol((char*)arg->addr);
argdefs[argi].type = ARGT_ENV;
//printf("argument %s from environment.\n", arg->addr);
}
//printf("arg_frame_idx: %d\n",arg_frame_idx);
if (!argdefs[argi].env && arg_frame_idx<0) {
printf("undefined symbol %s given for argument %s.\n",arg->addr,arg_name);
return 0;
}
}
else if (given_tag == signature_arg->value || signature_arg->value==TAG_ANY) {
argdefs[argi].cell = arg;
argdefs[argi].slot = argi-1;
argdefs[argi].type = ARGT_CELL;
if (given_tag == TAG_SYM || given_tag == TAG_CONS || given_tag == TAG_INT || given_tag == TAG_STR || given_tag == TAG_BYTES) {
argdefs[argi].type = ARGT_CONST;
//printf("const arg of type %d at %p\n",arg->tag,arg);
}
} else {
// check if we can typecast
// else, fail with type error
printf("!! type mismatch for argument %s (given %s, expected %s)!\n",arg_name,tag_to_str(given_tag),tag_to_str(signature_arg->value));
return 0;
}
} else {
if (!arg && signature_arg) {
// missing arguments
printf("!! argument %s missing!\n",arg_name);
return 0;
} else if (arg && !signature_arg) {
// surplus arguments
printf("!! surplus arguments!\n");
return 0;
}
}
argi++;
} while (argi<MAXARGS && (args = cdr(args)) && (!signature_args || (signature_args = cdr(signature_args))));
if (op->tag == TAG_BUILTIN) {
switch (op->value) {
case BUILTIN_BITAND: {
load_int(ARGR0,argdefs[0], frame);
load_int(R2,argdefs[1], frame);
jit_andr(ARGR0,R2);
if (return_type == TAG_ANY) jit_call(alloc_int, "alloc_int");
else compiled_type = TAG_INT;
break;
}
case BUILTIN_BITOR: {
load_int(ARGR0,argdefs[0], frame);
load_int(R2,argdefs[1], frame);
jit_orr(ARGR0,R2);
if (return_type == TAG_ANY) jit_call(alloc_int, "alloc_int");
else compiled_type = TAG_INT;
break;
}
case BUILTIN_BITXOR: {
load_int(ARGR0,argdefs[0], frame);
load_int(R2,argdefs[1], frame);
jit_xorr(ARGR0,R2);
if (return_type == TAG_ANY) jit_call(alloc_int, "alloc_int");
else compiled_type = TAG_INT;
break;
}
case BUILTIN_SHL: {
load_int(ARGR0,argdefs[0], frame);
load_int(R2,argdefs[1], frame);
jit_shlr(ARGR0,R2);
if (return_type == TAG_ANY) jit_call(alloc_int, "alloc_int");
else compiled_type = TAG_INT;
break;
}
case BUILTIN_SHR: {
load_int(ARGR0,argdefs[0], frame);
load_int(R2,argdefs[1], frame);
jit_shrr(ARGR0,R2);
if (return_type == TAG_ANY) jit_call(alloc_int, "alloc_int");
else compiled_type = TAG_INT;
break;
}
case BUILTIN_ADD: {
load_int(ARGR0,argdefs[0], frame);
load_int(R2,argdefs[1], frame);
jit_addr(ARGR0,R2);
if (return_type == TAG_ANY) jit_call(alloc_int, "alloc_int");
else compiled_type = TAG_INT;
break;
}
case BUILTIN_SUB: {
load_int(ARGR0,argdefs[0], frame);
load_int(R2,argdefs[1], frame);
jit_subr(ARGR0,R2);
if (return_type == TAG_ANY) jit_call(alloc_int, "alloc_int");
else compiled_type = TAG_INT;
break;
}
case BUILTIN_MUL: {
load_int(ARGR0,argdefs[0], frame);
load_int(R2,argdefs[1], frame);
jit_mulr(ARGR0,R2);
if (return_type == TAG_ANY) jit_call(alloc_int, "alloc_int");
else compiled_type = TAG_INT;
break;
}
case BUILTIN_DIV: {
load_int(ARGR0,argdefs[0], frame);
load_int(R2,argdefs[1], frame);
jit_divr(ARGR0,R2);
if (return_type == TAG_ANY) jit_call(alloc_int, "alloc_int");
else compiled_type = TAG_INT;
break;
}
case BUILTIN_MOD: {
load_int(ARGR0,argdefs[0], frame);
load_int(R2,argdefs[1], frame);
jit_modr(ARGR0,R2);
if (return_type == TAG_ANY) jit_call(alloc_int, "alloc_int");
else compiled_type = TAG_INT;
break;
}
case BUILTIN_GT: {
load_int(ARGR0,argdefs[0], frame);
load_int(R2,argdefs[1], frame);
jit_subr(R2,ARGR0);
jit_movi(ARGR0,0);
jit_movi(R2,1);
jit_movneg(ARGR0,R2);
if (return_type == TAG_ANY) jit_call(alloc_int, "alloc_int");
else compiled_type = TAG_INT;
break;
}
case BUILTIN_LT: {
load_int(ARGR0,argdefs[0], frame);
load_int(R2,argdefs[1], frame);
jit_subr(ARGR0,R2);
jit_movi(ARGR0,0);
jit_movi(R2,1);
jit_movneg(ARGR0,R2);
if (return_type == TAG_ANY) jit_call(alloc_int, "alloc_int");
else compiled_type = TAG_INT;
break;
}
case BUILTIN_DEF: {
// TODO in the future, we could pre-allocate symbols
// and especially their types based on type inference
jit_lea(ARGR0,argdefs[0].cell); // load symbol address
load_cell(ARGR1,argdefs[1],frame);
push_frame_regs(frame->f);
jit_call(insert_global_symbol, "insert_global_symbol");
pop_frame_regs(frame->f);
break;
}
case BUILTIN_LET: {
if (!frame->f) {
printf("<error: let is not allowed on global level, only in fn>\r\n");
return 0;
}
int is_int = 0;
int offset = MAXARGS + frame->locals;
int fidx = get_sym_frame_idx(argdefs[0].cell->addr, fn_frame, 0);
// el cheapo type inference
if (1 &&
(argdefs[1].type == ARGT_INT ||
argdefs[1].type == ARGT_STACK_INT ||
(argdefs[1].type == ARGT_CONST && argdefs[1].cell->tag == TAG_INT) ||
(fidx>=0 && fn_frame[fidx].type == ARGT_STACK_INT) // already defined as int TODO: error on attempted type change
)) {
load_int(R0, argdefs[1], frame);
is_int = 1;
compiled_type = TAG_INT;
} else {
load_cell(R0, argdefs[1], frame);
compiled_type = TAG_ANY;
}
int is_reg = 0;
if (fidx >= 0) {
// existing stack entry
offset = fidx;
//printf("+~ frame entry %s, existing stack-local idx %d\n",fn_frame[offset].name,fn_frame[offset].slot);
if (fn_frame[offset].type == ARGT_REG) {
is_reg = 1;
}
} else {
fn_frame[offset].name = argdefs[0].cell->addr;
fn_frame[offset].cell = NULL;
if (is_int) {
fn_frame[offset].type = ARGT_STACK_INT;
} else {
fn_frame[offset].type = ARGT_STACK;
}
fn_frame[offset].slot = frame->locals;
//printf("++ frame entry %s, new stack-local idx %d, is_int %d\n",fn_frame[offset].name,fn_frame[offset].slot,is_int);
frame->locals++;
}
if (!is_reg) {
jit_str_stack(R0,PTRSZ*(fn_frame[offset].slot+frame->sp));
}
if (compiled_type == TAG_INT && return_type == TAG_ANY) {
jit_movr(ARGR0,R0);
jit_call(alloc_int, "alloc_int");
compiled_type = TAG_ANY;
}
if (is_reg) {
jit_movr(LBDREG + fn_frame[offset].slot, R0);
printf("let %s to reg: %d\r\n",fn_frame[offset].name, LBDREG + fn_frame[offset].slot);
}
break;
}
case BUILTIN_FN: {
if (argi<2) {
printf("error: trying to define fn without body.\n");
return 0;
}
// body
Cell* fn_body = argdefs[argi-2].cell;
// estimate stack space for locals
int num_lets = analyze_fn(fn_body,NULL,0);
// scan args (build signature)
Cell* fn_args = alloc_nil();
Arg fn_new_frame[MAXFRAME];
for (int i=0; i<MAXFRAME; i++) {
fn_new_frame[i].type = 0;
fn_new_frame[i].slot = -1;
fn_new_frame[i].name = NULL;
}
int spo_count = 0;
int fn_argc = 0;
for (int j=argi-3; j>=0; j--) {
Cell* arg = alloc_cons(alloc_sym(argdefs[j].cell->addr),alloc_int(TAG_ANY));
fn_args = alloc_cons(arg,fn_args);
if (j>=ARG_SPILLOVER) { // max args passed in registers
fn_new_frame[j].type = ARGT_STACK;
fn_new_frame[j].slot = num_lets + 2 + ((argi-3)-j);
spo_count++;
}
else {
fn_new_frame[j].type = ARGT_REG;
fn_new_frame[j].slot = j;
}
fn_new_frame[j].name = argdefs[j].cell->addr;
fn_argc++;
//printf("arg j %d: %s\r\n",j,fn_new_frame[j].name);
}
//char sig_debug[128];
//lisp_write(fn_args, sig_debug, sizeof(sig_debug));
//printf("signature: %s\n",sig_debug);
//lisp_write(fn_body, sig_debug, sizeof(sig_debug));
Cell* lambda = alloc_lambda(alloc_cons(fn_args,fn_body));
lambda->next = 0;
char label_fn[64];
char label_fe[64];
sprintf(label_fn,"f0_%p",lambda);
sprintf(label_fe,"f1_%p",lambda);
jit_jmp(label_fe);
jit_label(label_fn);
jit_movi(R2,(jit_word_t)lambda|STACK_FRAME_MARKER);
jit_push(R2,R2);
jit_dec_stack(num_lets*PTRSZ);
Frame* nframe_ptr;
Frame nframe = {fn_new_frame, 0, 0, frame->stack_end};
if (debug_mode) {
// in debug mode, we need a copy of the frame definition at runtime
nframe_ptr = malloc(sizeof(Frame));
memcpy(nframe_ptr, &nframe, sizeof(Frame));
Arg* nargs_ptr = malloc(sizeof(Arg)*MAXFRAME);
memcpy(nargs_ptr, nframe.f, sizeof(Arg)*MAXFRAME);
nframe_ptr->f = nargs_ptr;
printf("frame copied: %p args: %p\r\n",nframe_ptr,nframe_ptr->f);
} else {
nframe_ptr = &nframe;
}
int tag = compile_expr(fn_body, nframe_ptr, TAG_ANY); // new frame, fresh sp
if (!tag) return 0;
//printf(">> fn has %d args and %d locals. predicted locals: %d\r\n",fn_argc,nframe.locals,num_lets);
jit_inc_stack(num_lets*PTRSZ);
jit_inc_stack(PTRSZ);
jit_ret();
jit_label(label_fe);
jit_lea(R0,lambda);
#ifdef CPU_ARM
Label* fn_lbl = find_label(label_fn);
//printf("fn_lbl idx: %d code: %p\r\n",fn_lbl->idx,code);
lambda->next = code + fn_lbl->idx;
//printf("fn_lbl next: %p\r\n",lambda->next);
#endif
break;
}
case BUILTIN_IF: {
// load the condition
load_int(R0, argdefs[0], frame);
char label_skip[64];
sprintf(label_skip,"else_%d",++label_skip_count);
// compare to zero
jit_cmpi(R0,0);
jit_je(label_skip);
int tag = compile_expr(argdefs[1].cell, frame, return_type);
if (!tag) return 0;
// else?
if (argdefs[2].cell) {
char label_end[64];
sprintf(label_end,"endif_%d",++label_skip_count);
jit_jmp(label_end);
jit_label(label_skip);
tag = compile_expr(argdefs[2].cell, frame, return_type);
if (!tag) return 0;
jit_label(label_end);
} else {
jit_label(label_skip);
}
break;
}
case BUILTIN_WHILE: {
// load the condition
char label_loop[64];
sprintf(label_loop,"loop_%d",++label_skip_count);
char label_skip[64];
sprintf(label_skip,"skip_%d",label_skip_count);
jit_label(label_loop);
int compiled_type = compile_expr(argdefs[0].cell, frame, TAG_INT);
if (!compiled_type) return 0;
if (compiled_type != TAG_INT) {
jit_ldr(R0);
jit_cmpi(R0,0);
} else {
jit_cmpi(ARGR0,0);
}
//load_int(R1,argdefs[0]);
// compare to zero
jit_je(label_skip);
int tag = compile_expr(argdefs[1].cell, frame, return_type);
if (!tag) return 0;
jit_jmp(label_loop);
jit_label(label_skip);
break;
}
case BUILTIN_DO: {
args = orig_args;
Cell* arg;
if (!car(args)) {
printf("<empty (do) not allowed>\r\n");
return 0;
}
while ((arg = car(args))) {
int tag;
if (car(cdr(args))) {
// discard all returns except for the last one
tag = compile_expr(arg, frame, TAG_VOID);
} else {
tag = compile_expr(arg, frame, return_type);
}
if (!tag) return 0;
args = cdr(args);
}
break;
}
case BUILTIN_LIST: {
args = orig_args;
Cell* arg;
int n = 0;
while ((arg = car(args))) {
int tag = compile_expr(arg, frame, TAG_ANY);
if (!tag) return 0;
jit_push(R0,R0);
frame->sp++;
args = cdr(args);
n++;
}
jit_call(alloc_nil, "list:alloc_nil");
jit_movr(ARGR1,R0);
for (int i=0; i<n; i++) {
jit_pop(ARGR0,ARGR0);
frame->sp--;
jit_call(alloc_cons, "list:alloc_cons");
jit_movr(ARGR1,R0);
}
break; // FIXME
}
case BUILTIN_QUOTE: {
args = orig_args;
if (!car(args)) {
printf("<empty (quote) not allowed>\r\n");
return 0;
}
Cell* arg = car(args);
jit_lea(R0,arg);
break;
}
case BUILTIN_CAR: {
load_cell(R0,argdefs[0], frame);
// type check -------------------
jit_movr(R1,R0);
jit_addi(R1,2*PTRSZ);
jit_ldr(R1);
jit_lea(R2,consed_type_error);
jit_cmpi(R1,TAG_CONS);
jit_movne(R0,R2);
// ------------------------------
jit_ldr(R0);
jit_lea(R2,reusable_nil);
jit_cmpi(R0,0); // check for null cell
jit_moveq(R0,R2);
break;
}
case BUILTIN_CDR: {
load_cell(R0,argdefs[0], frame);
jit_addi(R0,PTRSZ);
// type check -------------------
jit_movr(R1,R0);
jit_addi(R1,PTRSZ); // because already added PTRSZ
jit_ldr(R1);
jit_lea(R2,consed_type_error);
jit_cmpi(R1,TAG_CONS);
jit_movne(R0,R2);
// ------------------------------
jit_ldr(R0);
jit_lea(R2,reusable_nil);
jit_cmpi(R0,0); // check for null cell
jit_moveq(R0,R2);
break;
}
case BUILTIN_CONS: {
load_cell(ARGR0,argdefs[0], frame);
load_cell(ARGR1,argdefs[1], frame);
jit_call(alloc_cons,"alloc_cons");
break;
}
case BUILTIN_CONCAT: {
load_cell(ARGR0,argdefs[0], frame);
load_cell(ARGR1,argdefs[1], frame);
jit_call(alloc_concat,"alloc_concat");
break;
}
case BUILTIN_SUBSTR: {
load_cell(ARGR0,argdefs[0], frame);
load_int(ARGR1,argdefs[1], frame);
load_int(ARGR2,argdefs[2], frame);
jit_call(alloc_substr,"alloc_substr");
break;
}
case BUILTIN_GET: {
load_cell(R1,argdefs[0], frame);
load_int(R2,argdefs[1], frame); // offset -> R2
char label_skip[64];
sprintf(label_skip,"skip_%d",++label_skip_count);
char label_ok[64];
sprintf(label_ok,"ok_%d",label_skip_count);
// init r3
jit_movi(R3, 0);
// todo: compile-time checking would be much more awesome
// type check
jit_addi(R1,2*PTRSZ);
jit_ldr(R1);
jit_cmpi(R1,TAG_BYTES); // todo: better perf with mask?
jit_je(label_ok);
jit_cmpi(R1,TAG_STR);
jit_je(label_ok);
// wrong type
jit_jmp(label_skip);
// good type
jit_label(label_ok);
load_cell(R0,argdefs[0], frame);
#ifdef CHECK_BOUNDS
// bounds check -----
jit_movr(R1,R0);
jit_addi(R1,PTRSZ);
jit_ldr(R1);
jit_cmpr(R2,R1);
jit_jge(label_skip);
// -------------------
#endif
jit_movr(R1,R0);
jit_ldr(R1); // string address
jit_addr(R1,R2);
jit_ldrb(R1); // data in r3
jit_label(label_skip);
jit_movr(ARGR0, R3);
if (return_type == TAG_ANY) jit_call(alloc_int, "alloc_int");
else compiled_type = TAG_INT;
break;
}
case BUILTIN_PUT: {
char label_skip[64];
sprintf(label_skip,"skip_%d",++label_skip_count);
load_int(R3,argdefs[2], frame); // byte to store -> R3
load_int(R2,argdefs[1], frame); // offset -> R2
load_cell(R0,argdefs[0], frame);
#ifdef CHECK_BOUNDS
// bounds check -----
jit_movr(R1,R0);
jit_addi(R1,PTRSZ);
jit_ldr(R1);
jit_cmpr(R2,R1);