Jump to content

Smart bytecode compiler


Recommended Posts

As an experiment in compiler tech, I have written a compiler and bytecode runtime in smart pascal.

While not for everyone, it shows you how to write a parser/tokenizer, assembler and disassembler, and finally a bytecode runtime. This is very handy if you aspire to create.. perhaps a basic language for the web, AI speach where text is compiled into assembly, game engines .. the list is endless.

Anyways, I will include a working assembler / compiler, linker, parser and runtime as a demo in one of our updates (not the next, but most probably the one after that).

If you compile and run the compiled code through kripken's JS to LLVM bitcode converter - you have  a system that runs en-par with native software (asm.js) in the browser. I will write a short tutorial with the basics you need to know. It runs much faster than vanilla JS and generates code that can easily run under C or Delphi.

It's pretty cool! Brilliant for writing larger and more elaborate systems. It is in many ways close to Java 1.0 or the initial CIL (common language runtime).

image.png

Link to post
Share on other sites

Just to make sure there is no missunderstanding: This is a bytecode assembler, disassembler and runtime implemented in Smart pascal. It is not a "smart to bytecode" compiler where you can take a smart project and generate bytecodes. Although i could probably create a bootstrap that injects your smart code into a single .exe file -- but that already exists with NodeWebkit (google it).

What you get in these units is a runtime engine that uses assembly close to 68k and arm, but where variables is a known factor. So moving data between registers and variables is valid. Allocating variables on the fly is likewise allowed.

The next iteration of the project is to add class and instance awareness to the virtual cpu, so that classes can be defined, members defined and instances created and managed. Then you essentially have Java but one you can shape and do whatever you like with.

This is very low-level stuff and to make use of it you need a high-level parser that assembles the binary LDEF files (read: parses whatever language you have made, then uses the assembler to emit bytecodes).

The cool part is that i have also implemented the same in Delphi, so it can run native there or via FPC just about anywhere.

The ASM instructions are designed to be easy to port to asm.js, which is a huge topic i cant cover here -- just wanted to clear up any missunderstanding!

But if you are into languages and perhaps have wanted to create your own lingo, then this demonstrates basic principles. I have avoided too much binary for the "cpu" factors to make it readable and understandable. A faster version can easily be implemented at a later date -- its a journey :)

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...