FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Remove oparg builders by ShaharNaveh · Pull Request #7537 · RustPython/RustPython · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .rs  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
34 changes: 6 additions & 28 deletions crates/codegen/src/compile.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -9041,20 +9041,14 @@ impl Compiler {
/// Emit LOAD_ATTR for attribute access (method=false).
/// Encodes: (name_idx << 1) | 0
fn emit_load_attr(&mut self, name_idx: u32) {
let encoded = LoadAttr::builder()
.name_idx(name_idx)
.is_method(false)
.build();
let encoded = LoadAttr::new(name_idx, false);
self.emit_arg(encoded, |namei| Instruction::LoadAttr { namei })
}

/// Emit LOAD_ATTR with method flag set (for method calls).
/// Encodes: (name_idx << 1) | 1
fn emit_load_attr_method(&mut self, name_idx: u32) {
let encoded = LoadAttr::builder()
.name_idx(name_idx)
.is_method(true)
.build();
let encoded = LoadAttr::new(name_idx, true);
self.emit_arg(encoded, |namei| Instruction::LoadAttr { namei })
}

Expand All @@ -9068,44 +9062,28 @@ impl Compiler {
/// Emit LOAD_SUPER_ATTR for 2-arg super().attr access.
/// Encodes: (name_idx << 2) | 0b10 (method=0, class=1)
fn emit_load_super_attr(&mut self, name_idx: u32) {
let encoded = LoadSuperAttr::builder()
.name_idx(name_idx)
.is_load_method(false)
.has_class(true)
.build();
let encoded = LoadSuperAttr::new(name_idx, false, true);
self.emit_arg(encoded, |namei| Instruction::LoadSuperAttr { namei })
}

/// Emit LOAD_SUPER_ATTR for 2-arg super().method() call.
/// Encodes: (name_idx << 2) | 0b11 (method=1, class=1)
fn emit_load_super_method(&mut self, name_idx: u32) {
let encoded = LoadSuperAttr::builder()
.name_idx(name_idx)
.is_load_method(true)
.has_class(true)
.build();
let encoded = LoadSuperAttr::new(name_idx, true, true);
self.emit_arg(encoded, |namei| Instruction::LoadSuperAttr { namei })
}

/// Emit LOAD_SUPER_ATTR for 0-arg super().attr access.
/// Encodes: (name_idx << 2) | 0b00 (method=0, class=0)
fn emit_load_zero_super_attr(&mut self, name_idx: u32) {
let encoded = LoadSuperAttr::builder()
.name_idx(name_idx)
.is_load_method(false)
.has_class(false)
.build();
let encoded = LoadSuperAttr::new(name_idx, false, false);
self.emit_arg(encoded, |namei| Instruction::LoadSuperAttr { namei })
}

/// Emit LOAD_SUPER_ATTR for 0-arg super().method() call.
/// Encodes: (name_idx << 2) | 0b01 (method=1, class=0)
fn emit_load_zero_super_method(&mut self, name_idx: u32) {
let encoded = LoadSuperAttr::builder()
.name_idx(name_idx)
.is_load_method(true)
.has_class(false)
.build();
let encoded = LoadSuperAttr::new(name_idx, true, false);
self.emit_arg(encoded, |namei| Instruction::LoadSuperAttr { namei })
}

Expand Down
74 changes: 4 additions & 70 deletions crates/compiler-core/src/bytecode/oparg.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -918,8 +918,8 @@ impl VarNums {

impl LoadAttr {
#[must_use]
pub fn builder() -> LoadAttrBuilder {
LoadAttrBuilder::default()
pub const fn new(name_idx: u32, is_method: bool) -> Self {
Self::from_u32((name_idx << 1) | (is_method as u32))
}

#[must_use]
Expand All @@ -933,36 +933,10 @@ impl LoadAttr {
}
}

#[derive(Clone, Copy, Default)]
pub struct LoadAttrBuilder {
name_idx: u32,
is_method: bool,
}

impl LoadAttrBuilder {
#[must_use]
pub const fn build(self) -> LoadAttr {
let value = (self.name_idx << 1) | (self.is_method as u32);
LoadAttr::from_u32(value)
}

#[must_use]
pub const fn name_idx(mut self, value: u32) -> Self {
self.name_idx = value;
self
}

#[must_use]
pub const fn is_method(mut self, value: bool) -> Self {
self.is_method = value;
self
}
}

impl LoadSuperAttr {
#[must_use]
pub fn builder() -> LoadSuperAttrBuilder {
LoadSuperAttrBuilder::default()
pub const fn new(name_idx: u32, is_load_method: bool, has_class: bool) -> Self {
Self::from_u32((name_idx << 2) | (is_load_method as u32) | ((has_class as u32) << 1))
}

#[must_use]
Expand All @@ -980,43 +954,3 @@ impl LoadSuperAttr {
(self.0 & 2) == 2
}
}

#[derive(Clone, Copy, Default)]
pub struct LoadSuperAttrBuilder {
name_idx: u32,
is_load_method: bool,
has_class: bool,
}

impl LoadSuperAttrBuilder {
#[must_use]
pub const fn build(self) -> LoadSuperAttr {
let value =
(self.name_idx << 2) | ((self.has_class as u32) << 1) | (self.is_load_method as u32);
LoadSuperAttr::from_u32(value)
}

#[must_use]
pub const fn name_idx(mut self, value: u32) -> Self {
self.name_idx = value;
self
}

#[must_use]
pub const fn is_load_method(mut self, value: bool) -> Self {
self.is_load_method = value;
self
}

#[must_use]
pub const fn has_class(mut self, value: bool) -> Self {
self.has_class = value;
self
}
}

impl From<LoadSuperAttrBuilder> for LoadSuperAttr {
fn from(builder: LoadSuperAttrBuilder) -> Self {
builder.build()
}
}
Loading

Back | FazBrowse Home | New Git URL