logo logo

 Back to main page

The NWNX Community Forum

 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
 
JIT compiler for NWScript

 
Post new topic   Reply to topic    nwnx.org Forum Index -> General Discussion
View previous topic :: View next topic  
Author Message
Skywing



Joined: 03 Jan 2008
Posts: 321

PostPosted: Sun Aug 15, 2010 23:37    Post subject: JIT compiler for NWScript Reply with quote

Hi,

The latest public_nwn2dev snapshot I've put up at http://www.nynaeve.net/Skywing/nwn2/nwn2dev/public_nwn2dev.zip includes a just-in-time compiler for NWScript. The just in time compiler takes compiled NWScript files (*.ncs) and turns them into MSIL, when is turned into native code. No modification to the *.ncs files is necessary; the JIT system is intended to be integrated with a script host.

The JIT system is structured in such a way so as to make it possible to write a NWNX plugin to patch the JIT engine into the stock nwserver/nwn2server.exe. While I have yet to start such a project (instead I have integrated the JIT system into my ground-up nwn2server replacement), all the pieces are available now.

Computationally-bound scripts show on the order of a 22x speed improvement with the MSIL JIT (versus the interpretive VM). Scripts that use mostly engine calls see more modest improvements (a typical lower bound would be 2-3x faster, with further speed improvements depending on the ratio of work done in NWScript versus work done in engine code). Note that these measurements were taken against my NWScript VM and not the stock game's VM.

In order to create a NWNX plugin to support the JIT system, the following would need to be done (should someone find this a sufficiently interesting project idea):

- Hook CVirtualMachine::StackPush* and CVirtualMacine::StackPop* to detour to the NWScriptStack::StackPush* and NWScriptStack::StackPop* implementations in NWNScriptLib in the public_nwn2dev drop.
- Hook CVirtualMachine::StackPopCommand_Internal to create a saved state object using NWScriptSaveState (or the NWScriptJITLib wrapper).
- Hook CVirtualMachine::RunScript to JIT code for the script (if it has not been done already) via NWScriptGenerateCode. Once JIT'd code is available, invoke NWScriptExecuteScript to run the script.
- Hook CVirtualMachine::RunScriptSituation to invoke NWScriptScriptSituation. The NWSCRIPT_JITRESUME handle that a previous call to CVirtualMachine::StackPopCommand_Internal created should be used. The best way to do this would be to stuff the handle into the script situation data returned by StackPopCommand_Internal (as we would be taking over all usage of the script situation object anyway)
- Implement INWScriptStack to call into the appropriate CVirtualMachine::StackPush*/StackPop* functions for each StackPush*/StackPop* API. This places parameters on the internal CVirtualMachine stack so that called engine functions can use them.
- Implement INWScriptActions to call the engine function dispatcher (CNWVirtualMachineCommands::ExecuteCommand).
- Implement INWScriptActions to call into CNWVirtualMachineCommands::CreateGameDefinedStructure, CNWVirtualMachineCommands::DestroyGameDefinedStructure and CNWVirtualMachineCommands::GetEqualGameDefinedStructure to manage engine structures according to the EngineStructure/INWScriptActions interface.

This architecture is designed such that scripts push data onto the game's native CVirtualMachine stack when calling engine functions. The calling convention that the JIT system uses for this is compatible with that used by the stock server.
Back to top
View user's profile Send private message
Nob



Joined: 16 Apr 2005
Posts: 21

PostPosted: Wed Aug 31, 2011 8:10    Post subject: Reply with quote

Will this work using Mono under Linux?
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    nwnx.org Forum Index -> General Discussion All times are GMT + 2 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group