-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeneme.c
573 lines (530 loc) · 20.5 KB
/
deneme.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
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
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include "hashmap.h"
#include "standart.h"
#include "deneme.h"
#include "stack.h"
char mainStatement[257]; /// The given statement
int counter = 0; /// Iterator
char deflastChar[2] = {'\n', 0}; /// Our default last characters
char lastforparanthesis[3] = {')', '\n', 0}; /// Last characters for expressions inside parenthesis
char lastforfunc[3] = {',', '\n', 0}; /// Last characters for expressions inside functions
bool8 error = FALSE; /// Line error checker flag, checks whether there exists an error in the line being read
HashMap variables; /// Hashmap of variables
char *functions[6] = {"xor", "ls", "rs", "lr", "rr", "not"}; /// Array of function names
FILE *output; /// Pointer to the output file
int linecounter = 1; /// Line counter, counts which line we are reading
int variableCounter = 0; /// Variable counter, counts how many LLVM IR variables we've used so far
/// (counts the number of %1,%2,...,%x type of variables, not a,b,x type of variables)
bool8 areThereAnyErrors = FALSE; /// Overall error checker flag, checks whether there exists a line with error
/// (if there are no errors in the entire file, it remains FALSE, otherwise it turns into TRUE)
/// This is our main function. It reads lines until we reach the end of file
int main(int argc, char **argv) {
FILE *input; // Pointer to the input file
/// Opening the input file for reading
input = fopen(argv[1], "r");
/// Changing the file extension to .ll
int i = 0;
while (argv[1][i] != '\0') {
if (argv[1][i] == '.') {
argv[1][i + 1] = 'l';
argv[1][i + 2] = 'l';
argv[1][i + 3] = '\0';
break;
}
i++;
if (i > 1024) {
areThereAnyErrors = TRUE;
printf("Error: File name is too long");
return 0;
}
}
/// Writing to the output file
output = fopen(argv[1], "w");
/// Initialization of HashMap
variables = initializeHashMap();
/// Writing the beginning part of our output file
fprintf(output, "; ModuleID = 'advcalc2ir'\n");
fprintf(output, "declare i32 @printf(i8*, ...)\n");
fprintf(output, "@print.str = constant [4 x i8] c\"%s%s\\0A\\00\"\n", "%", "d");
fprintf(output, "\n");
fprintf(output, "define i32 @main() {\n");
/// Our main loop
while (TRUE) {
// Reseting the line error flag for each line being read
error = FALSE;
if (!fgets(mainStatement, 257, input)) {
/// Writing the ending part of our output file
fprintf(output, "ret i32 0\n");
fprintf(output, "}\n");
// In order to free allocated memories
deconstractor(&variables);
// Closing the input file
fclose(input);
// If no errors occur, just close the output file
if (!areThereAnyErrors) {
fclose(output);
}
// If any errors occur during the reading of the file, close and remove the output file
else {
fclose(output);
remove(argv[1]);
}
return 0;
}
/// The part which reads lines and solves them
counter = 0;
solveStatement();
linecounter++;
}
}
/*
* Our BNF rules for creating statements is (shortly):
* <statement> := <assignment> | <expression>
* <assignment> := <var> = <expression>
* This function checks if the statement is an assignment or expression and proceeds accordingly
*/
void solveStatement() {
char *assignedVar = calloc(257, sizeof(char));
getVar(assignedVar);
int solution;
dismissblank();
// Checks if this is an expression or an assignment
if (*assignedVar == '\0' || (mainStatement[counter] != '=')) {
if (*assignedVar == '\0' && (mainStatement[counter] == '\0')) { return; }
counter = 0;
solution = solveExpr(deflastChar);
if (error) {
// In case of an error, all the instructions on instructions stack will be discarded
// areThereAnyErrors flag will be TRUE, and print "Error on line (linecounter)!"
drain();
areThereAnyErrors = TRUE;
printf("Error on line %d!\n", linecounter);
} else {
// Otherwise, write all the instructions to the output file
writeAllInstructions(output);
// Writing the LLVM IR printing command to the output file
char *printed = pop();
fprintf(output,
"call i32 (i8*, ...) @printf(i8* getelementptr ([4 x i8], [4 x i8]* @print.str, i32 0, i32 0), i32 %s)\n",
printed);
free(printed);
getNewVar();
}
} else {
// Checks if the variable's name is a function name
for (int i = 0; i < 6; i++) {
if (chadstrcmp(functions[i], assignedVar) == 0) {
// In case of an error, all the instructions on instructions stack will be discarded
// areThereAnyErrors flag will be TRUE, and print "Error on line (linecounter)!"
drain();
areThereAnyErrors = TRUE;
printf("Error on line %d!\n", linecounter);
return;
}
}
counter++;
dismissblank();
solution = solveExpr(deflastChar);
if (error) {
// In case of an error, all the instructions on instructions stack will be discarded
// areThereAnyErrors flag will be TRUE, and print "Error on line (linecounter)!"
drain();
areThereAnyErrors = TRUE;
printf("Error on line %d!\n", linecounter); //asdf
} else {
// Once a value has been allocated, it cannot be allocated again. This part checks
// whether a variable has already been allocated or not
if (!isAllocated(&variables, assignedVar)) {
fprintf(output, "%c%s = alloca i32\n", '%', assignedVar); //asdf
}
// Otherwise, write all the instructions to the output file
writeAllInstructions(output);
// Writing the LLVM IR value storing command to the output file
char *printed = pop();
fprintf(output, "store i32 %s, i32* %c%s\n", printed, '%', assignedVar); //asdf
free(printed);
add_new_element(&variables, assignedVar, solution);
}
}
return;
}
/*
* Our EBNF rules for creating strings is (shortly):
* <string> := '('<expression>')' | <var> | func'('<expression>, <expression>')'
* | not'('<expression>')' | <num>
* This function gets the nearest string and solves it
*/
int getString() {
dismissblank();
// Checks if it is a (<expression>)
if (mainStatement[counter] == '(') {
counter++;
dismissblank();
int solution = solveExpr(lastforparanthesis);
if (error) { return 0; }
if (mainStatement[counter] != ')') {
error = TRUE;
return 0;
}
counter++;
return solution;
}
// Checks if it is a <var> | func(<expression>, <expression>) | not(<expression>)
if ((mainStatement[counter] >= 'A' && mainStatement[counter] <= 'Z') ||
(mainStatement[counter] >= 'a' && mainStatement[counter] <= 'z')) {
char str[257];
getVar(str);
int right;
int left;
dismissblank();
// Checks if it is a func(<expression>, <expression>) | not(<expression>)
if (chadstrcmp(str, functions[0]) == 0) {
initialize_sides(&left, &right);
if (error) { return 0; }
// If there are no errors, create the equivalent LLVM IR instruction, push it into irstack
// and add the variable string (%variableCounter) to the (variable) stack
char *newVar = getNewVar();
char *rightSide = pop();
char *leftSide = pop();
char *ins = (char *) malloc(sizeof(char) * 257);
sprintf(ins, "%s = xor i32 %s,%s", newVar, leftSide, rightSide);
irpush(ins);
free(rightSide);
free(leftSide);
push(newVar);
return left ^ right;
}
// Checks if it is a ls(<expression>, <expression>)
if (chadstrcmp(str, functions[1]) == 0) {
initialize_sides(&left, &right);
if (error) { return 0; }
// If there are no errors, create the equivalent LLVM IR instruction, push it into irstack
// and add the variable string (%variableCounter) to the (variable) stack
char *newVar = getNewVar();
char *rightSide = pop();
char *leftSide = pop();
char *ins = (char *) malloc(sizeof(char) * 257);
sprintf(ins, "%s = shl i32 %s,%s", newVar, leftSide, rightSide);
irpush(ins);
free(rightSide);
free(leftSide);
push(newVar);
return left << right;
}
// Checks if it is a rs(<expression>, <expression>)
if (chadstrcmp(str, functions[2]) == 0) {
initialize_sides(&left, &right);
if (error) { return 0; }
// If there are no errors, create the equivalent LLVM IR instruction, push it into irstack
// and add the variable string (%variableCounter) to the (variable) stack
char *newVar = getNewVar();
char *rightSide = pop();
char *leftSide = pop();
char *ins = (char *) malloc(sizeof(char) * 257);
sprintf(ins, "%s = ashr i32 %s,%s", newVar, leftSide, rightSide);
irpush(ins);
free(rightSide);
free(leftSide);
push(newVar);
return left >> right;
}
// Checks if it is a lr(<expression>, <expression>)
if (chadstrcmp(str, functions[3]) == 0) {
initialize_sides(&left, &right);
if (error) { return 0; }
// If there are no errors, create the equivalent LLVM IR instructions, push them into irstack
// and add the variable strings (%variableCounter) to the (variable) stack
char *rightSide = pop();
char *leftSide = pop();
char *newVar_1 = getNewVar();
char *ins = (char *) malloc(sizeof(char) * 257);
sprintf(ins, "%s = urem i32 %s,%s", newVar_1, rightSide, "32");
irpush(ins);
ins = (char *) malloc(sizeof(char) * 257);
char *newVar = getNewVar();
sprintf(ins, "%s = shl i32 %s,%s", newVar, leftSide, newVar_1);
irpush(ins);
char *newVar2 = getNewVar();
ins = (char *) malloc(sizeof(char) * 257);
sprintf(ins, "%s = sub i32 %s,%s", newVar2, "32", newVar_1);
irpush(ins);
char *newVar3 = getNewVar();
ins = (char *) malloc(sizeof(char) * 257);
sprintf(ins, "%s = ashr i32 %s,%s", newVar3, leftSide, newVar2);
irpush(ins);
char *newVar4 = getNewVar();
ins = (char *) malloc(sizeof(char) * 257);
sprintf(ins, "%s = or i32 %s,%s", newVar4, newVar, newVar3);
irpush(ins);
push(newVar4);
free(newVar_1);
free(newVar);
free(newVar2);
free(newVar3);
free(rightSide);
free(leftSide);
right %= 32;
return (left << right) | (left >> (32 - right));
}
// Checks if it is a rr(<expression>, <expression>)
if (chadstrcmp(str, functions[4]) == 0) {
initialize_sides(&left, &right);
if (error) { return 0; }
// If there are no errors, create the equivalent LLVM IR instructions, push them into irstack
// and add the variable strings (%variableCounter) to the (variable) stack
char *rightSide = pop();
char *leftSide = pop();
char *newVar_1 = getNewVar();
char *ins = (char *) malloc(sizeof(char) * 257);
sprintf(ins, "%s = urem i32 %s,%s", newVar_1, rightSide, "32");
irpush(ins);
ins = (char *) malloc(sizeof(char) * 257);
char *newVar = getNewVar();
sprintf(ins, "%s = ashr i32 %s,%s", newVar, leftSide, newVar_1);
irpush(ins);
char *newVar2 = getNewVar();
ins = (char *) malloc(sizeof(char) * 257);
sprintf(ins, "%s = sub i32 %s,%s", newVar2, "32", newVar_1);
irpush(ins);
char *newVar3 = getNewVar();
ins = (char *) malloc(sizeof(char) * 257);
sprintf(ins, "%s = shl i32 %s,%s", newVar3, leftSide, newVar2);
irpush(ins);
char *newVar4 = getNewVar();
ins = (char *) malloc(sizeof(char) * 257);
sprintf(ins, "%s = or i32 %s,%s", newVar4, newVar, newVar3);
irpush(ins);
push(newVar4);
free(newVar_1);
free(newVar);
free(newVar2);
free(newVar3);
free(rightSide);
free(leftSide);
right %= 32;
return (left >> right) | (left << (32 - right));
}
// Checks if it is a not(<expression>)
if (chadstrcmp(str, functions[5]) == 0) {
if (mainStatement[counter] == '(') {
counter++;
dismissblank();
left = solveExpr(lastforparanthesis);
if (error) { return 0; }
if (mainStatement[counter] != ')') {
error = TRUE;
return 0;
}
counter++;
// If there are no errors, create the equivalent LLVM IR instruction, push it into irstack
// and add the variable string (%variableCounter) to the (variable) stack
char *newVar = getNewVar();
char *moment = pop();
char *ins = (char *) malloc(sizeof(char) * 257);
sprintf(ins, "%s = xor i32 %s, -1", newVar, moment);
irpush(ins);
free(moment);
push(newVar);
return ~left;
} else {
error = TRUE;
return 0;
}
}
// If there are no errors, create the equivalent LLVM IR instruction, push it into irstack
// and add the variable string (%variableCounter) to the (variable) stack
char *newVar = getNewVar();
char *ins = (char *) malloc(sizeof(char) * 257);
sprintf(ins, "%s = load i32, i32* %c%s", newVar, '%', str);
irpush(ins);
push(newVar);
return getValue(&variables, str);
}
// Checks if it is a <num>
if ((mainStatement[counter] >= '0' && mainStatement[counter] <= '9')) {
// If there are no errors, add the number to the (variable) stack
char *number = calloc(257, sizeof(char));
int solution = 0;
int tempcounter = 0;
while ((mainStatement[counter] >= '0' && mainStatement[counter] <= '9')) {
solution = 10 * solution + (mainStatement[counter] - '0');
number[tempcounter] = mainStatement[counter];
counter++;
tempcounter++;
}
number[tempcounter] = '\0';
push(number);
return solution;
}
error = TRUE;
return 0;
}
/// Gets operation (stays in the current operator)
/// Returns to precedence of the given operator
int getOper(char op) {
dismissblank();
if (op == '|') { return or; }
if (op == '&') { return and; }
if (op == '+') { return sum; }
if (op == '-') { return min; }
if (op == '*') { return mul; }
if (op == '/') { return sdiv; }
if (op == '%') { return urem; }
error = TRUE;
return notoperator;
}
/// Gets the nearest variable from the counter
void getVar(char *assignedvar) {
char *debug = mainStatement;
int *debug2 = &counter;
dismissblank();
for (int i = 0; i < 257; i++) {
if ((mainStatement[counter] >= 'A' && mainStatement[counter] <= 'Z') ||
(mainStatement[counter] >= 'a' && mainStatement[counter] <= 'z')) {
assignedvar[i] = mainStatement[counter];
counter++;
} else {
assignedvar[i] = 0;
dismissblank();
return;
}
}
}
/// Dismisses all blanks until it hits something else
void dismissblank() {
while (isspace(mainStatement[counter])) {
counter++;
}
}
/// Solves the expression starting from the counter until it reaches a last character
int solveExpr(char *last) {
if (error) { return 0; }
int *counterchecker = &counter;
char *mainStatementchecker = mainStatement;
return solveOp(or, last);
}
/// Solves the operations in a recursive manner, considering precedences as well
int solveOp(int precedence, char *last) {
// As we don't have any operation having bigger precedence than *,/,%
// if the precedence is higher than their precedences, just apply getString
// to obtain the number
if (precedence == mul + 1) { return getString(); }
// In case of error, return 0
if (error) { return 0; }
int right;
char op;
// Solving the left part (recursively)
int left = solveOp(precedence + 1, last);
if (error) { return 0; }
dismissblank();
// The main solving loop
while (TRUE) {
// Checks if the operator is a last character or not
op = mainStatement[counter];
int i = 0;
if (op == 0) {
return left;
}
while (last[i] != 0) {
if (op == last[i]) {
return left;
}
i++;
}
// In case of seeing an operation with different precedence, return the left part
if (getOper(op) != precedence) {
return left;
}
counter++;
// Solving the right part
right = solveOp(precedence + 1, last);
if (error) { return 0; }
dismissblank();
// Creating a new variable, which holds the result of the operation for LLVM IR
char *newVar = getNewVar();
char *rightSide = pop();
char *leftSide = pop();
char *ins = (char *) malloc(sizeof(char) * 257);
// Writing the proper LLVM IR instruction and pushing it into the irstack
if (op == '*') {
left = left * right;
sprintf(ins, "%s = mul i32 %s,%s", newVar, leftSide, rightSide);
} else if (op == '+') {
left = left + right;
sprintf(ins, "%s = add i32 %s,%s", newVar, leftSide, rightSide);
} else if (op == '-') {
left = left - right;
sprintf(ins, "%s = sub i32 %s,%s", newVar, leftSide, rightSide);
} else if (op == '/') {
left = left / right;
sprintf(ins, "%s = sdiv i32 %s,%s", newVar, leftSide, rightSide);
} else if (op == '&') {
left = left & right;
sprintf(ins, "%s = and i32 %s,%s", newVar, leftSide, rightSide);
} else if (op == '|') {
left = left | right;
sprintf(ins, "%s = or i32 %s,%s", newVar, leftSide, rightSide);
} else if (op == '%') {
left = left % right;
sprintf(ins, "%s = urem i32 %s,%s", newVar, leftSide, rightSide);
} else {
error = TRUE;
return 0;
}
// Frees the popped variables
irpush(ins);
free(rightSide);
free(leftSide);
push(newVar);
}
}
/// Gets the first and second parameters of a function
void initialize_sides(int *left, int *right) {
if (mainStatement[counter] == '(') {
counter++;
dismissblank();
*left = solveExpr(lastforfunc);
if (error) { return; }
if (mainStatement[counter] != ',') {
error = TRUE;
return;
}
counter++;
*right = solveExpr(lastforparanthesis);
if (error) { return; }
if (mainStatement[counter] != ')') {
error = TRUE;
return;
}
counter++;
} else {
error = TRUE;
return;
}
}
/// This returns us %(variableCounter) as a string
char *getNewVar() {
char *tempvar = calloc(12, sizeof(char));
variableCounter++;
tempvar[0] = '%';
toString(tempvar + 1, variableCounter);
return tempvar;
}
/// Turns a number into its string form
void toString(char *str, int num) {
int i = 0;
char ablacim[257];
while (num > 0) {
ablacim[i] = (num % 10) + '0';
num /= 10;
i++;
}
str[i] = '\0';
int last = i;
while (i > 0) {
str[last - i] = ablacim[i - 1];
i--;
}
}