Coder Social home page Coder Social logo

cepawiel / experimental-rust-libronin Goto Github PK

View Code? Open in Web Editor NEW
2.0 3.0 0.0 643 KB

Example of Rust Code building for Dreamcast using GCCRS and Libronin

Dockerfile 0.11% Shell 0.02% Makefile 1.17% C 95.43% Assembly 2.45% Rust 0.06% Pike 0.31% DIGITAL Command Language 0.18% Module Management System 0.07% Roff 0.21%

experimental-rust-libronin's Introduction

Rust on Dreamcast

This uses GCCRS with no std/core library at this time. If you know how or manage to compile either let me know I'd be excited to know how! GCCRS does not fully support Rust at the moment, so keep that in mind should you decide to modify the example.

How to Build

Only build dependency is Docker Just run "./build.sh" to compile toolchain followed by libronin and the Rust example (libronin/examples/ex_rust.rs). The output elf (libronin/examples/ex_rust.elf) will be located alongside the source file. Tested working on both lxdream-nitro and Dreamcast Hardware using dcload-ip.

Sources

GCCRS

libronin

KallistiOS

dc-chain Toolchain File

experimental-rust-libronin's People

Contributors

cepawiel avatar

Stargazers

Memorix101 avatar Falco Girgis avatar

Watchers

 avatar Falco Girgis avatar  avatar

experimental-rust-libronin's Issues

Try using slices in rust example

I loved seeing your example over at: https://github.com/cepawiel/experimental-rust-libronin/blob/main/libronin/examples/ex_rust.rs

Which got me thinking surely we could make it a bit better and we do already support a bunch of intrinsics and while loops so i came up with this example:

https://godbolt.org/z/Tq4517bTr

// { dg-options "-w" }
mod intrinsics {
    extern "rust-intrinsic" {
        #[rustc_const_stable(feature = "const_ptr_offset", since = "1.61.0")]
        pub fn offset<T>(ptr: *const T, count: isize) -> *const T;
    }
}

mod mem {
    extern "rust-intrinsic" {
        fn size_of<T>() -> usize;
        fn transmute<T, U>(_: T) -> U;
    }
}

extern "C" {
    fn printf(s: *const i8, ...);
}

struct FatPtr<T> {
    data: *const T,
    len: usize,
}

pub union Repr<T> {
    rust: *const [T],
    rust_mut: *mut [T],
    raw: FatPtr<T>,
}

pub enum Option<T> {
    None,
    Some(T),
}

#[lang = "Range"]
pub struct Range<Idx> {
    pub start: Idx,
    pub end: Idx,
}

#[lang = "const_slice_ptr"]
impl<T> *const [T] {
    pub const fn len(self) -> usize {
        unsafe { Repr { rust: self }.raw.len }
    }

    pub const fn as_ptr(self) -> *const T {
        self as *const T
    }
}

#[lang = "const_ptr"]
impl<T> *const T {
    pub const unsafe fn offset(self, count: isize) -> *const T {
        unsafe { intrinsics::offset(self, count) }
    }

    pub const unsafe fn add(self, count: usize) -> Self {
        unsafe { self.offset(count as isize) }
    }

    pub const fn as_ptr(self) -> *const T {
        self as *const T
    }
}

const fn slice_from_raw_parts<T>(data: *const T, len: usize) -> *const [T] {
    unsafe {
        Repr {
            raw: FatPtr { data, len },
        }
        .rust
    }
}

#[lang = "index"]
trait Index<Idx> {
    type Output;

    fn index(&self, index: Idx) -> &Self::Output;
}

impl<T> [T] {
    pub const fn is_empty(&self) -> bool {
        self.len() == 0
    }

    pub const fn len(&self) -> usize {
        unsafe { Repr { rust: self }.raw.len }
    }
}

pub unsafe trait SliceIndex<T> {
    type Output;

    fn get(self, slice: &T) -> Option<&Self::Output>;

    unsafe fn get_unchecked(self, slice: *const T) -> *const Self::Output;

    fn index(self, slice: &T) -> &Self::Output;
}

unsafe impl<T> SliceIndex<[T]> for usize {
    type Output = T;

    fn get(self, slice: &[T]) -> Option<&T> {
        unsafe { Option::Some(&*self.get_unchecked(slice)) }
    }

    unsafe fn get_unchecked(self, slice: *const [T]) -> *const T {
        // SAFETY: the caller guarantees that `slice` is not dangling, so it
        // cannot be longer than `isize::MAX`. They also guarantee that
        // `self` is in bounds of `slice` so `self` cannot overflow an `isize`,
        // so the call to `add` is safe.
        unsafe { slice.as_ptr().add(self) }
    }

    fn index(self, slice: &[T]) -> &T {
        unsafe { &*self.get_unchecked(slice) }
    }
}

unsafe impl<T> SliceIndex<[T]> for Range<usize> {
    type Output = [T];

    fn get(self, slice: &[T]) -> Option<&[T]> {
        if self.start > self.end || self.end > slice.len() {
            Option::None
        } else {
            unsafe { Option::Some(&*self.get_unchecked(slice)) }
        }
    }

    unsafe fn get_unchecked(self, slice: *const [T]) -> *const [T] {
        unsafe {
            let a: *const T = slice.as_ptr();
            let b: *const T = a.add(self.start);
            slice_from_raw_parts(b, self.end - self.start)
        }
    }

    fn index(self, slice: &[T]) -> &[T] {
        unsafe { &*self.get_unchecked(slice) }
    }
}

impl<T, I> Index<I> for [T]
where
    I: SliceIndex<[T]>,
{
    type Output = I::Output;

    fn index(&self, index: I) -> &I::Output {
        index.index(self)
    }
}

fn main() -> i32 {
    let a:&str = "Hello world";
    let stringSlice = unsafe { mem::transmute::<&str, &[u8]>(a) };

    let i = 0;
    let len = stringSlice.len ();
    loop {
        let character = stringSlice[i];

        unsafe {
            let a = "%c\n\0";
            let b = a as *const str;
            let c = b as *const i8;

            printf (c, character as u32);
        }

        i += 1;
        if i >= len {
            break;
        }
    }

    0
}

Note it requires a bunch of these libcore traits and lang items

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.