* asranlib/asranlib.c, link/lkar.h, link/lkar.c:
[fw/sdcc] / as / z80 / assubr.c
1 /* assubr.c
2
3    Copyright (C) 1989-1995 Alan R. Baldwin
4    721 Berkeley St., Kent, Ohio 44240
5
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3, or (at your option) any
9 later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program.  If not, see <http://www.gnu.org/licenses/>. */
18
19 /*
20  * Extensions: P. Felber, M. Hope
21  */
22
23 #include <stdio.h>
24 #include <setjmp.h>
25 #include <string.h>
26 #include "asm.h"
27
28 /*)Module       assubr.c
29  *
30  *      The module assubr.c contains the error
31  *      processing routines.
32  *
33  *      assubr.c contains the following functions:
34  *              VOID    aerr()
35  *              VOID    diag()
36  *              VOID    err()
37  *              VOID    qerr()
38  *              VOID    rerr()
39  *
40  *      assubr.c contains the local array of *error[]
41  */
42
43 /*)Function     VOID    err(c)
44  *
45  *              int     c               error type character
46  *
47  *      The function err() logs the error code character
48  *      suppressing duplicate errors.  If the error code
49  *      is 'q' then the parse of the current assembler-source
50  *      text line is terminated.
51  *
52  *      local variables:
53  *              char *  p               pointer to the error array
54  *
55  *      global variables:
56  *              char    eb[]            array of generated error codes
57  *
58  *      functions called:
59  *              VOID    longjmp()       c_library
60  *
61  *      side effects:
62  *              The error code may be inserted into the
63  *              error code array eb[] or the parse terminated.
64  */
65
66 VOID
67 err(c)
68 register int c;
69 {
70         register char *p;
71
72 #ifndef SDK
73         aserr++;
74 #endif /* SDK */
75         p = eb;
76         while (p < ep)
77                 if (*p++ == c)
78                         return;
79         if (p < &eb[NERR]) {
80                 *p++ = c;
81                 ep = p;
82         }
83         if (c == 'q')
84                 longjmp(jump_env, -1);
85 }
86
87 /*)Function     VOID    diag()
88  *
89  *      The function diag() prints any error codes and
90  *      the source line number to the stderr output device.
91  *
92  *      local variables:
93  *              char *  p               pointer to error code array eb[]
94  *
95  *      global variables:
96  *              int     cfile           current source file index
97  *              char    eb[]            array of generated error codes
98  *              char *  ep              pointer into error list
99  *              int     incfile         current include file index
100  *              char    incfn[]         array of include file names
101  *              int     incline[]       array of include line numbers
102  *              char    srcfn[]         array of source file names
103  *              int     srcline[]       array of source line numbers
104  *              FILE *  stderr          c_library
105  *
106  *      functions called:
107  *              int     fprintf()       c_library
108  *              char *  geterr()        assubr.c
109  *
110  *      side effects:
111  *              none
112  */
113
114 VOID
115 diag()
116 {
117         register char *p,*errstr;
118
119         if (eb != ep) {
120                 p = eb;
121 #ifndef SDK
122                 fprintf(stderr, "?ASxxxx-Error-<");
123                 while (p < ep) {
124                         fprintf(stderr, "%c", *p++);
125                 }
126                 fprintf(stderr, "> in line ");
127                 if (incfil >= 0) {
128                         fprintf(stderr, "%d", incline[incfil]);
129                         fprintf(stderr, " of %s\n", incfn[incfil]);
130                 } else {
131                         fprintf(stderr, "%d", srcline[cfile]);
132                         fprintf(stderr, " of %s\n", srcfn[cfile]);
133                 }
134                 p = eb;
135 #endif /* SDK */
136                 while (p < ep) {
137                         if ((errstr = geterr(*p++)) != NULL) {
138 #ifdef SDK
139                                 /* Modified to conform to gcc error standard, M. Hope, 7 Feb 98. */
140                                 if (incfil >= 0) {
141                                         fprintf(stderr, "%s:", incfn[incfil]);
142                                         fprintf(stderr, "%d: Error:", incline[incfil]);
143                                 }
144                                 else {
145                                         fprintf(stderr, "%s:", srcfn[cfile]);
146                                         fprintf(stderr, "%d: Error:", srcline[cfile]);
147                                 }
148                                 fprintf(stderr, " %s\n", errstr);
149 #else
150                                 fprintf(stderr, "              %s\n", errstr);
151 #endif /* SDK */
152                         }
153                 }
154 #ifdef SDK
155                 aserr++;
156 #endif /* SDK */
157         }
158 }
159
160 /*)Functions:   VOID    aerr()
161  *              VOID    qerr()
162  *              VOID    rerr()
163  *
164  *      The functions aerr(), qerr(), and rerr() report their
165  *      respective error type.  These are included only for
166  *      convenience.
167  *
168  *      local variables:
169  *              none
170  *
171  *      global variables:
172  *              none
173  *
174  *      functions called:
175  *              VOID    err()           assubr.c
176  *
177  *      side effects:
178  *              The appropriate error code is inserted into the
179  *              error array and the parse may be terminated.
180  */
181
182 /*
183  * Note an 'r' error.
184  */
185 VOID
186 rerr()
187 {
188         err('r');
189 }
190
191 /*
192  * Note an 'a' error.
193  */
194 VOID
195 aerr()
196 {
197         err('a');
198 }
199
200 /*
201  * Note a 'q' error.
202  */
203 VOID
204 qerr()
205 {
206         err('q');
207 }
208
209 /*
210  * ASxxxx assembler errors
211  */
212 char *errors[] = {
213         "<.> use \". = . + <arg>\" not \". = <arg>\"",
214         "<a> machine specific addressing or addressing mode error",
215         "<b> direct page boundary error",
216         "<d> direct page addressing error",
217         "<i> .include file error or an .if/.endif mismatch",
218         "<m> multiple definitions error",
219         "<o> .org in REL area or directive / mnemonic error",
220         "<p> phase error: label location changing between passes 2 and 3",
221         "<q> missing or improper operators, terminators, or delimiters",
222         "<r> relocation error",
223         "<u> undefined symbol encountered during assembly",
224         NULL
225 };
226         
227 /*)Function:    char    *getarr(c)
228  *
229  *              int     c               the error code character
230  *
231  *      The function geterr() scans the list of errors returning the
232  *      error string corresponding to the input error character.
233  *
234  *      local variables:
235  *              int     i               error index counter
236  *
237  *      global variables:
238  *              char    *errors[]       array of pointers to the
239  *                                      error strings
240  *
241  *      functions called:
242  *              none
243  *
244  *      side effects:
245  *              A pointer to the appropriate
246  *              error code string is returned.
247  */
248 char *
249 geterr(c)
250 int c;
251 {
252         int     i;
253
254         for (i=0; errors[i]!=NULL; i++) {
255                 if (c == errors[i][1]) {
256                         return(errors[i]);
257                 }
258         }
259         return(NULL);
260 }
261