[Tech Note] Code morphing

General Discussions about the API

[Tech Note] Code morphing

Postby oliver » Sat Jan 01, 2011 5:47 pm

MorphEngine employs code-morphing to provide an easier-to-use environment to the (programming) user, and faster execution.

To make code easier to write and read, MorphEngine changes user-written JavaScript code and extends the calling scope.

For example, this function
Code: Select all
function(x) {
    return sin(x * A);

is normally only valid JavaScript code if sin() and A are both properties of a "global" state object. That is, "window.sin()" and "window.A" in case of client-side JavaScript, would have to be defined.

Of course, MorphEngine does not pollute the global state, and the example code above does not refer to global state.
In a MorphEngine calculator, sin() refers to a real-valued "sin" function in the calculator.functions object (which is not simply Math.sin(), but an implementation that a) knows about the current angular mode b) maps periods of pi/2 to exact values of 0 or 1 respectively, instead of relying on inaccurate factory function results), and "A" might refer to a variable "A" in the current folder, or may be an injected constant.

Using the principal tools of function introspection, the "with" operator, and function re-assignment, MorphEngine extends user-written code to "blend" desired objects into the scope chain.
All of calculator.functions, calculator.vars, calculator.vars[calculator.currentDataCategory], and others, are blended in.

This is transparent to the user. If you inspect a function (via "edit" or "visit"), it will always look as you wrote it.

Another instance of code-morphing is natural math entry syntax.
This definition
Code: Select all
f(x) = sin(x)*A

becomes equivalent to the function above through code-morphing.
Code completion and the steps above are employed to realize this.

Code morphing is meant to be theme in this engine and there will be more instances of it being performed.
Obtaining higher performance through code morphing is a goal.

MorphEngine will soon pick up Generators (a JS 1.7 feature) and morph code to make long-running functions interruptible. (This involves invisibly placing "yield" statements into user-written code, and iterating it to completion, instead of simply executing it.)
Use of generators will also permit the engine to call out to the native environment for certain functions (such as compute-intensive matrix operations). This again will be transparent to the user. Code written today, will simply execute faster tomorrow.

RPL is compiled and executed within MorphEngine's JavaScript environment. RPL commands effectively become JavaScript function calls. The engine will soon go one step further and morph (transform) RPL into JavaScript, and execute that (which implies picking up the speed advantages of a JIT'ed JavaScript compiler). The current approach is already faster than an HP-49 emulator on the same hardware. After code morphing, a factor 20 or so additional improvement will be achieved.
Site Admin
Posts: 433
Joined: Sat May 01, 2010 2:11 pm

Return to General

Who is online

Users browsing this forum: No registered users and 1 guest