[go: up one dir, main page]

File: symtab.c

package info (click to toggle)
z80dasm 1.1.5-1
  • links: PTS
  • area: main
  • in suites: buster
  • size: 3,276 kB
  • sloc: asm: 70,115; ansic: 2,541; sh: 1,255; makefile: 275
file content (350 lines) | stat: -rw-r--r-- 6,207 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/* Copyright (C) 2007-2012 Tomaz Solc                                      */

/* This program is free software; you can redistribute it and/or modify    */
/* it under the terms of the GNU General Public License as published by    */
/* the Free Software Foundation; either version 2 of the License, or       */
/* (at your option) any later version.                                     */

/* This program is distributed in the hope that it will be useful,         */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of          */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           */
/* GNU General Public License for more details.                            */

/* You should have received a copy of the GNU General Public License       */
/* along with this program; if not, write to the Free Software             */
/* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "symtab.h"
#include "dz80.h"

struct symbol *symbols=NULL;

/* TODO: This symbol table implementation in slow. */

void symbol_remove(struct symbol *symb)
{
	struct reference *ref,*refn;

	msg(2, "Debug: removing symbol '%s' with value 0x%04x\n", 
						symb->name, symb->val);

	ref=symb->ref;
	while(ref!=NULL) {
		refn=ref->next;
		free(ref);
		ref=refn;
	}

	free(symb->name);

	if(symb->prev!=NULL) {
		symb->prev->next=symb->next;
	} else {
		symbols=symb->next;
	}

	if(symb->next!=NULL) {
		symb->next->prev=symb->prev;
	}

	free(symb);
}

struct symbol *symbol_find(int val)
{
	struct symbol *cur;

	cur=symbols;
	while(cur!=NULL) {
		if(cur->val == val) {
			return cur;
		}
		cur=cur->next;
	}

	return NULL;
}

struct symbol *symbol_find_next(int val, struct symbol *cur)
{
	if(cur==NULL) {
		cur=symbols;
	} else {
		cur=cur->next;
	}

	while(cur!=NULL) {
		if(cur->val == val) {
			return cur;
		}
		cur=cur->next;
	}

	return NULL;
}

int symbol_compare(struct symbol *a, struct symbol *b)
{
	if(a->val > b->val) {
		return 1;
	} else if(a->val < b->val) {
		return -1;
	}

	if(a->weight > b->weight) {
		return 1;
	} else if(a->weight < b->weight) {
		return -1;
	}

	return 0;
}

/* Helper function for symbol_new(). It finds a place where the new 
 * symbol will be inserted into the linked list */
void symbol_find_place(struct symbol *new, 
				struct symbol **prev, struct symbol **next)
{
	struct symbol *cur;

	*prev=NULL;
	cur=symbols;
	while(cur!=NULL) {
		*next=cur;
		if(symbol_compare(cur, new) > 0) {
			return;
		}
		*prev=cur;
		cur=cur->next;
	}

	*next=NULL;
	return;
}

struct symbol *symbol_find_inrange(int val, int range)
{
	struct symbol *cur;

	cur=symbols;
	while(cur!=NULL) {
		if(val<=cur->val && (val+range)>cur->val) {
			return cur;			
		}
		cur=cur->next;
	}

	return NULL;
}

struct symbol *symbol_find_range(int val)
{
	struct symbol *cur;

	cur=symbols;
	while(cur!=NULL) {
		if(cur->val<=val && (cur->val+cur->range)>val) {
			return cur;			
		}
		cur=cur->next;
	}

	return NULL;
}

struct symbol *symbol_new(char *name, int val, int weight, int included)
{
	struct symbol *prev, *next;
	struct symbol *dest;

	msg(2, "Debug: defining new symbol '%s' with value 0x%04x\n", 
							name, val);

	dest=symbol_find(val);
	if(dest!=NULL) {
		if(!strcmp(name, dest->name)) return NULL;
		/*
		msg(0, "Warning: two symbols with same value defined "
			"('%s' and '%s')\n", name, dest->name);
	 	*/
	}

	dest=calloc(1, sizeof(*dest));
	if(dest==NULL) return NULL;

	dest->name=strdup(name);
	dest->val=val;
	dest->weight=weight;

	dest->automatic=0;
	dest->range=1;
	dest->included=included;
	dest->label=0;

	symbol_find_place(dest, &prev, &next);

	dest->prev=prev;
	dest->next=next;

	if(next!=NULL) {
		next->prev=dest;
	}
	if(prev!=NULL) {
		prev->next=dest;
	} else {
		symbols=dest;
	}

	dest->ref=NULL;

	return dest;
}

struct symbol *symbol_newref(int val, int addr, enum referencetype type)
{
	struct symbol *dest;
	struct reference *r;

	/* strlen("sub_0000h")+1 = 10 */
	char name[10];

	dest=symbol_find(val);

	if(dest==NULL) {
		if(type==cldest) {
			sprintf(name, "sub_%04xh", val);
		} else {
			sprintf(name, "l%04xh", val);
		}

		dest=symbol_new(name, val, 50, 0);

		dest->automatic=1;
	}

	r=calloc(1, sizeof(*r));

	r->addr=addr;
	r->type=type;

	r->next=dest->ref;
	dest->ref=r;

	return dest;
}

int symbol_setlabel(int val, int range)
{
	struct symbol *dest;

	dest=symbol_find(val);
	if(dest==NULL) return 1;

	dest->range=range;
	dest->label=1;

	return 0;
}

void symbol_remove_nonlabels()
{
	struct symbol *symb,*symbn;

	symb=symbols;
	while(symb!=NULL) {
		symbn=symb->next;

		if((!symb->label) && symb->automatic) {
			symbol_remove(symb);
		}

		symb=symbn;
	}
}

int symbol_export_nonlabels(FILE *f)
{
	struct symbol *cur;

	cur=symbols;
	while(cur!=NULL) {
		if((!cur->label) && cur->ref != NULL) {
			fprintf(f, "%s:\tequ 0x%04x\n", cur->name, cur->val);
		}
		cur=cur->next;
	}

	return 0;
}

int symbol_export(FILE *f)
{
	struct symbol *cur;
	struct reference *r;

	fprintf(f, "; Symbol file\n\n");

	symbol_export_includes(f);

	fprintf(f, "\n");

	cur=symbols;
	while(cur!=NULL) {
		if(cur->included) {
			cur=cur->next;
			continue;
		}

		if(cur->comment!=NULL) {
			fprintf(f, "%s", cur->comment);
		}

		fprintf(f, "%s:\tequ 0x%04x\n", cur->name, cur->val);
		fprintf(f, "; Used at following locations:\n");

		r=cur->ref;
		while(r!=NULL) {
			fprintf(f, "; 0x%04x\t", r->addr);
			switch(r->type) {
				case undef:
					fprintf(f, "(unknown)");	
					break;
				case jpdest:	
					fprintf(f, "(jp destination)");
					break;
				case jrdest:	
					fprintf(f, "(jr destination)");
					break;
				case djdest:	
					fprintf(f, "(djnz destination)");
					break;
				case cldest: 
					fprintf(f, "(call destination)");
					break;
				case vrbyte: 
					fprintf(f, "(byte load pointer)");
					break; 
				case vrword: 
					fprintf(f, "(word load pointer)");
					break;
				case cstadd: 
					fprintf(f, "(immediate value)");
					break;
				case cstdfw: 
					fprintf(f, "(defw directive)");
					break;
			}
			fprintf(f, "\n");
			r=r->next;
		}
		fprintf(f, "\n");

		cur=cur->next;
	}

	return 1;
}