]> git.sur5r.net Git - cc65/blob - src/cc65/codeent.c
bdf8d268a8c050de4e81cbc286558713c49b1bd4
[cc65] / src / cc65 / codeent.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 codeent.c                                 */
4 /*                                                                           */
5 /*                            Code segment entry                             */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2001     Ullrich von Bassewitz                                        */
10 /*              Wacholderweg 14                                              */
11 /*              D-70597 Stuttgart                                            */
12 /* EMail:       uz@musoftware.de                                             */
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 "codeinfo.h"
42 #include "error.h"
43 #include "global.h"
44 #include "codelab.h"
45 #include "opcodes.h"
46 #include "codeent.h"
47
48
49
50 /*****************************************************************************/
51 /*                                   Data                                    */
52 /*****************************************************************************/
53
54
55
56 /*****************************************************************************/
57 /*                                   Code                                    */
58 /*****************************************************************************/
59
60
61
62 CodeEntry* NewCodeEntry (const OPCDesc* D, am_t AM, const char* Arg, CodeLabel* JumpTo)
63 /* Create a new code entry, initialize and return it */
64 {
65     /* Allocate memory */
66     CodeEntry* E = xmalloc (sizeof (CodeEntry));
67
68     /* Initialize the fields */
69     E->OPC      = D->OPC;
70     E->AM       = AM;
71     E->Size     = GetInsnSize (E->OPC, E->AM);
72     E->Hints    = 0;
73     E->Arg      = (Arg && Arg[0] != '\0')? xstrdup (Arg) : 0;
74     E->Num      = 0;
75     E->Flags    = 0;
76     E->Use      = D->Use;
77     E->Chg      = D->Chg;
78     if (E->OPC == OPC_JSR && E->Arg) {
79         /* A subroutine call */
80         GetFuncInfo (E->Arg, &E->Use, &E->Chg);
81     } else {
82         /* Some other instruction */
83         E->Use |= GetAMUseInfo (AM);
84     }
85     E->JumpTo   = JumpTo;
86     InitCollection (&E->Labels);
87
88     /* If we have a label given, add this entry to the label */
89     if (JumpTo) {
90         CollAppend (&JumpTo->JumpFrom, E);
91     }
92
93     /* Return the initialized struct */
94     return E;
95 }
96
97
98
99 void FreeCodeEntry (CodeEntry* E)
100 /* Free the given code entry */
101 {
102     /* Free the string argument if we have one */
103     xfree (E->Arg);
104
105     /* Cleanup the collection */
106     DoneCollection (&E->Labels);
107
108     /* Free the entry */
109     xfree (E);
110 }
111
112
113
114 int CodeEntryHasLabel (const CodeEntry* E)
115 /* Check if the given code entry has labels attached */
116 {
117     return (CollCount (&E->Labels) > 0);
118 }
119
120
121
122 void OutputCodeEntry (const CodeEntry* E, FILE* F)
123 /* Output the code entry to a file */
124 {
125     const OPCDesc* D;
126     unsigned Chars;
127
128     /* If we have a label, print that */
129     unsigned LabelCount = CollCount (&E->Labels);
130     unsigned I;
131     for (I = 0; I < LabelCount; ++I) {
132         OutputCodeLabel (CollConstAt (&E->Labels, I), F);
133     }
134
135     /* Get the opcode description */
136     D = GetOPCDesc (E->OPC);
137
138     /* Print the mnemonic */
139     Chars = fprintf (F, "\t%s", D->Mnemo);
140
141     /* Print the operand */
142     switch (E->AM) {
143
144         case AM_IMP:
145             /* implicit */
146             break;
147
148         case AM_ACC:
149             /* accumulator */
150             Chars += fprintf (F, "%*sa", 9-Chars, "");
151             break;
152
153         case AM_IMM:
154             /* immidiate */
155             Chars += fprintf (F, "%*s#%s", 9-Chars, "", E->Arg);
156             break;
157
158         case AM_ZP:
159         case AM_ABS:
160             /* zeropage and absolute */
161             Chars += fprintf (F, "%*s%s", 9-Chars, "", E->Arg);
162             break;
163
164         case AM_ZPX:
165         case AM_ABSX:
166             /* zeropage,X and absolute,X */
167             Chars += fprintf (F, "%*s%s,x", 9-Chars, "", E->Arg);
168             break;
169
170         case AM_ABSY:
171             /* absolute,Y */
172             Chars += fprintf (F, "%*s%s,y", 9-Chars, "", E->Arg);
173             break;
174
175         case AM_ZPX_IND:
176             /* (zeropage,x) */
177             Chars += fprintf (F, "%*s(%s,x)", 9-Chars, "", E->Arg);
178             break;
179
180         case AM_ZP_INDY:
181             /* (zeropage),y */
182             Chars += fprintf (F, "%*s(%s),y", 9-Chars, "", E->Arg);
183             break;
184
185         case AM_ZP_IND:
186             /* (zeropage) */
187             Chars += fprintf (F, "%*s(%s)", 9-Chars, "", E->Arg);
188             break;
189
190         case AM_BRA:
191             /* branch */
192             CHECK (E->JumpTo != 0);
193             Chars += fprintf (F, "%*s%s", 9-Chars, "", E->JumpTo->Name);
194             break;
195
196         default:
197             Internal ("Invalid addressing mode");
198
199     }
200
201     /* Print usage info if requested by the debugging flag */
202 //    if (Debug) {
203         Chars += fprintf (F,
204                           "%*s; USE: %c%c%c CHG: %c%c%c",
205                           30-Chars, "",
206                           (E->Use & REG_A)? 'A' : '_',
207                           (E->Use & REG_X)? 'X' : '_',
208                           (E->Use & REG_Y)? 'Y' : '_',
209                           (E->Chg & REG_A)? 'A' : '_',
210                           (E->Chg & REG_X)? 'X' : '_',
211                           (E->Chg & REG_Y)? 'Y' : '_');
212 //    }
213
214     /* Terminate the line */
215     fprintf (F, "\n");
216 }
217
218
219
220