Repository navigation
Expand file tree
/
Copy pathllvmexecutablecode.cpp
More file actions
153 lines (123 loc) · 4.74 KB
/
Copy pathllvmexecutablecode.cpp
File metadata and controls
153 lines (123 loc) · 4.74 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
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
// SPDX-License-Identifier: Apache-2.0
#include <scratchcpp/executioncontext.h>
#include <scratchcpp/value_functions.h>
#include <scratchcpp/stage.h>
#include <scratchcpp/iengine.h>
#include <scratchcpp/promise.h>
#include <scratchcpp/thread.h>
#include <iostream>
#include <llvm/Support/Error.h>
#include "llvmexecutablecode.h"
#include "llvmcompilercontext.h"
#include "llvmexecutioncontext.h"
using namespace libscratchcpp;
static const void *END_THREAD_SENTINEL = (void *)0x1;
LLVMExecutableCode::LLVMExecutableCode(LLVMCompilerContext *ctx, function_id_t functionId, const std::string &mainFunctionName, const std::string &resumeFunctionName, Compiler::CodeType codeType) :
m_ctx(ctx),
m_functionId(functionId),
m_mainFunctionName(mainFunctionName),
m_resumeFunctionName(resumeFunctionName),
m_codeType(codeType)
{
assert(m_ctx);
if (m_ctx->jitInitialized()) {
std::cerr << "error: cannot create LLVM code after JIT compiler had been initialized" << std::endl;
assert(false);
}
m_ctx->addCode(this);
}
void LLVMExecutableCode::run(ExecutionContext *context)
{
assert(std::holds_alternative<MainFunctionType>(m_mainFunction));
assert(m_resumeFunction);
LLVMExecutionContext *ctx = getContext(context);
if (ctx->finished())
return;
auto promise = ctx->promise();
if (promise) {
if (promise->isResolved())
ctx->setPromise(nullptr);
else
return;
}
if (ctx->coroutineHandle()) {
bool done = m_resumeFunction(ctx->coroutineHandle());
if (done)
ctx->setCoroutineHandle(nullptr);
ctx->setFinished(done);
} else {
Target *target = ctx->thread()->target();
MainFunctionType f = std::get<MainFunctionType>(m_mainFunction);
void *handle = f(context, target, target->variableData(), target->listData());
if (!handle || handle == END_THREAD_SENTINEL) {
ctx->setFinished(true);
ctx->setCoroutineHandle(nullptr);
} else
ctx->setCoroutineHandle(handle);
}
}
ValueData LLVMExecutableCode::runReporter(ExecutionContext *context)
{
assert(std::holds_alternative<ReporterFunctionType>(m_mainFunction));
Target *target = context->thread()->target();
ReporterFunctionType f = std::get<ReporterFunctionType>(m_mainFunction);
return f(context, target, target->variableData(), target->listData());
}
bool LLVMExecutableCode::runPredicate(ExecutionContext *context)
{
assert(std::holds_alternative<PredicateFunctionType>(m_mainFunction));
Target *target = context->thread()->target();
PredicateFunctionType f = std::get<PredicateFunctionType>(m_mainFunction);
return f(context, target, target->variableData(), target->listData());
}
void LLVMExecutableCode::kill(ExecutionContext *context)
{
LLVMExecutionContext *ctx = getContext(context);
// NOTE: Do not destroy coroutine, it will be destroyed later
// (really bad things happen when a coroutine destroys itself...)
// The code is finished and the coroutine won't be used again until reset() is called
ctx->setFinished(true);
ctx->setPromise(nullptr);
}
void LLVMExecutableCode::reset(ExecutionContext *context)
{
LLVMExecutionContext *ctx = getContext(context);
if (ctx->coroutineHandle()) {
// TODO: Can a script reset itself? (really bad things happen when a coroutine destroys itself...)
m_ctx->destroyCoroutine(ctx->coroutineHandle());
ctx->setCoroutineHandle(nullptr);
}
ctx->setFinished(false);
ctx->setPromise(nullptr);
}
bool LLVMExecutableCode::isFinished(ExecutionContext *context) const
{
return getContext(context)->finished();
}
std::shared_ptr<ExecutionContext> LLVMExecutableCode::createExecutionContext(Thread *thread) const
{
if (!m_ctx->jitInitialized())
m_ctx->initJit();
switch (m_codeType) {
case Compiler::CodeType::Script:
m_mainFunction = m_ctx->lookupFunction<MainFunctionType>(m_mainFunctionName);
break;
case Compiler::CodeType::Reporter:
m_mainFunction = m_ctx->lookupFunction<ReporterFunctionType>(m_mainFunctionName);
break;
case Compiler::CodeType::HatPredicate:
m_mainFunction = m_ctx->lookupFunction<PredicateFunctionType>(m_mainFunctionName);
break;
}
m_resumeFunction = m_ctx->lookupFunction<ResumeFunctionType>(m_resumeFunctionName);
return std::make_shared<LLVMExecutionContext>(m_ctx, thread);
}
function_id_t LLVMExecutableCode::functionId() const
{
return m_functionId;
}
LLVMExecutionContext *LLVMExecutableCode::getContext(ExecutionContext *context)
{
assert(dynamic_cast<LLVMExecutionContext *>(context));
return static_cast<LLVMExecutionContext *>(context);
}