Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement open, read, write, close in compatible environments #297

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion crates/mipsy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,20 @@ build = "build.rs"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[features]
default = ["raw_io"]
raw_io = ["libc", "mipsy_interactive/raw_io"]

[dependencies]
mipsy_lib = { version = "0.1.0", path = "../mipsy_lib" }
mipsy_parser = { version = "0.1.0", path = "../mipsy_parser" }
mipsy_interactive = { version = "0.1.0", path = "../mipsy_interactive" }
mipsy_interactive = { version = "0.1.0", path = "../mipsy_interactive", default-features = false }
mipsy_utils = { version = "0.1.0", path = "../mipsy_utils" }
mipsy_instructions = { version = "0.1.0", path = "../mipsy_instructions", features = ["rt_yaml"] }
clap = { version = "4.0.4", features = ["derive", "wrap_help"] } # cli arg parsing
colored = "2" # for ansi colors
text_io = "0.1.8" # to read values in, w/out per line
libc = { version = "0.2.152", optional = true }

[build-dependencies]
vergen = { version = "7.5.1", default-features = false, features = ["git"] } # for version info
156 changes: 73 additions & 83 deletions crates/mipsy/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,97 +384,62 @@ fn main() {
let character: char = get_input_eof("character").unwrap_or('\0');
runtime = guard(character as u8);
}
#[cfg(feature = "raw_io")]
Open(args, guard) => {
use std::ffi::CString;

let name = CString::new(args.path)
.expect("runtime should have removed \\0");
let fd = unsafe {
libc::open(name.as_ptr(), args.flags as i32, args.mode)
};
runtime = guard(fd);
}
#[cfg(feature = "raw_io")]
Read(args, guard) => {
let mut vec = vec![0u8; args.len as usize];
let read = unsafe {
libc::read(args.fd as i32, vec.as_mut_ptr().cast(), vec.len())
};
runtime = guard((read as i32, vec));
}
#[cfg(feature = "raw_io")]
Write(args, guard) => {
let write = unsafe {
libc::write(
args.fd as i32,
args.buf.as_ptr().cast(),
args.buf.len(),
)
} as i32;
runtime = guard(write);
}
#[cfg(feature = "raw_io")]
Close(args, guard) => {
let close = unsafe { libc::close(args.fd as i32) } as i32;
runtime = guard(close);
}
#[allow(unreachable_patterns)] // fall-through
Open(_args, guard) => {
// TODO: implement file open for mipsy cli frontend
runtime = guard(-1);
runtime.timeline_mut().pop_last_state();
println!();
RuntimeError::new(Error::InvalidSyscall {
syscall: SYS13_OPEN,
reason: InvalidSyscallReason::Unimplemented,
})
.show_error(
ErrorContext::Binary,
files
.iter()
.map(|(tag, content)| {
(Rc::from(&**tag), Rc::from(&**content))
})
.collect(),
&iset,
&binary,
&runtime,
);
process::exit(1);
disabled_raw_io(SYS13_OPEN, guard(-1), files, iset, binary);
}
#[allow(unreachable_patterns)] // fall-through
Read(_args, guard) => {
// TODO: implement file read for mipsy cli frontend
runtime = guard((-1, Vec::new()));
runtime.timeline_mut().pop_last_state();
println!();
RuntimeError::new(Error::InvalidSyscall {
syscall: SYS14_READ,
reason: InvalidSyscallReason::Unimplemented,
})
.show_error(
ErrorContext::Binary,
files
.iter()
.map(|(tag, content)| {
(Rc::from(&**tag), Rc::from(&**content))
})
.collect(),
&iset,
&binary,
&runtime,
disabled_raw_io(
SYS14_READ,
guard((-1, vec![])),
files,
iset,
binary,
);
process::exit(1);
}
#[allow(unreachable_patterns)] // fall-through
Write(_args, guard) => {
// TODO: implement file write for mipsy cli frontend
runtime = guard(-1);
runtime.timeline_mut().pop_last_state();
println!();
RuntimeError::new(Error::InvalidSyscall {
syscall: SYS15_WRITE,
reason: InvalidSyscallReason::Unimplemented,
})
.show_error(
ErrorContext::Binary,
files
.iter()
.map(|(tag, content)| {
(Rc::from(&**tag), Rc::from(&**content))
})
.collect(),
&iset,
&binary,
&runtime,
);
process::exit(1);
disabled_raw_io(SYS15_WRITE, guard(-1), files, iset, binary);
}
#[allow(unreachable_patterns)] // fall-through
Close(_args, guard) => {
// TODO: implement file close for mipsy cli frontend
runtime = guard(-1);
runtime.timeline_mut().pop_last_state();
println!();
RuntimeError::new(Error::InvalidSyscall {
syscall: SYS16_CLOSE,
reason: InvalidSyscallReason::Unimplemented,
})
.show_error(
ErrorContext::Binary,
files
.iter()
.map(|(tag, content)| {
(Rc::from(&**tag), Rc::from(&**content))
})
.collect(),
&iset,
&binary,
&runtime,
);
process::exit(1);
disabled_raw_io(SYS16_CLOSE, guard(-1), files, iset, binary);
}
ExitStatus(args, _new_runtime) => {
std::process::exit(args.exit_code);
Expand Down Expand Up @@ -514,6 +479,31 @@ fn main() {
}
}

fn disabled_raw_io(
syscall: i32,
mut runtime: Runtime,
files: Vec<(String, String)>,
iset: InstSet,
binary: Binary,
) -> ! {
runtime.timeline_mut().pop_last_state();
let err = RuntimeError::new(Error::InvalidSyscall {
syscall,
reason: InvalidSyscallReason::Disabled,
});
err.show_error(
ErrorContext::Binary,
files
.iter()
.map(|(tag, content)| (Rc::from(&**tag), Rc::from(&**content)))
.collect(),
&iset,
&binary,
&runtime,
);
process::exit(1);
}

fn read_string(_max_len: u32) -> String {
loop {
let input: String = get_input("string", true);
Expand Down
9 changes: 6 additions & 3 deletions crates/mipsy_interactive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ version = "0.1.0"
authors = ["insou22 <[email protected]>"]
edition = "2021"

[lib]
path = "src/lib.rs"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[features]
default = ["raw_io"]
raw_io = ["libc"]

[dependencies]
mipsy_lib = { version = "0.1.0", path = "../mipsy_lib" }
mipsy_parser = { version = "0.1.0", path = "../mipsy_parser" }
Expand All @@ -25,3 +26,5 @@ text_io = "0.1.8" # to read values in, w/out per line
dirs = "3.0" # for user config directory
ctrlc = { version = "3.0", features = ["termination"] } # for interrupt handling during execution
termsize = "0.1" # to get terminal width info

libc = { version = "0.2.152", optional = true }
58 changes: 33 additions & 25 deletions crates/mipsy_interactive/src/interactive/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,77 +422,85 @@ impl State {
let value = runtime_handler::sys12_read_char(verbose);
self.runtime = guard(value);
}
#[cfg(feature = "raw_io")]
Open(args, guard) => {
let value = runtime_handler::sys13_open(verbose, args);
self.runtime = guard(value);
}
#[cfg(feature = "raw_io")]
Read(args, guard) => {
let value = runtime_handler::sys14_read(verbose, args);
self.runtime = guard(value);
}
#[cfg(feature = "raw_io")]
Write(args, guard) => {
let value = runtime_handler::sys15_write(verbose, args);
self.runtime = guard(value);
}
#[cfg(feature = "raw_io")]
Close(args, guard) => {
let value = runtime_handler::sys16_close(verbose, args);
self.runtime = guard(value);
}
#[allow(unreachable_patterns)] // fall-through
Open(_args, guard) => {
// TODO: implement file open for mipsy interactive frontend

let mut new_runtime = guard(-1);
new_runtime.timeline_mut().pop_last_state();
self.runtime = new_runtime;

return Err(CommandError::RuntimeError {
mipsy_error: MipsyError::Runtime(RuntimeError::new(
Error::InvalidSyscall {
syscall: SYS13_OPEN,
reason: InvalidSyscallReason::Unimplemented,
reason: InvalidSyscallReason::Disabled,
},
)),
});

// let value = runtime_handler::sys13_open(verbose, args);
// self.runtime = Some(guard(value));
}
#[allow(unreachable_patterns)] // fall-through
Read(_args, guard) => {
// TODO: implement file read for mipsy interactive frontend

let mut new_runtime = guard((-1, Vec::new()));
let mut new_runtime = guard((-1, vec![]));
new_runtime.timeline_mut().pop_last_state();
self.runtime = new_runtime;

return Err(CommandError::RuntimeError {
mipsy_error: MipsyError::Runtime(RuntimeError::new(
Error::InvalidSyscall {
syscall: SYS14_READ,
reason: InvalidSyscallReason::Unimplemented,
reason: InvalidSyscallReason::Disabled,
},
)),
});

// let value = runtime_handler::sys14_read(verbose, args);
// self.runtime = Some(guard(value));
}
#[allow(unreachable_patterns)] // fall-through
Write(_args, guard) => {
// TODO: implement file write for mipsy interactive frontend

let mut new_runtime = guard(-1);
new_runtime.timeline_mut().pop_last_state();
self.runtime = new_runtime;

return Err(CommandError::RuntimeError {
mipsy_error: MipsyError::Runtime(RuntimeError::new(
Error::InvalidSyscall {
syscall: SYS15_WRITE,
reason: InvalidSyscallReason::Unimplemented,
reason: InvalidSyscallReason::Disabled,
},
)),
});

// let value = runtime_handler::sys15_write(verbose, args);
// self.runtime = Some(guard(value));
}
#[allow(unreachable_patterns)] // fall-through
Close(_args, guard) => {
// TODO: implement file close for mipsy interactive frontend

let mut new_runtime = guard(-1);
new_runtime.timeline_mut().pop_last_state();
self.runtime = new_runtime;

return Err(CommandError::RuntimeError {
mipsy_error: MipsyError::Runtime(RuntimeError::new(
Error::InvalidSyscall {
syscall: SYS16_CLOSE,
reason: InvalidSyscallReason::Unimplemented,
reason: InvalidSyscallReason::Disabled,
},
)),
});

// let value = runtime_handler::sys16_close(verbose, args);
// self.runtime = Some(guard(value));
}
ExitStatus(args, new_runtime) => {
self.runtime = new_runtime;
Expand Down
Loading