-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoperations.c
More file actions
657 lines (582 loc) · 15.3 KB
/
Copy pathoperations.c
File metadata and controls
657 lines (582 loc) · 15.3 KB
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
#include "operations.h"
/*
operations.c
This file contains the implementation of the operation handling functions.
It defines the operations table, and holds functions to validate operations, instructions and operands.
*/
/* Constant operations array that holds information for each code operation.
Table holds: name, opcode, funct, allowed source methods, allowed target methods*/
Operation OPERATIONS[NUM_OF_OPERATIONS] = {
{"mov", 0, 0, {IMMEDIATE, DIRECT, REGISTER, -1}, {DIRECT, REGISTER, -1}},
{"cmp", 1, 0, {IMMEDIATE, DIRECT, REGISTER, -1}, {IMMEDIATE, DIRECT, REGISTER, -1}},
{"add", 2, 1, {IMMEDIATE, DIRECT, REGISTER, -1}, {DIRECT, REGISTER, -1}},
{"sub", 2, 2, {IMMEDIATE, DIRECT, REGISTER, -1}, {DIRECT, REGISTER, -1}},
{"lea", 4, 0, {DIRECT, IMMEDIATE, -1}, {DIRECT, REGISTER, -1}},
{"clr", 5, 1, {NONE}, {DIRECT, REGISTER, -1}},
{"not", 5, 2, {NONE}, {DIRECT, REGISTER, -1}},
{"inc", 5, 3, {NONE}, {DIRECT, REGISTER, -1}},
{"dec", 5, 4, {NONE}, {DIRECT, REGISTER, -1}},
{"jmp", 9, 1, {NONE}, {DIRECT, RELATIVE, -1}},
{"bne", 9, 2, {NONE}, {DIRECT, RELATIVE, -1}},
{"jsr", 9, 3, {NONE}, {DIRECT, RELATIVE, -1}},
{"red", 12, 0, {NONE}, {DIRECT, REGISTER, -1}},
{"prn", 13, 0, {NONE}, {IMMEDIATE, DIRECT, REGISTER, -1}},
{"rts", 14, 0, {NONE}, {NONE}},
{"stop", 15, 0, {NONE}, {NONE}}};
int get_register_index(char *register_name)
{
/*Check if register name is valid*/
if (register_name == NULL)
{
return -1;
}
if (register_name[0] != 'r')
{
return -1;
}
if (strlen(register_name) != REGISTER_NAME_LENGTH)
{
return -1;
}
if (register_name[1] < '0' || register_name[1] > '7')
{
return -1;
}
/*get number of register from 1 - 7*/
return register_name[1] - '0';
}
int is_operation_name(char *name)
{
Operation *operation = NULL;
if (name == NULL)
{
return FALSE;
}
/*Check if name is a valid operation by searching in the operations table*/
operation = get_operation(name);
if (operation != NULL)
{
return TRUE;
}
return FALSE;
}
int is_register_name(char *name)
{
if (name == NULL)
{
return FALSE;
}
/*Check if name is a valid register name by searching in the register table*/
if (get_register_index(name) != -1)
{
return TRUE;
}
return FALSE;
}
Operation *get_operation(char *name)
{
int i;
if (name == NULL)
{
return NULL; /*Operation name is NULL*/
}
/*Search for the operation name in the operations table*/
for (i = 0; i < NUM_OF_OPERATIONS; i++)
{
if (strcmp(OPERATIONS[i].name, name) == 0)
{
return &OPERATIONS[i]; /*Return pointer to the operation details*/
}
}
return NULL; /*Operation not found*/
}
OperandType *get_allowed_addressing_methods(char *name, int source_or_target)
{
Operation *operation = NULL;
if (name == NULL)
{
return NULL;
}
/*Get operation from the operations table*/
operation = get_operation(name);
if (operation == NULL)
{
return NULL; /*Operation not found*/
}
/*If source_or_target is SOURCE, return the allowed source methods. if target, return allowed target methods*/
if (source_or_target == SOURCE)
{
return operation->allowed_source;
}
else if (source_or_target == TARGET)
{
return operation->allowed_target;
}
return NULL;
}
int is_data_operation(char *word)
{
if (word == NULL)
{
return FALSE;
}
/*Check if word is .data or .string which is a data operation*/
if ((strcmp(word, DATA_INSTRUCTION) == 0) || (strcmp(word, STRING_INSTRUCTION) == 0))
{
return TRUE;
}
return FALSE;
}
int is_extern_operation(char *word)
{
if (word == NULL)
{
return FALSE;
}
/*Check if word is .extern which is an extern operation*/
if (strcmp(word, EXTERN_INSTRUCTION) == 0)
{
return TRUE;
}
return FALSE;
}
int is_entry_operation(char *word)
{
if (word == NULL)
{
return FALSE;
}
/*Check if word is .entry which is an entry operation*/
if (strcmp(word, ENTRY_INSTRUCTION) == 0)
{
return TRUE;
}
return FALSE;
}
int is_code_operation(char *word)
{
if (word == NULL)
{
return FALSE;
}
/*Check if word is a valid code operation name*/
/*Searches if word is found in the code operations table*/
return is_operation_name(word);
}
int is_label_dec(char *word_original, int line_number, ErrorObject *error, MacroNode *macro_list)
{
char *word = NULL;
char *colon = NULL;
ErrorCode result = SUCCESS;
if (!word_original)
{
return FALSE;
}
/*Copy word to new string to avoid strtok changing the original*/
word = (char *)malloc(strlen(word_original) + 1);
strcpy(word, word_original);
colon = strchr(word, ':');
if (colon == NULL) /*Check if the ":" are existent*/
{
free(word);
return FALSE;
}
/*Check if there is a space after the ":"*/
if (*(colon + 1) == ' ')
{
free(word);
return FALSE;
}
/*Remove the ":" from word*/
*colon = '\0';
/*Check that there were no spaces before :*/
if (word[strlen(word) - 1] == ' ')
{
free(word);
return FALSE;
}
/*Check if label name is valid*/
result = validate_label_name(word, macro_list);
if (result != SUCCESS)
{
if (error)
fill_error_object(result, line_number, word, error);
/*If name is invalid, its still a valid declaration. fill error object but return true*/
}
free(word);
return TRUE;
}
ErrorCode validate_label_name(char *word, MacroNode *macro_list)
{
int i;
if (!macro_list)
{
return ERROR_NULL_PARAM;
}
if (!word)
{
return ERROR_LABEL_EMPTY_NAME;
}
/*Check maximum length of label*/
if (strlen(word) > MAX_LABEL)
{
return ERROR_LABEL_NAME_TOO_LONG;
}
if (isspace(word[0]) || word[0] == '\0')
{
return ERROR_LABEL_EMPTY_NAME;
}
/*Check if label starts with alphabatic letter*/
if (!isalpha(word[0]))
{
/* handle_line_error(ERROR_LABEL_INVALID_START, line_number, word);
return FALSE; */
return ERROR_LABEL_INVALID_START;
}
/*Check if label letters are alphabatic or digits*/
for (i = 1; (i < strlen(word)); i++)
{
if (!isalnum(word[i])) /*If word[i] is not alphanumeric*/
{
if ((i == strlen(word) - 1) && (word[i] == '\n')) /*If last char is \n, its valid*/
return TRUE;
else
{
return ERROR_LABEL_NOT_ALPHANUMERIC;
}
}
}
/*Check if label is a reserved word*/
if (is_reserved_word(word))
{
return ERROR_LABEL_RESERVED_WORD;
}
/*Check if label is already a macro name (macro names can't be used for labels)*/
if (is_macro(word, macro_list))
{
return ERROR_LABEL_IS_MACRO_NAME;
}
return SUCCESS;
}
int is_reserved_word(char *word)
{
if (word == NULL)
{
return FALSE;
}
/*Check if word is a reserved word by checking if it's a code, data, extern,
entry operation or a register name \ macro name*/
if (is_code_operation(word))
return TRUE;
if (is_data_operation(word))
return TRUE;
if (is_extern_operation(word))
return TRUE;
if (is_entry_operation(word))
return TRUE;
if ((strcmp(word, "mcro") == 0) || (strcmp(word, "mcroend") == 0))
return TRUE;
if (is_register_name(word))
return TRUE;
/*Check if equals to entry, data, extern without the .
they are also reserved words*/
if ((strcmp(word, "entry") == 0) || (strcmp(word, "data") == 0) || (strcmp(word, "extern") == 0))
return TRUE;
return FALSE;
}
ErrorCode validate_data(char *word)
{
int i = 0;
if (word == NULL)
{
return ERROR_NO_OPERAND;
}
/*Check if theres an extra comma at the start or end*/
if (word[0] == ',' || word[strlen(word) - 1] == ',')
{
return ERROR_EXTRA_COMMA;
}
/*Check if there are two commas in a row*/
for (i = 0; word[i] != '\0'; i++)
{
if (word[i] == ',' && word[i + 1] == ',')
{
return ERROR_EXTRA_COMMA;
}
}
word = strtok(word, ","); /*Split the string by commas*/
while (word != NULL) /*For every operand seperated by a comma, check if valid number*/
{
if (!is_integer(word, strlen(word)))
{
return ERROR_INVALID_NUMBER;
}
word = strtok(NULL, ",");
}
/*If there is extra content after numbers*/
if (word != NULL)
{
return ERROR_EXTRA_TEXT;
}
return SUCCESS;
}
ErrorCode validate_string(char *word)
{
if (word == NULL)
{
return ERROR_NO_OPERAND;
}
/*Check if word is a string*/
if (word[0] != '"')
{
return ERROR_INVALID_STRING;
}
word++;
while (word[0] != '"') /*Loop until finding the end quotes. if found \0 before it, string doesn't end in quotes.*/
{
if (word[0] == '\0')
{
return ERROR_INVALID_STRING;
}
word++;
}
if (word[1] != '\0') /*Check if there is extra content after the end of the string*/
{
return ERROR_INVALID_STRING;
}
return SUCCESS;
}
int is_integer(char *str, int len)
{
int i = 0;
if (str == NULL)
return 0;
while ((i < len) && (str[0] == ' ' || str[0] == '\t')) /*Skip spaces at the start*/
{
str++;
i++;
}
if ((i < len) && (str[0] == '+' || str[0] == '-')) /*Check if start is + or -, skip if found*/
{
str++;
i++;
}
if ((i < len) && !isdigit(str[0])) /*Check if first char is digit*/
{
return FALSE;
}
while ((i < len) && isdigit(str[0])) /*Loop over rest of the chars, skip if found a digit*/
{
str++;
i++;
}
while ((i < len) && (str[0] == ' ' || str[0] == '\t' || str[0] == '\n')) /*Skip spaces, tabs and new line at the end*/
{
str++;
i++;
}
/*If str[0] is not \0, the string is not a number because we didn't skip all of the chars (which means they are invalid)*/
return str[0] == '\0';
}
ErrorCode validate_extern(char *word, MacroNode *macro_list)
{
ErrorCode result = SUCCESS;
char *extra_content = NULL;
word = strtok(word, " ");
if (!macro_list)
{
return ERROR_NULL_PARAM;
}
if (!word)
{
/*No data found*/
return ERROR_EXTERN_EMPTY_NAME;
}
extra_content = strtok(NULL, " ");
if (extra_content != NULL)
{
/*Extra content found after extern label operation*/
return ERROR_EXTRA_TEXT;
}
/*Check if operand is a valid label name*/
result = validate_label_name(word, macro_list);
return result;
}
ErrorCode validate_entry(char *word)
{
char *extra_content = NULL;
if (word == NULL)
{
return ERROR_NO_OPERAND;
}
word = strtok(word, " ");
if (word == NULL)
{
/*No data found*/
return ERROR_EXTERN_EMPTY_NAME;
}
extra_content = strtok(NULL, " ");
if (extra_content != NULL)
{
/*Extra content found after entry label operation*/
return ERROR_EXTRA_TEXT;
}
return SUCCESS;
}
ErrorCode validate_code(char *operation, char *oprands)
{
OperandType *allowed_source_methods;
OperandType *allowed_target_methods;
OperandType current_oprand_type = NONE;
int i = 0;
int result = SUCCESS;
if (operation == NULL)
{
return ERROR_NULL_PARAM;
}
allowed_source_methods = get_allowed_addressing_methods(operation, SOURCE); /*Get allowed source methods*/
allowed_target_methods = get_allowed_addressing_methods(operation, TARGET); /*Get allowed target methods*/
if (allowed_source_methods == NULL || allowed_target_methods == NULL)
{
return ERROR;
}
oprands = strtok(oprands, ","); /*Split the oprands by commas*/
if (allowed_source_methods[0] != NONE) /*if source operands are allowed for operation*/
{
if (oprands == NULL)
{
/*Error: No oprands found, but needed for command*/
return ERROR_INVALID_AMOUNT_OF_OPERANDS;
}
current_oprand_type = get_operand_type(oprands);
if (current_oprand_type == NONE)
{
/*Error: Operand type is not defined*/
return ERROR_INVALID_OPERAND_TYPE;
}
for (i = 0; allowed_source_methods[i] != END_OF_ARRAY; i++) /*Loop over allowed source types*/
{
if (allowed_source_methods[i] == current_oprand_type) /*If operand type is allowed, skip to next operand. mark as success*/
{
oprands = strtok(NULL, ",");
result = SUCCESS;
break;
}
result = ERROR;
}
}
if (allowed_target_methods[0] != NONE) /*if target operands are allowed for operation*/
{
if (oprands == NULL)
{
/*Error: No oprands found, but needed for command*/
return ERROR_INVALID_AMOUNT_OF_OPERANDS;
}
current_oprand_type = get_operand_type(oprands);
if (current_oprand_type == NONE)
{
/*Error: Operand type is not defined*/
return ERROR_INVALID_OPERAND_TYPE;
}
for (i = 0; allowed_target_methods[i] != END_OF_ARRAY; i++) /*Loop over allowed target types*/
{
if (allowed_target_methods[i] == current_oprand_type) /*If operand type is allowed, skip to next operand. mark as success*/
{
oprands = strtok(NULL, ",");
result = SUCCESS;
break;
}
result = ERROR;
}
oprands = strtok(NULL, ",");
}
if (oprands != NULL)
{
/*Error: Too many oprands*/
return ERROR_INVALID_AMOUNT_OF_OPERANDS;
}
if (result == ERROR)
{
return ERROR_OPERAND_NOT_ALLOWED;
}
return result;
}
int get_operand_type(char *word)
{
if (!word)
{
return NONE;
}
if (is_immediate(word))
{
return IMMEDIATE;
}
else if (is_register(word))
{
return REGISTER;
}
else if (is_relative(word))
{
return RELATIVE;
}
else if (is_direct(word))
{
return DIRECT;
}
return NONE;
}
int is_immediate(char *word)
{
if (!word)
{
return FALSE;
}
if (word[0] == '#') /*Immidiate operands start with #*/
{
word++;
if (is_integer(word, strlen(word))) /*Immediate operands are a valid integer*/
{
return TRUE;
}
}
return FALSE;
}
int is_direct(char *word)
{
if (!word)
{
return FALSE;
}
return TRUE;
}
int is_relative(char *word)
{
if (!word)
{
return FALSE;
}
if (word[0] == '&')
{
/*
word++;
if (is_legal_label_name(word))
{
return TRUE;
}
*/
return TRUE;
}
return FALSE;
}
int is_register(char *word)
{
if (!word)
{
return FALSE;
}
if (is_register_name(word))
{
return TRUE;
}
return FALSE;
}