Skip to content

Commit ec25472

Browse files
committed
Adjust CodeFlag
1 parent 376d5ba commit ec25472

File tree

5 files changed

+33
-50
lines changed

5 files changed

+33
-50
lines changed

crates/codegen/src/compile.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ enum CollectionType {
377377
impl Compiler {
378378
fn new(opts: CompileOpts, source_file: SourceFile, code_name: String) -> Self {
379379
let module_code = ir::CodeInfo {
380-
flags: bytecode::CodeFlags::NEW_LOCALS,
380+
flags: bytecode::CodeFlags::NEWLOCALS,
381381
source_path: source_file.name().to_owned(),
382382
private: None,
383383
blocks: vec![ir::Block::default()],
@@ -749,19 +749,19 @@ impl Compiler {
749749
CompilerScope::Module => (bytecode::CodeFlags::empty(), 0, 0, 0),
750750
CompilerScope::Class => (bytecode::CodeFlags::empty(), 0, 0, 0),
751751
CompilerScope::Function | CompilerScope::AsyncFunction | CompilerScope::Lambda => (
752-
bytecode::CodeFlags::NEW_LOCALS | bytecode::CodeFlags::IS_OPTIMIZED,
752+
bytecode::CodeFlags::NEWLOCALS | bytecode::CodeFlags::OPTIMIZED,
753753
0, // Will be set later in enter_function
754754
0, // Will be set later in enter_function
755755
0, // Will be set later in enter_function
756756
),
757757
CompilerScope::Comprehension => (
758-
bytecode::CodeFlags::NEW_LOCALS | bytecode::CodeFlags::IS_OPTIMIZED,
758+
bytecode::CodeFlags::NEWLOCALS | bytecode::CodeFlags::OPTIMIZED,
759759
0,
760760
1, // comprehensions take one argument (.0)
761761
0,
762762
),
763763
CompilerScope::TypeParams => (
764-
bytecode::CodeFlags::NEW_LOCALS | bytecode::CodeFlags::IS_OPTIMIZED,
764+
bytecode::CodeFlags::NEWLOCALS | bytecode::CodeFlags::OPTIMIZED,
765765
0,
766766
0,
767767
0,
@@ -1298,7 +1298,7 @@ impl Compiler {
12981298
let parent_obj_name = &parent.metadata.name;
12991299

13001300
// Determine if parent is a function-like scope
1301-
let is_function_parent = parent.flags.contains(bytecode::CodeFlags::IS_OPTIMIZED)
1301+
let is_function_parent = parent.flags.contains(bytecode::CodeFlags::OPTIMIZED)
13021302
&& !parent_obj_name.starts_with("<") // Not a special scope like <lambda>, <listcomp>, etc.
13031303
&& parent_obj_name != "<module>"; // Not the module scope
13041304

@@ -1914,7 +1914,7 @@ impl Compiler {
19141914
&& self
19151915
.current_code_info()
19161916
.flags
1917-
.contains(bytecode::CodeFlags::IS_GENERATOR)
1917+
.contains(bytecode::CodeFlags::GENERATOR)
19181918
{
19191919
return Err(self.error_ranged(
19201920
CodegenErrorType::AsyncReturnValue,
@@ -2062,7 +2062,7 @@ impl Compiler {
20622062
}
20632063

20642064
self.push_output(
2065-
bytecode::CodeFlags::NEW_LOCALS | bytecode::CodeFlags::IS_OPTIMIZED,
2065+
bytecode::CodeFlags::NEWLOCALS | bytecode::CodeFlags::OPTIMIZED,
20662066
parameters.posonlyargs.len().to_u32(),
20672067
(parameters.posonlyargs.len() + parameters.args.len()).to_u32(),
20682068
parameters.kwonlyargs.len().to_u32(),
@@ -2080,11 +2080,11 @@ impl Compiler {
20802080
}
20812081

20822082
if let Some(name) = parameters.vararg.as_deref() {
2083-
self.current_code_info().flags |= bytecode::CodeFlags::HAS_VARARGS;
2083+
self.current_code_info().flags |= bytecode::CodeFlags::VARARGS;
20842084
self.varname(name.name.as_str())?;
20852085
}
20862086
if let Some(name) = parameters.kwarg.as_deref() {
2087-
self.current_code_info().flags |= bytecode::CodeFlags::HAS_VARKEYWORDS;
2087+
self.current_code_info().flags |= bytecode::CodeFlags::VARKEYWORDS;
20882088
self.varname(name.name.as_str())?;
20892089
}
20902090

@@ -3103,7 +3103,7 @@ impl Compiler {
31033103
self.enter_function(name, parameters)?;
31043104
self.current_code_info()
31053105
.flags
3106-
.set(bytecode::CodeFlags::IS_COROUTINE, is_async);
3106+
.set(bytecode::CodeFlags::COROUTINE, is_async);
31073107

31083108
// Set up context
31093109
let prev_ctx = self.ctx;
@@ -3233,7 +3233,7 @@ impl Compiler {
32333233
// Enter type params scope
32343234
let type_params_name = format!("<generic parameters of {name}>");
32353235
self.push_output(
3236-
bytecode::CodeFlags::IS_OPTIMIZED | bytecode::CodeFlags::NEW_LOCALS,
3236+
bytecode::CodeFlags::OPTIMIZED | bytecode::CodeFlags::NEWLOCALS,
32373237
0,
32383238
num_typeparam_args as u32,
32393239
0,
@@ -3664,7 +3664,7 @@ impl Compiler {
36643664
if is_generic {
36653665
let type_params_name = format!("<generic parameters of {name}>");
36663666
self.push_output(
3667-
bytecode::CodeFlags::IS_OPTIMIZED | bytecode::CodeFlags::NEW_LOCALS,
3667+
bytecode::CodeFlags::OPTIMIZED | bytecode::CodeFlags::NEWLOCALS,
36683668
0,
36693669
0,
36703670
0,
@@ -6361,9 +6361,9 @@ impl Compiler {
63616361
in_async_scope: prev_ctx.in_async_scope || is_async,
63626362
};
63636363

6364-
let flags = bytecode::CodeFlags::NEW_LOCALS | bytecode::CodeFlags::IS_OPTIMIZED;
6364+
let flags = bytecode::CodeFlags::NEWLOCALS | bytecode::CodeFlags::OPTIMIZED;
63656365
let flags = if is_async {
6366-
flags | bytecode::CodeFlags::IS_COROUTINE
6366+
flags | bytecode::CodeFlags::COROUTINE
63676367
} else {
63686368
flags
63696369
};
@@ -7030,7 +7030,7 @@ impl Compiler {
70307030
}
70317031

70327032
fn mark_generator(&mut self) {
7033-
self.current_code_info().flags |= bytecode::CodeFlags::IS_GENERATOR
7033+
self.current_code_info().flags |= bytecode::CodeFlags::GENERATOR
70347034
}
70357035

70367036
/// Whether the expression contains an await expression and

crates/codegen/src/ir.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,8 @@ impl CodeInfo {
283283

284284
let total_args = self.metadata.argcount
285285
+ self.metadata.kwonlyargcount
286-
+ self.flags.contains(CodeFlags::HAS_VARARGS) as u32
287-
+ self.flags.contains(CodeFlags::HAS_VARKEYWORDS) as u32;
286+
+ self.flags.contains(CodeFlags::VARARGS) as u32
287+
+ self.flags.contains(CodeFlags::VARKEYWORDS) as u32;
288288

289289
let mut found_cellarg = false;
290290
let cell2arg = self

crates/compiler-core/src/bytecode.rs

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -357,28 +357,15 @@ pub struct CodeObject<C: Constant = ConstantData> {
357357
bitflags! {
358358
#[derive(Copy, Clone, Debug, PartialEq)]
359359
pub struct CodeFlags: u16 {
360-
const NEW_LOCALS = 0x01;
361-
const IS_GENERATOR = 0x02;
362-
const IS_COROUTINE = 0x04;
363-
const HAS_VARARGS = 0x08;
364-
const HAS_VARKEYWORDS = 0x10;
365-
const IS_OPTIMIZED = 0x20;
360+
const OPTIMIZED = 0x0001;
361+
const NEWLOCALS = 0x0002;
362+
const VARARGS = 0x0004;
363+
const VARKEYWORDS = 0x0008;
364+
const GENERATOR = 0x0020;
365+
const COROUTINE = 0x0080;
366366
}
367367
}
368368

369-
impl CodeFlags {
370-
pub const NAME_MAPPING: &'static [(&'static str, Self)] = &[
371-
("GENERATOR", Self::IS_GENERATOR),
372-
("COROUTINE", Self::IS_COROUTINE),
373-
(
374-
"ASYNC_GENERATOR",
375-
Self::from_bits_truncate(Self::IS_GENERATOR.bits() | Self::IS_COROUTINE.bits()),
376-
),
377-
("VARARGS", Self::HAS_VARARGS),
378-
("VARKEYWORDS", Self::HAS_VARKEYWORDS),
379-
];
380-
}
381-
382369
/// an opcode argument that may be extended by a prior ExtendedArg
383370
#[derive(Copy, Clone, PartialEq, Eq)]
384371
#[repr(transparent)]
@@ -1550,14 +1537,14 @@ impl<C: Constant> CodeObject<C> {
15501537
let args = &self.varnames[..nargs];
15511538
let kwonlyargs = &self.varnames[nargs..varargs_pos];
15521539

1553-
let vararg = if self.flags.contains(CodeFlags::HAS_VARARGS) {
1540+
let vararg = if self.flags.contains(CodeFlags::VARARGS) {
15541541
let vararg = &self.varnames[varargs_pos];
15551542
varargs_pos += 1;
15561543
Some(vararg)
15571544
} else {
15581545
None
15591546
};
1560-
let varkwarg = if self.flags.contains(CodeFlags::HAS_VARKEYWORDS) {
1547+
let varkwarg = if self.flags.contains(CodeFlags::VARKEYWORDS) {
15611548
Some(&self.varnames[varargs_pos])
15621549
} else {
15631550
None

crates/vm/src/builtins/function.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ impl PyFunction {
124124

125125
let mut vararg_offset = total_args;
126126
// Pack other positional arguments in to *args:
127-
if code.flags.contains(bytecode::CodeFlags::HAS_VARARGS) {
127+
if code.flags.contains(bytecode::CodeFlags::VARARGS) {
128128
let vararg_value = vm.ctx.new_tuple(args_iter.collect());
129129
fastlocals[vararg_offset] = Some(vararg_value.into());
130130
vararg_offset += 1;
@@ -155,7 +155,7 @@ impl PyFunction {
155155
}
156156

157157
// Do we support `**kwargs` ?
158-
let kwargs = if code.flags.contains(bytecode::CodeFlags::HAS_VARKEYWORDS) {
158+
let kwargs = if code.flags.contains(bytecode::CodeFlags::VARKEYWORDS) {
159159
let d = vm.ctx.new_dict();
160160
fastlocals[vararg_offset] = Some(d.clone().into());
161161
Some(d)
@@ -414,7 +414,7 @@ impl Py<PyFunction> {
414414

415415
let code = self.code.lock().clone();
416416

417-
let locals = if code.flags.contains(bytecode::CodeFlags::NEW_LOCALS) {
417+
let locals = if code.flags.contains(bytecode::CodeFlags::NEWLOCALS) {
418418
ArgMapping::from_dict_exact(vm.ctx.new_dict())
419419
} else if let Some(locals) = locals {
420420
locals
@@ -436,8 +436,8 @@ impl Py<PyFunction> {
436436
self.fill_locals_from_args(&frame, func_args, vm)?;
437437

438438
// If we have a generator, create a new generator
439-
let is_gen = code.flags.contains(bytecode::CodeFlags::IS_GENERATOR);
440-
let is_coro = code.flags.contains(bytecode::CodeFlags::IS_COROUTINE);
439+
let is_gen = code.flags.contains(bytecode::CodeFlags::GENERATOR);
440+
let is_coro = code.flags.contains(bytecode::CodeFlags::COROUTINE);
441441
match (is_gen, is_coro) {
442442
(true, false) => {
443443
Ok(PyGenerator::new(frame, self.__name__(), self.__qualname__()).into_pyobject(vm))

crates/vm/src/frame.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ impl Frame {
186186
Ok(())
187187
};
188188
map_to_dict(&code.cellvars, &self.cells_frees)?;
189-
if code.flags.contains(bytecode::CodeFlags::IS_OPTIMIZED) {
189+
if code.flags.contains(bytecode::CodeFlags::OPTIMIZED) {
190190
map_to_dict(&code.freevars, &self.cells_frees[code.cellvars.len()..])?;
191191
}
192192
}
@@ -1000,11 +1000,7 @@ impl ExecutingFrame<'_> {
10001000
let iterable = self.pop_value();
10011001
let iter = if iterable.class().is(vm.ctx.types.coroutine_type) {
10021002
// Coroutine requires CO_COROUTINE or CO_ITERABLE_COROUTINE flag
1003-
if !self
1004-
.code
1005-
.flags
1006-
.intersects(bytecode::CodeFlags::IS_COROUTINE)
1007-
{
1003+
if !self.code.flags.intersects(bytecode::CodeFlags::COROUTINE) {
10081004
return Err(vm.new_type_error(
10091005
"cannot 'yield from' a coroutine object in a non-coroutine generator"
10101006
.to_owned(),
@@ -1662,7 +1658,7 @@ impl ExecutingFrame<'_> {
16621658
// arg=0: direct yield (wrapped for async generators)
16631659
// arg=1: yield from await/yield-from (NOT wrapped)
16641660
let wrap = oparg.get(arg) == 0;
1665-
let value = if wrap && self.code.flags.contains(bytecode::CodeFlags::IS_COROUTINE) {
1661+
let value = if wrap && self.code.flags.contains(bytecode::CodeFlags::COROUTINE) {
16661662
PyAsyncGenWrappedValue(value).into_pyobject(vm)
16671663
} else {
16681664
value

0 commit comments

Comments
 (0)