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