This repository has been archived by the owner on Oct 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathllforth.cpp
129 lines (117 loc) · 2.94 KB
/
llforth.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
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
#include <iostream>
#include <fstream>
#include <unistd.h>
#include <llvm/PassManager.h>
#include <llvm/CodeGen/Passes.h>
#include <llvm/LinkAllPasses.h>
#include <llvm/Target/TargetData.h>
#include <llvm/Bitcode/ReaderWriter.h>
#include <llvm/PassManager.h>
#include <llvm/CodeGen/Passes.h>
#include <llvm/LinkAllPasses.h>
#include <llvm/Target/TargetData.h>
#include "engine.h"
#include "jit.h"
static bool verbose = false;
static std::string input_filename("");
static std::string output_filename("");
static bool optimize = false;
extern void kk()
{
printf("KAKA\n");
}
void show_help()
{
std::cout << "llforth [OPTIONS]..." << std::endl << std::endl;
std::cout << " -h show help" << std::endl;
std::cout << " -v verbose output" << std::endl;
std::cout << " -o filename object filename" << std::endl;
std::cout << " -O run optimize passes" << std::endl;
std::cout << " -i input filename" << std::endl;
exit(0);
}
void read_args(int argc, char **argv)
{
int c;
extern char *optarg;
extern int optopt;
while((c = getopt(argc, argv, "vho:Oi:")) != -1)
switch(c)
{
case 'h':
show_help();
break;
case 'v':
verbose = true;
break;
case 'o':
output_filename = optarg;
break;
case 'O':
optimize = true;
break;
case 'i':
input_filename = optarg;
break;
case '?':
std::cerr << "Unknown option -" << (char)optopt << std::endl;
}
}
void compile(std::istream &in)
{
JIT::GetSingleton().SetOptimize(optimize);
Engine &e = Engine::GetSingleton();
e.SetInputStream(in);
e.SetVerbose(verbose);
e.MainLoop();
if(output_filename != "")
{
llvm::Module *module = JIT::GetSingleton().GetModule();
if(optimize)
{
llvm::PassManager pm;
pm.add(new llvm::TargetData(module));
pm.add(llvm::createIPSCCPPass());
pm.add(llvm::createGlobalOptimizerPass());
// pm.add(llvm::createInstructionCombiningPass()); doesn't works well with CallingConv::Fast
pm.add(llvm::createFunctionInliningPass());
pm.add(llvm::createGlobalOptimizerPass());
pm.add(llvm::createInstructionCombiningPass());
pm.add(llvm::createGVNPass());
pm.add(llvm::createPromoteMemoryToRegisterPass());
pm.add(llvm::createCFGSimplificationPass());
pm.add(llvm::createDeadArgEliminationPass());
pm.add(llvm::createDeadCodeEliminationPass());
pm.add(llvm::createDeadInstEliminationPass());
pm.add(llvm::createDeadStoreEliminationPass());
pm.add(llvm::createDeadTypeEliminationPass());
pm.run(*module);
}
if(verbose)
module->dump();
// save object file
std::ofstream of(output_filename.c_str(), std::ios::binary);
llvm::WriteBitcodeToFile(module, of);
of.close();
}
}
int main(int argc, char **argv)
{
read_args(argc, argv);
try
{
if(input_filename.size() != 0)
{
std::ifstream is(input_filename.c_str());
compile(is);
}
else
compile(std::cin);
return 0;
}
catch(std::string &error)
{
std::cout << "Exception: " << error << std::endl;
return 1;
}
}