int terra_initwithoptions(lua_State * L, terra_Options * options) { terra_State * T = (terra_State*) lua_newuserdata(L, sizeof(terra_State)); terra_ongc(L, -1, terra_free); assert(T); memset(T,0,sizeof(terra_State)); //some of lua stuff expects pointers to be null on entry T->options = *options; #ifdef __arm__ T->options.usemcjit = true; //force MCJIT since old JIT is partially broken on ARM #endif T->numlivefunctions = 1; T->L = L; assert (T->L); lua_newtable(T->L); lua_insert(L, -2); lua_setfield(L, -2, "__terrastate"); //reference to our T object, so that we can load it from the lua state on other API calls lua_setfield(T->L,LUA_GLOBALSINDEX,"terra"); //create global terra object terra_kindsinit(T); //initialize lua mapping from T_Kind to/from string setterrahome(T->L); //find the location of support files such as the clang resource directory int err = terra_compilerinit(T); if(err) { return err; } err = terra_loadandrunbytecodes(T->L,(const unsigned char*)luaJIT_BC_strict,luaJIT_BC_strict_SIZE, "strict.lua") || terra_loadandrunbytecodes(T->L,(const unsigned char*)luaJIT_BC_asdl,luaJIT_BC_asdl_SIZE, "asdl.lua") #ifndef TERRA_EXTERNAL_TERRALIB || terra_loadandrunbytecodes(T->L,(const unsigned char*)luaJIT_BC_terralib,luaJIT_BC_terralib_SIZE, "terralib.lua"); #else // make it possible to quickly iterate in terralib.lua when developing || luaL_loadfile(T->L,TERRA_EXTERNAL_TERRALIB)
int terra_init(lua_State * L) { terra_State * T = (terra_State*) malloc(sizeof(terra_State)); assert(T); memset(T,0,sizeof(terra_State)); //some of lua stuff expects pointers to be null on entry T->L = L; assert (T->L); lua_newtable(T->L); lua_pushlightuserdata(L, T); lua_setfield(L, -2, "__terrastate"); //reference to our T object, so that we can load it from the lua state on other API calls lua_setfield(T->L,LUA_GLOBALSINDEX,"terra"); //create global terra object terra_kindsinit(T); //initialize lua mapping from T_Kind to/from string int err = terra_loadandrunbytecodes(T->L,luaJIT_BC_strict,luaJIT_BC_strict_SIZE, "strict.lua") || terra_loadandrunbytecodes(T->L,luaJIT_BC_terralib,luaJIT_BC_terralib_SIZE, "terralib.lua"); if(err) { free(T); return err; } terra_cwrapperinit(T); lua_getfield(T->L,LUA_GLOBALSINDEX,"terra"); lua_pushcfunction(T->L,terra_luaload); lua_setfield(T->L,-2,"load"); lua_pushcfunction(T->L,terra_lualoadstring); lua_setfield(T->L,-2,"loadstring"); lua_pushcfunction(T->L,terra_lualoadfile); lua_setfield(T->L,-2,"loadfile"); lua_newtable(T->L); lua_setfield(T->L,-2,"_trees"); //to hold parser generated trees lua_pop(T->L,1); luaX_init(T); err = terra_compilerinit(T); if(err) { free(T); return err; } err = terra_cudainit(T); /* if cuda is not enabled, this does nothing */ if(err) { free(T); return err; } return 0; }
int terra_cudainit(struct terra_State * T) { lua_getfield(T->L,LUA_GLOBALSINDEX,"terra"); lua_getfield(T->L,-1,"cudahome"); const char * cudahome = lua_tostring(T->L,-1); lua_pop(T->L,1); if(!cudahome) return 0; //any early return disables cuda //dynamically load libnvvm libraries, the other libraries are loaded from the cudalib library only when needed llvm::SmallString<256> libnvvmpath; libnvvmpath.append(cudahome); libnvvmpath.append(libnvvm); if(llvm::sys::DynamicLibrary::LoadLibraryPermanently(libnvvmpath.c_str())) return 0; //couldn't find the libnvvm library, do not load cudalib.lua T->cuda = (terra_CUDAState*) malloc(sizeof(terra_CUDAState)); T->cuda->initialized = 0; /* actual CUDA initalization is done on first call to terra_cudacompile */ /* this function just registers all the Lua state associated with CUDA */ lua_pushlightuserdata(T->L,(void*)T); lua_pushcclosure(T->L,terra_toptx,1); lua_setfield(T->L,-2,"toptximpl"); int err = terra_loadandrunbytecodes(T->L, luaJIT_BC_cudalib,luaJIT_BC_cudalib_SIZE, "cudalib.lua"); if(err) { return err; } return 0; }
int terra_cudainit(struct terra_State * T) { lua_getfield(T->L,LUA_GLOBALSINDEX,"terra"); lua_getfield(T->L,-1,"cudalibpaths"); lua_getfield(T->L,-1,"nvvm"); const char * libnvvmpath = lua_tostring(T->L,-1); lua_pop(T->L,2); //path and cudalibpaths if(llvm::sys::DynamicLibrary::LoadLibraryPermanently(libnvvmpath)) { llvm::SmallString<256> err; err.append("failed to load libnvvm at: "); err.append(libnvvmpath); lua_pushstring(T->L,err.c_str()); lua_setfield(T->L,-2,"cudaloaderror"); lua_pop(T->L,1); //terralib return 0; //couldn't find the libnvvm library, do not load cudalib.lua } T->cuda = (terra_CUDAState*) malloc(sizeof(terra_CUDAState)); T->cuda->initialized = 0; /* actual CUDA initalization is done on first call to terra_cudacompile */ /* this function just registers all the Lua state associated with CUDA */ lua_pushlightuserdata(T->L,(void*)T); lua_pushcclosure(T->L,terra_toptx,1); lua_setfield(T->L,-2,"toptximpl"); lua_pop(T->L,1); //terralib int err = terra_loadandrunbytecodes(T->L, (const unsigned char *)luaJIT_BC_cudalib,luaJIT_BC_cudalib_SIZE, "cudalib.lua"); if(err) { return err; } return 0; }
int terra_initwithoptions(lua_State * L, terra_Options * options) { terra_State * T = (terra_State*) lua_newuserdata(L, sizeof(terra_State)); terra_ongc(L, -1, terra_free); assert(T); memset(T,0,sizeof(terra_State)); //some of lua stuff expects pointers to be null on entry T->options = *options; #ifdef __arm__ T->options.usemcjit = true; //force MCJIT since old JIT is partially broken on ARM #endif T->numlivefunctions = 1; T->L = L; assert (T->L); lua_newtable(T->L); lua_insert(L, -2); lua_setfield(L, -2, "__terrastate"); //reference to our T object, so that we can load it from the lua state on other API calls lua_setfield(T->L,LUA_GLOBALSINDEX,"terra"); //create global terra object terra_kindsinit(T); //initialize lua mapping from T_Kind to/from string setterrahome(T->L); //find the location of support files such as the clang resource directory int err = terra_compilerinit(T); if(err) { return err; } err = terra_loadandrunbytecodes(T->L,luaJIT_BC_strict,luaJIT_BC_strict_SIZE, "strict.lua") || terra_loadandrunbytecodes(T->L,luaJIT_BC_terralib,luaJIT_BC_terralib_SIZE, "terralib.lua"); if(err) { return err; } terra_cwrapperinit(T); lua_getfield(T->L,LUA_GLOBALSINDEX,"terra"); lua_pushcfunction(T->L,terra_luaload); lua_setfield(T->L,-2,"load"); lua_pushcfunction(T->L,terra_lualoadstring); lua_setfield(T->L,-2,"loadstring"); lua_pushcfunction(T->L,terra_lualoadfile); lua_setfield(T->L,-2,"loadfile"); lua_newtable(T->L); lua_setfield(T->L,-2,"_trees"); //to hold parser generated trees lua_pushinteger(L, T->options.verbose); lua_setfield(L, -2, "isverbose"); lua_pushinteger(L, T->options.debug); lua_setfield(L, -2, "isdebug"); terra_registerinternalizedfiles(L,-1); lua_pop(T->L,1); //'terra' global luaX_init(T); terra_debuginit(T); err = terra_cudainit(T); /* if cuda is not enabled, this does nothing */ if(err) { return err; } return 0; }