https://github.com/tenderlove/jit_buffer
General purpose buffer for use with building JITs
https://github.com/tenderlove/jit_buffer
jit ruby
Last synced: 12 months ago
JSON representation
General purpose buffer for use with building JITs
- Host: GitHub
- URL: https://github.com/tenderlove/jit_buffer
- Owner: tenderlove
- License: apache-2.0
- Created: 2022-03-11T19:32:08.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2023-10-13T23:41:25.000Z (over 2 years ago)
- Last Synced: 2025-04-15T06:04:36.283Z (about 1 year ago)
- Topics: jit, ruby
- Language: Ruby
- Homepage:
- Size: 30.3 KB
- Stars: 16
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# JIT Buffer
This is a general purpose JIT Buffer object for building JITs in Ruby.
## Usage
Create a JIT Buffer, then specify the size. The JIT Buffer can only be
writeable or executable, but not both at the same time.
It starts life as executable, so you need to mark it writeable before writing.
```ruby
# Make a buffer of size 4096
buffer = JITBuffer.new 4096
# Make writeable
buffer.writeable!
# Write some stuff
buffer.write "hello"
```
If you want to execute the JIT instructions, you need to mark it executable
again.
Here is a full example that only works on ARM64:
```ruby
require "jit_buffer"
# ARM instructions
def movz reg, imm
insn = 0b0_10_100101_00_0000000000000000_00000
insn |= (1 << 31) # 64 bit
insn |= (imm << 5) # immediate
insn |= reg # reg
end
def ret xn = 30
insn = 0b1101011_0_0_10_11111_0000_0_0_00000_00000
insn |= (xn << 5)
insn
end
buffer = JITBuffer.new 4096
# Assemble some instructions
insns = [
movz(0, 42), # mov X0, 42
ret # ret
].pack("L