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