From b79b27c398f95b91246c143bbf1b3b3c6e0902ef Mon Sep 17 00:00:00 2001 From: Able Date: Thu, 10 Feb 2022 21:34:02 -0600 Subject: [PATCH] libwasm --- .gitignore | 2 ++ Cargo.toml | 8 ++++++ src/lib.rs | 7 +++++ src/logger/mod.rs | 67 +++++++++++++++++++++++++++++++++++++++++++++++ src/syscalls.rs | 4 +++ 5 files changed, 88 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.toml create mode 100644 src/lib.rs create mode 100644 src/logger/mod.rs create mode 100644 src/syscalls.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..96ef6c0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target +Cargo.lock diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..8cab3f3 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "libwasm" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..ca32d84 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,7 @@ +#[macro_use] +pub mod logger; +pub mod syscalls; + +pub use core::*; + +use logger::LogLevel; diff --git a/src/logger/mod.rs b/src/logger/mod.rs new file mode 100644 index 0000000..fe4aca1 --- /dev/null +++ b/src/logger/mod.rs @@ -0,0 +1,67 @@ +#![allow(unused_macros)] + +#[repr(C)] +pub enum LogLevel { + Error, + Warn, + Info, + Debug, + Trace, +} + +extern "C" { + pub fn host_log(log_level: LogLevel, log_message: *const u8, log_message_len: u32); +} + +macro_rules! log { + ($a:expr, $b:expr) => {{ + let log_level: LogLevel = $a; + let log_message: &[u8] = $b; + + let pointer = log_message.as_ptr(); + + unsafe { + $crate::logger::host_log(log_level, pointer, log_message.len() as u32); + } + }}; +} + +macro_rules! debug { + ($a:expr) => { + use crate::logger::{LogLevel::*, *}; + + log!(Debug, $a); + }; +} + +macro_rules! error { + ($a:expr) => { + use crate::logger::{LogLevel::*, *}; + + log!(Error, $a); + }; +} + +macro_rules! trace { + ($a:expr) => { + use crate::logger::*; + + log!(Trace, $a); + }; +} + +macro_rules! warn { + ($a:expr) => { + use crate::logger::{LogLevel::*, *}; + + log!(Warn, $a); + }; +} + +macro_rules! info { + ($a:expr) => { + use crate::logger::{LogLevel::*, *}; + + log!(Info, $a); + }; +} diff --git a/src/syscalls.rs b/src/syscalls.rs new file mode 100644 index 0000000..376456f --- /dev/null +++ b/src/syscalls.rs @@ -0,0 +1,4 @@ +/// All system calls are defined here. +extern "C" { + pub fn add(a: u32, b: u32) -> u32; +}