-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
169 lines (141 loc) · 4.21 KB
/
main.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
#include <stdio.h>
#include "tok.h"
#include "error.h"
#include "parser.h"
void __main__(int argc, char** argv, const char * const envp, const char *m_name, Ast **stmts)
{
static LLVMBuilderRef builder;
static LLVMModuleRef mod;
builder = LLVMCreateBuilder();
mod = LLVMModuleCreateWithName(m_name);
/**LLVMMemoryBufferRef mem;
LLVMCreateMemoryBufferWithContentsOfFile("test.bc", &mem, NULL);
LLVMModuleRef mod;
LLVMParseBitcode2(mem, &mod);*/
char* error = NULL;
// Declare Malloc Function
LLVMTypeRef malloc_args[] = { LLVMInt64Type() };
LLVMTypeRef mallocty = LLVMFunctionType (
LLVMPointerType(LLVMInt8Type(), 0),
malloc_args,
1,
false
);
LLVMValueRef malloc_fn = __add__("malloc", LLVMAddFunction(mod, "malloc", mallocty));
// Declare PrintF Function
LLVMTypeRef printf_args[] = { LLVMPointerType(LLVMInt8Type(), 0) };
LLVMTypeRef printfty = LLVMFunctionType(
LLVMInt32Type(),
printf_args,
0,
true // VarArgs ...
);
LLVMValueRef printf_fn = __add__("printf", LLVMAddFunction(mod, "printf", printfty));
// Declare StrCat Function
LLVMTypeRef strcat_args[] = { LLVMPointerType(LLVMInt8Type(), 0), LLVMPointerType(LLVMInt8Type(), 0) };
LLVMTypeRef strcat = LLVMFunctionType(
LLVMPointerType(LLVMInt8Type(), 0),
strcat_args,
2,
false
);
__add__("strcat", LLVMAddFunction(mod, "strcat", strcat));
LLVMTypeRef strcmp_args[] = { LLVMPointerType(LLVMInt8Type(), 0), LLVMPointerType(LLVMInt8Type(), 0) };
LLVMTypeRef strcmp = LLVMFunctionType(
LLVMInt32Type(),
strcmp_args,
2,
false
);
__add__("strcmp", LLVMAddFunction(mod, "strcmp", strcmp));
// Declare Gets Function
LLVMTypeRef gets_args[] = { LLVMPointerType(LLVMInt8Type(), 0) };
LLVMTypeRef gets = LLVMFunctionType (
LLVMPointerType(LLVMInt8Type(), 0),
gets_args,
1,
false
);
LLVMValueRef get = __add__("gets", LLVMAddFunction(mod, "gets", gets));
LLVMValueRef input_fn = add_input(mod);
LLVMTypeRef srand_args[] = { LLVMInt32Type()};
LLVMTypeRef srandty = LLVMFunctionType(
LLVMVoidType(),
srand_args,
1,
false
);
LLVMValueRef srand = __add__("srand", LLVMAddFunction(mod, "srand", srandty));
LLVMTypeRef rand_args[] = { LLVMVoidType() };
LLVMTypeRef randty = LLVMFunctionType(
LLVMInt32Type(),
rand_args,
0,
false
);
LLVMValueRef rand = __add__("rand", LLVMAddFunction(mod, "rand", randty));
LLVMTypeRef sprintf_args[] = { LLVMPointerType(LLVMInt8Type(), 0), LLVMPointerType(LLVMInt8Type(), 0) };
LLVMTypeRef sprintfty = LLVMFunctionType(
LLVMInt32Type(),
sprintf_args,
0,
true // VarArgs ...
);
LLVMValueRef sprintf_fn = __add__("sprintf", LLVMAddFunction(mod, "sprintf", sprintfty));
LLVMExecutionEngineRef engine;
LLVMCreateExecutionEngineForModule(&engine, mod, &error);
LLVMTypeRef main_type = LLVMFunctionType (
LLVMVoidType(),
NULL,
0,
false
);
LLVMValueRef main_function = __add__("main", LLVMAddFunction(mod, "main", main_type));
LLVMBasicBlockRef block = LLVMAppendBasicBlock(main_function, "entry");
LLVMPositionBuilderAtEnd(builder, block);
for (int i = 0; i < vec_size(stmts); i++)
resolve_ast(engine, builder, stmts[i]);
LLVMBuildRetVoid(builder);
printf("Module: %s", LLVMPrintModuleToString(mod));
//LLVMPrintModuleToFile(mod, "mains.ll", &error);
LLVMWriteBitcodeToFile(mod, "main.bc");
system("lli main.bc");
}
char *read_file(const char *file)
{
// Create and Open File
FILE *f = NULL;
fopen_s(&f, file, "r");
// Null Check
if (!f)
{
fatal("File could not be opened");
return EXIT_FAILURE;
}
// Get File Length
fseek(f, 0, SEEK_END);
long len = ftell(f);
fseek(f, 0, SEEK_SET);
// Create Buffer
char* buf = calloc(len, 1);
if (buf)
fread(buf, 1, len, f);
fclose(f);
buf[len] = '\0';
return buf;
}
int main(int argc, char** argv, char* const* envp)
{
// Init Reserved Tokens
__initreserved__();
// Create a parser instance
Parser parser = __parser__();
// Read Whole File
char *buf = read_file("main.ice");
// Read all tokens
__readtok__(&parser.tokenizer, buf);
// Run Code
__main__(argc, argv, envp, "mod", __parse__(&parser));
// Free all allocated
__sfree__();
}