Sphereserver / Source

http://spherecommunity.net
Apache License 2.0
107 stars 58 forks source link

Proposal: Compiled functions / triggers. #15

Open roberpot opened 8 years ago

roberpot commented 8 years ago

Sphere core processing unit

The idea behind this doc is to speedup the execution of sphere scripted triggers and functions. We can create a virtual CPU with a custom instruction set and compile the triggers / functions to that bytecode.

Pros:

Cons:

LOCAL.T = <SRC.STR> + 1
SERV.LOG = <EVAL <LOCAL.T>>

This has to change to:

SERV.LOG = <EVAL <SRC.STR> + 1>

We have to change:

  1. On startup time, scripted functions and triggers are compiled to a byte code like format. All are stored together (code and static data like strings, we will see in depth this later).
  2. On runtime, when a function or trigger is called, we call a scpu method, setting the arguments and call data and run it.
  3. The method that runs the routine returns the status code of execution.

Compilation process:

  1. Load scripted function to the parser (i recommend bison and flex generated parser).
  2. Parser generates a tree from the source code.
  3. Apply some optimizations to the tree (a visitor that constructs a new optimized tree).
  4. Generate the byte code from the tree.
  5. Add to the generated code an end instruction to return default value (for functions / triggers with no return).
  6. Generate the static data from the symbol table.
  7. When all the functions are compiled, we have to update call references to put the correct address.

All the functions / triggers are stored on the same memory, this allows us to do calls over other functions in a simple way.

SCPU Architecture

I recommend Harvard Architecture for simplicity.

User Registers:

Internal Registers:

Memory:

Call procedure:

  1. Push FP to SP.
  2. Push AC to SP.
  3. Increment CC.
  4. Add parameters on reverse order to the stack.
  5. Add the return address to stack.
  6. Set FP = SP.
  7. Jump to the call address.

Ret procedure:

  1. Set A with the return value.
  2. Pop SP until local space is empty.
  3. Pop SP to PC..
  4. Pop parameters from SP.
  5. Decrement CC.
  6. Pop SP to AC.
  7. Pop SP to FP.

Init procedure:

  1. Search address from the called function / trigger and set PC to this address.
  2. Push 0 to SP (false FP).
  3. Push 0 to SP (false AC).
  4. Push args to SP.
  5. Set AC with argument count.
  6. Push 0 to SP (return address from the first call).
  7. Start.

Program Memory Organization:

Const Memory Organization:

Waiting for community to continue work on this.

0x00000000 STOP

Stop execution. Get the A register value and transform to a valid return value and return it.

THIS IS A DRAFT

denizsokmen commented 8 years ago

Can't we just integrate LuaJIT for the maximum speed and still support old scripting for compatibility reasons and sloooowly switch to LUA scripting in time :P ?