Skip to content

Commit

Permalink
Some notes on the next chunk of compiler work.
Browse files Browse the repository at this point in the history
  • Loading branch information
sciguyryan committed Jul 5, 2024
1 parent 5f00455 commit 0b0f407
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
15 changes: 14 additions & 1 deletion redox-core/src/compiling/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ use crate::{
utils,
};

use super::executable::Executable;

pub struct Compiler {
/// A vector containing the compiled bytecode.
bytes: Vec<u8>,

local_labels: Vec<String>,
data_labels: Vec<String>,
global_labels: Vec<String>,
label_positions: HashMap<String, usize>,
}
Expand All @@ -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(),
}
Expand Down Expand Up @@ -57,14 +61,20 @@ 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);

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.
Expand All @@ -76,6 +86,9 @@ impl Compiler {

self.compile(&instructions);

// TODO - package the bytecode into an executable.
let executable = Executable::new();

&self.bytes
}

Expand Down
12 changes: 12 additions & 0 deletions redox-core/src/compiling/executable.rs
Original file line number Diff line number Diff line change
@@ -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 {}
}
}
1 change: 1 addition & 0 deletions redox-core/src/compiling/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod compiler;
pub mod decompiler;
pub mod executable;
6 changes: 2 additions & 4 deletions redox-terminal/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];
Expand Down

0 comments on commit 0b0f407

Please sign in to comment.