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