forked from koturn/Whitespace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
whitespace.c
1644 lines (1503 loc) · 41.4 KB
/
whitespace.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*!
* @file whitespace.c
* @brief An interpreter and C-translator of Whitespace
* @author koturn
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#if defined(_MSC_VER) && defined(_DEBUG)
# include <msvcdbg.h>
#endif
#ifndef MAX_SOURCE_SIZE
# define MAX_SOURCE_SIZE 65536
#endif
#ifndef MAX_BYTECODE_SIZE
# define MAX_BYTECODE_SIZE 1048576
#endif
#ifndef MAX_LABEL_LENGTH
# define MAX_LABEL_LENGTH 65536
#endif
#ifndef MAX_N_LABEL
# define MAX_N_LABEL 1024
#endif
#ifndef UNDEF_LIST_SIZE
# define UNDEF_LIST_SIZE 256
#endif
#ifndef STACK_SIZE
# define STACK_SIZE 65536
#endif
#ifndef HEAP_SIZE
# define HEAP_SIZE 65536
#endif
#ifndef CALL_STACK_SIZE
# define CALL_STACK_SIZE 65536
#endif
#ifndef WS_INT
# define WS_INT int
#endif
#ifndef WS_ADDR_INT
# define WS_ADDR_INT unsigned int
#endif
#ifndef INDENT_STR
# define INDENT_STR " "
#endif
#define TRUE 1
#define FALSE 0
#define UNDEF_ADDR ((WsAddrInt) -1)
#define LENGTHOF(array) (sizeof(array) / sizeof((array)[0]))
#define ADDR_DIFF(a, b) \
((const unsigned char *) (a) - (const unsigned char *) (b))
#define SWAP(type, a, b) \
do { \
type __tmp_swap_var__ = *(a); \
*(a) = *(b); \
*(b) = __tmp_swap_var__; \
} while (0)
enum OpCode {
FLOW_HALT = 0x00,
STACK_PUSH, STACK_DUP_N, STACK_DUP, STACK_SLIDE, STACK_SWAP, STACK_DISCARD,
ARITH_ADD, ARITH_SUB, ARITH_MUL, ARITH_DIV, ARITH_MOD,
HEAP_STORE, HEAP_LOAD,
FLOW_LABEL, FLOW_GOSUB, FLOW_JUMP, FLOW_BEZ, FLOW_BLTZ, FLOW_ENDSUB,
IO_PUT_CHAR, IO_PUT_NUM, IO_READ_CHAR, IO_READ_NUM
};
typedef WS_INT WsInt;
typedef WS_ADDR_INT WsAddrInt;
typedef struct {
const char *in_filename;
const char *out_filename;
int mode;
} Param;
typedef struct {
WsAddrInt addr;
int n_undef;
char *label;
WsAddrInt *undef_list;
} LabelInfo;
static void
parse_arguments(Param *param, int argc, char *argv[]);
static void
show_usage(const char *progname);
static int
read_file(FILE *fp, char *code, size_t length);
static void
execute(const unsigned char *bytecode);
static void
compile(unsigned char *bytecode, size_t *bytecode_size, const char *code);
static void
gen_stack_code(unsigned char **bytecode_ptr, const char **code_ptr);
static void
gen_arith_code(unsigned char **bytecode_ptr, const char **code_ptr);
static void
gen_heap_code(unsigned char **bytecode_ptr, const char **code_ptr);
static void
gen_io_code(unsigned char **bytecode_ptr, const char **code_ptr);
static void
gen_flow_code(unsigned char **bytecode_ptr, const char **code_ptr, unsigned char *base);
static void
process_label_define(unsigned char **bytecode_ptr, const char **code_ptr, unsigned char *base);
static void
process_label_jump(unsigned char **bytecode_ptr, const char **code_ptr, unsigned char *base);
static LabelInfo *
search_label(const char *label);
static void
add_label(const char *_label, WsAddrInt addr);
static void
add_undef_label(const char *_label, WsAddrInt pos);
static void
free_label_info_list(LabelInfo *label_info_list[]);
static void
stack_push(WsInt e);
static WsInt
stack_pop(void);
static void
stack_dup_n(size_t n);
static void
stack_slide(size_t n);
static void
stack_swap(void);
static int
read_nstr(const char **code_ptr);
static char *
read_label(const char **code_ptr);
static int
translate(FILE *fp, const char *code);
static void
print_stack_code(FILE *fp, const char **code_ptr);
static void
print_arith_code(FILE *fp, const char **code_ptr);
static void
print_heap_code(FILE *fp, const char **code_ptr);
static void
print_io_code(FILE *fp, const char **code_ptr);
static void
print_flow_code(FILE *fp, const char **code_ptr);
static void
print_code_header(FILE *fp);
static void
print_code_footer(FILE *fp);
static void
show_bytecode(const unsigned char *bytecode, size_t bytecode_size);
static void
show_mnemonic(FILE *fp, const unsigned char *bytecode, size_t bytecode_size);
static void
filter(FILE *fp, const char *code);
static WsInt stack[STACK_SIZE] = {0};
static size_t stack_idx = 0;
static LabelInfo *label_info_list[MAX_N_LABEL] = {NULL};
static size_t n_label_info = 0;
/*!
* @brief Entry point of thie program
* @param [in] argc The number of argument (include this program name)
* @param [in] argv The array off argument strings
* @return Status-code
*/
int
main(int argc, char *argv[])
{
static char code[MAX_SOURCE_SIZE] = {0};
static unsigned char bytecode[MAX_BYTECODE_SIZE] = {0};
Param param = {NULL, NULL, '*'};
FILE *ifp, *ofp;
size_t bytecode_size;
parse_arguments(¶m, argc, argv);
if (param.in_filename == NULL) {
fprintf(stderr, "Invalid arguments\n");
return EXIT_FAILURE;
}
if (!strcmp(param.in_filename, "-")) {
ifp = stdin;
} else if ((ifp = fopen(param.in_filename, "r")) == NULL) {
fprintf(stderr, "Unable to open file: %s\n", argv[1]);
return EXIT_FAILURE;
}
if (!read_file(ifp, code, LENGTHOF(code))) {
return EXIT_FAILURE;
}
if (ifp != stdin) {
fclose(ifp);
}
switch (param.mode) {
case 'b':
compile(bytecode, &bytecode_size, code);
show_bytecode(bytecode, bytecode_size);
break;
case 'f':
if (param.out_filename == NULL) {
filter(stdout, code);
} else {
if ((ofp = fopen(param.out_filename, "w")) == NULL) {
fprintf(stderr, "Unable to open file: %s\n", param.out_filename);
return EXIT_FAILURE;
}
filter(ofp, code);
fclose(ofp);
}
break;
case 'm':
compile(bytecode, &bytecode_size, code);
show_mnemonic(stdout, bytecode, bytecode_size);
break;
case 't':
if (param.out_filename == NULL) {
translate(stdout, code);
} else {
if ((ofp = fopen(param.out_filename, "w")) == NULL) {
fprintf(stderr, "Unable to open file: %s\n", param.out_filename);
return EXIT_FAILURE;
}
translate(ofp, code);
fclose(ofp);
}
break;
default:
compile(bytecode, &bytecode_size, code);
execute(bytecode);
break;
}
return EXIT_SUCCESS;
}
/*!
* @brief Parse comamnd-line arguments and set parameters.
*
* 'argv' is sorted after called getopt_long().
* @param [out] param Parameters of this program
* @param [in] argc A number of command-line arguments
* @param [in,out] argv Coomand-line arguments
*/
static void
parse_arguments(Param *param, int argc, char *argv[])
{
static const struct option opts[] = {
{"bytecode", no_argument, NULL, 'b'},
{"filter", no_argument, NULL, 'f'},
{"help", no_argument, NULL, 'h'},
{"mnemonic", no_argument, NULL, 'm'},
{"output", required_argument, NULL, 'o'},
{"translate", no_argument, NULL, 't'},
{0, 0, 0, 0} /* must be filled with zero */
};
int ret;
int optidx = 0;
while ((ret = getopt_long(argc, argv, "bfhmo:t", opts, &optidx)) != -1) {
switch (ret) {
case 'b': /* -b, --bytecode */
case 'f': /* -f, --filter */
case 'm': /* -m or --mnemonic */
case 't': /* -t or --translate */
param->mode = ret;
break;
case 'h': /* -h, --help */
show_usage(argv[0]);
exit(EXIT_SUCCESS);
case 'o': /* -o or --output */
param->out_filename = optarg;
break;
case '?': /* unknown option */
show_usage(argv[0]);
exit(EXIT_FAILURE);
}
}
if (optind != argc - 1) {
fputs("Please specify one whitespace source code\n", stderr);
show_usage(argv[0]);
exit(EXIT_FAILURE);
}
param->in_filename = argv[optind];
}
/*!
* @brief Show usage of this program and exit
* @param [in] progname A name of this program
*/
static void
show_usage(const char *progname)
{
printf(
"[Usage]\n"
" $ %s FILE [options]\n"
"[Options]\n"
" -b, --bytecode\n"
" Show code in hexadecimal\n"
" -f, --filter\n"
" Visualize whitespace source code\n"
" -h, --help\n"
" Show help and exit\n"
" -m, --mnemonic\n"
" Show byte code in mnemonic format\n"
" -o FILE, --output=FILE\n"
" Specify output filename\n"
" -t, --translate\n"
" Translate Whitespace to C source code\n", progname);
}
/* ------------------------------------------------------------------------- *
* Interpretor *
* ------------------------------------------------------------------------- */
/*!
* @brief Execute whitespace
* @param [in] bytecode Bytecode of whitespace
*/
static void
execute(const unsigned char *bytecode)
{
static int heap[HEAP_SIZE] = {0};
static size_t call_stack[CALL_STACK_SIZE] = {0};
size_t call_stack_idx = 0;
const unsigned char *base = bytecode;
int a = 0, b = 0;
for (; *bytecode; bytecode++) {
switch (*bytecode) {
case STACK_PUSH:
bytecode++;
stack_push(*((const WsInt *) bytecode));
bytecode += sizeof(WsInt) - 1;
break;
case STACK_DUP_N:
bytecode++;
stack_dup_n((size_t) *((const WsInt *) bytecode));
bytecode += sizeof(WsInt) - 1;
break;
case STACK_DUP:
stack_dup_n(0);
break;
case STACK_SLIDE:
bytecode++;
stack_slide((size_t) *((const WsInt *) bytecode));
bytecode += sizeof(WsInt) - 1;
break;
case STACK_SWAP:
stack_swap();
break;
case STACK_DISCARD:
stack_pop();
break;
case ARITH_ADD:
a = stack_pop();
b = stack_pop();
stack_push(b + a);
break;
case ARITH_SUB:
a = stack_pop();
b = stack_pop();
stack_push(b - a);
break;
case ARITH_MUL:
a = stack_pop();
b = stack_pop();
stack_push(b * a);
break;
case ARITH_DIV:
a = stack_pop();
b = stack_pop();
assert(b != 0);
stack_push(b / a);
break;
case ARITH_MOD:
a = stack_pop();
b = stack_pop();
assert(b != 0);
stack_push(b % a);
break;
case HEAP_STORE:
a = stack_pop();
b = stack_pop();
assert(0 <= b && b < (int) LENGTHOF(heap));
heap[b] = a;
break;
case HEAP_LOAD:
a = stack_pop();
assert(0 <= a && a < (int) LENGTHOF(heap));
stack_push(heap[a]);
break;
case FLOW_GOSUB:
call_stack[call_stack_idx++] = (size_t) (ADDR_DIFF(bytecode, base)) + sizeof(WsAddrInt);
bytecode++;
bytecode = &base[*((const WsAddrInt *) bytecode)] - 1;
break;
case FLOW_JUMP:
bytecode++;
bytecode = &base[*((const WsAddrInt *) bytecode)] - 1;
break;
case FLOW_BEZ:
if (!stack_pop()) {
bytecode++;
bytecode = &base[*((const WsAddrInt *) bytecode)] - 1;
} else {
bytecode += sizeof(WsAddrInt);
}
break;
case FLOW_BLTZ:
if (stack_pop() < 0) {
bytecode++;
bytecode = &base[*((const WsAddrInt *) bytecode)] - 1;
} else {
bytecode += sizeof(WsAddrInt);
}
break;
case FLOW_ENDSUB:
bytecode = &base[call_stack[--call_stack_idx]];
break;
case IO_PUT_CHAR:
putchar(stack_pop());
break;
case IO_PUT_NUM:
printf("%d", stack_pop());
break;
case IO_READ_CHAR:
a = stack_pop();
assert(0 <= a && a < (int) LENGTHOF(heap));
fflush(stdout);
heap[a] = getchar();
break;
case IO_READ_NUM:
a = stack_pop();
assert(0 <= a && a < (int) LENGTHOF(heap));
fflush(stdout);
scanf("%d", &heap[a]);
break;
case FLOW_HALT:
printf("HALT\n");
break;
default:
fprintf(stderr, "Undefined instruction is detected [%02x]\n", *bytecode);
}
}
}
/*!
* @brief Compile whitespace source code into bytecode
* @param [out] bytecode Bytecode buffer
* @param [in] code Brainfuck source code
*/
static void
compile(unsigned char *bytecode, size_t *bytecode_size, const char *code)
{
unsigned char *base = bytecode;
for (; *code != '\0'; code++) {
switch (*code) {
case ' ': /* Stack Manipulation */
gen_stack_code(&bytecode, &code);
break;
case '\t': /* Arithmetic, Heap Access or I/O */
switch (*++code) {
case ' ': /* Arithmetic */
gen_arith_code(&bytecode, &code);
break;
case '\t': /* Heap Access */
gen_heap_code(&bytecode, &code);
break;
case '\n': /* I/O */
gen_io_code(&bytecode, &code);
break;
}
break;
case '\n': /* Flow Control */
gen_flow_code(&bytecode, &code, base);
break;
}
}
*bytecode_size = (size_t) ADDR_DIFF(bytecode, base);
free_label_info_list(label_info_list);
}
/*!
* @brief Generate bytecode about stack manipulation
* @param [out] bytecode_ptr Pointer to bytecode buffer
* @param [in,out] code_ptr pointer to whitespace source code
*/
static void
gen_stack_code(unsigned char **bytecode_ptr, const char **code_ptr)
{
unsigned char *bytecode = *bytecode_ptr;
const char *code = *code_ptr;
switch (*++code) {
case ' ':
*bytecode++ = STACK_PUSH;
*((WsInt *) bytecode) = read_nstr(&code);
bytecode += sizeof(WsInt);
break;
case '\t':
switch (*++code) {
case ' ':
*bytecode++ = STACK_DUP_N;
*((WsInt *) bytecode) = read_nstr(&code);
bytecode += sizeof(WsInt);
break;
case '\t':
fputs("Undefined Stack manipulation command is detected: [S][TT]\n", stderr);
break;
case '\n':
*bytecode++ = STACK_SLIDE;
*((WsInt *) bytecode) = read_nstr(&code);
bytecode += sizeof(WsInt);
break;
}
break;
case '\n':
switch (*++code) {
case ' ':
*bytecode++ = STACK_DUP_N;
*((WsInt *) bytecode) = 0;
bytecode += sizeof(WsInt);
break;
case '\t':
*bytecode++ = STACK_SWAP;
break;
case '\n':
*bytecode++ = STACK_DISCARD;
break;
}
break;
}
*bytecode_ptr = bytecode;
*code_ptr = code;
}
/*!
* @brief Generate bytecode about arithmetic
* @param [out] bytecode_ptr Pointer to bytecode buffer
* @param [in,out] code_ptr pointer to whitespace source code
*/
static void
gen_arith_code(unsigned char **bytecode_ptr, const char **code_ptr)
{
unsigned char *bytecode = *bytecode_ptr;
const char *code = *code_ptr;
switch (*++code) {
case ' ':
switch (*++code) {
case ' ':
*bytecode++ = ARITH_ADD;
break;
case '\t':
*bytecode++ = ARITH_SUB;
break;
case '\n':
*bytecode++ = ARITH_MUL;
break;
}
break;
case '\t':
switch (*++code) {
case ' ':
*bytecode++ = ARITH_DIV;
break;
case '\t':
*bytecode++ = ARITH_MOD;
break;
case '\n':
fputs("Undefined arithmetic command is detected: [TS][TN]\n", stderr);
break;
}
break;
case '\n':
fputs("Undefined arithmetic command is detected: [TS][N]\n", stderr);
break;
}
*bytecode_ptr = bytecode;
*code_ptr = code;
}
/*!
* @brief Generate bytecode about heap access
* @param [out] bytecode_ptr Pointer to bytecode buffer
* @param [in,out] code_ptr pointer to whitespace source code
*/
static void
gen_heap_code(unsigned char **bytecode_ptr, const char **code_ptr)
{
unsigned char *bytecode = *bytecode_ptr;
const char *code = *code_ptr;
switch (*++code) {
case ' ':
*bytecode++ = HEAP_STORE;
break;
case '\t':
*bytecode++ = HEAP_LOAD;
break;
case '\n':
fputs("Undefined heap access command is detected: [TT][N]\n", stderr);
break;
}
*bytecode_ptr = bytecode;
*code_ptr = code;
}
/*!
* @brief Generate bytecode about flow control
* @param [out] bytecode_ptr Pointer to bytecode buffer
* @param [in,out] code_ptr pointer to whitespace source code
* @param [in] base Base address of the bytecode buffer
*/
static void
gen_flow_code(unsigned char **bytecode_ptr, const char **code_ptr, unsigned char *base)
{
unsigned char *bytecode = *bytecode_ptr;
const char *code = *code_ptr;
switch (*++code) {
case ' ':
switch (*++code) {
case ' ':
process_label_define(&bytecode, &code, base);
break;
case '\t':
*bytecode++ = FLOW_GOSUB;
process_label_jump(&bytecode, &code, base);
break;
case '\n':
*bytecode++ = FLOW_JUMP;
process_label_jump(&bytecode, &code, base);
break;
}
break;
case '\t':
switch (*++code) {
case ' ':
*bytecode++ = FLOW_BEZ;
process_label_jump(&bytecode, &code, base);
break;
case '\t':
*bytecode++ = FLOW_BLTZ;
process_label_jump(&bytecode, &code, base);
break;
case '\n':
*bytecode++ = FLOW_ENDSUB;
break;
}
break;
case '\n':
if (*++code == '\n') {
*bytecode++ = FLOW_HALT;
} else {
fputs("Undefined flow control command is detected: [N][S/T]\n", stderr);
}
break;
}
*bytecode_ptr = bytecode;
*code_ptr = code;
}
/*!
* @brief Generate bytecode about I/O
* @param [out] bytecode_ptr Pointer to bytecode buffer
* @param [in,out] code_ptr pointer to whitespace source code
*/
static void
gen_io_code(unsigned char **bytecode_ptr, const char **code_ptr)
{
unsigned char *bytecode = *bytecode_ptr;
const char *code = *code_ptr;
switch (*++code) {
case ' ':
switch (*++code) {
case ' ':
*bytecode++ = IO_PUT_CHAR;
break;
case '\t':
*bytecode++ = IO_PUT_NUM;
break;
case '\n':
fputs("Undefined I/O command is detected: [TN][SN]\n", stderr);
break;
}
break;
case '\t':
switch (*++code) {
case ' ':
*bytecode++ = IO_READ_CHAR;
break;
case '\t':
*bytecode++ = IO_READ_NUM;
break;
case '\n':
fputs("Undefined I/O command is detected: [TN][TN]\n", stderr);
break;
}
break;
case '\n':
fputs("Undefined I/O command is detected: [TN][N]\n", stderr);
break;
}
*bytecode_ptr = bytecode;
*code_ptr = code;
}
/*!
* @brief Check given label is already defined or not
*
* If label is already defined, return the label information
* @param [in] label Label you want to check
* @return Label information
*/
static LabelInfo *
search_label(const char *label)
{
size_t i;
for (i = 0; i < n_label_info; i++) {
if (!strcmp(label, label_info_list[i]->label)) {
return label_info_list[i];
}
}
return NULL;
}
/*!
* @brief Write where to jump to the bytecode
* @param [out] bytecode_ptr Pointer to bytecode buffer
* @param [in,out] code_ptr pointer to whitespace source code
* @param [in] base Base address of the bytecode buffer
*/
static void
process_label_define(unsigned char **bytecode_ptr, const char **code_ptr, unsigned char *base)
{
const char *code = *code_ptr;
unsigned char *bytecode = *bytecode_ptr;
char *label = read_label(&code);
LabelInfo *label_info = search_label(label);
if (label_info == NULL) {
add_label(label, (WsAddrInt) ADDR_DIFF(bytecode, base));
} else {
if (label_info->addr == UNDEF_ADDR) {
int i;
for (i = 0; i < label_info->n_undef; i++) {
*((WsAddrInt *) &base[label_info->undef_list[i]]) = (WsAddrInt) ADDR_DIFF(bytecode, base);
}
label_info->addr = (WsAddrInt) ADDR_DIFF(bytecode, base);
free(label_info->undef_list);
label_info->undef_list = NULL;
} else {
fputs("Duplicate label definition\n", stderr);
}
}
*code_ptr = code;
*bytecode_ptr = bytecode;
}
/*!
* @brief Write where to jump to the bytecode
*
* If label is not defined yet, write it after label is defined.
* @param [out] bytecode_ptr Pointer to bytecode buffer
* @param [in,out] code_ptr pointer to whitespace source code
* @param [in] base Base address of the bytecode buffer
*/
static void
process_label_jump(unsigned char **bytecode_ptr, const char **code_ptr, unsigned char *base)
{
const char *code = *code_ptr;
unsigned char *bytecode = *bytecode_ptr;
char *label = read_label(&code);
LabelInfo *label_info = search_label(label);
if (label_info == NULL) {
add_undef_label(label, (WsAddrInt) ADDR_DIFF(bytecode, base));
} else if (label_info->addr == UNDEF_ADDR) {
label_info->undef_list[label_info->n_undef++] = (WsAddrInt) ADDR_DIFF(bytecode, base);
} else {
*((WsAddrInt *) bytecode) = label_info->addr;
}
bytecode += sizeof(WsAddrInt);
*code_ptr = code;
*bytecode_ptr = bytecode;
}
/*!
* @brief Add label information to the label list
* @param [in] _label Label name
* @param [in] addr Label position
*/
static void
add_label(const char *_label, WsAddrInt addr)
{
char *label = (char *) calloc(strlen(_label) + 1, sizeof(char));
LabelInfo *label_info = (LabelInfo *) calloc(1, sizeof(LabelInfo));
if (label == NULL || label_info == NULL) {
fprintf(stderr, "Failed to allocate heap for label\n");
exit(EXIT_FAILURE);
}
strcpy(label, _label);
free(label_info->undef_list);
label_info->undef_list = NULL;
label_info->label = label;
label_info->addr = addr;
label_info->n_undef = 0;
label_info_list[n_label_info++] = label_info;
}
/*!
* @brief Add unseen/undefined label to the label list
* @param [in] _label Label name
* @param [in] pos The position given label was found
*/
static void
add_undef_label(const char *_label, WsAddrInt pos)
{
char *label = (char *) calloc(strlen(_label) + 1, sizeof(char));
LabelInfo *label_info = (LabelInfo *) calloc(1, sizeof(LabelInfo));
label_info->undef_list = (WsAddrInt *) calloc(UNDEF_LIST_SIZE, sizeof(WsAddrInt));
if (label == NULL || label_info == NULL || label_info->undef_list == NULL) {
fprintf(stderr, "Failed to allocate heap for label\n");
exit(EXIT_FAILURE);
}
strcpy(label, _label);
label_info->undef_list[0] = pos;
label_info->label = label;
label_info->addr = UNDEF_ADDR;
label_info->n_undef = 1;
label_info_list[n_label_info++] = label_info;
}
/*!
* @brief Free label informations
* @param [in] label_info_list Label list
*/
static void
free_label_info_list(LabelInfo *label_info_list[])
{
size_t i = 0;
for (i = 0; i < n_label_info; i++) {
free(label_info_list[i]->label);
free(label_info_list[i]->undef_list);
free(label_info_list[i]);
}
}
/* ------------------------------------------------------------------------- *
* Stack Manipulation (IMP: [Space]) *
* ------------------------------------------------------------------------- */
/*!
* @brief Push given number onto the stack
* @param [in] e A number you want to push onto the stack
*/
static void
stack_push(WsInt e)
{
assert(stack_idx < LENGTHOF(stack));
stack[stack_idx++] = e;
}
/*!
* @brief Pop out one element from the top of the stack
* @return An element of the top of the stack
*/
static WsInt
stack_pop(void)
{
assert(stack_idx > 0);
return stack[--stack_idx];
}
/*!
* @brief Copy the nth item on the stack onto the top of the stack
*/
static void
stack_dup_n(size_t n)
{
assert(n < stack_idx && stack_idx < LENGTHOF(stack) - 1);
stack[stack_idx] = stack[stack_idx - (n + 1)];
stack_idx++;
}
/*!
* @brief Slide n items off the stack, keeping the top item
* @param [in] n The number of items you want to slide off the stack
*/
static void
stack_slide(size_t n)
{
assert(stack_idx > n);
stack[stack_idx - (n + 1)] = stack[stack_idx - 1];
stack_idx -= n;
}
/*!
* @brief Swap the top two items on the stack
*/
static void
stack_swap(void)
{
assert(stack_idx > 1);
SWAP(int, &stack[stack_idx - 1], &stack[stack_idx - 2]);
}
/*!
* @brief Read whitespace-source code characters and push into given array.
* @param [in,out] fp File pointer to the whitespace source code
* @param [out] code The array you want to store the source code
* @param [in] length Max size of given array of code
* @return Status-code
*/
static int
read_file(FILE *fp, char *code, size_t length)
{
int ch;
size_t cnt = 0;
for (; (ch = fgetc(fp)) != EOF; cnt++) {
if (cnt > length) {
fprintf(stderr, "Buffer overflow!\n");
return FALSE;
}
switch (ch) {
case ' ':
case '\n':
case '\t':
*code++ = (char) ch;
break;
}