Exemple #1
0
	bool loadModule(const AST::Module* module)
	{
		// Free any existing memory.
		vmShrinkMemory(vmGrowMemory(0));

		// Initialize the module's requested initial memory.
		if(vmGrowMemory((int32)module->initialNumPagesMemory * AST::numBytesPerPage) != 0)
		{
			std::cerr << "Failed to commit the requested initial memory for module instance (" << module->initialNumPagesMemory*AST::numBytesPerPage/1024 << "KB requested)" << std::endl;
			return false;
		}

		// Copy the module's data segments into VM memory.
		if(module->initialNumPagesMemory * AST::numBytesPerPage >= (1ull<<32)) { throw; }
		for(auto dataSegment : module->dataSegments)
		{
			if(dataSegment.baseAddress + dataSegment.numBytes > module->initialNumPagesMemory * AST::numBytesPerPage)
			{
				std::cerr << "Module data segment exceeds initial memory allocation" << std::endl;
				return false;
			}
			memcpy(instanceMemoryBase + dataSegment.baseAddress,dataSegment.data,dataSegment.numBytes);
		}

		// Generate machine code for the module.
		if(!LLVMJIT::compileModule(module)) { return false; }

		// Initialize the intrinsics.
		initEmscriptenIntrinsics(module);
		initWebAssemblyIntrinsics();
		initWAVMIntrinsics();

		// Call the module's start function.
		if(module->startFunctionIndex != UINTPTR_MAX)
		{
			assert(module->functions[module->startFunctionIndex]->type == AST::FunctionType());
			invokeFunction(module,module->startFunctionIndex,nullptr);
		}

		return true;
	}
	void init()
	{
		LLVMJIT::init();
		initWAVMIntrinsics();
	}