-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassemble.c
1630 lines (1461 loc) · 50 KB
/
assemble.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************************************
*
* This code is Public Domain.
*
* ========================================================================
*
* Description: assemble a module.
*
****************************************************************************/
#include <ctype.h>
#include <time.h>
#include "globals.h"
#include "memalloc.h"
#include "parser.h"
#include "reswords.h"
#include "input.h"
#include "tokenize.h"
#include "condasm.h"
#include "segment.h"
#include "assume.h"
#include "proc.h"
#include "expreval.h"
#include "hll.h"
#include "context.h"
#include "types.h"
#include "labels.h"
#include "macro.h"
#include "extern.h"
#include "fixup.h"
#include "omf.h"
#include "fastpass.h"
#include "listing.h"
#include "msgtext.h"
#include "myassert.h"
#include "linnum.h"
#include "cpumodel.h"
#if DLLIMPORT
#include "mangle.h"
#endif
#if COFF_SUPPORT
#include "coff.h"
#endif
#if ELF_SUPPORT
#include "elf.h"
#endif
#if BIN_SUPPORT
#include "bin.h"
#endif
#if 1 //def __SW_BD
#include <setjmp.h>
jmp_buf jmpenv;
#endif
#ifdef __SW_BD
#define EXPQUAL __stdcall
#else
#define EXPQUAL
#endif
#define USELSLINE 1 /* must match switch in listing.c! */
extern void ProcCheckOpen( void );
extern void SortSegments( void );
extern uint_32 LastCodeBufSize;
extern char *DefaultDir[NUM_FILE_TYPES];
extern const char *ModelToken[];
#if FASTMEM==0
extern void FreeLibQueue();
#endif
#ifdef DEBUG_OUT
extern int lq_line;
#endif
/* fields: next, name, segment, offset/value */
struct asym WordSize = { NULL,"@WordSize", 0 };
/* names for output formats. order must match enum oformat */
static const struct format_options formatoptions[] = {
{ NULL, BIN_DISALLOWED, "BIN" },
{ NULL, OMF_DISALLOWED, "OMF" },
#if COFF_SUPPORT
{ NULL, COFF32_DISALLOWED, "COFF" },
#endif
#if ELF_SUPPORT
{ elf_init, ELF32_DISALLOWED, "ELF" },
#endif
};
#if AMD64_SUPPORT
#if COFF_SUPPORT
const struct format_options coff64_fmtopt = { NULL, COFF64_DISALLOWED, "PE32+" };
#endif
#if ELF_SUPPORT
const struct format_options elf64_fmtopt = { elf_init, ELF64_DISALLOWED, "ELF64" };
#endif
#endif
struct module_info ModuleInfo;
unsigned int Parse_Pass; /* assembly pass */
unsigned int GeneratedCode;
struct qdesc LinnumQueue; /* queue of line_num_info items */
bool write_to_file; /* write object module */
/* Write a byte to the segment buffer.
* in OMF, the segment buffer is flushed when the max. record size is reached.
*/
void OutputByte( unsigned char byte )
/***********************************/
{
if( write_to_file == TRUE ) {
uint_32 idx = CurrSeg->e.seginfo->current_loc - CurrSeg->e.seginfo->start_loc;
#ifdef DEBUG_OUT
if ( CurrSeg->e.seginfo->current_loc < CurrSeg->e.seginfo->start_loc ) {
//_asm int 3;
}
#endif
myassert( CurrSeg->e.seginfo->current_loc >= CurrSeg->e.seginfo->start_loc );
if( Options.output_format == OFORMAT_OMF && idx >= MAX_LEDATA_THRESHOLD ) {
omf_FlushCurrSeg();
idx = CurrSeg->e.seginfo->current_loc - CurrSeg->e.seginfo->start_loc;
}
//DebugMsg(("OutputByte: buff=%p, idx=%" FX32 ", byte=%X, codebuff[0]=%X\n", CurrSeg->e.seginfo->CodeBuffer, idx, byte, *CurrSeg->e.seginfo->CodeBuffer ));
CurrSeg->e.seginfo->CodeBuffer[idx] = byte;
}
#if 1
/* check this in pass 1 only */
else if( CurrSeg->e.seginfo->current_loc < CurrSeg->e.seginfo->start_loc ) {
DebugMsg(("OutputByte: segment start loc changed from %" FX32 "h to %" FX32 "h\n",
CurrSeg->e.seginfo->start_loc,
CurrSeg->e.seginfo->current_loc));
CurrSeg->e.seginfo->start_loc = CurrSeg->e.seginfo->current_loc;
}
#endif
CurrSeg->e.seginfo->current_loc++;
CurrSeg->e.seginfo->bytes_written++;
CurrSeg->e.seginfo->written = TRUE;
if( CurrSeg->e.seginfo->current_loc > CurrSeg->sym.max_offset )
CurrSeg->sym.max_offset = CurrSeg->e.seginfo->current_loc;
}
#if 0 /* v2.03: OutputCodeByte is obsolete */
void OutputCodeByte( unsigned char byte )
/***************************************/
{
// if ( ModuleInfo.CommentDataInCode )
// omf_OutSelect( FALSE );
OutputByte( byte );
}
#endif
void FillDataBytes( unsigned char byte, int len )
/***********************************************/
{
if ( ModuleInfo.CommentDataInCode )
omf_OutSelect( TRUE );
for( ; len; len-- )
OutputByte( byte );
}
/*
* this function is to output (small, <= 8) amounts of bytes which must
* not be separated ( for omf, because of fixups )
*/
void OutputBytes( const unsigned char *pbytes, int len, struct fixup *fixup )
/***************************************************************************/
{
if( write_to_file == TRUE ) {
uint_32 idx = CurrSeg->e.seginfo->current_loc - CurrSeg->e.seginfo->start_loc;
#if 0 /* def DEBUG_OUT */
if ( CurrSeg->e.seginfo->current_loc < CurrSeg->e.seginfo->start_loc )
_asm int 3;
#endif
myassert( CurrSeg->e.seginfo->current_loc >= CurrSeg->e.seginfo->start_loc );
if( Options.output_format == OFORMAT_OMF && ((idx + len) > MAX_LEDATA_THRESHOLD ) ) {
omf_FlushCurrSeg();
idx = CurrSeg->e.seginfo->current_loc - CurrSeg->e.seginfo->start_loc;
}
if ( fixup )
store_fixup( fixup, (int_32 *)pbytes );
//DebugMsg(("OutputBytes: buff=%p, idx=%" FX32 ", byte=%X\n", CurrSeg->e.seginfo->CodeBuffer, idx, *pbytes ));
memcpy( &CurrSeg->e.seginfo->CodeBuffer[idx], pbytes, len );
}
#if 1
/* check this in pass 1 only */
else if( CurrSeg->e.seginfo->current_loc < CurrSeg->e.seginfo->start_loc ) {
DebugMsg(("OutputBytes: segment start loc changed from %" FX32 "h to %" FX32 "h\n",
CurrSeg->e.seginfo->start_loc,
CurrSeg->e.seginfo->current_loc));
CurrSeg->e.seginfo->start_loc = CurrSeg->e.seginfo->current_loc;
}
#endif
CurrSeg->e.seginfo->current_loc += len;
CurrSeg->e.seginfo->bytes_written += len;
CurrSeg->e.seginfo->written = TRUE;
if( CurrSeg->e.seginfo->current_loc > CurrSeg->sym.max_offset )
CurrSeg->sym.max_offset = CurrSeg->e.seginfo->current_loc;
}
/* set current offset in a segment (usually CurrSeg) without to write anything */
ret_code SetCurrOffset( struct dsym *seg, uint_32 value, bool relative, bool select_data )
/****************************************************************************************/
{
if( relative )
value += seg->e.seginfo->current_loc;
if ( Options.output_format == OFORMAT_OMF ) {
if ( seg == CurrSeg ) {
if ( write_to_file == TRUE )
omf_FlushCurrSeg( );
/* for debugging, tell if data is located in code sections*/
if( select_data )
if ( ModuleInfo.CommentDataInCode )
omf_OutSelect( TRUE );
LastCodeBufSize = value;
}
seg->e.seginfo->start_loc = value;
/* for -bin, if there's an ORG (relative==false) and no initialized data
* has been set yet, set start_loc!
* v1.96: this is now also done for COFF and ELF
*/
/* else if ( Options.output_format == OFORMAT_BIN && relative == FALSE ) { */
} else {
if ( write_to_file == FALSE ) {
if ( relative ) {
#if 0 /* don't include "preceding" uninitialized data */
if( seg->e.seginfo->current_loc < seg->e.seginfo->start_loc )
seg->e.seginfo->start_loc = seg->e.seginfo->current_loc;
#endif
} else {
if ( seg->e.seginfo->bytes_written == 0 )
seg->e.seginfo->start_loc = value;
}
}
}
seg->e.seginfo->current_loc = value;
seg->e.seginfo->written = FALSE;
if( seg->e.seginfo->current_loc > seg->sym.max_offset )
seg->sym.max_offset = seg->e.seginfo->current_loc;
return( NOT_ERROR );
}
/* finish module writes
* for OMF, just write the MODEND record
* for COFF,ELF and BIN, write the section data and symbol table
*/
static ret_code WriteContent( void )
/**********************************/
{
DebugMsg(("WriteContent enter\n"));
switch ( Options.output_format ) {
case OFORMAT_OMF:
//if( ModendRec == NULL ) {
// EmitError( UNEXPECTED_END_OF_FILE );
// return( ERROR );
//}
/* -if Zi is set, write symbols and types */
if ( Options.debug_symbols )
omf_write_debug_tables();
omf_write_modend( ModuleInfo.start_fixup, ModuleInfo.start_displ );
break;
#if COFF_SUPPORT
case OFORMAT_COFF:
coff_write_data( &ModuleInfo );
coff_write_symbols( &ModuleInfo );
break;
#endif
#if ELF_SUPPORT
case OFORMAT_ELF:
elf_write_data( &ModuleInfo );
break;
#endif
#if BIN_SUPPORT
case OFORMAT_BIN:
bin_write_data( &ModuleInfo );
break;
#endif
}
#if DLLIMPORT
if ( Options.names[OPTN_LNKDEF_FN] ) {
FILE *ld;
struct dsym *curr;
ld = fopen( Options.names[OPTN_LNKDEF_FN], "w" );
if ( ld == NULL ) {
EmitErr( CANNOT_OPEN_FILE, Options.names[OPTN_LNKDEF_FN], ErrnoStr() );
return( ERROR );
}
for ( curr = SymTables[TAB_EXT].head; curr != NULL ; curr = curr->next ) {
DebugMsg(("WriteContent: ext=%s, isproc=%u, weak=%u\n", curr->sym.name, curr->sym.isproc, curr->sym.weak ));
if ( curr->sym.isproc && ( curr->sym.weak == FALSE || curr->sym.iat_used ) &&
curr->sym.dllname && *curr->sym.dllname != NULLC ) {
int size;
Mangle( &curr->sym, StringBufferEnd );
size = sprintf( CurrSource, "import '%s' %s.%s\n", StringBufferEnd, curr->sym.dllname, curr->sym.name );
if ( fwrite( CurrSource, 1, size, ld ) != size )
WriteError();
}
}
fclose( ld );
}
#endif
DebugMsg(("WriteContent exit\n"));
return( NOT_ERROR );
}
#if BIN_SUPPORT || PE_SUPPORT
static ret_code CheckExternal( void )
/***********************************/
{
struct dsym *curr;
for ( curr = SymTables[TAB_EXT].head; curr != NULL ; curr = curr->next )
if( curr->sym.weak == FALSE || curr->sym.used == TRUE ) {
DebugMsg(("CheckExternal: error, %s weak=%u\n", curr->sym.name, curr->sym.weak ));
EmitErr( FORMAT_DOESNT_SUPPORT_EXTERNALS, curr->sym.name );
return( ERROR );
}
return( NOT_ERROR );
}
#endif
/*
* write the OMF/COFF/ELF header
* for OMF, this is called twice, once after Pass 1 is done
* and then again after assembly has finished without errors.
* for COFF/ELF/BIN, it's just called once after assembly passes.
*/
static ret_code WriteHeader( bool initial )
/*****************************************/
{
ret_code rc = NOT_ERROR;
struct dsym *curr;
DebugMsg(("WriteHeader(%u) enter\n", initial));
/* check limit of segments */
for( curr = SymTables[TAB_SEG].head; curr; curr = curr->next ) {
if ( curr->e.seginfo->Ofssize == USE16 && curr->sym.max_offset > 0x10000 ) {
if ( Options.output_format == OFORMAT_OMF )
EmitErr( SEGMENT_EXCEEDS_64K_LIMIT, curr->sym.name );
else
EmitWarn( 2, SEGMENT_EXCEEDS_64K_LIMIT, curr->sym.name );
}
}
switch ( Options.output_format ) {
case OFORMAT_OMF:
if ( initial == TRUE ) {
omf_write_header();
/* if( Options.no_dependencies == FALSE ) */
if( Options.line_numbers )
omf_write_autodep();
if( ModuleInfo.segorder == SEGORDER_DOSSEG )
omf_write_dosseg();
else if( ModuleInfo.segorder == SEGORDER_ALPHA )
SortSegments();
omf_write_lib();
omf_write_lnames();
}
omf_write_seg( initial );
if ( initial == TRUE ) {
omf_write_grp();
omf_write_extdef();
omf_write_comdef();
omf_write_alias();
}
omf_write_public( initial );
if ( initial == TRUE ) {
omf_write_export();
omf_end_of_pass1();
}
break;
#if COFF_SUPPORT
case OFORMAT_COFF:
#if PE_SUPPORT
if ( ModuleInfo.header_format == HFORMAT_PE32
#if AMD64_SUPPORT
|| ModuleInfo.header_format == HFORMAT_PE64
#endif
) {
rc = CheckExternal();
break;
}
#endif
coff_write_header( &ModuleInfo );
coff_write_section_table( &ModuleInfo );
break;
#endif
#if ELF_SUPPORT
case OFORMAT_ELF:
elf_write_header( &ModuleInfo );
break;
#endif
#if BIN_SUPPORT
case OFORMAT_BIN:
rc = CheckExternal(); /* check if externals are used */
break;
#endif
#ifdef DEBUG_OUT
default:
/* this shouldn't happen */
printf("unknown output format: %u\n", Options.output_format);
rc = ERROR;
#endif
}
DebugMsg(("WriteHeader exit, rc=%d\n", rc));
return( rc );
}
#define is_valid_first_char( ch ) ( isalpha(ch) || ch=='_' || ch=='@' || ch=='$' || ch=='?' || ch=='.' )
static int is_valid_identifier( char *id )
/****************************************/
{
/* special handling of first char of an id: it can't be a digit,
but can be a dot (don't care about ModuleInfo.dotname!). */
if( is_valid_first_char( *id ) == 0 )
return( ERROR );
id++;
for( ; *id != NULLC; id++ ) {
if ( is_valid_id_char( *id ) == FALSE )
return( ERROR );
}
/* don't allow a single dot! */
if ( *(id-1) == '.' )
return( ERROR );
return( NOT_ERROR );
}
/* add text macros defined with the -D cmdline switch */
static void add_cmdline_tmacros( void )
/****************************************/
{
struct qitem *p;
char *name;
char *value;
int len;
struct asym *sym;
DebugMsg(("add_cmdline_tmacros enter\n"));
for ( p = Options.queues[OPTQ_MACRO]; p; p = p->next ) {
DebugMsg(("add_cmdline_tmacros: found >%s<\n", p->value));
name = p->value;
value = strchr( name, '=' );
if( value == NULL ) {
/* v2.06: ensure that 'value' doesn't point to r/o space */
//value = "";
value = name + strlen( name ); /* use the terminating NULL */
} else {
len = value - name;
name = (char *)myalloca( len + 1 );
memcpy( name, p->value, len );
*(name + len) = NULLC;
value++;
}
/* there's no check whether the name is a reserved word!
*/
if( is_valid_identifier( name ) == ERROR ) {
DebugMsg(("add_cmdline_tmacros: name >%s< invalid\n", name ));
EmitErr( SYNTAX_ERROR_EX, name );
} else {
sym = SymSearch( name );
if ( sym == NULL ) {
sym = SymCreate( name );
sym->state = SYM_TMACRO;
}
if ( sym->state == SYM_TMACRO ) {
sym->isdefined = TRUE;
sym->predefined = TRUE;
sym->string_ptr = value;
} else
EmitErr( SYMBOL_ALREADY_DEFINED, name );
}
}
return;
}
/* add the include paths set by -I option */
static void add_incpaths( void )
/******************************/
{
struct qitem *p;
DebugMsg(("add_incpaths: enter\n"));
for ( p = Options.queues[OPTQ_INCPATH]; p; p = p->next ) {
AddStringToIncludePath( p->value );
}
}
/* this is called for every pass.
* symbol table and ModuleInfo are initialized.
*/
static void CmdlParamsInit( int pass )
/************************************/
{
struct qitem *pq;
DebugMsg(("CmdlParamsInit(%u) enter\n", pass));
#if BUILD_TARGET
if ( pass == PASS_1 ) {
struct asym *sym;
char *tmp;
char *p;
_strupr( Options.build_target );
tmp = myalloca( strlen( Options.build_target ) + 5 ); /* null + 4 uscores */
strcpy( tmp, uscores );
strcat( tmp, Options.build_target );
strcat( tmp, uscores );
/* define target */
sym = CreateVariable( tmp, 0 );
sym->predefined = TRUE;
p = NULL;
if( stricmp( Options.build_target, "DOS" ) == 0 ) {
p = "__MSDOS__";
} else if( stricmp( Options.build_target, "NETWARE" ) == 0 ) {
if( ( ModuleInfo.curr_cpu & P_CPU_MASK ) >= P_386 ) {
p = "__NETWARE_386__";
} else {
/* do nothing ... __NETWARE__ already defined */
}
} else if( stricmp( Options.build_target, "WINDOWS" ) == 0 ) {
if( ( ModuleInfo.curr_cpu & P_CPU_MASK ) >= P_386 ) {
p = "__WINDOWS_386__";
} else {
/* do nothing ... __WINDOWS__ already defined */
}
} else if( stricmp( Options.build_target, "QNX" ) == 0 ) {
p = "__UNIX__";
} else if( stricmp( Options.build_target, "LINUX" ) == 0 ) {
p = "__UNIX__";
}
if ( p ) {
sym = CreateVariable( p, 0 );
sym->predefined = TRUE;
}
}
#endif
for ( pq = Options.queues[OPTQ_FINCLUDE]; pq; pq = pq->next ) {
DebugMsg(("add_include file: %s\n", pq->value ));
InputQueueFile( pq->value, NULL );
}
if ( pass == PASS_1 ) {
char *env;
/* v2.06: this is done in ModulePassInit now */
//SetCPU( Options.cpu );
add_cmdline_tmacros();
add_incpaths();
if ( Options.ignore_include == FALSE )
if ( env = getenv( "INCLUDE" ) )
AddStringToIncludePath( env );
}
DebugMsg(("CmdlParamsInit exit\n"));
return;
}
void WritePreprocessedLine( const char *string )
/**********************************************/
/* print out preprocessed source lines
*/
{
static bool PrintEmptyLine = TRUE;
const char *p;
#if 0 /* v2.08: removed, obsolete */
/* filter some macro specific directives */
if ( tokenarray[0].token == T_DIRECTIVE &&
( tokenarray[0].tokval == T_ENDM ||
tokenarray[0].tokval == T_EXITM))
return;
/* don't print generated code - with one exception:
if the code was generated as a result of structure initialization,
then do!
*/
if ( GeneratedCode )
return;
#endif
if ( Token_Count > 0 ) {
/* v2.08: don't print a leading % (this char is no longer filtered) */
for ( p = string; isspace( *p ); p++ );
printf("%s\n", *p == '%' ? p+1 : string );
PrintEmptyLine = TRUE;
} else if ( PrintEmptyLine ) {
PrintEmptyLine = FALSE;
printf("\n");
}
}
/* set Masm v5.1 compatibility options */
void SetMasm510( bool value )
/***************************/
{
ModuleInfo.m510 = value;
ModuleInfo.oldstructs = value;
/* ModuleInfo.oldmacros = value; not implemented yet */
ModuleInfo.dotname = value;
ModuleInfo.setif2 = value;
if ( value ) {
if ( ModuleInfo.model == MODEL_NONE ) {
/* if no model is specified, set OFFSET:SEGMENT */
ModuleInfo.offsettype = OT_SEGMENT;
if ( ModuleInfo.langtype == LANG_NONE ) {
ModuleInfo.scoped = FALSE;
ModuleInfo.procs_private = TRUE;
}
}
}
return;
}
/* called for each pass */
static void ModulePassInit( void )
/********************************/
{
enum cpu_info cpu = Options.cpu;
enum model_type model = Options.model;
DebugMsg(( "ModulePassInit enter\n" ));
/* set default values not affected by the masm 5.1 compat switch */
ModuleInfo.procs_private = FALSE;
ModuleInfo.procs_export = FALSE;
ModuleInfo.offsettype = OT_GROUP;
ModuleInfo.scoped = TRUE;
#if FASTPASS
/* v2.03: don't generate the code if fastpass is active */
/* v2.08: query UseSavedState instead of StoreState */
//if ( StoreState == FALSE ) {
if ( UseSavedState == FALSE ) {
#endif
NewLineQueue(); /* ensure line queue is empty */
ModuleInfo.langtype = Options.langtype;
ModuleInfo.fctype = Options.fctype;
#if AMD64_SUPPORT
if ( ModuleInfo.header_format == HFORMAT_WIN64 || ModuleInfo.header_format == HFORMAT_ELF64 ) {
/* v2.06: force cpu to be at least P_64, without side effect to Options.cpu */
if ( ( cpu & P_CPU_MASK ) < P_64 ) /* enforce cpu to be 64-bit */
cpu = P_64;
/* ignore -m switch for 64-bit formats.
* there's no other model than FLAT possible.
*/
model = MODEL_FLAT;
if ( ModuleInfo.header_format == HFORMAT_WIN64 ) {
if ( ModuleInfo.langtype == LANG_NONE ) {
ModuleInfo.langtype = LANG_FASTCALL;
}
ModuleInfo.fctype = FCT_WIN64;
}
} else
#endif
/* if model FLAT is to be set, ensure that cpu is compat. */
if ( model == MODEL_FLAT && ( cpu & P_CPU_MASK ) < P_386 ) /* cpu < 386? */
cpu = P_386;
SetCPU( cpu );
/* table ModelToken starts with MODEL_TINY, which is index 1" */
if ( model != MODEL_NONE )
AddLineQueueX( "%r %s", T_DOT_MODEL, ModelToken[model - 1] );
#if FASTPASS
}
#endif
SetMasm510( Options.masm51_compat );
ModuleInfo.defOfssize = USE16;
ModuleInfo.ljmp = TRUE;
ModuleInfo.list = Options.write_listing;
ModuleInfo.cref = TRUE;
ModuleInfo.listif = Options.listif;
ModuleInfo.list_generated_code = Options.list_generated_code;
ModuleInfo.list_macro = Options.list_macro;
ModuleInfo.case_sensitive = Options.case_sensitive;
ModuleInfo.convert_uppercase = Options.convert_uppercase;
SymSetCmpFunc();
ModuleInfo.segorder = SEGORDER_SEQ;
ModuleInfo.radix = 10;
ModuleInfo.fieldalign = Options.fieldalign;
#if PROCALIGN
ModuleInfo.procalign = 0;
#endif
}
void RunLineQueue( void )
/***********************/
{
struct input_status oldstat;
struct asm_tok *tokenarray;
DebugMsg1(( "RunLineQueue() enter\n" ));
/* v2.03: ensure the current source buffer is still aligned */
tokenarray = PushInputStatus( &oldstat );
GeneratedCode++;
while ( ( Token_Count = GetPreprocessedLine( CurrSource, tokenarray ) ) >= 0 ) {
if ( Token_Count )
ParseLine( tokenarray );
}
#ifdef DEBUG_OUT
if ( ModuleInfo.EndDirFound == TRUE ) {
DebugMsg(("!!!!! Error: End directive found in generated-code parser loop!\n"));
}
lq_line = 0;
#endif
GeneratedCode--;
PopInputStatus( &oldstat );
DebugMsg1(( "RunLineQueue() exit\n" ));
return;
}
/* this is called by InitializeStructure(), which is a special case */
void RunLineQueueEx( void )
/*************************/
{
struct input_status oldstat;
struct asm_tok *tokenarray;
DebugMsg1(( "RunLineQueueEx() enter\n" ));
tokenarray = PushInputStatus( &oldstat );
while ( ( Token_Count = GetPreprocessedLine( CurrSource, tokenarray ) ) >= 0 ) {
if ( Token_Count ) {
ParseLine( tokenarray );
/* handle special case 'structure initialization' */
if ( Options.preprocessor_stdout == TRUE )
WritePreprocessedLine( CurrSource );
}
}
#ifdef DEBUG_OUT
if ( ModuleInfo.EndDirFound == TRUE ) {
DebugMsg(("!!!!! Error: End directive found in generated-code parser loop!\n"));
}
lq_line = 0;
#endif
PopInputStatus( &oldstat );
DebugMsg1(( "RunLineQueueEx() exit\n" ));
return;
}
/*
* set index field for EXTERN/PROTO/COMM.
* This is called after PASS 1 has been finished.
*/
static void set_ext_idx( void )
/*****************************/
{
struct dsym *curr;
uint index = 0;
DebugMsg(("set_ext_idx() enter\n"));
/* scan ALIASes for COFF/ELF */
#if FASTPASS
#if COFF_SUPPORT || ELF_SUPPORT
if ( Options.output_format == OFORMAT_COFF
#if ELF_SUPPORT
|| Options.output_format == OFORMAT_ELF
#endif
) {
for( curr = SymTables[TAB_ALIAS].head ; curr != NULL ;curr = curr->next ) {
struct asym *sym;
sym = curr->sym.substitute;
/* check if symbol is external or public */
if ( sym == NULL ||
( sym->state != SYM_EXTERNAL &&
( sym->state != SYM_INTERNAL || sym->public == FALSE ))) {
SkipSavedState();
break;
}
/* make sure it becomes a strong external */
if ( sym->state == SYM_EXTERNAL )
sym->used = TRUE;
}
}
#endif
#endif
/* scan the EXTERN/EXTERNDEF items */
for( curr = SymTables[TAB_EXT].head ; curr != NULL ;curr = curr->next ) {
/* v2.01: externdefs which have been "used" become "strong" */
if ( curr->sym.used )
curr->sym.weak = FALSE;
/* skip COMM and unused EXTERNDEF/PROTO items. */
if (( curr->sym.iscomm == TRUE ) || ( curr->sym.weak == TRUE ))
continue;
#if FASTPASS==0
/* v2.05: clear fixup list (used for backpatching in pass one) */
if ( curr->sym.fixup ) {
struct fixup *c;
struct fixup *n;
for( c = curr->sym.fixup ; c; ) {
n = c->nextbp;
LclFree( c );
c = n;
}
}
#endif
index++;
curr->sym.ext_idx = index;
/* optional alternate symbol must be INTERNAL or EXTERNAL.
* COFF ( and ELF? ) also wants internal symbols public.
*/
#if FASTPASS
if ( curr->sym.altname ) {
if ( curr->sym.altname->state == SYM_INTERNAL ) {
#if COFF_SUPPORT || ELF_SUPPORT
/* for COFF/ELF, the altname must be public or external */
if ( curr->sym.altname->public == FALSE &&
( Options.output_format == OFORMAT_COFF
#if ELF_SUPPORT
|| Options.output_format == OFORMAT_ELF
#endif
) ) {
SkipSavedState();
}
#endif
} else if ( curr->sym.altname->state != SYM_EXTERNAL ) {
/* do not use saved state, scan full source in second pass */
SkipSavedState();
}
}
#endif
}
/* now scan the COMM items */
for( curr = SymTables[TAB_EXT].head; curr != NULL; curr = curr->next ) {
if ( curr->sym.iscomm == FALSE )
continue;
index++;
curr->sym.ext_idx = index;
}
DebugMsg(("set_ext_idx() exit\n"));
return;
}
#if 0 /* v2.07: removed */
/* scan - and clear - global queue (EXTERNDEFs).
* items which have been defined within the module
* will become public.
* PROTOs aren't included in the global queue.
* They will become public when - and if - the PROC directive
* for the symbol is met.
*/
static void scan_globals( void )
/******************************/
{
struct qnode *curr;
struct qnode *next;
struct asym *sym;
/* turn EXTERNDEFs into PUBLICs if defined in the module.
* PROCs are handled differently - so ignore these entries here!
*/
/* obsolete since v2.07.
* it's simpler and better to make the symbol public if it turns
* from SYM_EXTERNAL to SYM_INTERNAL.
* the other case, that is, the EXTERNDEF comes AFTER the definition,
* is handled in ExterndefDirective()
*/
DebugMsg(("scan_globals: GlobalQueue=%X\n", ModuleInfo.g.GlobalQueue));
for ( curr = ModuleInfo.g.GlobalQueue.head; curr; curr = next ) {
next = curr->next;
sym = (struct asym *)curr->elmt;
DebugMsg(("scan_globals: %s state=%u used=%u public=%u\n", sym->name, sym->state, sym->used, sym->public ));
if( sym->state == SYM_INTERNAL && sym->public == FALSE && sym->isproc == FALSE ) {
/* add it to the public queue */
sym->public = TRUE;
QEnqueue( &ModuleInfo.g.PubQueue, curr );
DebugMsg(("scan_globals: %s added to public queue\n", sym->name ));
continue; /* don't free this item! */
}
LclFree( curr );
}
/* the queue is empty now */
ModuleInfo.g.GlobalQueue.head = NULL;
}
#endif
/* checks after pass one has been finished without errors */
static void PassOneChecks( void )
/*******************************/
{
#if FASTPASS || defined(DEBUG_OUT)
struct dsym *curr;
#endif
#if FASTPASS
struct qnode *q;
#endif
/* check for open structures and segments has been done inside the
* END directive handling already */
ProcCheckOpen();
HllCheckOpen();
CondCheckOpen();
if( ModuleInfo.EndDirFound == FALSE )
EmitError( END_DIRECTIVE_REQUIRED );
#ifdef DEBUG_OUT
for ( curr = SymTables[TAB_UNDEF].head; curr; curr = curr->next ) {
DebugMsg(("PassOneChecks: undefined symbol %s\n", curr->sym.name ));
}
#endif
#if FASTPASS
if ( SymTables[TAB_UNDEF].head ) {
/* to force a full second pass in case of missing symbols,
* activate the next line. It was implemented to have proper
* error displays if a forward reference wasn't found.
* However, v1.95 final won't need this anymore, because both
* filename + lineno for every line is known now in pass 2.
*/
/* SkipSavedState(); */
}
/* check if there's an undefined segment reference.
* This segment was an argument to a group definition then.
* Just do a full second pass, the GROUP directive will report
* the error.
*/
for( curr = SymTables[TAB_SEG].head; curr; curr = curr->next ) {
if( curr->sym.segment == NULL ) {
DebugMsg(("PassOneChecks: undefined segment %s\n", curr->sym.name ));
SkipSavedState();
break;
}
}
/* v2.04: scan the publics queue. This check was previously done
* in GetPublicData(), but there it works for OMF format only.
*/
for( q = ModuleInfo.g.PubQueue.head; q; q = q->next ) {
const struct asym *sym = q->elmt;
if ( sym->state == SYM_INTERNAL )
continue;
if ( sym->state != SYM_EXTERNAL || sym->weak == FALSE )
SkipSavedState();
break;
}
#if COFF_SUPPORT
/* if there's an item in the safeseh list which is not an
* internal proc, make a full second pass to emit a proper
* error msg at the .SAFESEH directive
*/
if ( ModuleInfo.g.SafeSEHList.head ) {
struct qnode *node;
for ( node = ModuleInfo.g.SafeSEHList.head; node; node = node->next )
if ( ((struct asym *)node->elmt)->state != SYM_INTERNAL || ((struct asym *)node->elmt)->isproc == FALSE ) {
SkipSavedState();
break;
}
}
#endif
#endif
#ifdef DEBUG_OUT
DebugMsg(("PassOneChecks: forward references:\n"));
for( curr = SymTables[TAB_SEG].head; curr; curr = curr->next ) {
int i;
int j;
struct asym * sym;
struct fixup * fix;
for ( i = 0, j = 0, sym = curr->e.seginfo->labels;sym;sym = (struct asym *)((struct dsym *)sym)->next ) {
i++;
for ( fix = sym->fixup; fix ; fix = fix->nextbp, j++ );
}
DebugMsg(("PassOneChecks: segm=%s, labels=%u forward refs=%u\n", curr->sym.name, i, j));
}
#endif
return;
}
/* do ONE assembly pass
* the FASTPASS variant (which is default now) doesn't scan the full source