Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
AngheloAlf committed Feb 22, 2024
1 parent 3ffa421 commit 71cc1e6
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 43 deletions.
7 changes: 5 additions & 2 deletions slinky-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use slinky::{Document, LinkerWriter};

fn main() {
// TODO: don't use expect?
let document = Document::read_file(Path::new("test_case.yaml")).expect("Error while reading input file");
let document =
Document::read_file(Path::new("test_case.yaml")).expect("Error while reading input file");

let mut writer = LinkerWriter::new(&document.options);
writer.begin_sections();
Expand All @@ -16,5 +17,7 @@ fn main() {
}
writer.end_sections();

writer.save_linker_script(Path::new("test_case.ld")).expect("Error writing the linker script");
writer
.save_linker_script(Path::new("test_case.ld"))
.expect("Error writing the linker script");
}
12 changes: 10 additions & 2 deletions slinky/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,19 @@ impl Document {
pub fn read_file(path: &Path) -> Result<Self, SlinkyError> {
let f = match fs::File::open(path) {
Ok(f) => f,
Err(e) => return Err(SlinkyError::FailedFileOpen{description: e.to_string()}),
Err(e) => {
return Err(SlinkyError::FailedFileOpen {
description: e.to_string(),
})
}
};
let mut document: Document = match serde_yaml::from_reader(f) {
Ok(d) => d,
Err(e) => return Err(SlinkyError::FailedYamlParsing{description: e.to_string()}),
Err(e) => {
return Err(SlinkyError::FailedYamlParsing {
description: e.to_string(),
})
}
};

for segment in &mut document.segments {
Expand Down
5 changes: 2 additions & 3 deletions slinky/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
/* SPDX-FileCopyrightText: © 2024 decompals */
/* SPDX-License-Identifier: MIT */


#[derive(Clone, Debug, PartialEq, Eq, Hash, thiserror::Error)]
pub enum SlinkyError {
#[error("Unable to open file: {description}")]
FailedFileOpen {description: String},
FailedFileOpen { description: String },
#[error("Unable parse yaml: {description}")]
FailedYamlParsing { description: String },
}
}
4 changes: 2 additions & 2 deletions slinky/src/file_info.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
/* SPDX-FileCopyrightText: © 2024 decompals */
/* SPDX-License-Identifier: MIT */

use std::path::PathBuf;
use serde::Deserialize;
use std::path::PathBuf;

use crate::file_kind::FileKind;

#[derive(Deserialize, PartialEq, Debug)]
pub struct FileInfo {
pub path: PathBuf,

#[serde(default="default_fileinfo_kind")]
#[serde(default = "default_fileinfo_kind")]
pub kind: FileKind,
}

Expand Down
9 changes: 4 additions & 5 deletions slinky/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,26 @@

mod error;

mod options;
mod paths_configs;
mod segment_symbols_style;
mod options;

mod file_kind;
mod file_info;
mod file_kind;
mod segment;

mod document;

mod linker_writer;


pub use error::SlinkyError;

pub use options::Options;
pub use paths_configs::PathsConfigs;
pub use segment_symbols_style::SegmentSymbolsStyle;
pub use options::Options;

pub use file_kind::FileKind;
pub use file_info::FileInfo;
pub use file_kind::FileKind;
pub use segment::Segment;

pub use document::Document;
Expand Down
37 changes: 27 additions & 10 deletions slinky/src/linker_writer.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
/* SPDX-FileCopyrightText: © 2024 decompals */
/* SPDX-License-Identifier: MIT */

use std::{collections::HashSet, fs::{self, File}, io::Write, path::{Path, PathBuf}};
use std::{
collections::HashSet,
fs::{self, File},
io::Write,
path::{Path, PathBuf},
};

use crate::file_kind::FileKind;
use crate::segment::Segment;
use crate::options::Options;
use crate::segment::Segment;

pub struct LinkerWriter<'a> {
pub linker_symbols: HashSet<String>,
Expand All @@ -21,7 +26,7 @@ impl<'a> LinkerWriter<'a> {
Self {
linker_symbols: HashSet::new(),
indent_level: 0,
buffer:Vec::new(),
buffer: Vec::new(),
options: options,
}
}
Expand All @@ -48,7 +53,10 @@ impl<'a> LinkerWriter<'a> {

let rom_start_sym = style.segment_rom_start(&segment.name);
self.write_symbol(&rom_start_sym, "__romPos");
self.write_symbol(&style.segment_vram_start(&segment.name), &format!("ADDR({})", emitted_segment_name));
self.write_symbol(
&style.segment_vram_start(&segment.name),
&format!("ADDR({})", emitted_segment_name),
);

self.write_segment_start(segment, &emitted_segment_name, false);
// TODO: FILL()
Expand All @@ -63,7 +71,10 @@ impl<'a> LinkerWriter<'a> {
self.write_files_for_section(segment, section);

self.write_symbol(&section_end_sym, ".");
self.write_symbol(&section_size_sym, &format!("ABSOLUTE({} - {})", section_end_sym, section_start_sym));
self.write_symbol(
&section_size_sym,
&format!("ABSOLUTE({} - {})", section_end_sym, section_start_sym),
);
}

self.write_segment_end(segment, &emitted_segment_name, false);
Expand All @@ -79,7 +90,10 @@ impl<'a> LinkerWriter<'a> {
self.write_files_for_section(segment, section);
}
self.write_symbol(&section_end_sym, ".");
self.write_symbol(&section_size_sym, &format!("ABSOLUTE({} - {})", section_end_sym, section_start_sym));
self.write_symbol(
&section_size_sym,
&format!("ABSOLUTE({} - {})", section_end_sym, section_start_sym),
);
}
self.write_segment_end(segment, &emitted_segment_name, true);

Expand All @@ -88,10 +102,9 @@ impl<'a> LinkerWriter<'a> {
self.writeln("");
}


pub fn save_linker_script(&self, path: &Path) -> Result<(), std::io::Error> {
match path.parent() {
None => {},
None => {}
Some(parent) => fs::create_dir_all(parent)?,
}

Expand Down Expand Up @@ -184,13 +197,17 @@ impl LinkerWriter<'_> {

path.extend(&file.path);

let wildcard = if segment.wildcard_sections.unwrap() {"*"} else {""};
let wildcard = if segment.wildcard_sections.unwrap() {
"*"
} else {
""
};

// TODO: figure out glob support
match file.kind {
FileKind::Object => {
self.writeln(&format!("{}({}{});", path.display(), section, wildcard));
},
}
FileKind::Archive => todo!(),
}
}
Expand Down
15 changes: 12 additions & 3 deletions slinky/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ pub struct Options {
pub paths: PathsConfigs,

// Options passed down to each segment

pub use_subalign: bool,
pub subalign: u64,

Expand All @@ -25,8 +24,18 @@ pub struct Options {
impl Default for Options {
fn default() -> Self {
Self {
alloc_sections: vec![".text".into(), ".data".into(), ".rodata".into(), ".sdata".into()],
noload_sections: vec![".sbss".into(), ".scommon".into(), ".bss".into(), "COMMON".into()],
alloc_sections: vec![
".text".into(),
".data".into(),
".rodata".into(),
".sdata".into(),
],
noload_sections: vec![
".sbss".into(),
".scommon".into(),
".bss".into(),
"COMMON".into(),
],
segment_symbols_style: SegmentSymbolsStyle::Splat,
paths: PathsConfigs::default(),

Expand Down
6 changes: 2 additions & 4 deletions slinky/src/paths_configs.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* SPDX-FileCopyrightText: © 2024 decompals */
/* SPDX-License-Identifier: MIT */

use std::path::PathBuf;
use serde::Deserialize;
use std::path::PathBuf;

#[derive(Deserialize, PartialEq, Debug)]
#[serde(default)]
Expand All @@ -12,8 +12,6 @@ pub struct PathsConfigs {

impl Default for PathsConfigs {
fn default() -> Self {
Self {
base_path: None,
}
Self { base_path: None }
}
}
1 change: 0 additions & 1 deletion slinky/src/segment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,3 @@ pub struct Segment {

pub wildcard_sections: Option<bool>,
}

27 changes: 16 additions & 11 deletions slinky/src/segment_symbols_style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,44 +25,50 @@ impl SegmentSymbolsStyle {
}
}


pub fn segment_vram_start(&self, seg_name: &str) -> String {
match self {
SegmentSymbolsStyle::Splat => format!("{}_VRAM", seg_name),
SegmentSymbolsStyle::Makerom => format!("_{}SegmentStart", seg_name),
}
}


pub fn segment_vram_end(&self, seg_name: &str) -> String {
match self {
SegmentSymbolsStyle::Splat => format!("{}_VRAM_END", seg_name),
SegmentSymbolsStyle::Makerom => format!("_{}SegmentEnd", seg_name),
}
}


fn convert_section_name_to_linker_format(&self, section_type: &str) -> String {
match self {
SegmentSymbolsStyle::Splat => {
section_type.replace(".", "_")
},
SegmentSymbolsStyle::Splat => section_type.replace(".", "_"),
SegmentSymbolsStyle::Makerom => {
if section_type == ".rodata" {
"RoData".to_string()
} else {
// TODO: there must be a better way to Capitalize a string
if section_type.chars().nth(0) == Some('.') {
section_type.chars().nth(1).expect("").to_uppercase().to_string() + &section_type[2..]
section_type
.chars()
.nth(1)
.expect("")
.to_uppercase()
.to_string()
+ &section_type[2..]
} else {
section_type.chars().nth(0).expect("").to_uppercase().to_string() + &section_type[1..]
section_type
.chars()
.nth(0)
.expect("")
.to_uppercase()
.to_string()
+ &section_type[1..]
}
}
},
}
}
}


pub fn segment_section_start(&self, seg_name: &str, section_type: &str) -> String {
let sec = self.convert_section_name_to_linker_format(&section_type);

Expand All @@ -89,5 +95,4 @@ impl SegmentSymbolsStyle {
SegmentSymbolsStyle::Makerom => format!("_{}Segment{}Size", seg_name, sec),
}
}

}

0 comments on commit 71cc1e6

Please sign in to comment.