-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.c
More file actions
151 lines (118 loc) · 4.03 KB
/
main.c
File metadata and controls
151 lines (118 loc) · 4.03 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
#include "syscalls/syscalls.h"
#include "data/format/scanner/scanner.h"
#include "parser/parser.h"
#include "data/format/helpers/token_stream.h"
#include "ir/irgen.h"
#include "semantic/sem_analysis.h"
#include "files/buffer.h"
#include "files/helpers.h"
#include "alloc/allocate.h"
#include "ir/arch_transformer.h"
#include "codegen/code_generator.h"
#include "debug/profiler.h"
#include "semantic/symbol_serialize.h"
typedef struct {
uint32_t file;
uint32_t line_number;
uint32_t column;
} ln_report;
ln_report parse_ln(uint32_t pos, char *content){
ln_report rep = {
.line_number = 1,
.column = 1,
};
for (uint32_t i = 0; i < pos && content[i]; i++){
if (content[i] == '\n'){
rep.column = 1;
rep.line_number++;
} else rep.column++;
}
return rep;
}
const char* outname = "output";
const char* sym_out = "symbols.sym";
static buffer buf;
bool parse_arguments(int argc, char *argv[]){
buf = (buffer){
.buffer = zalloc(0x10000),
.limit = 0x10000,
.buffer_size = 0,
.options = buffer_can_grow,
.cursor = 0,
};
for (int i = 1; i < argc; i++){
if (*argv[i] != '-'){
char *content = read_full_file(argv[i],0);
if (!content){
print("Failed to open file %s",argv[i]);
return false;
}
buffer_write_const(&buf, content);
} else {
if (strcmp(argv[i], "-o") == 0){
i++;
outname = argv[i];
}
}
}
if (!buf.buffer_size){
char *content = read_full_file("street.cred",0);
buffer_write_const(&buf, content);
}
return true;
}
int main(int argc, char *argv[]){
profiler_init();
if (!parse_arguments(argc, argv)) return -1;
Scanner scan = scanner_make(buf.buffer,strlen(buf.buffer));
Tokenizer tk = tokenizer_make(&scan);
tk.skip_type_check = true;
tk.comment_type = TOKENIZER_COMMENT_TYPE_SLASH;
tk.parse_newline = argc && strcmp(argv[1],"-nl") == 0;
TokenStream ts;
ts_init(&ts,&tk);
parser_sm parser = (parser_sm){
.current_rule = 0,
.option = 0,
.sequence = 0,
.scan = &scan,
.scanner_pos = scan.pos,
};
u64 sud = profiler_delta();
parse_result parse_res = parse(buf.buffer, &ts, &parser);
u64 pd = profiler_delta();
if (!parse_res.result){
ln_report ln = parse_ln(parse_res.fail_info.found.pos, buf.buffer);
print("Expected %s, found %v (%i) in %s (l%i:%i in file %i)",token_name(parse_res.fail_info.expected.value),token_to_slice(parse_res.fail_info.found),parse_res.fail_info.found.kind,rule_names[parse_res.fail_info.rule],ln.line_number,ln.column,ln.file);
return -1;
}
print("Syntactic analysis finished %ims",pd);
// symbol_table *existing_symbols = deserialize_table("test.sym");
// debug_ast(parse_res.ast_stack);
symbol_table *symbols = analyze_semantics(parse_res.ast_stack);
if (!symbols) return -1;
u64 sd = profiler_delta();
print("Semantic analysis finished %ims",sd);
serialize_table(symbols, sym_out);
// return 0;
u64 sg = profiler_delta();
codegen ir = gen_code(parse_res.ast_stack, outname);
if (!ir.ptr) return -1;
u64 id = profiler_delta();
print("IR generation finished %ims",id);
// return 0;
ir = perform_transformations(ir);
if (!ir.ptr) return -1;
u64 td = profiler_delta();
generate_code(outname, ir);
u64 gd = profiler_delta();
print("Compilation finished. Total time %llims",sud + pd + sd + sg + id + td + gd);
print("Setup took %llims",sud);
print("Parsing took %llims",pd);
print("Semantic analysis took %llims",sd);
print("Symbol generation took %llims",sg);
print("IR Gen took %llims",id);
print("Transformation took %llims",td);
print("Generation took %llims",gd);
return 0;
}