-
Notifications
You must be signed in to change notification settings - Fork 1
/
vm.c
1570 lines (1504 loc) · 48.7 KB
/
vm.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 "vm.h"
#include "codegen.h"
#include "memory.h"
#include "error.h"
#include "stdint.h"
#include "math.h"
#include "global.h"
#include "native.h"
#include "imports.h"
#include "table.h"
#include "struct.h"
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <limits.h>
#include <stdarg.h>
// Forward Declarations
static struct data eval_binop(struct vm * vm, enum vm_operator op, struct data a, struct data b);
static struct data eval_uniop(struct vm * vm, enum vm_operator op, struct data a);
static struct data type_of(struct data a);
static char* type_of_str(struct data a);
static struct data size_of(struct data a);
static struct data value_of(struct data a);
static struct data char_of(struct data a);
struct vm *vm_init() {
struct vm* vm = safe_malloc(sizeof(*vm));
srand(time(NULL));
vm->bytecode = 0;
vm->instruction_ptr = 0;
vm->bytecode_size = 0;
vm->last_pushed_identifier = 0;
vm->line = 0;
vm->memory = memory_init();
return vm;
}
void vm_destroy(struct vm * vm) {
memory_destroy(vm->memory);
safe_free(vm);
}
void vm_cleanup_if_repl(struct vm * vm) {
// bytecode could be null if codegen didn't generate anything
if (vm->bytecode) safe_free(vm->bytecode);
}
#define num_args(...) (sizeof((char*[]){__VA_ARGS__})/sizeof(char*))
#define first_that(memory, condition, ...) first_that_impl(memory, condition, num_args(__VA_ARGS__), __VA_ARGS__)
static char* first_that_impl(struct memory* memory, bool (*condition)(struct memory *, char*), int count, ...) {
va_list ap;
va_start(ap, count);
for (int i = 0; i < count; i++) {
char* s = va_arg(ap, char*);
if (condition(memory, s)) {
return s;
}
}
va_end(ap);
return 0;
}
static bool _id_exist(struct memory* memory, char* fn) {
return id_exist(memory, fn, true);
}
static char* get_binary_overload_name(enum vm_operator op, struct data a, struct data b) {
char* type_a = type_of_str(a);
char* type_b = type_of_str(b);
char* fn_name = safe_concat(OPERATOR_OVERLOAD_PREFIX, type_a,
operator_string[op], type_b);
return fn_name;
}
static char* get_unary_overload_name(enum vm_operator op, struct data a) {
char* type_a = type_of_str(a);
char* fn_name = safe_concat(OPERATOR_OVERLOAD_PREFIX,
operator_string[op], type_a);
return fn_name;
}
static char* get_print_overload_name(struct data a) {
char* type_a = type_of_str(a);
char* fn_name = safe_concat(OPERATOR_OVERLOAD_PREFIX "@", type_a);
return fn_name;
}
address vm_load_code(struct vm* vm, uint8_t* new_bytecode, size_t size, bool append) {
if (get_settings_flag(SETTINGS_DRY_RUN)) {
return 0;
}
// Verify Header
address start_at;
size_t saved_size = vm->bytecode_size;
if (!append) {
if (vm->bytecode) {
safe_free(vm->bytecode);
}
vm->bytecode = new_bytecode;
start_at = verify_header(vm->bytecode, size);
}
else {
// REPL Bytecode has no headers!
// Resize Bytecode Block, Offset New Addresses, Push to End
vm->bytecode_size += size;
if (vm->bytecode) {
// This gets rid of the OP_HALT from the previous chain of BC
vm->bytecode_size--;
vm->bytecode = safe_realloc(vm->bytecode, vm->bytecode_size * sizeof(uint8_t));
}
else {
vm->bytecode = safe_malloc(vm->bytecode_size * sizeof(uint8_t));
}
if (saved_size != 0) {
start_at = saved_size - 1;
}
else {
start_at = 0;
}
offset_addresses(new_bytecode, size, start_at);
for (size_t i = 0; i < size; i++) {
vm->bytecode[start_at + i] = new_bytecode[i];
}
}
return start_at;
}
void vm_run_instruction(struct vm* vm, enum opcode op) {
switch (op) {
case OP_PUSH: {
struct data t = get_data(vm->bytecode + vm->instruction_ptr, &vm->instruction_ptr);
// t will never be a reference type
struct data d;
if (t.type == D_IDENTIFIER) {
if (streq(t.value.string, "time")) {
d = time_data();
}
else {
struct data* value = get_address_of_id(vm->memory, t.value.string, true, NULL);
if (!value) {
error_runtime(vm->memory, vm->line, MEMORY_ID_NOT_FOUND, t.value.string);
break;
}
d = copy_data(*value);
}
}
else {
d = copy_data(t);
}
vm->last_pushed_identifier = t.value.string;
push_arg(vm->memory, d);
break;
}
case OP_BIN: {
enum vm_operator op = vm->bytecode[vm->instruction_ptr++];
struct data a = pop_arg(vm->memory, vm->line);
struct data b = pop_arg(vm->memory, vm->line);
struct data any_d = any_data();
char* a_and_b = get_binary_overload_name(op, a, b);
char* any_a = get_binary_overload_name(op, any_d, b);
char* any_b = get_binary_overload_name(op, a, any_d);
destroy_data_runtime(vm->memory, &any_d);
char* fn_name = first_that(vm->memory, _id_exist, a_and_b, any_a, any_b);
if (fn_name) {
push_arg(vm->memory, make_data(D_END_OF_ARGUMENTS, data_value_num(0)));
push_arg(vm->memory, b);
push_arg(vm->memory, a);
push_arg(vm->memory, copy_data(*get_address_of_id(vm->memory, fn_name, true, NULL)));
safe_free(a_and_b);
safe_free(any_a);
safe_free(any_b);
goto wendy_vm_call;
}
else {
push_arg(vm->memory, eval_binop(vm, op, a, b));
destroy_data_runtime(vm->memory, &a);
destroy_data_runtime(vm->memory, &b);
}
safe_free(a_and_b);
safe_free(any_a);
safe_free(any_b);
break;
}
case OP_UNA: {
enum vm_operator op = vm->bytecode[vm->instruction_ptr++];
struct data a = pop_arg(vm->memory, vm->line);
char* fn_name = get_unary_overload_name(op, a);
if (id_exist(vm->memory, fn_name, true)) {
push_arg(vm->memory, make_data(D_END_OF_ARGUMENTS, data_value_num(0)));
push_arg(vm->memory, a);
push_arg(vm->memory, copy_data(*get_address_of_id(vm->memory, fn_name, true, NULL)));
safe_free(fn_name);
goto wendy_vm_call;
}
else {
// Uni-op has a special spread vm_operator that returns noneret
struct data result = eval_uniop(vm, op, a);
push_arg(vm->memory, result);
destroy_data_runtime(vm->memory, &a);
}
safe_free(fn_name);
break;
}
case OP_SRC: {
void* ad = &vm->bytecode[vm->instruction_ptr];
vm->line = get_address(ad, &vm->instruction_ptr);
break;
}
case OP_NATIVE: {
void* ag = &vm->bytecode[vm->instruction_ptr];
address args = get_address(ag, &vm->instruction_ptr);
char* name = get_string(vm->bytecode + vm->instruction_ptr, &vm->instruction_ptr);
native_call(vm, name, args);
break;
}
case OP_DECL: {
char *id = get_string(vm->bytecode + vm->instruction_ptr, &vm->instruction_ptr);
if (id_exist_local_frame_ignore_closure(vm->memory, id)) {
error_runtime(vm->memory, vm->line, VM_VAR_DECLARED_ALREADY, id);
return;
}
struct data* result = push_stack_entry(vm->memory, id, vm->line);
push_arg(vm->memory, make_data(D_INTERNAL_POINTER, data_value_ptr(result)));
vm->last_pushed_identifier = id;
break;
}
case OP_WHERE: {
char* id = get_string(vm->bytecode + vm->instruction_ptr, &vm->instruction_ptr);
vm->last_pushed_identifier = id;
struct data* result = get_address_of_id(vm->memory, id, true, NULL);
if (!result) {
error_runtime(vm->memory, vm->line, MEMORY_ID_NOT_FOUND, id);
break;
}
push_arg(vm->memory, make_data(D_INTERNAL_POINTER,
data_value_ptr(result)
));
break;
}
case OP_IMPORT: {
char* name = get_string(vm->bytecode + vm->instruction_ptr, &vm->instruction_ptr);
address a = get_address(vm->bytecode + vm->instruction_ptr, &vm->instruction_ptr);
if (has_already_imported_library(name)) {
vm->instruction_ptr = a;
}
else {
add_imported_library(name);
}
break;
}
case OP_ARGCLN: {
// TODO: 128 limit here seems a bit arbitrary and we
struct data* extra_args = wendy_list_malloc(vm->memory, 128);
size_t count = 0;
while (top_arg(vm->memory, vm->line)->type != D_END_OF_ARGUMENTS) {
if (top_arg(vm->memory, vm->line)->type == D_NAMED_ARGUMENT_NAME) {
struct data identifier = pop_arg(vm->memory, vm->line);
struct data* loc = get_address_of_id(vm->memory, identifier.value.string, false, NULL);
if (!loc) {
error_runtime(vm->memory, vm->line, MEMORY_ID_NOT_FOUND, identifier.value.string);
break;
}
destroy_data_runtime(vm->memory, loc);
*loc = pop_arg(vm->memory, vm->line);
destroy_data_runtime(vm->memory, &identifier);
}
else {
extra_args[count + 1] = pop_arg(vm->memory, vm->line);
count += 1;
}
}
// We need to re-write the list counter
((struct list_header*) extra_args[0].value.reference)->size = count;
// Assign "arguments" variable with rest of the arguments.
*push_stack_entry(vm->memory, "arguments", vm->line) =
make_data(D_LIST, data_value_ptr(extra_args));
// Pop End of Arguments
struct data eoargs = pop_arg(vm->memory, vm->line);
destroy_data_runtime(vm->memory, &eoargs);
break;
}
case OP_RET: {
size_t trace = vm->memory->call_stack_pointer - 1;
while (vm->memory->call_stack[trace].is_automatic) {
trace -= 1;
}
if (trace == 0) {
goto VM_OP_HALT;
}
pop_frame(vm->memory, true, &vm->instruction_ptr);
break;
}
case OP_INC: {
struct data ptr = pop_arg(vm->memory, vm->line);
if (ptr.type != D_INTERNAL_POINTER) {
error_runtime(vm->memory, vm->line, VM_INTERNAL_ERROR, "INC on non-pointer");
return;
}
struct data* arg = ptr.value.reference;
if (arg->type != D_NUMBER) {
error_runtime(vm->memory, vm->line, VM_TYPE_ERROR, "INC");
return;
}
arg->value.number += 1;
break;
}
case OP_DUPTOP: {
struct data* top = top_arg(vm->memory, vm->line);
push_arg(vm->memory, copy_data(*top));
break;
}
case OP_POP: {
pop_arg(vm->memory, vm->line);
break;
}
case OP_ROTTWO: {
struct data first = pop_arg(vm->memory, vm->line);
struct data second = pop_arg(vm->memory, vm->line);
push_arg(vm->memory, first);
push_arg(vm->memory, second);
break;
}
case OP_MKTBL: {
size_t size = get_address(&vm->bytecode[vm->instruction_ptr], &vm->instruction_ptr);
struct table* table = table_create();
struct data* table_storage = refcnt_malloc(vm->memory, 1);
table_storage[0] = make_data(D_TABLE_INTERNAL_POINTER, data_value_ptr((struct data*) table));
for (size_t i = 0; i < size; i++) {
struct data key = pop_arg(vm->memory, vm->line);
struct data data;
if (key.type != D_TABLE_KEY) {
// next is the value at the key
data = key;
key = pop_arg(vm->memory, vm->line);
wendy_assert(key.type == D_TABLE_KEY, "MKTBL entry is not an Table Key type, but is %s", data_string[key.type]);
}
else {
data = none_data();
}
struct data* _data = table_insert(table, key.value.string, vm->memory);
*_data = data;
destroy_data_runtime(vm->memory, &key);
}
struct data reference = make_data(D_TABLE, data_value_ptr(table_storage));
push_arg(vm->memory, reference);
break;
}
case OP_DEC: {
struct data ptr = pop_arg(vm->memory, vm->line);
if (ptr.type != D_INTERNAL_POINTER) {
error_runtime(vm->memory, vm->line, VM_INTERNAL_ERROR, "DEC on non-pointer");
return;
}
struct data* arg = ptr.value.reference;
if (arg->type != D_NUMBER) {
error_runtime(vm->memory, vm->line, VM_TYPE_ERROR, "DEC");
return;
}
arg->value.number -= 1;
break;
}
case OP_FRM: {
push_auto_frame(vm->memory, vm->instruction_ptr, "automatic", vm->line);
break;
}
case OP_END: {
pop_frame(vm->memory, false, &vm->instruction_ptr);
break;
}
case OP_MKREF: {
enum data_type type = vm->bytecode[vm->instruction_ptr++];
size_t size = get_address(&vm->bytecode[vm->instruction_ptr], &vm->instruction_ptr);
struct data reference = make_data(type, data_value_ptr(NULL));
if (!is_reference(reference)) {
error_runtime(vm->memory, vm->line, VM_INTERNAL_ERROR,
"MKREF called on non-reference type");
return;
}
struct data* storage = refcnt_malloc(vm->memory, size);
// can't simply check additional_space because spread could
// expand to 1 element too
bool has_spread = false;
size_t additional_space = 0;
size_t i = size;
while (i > 0) {
struct data next = pop_arg(vm->memory, vm->line);
storage[i - 1] = next;
i -= 1;
if (next.type == D_SPREAD) {
additional_space += size_of(*next.value.reference).value.number - 1;
has_spread = true;
}
}
// Make this valid, codegen generates a number instead of a pointer
if (type == D_LIST) {
size_t list_size = storage[0].value.number;
storage[0] = list_header_data(list_size, list_size);
}
if (has_spread) {
struct data* new_storage =
refcnt_malloc(vm->memory, size + additional_space);
size_t j = 0;
for (size_t i = 0; i < size; i++) {
if (storage[i].type == D_SPREAD) {
struct data spread = storage[i].value.reference[0];
if (spread.type == D_LIST) {
for (size_t k = 0; k < wendy_list_size(&spread); k++) {
new_storage[j++] = copy_data(spread.value.reference[k + 1]);
}
}
else if (spread.type == D_RANGE) {
int start = range_start(spread);
int end = range_end(spread);
for (int k = start; k != end; start < end ? k++ : k--) {
new_storage[j++] = make_data(D_NUMBER, data_value_num(k));
}
}
else if (spread.type == D_STRING) {
size_t len = strlen(spread.value.string);
for (size_t k = 0; k < len; k++) {
struct data str = make_data(D_STRING, data_value_str(" "));
str.value.string[0] = spread.value.string[k];
new_storage[j++] = str;
}
}
}
else {
new_storage[j++] = storage[i];
}
}
refcnt_free(vm->memory, storage);
storage = new_storage;
if (storage[0].type == D_LIST_HEADER) {
// - 1 for header
storage[0] = list_header_data(size + additional_space - 1,
size + additional_space - 1);
}
}
reference.value.reference = storage;
push_arg(vm->memory, reference);
break;
}
case OP_NTHPTR: {
struct data number = pop_arg(vm->memory, vm->line);
struct data list = pop_arg(vm->memory, vm->line);
if (number.type != D_NUMBER && number.type != D_RANGE) {
error_runtime(vm->memory, vm->line, VM_INVALID_LVALUE_LIST_SUBSCRIPT);
goto nthptr_cleanup;
}
if (list.type != D_LIST) {
error_runtime(vm->memory, vm->line, VM_NOT_A_LIST);
goto nthptr_cleanup;
}
struct data* list_data = list.value.reference;
if (list_data->type != D_LIST_HEADER) {
error_runtime(vm->memory, vm->line, VM_INTERNAL_ERROR, "List doesn't point to list header!");
goto nthptr_cleanup;
}
size_t list_size = wendy_list_size(&list);
if (number.type == D_NUMBER) {
if (number.value.number >= list_size) {
error_runtime(vm->memory, vm->line, VM_LIST_REF_OUT_RANGE);
goto nthptr_cleanup;
}
push_arg(vm->memory, make_data(D_INTERNAL_POINTER,
data_value_ptr(&list_data[(int)number.value.number + 1])));
}
else if (number.type == D_RANGE) {
int start = range_start(number);
int end = range_end(number);
// Test for end is different because end is exclusive
if (start < 0 || end < -1 || start >= (int) list_size || end > (int) list_size) {
error_runtime(vm->memory, vm->line, VM_LIST_REF_OUT_RANGE);
goto nthptr_cleanup;
}
struct data *internal = refcnt_malloc(vm->memory, 2);
internal[0] = copy_data(list);
internal[1] = copy_data(number); // which is a range
push_arg(vm->memory, make_data(D_LIST_RANGE_LVALUE,
data_value_ptr(internal)));
}
nthptr_cleanup:
destroy_data_runtime(vm->memory, &list);
destroy_data_runtime(vm->memory, &number);
break;
}
case OP_CLOSURE: {
// create_closure() could return NULL when there's no closure, so the
// refcnt code is structured to ignore null pointer references
push_arg(vm->memory, make_data(D_CLOSURE, data_value_ptr(create_closure(vm->memory))));
break;
}
case OP_MEMPTR: {
// Member Pointer
// Structs can only modify Static members, instances modify instance
// members.
// Either will be allowed to look through static parameters.
struct data instance = pop_arg(vm->memory, vm->line);
char* member = get_string(vm->bytecode + vm->instruction_ptr, &vm->instruction_ptr);
if (instance.type != D_STRUCT &&
instance.type != D_STRUCT_INSTANCE &&
instance.type != D_TABLE) {
if (instance.type == D_NONERET) {
error_runtime(vm->memory, vm->line, VM_NOT_A_STRUCT_MAYBE_FORGOT_RET_THIS);
} else {
error_runtime(vm->memory, vm->line, VM_NOT_A_STRUCT);
}
destroy_data_runtime(vm->memory, &instance);
break;
}
if (instance.type == D_TABLE) {
struct table* table = (struct table*) instance.value.reference[0].value.reference;
if (!table_exist(table, member)) {
struct data type = type_of(instance);
error_runtime(vm->memory, vm->line, VM_MEMBER_NOT_EXIST, member, type.value.string);
destroy_data_runtime(vm->memory, &type);
break;
}
push_arg(vm->memory,
make_data(D_INTERNAL_POINTER, data_value_ptr(
table_find(table, member)
)
)
);
}
else {
// Struct or struct instance
struct data* ptr = struct_get_field(vm, instance, member);
if (!ptr) {
struct data type = type_of(instance);
error_runtime(vm->memory, vm->line, VM_MEMBER_NOT_EXIST, member, type.value.string);
destroy_data_runtime(vm->memory, &type);
}
else {
push_arg(vm->memory, make_data(D_INTERNAL_POINTER, data_value_ptr(ptr)));
}
}
destroy_data_runtime(vm->memory, &instance);
break;
}
case OP_JMP: {
address addr = get_address(&vm->bytecode[vm->instruction_ptr], &vm->instruction_ptr);
vm->instruction_ptr = addr;
break;
}
case OP_JIF: {
// Jump IF False Instruction
struct data top = pop_arg(vm->memory, vm->line);
address addr = get_address(&vm->bytecode[vm->instruction_ptr], &vm->instruction_ptr);
if (top.type != D_TRUE && top.type != D_FALSE) {
error_runtime(vm->memory, vm->line, VM_COND_EVAL_NOT_BOOL);
}
if (top.type == D_FALSE) {
vm->instruction_ptr = addr;
}
destroy_data_runtime(vm->memory, &top);
break;
}
case OP_CALL:
wendy_vm_call: {
struct data top = pop_arg(vm->memory, vm->line);
if (top.type != D_FUNCTION && top.type != D_STRUCT && top.type != D_STRUCT_FUNCTION) {
error_runtime(vm->memory, vm->line, VM_FN_CALL_NOT_FN);
destroy_data_runtime(vm->memory, &top);
break;
}
struct data boundName = top.value.reference[2];
char* function_disp = safe_malloc((128 + strlen(boundName.value.string)) * sizeof(char));
function_disp[0] = 0;
if (boundName.value.string && streq(boundName.value.string, "self")) {
sprintf(function_disp, "annonymous:0x%X", vm->instruction_ptr);
}
else {
sprintf(function_disp, "%s:0x%X", boundName.value.string, vm->instruction_ptr);
}
push_frame(vm->memory, function_disp, vm->instruction_ptr, vm->line);
safe_free(function_disp);
if (top.type == D_STRUCT) {
// Calling Struct Constructor
struct data* metadata = top.value.reference;
struct data old_top = top;
// Select `init` function.
wendy_assert(metadata[2].type == D_TABLE, "not a table!");
struct table* static_table = (struct table*) metadata[2].value.reference[0].value.reference;
// Better Exist
wendy_assert(table_exist(static_table, "init"), "struct static table has no init!");
top = copy_data(*table_find(static_table, "init"));
if (top.type != D_FUNCTION) {
error_runtime(vm->memory, vm->line, VM_STRUCT_CONSTRUCTOR_NOT_A_FUNCTION);
destroy_data_runtime(vm->memory, &top);
break;
}
top.type = D_STRUCT_FUNCTION;
// Struct ref, passed into init as *Class
push_arg(vm->memory, old_top);
push_arg(vm->memory,
make_data(D_STRUCT_INSTANCE,
data_value_ptr(
struct_create_instance(vm, metadata)
)
)
);
}
struct data addr = top.value.reference[0];
if (addr.type != D_INSTRUCTION_ADDRESS) {
error_runtime(vm->memory, vm->line, VM_INTERNAL_ERROR, "Address of function is not D_INSTRUCTION_ADDRESS");
destroy_data_runtime(vm->memory, &top);
break;
}
vm->instruction_ptr = (address) addr.value.number;
// Resolve Spread Objects in the Call List
size_t ptr = vm->memory->working_stack_pointer - 1;
bool has_spread = false;
size_t additional_size = 0;
size_t argc = 0;
while (vm->memory->working_stack[ptr].type != D_END_OF_ARGUMENTS) {
if (vm->memory->working_stack[ptr].type == D_SPREAD) {
has_spread = true;
additional_size +=
size_of(*vm->memory->working_stack[ptr].value.reference).value.number - 1;
}
argc += 1;
ptr -= 1;
}
if (has_spread) {
// Expand the Stack if required
ensure_working_stack_size(vm->memory, additional_size);
// In-place move from the back
size_t og_ptr = vm->memory->working_stack_pointer - 1;
size_t new_ptr = vm->memory->working_stack_pointer + additional_size - 1;
for (; vm->memory->working_stack[og_ptr].type != D_END_OF_ARGUMENTS; og_ptr--) {
if (vm->memory->working_stack[og_ptr].type == D_SPREAD) {
struct data og_spread = vm->memory->working_stack[og_ptr];
struct data spread = og_spread.value.reference[0];
if (spread.type == D_LIST) {
for (size_t k = 0; k < wendy_list_size(&spread); k++) {
vm->memory->working_stack[new_ptr--] = copy_data(spread.value.reference[k + 1]);
}
}
else if (spread.type == D_RANGE) {
int start = range_start(spread);
int end = range_end(spread);
for (int k = start; k != end; start < end ? k++ : k--) {
vm->memory->working_stack[new_ptr--] = make_data(D_NUMBER, data_value_num(k));
}
}
else if (spread.type == D_STRING) {
size_t len = strlen(spread.value.string);
for (size_t k = 0; k < len; k++) {
struct data str = make_data(D_STRING, data_value_str(" "));
str.value.string[0] = spread.value.string[k];
vm->memory->working_stack[new_ptr--] = str;
}
}
destroy_data_runtime(vm->memory, &og_spread);
}
else {
vm->memory->working_stack[new_ptr--] = vm->memory->working_stack[og_ptr];
}
}
vm->memory->working_stack_pointer += additional_size;
}
if (top.type == D_STRUCT_FUNCTION) {
// Either we pushed the new instance on the stack on top, or
// codegen generated the instance on the top.
struct data instance = pop_arg(vm->memory, vm->line);
if (instance.type != D_STRUCT_INSTANCE &&
instance.type != D_STRUCT &&
instance.type != D_TABLE) {
error_runtime(vm->memory, vm->line, VM_INTERNAL_ERROR, "D_STRUCT_FUNCTION encountered but top of stack is not a instance nor a struct.");
destroy_data_runtime(vm->memory, &top);
destroy_data_runtime(vm->memory, &instance);
break;
}
*push_stack_entry(vm->memory, "this", vm->line) = instance;
}
// Push closure variables
struct data *list_data = top.value.reference[1].value.reference;
size_t size = wendy_list_size(&top.value.reference[1]);
// Move the pointer to the "first" item, because the 0th item is the list-header
list_data += 1;
for (size_t i = 0; i < size; i += 2) {
if (list_data[i].type != D_IDENTIFIER) {
error_runtime(vm->memory, vm->line, VM_INTERNAL_ERROR, "Did not find a D_IDENTIFIER in function closure");
destroy_data_runtime(vm->memory, &top);
break;
}
*push_closure_entry(vm->memory, list_data[i].value.string, vm->line) = copy_data(list_data[i + 1]);
}
// At this point, we put `top` back into the stack, so no need to destroy it
if (strcmp(boundName.value.string, "self") != 0) {
*push_stack_entry(vm->memory, "self", vm->line) = copy_data(top);
}
*push_stack_entry(vm->memory, boundName.value.string, vm->line) = top;
break;
}
case OP_WRITE: {
struct data ptr = pop_arg(vm->memory, vm->line);
if (ptr.type != D_INTERNAL_POINTER && ptr.type != D_LIST_RANGE_LVALUE) {
error_runtime(vm->memory, vm->line, VM_INTERNAL_ERROR, "WRITE on non-pointer");
destroy_data_runtime(vm->memory, &ptr);
return;
}
if (top_arg(vm->memory, vm->line)->type == D_END_OF_ARGUMENTS ||
top_arg(vm->memory, vm->line)->type == D_NAMED_ARGUMENT_NAME) {
if (ptr.value.reference->type == D_EMPTY) {
*(ptr.value.reference) = none_data();
}
break;
}
struct data value = pop_arg(vm->memory, vm->line);
// Since value is written back, we don't need to destroy it
if (value.type == D_NONERET) {
error_runtime(vm->memory, vm->line, VM_ASSIGNING_NONERET);
destroy_data_runtime(vm->memory, &value);
break;
}
if (ptr.type == D_INTERNAL_POINTER) {
destroy_data_runtime(vm->memory, &ptr.value.reference[0]);
*(ptr.value.reference) = value;
if (ptr.value.reference->type == D_FUNCTION) {
// Write Name to Function
char* bind_name = vm->last_pushed_identifier;
struct data* fn_data = ptr.value.reference->value.reference;
fn_data[2].value.string = safe_realloc(
fn_data[2].value.string, strlen(bind_name) + 1);
strcpy(fn_data[2].value.string, bind_name);
}
// We stole value, so don't need to destroy it here
}
else if (ptr.type == D_LIST_RANGE_LVALUE) {
struct data list = ptr.value.reference[0];
struct data* list_data = list.value.reference;
struct data range = ptr.value.reference[1];
int start = range_start(range);
int end = range_end(range);
// No range checking needed here, OP_NTHPTR checks the bounds, and
// the rvalue side get's evaluated first, so no side effects can
// occur between NTHPTR and WRITE (I hope)...
if (value.type == D_LIST) {
size_t needed_size = abs(start - end);
size_t list_size = wendy_list_size(&value);
struct data* value_data = value.value.reference;
if (list_size != needed_size) {
error_runtime(vm->memory, vm->line, VM_LIST_RANGE_ASSIGN_SIZE_MISMATCH,
needed_size, list_size);
goto write_list_range_lvalue_cleanup;
}
int i = 0;
for (int k = start; k != end; start < end ? k++ : k--) {
destroy_data_runtime(vm->memory, &list_data[k + 1]);
list_data[k + 1] = copy_data(value_data[i + 1]);
i++;
}
}
else {
for (int k = start; k != end; start < end ? k++ : k--) {
destroy_data_runtime(vm->memory, &list_data[k + 1]);
list_data[k + 1] = copy_data(value);
}
}
write_list_range_lvalue_cleanup:
destroy_data_runtime(vm->memory, &value);
destroy_data_runtime(vm->memory, &ptr);
}
break;
}
case OP_OUT: {
struct data t = pop_arg(vm->memory, vm->line);
if (t.type != D_NONERET) {
char* fn_name = get_print_overload_name(t);
if (id_exist(vm->memory, fn_name, true)) {
push_arg(vm->memory, make_data(D_END_OF_ARGUMENTS, data_value_num(0)));
push_arg(vm->memory, t);
push_arg(vm->memory, copy_data(*get_address_of_id(vm->memory, fn_name, true, NULL)));
safe_free(fn_name);
/* This i-- allows the overloaded function to return
* a string / object and have that be the printed
* output, i.e. it will call function and execute
* the OP_OUT again */
vm->instruction_ptr--;
goto wendy_vm_call;
}
safe_free(fn_name);
print_data(&t);
}
destroy_data_runtime(vm->memory, &t);
break;
}
case OP_OUTL: {
struct data t = pop_arg(vm->memory, vm->line);
if (t.type != D_NONERET) {
char* fn_name = get_print_overload_name(t);
if (id_exist(vm->memory, fn_name, true)) {
push_arg(vm->memory, make_data(D_END_OF_ARGUMENTS, data_value_num(0)));
push_arg(vm->memory, t);
push_arg(vm->memory, copy_data(*get_address_of_id(vm->memory, fn_name, true, NULL)));
safe_free(fn_name);
/* This i-- allows the overloaded function to return
* a string / object and have that be the printed
* output, i.e. it will call function and execute
* the OP_OUT again */
vm->instruction_ptr--;
goto wendy_vm_call;
}
safe_free(fn_name);
print_data_inline(&t, stdout);
}
destroy_data_runtime(vm->memory, &t);
break;
}
case OP_IN: {
// Scan one line from the input.
struct data ptr = pop_arg(vm->memory, vm->line);
if (ptr.type != D_INTERNAL_POINTER) {
error_runtime(vm->memory, vm->line, VM_INTERNAL_ERROR, "INC on non-pointer");
return;
}
struct data* storage = ptr.value.reference;
destroy_data_runtime(vm->memory, storage);
char buffer[INPUT_BUFFER_SIZE];
while(!fgets(buffer, INPUT_BUFFER_SIZE, stdin)) {};
char* end_ptr = buffer;
errno = 0;
double d = strtod(buffer, &end_ptr);
// null terminator or newline character
if (errno != 0 || (*end_ptr != 0 && *end_ptr != 10)) {
size_t len = strlen(buffer);
// remove last newline
buffer[len - 1] = 0;
*storage = make_data(D_STRING, data_value_str(buffer));
}
else {
// conversion successful
*storage = make_data(D_NUMBER, data_value_num(d));
}
break;
}
case OP_HALT:
VM_OP_HALT:
return;
// No default: here because we want compiler to catch missing cases.
}
}
void vm_set_instruction_pointer(struct vm* vm, address start) {
vm->instruction_ptr = start;
}
void vm_run(struct vm *vm) {
if (get_settings_flag(SETTINGS_DRY_RUN)) {
return;
}
size_t starting_stack_pointer = vm->memory->call_stack_pointer;
for (;;) {
reset_error_flag();
enum opcode op = vm->bytecode[vm->instruction_ptr];
if (get_settings_flag(SETTINGS_TRACE_VM)) {
// This branch could slow down the VM but the CPU should branch
// predict after one or two iterations.
printf(BLU "<+%04X>: " RESET "%s\n", vm->instruction_ptr, opcode_string[op]);
}
vm->instruction_ptr += 1;
if (op == OP_HALT) return;
vm_run_instruction(vm, op);
if (vm->memory->call_stack_pointer < starting_stack_pointer) {
// We returned out of the starting stack frame
return;
}
if (get_error_flag()) {
clear_working_stack(vm->memory);
break;
}
}
}
static struct data eval_binop(struct vm * vm, enum vm_operator op, struct data a, struct data b) {
if (op == O_ELVIS) {
if (a.type == D_NONE) {
return copy_data(b);
}
return copy_data(a);
}
if (op == O_SUBSCRIPT) {
// Array Reference, or String
// A must be a list/string/range, b must be a number.
if (a.type != D_LIST && a.type != D_STRING && a.type != D_RANGE) {
error_runtime(vm->memory, vm->line, VM_TYPE_ERROR, operator_string[op]);
return none_data();
}
int list_size;
if (a.type == D_STRING) {
list_size = strlen(a.value.string);
}
else if (a.type == D_RANGE) {
list_size = abs(range_end(a) - range_start(a));
}
else {
list_size = wendy_list_size(&a);
}
if (b.type != D_NUMBER && b.type != D_RANGE) {
error_runtime(vm->memory, vm->line, VM_INVALID_LIST_SUBSCRIPT);
return none_data();
}
if ((b.type == D_NUMBER && (int)(b.value.number) >= list_size) ||
(b.type == D_RANGE &&
((range_start(b) > list_size || range_end(b) > list_size ||
range_start(b) < 0 || range_end(b) < 0)))) {
error_runtime(vm->memory, vm->line, VM_LIST_REF_OUT_RANGE);
return none_data();
}
if (b.type == D_NUMBER) {
if (a.type == D_STRING) {
struct data c = make_data(D_STRING, data_value_str("0"));
c.value.string[0] = a.value.string[(int)floor(b.value.number)];
return c;
}
else if (a.type == D_RANGE) {
int end = range_end(a);
int start = range_start(a);
int index = (int)floor(b.value.number);
if (start < end) {
return make_data(D_NUMBER, data_value_num(start + index));
}
else {
return make_data(D_NUMBER, data_value_num(start - index));
}
}
// Add 1 to offset because of the header.
int offset = floor(b.value.number) + 1;
struct data* list_data = a.value.reference;
return copy_data(list_data[offset]);
}
else {
int start = range_start(b);
int end = range_end(b);
int subarray_size = start - end;
if (subarray_size < 0) subarray_size *= -1;
if (a.type == D_STRING) {
struct data c = make_data(D_STRING, data_value_size(abs(start - end)));
int n = 0;
for (int i = start; i != end; start < end ? i++ : i--) {
c.value.string[n++] = a.value.string[i];
}
c.value.string[n] = 0;
return c;
}
else if (a.type == D_RANGE) {
// Range of a Range...
int a_start = range_start(a);
int a_end = range_end(a); UNUSED(a_end);
int b_start = range_start(b);
int b_end = range_end(b);
// Very relaxed about ranges, don't really care if it's out of
// bounds for now.
// if (a_start + b_start > a_end || a_start + b_end < a_start) {
// error_runtime(vm->memory, line, VM_RANGE_REF_OUT_OF_RANGE);
// }
return range_data(a_start + b_start, a_start + b_end);
}