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