1 /*****************************************************************************/
5 /* Linker assertions for the ca65 crossassembler */
9 /* (C) 2003-2011, Ullrich von Bassewitz */
10 /* Roemerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
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. */
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: */
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 */
32 /*****************************************************************************/
50 /*****************************************************************************/
52 /*****************************************************************************/
56 /* An assertion entry */
57 typedef struct Assertion Assertion;
59 ExprNode* Expr; /* Expression to evaluate */
60 AssertAction Action; /* Action to take */
61 unsigned Msg; /* Message to print (if any) */
62 Collection LI; /* Line infos for the assertion */
65 /* Collection with all assertions for a module */
66 static Collection Assertions = STATIC_COLLECTION_INITIALIZER;
70 /*****************************************************************************/
72 /*****************************************************************************/
76 static Assertion* NewAssertion (ExprNode* Expr, AssertAction Action, unsigned Msg)
77 /* Create a new Assertion struct and return it */
80 Assertion* A = xmalloc (sizeof (Assertion));
82 /* Initialize the fields */
86 A->LI = EmptyCollection;
87 GetFullLineInfo (&A->LI);
89 /* Return the new struct */
95 void AddAssertion (ExprNode* Expr, AssertAction Action, unsigned Msg)
96 /* Add an assertion to the assertion table */
98 /* Add an assertion object to the table */
99 CollAppend (&Assertions, NewAssertion (Expr, Action, Msg));
104 void CheckAssertions (void)
105 /* Check all assertions and evaluate the ones we can evaluate here. */
109 /* Get the number of assertions */
110 unsigned Count = CollCount (&Assertions);
112 /* Check the assertions */
113 for (I = 0; I < Count; ++I) {
117 /* Get the next assertion */
118 Assertion* A = CollAtUnchecked (&Assertions, I);
120 /* Ignore it, if it should only be evaluated by the linker */
121 if (!AssertAtAsmTime (A->Action)) {
125 /* Can we evaluate the expression? */
126 if (IsConstExpr (A->Expr, &Val) && Val == 0) {
127 /* Apply the action */
128 const char* Msg = GetString (A->Msg);
131 case ASSERT_ACT_WARN:
132 LIWarning (&A->LI, 0, "%s", Msg);
135 case ASSERT_ACT_ERROR:
136 LIError (&A->LI, "%s", Msg);
140 Internal ("Illegal assert action specifier");
149 void WriteAssertions (void)
150 /* Write the assertion table to the object file */
154 /* Get the number of assertions */
155 unsigned Count = CollCount (&Assertions);
157 /* Tell the object file module that we're about to start the assertions */
158 ObjStartAssertions ();
160 /* Write the string count to the list */
163 /* Write the assertions */
164 for (I = 0; I < Count; ++I) {
166 /* Get the next assertion */
167 Assertion* A = CollAtUnchecked (&Assertions, I);
169 /* Write it to the file */
171 ObjWriteVar ((unsigned) A->Action);
172 ObjWriteVar (A->Msg);
173 WriteLineInfo (&A->LI);
176 /* Done writing the assertions */