Skip to content

Commit

Permalink
Emit vram class size symbol
Browse files Browse the repository at this point in the history
  • Loading branch information
AngheloAlf committed Apr 29, 2024
1 parent 4494c5d commit fb9ac91
Show file tree
Hide file tree
Showing 8 changed files with 517 additions and 13 deletions.
7 changes: 7 additions & 0 deletions slinky/src/linker_symbols_style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,11 @@ impl LinkerSymbolsStyle {
LinkerSymbolsStyle::Makerom => format!("_{}ClassVramEnd", name),
}
}

pub fn vram_class_size(&self, name: &str) -> String {
match self {
LinkerSymbolsStyle::Splat => format!("{}_CLASS_VRAM_SIZE", name),
LinkerSymbolsStyle::Makerom => format!("_{}ClassVramSize", name),
}
}
}
41 changes: 28 additions & 13 deletions slinky/src/linker_writer.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* SPDX-FileCopyrightText: © 2024 decompals */
/* SPDX-License-Identifier: MIT */

use std::collections::HashMap;
use std::path::PathBuf;
use std::{io::Write, path::Path};

Expand All @@ -19,7 +18,7 @@ pub struct LinkerWriter<'a> {

single_segment: bool,

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

/* Options to control stuff */
emit_sections_kind_symbols: bool,
Expand All @@ -30,7 +29,7 @@ pub struct LinkerWriter<'a> {

impl<'a> LinkerWriter<'a> {
pub fn new(d: &'a Document) -> Self {
let mut vram_classes = HashMap::with_capacity(d.vram_classes.len());
let mut vram_classes = indexmap::IndexMap::with_capacity(d.vram_classes.len());
for vram_class in &d.vram_classes {
vram_classes.insert(vram_class.name.clone(), vram_class.clone());
}
Expand Down Expand Up @@ -80,9 +79,31 @@ impl<'a> LinkerWriter<'a> {
}

pub fn end_sections(&mut self) {
let style = &self.d.settings.linker_symbols_style;
let mut need_ln = false;

for (vram_class_name, vram_class) in &self.vram_classes {
if !vram_class.emitted {
continue;
}

self.buffer.write_symbol(
&style.vram_class_size(vram_class_name),
&format!(
"{} - {}",
style.vram_class_end(vram_class_name),
style.vram_class_start(vram_class_name),
),
);

need_ln = true;
}

if !self.d.settings.sections_allowlist.is_empty() {
if need_ln {
self.buffer.write_empty_line();
}

let address = " 0";

for sect in &self.d.settings.sections_allowlist {
Expand Down Expand Up @@ -168,13 +189,9 @@ impl<'a> LinkerWriter<'a> {
} else {
self.buffer.write_symbol(&vram_class_sym, "0x00000000");
for other_class_name in &vram_class.follows_classes {
self.buffer.write_symbol(
self.buffer.write_symbol_max_self(
&vram_class_sym,
&format!(
"MAX({}, {})",
vram_class_sym,
style.vram_class_end(other_class_name)
),
&style.vram_class_end(other_class_name),
);
}
}
Expand Down Expand Up @@ -225,10 +242,8 @@ impl<'a> LinkerWriter<'a> {
self.buffer.write_empty_line();

let vram_class_sym_end = style.vram_class_end(vram_class_name);
self.buffer.write_symbol(
&vram_class_sym_end,
&format!("MAX({}, {})", vram_class_sym_end, main_seg_sym_end),
);
self.buffer
.write_symbol_max_self(&vram_class_sym_end, &main_seg_sym_end);
}

self.buffer.write_empty_line();
Expand Down
4 changes: 4 additions & 0 deletions slinky/src/script_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ impl ScriptBuffer {
symbol, symbol, align_value
));
}

pub fn write_symbol_max_self(&mut self, symbol: &str, other_sym: &str) {
self.writeln(&format!("{} = MAX({}, {});", symbol, symbol, other_sym));
}
}

impl ScriptBuffer {
Expand Down
8 changes: 8 additions & 0 deletions tests/partial_linking/vram_classes.ld
Original file line number Diff line number Diff line change
Expand Up @@ -925,6 +925,14 @@ SECTIONS

segment_06_CLASS_VRAM_END = MAX(segment_06_CLASS_VRAM_END, assets4_VRAM_END);

battle_partner_CLASS_VRAM_SIZE = battle_partner_CLASS_VRAM_END - battle_partner_CLASS_VRAM_START;
battle_code_CLASS_VRAM_SIZE = battle_code_CLASS_VRAM_END - battle_code_CLASS_VRAM_START;
heaps2_CLASS_VRAM_SIZE = heaps2_CLASS_VRAM_END - heaps2_CLASS_VRAM_START;
world_script_api_CLASS_VRAM_SIZE = world_script_api_CLASS_VRAM_END - world_script_api_CLASS_VRAM_START;
texture_memory_CLASS_VRAM_SIZE = texture_memory_CLASS_VRAM_END - texture_memory_CLASS_VRAM_START;
segment_05_CLASS_VRAM_SIZE = segment_05_CLASS_VRAM_END - segment_05_CLASS_VRAM_START;
segment_06_CLASS_VRAM_SIZE = segment_06_CLASS_VRAM_END - segment_06_CLASS_VRAM_START;

.shstrtab 0 :
{
*(.shstrtab);
Expand Down
2 changes: 2 additions & 0 deletions tests/partial_linking/vram_classes.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
settings:
base_path: build

symbols_header_path: tests/partial_linking/vram_classes.h

partial_scripts_folder: tests/partial_linking/vram_classes
partial_build_segments_folder: segments

Expand Down
Loading

0 comments on commit fb9ac91

Please sign in to comment.