-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlang.c
98 lines (81 loc) · 1.35 KB
/
lang.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
#include "error.h"
#include "parser.h"
#include "llvmgen.h"
#include <llvm-c/Core.h>
#include <llvm-c/BitWriter.h>
#include <stdio.h>
#include <string.h>
typedef struct
{
bool dumpPT;
bool dumpLLVM;
bool run;
const char* output;
}Config;
int run(Config config, int argc, const char* argv[])
{
Stream* s=openStream(argv[0]);
Node m;
if(!parseModule(s,&m)) {
closeStream(s);
return -1;
}
if(config.dumpPT)
{
dumpNode(&m);
}
LLVMModuleRef llvmModule=compileModule(&m);
closeStream(s);
freeNode(&m);
if(config.dumpLLVM)
{
LLVMDumpModule(llvmModule);
}
if(config.run)
return runModule(llvmModule,argc,argv);
else
{
LLVMWriteBitcodeToFile(llvmModule,config.output);
return 0;
}
}
int main(int argc, const char* argv[])
{
Config config={
.output="out.bc",
.run=true
};
for(int i=1;i<argc;i++)
{
if(strlen(argv[i])>0)
{
if(argv[i][0]=='-')
{
if(strcmp(argv[i],"--dump-pt")==0)
{
config.dumpPT=true;
}
else if(strcmp(argv[i],"--dump-llvm")==0)
{
config.dumpLLVM=true;
}
else if(strcmp(argv[i],"--output")==0)
{
config.run=false;
assert(i+1<argc);
config.output=argv[i+1];
i++;
}
else
{
panic("unknown option: %s",argv[i]);
}
}
else
{
return run(config,argc-i,&argv[i]);
}
}
}
panic("no filename specified");
}