gojit

package module
v0.0.0-...-744fdc3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 21, 2020 License: MIT Imports: 4 Imported by: 0

README

gojit -- pure-golang runtime code-generation

This is the result of my spending the hack day at Gophercon 2014 playing with doing JIT from golang code. This repository contains several packages:

  • gojit

    Contains the basic JIT support -- allocate executable chunks of memory, and convert them into callable golang functions.

  • amd64

    Contains a simplistic amd64 assembler designed for use with gojit

  • bf

    Contains a just-in-time compiler for Brainfuck that demos the above packages

  • gobf

    Contains a binary that provides a command-line interface to bf

Using

gobf can be fetched using

go get github.com/nelhage/gojit/gobf

And then run as gobf file.bf. For some built-in examples:

$ gobf $GOPATH/src/github.com/nelhage/gojit/bf/test/hello.bf
Hello World!
$ gobf $GOPATH/src/github.com/nelhage/gojit/bf/test/hello.bf | gobf $GOPATH/src/github.com/nelhage/gojit/bf/test/rot13.bf
Uryyb Jbeyq!

Portability

This code has been tested on darwin/amd64 and linux/amd64. It is extremely unlikely to work anywhere else.

Documentation

Overview

Package gojit contains basic support for writing JITs in golang. It contains functions for allocating byte slices in executable memory, and converting between such slices and golang function types.

Index

Constants

View Source
const PageSize = 4096

PageSize is the size of a memory page. The len argument to Alloc should be an integer multiple of the page size.

Variables

This section is empty.

Functions

func Addr

func Addr(b []byte) uintptr

Addr returns the address in memory of a byte slice, as a uintptr

func Alloc

func Alloc(len int) ([]byte, error)

Alloc returns a byte slice of the specified length that is marked RWX -- i.e. the memory in it can be both written and executed. This is just a simple wrapper around syscall.Mmap.

len most likely needs to be a multiple of PageSize.

func Build

func Build(b []byte) func()

Build returns a nullary golang function that will result in jumping into the specified byte slice. The slice should in most cases be a slice returned by Alloc, although you could also use syscall.Mmap or syscall.Mprotect directly.

func BuildCgo

func BuildCgo(b []byte) func()

BuildCgo is like Build, but the resulting provided code will be called by way of the cgo runtime. This has the advantage of being much easier and safer to program against (your JIT'd code need only conform to your platform's C ABI), at the cost of significant overhead for each call into your code.

func BuildTo

func BuildTo(b []byte, out interface{})

BuildTo converts a byte-slice into an arbitrary-signatured function. The out argument should be a pointer to a variable of `func' type.

Arguments to the resulting function will be passed to code using a hybrid of the GCC and 6c ABIs: The compiled code will receive, via the GCC ABI, a single argument, a void* pointing at the beginning of the 6c argument frame. For concreteness, on amd64, a func([]byte) int would result in %rdi pointing at the 6c stack frame, like so:

24(%rdi) [ return value ]
16(%rdi) [  cap(slice)  ]
8(%rdi)  [  len(slice)  ]
0(%rdi)  [ uint8* data  ]

func BuildToCgo

func BuildToCgo(b []byte, out interface{})

BuildToCgo is as Build, but uses cgo like BuildCGo

func Release

func Release(b []byte) error

Release frees a buffer allocated by Alloc

Types

type ABI

type ABI int

Directories

Path Synopsis
Package amd64 implements a simple amd64 assembler.
Package amd64 implements a simple amd64 assembler.
Package bf implements a JIT compiler for the Brainfuck programming language.
Package bf implements a JIT compiler for the Brainfuck programming language.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL