* asranlib/asranlib.c, link/lkar.h, link/lkar.c:
[fw/sdcc] / as / mcs51 / 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 #include <stdio.h>
20 #include <setjmp.h>
21 #include <string.h>
22 #include "asm.h"
23
24 /*)Module       assubr.c
25  *
26  *      The module assubr.c contains the error
27  *      processing routines.
28  *
29  *      assubr.c contains the following functions:
30  *              VOID    aerr()
31  *              VOID    diag()
32  *              VOID    err()
33  *              VOID    qerr()
34  *              VOID    rerr()
35  *
36  *      assubr.c contains the local array of *error[]
37  */
38
39 /*)Function     VOID    err(c)
40  *
41  *              int     c               error type character
42  *
43  *      The function err() logs the error code character
44  *      suppressing duplicate errors.  If the error code
45  *      is 'q' then the parse of the current assembler-source
46  *      text line is terminated.
47  *
48  *      local variables:
49  *              char *  p               pointer to the error array
50  *
51  *      global variables:
52  *              char    eb[]            array of generated error codes
53  *
54  *      functions called:
55  *              VOID    longjmp()       c_library
56  *
57  *      side effects:
58  *              The error code may be inserted into the
59  *              error code array eb[] or the parse terminated.
60  */
61
62 VOID
63 err(c)
64 register int c;
65 {
66         register char *p;
67
68         aserr++;
69         p = eb;
70         while (p < ep)
71                 if (*p++ == c)
72                         return;
73         if (p < &eb[NERR]) {
74                 *p++ = c;
75                 ep = p;
76         }
77         if (c == 'q')
78                 longjmp(jump_env, -1);
79 }
80
81 /*)Function     VOID    diag()
82  *
83  *      The function diag() prints any error codes and
84  *      the source line number to the stderr output device.
85  *
86  *      local variables:
87  *              char *  p               pointer to error code array eb[]
88  *
89  *      global variables:
90  *              int     cfile           current source file index
91  *              char    eb[]            array of generated error codes
92  *              char *  ep              pointer into error list
93  *              int     incfile         current include file index
94  *              char    incfn[]         array of include file names
95  *              int     incline[]       array of include line numbers
96  *              char    srcfn[]         array of source file names
97  *              int     srcline[]       array of source line numbers
98  *              FILE *  stderr          c_library
99  *
100  *      functions called:
101  *              int     fprintf()       c_library
102  *              char *  geterr()        assubr.c
103  *
104  *      side effects:
105  *              none
106  */
107
108 extern int fatalErrors;
109
110 VOID
111 diag()
112 {
113         register char *p,*errstr;
114
115         if (eb != ep) {
116                 fatalErrors++;
117                 p = eb;
118                 fprintf(stderr, "?ASxxxx-Error-<");
119                 while (p < ep) {
120                         fprintf(stderr, "%c", *p++);
121                 }
122                 fprintf(stderr, "> in line ");
123                 if (incfil >= 0) {
124                         fprintf(stderr, "%d", incline[incfil]);
125                         fprintf(stderr, " of %s\n", incfn[incfil]);
126                 } else {
127                         fprintf(stderr, "%d", srcline[cfile]);
128                         fprintf(stderr, " of %s\n", srcfn[cfile]);
129                 }
130                 p = eb;
131                 while (p < ep) {
132                         if ((errstr = geterr(*p++)) != NULL) {
133                                 fprintf(stderr, "              %s\n", errstr);
134                         }
135                 }
136         }
137 }
138
139 /*)Function     VOID    warnBanner()
140  *
141  *      The function warnBanner() prints a generic warning message
142  *      header (including the current source file/line) and positions
143  *      the output for a more specific warning message.
144  *
145  *      It is assumed that the call to warnBanner will be followed with
146  *      a fprintf to stderr (or equivalent) with the specific warning
147  *      text.
148  *
149  *      local variables:
150  *              none
151  *
152  *      global variables:
153  *              int     cfile           current source file index
154  *              int     incfile         current include file index
155  *              char    incfn[]         array of include file names
156  *              int     incline[]       array of include line numbers
157  *              char    srcfn[]         array of source file names
158  *              int     srcline[]       array of source line numbers
159  *              FILE *  stderr          c_library
160  *
161  *      functions called:
162  *              int     fprintf()       c_library
163  *
164  *      side effects:
165  *              none
166  */
167 VOID
168 warnBanner(void)
169 {
170         fprintf(stderr, "?ASxxxx-Warning in line ");
171         if (incfil >= 0) {
172                 fprintf(stderr, "%d", incline[incfil]);
173                 fprintf(stderr, " of %s\n", incfn[incfil]);
174         } else {
175                 fprintf(stderr, "%d", srcline[cfile]);
176                 fprintf(stderr, " of %s\n", srcfn[cfile]);
177         }
178         fprintf(stderr, "               ");
179 }       
180
181 /*)Functions:   VOID    aerr()
182  *              VOID    qerr()
183  *              VOID    rerr()
184  *
185  *      The functions aerr(), qerr(), and rerr() report their
186  *      respective error type.  These are included only for
187  *      convenience.
188  *
189  *      local variables:
190  *              none
191  *
192  *      global variables:
193  *              none
194  *
195  *      functions called:
196  *              VOID    err()           assubr.c
197  *
198  *      side effects:
199  *              The appropriate error code is inserted into the
200  *              error array and the parse may be terminated.
201  */
202
203 /*
204  * Note an 'r' error.
205  */
206 VOID
207 rerr()
208 {
209         err('r');
210 }
211
212 /*
213  * Note an 'a' error.
214  */
215 VOID
216 aerr()
217 {
218         err('a');
219 }
220
221 /*
222  * Note a 'q' error.
223  */
224 VOID
225 qerr()
226 {
227         err('q');
228 }
229
230 /*
231  * ASxxxx assembler errors
232  */
233 char *errors[] = {
234         "<.> use \". = . + <arg>\" not \". = <arg>\"",
235         "<a> machine specific addressing or addressing mode error",
236         "<b> direct page boundary error",
237         "<d> direct page addressing error",
238         "<i> .include file error or an .if/.endif mismatch",
239         "<m> multiple definitions error",
240         "<o> .org in REL area or directive / mnemonic error",
241         "<p> phase error: label location changing between passes 2 and 3",
242         "<q> missing or improper operators, terminators, or delimiters",
243         "<r> relocation error",
244         "<u> undefined symbol encountered during assembly",
245         NULL
246 };
247         
248 /*)Function:    char    *getarr(c)
249  *
250  *              int     c               the error code character
251  *
252  *      The function geterr() scans the list of errors returning the
253  *      error string corresponding to the input error character.
254  *
255  *      local variables:
256  *              int     i               error index counter
257  *
258  *      global variables:
259  *              char    *errors[]       array of pointers to the
260  *                                      error strings
261  *
262  *      functions called:
263  *              none
264  *
265  *      side effects:
266  *              A pointer to the appropriate
267  *              error code string is returned.
268  */
269 char *
270 geterr(c)
271 int c;
272 {
273         int     i;
274
275         for (i=0; errors[i]!=NULL; i++) {
276                 if (c == errors[i][1]) {
277                         return(errors[i]);
278                 }
279         }
280         return(NULL);
281 }
282