-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.c
279 lines (242 loc) · 7.82 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
/****************************************************************************
*
* 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"
enum {
CONT_ASSUMES = 0x01,
CONT_RADIX = 0x02,
CONT_LISTING = 0x04,
CONT_CPU = 0x08,
CONT_ALIGNMENT = 0x10, /* new for v2.0, specific for JWasm */
};
static char *contextnames[] = {
"ASSUMES", "RADIX", "LISTING", "CPU", "ALIGNMENT", NULL };
#if AMD64_SUPPORT
#define NUM_STDREGS 16
#else
#define NUM_STDREGS 8
#endif
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 */
};
struct context {
struct context *next;
uint_8 flags;
struct radix_context rc;
struct alignment_context alc;
struct listing_context lc;
struct cpu_context cc;
struct assumes_context ac; /* must be last member */
};
extern struct asym *sym_Cpu;
static struct context *ContextStack;
#if FASTPASS
static int saved_numcontexts;
static struct context *saved_contexts;
#endif
static int GetContextSize( uint_8 flags )
/***************************************/
{
if ( flags & CONT_ASSUMES )
return( sizeof( struct context ) );
/* spare the large assumes context space if not needed */
return( sizeof( struct context ) - sizeof( struct assumes_context ) );
}
ret_code ContextDirective( int i, struct asm_tok tokenarray[] )
/*************************************************************/
{
int type;
int start = i;
int directive = tokenarray[i].tokval;
uint_8 flags = 0;
uint_8 all;
struct context *pcontext;
all = CONT_ASSUMES | CONT_RADIX | CONT_LISTING | CONT_CPU;
if ( Options.strict_masm_compat == FALSE )
all |= CONT_ALIGNMENT;
DebugMsg(( "%s directive enter\n", tokenarray[i].string_ptr ));
i++;
while ( tokenarray[i].token == T_ID ) {
char **p;
for ( p = contextnames, type = 0; *p ; p++, type++ ) {
if ( _stricmp(*p, tokenarray[i].string_ptr ) == 0 ) {
flags |= ( 1 << type );
break;
}
}
if ( *p == NULL )
if ( _stricmp( "ALL", tokenarray[i].string_ptr ) == 0 ) {
flags |= all;
} else {
break;
}
/* reject ALIGNMENT if strict masm compat is on */
if ( Options.strict_masm_compat && ( flags & CONT_ALIGNMENT ) )
break;
i++;
if ( tokenarray[i].token == T_COMMA && tokenarray[i+1].token != T_FINAL )
i++;
}
if ( tokenarray[i].token != T_FINAL || flags == 0 ) {
EmitErr( SYNTAX_ERROR_EX, tokenarray[i].string_ptr );
return( ERROR );
}
switch ( directive ) {
case T_POPCONTEXT:
DebugMsg(( "POPCONTEXT flags=%X\n", flags ));
/* for POPCONTEXT, check if the items are pushed */
pcontext = ContextStack;
if ( pcontext == NULL || ( pcontext->flags & flags ) != flags ) {
EmitErr( UNMATCHED_BLOCK_NESTING, tokenarray[start].string_ptr );
return( ERROR );
}
ContextStack = pcontext->next;
/* restore the values */
if ( flags & CONT_ASSUMES ) {
SetSegAssumeTable( pcontext->ac.SegAssumeTable );
SetStdAssumeTable( pcontext->ac.StdAssumeTable, pcontext->ac.type_content );
}
if ( flags & CONT_RADIX ) {
ModuleInfo.radix = pcontext->rc.radix;
}
if ( flags & CONT_ALIGNMENT ) {
ModuleInfo.fieldalign = pcontext->alc.fieldalign;
ModuleInfo.procalign = pcontext->alc.procalign;
}
if ( flags & CONT_LISTING ) {
ModuleInfo.list_macro = pcontext->lc.list_macro;
ModuleInfo.list = pcontext->lc.list;
ModuleInfo.cref = pcontext->lc.cref;
ModuleInfo.listif = pcontext->lc.listif;
ModuleInfo.list_generated_code = pcontext->lc.list_generated_code;
}
if ( flags & CONT_CPU ) {
ModuleInfo.cpu = pcontext->cc.cpu;
if ( sym_Cpu )
sym_Cpu->value = pcontext->cc.cpu;
ModuleInfo.curr_cpu = pcontext->cc.curr_cpu;
}
LclFree( pcontext );
break;
case T_PUSHCONTEXT:
DebugMsg(( "PUSHCONTEXT flags=%X\n", flags ));
/* setup a context item */
pcontext = LclAlloc( GetContextSize( flags) );
pcontext->flags = flags;
if ( flags & CONT_ASSUMES ) {
GetSegAssumeTable( pcontext->ac.SegAssumeTable );
GetStdAssumeTable( pcontext->ac.StdAssumeTable, pcontext->ac.type_content );
}
if ( flags & CONT_RADIX ) {
pcontext->rc.radix = ModuleInfo.radix;
}
if ( flags & CONT_ALIGNMENT ) {
pcontext->alc.fieldalign = ModuleInfo.fieldalign;
pcontext->alc.procalign = ModuleInfo.procalign;
}
if ( flags & CONT_LISTING ) {
pcontext->lc.list_macro = ModuleInfo.list_macro;
pcontext->lc.list = ModuleInfo.list;
pcontext->lc.cref = ModuleInfo.cref;
pcontext->lc.listif = ModuleInfo.listif;
pcontext->lc.list_generated_code = ModuleInfo.list_generated_code;
}
if ( flags & CONT_CPU ) {
pcontext->cc.cpu = ModuleInfo.cpu;
pcontext->cc.curr_cpu = ModuleInfo.curr_cpu;
}
pcontext->next = ContextStack;
ContextStack = pcontext;
break;
}
return( NOT_ERROR );
}
#if FASTPASS
/* save current context status */
void ContextSaveState( void )
/***************************/
{
int i;
uint_32 size = 0;
struct context *p;
struct context *p2;
for ( i = 0, p=ContextStack ; p ; i++, p = p->next )
size += GetContextSize( p->flags );
saved_numcontexts = i;
if ( i ) {
saved_contexts = LclAlloc( size );
for ( p = ContextStack, p2 = saved_contexts ; p ; p = p->next ) {
memcpy( p2, p, GetContextSize( p->flags ) );
p2 = (struct context *)((char *)p2 + GetContextSize ( p->flags ) );
}
}
}
/* restore context status */
static void ContextRestoreState( void )
/*************************************/
{
int i;
int size;
struct context *p;
struct context *p2;
struct context *p3 = NULL;
for ( i = saved_numcontexts, p2 = saved_contexts; i ; i-- ) {
size = GetContextSize( p2->flags );
p = LclAlloc( size );
if ( p3 == NULL )
ContextStack = p;
else
p3->next = p;
p3 = p;
memcpy( p, p2, size );
p->next = NULL;
p2 = (struct context *)((char *)p2 + size );
}
}
#endif
/* init context, called once per pass */
void ContextInit( int pass )
/**************************/
{
ContextStack = NULL;
#if FASTPASS
if ( pass == PASS_1 )
saved_numcontexts = 0;
else {
ContextRestoreState();
}
#endif
}