-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
80 lines (72 loc) · 2.75 KB
/
main.cpp
File metadata and controls
80 lines (72 loc) · 2.75 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
#include <llvm/IR/LegacyPassManager.h>
#include <llvm/Support/FileSystem.h>
#include <llvm/Support/Host.h>
#include <llvm/Support/raw_ostream.h>
#include <llvm/Support/TargetRegistry.h>
#include <llvm/Support/TargetSelect.h>
#include <llvm/Target/TargetMachine.h>
#include <llvm/Target/TargetOptions.h>
#include "mast.hpp"
#include <stdio.h>
#include <stdlib.h>
using namespace std;
using namespace llvm;
extern Ast *yyroot;
extern int yyparse();
LLVMContext TheContext;
IRBuilder<> Builder(TheContext);
Module TheModule("ARITHMETIC", TheContext);
map<string, Value*> NamedValues;
int main(int argc, char **argv) {
printf("> ");
yyparse();
printf("%s\n", yyroot->str().c_str());
FunctionType *FT = FunctionType::get(Type::getDoubleTy(TheContext), false);
Function *F = Function::Create(FT, Function::ExternalLinkage, "expression", &TheModule);
BasicBlock *BB = BasicBlock::Create(TheContext, "entry", F);
Builder.SetInsertPoint(BB);
Builder.CreateRet(yyroot->code());
verifyFunction(*F);
FunctionType *PFT = FunctionType::get(Type::getInt32Ty(TheContext), true);
Function *PF = Function::Create(PFT, Function::ExternalLinkage, "printf", &TheModule);
FunctionType *MFT = FunctionType::get(Type::getVoidTy(TheContext), false);
Function *MF = Function::Create(MFT, Function::ExternalLinkage, "main", &TheModule);
BasicBlock *MBB = BasicBlock::Create(TheContext, "entry", MF);
Builder.SetInsertPoint(MBB);
// Value *fmt = ConstantDataArray::getString(TheContext, "%f\n");
Value *fmt = Builder.CreateGlobalStringPtr(StringRef("%f\n"));
Value *expr = Builder.CreateCall(F, {}, "callexpression");
Builder.CreateCall(PF, {fmt, expr}, "callprintf");
Builder.CreateRetVoid();
TheModule.print(errs(), nullptr);
auto TargetTriple = sys::getDefaultTargetTriple();
InitializeAllTargetInfos();
InitializeAllTargets();
InitializeAllTargetMCs();
InitializeAllAsmParsers();
InitializeAllAsmPrinters();
string Error;
auto Target = TargetRegistry::lookupTarget(TargetTriple, Error);
auto CPU = "generic";
auto Features = "";
TargetOptions opt;
auto RM = Optional<Reloc::Model>();
auto TargetMachine = Target->createTargetMachine(TargetTriple, CPU, Features, opt, RM);
TheModule.setDataLayout(TargetMachine->createDataLayout());
TheModule.setTargetTriple(TargetTriple);
auto Filename = "output.o";
std::error_code EC;
raw_fd_ostream dest(Filename, EC, sys::fs::F_None);
legacy::PassManager pass;
auto FileType = TargetMachine::CGFT_ObjectFile;
if (TargetMachine->addPassesToEmitFile(pass, dest, FileType)) {
errs() << "TargetMachine can't emit a file of this type";
return 1;
}
pass.run(TheModule);
dest.flush();
return system("clang output.o -o test.out");
}
void yyerror(const char *s) {
fprintf(stderr, "error: %s\n", s);
}