-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompiler.c
More file actions
218 lines (203 loc) · 4.99 KB
/
Copy pathcompiler.c
File metadata and controls
218 lines (203 loc) · 4.99 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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdbool.h>
#include <stdint.h>
bool isAnOperator(char* s);
bool validVariable(char* s);
bool legalDigit(char *s);
bool canOptimize(int argc, char *a[]);
void printAssembly(char *a[], int argc);
void optimizedAssembly(char *a[], int argc);
void optimizedPush(char *s);
void optimizedExpression(char* num, char* s);
void printExpression(char* s, int numsOnStack);
void push(char* s);
char* getString(char* num);
int main (int argc, char *argv[]) {
int i, varCount = 0, opCount = 0;
if(argc == 0 || argc == 1 || argc == 3) {
fprintf(stderr, "Sorry, that is not a valid RPN function.\n");
exit(EXIT_FAILURE);
}
for(i = 1; i < argc; i++) {
// printf("%s\n", argv[i]);
char *temp = *argv;
if(!isAnOperator(argv[i]) && !validVariable(argv[i]) && !legalDigit(argv[i])) {
fprintf(stderr, "You can only input any digit, x, y, z, +, -, or \"*\".\n");
exit(EXIT_FAILURE);
}
if(isAnOperator(argv[i]))
opCount++;
else if(legalDigit(argv[i]) || validVariable(argv[i]))
varCount++;
//printf("op: %d var: %d\n", opCount, varCount);
if(opCount == varCount) {
fprintf(stderr, "You did not input a proper expression.\n");
exit(EXIT_FAILURE);
}
temp++;
}
if(!((varCount - opCount) == 1)) {
fprintf(stderr, "You have too few or too many operators.\n");
exit(EXIT_FAILURE);
}
printAssembly(argv, argc);
exit(EXIT_SUCCESS);
}
bool legalDigit(char *str) {
char* temp; int num;
num = strtol(str, &temp, 10);
if(temp == NULL)
return false;
else if(*temp == 0)
return true;
else
return false;
}
bool isAnOperator(char* s) {
//int count = 0;
//while(*s) {
//count++;
//*s++;
//}
//printf("count: %d\n", count);
return ((strcmp(s, "+") == 0 || strcmp(s, "-") == 0 || strcmp(s, "*") == 0));
}
bool validVariable(char* s) {
char *x, *y, *z;
x = "x";
y = "y";
z = "z";
return ((strcmp(s, x) == 0) || (strcmp(s, y) == 0) || (strcmp(s, z) == 0));
}
void printAssembly(char *a[], int argc) {
int i = 2; char *temp = *a; int numsOnStack = 1;
printf(" .globl compute\n");
printf("compute:\n");
if(canOptimize(argc, a))
optimizedAssembly(a, argc);
else {
push(a[1]);
while(a[i]) {
if(isAnOperator(a[i])) {
printExpression(a[i], numsOnStack);
numsOnStack--;
}
else {
push(a[i]);
numsOnStack++;
}
temp++;
i++;
}
printf(" popq %%rax\n");
printf(" retq\n");
}
}
bool canOptimize(int argc, char *a[]) {
int i;
for(i = 3; i < argc; i += 2)
if(!((i % 2 == 1) && isAnOperator(a[i])))
return false;
return true;
}
void printExpression(char* s, int nos) {
char *plus, *minus, *mul;
plus = "+";
minus = "-";
mul = "*";
printf(" popq %%r10\n");
printf(" popq %%r11\n");
if(strcmp(s, plus) == 0)
printf(" addq %%r10, %%r11\n");
else if(strcmp(s, minus) == 0)
printf(" subq %%r10, %%r11\n");
else if(strcmp(s, mul) == 0) {
printf(" imulq %%r10, %%r11\n");
}
printf(" pushq %%r11\n");
}
void push(char* s) {
char *x, *y, *z;
x = "x";
y = "y";
z = "z";
if(strcmp(s, x) == 0)
printf(" pushq %%rdi\n");
else if(strcmp(s, y) == 0)
printf(" pushq %%rsi\n");
else if(strcmp(s, z) == 0)
printf(" pushq %%rdx\n");
else
printf(" pushq $%s\n", s);
}
void optimizedAssembly(char *a[], int argc) {
char *x, *y, *z; char* pass; int i = 3; char *str;
x = "x";
y = "y";
z = "z";
optimizedPush(a[1]);
while(a[i]) {
optimizedExpression(a[i - 1], a[i]);
i += 2;
if(a[i - 1] == NULL || a[i] == NULL)
break;
}
printf(" retq\n");
}
void optimizedPush(char *s) {
char *x, *y, *z;
x = "x";
y = "y";
z = "z";
if(strcmp(s, x) == 0)
printf(" movq %%rdi, %%rax\n");
else if(strcmp(s, y) == 0)
printf(" movq %%rsi, %%rax\n");
else if(strcmp(s, z) == 0)
printf(" movq %%rdx, %%rax\n");
else
printf(" movq $%s, %%rax\n", s);
}
void optimizedExpression(char* num, char* s) {
char *plus, *minus, *mul; char *x, *y, *z; char str[80];
plus = "+"; minus = "-"; mul = "*";
x = "x"; y = "y"; z = "z";
//str = getString(num);
//str = "4";
//printf("%s\n", str);
//malloc(80 * sizeof(str));
if(strcmp(num, x) == 0)
strcpy(str, "%rdi");
else if(strcmp(num, y) == 0)
strcpy(str, "%rsi");
else if(strcmp(num, z) == 0)
strcpy(str, "%rdx");
else {
strcpy(str, "$");
strcat(str, num);
}
if(strcmp(s, plus) == 0)
printf(" addq %s, %%rax\n", str);
else if(strcmp(s, minus) == 0)
printf(" subq %s, %%rax\n", str);
else if(strcmp(s, mul) == 0)
printf(" imulq %s, %%rax\n", str);
}
char* getString(char* var) {
char *x, *y, *z; char str[80];
x = "x"; y = "y"; z = "z";
malloc(sizeof str);
if(strcmp(var, x) == 0)
return "%rdi";
else if(strcmp(var, y) == 0)
return "%rsi";
else if(strcmp(var, z) == 0)
return "%rdx";
else {
strcpy(str, "$");
return strcat(str, var);
}
}