Repository navigation
Expand file tree
/
Copy pathmain.c
More file actions
159 lines (140 loc) · 4.41 KB
/
Copy pathmain.c
File metadata and controls
159 lines (140 loc) · 4.41 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
/**
* COMP 412 Lab 2 Driver Code
*/
#include "alloc.h"
void print_help() {
printf("COMP Reference Allocator (Lab 2)\n");
printf("Command Syntax:\n");
printf("\t412alloc [flags] filename \n");
printf("\n");
printf("Required Arguments:\n");
printf("\tk\t\tspecifies the number of registers available to the allocator and is in the range 3 <= k <= 64\n");
printf("\tfilename\tis the path (absolute or relative) to the input file\n");
printf("\n");
printf("Optional Flags:\n");
printf("\t-h\tprints this message\n");
printf("\t-x\trenames registers and prints ILOC IR to command line\n");
printf("\t k\tscans and parses filename ILOC, renames registers, then\n");
printf("\t \tallocates renames ILOC code into k physical registers,\n");
printf("\t \twhere 3 <= k <= 64\n");
}
int main(int argc, char **argv) {
// Flags and their values
uint8_t hflag = 0;
uint8_t xflag = 0;
uint8_t kflag = 0;
uint8_t flags = 0;
int status = 0;
uint32_t k;
ctx* context;
StateIR* ir;
Alloc_State* all;
char *filename;
// Check arguments
for (int i = 1; i < argc; i+=2) {
char* cop = argv[i];
if (cop[0] == '-') {
if (strchr(cop, 'x')) {
xflag = i;
}
if (strchr(cop, 'h')) {
hflag = i;
}
} else if (i == 1 && argc == 2) {
break;
} else {
if (!kflag) {
k = atoi(cop);
if (k >= 3 && k <= 64) {
kflag = i;
continue;
}
}
fprintf(stderr, "ERROR: Bad arguments.\n");
hflag = 1;
break;
}
}
filename = argv[argc - 1];
if (flags > 1) {
fprintf(stderr, "ERROR: Multiple command-line flags found.\nTry '-h' for information on command-line syntax.\n");
}
if ((strlen(filename) < 1) || (argc <= 1)) {
fprintf(stderr, "ERROR: Missing file path.\n");
print_help();
return status;
}
// Process Flags
if (hflag) {
print_help();
return(0);
}
// Check to open file
context = (ctx*) malloc(sizeof(ctx));
if (context == NULL) {
fprintf(stderr, "ERROR: Failed to initialize global context\n");
return -1;
}
if ((status = init_ctx(context, filename))) {
fprintf(stderr, "ERROR: Could not open input file.\n");
print_help();
return status;
}
// Initialize IR Representation
ir = (StateIR*) malloc(sizeof(StateIR));
if (ir == NULL) {
fprintf(stderr, "ERROR: Failed to initialize IR State\n");
fclose(context->file);
return -1;
}
if ((status = init_IR(ir))) {
fprintf(stderr, "ERROR: Failed to initialize IR State\n");
fclose(context->file);
return status;
}
context->ir = ir;
if (xflag) {
if ((status = parse(context)) >= 0) {
if ((status = rename_reg(context->ir)) >= 0) {
print_vr(context->ir);
fclose(context->file);
return 0;
}
printf("Failed to allocate intermediate arrays, run terminates.\n");
}
printf("Due to the syntax error, run terminates.\n");
fclose(context->file);
return status;
}
// TODO: code for lab2
// Initialize Alloc Representation
all = (Alloc_State*) malloc(sizeof(Alloc_State));
if (all == NULL) {
fprintf(stderr, "ERROR: Failed to initialize Alloc State\n");
fclose(context->file);
return -1;
}
if (kflag) {
// printf("in kflag\n");
if ((status = parse(context)) >= 0) {
int max_vr;
if ((max_vr = rename_reg(context->ir)) >= 0) {
// printf("here!\n");
uint32_t max2 = (uint32_t) max_vr;
if (!(status = init_alloc(all, context->ir, max2, k))) {
reallocate(all, max_vr);
print_pr(context->ir);
fclose(context->file);
return 0;
}
}
printf("Failed to allocate intermediate arrays, run terminates.\n");
}
printf("Due to the syntax error, run terminates.\n");
fclose(context->file);
return status;
}
// Close open file
fclose(context->file);
return 0;
}