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