{"id":331,"date":"2010-08-02T07:00:58","date_gmt":"2010-08-02T12:00:58","guid":{"rendered":"http:\/\/www.nynaeve.net\/?p=331"},"modified":"2019-12-13T17:41:57","modified_gmt":"2019-12-13T22:41:57","slug":"side-projects-nwscript-jit-engine-introduction","status":"publish","type":"post","link":"http:\/\/www.nynaeve.net\/?p=331","title":{"rendered":"Side projects: NWScript JIT engine (introduction)"},"content":{"rendered":"<p>As an interesting experiment in program analysis, one of the side projects that I&#8217;ve been working on in my spare time is an execution environment for <a href=\"http:\/\/en.wikipedia.org\/wiki\/NWScript\" title=\"NWScript Wikipedia Page\">NWScript<\/a>, the scripting language for Neverwinter Nights \/ Neverwinter Nights 2 and the Dragon Age series of games.  (I happen to have spent some time analyzing NWN\/NWN2 recently, hence this project.)<\/p>\n<p><u><em>NWScript 101<\/em><\/u><\/p>\n<p>From a source-level perspective, NWScript provides a C-like programming environment (there are no native arrays or pointers in the NWN\/NWN2 iterations of NWScript).  Like C, NWScript source text must be compiled to an object-code form in order for it to be consumed by the execution environment.<\/p>\n<p>As a result, only the source text compiler for NWScript must deal with the program source code directly; the actual execution environment only deals with the NWScript object code.  I&#8217;ll go into more details as to how the object code is structured in a future posting, but here&#8217;s a brief overview necessary to understand the scope of the project:<\/p>\n<p>The NWScript object code format is essentially simply a raw instruction stream (think an old-style .com file), consisting of a series of NWScript instructions.  The instruction set (in NWN\/NWN2) is a stack-based, strongly-typed, small-complexity set of 45 distinct, variable-length instructions.  (Some instructions perform several overloaded functions based on their operand types.)  All control transfers in NWScript must be performed to fixed locations hardwired at compile time.<\/p>\n<p>These properties combine to make the NWScript object code format readily analyzable (given sufficient effort), despite the fact that it is essentially provided in a completely unstructured, raw instruction-stream based form.<\/p>\n<p><u><em>Project scope<\/em><\/u><\/p>\n<p>The execution environment that I set out on creating involves several different components:<\/p>\n<ul>\n<li>A <em>NWScript VM<\/em>, capable of interpretive execution of the NWScript object code format.  (The BioWare games in question also use an interpretive VM style of execution environment.)  While potentially slow(er), the NWScript VM supports even atypical, hand-built NWScript programs (as opposed to those emitted by a compiler).<\/li>\n<li>A <em>NWScript analyzer<\/em>, capable of inspecting a NWScript program in object code form and performing several levels of analysis, up to producing a high-level, full-featured IR (or intermediate representation) describing the NWScript program&#8217;s behavior.   The IR raising phase implemented by the NWScript analyzer is agnostic to any particular following backend phase, and it provides an opportunity to optimize the generated IR while it is in a backend-neutral form.<\/li>\n<li>A <em>NWScript JIT backend<\/em>, capable of translating the high-level NWScript IR into machine-level instructions for native execution.  The JIT backend is intended to be essentially a simple translator to a JIT environment that consumes NWScript IR (generated by the analysis phase), and ultimately emits native code.  For this project, the first NWScript JIT backend that I have created emits MSIL that can be compiled to native code by the .NET JIT environment.<\/li>\n<\/ul>\n<p>Out of scope for this particular posting series is the concept of a <em>NWScript host<\/em>, which is a program that invokes the NWScript execution environment in order to run a script.  The NWScript host provides a series of extension points (<em>action service handlers<\/em>) that may be called by the script program in order to interface with the outside world.  An example of a NWScript host might be the NWN1\/NWN2 game servers.<\/p>\n<p>An advanced NWScript host might utilize both the NWScript VM and the NWScript JIT system provided by this project (for example, scripts could be interpretively executed by the script VM until background JIT compilation finishes, after which subsequent execution could fall over to the JIT&#8217;d version of the script program).<\/p>\n<p>Next time, I&#8217;ll go into some more gory details on the nature of the NWScript object code.<\/p>\n<p><u><em>Credits<\/em><\/u><\/p>\n<p>Tim Smith (Torlack), who now works at BioWare, documented the NWScript instruction set several years ago, and <a href=\"http:\/\/qstuff.blogspot.com\/\" title=\"Justin's blog\">Justin Olbrantz<\/a> contributed much of the code to the IR-raising portion of this project.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As an interesting experiment in program analysis, one of the side projects that I&#8217;ve been working on in my spare time is an execution environment for NWScript, the scripting language for Neverwinter Nights \/ Neverwinter Nights 2 and the Dragon Age series of games. (I happen to have spent some time analyzing NWN\/NWN2 recently, hence [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[51],"_links":{"self":[{"href":"http:\/\/www.nynaeve.net\/index.php?rest_route=\/wp\/v2\/posts\/331"}],"collection":[{"href":"http:\/\/www.nynaeve.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.nynaeve.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.nynaeve.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.nynaeve.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=331"}],"version-history":[{"count":11,"href":"http:\/\/www.nynaeve.net\/index.php?rest_route=\/wp\/v2\/posts\/331\/revisions"}],"predecessor-version":[{"id":506,"href":"http:\/\/www.nynaeve.net\/index.php?rest_route=\/wp\/v2\/posts\/331\/revisions\/506"}],"wp:attachment":[{"href":"http:\/\/www.nynaeve.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=331"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.nynaeve.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=331"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.nynaeve.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=331"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}