-
Notifications
You must be signed in to change notification settings - Fork 98
/
phase_1.c
13196 lines (10583 loc) · 361 KB
/
phase_1.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
#include "flags.h"
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <math.h>
#include <time.h>
#include "defines.h"
#include "include.h"
#include "parse.h"
#include "phase_1.h"
#include "phase_2.h"
#include "phase_3.h"
#include "stack.h"
#include "hashmap.h"
#include "printf.h"
#include "decode.h"
#include "mersenne.h"
#include "main.h"
#if defined(GB)
char g_licenseecodenew_c1, g_licenseecodenew_c2;
int g_nintendologo_defined = 0;
int g_computechecksum_defined = 0, g_computecomplementcheck_defined = 0;
int g_romgbc = 0, g_romdmg = 0, g_romsgb = 0, g_romsize = 0, g_romsize_defined = 0;
int g_cartridgetype = 0, g_cartridgetype_defined = 0;
int g_countrycode = 0, g_countrycode_defined = 0;
int g_licenseecodenew_defined = 0, g_licenseecodeold = 0, g_licenseecodeold_defined = 0;
int g_version = 0, g_version_defined = 0;
static int s_gbheader_defined = 0;
#endif
#if defined(Z80)
char *g_sdsctag_name_str = NULL, *g_sdsctag_notes_str = NULL, *g_sdsctag_author_str = NULL;
int g_sdsctag_name_type, g_sdsctag_notes_type, g_sdsctag_author_type, g_sdsc_ma, g_sdsc_mi;
int g_sdsctag_name_value, g_sdsctag_notes_value, g_sdsctag_author_value;
int g_computesmschecksum_defined = 0, g_sdsctag_defined = 0, g_smstag_defined = 0;
int g_smsheader_defined = 0, g_smsversion = 0, g_smsversion_defined = 0, g_smsregioncode = 0, g_smsregioncode_defined = 0;
int g_smsproductcode_defined = 0, g_smsproductcode1 = 0, g_smsproductcode2 = 0, g_smsproductcode3 = 0, g_smsreservedspace1 = 0;
int g_smsreservedspace2 = 0, g_smsromsize = 0, g_smsromsize_defined = 0;
int g_smsforcechecksum = 0, g_smsforcechecksum_defined = 0, g_smschecksumsize = 0, g_smschecksumsize_defined = 0;
static int s_smsreservedspace_defined = 0;
#endif
#if defined(MC68000)
char g_smdheader_systemtype[17] = "SEGA MEGA DRIVE ";
char g_smdheader_copyright[17] = " ";
char g_smdheader_titledomestic[49] = " ";
char g_smdheader_titleoverseas[49] = " ";
char g_smdheader_serialnumber[15] = " ";
char g_smdheader_devicesupport[17] = "J ";
int g_smdheader_romaddressrange_start = 0, g_smdheader_romaddressrange_end = -1;
int g_smdheader_ramaddressrange_start = 0xff0000, g_smdheader_ramaddressrange_end = 0xffffff;
char g_smdheader_extramemory_type_1[3] = "RA";
int g_smdheader_extramemory_type_2 = 0xa0, g_smdheader_extramemory_type_3 = 0x20, g_smdheader_extramemory_start = 0, g_smdheader_extramemory_end = 0;
char g_smdheader_modemsupport[13] = " ";
char g_smdheader_regionsupport[4] = "JUE";
int g_computesmdchecksum_defined = 0, g_smdheader_defined = NO;
#endif
int g_org_defined = 1, g_background_defined = 0;
int g_rombanks = 0, g_rombanks_defined = 0, g_max_address = 0;
int g_rambanks = 0, g_rambanks_defined = 0;
int g_emptyfill = 0, g_emptyfill_defined = 0;
int g_section_status = OFF, g_section_id = 1;
int g_parsed_int, g_source_index, g_ifdef = 0, g_slots_amount = 0;
int g_memorymap_defined = 0;
int g_banksize_defined = 0, g_banksize = 0;
int g_rombankmap_defined = 0, *g_banks = NULL, *g_bankaddress = NULL;
int g_bankheader_status = OFF;
int g_macro_active = 0;
int g_smc_defined = 0;
int g_asciitable_defined = 0;
int g_saved_structures_count = 0;
unsigned char g_asciitable[256];
extern int g_resolve_stack_calculations;
#if defined(W65816)
char g_snesid[4];
int g_snesid_defined = 0, g_snesromsize = 0;
int g_sramsize_defined = 0, g_sramsize = 0, g_country_defined = 0, g_country = 0;
int g_cartridgetype = 0, g_cartridgetype_defined = 0, g_licenseecode_defined = 0, g_licenseecode = 0;
int g_version_defined = 0, g_version = 0;
int g_hirom_defined = 0, g_lorom_defined = 0, g_slowrom_defined = 0, g_fastrom_defined = 0, g_snes_mode = 0;
int g_exlorom_defined = 0, g_exhirom_defined = 0;
int g_computesneschecksum_defined = 0, g_use_wdc_standard = 0;
static int s_snesemuvector_defined = 0, s_snesheader_defined = 0, s_snesnativevector_defined = 0;
#endif
#if defined(GB) || defined(W65816)
char g_name[32];
int g_name_defined = 0;
#endif
#if defined(SPC700)
extern int g_input_number_expects_dot;
#endif
int g_sizeof_g_tmp = 4096, g_global_listfile_items = 0, *g_global_listfile_ints = NULL;
int g_romheader_baseaddress = -1, g_romheader_baseaddress_defined = 0;
char *g_tmp = NULL, *g_global_listfile_cmds = NULL;
char *g_label_stack[256];
char g_current_directive[MAX_NAME_LENGTH + 1];
unsigned char *g_rom_banks = NULL, *g_rom_banks_usage_table = NULL;
struct structure **g_saved_structures = NULL;
struct export_def *g_export_first = NULL, *g_export_last = NULL;
struct map_t *g_defines_map = NULL;
struct macro_static *g_macros_first = NULL, *g_macros_last = NULL;
struct section_def *g_sections_first = NULL, *g_sections_last = NULL, *g_sec_tmp, *g_sec_next;
struct macro_runtime *g_macro_stack = NULL, *g_macro_runtime_current = NULL;
struct repeat_runtime *g_repeat_stack = NULL;
struct slot g_slots[256];
struct structure *g_structures_first = NULL;
struct filepointer *g_filepointers = NULL;
struct map_t *g_namespace_map = NULL;
struct after_section *g_after_sections = NULL;
struct label_sizeof *g_label_sizeofs = NULL;
struct block_name *g_block_names = NULL;
struct stringmaptable *g_stringmaptables = NULL;
struct array *g_arrays_first = NULL;
struct string *g_fopen_filenames_first = NULL, *g_fopen_filenames_last = NULL;
struct function *g_functions_first = NULL, *g_functions_last = NULL;
struct namespace *g_namespaces_first = NULL;
extern char *g_buffer, *unfolded_buffer, g_label[MAX_NAME_LENGTH + 1], *g_include_dir, *g_full_name;
extern int g_source_file_size, g_input_number_error_msg, g_verbose_level, g_output_format, g_open_files, g_input_parse_if;
extern int g_last_stack_id, g_latest_stack, g_ss, g_commandline_parsing, g_newline_beginning, g_expect_calculations, g_input_parse_special_chars;
extern int g_extra_definitions, g_string_size, g_input_float_mode, g_operand_hint, g_operand_hint_type, g_dsp_enable_label_address_conversion;
extern int g_include_dir_size, g_parse_floats, g_listfile_data, g_quiet, g_parsed_double_decimal_numbers;
extern int g_create_sizeof_definitions, g_input_allow_leading_hashtag, g_input_has_leading_hashtag, g_input_allow_leading_ampersand;
extern int g_plus_and_minus_ends_label, g_get_next_token_use_substitution, g_input_number_turn_values_into_strings;
extern int g_continue_parsing_after_an_error, g_continued_parsing_after_an_error, g_allow_labels_without_colon;
extern FILE *g_file_out_ptr;
extern double g_parsed_double;
extern char *g_final_name;
extern struct active_file_info *g_active_file_info_first, *g_active_file_info_last, *g_active_file_info_tmp;
extern struct file_name_info *g_file_name_info_first, *g_file_name_info_last, *g_file_name_info_tmp;
extern struct incbin_file_data *g_incbin_file_data_first, *g_ifd_tmp;
extern int g_makefile_rules, g_makefile_skip_file_handling, g_parsing_function_body, g_force_add_namespace, g_is_file_isolated_counter, g_force_ignore_namespace;
extern int create_tmp_file(FILE **);
static int s_macro_stack_size = 0, s_repeat_stack_size = 0;
static int s_bank = 0, s_bank_defined = 1, s_line_count_status = ON;
static int s_block_status = 0, s_block_name_id = 0, s_parse_dstruct_result;
static int s_dstruct_status = OFF, s_current_slot = 0;
static int s_enumid_defined = 0, s_enumid = 0, s_enumid_adder = 1, s_enumid_export = 0;
static int s_repeat_active = 0, s_saved_structures_max = 0, s_skip_elifs[256];
#if defined(MCS6502) || defined(WDC65C02) || defined(CSG65CE02) || defined(W65816) || defined(HUC6280) || defined(MC6800) || defined(MC6801) || defined(MC6809)
int g_xbit_size = 0, g_accu_size = 8, g_index_size = 8;
#endif
/* vars used when in an enum, ramsection, or struct. */
/* some variables named "enum_" are used in enums, ramsections, and structs. */
int g_in_enum = NO, g_in_ramsection = NO, g_in_struct = NO, g_macro_id = 0;
static int s_enum_export, s_enum_ord;
static int s_enum_offset; /* Offset relative to enum start where we're at right now */
static int s_last_enum_offset;
static int s_base_enum_offset; /* start address of enum */
static int s_enum_sizeof_pass; /* set on second pass through enum/ramsection, generating _sizeof labels */
/* temporary struct used to build up enums/ramsections (and, of course, structs)
this gets temporarily replaced when inside a union (each union is considered a separate struct). */
static struct structure *s_active_struct = NULL;
static int s_union_base_offset; /* start address of current union */
static int s_max_enum_offset; /* highest position seen within current union group */
static struct structure *s_union_first_struct = NULL; /* first struct in current union */
static struct union_stack *s_union_stack = NULL; /* stores variables for nested unions */
/* for .TABLE, .DATA and .ROW */
static char s_table_format[256];
static int s_table_defined = 0, s_table_size = 0, s_table_index = 0;
static int s_source_index_old = 0, s_line_current_old = 0;
static int s_defaultslot_defined = 0, s_defaultslot;
static int s_autopriority = 65535;
static struct section_def *s_active_ramsection = NULL;
static char s_replacement_memory[1024], s_replacement_backup[1024];
static int s_replacement_length = 0, s_replacement_index = -1;
#define no_library_files(name) \
do { \
if (g_output_format == OUTPUT_LIBRARY) { \
print_error(ERROR_DIR, "Library files don't take " name ".\n"); \
return FAILED; \
} \
} while (0)
int strcaselesscmp(char *s1, char *s2) {
if (s1 == NULL || s2 == NULL)
return 0;
while (*s1 != 0 && *s2 != 0) {
if (toupper((int)*s1) != toupper((int)*s2))
return 1;
s1++;
s2++;
}
if (*s2 != 0)
return 1;
if (*s1 != 0)
return -1;
return 0;
}
static char *_string_duplicate_size(char *p, int size) {
char *result;
if (p == NULL)
return NULL;
result = calloc(sizeof(char), size + 1);
if (result == NULL) {
print_error(ERROR_DIR, "Out of memory while allocating a new string (\"%s\").\n", p);
return NULL;
}
memcpy(result, p, size);
result[size] = 0;
return result;
}
static char *_string_duplicate(char *p) {
return _string_duplicate_size(p, (int)strlen(p));
}
static int _add_namespace_to_string(char *s, int sizeof_s, char *type, char *namespace) {
char buf[MAX_NAME_LENGTH + 1];
snprintf(buf, sizeof(buf), "%s.%s", namespace, s);
buf[sizeof(buf)-1] = 0;
if (strlen(buf) >= (size_t)sizeof_s) {
print_error(ERROR_ERR, "The current file namespace \"%s\" cannot be added to %s's \"%s\" name - increase MAX_NAME_LENGTH in shared.h and recompile WLA.\n", namespace, type, s);
return FAILED;
}
strcpy(s, buf);
return SUCCEEDED;
}
int add_namespace_to_string(char *s, int sizeof_s, char *type) {
if (g_active_file_info_last == NULL || g_active_file_info_last->namespace[0] == 0)
return SUCCEEDED;
return _add_namespace_to_string(s, sizeof_s, type, g_active_file_info_last->namespace);
}
static int _get_slot_number_by_its_name(char *slot_name, int *number) {
char c1 = slot_name[0];
int i;
for (i = 0; i < g_slots_amount; i++) {
if (c1 == g_slots[i].name[0]) {
if (strcmp(slot_name, g_slots[i].name) == 0) {
*number = i;
return SUCCEEDED;
}
}
}
print_error(ERROR_DIR, "Cannot find SLOT \"%s\".\n", slot_name);
return FAILED;
}
static int _get_slot_number_by_a_value(int value, int *slot) {
int i;
if (value < 0) {
*slot = value;
return FAILED;
}
if (value < g_slots_amount) {
/* value can be the direct SLOT ID, but can it be a SLOT's address as well? */
for (i = 0; i < g_slots_amount; i++) {
if (g_slots[i].address == value && value != i) {
print_error(ERROR_WRN, "There is a SLOT number %d, but there also is a SLOT (with ID %d) with starting address %d ($%x)... Using SLOT %d.\n", value, i, value, value, value);
break;
}
}
*slot = value;
return SUCCEEDED;
}
for (i = 0; i < g_slots_amount; i++) {
if (g_slots[i].address == value) {
*slot = i;
return SUCCEEDED;
}
}
*slot = -1;
print_error(ERROR_DIR, "Cannot find SLOT %d.\n", value);
return FAILED;
}
int macro_get(char *name, int add_namespace, struct macro_static **macro_out) {
struct macro_static *macro;
char fullname[MAX_NAME_LENGTH + 1];
char c1;
strcpy(fullname, name);
if (add_namespace == YES) {
/* are we inside a .MACRO that's been namespaced? */
if (g_macro_active > 0) {
struct macro_runtime *rt;
struct macro_static *st;
rt = &g_macro_stack[g_macro_active - 1];
st = rt->macro;
if (st->defined_namespace[0] != 0) {
/* yes! try to use the .MACRO's namespace */
if (_add_namespace_to_string(fullname, sizeof(fullname), ".MACRO", st->defined_namespace) == FAILED) {
*macro_out = NULL;
return FAILED;
}
}
}
else if (g_active_file_info_last->namespace[0] != 0) {
if (add_namespace_to_string(fullname, sizeof(fullname), ".MACRO") == FAILED) {
*macro_out = NULL;
return FAILED;
}
}
}
c1 = fullname[0];
macro = g_macros_first;
while (macro != NULL) {
if (c1 == macro->name[0]) {
if (strcmp(macro->name, fullname) == 0)
break;
}
macro = macro->next;
}
*macro_out = macro;
return SUCCEEDED;
}
static int _macro_stack_grow(void) {
if (g_macro_active == s_macro_stack_size) {
struct macro_runtime *macro;
int old_size;
/* enlarge the macro stack */
old_size = s_macro_stack_size;
s_macro_stack_size = (s_macro_stack_size<<1)+2;
macro = calloc(sizeof(struct macro_runtime) * s_macro_stack_size, 1);
if (macro == NULL) {
print_error(ERROR_ERR, "Out of memory error while enlarging macro stack buffer.\n");
return FAILED;
}
if (g_macro_stack != NULL) {
memcpy(macro, g_macro_stack, sizeof(struct macro_runtime) * old_size);
free(g_macro_stack);
}
g_macro_stack = macro;
g_macro_runtime_current = &g_macro_stack[g_macro_active - 1];
}
return SUCCEEDED;
}
static int _macro_start(struct macro_static *m, struct macro_runtime *mrt, int caller, int nargs) {
g_macro_runtime_current = mrt;
g_macro_active++;
m->calls++;
/* macro call start */
fprintf(g_file_out_ptr, "i%d %s ", m->id, m->name);
mrt->caller = caller;
mrt->macro = m;
mrt->macro_return_i = g_source_index;
mrt->macro_return_line = g_active_file_info_last->line_current;
mrt->macro_return_filename_id = g_active_file_info_last->filename_id;
if ((g_extra_definitions == ON) && (g_active_file_info_last->filename_id != m->filename_id)) {
redefine("WLA_FILENAME", 0.0, get_file_name(m->filename_id), DEFINITION_TYPE_STRING, (int)strlen(get_file_name(m->filename_id)));
redefine("wla_filename", 0.0, get_file_name(m->filename_id), DEFINITION_TYPE_STRING, (int)strlen(get_file_name(m->filename_id)));
}
fprintf(g_file_out_ptr, "k%d ", m->start_line);
g_active_file_info_last->line_current = m->start_line;
g_active_file_info_last->filename_id = m->filename_id;
g_source_index = m->start;
/* redefine NARGS */
if (redefine("NARGS", (double)nargs, NULL, DEFINITION_TYPE_VALUE, 0) == FAILED)
return FAILED;
if (redefine("nargs", (double)nargs, NULL, DEFINITION_TYPE_VALUE, 0) == FAILED)
return FAILED;
return SUCCEEDED;
}
static int _macro_start_dxm(struct macro_static *m, int caller, char *name, int first) {
struct macro_runtime *mrt;
int start, number_result;
/* start running a macro... run until .ENDM */
if (_macro_stack_grow() == FAILED)
return FAILED;
mrt = &g_macro_stack[g_macro_active];
start = g_source_index;
if (first == NO && mrt->string_current < mrt->string_last) {
number_result = SUCCEEDED;
g_parsed_int = mrt->string[mrt->string_current++];
}
else {
number_result = input_number();
mrt->string_current = 0;
mrt->string_last = 0;
}
if (first == YES)
mrt->offset = 0;
else
mrt->offset++;
if (number_result == INPUT_NUMBER_EOL && first == NO) {
next_line();
return SUCCEEDED;
}
mrt->argument_data = calloc(sizeof(struct macro_argument *) << 1, 1);
mrt->argument_data[0] = calloc(sizeof(struct macro_argument), 1);
mrt->argument_data[1] = calloc(sizeof(struct macro_argument), 1);
if (mrt->argument_data == NULL || mrt->argument_data[0] == NULL || mrt->argument_data[1] == NULL) {
print_error(ERROR_NONE, "Out of memory error while collecting macro arguments.\n");
return FAILED;
}
mrt->argument_data[1]->type = SUCCEEDED;
mrt->argument_data[1]->value = mrt->offset;
/* filter all the data through that macro */
mrt->argument_data[0]->start = start;
mrt->argument_data[0]->type = number_result;
if (number_result == FAILED)
return FAILED;
else if (number_result == INPUT_NUMBER_EOL) {
print_error(ERROR_INP, ".%s needs data.\n", name);
return FAILED;
}
mrt->supplied_arguments = 2;
if (number_result == INPUT_NUMBER_ADDRESS_LABEL)
strcpy(mrt->argument_data[0]->string, g_label);
else if (number_result == INPUT_NUMBER_STRING) {
mrt->argument_data[0]->type = SUCCEEDED;
mrt->argument_data[0]->value = g_label[0];
strcpy(mrt->string, g_label);
mrt->string_current = 1;
mrt->string_last = (int)strlen(g_label);
/*
print_text(NO, "got string %s!\n", label);
*/
}
else if (number_result == INPUT_NUMBER_STACK)
mrt->argument_data[0]->value = (double)g_latest_stack;
else if (number_result == SUCCEEDED)
mrt->argument_data[0]->value = g_parsed_int;
else
return FAILED;
if (_macro_start(m, mrt, caller, 1) == FAILED)
return FAILED;
return SUCCEEDED;
}
static int _macro_start_incbin(struct macro_static *m, struct macro_incbin *incbin_data, int first) {
struct macro_runtime *mrt;
/* start running a macro... run until .ENDM */
if (_macro_stack_grow() == FAILED)
return FAILED;
mrt = &g_macro_stack[g_macro_active];
if (first == YES)
mrt->incbin_data = incbin_data;
else
incbin_data = mrt->incbin_data;
if (incbin_data->left <= 0) {
/* free the incbin_data structure! it seems we came here from .ENDM and just ran out of data...
NOTE: don't free mrt->incbin_data->data as it's a copied pointer */
free(mrt->incbin_data);
mrt->incbin_data = NULL;
return SUCCEEDED;
}
if (first == YES)
mrt->offset = 0;
else
mrt->offset++;
mrt->argument_data = calloc(sizeof(struct macro_argument *) * 3, 1);
mrt->argument_data[0] = calloc(sizeof(struct macro_argument), 1);
mrt->argument_data[1] = calloc(sizeof(struct macro_argument), 1);
mrt->argument_data[2] = calloc(sizeof(struct macro_argument), 1);
if (mrt->argument_data == NULL || mrt->argument_data[0] == NULL || mrt->argument_data[1] == NULL || mrt->argument_data[2] == NULL) {
print_error(ERROR_NONE, "Out of memory error while collecting macro arguments.\n");
return FAILED;
}
/* filter all the data through that macro */
mrt->argument_data[0]->type = SUCCEEDED;
mrt->argument_data[0]->start = g_source_index;
mrt->argument_data[1]->type = SUCCEEDED;
mrt->argument_data[1]->value = mrt->offset;
mrt->argument_data[2]->type = SUCCEEDED;
mrt->argument_data[2]->value = incbin_data->filter_size;
mrt->supplied_arguments = 3;
if (incbin_data->filter_size == 1) {
if (incbin_data->swap != 0) {
if (incbin_data->swap == 1) {
mrt->argument_data[0]->value = incbin_data->data[incbin_data->position + 1];
incbin_data->swap = 2;
}
else {
mrt->argument_data[0]->value = incbin_data->data[incbin_data->position];
incbin_data->position += 2;
incbin_data->swap = 1;
}
}
else
mrt->argument_data[0]->value = incbin_data->data[incbin_data->position++];
}
else if (incbin_data->filter_size == 2) {
int data1, data2;
data1 = incbin_data->data[incbin_data->position++];
data2 = incbin_data->data[incbin_data->position++];
if (incbin_data->swap != 0)
mrt->argument_data[0]->value = (data1 << 8) | data2;
else
mrt->argument_data[0]->value = (data2 << 8) | data1;
}
else if (incbin_data->filter_size == 3) {
int data1, data2, data3;
data1 = incbin_data->data[incbin_data->position++];
data2 = incbin_data->data[incbin_data->position++];
data3 = incbin_data->data[incbin_data->position++];
if (incbin_data->swap != 0)
mrt->argument_data[0]->value = (data1 << 16) | (data2 << 8) | data3;
else
mrt->argument_data[0]->value = (data3 << 16) | (data2 << 8) | data1;
}
else if (incbin_data->filter_size == 4) {
int data1, data2, data3, data4;
data1 = incbin_data->data[incbin_data->position++];
data2 = incbin_data->data[incbin_data->position++];
data3 = incbin_data->data[incbin_data->position++];
data4 = incbin_data->data[incbin_data->position++];
if (incbin_data->swap != 0)
mrt->argument_data[0]->value = (data1 << 24) | (data2 << 16) | (data3 << 8) | data4;
else
mrt->argument_data[0]->value = (data4 << 24) | (data3 << 16) | (data2 << 8) | data1;
}
incbin_data->left -= incbin_data->filter_size;
if (_macro_start(m, mrt, MACRO_CALLER_INCBIN, 3) == FAILED)
return FAILED;
return SUCCEEDED;
}
static int _macro_insert_bytes(char *name, int size) {
struct definition *d;
int error = NO;
if (hashmap_get(g_defines_map, "_out", (void*)&d) != MAP_OK)
hashmap_get(g_defines_map, "_OUT", (void*)&d);
if (d == NULL) {
print_error(ERROR_DIR, "No \"_OUT/_out\" defined, .%s takes its output from there.\n", name);
return FAILED;
}
if (size == 1) {
if (d->type == DEFINITION_TYPE_VALUE) {
if (d->value < -128 || d->value > 255) {
print_error(ERROR_DIR, ".%s expects 8-bit data, %d is out of range!\n", name, (int)d->value);
return FAILED;
}
fprintf(g_file_out_ptr, "d%d ", (int)d->value);
}
else if (d->type == DEFINITION_TYPE_STACK)
fprintf(g_file_out_ptr, "c%d ", (int)d->value);
else
error = YES;
}
else if (size == 2) {
if (d->type == DEFINITION_TYPE_VALUE) {
if (d->value < -32768 || d->value > 65535) {
print_error(ERROR_DIR, ".%s expects 16-bit data, %d is out of range!\n", name, (int)d->value);
return FAILED;
}
fprintf(g_file_out_ptr, "y%d ", (int)d->value);
}
else if (d->type == DEFINITION_TYPE_STACK)
fprintf(g_file_out_ptr, "C%d ", (int)d->value);
else
error = YES;
}
else if (size == 3) {
if (d->type == DEFINITION_TYPE_VALUE) {
if (d->value < -8388608 || d->value > 16777215) {
print_error(ERROR_DIR, ".%s expects 24-bit data, %d is out of range!\n", name, (int)d->value);
return FAILED;
}
fprintf(g_file_out_ptr, "z%d ", (int)d->value);
}
else if (d->type == DEFINITION_TYPE_STACK)
fprintf(g_file_out_ptr, "T%d ", (int)d->value);
else
error = YES;
}
else if (size == 4) {
if (d->type == DEFINITION_TYPE_VALUE)
fprintf(g_file_out_ptr, "u%d ", (int)d->value);
else if (d->type == DEFINITION_TYPE_STACK)
fprintf(g_file_out_ptr, "U%d ", (int)d->value);
else
error = YES;
}
if (error == YES) {
print_error(ERROR_DIR, ".%s cannot handle strings in \"_OUT/_out\".\n", name);
return FAILED;
}
return SUCCEEDED;
}
static int _macro_insert_word_db(char *name) {
struct definition *d;
if (hashmap_get(g_defines_map, "_out", (void*)&d) != MAP_OK)
hashmap_get(g_defines_map, "_OUT", (void*)&d);
if (d == NULL) {
print_error(ERROR_DIR, "No \"_OUT/_out\" defined, .%s takes its output from there.\n", name);
return FAILED;
}
if (d->type == DEFINITION_TYPE_VALUE) {
if (d->value < -32768 || d->value > 65535) {
print_error(ERROR_DIR, ".%s expects 16-bit data, %d is out of range!\n", name, (int)d->value);
return FAILED;
}
fprintf(g_file_out_ptr, "y%d ", (int)d->value);
/*
print_text(NO, ".DWM: VALUE: %d\n", (int)d->value);
*/
}
else if (d->type == DEFINITION_TYPE_STACK) {
fprintf(g_file_out_ptr, "C%d ", (int)d->value);
/*
print_text(NO, ".DWM: STACK: %d\n", (int)d->value);
*/
}
else {
print_error(ERROR_DIR, ".%s cannot handle strings in \"_OUT/_out\".\n", name);
return FAILED;
}
return SUCCEEDED;
}
static int _macro_insert_long_db(char *name) {
struct definition *d;
if (hashmap_get(g_defines_map, "_out", (void*)&d) != MAP_OK)
hashmap_get(g_defines_map, "_OUT", (void*)&d);
if (d == NULL) {
print_error(ERROR_DIR, "No \"_OUT/_out\" defined, .%s takes its output from there.\n", name);
return FAILED;
}
if (d->type == DEFINITION_TYPE_VALUE) {
if (d->value < -8388608 || d->value > 16777215) {
print_error(ERROR_DIR, ".%s expects 24-bit data, %d is out of range!\n", name, (int)d->value);
return FAILED;
}
fprintf(g_file_out_ptr, "z%d ", (int)d->value);
/*
print_text(NO, ".DLM: VALUE: %d\n", (int)d->value);
*/
}
else if (d->type == DEFINITION_TYPE_STACK) {
fprintf(g_file_out_ptr, "T%d ", (int)d->value);
/*
print_text(NO, ".DLM: STACK: %d\n", (int)d->value);
*/
}
else {
print_error(ERROR_DIR, ".%s cannot handle strings in \"_OUT/_out\".\n", name);
return FAILED;
}
return SUCCEEDED;
}
static int _macro_insert_double_dw(char *name) {
struct definition *d;
if (hashmap_get(g_defines_map, "_out", (void*)&d) != MAP_OK)
hashmap_get(g_defines_map, "_OUT", (void*)&d);
if (d == NULL) {
print_error(ERROR_DIR, "No \"_OUT/_out\" defined, .%s takes its output from there.\n", name);
return FAILED;
}
if (d->type == DEFINITION_TYPE_VALUE) {
if (d->value < -2147483648.0 || d->value > 2147483647.0) {
print_error(ERROR_DIR, ".%s expects 32-bit data, %d is out of range!\n", name, (int)d->value);
return FAILED;
}
fprintf(g_file_out_ptr, "u%d ", (int)d->value);
/*
print_text(NO, ".DLM: VALUE: %d\n", (int)d->value);
*/
}
else if (d->type == DEFINITION_TYPE_STACK) {
fprintf(g_file_out_ptr, "U%d ", (int)d->value);
/*
print_text(NO, ".DLM: STACK: %d\n", (int)d->value);
*/
}
else {
print_error(ERROR_DIR, ".%s cannot handle strings in \"_OUT/_out\".\n", name);
return FAILED;
}
return SUCCEEDED;
}
static struct structure* _get_structure(char *name) {
struct structure *s = g_structures_first;
char c1 = name[0];
while (s != NULL) {
if (c1 == s->name[0]) {
if (strcmp(name, s->name) == 0)
return s;
}
s = s->next;
}
return NULL;
}
int directive_define_def_equ(void);
static void _forget_macro_replacements(int move_index) {
if (s_replacement_index < 0)
return;
/* return what we replaced last time */
strncpy(&g_buffer[s_replacement_index], s_replacement_backup, s_replacement_length);
if (move_index == SUCCEEDED) {
/* relocate g_source_index */
g_source_index = s_replacement_index;
while (g_buffer[g_source_index++] != 0xA)
;
}
s_replacement_index = -1;
}
static int _run_macro_replacements(void) {
int i = 0, s, replaces = 0;
char c;
/* skip empty lines */
while (g_source_index < g_source_file_size) {
if (g_buffer[g_source_index] == 0xA) {
g_source_index++;
next_line();
continue;
}
break;
}
/* process white space */
s = g_source_index;
c = g_buffer[s++];
while (c == ' ') {
s_replacement_memory[i++] = c;
c = g_buffer[s++];
}
if (c == '.') {
/* directive! abort! */
return SUCCEEDED;
}
/* anything to replace on this line? */
s--;
while (i < (int)sizeof(s_replacement_memory)) {
c = g_buffer[s++];
/* don't replace inside strings */
if (c == '"') {
s_replacement_memory[i++] = c;
while (i < (int)sizeof(s_replacement_memory)) {
c = g_buffer[s++];
if (c == '\\' && g_buffer[s] == '"') {
s_replacement_memory[i++] = c;
s_replacement_memory[i++] = '"';
s++;
continue;
}
else if (c == '"') {
s_replacement_memory[i++] = c;
break;
}
else
s_replacement_memory[i++] = c;
}
continue;
}
if (c == '\\' && g_buffer[s] >= '0' && g_buffer[s] <= '9') {
struct macro_argument *ma;
int k, x = i, param = 0, add_quotes = NO;
/* parse param index */
s_replacement_memory[x++] = c;
for (k = 0; k < 4; k++) {
c = g_buffer[s++];
s_replacement_memory[x++] = c;
if (c >= '0' && c <= '9')
param = (param * 10) + (c - '0');
else {
s--;
break;
}
}
if (param > g_macro_runtime_current->supplied_arguments) {
print_error(ERROR_NUM, "Referencing argument number %d inside macro \"%s\". The macro has only %d arguments.\n", param, g_macro_runtime_current->macro->name, g_macro_runtime_current->supplied_arguments);
return FAILED;
}
if (param == 0) {
print_error(ERROR_NUM, "Referencing argument number %d inside macro \"%s\". Macro arguments are counted from 1.\n", param, g_macro_runtime_current->macro->name);
return FAILED;
}
ma = g_macro_runtime_current->argument_data[param - 1];
/* only string parameters can be used to replace references */
if (ma->type == INPUT_NUMBER_STRING) {
/* add quotes? */
if (g_buffer[s] == '.' &&
toupper((int)g_buffer[s+1]) == 'L' &&
toupper((int)g_buffer[s+2]) == 'E' &&
toupper((int)g_buffer[s+3]) == 'N' &&
toupper((int)g_buffer[s+4]) == 'G' &&
toupper((int)g_buffer[s+5]) == 'T' &&
toupper((int)g_buffer[s+6]) == 'H')
add_quotes = YES;
if (add_quotes == YES)
s_replacement_memory[i++] = '"';
strcpy(&s_replacement_memory[i], ma->string);
i += (int)strlen(ma->string);
if (add_quotes == YES)
s_replacement_memory[i++] = '"';
replaces++;
}
else {
/* no replacement -> continue as usual */
i = x;
s++;
}
}
else if (c == 0xA) {
s_replacement_memory[i++] = 0xA;
s_replacement_memory[i] = 0;
if (replaces > 0) {
/* backup and overwrite! */
i++;
strncpy(s_replacement_backup, &g_buffer[g_source_index], i);
if (g_source_index + i < g_source_file_size)
strcpy(&g_buffer[g_source_index], s_replacement_memory);
else {
print_error(ERROR_NUM, "Expected buffer overrun in _run_macro_replacements()! Please submit a bug report!\n");
return FAILED;
}
s_replacement_length = i;
s_replacement_index = g_source_index;
}
break;
}
else
s_replacement_memory[i++] = c;
}
if (i >= (int)sizeof(s_replacement_memory) - 1) {
print_error(ERROR_LOG, "Out of s_replacement_memory buffer size (buffer %d bytes, got %d bytes) in phase_1.c!\n", (int)sizeof(s_replacement_memory), i);
return FAILED;