-
Notifications
You must be signed in to change notification settings - Fork 0
/
zassemble.c
1195 lines (1003 loc) · 35.3 KB
/
zassemble.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
//-----------------------------------------------------------------------------
/*! \file zassemble.c
*
* \author Emily Shaffer
* \author James Massucco
* \date 11/04/2015
*
* \brief Program for translating assembly into machine code
* \details This program is for use with a custom assembly language
* based on MIPS and developed by and for Northeastern's
* Digital Logic Design and Computer Organization class.
* This program's purpose is to take a .s/.asm file with
* assembly code and translate into machine code capable of
* being run on a processor designed by students in laboratory.
*/
/*------------------------------------------------------------------------------
* #$-Include Files-$#
*-----------------------------------------------------------------------------*/
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <unistd.h>
/*------------------------------------------------------------------------------
* #$-Defines-$#
*-----------------------------------------------------------------------------*/
//Opcode defines
#define LW 0x0 //I
#define SW 0x1 //I
#define ADD 0x2 //R
#define ADDI 0x3 //I
#define INV 0x4 //R
#define AND 0X5 //R
#define ANDI 0x6 //I
#define OR 0x7 //R
#define ORI 0x8 //I
#define SRA 0x9 //I
#define SLL 0xA //I
#define BEQ 0xB //J
#define BNE 0xC //J
#define CLR 0xD //R
//ANSI color defines
#define ANSI_COLOR_RED "\x1b[31m"
#define ANSI_COLOR_RESET "\x1b[0m"
//Global variables for error handling
static char* fullLine;
static int lineNumber = 1;
static int instructionNumber = 1;
//This will go away
static int error_count = 0;
/*------------------------------------------------------------------------------
* #$-Typedefs-$#
*-----------------------------------------------------------------------------*/
typedef unsigned short ushort;
typedef unsigned char uchar;
//-----------------------------------------------------------------------------
/*! \brief Generic machine code instruction bitfield
*/
typedef struct INSTR_
{
ushort : 12;
ushort opcode : 4;
} INSTR;
//-----------------------------------------------------------------------------
/*! \brief R-Type machine code instruction bitfield
* \note "pad" field included in order to fill with zeros during
* file write, so it does not contain junk values
*/
typedef struct R_INSTR_
{
ushort pad : 6;
ushort rd : 2;
ushort rt : 2;
ushort rs : 2;
ushort opcode : 4;
} R_INSTR;
//-----------------------------------------------------------------------------
/*! \brief IJ-Type machine code instruction bitfield
*/
typedef struct IJ_INSTR_
{
ushort immed : 8;
ushort rd : 2;
ushort rs : 2;
ushort opcode : 4;
} IJ_INSTR;
//-----------------------------------------------------------------------------
/*! \brief Error enumeration
*/
typedef enum ERROR_
{
NO_INPUT,
INV_INPUT,
INV_OUTPUT,
INV_OPCODE,
INV_LABEL,
PARSE_ERR,
PC_ERR
} ERROR;
//-----------------------------------------------------------------------------
/*! \brief Node for instruction linked list
* \details Instructions are arranged in a linked list structure.
* Each instruction node contains a bitfield of the machine
* code instruction, a label reference appearing in a branch
* or jump instruction, and a pointer to the next instruction
* node.
* \param label_reference A label referenced in a branch or jump
* instruction
* \param instr_LL_head Global static pointer to head of instruction LL
*/
typedef struct INSTR_NODE_
{
INSTR instr;
char* label_reference;
struct INSTR_NODE_ *next;
} INSTR_NODE;
static INSTR_NODE* instr_LL_head = NULL;
//-----------------------------------------------------------------------------
/*! \brief Node for label linked list
* \details Labels are arranged in a linked list structure. Each label
* node contains the label string, the line on which it was
* found, and a pointer to the next label node.
* \param label_LL_head Global static pointer to head of label LL
*/
typedef struct LABEL_NODE_
{
char *label;
int line;
struct LABEL_NODE_ *next;
} LABEL_NODE;
static LABEL_NODE* label_LL_head = NULL;
//-----------------------------------------------------------------------------
/*! \brief Node for error linked list
* \details Errors are arranged in a linked list structure. Each error
* node contains the error code, the line on which it was
* found, the full line string, and a pointer to the next error node.
* \param error_LL_head Global static pointer to head of label LL
*/
typedef struct ERROR_NODE_
{
ERROR error;
int line_number;
char* full_line;
char* label_reference;
struct ERROR_NODE_ *next;
} ERROR_NODE;
static ERROR_NODE* error_LL_head = NULL;
/*------------------------------------------------------------------------------
* #$-Instruction Functions-$#
*-----------------------------------------------------------------------------*/
//-----------------------------------------------------------------------------
/*! \brief Function to write an instruction to a file in hex format
* \details Takes an instruction and writes it to file fd in hex. Also
* Adds a necessary initialization string before the first
* instruction is written.
* \param firstInstruction Tracker needed to prepend instruction
* list with necessary initialization string
*/
static bool firstInstruction = true;
void writeInstr(INSTR instr, FILE* fd)
{
if (firstInstruction) {
fprintf(fd, "memory_initialization_radix=16;\nmemory_initialization_vector=%04hx", instr);
firstInstruction = false;
} else {
fprintf(fd, ",%04hx", instr);
}
}
//-----------------------------------------------------------------------------
/*! \brief Function to add a machine code instruction to LL
* \details Takes a generic type machine code instruction and appends
* it to a linked list of instructions
*/
INSTR_NODE* addInstruction(INSTR *new, INSTR_NODE **current)
{
//Create new instruction node
INSTR_NODE *newItem = (INSTR_NODE*)malloc(sizeof(INSTR_NODE));
newItem->next = NULL;
//Populate with instruction
newItem->instr = *new;
//If this is the first instruction
if (instr_LL_head == NULL)
{
instr_LL_head = newItem;
*current = instr_LL_head;
}
//If this is not the first instruction
else if (*current != NULL)
{
(*current)->next = newItem;
*current = newItem;
}
return *current;
}
//-----------------------------------------------------------------------------
/*! \brief Wrapper for adding R-type instruction to LL
* \details Takes an instruction in terms of R-type specific parameters,
* populates them into an R-type machine code instruction bitfield,
* then casts as a generic machine code instruction and calls
* addInstruction()
*/
INSTR_NODE* addRInstr(ushort opcode, ushort rs, ushort rt, ushort rd, INSTR_NODE **current)
{
R_INSTR *instr = (R_INSTR*)malloc(sizeof(R_INSTR));
instr->opcode = opcode;
instr->rs = rs;
instr->rt = rt;
instr->rd = rd;
instr->pad = 0;
return addInstruction((INSTR*)instr, current);
}
//-----------------------------------------------------------------------------
/*! \brief Wrapper for adding IJ-type instruction to LL
* \details Takes an instruction in terms of IJ-type specific parameters,
* populates them into an IJ-type machine code instruction bitfield,
* and casts as a generic machine code instruction and calls
* addInstruction(). Also adds a label_reference to the created
* list item (for branch/jump commands)
*/
INSTR_NODE* addIJInstr(ushort opcode, ushort rs, ushort rd, ushort immed, char* label_reference, INSTR_NODE **current)
{
IJ_INSTR *instr = (IJ_INSTR*)malloc(sizeof(IJ_INSTR));
instr->opcode = opcode;
instr->rs = rs;
instr->rd = rd;
instr->immed = immed;
if ( (*current = addInstruction((INSTR*)instr, current)) )
{
(*current)->label_reference = (char*)malloc(255);
strcpy((*current)->label_reference, label_reference);
}
return *current;
}
/*------------------------------------------------------------------------------
* #$-Error Functions-$#
*-----------------------------------------------------------------------------*/
//-----------------------------------------------------------------------------
/*! \brief Return -1 for empty/NULL string
*/
int lineIsEmpty(char* parse_buf)
{
if ( parse_buf == NULL ||
(strcmp(parse_buf, "\n") == 0) ||
(strcmp(parse_buf, "\r") == 0) ||
(strcmp(parse_buf, "") == 0) )
{
return -1;
}
return 0;
}
//-----------------------------------------------------------------------------
/*! \brief Function to add an error to a LL
*/
int addError(ERROR error)
{
//Create new error node
ERROR_NODE *newItem = (ERROR_NODE*)malloc(sizeof(ERROR_NODE));
newItem->next = NULL;
//Populate with error
newItem->error = error;
//Populate with lineNumber
newItem->line_number = lineNumber;
if ( !lineIsEmpty(fullLine) )
{
//Allocate memory for line
newItem->full_line = (char*) malloc(strlen(fullLine)*sizeof(char));
//Populate with full line
strcpy( (newItem->full_line), fullLine);
}
//If this is the first instruction
if (error_LL_head == NULL)
{
error_LL_head = newItem;
}
//If this is not the first instruction
else
{
//Tracker to traverse LL
ERROR_NODE* current = error_LL_head;
//While we aren't at the last node
while (current->next != NULL)
{
//Advance to next node
current = current->next;
}
//Append new error
current->next = newItem;
}
return 0;
}
//-----------------------------------------------------------------------------
/*! \brief Function to handle errors
*/
int handleErrors(void)
{
ERROR_NODE* current_error = error_LL_head;
//If no errors, return 1
if (current_error == NULL)
{
return 1;
}
//Traverse until we're at the last error
while (current_error != NULL)
{
fprintf(stderr, ANSI_COLOR_RED "error" ANSI_COLOR_RESET);
switch (current_error->error)
{
case NO_INPUT:
fprintf(stderr, ": No input file specified. Call program using syntax\n\n\t./zassemble input_assembly.s [output_machine_code.coe]\n\n");
break;
case INV_INPUT:
fprintf(stderr, ": Input file specified could not be opened, or is invalid.\n\n");
break;
case INV_OUTPUT:
fprintf(stderr, ": Output file specified could not be opened, or is invalid.\n\n");
break;
case INV_OPCODE:
fprintf(stderr, ": line %d: ", current_error->line_number);
fprintf(stderr, "The instruction contains an unrecognized opcode: \n%s", current_error->full_line);
fprintf(stderr, "\n\n");
break;
case INV_LABEL:
//fprintf(stderr, ": line %d: ", current_error->line_number);
fprintf(stderr, ": Invalid label. Check to ensure all labels referenced in branch/jump instructions are valid.");
fprintf(stderr, "\n\n");
break;
case PARSE_ERR:
fprintf(stderr, ": line %d: ", current_error->line_number);
fprintf(stderr, "The instruction contains invalid syntax: \n%s", current_error->full_line);
fprintf(stderr, "\n\n");
break;
case PC_ERR:
fprintf(stderr, ": Too many instructions. You may only have up to 256 instructions in your program.");
}
//Advance one error
current_error = current_error->next;
}
//A zero return indicates there were errors and they were handled
return 0;
}
/*------------------------------------------------------------------------------
* #$-Label Functions-$#
*-----------------------------------------------------------------------------*/
//-----------------------------------------------------------------------------
/*! \brief Function to add a node to a label linked list
* \details Prepends a new node storing a label string and a line number
* to the linked list pointed to by head
* \param label_LL_head Pointer to the head of (file global) label LL.
*/
void addLabel(char* label, int line)
{
//Create new node
LABEL_NODE *newNode;
newNode = (LABEL_NODE*) malloc(sizeof(LABEL_NODE));
//Save label and line number in new node
newNode->label = (char*) malloc(strlen(label)*sizeof(char));
strcpy(newNode->label, label);
newNode->line = line;
//Prepend new node onto existing linked list
newNode->next = label_LL_head;
label_LL_head = newNode;
}
//-----------------------------------------------------------------------------
/*! \brief Function to get relative position to a label
* \details Takes in a label referenced in a branch or jump instruction
* and the line of that instruction and returns the relative
* number of line numbers away the specified label is.
* \note A negative return value indicates jump up
*/
uchar getLabelJump(char* label, int current_line)
{
//Traverse label LL
LABEL_NODE *current = label_LL_head;
while (current != NULL)
{
//If label is found, return relative jump
if (strcmp(label, current->label) == 0)
{
return (uchar)((current->line) - (current_line));
}
current = current->next;
}
//If label is not found, generate an error
addError(INV_LABEL);
return -1;
}
/*------------------------------------------------------------------------------
* #$-Parsing Functions-$#
*-----------------------------------------------------------------------------*/
//-----------------------------------------------------------------------------
/*! \brief Replaces any tabs in parse_buf with spaces
* \details Takes in parse_buf, searches for tab characters, and
* replaces those found with spaces.
*/
int replaceTabsWithSpaces(char* parse_buf)
{
char* tab_location;
//Get pointer to first occurence of '\t' in parse_buf
tab_location = strchr(parse_buf, '\t');
//If pointer isn't NULL, we found one
while (tab_location != NULL)
{
//Edit parse_buf to replace tab with space
parse_buf[tab_location-parse_buf] = ' ';
//This syntax advnaces strchr() to find next tab
tab_location = strchr(tab_location + 1, '\t');
}
return lineIsEmpty(parse_buf);
}
//-----------------------------------------------------------------------------
/*! \brief Removes any comments from parse_buf
* \details Takes in parse_buf, tokenizes based on comment flag "#",
* and removes everything after the first "#".
*/
int removeComments(char* parse_buf)
{
char* token;
char* comment_location;
//See where comment is
comment_location = strchr(parse_buf, '#');
//If pointer isn't NULL, we found one
if (comment_location != NULL)
{
//Edit parse_buf to replace tab with space
parse_buf[comment_location-parse_buf] = '\0';
}
//If the line without a comment is empty, return a 1 code
return lineIsEmpty(parse_buf);
}
//-----------------------------------------------------------------------------
/*! \brief Categorizes and removes labels from parse_buf
* \details Takes in parse_buf, tokenizes based on ":",
* then stores everything after ":" in parse_buf.
*/
int parseForLabels(char* parse_buf)
{
char tokenizer[255], label[255];
char* token;
//Check for empty line
if ( lineIsEmpty(parse_buf) )
{
return -1;
}
//Initialize tokenizer
strcpy(tokenizer, parse_buf);
//Tokenize, split on ":" (for labels)
token = strtok(tokenizer, ":");
// If we found a ":"
if (strcmp(token, parse_buf) != 0)
{
//Check for empty string
if ( lineIsEmpty(token) )
{
return -1;
}
//Add label
addLabel(token, instructionNumber);
// Store everything after label in parse_buf
token = strtok(NULL, "\n");
//Check for empty line
if ( lineIsEmpty(token) )
{
return -1;
}
//Copy line w/ label removed back to parse_buf
strcpy(parse_buf, token);
}
//Return 0 since parse_buf isn't empty
return 0;
}
//-----------------------------------------------------------------------------
/*! \brief Translates opcode from string to hex
* \details Takes an opcode as a string and translates to hex. Returns
* an error if the opcode was not recognized
*/
ushort translateOpcode(char *opcode)
{
if (0 == strcmp(opcode, "lw"))
return LW;
else if (0 == strcmp(opcode, "sw"))
return SW;
else if (0 == strcmp(opcode, "add"))
return ADD;
else if (0 == strcmp(opcode, "addi"))
return ADDI;
else if (0 == strcmp(opcode, "inv"))
return INV;
else if (0 == strcmp(opcode, "and"))
return AND;
else if (0 == strcmp(opcode, "andi"))
return ANDI;
else if (0 == strcmp(opcode, "or"))
return OR;
else if (0 == strcmp(opcode, "ori"))
return ORI;
else if (0 == strcmp(opcode, "sra"))
return SRA;
else if (0 == strcmp(opcode, "sll"))
return SLL;
else if (0 == strcmp(opcode, "beq"))
return BEQ;
else if (0 == strcmp(opcode, "bne"))
return BNE;
else if (0 == strcmp(opcode, "clr"))
return CLR;
else
{
addError(INV_OPCODE);
return 0xF;
}
}
//-----------------------------------------------------------------------------
/*! \brief Gets opcode from parse_buf and translates to numerical opcode
* Basically a wrapper for translateOpcode( )
*/
int getOpcode(char* parse_buf, INSTR* instr_ptr)
{
char tokenizer[255];
char* token;
//Initialize tokenizer
strcpy(tokenizer, parse_buf);
//Parse for opcode
token = strtok(tokenizer, " ");
//Check for empty string
if ( lineIsEmpty(token) )
{
return -1;
}
instr_ptr->opcode = translateOpcode(token);
//Pull everything after opcode into parse_buf
token = strtok(NULL, "\n");
//Check for empty string
if ( lineIsEmpty(token) )
{
return -1;
}
strcpy(parse_buf, token);
//Return numerical opcode
return 0;
}
//-----------------------------------------------------------------------------
/*! \brief Parses string, returns immediate value
* \details Takes a string assumed to contain an immediate value;
* if the string contains "0x", the value is interpretted as
* hex, otherwise, it is interpretted as decimal.
*/
int getImmed(char* immediate_string)
{
char* zero_indicator;
char* x_indicator;
//char* token;
int immedNum;
//Look for zero in token
zero_indicator = strchr(immediate_string, '0');
//Look for x in token
x_indicator = strchr(immediate_string, 'x');
//Case where "0x" is present
if (x_indicator == (zero_indicator + 1))
{
immediate_string = x_indicator + 1;
sscanf(immediate_string, "%x", &immedNum);
}
//Case where "0x" is not present
else if (x_indicator == NULL)
{
sscanf(immediate_string, "%d", &immedNum);
}
//Case indicates error
else
{
//Need some kind of error handle here
return 0;
}
return immedNum;
}
//-----------------------------------------------------------------------------
/*! \brief Function to populate an instr bitfield based on a string
* \details Based on the internal value of strtok, function parses
* the assembly instruction for registers, immediates, and
* labels. The first register is found the same for all
* instruction types; after that, different subsets of
* instructions are treated differently.
*/
int populateInstr(INSTR *instr, char* parse_buf, char* label_reference)
{
int rFirst = -1, rSecond = -1, rThird = -1;
int result = 0;
int immedNum;
int scanError = 0;
char immedStr[255];
strcpy(immedStr, "");
//Grab first space-delimited argument
char *token = strtok(parse_buf, " ");
//Check for empty string
if ( lineIsEmpty(token) )
{
return -1;
}
//Parse first register
sscanf(token, "$%i,", &rFirst);
// First we treat normal R-type
// Format:
// opcode $rd, $rs, $rt
if (instr->opcode == ADD ||
instr->opcode == AND ||
instr->opcode == OR )
{
//Grab second register
token = strtok(NULL, " ");
//Check for empty string
if ( lineIsEmpty(token) )
{
return -1;
}
//Parse second register
sscanf(token, "$%i,", &rSecond);
// Grab third register
token = strtok(NULL, " ");
//Check for empty string
if ( lineIsEmpty(token) )
{
return -1;
}
//Parse third register
sscanf(token, "$%i", &rThird);
R_INSTR *r_instr = (R_INSTR*)instr;
//Populate instructions according to format
// opcode $rd, $rs, $rt
r_instr->rs = rSecond;
r_instr->rt = rThird;
r_instr->rd = rFirst;
//Check for unread registers
if (rFirst == -1 ||
rSecond == -1 ||
rThird == -1 ||
scanError == -1 )
{
addError(PARSE_ERR);
}
}
//Next treat weird INV
// Format:
// opcode $rd, $rt
else if (instr->opcode == INV)
{
//Grab second register
token = strtok(NULL, " ");
//Check for empty string
if ( lineIsEmpty(token) )
{
return -1;
}
//Parse second register
sscanf(token, "$%i", &rSecond);
R_INSTR *r_instr = (R_INSTR*)instr;
//Populate instructions according to format
// opcode $rd, $rs, $rt
r_instr->rs = 0;
r_instr->rt = rSecond;
r_instr->rd = rFirst;
//Check for unread registers
if (rFirst == -1 ||
rSecond == -1 ||
scanError == -1 )
{
addError(PARSE_ERR);
}
}
//Next treat weird R-type (inv/clr)
// Format:
// opcode $rt
else if (instr->opcode == CLR)
{
R_INSTR *r_instr = (R_INSTR*)instr;
//Populate instructions according to format
// opcode $rt
// (duplicate $rt into $rd)
r_instr->rs = 0;
r_instr->rt = rFirst;
r_instr->rd = rFirst;
//Check for unread registers
if (rFirst == -1 ||
scanError == -1 )
{
addError(PARSE_ERR);
}
}
// Next treat weird IJ-type (load/store)
// Format:
// opcode $rt, offs($rs)
//TODO: Ensure these instructios work
else if(instr->opcode == LW ||
instr->opcode == SW )
{
//Grab immediate
token = strtok(NULL, "(");
//Check for empty string
if ( lineIsEmpty(token) )
{
return -1;
}
immedNum = getImmed(token);
//Parse immediate
//sscanf(token, "%x", &immedNum);
//Grab second register
token = strtok(NULL, ")");
//Check for empty string
if ( lineIsEmpty(token) )
{
return -1;
}
//Parse second register
sscanf(token, "$%i", &rSecond);
//Populate instructions according to format
// opcode $rt, offst($rs)
IJ_INSTR *ij_instr = (IJ_INSTR*)instr;
ij_instr->rs = rSecond;
ij_instr->rd = rFirst;
ij_instr->immed = immedNum;
//Check for unread registers
if (rFirst == -1 ||
rSecond == -1 ||
scanError == -1 )
{
addError(PARSE_ERR);
}
}
// Now treat IJ instructions with a LABEL
// Format:
// opcode $rd, $rs, LABEL
else if (instr->opcode == BEQ ||
instr->opcode == BNE )
{
//Grab second register
token = strtok(NULL, " ");
//Check for empty string
if ( lineIsEmpty(token) )
{
return -1;
}
//Parse second register
sscanf(token, "$%i,", &rSecond);
//Grab label
token = strtok(NULL, " \n");
//Check for empty string
if ( lineIsEmpty(token) )
{
return -1;
}
//Parse label
sscanf(token, "%s", immedStr);
//Populate instructions according to format
// opcode $rs, $rt, label
IJ_INSTR *ij_instr = (IJ_INSTR*)instr;
ij_instr->rs = rFirst;
ij_instr->rd = rSecond;
ij_instr->immed = (uchar)(-1); //immed is only 8 bits
strcpy(label_reference, immedStr);
//Check for unread registers
if (rFirst == -1 ||
rSecond == -1 ||
strcmp(immedStr, "") == 0 ||
scanError == -1 )
{
addError(PARSE_ERR);
}
}
// Now treat all other IJ type instructions
// Format:
// opcode $rd, $rs, IMM
else if (instr->opcode == ADDI ||
instr->opcode == ANDI ||
instr->opcode == ORI ||
instr->opcode == SRA ||
instr->opcode == SLL )
{
//Grab second register
token = strtok(NULL, " ");
//Check for empty string
if ( lineIsEmpty(token) )
{
return -1;
}
//Parse second register
sscanf(token, "$%i,", &rSecond);
//Grab immediate
token = strtok(NULL, " \n");
//Check for empty string
if ( lineIsEmpty(token) )
{
return -1;
}
immedNum = getImmed(token);
//Parse immediate
//sscanf(token, "%x", &immedNum);
// Populate instructions according to format
// opcode $rd, $rs, IMM
IJ_INSTR *ij_instr = (IJ_INSTR*)instr;
ij_instr->rs = rSecond;
ij_instr->rd = rFirst;
ij_instr->immed = immedNum;
//Check for unread registers
if (rFirst == -1 ||
rSecond == -1 ||
scanError == -1 )
{
addError(PARSE_ERR);
}
}
return 0;
}
//-----------------------------------------------------------------------------
/*! \brief Completes parse of one line
*/
int parseOneLine(char* parse_buf, INSTR* instr_ptr, INSTR_NODE** ptr_to_current)
{
char label_reference[255];
//Skip parsing if parse_buf is newline, empty, or NULL,
//or if we're at the end of the file
if ( !lineIsEmpty(parse_buf) )
{
//Replace all tabs with spaces
if ( replaceTabsWithSpaces(parse_buf) )
{
//Non-zero return indicates empty line
return -1;
}
//Remove all comments
if ( removeComments(parse_buf) )
{
//Non-zero return indicates empty line
return -1;
}
//Parse for labels
if( parseForLabels(parse_buf) )
{
//Non-zero return indicates empty line
return -1;
}
//
if ( getOpcode(parse_buf, instr_ptr) ) //TODO: Treat invalid opcodes
{
//Non-zero return indicates empty line