]> git.sur5r.net Git - cc65/blob - src/ld65/asserts.c
Improved code generation
[cc65] / src / ld65 / asserts.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 asserts.c                                 */
4 /*                                                                           */
5 /*                      Assertions for the ld65 linker                       */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 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 /* common */
37 #include "assertdefs.h"
38 #include "coll.h"
39 #include "xmalloc.h"
40
41 /* ld65 */
42 #include "asserts.h"
43 #include "error.h"
44 #include "expr.h"
45 #include "fileio.h"
46 #include "objdata.h"
47 #include "spool.h"
48
49
50
51 /*****************************************************************************/
52 /*                                   Data                                    */
53 /*****************************************************************************/
54
55
56
57 /* List with all assertions */
58 static Collection Assertions = STATIC_COLLECTION_INITIALIZER;
59
60
61
62 /*****************************************************************************/
63 /*                                   Code                                    */
64 /*****************************************************************************/
65
66
67
68 Assertion* ReadAssertion (FILE* F, struct ObjData* O)
69 /* Read an assertion from the given file */
70 {
71     /* Allocate memory */
72     Assertion* A = xmalloc (sizeof (Assertion));
73
74     /* Read the fields from the file */
75     A->Expr = ReadExpr (F, O);
76     A->Action = ReadVar (F);
77     A->Msg = MakeGlobalStringId (O, ReadVar (F));
78     ReadFilePos (F, &A->Pos);
79
80     /* Set remaining fields */
81     A->Obj = O;
82
83     /* Add the assertion to the global list */
84     CollAppend (&Assertions, A);
85
86     /* Return the new struct */
87     return A;
88 }
89
90
91
92 void CheckAssertions (void)
93 /* Check all assertions */
94 {
95     unsigned I;
96
97     /* Walk over all assertions */
98     for (I = 0; I < CollCount (&Assertions); ++I) {
99
100         /* Get the assertion */
101         Assertion* A = CollAtUnchecked (&Assertions, I);
102
103         /* If the expression is not constant, we're not able to handle it */
104         if (!IsConstExpr (A->Expr)) {
105             Warning ("Cannot evaluate assertion in module `%s', line %lu",
106                      GetSourceFileName (A->Obj, A->Pos.Name), A->Pos.Line);
107         } else if (GetExprVal (A->Expr) == 0) {
108
109             /* Assertion failed */
110             const char* Module  = GetSourceFileName (A->Obj, A->Pos.Name);
111             const char* Message = GetString (A->Msg);
112
113             switch (A->Action) {
114
115                 case ASSERT_ACT_WARN:
116                     Warning ("%s(%lu): %s", Module, A->Pos.Line, Message);
117                     break;
118
119                 case ASSERT_ACT_ERROR:
120                     Error ("%s(%lu): %s", Module, A->Pos.Line, Message);
121                     break;
122
123                 default:
124                     Internal ("Invalid assertion action (%u) in module `%s', "
125                               "line %lu (file corrupt?)",
126                               A->Action, Module, A->Pos.Line);
127                     break;
128             }
129         }
130     }
131 }
132
133
134