@@ -31,44 +31,53 @@ LLVMExecutableCode::LLVMExecutableCode(std::unique_ptr<llvm::Module> module) :
3131 }
3232
3333 // Lookup functions
34- size_t i = 0 ;
35-
36- while (true ) {
37- auto func = m_jit->get ()->lookup (" f" + std::to_string (i));
38-
39- if (func)
40- m_functions.push_back ((FunctionType)(func->getValue ()));
41- else {
42- // Ignore error
43- llvm::consumeError (func.takeError ());
44- break ;
45- }
46-
47- i++;
48- }
34+ m_mainFunction = (MainFunctionType)lookupFunction (" f" );
35+ assert (m_mainFunction);
36+ m_resumeFunction = (ResumeFunctionType)lookupFunction (" resume" );
37+ assert (m_resumeFunction);
4938}
5039
5140void LLVMExecutableCode::run (ExecutionContext *context)
5241{
5342 LLVMExecutionContext *ctx = getContext (context);
5443
55- if (ctx->pos () < m_functions.size ())
56- ctx->setPos (m_functions[ctx->pos ()](context->target ()));
44+ if (ctx->finished ())
45+ return ;
46+
47+ if (ctx->coroutineHandle ()) {
48+ bool done = m_resumeFunction (ctx->coroutineHandle ());
49+
50+ if (done)
51+ ctx->setCoroutineHandle (nullptr );
52+
53+ ctx->setFinished (done);
54+ } else {
55+ void *handle = m_mainFunction (context->target ());
56+
57+ if (!handle)
58+ ctx->setFinished (true );
59+
60+ ctx->setCoroutineHandle (handle);
61+ }
5762}
5863
5964void LLVMExecutableCode::kill (ExecutionContext *context)
6065{
61- getContext (context)->setPos (m_functions.size ());
66+ LLVMExecutionContext *ctx = getContext (context);
67+ ctx->setCoroutineHandle (nullptr );
68+ ctx->setFinished (true );
6269}
6370
6471void LLVMExecutableCode::reset (ExecutionContext *context)
6572{
66- getContext (context)->setPos (0 );
73+ LLVMExecutionContext *ctx = getContext (context);
74+ ctx->setCoroutineHandle (nullptr );
75+ ctx->setFinished (false );
6776}
6877
6978bool LLVMExecutableCode::isFinished (ExecutionContext *context) const
7079{
71- return getContext (context)->pos () >= m_functions. size ();
80+ return getContext (context)->finished ();
7281}
7382
7483void LLVMExecutableCode::promise ()
@@ -84,6 +93,18 @@ std::shared_ptr<ExecutionContext> LLVMExecutableCode::createExecutionContext(Tar
8493 return std::make_shared<LLVMExecutionContext>(target);
8594}
8695
96+ uint64_t LLVMExecutableCode::lookupFunction (const std::string &name)
97+ {
98+ auto func = m_jit->get ()->lookup (name);
99+
100+ if (func)
101+ return func->getValue ();
102+ else {
103+ llvm::errs () << " error: failed to lookup LLVM function: " << toString (func.takeError ()) << " \n " ;
104+ return 0 ;
105+ }
106+ }
107+
87108LLVMExecutionContext *LLVMExecutableCode::getContext (ExecutionContext *context)
88109{
89110 assert (dynamic_cast <LLVMExecutionContext *>(context));
0 commit comments