Mercurial > hg-old > index.cgi
comparison src/pass1.c @ 21:3c0e5f311c95
Added reading of input file for pass1
author | lost |
---|---|
date | Fri, 02 Jan 2009 00:43:06 +0000 |
parents | 05d4115b4860 |
children | b704f7ffc8ba |
comparison
equal
deleted
inserted
replaced
20:610710a7859f | 21:3c0e5f311c95 |
---|---|
22 | 22 |
23 First pass involves the following: | 23 First pass involves the following: |
24 | 24 |
25 1. read all lines from the main source file, following all "include" | 25 1. read all lines from the main source file, following all "include" |
26 directives as appropriate | 26 directives as appropriate |
27 2. parse each line into a symbol, operation code, and operand as appropriate | 27 2. each operand is evaluated for syntax and futher for value if there are |
28 3. each operand is evaluated for syntax and futher for value if there are | |
29 multiple addressing sizes available; any undefined or not fully resolved | 28 multiple addressing sizes available; any undefined or not fully resolved |
30 value will default to the largest addressing size available (16 bit) | 29 value will default to the largest addressing size available (16 bit) |
31 4. addresses are assigned to every symbol defined in the assembly | 30 3. addresses are assigned to every symbol defined in the assembly |
32 5. macros are defined and expanded at this pass | 31 4. macros are defined and expanded at this pass |
33 | 32 |
33 * note: the lines are re-evaluated on the second pass | |
34 | 34 |
35 All source lines are read into memory with a record of the file name and | |
36 line number within the files. | |
37 | |
38 Lines are one of the following formats: | |
39 | |
40 <symbol> <opcode> <operand> <comment> | |
41 <symbol> <opcode> <comment> | |
42 <opcode> <operand> <comment> | |
43 <opcode> <comment> | |
44 | |
45 A "*" or ";" appearing anywhere on the line that is not otherwise interpreted | |
46 as part of an operation code or operand introduces a comment. | |
47 | |
48 Certain lwasm specific operations are prefixed with a "*" to aid in source | |
49 code portability (like *pragma). | |
35 */ | 50 */ |
36 | 51 |
37 #ifdef HAVE_CONFIG_H | 52 #ifdef HAVE_CONFIG_H |
38 #include "config.h" | 53 #include "config.h" |
39 #endif | 54 #endif |
40 | 55 |
41 #include <argp.h> | |
42 #include <errno.h> | 56 #include <errno.h> |
43 #include <stdio.h> | 57 #include <stdio.h> |
44 #include <stdlib.h> | 58 #include <stdlib.h> |
45 | 59 |
46 #include "lwasm.h" | 60 #include "lwasm.h" |
61 #include "util.h" | |
62 | |
63 // we can't use standard line inputting functions here because we have to | |
64 // handle non-standard line terminations (CR, LF, CRLF, or LFCR) | |
65 int lwasm_read_file(asmstate_t *as, const char *filename) | |
66 { | |
67 FILE *f; | |
68 int c, c2; | |
69 lwasm_line_t *nl; | |
70 int lineno = 1; | |
71 char *fnref; | |
72 | |
73 // ought to be long enough...we truncate longer lines | |
74 char linebuff[2049]; | |
75 int lbloc = 0; | |
76 int eol = 0; | |
77 | |
78 // add filename to list | |
79 as -> filelist = lwasm_realloc(as -> filelist, sizeof(char *) * (as -> filelistlen + 1)); | |
80 fnref = as -> filelist[as -> filelistlen] = lwasm_strdup(filename); | |
81 as -> filelistlen += 1; | |
82 | |
83 f = fopen(filename, "r"); | |
84 if (!f) | |
85 return -1; | |
86 | |
87 for (;;) | |
88 { | |
89 c = fgetc(f); | |
90 if (c == EOF) | |
91 { | |
92 linebuff[lbloc] = '\0'; | |
93 eol = 1; | |
94 } | |
95 else if (c == '\r') | |
96 { | |
97 linebuff[lbloc] = '\0'; | |
98 eol = 1; | |
99 // check for '\n': | |
100 c2 = fgetc(f); | |
101 if (c2 == EOF) | |
102 c = EOF; | |
103 else if (c2 != '\n') | |
104 ungetc(c2, f); | |
105 } | |
106 else if (c == '\n') | |
107 { | |
108 linebuff[lbloc] = '\0'; | |
109 eol = 1; | |
110 // check for '\r': | |
111 c2 = fgetc(f); | |
112 if (c2 == EOF) | |
113 c = EOF; | |
114 else if (c2 != '\r') | |
115 ungetc(c2, f); | |
116 } | |
117 else | |
118 { | |
119 // silently ignore characters past 2K on a line... FIXME | |
120 if (lbloc < 2048) | |
121 linebuff[lbloc++] = c; | |
122 } | |
123 if (eol) | |
124 { | |
125 fprintf(stderr, "READ: %s\n", linebuff); | |
126 eol = 0; | |
127 lbloc = 0; | |
128 nl = lwasm_alloc(sizeof(lwasm_line_t)); | |
129 nl -> text = lwasm_strdup(linebuff); | |
130 nl -> lineno = lineno++; | |
131 nl -> filename = fnref; | |
132 nl -> next = NULL; | |
133 nl -> prev = as -> linestail; | |
134 if (as -> linestail) | |
135 as -> linestail -> next = nl; | |
136 else | |
137 as -> linestail = nl; | |
138 if (!(as -> lineshead)) | |
139 as -> lineshead = nl; | |
140 } | |
141 if (c == EOF) | |
142 break; | |
143 } | |
144 | |
145 fclose(f); | |
146 return 0; | |
147 } | |
47 | 148 |
48 void lwasm_pass1(asmstate_t *as) | 149 void lwasm_pass1(asmstate_t *as) |
49 { | 150 { |
151 as -> passnum = 1; | |
152 | |
153 if (lwasm_read_file(as, as -> infile) < 0) | |
154 { | |
155 fprintf(stderr, "Error reading input file '%s'", as -> infile); | |
156 perror(""); | |
157 exit(1); | |
158 } | |
159 | |
50 } | 160 } |