1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 codelab.c                                 */
4 /*                                                                           */
5 /*                           Code label structure                            */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2001-2009, 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 "check.h"
38 #include "xmalloc.h"
39 
40 /* cc65 */
41 #include "codeent.h"
42 #include "codelab.h"
43 #include "output.h"
44 
45 
46 
47 /*****************************************************************************/
48 /*                                   Code                                    */
49 /*****************************************************************************/
50 
51 
52 
NewCodeLabel(const char * Name,unsigned Hash)53 CodeLabel* NewCodeLabel (const char* Name, unsigned Hash)
54 /* Create a new code label, initialize and return it */
55 {
56     /* Allocate memory */
57     CodeLabel* L = xmalloc (sizeof (CodeLabel));
58 
59     /* Initialize the fields */
60     L->Next  = 0;
61     L->Name  = xstrdup (Name);
62     L->Hash  = Hash;
63     L->Owner = 0;
64     InitCollection (&L->JumpFrom);
65 
66     /* Return the new label */
67     return L;
68 }
69 
70 
71 
FreeCodeLabel(CodeLabel * L)72 void FreeCodeLabel (CodeLabel* L)
73 /* Free the given code label */
74 {
75     /* Free the name */
76     xfree (L->Name);
77 
78     /* Free the collection */
79     DoneCollection (&L->JumpFrom);
80 
81     /* Delete the struct */
82     xfree (L);
83 }
84 
85 
86 
CL_AddRef(CodeLabel * L,struct CodeEntry * E)87 void CL_AddRef (CodeLabel* L, struct CodeEntry* E)
88 /* Let the CodeEntry E reference the label L */
89 {
90     /* The insn at E jumps to this label */
91     E->JumpTo = L;
92 
93     /* Replace the code entry argument with the name of the new label */
94     CE_SetArg (E, L->Name);
95 
96     /* Remember that in the label */
97     CollAppend (&L->JumpFrom, E);
98 }
99 
100 
101 
CL_MoveRefs(CodeLabel * OldLabel,CodeLabel * NewLabel)102 void CL_MoveRefs (CodeLabel* OldLabel, CodeLabel* NewLabel)
103 /* Move all references to OldLabel to point to NewLabel. OldLabel will have no
104 ** more references on return.
105 */
106 {
107     /* Walk through all instructions referencing the old label */
108     unsigned Count = CL_GetRefCount (OldLabel);
109     while (Count--) {
110 
111         /* Get the instruction that references the old label */
112         CodeEntry* E = CL_GetRef (OldLabel, Count);
113 
114         /* Change the reference to the new label */
115         CHECK (E->JumpTo == OldLabel);
116         CL_AddRef (NewLabel, E);
117 
118     }
119 
120     /* There are no more references to the old label */
121     CollDeleteAll (&OldLabel->JumpFrom);
122 }
123 
124 
125 
CL_Output(const CodeLabel * L)126 void CL_Output (const CodeLabel* L)
127 /* Output the code label to the output file */
128 {
129     WriteOutput ("%s:", L->Name);
130     if (strlen (L->Name) > 6) {
131         /* Label is too long, add a linefeed */
132         WriteOutput ("\n");
133     }
134 }
135