2#ifndef INCLUDED_RUNTIME_CPP_
3#define INCLUDED_RUNTIME_CPP_
31 int RunMain(
const int arg,
const char** argv,
const char** penv);
35 std::string
ReplaceFileExt(
const char * str,
const char *ext_old,
const char *ext_new);
36 std::string
ReadFile(
const char *fileName);
38 std::string
GetDoc(std::string name);
157 typedef std::shared_timed_mutex
Lock;
158#ifndef DEADLOCK_TIMEOUT
159#define DEADLOCK_TIMEOUT std::chrono::seconds(5)
162 typedef std::shared_mutex
Lock;
163#ifdef DEADLOCK_TIMEOUT
164#error The release build does not support DEADLOCK TIMEOUT, as it uses a lock without a time limit, which is faster!
172#define SCOPE_LOCK_DEADLOCK_MESSAGE(THIS) LOG_RUNTIME("Deadlock timeout!")
174#define SCOPE_LOCK_READ_NAME(THIS, VARNAME) ReadLock VARNAME(*THIS, std::defer_lock);\
175 if (!VARNAME.try_lock_for(DEADLOCK_TIMEOUT)) \
176 SCOPE_LOCK_DEADLOCK_MESSAGE(THIS)
178#define SCOPE_LOCK_WRITE_NAME(THIS, VARNAME) WriteLock VARNAME(*THIS, std::defer_lock);\
179 if (!VARNAME.try_lock_for(DEADLOCK_TIMEOUT)) \
180 SCOPE_LOCK_DEADLOCK_MESSAGE(THIS)
182#define SCOPE_LOCK_READ(THIS) SCOPE_LOCK_READ_NAME(THIS, r_lock)
183#define SCOPE_LOCK_WRITE(THIS) SCOPE_LOCK_WRITE_NAME(THIS, w_lock)
221 virtual void Clear();
230 static ObjPtr CreateNative(
const char *proto,
const char *module =
nullptr,
bool lazzy =
false,
const char *mangle_name =
nullptr);
231 static ObjPtr CreateNative(
TermPtr proto,
const char *module =
nullptr,
bool lazzy =
false,
const char *mangle_name =
nullptr);
237 static std::string
Escape(
const std::string_view str);
266 typedef ffi_status
ffi_prep_cif_type(ffi_cif *cif, ffi_abi abi,
unsigned int nargs, ffi_type *rtype, ffi_type **atypes);
267 typedef ffi_status
ffi_prep_cif_var_type(ffi_cif *cif, ffi_abi abi,
unsigned int nfixedargs,
unsigned int ntotalargs, ffi_type *rtype, ffi_type **atypes);
268 typedef void ffi_call_type(ffi_cif *cif,
void (*fn)(
void),
void *rvalue,
void **avalue);
295 static RuntimePtr Init(
int argc = 0,
const char** argv =
nullptr,
const char** penv =
nullptr);
315 static bool ModuleSave(
const FileModule &data,
const std::string_view filename,
const std::string_view module_name =
"");
316 static bool ModuleRead(
FileModule &data,
const std::string_view filename,
const std::string_view modulename =
"");
345 static bool pred_compare(
const std::string &find,
const std::string &str) {
347 while (pos < find.size() && pos < str.size()) {
348 if (find[pos] != str[pos]) {
354 return find.empty() || (pos && find.size() == pos);
357 std::vector<std::wstring>
SelectPredict(std::wstring wstart,
size_t overage_count = 0) {
361 std::vector<std::wstring>
SelectPredict(std::string start,
size_t overage_count = 0);
410 bool CompileCppSource(
const std::string_view source, std::string &out, std::vector<std::string> opts = {});
420 for (
int i = 0; i < argc; i++) {
421 result.push_back(std::string(argv[i]));
428 for (
int i = 0; i < argc; i++) {
434 static std::vector<std::string>
SplitChar(std::string_view str,
const std::string_view delimiter) {
435 std::vector<std::string>
result;
436 while (!str.empty()) {
437 size_t pos = str.find_first_of(delimiter.begin());
439 pos = str.find_first_not_of(delimiter.begin());
440 if (pos == std::string::npos) {
443 str.remove_prefix(pos);
445 if (pos == std::string::npos) {
446 result.push_back(std::string(str.begin(), str.end()));
449 result.push_back(std::string(str.begin(), str.begin() + pos));
450 str.remove_prefix(pos);
456 static std::vector<std::string>
SplitString(
const std::string_view str,
const std::string_view delim) {
458 std::vector<std::string>
result;
462 s.erase(0, s.find_first_not_of(delim.begin()));
464 pos = s.find(delim.begin());
465 if (pos == std::string::npos) {
469 result.push_back(s.substr(0, pos));
472 s.erase(0, s.find_first_not_of(delim.begin()));
481 std::vector<std::string> load;
483 for (
int i = 0; i < args.size(); i++) {
487 if (args[i].find(
"--nlc-search=") == 0) {
489 list = args[i].substr(strlen(
"--nlc-search="));
491 }
else if (args[i].find(
"--nlc-cache-dir=") == 0) {
492 m_cache_dir = args[i].substr(strlen(
"--nlc-cache-dir="));
493 }
else if (args[i].find(
"--nlc-temp-dir=") == 0) {
494 m_temp_dir = args[i].substr(strlen(
"--nlc-temp-dir="));
495 }
else if (args[i].find(
"--nlc-user-dir=") == 0) {
496 m_user_dir = args[i].substr(strlen(
"--nlc-user-dir="));
497 }
else if (args[i].compare(
"--nlc-no-link-rt") == 0) {
500 }
else if (args[i].compare(
"--nlc-no-link-jit") == 0) {
502 }
else if (args[i].compare(
"--nlc-no-runtime") == 0) {
504 }
else if (args[i].compare(
"--nlc-no-dsl") == 0) {
515 }
else if (args[i].compare(
"--nlc-embed-source") == 0) {
516 LOG_RUNTIME(
"Flag '--nlc-embed-source' not implemented!");
518 }
else if (args[i].compare(
"--nlc-no-embed-source") == 0) {
520 }
else if (args[i].compare(
"--nlc-no-import-module") == 0) {
522 }
else if (args[i].compare(
"--nlc-no-import-native") == 0) {
524 }
else if (args[i].compare(
"--nlc-no-eval-enable") == 0) {
526 }
else if (args[i].compare(
"--nlc-no-assert") == 0) {
528 }
else if (args[i].find(
"--nlc-error-limit=") == 0) {
529 std::string value(args[i]);
530 m_diag->m_error_limit =
parseInteger(value.substr(strlen(
"--nlc-error-limit=")).c_str());
534 LOG_RUNTIME(
"System argument '%s' not recognized!", args[i].c_str());
557 for (
auto &elem : load) {
static ObjPtr CreateString(const std::string_view str, Sync *sync=nullptr)
static ObjPtr CreateDict(Sync *sync=nullptr)
static ffi_type * m_ffi_type_uint64
static StringArray MakeMainArgs(int argc, const char **argv, const char **penv)
static ffi_type * m_ffi_type_float
static ffi_prep_cif_type * m_ffi_prep_cif
ObjPtr GetTypeFromString(const std::string_view type, bool *has_error=nullptr)
const RunTime & operator=(const RunTime &)=delete
static ffi_prep_cif_var_type * m_ffi_prep_cif_var
bool RegisterBuildinType(ObjType type, std::vector< std::string > parents)
static std::string Escape(const std::string_view str)
static ObjType BaseTypeFromString(RunTime *rt, const std::string_view text, bool *has_error=nullptr)
static ffi_type * m_ffi_type_uint32
static ffi_type * m_wide_char_type_ffi
ObjPtr CreateDict(const TermPtr &term, Context *runner=nullptr)
static std::vector< std::string > SplitString(const std::string_view str, const std::string_view delim)
bool CompileCppSource(const std::string_view source, std::string &out, std::vector< std::string > opts={})
static std::string GetLastErrorMessage()
static ffi_call_type * m_ffi_call
void GlobalNameBuildinRegister()
static ffi_type * m_ffi_type_sint64
static ffi_type * m_ffi_type_pointer
static ffi_type * m_ffi_type_sint8
ObjPtr ExecModule(const char *module, const char *output, bool cached, Context *ctx)
static ffi_type * m_ffi_type_uint16
static ObjType m_integer_type
bool ParseArgs(StringArray args)
void ffi_call_type(ffi_cif *cif, void(*fn)(void), void *rvalue, void **avalue)
static ffi_type * m_ffi_type_void
static void * GetNativeAddress(void *handle, const std::string_view name)
ObjPtr CreateFunction(TermPtr proto, TermPtr block)
static bool pred_compare(const std::string &find, const std::string &str)
static ObjType m_wide_char_type
bool RegisterSystemFunc(const char *source)
static ffi_type * m_ffi_type_uint8
TermPtr ParseBuildin(const std::string_view src)
bool LoadModuleFromFile(const char *name_str, bool init)
static void * m_ffi_handle
ffi_status ffi_prep_cif_var_type(ffi_cif *cif, ffi_abi abi, unsigned int nfixedargs, unsigned int ntotalargs, ffi_type *rtype, ffi_type **atypes)
static bool ModuleRead(FileModule &data, const std::string_view filename, const std::string_view modulename="")
void CreateArgs_(ObjPtr &args, const TermPtr &term, Context *runner=nullptr)
ObjPtr OpLoadModule(TermPtr term)
static ObjPtr MakeObjArgs(int argc, const char **argv)
static ffi_type * m_ffi_type_double
ObjPtr CreateTensor(const TermPtr &term, Context *runner=nullptr)
StringArray m_module_loader
static std::string NativeNameMangling(const Term *term, RunTime *rt)
static RuntimePtr Init(StringArray args)
static ObjPtr CreateNative(const char *proto, const char *module=nullptr, bool lazzy=false, const char *mangle_name=nullptr)
static std::string ffi_file
RunTime(const StringArray &args={})
RunTime(const RunTime &)=delete
static bool ExpandFileName(std::string &filename)
static std::vector< std::string > SplitChar(std::string_view str, const std::string_view delimiter)
void InitInternal(const StringArray args)
ObjPtr ObjFromModule(ModulePtr module, TermPtr term)
static bool ModuleSave(const FileModule &data, const std::string_view filename, const std::string_view module_name="")
ffi_status ffi_prep_cif_type(ffi_cif *cif, ffi_abi abi, unsigned int nargs, ffi_type *rtype, ffi_type **atypes)
static ffi_type * m_ffi_type_sint32
static ffi_type * m_ffi_type_sint16
bool RegisterBuildinFunc(std::string proto, void *func)
bool UnLoadModule(const char *name_str, bool deinit)
std::vector< std::string > m_search_dir
std::vector< std::wstring > SelectPredict(std::wstring wstart, size_t overage_count=0)
__attribute__((weak)) std
#define LOG_RUNTIME(format,...)
std::shared_ptr< Module > ModulePtr
std::string GetDoc(std::string name)
std::unique_lock< Lock > WriteLock
std::shared_lock< Lock > ReadLock
std::shared_ptr< Term > TermPtr
int64_t parseInteger(const char *str)
std::shared_ptr< Obj > ObjPtr
std::shared_ptr< RunTime > RuntimePtr
std::vector< std::string > StringArray
EXTERN_C int nlc_prinft_sub_(char const *format,...)
std::shared_timed_mutex Lock
std::string MakeLocalName(std::string name)
std::string ReadFile(const char *fileName)
ObjType typeFromString(TermPtr &term, RunTime *rt, bool *has_error)
std::shared_ptr< Diag > DiagPtr
std::string ReplaceFileExt(const char *str, const char *ext_old, const char *ext_new)
int RunMain(const int arg, const char **argv, const char **penv)
std::string GetFileExt(const char *str)
std::string utf8_encode(const std::wstring wstr)
std::string MakeConstructorName(std::string name)
std::string AddDefaultFileExt(const char *str, const char *ext_default)
ObjPtr CreateTensor(torch::Tensor tensor)
void ConvertStringToTensor(const std::string &from, torch::Tensor &to, ObjType type)