-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.c
987 lines (877 loc) · 25 KB
/
parse.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
#include <stdlib.h> /* for strtol */
#include <stdio.h> /* for fopen, fclose, fgets */
#include <string.h> /* for strncpy and strncat */
#include <ctype.h> /* for isalpha and isalnum */
#include <limits.h> /* for LONG_MIN and LONG_MAX */
#include "consts.h"
#include "types.h"
#include "table.h"
#include "parse.h"
/* gloabl variables that the parsed file is stored in
* used by the output function in output.c */
full_instruction_t full_instructions[CODE_SECTION_MAX_LENGTH];
int full_instruction_index = 0;
char data_section[DATA_SECTION_MAX_LENGTH];
unsigned int data_index = 0;
unsigned int code_index = 0;
/* global variables used internaly for parsing */
static int label_defined;
static enum {
FIRST_PASS,
SECOND_PASS
} pass; /* parser pass number */
static char label_definition[MAX_LABEL_LENGTH];
static char label_declaration[MAX_LABEL_LENGTH];
static char *input_line = NULL; /* the current parsed line */
static char *input_line_start = NULL; /* the current parsed line start */
static char *input_filename = NULL; /* used for errors */
static unsigned int input_linenumber = 0; /* used for errors */
/* available instructions */
static instruction_t instructions[] = {
{"mov", IMMEDIATE_ADDRESS | DIRECT_ADDRESS | INDEX_ADDRESS | DIRECT_REGISTER_ADDRESS,
DIRECT_ADDRESS | INDEX_ADDRESS | DIRECT_REGISTER_ADDRESS,
2,
0},
{"cmp", IMMEDIATE_ADDRESS | DIRECT_ADDRESS | INDEX_ADDRESS | DIRECT_REGISTER_ADDRESS,
IMMEDIATE_ADDRESS | DIRECT_ADDRESS | INDEX_ADDRESS | DIRECT_REGISTER_ADDRESS,
2,
1},
{"add", IMMEDIATE_ADDRESS | DIRECT_ADDRESS | INDEX_ADDRESS | DIRECT_REGISTER_ADDRESS,
DIRECT_ADDRESS | INDEX_ADDRESS | DIRECT_REGISTER_ADDRESS,
2,
2},
{"sub", IMMEDIATE_ADDRESS | DIRECT_ADDRESS | INDEX_ADDRESS | DIRECT_REGISTER_ADDRESS,
DIRECT_ADDRESS | INDEX_ADDRESS | DIRECT_REGISTER_ADDRESS,
2,
3},
{"lea", DIRECT_ADDRESS | INDEX_ADDRESS | DIRECT_REGISTER_ADDRESS,
DIRECT_ADDRESS | INDEX_ADDRESS | DIRECT_REGISTER_ADDRESS,
2,
6},
{"not", NO_ADDRESS,
DIRECT_ADDRESS | INDEX_ADDRESS | DIRECT_REGISTER_ADDRESS,
1,
4},
{"clr", NO_ADDRESS,
DIRECT_ADDRESS | INDEX_ADDRESS | DIRECT_REGISTER_ADDRESS,
1,
5},
{"inc", NO_ADDRESS,
DIRECT_ADDRESS | INDEX_ADDRESS | DIRECT_REGISTER_ADDRESS,
1,
7},
{"dec", NO_ADDRESS,
DIRECT_ADDRESS | INDEX_ADDRESS | DIRECT_REGISTER_ADDRESS,
1,
8},
{"jmp", NO_ADDRESS,
DIRECT_ADDRESS | INDEX_ADDRESS | DIRECT_REGISTER_ADDRESS,
1,
9},
{"bne", NO_ADDRESS,
DIRECT_ADDRESS | INDEX_ADDRESS | DIRECT_REGISTER_ADDRESS,
1,
10},
{"red", NO_ADDRESS,
DIRECT_ADDRESS | INDEX_ADDRESS | DIRECT_REGISTER_ADDRESS,
1,
11},
{"prn", NO_ADDRESS,
IMMEDIATE_ADDRESS | DIRECT_ADDRESS | INDEX_ADDRESS | DIRECT_REGISTER_ADDRESS,
1,
12},
{"jsr", NO_ADDRESS,
DIRECT_ADDRESS,
1,
13},
{"rts", NO_ADDRESS,
NO_ADDRESS,
0,
14},
{"stop", NO_ADDRESS,
NO_ADDRESS,
0,
15},
};
/* =============================================
* =============================================
* Note: all parsing function start with parse_
* and have the same basic structure: on error
* they return 1 so logical operators where used
* to simplify parsing this way:
* || - means that there was no error next parsing
* function should be called
* ==============================================
* ============================================*/
/************************************************
* NAME: parse_error
* PARAMS: gripe - the error message
* DESCRIPTION: output an error message based
* on current parsed file, current
* parsed line and current parsed
* location on line
***********************************************/
void parse_error(char *gripe)
{
fprintf(stderr, "%s:%u:%u: error: %s\n",
input_filename,
input_linenumber,
(unsigned int)(input_line - input_line_start),
gripe);
}
/************************************************
* NAME: parse_string
* PARAMS: s - the string to parse
* RETURN VALUE: 1 on error, 0 on success
* DESCRIPTION: parse a string
***********************************************/
static int parse_string(char *s)
{
char gripe[MAX_LINE_LENGTH];
if (strncmp(input_line, s, strlen(s)) == 0) {
input_line += strlen(s);
return 0;
}
sprintf(gripe, "expected %s", s);
parse_error(gripe);
return 1;
}
/************************************************
* NAME: isblank
* PARAMS: c - checked character
* RETURN VALUE: 1 if blank
* DESCRIPTION: check if a charcater is a blank
* character
***********************************************/
static int isblank(int c)
{
return c == ' ' || c == '\t';
}
/************************************************
* NAME: parse_whitespace
* RETURN VALUE: 0 always (compatible parse
* function return value)
* DESCRIPTION: promote input_line to a nonblank
* character
***********************************************/
static int parse_whitespace(void)
{
while (isblank(*input_line)) {
input_line++;
}
return 0;
}
/************************************************
* NAME: parse_whitespace_must
* RETURN VALUE: 1 on error, 0 on success
* DESCRIPTION: parse all whitespace and report
* if there is no whitespace at all
************************************************/
static int parse_whitespace_must(void)
{
if (!isblank(*input_line)) {
parse_error("expected whitespace");
return 1;
}
return parse_whitespace();
}
/************************************************
* NAME: is_comment
* RETURN VALUE: 1 if the current parsed line is
* a comment, 0 otherwise
* DESCRIPTION: check if the current parsed line
* is a comment by checking the first
* character
************************************************/
static int is_comment(void)
{
return input_line[0] == ';';
}
/************************************************
* NAME: is_empty
* RETURN VALUE: 1 if the current parsed line is
* empty , 0 otherwise
* DESCRIPTION: an empty line is a line that
* contains only blank characters
************************************************/
static int is_empty(void)
{
char *p;
for (p = input_line; *p != '\n' && *p != '\0'; p++) {
if (!isblank(*p)) {
return 0;
}
}
return 1;
}
/************************************************
* NAME: parse_label
* RETURN VALUE: 0 on success, 1 otherwise
* PARAMS: name - parsed label
* DESCRIPTION: parse a label and return it in
* the name output parameter
************************************************/
static int parse_label(char *name)
{
/* used for getting the size of the length */
int label_length = 0;
char *label_begin = input_line;
if (!isalpha(*input_line)) {
parse_error("label must start with alphabetic character");
return 1;
}
/* skip all alphanumberic characters */
while (isalnum(*input_line)) {
input_line++;
label_length++;
}
if (label_length > MAX_LABEL_LENGTH) {
parse_error("label too long");
return 1;
}
/* copy the parsed string to the out parameter */
strncpy(name, label_begin, label_length);
/* put null at the end of the string */
name[label_length] = '\0';
return 0;
}
/************************************************
* NAME: parse_label_use
* RETURN VALUE: 0 on success, 1 otherwise
* PARAMS: name - parsed label
* DESCRIPTION: parse a label and check if it's
* installed on the second pass
************************************************/
static int parse_label_use(char *name)
{
if (parse_label(name)) {
return 1;
}
if (pass == SECOND_PASS && !lookup_label(name)) {
parse_error("label that isn't defined is used");
return 1;
}
return 0;
}
/************************************************
* NAME: parse_label_definition
* RETURN VALUE: 0 on success, 1 otherwise
* DESCRIPTION: parse a label and return it in
* the name output parameter
************************************************/
static int parse_label_definition(void)
{
if (strchr(input_line, ':')) {
label_defined = 1;
return parse_label(label_definition) || parse_string(":");
}
return 0;
}
/************************************************
* NAME: parse_label_declaration
* RETURN VALUE: 0 on success, 1 otherwise
* DESCRIPTION: parse the label declration
* (global variable)
************************************************/
static int parse_label_declaration(void)
{
return parse_label(label_declaration);
}
/************************************************
* NAME: parse_number
* RETURN VALUE: 0 on success, 1 otherwise
* PARAMS: x - number output parameter
* DESCRIPTION: parse an integer
************************************************/
static int parse_number(long *x)
{
char *p;
*x = strtol(input_line, &p, 10);
if (*x == LONG_MIN || *x == LONG_MAX) {
parse_error("long overflow or underflow");
return 1;
}
input_line = p;
return 0;
}
/************************************************
* NAME: parse_data_number
* RETURN VALUE: 0 on success, 1 otherwise
* DESCRIPTION: parse an integer into data section
************************************************/
static int parse_data_number(void)
{
long int x;
if (parse_number(&x)) {
return 1;
}
data_section[data_index] = x;
data_index++;
return 0;
}
/************************************************
* NAME: install_label_defintion
* RETURN VALUE: 0 on success, 1 otherwise
* PARAMS: section - the section associated with
* the label to install
* DESCRIPTION: install a label defintion
************************************************/
static int install_label_defintion(label_section_t section)
{
label_t *l;
/* look if the label is already installed */
l = lookup_label(label_definition);
if (l != NULL) {
if (l->type == EXTERNAL) {
parse_error("label declared as external " \
"cannot be defined");
return 1;
} else if (l->has_address) {
parse_error("label already defined");
return 1;
}
}
else {
if (install_label(label_definition, &l)) {
return 1;
}
l->type = REGULAR;
strncpy(l->name, label_definition, strlen(label_definition));
}
l->section = section;
l->address = section == CODE ? code_index : data_index;
l->has_address = 1;
return 0;
}
/************************************************
* NAME: install_label_declaration
* RETURN VALUE: 0 on success, 1 otherwise
* PARAMS: type - the type of the label to be
* installed
* DESCRIPTION: install a label declration
************************************************/
static int install_label_declaration(label_type_t type)
{
label_t *l;
/* check if the label is already installed
* and if so, check if it was declared before */
l = lookup_label(label_declaration);
if (l != NULL && l->type != REGULAR) {
parse_error("label already declared");
return 1;
}
else {
/* install the new label */
if (install_label(label_declaration, &l)) {
return 1;
}
l->has_address = 0;
strncpy(l->name, label_declaration, strlen(label_declaration));
}
l->type = type;
return 0;
}
/************************************************
* NAME: parse_data_directive
* RETURN VALUE: 0 on success, 1 otherwise
* DESCRIPTION: parse a data directive
************************************************/
static int parse_data_directive(void)
{
/* if a label is defined install it */
if (label_defined && install_label_defintion(DATA)) {
return 1;
}
/* parse at least one number */
if (parse_data_number()) {
return 1;
}
/* parse the rest of the numbers */
while (strchr(input_line, ',')) {
if (parse_whitespace() || \
parse_string(",") || \
parse_whitespace() || \
parse_data_number() || \
parse_whitespace()) {
return 1;
}
}
return 0;
}
/************************************************
* NAME: parse_string_directive
* RETURN VALUE: 0 on success, 1 otherwise
* DESCRIPTION: parse a string directive
************************************************/
static int parse_string_directive(void)
{
/* a string starts with " */
if (parse_string("\"")) {
return 1;
}
/* install the label if defined */
if (label_defined && install_label_defintion(DATA)) {
return 1;
}
/* parse all chaacters until " */
while (*input_line && *input_line != '"')
{
data_section[data_index] = *input_line;
data_index++;
input_line++;
}
/* expect a closing " */
if (parse_string("\"")) {
return 1;
}
/* close the string */
data_section[data_index] = '\0';
/* for last byte */
data_index++;
return 0;
}
/************************************************
* NAME: parse_entry_directive
* RETURN VALUE: 0 on success, 1 otherwise
* DESCRIPTION: parse an entry directive
************************************************/
static int parse_entry_directive(void)
{
return parse_label_declaration() || install_label_declaration(ENTRY);
}
/************************************************
* NAME: parse_extern_directive
* RETURN VALUE: 0 on success, 1 otherwise
* DESCRIPTION: parse an extern directive
************************************************/
static int parse_extern_directive(void)
{
return parse_label_declaration() || install_label_declaration(EXTERNAL);
}
/************************************************
* NAME: parse_end_of_line
* RETURN VALUE: 0 on success, 1 otherwise
* DESCRIPTION: parse NULL or newline at the end
* at the end of the line
************************************************/
static int parse_end_of_line(void)
{
parse_whitespace();
if (*input_line != '\0' && *input_line != '\n') {
parse_error("garbage in end of line");
return 1;
}
return 0;
}
/************************************************
* NAME: parse_directive
* RETURN VALUE: 0 on success, 1 otherwise
* DESCRIPTION: parse a directive
************************************************/
static int parse_directive(void)
{
struct {
char name[MAX_DIRECTIVE_NAME_LENGTH];
int (*function)(void);
} directives[] = {
{"data", parse_data_directive},
{"string", parse_string_directive},
{"entry", parse_entry_directive},
{"extern", parse_extern_directive},
};
int i;
/* try to find a matching directive and if so run it's parsing function */
for (i = 0; sizeof(directives)/sizeof(directives[0]); i++) {
if (strncmp(input_line, directives[i].name, strlen(directives[i].name)) == 0) {
return parse_string(directives[i].name) || parse_whitespace_must() || directives[i].function();
}
}
/* if flow reaches here no directive was found */
parse_error("no such directive");
return 1;
}
/************************************************
* NAME: parse_instruction_comb
* RETURN VALUE: 0 on success, 1 otherwise
* PARAMS: full_instruction - the instrcution
* fill the comb and
* type value at
* DESCRIPTION: parse the comb value after the
* instruction
************************************************/
static int parse_instruction_comb(full_instruction_t *full_instruction)
{
full_instruction->type = 0;
full_instruction->comb = 0;
if (parse_string("/")) {
return 1;
}
if (*input_line == '0') {
return parse_string("0");
} else if (*input_line == '1') {
full_instruction->type = 1;
if (parse_string("1")) {
return 1;
}
if (parse_string("/")) {
return 1;
}
if (*input_line == '0') {
if (parse_string("0")) {
return 1;
}
} else if (*input_line == '1') {
if (parse_string("1")) {
return 1;
}
full_instruction->comb += 2;
} else {
parse_error("expected 0 or 1");
return 1;
}
if (parse_string("/")) {
return 1;
}
if (*input_line == '0') {
if (parse_string("0")) {
return 1;
}
} else if (*input_line == '1') {
if (parse_string("1")) {
return 1;
}
full_instruction->comb += 1;
} else {
parse_error("expected 0 or 1");
return 1;
}
} else {
parse_error("expected 0 or 1");
return 1;
}
return 0;
}
/************************************************
* NAME: is_register_operand
* RETURN VALUE: is register operand
* DESCRIPTION: check if parsing a register operand
* ************************************************/
static int is_register_operand(void)
{
return input_line[0] == 'r' && \
input_line[1] >= '0' && \
input_line[1] <= '7';
}
/************************************************
* NAME: is_immediate_operand
* RETURN VALUE: is immediate operand
* DESCRIPTION: check if parsing an immediate operand
* ************************************************/
static int is_immediate_operand(void)
{
return input_line[0] == '#';
}
/************************************************
* NAME: is_direct_register_operand
* RETURN VALUE: is a direct register operand
* DESCRIPTION: check if parsing a direct register
* operand
* ************************************************/
static int is_direct_register_operand(void)
{
return is_register_operand();
}
/************************************************
* NAME: parse_register
* RETURN VALUE: 0 on success, 1 otherwise
* PARAMS: reg - a pointer to the register operand
* DESCRIPTION: parse a register operand
* ************************************************/
static int parse_register(int *reg)
{
if (parse_string("r")) {
return 1;
}
*reg = *input_line++ - '0';
if (*reg < 0 || *reg > 7) {
parse_error("invalid register");
return 1;
}
return 0;
}
/************************************************
* NAME: parse_instruction_operand_immediate
* RETURN VALUE: 0 on success, 1 otherwise
* PARAMS: immediate - a pointer to the immediate
* operand
* DESCRIPTION: parse an immediate operand
* ************************************************/
static int parse_instruction_operand_immediate(long *immediate)
{
return parse_string("#") || parse_number(immediate);
}
/************************************************
* NAME: parse_instruction_operand_register_direct
* RETURN VALUE: 0 on success, 1 otherwise
* PARAMS: reg - a pointer to the register operand
* DESCRIPTION: parse a direct register operand
* ************************************************/
static int parse_instruction_operand_register_direct(int *reg)
{
return parse_register(reg);
}
/************************************************
* NAME: parse_instruction_operand
* RETURN VALUE: 0 on success, 1 otherwise
* PARAMS: operand - a pointer to the operand to
* to be parsed
* available_address_modes - the available
* address modes
* DESCRIPTION: parse an operand
* ************************************************/
static int parse_instruction_operand(operand_t *operand, int available_address_modes)
{
/* check for immediate operand */
if (is_immediate_operand()) {
code_index++;
operand->type = IMMEDIATE_ADDRESS;
if (parse_instruction_operand_immediate(&(operand->value.immediate))) {
return 1;
}
/* check for direct register operand */
} else if (is_direct_register_operand()) {
operand->type = DIRECT_REGISTER_ADDRESS;
if (parse_instruction_operand_register_direct(&(operand->value.reg))) {
return 1;
}
}
/* check for direct or index operand */
else {
/* parse the label */
code_index++;
if (parse_label_use(operand->value.label)) {
return 1;
}
parse_whitespace();
/* check for index operand */
if (*input_line == '{') {
operand->type = INDEX_ADDRESS;
if (parse_string("{")) {
return 1;
}
/* check for index register operand */
if (is_register_operand()) {
operand->index_type = REGISTER;
if (parse_register(&(operand->index.reg))) {
return 1;
}
/* check for index immediate operand */
} else if (isdigit(*input_line) || *input_line == '-' || *input_line == '+') {
operand->index_type = IMMEDIATE;
code_index++;
if (parse_number(&(operand->index.immediate))) {
return 1;
}
/* index direct operand */
} else if (!parse_label_use(operand->index.label)) {
operand->index_type = LABEL;
code_index++;
} else {
parse_error("invalid index addersing");
return 1;
}
if (parse_string("}")) {
return 1;
}
} else {
operand->type = DIRECT_ADDRESS;
}
}
/* check if the address mode is available */
if (!(operand->type & available_address_modes)) {
parse_error("address mode not allowed for this instruction");
return 1;
}
return 0;
}
/************************************************
* NAME: parse_instruction_name
* RETURN VALUE: 0 on success, 1 otherwise
* PARAMS: instruction - a pointer to the parsed
* instruction
* DESCRIPTION: parse the instruction name
* ************************************************/
static int parse_instruction_name(instruction_t **instruction)
{
int i;
for (i = 0; i < sizeof(instructions)/sizeof(instructions[0]); i++) {
if (strncmp(input_line, instructions[i].name, strlen(instructions[i].name)) == 0) {
*instruction = &instructions[i];
return parse_string((*instruction)->name);
}
}
/* instruction not found */
parse_error("invalid instruction");
return 1;
}
/************************************************
* NAME: parse_instruction
* RETURN VALUE: 0 on success, 1 otherwise
* DESCRIPTION: parse an instruction
*************************************************/
static int parse_instruction(void)
{
/* get an instruction to hold parseed instruction */
full_instruction_t *full_instruction = &full_instructions[full_instruction_index];
full_instruction_index++;
/* install label if on first pass */
if (label_defined && pass == FIRST_PASS && install_label_defintion(CODE)) {
return 1;
}
/* parse instruction name */
if (parse_instruction_name(&(full_instruction->instruction))) {
return 1;
}
/* parse instruction comb */
if (parse_instruction_comb(full_instruction)) {
return 1;
}
code_index++;
/* parse instruction operands */
switch (full_instruction->instruction->num_opernads) {
case 2:
return parse_whitespace_must() || \
parse_instruction_operand(&(full_instruction->src_operand),
full_instruction->instruction->src_address_modes) || \
parse_whitespace() || \
parse_string(",") || \
parse_whitespace() || \
parse_instruction_operand(&(full_instruction->dest_operand),
full_instruction->instruction->dest_address_modes);
case 1:
return parse_whitespace_must() || \
parse_instruction_operand(&(full_instruction->dest_operand),
full_instruction->instruction->dest_address_modes);
}
return 0;
}
/************************************************
* NAME: parse_action_line
* RETURN VALUE: 0 on success, 1 otherwise
* DESCRIPTION: parse an action line
* ************************************************/
static int parse_action_line(void)
{
/* try to parse the label defintion */
if (parse_label_definition()) {
return 1;
}
/* if a label was defined whitespace must
* be after it's defintion, else whitespace
* can occurr but not must */
if (label_defined) {
if (parse_whitespace_must()) {
return 1;
}
} else {
parse_whitespace();
}
/* check for directive */
if (*input_line == '.') {
/* skip directive parsing on second pass */
if (pass == SECOND_PASS) {
return 0;
} else if (parse_string(".") || parse_directive()) {
return 1;
}
} else {
/* if it's not a directive it must
* be an instruction */
if (parse_instruction()) {
return 1;
}
}
return parse_end_of_line();
}
/************************************************
* NAME: parse_line
* RETURN VALUE: 0 on success, 1 otherwise
* PARAMS: line - the line to parse
* DESCRIPTION: run the first and second pass on
* a given file
* ************************************************/
static int parse_line(char *line)
{
/* init global variables corresponding to a single
* line parse */
label_defined = 0;
input_line = input_line_start = line;
/* parse a line only if it's not a
* comment and not empty */
if (!is_comment() && !is_empty()) {
return parse_action_line();
}
return 0;
}
/* exported functions */
/************************************************
* NAME: parse_file
* RETURN VALUE: 0 on success, 1 otherwise
* PARAMS: filename - the filename to parse
* DESCRIPTION: run the first and second pass on
* a given file
* ************************************************/
int parse_file(char *filename)
{
FILE *fp;
char line[MAX_LINE_LENGTH];
int failed = 0;
init_labels();
fp = fopen(filename, "rt");
if (NULL == fp) {
perror("couldn't open assembly file");
return 1;
}
/* for error reporting */
input_filename = filename;
/* initialized global variables for first pass */
pass = FIRST_PASS;
full_instruction_index = 0;
code_index = 0;
data_index = 0;
/* first pass (expecting failure) */
for (input_linenumber = 1;
fgets(line, MAX_LINE_LENGTH, fp);
input_linenumber++) {
failed |= parse_line(line);
}
/* first pass failed so close the file
* and return (no need to do a second pass) */
if (failed)
{
fclose(fp);
return 1;
}
/* rewind to the beginning of the file
* note that rewind (from stdlib) is not used on purpose */
if (fseek(fp, 0, SEEK_SET)) {
perror("input file rewind failed");
fclose(fp);
}
/* initialized global variables for second pass
* data index is not initialized on purpose */
pass = SECOND_PASS;
full_instruction_index = 0;
code_index = 0;
/* second pass */
for (input_linenumber = 1;
fgets(line, MAX_LINE_LENGTH, fp);
input_linenumber++) {
if (parse_line(line))
{
failed = 1;
}
}
fclose(fp);
return failed;
}