-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathidentifiers.c
More file actions
210 lines (187 loc) · 5.01 KB
/
identifiers.c
File metadata and controls
210 lines (187 loc) · 5.01 KB
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
/**
** "i8086" An assembler for the 16-bit Intel x86 CPUs
**
** Copyright (C) 2024 Jeff Penfold (jeff.penfold@googlemail.com)
**
** 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 3 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, see <https://www.gnu.org/licenses/>.
**/
/*
* identifiers
* ===========
*
* System for classification and storage of labels with values.
*/
#include "os.h"
#include "includes.h"
/*
* Sabed labels are all located below this variable.
*/
static id_record *saved_labels = NIL( id_record );
/*
* The 'Uniqueness' number that tracks labels as they are defined
* allowing the creation of "localised" labels.
*/
static int uniqueness = 0;
/*
* Reset identifier system to "top of input" for the start
* of a new pass.
*/
void restart_identifiers( void ) {
uniqueness = 0;
}
/*
* UNIQUENESS_SIZE is a possible cause for inexplicable crashes
* in the assembler, if the value of uniqueness exceeds a 16 bit
* unsigned value.
*/
#define UNIQUENESS_SIZE 6
#define UNIQUENESS_PREFIX "L%04X_"
#define MAXIMUM_UNIQUENESS MAX_UWORD
/*
* Save/Find a label record.
*/
id_record *find_label( char *label, boolean definition ) {
id_record **adrs,
*look;
ASSERT( label != NIL( char ));
/*
* If the label starts with a PERIOD then this is
* a locally referenced label.
*/
if( *label == PERIOD ) {
char *temp;
int len;
/*
* If there is a period at the start then we modify
* the label to include the 'uniqueness' of the
* last normal label defined.
*/
len = strlen( label ) + UNIQUENESS_SIZE + 1;
temp = STACK_ARRAY( char, len );
sprintf( temp, UNIQUENESS_PREFIX "%s", uniqueness, label+1 );
if( BOOL( command_flags & more_verbose )) printf( "Localise %s -> %s\n", label, temp );
label = temp;
}
else if( definition ) {
/*
* We are defining a label, we need to establish
* a uniqueness tag for this symbol to be applied
* to locally referential label used after it.
*/
if( uniqueness < MAXIMUM_UNIQUENESS ) {
uniqueness++;
}
else {
log_error( "Uniqueness counter exceeds maximum" );
}
}
/*
* Find/Insert the label into the list of all
* known labels.
*/
adrs = &( saved_labels );
if( BOOL( command_flags & ignore_label_case )) {
while(( look = *adrs )) {
if( strcasecmp( look->id, label ) == 0 ) {
return( look );
}
adrs = &( look->next );
}
}
else {
while(( look = *adrs )) {
if( strcmp( look->id, label ) == 0 ) {
return( look );
}
adrs = &( look->next );
}
}
look = NEW( id_record );
look->id = strdup( label );
look->type = class_unknown;
look->next = NIL( id_record );
*adrs = look;
return( look );
}
/*
* Dump the content of a constant value record, used
* as part of the verbose/debug output.
*/
void dump_value( constant_value *v ) {
char scope[ BUFFER_FOR_SCOPE ];
ASSERT( v != NIL( constant_value ));
if( v->segment ) printf( " %s:", v->segment->name );
scope[ convert_scope_to_text( FALSE, v->scope, scope, BUFFER_FOR_SCOPE-1 )] = EOS;
printf( " %d($%04x)%s", (int)v->value, (unsigned int)v->value, scope );
}
/*
* To support verbose-ness and debugging this routine can be called
* to produce a dump of the label held by the assembler at this point
* in time.
*/
void dump_labels( void ) {
static char *segment_names[ SEGMENT_REGISTERS ] = { "CS", "DS", "SS", "ES" };
id_record *look;
printf( "Symbols:\n" );
for( look = saved_labels; look; look = look->next ) {
printf( "\t%s: ", look->id );
switch( look->type ) {
case class_unknown: {
printf( "Undefined.\n" );
break;
}
case class_label: {
printf( "label:" );
dump_value( &( look->var.value ));
printf( ".\n" );
break;
}
case class_const: {
printf( "const:" );
dump_value( &( look->var.value ));
printf( ".\n" );
break;
}
case class_group: {
segment_record *seg;
ASSERT( look->var.group != NIL( segment_group ));
printf( "group:" );
for( seg = look->var.group->segments; seg; seg = seg->next ) printf( " %s", seg->name );
printf( ".\n" );
break;
}
case class_segment: {
segment_record *seg;
ASSERT( look->var.segment != NIL( segment_record ));
seg = look->var.segment;
printf( "segment: " );
if( seg->seg_reg < SEGMENT_REGISTERS ) {
printf( "%s:", segment_names[ seg->seg_reg ]);
}
else {
printf( "%d:", seg->seg_reg );
}
printf( " Start $%04x, Size %d.\n", (unsigned int)seg->start, (int)seg->size );
break;
}
default: {
ABORT( "Programmer Error!" );
break;
}
}
}
}
/*
* EOF
*/