-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptab.c
More file actions
116 lines (103 loc) · 2.56 KB
/
Copy pathoptab.c
File metadata and controls
116 lines (103 loc) · 2.56 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
#include "optab.h"
int hash(char str[]) {
/*
* the hash function modulo 20
* Arguments:
* char str[] - the string on which the hash is computed
* Returns:
* the bucket id within the range 0 ~ 19 (total of 20 buckets)
*/
const int len = strlen(str);
int ret = 0;
for(int i=0; i<len; i++) ret += (int)str[i];
return ret % 20;
}
void construct_optab(HASH_NODE *optab[20]) {
/*
* constructs the optab
* Arguments:
* HASH_NODE *optab[] - array of 20 linked lists
*/
int idx;
char line[100];
char tok1[20]; char tok2[20]; char tok3[20];
FILE *fp = fopen("opcode.txt","r");
/* file error handling */
if(!fp) {
fprintf(stderr,"opcode.txt not found!\n");
exit(1);
}
/* read opcode.txt and add each line to optab */
while(fscanf(fp,"%[^\n]s",line)!=EOF) {
fgetc(fp);
/* parse line */
idx=0;
get_token(line, tok1, &idx, 100); /* opcode */
get_token(line, tok2, &idx, 100); /* mnemonic */
get_token(line, tok3, &idx, 100); /* opcode_len */
/* add line to optab */
add_optab(optab, tok2, parse_op(tok1), (int)(tok3[0]-'0'));
}
fclose(fp);
}
void add_optab(HASH_NODE *optab[20], char mnemonic[10], int opcode, int opcode_len) {
/*
* adds (opcode, mnemonic) into optab
* Arguments:
* HASH_NODE *optab[] - array of 20 linked lists
* char mnemonic[]
* int opcode
* int opcode_len
*/
int bucket;
/* make HASH_NODE */
HASH_NODE* item = (HASH_NODE*)malloc(sizeof(HASH_NODE));
strcpy(item->mnemonic,mnemonic);
item->opcode = opcode;
item->opcode_len = opcode_len;
/* get bucket id */
bucket = hash(mnemonic);
/* push item in front of optab */
item->link = optab[bucket];
optab[bucket] = item;
}
void opcodelist(HASH_NODE *optab[20]) {
/*
* prints out the whole optab
* Arguments:
* HASH_NODE *optab[] - array of 20 linked lists
*/
HASH_NODE *curr;
int arrow; /* flag for printing arrow */
for(int i=0; i<20; i++) {
curr = optab[i];
printf("%d : ",i);
arrow = 0;
while(curr) {
if(arrow) printf(" -> ");
printf("[%s,%.2X]", curr->mnemonic, curr->opcode);
curr = curr->link;
arrow = 1;
}
printf("\n");
}
}
HASH_NODE *search_optab(HASH_NODE *optab[20], char mnemonic[10]) {
/*
* searches the node containing the given mnemonic
* Arguments:
* HASH_NODE *optab[] - array of 20 linked lists
* char mnemonic[] - the key for the search
*/
int bucket = hash(mnemonic);
HASH_NODE *curr = optab[bucket];
while(curr) {
if(!strcmp(mnemonic,curr->mnemonic)) {
/* mnemonic found in optab */
return curr;
}
curr = curr->link;
}
/* mnemonic not found in optab */
return NULL;
}