-
Notifications
You must be signed in to change notification settings - Fork 6
/
bcc.cpp
70 lines (66 loc) · 1.83 KB
/
bcc.cpp
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
// For printf and file used below
#include <inttypes.h>
//#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <BCC.h>
char program[BCC_MAX_PROGRAM_SIZE];
BCC compiler;
void error_callback(char *position, const char *string) {
printf("\nCompilation error: ");
printf("%s", string);
};
int main(int argc, char* argv[]) {
printf("BCC (BIP Compiler Collection) Giovanni Blu Mitolo 2023 \n");
printf("Source: %s \n", argv[1]);
printf("Target: %s \n", argv[2]);
FILE * p_file;
long p_size;
size_t result;
// Open file
p_file = fopen(argv[1], "r");
if(p_file == NULL) {
printf("Unable to open the source file.\n");
exit(-3);
}
// Obtain file size:
fseek(p_file, 0, SEEK_END);
p_size = ftell(p_file);
rewind(p_file);
printf("Source length: ");
std::cout << p_size;
printf("B");
if((sizeof(char) * p_size) >= BCC_MAX_PROGRAM_SIZE) {
printf("\nProgram too big, configure BCC_MAX_PROGRAM_SIZE.");
exit(-2);
}
// Copy the file into the buffer:
result = fread(program, 1, p_size, p_file);
if(result != p_size) {
printf("\nUnable to read source file.");
exit(-1);
}
// Close source file
fclose(p_file);
// Compile program
compiler.error_callback = error_callback;
uint32_t t = BPM_MICROS();
if(!compiler.run(program)) {
printf("\nCompilation failed: check your code and retry\n");
exit(0);
}
t = BPM_MICROS() - t;
// Save program in target file
FILE *o_file = fopen(argv[2], "w");
fwrite(program, sizeof(char), strlen(program), o_file);
// Obtain file size:
fseek(o_file, 0, SEEK_END);
int o_size = ftell(o_file);
rewind(o_file);
printf(", BIP length: ");
std::cout << o_size;
printf("B, reduction: %f", (100 - (100 / ((float)p_size / (float)o_size))));
fclose(o_file);
printf("%% \nCompilation time: %d microseconds \n", (int)(t));
exit(1);
};