Mercurial > hg-old > index.cgi
annotate lwasm/pass1.c @ 161:56d60169fc4d
Bumped version to 2.2 for next release
author | lost |
---|---|
date | Sat, 31 Jan 2009 06:33:42 +0000 |
parents | 427e268e876b |
children | bae1e3ecdce1 |
rev | line source |
---|---|
13
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff
changeset
|
1 /* |
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff
changeset
|
2 pass1.c |
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff
changeset
|
3 Copyright © 2008 William Astle |
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff
changeset
|
4 |
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff
changeset
|
5 This file is part of LWASM. |
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff
changeset
|
6 |
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff
changeset
|
7 LWASM is free software: you can redistribute it and/or modify it under the |
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff
changeset
|
8 terms of the GNU General Public License as published by the Free Software |
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff
changeset
|
9 Foundation, either version 3 of the License, or (at your option) any later |
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff
changeset
|
10 version. |
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff
changeset
|
11 |
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff
changeset
|
12 This program is distributed in the hope that it will be useful, but WITHOUT |
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff
changeset
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff
changeset
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff
changeset
|
15 more details. |
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff
changeset
|
16 |
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff
changeset
|
17 You should have received a copy of the GNU General Public License along with |
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff
changeset
|
18 this program. If not, see <http://www.gnu.org/licenses/>. |
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff
changeset
|
19 |
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff
changeset
|
20 |
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff
changeset
|
21 Handles first pass of assembly |
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff
changeset
|
22 |
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff
changeset
|
23 First pass involves the following: |
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff
changeset
|
24 |
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff
changeset
|
25 1. read all lines from the main source file, following all "include" |
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff
changeset
|
26 directives as appropriate |
21 | 27 2. each operand is evaluated for syntax and futher for value if there are |
13
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff
changeset
|
28 multiple addressing sizes available; any undefined or not fully resolved |
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff
changeset
|
29 value will default to the largest addressing size available (16 bit) |
21 | 30 3. addresses are assigned to every symbol defined in the assembly |
31 4. macros are defined and expanded at this pass | |
32 | |
33 * note: the lines are re-evaluated on the second pass | |
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: | |
13
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff
changeset
|
39 |
21 | 40 <symbol> <opcode> <operand> <comment> |
41 <symbol> <opcode> <comment> | |
42 <opcode> <operand> <comment> | |
43 <opcode> <comment> | |
13
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff
changeset
|
44 |
21 | 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). | |
13
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff
changeset
|
50 */ |
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff
changeset
|
51 |
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff
changeset
|
52 #ifdef HAVE_CONFIG_H |
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff
changeset
|
53 #include "config.h" |
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff
changeset
|
54 #endif |
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff
changeset
|
55 |
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff
changeset
|
56 #include <errno.h> |
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff
changeset
|
57 #include <stdio.h> |
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff
changeset
|
58 #include <stdlib.h> |
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff
changeset
|
59 |
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff
changeset
|
60 #include "lwasm.h" |
21 | 61 #include "util.h" |
62 | |
35
39d750ee8d34
Added error display and fixed infinite loop in lwasm_parse_line()
lost
parents:
28
diff
changeset
|
63 |
39d750ee8d34
Added error display and fixed infinite loop in lwasm_parse_line()
lost
parents:
28
diff
changeset
|
64 extern int lwasm_parse_line(asmstate_t *as, lwasm_line_t *l); |
39d750ee8d34
Added error display and fixed infinite loop in lwasm_parse_line()
lost
parents:
28
diff
changeset
|
65 |
21 | 66 // we can't use standard line inputting functions here because we have to |
67 // handle non-standard line terminations (CR, LF, CRLF, or LFCR) | |
68 int lwasm_read_file(asmstate_t *as, const char *filename) | |
69 { | |
70 FILE *f; | |
71 int c, c2; | |
72 lwasm_line_t *nl; | |
73 int lineno = 1; | |
74 char *fnref; | |
75 | |
76 // ought to be long enough...we truncate longer lines | |
77 char linebuff[2049]; | |
78 int lbloc = 0; | |
79 int eol = 0; | |
80 | |
81 // add filename to list | |
82 as -> filelist = lwasm_realloc(as -> filelist, sizeof(char *) * (as -> filelistlen + 1)); | |
83 fnref = as -> filelist[as -> filelistlen] = lwasm_strdup(filename); | |
84 as -> filelistlen += 1; | |
85 | |
89
11d38c9e5095
Made reading input files use binary mode to avoid newline translation since we handle it ourselves manually
lost
parents:
85
diff
changeset
|
86 f = fopen(filename, "rb"); |
21 | 87 if (!f) |
88 return -1; | |
89 | |
90 for (;;) | |
91 { | |
92 c = fgetc(f); | |
93 if (c == EOF) | |
94 { | |
95 linebuff[lbloc] = '\0'; | |
96 eol = 1; | |
97 } | |
98 else if (c == '\r') | |
99 { | |
100 linebuff[lbloc] = '\0'; | |
101 eol = 1; | |
102 // check for '\n': | |
103 c2 = fgetc(f); | |
104 if (c2 == EOF) | |
105 c = EOF; | |
106 else if (c2 != '\n') | |
107 ungetc(c2, f); | |
108 } | |
109 else if (c == '\n') | |
110 { | |
111 linebuff[lbloc] = '\0'; | |
112 eol = 1; | |
113 // check for '\r': | |
114 c2 = fgetc(f); | |
115 if (c2 == EOF) | |
116 c = EOF; | |
117 else if (c2 != '\r') | |
118 ungetc(c2, f); | |
119 } | |
120 else | |
121 { | |
122 // silently ignore characters past 2K on a line... FIXME | |
123 if (lbloc < 2048) | |
124 linebuff[lbloc++] = c; | |
125 } | |
126 if (eol) | |
127 { | |
128 eol = 0; | |
129 lbloc = 0; | |
130 nl = lwasm_alloc(sizeof(lwasm_line_t)); | |
131 nl -> text = lwasm_strdup(linebuff); | |
132 nl -> lineno = lineno++; | |
133 nl -> filename = fnref; | |
134 nl -> next = NULL; | |
135 nl -> prev = as -> linestail; | |
26 | 136 nl -> err = NULL; |
28 | 137 nl -> fsize = 0; |
37
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
35
diff
changeset
|
138 nl -> sym = NULL; |
42 | 139 nl -> bytes = NULL; |
140 nl -> codelen = 0; | |
141 nl -> codesize = 0; | |
46 | 142 nl -> nocodelen = 0; |
49
21ae0fab469b
Added needed infra for useful listing of EQU and ORG type statements
lost
parents:
46
diff
changeset
|
143 nl -> addrset = 0; |
21ae0fab469b
Added needed infra for useful listing of EQU and ORG type statements
lost
parents:
46
diff
changeset
|
144 nl -> symaddr = -1; |
67 | 145 nl -> badop = 0; |
85 | 146 nl -> relocoff = -1; |
21 | 147 if (as -> linestail) |
148 as -> linestail -> next = nl; | |
44 | 149 as -> linestail = nl; |
21 | 150 if (!(as -> lineshead)) |
151 as -> lineshead = nl; | |
35
39d750ee8d34
Added error display and fixed infinite loop in lwasm_parse_line()
lost
parents:
28
diff
changeset
|
152 lwasm_parse_line(as, nl); |
54
360d53062bb9
Fixed typo in instruction table and added END directive
lost
parents:
49
diff
changeset
|
153 if (as -> endseen) |
360d53062bb9
Fixed typo in instruction table and added END directive
lost
parents:
49
diff
changeset
|
154 break; |
21 | 155 } |
156 if (c == EOF) | |
157 break; | |
158 } | |
159 | |
160 fclose(f); | |
161 return 0; | |
162 } | |
13
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff
changeset
|
163 |
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff
changeset
|
164 void lwasm_pass1(asmstate_t *as) |
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff
changeset
|
165 { |
21 | 166 as -> passnum = 1; |
27
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
167 as -> addr = 0; |
58 | 168 as -> nextcontext = 1; |
21 | 169 |
44 | 170 debug_message(1, "Entering pass 1"); |
21 | 171 if (lwasm_read_file(as, as -> infile) < 0) |
172 { | |
173 fprintf(stderr, "Error reading input file '%s'", as -> infile); | |
174 perror(""); | |
175 exit(1); | |
176 } | |
177 | |
13
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff
changeset
|
178 } |