Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
gazelle-LLVM-prototype
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Naman Dixit
gazelle-LLVM-prototype
Commits
8eb0ee24
Commit
8eb0ee24
authored
May 20, 2022
by
Naman Dixit
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Simplified the interpreter
parent
55852ebc
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
53 additions
and
51 deletions
+53
-51
src/main.cpp
src/main.cpp
+52
-50
version.linux
version.linux
+1
-1
No files found.
src/main.cpp
View file @
8eb0ee24
...
...
@@ -117,7 +117,14 @@ static llvm::codegen::RegisterCodeGenFlags CGF;
static
llvm
::
ExitOnError
ExitOnErr
;
#pragma clang diagnostic pop
int
demo2
(
int
x
);
int
demo2
(
int
x
)
{
return
x
*
2
;
}
int
main
(
int
argc
,
char
*
argv
[],
char
*
const
*
envp
)
{
(
void
)
envp
;
llvm
::
InitLLVM
X
(
argc
,
argv
);
ExitOnErr
.
setBanner
(
"Error: "
);
...
...
@@ -132,9 +139,10 @@ int main(int argc, char *argv[], char * const *envp) {
std
::
string
code_name
=
"test.c"
;
std
::
string
code
=
R"END(
#include <stdio.h>
int
main
(void)
int
demo
(void)
{
printf("Hello, World!\n");
// printf("demo2: %d\n", demo2(20));
return 42;
}
)END"
;
...
...
@@ -144,15 +152,10 @@ int main (void)
args
.
push_back
(
code_name
.
c_str
());
// Prepare DiagnosticEngine
clang
::
DiagnosticOptions
DiagOpts
;
clang
::
TextDiagnosticPrinter
*
textDiagPrinter
=
new
clang
::
TextDiagnosticPrinter
(
llvm
::
errs
(),
&
DiagOpts
);
clang
::
IntrusiveRefCntPtr
<
clang
::
DiagnosticIDs
>
pDiagIDs
;
clang
::
DiagnosticsEngine
*
pDiagnosticsEngine
=
new
clang
::
DiagnosticsEngine
(
pDiagIDs
,
&
DiagOpts
,
textDiagPrinter
);
clang
::
DiagnosticOptions
*
DiagOpts
=
new
clang
::
DiagnosticOptions
;
clang
::
DiagnosticsEngine
DiagnosticsEngine
(
new
clang
::
DiagnosticIDs
,
DiagOpts
,
new
clang
::
TextDiagnosticPrinter
(
llvm
::
errs
(),
DiagOpts
));
// Initialize CompilerInvocation
clang
::
CompilerInvocation
*
CI
=
new
clang
::
CompilerInvocation
();
...
...
@@ -170,14 +173,13 @@ int main (void)
args
.
push_back
(
"/usr/include"
);
llvm
::
ArrayRef
<
const
char
*>
args_array
(
args
);
clang
::
CompilerInvocation
::
CreateFromArgs
(
*
CI
,
args_array
,
*
p
DiagnosticsEngine
);
clang
::
CompilerInvocation
::
CreateFromArgs
(
*
CI
,
args_array
,
DiagnosticsEngine
);
// Map code filename to a memoryBuffer
llvm
::
StringRef
code_data
(
code
);
std
::
unique_ptr
<
llvm
::
MemoryBuffer
>
buffer
=
llvm
::
MemoryBuffer
::
getMemBufferCopy
(
code_data
);
CI
->
getPreprocessorOpts
().
addRemappedFile
(
code_name
,
buffer
.
get
());
// Create and initialize CompilerInstance
clang
::
CompilerInstance
Clang
;
std
::
shared_ptr
<
clang
::
CompilerInvocation
>
CI_shared_ptr
(
CI
);
...
...
@@ -187,7 +189,7 @@ int main (void)
// Set target (I guess I can initialize only the BPF target, but I don't know how)
const
std
::
shared_ptr
<
clang
::
TargetOptions
>
targetOptions
=
std
::
make_shared
<
clang
::
TargetOptions
>
();
targetOptions
->
Triple
=
std
::
string
(
"bpf"
);
clang
::
TargetInfo
*
pTargetInfo
=
clang
::
TargetInfo
::
CreateTargetInfo
(
*
pDiagnosticsEngine
,
targetOptions
);
clang
::
TargetInfo
*
pTargetInfo
=
clang
::
TargetInfo
::
CreateTargetInfo
(
DiagnosticsEngine
,
targetOptions
);
Clang
.
setTarget
(
pTargetInfo
);
// Create and execute action
...
...
@@ -195,6 +197,7 @@ int main (void)
//clang::CodeGenAction *compilerAction = new clang::EmitAssemblyAction();
Clang
.
ExecuteAction
(
*
compilerAction
);
buffer
.
release
();
...
...
@@ -238,22 +241,22 @@ int main (void)
builder
.
setMCPU
(
llvm
::
codegen
::
getCPUStr
());
builder
.
setMAttrs
(
llvm
::
codegen
::
getFeatureList
());
if
(
auto
RM
=
llvm
::
codegen
::
getExplicitRelocModel
())
{
builder
.
setRelocationModel
(
RM
.
getValue
());
}
//
if (auto RM = llvm::codegen::getExplicitRelocModel()) {
//
builder.setRelocationModel(RM.getValue());
//
}
if
(
auto
CM
=
llvm
::
codegen
::
getExplicitCodeModel
())
{
builder
.
setCodeModel
(
CM
.
getValue
());
}
//
if (auto CM = llvm::codegen::getExplicitCodeModel()) {
//
builder.setCodeModel(CM.getValue());
//
}
builder
.
setEngineKind
(
llvm
::
EngineKind
::
Interpreter
);
builder
.
setOptLevel
(
llvm
::
CodeGenOpt
::
None
);
llvm
::
TargetOptions
Options
=
llvm
::
codegen
::
InitTargetOptionsFromCodeGenFlags
(
llvm
::
Triple
());
if
(
llvm
::
codegen
::
getFloatABIForCalls
()
!=
llvm
::
FloatABI
::
Default
)
{
Options
.
FloatABIType
=
llvm
::
codegen
::
getFloatABIForCalls
();
}
//
if (llvm::codegen::getFloatABIForCalls() != llvm::FloatABI::Default) {
//
Options.FloatABIType = llvm::codegen::getFloatABIForCalls();
//
}
builder
.
setTargetOptions
(
Options
);
...
...
@@ -270,10 +273,7 @@ int main (void)
EE
->
DisableLazyCompilation
(
false
);
if
(
llvm
::
StringRef
(
code_name
).
endswith
(
".bc"
))
code_name
.
erase
(
code_name
.
length
()
-
3
);
llvm
::
Function
*
EntryFn
=
Mod
->
getFunction
(
"main"
);
llvm
::
Function
*
EntryFn
=
Mod
->
getFunction
(
"demo"
);
if
(
!
EntryFn
)
{
llvm
::
WithColor
::
error
(
llvm
::
errs
(),
code_name
.
c_str
())
<<
'\''
<<
"main"
<<
"
\'
function not found in module.
\n
"
;
...
...
@@ -282,35 +282,37 @@ int main (void)
errno
=
0
;
int
Result
=
-
1
;
llvm
::
FunctionCallee
Exit
=
Mod
->
getOrInsertFunction
(
"exit"
,
llvm
::
Type
::
getVoidTy
(
Context
),
llvm
::
Type
::
getInt32Ty
(
Context
));
// llvm::FunctionCallee Exit = Mod->getOrInsertFunction("exit",
// llvm::Type::getVoidTy(Context),
// llvm::Type::getInt32Ty(Context));
EE
->
runStaticConstructorsDestructors
(
false
);
(
void
)
EE
->
getPointerToFunction
(
EntryFn
);
//
(void)EE->getPointerToFunction(EntryFn);
// Run main.
Result
=
EE
->
runFunctionAsMain
(
EntryFn
,
std
::
vector
<
std
::
string
>
(),
envp
);
llvm
::
GenericValue
Result
=
EE
->
runFunction
(
EntryFn
,
llvm
::
ArrayRef
<
llvm
::
GenericValue
>
());
uint64_t
ResultValue
=
Result
.
IntVal
.
getZExtValue
();
// Run static destructors.
EE
->
runStaticConstructorsDestructors
(
true
);
if
(
llvm
::
Function
*
ExitF
=
llvm
::
dyn_cast
<
llvm
::
Function
>
(
Exit
.
getCallee
()
->
stripPointerCasts
()))
{
if
(
ExitF
->
getFunctionType
()
==
Exit
.
getFunctionType
())
{
std
::
vector
<
llvm
::
GenericValue
>
Args
;
llvm
::
GenericValue
ResultGV
;
ResultGV
.
IntVal
=
llvm
::
APInt
(
32
,
static_cast
<
uint64_t
>
(
Result
));
Args
.
push_back
(
ResultGV
);
EE
->
runFunction
(
ExitF
,
Args
);
llvm
::
WithColor
::
error
(
llvm
::
errs
(),
code_name
.
c_str
())
<<
"exit("
<<
Result
<<
") returned!
\n
"
;
abort
();
}
llvm
::
WithColor
::
error
(
llvm
::
errs
(),
code_name
.
c_str
())
<<
"exit defined with wrong prototype!
\n
"
;
abort
();
}
return
Result
;
std
::
cout
<<
"Returns: "
<<
ResultValue
<<
std
::
endl
;
// if (llvm::Function *ExitF =
// llvm::dyn_cast<llvm::Function>(Exit.getCallee()->stripPointerCasts())) {
// if (ExitF->getFunctionType() == Exit.getFunctionType()) {
// std::vector<llvm::GenericValue> Args;
// llvm::GenericValue ResultGV;
// ResultGV.IntVal = llvm::APInt(32, static_cast<uint64_t>(Result));
// Args.push_back(ResultGV);
// EE->runFunction(ExitF, Args);
// llvm::WithColor::error(llvm::errs(), code_name.c_str())
// << "exit(" << Result << ") returned!\n";
// abort();
// }
// llvm::WithColor::error(llvm::errs(), code_name.c_str()) << "exit defined with wrong prototype!\n";
// abort();
// }
return
static_cast
<
int
>
(
ResultValue
);
}
version.linux
View file @
8eb0ee24
176
221
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment