File: label.c

package info (click to toggle)
crasm 1.8-1
  • links: PTS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, stretch
  • size: 376 kB
  • sloc: ansic: 3,505; makefile: 65
file content (261 lines) | stat: -rwxr-xr-x 5,400 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
/*  Copyright (C) 1987- Leon Bottou
 * 
 *  This is free documentation; 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.
 * 
 *  The GNU General Public License's references to "object code"
 *  and "executables" are to be interpreted as the output of any
 *  document formatting or typesetting system, including
 *  intermediate and printed output.
 * 
 *  This manual 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 manual. Otherwise check the web site
 *  of the Free Software Foundation at http://www.fsf.org.
 */
/*
	CRASM: label.c
	utilisation des labels
	LYB 9/87
*/

#include "dcl.h"
#include <ctype.h>
#include "label.h"
#define LABELARRAYSIZE 100



struct labelarray
{	int position;
	struct label label[LABELARRAYSIZE];
} *labelarray_inst = NULL;

struct label *lroot = NULL;

/* checklabel(name)
   Retourne TRUE si name est une chaine acceptable pour un label
*/

int
checklabel(char *name)
{
	register char c;
	if ( strlen(name)>LABLEN )
		return FALSE;
	c=*name;
	if ( c=='*' && name[1]==0 )
		return TRUE;
	if ( !isalpha(c) && c!='_' && c!='.' )
		return FALSE;
	while (( c=*++name )!=0 )
		if ( !isalnum(c) && c!='_'  && c!='.' )
			return FALSE;
	return TRUE;
}



/* searchlabel(name)
   Retourne NULL ou l'adresse du label 'name'
   stocke dans  addrpos  l'adresse du pointeur vers ce label 
*/

static struct label **addrpos;
static 	char localname[LABLEN+2];

struct label *
searchlabel(char *name)
{
	register struct label *q;
	register int r;
	register char *s;
	extern int segment;
	
	s=localname;
	
	if ( name[0]=='.' )
	{	sprintf(s,"%d",segment);
		while (*s)
			s++;
	};
	
	while (s-localname < LABLEN)
		if (*name)
			*s++= toupper(*name++);
		else
			*s++= 0;
	
	s=localname;
	
	q = lroot;
	addrpos = &lroot;
	while ( q!=NULL && (r=strncmp(s,q->name,LABLEN))!=0 )
		if (r>0)
			q=* (addrpos= &(q->right)) ;
		else
			q=* (addrpos= &(q->left )) ;

	return q;
}



/* makelabel(name,flags,type)
   Cree et place le label ci dessus.
*/

struct label *
makelabel(char *name, unsigned char flags, unsigned char type)
{
	register struct label *q;
	
	q=searchlabel(name);
	if ( q != NULL )
		fatal("XX: makelabel");
	
	if (labelarray_inst==NULL)
	{	if ((labelarray_inst=malloc(sizeof *labelarray_inst)))
			memset(labelarray_inst, 0, sizeof(*labelarray_inst));
		else
			fatal("no memory");
	}
	q= &( labelarray_inst->label[ labelarray_inst->position++ ] ); 
	if ( labelarray_inst->position >= LABELARRAYSIZE )
		labelarray_inst=NULL;
	
	q->left=q->right=NULL;

	*addrpos=q;
	
	strncpy(q->name,localname,LABLEN);
	q->type  = type;
	q->flags = flags;
	
	return q;
}

/* findlabel(name)
   Renvoie un pointeur sur le label 'name'. Si necessaire, cree un
   label UNDEF|FORWARD type  L_ADDR: seule reference en avant autorisee.
*/

struct label *
findlabel(char *name)
{
	register struct label *q;
	
	q=searchlabel(name);
	if ( q==NULL )
		q=makelabel(name, UNDEF|FORWARD|NOREDEF,
				asmflags&F_RELATIF ? L_RELATIVE : L_ABSOLUTE );
	q->flags |= USED;
	return q;
}


/* deflabel(name,flags,type,value)
   Definit un label name, de type precise, pour une valeur val1.
   Si ce label existe deja, il doit etre redefini du meme type, et si
   ce type est L_ADDR, il doit etre indefini.
*/

struct label *
deflabel(char *name, unsigned char flags, unsigned char type, int value)
{
	register struct label *q;
	
	q=searchlabel(name);
	if (q==NULL)
		q = makelabel(name,UNDEF,type);
		
	
	if ( !(q->flags & UNDEF) )
		if ( (q->flags & NOREDEF) || (flags & NOREDEF) )
		{	if ( q->type == L_ABSOLUTE || q->type == L_RELATIVE )
				error("illegal label redefinition");
			else
				error("attempt to redefine a keyword");
		}
	
	q->flags &= USED | NOREDEF | FORWARD;
	flags    &= ~USED;
	
	q->type  = type;
	q->flags |= flags;
	q->ptr = NULL;
	q->value = value;
	return q;
}


/* init_label_list
   Ordonne en arbre un tableau de labels
*/

void
init_label_list(struct label *array)
{
	register int i,count,incr;
	register struct label *q;
	
	q=array;
	count=0;
	while ( q++->left != (void*)-1 )
		count++;
		
	incr=1;
	array--;
	while (incr<=count)
		incr <<= 1;
	while (incr>1)
	{	i=incr>>1;
		while (i<=count)
		{	q = array+i;
			if ( searchlabel(q->name) != NULL )
			{	if ( q->flags & UNDEF )
					q->flags &= ~UNDEF;
				else
					warning(
					"user's symbol overrides new keyword");
			}
			else
			{	*addrpos = q;
				strncpy(q->name,localname,LABLEN);
			};
			i += incr;
		}
		incr >>= 1;
	}
}

/* undeflabels(lroot)
   UNDEF tous les labels de type positif, sauf les FORWARD
*/

void
undeflabels(struct label *q)
{
	extern struct label pseudos[];
	
	if (q)
	{	if ( !(q->flags & NOREDEF) || (q->flags & UNDEF) )
			q->flags &= ~FORWARD;

	/* FORWARD ne subsiste que si on a 
	   - un label non redefinissable,
	   - ce label est effectivement defini en avant quelque-part
	*/
		q->flags |= UNDEF;			
		undeflabels(q->left);
		undeflabels(q->right);
	}
	for ( q=pseudos; q->left != (struct label*)-1; q++ )
		q->flags &= ~UNDEF;
}