Skip to content

Commit

Permalink
common sym display
Browse files Browse the repository at this point in the history
  • Loading branch information
AngheloAlf committed Dec 4, 2024
1 parent cccdcc7 commit 6ed438a
Show file tree
Hide file tree
Showing 11 changed files with 297 additions and 103 deletions.
9 changes: 5 additions & 4 deletions src/spimdisasm/src/context/the_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,18 +398,19 @@ pub(crate) mod python_bindings {
)
}

/*
#[pyo3(name = "create_section_bss")]
pub fn py_create_section_bss(
&mut self,
settings: &SectionNoloadSettings,
name: String,
vram_range: AddressRange<Vram>,
vram_start: u32, // Vram // TODO
vram_end: u32, // Vram // TODO
parent_segment_info: ParentSegmentInfo,
) -> Result<SectionNoload, OwnedSegmentNotFoundError> {
self.create_section_bss(settings, name, vram_range, parent_segment_info)
let vram_ranges = AddressRange::new(Vram::new(vram_start), Vram::new(vram_end));

self.create_section_bss(settings, name, vram_ranges, parent_segment_info)
}
*/

#[pyo3(name = "create_section_gcc_except_table")]
pub fn py_create_section_gcc_except_table(
Expand Down
12 changes: 9 additions & 3 deletions src/spimdisasm/src/metadata/segment_metadata.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
/* SPDX-FileCopyrightText: © 2024 Decompollaborate */
/* SPDX-License-Identifier: MIT */

use alloc::collections::btree_map::BTreeMap;
use alloc::collections::btree_map::{self, BTreeMap};
use alloc::vec::Vec;

#[cfg(not(feature = "nightly"))]
use ::polonius_the_crab::prelude::*;

#[cfg(feature = "nightly")]
use alloc::collections::btree_map;
#[cfg(feature = "nightly")]
use core::ops::Bound;

Expand Down Expand Up @@ -399,6 +397,14 @@ impl SegmentMetadata {
}
}
}

pub(crate) fn find_symbols_range(
&self,
vram_start: Vram,
vram_end: Vram,
) -> btree_map::Range<'_, Vram, SymbolMetadata> {
self.symbols.range(vram_start..vram_end)
}
}

impl SegmentMetadata {
Expand Down
2 changes: 0 additions & 2 deletions src/spimdisasm/src/sections/section_executable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -556,8 +556,6 @@ fn find_functions_check_function_ended(

#[cfg(feature = "pyo3")]
pub(crate) mod python_bindings {
use rabbitizer::InstructionFlags;

use crate::symbols::display::FunctionDisplaySettings;

use super::*;
Expand Down
43 changes: 30 additions & 13 deletions src/spimdisasm/src/sections/section_noload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,39 @@ impl SectionNoload {
let mut noload_symbols = Vec::new();
let mut symbol_vrams = BTreeSet::new();

// let owned_segment = context.find_owned_segment(&parent_segment_info)?;
let owned_segment = context.find_owned_segment(&parent_segment_info)?;

let mut symbols_info = BTreeSet::new();
// Ensure there's a symbol at the beginning of the section.
symbols_info.insert(vram_range.start());

// let mut maybe_pointers_to_other_sections = Vec::new();
/*
# If something that could be a pointer found in data happens to be in
# the middle of this bss file's addresses space then consider it as a
# new bss variable
for ptr in self.getAndPopPointerInDataReferencesRange(self.bssVramStart, self.bssVramEnd):
# Check if the symbol already exists, in case the user has provided size
contextSym = self.getSymbol(ptr, tryPlusOffset=True)
if contextSym is None:
self.addSymbol(ptr, sectionType=self.sectionType, isAutogenerated=True)
*/

// TODO: fill `symbols_info``
// TODO: fill `symbols_info`

for (sym_vram, sym) in
owned_segment.find_symbols_range(vram_range.start(), vram_range.end())
{
symbols_info.insert(*sym_vram);

if let Some(size) = sym.user_declared_size() {
// TODO: signal this symbol is an autogenerated pad
let next_vram = sym.vram() + size;
if next_vram != vram_range.end() {
// Avoid generating a symbol at the end of the section
symbols_info.insert(sym.vram() + size);
}
}
}

let symbols_info_vec: Vec<Vram> = symbols_info.into_iter().collect();

Expand All @@ -101,13 +125,6 @@ impl SectionNoload {
noload_symbols.push(sym);
}

// let owned_segment_mut = context.find_owned_segment_mut(&parent_segment_info)?;
// for (possible_pointer, rom_address_referencing_pointer) in maybe_pointers_to_other_sections
// {
// owned_segment_mut
// .add_possible_pointer_in_data(possible_pointer, rom_address_referencing_pointer);
// }

Ok(Self {
name,
vram_range,
Expand Down Expand Up @@ -152,6 +169,8 @@ impl Section for SectionNoload {

#[cfg(feature = "pyo3")]
pub(crate) mod python_bindings {
use crate::symbols::display::SymNoloadDisplaySettings;

use super::*;

#[pymethods]
Expand All @@ -169,18 +188,16 @@ pub(crate) mod python_bindings {
self.noload_symbols.len()
}

/*
#[pyo3(name = "display_sym")]
pub fn py_display_sym(
&self,
context: &Context,
index: usize,
settings: &SymDataDisplaySettings,
settings: &SymNoloadDisplaySettings,
) -> Option<String> {
self.noload_symbols
.get(index)
.map(|sym| sym.display(context, settings).to_string())
}
*/
}
}
77 changes: 20 additions & 57 deletions src/spimdisasm/src/symbols/display/function_display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

use core::fmt;

use alloc::string::String;
use rabbitizer::{DisplayFlags, Instruction, Vram};

#[cfg(feature = "pyo3")]
Expand All @@ -16,30 +15,29 @@ use crate::{
symbols::{trait_symbol::RomSymbol, Symbol, SymbolFunction},
};

use super::SymCommonDisplaySettings;

#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "pyo3", pyclass(module = "spimdisasm"))]
pub struct FunctionDisplaySettings {
common: SymCommonDisplaySettings,

display_flags: DisplayFlags,
line_end: Option<String>,

asm_label_indentation: u8,

_gp_rel_hack: bool,
}

impl FunctionDisplaySettings {
pub fn new(display_flags: DisplayFlags) -> Self {
Self {
common: SymCommonDisplaySettings::new(),
display_flags,
line_end: None,
asm_label_indentation: 2,
_gp_rel_hack: false,
}
}

pub(crate) fn line_end(&self) -> &str {
if let Some(line_end) = &self.line_end {
line_end
} else {
"\n"
}
}
}

#[derive(Debug, Copy, Clone, Hash, PartialEq)]
Expand Down Expand Up @@ -105,61 +103,26 @@ impl FunctionDisplay<'_, '_, '_> {
f,
"{}:{}",
sym_label.display_name(),
self.settings.line_end()
self.settings.common.line_end()
)?;
}
}

Ok(())
}

fn display_asm_comment(&self, f: &mut fmt::Formatter<'_>, instr: &Instruction) -> fmt::Result {
// TODO:
/*
indentation = " " * common.GlobalConfig.ASM_INDENTATION
if not common.GlobalConfig.ASM_COMMENT:
return indentation
if emitRomOffset:
offsetHex = "{0:0{1}X} ".format(localOffset + self.inFileOffset + self.commentOffset, common.GlobalConfig.ASM_COMMENT_OFFSET_WIDTH)
else:
offsetHex = ""
currentVram = self.getVramOffset(localOffset)
vramHex = f"{currentVram:08X}"
wordValueHex = ""
if wordValue is not None:
if isDouble:
wordValueHex = f"{common.Utils.qwordToCurrenEndian(wordValue):016X} "
else:
wordValueHex = f"{common.Utils.wordToCurrenEndian(wordValue):08X} "
return f"{indentation}/* {offsetHex}{vramHex} {wordValueHex}*/
"
*/

write!(f, "/* ")?;
let current_vram = instr.vram();
if let Some(rom) = self.sym.rom_vram_range().rom_from_vram(current_vram) {
// TODO: implement display for RomAddress
write!(f, "{:06X} ", rom.inner())?;
}
write!(f, "{} ", current_vram)?;
// TODO: endian
write!(f, "{:08X} ", instr.word())?;

write!(f, "*/")
}

fn display_instruction(
&self,
f: &mut fmt::Formatter<'_>,
instr: &Instruction,
prev_instr_had_delay_slot: bool,
) -> fmt::Result {
self.display_asm_comment(f, instr)?;
let vram = instr.vram();
let rom = self.sym.rom_vram_range().rom_from_vram(vram);
self.settings
.common
.display_asm_comment(f, rom, vram, Some(instr.word()))?;

// TODO: why two spaces instead of one?
write!(f, " ")?;

Expand All @@ -175,7 +138,7 @@ impl FunctionDisplay<'_, '_, '_> {
f,
"{}{}",
instr.display(imm_override, &self.settings.display_flags),
self.settings.line_end()
self.settings.common.line_end()
)
}

Expand All @@ -198,9 +161,9 @@ impl fmt::Display for FunctionDisplay<'_, '_, '_> {
.ok_or(fmt::Error)?;

let name = metadata.display_name();
write!(f, ".globl {}{}", name, self.settings.line_end())?;
write!(f, ".globl {}{}", name, self.settings.common.line_end())?;

write!(f, "{}:{}", name, self.settings.line_end())?;
write!(f, "{}:{}", name, self.settings.common.line_end())?;

let mut prev_instr_had_delay_slot = false;
for instr in self.sym.instructions() {
Expand All @@ -211,7 +174,7 @@ impl fmt::Display for FunctionDisplay<'_, '_, '_> {
prev_instr_had_delay_slot = instr.opcode().has_delay_slot();
}

write!(f, ".end {}{}", name, self.settings.line_end())
write!(f, ".end {}{}", name, self.settings.common.line_end())
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/spimdisasm/src/symbols/display/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
/* SPDX-License-Identifier: MIT */

mod function_display;
mod sym_common_display;
mod sym_data_display;
mod sym_noload_display;

pub use function_display::{FunctionDisplay, FunctionDisplaySettings};
pub(crate) use sym_common_display::SymCommonDisplaySettings;
pub use sym_data_display::{SymDataDisplay, SymDataDisplaySettings};
pub use sym_noload_display::{SymNoloadDisplay, SymNoloadDisplaySettings};
88 changes: 88 additions & 0 deletions src/spimdisasm/src/symbols/display/sym_common_display.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/* SPDX-FileCopyrightText: © 2024 Decompollaborate */
/* SPDX-License-Identifier: MIT */

use core::fmt;

use alloc::string::String;
use rabbitizer::Vram;

use crate::rom_address::RomAddress;

#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct SymCommonDisplaySettings {
line_end: Option<String>,

emit_asm_comment: bool,

asm_indentation: u8,
}

impl SymCommonDisplaySettings {
pub fn new() -> Self {
Self {
line_end: None,
emit_asm_comment: true,
asm_indentation: 4,
}
}

pub fn line_end(&self) -> &str {
if let Some(line_end) = &self.line_end {
line_end
} else {
"\n"
}
}
}

impl SymCommonDisplaySettings {
pub fn display_asm_comment(
&self,
f: &mut fmt::Formatter<'_>,
rom: Option<RomAddress>,
vram: Vram,
word: Option<u32>,
) -> fmt::Result {
if self.asm_indentation > 0 {
write!(f, "{:width$}", " ", width = self.asm_indentation as usize)?;
}

if !self.emit_asm_comment {
return Ok(());
}

// TODO:
/*
if emitRomOffset:
offsetHex = "{0:0{1}X} ".format(localOffset + self.inFileOffset + self.commentOffset, common.GlobalConfig.ASM_COMMENT_OFFSET_WIDTH)
else:
offsetHex = ""
currentVram = self.getVramOffset(localOffset)
vramHex = f"{currentVram:08X}"
wordValueHex = ""
if wordValue is not None:
if isDouble:
wordValueHex = f"{common.Utils.qwordToCurrenEndian(wordValue):016X} "
else:
wordValueHex = f"{common.Utils.wordToCurrenEndian(wordValue):08X} "
return f"{indentation}/* {offsetHex}{vramHex} {wordValueHex}*/
"
*/

write!(f, "/* ")?;
if let Some(rom) = rom {
// TODO: implement display for RomAddress
write!(f, "{:06X} ", rom.inner())?;
}
write!(f, "{} ", vram)?;
if let Some(word) = word {
// TODO: endian
write!(f, "{:08X} ", word)?;
}

write!(f, "*/")
}
}
Loading

0 comments on commit 6ed438a

Please sign in to comment.