Skip to content

Commit

Permalink
Implement dir
Browse files Browse the repository at this point in the history
Closes #67
  • Loading branch information
AngheloAlf committed Apr 30, 2024
1 parent 65632f0 commit 8b2948c
Show file tree
Hide file tree
Showing 13 changed files with 646 additions and 35 deletions.
27 changes: 27 additions & 0 deletions docs/file_format/segments.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,33 @@ The name of an existing [`vram_class`](vram_class.md).

`null`

## `dir`

Used as a prefix for all the files emitted for this Segment.

### Example

```yaml
settings:
base_path: build
segments:
- name: omo2_1
dir: src/battle/area/omo2_1
files:
- { path: actor/shy_squad.o }
```

Emits the `build/src/battle/area/omo2_1/actor/shy_squad.o` file path.

### Valid values

Any valid path.

### Default value

Empty path.

## `alloc_sections`

List of allocatable sections (the ones that take ROM space) for this specific
Expand Down
2 changes: 1 addition & 1 deletion slinky/src/absent_nullable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ where
}
}

// TODO: consider changing the "default" callbacks to return a result
// TODO: consider changing the "default" callbacks to return a Result
impl<T> AbsentNullable<T> {
pub fn get_non_null<F>(self, name: &str, default: F) -> Result<T, SlinkyError>
where
Expand Down
29 changes: 23 additions & 6 deletions slinky/src/linker_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ pub struct LinkerWriter<'a> {
// Used for dependency generation
files_paths: indexmap::IndexSet<PathBuf>,

single_segment: bool,

vram_classes: indexmap::IndexMap<String, VramClass>,

single_segment: bool,
reference_partial_objects: bool,

/* Options to control stuff */
emit_sections_kind_symbols: bool,
emit_section_symbols: bool,
Expand All @@ -39,17 +40,26 @@ impl<'a> LinkerWriter<'a> {

files_paths: indexmap::IndexSet::new(),

single_segment: false,

vram_classes,

single_segment: false,
reference_partial_objects: false,

emit_sections_kind_symbols: true,
emit_section_symbols: true,

d,
}
}

pub fn new_reference_partial_objects(d: &'a Document) -> Self {
let mut s = Self::new(d);

s.reference_partial_objects = true;

s
}

pub fn add_all_segments(&mut self, segments: &[Segment]) {
if self.d.settings.single_segment_mode {
assert!(segments.len() == 1);
Expand Down Expand Up @@ -647,6 +657,13 @@ impl LinkerWriter<'_> {
}

fn emit_section(&mut self, segment: &Segment, section: &str) {
let mut base_path = PathBuf::new();

base_path.extend(&self.d.settings.base_path);
if !self.reference_partial_objects {
base_path.extend(&segment.dir);
}

for file in &segment.files {
if !file.section_order.is_empty() {
// Keys specify the section and value specify where it will be put
Expand All @@ -660,12 +677,12 @@ impl LinkerWriter<'_> {
// Check if any other section should be placed be placed here
for (k, v) in &file.section_order {
if v == section {
self.emit_file(file, segment, k, &self.d.settings.base_path);
self.emit_file(file, segment, k, &base_path);
}
}
}

self.emit_file(file, segment, section, &self.d.settings.base_path);
self.emit_file(file, segment, section, &base_path);
}
}

Expand Down
2 changes: 1 addition & 1 deletion slinky/src/partial_linker_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub struct PartialLinkerWriter<'a> {
impl<'a> PartialLinkerWriter<'a> {
pub fn new(d: &'a Document) -> Self {
Self {
main_writer: LinkerWriter::new(d),
main_writer: LinkerWriter::new_reference_partial_objects(d),

partial_writers: Vec::new(),

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

use std::path::PathBuf;

use serde::Deserialize;

use crate::{
Expand Down Expand Up @@ -33,6 +35,9 @@ pub struct Segment {
/// Not compatible with `fixed_vram`, `fixed_symbol` or `follows_segment`.
pub vram_class: Option<String>,

/// Used as a prefix for all the files emitted for this Segment.
pub dir: PathBuf,

// The default value of the following members come from Settings
pub alloc_sections: Vec<String>,
pub noload_sections: Vec<String>,
Expand Down Expand Up @@ -64,6 +69,9 @@ pub(crate) struct SegmentSerial {
#[serde(default)]
pub vram_class: AbsentNullable<String>,

#[serde(default)]
pub dir: AbsentNullable<PathBuf>,

// The default of the following come from Options
#[serde(default)]
pub alloc_sections: AbsentNullable<Vec<String>>,
Expand Down Expand Up @@ -162,6 +170,8 @@ impl SegmentSerial {
});
}

let dir = self.dir.get_non_null("dir", PathBuf::new)?;

let alloc_sections = self
.alloc_sections
.get_non_null("alloc_sections", || settings.alloc_sections.clone())?;
Expand Down Expand Up @@ -196,6 +206,7 @@ impl SegmentSerial {
fixed_symbol,
follows_segment,
vram_class,
dir,
alloc_sections,
noload_sections,
subalign,
Expand Down
111 changes: 111 additions & 0 deletions tests/partial_linking/vram_classes.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,116 @@ extern char boot_VRAM_END[];
extern char boot_VRAM_SIZE[];
extern char boot_ROM_END[];
extern char boot_ROM_SIZE[];
extern char battle_area2_VRAM_CLASS_START[];
extern char battle_area2_VRAM_CLASS_END[];
extern char omo2_1_ROM_START[];
extern char omo2_1_VRAM[];
extern char omo2_1_alloc_VRAM[];
extern char omo2_1_TEXT_START[];
extern char omo2_1_TEXT_END[];
extern char omo2_1_TEXT_SIZE[];
extern char omo2_1_DATA_START[];
extern char omo2_1_DATA_END[];
extern char omo2_1_DATA_SIZE[];
extern char omo2_1_RODATA_START[];
extern char omo2_1_RODATA_END[];
extern char omo2_1_RODATA_SIZE[];
extern char omo2_1_SDATA_START[];
extern char omo2_1_SDATA_END[];
extern char omo2_1_SDATA_SIZE[];
extern char omo2_1_alloc_VRAM_END[];
extern char omo2_1_alloc_VRAM_SIZE[];
extern char omo2_1_noload_VRAM[];
extern char omo2_1_SBSS_START[];
extern char omo2_1_SBSS_END[];
extern char omo2_1_SBSS_SIZE[];
extern char omo2_1_SCOMMON_START[];
extern char omo2_1_SCOMMON_END[];
extern char omo2_1_SCOMMON_SIZE[];
extern char omo2_1_BSS_START[];
extern char omo2_1_BSS_END[];
extern char omo2_1_BSS_SIZE[];
extern char omo2_1COMMON_START[];
extern char omo2_1COMMON_END[];
extern char omo2_1COMMON_SIZE[];
extern char omo2_1_noload_VRAM_END[];
extern char omo2_1_noload_VRAM_SIZE[];
extern char omo2_1_VRAM_END[];
extern char omo2_1_VRAM_SIZE[];
extern char omo2_1_ROM_END[];
extern char omo2_1_ROM_SIZE[];
extern char omo2_2_ROM_START[];
extern char omo2_2_VRAM[];
extern char omo2_2_alloc_VRAM[];
extern char omo2_2_TEXT_START[];
extern char omo2_2_TEXT_END[];
extern char omo2_2_TEXT_SIZE[];
extern char omo2_2_DATA_START[];
extern char omo2_2_DATA_END[];
extern char omo2_2_DATA_SIZE[];
extern char omo2_2_RODATA_START[];
extern char omo2_2_RODATA_END[];
extern char omo2_2_RODATA_SIZE[];
extern char omo2_2_SDATA_START[];
extern char omo2_2_SDATA_END[];
extern char omo2_2_SDATA_SIZE[];
extern char omo2_2_alloc_VRAM_END[];
extern char omo2_2_alloc_VRAM_SIZE[];
extern char omo2_2_noload_VRAM[];
extern char omo2_2_SBSS_START[];
extern char omo2_2_SBSS_END[];
extern char omo2_2_SBSS_SIZE[];
extern char omo2_2_SCOMMON_START[];
extern char omo2_2_SCOMMON_END[];
extern char omo2_2_SCOMMON_SIZE[];
extern char omo2_2_BSS_START[];
extern char omo2_2_BSS_END[];
extern char omo2_2_BSS_SIZE[];
extern char omo2_2COMMON_START[];
extern char omo2_2COMMON_END[];
extern char omo2_2COMMON_SIZE[];
extern char omo2_2_noload_VRAM_END[];
extern char omo2_2_noload_VRAM_SIZE[];
extern char omo2_2_VRAM_END[];
extern char omo2_2_VRAM_SIZE[];
extern char omo2_2_ROM_END[];
extern char omo2_2_ROM_SIZE[];
extern char omo2_3_ROM_START[];
extern char omo2_3_VRAM[];
extern char omo2_3_alloc_VRAM[];
extern char omo2_3_TEXT_START[];
extern char omo2_3_TEXT_END[];
extern char omo2_3_TEXT_SIZE[];
extern char omo2_3_DATA_START[];
extern char omo2_3_DATA_END[];
extern char omo2_3_DATA_SIZE[];
extern char omo2_3_RODATA_START[];
extern char omo2_3_RODATA_END[];
extern char omo2_3_RODATA_SIZE[];
extern char omo2_3_SDATA_START[];
extern char omo2_3_SDATA_END[];
extern char omo2_3_SDATA_SIZE[];
extern char omo2_3_alloc_VRAM_END[];
extern char omo2_3_alloc_VRAM_SIZE[];
extern char omo2_3_noload_VRAM[];
extern char omo2_3_SBSS_START[];
extern char omo2_3_SBSS_END[];
extern char omo2_3_SBSS_SIZE[];
extern char omo2_3_SCOMMON_START[];
extern char omo2_3_SCOMMON_END[];
extern char omo2_3_SCOMMON_SIZE[];
extern char omo2_3_BSS_START[];
extern char omo2_3_BSS_END[];
extern char omo2_3_BSS_SIZE[];
extern char omo2_3COMMON_START[];
extern char omo2_3COMMON_END[];
extern char omo2_3COMMON_SIZE[];
extern char omo2_3_noload_VRAM_END[];
extern char omo2_3_noload_VRAM_SIZE[];
extern char omo2_3_VRAM_END[];
extern char omo2_3_VRAM_SIZE[];
extern char omo2_3_ROM_END[];
extern char omo2_3_ROM_SIZE[];
extern char battle_partner_VRAM_CLASS_START[];
extern char battle_partner_VRAM_CLASS_END[];
extern char battle_partner_goompa_ROM_START[];
Expand Down Expand Up @@ -447,6 +557,7 @@ extern char assets4_VRAM_END[];
extern char assets4_VRAM_SIZE[];
extern char assets4_ROM_END[];
extern char assets4_ROM_SIZE[];
extern char battle_area2_VRAM_CLASS_SIZE[];
extern char battle_partner_VRAM_CLASS_SIZE[];
extern char battle_code_VRAM_CLASS_SIZE[];
extern char heaps2_VRAM_CLASS_SIZE[];
Expand Down
Loading

0 comments on commit 8b2948c

Please sign in to comment.