Skip to content

Commit d1c6a03

Browse files
committed
rustfmt.toml: adopt an opinionated style
1 parent 550fbaf commit d1c6a03

20 files changed

+178
-269
lines changed

examples/sim/dummy_terminal.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use std::io::{Write, stdout};
1+
use std::io::Write;
2+
use std::io::stdout;
23
use std::str;
34

45
use simmerv::terminal::Terminal;
@@ -8,9 +9,7 @@ use simmerv::terminal::Terminal;
89
pub struct DummyTerminal {}
910

1011
impl DummyTerminal {
11-
pub const fn new() -> Self {
12-
Self {}
13-
}
12+
pub const fn new() -> Self { Self {} }
1413
}
1514

1615
impl Terminal for DummyTerminal {
@@ -25,15 +24,11 @@ impl Terminal for DummyTerminal {
2524
let _ = stdout().flush();
2625
}
2726

28-
fn get_input(&mut self) -> u8 {
29-
0
30-
}
27+
fn get_input(&mut self) -> u8 { 0 }
3128

3229
// Wasm specific methods. No use.
3330

3431
fn put_input(&mut self, _value: u8) {}
3532

36-
fn get_output(&mut self) -> u8 {
37-
0
38-
}
33+
fn get_output(&mut self) -> u8 { 0 }
3934
}

examples/sim/nonblocknoecho.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use std::io::{self, Read, Stdin};
1+
use std::io::Read;
2+
use std::io::Stdin;
3+
use std::io::{self};
24

35
pub struct NonblockNoEcho {
46
stdin: i32,
@@ -10,7 +12,12 @@ impl NonblockNoEcho {
1012
#[allow(clippy::expect_used, clippy::unwrap_used)]
1113
pub fn new(capture_ctrlc: bool) -> Self {
1214
use std::os::unix::io::AsRawFd;
13-
use termios::{ECHO, ICANON, ISIG, TCSANOW, Termios, tcsetattr};
15+
use termios::ECHO;
16+
use termios::ICANON;
17+
use termios::ISIG;
18+
use termios::TCSANOW;
19+
use termios::Termios;
20+
use termios::tcsetattr;
1421
let stdin: i32 = std::io::stdin().as_raw_fd();
1522
assert_eq!(stdin, 0);
1623

examples/sim/popup_terminal.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use crate::nonblocknoecho::NonblockNoEcho;
22
use simmerv::terminal::Terminal;
3-
use std::io::{self, Stdout, Write};
3+
use std::io::Stdout;
4+
use std::io::Write;
5+
use std::io::{self};
46

57
/// Popup `Terminal` used for desktop program.
68
pub struct PopupTerminal {
@@ -30,7 +32,5 @@ impl Terminal for PopupTerminal {
3032
// Wasm specific methods. No use.
3133
fn put_input(&mut self, _value: u8) {}
3234

33-
fn get_output(&mut self) -> u8 {
34-
0
35-
}
35+
fn get_output(&mut self) -> u8 { 0 }
3636
}

rustfmt.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
edition = "2021"
2+
wrap_comments = true
3+
format_code_in_doc_comments = true
4+
imports_granularity = "Item"
5+
overflow_delimited_expr = true
6+
tab_spaces = 4
7+
fn_single_line = true

src/bounded.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ impl<const N: usize> Bounded<N> {
1616
}
1717

1818
#[must_use]
19-
pub const fn get(self) -> u8 {
20-
self.0
21-
}
19+
pub const fn get(self) -> u8 { self.0 }
2220
}
2321

2422
impl<T, const N: usize> Index<Bounded<N>> for [T; N] {

src/cpu.rs

Lines changed: 53 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,14 @@ use crate::riscv;
1010
use crate::rvc;
1111
use crate::terminal;
1212
pub use csr::*;
13-
use fp::{RoundingMode, Sf, Sf32, Sf64, cvt_i32_sf32, cvt_i64_sf32, cvt_u32_sf32, cvt_u64_sf32};
13+
use fp::RoundingMode;
14+
use fp::Sf;
15+
use fp::Sf32;
16+
use fp::Sf64;
17+
use fp::cvt_i32_sf32;
18+
use fp::cvt_i64_sf32;
19+
use fp::cvt_u32_sf32;
20+
use fp::cvt_u64_sf32;
1421
use log;
1522
use num_traits::FromPrimitive;
1623
use riscv::MemoryAccessType;
@@ -30,9 +37,7 @@ pub const PG_SHIFT: usize = 12; // 4K page size
3037
pub type Reg = Bounded<65>;
3138
impl Reg {
3239
#[must_use]
33-
pub const fn is_x0_dest(self) -> bool {
34-
self.get() == 64
35-
}
40+
pub const fn is_x0_dest(self) -> bool { self.get() == 64 }
3641
}
3742

3843
/// Generate a source integer `Reg`
@@ -155,9 +160,7 @@ impl Cpu {
155160

156161
#[allow(clippy::inline_always)]
157162
#[inline(always)]
158-
fn read_x(&self, r: Reg) -> i64 {
159-
self.rf[r]
160-
}
163+
fn read_x(&self, r: Reg) -> i64 { self.rf[r] }
161164

162165
#[allow(clippy::inline_always)]
163166
#[inline(always)]
@@ -169,26 +172,20 @@ impl Cpu {
169172
/// Reads Program counter
170173
#[must_use]
171174
#[allow(clippy::cast_sign_loss)]
172-
pub const fn read_pc(&self) -> i64 {
173-
self.pc
174-
}
175+
pub const fn read_pc(&self) -> i64 { self.pc }
175176

176177
/// Updates Program Counter
177178
///
178179
/// # Arguments
179180
/// * `value`
180-
pub const fn update_pc(&mut self, value: i64) {
181-
self.pc = value & !1;
182-
}
181+
pub const fn update_pc(&mut self, value: i64) { self.pc = value & !1; }
183182

184183
/// Reads integer register
185184
///
186185
/// # Arguments
187186
/// * `reg` Register number. Must be 0-31
188187
#[must_use]
189-
pub fn read_register(&self, reg: Reg) -> i64 {
190-
self.rf[reg]
191-
}
188+
pub fn read_register(&self, reg: Reg) -> i64 { self.rf[reg] }
192189

193190
/// Checks that float instructions are enabled and
194191
/// that the rounding mode is legal (XXX this should be part of format!)
@@ -203,7 +200,8 @@ impl Cpu {
203200
}
204201
}
205202

206-
/// Runs program N cycles. Fetch, decode, and execution are completed in a cycle so far.
203+
/// Runs program N cycles. Fetch, decode, and execution are completed in a
204+
/// cycle so far.
207205
#[allow(clippy::cast_sign_loss)]
208206
pub fn run_soc(&mut self, cpu_steps: usize) {
209207
for _ in 0..cpu_steps {
@@ -252,10 +250,12 @@ impl Cpu {
252250

253251
#[allow(clippy::cast_sign_loss)]
254252
fn handle_interrupt(&mut self) {
255-
use self::Trap::{
256-
MachineExternalInterrupt, MachineSoftwareInterrupt, MachineTimerInterrupt,
257-
SupervisorExternalInterrupt, SupervisorSoftwareInterrupt, SupervisorTimerInterrupt,
258-
};
253+
use self::Trap::MachineExternalInterrupt;
254+
use self::Trap::MachineSoftwareInterrupt;
255+
use self::Trap::MachineTimerInterrupt;
256+
use self::Trap::SupervisorExternalInterrupt;
257+
use self::Trap::SupervisorSoftwareInterrupt;
258+
use self::Trap::SupervisorTimerInterrupt;
259259
let minterrupt = self.mmu.mip & self.read_csr_raw(Csr::Mie);
260260
if minterrupt == 0 {
261261
return;
@@ -284,7 +284,8 @@ impl Cpu {
284284

285285
fn handle_exception(&mut self, exc: &Exception) {
286286
// XXX If we pass in the address we don't need
287-
// self.pc, but that requires us to call handle exception from a centrol location with access to the pc.
287+
// self.pc, but that requires us to call handle exception from a centrol
288+
// location with access to the pc.
288289
self.handle_trap(exc, self.insn_addr, false);
289290
}
290291

@@ -456,14 +457,16 @@ impl Cpu {
456457
PrivMode::M => {
457458
let status = self.read_csr_raw(Csr::Mstatus);
458459
let mie = (status >> 3) & 1;
459-
// clear MIE[3], override MPIE[7] with MIE[3], override MPP[12:11] with current privilege encoding
460+
// clear MIE[3], override MPIE[7] with MIE[3], override MPP[12:11] with current
461+
// privilege encoding
460462
let new_status = (status & !0x1888) | (mie << 7) | (current_priv_encoding << 11);
461463
self.write_csr_raw(Csr::Mstatus, new_status);
462464
}
463465
PrivMode::S => {
464466
let status = self.read_csr_raw(Csr::Sstatus);
465467
let sie = (status >> 1) & 1;
466-
// clear SIE[1], override SPIE[5] with SIE[1], override SPP[8] with current privilege encoding
468+
// clear SIE[1], override SPIE[5] with SIE[1], override SPP[8] with current
469+
// privilege encoding
467470
let new_status =
468471
(status & !0x122) | (sie << 5) | ((current_priv_encoding & 1) << 8);
469472
self.write_csr_raw(Csr::Sstatus, new_status);
@@ -493,15 +496,17 @@ impl Cpu {
493496
Some(csr)
494497
}
495498

496-
// XXX This is still so far from complete; copy the logic from Dromajo and review
497-
// each CSR. Do Not Blanket allow reads and writes from unsupported CSRs
499+
// XXX This is still so far from complete; copy the logic from Dromajo and
500+
// review each CSR. Do Not Blanket allow reads and writes from unsupported
501+
// CSRs
498502
#[allow(clippy::cast_sign_loss)]
499503
fn read_csr(&self, csrno: u16) -> Result<u64, Exception> {
500504
use PrivMode::S;
501505

502506
let illegal = Err(Exception {
503507
trap: Trap::IllegalInstruction,
504-
tval: i64::from(self.insn), // XXX we could assign this outside, eliminating the need for self.insn here
508+
tval: i64::from(self.insn), /* XXX we could assign this outside, eliminating the need
509+
* for self.insn here */
505510
});
506511

507512
let Some(csr) = self.has_csr_access_privilege(csrno) else {
@@ -524,7 +529,8 @@ impl Cpu {
524529
fn write_csr(&mut self, csrno: u16, mut value: u64) -> Result<(), Exception> {
525530
let illegal = Err(Exception {
526531
trap: Trap::IllegalInstruction,
527-
tval: i64::from(self.insn), // XXX we could assign this outside, eliminating the need for self.insn here
532+
tval: i64::from(self.insn), /* XXX we could assign this outside, eliminating the need
533+
* for self.insn here */
528534
});
529535

530536
let Some(csr) = self.has_csr_access_privilege(csrno) else {
@@ -644,32 +650,22 @@ impl Cpu {
644650
self.mmu.satp = value;
645651
self.mmu.clear_page_cache();
646652
}
647-
/*Csr::Cycle |*/ Csr::Mcycle => self.cycle = value,
653+
/* Csr::Cycle | */ Csr::Mcycle => self.cycle = value,
648654
_ => {
649655
self.csr[csr as usize] = value;
650656
}
651657
}
652658
}
653659

654-
fn _set_fcsr_nv(&mut self) {
655-
self.add_to_fflags(0x10);
656-
}
660+
fn _set_fcsr_nv(&mut self) { self.add_to_fflags(0x10); }
657661

658-
fn set_fcsr_dz(&mut self) {
659-
self.add_to_fflags(8);
660-
}
662+
fn set_fcsr_dz(&mut self) { self.add_to_fflags(8); }
661663

662-
fn _set_fcsr_of(&mut self) {
663-
self.add_to_fflags(4);
664-
}
664+
fn _set_fcsr_of(&mut self) { self.add_to_fflags(4); }
665665

666-
fn _set_fcsr_uf(&mut self) {
667-
self.add_to_fflags(2);
668-
}
666+
fn _set_fcsr_uf(&mut self) { self.add_to_fflags(2); }
669667

670-
fn _set_fcsr_nx(&mut self) {
671-
self.add_to_fflags(1);
672-
}
668+
fn _set_fcsr_nx(&mut self) { self.add_to_fflags(1); }
673669

674670
/// Disassembles an instruction pointed by Program Counter and
675671
/// and return the [possibly] writeback register
@@ -702,9 +698,7 @@ impl Cpu {
702698
}
703699

704700
/// Returns mutable `Mmu`
705-
pub const fn get_mut_mmu(&mut self) -> &mut Mmu {
706-
&mut self.mmu
707-
}
701+
pub const fn get_mut_mmu(&mut self) -> &mut Mmu { &mut self.mmu }
708702

709703
/// Returns mutable `Terminal`
710704
pub fn get_mut_terminal(&mut self) -> &mut Box<dyn Terminal> {
@@ -736,13 +730,9 @@ impl Cpu {
736730
}
737731

738732
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
739-
fn read_f64(&self, r: Reg) -> f64 {
740-
f64::from_bits(self.read_f(r) as u64)
741-
}
733+
fn read_f64(&self, r: Reg) -> f64 { f64::from_bits(self.read_f(r) as u64) }
742734

743-
fn write_f64(&mut self, r: Reg, f: f64) {
744-
self.write_f(r, f.to_bits() as i64);
745-
}
735+
fn write_f64(&mut self, r: Reg, f: f64) { self.write_f(r, f.to_bits() as i64); }
746736

747737
fn read_frm(&self) -> RoundingMode {
748738
assert_ne!(self.fs, 0);
@@ -2313,13 +2303,10 @@ const INSTRUCTIONS: [Instruction; INSTRUCTION_NUM] = [
23132303
let f = parse_format_r(word);
23142304
let s1 = cpu.read_x(f.rs1) as u64;
23152305
let s2 = cpu.read_x(f.rs2) as u64;
2316-
cpu.write_x(
2317-
f.rd,
2318-
match s2 {
2319-
0 => s1 as i64,
2320-
_ => s1.wrapping_rem(s2) as i64,
2321-
},
2322-
);
2306+
cpu.write_x(f.rd, match s2 {
2307+
0 => s1 as i64,
2308+
_ => s1.wrapping_rem(s2) as i64,
2309+
});
23232310
Ok(())
23242311
},
23252312
disassemble: dump_format_r,
@@ -2401,13 +2388,10 @@ const INSTRUCTIONS: [Instruction; INSTRUCTION_NUM] = [
24012388
let f = parse_format_r(word);
24022389
let s1 = cpu.read_x(f.rs1) as u32;
24032390
let s2 = cpu.read_x(f.rs2) as u32;
2404-
cpu.write_x(
2405-
f.rd,
2406-
match s2 {
2407-
0 => i64::from(s1 as i32),
2408-
_ => i64::from(s1.wrapping_rem(s2) as i32),
2409-
},
2410-
);
2391+
cpu.write_x(f.rd, match s2 {
2392+
0 => i64::from(s1 as i32),
2393+
_ => i64::from(s1.wrapping_rem(s2) as i32),
2394+
});
24112395
Ok(())
24122396
},
24132397
disassemble: dump_format_r,
@@ -3907,14 +3891,10 @@ mod test_cpu {
39073891
use crate::mmu::DRAM_BASE;
39083892
use crate::terminal::DummyTerminal;
39093893

3910-
fn create_cpu() -> Cpu {
3911-
Cpu::new(Box::new(DummyTerminal::new()))
3912-
}
3894+
fn create_cpu() -> Cpu { Cpu::new(Box::new(DummyTerminal::new())) }
39133895

39143896
#[test]
3915-
fn initialize() {
3916-
let _cpu = create_cpu();
3917-
}
3897+
fn initialize() { let _cpu = create_cpu(); }
39183898

39193899
#[test]
39203900
fn update_pc() {

src/csr.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use num_derive::FromPrimitive;
2-
use std::fmt::{Display, Formatter};
2+
use std::fmt::Display;
3+
use std::fmt::Formatter;
34

45
#[derive(FromPrimitive, Debug, Clone, Copy)]
56
pub enum Csr {
@@ -100,7 +101,8 @@ pub const MSTATUS_TSR: u64 = 1 << 22;
100101
pub const MSTATUS_UXL_MASK: u64 = 3 << MSTATUS_UXL_SHIFT;
101102
pub const MSTATUS_SXL_MASK: u64 = 3 << MSTATUS_SXL_SHIFT;
102103

103-
// MSTATUS_MASK are the only fields that are directly writable with an csr instruction
104+
// MSTATUS_MASK are the only fields that are directly writable with an csr
105+
// instruction
104106
pub const MSTATUS_MASK: u64 = MSTATUS_SIE
105107
| MSTATUS_MIE
106108
| MSTATUS_SPIE

0 commit comments

Comments
 (0)