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