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