Skip to content

Commit 440b8de

Browse files
authored
Move Instruction enum to its own file (#6693)
* Move `Instruction` to its own file * Fix codegen and frame.rs * Adjust script
1 parent 3909b18 commit 440b8de

File tree

5 files changed

+862
-828
lines changed

5 files changed

+862
-828
lines changed

crates/codegen/src/compile.rs

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1382,7 +1382,7 @@ impl Compiler {
13821382

13831383
if let Stmt::Expr(StmtExpr { value, .. }) = &last {
13841384
self.compile_expression(value)?;
1385-
emit!(self, Instruction::CopyItem { index: 1_u32 });
1385+
emit!(self, Instruction::Copy { index: 1_u32 });
13861386
emit!(
13871387
self,
13881388
Instruction::CallIntrinsic1 {
@@ -1420,7 +1420,7 @@ impl Compiler {
14201420
Stmt::FunctionDef(_) | Stmt::ClassDef(_) => {
14211421
let pop_instructions = self.current_block().instructions.pop();
14221422
let store_inst = compiler_unwrap_option(self, pop_instructions); // pop Instruction::Store
1423-
emit!(self, Instruction::CopyItem { index: 1_u32 });
1423+
emit!(self, Instruction::Copy { index: 1_u32 });
14241424
self.current_block().instructions.push(store_inst);
14251425
}
14261426
_ => self.emit_load_const(ConstantData::None),
@@ -1938,7 +1938,7 @@ impl Compiler {
19381938

19391939
for (i, target) in targets.iter().enumerate() {
19401940
if i + 1 != targets.len() {
1941-
emit!(self, Instruction::CopyItem { index: 1_u32 });
1941+
emit!(self, Instruction::Copy { index: 1_u32 });
19421942
}
19431943
self.compile_store(target)?;
19441944
}
@@ -2205,7 +2205,7 @@ impl Compiler {
22052205
);
22062206
}
22072207

2208-
emit!(self, Instruction::CopyItem { index: 1_u32 });
2208+
emit!(self, Instruction::Copy { index: 1_u32 });
22092209
self.store_name(name.as_ref())?;
22102210
}
22112211
TypeParam::ParamSpec(TypeParamParamSpec { name, default, .. }) => {
@@ -2230,7 +2230,7 @@ impl Compiler {
22302230
);
22312231
}
22322232

2233-
emit!(self, Instruction::CopyItem { index: 1_u32 });
2233+
emit!(self, Instruction::Copy { index: 1_u32 });
22342234
self.store_name(name.as_ref())?;
22352235
}
22362236
TypeParam::TypeVarTuple(TypeParamTypeVarTuple { name, default, .. }) => {
@@ -2256,7 +2256,7 @@ impl Compiler {
22562256
);
22572257
}
22582258

2259-
emit!(self, Instruction::CopyItem { index: 1_u32 });
2259+
emit!(self, Instruction::Copy { index: 1_u32 });
22602260
self.store_name(name.as_ref())?;
22612261
}
22622262
};
@@ -2396,7 +2396,7 @@ impl Compiler {
23962396

23972397
if let Some(cleanup) = finally_cleanup_block {
23982398
self.switch_to_block(cleanup);
2399-
emit!(self, Instruction::CopyItem { index: 3_u32 });
2399+
emit!(self, Instruction::Copy { index: 3_u32 });
24002400
emit!(self, Instruction::PopExcept);
24012401
emit!(
24022402
self,
@@ -2457,7 +2457,7 @@ impl Compiler {
24572457
// check if this handler can handle the exception:
24582458
if let Some(exc_type) = type_ {
24592459
// Duplicate exception for test:
2460-
emit!(self, Instruction::CopyItem { index: 1_u32 });
2460+
emit!(self, Instruction::Copy { index: 1_u32 });
24612461

24622462
// Check exception type:
24632463
self.compile_expression(exc_type)?;
@@ -2598,7 +2598,7 @@ impl Compiler {
25982598
// POP_EXCEPT: pop prev_exc from stack and restore -> [prev_exc, lasti, exc]
25992599
// RERAISE 1: reraise with lasti
26002600
self.switch_to_block(cleanup_block);
2601-
emit!(self, Instruction::CopyItem { index: 3_u32 });
2601+
emit!(self, Instruction::Copy { index: 3_u32 });
26022602
emit!(self, Instruction::PopExcept);
26032603
emit!(
26042604
self,
@@ -2693,7 +2693,7 @@ impl Compiler {
26932693
if let Some(cleanup) = finally_cleanup_block {
26942694
self.switch_to_block(cleanup);
26952695
// COPY 3: copy the exception from position 3
2696-
emit!(self, Instruction::CopyItem { index: 3_u32 });
2696+
emit!(self, Instruction::Copy { index: 3_u32 });
26972697
// POP_EXCEPT: restore prev_exc as current exception
26982698
emit!(self, Instruction::PopExcept);
26992699
// RERAISE 1: reraise with lasti from stack
@@ -2787,7 +2787,7 @@ impl Compiler {
27872787
emit!(self, Instruction::BuildList { size: 0 });
27882788
// Stack: [prev_exc, exc, []]
27892789
// ADDOP_I(c, loc, COPY, 2);
2790-
emit!(self, Instruction::CopyItem { index: 2 });
2790+
emit!(self, Instruction::Copy { index: 2 });
27912791
// Stack: [prev_exc, exc, [], exc_copy]
27922792
// Now stack is: [prev_exc, orig, list, rest]
27932793
}
@@ -2817,7 +2817,7 @@ impl Compiler {
28172817

28182818
// ADDOP_I(c, loc, COPY, 1);
28192819
// ADDOP_JUMP(c, loc, POP_JUMP_IF_NONE, no_match);
2820-
emit!(self, Instruction::CopyItem { index: 1 });
2820+
emit!(self, Instruction::Copy { index: 1 });
28212821
self.emit_load_const(ConstantData::None);
28222822
emit!(self, Instruction::IsOp(bytecode::Invert::No)); // is None?
28232823
emit!(
@@ -2947,7 +2947,7 @@ impl Compiler {
29472947
// Stack: [prev_exc, result]
29482948

29492949
// COPY 1
2950-
emit!(self, Instruction::CopyItem { index: 1 });
2950+
emit!(self, Instruction::Copy { index: 1 });
29512951
// Stack: [prev_exc, result, result]
29522952

29532953
// POP_JUMP_IF_NOT_NONE reraise
@@ -3149,7 +3149,7 @@ impl Compiler {
31493149

31503150
// Handle docstring if present
31513151
if let Some(doc) = doc_str {
3152-
emit!(self, Instruction::CopyItem { index: 1_u32 });
3152+
emit!(self, Instruction::Copy { index: 1_u32 });
31533153
self.emit_load_const(ConstantData::Str {
31543154
value: doc.to_string().into(),
31553155
});
@@ -3633,7 +3633,7 @@ impl Compiler {
36333633

36343634
if let Some(classcell_idx) = classcell_idx {
36353635
emit!(self, Instruction::LoadClosure(classcell_idx.to_u32()));
3636-
emit!(self, Instruction::CopyItem { index: 1_u32 });
3636+
emit!(self, Instruction::Copy { index: 1_u32 });
36373637
let classcell = self.name("__classcell__");
36383638
emit!(self, Instruction::StoreName(classcell));
36393639
} else {
@@ -4081,7 +4081,7 @@ impl Compiler {
40814081
// to be in the exception table for these instructions.
40824082
// If we cleared fblock, exceptions here would propagate uncaught.
40834083
self.switch_to_block(cleanup_block);
4084-
emit!(self, Instruction::CopyItem { index: 3 });
4084+
emit!(self, Instruction::Copy { index: 3 });
40854085
emit!(self, Instruction::PopExcept);
40864086
emit!(self, Instruction::Reraise { depth: 1 });
40874087

@@ -4384,7 +4384,7 @@ impl Compiler {
43844384
continue;
43854385
}
43864386
// Duplicate the subject.
4387-
emit!(self, Instruction::CopyItem { index: 1_u32 });
4387+
emit!(self, Instruction::Copy { index: 1_u32 });
43884388
if i < star {
43894389
// For indices before the star, use a nonnegative index equal to i.
43904390
self.emit_load_const(ConstantData::Integer { value: i.into() });
@@ -4457,7 +4457,7 @@ impl Compiler {
44574457

44584458
// Otherwise, there is a sub-pattern. Duplicate the object on top of the stack.
44594459
pc.on_top += 1;
4460-
emit!(self, Instruction::CopyItem { index: 1_u32 });
4460+
emit!(self, Instruction::Copy { index: 1_u32 });
44614461
// Compile the sub-pattern.
44624462
self.compile_pattern(p.pattern.as_ref().unwrap(), pc)?;
44634463
// After success, decrement the on_top counter.
@@ -4558,7 +4558,7 @@ impl Compiler {
45584558
// 2. Emit MATCH_CLASS with nargs.
45594559
emit!(self, Instruction::MatchClass(u32::try_from(nargs).unwrap()));
45604560
// 3. Duplicate the top of the stack.
4561-
emit!(self, Instruction::CopyItem { index: 1_u32 });
4561+
emit!(self, Instruction::Copy { index: 1_u32 });
45624562
// 4. Load None.
45634563
self.emit_load_const(ConstantData::None);
45644564
// 5. Compare with IS_OP 1.
@@ -4723,7 +4723,7 @@ impl Compiler {
47234723
pc.on_top += 2; // subject and keys_tuple are underneath
47244724

47254725
// Check if match succeeded
4726-
emit!(self, Instruction::CopyItem { index: 1_u32 });
4726+
emit!(self, Instruction::Copy { index: 1_u32 });
47274727
// Stack: [subject, keys_tuple, values_tuple, values_tuple_copy]
47284728

47294729
// Check if copy is None (consumes the copy like POP_JUMP_IF_NONE)
@@ -4776,7 +4776,7 @@ impl Compiler {
47764776
// Copy rest_dict which is at position (1 + remaining) from TOS
47774777
emit!(
47784778
self,
4779-
Instruction::CopyItem {
4779+
Instruction::Copy {
47804780
index: 1 + remaining
47814781
}
47824782
);
@@ -4833,7 +4833,7 @@ impl Compiler {
48334833
pc.fail_pop.clear();
48344834
pc.on_top = 0;
48354835
// Emit a COPY(1) instruction before compiling the alternative.
4836-
emit!(self, Instruction::CopyItem { index: 1_u32 });
4836+
emit!(self, Instruction::Copy { index: 1_u32 });
48374837
self.compile_pattern(alt, pc)?;
48384838

48394839
let n_stores = pc.stores.len();
@@ -5080,7 +5080,7 @@ impl Compiler {
50805080
for (i, m) in cases.iter().enumerate().take(case_count) {
50815081
// Only copy the subject if not on the last case
50825082
if i != case_count - 1 {
5083-
emit!(self, Instruction::CopyItem { index: 1_u32 });
5083+
emit!(self, Instruction::Copy { index: 1_u32 });
50845084
}
50855085

50865086
pattern_context.stores = Vec::with_capacity(1);
@@ -5127,7 +5127,7 @@ impl Compiler {
51275127
if let Some(ref guard) = m.guard {
51285128
// Compile guard and jump to end if false
51295129
self.compile_expression(guard)?;
5130-
emit!(self, Instruction::CopyItem { index: 1_u32 });
5130+
emit!(self, Instruction::Copy { index: 1_u32 });
51315131
emit!(self, Instruction::PopJumpIfFalse { target: end });
51325132
emit!(self, Instruction::PopTop);
51335133
}
@@ -5207,13 +5207,13 @@ impl Compiler {
52075207

52085208
// store rhs for the next comparison in chain
52095209
emit!(self, Instruction::Swap { index: 2 });
5210-
emit!(self, Instruction::CopyItem { index: 2 });
5210+
emit!(self, Instruction::Copy { index: 2 });
52115211

52125212
self.compile_addcompare(op);
52135213

52145214
// if comparison result is false, we break with this value; if true, try the next one.
52155215
/*
5216-
emit!(self, Instruction::CopyItem { index: 1 });
5216+
emit!(self, Instruction::Copy { index: 1 });
52175217
// emit!(self, Instruction::ToBool); // TODO: Uncomment this
52185218
emit!(self, Instruction::PopJumpIfFalse { target: cleanup });
52195219
emit!(self, Instruction::PopTop);
@@ -5398,16 +5398,16 @@ impl Compiler {
53985398
// But we can't use compile_subscript directly because we need DUP_TOP2
53995399
self.compile_expression(value)?;
54005400
self.compile_expression(slice)?;
5401-
emit!(self, Instruction::CopyItem { index: 2_u32 });
5402-
emit!(self, Instruction::CopyItem { index: 2_u32 });
5401+
emit!(self, Instruction::Copy { index: 2_u32 });
5402+
emit!(self, Instruction::Copy { index: 2_u32 });
54035403
emit!(self, Instruction::Subscript);
54045404
AugAssignKind::Subscript
54055405
}
54065406
Expr::Attribute(ExprAttribute { value, attr, .. }) => {
54075407
let attr = attr.as_str();
54085408
self.check_forbidden_name(attr, NameUsage::Store)?;
54095409
self.compile_expression(value)?;
5410-
emit!(self, Instruction::CopyItem { index: 1_u32 });
5410+
emit!(self, Instruction::Copy { index: 1_u32 });
54115411
let idx = self.name(attr);
54125412
emit!(self, Instruction::LoadAttr { idx });
54135413
AugAssignKind::Attr { idx }
@@ -5564,7 +5564,7 @@ impl Compiler {
55645564
for value in values {
55655565
self.compile_expression(value)?;
55665566

5567-
emit!(self, Instruction::CopyItem { index: 1_u32 });
5567+
emit!(self, Instruction::Copy { index: 1_u32 });
55685568
match op {
55695569
BoolOp::And => {
55705570
emit!(
@@ -6062,7 +6062,7 @@ impl Compiler {
60626062
range: _,
60636063
}) => {
60646064
self.compile_expression(value)?;
6065-
emit!(self, Instruction::CopyItem { index: 1_u32 });
6065+
emit!(self, Instruction::Copy { index: 1_u32 });
60666066
self.compile_store(target)?;
60676067
}
60686068
Expr::FString(fstring) => {

0 commit comments

Comments
 (0)