-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
semantic.c
5835 lines (4904 loc) · 172 KB
/
semantic.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
/*
* Copyright (C) 2022 Clownacy
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "semantic.h"
#include <assert.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "clowncommon/clowncommon.h"
#include "dictionary.h"
#include "strcmpci.h"
#include "syntactic.h"
#define YY_NO_UNISTD_H
#include "lexical.h"
#define PROGRAM_COUNTER_OF_STATEMENT ",PROGRAM_COUNTER_OF_STATEMENT"
#define PROGRAM_COUNTER_OF_EXPRESSION ",PROGRAM_COUNTER_OF_EXPRESSION"
typedef ClownAssembler_TextInput TextInput;
typedef ClownAssembler_BinaryOutput BinaryOutput;
typedef ClownAssembler_TextOutput TextOutput;
typedef enum SymbolType
{
SYMBOL_CONSTANT,
SYMBOL_VARIABLE,
SYMBOL_MACRO,
SYMBOL_LABEL
} SymbolType;
typedef struct Location
{
struct Location *previous;
char *file_path;
unsigned long line_number;
} Location;
typedef struct FixUp
{
struct FixUp *next;
Statement statement;
unsigned long program_counter;
size_t output_position;
char *last_global_label;
char *source_line;
Location location;
char *label;
} FixUp;
typedef struct SourceLineListNode
{
struct SourceLineListNode *next;
char *source_line;
} SourceLineListNode;
typedef struct SourceLineList
{
SourceLineListNode *head;
SourceLineListNode *tail;
} SourceLineList;
typedef enum Mode
{
MODE_NORMAL,
MODE_REPT,
MODE_MACRO,
MODE_WHILE
} Mode;
typedef struct SemanticState
{
cc_bool success;
const BinaryOutput *output_callbacks;
const TextOutput *listing_callbacks;
const TextOutput *error_callbacks;
cc_bool equ_set_descope_local_labels;
cc_bool warnings_enabled;
size_t output_position;
cc_bool output_written_to;
unsigned long start_position;
unsigned long program_counter;
cc_bool obj_active;
unsigned long obj_delta;
FixUp *fix_up_list_head;
FixUp *fix_up_list_tail;
cc_bool fix_up_needed;
cc_bool doing_fix_up;
cc_bool doing_final_pass;
Dictionary_State dictionary;
char *last_global_label;
Location *location;
yyscan_t flex_state;
char line_buffer[1024];
const char *source_line;
unsigned int current_if_level;
unsigned int false_if_level;
cc_bool true_already_found;
cc_bool end;
unsigned int listing_counter;
Mode mode;
union
{
struct
{
unsigned long repetitions;
unsigned long line_number;
SourceLineList source_line_list;
} rept;
struct
{
char *name;
unsigned long line_number;
IdentifierList parameter_names;
SourceLineList source_line_list;
cc_bool is_short, uses_label;
} macro;
struct
{
Expression expression;
char *source_line;
unsigned long line_number;
SourceLineList source_line_list;
} while_statement;
} shared;
} SemanticState;
typedef struct Macro
{
cc_bool uses_label;
char *name;
IdentifierListNode *parameter_names;
SourceLineListNode *source_line_list_head;
unsigned int current_invocation;
} Macro;
/* Some forward declarations that are needed because some functions recurse into each other. */
static void AssembleFile(SemanticState *state, const TextInput *input_callbacks);
static void AssembleLine(SemanticState *state, const char *source_line);
/* Prevent errors when '__attribute__((format(printf, X, X)))' is not supported. */
/* GCC 3.2 is the earliest version of GCC of which I can find proof of supporting this. */
#if defined(__GNUC__) && defined(__GNUC_MINOR__) && (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 2))
#define ATTRIBUTE_PRINTF(a, b) __attribute__((format(printf, a, b)))
#else
#define ATTRIBUTE_PRINTF(a, b)
#endif
/* IO Callbacks */
static int TextInput_fgetc(const TextInput* const callbacks)
{
return callbacks->read_character((void*)callbacks->user_data);
}
static char* TextInput_fgets(char* const buffer, const size_t buffer_size, const TextInput* const callbacks)
{
return callbacks->read_line((void*)callbacks->user_data, buffer, buffer_size);
}
static cc_bool BinaryOutput_exists(const BinaryOutput* const callbacks)
{
return callbacks != NULL && callbacks->user_data != NULL;
}
static void BinaryOutput_fputc(const int character, const BinaryOutput* const callbacks)
{
callbacks->write_character((void*)callbacks->user_data, character);
}
static void BinaryOutput_fseek(SemanticState* const state, const BinaryOutput* const callbacks, const size_t position)
{
state->output_position = position;
callbacks->seek((void*)callbacks->user_data, position);
}
static void BinaryOutput_fwrite(const char* const buffer, const size_t size, const size_t count, const BinaryOutput* const callbacks)
{
callbacks->write_characters((void*)callbacks->user_data, buffer, size * count);
}
static cc_bool TextOutput_exists(const TextOutput* const callbacks)
{
return callbacks != NULL && callbacks->user_data != NULL;
}
static void TextOutput_vfprintf(const TextOutput* const callbacks, const char* const format, va_list args)
{
if (TextOutput_exists(callbacks))
callbacks->print_formatted((void*)callbacks->user_data, format, args);
}
ATTRIBUTE_PRINTF(2, 3) static void TextOutput_fprintf(const TextOutput* const callbacks, const char* const format, ...)
{
if (TextOutput_exists(callbacks))
{
va_list args;
va_start(args, format);
TextOutput_vfprintf(callbacks, format, args);
va_end(args);
}
}
static void TextOutput_fputs(const char* const string, const TextOutput* const callbacks)
{
if (TextOutput_exists(callbacks))
callbacks->write_string((void*)callbacks->user_data, string);
}
static void TextOutput_fputc(const int character, const TextOutput* const callbacks)
{
if (TextOutput_exists(callbacks))
callbacks->write_character((void*)callbacks->user_data, character);
}
static void WriteOutputByte(SemanticState* const state, const unsigned char byte)
{
state->output_written_to = cc_true;
++state->output_position;
BinaryOutput_fputc(byte, state->output_callbacks);
}
/* Default FILE-based IO callbacks */
static int ReadCharacter(void* const user_data)
{
const int value = fgetc((FILE*)user_data);
return value == EOF ? -1 : value;
}
static char* ReadLine(void* const user_data, char* const buffer, const size_t buffer_size)
{
return fgets(buffer, buffer_size, (FILE*)user_data);
}
static void Seek(void* const user_data, const size_t position)
{
fseek((FILE*)user_data, position, SEEK_SET);
}
static void WriteCharacter(void* const user_data, const int character)
{
fputc(character, (FILE*)user_data);
}
static void WriteCharacters(void* const user_data, const char* const characters, const size_t total_characters)
{
fwrite(characters, 1, total_characters, (FILE*)user_data);
}
static void WriteString(void* const user_data, const char* const string)
{
fputs(string, (FILE*)user_data);
}
static void PrintFormatted(void* const user_data, const char* const format, va_list args)
{
vfprintf((FILE*)user_data, format, args);
}
/* Other stuff */
static void ErrorMessageCommon(SemanticState *state)
{
const Location *location;
for (location = state->location; location != NULL; location = location->previous)
TextOutput_fprintf(state->error_callbacks, "\nOn line %lu of '%s'...", location->line_number, location->file_path != NULL ? location->file_path : "[No path given]");
TextOutput_fprintf(state->error_callbacks, "\n%s\n\n", state->source_line != NULL ? state->source_line : "[No source line]");
}
ATTRIBUTE_PRINTF(2, 3) static void SemanticWarning(SemanticState *state, const char *fmt, ...)
{
if (state->warnings_enabled)
{
va_list args;
TextOutput_fputs("Warning: ", state->error_callbacks);
va_start(args, fmt);
TextOutput_vfprintf(state->error_callbacks, fmt, args);
va_end(args);
ErrorMessageCommon(state);
}
}
ATTRIBUTE_PRINTF(2, 3) static void SemanticError(SemanticState *state, const char *fmt, ...)
{
va_list args;
TextOutput_fputs("Error: ", state->error_callbacks);
va_start(args, fmt);
TextOutput_vfprintf(state->error_callbacks, fmt, args);
va_end(args);
ErrorMessageCommon(state);
state->success = cc_false;
}
ATTRIBUTE_PRINTF(2, 3) static void InternalError(SemanticState *state, const char *fmt, ...)
{
va_list args;
TextOutput_fputs("Internal error: ", state->error_callbacks);
va_start(args, fmt);
TextOutput_vfprintf(state->error_callbacks, fmt, args);
va_end(args);
ErrorMessageCommon(state);
state->success = cc_false;
}
static void OutOfMemoryError(SemanticState *state)
{
TextOutput_fputs("Out-of-memory error.", state->error_callbacks);
ErrorMessageCommon(state);
state->success = cc_false;
}
void m68kasm_warning(void *scanner, Statement *statement, const char *message)
{
SemanticState *state = (SemanticState*)m68kasm_get_extra(scanner);
if (state->warnings_enabled)
{
(void)statement;
TextOutput_fprintf(state->error_callbacks, "Warning: %s", message);
ErrorMessageCommon(state);
}
}
void m68kasm_error(void *scanner, Statement *statement, const char *message)
{
SemanticState *state = (SemanticState*)m68kasm_get_extra(scanner);
(void)statement;
TextOutput_fprintf(state->error_callbacks, "Error: %s", message);
ErrorMessageCommon(state);
}
static void* MallocAndHandleError(SemanticState *state, size_t size)
{
void *memory = malloc(size);
if (memory == NULL)
OutOfMemoryError(state);
return memory;
}
static char* DuplicateStringWithLength(SemanticState *state, const char *string, const size_t length)
{
char *duplicated_string;
duplicated_string = (char*)MallocAndHandleError(state, length + 1);
if (duplicated_string != NULL)
{
memcpy(duplicated_string, string, length);
duplicated_string[length] = '\0';
}
return duplicated_string;
}
static char* DuplicateString(SemanticState *state, const char *string)
{
return DuplicateStringWithLength(state, string, strlen(string));
}
static unsigned int GetDecimalIntegerStringLength(unsigned int integer)
{
unsigned int string_length;
string_length = 1;
while (integer >= 10)
{
integer /= 10;
++string_length;
}
return string_length;
}
static unsigned int GetHexadecimalIntegerStringLength(unsigned int integer)
{
unsigned int string_length;
string_length = 1;
while (integer >= 0x10)
{
integer /= 0x10;
++string_length;
}
return string_length;
}
static char* DecimalIntegerToString(SemanticState *state, const unsigned int integer)
{
char* const integer_string = (char*)MallocAndHandleError(state, GetDecimalIntegerStringLength(integer) + 1);
if (integer_string != NULL)
sprintf(integer_string, "%u", integer);
return integer_string;
}
static char* HexadecimalIntegerToString(SemanticState *state, const unsigned int integer)
{
char* const integer_string = (char*)MallocAndHandleError(state, GetHexadecimalIntegerStringLength(integer) + 1);
if (integer_string != NULL)
sprintf(integer_string, "%X", integer);
return integer_string;
}
static char* ComputeUniqueMacroSuffix(SemanticState *state, Macro* const macro)
{
const unsigned int integer_string_length = GetDecimalIntegerStringLength(macro->current_invocation);
const unsigned int suffix_string_length = 1 + CC_MAX(3, integer_string_length);
char* const suffix = (char*)MallocAndHandleError(state, suffix_string_length + 1);
if (suffix != NULL)
sprintf(suffix, "_%03u", macro->current_invocation);
++macro->current_invocation;
return suffix;
}
static FILE* fopen_backslash(SemanticState *state, const char *path, const char *mode)
{
FILE *file;
char* const path_copy = DuplicateString(state, path);
if (path_copy == NULL)
{
file = NULL;
}
else
{
char *backslash;
backslash = path_copy;
while ((backslash = strchr(backslash, '\\')) != NULL)
*backslash++ = '/';
file = fopen(path_copy, mode);
free(path_copy);
}
return file;
}
static cc_bool FindMacroParameter(const char* const remaining_line, const char* const identifier, const char** const other_parameter_position_out, const char** const other_parameter_end_out)
{
const char *other_parameter_position;
const char *other_parameter_end;
other_parameter_position = strstr(remaining_line, identifier);
other_parameter_end = other_parameter_position + strlen(identifier);
/* Obviously bail if the identifier wasn't found. */
if (other_parameter_position != NULL)
{
/* If the identifier was in the middle of a larger block of letters/numbers, then don't replace it. */
/* (This is what AS does, and the Sonic 1 disassembly relies on this). */
if (other_parameter_position == remaining_line
|| ((other_parameter_position[-1] < 'a' || other_parameter_position[-1] > 'z')
&& (other_parameter_position[-1] < 'A' || other_parameter_position[-1] > 'Z')
&& (other_parameter_position[-1] < '0' || other_parameter_position[-1] > '9')))
{
if ((other_parameter_end[0] < 'a' || other_parameter_end[0] > 'z')
&& (other_parameter_end[0] < 'A' || other_parameter_end[0] > 'Z')
&& (other_parameter_end[0] < '0' || other_parameter_end[0] > '9'))
{
/* If the parameter is surrounded by backslashes, then expand the match to replace those too. */
/* asm68k allows backslashes before and after the parameter to separate them from surrounding characters. */
if (other_parameter_position != remaining_line && other_parameter_position[-1] == '\\')
{
--other_parameter_position;
if (other_parameter_end[0] == '\\')
++other_parameter_end;
}
*other_parameter_position_out = other_parameter_position;
*other_parameter_end_out = other_parameter_end;
return cc_true;
}
}
}
return cc_false;
}
/* TODO: Duplicate this and use function pointers. */
static void OutputByte(SemanticState *state, unsigned int byte)
{
/* Write to listing file. */
if (!state->doing_fix_up && TextOutput_exists(state->listing_callbacks))
{
/* We can only write up to 10 bytes. */
if (state->listing_counter <= 10)
{
if (state->listing_counter == 10)
{
/* After the last byte, we output a '+' to signal there's more. */
TextOutput_fputc('+', state->listing_callbacks);
}
else
{
if (state->listing_counter % 2 == 0)
TextOutput_fputc(' ', state->listing_callbacks);
TextOutput_fprintf(state->listing_callbacks, "%02X", byte);
}
++state->listing_counter;
}
}
/* Write to output file. */
WriteOutputByte(state, byte);
}
static char* ExpandIdentifier(SemanticState *state, const char* const identifier, const size_t identifier_length, size_t* const expanded_identifier_length)
{
char *expanded_identifier = NULL;
if (identifier_length != 0 && (identifier[0] == '@' || identifier[0] == '.'))
{
if (state->last_global_label == NULL)
{
expanded_identifier = DuplicateStringWithLength(state, identifier + 1, identifier_length - 1);
if (expanded_identifier_length != NULL)
*expanded_identifier_length = identifier_length - 1;
}
else
{
const size_t prefix_length = strlen(state->last_global_label);
const size_t suffix_length = identifier_length;
expanded_identifier = (char*)MallocAndHandleError(state, prefix_length + suffix_length + 1);
if (expanded_identifier != NULL)
{
memcpy(&expanded_identifier[0], state->last_global_label, prefix_length);
memcpy(&expanded_identifier[prefix_length], identifier, suffix_length);
expanded_identifier[prefix_length + suffix_length] = '\0';
if (expanded_identifier_length != NULL)
*expanded_identifier_length = prefix_length + suffix_length;
}
}
}
return expanded_identifier;
}
static Dictionary_Entry* LookupSymbol(SemanticState *state, const char *identifier, size_t identifier_length)
{
Dictionary_Entry *dictionary_entry;
size_t expanded_identifier_length;
char* const expanded_identifier = ExpandIdentifier(state, identifier, identifier_length, &expanded_identifier_length);
if (expanded_identifier != NULL)
{
identifier = expanded_identifier;
identifier_length = expanded_identifier_length;
}
dictionary_entry = Dictionary_LookUp(&state->dictionary, identifier, identifier_length);
free(expanded_identifier);
return dictionary_entry;
}
static Dictionary_Entry* LookupSymbolAndCreateIfNotExist(SemanticState *state, const char *identifier, size_t identifier_length)
{
Dictionary_Entry *dictionary_entry;
size_t expanded_identifier_length;
char* const expanded_identifier = ExpandIdentifier(state, identifier, identifier_length, &expanded_identifier_length);
if (expanded_identifier != NULL)
{
identifier = expanded_identifier;
identifier_length = expanded_identifier_length;
}
if (!Dictionary_LookUpAndCreateIfNotExist(&state->dictionary, identifier, identifier_length, &dictionary_entry))
{
OutOfMemoryError(state);
dictionary_entry = NULL;
}
free(expanded_identifier);
return dictionary_entry;
}
static Dictionary_Entry* CreateSymbol(SemanticState *state, const char *identifier, const size_t identifier_length)
{
Dictionary_Entry *dictionary_entry;
dictionary_entry = LookupSymbolAndCreateIfNotExist(state, identifier, identifier_length);
if (dictionary_entry != NULL && dictionary_entry->type != -1)
{
SemanticError(state, "Symbol '%s' cannot be redefined.", identifier);
dictionary_entry = NULL;
}
return dictionary_entry;
}
static cc_bool GetSymbolInteger(SemanticState *state, const char *identifier, const size_t identifier_length, const cc_bool must_evaluate_on_first_pass, unsigned long* const value)
{
cc_bool success = cc_false;
Dictionary_Entry *dictionary_entry;
dictionary_entry = LookupSymbol(state, identifier, identifier_length);
if (dictionary_entry == NULL || dictionary_entry->type == -1)
{
if (must_evaluate_on_first_pass)
SemanticError(state, "Symbol '%s' must be evaluable on first pass.", identifier);
else if (state->doing_final_pass)
SemanticError(state, "Symbol '%s' does not exist.", identifier);
else
state->fix_up_needed = cc_true;
}
else if (dictionary_entry->type != SYMBOL_LABEL && dictionary_entry->type != SYMBOL_CONSTANT && dictionary_entry->type != SYMBOL_VARIABLE)
{
SemanticError(state, "Symbol '%s' is not a label, constant, or variable.", identifier);
}
else
{
success = cc_true;
*value = dictionary_entry->shared.unsigned_long;
}
return success;
}
static void AddToSourceLineList(SemanticState *state, SourceLineList *source_line_list, const char *source_line)
{
const size_t source_line_length = strlen(source_line);
SourceLineListNode* const source_line_list_node = (SourceLineListNode*)MallocAndHandleError(state, sizeof(SourceLineListNode) + source_line_length + 1);
if (source_line_list_node != NULL)
{
/* Append to the end of the list. */
if (source_line_list->tail != NULL)
source_line_list->tail->next = source_line_list_node;
else
source_line_list->head = source_line_list_node;
source_line_list->tail = source_line_list_node;
/* Initialise the list node. */
source_line_list_node->next = NULL;
source_line_list_node->source_line = (char*)(source_line_list_node + 1);
memcpy(source_line_list_node->source_line, source_line, source_line_length + 1);
}
}
static void FreeSourceLineList(SourceLineListNode *source_line_list_head)
{
SourceLineListNode *source_line_list_node;
source_line_list_node = source_line_list_head;
while (source_line_list_node != NULL)
{
SourceLineListNode *next_source_line_list_node = source_line_list_node->next;
free(source_line_list_node);
source_line_list_node = next_source_line_list_node;
}
}
static cc_bool ResolveExpression(SemanticState *state, Expression *expression, unsigned long *value, cc_bool fold)
{
/* TODO - Maybe we should be doing signed arithmetic... that's what ProASM does, after all. */
cc_bool success = cc_true;
switch (expression->type)
{
case EXPRESSION_SUBTRACT:
case EXPRESSION_ADD:
case EXPRESSION_MULTIPLY:
case EXPRESSION_DIVIDE:
case EXPRESSION_MODULO:
case EXPRESSION_LOGICAL_OR:
case EXPRESSION_LOGICAL_AND:
case EXPRESSION_BITWISE_OR:
case EXPRESSION_BITWISE_XOR:
case EXPRESSION_BITWISE_AND:
case EXPRESSION_EQUALITY:
case EXPRESSION_INEQUALITY:
case EXPRESSION_LESS_THAN:
case EXPRESSION_LESS_OR_EQUAL:
case EXPRESSION_MORE_THAN:
case EXPRESSION_MORE_OR_EQUAL:
case EXPRESSION_LEFT_SHIFT:
case EXPRESSION_RIGHT_SHIFT:
{
unsigned long left_value;
unsigned long right_value;
/* Resolve both of these separately so that they are each properly hardcoded (C short-circuiting could cause the second call to never occur). */
if (!ResolveExpression(state, &expression->shared.subexpressions[0], &left_value, fold))
success = cc_false;
if (!ResolveExpression(state, &expression->shared.subexpressions[1], &right_value, fold))
success = cc_false;
if (success)
{
switch (expression->type)
{
case EXPRESSION_NUMBER:
case EXPRESSION_IDENTIFIER:
case EXPRESSION_STRING:
case EXPRESSION_PROGRAM_COUNTER_OF_STATEMENT:
case EXPRESSION_PROGRAM_COUNTER_OF_EXPRESSION:
case EXPRESSION_STRLEN:
case EXPRESSION_DEF:
case EXPRESSION_NEGATE:
case EXPRESSION_BITWISE_NOT:
case EXPRESSION_LOGICAL_NOT:
/* This code should never be ran. */
break;
case EXPRESSION_SUBTRACT:
*value = left_value - right_value;
break;
case EXPRESSION_ADD:
*value = left_value + right_value;
break;
case EXPRESSION_MULTIPLY:
*value = left_value * right_value;
break;
case EXPRESSION_DIVIDE:
*value = left_value / right_value;
break;
case EXPRESSION_MODULO:
*value = left_value % right_value;
break;
case EXPRESSION_LOGICAL_OR:
*value = left_value != 0 || right_value != 0 ? -1 : 0;
break;
case EXPRESSION_LOGICAL_AND:
*value = left_value != 0 && right_value != 0 ? -1 : 0;
break;
case EXPRESSION_BITWISE_OR:
*value = left_value | right_value;
break;
case EXPRESSION_BITWISE_XOR:
*value = left_value ^ right_value;
break;
case EXPRESSION_BITWISE_AND:
*value = left_value & right_value;
break;
case EXPRESSION_EQUALITY:
*value = left_value == right_value ? -1 : 0;
break;
case EXPRESSION_INEQUALITY:
*value = left_value != right_value ? -1 : 0;
break;
case EXPRESSION_LESS_THAN:
*value = left_value < right_value ? -1 : 0;
break;
case EXPRESSION_LESS_OR_EQUAL:
*value = left_value <= right_value ? -1 : 0;
break;
case EXPRESSION_MORE_THAN:
*value = left_value > right_value ? -1 : 0;
break;
case EXPRESSION_MORE_OR_EQUAL:
*value = left_value >= right_value ? -1 : 0;
break;
case EXPRESSION_LEFT_SHIFT:
*value = left_value << right_value;
break;
case EXPRESSION_RIGHT_SHIFT:
*value = left_value >> right_value;
break;
}
/* Prevent 'bleeding' out of the 68k's 32-bit range. */
*value &= 0xFFFFFFFF;
}
break;
}
case EXPRESSION_NEGATE:
case EXPRESSION_BITWISE_NOT:
case EXPRESSION_LOGICAL_NOT:
if (!ResolveExpression(state, expression->shared.subexpressions, value, fold))
{
success = cc_false;
}
else
{
switch (expression->type)
{
case EXPRESSION_NUMBER:
case EXPRESSION_IDENTIFIER:
case EXPRESSION_STRING:
case EXPRESSION_PROGRAM_COUNTER_OF_STATEMENT:
case EXPRESSION_PROGRAM_COUNTER_OF_EXPRESSION:
case EXPRESSION_STRLEN:
case EXPRESSION_DEF:
case EXPRESSION_SUBTRACT:
case EXPRESSION_ADD:
case EXPRESSION_MULTIPLY:
case EXPRESSION_DIVIDE:
case EXPRESSION_MODULO:
case EXPRESSION_LOGICAL_OR:
case EXPRESSION_LOGICAL_AND:
case EXPRESSION_BITWISE_OR:
case EXPRESSION_BITWISE_XOR:
case EXPRESSION_BITWISE_AND:
case EXPRESSION_EQUALITY:
case EXPRESSION_INEQUALITY:
case EXPRESSION_LESS_THAN:
case EXPRESSION_LESS_OR_EQUAL:
case EXPRESSION_MORE_THAN:
case EXPRESSION_MORE_OR_EQUAL:
case EXPRESSION_LEFT_SHIFT:
case EXPRESSION_RIGHT_SHIFT:
/* This code should never be ran. */
break;
case EXPRESSION_NEGATE:
*value = 0 - *value;
break;
case EXPRESSION_BITWISE_NOT:
*value = ~*value;
break;
case EXPRESSION_LOGICAL_NOT:
*value = *value == 0 ? -1 : 0;
break;
}
/* Prevent 'bleeding' out of the 68k's 32-bit range. */
*value &= 0xFFFFFFFF;
}
break;
case EXPRESSION_NUMBER:
*value = expression->shared.unsigned_long;
break;
case EXPRESSION_IDENTIFIER:
if (!GetSymbolInteger(state, expression->shared.string, strlen(expression->shared.string), cc_false, value))
success = cc_false;
break;
case EXPRESSION_STRING:
{
const size_t length = strlen(expression->shared.string);
if (length > 4)
{
success = cc_false;
SemanticError(state, "Character literals cannot be more than 4 characters long.");
}
else
{
size_t i;
*value = 0;
for (i = 0; i < length; ++i)
{
*value <<= 8;
*value |= expression->shared.string[i];
}
}
break;
}
case EXPRESSION_PROGRAM_COUNTER_OF_STATEMENT:
*value = LookupSymbol(state, PROGRAM_COUNTER_OF_STATEMENT, sizeof(PROGRAM_COUNTER_OF_STATEMENT) - 1)->shared.unsigned_long;
break;
case EXPRESSION_PROGRAM_COUNTER_OF_EXPRESSION:
*value = LookupSymbol(state, PROGRAM_COUNTER_OF_EXPRESSION, sizeof(PROGRAM_COUNTER_OF_EXPRESSION) - 1)->shared.unsigned_long;
break;
case EXPRESSION_STRLEN:
*value = strlen(expression->shared.string);
break;
case EXPRESSION_DEF:
*value = LookupSymbol(state, expression->shared.string, strlen(expression->shared.string)) != NULL;
break;
}
/* Now that we have resolved the value, let's hardcode it here so that we don't ever have to calculate it again. */
/* This is especially useful for fix-ups, which may otherwise depend on identifiers that no longer exist at the time is value is resolved again. */
if (success && fold)
{
switch (expression->type)
{
case EXPRESSION_SUBTRACT:
case EXPRESSION_ADD:
case EXPRESSION_MULTIPLY:
case EXPRESSION_DIVIDE:
case EXPRESSION_MODULO:
case EXPRESSION_LOGICAL_OR:
case EXPRESSION_LOGICAL_AND:
case EXPRESSION_BITWISE_OR:
case EXPRESSION_BITWISE_XOR:
case EXPRESSION_BITWISE_AND:
case EXPRESSION_EQUALITY:
case EXPRESSION_INEQUALITY:
case EXPRESSION_LESS_THAN:
case EXPRESSION_LESS_OR_EQUAL:
case EXPRESSION_MORE_THAN:
case EXPRESSION_MORE_OR_EQUAL:
case EXPRESSION_LEFT_SHIFT:
case EXPRESSION_RIGHT_SHIFT:
case EXPRESSION_NEGATE:
case EXPRESSION_BITWISE_NOT:
case EXPRESSION_LOGICAL_NOT:
free(expression->shared.subexpressions);
break;