comparison label.c @ 0:4410c9c7750d

Initial import.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 24 Feb 2015 18:53:52 +0200
parents
children cfcae2ef1c2b
comparison
equal deleted inserted replaced
-1:000000000000 0:4410c9c7750d
1 /*\
2 * dxa v0.1.1 -- symbolic 65xx disassembler
3 *
4 * Copyright (C) 1993, 1994 Marko M\"akel\"a
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 *
20 * Contacting the author:
21 *
22 * Via Internet E-mail:
23 * <Marko.Makela@FTP.FUNET.FI>
24 *
25 * Via Snail Mail:
26 * Marko M\"akel\"a
27 * Sillitie 10 A
28 * FIN-01480 VANTAA
29 * Finland
30 \*/
31
32 #define _LABEL_C_
33
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <string.h>
37
38 #include "proto.h"
39 #include "options.h"
40 #include "opcodes.h"
41
42 label *labeltable;
43 char defaultlabel[5];
44 unsigned numLabels = 0;
45
46 #ifndef __STDC__
47 void
48 AddLabel (address, name)
49 ADDR_T address;
50 char *name;
51 #else
52 void AddLabel (ADDR_T address, char *name)
53 #endif
54 {
55 label *entry;
56 char *buffer;
57
58 if (!((buffer = malloc (strlen (name)))))
59 return;
60
61 entry = numLabels ?
62 realloc (labeltable, (numLabels + 1) * sizeof *entry) :
63 malloc (sizeof *entry);
64
65 if (!entry) return;
66
67 labeltable = entry;
68 entry = &labeltable[numLabels++];
69
70 entry->address = address;
71 entry->name = buffer;
72 while ((*buffer++ = *name++));
73 }
74
75 #ifndef __STDC__
76 char *
77 Label (address, admode)
78 ADDR_T address;
79 int admode;
80 #else
81 char *Label (ADDR_T address, int admode)
82 #endif
83 {
84 label *entry;
85
86 if (!IsLabeled (address)) {
87
88 /* dirty kludge to allow zero page stuff to still work. this sometimes
89 guesses wrong */
90 if (admode == zp)
91 sprintf (defaultlabel, "$%02x", address);
92 else
93 sprintf (defaultlabel, "$%04x", address);
94 return defaultlabel;
95 }
96
97 for (entry = &labeltable[numLabels]; entry-- > labeltable;)
98 if (entry->address == address)
99 return entry->name;
100
101 sprintf (defaultlabel, "l%x", address);
102
103 return defaultlabel;
104 }
105
106 #ifndef __STDC__
107 void
108 Collect ()
109 #else
110 void Collect (void)
111 #endif
112 {
113 unsigned counter = 0;
114 table *entry = NULL, *entry2;
115 label *labels;
116
117 if (fVerbose)
118 fprintf (stderr, "%s: collecting garbage.\n", prog);
119
120 while ((entry = entry2 = FindNextEntryType (entry, 0, 0))) {
121 counter++;
122
123 PutLabel (entry->address);
124 PutLowByte (entry->address);
125 PutHighByte (entry->address);
126
127 while ((entry2 = FindNextEntry (entry2, entry->address,
128 ~0, entry->type)))
129 DeleteEntry (entry2); /* remove duplicate warnings */
130 }
131
132 if ((entry = malloc (counter * sizeof *entry))) { /* compact the table */
133 entrycount = counter;
134
135 for (entry2 = scantable; counter; entry2++) {
136 if (!entry2->type) continue;
137
138 memcpy (&entry[--counter], entry2, sizeof *entry);
139 }
140
141 free (scantable);
142 scantable = entry;
143 }
144
145 for (labels = &labeltable[numLabels]; labels-- > labeltable;)
146 if ((ADDR_T)(labels->address - StartAddress) <
147 (ADDR_T)(EndAddress - StartAddress)) {
148 PutLabel (labels->address);
149 PutLowByte (labels->address);
150 PutHighByte (labels->address);
151 }
152 }