annotate old-trunk/lwasm/old/pass1.c @ 354:60568b123281

Added os9 opcodes and ERROR
author lost@starbug
date Tue, 30 Mar 2010 23:10:01 -0600
parents eb230fa7d28e
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
1 /*
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
2 pass1.c
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
3 Copyright © 2008 William Astle
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
4
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
5 This file is part of LWASM.
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
6
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
7 LWASM is free software: you can redistribute it and/or modify it under the
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
8 terms of the GNU General Public License as published by the Free Software
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
9 Foundation, either version 3 of the License, or (at your option) any later
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
10 version.
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
11
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
12 This program is distributed in the hope that it will be useful, but WITHOUT
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
15 more details.
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
16
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
17 You should have received a copy of the GNU General Public License along with
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
18 this program. If not, see <http://www.gnu.org/licenses/>.
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
19
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
20
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
21 Handles first pass of assembly
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
22
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
23 First pass involves the following:
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
24
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
25 1. read all lines from the main source file, following all "include"
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
26 directives as appropriate
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
27 2. each operand is evaluated for syntax and futher for value if there are
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
28 multiple addressing sizes available; any undefined or not fully resolved
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
29 value will default to the largest addressing size available (16 bit)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
30 3. addresses are assigned to every symbol defined in the assembly
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
31 4. macros are defined and expanded at this pass
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
32
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
33 * note: the lines are re-evaluated on the second pass
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
34
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
35 All source lines are read into memory with a record of the file name and
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
36 line number within the files.
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
37
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
38 Lines are one of the following formats:
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
39
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
40 <symbol> <opcode> <operand> <comment>
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
41 <symbol> <opcode> <comment>
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
42 <opcode> <operand> <comment>
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
43 <opcode> <comment>
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
44
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
45 A "*" or ";" appearing anywhere on the line that is not otherwise interpreted
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
46 as part of an operation code or operand introduces a comment.
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
47
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
48 Certain lwasm specific operations are prefixed with a "*" to aid in source
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
49 code portability (like *pragma).
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
50 */
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
51
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
52 #include <config.h>
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
53
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
54 #include <errno.h>
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
55 #include <stdio.h>
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
56 #include <stdlib.h>
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
57
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
58 #include "lwasm.h"
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
59 #include "util.h"
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
60
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
61
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
62 extern int lwasm_parse_line(asmstate_t *as, lwasm_line_t *l);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
63
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
64 // we can't use standard line inputting functions here because we have to
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
65 // handle non-standard line terminations (CR, LF, CRLF, or LFCR)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
66 int lwasm_read_file(asmstate_t *as, const char *filename)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
67 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
68 FILE *f;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
69 int c, c2;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
70 lwasm_line_t *nl;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
71 int lineno = 1;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
72 char *fnref;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
73
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
74 // ought to be long enough...we truncate longer lines
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
75 char linebuff[2049];
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
76 int lbloc = 0;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
77 int eol = 0;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
78
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
79 // add filename to list
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
80 as -> filelist = lwasm_realloc(as -> filelist, sizeof(char *) * (as -> filelistlen + 1));
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
81 fnref = as -> filelist[as -> filelistlen] = lwasm_strdup(filename);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
82 as -> filelistlen += 1;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
83
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
84 f = fopen(filename, "rb");
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
85 if (!f)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
86 return -1;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
87
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
88 for (;;)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
89 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
90 c = fgetc(f);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
91 if (c == EOF)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
92 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
93 linebuff[lbloc] = '\0';
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
94 eol = 1;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
95 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
96 else if (c == '\r')
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
97 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
98 linebuff[lbloc] = '\0';
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
99 eol = 1;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
100 // check for '\n':
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
101 c2 = fgetc(f);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
102 if (c2 == EOF)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
103 c = EOF;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
104 else if (c2 != '\n')
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
105 ungetc(c2, f);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
106 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
107 else if (c == '\n')
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
108 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
109 linebuff[lbloc] = '\0';
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
110 eol = 1;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
111 // check for '\r':
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
112 c2 = fgetc(f);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
113 if (c2 == EOF)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
114 c = EOF;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
115 else if (c2 != '\r')
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
116 ungetc(c2, f);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
117 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
118 else
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
119 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
120 // silently ignore characters past 2K on a line... FIXME
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
121 if (lbloc < 2048)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
122 linebuff[lbloc++] = c;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
123 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
124 if (eol)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
125 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
126 eol = 0;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
127 lbloc = 0;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
128 nl = lwasm_alloc(sizeof(lwasm_line_t));
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
129 nl -> text = lwasm_strdup(linebuff);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
130 nl -> lineno = lineno++;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
131 nl -> filename = fnref;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
132 nl -> next = NULL;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
133 nl -> prev = as -> linestail;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
134 nl -> err = NULL;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
135 nl -> fsize = 0;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
136 nl -> sym = NULL;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
137 nl -> bytes = NULL;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
138 nl -> codelen = 0;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
139 nl -> codesize = 0;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
140 nl -> nocodelen = 0;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
141 nl -> addrset = 0;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
142 nl -> symaddr = -1;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
143 nl -> badop = 0;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
144 nl -> relocoff = -1;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
145 if (as -> linestail)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
146 as -> linestail -> next = nl;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
147 as -> linestail = nl;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
148 if (!(as -> lineshead))
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
149 as -> lineshead = nl;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
150 lwasm_parse_line(as, nl);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
151 if (as -> endseen)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
152 break;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
153 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
154 if (c == EOF)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
155 break;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
156 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
157
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
158 fclose(f);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
159 return 0;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
160 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
161
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
162 void lwasm_pass1(asmstate_t *as)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
163 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
164 as -> passnum = 1;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
165 as -> addr = 0;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
166 as -> nextcontext = 1;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
167
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
168 as -> inmod = 0;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
169
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
170 debug_message(1, "Entering pass 1");
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
171 if (lwasm_read_file(as, as -> infile) < 0)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
172 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
173 fprintf(stderr, "Error reading input file '%s'", as -> infile);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
174 perror("");
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
175 exit(1);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
176 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
177
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
178 }