-
Notifications
You must be signed in to change notification settings - Fork 0
/
passes_utils.c
493 lines (455 loc) · 16.3 KB
/
passes_utils.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
#include "head.h"
#include "linked_list_utils.h"
#include "utils.h"
#include "commands.h"
/*The function return the number of commas in the line array*/
int get_number_of_sign(char line[], char sign) {
int i = 0;
int num_of_sign = 0;
while(line[i]){
if (line[i] == sign)
num_of_sign++;
i++;
}
return num_of_sign;
}
/*The function check if there is more argument or not*/
int check_if_token_is_empty(char *token){
token = strtok(NULL, delimit);
if (token != NULL){
return FALSE;
}
return TRUE;
}
int check_name(char *exp, int label_len, int num_of_line) {
/* The function gets a label name and checks that it meets all the required conditions
* A valid label begins with an alphabet letter, followed by a series of letters and/or numbers without spaces.
* The maximum length of a label is 31. */
int i;
if(label_len > MAX_NAME_LEN) {
printf("Error in line %d: The maximum length of a label is 31: %s \n",num_of_line,exp);
return FALSE;
}
if (!isalpha(exp[FIRST_INDEX])){
printf("Error in line %d: A valid label begins with an alphabet letter: %s\n",num_of_line,exp);
return FALSE;
}
for (i = 0; i < label_len; i++) {
if ((exp[i] == SPACE_CHAR) || (exp[i] == TAB)) {
printf("Error in line %d: Spaces in the middle of the label name is invalid: %s \n",num_of_line,exp);
return FALSE;
}
if (!isalnum(exp[i])) {
printf("Error in line %d: An invalid character is found in the label name: %s \n",num_of_line,exp);
return FALSE;
}
}
return TRUE;
}
int check_if_name_exist(char label_name[]) { /* The function gets a label name and checks if it exists in the symbol table */
symbol_tab *sym;
sym = find_symbol(heads_list.symbols_head, label_name);
if (sym != NULL) {
return TRUE;
}
return FALSE;
}
/* *The function gets a pointer to a string and checks if there is a label.
*it is also checked whether the label name is valid:
The character ':' must be attached to the label name.
There must be a space between the label name and the continuation of the sentence.
And other rules that are tested in calling the function: check_name */
int search_label(char *exp, int num_of_pass, int num_of_line) {
int i;
for (i = 0; exp[i]; i++) {
if (exp[i] == ':') {
if (num_of_pass == SECOND_PASS) {
return i+1;
}
if ((exp[i-1] == SPACE_CHAR) || (exp[i-1] == TAB)){
printf("Error in line %d: The character ':' must be attached to the label name: %s",num_of_line,exp);
}
else if ((exp[i+1] != SPACE_CHAR) && (exp[i+1] != TAB)){
printf("Error in line %d: There must be a space between the label name and the continuation of the sentence: %s",num_of_line,exp);
}
else if (check_name(exp, i, num_of_line)) { /* check if the label name meets the required conditions */
if(!check_if_name_exist(exp)) { /* check if the label name is already in use */
return i+1;
}
else {
printf("Error in line %d: The label name is already in use: %s \n",num_of_line,exp);
}
}
return INVALID_LABEL_NAME;
}
}
return FALSE;
}
int is_this_an_immediate_addressing(char *operand, int num_of_line){
char *operand_name = operand;
int i, first = 0;
if (operand_name[FIRST_INDEX] == '#'){
first = 1;
}
else{
return FALSE;
}
if (operand_name[SECOND_INDEX] == '-'){
first = 2;
}
if (!isdigit(operand_name[first])) {
printf("Error in line: %d !!! The operand value is invalid: %s\n", num_of_line, operand);
return FAILED;
}
for (i = first; operand_name[i]; i++){
if (operand_name[i] == '.'){
printf("Error in line: %d !!! The value does not belong to the Z group: %s \n", num_of_line, operand);
return FAILED;
}
if (!isdigit(operand_name[i]) && ((operand[i] != SPACE_CHAR) && (operand[i] != TAB))){
printf("Error in line %d !!! An invalid character: %c, is found in the operand value: %s \n", num_of_line, operand_name[i], operand);
return FAILED;
}
}
return TRUE;
}
int is_this_an_register_direct_addressing(char *operand, int num_of_line){
char *operand_name = operand;
int x, i;
int vaild_num = TRUE;
if (operand_name[FIRST_INDEX] == 'r'){
for (i = SECOND_INDEX; operand_name[i]; i++){
if (!isdigit(operand_name[i]))
{
vaild_num = FALSE;
}
}
if (vaild_num){
x = atoi(operand_name + 1);
if ((x >= 0) && (x <= 15)){
return TRUE;
}
else{
printf("Error in line %d !!! The value of the register should be between 0 and 15: %s\n", num_of_line, operand);
return FAILED;
}
}
}
return FALSE;
}
int is_this_an_index_addressing(char *operand, int num_of_pass, int num_of_line){
char *operand_name = operand;
char label_name[MAX_NAME_LEN] = {END_OF_STRING};
int x, i,z, j = 0;
int closing_bracket = FALSE, opening_bracket = FALSE;
for (i = 0; operand_name[i]; i++){
if (operand[i] && closing_bracket) {
printf("Error in line: %d !!! invalid operand: %s\n", num_of_line, operand);
return FAILED;
}
if ((operand_name[i] == '[') && (!opening_bracket)){
opening_bracket = TRUE;
if (operand_name[i+1] != 'r'){
printf("Error in line: %d !!! invalid operand: %s\n", num_of_line, operand);
return FAILED;
}
i = i + 2;
for (z = i; operand_name[z]; z++){
if (operand_name[z] == ']'){
closing_bracket = TRUE;
i=z;
break;
}
if(!isdigit(operand_name[z])) {
printf("Error in line: %d !!! invalid operand: %s\n", num_of_line, operand);
return FAILED;
}
}
}
if ((operand_name[i] == '[') && (opening_bracket)){
printf("Error in line: %d !!! invalid operand: %s\n", num_of_line, operand);
return FAILED;
}
}
if (!closing_bracket)
return FALSE;
else if ((closing_bracket) && (operand_name[i - 1] != ']')){
printf("Error in line: %d !!! invalid operand: %s\n", num_of_line, operand);
return FAILED;
}
else {
while (operand_name[j] != '['){
label_name[j] = operand_name[j];
j++;
}
if (check_name(label_name, j - 2, num_of_line)){
if (num_of_pass == SECOND_PASS){
if (!check_if_name_exist(label_name)){
printf("Error in line: %d !!! The label name: %s is not defined \n", num_of_line, label_name);
return FAILED;
}
}
}
else {
return FAILED;
}
j = j + 2;
x = atoi(operand + j);
if ((x < 10) || (x > 15)){
printf("Error in line %d !!! The value of the register should be between 10 and 15: %s\n", num_of_line, operand);
return FAILED;
}
}
return TRUE;
}
/* The function receives an operand and returns the correct addressing mode to it.
In case errors are found, a message will be printed for them and FAILED will be returned .
*/
int get_addressing_modes(char *operand, int num_of_pass, int num_of_line){
int status;
if ((status = is_this_an_immediate_addressing(operand, num_of_line))){
if (status == FAILED){
return FAILED;
}
return IMMEDIATE_ADDRESSING;
}
if ((status = is_this_an_register_direct_addressing(operand,num_of_line))){
if (status == FAILED){
return FAILED;
}
return REGISTER_DIRECT_ADDRESSING;
}
if ((status = is_this_an_index_addressing(operand, num_of_pass, num_of_line))){
if (status == FAILED){
return FAILED;
}
return INDEX_ADDRESSING;
}
if (num_of_pass == FIRST_PASS){
if (check_name(operand, strlen(operand), num_of_line))
return DIRECT_ADDRESSING;
}
if (num_of_pass == SECOND_PASS){
if (check_if_name_exist(operand)){
return DIRECT_ADDRESSING;
}
else{
printf("Error in line: %d, label name doesnt exist: %s\n",num_of_line,operand);
return FAILED;
}
}
return FAILED;
}
/*The function returns the index of the function in the commands array*/
int get_addressing_mode_of_operand(char *token, int number_of_line){
int method_number;
token = strtok(NULL, delimit);
if (token == NULL){
printf("Error missing operand in line: %d\n", number_of_line);
return FAILED;
}
remove_white_spaces(token);
method_number = get_addressing_modes(token, FIRST_PASS, number_of_line);
/*If operand not a valid operand */
if (method_number == FAILED){
return FAILED;
}
if (!(check_if_token_is_empty(token))){
printf("Error to much operands in line: %d\n", number_of_line);
return FAILED;
}
return method_number;
}
int get_addressing_modes_of_two_operands(char *token, int number_of_line, int *method_of_first_operand, int *method_of_second_operand){ /*Get the first opernd */
token = strtok(NULL, COMMA);
if (token == NULL){
printf("Error missing operand in line: %d\n", number_of_line);
return FAILED;
}
del_edge_spaces(token);
*method_of_first_operand = get_addressing_modes(token, FIRST_PASS, number_of_line);
/*If operand not a valid operand */
if (*method_of_first_operand == FAILED)
{
return FAILED;
}
/*Get the second opernd */
token = strtok(NULL, delimit);
if (token == NULL){
printf("Error missing operand in line: %d\n", number_of_line);
return FAILED;
}
remove_white_spaces(token);
*method_of_second_operand = get_addressing_modes(token, FIRST_PASS, number_of_line);
/*If operand not a valid operand */
if (*method_of_second_operand == FAILED){
return FAILED;
}
if (!(check_if_token_is_empty(token))){
printf("Error to much operands in line: %d\n", number_of_line);
return FAILED;
}
return SUCCEEDED;
}
int push_symbol_data(char* label_name, int value, int base_address, int offset, int attributes) {
symbol_tab *sym;
sym = fill_data(heads_list.symbols_head, label_name, value, base_address, offset, attributes);
if(sym == NULL){
return FAILED;
}
return SUCCEEDED;
}
void push_symbol_name(char label_name[]){
symbol_tab *sym;
sym = new_symbol(label_name);
if(sym != NULL)
heads_list.symbols_head = add_symbol(heads_list.symbols_head, sym);
}
int type_of_guidance(char line[]) { /* is this data/string/entry/extern guidance?
in case of errors in the guidence name, an error will be printed */
if ((!strncmp(DATA, line, 5)) && isspace(line[5])) {
return DATA_GUIDANCE_LINE;
}
if ((!strncmp(STRING, line, 7)) && isspace(line[7])) {
return STRING_GUIDANCE_LINE;
}
if ((!strncmp(ENTRY, line, 6)) && isspace(line[6])) {
return ENTRY_GUIDANCE_LINE;
}
if ((!strncmp(EXTERN, line, 7)) && isspace(line[7])) {
return EXTERN_GUIDANCE_LINE;
}
return FAILED;
}
int push_symbol_after_extern(char line[], int num_of_line) {/* Add the symbol to the symbols table in the following case: .extern label */
char *tmp_line;
int len;
tmp_line = line + 8;
del_all_spaces(tmp_line);
len = strlen(tmp_line) - 2;
if (check_name(tmp_line, len, num_of_line)) {
if(!check_if_name_exist(tmp_line)) {
push_symbol_name(tmp_line);
return SUCCEEDED;
}
else {
printf("Error in line %d: The label name: %s is already exist \n",num_of_line, line);
}
}
return FAILED;
}
int handle_guidance(char line[],int num_of_pass, int num_of_line) {
int guidance_type;
char *label_name;
guidance_type = type_of_guidance(line);
if (guidance_type == EXTERN_GUIDANCE_LINE) {
if(num_of_pass == SECOND_PASS) { /* For extern we already perform what is required in the first pass */
return EXTERN_GUIDANCE_LINE;
}
if (push_symbol_after_extern(line, num_of_line)) { /* We will add the label name after ".extern" to the symbol table */
return EXTERN_GUIDANCE_LINE;
}
return INVALID_LABEL; /* FAILED !!! error should be ON */
}
if (guidance_type == ENTRY_GUIDANCE_LINE) {
if (num_of_pass == SECOND_PASS) { /* For entry we will perform operations only in the second pass */
label_name = line + 7; /* .entry label ---> label */
del_all_spaces(label_name);
if (check_if_name_exist(label_name)) {
add_attributes(heads_list.symbols_head, label_name, ENTRY_ATTRIBUTE);/* update the attribute[1] of the symbol to ENTRY */
}
else {
printf("Error in line: %d, the lable name: %s doesn't exist\n",num_of_line, label_name);
return INVALID_LABEL;
}
}
return ENTRY_GUIDANCE_LINE;
}
if (guidance_type == STRING_GUIDANCE_LINE) {
return STRING_GUIDANCE_LINE;
}
if (guidance_type == DATA_GUIDANCE_LINE) {
return DATA_GUIDANCE_LINE;
}
printf("Error in line: %d, an unkonwn guidnace name: %s \n", num_of_line, line);
return UNKNOWN_GUIDANCE_TYPE; /* FAILED !!! error should be TRUE */
}
int lookup_cmd(char *token){
int i;
for (i = 0; i < NUMBER_OF_FUNCTIONS; i++){
char command[50];
int len;
strcpy(command, commands[i].name);
len = strlen(commands[i].name);
command[len] = NEW_LINE; /*Add \n to the command, in case that the command entered alone*/
command[len + 1] = END_OF_STRING; /*Close the string*/
if ((strcmp(token, commands[i].name) == 0) || (strcmp(token, command) == 0)) /*Checks if the command equals to one of the known commands*/
return i;
}
return -1;
}
int get_type_of_line(char line[], int *label_exist, int num_of_pass, int num_of_line) {
/* * check is this empty/comment/instruction/guidance line ?
* also uses functions that check the validity of the label name and adds their name to the symbol table if applicable.
*/
int possible_label;
char *exp;
int guidance_type;
char tmp[BUFFERSIZE];
strcpy(tmp, line);
if (tmp[FIRST_INDEX] == NEW_LINE) {
return EMPTY_LINE;
}
if (tmp[FIRST_INDEX] == ';') {
return COMMENT_LINE;
}
if (tmp[FIRST_INDEX] == '.') {
guidance_type = handle_guidance(tmp, num_of_pass, num_of_line); /* .extern label1 // .entry label2 // .data 6,-9 // .string "abcd" */
return guidance_type;
}
if((possible_label = search_label(tmp, num_of_pass, num_of_line))) {
*label_exist = TRUE;
if (possible_label == INVALID_LABEL_NAME) {/* lab$el // 3label // lab el */
return INVALID_LABEL;
}
exp = tmp + possible_label; /* label1: add r3,r2 ----> add r3,r2 // label2: .entry K ----> .entry K */
exp = del_prim_spaces(exp);
if (exp[FIRST_INDEX] == '.') {
guidance_type = handle_guidance(exp, num_of_pass, num_of_line);
if ((guidance_type == DATA_GUIDANCE_LINE) || (guidance_type == STRING_GUIDANCE_LINE)) { /* label1: .data 6,-9 */
if (num_of_pass == FIRST_PASS) {
take_first_word(tmp); /* label: .data 6,-9 ----> label */
if (check_if_name_exist(tmp)){
printf("Error in line: %d, the lable name: %s already exist\n",num_of_line, tmp);
return FAILED;
}
push_symbol_name(tmp);
}
}
return guidance_type;
}
take_first_word(exp); /* add r3,r2 -----> add // prn #48 ----> prn */
if (lookup_cmd(exp) != -1) { /* exp belongs to the list of instructions */
if (num_of_pass == SECOND_PASS) {
return INSTRUCTION_LINE;
}
take_first_word(tmp); /* label: add r5,r7 ----> label */
if (check_if_name_exist(tmp)){
printf("Error in line: %d, the lable name: %s allready exist\n",num_of_line, tmp);
return FAILED;
}
push_symbol_name(tmp);
return INSTRUCTION_LINE;
}
else {
printf("Error in line: %d, unkown input: %s\n",num_of_line, exp);
return FAILED; /* label: HELLO // label: 234 */
}
}
take_first_word(tmp); /* sub r5,r2 -----> sub */
if(lookup_cmd(tmp) != -1) { /* line belongs to the list of instructions */
return INSTRUCTION_LINE;
}
printf("Error in line: %d, unkown input: %s",num_of_line, line);
return FAILED;
}