Giter Club home page Giter Club logo

dynasm-jit-demo's Introduction

DynASM demo

An example of a practical use of DynASM, the dynamic assembler from the LuaJIT project by Mike Pall:

An indespensable resource for use of DynASM, except for the official sources is the unofficial documentation by Peter Cawley, which includes a tutorial and a reference for the DynASM API and the x86/x86-64 instructions:

A great blog post introducing JITs and featuring DynASM has been written by Josh Haberman:

This example demonstrates a template JIT compilation of a program for a very simple stack machine. The bytecode design, an example program and the original interpreter implemenation is due to Martin Dørum:

The demo features:

  • dynasm directory: subset of DynASM for the x86-64 architecture. By Mike Pall (MIT license).

  • dynasm/minilua.c: minified, single file PUC Lua 5.1 from PUC-Rio (MIT license) with bit operation extensions by Mike Pall.

  • src/demo.c: single file demo showing the use of DynASM. Heavily based on Peter Cawley's unofficiall DynASM documentation (CC BY 3.0) and on the code from Martin Dørum's blog post (shamefully stolen).

  • meson.build: a build for for the Meson build system. It compiles minilua.c into a Lua interpreter, runs with it dynasm/dynasm.lua Lua script, which preprocesses the src/demo.c C file into code with calls to DynASM C API. The DynASM C runtime is compiled directly into src/demo.c through includes of dynasm/dasm_proto.h and dynasm/dasm_x86.h.

The inclusion of minilua makes the project self contained---it needs just a C compiler (and Meson). If your Lua interpretere supports bit operations, you can use it as well (in particular luajit works).

The src/demo.c is thoroughly commented. It can be overwhelming though and the example is not very realistic. Several iterations are made on top, to show possible improvements. These are currently without comments and are contained in the following git branches:

  • master - thoroughly documented compiliation of bytecode for a stack machine, which uses x86-64 stack as the stack.

  • part1 - same as master, but without comments.

  • part2 - a custom stack is allocated and custom stack pointer is managed.

  • part3 - DynASM "type maps" (.type directives) are used to improve the readability of the assembly.

  • part4 - a state struct is introduced, which is used to hold DynASM state as well as other state for a possible interpreter, which JITs some bytecodes and interpreters others. The example is too simplistic to show anything real, but at least shows how to integrate custom state struct with DynASM and how to move state from it to registers.

Some other JIT/x86-64 resources I found useful:

x86-64 basics and ABI:

int3 and debugging:

Issues with relative offsets, global variables, linking (or lack thereof in case of JITed code):

Debugging issues:

Starting with JITs:

GDB:

Useful things to have in GDB config files:

# ~/.config/gdb/gdbearlyinit

# Don't show license and help information on each startup
set startup-quietly on
# ~/.config/gdb/gdbinit

# Don't ask for confirmations.
set confirm off

# Don't be verbose.
set verbose off

# Don't show thread events.
set print thread-events off

# Don't stop when more than one screen is output.
set pagination off

# Enable pretty prints.
set print pretty on

# Demangle C++ names.
set print asm-demangle on

# Save history.
set history save on
set history filename ~/.local/share/gdb/history

# Use Intel syntax.
set disassembly-flavor intel

See also: https://www.reddit.com/r/C_Programming/comments/12xhiie/how_do_you_use_gdb_without_the_tui_are_there/jhiznhj/

dynasm-jit-demo's People

Contributors

vlasakm avatar

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.