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