Skip to content

Commit ed303e8

Browse files
add copy and patch jit infrastructure with a stencil compilation pipeline
1 parent 3ff34d8 commit ed303e8

18 files changed

Lines changed: 600 additions & 2 deletions

Cargo.lock

Lines changed: 106 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[workspace]
2-
members = ["gabagool", "gabagool-debug-adapter"]
2+
members = ["gabagool", "gabagool-debug-adapter", "gabagool-stencils"]
33
default-members = ["gabagool"]
44
resolver = "2"
55

gabagool-stencils/Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "gabagool-stencils"
3+
version.workspace = true
4+
edition.workspace = true
5+
authors.workspace = true
6+
license.workspace = true
7+
8+
[dependencies]

gabagool-stencils/src/lib.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
pub fn add(left: u64, right: u64) -> u64 {
2+
left + right
3+
}
4+
5+
#[cfg(test)]
6+
mod tests {
7+
use super::*;
8+
9+
#[test]
10+
fn it_works() {
11+
let result = add(2, 2);
12+
assert_eq!(result, 4);
13+
}
14+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
#ifndef STENCIL_CONTEXT_H
3+
#define STENCIL_CONTEXT_H
4+
5+
#include <stdint.h>
6+
7+
typedef struct StencilContext StencilContext;
8+
9+
// every stencil must be a funciton with this signature
10+
typedef void (*StencilFn)(StencilContext *ctx);
11+
12+
typedef struct {
13+
uint64_t imm0;
14+
uint64_t imm1;
15+
} OpImmediate;
16+
17+
// this is the entire execution state, passed to every stencil
18+
struct StencilContext {
19+
uint64_t *stack;
20+
uint64_t stack_pointer;
21+
uint64_t *locals;
22+
uint64_t *mem_base;
23+
uint64_t mem_len;
24+
uint64_t *globals;
25+
const OpImmediate *imm_table;
26+
const StencilFn *fn_table;
27+
uint32_t pc;
28+
uint8_t snapshot_flag;
29+
uint8_t exit_reason;
30+
uint32_t exit_value;
31+
};
32+
33+
#define EXIT_SNAPSHOT 0
34+
#define EXIT_RETURN 1
35+
36+
#endif

gabagool-stencils/src/stencils.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
#include "stencil_context.h"
3+
4+
void nop(StencilContext *ctx) {
5+
ctx->pc += 1;
6+
7+
if (ctx->snapshot_flag) {
8+
ctx->exit_reason = EXIT_SNAPSHOT;
9+
return;
10+
}
11+
12+
__attribute__((musttail)) return ctx->fn_table[ctx->pc](ctx);
13+
}
14+
15+
void i32_const(StencilContext *ctx) {
16+
ctx->stack[ctx->stack_pointer] = (uint32_t)ctx->imm_table[ctx->pc].imm0;
17+
18+
ctx->stack_pointer += 1;
19+
ctx->pc += 1;
20+
21+
if (ctx->snapshot_flag) {
22+
ctx->exit_reason = EXIT_SNAPSHOT;
23+
return;
24+
}
25+
26+
__attribute__((musttail)) return ctx->fn_table[ctx->pc](ctx);
27+
}
28+
29+
void return_(StencilContext *ctx) { ctx->exit_reason = EXIT_RETURN; }

gabagool/Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,19 @@ license.workspace = true
1010

1111
[dev-dependencies]
1212
insta = "1"
13+
wat = "1"
14+
15+
[dependencies]
16+
libc = { version = "0.2.183", optional = true }
1317

1418
[build-dependencies]
1519
wast = { version = "245.0.1", optional = true }
20+
cc = { version = "1.2.57", optional = true }
21+
object = { version = "0.38.1", optional = true }
22+
1623

1724
[features]
1825
core-tests = ["wast"]
1926
component-tests = []
2027
debugger = []
28+
jit = ["cc", "libc", "object"]

0 commit comments

Comments
 (0)