1 /*****************************************************************************/
5 /* Node for the tree that is generated for a switch statement */
9 /* (C) 2001 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
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 /*****************************************************************************/
49 /*****************************************************************************/
51 /*****************************************************************************/
55 CaseNode* NewCaseNode (unsigned char Value)
56 /* Create and initialize a new CaseNode */
59 CaseNode* N = xmalloc (sizeof (CaseNode));
61 /* Initialize the fields */
66 /* Return the new node */
72 void FreeCaseNode (CaseNode* N)
73 /* Delete a case node plus all sub nodes */
76 FreeCaseNodeColl (N->Nodes);
83 void FreeCaseNodeColl (Collection* Nodes)
84 /* Free a collection of case nodes */
87 for (I = 0; I < CollCount (Nodes); ++I) {
88 FreeCaseNode (CollAtUnchecked (Nodes, I));
90 FreeCollection (Nodes);
95 int SearchCaseNode (const Collection* Nodes, unsigned char Key, int* Index)
96 /* Search for a node in the given collection. If the node has been found,
97 * set Index to the index of the node and return true. If the node was not
98 * found, set Index the the insertion position of the node and return
102 /* Do a binary search */
104 int Last = CollCount (Nodes) - 1;
107 while (First <= Last) {
109 /* Set current to mid of range */
110 int Current = (Last + First) / 2;
112 /* Get the entry from this position */
113 const CaseNode* N = CollConstAt (Nodes, Current);
115 /* Compare the values */
116 if (N->Value < Key) {
120 if (N->Value == Key) {
121 /* Found. We cannot have duplicates, so end the search here. */
135 unsigned InsertCaseValue (Collection* Nodes, unsigned long Val, unsigned Depth)
136 /* Insert a new case value into a CaseNode tree with the given depth. Return
137 * the code label for the value.
141 unsigned CaseLabel = GetLocalLabel (); /* Code label */
148 unsigned char Key = (Val >> (Depth * CHAR_BIT)) & 0xFF;
150 /* Search for the node in the collection */
151 if (SearchCaseNode (Nodes, Key, &Index) == 0) {
153 /* Node not found - insert one */
154 N = NewCaseNode (Key);
155 CollInsert (Nodes, N, Index);
157 /* If this is not the last round, create the collection for
158 * the subnodes, otherwise get a label for the code.
161 N->Nodes = NewCollection ();
163 N->Label = CaseLabel;
167 /* Node found, get it */
168 N = CollAt (Nodes, Index);
170 /* If this is the last round and we found a node, we have a
171 * duplicate case label in a switch.
174 Error ("Duplicate case label");
178 /* Get the collection from the node for the next round. */
182 /* Return the label of the node we found/created */