Coder Social home page Coder Social logo

johannst / juicebox-asm Goto Github PK

View Code? Open in Web Editor NEW
10.0 1.0 2.0 1.76 MB

x64 jit assembler

Home Page: https://johannst.github.io/juicebox-asm/

License: MIT License

Rust 98.68% Makefile 1.32%
x64 asm assembler assembler-x86 jit jit-assembler rust rust-lang x86-64 assembly

juicebox-asm's Introduction

juicebox-asm

Rust Rustdoc

An x64 jit assembler for learning purpose with the following two main goals:

  • Learn about x64 instruction encoding.
  • Learn how to use the rust type system to disallow invalid operands.

Example

use juicebox_asm::insn::*;
use juicebox_asm::Runtime;
use juicebox_asm::{Asm, Imm32, Label, Reg32::*};

fn main() {
    let mut asm = Asm::new();

    // Reference implementation
    //   int ret = 0;
    //   int n   = 42;
    //
    // loop:
    //   ret += n;
    //   --n;
    //   if (n != 0) goto loop;
    //
    //   return;

    let mut lp = Label::new();

    asm.mov(eax, Imm32::from(0));
    asm.mov(ecx, Imm32::from(42));

    asm.bind(&mut lp);
    asm.add(eax, ecx);
    asm.dec(ecx);
    asm.test(ecx, ecx);
    asm.jnz(&mut lp);

    asm.ret();

    let mut rt = Runtime::new();
    let func = unsafe { rt.add_code::<extern "C" fn() -> u32>(&asm.into_code()) };
    assert_eq!(func(), (0..=42).into_iter().sum());
}

The examples/ folder provides additional examples:

  • fib.rs jit compiles a function to compute the fibonacci sequence.
  • add.rs jit compiles a function calling another function compiled into the example binary.
  • tiny_vm.rs defines a minimal virtual machine (VM) with registers, instructions, data & instruction memory. The VM demonstrates a simple jit compiler which has a jit cache and translates each basic block on first execution when running a VM guest image. For reference an interepter is also implemented.

git hook for local development

The ci/ checks can be run automatically during local development by installing the following pre-commit git hook.

echo 'make -C ci' > .git/hooks/pre-commit
chmod +x .git/hooks/pre-commit

License

This project is licensed under the MIT license.

juicebox-asm's People

Contributors

johannst avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

gmh5225 killvxk

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.