Mercurial > hg-old > index.cgi
annotate src/pass1.c @ 27:f736579569b4
Added handlers for inherent and register to register instructions
author | lost |
---|---|
date | Fri, 02 Jan 2009 02:56:04 +0000 |
parents | d2e86babd958 |
children | c0ff62e5ad39 |
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 | |
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 eol = 0; | |
126 lbloc = 0; | |
127 nl = lwasm_alloc(sizeof(lwasm_line_t)); | |
128 nl -> text = lwasm_strdup(linebuff); | |
129 nl -> lineno = lineno++; | |
130 nl -> filename = fnref; | |
131 nl -> next = NULL; | |
132 nl -> prev = as -> linestail; | |
26 | 133 nl -> err = NULL; |
21 | 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 } | |
13
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff
changeset
|
148 |
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff
changeset
|
149 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
|
150 { |
21 | 151 as -> passnum = 1; |
27
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
152 as -> addr = 0; |
21 | 153 |
154 if (lwasm_read_file(as, as -> infile) < 0) | |
155 { | |
156 fprintf(stderr, "Error reading input file '%s'", as -> infile); | |
157 perror(""); | |
158 exit(1); | |
159 } | |
160 | |
13
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff
changeset
|
161 } |