-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimsegm.c
364 lines (317 loc) · 10.9 KB
/
simsegm.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
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
/****************************************************************************
*
* This code is Public Domain.
*
* ========================================================================
*
* Description: Processing simplified segment directives:
* - .CODE, .DATA, .DATA?, .CONST, .STACK, .FARDATA, .FARDATA?
****************************************************************************/
#include <ctype.h>
#include "globals.h"
#include "memalloc.h"
#include "parser.h"
#include "segment.h"
#include "input.h"
#include "expreval.h"
#include "fastpass.h"
#include "listing.h"
#include "msgtext.h"
#include "tokenize.h"
#include "myassert.h"
#define DEFAULT_STACK_SIZE 1024
#ifdef __I86__
#define NUMQUAL (long)
#else
#define NUMQUAL
#endif
extern const char szDgroup[];
static char *SegmNames[ SIM_LAST ];
static const char * const SegmNamesDef[ SIM_LAST ] = {
"_TEXT", "STACK", "_DATA", "_BSS", "FAR_DATA", "FAR_BSS", "CONST"
};
static const char * const SegmClass[ SIM_LAST ] = {
"CODE", "STACK", "DATA", "BSS", "FAR_DATA", "FAR_BSS", "CONST"
};
static const char * const SegmCombine[ SIM_LAST ] = {
"PUBLIC", "STACK", "PUBLIC", "PUBLIC", "PRIVATE", "PRIVATE", "PUBLIC"
};
char *SimGetSegName( enum sim_seg segno )
/***************************************/
{
return( SegmNames[segno] );
}
const char *GetCodeClass( void )
/******************************/
{
/* option -nc set? */
if ( Options.names[OPTN_CODE_CLASS] )
return( Options.names[OPTN_CODE_CLASS] );
return( SegmClass[SIM_CODE] );
}
/* emit DGROUP GROUP instruction */
static void AddToDgroup( enum sim_seg segm, const char *name )
/************************************************************/
{
/* no DGROUP for FLAT or COFF/ELF */
if( ModuleInfo.model == MODEL_FLAT
#if COFF_SUPPORT
|| Options.output_format == OFORMAT_COFF
#endif
#if ELF_SUPPORT
|| Options.output_format == OFORMAT_ELF
#endif
)
return;
if( name == NULL )
name = SegmNames[segm];
AddLineQueueX( "%s %r %s", szDgroup, T_GROUP, name );
}
/* generate code to close the current segment */
static void close_currseg( void )
/*******************************/
{
if ( CurrSeg ) {
DebugMsg1(("close_currseg: current seg=%s\n", CurrSeg->sym.name));
AddLineQueueX( "%s %r", CurrSeg->sym.name, T_ENDS );
}
}
/* translate a simplified segment directive to
* a standard segment directive line
*/
static void SetSimSeg( enum sim_seg segm, const char *name )
/**********************************************************/
{
char *pAlign = "WORD";
char *pAlignSt = "PARA";
char *pUse = "";
struct asym *sym;
const char *pFmt;
const char *pClass;
if ( ModuleInfo.defOfssize > USE16 ) {
if ( ModuleInfo.model == MODEL_FLAT )
pUse = "FLAT";
else
pUse = "USE32";
if (( ModuleInfo.curr_cpu & P_CPU_MASK ) <= P_386 )
pAlign = "DWORD";
else
pAlign = "PARA";
pAlignSt = pAlign;
}
if ( segm == SIM_CODE )
pClass = GetCodeClass();
else
pClass = SegmClass[segm];
if ( segm == SIM_STACK || segm == SIM_FARDATA || segm == SIM_FARDATA_UN )
pAlign = pAlignSt;
pFmt = "%s %r %s %s %s '%s'";
if ( name == NULL ) {
name = SegmNames[segm];
if ( ModuleInfo.simseg_init & ( 1 << segm ) )
pFmt = "%s %r";
else {
ModuleInfo.simseg_init |= ( 1 << segm );
/* v2.05: if segment exists already, use the current attribs.
* This allows a better mix of full and simplified segment
* directives. Masm behaves differently: the attributes
* of the simplified segment directives have highest priority.
*/
sym = SymSearch( name );
if ( sym && sym->state == SYM_SEG && ((struct dsym *)sym)->e.seginfo->lname_idx != 0 )
pFmt = "%s %r";
}
} else {
sym = SymSearch( name );
/* v2.04: testing for state SYM_SEG isn't enough. The segment
* might have been "defined" by a GROUP directive. Additional
* check for segment's lname index is needed.
*/
//if ( sym && sym->state == SYM_SEG )
if ( sym && sym->state == SYM_SEG && ((struct dsym *)sym)->e.seginfo->lname_idx != 0 )
pFmt = "%s %r";
}
AddLineQueueX( pFmt, name, T_SEGMENT, pAlign, pUse, SegmCombine[segm], pClass );
return;
}
static void EndSimSeg( enum sim_seg segm )
/****************************************/
{
AddLineQueueX( "%s %r", SegmNames[segm], T_ENDS );
return;
}
ret_code SimplifiedSegDir( int i, struct asm_tok tokenarray[] )
/*************************************************************/
/*
Handles simplified segment directives:
.CODE, .STACK, .DATA, .DATA?, .FARDATA, .FARDATA?, .CONST
*/
{
const char *name = NULL;
char init;
int type;
struct expr opndx;
DebugMsg1(("SimplifiedSegDir(%s) enter\n", tokenarray[i].string_ptr ));
LstWrite( LSTTYPE_DIRECTIVE, 0, NULL );
if( ModuleInfo.model == MODEL_NONE ) {
EmitError( MODEL_IS_NOT_DECLARED );
return( ERROR );
}
//type = tokenarray[i].value;
type = GetSflagsSp( tokenarray[i].tokval );
i++; /* get past the directive token */
if( type == SIM_STACK ) {
if ( EvalOperand( &i, tokenarray, Token_Count, &opndx, 0 ) == ERROR )
return( ERROR );
if( opndx.kind == EXPR_EMPTY )
opndx.value = DEFAULT_STACK_SIZE;
else if( opndx.kind != EXPR_CONST ) {
EmitError( CONSTANT_EXPECTED );
return( ERROR );
}
} else {
/* Masm accepts a name argument for .CODE and .FARDATA[?] only.
* JWasm also accepts this for .DATA[?] and .CONST unless
* option -Zne is set.
*/
if( tokenarray[i].token == T_ID &&
( type == SIM_CODE || type == SIM_FARDATA || type == SIM_FARDATA_UN
|| ( Options.strict_masm_compat == FALSE &&
( type == SIM_DATA || type == SIM_DATA_UN || type == SIM_CONST )))) {
name = tokenarray[i].string_ptr;
i++;
}
}
if ( tokenarray[i].token != T_FINAL ) {
EmitErr( SYNTAX_ERROR_EX, tokenarray[i].string_ptr );
return( ERROR );
}
NewLineQueue();
if( type != SIM_STACK )
close_currseg(); /* emit a "xxx ENDS" line to close current seg */
if ( name == NULL )
init = ( ModuleInfo.simseg_init & ( 1 << type ) );
switch( type ) {
case SIM_CODE: /* .code */
SetSimSeg( SIM_CODE, name );
if( ModuleInfo.model == MODEL_TINY ) {
/* v2.05: add the named code segment to DGROUP */
if ( name )
AddToDgroup( SIM_CODE, name );
name = szDgroup;
} else if( ModuleInfo.model == MODEL_FLAT ) {
name = "FLAT";
} else {
if( name == NULL )
name = SegmNames[SIM_CODE];
}
AddLineQueueX( "%r %r:%s", T_ASSUME, T_CS, name );
break;
case SIM_STACK: /* .stack */
/* if code is generated which does "emit" bytes,
* the original source line has to be saved.
* v2.05: must not be done after LstWrite() has been called!
* Also, there are no longer bytes "emitted".
*/
//FStoreLine();
SetSimSeg( SIM_STACK, NULL );
AddLineQueueX( "ORG 0%xh", NUMQUAL opndx.value );
EndSimSeg( SIM_STACK );
/* add stack to dgroup for some segmented models */
if ( !init )
if ( ModuleInfo.distance != STACK_FAR )
AddToDgroup( SIM_STACK, NULL );
break;
case SIM_DATA: /* .data */
case SIM_DATA_UN: /* .data? */
case SIM_CONST: /* .const */
SetSimSeg( type, name );
AddLineQueueX( "%r %r:ERROR", T_ASSUME, T_CS );
if ( name || (!init) )
AddToDgroup( type, name );
break;
case SIM_FARDATA: /* .fardata */
case SIM_FARDATA_UN: /* .fardata? */
SetSimSeg( type, name );
AddLineQueueX( "%r %r:ERROR", T_ASSUME, T_CS );
break;
default: /* shouldn't happen */
/**/myassert( 0 );
break;
}
RunLineQueue();
DebugMsg1(("SimplifiedSegDir exit\n"));
return( NOT_ERROR );
}
/*
* Set default values for .CODE and .DATA segment names.
* Called by ModelDirective(), at Pass 1 only.
*/
void SetModelDefaultSegNames( void )
/**********************************/
{
/* init segment names with default values */
memcpy( SegmNames, SegmNamesDef, sizeof(SegmNames) );
/* option -nt set? */
if( Options.names[OPTN_TEXT_SEG] ) {
SegmNames[SIM_CODE] = LclAlloc( strlen( Options.names[OPTN_TEXT_SEG] ) + 1 );
strcpy( SegmNames[SIM_CODE], Options.names[OPTN_TEXT_SEG] );
} else {
if ( SIZE_CODEPTR & ( 1 << ModuleInfo.model ) ) {
/* for some models, the code segment contains the module name */
SegmNames[SIM_CODE] = LclAlloc( strlen( SegmNamesDef[SIM_CODE] ) + strlen( ModuleInfo.name ) + 1 );
strcpy( SegmNames[SIM_CODE], ModuleInfo.name );
strcat( SegmNames[SIM_CODE], SegmNamesDef[SIM_CODE] );
}
}
/* option -nd set? */
if ( Options.names[OPTN_DATA_SEG] ) {
SegmNames[SIM_DATA] = LclAlloc( strlen( Options.names[OPTN_DATA_SEG] ) + 1 );
strcpy( SegmNames[SIM_DATA], Options.names[OPTN_DATA_SEG] );
}
return;
}
/* Called by SetModel() [.MODEL directive].
* Initializes simplified segment directives.
* NewLineQueue() has already been called,
* and the caller will run RunLineQueue() later.
* Called for each pass.
*/
ret_code ModelSimSegmInit( int model )
/************************************/
{
char buffer[20];
if ( Parse_Pass == PASS_1 ) {
DebugMsg1(("ModelSimSegmInit() enter, pass one\n" ));
/* create default code segment (_TEXT) */
SetSimSeg( SIM_CODE, NULL );
EndSimSeg( SIM_CODE );
/* create default data segment (_DATA) */
SetSimSeg( SIM_DATA, NULL ) ;
EndSimSeg( SIM_DATA );
/* create DGROUP for BIN/OMF if model isn't FLAT */
if( model != MODEL_FLAT &&
( Options.output_format == OFORMAT_OMF ||
Options.output_format == OFORMAT_BIN )) {
strcpy( buffer, "%s %r %s" );
if( model == MODEL_TINY ) {
strcat( buffer, ", %s" );
AddLineQueueX( buffer, szDgroup, T_GROUP, SegmNames[SIM_CODE], SegmNames[SIM_DATA] );
} else
AddLineQueueX( buffer, szDgroup, T_GROUP, SegmNames[SIM_DATA] );
}
DebugMsg1(("ModelSimSegmInit() exit\n" ));
}
return( NOT_ERROR );
}
/* called when END has been found */
void ModelSimSegmExit( void )
/***************************/
{
/* a model is set. Close current segment if one is open. */
if ( CurrSeg ) {
NewLineQueue();
close_currseg();
RunLineQueue();
}
}