Skip to content

Commit 75dcf80

Browse files
Tidy codegen::ir::BlockIdx api (#6413)
* Tidy `codegen::ir::BlockIdx` api * Update crates/codegen/src/ir.rs Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --------- Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
1 parent 5a5b721 commit 75dcf80

File tree

2 files changed

+37
-24
lines changed

2 files changed

+37
-24
lines changed

crates/codegen/src/compile.rs

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ pub struct CompileOpts {
122122

123123
#[derive(Debug, Clone, Copy)]
124124
struct CompileContext {
125-
loop_data: Option<(ir::BlockIdx, ir::BlockIdx)>,
125+
loop_data: Option<(BlockIdx, BlockIdx)>,
126126
in_class: bool,
127127
func: FunctionContext,
128128
}
@@ -356,7 +356,7 @@ impl Compiler {
356356
source_path: source_file.name().to_owned(),
357357
private: None,
358358
blocks: vec![ir::Block::default()],
359-
current_block: ir::BlockIdx(0),
359+
current_block: BlockIdx::new(0),
360360
metadata: ir::CodeUnitMetadata {
361361
name: code_name.clone(),
362362
qualname: Some(code_name),
@@ -745,7 +745,7 @@ impl Compiler {
745745
source_path: source_path.clone(),
746746
private,
747747
blocks: vec![ir::Block::default()],
748-
current_block: BlockIdx(0),
748+
current_block: BlockIdx::new(0),
749749
metadata: ir::CodeUnitMetadata {
750750
name: name.to_owned(),
751751
qualname: None, // Will be set below
@@ -4370,7 +4370,7 @@ impl Compiler {
43704370
&mut self,
43714371
expression: &Expr,
43724372
condition: bool,
4373-
target_block: ir::BlockIdx,
4373+
target_block: BlockIdx,
43744374
) -> CompileResult<()> {
43754375
// Compile expression for test, and jump to label if false
43764376
match &expression {
@@ -5187,7 +5187,7 @@ impl Compiler {
51875187
let return_none = init_collection.is_none();
51885188
// Create empty object of proper type:
51895189
if let Some(init_collection) = init_collection {
5190-
self._emit(init_collection, OpArg(0), ir::BlockIdx::NULL)
5190+
self._emit(init_collection, OpArg(0), BlockIdx::NULL)
51915191
}
51925192

51935193
let mut loop_labels = vec![];
@@ -5328,7 +5328,7 @@ impl Compiler {
53285328
}
53295329

53305330
// Low level helper functions:
5331-
fn _emit(&mut self, instr: Instruction, arg: OpArg, target: ir::BlockIdx) {
5331+
fn _emit(&mut self, instr: Instruction, arg: OpArg, target: BlockIdx) {
53325332
let range = self.current_source_range;
53335333
let location = self
53345334
.source_file
@@ -5345,7 +5345,7 @@ impl Compiler {
53455345
}
53465346

53475347
fn emit_no_arg(&mut self, ins: Instruction) {
5348-
self._emit(ins, OpArg::null(), ir::BlockIdx::NULL)
5348+
self._emit(ins, OpArg::null(), BlockIdx::NULL)
53495349
}
53505350

53515351
fn emit_arg<A: OpArgType, T: EmitArg<A>>(
@@ -5393,25 +5393,25 @@ impl Compiler {
53935393
&mut info.blocks[info.current_block]
53945394
}
53955395

5396-
fn new_block(&mut self) -> ir::BlockIdx {
5396+
fn new_block(&mut self) -> BlockIdx {
53975397
let code = self.current_code_info();
5398-
let idx = ir::BlockIdx(code.blocks.len().to_u32());
5398+
let idx = BlockIdx::new(code.blocks.len().to_u32());
53995399
code.blocks.push(ir::Block::default());
54005400
idx
54015401
}
54025402

5403-
fn switch_to_block(&mut self, block: ir::BlockIdx) {
5403+
fn switch_to_block(&mut self, block: BlockIdx) {
54045404
let code = self.current_code_info();
54055405
let prev = code.current_block;
54065406
assert_ne!(prev, block, "recursive switching {prev:?} -> {block:?}");
54075407
assert_eq!(
54085408
code.blocks[block].next,
5409-
ir::BlockIdx::NULL,
5409+
BlockIdx::NULL,
54105410
"switching {prev:?} -> {block:?} to completed block"
54115411
);
5412-
let prev_block = &mut code.blocks[prev.0 as usize];
5412+
let prev_block = &mut code.blocks[prev.idx()];
54135413
assert_eq!(
5414-
prev_block.next.0,
5414+
u32::from(prev_block.next),
54155415
u32::MAX,
54165416
"switching {prev:?} -> {block:?} from block that's already got a next"
54175417
);
@@ -5708,22 +5708,19 @@ trait EmitArg<Arg: OpArgType> {
57085708
fn emit(
57095709
self,
57105710
f: impl FnOnce(OpArgMarker<Arg>) -> Instruction,
5711-
) -> (Instruction, OpArg, ir::BlockIdx);
5711+
) -> (Instruction, OpArg, BlockIdx);
57125712
}
57135713
impl<T: OpArgType> EmitArg<T> for T {
5714-
fn emit(
5715-
self,
5716-
f: impl FnOnce(OpArgMarker<T>) -> Instruction,
5717-
) -> (Instruction, OpArg, ir::BlockIdx) {
5714+
fn emit(self, f: impl FnOnce(OpArgMarker<T>) -> Instruction) -> (Instruction, OpArg, BlockIdx) {
57185715
let (marker, arg) = OpArgMarker::new(self);
5719-
(f(marker), arg, ir::BlockIdx::NULL)
5716+
(f(marker), arg, BlockIdx::NULL)
57205717
}
57215718
}
5722-
impl EmitArg<bytecode::Label> for ir::BlockIdx {
5719+
impl EmitArg<bytecode::Label> for BlockIdx {
57235720
fn emit(
57245721
self,
57255722
f: impl FnOnce(OpArgMarker<bytecode::Label>) -> Instruction,
5726-
) -> (Instruction, OpArg, ir::BlockIdx) {
5723+
) -> (Instruction, OpArg, BlockIdx) {
57275724
(f(OpArgMarker::marker()), OpArg::null(), self)
57285725
}
57295726
}

crates/codegen/src/ir.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,30 @@ pub struct CodeUnitMetadata {
2929
// use rustpython_parser_core::source_code::{LineNumber, SourceLocation};
3030

3131
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
32-
pub struct BlockIdx(pub u32);
32+
pub struct BlockIdx(u32);
33+
3334
impl BlockIdx {
34-
pub const NULL: Self = Self(u32::MAX);
35-
const fn idx(self) -> usize {
35+
pub const NULL: Self = Self::new(u32::MAX);
36+
37+
/// Creates a new instance of [`BlockIdx`] from a [`u32`].
38+
#[must_use]
39+
pub const fn new(value: u32) -> Self {
40+
Self(value)
41+
}
42+
43+
/// Returns the inner value as a [`usize`].
44+
#[must_use]
45+
pub const fn idx(self) -> usize {
3646
self.0 as usize
3747
}
3848
}
3949

50+
impl From<BlockIdx> for u32 {
51+
fn from(block_idx: BlockIdx) -> Self {
52+
block_idx.0
53+
}
54+
}
55+
4056
impl ops::Index<BlockIdx> for [Block] {
4157
type Output = Block;
4258

0 commit comments

Comments
 (0)