Introducing Newton Scripting language

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

Re: Intruducing the Newton Scripting language.

Postby Stucuk » Fri Dec 21, 2012 12:51 am

So it will work like a DLL, where certain functions in your application are marked as Exports allowing the script system to link to them at run-time? If so i never knew you could do that with an application(Only ever heard of DLL's exporting). Tested it out and it compiles fine(Though i have not found anything about it online, no matter how many searches i do).

Code: Select all
procedure Test;
begin
 //
end;

function Test2 : Integer;
begin
 Result := 1;
end;

exports Test, Test2;
User avatar
Stucuk
 
Posts: 801
Joined: Sat Mar 12, 2005 3:54 pm
Location: Scotland

Re: Intruducing the Newton Scripting language.

Postby Julio Jerez » Fri Dec 21, 2012 9:55 am

Oh yer every time a new funtion is integraated the the host code have to be recompiled, wit teh teh vortual machine the is part of endering SDK will have to be reccompled as well
teh virtual machine will hav eteh table of funtion pointers that is fill each when i load a new script
In a way it will have a funtion of table, liek you said but thsi tabel will be maintaned by the scripth compiler automatically
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Intruducing the Newton Scripting language.

Postby Julio Jerez » Sun Dec 23, 2012 6:08 pm

Now I cover enoght for the optimizing compiler, I am very happy wit the resutl so far, as a test here is a fibonacci function
Code: Select all
function _Fibonacci::_Calculate
   enter 14
   argument _n   ; passed on the stack
   reg3 = [_n]
   reg1 = 0
   if (reg3 != reg1) goto label1
   reg0 = 0
   goto label0
label1:
   reg1 = 1
   if (reg3 != reg1) goto label2
   reg0 = 1
   goto label0
label2:
   reg1 = reg3 - 1
   push reg1
   call _Fibonacci::_Calculate
   reg2 = reg0
   reg1 = reg3 - 2
   push reg1
   call _Fibonacci::_Calculate
   reg0 = reg2 + reg0
label0:
   exit 14
   ret 4



this is what visual studio in release mode produces
Code: Select all
?Calculate@@YAHH@Z
   push   esi
   mov   esi, DWORD PTR _n$[esp]
   cmp   esi, 1
   jne   SHORT $LN1@Calculate
   mov   eax, esi
   pop   esi
   ret   0
$LN1@Calculate:
   lea   eax, DWORD PTR [esi-1]
   push   edi
   test   eax, eax
   jne   SHORT $LN16@Calculate
   xor   edi, edi
   jmp   SHORT $LN11@Calculate
$LN16@Calculate:
   push   eax
   call   ?Calculate@@YAHH@Z         ; Calculate
   add   esp, 4
   mov   edi, eax
$LN11@Calculate:
   lea   eax, DWORD PTR [esi-2]
   test   eax, eax
   jne   SHORT $LN24@Calculate
   mov   eax, edi
   pop   edi
   pop   esi
   ret   0
$LN24@Calculate:
   push   eax
   call   ?Calculate@@YAHH@Z         ; Calculate
   add   esp, 4
   add   eax, edi
   pop   edi
   pop   esi
   ret   0


The script compiler is much superior than visual studio, it generates better code with but it use one more register that VS.
This is very very encoraging to me, now I nee to test if teh code actually run correctly, because the opmizer doe make it much harder to verify correctness.
so now it time to start making the virtual machine to teat teh code as I keep develping the script compiler.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Intruducing the Newton Scripting language.

Postby Julio Jerez » Tue Dec 25, 2012 11:51 am

today I complete my first complete program
Code: Select all
function _Fibonacci::_main
   enter 1
   reg0 = 0
   push reg0
   call _Fibonacci::_Calculate
   reg0 = 1
   push reg0
   call _Fibonacci::_Calculate
   reg0 = 2
   push reg0
   call _Fibonacci::_Calculate
   reg0 = 3
   push reg0
   call _Fibonacci::_Calculate
   reg0 = 4
   push reg0
   call _Fibonacci::_Calculate
   exit 1
   ret 0

function _Fibonacci::_Calculate
   enter 14
   argument _n   ; passed on the stack
   reg3 = [_n]
   reg1 = 0
   if (reg3 != reg1) goto label1
   reg0 = 0
   goto label0
label1:
   reg1 = 1
   if (reg3 != reg1) goto label2
   reg0 = 1
   goto label0
label2:
   reg1 = reg3 - 1
   push reg1
   call _Fibonacci::_Calculate
   reg2 = reg0
   reg1 = reg3 - 2
   push reg1
   call _Fibonacci::_Calculate
   reg0 = reg2 + reg0
label0:
   exit 14
   ret 4


it is very eassy, and even have a bug in main, but it is enough to flesh out the entire language.
I calculate teh fibinacii value of teh first foru ordinal numbers.
I also realice that I nee to write teh runtime library for stuff like print and console inputs.

I realiced already that the compiler need to passes, thsi is why the is a bug in main, the funtion calculate is not know at the time the main is compiled, and therfore teh retur tyye is not found, the optimized see that
the funtion do no have return type and consider the assimn to reg 0 as dead code and delete, thsi is actually good, but it is a bug.
anyaway now I will write the vitual machine run time to test the binay,m and form there I continue improving as i go.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Intruducing the Newton Scripting language.

Postby Julio Jerez » Tue Dec 25, 2012 6:48 pm

now this even better than VS2010 and GCC, it manage to generete better code while using fewer registers,
minimazing the register usage is very imprtnat for CPU with small number of registers like Intell pentium

Code: Select all
function _Fibonacci::_Calculate
   enter 6
   argument _n   ; passed on the stack
   reg2 = [_n]
   reg0 = 0
   if (reg2 != reg0) goto label1
   reg0 = 0
   goto label0
label1:
   reg0 = 1
   if (reg2 != reg0) goto label2
   reg0 = 1
   goto label0
label2:
   reg0 = reg2 - 1
   push reg0
   call _Fibonacci::_Calculate
   reg1 = reg0
   reg0 = reg2 - 2
   push reg0
   call _Fibonacci::_Calculate
   reg0 = reg1 + reg0
label0:
   exit 6
   ret 4
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Intruducing the Newton Scripting language.

Postby Julio Jerez » Sun Dec 30, 2012 10:44 am

finally I have a fully funtional compiler program, it is a standard java quick sort for integers
Code: Select all
   public static void qsort (int[] a, int left, int right)
   {
      int i = left, j = right;
     
      int tmp;
      int pivot = a[(left + right) / 2];
      while (i <= j) {
            while (a[i] < pivot)
                  i++;
            while (a[j] > pivot)
                  j--;

            if (i <= j) {
                  tmp = a[i];
                  a[i] = a[j];
                  a[j] = tmp;
                  i++;
                  j--;
            }
      }
      if (left < j)
      qsort (a, left, j);

      if (i < right)
      qsort (a, i, right);
   }


and this is the output of the script compiler,
Code: Select all
function _Fibonacci::_qsort
   enter 255
   argument _a   ; passed on the stack frame t1
   argument _left   ; passed on the stack frame t2
   argument _right   ; passed on the stack frame t3
   reg5 = [_a]
   reg2 = [_left]
   reg1 = [_right]
   reg7 = reg2
   reg6 = reg1
   reg0 = reg2 + reg1
   reg0 = reg0 / 2
   reg0 = [reg5 + reg0 * 4]
   if (reg2 > reg1) goto label_11
label_1:
   reg7 = reg7 - 1
label_3:
   reg7 = reg7 + 1
   reg4 = [reg5 + reg7 * 4]
   if (reg4 < reg0) goto label_3
   reg6 = reg6 + 1
label_6:
   reg6 = reg6 - 1
   reg3 = [reg5 + reg6 * 4]
   if (reg3 < reg0) goto label_6
   if (reg7 > reg6) goto label_9
   [reg5 + reg7 * 4] = reg3
   [reg5 + reg6 * 4] = reg4
   reg7 = reg7 + 1
   reg6 = reg6 - 1
label_9:
   if (reg7 <= reg6) goto label_1
label_11:
   if (reg2 >= reg6) goto label_12
   push reg6
   push reg2
   push reg5
   call _Fibonacci::_qsort
label_12:
   if (reg7 >= reg1) goto label_14
   push reg1
   push reg7
   push reg5
   call _Fibonacci::_qsort
label_14:
   leave 255
   ret 12

the code output is by far better tha what Visual studion produce in relase mode, but if have a drawback, my script uses minum 8 registers while Visual studion uses only 6
My guess is that Visual studio too uses 8, however the one of these things may be happenings.
1- intel x87 CPU can use base memory location as pseudo registers, the visual studio compute may use a heuristic when the most frequent variable are in CPU register, and the rest in Base memory as registers with a penalty cost
2- the Visual studio compiler is not using based memory as pseudo variables and is simply spilling the least frequent variable to base memory
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Introducing Newton Scripting language

Postby Julio Jerez » Tue Jan 07, 2014 3:44 pm

over the holiday I spend soem tiem working on the script compiler,
I now added the registare allocator, whi handel spills and coallesesing,

to compare how good the optimizer is, I amd caparing to visual studio 2010 optimized, relsei more with all optization on, excect global optimization

here is the result on one small funtions

script code
Code: Select all
public class coalescing
{
   public static int coalescing (int a, int b)
   {
      int d = 0;
      int e = a;
      do {
         d = d + b;
         e = e - 1;
      } while (e>0);
      return d;
   }
}

visual studio 2010 release with all optimation on
Code: Select all
?coalescing@@YAHHH@Z PROC            ; coalescing, COMDAT
   push   ebp
   mov   ebp, esp
   mov   ecx, DWORD PTR _a$[ebp]
   mov   edx, DWORD PTR _b$[ebp]
   xor   eax, eax
$LL3@coalescing:
   dec   ecx
   add   eax, edx
   test   ecx, ecx
   jg   SHORT $LL3@coalescing
   pop   ebp
   ret   0

Newton script compiler output
Code: Select all
function _coalescing::_coalescing
   enter 7
   argument _a   ; passed on the stack frame t1
   argument _b   ; passed on the stack frame t2
   $0 = [_a]
   $2 = [_b]
   $3 = 0
   $1 = 0
label_1:
   $3 = $3 + $2
   $0 = $0 - 1
   if ($0 > $1) goto label_1
   leave 7
   ret 8


Visual studio manage to make the c ode using 3 registers,
Netwon script uses 4 but thes only reason why is that the newton scrip use Risc style instruction set,
so the if statement needs to test against two register, while Intel has special isntrution to test agaisnt a flag.
that seems like an advantage but in the long run it is not, so I am keeping the risc style intruction set.

The Newton scrip comipiler now generates code as optimal as Visual Studio 2010, whan using 7 working regsiter,
and much more efficient than Vs2010 when using 31 registers.

he reason I am interested in issuing code with 7 resisters, is because in the future, the code can be translate to Just in time compiler,
in one pass by simple translatering each instruction to the correspounding assembler instruction on the target machine.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Introducing Newton Scripting language

Postby Forthehorde » Wed Jan 08, 2014 11:14 am

What can we use this for? Sorry I am a bit confuse, is there some use for this for game development? Can we write some kind of scripts to control physics or what?
User avatar
Forthehorde
 
Posts: 26
Joined: Sun Dec 29, 2013 6:17 am

Re: Introducing Newton Scripting language

Postby Julio Jerez » Wed Jan 08, 2014 12:28 pm

you never used a scripting language on a game?
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Introducing Newton Scripting language

Postby Forthehorde » Wed Jan 08, 2014 1:14 pm

Well I used a bit of Lua but not much, is that what newton script will become, a production ready script kit?
User avatar
Forthehorde
 
Posts: 26
Joined: Sun Dec 29, 2013 6:17 am

Re: Introducing Newton Scripting language

Postby Julio Jerez » Wed Jan 08, 2014 2:34 pm

Lua is very cool, and I considered using lua for this, however Lua drawback is that it is not an object orienetd language.
you can write object oreinted code in Lua very much the same way you can write object oriebted code in C or Parcal,
but those lagunges are procedural languages.
I also considered Phyton, but Python is too powefull for its own good, Phyton is Proceduarl and Object oriented,
and that powerful functionality is preciselly what makes it is not so good for a scriting language.
Phys os like C++ and C# and Objective C
I was lokking for somethonmg that is strictlly object orieneted language, and teh only languages that fall on that catgory are
is Java.
so my script is inspuiled by teh Java language.

There is more than jsut a script ready kit.
My immediate used for the is to integrated int the Editor.
This will make adding funtionality to the editor eassier than adding a Plugin in in C++ for each funtionality.

and example of what I mean is for example in yor other topic, you were asking hwo to run autmatically a set of command
when teh edtor was launchd.

here is the answer.
one the script is integrated with the editor, the editor will have a output window where it will print every commnad entered by the user.
then you can get those cammand and make a macro cmmand and save it as a new script.
then for you off line tool you can launch the editor with the scrip as an agument, and it will execute the script on each file you pass it.

tehr will elminate the comlex einteface of eth dScene, dNewton, and all theo ether tool in teh newton toll forder.
teher are verr poweful, but take a lot to learn them.

that's not teh only use, the script is designe to integrate natually with high level languages.

here is an exampleof an inetface with a c or c++ code.

Code: Select all
class string
{
  string ()
    {
    }

   string (byte[] data)
    {
        m_data = new init (data);
    }
   
    // public interface to native code
    native int lenght() const;

    // private interface to native code
    private native void[] init (byte[] data);

    // data is native
    private void[] m_data;
}


the keyword, native intruct the script compiler to generate a header file where teh application will impement functions
Code: Select all
void* init (VirtualMachinen*const machine, void* const parentClass, int argumentCount,  ...);
void* length (VirtualMachinen*const machine, void* const parentClass, int argumentCount,  ...)

from there you the application can do anthing it want, it can call also scripfuntion, vei ateh vistual machine pointe.


once that call is a base call teh application can overwite teh function on any sub class, exampel

Code: Select all
class myString: public string
{
    int lenght() const
    {
        return super::lenth();
     }
}
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Introducing Newton Scripting language

Postby Forthehorde » Wed Jan 08, 2014 6:53 pm

Looks great, I am just wondering will you make some tutorials/doc on its use? Every good script needs nice documentation. I think I will use this for my game instead of Lua.
User avatar
Forthehorde
 
Posts: 26
Joined: Sun Dec 29, 2013 6:17 am

Re: Introducing Newton Scripting language

Postby speps » Wed Jan 15, 2014 3:49 pm

Forthehorde wrote:I think I will use this for my game instead of Lua.


Don't ! Lua is great because it has a lot of documentation and a lot of support if you need help. Also, the code is small and can be ported to every platform you can think of (there is a PS3 version running solely on SPUs). Don't jump on a new fancy tech unless you know it's what you need. For most uses (including physics scripting) Lua is great.

I used AngelScript before (shipped in the physics game called Puddle, released on 360, PS3, WiiU, Android, Vita) and the documentation, setup, integration with C++ and support are just amazing : http://www.angelcode.com/angelscript/ The performance might be a bit bad but it's an object oriented scripting language very close to C++ for the syntax.

I don't understand the goal of Julio as there are loads of alternatives to Lua, including an object oriented spinoff (Squirrel)...
speps
 
Posts: 6
Joined: Thu Dec 19, 2013 4:23 pm

Re: Introducing Newton Scripting language

Postby Julio Jerez » Wed Jan 15, 2014 4:01 pm

speps wrote:
Forthehorde wrote:I don't understand the goal of Julio as there are loads of alternatives to Lua, including an object oriented spinoff (Squirrel)...

Lua is not object oriented.
are those lanaguage more documented and more used than Java or C#
The schipting language is a compiler for Java simular to what Mono does for C#
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Introducing Newton Scripting language

Postby speps » Thu Jan 16, 2014 1:18 pm

I know Lua is not object-oriented, but there are several object-oriented scripting languages out there. Sure they might not be as popular as Java or C# but it's not a reason to create your own in my opinion.

It's great that it runs on the JVM, but I really don't see the point vis-a-vis of Newton Game Dynamics (physics engine).
speps
 
Posts: 6
Joined: Thu Dec 19, 2013 4:23 pm

PreviousNext

Return to General Discussion

Who is online

Users browsing this forum: No registered users and 11 guests

cron