aboutsummaryrefslogtreecommitdiff
path: root/src/lower.c
blob: 5e0e41e62c11f5e988c1a7d571c2fcee167babc1 (plain) (blame)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>

#include <posthaste/lower.h>
#include <posthaste/scope.h>
#include <posthaste/utils.h>
#include <posthaste/date.h>

/* The bytecode is similar in construction to Lua's, as in there's two arrays, one
 * for global values and one for local values. The local value array is referred
 * to as the stack. In theory each function/procedure could get its own stack,
 * but in this case there's just a single global stack, and each call moves a
 * stack pointer up to signify entering a new function. Procedure/function
 * formal parameters are the first things on the stack.
 *
 * All instructions are of the three-value kinds, i.e. an instruction has an
 * opcode, an output location and two input locations, not all of which have to
 * be used. There's a special null_loc() to signify an unused location.
 * Locations are effectively just indexes into either the global or local
 * array. I've also added a separate CONST instruction that pushes a constant
 * value to a specified output location.
 *
 * Individual instructions are very wide, which decreases cache performance and
 * makes this particular implementation fairly slow to interpret, but the extra
 * width means it's a little bit easier to work due to not having to
 * compress/decompress anything. In particular, I tried to make JIT compilation
 * as easy as possible, at expense of interpretation speed.
 */

/* globals are kind of ugly and could/should be made into parameters (or
 * potentially even members of some all-encompassing state) but this works for
 * this simple implementation, implementing the change would mosty just require
 * changing function signatures which is boring and I don't want to do it right
 * now */
static struct vec fns = {0};

/* locs use global 0 to mean unitialized and global 1 is reserved as null_loc(),
 * so skip two first globals */
static size_t globals = 2;

/* get register/stack slot for AST node */
#define regno(x) ((x)->l.s)
#define reg(x) EJIT_GPR(regno(x))

size_t num_globals()
{
	return globals;
}

static void lower(struct fn *f, struct ast *n);
static void lower_list(struct fn *f, struct ast *l)
{
	foreach_node(n, l) {
		lower(f, n);
	}
}

static struct loc build_local_loc(size_t idx)
{
	return (struct loc){.g = 0, .s = idx};
}

static struct loc build_global_loc(size_t idx)
{
	return (struct loc){.g = idx, .s = 0};
}

static struct loc null_loc()
{
	/* don't exactly love this but I guess it works */
	return build_global_loc(1);
}

static enum ejit_type ejit_type_from(enum type_kind t)
{
	switch (t) {
	case TYPE_VOID: return EJIT_VOID;
	case TYPE_STRING: return EJIT_POINTER;
	case TYPE_INT:
	case TYPE_BOOL:
	case TYPE_DATE: return EJIT_INT64;
	default: abort();
	}
}

static void put(struct fn *f, struct loc l)
{
	assert(l.g != 1);
	/* local values are already where they should be */
	if (l.g <= 1)
		return;

	/* something like this I think, should still check the type width */
	ejit_stxi_64(f->f, EJIT_GPR(l.s), EJIT_GPR(0), l.g * sizeof(int64_t));
}

static void get(struct fn *f, struct loc l)
{
	assert(l.g != 1);
	if (l.g <= 1)
		return;

	ejit_ldxi_u64(f->f, EJIT_GPR(l.s), EJIT_GPR(0), l.g * sizeof(int64_t));
}

static void lower_var(struct fn *f, struct ast *n)
{
	n->l = build_local_loc(f->sp++);
	struct ast *expr = var_expr(n);
	lower(f, expr);
	ejit_movr(f->f, reg(n), reg(expr));
}

static void lower_formal(struct fn *f, struct ast *n)
{
	n->l = build_local_loc(f->sp++);
}

static struct vec lower_formals(struct fn *f, struct ast *n)
{
	struct vec formals = vec_create(sizeof(struct ejit_operand));
	foreach_node(fn, n) {
		lower_formal(f, fn);

		struct ejit_operand arg = EJIT_OPERAND_GPR(
			regno(fn),
			ejit_type_from(fn->t)
			);
		vec_append(&formals, &arg);
	}

	return formals;
}

static void lower_date(struct fn *f, struct ast *n)
{
	n->l = build_local_loc(f->sp);
	ejit_movi(f->f, reg(n), n->v);
}

static void lower_string(struct fn *f, struct ast *n)
{
	n->l = build_local_loc(f->sp);
	ejit_movi(f->f, reg(n), (int64_t)n->id);
}

static void lower_int(struct fn *f, struct ast *n)
{
	n->l = build_local_loc(f->sp);
	ejit_movi(f->f, reg(n), n->v);
}

static void lower_add(struct fn *f, struct ast *n)
{
	n->l = build_local_loc(f->sp);
	struct ast *l = add_l(n);
	struct ast *r = add_r(n);

	/* since variable definitions can't appear in expressions, there's no
	 * danger of some variable claiming a stack location above this point and
	 * it being overwritten by accident. If variable definitions were
	 * expressions, we would probably have to do an extra pass before
	 * lowering that pre-assigns locations to all variables */
	f->sp += 1; lower(f, l);
	f->sp += 1; lower(f, r);
	/* technically we could reuse the stack location for l, but I found that
	 * reading the generated instruction sequences was a bit easier with
	 * separate output and input stack locations */
	f->sp -= 2;

	ejit_addr(f->f, reg(n), reg(l), reg(r));
}

static void lower_sub(struct fn *f, struct ast *n)
{
	n->l = build_local_loc(f->sp);
	struct ast *l = sub_l(n);
	struct ast *r = sub_r(n);

	f->sp += 1; lower(f, l);
	f->sp += 1; lower(f, r);
	f->sp -= 2;

	ejit_subr(f->f, reg(n), reg(l), reg(r));
}

static void lower_mul(struct fn *f, struct ast *n)
{
	n->l = build_local_loc(f->sp);
	struct ast *l = mul_l(n);
	struct ast *r = mul_r(n);

	f->sp += 1; lower(f, l);
	f->sp += 1; lower(f, r);
	f->sp -= 2;

	ejit_mulr(f->f, reg(n), reg(l), reg(r));
}

static void lower_div(struct fn *f, struct ast *n)
{
	n->l = build_local_loc(f->sp);
	struct ast *l = div_l(n);
	struct ast *r = div_r(n);

	f->sp += 1; lower(f, l);
	f->sp += 1; lower(f, r);
	f->sp -= 2;

	ejit_divr(f->f, reg(n), reg(l), reg(r));
}

static long escape_store_year(size_t argc, const struct ejit_arg args[argc])
{
	assert(argc == 2);
	assert(args[0].type == ejit_type_from(TYPE_DATE));
	assert(args[1].type == ejit_type_from(TYPE_INT));

	unsigned month = 0;
	unsigned day = 0;
	date_split(args[0].u64, NULL, &month, &day);
	return date_from_numbers(args[1].u64, month, day);
}

static void lower_store_year(struct fn *f, struct ast *base, struct ast* r)
{
	assert(base->t == TYPE_DATE);
	assert(r->t == TYPE_INT);
	struct ejit_operand args[2] = {
		EJIT_OPERAND_GPR(regno(base), ejit_type_from(TYPE_DATE)),
		EJIT_OPERAND_GPR(regno(r), ejit_type_from(TYPE_INT))
	};

	ejit_escapei(f->f, escape_store_year, 2, args);
	ejit_retval(f->f, reg(base));
}

static long escape_store_month(size_t argc, const struct ejit_arg args[argc])
{
	assert(argc == 2);
	assert(args[0].type == ejit_type_from(TYPE_DATE));
	assert(args[1].type == ejit_type_from(TYPE_INT));

	unsigned year = 0;
	unsigned day = 0;
	date_split(args[0].u64, &year, NULL, &day);
	return date_from_numbers(year, args[1].u64, day);
}

static void lower_store_month(struct fn *f, struct ast *base, struct ast* r)
{
	assert(base->t == TYPE_DATE);
	assert(r->t == TYPE_INT);
	struct ejit_operand args[2] = {
		EJIT_OPERAND_GPR(regno(base), ejit_type_from(TYPE_DATE)),
		EJIT_OPERAND_GPR(regno(r), ejit_type_from(TYPE_INT))
	};

	ejit_escapei(f->f, escape_store_month, 2, args);
	ejit_retval(f->f, reg(base));
}

static long escape_store_day(size_t argc, const struct ejit_arg args[argc])
{
	assert(argc == 2);
	assert(args[0].type == ejit_type_from(TYPE_DATE));
	assert(args[1].type == ejit_type_from(TYPE_INT));

	unsigned year = 0;
	unsigned month = 0;
	date_split(args[0].u64, &year, &month, NULL);
	return date_from_numbers(year, month, args[1].u64);
}

static void lower_store_day(struct fn *f, struct ast *base, struct ast* r)
{
	assert(base->t == TYPE_DATE);
	assert(r->t == TYPE_INT);
	struct ejit_operand args[2] = {
		EJIT_OPERAND_GPR(regno(base), ejit_type_from(TYPE_DATE)),
		EJIT_OPERAND_GPR(regno(r), ejit_type_from(TYPE_INT))
	};

	ejit_escapei(f->f, escape_store_day, 2, args);
	ejit_retval(f->f, reg(base));
}

static void lower_dot_assign(struct fn *f, struct ast *n)
{
	struct ast *r = assign_r(n);
	struct ast *l = assign_l(n);

	lower(f, r);

	struct ast *base = dot_base(l);
	lower(f, base);

	if (same_id(l->id, "day"))
		lower_store_day(f, l, r);

	else if (same_id(l->id, "month"))
		lower_store_month(f, l, r);

	else if (same_id(l->id, "year"))
		lower_store_year(f, l, r);
	else
		abort();

	n->l = base->l;
	put(f, n->l);
}

static void get_id_loc(struct fn *f, struct ast *n)
{
	assert(n->k == AST_ID);
	struct ast *exists = file_scope_find(n->scope, n->id);
	assert(exists);

	/* using ast nodes/scope lookup as convenient way to store variable ->
	 * location mappings */
	n->l = exists->l;
	assert(n->l.g != 1);
	if (n->l.g > 1) {
		/* global variables should get loaded to the stack for handling */
		n->l.s = f->sp;
	}
}

static void lower_id(struct fn *f, struct ast *n)
{
	/* first calculate location */
	get_id_loc(f, n);
	/* then value at location */
	get(f, n->l);
}

static void lower_assign(struct fn *f, struct ast *n)
{
	struct ast *l = assign_l(n);
	if (l->k == AST_DOT)
		return lower_dot_assign(f, n);

	struct ast *r = assign_r(n);

	lower(f, r);
	/* get location for variable, at this point we should be certain that
	 * we're dealing with a simple variable */
	get_id_loc(f, l);
	ejit_movr(f->f, reg(l), reg(r));
	/* maybe store possible global value to global array */
	put(f, l->l);

	n->l = null_loc();
}


static void lower_return(struct fn *f, struct ast *n)
{
	struct ast *expr = return_expr(n);
	lower(f, expr);
	ejit_retr(f->f, reg(expr));
	n->l = null_loc();
}

static long escape_load_weeknum(size_t argc, const struct ejit_arg args[argc])
{
	assert(argc == 1);
	assert(args[0].type == ejit_type_from(TYPE_DATE));

	struct tm time = tm_from_date(args[0].u64);
	return time.tm_yday / 7;
}

static void lower_load_weeknum(struct fn *f, struct ast *n, struct ast *base)
{
	assert(base->t == TYPE_DATE);
	struct ejit_operand args[1] = {
		EJIT_OPERAND_GPR(regno(base), ejit_type_from(TYPE_DATE))
	};

	ejit_escapei(f->f, escape_load_weeknum, 1, args);
	ejit_retval(f->f, reg(n));
}

static long escape_load_weekday(size_t argc, const struct ejit_arg args[argc])
{
	assert(argc == 1);
	assert(args[0].type == ejit_type_from(TYPE_DATE));

	struct tm time = tm_from_date(args[0].u64);
	return time.tm_wday;
}

static void lower_load_weekday(struct fn *f, struct ast *n, struct ast *base)
{
	assert(base->t == TYPE_DATE);
	struct ejit_operand args[1] = {
		EJIT_OPERAND_GPR(regno(base), ejit_type_from(TYPE_DATE))
	};

	ejit_escapei(f->f, escape_load_weekday, 1, args);
	ejit_retval(f->f, reg(n));
}

static long escape_load_year(size_t argc, const struct ejit_arg args[argc])
{
	assert(argc == 1);
	assert(args[0].type == ejit_type_from(TYPE_DATE));

	unsigned year = 0;
	date_split(args[0].u64, &year, NULL, NULL);
	return year;
}

static void lower_load_year(struct fn *f, struct ast *n, struct ast *base)
{
	assert(base->t == TYPE_DATE);
	struct ejit_operand args[1] = {
		EJIT_OPERAND_GPR(regno(base), ejit_type_from(TYPE_DATE))
	};

	ejit_escapei(f->f, escape_load_year, 1, args);
	ejit_retval(f->f, reg(n));
}

static long escape_load_month(size_t argc, const struct ejit_arg args[argc])
{
	assert(argc == 1);
	assert(args[0].type == ejit_type_from(TYPE_DATE));

	unsigned month = 0;
	date_split(args[0].u64, NULL, &month, NULL);
	return month;
}

static void lower_load_month(struct fn *f, struct ast *n, struct ast *base)
{
	assert(base->t == TYPE_DATE);
	struct ejit_operand args[1] = {
		EJIT_OPERAND_GPR(regno(base), ejit_type_from(TYPE_DATE))
	};

	ejit_escapei(f->f, escape_load_month, 1, args);
	ejit_retval(f->f, reg(n));
}

static long escape_load_day(size_t argc, const struct ejit_arg args[argc])
{
	assert(argc == 1);
	assert(args[0].type == ejit_type_from(TYPE_DATE));

	unsigned day = 0;
	date_split(args[0].u64, NULL, NULL, &day);
	return day;
}

static void lower_load_day(struct fn *f, struct ast *n, struct ast *base)
{
	assert(base->t == TYPE_DATE);
	struct ejit_operand args[1] = {
		EJIT_OPERAND_GPR(regno(base), ejit_type_from(TYPE_DATE))
	};

	ejit_escapei(f->f, escape_load_day, 1, args);
	ejit_retval(f->f, reg(n));
}

static void lower_attr(struct fn *f, struct ast *n)
{
	n->l = build_local_loc(f->sp);

	struct ast *base = attr_base(n);
	lower(f, base);

	if (same_id(n->id, "day"))
		lower_load_day(f, n, base);

	else if (same_id(n->id, "month"))
		lower_load_month(f, n, base);

	else if (same_id(n->id, "year"))
		lower_load_year(f, n, base);

	else if (same_id(n->id, "weekday"))
		lower_load_weekday(f, n, base);

	else if (same_id(n->id, "weeknum"))
		lower_load_weeknum(f, n, base);

	else
		abort();
}

static long escape_print_space(size_t argc, const struct ejit_arg args[argc])
{
	assert(argc == 0);
	putchar(' ');
	return 0;
}

static long escape_print_newline(size_t argc, const struct ejit_arg args[argc])
{
	assert(argc == 0);
	putchar('\n');
	return 0;
}

static void lower_print(struct fn *f, struct ast *p)
{
	foreach_node(n, print_items(p)) {
		lower(f, n);

		/* don't print space on last element */
		if (n->n)
			ejit_escapei(f->f, escape_print_space, 0, NULL);
	}

	ejit_escapei(f->f, escape_print_newline, 0, NULL);
	p->l = null_loc();
}

static void lower_proc_call(struct fn *f, struct ast *c)
{
	size_t count = 0;
	foreach_node(n, proc_call_args(c)) {
		/* place each argument in its own stack location to await being
		 * used */
		f->sp += 1; lower(f, n); count += 1;
	}

	size_t i = 0;
	struct ejit_operand args[count];
	foreach_node(n, proc_call_args(c)) {
		args[i] = EJIT_OPERAND_GPR(regno(n), ejit_type_from(n->t));
		i++;
	}

	struct ast *def = file_scope_find(c->scope, proc_call_id(c));
	ejit_calli(f->f, (struct ejit_func *)def->l.s, count, args);

	f->sp -= count;

	c->l = build_local_loc(f->sp);
	if (c->t != TYPE_VOID)
		ejit_retval(f->f, reg(c));
}

static void lower_func_call(struct fn *f, struct ast *c)
{
	size_t count = 0;
	foreach_node(n, func_call_args(c)) {
		f->sp += 1; lower(f, n); count += 1;
	}

	size_t i = 0;
	struct ejit_operand args[count];
	foreach_node(n, func_call_args(c)) {
		args[i] = EJIT_OPERAND_GPR(regno(n), ejit_type_from(n->t));
		i++;
	}

	struct ast *def = file_scope_find(c->scope, func_call_id(c));
	/* note, def->l.s is not actually a register, rather a pointer to the
	 * struct ejit_func */
	ejit_calli(f->f, (struct ejit_func *)def->l.s, count, args);

	f->sp -= count;

	c->l = build_local_loc(f->sp);
	if (c->t != TYPE_VOID)
		ejit_retval(f->f, reg(c));
}

static void lower_eq(struct fn *f, struct ast *n)
{
	n->l = build_local_loc(f->sp);
	struct ast *l = eq_l(n);
	struct ast *r = eq_r(n);

	f->sp += 1; lower(f, l);
	f->sp += 1; lower(f, r);
	f->sp -= 2;

	ejit_eqr(f->f, reg(n), reg(l), reg(r));
}

static void lower_lt(struct fn *f, struct ast *n)
{
	n->l = build_local_loc(f->sp);
	struct ast *l = lt_l(n);
	struct ast *r = lt_r(n);

	f->sp += 1; lower(f, l);
	f->sp += 1; lower(f, r);
	f->sp -= 2;

	ejit_ltr(f->f, reg(n), reg(l), reg(r));
}

static void lower_pos(struct fn *f, struct ast *n)
{
	struct ast *expr = pos_expr(n);
	lower(f, expr);
	n->l = expr->l;
}

static void lower_neg(struct fn *f, struct ast *n)
{
	n->l = build_local_loc(f->sp);
	struct ast *expr = neg_expr(n);
	f->sp += 1; lower(f, expr);
	f->sp -= 1;

	ejit_negr(f->f, reg(n), reg(expr));
}

static long escape_print_date(size_t argc, const struct ejit_arg args[argc])
{
	assert(argc == 1);
	assert(args[0].type == ejit_type_from(TYPE_DATE));

	char str[11] = {0};
	date_to_string(str, args[0].u64);
	printf("%s", str);
	return 0;
}

static void lower_print_date(struct fn *f, struct ast *n)
{
	struct ast *expr = print_date_expr(n);
	lower(f, expr);

	struct ejit_operand args[1] = {
		EJIT_OPERAND_GPR(regno(expr), ejit_type_from(TYPE_DATE))
	};

	ejit_escapei(f->f, escape_print_date, 1, args);
	n->l = null_loc();
}

static long escape_print_int(size_t argc, const struct ejit_arg args[argc])
{
	assert(argc == 1);
	assert(args[0].type == ejit_type_from(TYPE_INT));
	printf("%lli", (long long)args[0].l);
	return 0;
}

static void lower_print_int(struct fn *f, struct ast *n)
{
	struct ast *expr = print_int_expr(n);
	lower(f, expr);

	struct ejit_operand args[1] = {
		EJIT_OPERAND_GPR(regno(expr), ejit_type_from(TYPE_INT))
	};

	ejit_escapei(f->f, escape_print_int, 1, args);
	n->l = null_loc();
}

static long escape_print_string(size_t argc, const struct ejit_arg args[argc])
{
	assert(argc == 1);
	assert(args[0].type == ejit_type_from(TYPE_STRING));
	puts(args[0].p);
	return 0;
}

static void lower_print_string(struct fn *f, struct ast *n)
{
	struct ast *expr = print_string_expr(n);
	lower(f, expr);

	struct ejit_operand args[1] = {
		EJIT_OPERAND_GPR(regno(expr), ejit_type_from(TYPE_STRING))
	};

	ejit_escapei(f->f, escape_print_string, 1, args);
	n->l = null_loc();
}

static long escape_print_bool(size_t argc, const struct ejit_arg args[argc])
{
	assert(argc == 1);
	assert(args[0].type == ejit_type_from(TYPE_BOOL));
	puts(args[0].l ? "true" : "false");
	return 0;
}

static void lower_print_bool(struct fn *f, struct ast *n)
{
	struct ast *expr = print_bool_expr(n);
	lower(f, expr);

	struct ejit_operand args[1] = {
		EJIT_OPERAND_GPR(regno(expr), ejit_type_from(TYPE_BOOL))
	};

	ejit_escapei(f->f, escape_print_bool, 1, args);
	n->l = null_loc();
}

static ph_date_t date_add(ph_date_t d, long i)
{
	struct tm time = tm_from_date(d);
	time.tm_mday = i;
	mktime(&time);
	return date_from_tm(time);
}

static long escape_date_add(size_t argc, const struct ejit_arg args[argc])
{
	assert(argc == 2);
	assert(args[0].type == ejit_type_from(TYPE_DATE));
	assert(args[1].type == ejit_type_from(TYPE_INT));
	return date_add(args[0].u64, args[1].l);
}

static void lower_date_add(struct fn *f, struct ast *n)
{
	n->l = build_local_loc(f->sp);

	struct ast *l = date_add_l(n);
	struct ast *r = date_add_r(n);

	f->sp += 1; lower(f, l);
	f->sp += 1; lower(f, r);
	f->sp -= 2;

	struct ejit_operand args[2] = {
		EJIT_OPERAND_GPR(regno(l), ejit_type_from(TYPE_DATE)),
		EJIT_OPERAND_GPR(regno(r), ejit_type_from(TYPE_INT)),
	};

	ejit_escapei(f->f, escape_date_add, 2, args);
	ejit_retval(f->f, reg(n));
}

static ph_date_t date_sub(ph_date_t d, long i)
{
	struct tm time = tm_from_date(d);
	time.tm_mday -= i;
	mktime(&time);
	return date_from_tm(time);
}

static long escape_date_sub(size_t argc, const struct ejit_arg args[argc])
{
	assert(argc == 2);
	assert(args[0].type == ejit_type_from(TYPE_DATE));
	assert(args[1].type == ejit_type_from(TYPE_INT));
	return date_sub(args[0].u64, args[1].l);
}

static void lower_date_sub(struct fn *f, struct ast *n)
{
	n->l = build_local_loc(f->sp);

	struct ast *l = date_sub_l(n);
	struct ast *r = date_sub_r(n);

	f->sp += 1; lower(f, l);
	f->sp += 1; lower(f, r);
	f->sp -= 2;

	struct ejit_operand args[2] = {
		EJIT_OPERAND_GPR(regno(l), ejit_type_from(TYPE_DATE)),
		EJIT_OPERAND_GPR(regno(r), ejit_type_from(TYPE_INT)),
	};

	ejit_escapei(f->f, escape_date_sub, 2, args);
	ejit_retval(f->f, reg(n));
}

static long escape_date_diff(size_t argc, const struct ejit_arg args[argc])
{
	assert(argc == 2);
	assert(args[0].type == ejit_type_from(TYPE_DATE));
	assert(args[1].type == ejit_type_from(TYPE_DATE));

	struct tm tm0 = tm_from_date((ph_date_t)args[0].u64);
	struct tm tm1 = tm_from_date((ph_date_t)args[1].u64);

	time_t t0 = mktime(&tm0);
	time_t t1 = mktime(&tm1);

	double seconds = difftime(t0, t1);
	return round(seconds / 86400);
}

static void lower_date_diff(struct fn *f, struct ast *n)
{
	n->l = build_local_loc(f->sp);

	struct ast *l = date_diff_l(n);
	struct ast *r = date_diff_r(n);

	f->sp += 1; lower(f, l);
	f->sp += 1; lower(f, r);
	f->sp -= 2;

	struct ejit_operand args[2] = {
		EJIT_OPERAND_GPR(regno(l), ejit_type_from(TYPE_DATE)),
		EJIT_OPERAND_GPR(regno(r), ejit_type_from(TYPE_DATE)),
	};

	ejit_escapei(f->f, escape_date_diff, 2, args);
	ejit_retval(f->f, reg(n));
}

static void lower_until(struct fn *f, struct ast *n)
{
	struct ejit_label l = ejit_label(f->f);

	lower_list(f, until_body(n));

	struct ast *cond = until_cond(n);
	lower(f, cond);

	struct ejit_reloc r = ejit_beqi(f->f, reg(cond), 0);
	ejit_patch(f->f, r, l);
	n->l = null_loc();
}

static void lower_unless(struct fn *f, struct ast *n)
{
	struct ast *cond = unless_cond(n);
	lower(f, cond);

	struct ejit_reloc branch = ejit_bnei(f->f, reg(cond), 0);

	struct ast *body = unless_body(n);
	lower_list(f, body);

	struct ejit_reloc jump = ejit_jmp(f->f);

	struct ejit_label l = ejit_label(f->f);
	ejit_patch(f->f, branch, l);

	struct ast *otherwise = unless_otherwise(n);
	lower_list(f, otherwise);

	l = ejit_label(f->f);
	ejit_patch(f->f, jump, l);

	n->l = null_loc();
}

static void lower_unless_expr(struct fn *f, struct ast *n)
{
	n->l = build_local_loc(f->sp);

	struct ast *cond = unless_expr_cond(n);
	lower(f, cond);

	struct ejit_reloc branch = ejit_bnei(f->f, reg(cond), 0);

	struct ast *body = unless_expr_body(n);
	lower(f, body);
	ejit_movr(f->f, reg(n), reg(body));

	struct ejit_reloc jump = ejit_jmp(f->f);

	struct ejit_label l = ejit_label(f->f);
	ejit_patch(f->f, branch, l);

	struct ast *otherwise = unless_expr_otherwise(n);
	lower(f, otherwise);
	ejit_movr(f->f, reg(n), reg(otherwise));

	l = ejit_label(f->f);
	ejit_patch(f->f, jump, l);
}

static long escape_today(size_t argc, const struct ejit_arg args[argc])
{
	assert(argc == 0);
	return current_date();
}

static void lower_builtin_call(struct fn *f, struct ast *n)
{
	/* for now we only support Today(), which doesn't have any args */
	assert(same_id(builtin_call_id(n), "Today"));
	n->l = build_local_loc(f->sp);

	ejit_escapei(f->f, escape_today, 0, NULL);
	ejit_retval(f->f, reg(n));
}

static void lower(struct fn *f, struct ast *n)
{
	/* technically it would be possible for some lowering to use stack space
	 * without calling lower(), probably causing difficult to debug runtime weirdness.
	 * Not currently an issue, but something that would need to be taken
	 * into account if this instruction set was extended at some point. */
	if (f->max_sp < f->sp)
		f->max_sp = f->sp;

	switch (n->k) {
	case AST_PROC_DEF: break;
	case AST_FUNC_DEF: break;
	case AST_VAR_DEF: lower_var(f, n); break;
	case AST_FORMAL_DEF: lower_formal(f, n); break;
	case AST_CONST_STRING: lower_string(f, n); break;
	case AST_CONST_DATE: lower_date(f, n); break;
	case AST_CONST_INT: lower_int(f, n); break;
	case AST_ASSIGN: lower_assign(f, n); break;
	case AST_ADD: lower_add(f, n); break;
	case AST_SUB: lower_sub(f, n); break;
	case AST_MUL: lower_mul(f, n); break;
	case AST_DIV: lower_div(f, n); break;
	case AST_ID: lower_id(f, n); break;
	case AST_RETURN: lower_return(f, n); break;
	case AST_ATTR: lower_attr(f, n); break;
	case AST_PRINT: lower_print(f, n); break;
	case AST_PROC_CALL: lower_proc_call(f, n); break;
	case AST_FUNC_CALL: lower_func_call(f, n); break;
	case AST_BUILTIN_CALL: lower_builtin_call(f, n); break;
	case AST_EQ: lower_eq(f, n); break;
	case AST_LT: lower_lt(f, n); break;
	case AST_POS: lower_pos(f, n); break;
	case AST_NEG: lower_neg(f, n); break;
	case AST_PRINT_DATE: lower_print_date(f, n); break;
	case AST_PRINT_STRING: lower_print_string(f, n); break;
	case AST_PRINT_BOOL: lower_print_bool(f, n); break;
	case AST_PRINT_INT: lower_print_int(f, n); break;
	case AST_DATE_ADD: lower_date_add(f, n); break;
	case AST_DATE_SUB: lower_date_sub(f, n); break;
	case AST_DATE_DIFF: lower_date_diff(f, n); break;
	case AST_UNTIL: lower_until(f, n); break;
	case AST_UNLESS: lower_unless(f, n); break;
	case AST_UNLESS_EXPR: lower_unless_expr(f, n); break;

	/* handled by assign */
	case AST_DOT: break;
	}

	/* each ast node is assigned some location, regardless of if it actually
	 * needs one as a sanity check */
	assert(n->l.g || n->l.s);
}

static void lower_global_var(struct fn *f, struct ast *n) {
	n->l = build_global_loc(globals++);
	struct ast *expr = var_expr(n);
	lower(f, expr);

	/* move directly from expr result to global */
	struct loc l = expr->l;
	l.g = n->l.g;
	put(f, l);
}

static void add_proc(struct ast *n) {
	size_t idx = vec_len(&fns);
	/* global locs are effectively just indexes, so this is a nifty way to
	 * encode the procedure index into the AST for later use by
	 * lower_proc_call and so on */
	n->l = build_global_loc(idx);
	struct fn f = {.name = proc_id(n),
		       .idx = idx,
		       .sp = 0,
		       .max_sp = 0};

	vect_append(struct fn, fns, &f);
}

static void add_func(struct ast *n) {
	size_t idx = vec_len(&fns);
	n->l = build_global_loc(idx);
	struct fn f = {.name = func_id(n),
		       .idx = idx,
		       .sp = 0,
		       .max_sp = 0};

	vect_append(struct fn, fns, &f);
}

static void lower_proc_def(struct ast *d)
{
	struct fn *f = vec_at(&fns, d->l.s);
	assert(f);

	struct vec formals = lower_formals(f, func_formals(d));
	f->f = ejit_create_func(ejit_type_from(d->t), vec_len(&formals),
	                        formals.buf);
	f->sp = vec_len(&formals); f->max_sp = vec_len(&formals);
	vec_destroy(&formals);

	lower_list(f, proc_vars(d));
	lower_list(f, proc_body(d));

	if (d->t == TYPE_VOID)
		ejit_reti(f->f, 0);

	/* ph_date_t is inherently 64bit so we can't really use 32bit JIT */
	ejit_select_compile_func(f->f, f->max_sp + 1, 0, true, JIT);
}

static void lower_func_def(struct ast *d)
{
	struct fn *f = vec_at(&fns, d->l.s);
	assert(f);

	struct vec formals = lower_formals(f, func_formals(d));
	f->f = ejit_create_func(EJIT_VOID, vec_len(&formals), formals.buf);
	f->sp = vec_len(&formals); f->max_sp = vec_len(&formals);
	vec_destroy(&formals);

	lower_list(f, func_vars(d));
	lower_list(f, func_body(d));
	ejit_select_compile_func(f->f, f->max_sp + 1, 0, true, JIT);
}

struct fn *find_fn(size_t idx)
{
	return vec_at(&fns, idx);
}

int lower_ast(struct ast *tree)
{
	fns = vec_create(sizeof(struct fn));

	/* make body of file out to be a kind of main function, it will always
	 * be at index 0 */
	struct fn main = {.name = "main",
		          .idx = 0,
		          .sp = 0,
		          .max_sp = 0};

	vect_append(struct fn, fns, &main);

	/* first create function nodes in fns to assign each procedure an index */
	foreach_node(n, tree) {
		switch (n->k) {
		case AST_PROC_DEF: add_proc(n); break;
		case AST_FUNC_DEF: add_func(n); break;
		default:
		}
	}

	struct fn *f = &vect_at(struct fn, fns, 0);

	/* reserve arg 0 for global pointer */
	struct ejit_operand args[1] = {
		EJIT_OPERAND_GPR(0, EJIT_POINTER)
	};
	f->sp = 1; f->max_sp = 1;
	f->f = ejit_create_func(EJIT_VOID, 1, args);

	/* we can't treat file scope as a regular function, as all variable
	 * definitions must be made global, so we have a little bit of
	 * duplicated code here but that's fine */
	foreach_node(n, tree) {
		switch (n->k) {
		case AST_VAR_DEF: lower_global_var(f, n); break;
		case AST_PROC_DEF: lower_proc_def(n); break;
		case AST_FUNC_DEF: lower_func_def(n); break;
		default: lower(f, n);
		}
	}

	ejit_select_compile_func(f->f, f->max_sp + 1, 0, true, JIT);
	return 0;
}

static void destroy_fn(struct fn *f)
{
	ejit_destroy_func(f->f);
}

void destroy_lowering()
{
	foreach_vec(fi, fns) {
		struct fn *f = vec_at(&fns, fi);
		destroy_fn(f);
	}

	vec_destroy(&fns);
}