[go: up one dir, main page]

File: blocks.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 (448 lines) | stat: -rw-r--r-- 9,517 bytes parent folder | download | duplicates (2)
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/* 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 <errno.h>

#include "blocks.h"
#include "symtab.h"
#include "dz80.h"
#include "utils.h"

struct block *blocks=NULL;

/* This function is used to create a default block that covers the entire 
 * input file */
int block_init(int start, int end)
{
	struct block *dest;

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

	dest->start=start;
	dest->end=end;

	dest->type=code;

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

	dest->name=strdup("DEFAULT");

	blocks=dest;

	return 0;
}

struct block *block_find(int addr)
{
	struct block *cur;

	cur=blocks;
	while(cur!=NULL) {
		if(cur->start<=addr && cur->end>addr) {
			return cur;
		}
		cur=cur->next;
	}
	return NULL;
}

struct block *block_dup(struct block *src)
{
	struct block *dest;

	dest=malloc(sizeof(*dest));
	if(dest==NULL) return NULL;

	memcpy(dest, src, sizeof(*dest));

	dest->name=strdup(src->name);

	return(dest);
}

/* TODO: Add support for general case of overlapping blocks. Some examples:
 *
 * before: <---- 1 ----><---- 2 -----> 
 * after:  <-- 1 --><-- 3 --><-- 2 -->
 *
 * before: <--- 1 ---><--- 2 ---><--- 3 --->
 * after:  <-- 1 --><----- 4 -----><-- 3 -->
 */
static struct block *block_new_insert(char *name, int start, int end)
{
	struct block *parent, *parent2, *dest;

	parent=block_find(start);

	if(parent==NULL) {
		msg(0, "Error: Block defined outside of address space "
					"covered by binary data.\n");
		return NULL;
	}

	if(block_find(end-1) != parent) {
		msg(0, "Error: New block overlaps with two or more blocks.\n");
		msg(0, "       This case isn't supported.\n");
		return NULL;
	}

	/* Edge cases */

	if(start == parent->start && end == parent->end) {
		msg(0, "Warning: Block '%s' overlaps completely with '%s'.\n",
						name, parent->name);

		free(parent->name);
		parent->name=strdup(name);
		return(parent);
	}

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

	dest->name=strdup(name);
	dest->start=start;
	dest->end=end;

	if(start == parent->start) {
		/* new block at the beginning of the old block */

		/* before: <--------- parent -------> */
		/* after:  <-- dest --><-- parent --> */

		parent->start=dest->end;

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

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

		parent->prev=dest;

	} else if(end == parent->end) {
		/* new block at the end of the old block */

		/* before: <--------- parent -------> */
		/* after:  <-- parent --><-- dest --> */

		parent->end=dest->start;

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

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

		parent->next=dest;
	} else {
		/* new block in the middle of the old block */

		parent2=block_dup(parent);

		/* before: <--------- parent ----------------------> */
		/* after:  <-- parent --><-- dest --><-- parent2 --> */

		parent->end=dest->start;
		parent2->start=dest->end;

		parent->next=dest;

		dest->prev=parent;
		dest->next=parent2;

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

	return(dest);
}

struct block *block_new(char *name, int start, int end, enum blocktype type,
			enum labeltype start_label, enum labeltype end_label)
{
	struct block *dest;
	struct symbol *symb;
	char tmp[MAX_LINE_LEN];

	msg(2, "Debug: defining new block '%s' (start 0x%04x end 0x%04x)\n", 
							name, start, end);

	dest=block_new_insert(name, start, end);
	if(dest==NULL) return NULL;

	if(start_label == LABEL_START_END || start_label == LABEL_FIRST_LAST) {

		if(start_label == LABEL_START_END) {
			snprintf(tmp, MAX_LINE_LEN, "%s_start", name);
		} else {
			snprintf(tmp, MAX_LINE_LEN, "%s_first", name);
		}

		symb = symbol_new(tmp, start, 100, 0);

		snprintf(tmp, MAX_LINE_LEN, "\n; BLOCK '%s' (start 0x%04x "
					"end 0x%04x)\n", name, start, end);

		symb->comment = strdup(tmp);
	}

	if(end_label == LABEL_START_END) {
		snprintf(tmp, MAX_LINE_LEN, "%s_end", name);
		symbol_new(tmp, end, 0, 0);
	} else if(end_label == LABEL_FIRST_LAST) {
		snprintf(tmp, MAX_LINE_LEN, "%s_last", name);
		symbol_new(tmp, end - 1, 0, 0);
	}

	dest->type=type;
	dest->start_label = start_label;
	dest->end_label = end_label;

	return dest;
}

int block_load_file(char *filename)
{
	FILE *f;
	char line[MAX_LINE_LEN];

	int r;
	int start,end;
	enum blocktype type;
	enum labeltype start_label, end_label;
	int lineno;
	char *name;

	struct block *blk;

	f=fopen(filename, "r");
	if(f==NULL) {
		msg(0, "Error: Cannot open '%s': %s\n", 
						filename, strerror(errno));
		return -1;
	}

	lineno=1;
	while(fgets(line, MAX_LINE_LEN, f)!=NULL) {
		r=block_load_line(line, &name, &start, &end, &type,
						&start_label, &end_label);
		if(r<0) {
			msg(0, "       at line %d of '%s'\n", lineno, filename);
			fclose(f);
			return -1;
		}

		if(r==1) {
			blk=block_new(name, start, end, type,
						start_label, end_label);
			if(blk==NULL) {
				msg(0, "       at line %d of '%s'\n", lineno, 
								filename);
				fclose(f);
				return -1;
			}
			free(name);
		}

		lineno++;	
	}

	fclose(f);
	return 0;
}

/* TODO: Use split_line() from utils.c */
int block_load_line(char *line, char **name, 
				int *start, int *end, enum blocktype *type,
				enum labeltype *start_label,
				enum labeltype *end_label)
{
	int r;

	char *linec,*tok;

	char *named;
	int startd,endd,typed;
	enum labeltype start_labeld, end_labeld;

	/* First, remove any comments on the line. Everything past a ';' is
	 * ignored */

	if(line[0]==';') {
		return 0;
	} else {
		linec=strtok(line, ";");
	}

	if(linec==NULL) return 0;		/* empty line */

	/* Skip leading white space */

	tok=strtok(linec, " \t\r\n");

	if(tok==NULL) return 0;			/* empty line */

	if(tok[strlen(tok)-1]!=':') {
		msg(0, "Error: ':' expected after block name '%s'\n", tok);
		/* not a label */
		return -1;
	}

	tok[strlen(tok)-1]=0;
	named=tok;

	tok=strtok(NULL, " \t\r\n");
	
	if(tok == NULL) {
		msg(0, "Error: 'unlabeled', 'start' or 'first' expected\n");
		return -1;
	}

	if(!strcasecmp(tok, "start")) {
		start_labeld = LABEL_START_END;
	} else if(!strcasecmp(tok, "first")) {
		start_labeld = LABEL_FIRST_LAST;
	} else if(!strcasecmp(tok, "unlabeled")) {
		start_labeld = LABEL_NONE;

		tok = strtok(NULL, " \t\r\n");
		if(tok == NULL || (strcasecmp(tok, "start") &&
						strcasecmp(tok, "first"))) {
			msg(0, "Error: 'start' or 'first' expected, "
						"got '%s' instead\n", tok);
			return -1;
		}
	} else {
		msg(0, "Error: 'unlabeled', 'start' or 'first' expected\n");
		return -1;
	}

	tok=strtok(NULL, " \t\r\n");

	if(tok==NULL) {
		msg(0, "Error: Start address expected\n");
		return -1;
	}

	r=sscanf(tok, "%i", &startd);
	if(r!=1) {
		msg(0, "Error: Invalid start address '%s'\n", tok);
		return -1;
	} 

	tok=strtok(NULL, " \t\r\n");
	
	if(tok == NULL) {
		msg(0, "Error: 'unlabeled', 'end' or 'last' expected\n");
		return -1;
	}

	int is_last;
	if(!strcasecmp(tok, "end")) {
		end_labeld = LABEL_START_END;
		is_last = 0;
	} else if(!strcasecmp(tok, "last")) {
		end_labeld = LABEL_FIRST_LAST;
		is_last = 1;
	} else if(!strcasecmp(tok, "unlabeled")) {
		end_labeld = LABEL_NONE;

		tok = strtok(NULL, " \t\r\n");
		if(tok != NULL && !strcasecmp(tok, "end")) {
			is_last = 0;
		} else if(tok != NULL && !strcasecmp(tok, "last")) {
			is_last = 1;
		} else {
			msg(0, "Error: 'end' or 'last' expected, "
						"got '%s' instead\n", tok);
			return -1;
		}
	} else {
		msg(0, "Error: 'unlabeled', 'end' or 'last' expected\n");
		return -1;
	}

	tok=strtok(NULL, " \t\r\n");

	if(tok==NULL) {
		msg(0, "Error: End address expected\n");
		return -1;
	}

	r=sscanf(tok, "%i", &endd);
	if(r != 1) {
		msg(0, "Error: Invalid end address '%s'\n", tok);
		return -1;
	}

	if(is_last) {
		endd++;
	}

	if(endd <= startd) {
		msg(0, "Error: End address '%x' before start address '%x'\n",
								endd, startd);
		return -1;
	}

	tok=strtok(NULL, " \t\r\n");
	
	if(tok==NULL || strcasecmp(tok, "type")) {
		msg(0, "Error: 'type' expected\n");
		return -1;
	}

	tok=strtok(NULL, " \t\r\n");

	if(tok==NULL) {
		msg(0, "Error: Block type expected\n");
		return -1;
	}

	if(!strcmp(tok, "code")) {
		typed=code;	
	} else if(!strcmp(tok, "bytedata")) {
		typed=bytedata;
	} else if(!strcmp(tok, "worddata")) {
		typed=worddata;
	} else if(!strcmp(tok, "pointers")) {
		typed=pointers;
	} else {
		msg(0, "Error: Invalid block type '%s'\n", tok);
		return -1;
	}

	*name=strdup(named);
	*start=startd;
	*end=endd;
	*type=typed;
	*start_label = start_labeld;
	*end_label = end_labeld;

	return 1;
}