Skip to content

Commit

Permalink
Make compiling labels work. Weeee!
Browse files Browse the repository at this point in the history
  • Loading branch information
sciguyryan committed Jun 27, 2024
1 parent 33160f6 commit fa99db6
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ default-members = ["redox-terminal", "redox-core"]

[workspace.dependencies]
criterion = "0.5.1"
enumflags2 = "0.7.9"
enumflags2 = "0.7.10"
float-cmp = "0.9.0"
hashbrown = "0.14.5"
itertools = "0.13.0"
Expand Down
25 changes: 15 additions & 10 deletions redox-core/src/compiling/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ impl Compiler {

println!("{instructions:?}");

&[]
self.compile(&instructions);

&self.bytes
}

/// Calculate the positions of the local labels within the provided instruction list.
Expand Down Expand Up @@ -157,6 +159,16 @@ impl Compiler {
fn compile_instruction(&mut self, instruction: &Instruction) {
use Instruction as I;

// We don't need to do anything with labels.
if matches!(instruction, I::Label(_)) {
return;
}

// This pseudo-instruction shouldn't be constructed and exists to preserve the provided opcode when debugging.
if let I::Unknown(imm) = instruction {
unreachable!("invalid opcode id {imm}");
};

// First, we push the bytes for the opcode.
self.write_opcode(OpCode::from(instruction));

Expand Down Expand Up @@ -317,15 +329,8 @@ impl Compiler {
/******** [No Arguments] ********/
I::Nop | I::IntRet | I::RetArgsU32 | I::MachineReturn | I::Halt => {}

/* This pseudo-instruction shouldn't be constructed and exists as a compiler hint. */
I::Label(_) => {
unreachable!();
}

/* This pseudo-instruction shouldn't be constructed and exists to preserve the provided opcode when debugging. */
I::Unknown(imm) => {
unreachable!("invalid opcode id {imm}");
}
/* These are specifically handled above. */
I::Label(_) | I::Unknown(_) => {}
}
}

Expand Down
1 change: 1 addition & 0 deletions redox-core/src/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,7 @@ impl Cpu {
pub fn run(&mut self, com_bus: &mut CommunicationBus) {
while !self.is_halted {
let ins = self.fetch_decode_next_instruction(com_bus);
eprintln!("ins = {ins}");
self.run_instruction(com_bus, &ins);
}
}
Expand Down
11 changes: 6 additions & 5 deletions redox-terminal/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,12 @@ fn main() {
panic!("currently unsupported");
}

let code = "section .text\r\ncall :LABEL_1\r\n:LABEL_1";
let code = "section .text\r\npush 0\r\ncall :LABEL_1\r\nhlt\r\n:LABEL_1\r\nmov 0xdeadbeef, EAX\r\niret";

let mut compiler = Compiler::new();
let bytes = compiler.compile_assembly(code);
return;
let data = compiler.compile_assembly(code);

println!("{data:?}");

let instructions = &[
// Indicate that we want to make a seeded random number generator.
Expand All @@ -83,8 +84,8 @@ fn main() {
Instruction::Halt,
];

let mut compiler = Compiler::new();
let data = compiler.compile(instructions);
/*let mut compiler = Compiler::new();
let data = compiler.compile(instructions);*/

let mut vm = VirtualMachine::new(
vm::MIN_USER_SEGMENT_SIZE,
Expand Down

0 comments on commit fa99db6

Please sign in to comment.