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