- DelphiTools - https://www.delphitools.info -

BASM? Yes we can!

BASM may not be in the Delphi 64 preview [1], but a proof of concept of “BASM-for-DWS” is now available for DWScript in the SVN!

It builds upon recently introduced “language extensions”, to allow “asm” blocks, which will be pre-parsed (to allow BASM-like references to local variables), and then fed to NASM [2] for actual assembling (which you’ll need to download).

const cOne = 1.0;
function RSqrt(x : Float) : Float;
begin
   asm // Result := 1/Sqrt(x);
      fld x;
      fsqrt;
      fld cOne;  // could have used fld1 here
      fdivr;
      fstp Result;
   end;
end;

PrintLn(RSqrt(1/4));

You can have multiple asm sections, intersperse them with regular code, etc.

Language extensions for DWScript

The language extension mechanism allows to hook into the compiler, and handle part of the parsing on a source file. You can add to the language without affecting the DWS compiler core itself, and if not used explicitly, language extensions won’t be compiled, referred, or otherwise impact in any way DWS.

You activate extensions per TDelphiWebScript component, by dropping the relevant extension component (a subclass of TdwsCustomLangageExtension) anywhere you wish, and linking the relevant Script component via the Script property.

More pragmatic targets for extensions would be introducing unsafe features (like DLL imports), compile-time parsed SQL, opcodes for industrial automatons/printers, introducing constructs from other languages that may not fit with the general canon of Pascal, experimenting with alien syntaxes, etc. you name it.

Notes on DWScript’s asmExtension

BASM was picked to experiment with the extension mechanism, as it’s kind of a low hanging fruit: a block delimited by asm and end, with access to the symbol table, that generates a custom expression, and whose actual parsing can be handled by a ready-made tool.

Note that the BASM part introduced here, though usable, will likely remain minimalistic, at least until Delphi 64 comes around.

The assembler uses NASM, and so supports all the instructions NASM supports, though there are a few things to keep in mind:

Of course, using any asm makes the script execution wholly unsafe, that’s why it’s in a language extension.