c test program

This commit is contained in:
nora 2023-09-30 10:54:59 +02:00
parent 54e0e7604e
commit 1baf8df4a8
18 changed files with 150 additions and 68 deletions

14
Cargo.lock generated
View file

@ -2,13 +2,6 @@
# It is not intended for manual editing.
version = 3
[[package]]
name = "c"
version = "0.1.0"
dependencies = [
"libuwuc",
]
[[package]]
name = "cfg-if"
version = "1.0.0"
@ -21,3 +14,10 @@ version = "0.1.0"
dependencies = [
"cfg-if",
]
[[package]]
name = "rawc"
version = "0.1.0"
dependencies = [
"libuwuc",
]

View file

@ -1,5 +1,5 @@
[workspace]
members = ["libuwuc", "c"]
members = ["libuwuc", "rawc"]
resolver = "2"
[profile.dev]

View file

@ -1,11 +0,0 @@
#![no_std]
mod string;
// libcore seems to require this symbol, even though it's unused.
#[no_mangle]
fn rust_eh_personality() {
unsafe {
libuwuc::trap!();
}
}

View file

@ -1,13 +0,0 @@
#[no_mangle]
pub(crate) unsafe extern "C" fn memset(ptr: *mut u8, constant: u8, len: usize) {
for i in 0..len {
unsafe {
*ptr.add(i) = constant;
}
}
}
#[no_mangle]
pub(crate) unsafe extern "C" fn strlen(s: *const u8) -> usize {
libuwuc::mem::strlen(s)
}

View file

@ -2,13 +2,6 @@
# It is not intended for manual editing.
version = 3
[[package]]
name = "c"
version = "0.1.0"
dependencies = [
"libuwuc",
]
[[package]]
name = "cfg-if"
version = "1.0.0"
@ -19,8 +12,8 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
name = "example-user"
version = "0.1.0"
dependencies = [
"c",
"libuwuc",
"rawc",
]
[[package]]
@ -29,3 +22,10 @@ version = "0.1.0"
dependencies = [
"cfg-if",
]
[[package]]
name = "rawc"
version = "0.1.0"
dependencies = [
"libuwuc",
]

View file

@ -14,5 +14,5 @@ panic = "abort"
panic = "abort"
[dependencies]
c = { path = "../c" }
rawc = { path = "../rawc" }
libuwuc = { path = "../libuwuc" }

View file

@ -6,19 +6,7 @@ use core::ffi::c_char;
use libuwuc::{println, utils::SharedThinCstr};
extern crate c;
#[panic_handler]
#[cfg(not(test))]
fn handler(arg: &core::panic::PanicInfo) -> ! {
let args = format_args!("<no message>");
let payload = arg.message().unwrap_or(&args);
libuwuc::io::println!("panicked: {payload}");
if let Some(loc) = arg.location() {
libuwuc::io::println!(" at {}:{}:{}", loc.file(), loc.line(), loc.column());
}
libuwuc::start::exit(1);
}
extern crate rawc;
#[no_mangle]
extern "C" fn main(_argc: i32, _argv: *const *const c_char) -> i32 {

5
hello.c Normal file
View file

@ -0,0 +1,5 @@
#include<stdio.h>
int main(int argc, char *argv[]) {
puts("Hello, world!");
}

View file

@ -1,25 +1,19 @@
use core::ffi::c_char;
use crate::sys::syscall;
pub const STDIN: i32 = 0;
pub const STDOUT: i32 = 1;
pub const STDERR: i32 = 2;
pub const EOF: i32 = -1;
#[doc(hidden)]
pub struct Printer;
impl core::fmt::Write for Printer {
fn write_str(&mut self, mut s: &str) -> core::fmt::Result {
unsafe {
while s.len() > 0 {
let result =
syscall::syscall!(syscall::SYS_WRITE, STDOUT, s.as_ptr(), s.len()) as i64;
if result < 0 {
return Err(core::fmt::Error);
}
s = &s[(result as usize)..];
}
}
Ok(())
fn write_str(&mut self, s: &str) -> core::fmt::Result {
unsafe { write_all(STDOUT, s.as_bytes()).map_err(|_| core::fmt::Error) }
}
}
@ -33,3 +27,27 @@ macro_rules! println {
};
}
pub use println;
pub unsafe fn write_all(fd: i32, mut buf: &[u8]) -> Result<(), i64> {
while buf.len() > 0 {
let result = syscall::syscall!(syscall::SYS_WRITE, fd, buf.as_ptr(), buf.len()) as i64;
if result < 0 {
return Err(result);
}
buf = &buf[(result as usize)..];
}
Ok(())
}
pub unsafe fn puts(s: *const c_char) -> i32 {
let len = crate::mem::strlen(s as _);
let result = write_all(STDOUT, core::slice::from_raw_parts(s as _, len));
if result.is_err() {
return EOF;
}
let result = write_all(STDOUT, &[b'\n']);
if result.is_err() {
return EOF;
}
1
}

View file

@ -7,6 +7,30 @@ pub unsafe fn memset(ptr: *mut u8, constant: u8, len: usize) {
}
}
#[inline]
pub unsafe fn memcpy(dest: *mut u8, src: *const u8, size: usize) -> *mut u8 {
for i in 0..size {
dest.add(i).write(src.add(i).read());
}
dest
}
#[inline]
pub unsafe fn memcmp(s1: *const u8, s2: *const u8, size: usize) -> i32 {
for i in 0..size {
let a = s1.add(i).read();
let b = s2.add(i).read();
let cmp = a.cmp(&b) as i8;
if cmp != 0 {
return cmp as i32;
}
}
0
}
#[inline]
pub unsafe fn strlen(mut s: *const u8) -> usize {
let mut len = 0;

View file

@ -1,12 +1,12 @@
[package]
name = "c"
name = "rawc"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib]
crate-type = ["rlib"]
crate-type = ["staticlib", "rlib"]
[dependencies]

25
rawc/src/lib.rs Normal file
View file

@ -0,0 +1,25 @@
#![no_std]
#![feature(panic_info_message)]
mod stdio;
mod string;
// libcore seems to require this symbol, even though it's unused.
#[no_mangle]
fn rust_eh_personality() {
unsafe {
libuwuc::trap!();
}
}
#[panic_handler]
#[cfg(not(test))]
fn handler(arg: &core::panic::PanicInfo) -> ! {
let args = format_args!("<no message>");
let payload = arg.message().unwrap_or(&args);
libuwuc::io::println!("panicked: {payload}");
if let Some(loc) = arg.location() {
libuwuc::io::println!(" at {}:{}:{}", loc.file(), loc.line(), loc.column());
}
libuwuc::start::exit(1);
}

6
rawc/src/stdio.rs Normal file
View file

@ -0,0 +1,6 @@
use core::ffi::c_char;
#[no_mangle]
pub unsafe extern "C" fn puts(s: *const c_char) -> i32 {
libuwuc::io::puts(s)
}

25
rawc/src/string.rs Normal file
View file

@ -0,0 +1,25 @@
#[no_mangle]
pub unsafe extern "C" fn memset(ptr: *mut u8, constant: u8, len: usize) {
libuwuc::mem::memset(ptr, constant, len)
}
#[no_mangle]
pub unsafe extern "C" fn memcpy(dest: *mut u8, src: *const u8, size: usize) -> *mut u8 {
libuwuc::mem::memcpy(dest, src, size)
}
#[no_mangle]
pub unsafe fn memcmp(s1: *const u8, s2: *const u8, size: usize) -> i32 {
libuwuc::mem::memcmp(s1, s2, size)
}
#[no_mangle]
pub unsafe fn bcmp(s1: *const u8, s2: *const u8, size: usize) -> i32 {
libuwuc::mem::memcmp(s1, s2, size)
}
#[no_mangle]
pub unsafe extern "C" fn strlen(s: *const u8) -> usize {
libuwuc::mem::strlen(s)
}

2
rust-toolchain.toml Normal file
View file

@ -0,0 +1,2 @@
[toolchain]
channel = "nightly-2023-09-30"

View file

@ -1,6 +1,7 @@
{ pkgs ? import <nixpkgs> { } }: pkgs.mkShell {
buildInputs = with pkgs; [
rustup
gcc
];
shellHook = ''
export PATH=$PATH:''${CARGO_HOME:-~/.cargo}/bin

5
test_c.sh Executable file
View file

@ -0,0 +1,5 @@
#!/usr/bin/env bash
cargo build
./uwuc-gcc hello.c -o target/hello
./target/hello

7
uwuc-gcc Executable file
View file

@ -0,0 +1,7 @@
#!/usr/bin/env bash
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
args=("-nodefaultlibs" "-nostdlib" "-L${SCRIPT_DIR}/target/debug" "-lrawc")
exec gcc "$@" "${args[@]}"