From 0b0f407c9cfecf4aaa3511d1a1fb37ef96f380c8 Mon Sep 17 00:00:00 2001 From: Ryan Jones-Ward Date: Fri, 5 Jul 2024 11:39:51 +0100 Subject: [PATCH] Some notes on the next chunk of compiler work. --- redox-core/src/compiling/compiler.rs | 15 ++++++++++++++- redox-core/src/compiling/executable.rs | 12 ++++++++++++ redox-core/src/compiling/mod.rs | 1 + redox-terminal/src/main.rs | 6 ++---- 4 files changed, 29 insertions(+), 5 deletions(-) create mode 100644 redox-core/src/compiling/executable.rs diff --git a/redox-core/src/compiling/compiler.rs b/redox-core/src/compiling/compiler.rs index 6d6193d..c7a4589 100644 --- a/redox-core/src/compiling/compiler.rs +++ b/redox-core/src/compiling/compiler.rs @@ -7,11 +7,14 @@ use crate::{ utils, }; +use super::executable::Executable; + pub struct Compiler { /// A vector containing the compiled bytecode. bytes: Vec, local_labels: Vec, + data_labels: Vec, global_labels: Vec, label_positions: HashMap, } @@ -21,6 +24,7 @@ impl Compiler { Self { bytes: Vec::new(), local_labels: Vec::new(), + data_labels: Vec::new(), global_labels: Vec::new(), label_positions: HashMap::new(), } @@ -57,6 +61,12 @@ impl Compiler { let mut parser = AsmParser::new(); parser.parse(assembly); + // TODO - the data section(s) should be compiled first because their size, once computed, is invariant. + // TODO - references to the contained data may be made in the labels found within the code. + // TODO - the data items should be added as labels to the data_labels list. + + // TODO - all labelled jumps MUST to be offset by the size of the data sections! + let mut instructions = parser.parsed_instructions; self.load_labels(&instructions); @@ -64,7 +74,7 @@ impl Compiler { self.calculate_label_positions(&instructions); if optimize { - // TODO - optimizations should ideally go here. + // TODO - optimizations should go here. } // TODO - the calculate label positions method should be called here again. @@ -76,6 +86,9 @@ impl Compiler { self.compile(&instructions); + // TODO - package the bytecode into an executable. + let executable = Executable::new(); + &self.bytes } diff --git a/redox-core/src/compiling/executable.rs b/redox-core/src/compiling/executable.rs new file mode 100644 index 0000000..c5ea2d9 --- /dev/null +++ b/redox-core/src/compiling/executable.rs @@ -0,0 +1,12 @@ +// The structure of the executable file is as follows: +// [READ-ONLY DATA] +// [DATA] +// [CODE] + +pub struct Executable {} + +impl Executable { + pub fn new() -> Self { + Self {} + } +} diff --git a/redox-core/src/compiling/mod.rs b/redox-core/src/compiling/mod.rs index 562c9f3..6659819 100644 --- a/redox-core/src/compiling/mod.rs +++ b/redox-core/src/compiling/mod.rs @@ -1,2 +1,3 @@ pub mod compiler; pub mod decompiler; +pub mod executable; diff --git a/redox-terminal/src/main.rs b/redox-terminal/src/main.rs index 3bded2e..4dcb8dc 100644 --- a/redox-terminal/src/main.rs +++ b/redox-terminal/src/main.rs @@ -65,21 +65,19 @@ fn main() { if cfg!(target_endian = "big") { panic!("currently unsupported"); } + 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 data = compiler.compile_assembly(code, true); /*let instructions = &[ - /*// Indicate that we want to make a seeded random number generator. + // Indicate that we want to make a seeded random number generator. Instruction::OutU8Imm(0x1, 0x0), // Specify our seed. Instruction::OutU32Imm(0xdeadbeef, 0x0), // Read a PRNG from the device. Instruction::InU32Reg(0x0, RegisterId::EAX), Instruction::PushU32Imm(0xdeadbeef), - Instruction::Halt,*/ - Instruction::MovU32ImmU32Reg(0x1, RegisterId::EAX), - //Instruction::AddU32ImmU32Reg(0x2, RegisterId::EAX), Instruction::Halt, ];