Mercurial > hg > forks > dxa
annotate label.c @ 15:89183953bddc
Delete trailing whitespace.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Thu, 14 Oct 2021 01:40:24 +0300 |
parents | 84c0facfc43c |
children | a2a81589380d |
rev | line source |
---|---|
0 | 1 /*\ |
14
84c0facfc43c
Merge changes from upstream v0.1.4.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
2 * dxa -- symbolic 65xx disassembler |
0 | 3 * |
4 * Copyright (C) 1993, 1994 Marko M\"akel\"a | |
14
84c0facfc43c
Merge changes from upstream v0.1.4.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
5 * Copyright (C) 2019 Cameron Kaiser |
0 | 6 * |
7 * This program is free software; you can redistribute it and/or modify | |
8 * it under the terms of the GNU General Public License as published by | |
9 * the Free Software Foundation; either version 2 of the License, or | |
10 * (at your option) any later version. | |
11 * | |
12 * This program is distributed in the hope that it will be useful, | |
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 * GNU General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU General Public License | |
18 * along with this program; if not, write to the Free Software | |
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
20 * | |
21 \*/ | |
22 | |
23 #define _LABEL_C_ | |
24 | |
25 #include <stdlib.h> | |
26 #include <stdio.h> | |
27 #include <string.h> | |
28 | |
29 #include "proto.h" | |
30 #include "options.h" | |
31 #include "opcodes.h" | |
32 | |
33 label *labeltable; | |
4
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
34 char defaultlabel[512]; |
0 | 35 unsigned numLabels = 0; |
36 | |
2
ec2f8f6f1dc9
Cleanup pass #1: Get rid of some ancient K&R-isms.
Matti Hamalainen <ccr@tnsp.org>
parents:
1
diff
changeset
|
37 |
4
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
38 void AddLabel(ADDR_T address, char *name, int is_range, ADDR_T len) |
0 | 39 { |
4
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
40 label * entry = numLabels ? |
0 | 41 realloc (labeltable, (numLabels + 1) * sizeof *entry) : |
42 malloc (sizeof *entry); | |
43 | |
44 if (!entry) return; | |
45 | |
46 labeltable = entry; | |
47 entry = &labeltable[numLabels++]; | |
48 | |
49 entry->address = address; | |
4
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
50 entry->name = strdup(name); |
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
51 entry->is_range = is_range; |
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
52 entry->len = len; |
0 | 53 } |
54 | |
10 | 55 |
4
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
56 int IsInsideRegion(ADDR_T address) |
0 | 57 { |
58 label *entry; | |
59 | |
4
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
60 for (entry = &labeltable[numLabels]; entry-- > labeltable;) |
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
61 { |
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
62 int offs = address - entry->address; |
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
63 if (entry->is_range && offs > 0 && offs < entry->len) |
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
64 return TRUE; |
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
65 } |
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
66 return FALSE; |
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
67 } |
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
68 |
0 | 69 |
4
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
70 char *Label(ADDR_T address, int admode, int allow_range) |
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
71 { |
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
72 label *entry; |
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
73 int match = 0; |
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
74 |
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
75 if (!IsLabeled (address)) { |
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
76 // dirty kludge to allow zero page stuff to still work. this sometimes |
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
77 // guesses wrong |
14
84c0facfc43c
Merge changes from upstream v0.1.4.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
78 if (admode == zp) { |
4
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
79 snprintf(defaultlabel, sizeof(defaultlabel), "$%02x", address); |
14
84c0facfc43c
Merge changes from upstream v0.1.4.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
80 } else { |
4
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
81 snprintf(defaultlabel, sizeof(defaultlabel), "$%04x", address); |
14
84c0facfc43c
Merge changes from upstream v0.1.4.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
82 } |
4
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
83 |
0 | 84 return defaultlabel; |
85 } | |
86 | |
87 for (entry = &labeltable[numLabels]; entry-- > labeltable;) | |
4
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
88 { |
0 | 89 if (entry->address == address) |
90 return entry->name; | |
12
3c00e5162f34
Check user-defined labels first before generating one.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
91 } |
0 | 92 |
12
3c00e5162f34
Check user-defined labels first before generating one.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
93 for (entry = &labeltable[numLabels]; entry-- > labeltable;) |
3c00e5162f34
Check user-defined labels first before generating one.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
94 { |
3c00e5162f34
Check user-defined labels first before generating one.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
95 int offs = address - entry->address; |
4
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
96 if (entry->is_range && offs >= 0 && offs < entry->len) |
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
97 { |
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
98 match = 1; |
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
99 if (allow_range) |
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
100 { |
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
101 snprintf(defaultlabel, sizeof(defaultlabel), |
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
102 (offs < 16) ? "%s + %d" : "%s + $%x", |
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
103 entry->name, offs); |
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
104 return defaultlabel; |
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
105 } |
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
106 else |
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
107 break; |
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
108 } |
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
109 } |
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
110 |
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
111 if (match) |
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
112 snprintf(defaultlabel, sizeof(defaultlabel), "XXX_%x", address); |
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
113 else |
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
114 snprintf(defaultlabel, sizeof(defaultlabel), "l_%x", address); |
0 | 115 |
116 return defaultlabel; | |
117 } | |
118 | |
2
ec2f8f6f1dc9
Cleanup pass #1: Get rid of some ancient K&R-isms.
Matti Hamalainen <ccr@tnsp.org>
parents:
1
diff
changeset
|
119 |
0 | 120 void Collect (void) |
121 { | |
122 unsigned counter = 0; | |
123 table *entry = NULL, *entry2; | |
124 label *labels; | |
125 | |
126 if (fVerbose) | |
127 fprintf (stderr, "%s: collecting garbage.\n", prog); | |
128 | |
10 | 129 while ((entry = entry2 = FindNextEntryType (entry, 0, 0))) |
130 { | |
0 | 131 counter++; |
132 | |
133 PutLabel (entry->address); | |
134 PutLowByte (entry->address); | |
135 PutHighByte (entry->address); | |
136 | |
10 | 137 while ((entry2 = FindNextEntry (entry2, entry->address, ~0, entry->type))) |
138 { | |
0 | 139 DeleteEntry (entry2); /* remove duplicate warnings */ |
10 | 140 } |
0 | 141 } |
142 | |
143 if ((entry = malloc (counter * sizeof *entry))) { /* compact the table */ | |
144 entrycount = counter; | |
145 | |
146 for (entry2 = scantable; counter; entry2++) { | |
147 if (!entry2->type) continue; | |
148 | |
149 memcpy (&entry[--counter], entry2, sizeof *entry); | |
150 } | |
151 | |
152 free (scantable); | |
153 scantable = entry; | |
154 } | |
155 | |
156 for (labels = &labeltable[numLabels]; labels-- > labeltable;) | |
10 | 157 { |
158 if ((ADDR_T)(labels->address - StartAddress) < (ADDR_T)(EndAddress - StartAddress)) | |
159 { | |
0 | 160 PutLabel (labels->address); |
161 PutLowByte (labels->address); | |
162 PutHighByte (labels->address); | |
163 } | |
10 | 164 } |
0 | 165 } |