-
Notifications
You must be signed in to change notification settings - Fork 0
/
caj_lsl_compile.cpp
1592 lines (1494 loc) · 48.5 KB
/
caj_lsl_compile.cpp
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 "caj_lsl_parse.h"
#include "caj_vm.h"
#include "caj_vm_asm.h"
#include "caj_vm_ops.h"
#include <map>
#include <string>
#include <vector>
#include <cassert>
#include <stdio.h>
#include <stdarg.h>
#include <fcntl.h>
// Possible Linden dain bramage:
// Order of operations (second operand, first operand)
// String/key typecasts: https://jira.secondlife.com/browse/SVC-1710
// ...
struct var_desc {
uint8_t type; uint8_t is_global; uint16_t offset;
};
struct var_scope {
std::map<std::string, var_desc> vars;
struct var_scope *parent;
var_scope(var_scope *parent_scope) : parent(parent_scope) {
}
};
struct lsl_compile_state {
int error; int line_no, column_no;
var_scope globals;
// std::map<std::string, var_desc> vars; // locals
std::map<std::string, const vm_function*> funcs;
std::map<std::string, function*> sys_funcs;
std::map<std::string, int> states;
loc_atom var_stack;
var_scope *scope;
std::map<std::string, loc_atom> labels;
lsl_compile_state() : globals(NULL) { }
};
static const vm_function *make_function(vm_asm &vasm, function *func, int state_no = -1);
static uint32_t constify_list(vm_asm &vasm, lsl_compile_state &st,
expr_node *expr);
static uint32_t build_const_list(vm_asm &vasm, lsl_compile_state &st,
list_node *&item);
static void update_loc(lsl_compile_state &st, const expr_node *expr) {
st.line_no = expr->loc.first_line;
st.column_no = expr->loc.first_column;
}
static void update_loc(lsl_compile_state &st, const statement *statem) {
st.line_no = statem->loc.first_line;
st.column_no = statem->loc.first_column;
}
static void do_error(lsl_compile_state &st, const char* format, ...) {
va_list args;
if(st.error != 0) return;
printf("(%i, %i): ", st.line_no, st.column_no);
va_start (args, format);
vprintf (format, args);
va_end (args);
st.error = 1;
}
static void handle_arg_vars(vm_asm &vasm, lsl_compile_state &st,
func_arg *args, const vm_function* func,
var_scope *scope) {
for(int arg_no = 0; args != NULL; args = args->next, arg_no++) {
if(scope->vars.count(args->name)) {
printf("ERROR: duplicate function argument %s\n",args->name);
st.error = 1; return;
} else {
var_desc var; var.type = args->vtype; var.is_global = 0;
var.offset = func->arg_offsets[arg_no];
scope->vars[args->name] = var;
// printf("DEBUG: added argument %s\n", args->name);
}
}
}
static int statement_child_count(lsl_compile_state &st, statement *statem) {
switch(statem->stype) {
case STMT_DECL: return 0;
case STMT_EXPR: return 0;
case STMT_IF:
return 2; // child[1] may be null, but that should be OK!
case STMT_RET: return 0;
case STMT_WHILE: return 1;
case STMT_DO: return 1;
case STMT_FOR: return 1;
case STMT_BLOCK: return 1;
case STMT_JUMP: return 0;
case STMT_LABEL: return 0;
case STMT_STATE: return 0;
default:
do_error(st, "ERROR:statement_child_count unhandled statement type %i\n", statem->stype);
return 0;
}
}
static void extract_local_vars(vm_asm &vasm, lsl_compile_state &st,
statement *statem, var_scope *scope) {
for( ; statem != NULL; statem = statem->next) {
update_loc(st, statem);
if(statem->stype == STMT_DECL) {
assert(statem->expr[0] != NULL &&
statem->expr[0]->node_type == NODE_IDENT);
if(statem->expr[0]->u.ident.item != NULL) {
printf("ERROR: silly programmer, item accesses are for expressions\n");
st.error = 1; return;
}
char* name = statem->expr[0]->u.ident.name;
uint8_t vtype = statem->expr[0]->vtype;
if(scope->vars.count(name)) {
do_error(st, "ERROR: duplicate definition of local var %s\n",name);
return;
// FIXME - handle this
} else {
var_desc var; var.type = vtype; var.is_global = 0;
// FIXME - initialise these where possible
switch(vtype) {
case VM_TYPE_INT:
var.offset = vasm.const_int(0);
break;
case VM_TYPE_FLOAT:
var.offset = vasm.const_real(0.0f);
break;
case VM_TYPE_STR:
case VM_TYPE_KEY:
var.offset = vasm.const_str("");
break;
case VM_TYPE_LIST:
var.offset = vasm.empty_list();
break;
case VM_TYPE_VECT:
var.offset = vasm.const_int(0);
vasm.const_int(0); vasm.const_int(0);
break;
case VM_TYPE_ROT:
var.offset = vasm.const_int(0);
vasm.const_int(0); vasm.const_int(0); vasm.const_int(0);
break;
default:
do_error(st,"ERROR: unknown type of local var %s\n",name);
return;
// FIXME - handle this
}
scope->vars[name] = var;
}
} else {
int count = statement_child_count(st, statem);
if(st.error) return;
for(int i = 0; i < count; i++) {
var_scope *child_ctx = new var_scope(scope);
extract_local_vars(vasm, st, statem->child[i], child_ctx);
statem->child_vars[i] = child_ctx;
}
}
}
}
static uint8_t get_insn_ret_type(uint16_t insn) {
assert(insn < NUM_INSNS);
return vm_insns[insn].ret;
}
static var_desc get_variable(lsl_compile_state &st, const char* name) {
var_scope *scope = st.scope;
std::map<std::string, var_desc>::iterator iter = scope->vars.find(name);
//printf("DEBUG: searching for var %s in %p\n", name, scope);
while(iter == scope->vars.end() && scope->parent != NULL) {
scope = scope->parent;
iter = scope->vars.find(name);
//printf(" ...in %p\n", scope);
}
if(iter == scope->vars.end()) {
var_desc desc; desc.type = VM_TYPE_NONE;
do_error(st, "ERROR: missing variable %s\n", name);
return desc;
} else {
assert(iter->second.type <= VM_TYPE_MAX);
return iter->second;
}
}
static expr_node* arg_implicit_cast(lsl_compile_state &st, expr_node *expr, uint8_t arg_type) {
if(expr->vtype == arg_type) return expr;
if(expr->vtype == VM_TYPE_INT && arg_type == VM_TYPE_FLOAT) {
return enode_cast(expr, arg_type);
} else if(expr->vtype == VM_TYPE_STR && arg_type == VM_TYPE_KEY) {
return enode_cast(expr, arg_type); // FIXME?
} else if(expr->vtype == VM_TYPE_KEY && arg_type == VM_TYPE_STR) {
return enode_cast(expr, arg_type); // FIXME?
} else {
do_error(st, "ERROR: bad implicit cast from %s to %s\n",
type_names[expr->vtype], type_names[arg_type]);
return expr;
}
}
static int dotted_item_to_idx(char *item) {
assert(!(item[0] == 0 || item[1] != 0 ||
((item[0] < 'x' || item[0] > 'z') && item[0] != 's')));
if(item[0] == 's') return 3;
else return item[0] - 'x';
}
static void propagate_types(vm_asm &vasm, lsl_compile_state &st, expr_node *expr) {
uint16_t insn; uint8_t ltype, rtype; list_node *lnode;
update_loc(st, expr);
switch(expr->node_type) {
case NODE_CONST: break;
case NODE_IDENT:
// get_variable does all the nasty error handling for us!
expr->vtype = get_variable(st, expr->u.ident.name).type;
if(expr->u.ident.item != NULL && expr->vtype != VM_TYPE_VECT &&
expr->vtype != VM_TYPE_ROT) {
do_error(st, "ERROR: %s not a vector or rotation\n",
expr->u.ident.name);
return;
}
if(expr->u.ident.item != NULL) {
if(expr->u.ident.item[0] == 0 || expr->u.ident.item[1] != 0 ||
((expr->u.ident.item[0] < 'x' || expr->u.ident.item[0] > 'z') &&
expr->u.ident.item[0] != 's')) {
do_error(st, "ERROR: Bad dotted identifier: %s.%s\n",
expr->u.ident.name, expr->u.ident.item);
return;
}
int index = dotted_item_to_idx(expr->u.ident.item);
if(index > 2 && expr->vtype == VM_TYPE_VECT) {
do_error(st, "ERROR: %s not a rotation\n",
expr->u.ident.name);
return;
}
expr->vtype = VM_TYPE_FLOAT;
}
break;
case NODE_ASSIGN:
if(expr->u.child[0]->node_type != NODE_IDENT) {
// this is checked in the grammar, so this must be an assignment to a
// named constant that has been converted by the parser
assert(expr->u.child[0]->node_type == NODE_CONST);
do_error(st, "ERROR: assignment to a constant\n"); return;
}
propagate_types(vasm, st, expr->u.child[0]); // finds variable's type
if(st.error != 0) return;
propagate_types(vasm, st, expr->u.child[1]);
if(st.error != 0) return;
expr->vtype = VM_TYPE_NONE /* expr->u.child[0]->vtype */; // FIXME
// FIXME - do we really always want to auto-cast?
expr->u.child[1] = enode_cast(expr->u.child[1], expr->u.child[0]->vtype);
break;
case NODE_ASSIGNADD:
case NODE_ASSIGNSUB:
case NODE_ASSIGNMUL:
case NODE_ASSIGNDIV:
case NODE_ASSIGNMOD:
enode_split_assign(expr);
assert(expr->node_type == NODE_ASSIGN);
propagate_types(vasm, st, expr);
return;
case NODE_NEGATE:
propagate_types(vasm, st, expr->u.child[0]);
if(st.error != 0) return;
update_loc(st, expr);
expr->vtype = expr->u.child[0]->vtype;
break;
case NODE_VECTOR:
case NODE_ROTATION:
{
float v[4]; bool is_const = true;
int count = ( expr->node_type == NODE_VECTOR ? 3 : 4);
for(int i = 0; i < count; i++) {
propagate_types(vasm, st, expr->u.child[i]);
if(st.error != 0) return;
// FIXME - do we really always want to auto-cast?
expr->u.child[i] = enode_cast(expr->u.child[i], VM_TYPE_FLOAT);
}
update_loc(st, expr);
for(int i = 0; i < count; i++) {
if(expr->u.child[i]->node_type != NODE_CONST ||
expr->u.child[i]->vtype != VM_TYPE_FLOAT) {
is_const = false;
};
};
expr->vtype = expr->node_type == NODE_VECTOR ? VM_TYPE_VECT : VM_TYPE_ROT;
if(is_const) {
for(int i = 0; i < count; i++) {
v[i] = expr->u.child[i]->u.f;
free(expr->u.child[i]); // FIXME - do this right!
}
expr->node_type = NODE_CONST;
for(int i = 0; i < count; i++) expr->u.v[i] = v[i];
} else {
for(int i = 0; i < count; i++) {
propagate_types(vasm, st, expr->u.child[i]);
if(st.error != 0) return;
}
}
break;
}
case NODE_ADD:
case NODE_SUB:
case NODE_MUL:
case NODE_DIV:
case NODE_MOD:
case NODE_EQUAL:
case NODE_NEQUAL:
case NODE_LEQUAL:
case NODE_GEQUAL:
case NODE_LESS:
case NODE_GREATER:
case NODE_OR:
case NODE_AND:
case NODE_XOR:
case NODE_L_OR: // FIXME - think these need special handling for typecasts!
case NODE_L_AND:
case NODE_SHR:
case NODE_SHL:
propagate_types(vasm, st, expr->u.child[0]);
if(st.error != 0) return;
propagate_types(vasm, st, expr->u.child[1]);
if(st.error != 0) return;
update_loc(st, expr);
insn = get_insn_binop(expr->node_type, expr->u.child[0]->vtype,
expr->u.child[1]->vtype);
if(insn == 0) {
ltype = expr->u.child[0]->vtype;
rtype = expr->u.child[1]->vtype;
if(ltype == VM_TYPE_LIST && rtype != VM_TYPE_LIST) {
insn = get_insn_binop(expr->node_type, ltype, VM_TYPE_LIST);
if(insn != 0)
expr->u.child[1] = enode_cast(expr->u.child[1], VM_TYPE_LIST);
} else if(ltype != VM_TYPE_LIST && rtype == VM_TYPE_LIST) {
insn = get_insn_binop(expr->node_type, VM_TYPE_LIST, rtype);
if(insn != 0)
expr->u.child[0] = enode_cast(expr->u.child[0], VM_TYPE_LIST);
} else if(ltype == VM_TYPE_STR && rtype != VM_TYPE_STR) {
insn = get_insn_binop(expr->node_type, ltype, VM_TYPE_STR);
if(insn != 0)
expr->u.child[1] = enode_cast(expr->u.child[1], VM_TYPE_STR);
} else if(ltype != VM_TYPE_STR && rtype == VM_TYPE_STR) {
insn = get_insn_binop(expr->node_type, VM_TYPE_STR, rtype);
if(insn != 0)
expr->u.child[0] = enode_cast(expr->u.child[0], VM_TYPE_STR);
} else if(ltype == VM_TYPE_INT) {
insn = get_insn_binop(expr->node_type, VM_TYPE_FLOAT, rtype);
if(insn != 0)
expr->u.child[0] = enode_cast(expr->u.child[0], VM_TYPE_FLOAT);
} else if(rtype == VM_TYPE_INT) {
insn = get_insn_binop(expr->node_type, ltype, VM_TYPE_FLOAT);
if(insn != 0)
expr->u.child[1] = enode_cast(expr->u.child[1], VM_TYPE_FLOAT);
}
}
if((expr->node_type == NODE_EQUAL || expr->node_type == NODE_NEQUAL) &&
expr->u.child[0]->vtype == VM_TYPE_LIST &&
expr->u.child[1]->vtype == VM_TYPE_LIST) {
// we don't bother with a special instruction for this; not worth it.
expr->vtype = VM_TYPE_INT;
} else if(insn != 0) {
expr->vtype = get_insn_ret_type(insn);
} else {
do_error(st, "ERROR: bad types passed to operator %i %s : %s %s\n",
expr->node_type, node_names[expr->node_type],
type_names[ltype], type_names[rtype]);
return;
}
break;
/* FIXME - need to implement a bunch of stuff */
case NODE_NOT:
propagate_types(vasm, st, expr->u.child[0]);
if(st.error != 0) return;
update_loc(st, expr);
if(expr->u.child[0]->vtype != VM_TYPE_INT) {
do_error(st, "ERROR: bitwise NOT on non-integer"); return;
}
expr->vtype = VM_TYPE_INT;
break;
case NODE_L_NOT:
propagate_types(vasm, st, expr->u.child[0]);
if(st.error != 0) return;
update_loc(st, expr);
// no type enforcement, boolean context
expr->vtype = VM_TYPE_INT;
break;
case NODE_PREINC:
case NODE_POSTINC:
case NODE_PREDEC:
case NODE_POSTDEC:
propagate_types(vasm, st, expr->u.child[0]);
if(st.error != 0) return;
expr->vtype = expr->u.child[0]->vtype;
break;
case NODE_CAST:
propagate_types(vasm, st, expr->u.child[0]);
break;
case NODE_CALL:
{
int arg_count = 0, i;
for(lnode = expr->u.call.args; lnode != NULL; lnode = lnode->next) {
propagate_types(vasm, st, lnode->expr);
if(st.error != 0) return;
arg_count++;
}
update_loc(st, expr);
if(strcmp(expr->u.call.name,"print") == 0 && expr->u.call.args != NULL) {
expr->vtype = VM_TYPE_NONE; // HACK! - FIXME remove this!
} else {
std::map<std::string, const vm_function*>::iterator iter =
st.funcs.find(expr->u.call.name);
if(iter == st.funcs.end()) {
std::map<std::string, function*>::iterator sfiter =
st.sys_funcs.find(expr->u.call.name);
if(sfiter == st.sys_funcs.end()) {
do_error(st, "ERROR: call to unknown function %s\n", expr->u.call.name);
return;
} else {
st.funcs[expr->u.call.name] = make_function(vasm, sfiter->second);
iter = st.funcs.find(expr->u.call.name); // HACK.
assert(iter != st.funcs.end());
}
}
if(iter->second->arg_count != arg_count) {
do_error(st, "ERROR: wrong number of arguments to function %s\n", expr->u.call.name);
return;
}
i = 0;
for(lnode = expr->u.call.args; lnode != NULL; lnode = lnode->next, i++) {
lnode->expr = arg_implicit_cast(st, lnode->expr,
iter->second->arg_types[i]);
if(st.error != 0) return;
}
expr->vtype = iter->second->ret_type;
}
}
break;
case NODE_LIST:
{
for(lnode = expr->u.list; lnode != NULL; lnode = lnode->next) {
propagate_types(vasm, st, lnode->expr);
if(st.error != 0) return;
}
update_loc(st, expr);
}
break;
}
#if 0
#define NODE_ASSIGNADD 22
#define NODE_ASSIGNSUB 23
#define NODE_ASSIGNMUL 24
#define NODE_ASSIGNDIV 25
#define NODE_ASSIGNMOD 26
/* unary ops */
#define NODE_NEGATE 27
#define NODE_NOT 28 /* ~ - bitwise not */
#define NODE_L_NOT 29 /* ! - logical not */
#endif
}
static uint16_t get_insn_cast(uint8_t from_type, uint8_t to_type) {
switch(MK_VM_TYPE_PAIR(from_type, to_type)) {
case MK_VM_TYPE_PAIR(VM_TYPE_INT, VM_TYPE_NONE): return INSN_DROP_I;
case MK_VM_TYPE_PAIR(VM_TYPE_FLOAT, VM_TYPE_NONE): return INSN_DROP_I;
case MK_VM_TYPE_PAIR(VM_TYPE_VECT, VM_TYPE_NONE): return INSN_DROP_I3;
case MK_VM_TYPE_PAIR(VM_TYPE_ROT, VM_TYPE_NONE): return INSN_DROP_I4;
case MK_VM_TYPE_PAIR(VM_TYPE_STR, VM_TYPE_NONE): return INSN_DROP_P;
case MK_VM_TYPE_PAIR(VM_TYPE_KEY, VM_TYPE_NONE): return INSN_DROP_P;
case MK_VM_TYPE_PAIR(VM_TYPE_LIST, VM_TYPE_NONE): return INSN_DROP_P;
case MK_VM_TYPE_PAIR(VM_TYPE_INT, VM_TYPE_FLOAT): return INSN_CAST_I2F;
case MK_VM_TYPE_PAIR(VM_TYPE_FLOAT, VM_TYPE_INT): return INSN_CAST_F2I;
case MK_VM_TYPE_PAIR(VM_TYPE_INT, VM_TYPE_STR): return INSN_CAST_I2S;
case MK_VM_TYPE_PAIR(VM_TYPE_FLOAT, VM_TYPE_STR): return INSN_CAST_F2S;
case MK_VM_TYPE_PAIR(VM_TYPE_VECT, VM_TYPE_STR): return INSN_CAST_V2S;
case MK_VM_TYPE_PAIR(VM_TYPE_ROT, VM_TYPE_STR): return INSN_CAST_R2S;
case MK_VM_TYPE_PAIR(VM_TYPE_LIST, VM_TYPE_STR): return INSN_CAST_LIST2S;
case MK_VM_TYPE_PAIR(VM_TYPE_INT, VM_TYPE_LIST): return INSN_CAST_I2L;
case MK_VM_TYPE_PAIR(VM_TYPE_FLOAT, VM_TYPE_LIST): return INSN_CAST_F2L;
case MK_VM_TYPE_PAIR(VM_TYPE_STR, VM_TYPE_LIST): return INSN_CAST_S2L;
case MK_VM_TYPE_PAIR(VM_TYPE_KEY, VM_TYPE_LIST): return INSN_CAST_K2L;
case MK_VM_TYPE_PAIR(VM_TYPE_VECT, VM_TYPE_LIST): return INSN_CAST_V2L;
case MK_VM_TYPE_PAIR(VM_TYPE_ROT, VM_TYPE_LIST): return INSN_CAST_R2L;
case MK_VM_TYPE_PAIR(VM_TYPE_STR, VM_TYPE_INT): return INSN_CAST_S2I;
case MK_VM_TYPE_PAIR(VM_TYPE_STR, VM_TYPE_FLOAT): return INSN_CAST_S2F;
case MK_VM_TYPE_PAIR(VM_TYPE_STR, VM_TYPE_VECT): return INSN_CAST_S2V;
case MK_VM_TYPE_PAIR(VM_TYPE_STR, VM_TYPE_ROT): return INSN_CAST_S2R;
/* FIXME - fill out the rest of these */
default: return 0;
}
}
// Remember, vectors are stored on the stack in normal x,y,z order, which means
// they're pushed on in reverse: z, then y, then x.
// To complicate matters, the local variable indexes we use in the compiler
// are the reverse of what you might expect: higher values mean lower addresses
static void read_var(vm_asm &vasm, lsl_compile_state &st, var_desc var, int index = -1) {
assert(index < 0 || var.type == VM_TYPE_VECT || var.type == VM_TYPE_ROT);
if(var.is_global) {
switch(var.type) {
case VM_TYPE_INT:
case VM_TYPE_FLOAT:
vasm.insn(MAKE_INSN(ICLASS_RDG_I, var.offset));
break;
case VM_TYPE_VECT:
if(index < 0) {
for(int i = 2; i >= 0; i--)
vasm.insn(MAKE_INSN(ICLASS_RDG_I, var.offset+i));
} else {
vasm.insn(MAKE_INSN(ICLASS_RDG_I, var.offset+index));
}
break;
case VM_TYPE_ROT:
if(index < 0) {
for(int i = 3; i >= 0; i--)
vasm.insn(MAKE_INSN(ICLASS_RDG_I, var.offset+i));
} else {
vasm.insn(MAKE_INSN(ICLASS_RDG_I, var.offset+index));
}
break;
case VM_TYPE_STR:
case VM_TYPE_KEY:
case VM_TYPE_LIST:
vasm.insn(MAKE_INSN(ICLASS_RDG_P, var.offset));
break;
default:
do_error(st,"FIXME: can't handle access to vars of type %s \n",
type_names[var.type]);
return;
}
} else {
switch(var.type) {
case VM_TYPE_INT:
case VM_TYPE_FLOAT:
vasm.rd_local_int(var.offset);
break;
case VM_TYPE_VECT:
if(index < 0) {
for(int i = 0; i < 3; i++)
vasm.rd_local_int(var.offset+i);
} else {
vasm.rd_local_int(var.offset+(2-index));
}
break;
case VM_TYPE_ROT:
if(index < 0) {
for(int i = 0; i < 4; i++)
vasm.rd_local_int(var.offset+i);
} else {
vasm.rd_local_int(var.offset+(3-index));
}
break;
case VM_TYPE_STR:
case VM_TYPE_KEY:
case VM_TYPE_LIST:
vasm.rd_local_ptr(var.offset);
break;
default:
do_error(st, "FIXME: can't handle access to vars of type %s \n",
type_names[var.type]);
return;
}
}
}
static void write_var(vm_asm &vasm, lsl_compile_state &st, var_desc var, int index = -1) {
assert(index < 0 || var.type == VM_TYPE_VECT || var.type == VM_TYPE_ROT);
if(var.is_global) {
switch(var.type) {
case VM_TYPE_INT:
case VM_TYPE_FLOAT:
vasm.insn(MAKE_INSN(ICLASS_WRG_I, var.offset));
break;
case VM_TYPE_VECT:
if(index < 0) {
for(int i = 0; i < 3; i++)
vasm.insn(MAKE_INSN(ICLASS_WRG_I, var.offset+i));
} else {
vasm.insn(MAKE_INSN(ICLASS_WRG_I, var.offset+index));
}
break;
case VM_TYPE_ROT:
if(index < 0) {
for(int i = 0; i < 4; i++)
vasm.insn(MAKE_INSN(ICLASS_WRG_I, var.offset+i));
} else {
vasm.insn(MAKE_INSN(ICLASS_WRG_I, var.offset+index));
}
break;
case VM_TYPE_STR:
case VM_TYPE_KEY:
case VM_TYPE_LIST:
vasm.insn(MAKE_INSN(ICLASS_WRG_P, var.offset));
break;
default:
do_error(st, "FIXME: can't handle access to vars of type %s \n",
type_names[var.type]);
return;
}
} else {
switch(var.type) {
case VM_TYPE_INT:
case VM_TYPE_FLOAT:
vasm.wr_local_int(var.offset);
break;
case VM_TYPE_VECT:
if(index < 0) {
for(int i = 2; i >= 0; i--)
vasm.wr_local_int(var.offset+i);
} else {
vasm.wr_local_int(var.offset+(2-index));
}
break;
case VM_TYPE_ROT:
if(index < 0) {
for(int i = 3; i >= 0; i--)
vasm.wr_local_int(var.offset+i);
} else {
vasm.wr_local_int(var.offset+(3-index));
}
break;
case VM_TYPE_STR:
case VM_TYPE_KEY:
case VM_TYPE_LIST:
vasm.wr_local_ptr(var.offset); // FIXME - TODO
break;
default:
do_error(st, "FIXME: can't handle access to vars of type %s \n",
type_names[var.type]);
return;
}
}
}
// actually outputs a cast to boolean context
static void asm_cast_to_bool(vm_asm &vasm, lsl_compile_state &st, expr_node *expr) {
switch(expr->vtype) {
case VM_TYPE_INT:
break;
case VM_TYPE_FLOAT:
vasm.insn(INSN_CAST_F2B); break;
case VM_TYPE_STR:
vasm.insn(INSN_CAST_S2B); break;
case VM_TYPE_KEY:
vasm.insn(INSN_CAST_K2B); break;
case VM_TYPE_LIST:
vasm.insn(INSN_CAST_L2B); break;
case VM_TYPE_VECT:
vasm.insn(INSN_CAST_V2B); break;
case VM_TYPE_ROT:
vasm.insn(INSN_CAST_R2B); break;
default:
do_error(st, "ERROR: can't cast %s type to bool\n", type_names[expr->vtype]);
}
}
static void assemble_expr(vm_asm &vasm, lsl_compile_state &st, expr_node *expr) {
uint8_t insn;
update_loc(st, expr);
switch(expr->node_type) {
case NODE_CONST:
switch(expr->vtype) {
case VM_TYPE_INT:
vasm.const_int(expr->u.i); break;
case VM_TYPE_FLOAT:
vasm.const_real(expr->u.f); break;
case VM_TYPE_STR:
vasm.const_str(expr->u.s); break;
case VM_TYPE_VECT:
for(int i = 2; i >= 0; i--)
vasm.const_real(expr->u.v[i]);
break;
case VM_TYPE_ROT:
for(int i = 3; i >= 0; i--)
vasm.const_real(expr->u.v[i]);
break;
default:
do_error(st, "FIXME: unhandled const type %s\n", type_names[expr->vtype]);
return;
}
break;
case NODE_IDENT:
{
var_desc var = get_variable(st, expr->u.ident.name);
if(st.error != 0) return;
if(expr->u.ident.item == NULL) {
assert(var.type == expr->vtype);
read_var(vasm, st, var);
} else {
assert(expr->vtype == VM_TYPE_FLOAT &&
(var.type == VM_TYPE_VECT || var.type == VM_TYPE_ROT));
int offset = dotted_item_to_idx(expr->u.ident.item);
assert(offset >= 0 && offset < (var.type == VM_TYPE_VECT ? 3 : 4));
read_var(vasm, st, var, offset);
}
}
break;
case NODE_ASSIGN:
{
assert(expr->u.child[0]->node_type == NODE_IDENT); // checked in grammar
uint8_t vtype = expr->u.child[1]->vtype; // FIXME - use child[0]?
var_desc var = get_variable(st, expr->u.child[0]->u.ident.name);
if(st.error != 0) return;
if(expr->u.child[0]->u.ident.item == NULL) {
assert(var.type == vtype);
} else {
assert(vtype == VM_TYPE_FLOAT &&
(var.type == VM_TYPE_VECT || var.type == VM_TYPE_ROT));
}
assemble_expr(vasm, st, expr->u.child[1]);
if(st.error != 0) return;
update_loc(st, expr);
if(expr->u.child[0]->u.ident.item == NULL) {
write_var(vasm, st, var);
} else {
int offset = dotted_item_to_idx(expr->u.child[0]->u.ident.item);
assert(offset >= 0 && offset < (var.type == VM_TYPE_VECT ? 3 : 4));
write_var(vasm, st, var, offset);
}
}
break;
case NODE_VECTOR:
case NODE_ROTATION:
{
int count = ( expr->node_type == NODE_VECTOR ? 3 : 4);
for(int i = count-1; i >= 0; i--) {
// Note that this executes expressions in an odd order.
// FIXME? (would slow down & complicate compiled code slightly)
assemble_expr(vasm, st, expr->u.child[i]);
if(st.error != 0) return;
update_loc(st, expr);
}
break;
}
case NODE_NEGATE:
assemble_expr(vasm, st, expr->u.child[0]);
if(st.error != 0) return;
update_loc(st, expr);
switch(expr->vtype) {
case VM_TYPE_INT:
vasm.insn(INSN_NEG_I); break;
case VM_TYPE_FLOAT:
vasm.insn(INSN_NEG_F); break;
case VM_TYPE_VECT:
vasm.insn(INSN_NEG_V); break;
case VM_TYPE_ROT:
vasm.insn(INSN_NEG_R); break;
default:
do_error(st, "ERROR: bad type for unary - operator: %s\n",
type_names[expr->vtype]);
return;
}
break;
case NODE_ADD:
case NODE_SUB:
case NODE_MUL:
case NODE_DIV:
case NODE_MOD:
case NODE_EQUAL:
case NODE_NEQUAL:
case NODE_LEQUAL:
case NODE_GEQUAL:
case NODE_LESS:
case NODE_GREATER:
case NODE_OR:
case NODE_AND:
case NODE_XOR:
case NODE_L_OR:
case NODE_L_AND:
case NODE_SHR:
case NODE_SHL:
{
bool list_magic = false;
insn = get_insn_binop(expr->node_type, expr->u.child[0]->vtype,
expr->u.child[1]->vtype);
if((expr->node_type == NODE_EQUAL || expr->node_type == NODE_NEQUAL) &&
expr->u.child[0]->vtype == VM_TYPE_LIST &&
expr->u.child[1]->vtype == VM_TYPE_LIST) {
// we don't bother with a special instruction for this.
list_magic = true;
// yes, list != operator subtracts lengths, and this is
// a documented quirk that we have to live with.
insn = expr->node_type == NODE_EQUAL ? INSN_EQ_II : INSN_SUB_II;
} else if(insn == 0) {
do_error(st, "INTERNAL ERROR: no insn for binop %i %s %s\n",
expr->node_type, type_names[expr->u.child[0]->vtype],
type_names[expr->u.child[1]->vtype]);
return;
}
assemble_expr(vasm, st, expr->u.child[0]);
if(st.error != 0) return;
if(list_magic) vasm.insn(INSN_LISTLEN);
if(vasm.get_error() != NULL) {
do_error(st, "[left] ASSEMBLER ERROR: %s\n", vasm.get_error());
return;
}
assemble_expr(vasm, st, expr->u.child[1]);
if(st.error != 0) return;
if(list_magic) vasm.insn(INSN_LISTLEN);
if(vasm.get_error() != NULL) {
do_error(st, "[right] ASSEMBLER ERROR: %s\n", vasm.get_error());
return;
}
update_loc(st, expr);
vasm.insn(insn);
break;
}
case NODE_CALL:
// HACK
if(strcmp(expr->u.call.name,"print") == 0 && expr->u.call.args != NULL) {
switch(expr->u.call.args->expr->vtype) {
case VM_TYPE_INT:
assemble_expr(vasm, st, expr->u.call.args->expr);
if(st.error != 0) return;
vasm.insn(INSN_PRINT_I);
break;
case VM_TYPE_FLOAT:
assemble_expr(vasm, st, expr->u.call.args->expr);
if(st.error != 0) return;
vasm.insn(INSN_PRINT_F);
break;
case VM_TYPE_STR:
assemble_expr(vasm, st, expr->u.call.args->expr);
if(st.error != 0) return;
vasm.insn(INSN_PRINT_STR);
break;
default:
do_error(st, "ERROR: bad argument type to print() builtin: %s\n",
type_names[expr->u.call.args->expr->vtype]);
return;
}
} else {
std::map<std::string, const vm_function*>::iterator iter =
st.funcs.find(expr->u.call.name);
if(iter == st.funcs.end()) {
do_error(st, "ERROR: call to unknown function %s\n", expr->u.call.name);
return;
}
if(iter->second->func_num == 0xffff) {
// printf("DEBUG: beginning call to fake func %s\n", expr->u.call.name);
for(list_node *lnode = expr->u.call.args; lnode != NULL; lnode = lnode->next) {
// printf("DEBUG: assembling an argument %p\n", lnode);
assemble_expr(vasm, st, lnode->expr);
if(st.error != 0) return;
}
update_loc(st, expr);
// printf("DEBUG: doing call to fake func %s opcode %i\n",
// expr->u.call.name, (int)iter->second->insn_ptr);
vasm.insn(iter->second->insn_ptr);
} else {
//printf("DEBUG: beginning call to %s\n", expr->u.call.name);
vasm.begin_call(iter->second);
for(list_node *lnode = expr->u.call.args; lnode != NULL; lnode = lnode->next) {
//printf("DEBUG: assembling an argument %p\n", lnode);
assemble_expr(vasm, st, lnode->expr);
if(st.error != 0) return;
}
update_loc(st, expr);
//printf("DEBUG: doing call to %s\n", expr->u.call.name);
vasm.do_call(iter->second);
}
}
break;
case NODE_NOT:
assemble_expr(vasm, st, expr->u.child[0]);
if(st.error != 0) return;
update_loc(st, expr);
vasm.insn(INSN_NOT_I);
break;
case NODE_L_NOT:
assemble_expr(vasm, st, expr->u.child[0]);
if(st.error != 0) return;
update_loc(st, expr);
asm_cast_to_bool(vasm, st, expr->u.child[0]);
if(st.error) return;
vasm.insn(INSN_NOT_L);
break;
case NODE_CAST:
insn = get_insn_cast(expr->u.child[0]->vtype, expr->vtype);
if(expr->u.child[0]->vtype == expr->vtype ||
(expr->u.child[0]->vtype == VM_TYPE_STR && expr->vtype == VM_TYPE_KEY) ||
(expr->u.child[0]->vtype == VM_TYPE_KEY && expr->vtype == VM_TYPE_STR)) {
insn = INSN_NOOP;
} else if(insn == 0) {
do_error(st, "ERROR: couldn't cast %s -> %s\n",
type_names[expr->u.child[0]->vtype],
type_names[expr->vtype]);
st.error = 1; return;
}
assemble_expr(vasm, st, expr->u.child[0]);
if(st.error != 0) return;
update_loc(st, expr);
if(insn != INSN_NOOP)
vasm.insn(insn);
break;
case NODE_PREINC:
case NODE_PREDEC:
case NODE_POSTINC:
case NODE_POSTDEC:
{
assert(expr->u.child[0]->node_type == NODE_IDENT); // checked in grammar
uint8_t vtype = expr->u.child[0]->vtype;
var_desc var = get_variable(st, expr->u.child[0]->u.s);
if(st.error != 0) return;
assert(var.type == vtype);
int is_post = (expr->node_type == NODE_POSTINC ||
expr->node_type == NODE_POSTDEC);
uint16_t insn, dup_insn;
switch(vtype) {
case VM_TYPE_INT:
if(expr->node_type == NODE_PREINC || expr->node_type == NODE_POSTINC)
insn = INSN_INC_I;
else insn = INSN_DEC_I;
dup_insn = MAKE_INSN(ICLASS_RDL_I, 1);
break;
case VM_TYPE_FLOAT: // TODO
default:
do_error(st, "FIXME: can't handle increment of vars of type %s \n",
type_names[vtype]);
return;
}
read_var(vasm, st, var);
// special magic in cast_to_void changes the node's vtype to NONE if
// this is in a void context.
if(is_post && expr->vtype != VM_TYPE_NONE)
vasm.insn(dup_insn);
vasm.insn(insn);
if(!is_post && expr->vtype != VM_TYPE_NONE)
vasm.insn(dup_insn);
write_var(vasm, st, var);
}
break;
case NODE_LIST:
if(expr->u.list == NULL) {
vasm.empty_list(); // special case that should disappear
} else {
list_node *list = expr->u.list;
bool not_first = false;
while(list != NULL) {
if(list->expr->node_type == NODE_CONST) {
// build a constant list segment
uint32_t ptr = build_const_list(vasm, st, list);
if(st.error != 0) return;
update_loc(st, expr);
// TODO: merge identical list constants
uint16_t global_const = vasm.add_global_ptr(ptr, VM_TYPE_LIST);
vasm.insn(MAKE_INSN(ICLASS_RDG_P, global_const));
} else {
expr_node *child = list->expr;
insn = get_insn_cast(child->vtype, VM_TYPE_LIST);
if(child->vtype == VM_TYPE_LIST) {
do_error(st, "ERROR: nested lists are not allowed\n");
return;
} else if(insn == 0) {
do_error(st, "ERROR: couldn't cast %s to list\n",
type_names[child->vtype]);
return;
}
assemble_expr(vasm, st, child);
if(st.error != 0) return;
update_loc(st, expr);
vasm.insn(insn);
list = list->next;
}
if(not_first) {
vasm.insn(INSN_ADD_LL);
}
not_first = true;
if(vasm.get_error() != NULL) {
do_error(st, "ASSEMBLER ERROR: %s\n", vasm.get_error());
return;
}
}
}
break;
default: