Add zstd
This commit is contained in:
parent
5da4e192ec
commit
b34d4d95b7
17
Makefile
17
Makefile
|
@ -72,6 +72,7 @@ CARGO_FLAGS=\
|
|||
-C linker=tools/rust-lld-wrapper \
|
||||
-C link-args="--import-table --global-base=262144 $(STRIP_DEBUG_FLAG)" \
|
||||
-C link-args="build/softfloat.o" \
|
||||
-C link-args="build/zstddeclib.o" \
|
||||
--verbose
|
||||
|
||||
CORE_FILES=const.js config.js io.js main.js lib.js ide.js pci.js floppy.js \
|
||||
|
@ -155,14 +156,14 @@ src/rust/gen/analyzer.rs: $(ANALYZER_DEPENDENCIES)
|
|||
src/rust/gen/analyzer0f.rs: $(ANALYZER_DEPENDENCIES)
|
||||
./gen/generate_analyzer.js --output-dir build/ --table analyzer0f
|
||||
|
||||
build/v86.wasm: $(RUST_FILES) build/softfloat.o Cargo.toml
|
||||
build/v86.wasm: $(RUST_FILES) build/softfloat.o build/zstddeclib.o Cargo.toml
|
||||
mkdir -p build/
|
||||
-ls -lh build/v86.wasm
|
||||
cargo +nightly rustc --release $(CARGO_FLAGS)
|
||||
mv build/wasm32-unknown-unknown/release/v86.wasm build/v86.wasm
|
||||
ls -lh build/v86.wasm
|
||||
|
||||
build/v86-debug.wasm: $(RUST_FILES) build/softfloat.o Cargo.toml
|
||||
build/v86-debug.wasm: $(RUST_FILES) build/softfloat.o build/zstddeclib.o Cargo.toml
|
||||
mkdir -p build/
|
||||
-ls -lh build/v86-debug.wasm
|
||||
cargo +nightly rustc $(CARGO_FLAGS)
|
||||
|
@ -170,10 +171,18 @@ build/v86-debug.wasm: $(RUST_FILES) build/softfloat.o Cargo.toml
|
|||
ls -lh build/v86-debug.wasm
|
||||
|
||||
build/softfloat.o: lib/softfloat/softfloat.c
|
||||
clang -c \
|
||||
clang -c -Wall \
|
||||
--target=wasm32 -Os -flto -nostdlib -fvisibility=hidden -ffunction-sections -fdata-sections \
|
||||
-DSOFTFLOAT_FAST_INT64 -DINLINE_LEVEL=5 -DSOFTFLOAT_FAST_DIV32TO16 -DSOFTFLOAT_FAST_DIV64TO32 \
|
||||
-O2 -o build/softfloat.o lib/softfloat/softfloat.c
|
||||
-o build/softfloat.o \
|
||||
lib/softfloat/softfloat.c
|
||||
|
||||
build/zstddeclib.o: lib/zstd/zstddeclib.c
|
||||
clang -c -Wall \
|
||||
--target=wasm32 -Os -flto -nostdlib -fvisibility=hidden -ffunction-sections -fdata-sections \
|
||||
-I /usr/include \
|
||||
-o build/zstddeclib.o \
|
||||
lib/zstd/zstddeclib.c
|
||||
|
||||
clean:
|
||||
-rm build/libv86.js
|
||||
|
|
13526
lib/zstd/zstddeclib.c
Normal file
13526
lib/zstd/zstddeclib.c
Normal file
File diff suppressed because it is too large
Load diff
|
@ -287,6 +287,12 @@ CPU.prototype.wasm_patch = function(wm)
|
|||
this.codegen_finalize_finished = get_import("codegen_finalize_finished");
|
||||
|
||||
this.allocate_memory = get_import("allocate_memory");
|
||||
|
||||
this.zstd_create_ctx = get_import("zstd_create_ctx");
|
||||
this.zstd_get_src_ptr = get_import("zstd_get_src_ptr");
|
||||
this.zstd_free_ctx = get_import("zstd_free_ctx");
|
||||
this.zstd_read = get_import("zstd_read");
|
||||
this.zstd_read_free = get_import("zstd_read_free");
|
||||
};
|
||||
|
||||
CPU.prototype.jit_force_generate = function(addr)
|
||||
|
|
|
@ -35,3 +35,4 @@ mod softfloat;
|
|||
mod state_flags;
|
||||
mod util;
|
||||
mod wasmgen;
|
||||
mod zstd;
|
||||
|
|
105
src/rust/zstd.rs
Normal file
105
src/rust/zstd.rs
Normal file
|
@ -0,0 +1,105 @@
|
|||
use std::alloc;
|
||||
|
||||
extern "C" {
|
||||
fn ZSTD_createDStream() -> u32;
|
||||
fn ZSTD_freeDStream(ctx: u32) -> i32;
|
||||
fn ZSTD_decompressStream_simpleArgs(
|
||||
ctx: u32,
|
||||
dst: *mut u8,
|
||||
dstCapacity: u32,
|
||||
dstPos: *mut u32,
|
||||
src: *const u8,
|
||||
srcSize: u32,
|
||||
srcPos: *mut u32,
|
||||
) -> i32;
|
||||
|
||||
fn ZSTD_isError(err: i32) -> bool;
|
||||
}
|
||||
|
||||
const MALLOC_ALIGN: usize = 16;
|
||||
|
||||
// malloc and free are needed by the zstd library
|
||||
#[no_mangle]
|
||||
pub unsafe fn v86_malloc(size: u32) -> u32 {
|
||||
let layout = alloc::Layout::from_size_align(size as usize + 4, MALLOC_ALIGN).unwrap();
|
||||
let addr = alloc::alloc(layout);
|
||||
*(addr as *mut u32) = size as u32;
|
||||
addr as u32 + 4
|
||||
}
|
||||
#[no_mangle]
|
||||
pub unsafe fn v86_free(addr: u32) {
|
||||
let size = *((addr - 4) as *mut u32);
|
||||
let layout = alloc::Layout::from_size_align(size as usize + 4, MALLOC_ALIGN).unwrap();
|
||||
alloc::dealloc((addr - 4) as *mut u8, layout)
|
||||
}
|
||||
|
||||
pub struct ZstdContext {
|
||||
ctx: u32,
|
||||
src: *mut u8,
|
||||
src_size: u32,
|
||||
src_pos: u32,
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe fn zstd_create_ctx(src_size: u32) -> *mut ZstdContext {
|
||||
let src = alloc::alloc(alloc::Layout::from_size_align(src_size as usize, 1).unwrap());
|
||||
let ctx = ZSTD_createDStream();
|
||||
let result = alloc::alloc(alloc::Layout::new::<ZstdContext>()) as *mut ZstdContext;
|
||||
*result = ZstdContext {
|
||||
ctx,
|
||||
src,
|
||||
src_size,
|
||||
src_pos: 0,
|
||||
};
|
||||
result
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe fn zstd_get_src_ptr(ctx: *mut ZstdContext) -> *mut u8 { (*ctx).src }
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe fn zstd_free_ctx(ctx: *mut ZstdContext) {
|
||||
alloc::dealloc(
|
||||
(*ctx).src,
|
||||
alloc::Layout::from_size_align((*ctx).src_size as usize, 1).unwrap(),
|
||||
);
|
||||
ZSTD_freeDStream((*ctx).ctx);
|
||||
std::ptr::drop_in_place(ctx);
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe fn zstd_read(ctx: *mut ZstdContext, length: u32) -> *mut u8 {
|
||||
let dst = alloc::alloc(alloc::Layout::from_size_align(length as usize, 1).unwrap());
|
||||
let mut dst_pos = 0;
|
||||
let result = ZSTD_decompressStream_simpleArgs(
|
||||
(*ctx).ctx,
|
||||
dst,
|
||||
length,
|
||||
&mut dst_pos,
|
||||
(*ctx).src,
|
||||
(*ctx).src_size,
|
||||
&mut (*ctx).src_pos,
|
||||
);
|
||||
if ZSTD_isError(result) {
|
||||
dbg_log!(
|
||||
"ZSTD_decompressStream_simpleArgs returned error: {}",
|
||||
result
|
||||
);
|
||||
dbg_assert!(false);
|
||||
zstd_read_free(dst, length);
|
||||
return std::ptr::null_mut::<u8>();
|
||||
}
|
||||
if dst_pos != length {
|
||||
dbg_assert!(false, "ZSTD: Partial read");
|
||||
zstd_read_free(dst, length);
|
||||
return std::ptr::null_mut::<u8>();
|
||||
}
|
||||
dst
|
||||
}
|
||||
#[no_mangle]
|
||||
pub unsafe fn zstd_read_free(ptr: *mut u8, length: u32) {
|
||||
alloc::dealloc(
|
||||
ptr,
|
||||
alloc::Layout::from_size_align(length as usize, 1).unwrap(),
|
||||
);
|
||||
}
|
Loading…
Reference in a new issue