Vm to Jack Decompiler

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Vm to Jack Decompiler

Yasseen Ramadan
Hello everyone!


I'm currently writing a VM to Jack decompiler. It's in a work in progress rn.


https://github.com/YasseenRamadan2005/Jack-Decompiler/


Write now it mostly works, but you need to severely edit the decomp output because

1. It assumes that ~=, >= and <= are valid, but they only work for my custom compiler, not the provided one

2. Completely ignores types.
3. Pretty sure I messed up something when generating a[b] expression, because in Jack a cannot be an expression.

Regardless, it generates a good base line (get's you like 80% there). From then one it's up to you to put in the rest of the work.

I'll keep hammering away.

https://github.com/YasseenRamadan2005/Jack-Decompiler/tree/main/MY_OS


this contains the generated VM from my Compiler. jack_source is the decomp output no edit

https://github.com/YasseenRamadan2005/Jack-Decompiler/tree/main/HACK_OS

the Vm files are the provided HACK OS VM files.  jack_source is the decomp output no edit.

https://github.com/YasseenRamadan2005/Jack-Decompiler/tree/main/HACK_OS/manual%20labor

This is as far as I got to recreate the provided OS's Jack files. Output.jack is currently broken.


https://github.com/YasseenRamadan2005/Jack-Decompiler/tree/main/Chess


 is the VM files from

https://github.com/AndrewRShort/chess-vm-files



plus the Hack OS VM files. I haven't touched anything in the decomp output.







Right now, I'd say I'm around 60% done with the project. If there's any questions, I'll be happy to answer the best I can.

My decompiler uses a lot of code from my compiler, specifically how I group VM instructions. More on that here
https://yasseenramadan2005.github.io/nand2tetris/2025/06/28/Optimzed-VM-Translator.html

Reply | Threaded
Open this post in threaded view
|

Re: Vm to Jack Decompiler

dolomiti7
This post was updated on .
Hi,

I did something similar. Never published it so far, but since your code is online, I thought it might be a good idea to share it as well. It features some type inference (though imperfect because of lack of information in the VM files) and also includes the original stubs of the OS source files from the nand2tetris project to extract already defined variable names and javadoc comments, merging them into the generated source code. This can be extended by providing a custom decompiler.def file, allowing to add more symbol names. The file is just a concatenation of the .jack stub files (it is embedded in the jar).

The generated code is in most cases directly compilable and tries to overcome some oddities of the provided Jack Compiler (the stock compiler actually does some type checking, but inconsistent and sometimes not correct). In rare cases the type inference may result in code that causes errors with the stock compiler. You may either amend the type manually afterwards, or use the --char-as-int option, trying to enforce more int types, sometimes that helps.

The decompiler will handle VM code that is generated by the stock compiler or compilers generating a similar output. But there are ways to generate valid VM code that would break the decompiler (in fact, it cannot handle some of the VM code of my own compiler...).

Source code and Jar file:
https://github.com/axelkern/Jack-Decompiler