annotate lwasm/pass1.c @ 232:1009f302ac11

Documented the "Flags" term for object files
author lost
date Fri, 12 Jun 2009 04:17:22 +0000
parents bae1e3ecdce1
children a58f49a77441
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
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
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
30 3. addresses are assigned to every symbol defined in the assembly
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
31 4. macros are defined and expanded at this pass
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
32
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
33 * note: the lines are re-evaluated on the second pass
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
34
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
35 All source lines are read into memory with a record of the file name and
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
36 line number within the files.
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
37
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
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
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
40 <symbol> <opcode> <operand> <comment>
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
41 <symbol> <opcode> <comment>
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
42 <opcode> <operand> <comment>
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
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
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
45 A "*" or ";" appearing anywhere on the line that is not otherwise interpreted
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
46 as part of an operation code or operand introduces a comment.
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
47
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
48 Certain lwasm specific operations are prefixed with a "*" to aid in source
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
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
212
bae1e3ecdce1 More preparation for gnulib integration
lost
parents: 151
diff changeset
52 #include <config.h>
13
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff changeset
53
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff changeset
54 #include <errno.h>
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff changeset
55 #include <stdio.h>
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff changeset
56 #include <stdlib.h>
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff changeset
57
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff changeset
58 #include "lwasm.h"
21
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
59 #include "util.h"
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
60
35
39d750ee8d34 Added error display and fixed infinite loop in lwasm_parse_line()
lost
parents: 28
diff changeset
61
39d750ee8d34 Added error display and fixed infinite loop in lwasm_parse_line()
lost
parents: 28
diff changeset
62 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
63
21
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
64 // we can't use standard line inputting functions here because we have to
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
65 // handle non-standard line terminations (CR, LF, CRLF, or LFCR)
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
66 int lwasm_read_file(asmstate_t *as, const char *filename)
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
67 {
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
68 FILE *f;
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
69 int c, c2;
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
70 lwasm_line_t *nl;
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
71 int lineno = 1;
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
72 char *fnref;
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
73
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
74 // ought to be long enough...we truncate longer lines
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
75 char linebuff[2049];
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
76 int lbloc = 0;
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
77 int eol = 0;
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
78
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
79 // add filename to list
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
80 as -> filelist = lwasm_realloc(as -> filelist, sizeof(char *) * (as -> filelistlen + 1));
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
81 fnref = as -> filelist[as -> filelistlen] = lwasm_strdup(filename);
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
82 as -> filelistlen += 1;
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
83
89
11d38c9e5095 Made reading input files use binary mode to avoid newline translation since we handle it ourselves manually
lost
parents: 85
diff changeset
84 f = fopen(filename, "rb");
21
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
85 if (!f)
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
86 return -1;
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
87
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
88 for (;;)
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
89 {
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
90 c = fgetc(f);
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
91 if (c == EOF)
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
92 {
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
93 linebuff[lbloc] = '\0';
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
94 eol = 1;
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
95 }
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
96 else if (c == '\r')
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
97 {
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
98 linebuff[lbloc] = '\0';
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
99 eol = 1;
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
100 // check for '\n':
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
101 c2 = fgetc(f);
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
102 if (c2 == EOF)
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
103 c = EOF;
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
104 else if (c2 != '\n')
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
105 ungetc(c2, f);
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
106 }
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
107 else if (c == '\n')
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
108 {
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
109 linebuff[lbloc] = '\0';
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
110 eol = 1;
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
111 // check for '\r':
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
112 c2 = fgetc(f);
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
113 if (c2 == EOF)
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
114 c = EOF;
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
115 else if (c2 != '\r')
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
116 ungetc(c2, f);
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
117 }
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
118 else
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
119 {
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
120 // silently ignore characters past 2K on a line... FIXME
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
121 if (lbloc < 2048)
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
122 linebuff[lbloc++] = c;
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
123 }
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
124 if (eol)
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
125 {
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
126 eol = 0;
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
127 lbloc = 0;
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
128 nl = lwasm_alloc(sizeof(lwasm_line_t));
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
129 nl -> text = lwasm_strdup(linebuff);
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
130 nl -> lineno = lineno++;
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
131 nl -> filename = fnref;
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
132 nl -> next = NULL;
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
133 nl -> prev = as -> linestail;
26
d2e86babd958 Added error tracking infrastructure
lost
parents: 22
diff changeset
134 nl -> err = NULL;
28
c0ff62e5ad39 Added register list mode handler
lost
parents: 27
diff changeset
135 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
136 nl -> sym = NULL;
42
4bb7b723e5b7 Added pass2 code generation to lwasm_emit()
lost
parents: 37
diff changeset
137 nl -> bytes = NULL;
4bb7b723e5b7 Added pass2 code generation to lwasm_emit()
lost
parents: 37
diff changeset
138 nl -> codelen = 0;
4bb7b723e5b7 Added pass2 code generation to lwasm_emit()
lost
parents: 37
diff changeset
139 nl -> codesize = 0;
46
b962cee20bf4 Ported output modules forward from old version
lost
parents: 44
diff changeset
140 nl -> nocodelen = 0;
49
21ae0fab469b Added needed infra for useful listing of EQU and ORG type statements
lost
parents: 46
diff changeset
141 nl -> addrset = 0;
21ae0fab469b Added needed infra for useful listing of EQU and ORG type statements
lost
parents: 46
diff changeset
142 nl -> symaddr = -1;
67
d5fe306f1ab1 Fixed numerous bugs in macro handling
lost
parents: 58
diff changeset
143 nl -> badop = 0;
85
918be0c02239 Started adding object target output
lost
parents: 67
diff changeset
144 nl -> relocoff = -1;
21
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
145 if (as -> linestail)
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
146 as -> linestail -> next = nl;
44
2330b88f9600 Added simple output listing
lost
parents: 42
diff changeset
147 as -> linestail = nl;
21
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
148 if (!(as -> lineshead))
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
149 as -> lineshead = nl;
35
39d750ee8d34 Added error display and fixed infinite loop in lwasm_parse_line()
lost
parents: 28
diff changeset
150 lwasm_parse_line(as, nl);
54
360d53062bb9 Fixed typo in instruction table and added END directive
lost
parents: 49
diff changeset
151 if (as -> endseen)
360d53062bb9 Fixed typo in instruction table and added END directive
lost
parents: 49
diff changeset
152 break;
21
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
153 }
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
154 if (c == EOF)
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
155 break;
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
156 }
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
157
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
158 fclose(f);
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
159 return 0;
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
160 }
13
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff changeset
161
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff changeset
162 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
163 {
21
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
164 as -> passnum = 1;
27
f736579569b4 Added handlers for inherent and register to register instructions
lost
parents: 26
diff changeset
165 as -> addr = 0;
58
b1d81800bc91 Added symbol listing to list file; various fixes
lost
parents: 54
diff changeset
166 as -> nextcontext = 1;
21
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
167
44
2330b88f9600 Added simple output listing
lost
parents: 42
diff changeset
168 debug_message(1, "Entering pass 1");
21
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
169 if (lwasm_read_file(as, as -> infile) < 0)
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
170 {
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
171 fprintf(stderr, "Error reading input file '%s'", as -> infile);
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
172 perror("");
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
173 exit(1);
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
174 }
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
175
13
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff changeset
176 }