]> git.sur5r.net Git - cc65/blob - src/ca65/error.c
Fixed a bug: .ENDPROC without .PROC would crash the assembler
[cc65] / src / ca65 / error.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                  error.c                                  */
4 /*                                                                           */
5 /*                Error handling for the ca65 macroassembler                 */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 1998-2003 Ullrich von Bassewitz                                       */
10 /*               Römerstrasse 52                                             */
11 /*               D-70794 Filderstadt                                         */
12 /* EMail:        uz@cc65.org                                                 */
13 /*                                                                           */
14 /*                                                                           */
15 /* This software is provided 'as-is', without any expressed or implied       */
16 /* warranty.  In no event will the authors be held liable for any damages    */
17 /* arising from the use of this software.                                    */
18 /*                                                                           */
19 /* Permission is granted to anyone to use this software for any purpose,     */
20 /* including commercial applications, and to alter it and redistribute it    */
21 /* freely, subject to the following restrictions:                            */
22 /*                                                                           */
23 /* 1. The origin of this software must not be misrepresented; you must not   */
24 /*    claim that you wrote the original software. If you use this software   */
25 /*    in a product, an acknowledgment in the product documentation would be  */
26 /*    appreciated but is not required.                                       */
27 /* 2. Altered source versions must be plainly marked as such, and must not   */
28 /*    be misrepresented as being the original software.                      */
29 /* 3. This notice may not be removed or altered from any source              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <stdarg.h>
39
40 /* ca65 */
41 #include "filetab.h"
42 #include "nexttok.h"
43 #include "error.h"
44
45
46
47 /*****************************************************************************/
48 /*                                   Data                                    */
49 /*****************************************************************************/
50
51
52
53 /* Warning level */
54 unsigned WarnLevel          = 1;
55
56 /* Statistics */
57 unsigned ErrorCount     = 0;
58 unsigned WarningCount   = 0;
59
60
61
62 /*****************************************************************************/
63 /*                                 Warnings                                  */
64 /*****************************************************************************/
65
66
67
68 void WarningMsg (const FilePos* Pos, unsigned WarnNum, va_list ap)
69 /* Print warning message. */
70 {
71     static const struct {
72         unsigned char   Level;
73         const char*     Msg;
74     } Warnings [WARN_COUNT-1] = {
75         {   1,  "Mask error"                                    },
76         {   2,  "Symbol `%s' is defined but never used"         },
77         {   2,  "Symbol `%s' is imported but never used"        },
78         {   1,  "Cannot track processor status byte"            },
79         {   0,  "User warning: %s"                              },
80     };
81
82     if (Warnings [WarnNum-1].Level <= WarnLevel) {
83         fprintf (stderr, "%s(%lu): Warning #%u: ",
84                  GetFileName (Pos->Name), Pos->Line, WarnNum);
85         vfprintf (stderr, Warnings [WarnNum-1].Msg, ap);
86         fprintf (stderr, "\n");
87         ++WarningCount;
88     }
89 }
90
91
92
93 void Warning (unsigned WarnNum, ...)
94 /* Print warning message. */
95 {
96     va_list ap;
97     va_start (ap, WarnNum);
98     WarningMsg (&CurPos, WarnNum, ap);
99     va_end (ap);
100 }
101
102
103
104 void PWarning (const FilePos* Pos, unsigned WarnNum, ...)
105 /* Print warning message giving an explicit file and position. */
106 {
107     va_list ap;
108     va_start (ap, WarnNum);
109     WarningMsg (Pos, WarnNum, ap);
110     va_end (ap);
111 }
112
113
114
115 /*****************************************************************************/
116 /*                                  Errors                                   */
117 /*****************************************************************************/
118
119
120
121 void ErrorMsg (const FilePos* Pos, unsigned ErrNum, va_list ap)
122 /* Print an error message */
123 {
124     static const char* Msgs [ERR_COUNT-1] = {
125         "Command/operation not implemented",
126         "Cannot open include file `%s': %s",
127         "Cannot read from include file `%s': %s",
128         "Include nesting too deep",
129         "Invalid input character: %02X",
130         "Hex digit expected",
131         "Digit expected",
132         "`0' or `1' expected",
133         "Numerical overflow",
134         "Control statement expected",
135         "Too many characters",
136         "`:' expected",
137         "`(' expected",
138         "`)' expected",
139         "`]' expected",
140         "`,' expected",
141         "Boolean switch value expected (on/off/+/-)",
142         "`Y' expected",
143         "`X' expected",
144         "Integer constant expected",
145         "String constant expected",
146         "Character constant expected",
147         "Constant expression expected",
148         "Identifier expected",
149         "`.ENDMACRO' expected",
150         "Option key expected",
151         "`=' expected",
152         "Command is only valid in 65816 mode",
153         "User error: %s",
154         "String constant too long",
155         "Newline in string constant",
156         "Illegal character constant",
157         "Illegal addressing mode",
158         "Illegal character to start local symbols",
159         "Illegal use of local symbol",
160         "Illegal segment name: `%s'",
161         "Illegal segment attribute",
162         "Illegal macro package name",
163         "Illegal emulation feature",
164         "Illegal scope specifier",
165         "Syntax error",
166         "Symbol `%s' is already defined",
167         "Undefined symbol `%s'",
168         "Symbol `%s' is already marked as import",
169         "Symbol `%s' is already marked as export",
170         "Exported symbol `%s' is undefined",
171         "Exported values must be constant",
172         "Unexpected end of file",
173         "Unexpected end of line",
174         "Unexpected `%s'",
175         "Division by zero",
176         "Modulo operation with zero",
177         "Range error",
178         "Too many macro parameters",
179         "Macro parameter expected",
180         "Circular reference in symbol definition",
181         "Symbol `%s' redeclaration mismatch",
182         "Alignment value must be a power of 2",
183         "Duplicate `.ELSE'",
184         "Conditional assembly branch was never closed",
185         "Lexical level was not terminated correctly",
186         "No open lexical level",
187         "Segment attribute mismatch",
188         "Segment stack overflow",
189         "Segment stack is empty",
190         "Segment stack is not empty at end of assembly",
191         "CPU not supported",
192         "Counter underflow",
193         "Undefined label",
194         "Open `%s'",
195         "File name `%s' not found in file table",
196     };
197
198     fprintf (stderr, "%s(%lu): Error #%u: ",
199              GetFileName (Pos->Name), Pos->Line, ErrNum);
200     vfprintf (stderr, Msgs [ErrNum-1], ap);
201     fprintf (stderr, "\n");
202     ++ErrorCount;
203 }
204
205
206
207 void Error (unsigned ErrNum, ...)
208 /* Print an error message */
209 {
210     va_list ap;
211     va_start (ap, ErrNum);
212     ErrorMsg (&CurPos, ErrNum, ap);
213     va_end (ap);
214 }
215
216
217
218 void PError (const FilePos* Pos, unsigned ErrNum, ...)
219 /* Print an error message giving an explicit file and position. */
220 {
221     va_list ap;
222     va_start (ap, ErrNum);
223     ErrorMsg (Pos, ErrNum, ap);
224     va_end (ap);
225 }
226
227
228
229 void ErrorSkip (unsigned ErrNum, ...)
230 /* Print an error message and skip the rest of the line */
231 {
232     va_list ap;
233     va_start (ap, ErrNum);
234     ErrorMsg (&CurPos, ErrNum, ap);
235     va_end (ap);
236
237     SkipUntilSep ();
238 }
239
240
241
242 /*****************************************************************************/
243 /*                                   Code                                    */
244 /*****************************************************************************/
245
246
247
248 void Fatal (unsigned FatNum, ...)
249 /* Print a message about a fatal error and die */
250 {
251     static const char* Msgs [FAT_COUNT-1] = {
252         "Maximum number of input files reached",
253         "Out of memory",
254         "Too many segments",
255         "String too long",
256         "Cannot open input file `%s': %s",
257         "Cannot stat input file `%s': %s",
258         "Cannot open output file `%s': %s",
259         "Cannot write to output file `%s': %s",
260         "Cannot open listing file: %s",
261         "Cannot write to listing file: %s",
262         "Cannot read from listing file: %s",
263         "Too many nested constructs",
264         ".IF nesting too deep",
265         "Too many symbols",
266     };
267     va_list ap;
268
269     va_start (ap, FatNum);
270     fprintf (stderr, "Fatal #%u: ", FatNum);
271     vfprintf (stderr, Msgs [FatNum-1], ap);
272     fprintf (stderr, "\n");
273     va_end (ap);
274
275     /* And die... */
276     exit (EXIT_FAILURE);
277 }
278
279
280
281 void Internal (const char* Format, ...)
282 /* Print a message about an internal compiler error and die. */
283 {
284     va_list ap;
285     va_start (ap, Format);
286     fprintf (stderr, "Internal assembler error\n");
287     vfprintf (stderr, Format, ap);
288     va_end (ap);
289     fprintf (stderr, "\n");
290
291     exit (EXIT_FAILURE);
292 }
293
294
295