]> git.sur5r.net Git - cc65/blob - src/ca65/asserts.c
Fixes for the watcom makefiles:
[cc65] / src / ca65 / asserts.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 asserts.c                                 */
4 /*                                                                           */
5 /*               Linker assertions for the ca65 crossassembler               */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2003-2008, Ullrich von Bassewitz                                      */
10 /*                Roemerstrasse 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 /* ca65 */
42 #include "asserts.h"
43 #include "error.h"
44 #include "expr.h"
45 #include "objfile.h"
46 #include "scanner.h"
47 #include "spool.h"
48
49
50
51 /*****************************************************************************/
52 /*                                   Data                                    */
53 /*****************************************************************************/
54
55
56
57 /* An assertion entry */
58 typedef struct Assertion Assertion;
59 struct Assertion {
60     ExprNode*   Expr;           /* Expression to evaluate */
61     unsigned    Action;         /* Action to take */
62     unsigned    Msg;            /* Message to print (if any) */
63     FilePos     Pos;            /* File position of assertion */
64 };
65
66 /* Collection with all assertions for a module */
67 static Collection Assertions = STATIC_COLLECTION_INITIALIZER;
68
69
70
71 /*****************************************************************************/
72 /*                                   Code                                    */
73 /*****************************************************************************/
74
75
76
77 static Assertion* NewAssertion (ExprNode* Expr, unsigned Action, unsigned Msg)
78 /* Create a new Assertion struct and return it */
79 {
80     /* Allocate memory */
81     Assertion* A = xmalloc (sizeof (Assertion));
82
83     /* Initialize the fields */
84     A->Expr     = Expr;
85     A->Action   = Action;
86     A->Msg      = Msg;
87     A->Pos      = CurPos;
88
89     /* Return the new struct */
90     return A;
91 }
92
93
94
95 void AddAssertion (ExprNode* Expr, unsigned Action, unsigned Msg)
96 /* Add an assertion to the assertion table */
97 {
98     /* Add an assertion object to the table */
99     CollAppend (&Assertions, NewAssertion (Expr, Action, Msg));
100 }
101
102
103
104 void CheckAssertions (void)
105 /* Check all assertions and evaluate the ones we can evaluate here. */
106 {
107     unsigned I;
108
109     /* Get the number of assertions */
110     unsigned Count = CollCount (&Assertions);
111
112     /* Check the assertions */
113     for (I = 0; I < Count; ++I) {
114
115         /* Get the next assertion */
116         Assertion* A = CollAtUnchecked (&Assertions, I);
117
118         /* Can we evaluate the expression? */
119         long Val;
120         if (IsConstExpr (A->Expr, &Val) && Val == 0) {
121             /* Apply the action */
122             const char* Msg = GetString (A->Msg);
123             switch (A->Action) {
124
125                 case ASSERT_ACT_WARN:
126                     PWarning (&A->Pos, 0, "%s", Msg);
127                     break;
128
129                 case ASSERT_ACT_ERROR:
130                     PError (&A->Pos, "%s", Msg);
131                     break;
132
133                 default:
134                     Internal ("Illegal assert action specifier");
135                     break;
136             }
137         }
138     }
139 }
140
141
142
143 void WriteAssertions (void)
144 /* Write the assertion table to the object file */
145 {
146     unsigned I;
147
148     /* Get the number of assertions */
149     unsigned Count = CollCount (&Assertions);
150
151     /* Tell the object file module that we're about to start the assertions */
152     ObjStartAssertions ();
153
154     /* Write the string count to the list */
155     ObjWriteVar (Count);
156
157     /* Write the assertions */
158     for (I = 0; I < Count; ++I) {
159
160         /* Get the next assertion */
161         Assertion* A = CollAtUnchecked (&Assertions, I);
162
163         /* Write it to the file */
164         WriteExpr (A->Expr);
165         ObjWriteVar (A->Action);
166         ObjWriteVar (A->Msg);
167         ObjWritePos (&A->Pos);
168     }
169
170     /* Done writing the assertions */
171     ObjEndAssertions ();
172 }
173
174
175
176