Skip to content

Commit

Permalink
Remove some code for computing the data label locations that is no lo…
Browse files Browse the repository at this point in the history
…nger needed.
  • Loading branch information
sciguyryan committed Jul 18, 2024
1 parent 18e80fc commit 1a98763
Showing 1 changed file with 22 additions and 26 deletions.
48 changes: 22 additions & 26 deletions redox-core/src/compiling/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use super::{
pub struct Compiler {
/// A vector containing the compiled bytecode.
bytes: Vec<u8>,
/// A [`HashMap`] containing the known labels, and their associated [`LabelEntry`].
labels: HashMap<String, LabelEntry>,
}

Expand All @@ -26,6 +27,13 @@ impl Compiler {
}
}

fn assert_label_does_not_exist(&self, label: &String) {
assert!(
!self.labels.contains_key(label),
"a label has already been declared with the name {label}"
);
}

/// Compile a sequence of instructions into bytecode.
///
/// # Arguments
Expand Down Expand Up @@ -62,6 +70,7 @@ impl Compiler {

let mut instructions = parser.parsed_instructions;

// Now load the code labels.
self.load_code_labels(&instructions);

if optimize {
Expand All @@ -73,12 +82,6 @@ impl Compiler {
// TODO - this is because the instructions may have changed, altering the relative positions of the labels.
}

// We now need to calculate the length of the instructions.
let code_segment_len = Instruction::total_size_of_instructions(&instructions);

// TODO - this is a placeholder for the moment.
self.calculate_data_label_positions(code_segment_len);

// TODO - do we care enough to do this?
if optimize {
// TODO - a second optimization pass should go here to optimize any labels
Expand All @@ -97,18 +100,6 @@ impl Compiler {
&self.bytes
}

/// Calculate the positions of the local data labels within the provided instruction list.
///
/// # Arguments
///
/// * `instructions` - A slice of [`Instruction`]s that correspond to the instructions to be compiled.
#[inline]
fn calculate_data_label_positions(&mut self, data_segment_len: usize) {}

fn assert_label_does_not_exist(&self, label: &String) {
assert!(!self.labels.contains_key(label), "a label has already been declared with the name {label}");
}

/// Build a list of code labels found within the provided instruction list and compute their location relative to the start of the code block.
///
/// # Arguments
Expand All @@ -122,8 +113,10 @@ impl Compiler {
if let Instruction::Label(l) = ins {
self.assert_label_does_not_exist(l);

self.labels
.insert(l.clone(), LabelEntry::new(LabelType::Code, l.clone(), position));
self.labels.insert(
l.clone(),
LabelEntry::new(LabelType::Code, l.clone(), position),
);
}

position += ins.get_total_instruction_size();
Expand All @@ -142,8 +135,10 @@ impl Compiler {
for d in data_declarations {
self.assert_label_does_not_exist(&d.label);

self.labels
.insert(d.label.clone(), LabelEntry::new(LabelType::Code, d.label.clone(), position));
self.labels.insert(
d.label.clone(),
LabelEntry::new(LabelType::Code, d.label.clone(), position),
);

position += d.bytes.len();
}
Expand Down Expand Up @@ -173,13 +168,14 @@ impl Compiler {
match label_entry.label_type {
LabelType::Code => {
*ins = match ins {
Instruction::CallAbsU32Imm(_, label) => {
Instruction::CallRelCSU32Offset(label_entry.position as u32, label.clone())
}
Instruction::CallAbsU32Imm(_, label) => Instruction::CallRelCSU32Offset(
label_entry.position as u32,
label.clone(),
),
Instruction::CallRelCSU32Offset(_, _) => continue,
_ => unreachable!("unexpected labelled instruction instance - {ins}"),
}
},
}
LabelType::Data => todo!(),
LabelType::Global => todo!(),
}
Expand Down

0 comments on commit 1a98763

Please sign in to comment.