-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.c
325 lines (279 loc) · 9.92 KB
/
context.c
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
/****************************************************************************
*
* This code is Public Domain.
*
* ========================================================================
*
* Description: Processing of PUSHCONTEXT and POPCONTEXT directives.
*
****************************************************************************/
#include <ctype.h>
#include "globals.h"
#include "memalloc.h"
#include "parser.h"
#include "assume.h"
#include "expreval.h"
#include "fastpass.h"
#include "listing.h"
/* v2.10: static variables moved to ModuleInfo */
#define ContextStack ModuleInfo.g.ContextStack
#define ContextFree ModuleInfo.g.ContextFree
#if FASTPASS
#define cntSavedContexts ModuleInfo.g.cntSavedContexts
#define SavedContexts ModuleInfo.g.SavedContexts
#endif
enum context_type {
CONT_ASSUMES = 0x01,
CONT_RADIX = 0x02,
CONT_LISTING = 0x04,
CONT_CPU = 0x08,
CONT_ALIGNMENT = 0x10, /* new for v2.0, specific for JWasm */
CONT_ALL = CONT_ASSUMES | CONT_RADIX | CONT_LISTING | CONT_CPU,
};
static const enum context_type typetab[] = {
CONT_ASSUMES, CONT_RADIX, CONT_LISTING, CONT_CPU, CONT_ALIGNMENT, CONT_ALL
};
static const char * const contextnames[] = {
"ASSUMES", "RADIX", "LISTING", "CPU", "ALIGNMENT", "ALL"
};
#if AMD64_SUPPORT
#define NUM_STDREGS 16
#else
#define NUM_STDREGS 8
#endif
/* Masm has a max context nesting level of 10.
* JWasm has no restriction currently.
*/
struct assumes_context {
struct assume_info SegAssumeTable[NUM_SEGREGS];
struct assume_info StdAssumeTable[NUM_STDREGS];
struct stdassume_typeinfo type_content[NUM_STDREGS];
};
struct listing_context {
enum listmacro list_macro;
unsigned char list:1;
unsigned char cref:1;
unsigned char listif:1;
unsigned char list_generated_code:1;
};
struct cpu_context {
short cpu; /* saved ModuleInfo.cpu */
enum cpu_info curr_cpu; /* saved ModuleInfo.curr_cpu */
};
struct radix_context {
uint_8 radix; /* saved ModuleInfo.radix */
};
struct alignment_context {
uint_8 fieldalign; /* saved ModuleInfo.fieldalign */
uint_8 procalign; /* saved ModuleInfo.procalign */
};
/* v2.10: the type-specific data is now declared as a union;
* and PUSH|POPCONTEXT ALL will push/pop 4 single items.
* all items are equal in size, this made it possible to implement
* a "free items" heap.
*/
struct context {
struct context *next;
enum context_type type;
union {
struct radix_context rc;
struct alignment_context alc;
struct listing_context lc;
struct cpu_context cc;
struct assumes_context ac;
};
};
extern struct asym *sym_Cpu;
/* v2.10: major rewrite of this function */
ret_code ContextDirective( int i, struct asm_tok tokenarray[] )
/*************************************************************/
{
int start = i;
int directive = tokenarray[i].tokval;
enum context_type type;
int j;
struct context *curr;
DebugMsg(( "%s directive enter\n", tokenarray[i].string_ptr ));
i++; /* skip CONTEXT keyword */
while ( tokenarray[i].token == T_ID ) {
for ( j = 0, type = -1; j < ( sizeof(typetab) / sizeof(typetab[0]) ); j++ ) {
if ( _stricmp( contextnames[j], tokenarray[i].string_ptr ) == 0 ) {
type = typetab[j];
break;
}
}
if ( type == -1 )
break;
/* reject ALIGNMENT if strict masm compat is on */
if ( Options.strict_masm_compat ) {
if ( type == CONT_ALIGNMENT )
break;
else
type &= ~CONT_ALIGNMENT; /* in case ALIGNMENT is again included in ALL */
}
if ( directive == T_POPCONTEXT ) {
struct context *prev;
struct context *next;
DebugMsg(( "POPCONTEXT type=%X\n", type ));
/* for POPCONTEXT, check if appropriate items are on the stack */
for ( prev = NULL, curr = ContextStack; curr && type; curr = next ) {
DebugMsg(( "POPCONTEXT: found item with type=%X\n", curr->type ));
next = curr->next;
/* matching item on the stack? */
if ( !( curr->type & type ) ) {
prev = curr;
continue;
}
type &= ~curr->type;
if ( prev )
prev->next = next;
else
ContextStack = next;
curr->next = ContextFree;
ContextFree = curr;
/* restore the values */
switch ( curr->type ) {
case CONT_ASSUMES:
SetSegAssumeTable( curr->ac.SegAssumeTable );
SetStdAssumeTable( curr->ac.StdAssumeTable, curr->ac.type_content );
break;
case CONT_RADIX:
ModuleInfo.radix = curr->rc.radix;
break;
case CONT_ALIGNMENT:
ModuleInfo.fieldalign = curr->alc.fieldalign;
ModuleInfo.procalign = curr->alc.procalign;
break;
case CONT_LISTING:
ModuleInfo.list_macro = curr->lc.list_macro;
ModuleInfo.list = curr->lc.list;
ModuleInfo.cref = curr->lc.cref;
ModuleInfo.listif = curr->lc.listif;
ModuleInfo.list_generated_code = curr->lc.list_generated_code;
break;
case CONT_CPU:
ModuleInfo.cpu = curr->cc.cpu;
if ( sym_Cpu )
sym_Cpu->value = curr->cc.cpu;
ModuleInfo.curr_cpu = curr->cc.curr_cpu;
}
}
if ( type ) {
DebugMsg(( "POPCONTEXT error, remaining type flags=%X\n", type ));
return( EmitErr( UNMATCHED_BLOCK_NESTING, tokenarray[start].tokpos ) );
}
} else {
DebugMsg(( "PUSHCONTEXT type=%X\n", type ));
for ( j = 0; j < ( sizeof(typetab) / sizeof(typetab[0] ) ) && type; j++ ) {
if ( type & typetab[j] ) {
type &= ~typetab[j];
if ( ContextFree ) {
curr = ContextFree;
ContextFree = curr->next;
} else
curr = LclAlloc( sizeof( struct context ) );
curr->type = typetab[j];
curr->next = ContextStack;
ContextStack = curr;
switch ( typetab[j] ) {
case CONT_ASSUMES:
GetSegAssumeTable( curr->ac.SegAssumeTable );
GetStdAssumeTable( curr->ac.StdAssumeTable, curr->ac.type_content );
break;
case CONT_RADIX:
curr->rc.radix = ModuleInfo.radix;
break;
case CONT_ALIGNMENT:
curr->alc.fieldalign = ModuleInfo.fieldalign;
curr->alc.procalign = ModuleInfo.procalign;
break;
case CONT_LISTING:
curr->lc.list_macro = ModuleInfo.list_macro;
curr->lc.list = ModuleInfo.list;
curr->lc.cref = ModuleInfo.cref;
curr->lc.listif = ModuleInfo.listif;
curr->lc.list_generated_code = ModuleInfo.list_generated_code;
break;
case CONT_CPU:
curr->cc.cpu = ModuleInfo.cpu;
curr->cc.curr_cpu = ModuleInfo.curr_cpu;
break;
}
}
}
}
i++;
if ( tokenarray[i].token == T_COMMA && tokenarray[i+1].token != T_FINAL )
i++;
}
if ( tokenarray[i].token != T_FINAL || type == -1 ) {
return( EmitErr( SYNTAX_ERROR_EX, tokenarray[i].tokpos ) );
}
return( NOT_ERROR );
}
#if FASTPASS
/* save current context status */
void ContextSaveState( void )
/***************************/
{
int i;
struct context *src;
struct context *dst;
for ( i = 0, src = ContextStack ; src ; i++, src = src->next );
if ( i ) {
cntSavedContexts = i;
SavedContexts = LclAlloc( i * sizeof( struct context ) );
DebugMsg(( "ContextSaveState: SavedContexts=%X\n", SavedContexts ));
for ( src = ContextStack, dst = SavedContexts ; src ; src = src->next, dst++ ) {
memcpy( dst, src, sizeof( struct context ) );
}
}
}
/* restore context status */
static void ContextRestoreState( void )
/*************************************/
{
int i;
struct context *dst;
for ( i = cntSavedContexts ; i ; i-- ) {
if ( ContextFree ) {
dst = ContextFree;
ContextFree = dst->next;
} else
dst = LclAlloc( sizeof( struct context ) );
memcpy( dst, &SavedContexts[i-1], sizeof( struct context ) );
dst->next = ContextStack;
ContextStack = dst;
}
}
#endif
/* init context, called once per pass */
void ContextInit( int pass )
/**************************/
{
/* if ContextStack isn't NULL, then at least one PUSHCONTEXT
* didn't have a matching POPCONTEXT. No need to reset it to NULL -
* but might be ok to move the items to the ContextFree heap.
*/
//ContextStack = NULL;
#if FASTPASS
if ( pass > PASS_1 ) {
ContextRestoreState();
}
#endif
}
#if FASTMEM==0
void ContextFini( void )
/**********************/
{
struct context *curr;
struct context *next;
/* release the items in the ContextFree heap.
* there might also be some left in ContextStack...
*/
for ( curr = ContextFree; curr; curr = next ) {
next = curr->next;
LclFree( curr );
}
}
#endif