Skip to content

RichardBrown384/mostly-incomplete-mips-jit-tutorial

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

61 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

The Mostly Incomplete MIPS JIT Tutorial

Series of examples outlining how to build a x64 JIT for a MIPS processor.

There is an accompanying series of blog articles here.

About

This repository comprises a series of exercises written while trying to learn about how one might go about trying to dynamically recompile MIPS instructions for the x64 architecture.

There are already some good tutorials online on how one might go and do this for languages like brainfuck and I encourage you to read them. The emphasis here is to provide and outline of you might evolve an interpreting emulator into a JIT.

This repository does not currently contain a fully working JIT for a MIPS processor.

Disclaimers

Currently, the examples only work on architectures that conform to the System V AMD64 calling conventions. This means the examples should work on MacOS and Linux but not on Windows.

Examples

Example 1

In this example we try and figure out how to call a free C++ function from our JIT.

This is so that if we haven't implemented a function in our JIT we can call the interpreted version.

We learn about the x64 16-byte stack alignment requirements.

The discussion here about direct vs indirect calls is also worth reading.

Example 2

In this example we go a step further and pass arguments to our free C++ function.

Example 3

In this example we introduce a simplified MIPS processor and introduce what is called a threaded interpreter. This is to say we do a single pass over the instructions and simply call the underlying interpreted function.

Example 4

In this example we now attempt to re-implement one of the simpler instructions directly by reading and writing the guest registers to and from RAM.

Example 5

In this example we reattempt the previous example using register relative indirect addressing.

Example 6

In this example we attempt a trivial I-Type instruction.

Example 7

In this example we attempt to deal with arithmetic overflow.

Example 8

In this example we attempt to store a register in memory.

Example 9

In this example we attempt to load a register from memory.

Example 10

In this example we attempt to deal with branch instructions.

References

  1. Compiler explorer
  2. Calling conventions
  3. x64 instruction encoding
  4. x64 instruction reference
  5. Online Assembler & Disassembler

About

Series of examples outlining how to build a JIT for a MIPS processor

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published