-
Notifications
You must be signed in to change notification settings - Fork 4
/
assembler.cpp
1524 lines (1269 loc) · 36 KB
/
assembler.cpp
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 is the assembler part of the toolchain for WRAMP assembly
#
# Copyright (C) 2019 The University of Waikato, Hamilton, New Zealand.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
########################################################################
*/
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include "instructions.h"
#include "object_file.h"
using namespace std;
int num_globals = 0, num_local_refs = 0, num_unresolved = 0;
char *input_filename = NULL;
int current_line = 1;
const int max_line = 10000;
const int max_string = 10000;
unsigned int address[NUM_SEGMENTS];
seg_type current_segment;
const int max_label_length = 30;
char string_buffer[max_string];
struct label_entry
{
char name[max_label_length];
int address;
seg_type segment;
bool resolved;
bool global;
label_entry *next;
int name_ptr;
int line;
};
// GPR table
reg_type GPR_table[] = {
{"zero", 0},
{"sp", 14},
{"ra", 15},
{NULL, 0}};
// SPR table
reg_type SPR_table[] = {
{"cctrl", 4},
{"estat", 5},
{"icount", 6},
{"ccount", 7},
{"evec", 8},
{"ear", 9},
{"esp", 10},
{"ers", 11},
{"ptable", 12},
{"rbase", 13},
{NULL, 0}};
enum label_descriptor
{
absolute,
relative,
immediate
};
struct memory_entry
{
int line; // The line in the program file that caused this entry
unsigned int address; // The address that this will lie
unsigned int data; // The data
char label[max_label_length]; // The name of a label this entry needs resolved
label_descriptor reference_type; // The value we want from this label when resolved
memory_entry *next;
};
label_entry *label_list = NULL;
memory_entry *segment[NUM_SEGMENTS], *segment_end[NUM_SEGMENTS];
char symbol_buffer[max_label_length];
void init()
{
for (int i = 0; i < NUM_SEGMENTS; i++)
{
segment[i] = NULL;
segment_end[i] = NULL;
address[i] = 0;
}
current_segment = TEXT;
current_line = 1;
num_globals = 0;
num_local_refs = 0;
num_unresolved = 0;
}
// Remove all dynamically allocated data structures
void cleanup()
{
while (label_list != NULL)
{
label_entry *temp = label_list->next;
delete label_list;
label_list = temp;
}
for (int i = 0; i < NUM_SEGMENTS; i++)
{
while (segment[i] != NULL)
{
memory_entry *temp = segment[i]->next;
delete segment[i];
segment[i] = temp;
}
}
}
void bailout()
{
cleanup();
exit(1);
}
// Display an error message and die
void error(char *filename, int line_no, char *msg, char *param)
{
if (filename)
cerr << filename << ":" << current_line << ": ";
cerr << "ERROR: " << msg;
if (param)
cerr << "`" << param << "'";
cerr << endl;
bailout();
}
// Display an warning message
void warning(char *filename, int line_no, char *msg, char *param)
{
if (filename)
cerr << filename << ":" << current_line << ": ";
cerr << "WARNING: " << msg;
if (param)
cerr << "`" << param << "'";
cerr << endl;
}
void chew_whitespace(char *&ptr)
{
while (ptr != NULL && isspace(*ptr))
ptr++;
if (ptr != NULL && *ptr == '#')
*ptr = '\0';
}
int still_more(char *&ptr)
{
chew_whitespace(ptr);
return (ptr != NULL && *ptr != '\0' && !isspace(*ptr));
}
// This function will parse a symbol, returning true if one is found, or false otherwise
bool parse_symbol(char *&ptr, char *buffer)
{
int char_count = 0;
// Chew any leading whitespace
chew_whitespace(ptr);
// First character must be a letter or an underscore.
if (!(isalpha(*ptr) || *ptr == '_'))
return false;
do
{
*buffer++ = *ptr++;
char_count++;
if (char_count == max_label_length)
error(input_filename, current_line, "Label too long.", NULL);
} while (isalnum(*ptr) || *ptr == '_' || *ptr == '.');
*buffer++ = '\0';
return true;
}
// This searches for a reference to a label, creating a new entry
// if none is found
label_entry *get_label(char *name)
{
// Check for a label that is too long
int len = strlen(name);
if (isdigit(*name))
error(input_filename, current_line, "Label must not begin with a digit.", NULL);
if (strchr(name, ' ') != NULL)
error(input_filename, current_line, "Space in label.", NULL);
if (len >= max_label_length)
error(input_filename, current_line, "Label too long.", NULL);
label_entry *temp = label_list;
// Check for the label already existing
while (temp != NULL)
{
if (strcmp(temp->name, name) == 0)
return temp;
temp = temp->next;
}
temp = new label_entry;
temp->next = label_list;
temp->resolved = false;
temp->global = false;
strcpy(temp->name, name);
// cerr << "New label : '" << name << "'\n";
label_list = temp;
return label_list;
}
void clean_up_line(char *&buf)
{
char *temp;
// Remove any stray newline or carriage return characters
temp = buf;
while (*temp != '\0')
{
if (*temp == '\r' || *temp == '\n')
{
*temp = '\0';
break;
}
temp++;
}
// Change tabs into spaces
while ((temp = strchr(buf, '\t')) != NULL)
*temp = ' ';
}
void check_labels(char *&buf)
{
char *temp;
char *comment;
comment = strchr(buf, '#');
// Check for a label on this line
if ((temp = strchr(buf, ':')) != NULL)
{
// Check to see if the colon is after a comment marker
if (comment != NULL && temp > comment)
return;
// Check to see if the colon is within a string
if ((strchr(buf, '\"') != NULL) && (strchr(buf, '\"') < temp))
return;
// Check to see if the colon is a character constant
if (temp > buf && *(temp - 1) == '\'' && *(temp + 1) == '\'')
return;
// Extract a symbol from the start of this line.
if (parse_symbol(buf, symbol_buffer) == false)
error(input_filename, current_line, "Label expected on line (because of ':').", NULL);
chew_whitespace(buf);
if (*buf != ':')
error(input_filename, current_line, "Badly formed label.", NULL);
// Move past the colon
buf++;
// Register this label
label_entry *label = get_label(symbol_buffer);
if ((strcmp(label->name, "bss_size") == 0) ||
(strcmp(label->name, "data_size") == 0) ||
(strcmp(label->name, "text_size") == 0))
{
error(input_filename, current_line, "Illegal label name : ", label->name);
}
// If this label has already been resolved then we have a duplicate label
if (label->resolved == true)
error(input_filename, current_line, "Duplicate label : ", symbol_buffer);
// Fill in the values for this label
label->segment = current_segment;
label->address = address[current_segment];
label->resolved = true;
}
}
// This function creates a new empty program entry in the specified segment
memory_entry *add_entry(seg_type seg_no, int current_line)
{
memory_entry *new_entry;
if (segment[seg_no] == NULL && segment_end[seg_no] == NULL)
{
new_entry = (segment[seg_no] = (segment_end[seg_no] = new memory_entry));
}
else
{
segment_end[seg_no]->next = (new_entry = new memory_entry);
segment_end[seg_no] = new_entry;
}
if (new_entry == NULL)
error(input_filename, current_line, "Assembler error, could not allocate memory.", NULL);
new_entry->next = NULL;
new_entry->line = current_line;
new_entry->address = address[seg_no];
new_entry->label[0] = 0;
new_entry->data = 0;
// Increment the address counter for this segment
address[seg_no]++;
return new_entry;
}
void decode_char(char *&buf, unsigned char &chr)
{
if (*buf == '\\')
{
buf++;
switch (*buf)
{
case 'n':
chr = '\n';
break;
case 't':
chr = '\t';
break;
case 'r':
chr = '\r';
break;
case 'a':
chr = '\a';
break;
case '\\':
chr = '\\';
break;
case '\"':
chr = '\"';
break;
case '\'':
chr = '\'';
break;
case '0':
chr = '\0';
buf++;
while (isdigit(*buf))
{
chr = (chr << 3) + (*buf - '0');
buf++;
}
buf--;
break;
default:
error(input_filename, current_line, "Bad character escape sequence.", NULL);
break;
}
buf++;
return;
}
chr = *buf;
buf++;
}
int decode_GPR(char *&ptr)
{
// We must scan until we get an end-of-line ('\0')
// or until we see a comma, or until the register is too large
chew_whitespace(ptr);
int reg_no = 0;
if (tolower(*ptr) != 'r' && *ptr != '$')
error(input_filename, current_line, "Register identifier expected.", NULL);
ptr++;
if (*ptr >= '0' && *ptr <= '9')
{
// Register specified by number
do
{
reg_no = reg_no * 10;
reg_no += *ptr - '0';
if (reg_no >= 16)
error(input_filename, current_line, "Register identifier expected.", NULL);
ptr++;
} while (*ptr >= '0' && *ptr <= '9');
}
else if (tolower(*(ptr - 1)) == 'r')
{
error(input_filename, current_line, "Register identifier expected.", NULL);
}
else
{
// Here we are expecting a word specifier (as in $zero, or $istat)
char buffer[20] = {0};
int char_count = 0;
while (isalnum(*ptr) && char_count < 9)
{
buffer[char_count] = tolower(*ptr);
char_count++;
ptr++;
}
// chew_whitespace(ptr);
// if (*ptr != ',' && *ptr != '\0' && *ptr != ')')
// error(input_filename, current_line, "Register identifier expected", NULL);
buffer[char_count] = '\0';
int index = 0;
// Now we must match our string with a register
while (GPR_table[index].reg_name != NULL && (strcmp(GPR_table[index].reg_name, buffer) != 0))
index++;
if (GPR_table[index].reg_name == NULL)
error(input_filename, current_line, "Register identifier expected.", NULL);
reg_no = GPR_table[index].reg_num;
}
// Back up past the last (non-register) character we read
// ptr--;
return reg_no;
}
int decode_SPR(char *&ptr)
{
// We must scan until we get an end-of-line ('\0')
// or until we see a comma, or until the register is too large
chew_whitespace(ptr);
int reg_no = 0;
if (*ptr != '$')
error(input_filename, current_line, "Register identifier expected.", NULL);
ptr++;
// Here we are expecting a word specifier (as in $zero, or $istat)
char buffer[20] = {0};
int char_count = 0;
while (isalnum(*ptr) && char_count < 9)
{
buffer[char_count] = tolower(*ptr);
char_count++;
ptr++;
}
// if (*ptr != ',' && *ptr != '\0' && *ptr != ')')
// error(input_filename, current_line, "Special register identifier expected", NULL);
buffer[char_count] = '\0';
int index = 0;
// Now we must match our string with a register
while (SPR_table[index].reg_name != NULL && (strcmp(SPR_table[index].reg_name, buffer) != 0))
index++;
if (SPR_table[index].reg_name == NULL)
error(input_filename, current_line, "Special register identifier expected.", NULL);
reg_no = SPR_table[index].reg_num;
// Back up past the last (non-register) character we read
// ptr--;
return reg_no;
}
unsigned int parse_address(char *&ptr)
{
unsigned int value = 0;
if (ptr == NULL)
error(input_filename, current_line, "Numeric value expected.", NULL);
chew_whitespace(ptr);
// If this is hexadecimal
if (*ptr == '0' && tolower(*(ptr + 1)) == 'x')
{
ptr += 2;
if (!isxdigit(*ptr))
error(input_filename, current_line, "Numeric value expected.", NULL);
while (isxdigit(*ptr))
{
value = value << 4;
if (*ptr >= '0' && *ptr <= '9')
value += *ptr - '0';
else
value += tolower(*ptr) - 'a' + 10;
ptr++;
}
}
else
error(input_filename, current_line, "Hexadecimal address expected.", NULL);
if (value > 0xfffff)
error(input_filename, current_line, "Constant too large.", NULL);
return value;
}
unsigned int parse_word(char *&ptr)
{
unsigned int value = 0;
if (ptr == NULL)
error(input_filename, current_line, "Numeric value expected.", NULL);
chew_whitespace(ptr);
// If this is hexadecimal
if (*ptr == '0' && tolower(*(ptr + 1)) == 'x')
{
ptr += 2;
if (!isxdigit(*ptr))
error(input_filename, current_line, "Numeric value expected.", NULL);
while (isxdigit(*ptr))
{
if (value > 0x0fffffff)
error(input_filename, current_line, "Constant too large.", NULL);
value = value << 4;
if (*ptr >= '0' && *ptr <= '9')
value += *ptr - '0';
else
value += tolower(*ptr) - 'a' + 10;
ptr++;
}
}
else
{
bool negative = false;
if (*ptr == '-')
{
negative = true;
ptr++;
}
if (!isdigit(*ptr))
error(input_filename, current_line, "Numeric value expected.", NULL);
while (isdigit(*ptr))
{
if (value >= 0x19999999)
error(input_filename, current_line, "Constant too large.", NULL);
value = value * 10;
value += *ptr - '0';
ptr++;
}
if (negative == true)
{
if (value & 0x80000000)
error(input_filename, current_line, "Constant too large.", NULL);
value = ((unsigned)-((signed)value));
}
}
// ptr--;
return value;
}
unsigned int parse_half(char *&ptr)
{
unsigned int value = 0;
if (ptr == NULL)
error(input_filename, current_line, "Numeric value expected.", NULL);
chew_whitespace(ptr);
// If this is a character
if (*ptr == '\'')
{
if ((*(ptr + 1) == '\\' && *(ptr + 3) != '\'') ||
(*(ptr + 1) != '\\' && *(ptr + 2) != '\''))
{
error(input_filename, current_line, "Bad character constant.", NULL);
}
unsigned char chr = 0;
// Skip past the opening quote
ptr++;
decode_char(ptr, chr);
// Skip past the closing quote
ptr++;
value = chr;
}
// If this is hexadecimal
else if (*ptr == '0' && tolower(*(ptr + 1)) == 'x')
{
ptr += 2;
if (!isxdigit(*ptr))
error(input_filename, current_line, "Numeric value expected.", NULL);
while (isxdigit(*ptr))
{
value = value << 4;
if (*ptr >= '0' && *ptr <= '9')
value += *ptr - '0';
else
value += tolower(*ptr) - 'a' + 10;
ptr++;
}
}
else
{
bool negative = false;
if (*ptr == '-')
{
negative = true;
ptr++;
}
if (!isdigit(*ptr))
error(input_filename, current_line, "Numeric value expected.", NULL);
while (isdigit(*ptr))
{
value = value * 10;
value += *ptr - '0';
ptr++;
}
if (negative == true)
value = ((unsigned)-((signed)value)) & 0xffff;
}
if (value > 0xffff)
error(input_filename, current_line, "Constant too large.", NULL);
return value;
}
int parse_string(char *&ptr, char *buffer)
{
int i = 0;
if (ptr == NULL)
error(input_filename, current_line, "String expected.", NULL);
chew_whitespace(ptr);
if (*ptr++ != '\"')
error(input_filename, current_line, "String expected.", NULL);
while (*ptr != '\"')
{
unsigned char tmp;
if (*ptr == '\0')
error(input_filename, current_line, "Unterminated string.", NULL);
decode_char(ptr, tmp);
buffer[i] = tmp;
i++;
if (i == max_string)
error(input_filename, current_line, "String exceeds maximum length.", NULL);
}
ptr++;
return i;
}
void parse_line(char *buf)
{
char *temp;
// cerr << "parse_line : " << buf << endl;
clean_up_line(buf);
chew_whitespace(buf);
check_labels(buf);
// cerr << "tohere";
// Remove leading spaces
while (isspace(*buf))
buf++;
// Get rid of obvious comments
if (*buf == '#')
*buf = '\0';
// Empty line now?
if (*buf == '\0')
return;
// Here we can process the instruction
char *mnemonic = buf;
char *operands = strchr(buf, ' ');
if (operands != NULL)
*(operands++) = '\0';
chew_whitespace(operands);
int insn_num = 0;
unsigned int offset;
// Convert the mnemonic to lower case
temp = mnemonic;
while (*temp != '\0')
{
*(temp) = tolower(*temp);
temp++;
}
// cerr << "up to here...";
// Here we look up the mnemonic in our table.
while (insn_table[insn_num].mnemonic != NULL && (strcmp(insn_table[insn_num].mnemonic, mnemonic) != 0))
{
insn_num++;
}
// cerr << "now here...";
if (insn_table[insn_num].mnemonic == NULL)
{
// cerr << "mnemonic is : " << mnemonic << endl;
error(input_filename, current_line, "Bad mnemonic : ", mnemonic);
}
// cerr << "then here...";
// Handle assembler directives
if (insn_table[insn_num].type == DIRECTIVE)
{
if (strcmp(mnemonic, ".word") == 0)
{
if (current_segment != BSS)
{
if (operands == NULL)
{
error(input_filename, current_line, "Expecting value or label.", NULL);
}
chew_whitespace(operands);
if (*operands == '\0')
error(input_filename, current_line, "Expecting value or label.", NULL);
if (strchr(operands, '"') != NULL)
{
error(input_filename, current_line, "Expecting value or label.", NULL);
}
do
{
memory_entry *new_entry = add_entry(current_segment, current_line);
if (isdigit(*operands) || *operands == '-')
{
new_entry->data = parse_word(operands);
}
else
{
if (parse_symbol(operands, symbol_buffer))
{
// This word holds the value of a symbol
new_entry->reference_type = absolute;
strcpy(new_entry->label, symbol_buffer);
new_entry->data = 0;
}
else
{
// This word could be a character in '' or otherwise
unsigned char chr;
if (*operands == '\'')
{
// Skip the opening quote
operands++;
decode_char(operands, chr);
// Check and skip the ending quote
if (*operands != '\'')
{
error(input_filename, current_line, "Bad character constant.", NULL);
}
operands++;
new_entry->data = chr;
}
else
{
// Otherwise is invalid.
error(input_filename, current_line, "Expecting value or label.", NULL);
}
}
}
chew_whitespace(operands);
if (*operands == ',')
operands++;
chew_whitespace(operands);
} while (*operands != '\0');
if (*(operands - 1) == ',')
error(input_filename, current_line, "Expecting value or label.", NULL);
}
else
{
memory_entry *new_entry = add_entry(current_segment, current_line);
if (operands != NULL && operands[0] != 0)
{
// Attempt to initialise data in the bss segment
warning(input_filename, current_line, "Ignoring initial value in .bss segment.", NULL);
}
new_entry->data = 0;
}
}
else if (strcmp(mnemonic, ".space") == 0)
{
if (current_segment != BSS)
{
error(input_filename, current_line, "Can only use .space directive in .bss segment.", NULL);
}
// Possible here we should just parse an int (not allow hex values)
int num_words = parse_word(operands);
// Add the appropriate number of data items
for (int i = 0; i < num_words; i++)
add_entry(current_segment, current_line);
// cerr << "operands = '" << operands << "'\n";
if (still_more(operands))
error(input_filename, current_line, "Additional text after directive argument.", NULL);
}
else if (strcmp(mnemonic, ".asciiz") == 0 || strcmp(mnemonic, ".ascii") == 0)
{
if (current_segment == BSS)
{
error(input_filename, current_line, "Cannot specify a string in .bss segment.", NULL);
}
memory_entry *new_entry;
//cerr << "calling parse_string : " << operands << endl;
int len = parse_string(operands, string_buffer);
for (int i = 0; i < len; i++)
{
// Add the character
new_entry = add_entry(current_segment, current_line);
new_entry->data = string_buffer[i];
}
// cerr << "left : " << operands << endl;
if (still_more(operands))
error(input_filename, current_line, "Additional text after string.", NULL);
// Add the null terminator
if (strcmp(mnemonic, ".asciiz") == 0)
{
new_entry = add_entry(current_segment, current_line);
new_entry->data = 0;
}
}
else if (strcmp(mnemonic, ".equ") == 0)
{
if (operands == NULL)
{
error(input_filename, current_line, ".equ directive must specify a symbol name and value.", NULL);
}
if (parse_symbol(operands, symbol_buffer) == false)
error(input_filename, current_line, "Expected symbol name.", NULL);
chew_whitespace(operands);
if (*operands++ != ',')
error(input_filename, current_line, "Expected ',' after symbol name.", NULL);
// Make the specified symbol
label_entry *new_label = get_label(symbol_buffer);
if (new_label->resolved == true)
error(input_filename, current_line, "Duplicate label : ", symbol_buffer);
offset = parse_word(operands);
if (offset > 0x000fffff){
error(input_filename, current_line, "Constant is too large", NULL);
}
new_label->address = offset;
new_label->line = current_line;
new_label->resolved = true;
new_label->segment = NONE;
if (still_more(operands))
error(input_filename, current_line, "Additional text after directive arguments.", NULL);
}
else if (strcmp(mnemonic, ".data") == 0)
{
current_segment = DATA;
if (still_more(operands))
error(input_filename, current_line, "Additional text after directive.", NULL);
}
else if (strcmp(mnemonic, ".text") == 0)
{
current_segment = TEXT;
if (still_more(operands))
error(input_filename, current_line, "Additional text after directive.", NULL);
}
else if (strcmp(mnemonic, ".bss") == 0)
{
current_segment = BSS;
if (still_more(operands))
error(input_filename, current_line, "Additional text after directive.", NULL);
}
else if (strcmp(mnemonic, ".global") == 0)
{
if (operands == NULL || parse_symbol(operands, symbol_buffer) == false)
error(input_filename, current_line, "Global directive must specify a label.", NULL);
// Make the specified label global
label_entry *temp = get_label(symbol_buffer);
if (temp->global == false)
{
// Increment the global count
num_globals++;
temp->global = true;
temp->line = current_line;
}
if (still_more(operands))
error(input_filename, current_line, "Additional text after directive arguments.", NULL);
}
else if (strcmp(mnemonic, ".mask") == 0)
;
else if (strcmp(mnemonic, ".frame") == 0)
;
else if (strcmp(mnemonic, ".extern") == 0)
;
else
error(input_filename, current_line, "Unknown directive : ", mnemonic);
return;
}
//cerr << "up to here...\n";
// We do not allow any instructions in the data or bss segments
if (current_segment == DATA)
{
error(input_filename, current_line, "Instructions not permitted in data segment.", NULL);
}
if (current_segment == BSS)
{
error(input_filename, current_line, "Instructions not permitted in bss segment.", NULL);
}
memory_entry *new_entry = add_entry(current_segment, current_line);
new_entry->data = 0;
// Copy in the func & OPCode fields