@@ -2010,7 +2010,10 @@ impl Compiler {
20102010 NameOp :: Global => {
20112011 let idx = self . get_global_name_index ( & name) ;
20122012 let op = match usage {
2013- NameUsage :: Load => Instruction :: LoadGlobal ,
2013+ NameUsage :: Load => {
2014+ self . emit_load_global ( idx, false ) ;
2015+ return Ok ( ( ) ) ;
2016+ }
20142017 NameUsage :: Store => Instruction :: StoreGlobal ,
20152018 NameUsage :: Delete => Instruction :: DeleteGlobal ,
20162019 } ;
@@ -2973,24 +2976,14 @@ impl Compiler {
29732976 emit ! ( self , Instruction :: PopExcept ) ;
29742977
29752978 // RERAISE 0: re-raise the original exception to outer handler
2976- emit ! (
2977- self ,
2978- Instruction :: RaiseVarargs {
2979- kind: bytecode:: RaiseKind :: ReraiseFromStack
2980- }
2981- ) ;
2979+ emit ! ( self , Instruction :: Reraise { depth: 0 } ) ;
29822980 }
29832981
29842982 if let Some ( cleanup) = finally_cleanup_block {
29852983 self . switch_to_block ( cleanup) ;
29862984 emit ! ( self , Instruction :: Copy { index: 3_u32 } ) ;
29872985 emit ! ( self , Instruction :: PopExcept ) ;
2988- emit ! (
2989- self ,
2990- Instruction :: RaiseVarargs {
2991- kind: bytecode:: RaiseKind :: ReraiseFromStack
2992- }
2993- ) ;
2986+ emit ! ( self , Instruction :: Reraise { depth: 1 } ) ;
29942987 }
29952988
29962989 self . switch_to_block ( end_block) ;
@@ -3128,12 +3121,7 @@ impl Compiler {
31283121 // Stack at entry: [prev_exc (at handler_depth), lasti, exc]
31293122 // This RERAISE is within ExceptionHandler scope, so it routes to cleanup_block
31303123 // which does COPY 3; POP_EXCEPT; RERAISE
3131- emit ! (
3132- self ,
3133- Instruction :: RaiseVarargs {
3134- kind: bytecode:: RaiseKind :: ReraiseFromStack ,
3135- }
3136- ) ;
3124+ emit ! ( self , Instruction :: Reraise { depth: 1 } ) ;
31373125 }
31383126
31393127 // Switch to normal exit block - this is where handler body success continues
@@ -3181,12 +3169,7 @@ impl Compiler {
31813169 // RERAISE 0
31823170 // Stack: [prev_exc, exc] - exception is on stack from PUSH_EXC_INFO
31833171 // NOTE: We emit RERAISE 0 BEFORE popping fblock so it is within cleanup handler scope
3184- emit ! (
3185- self ,
3186- Instruction :: RaiseVarargs {
3187- kind: bytecode:: RaiseKind :: ReraiseFromStack ,
3188- }
3189- ) ;
3172+ emit ! ( self , Instruction :: Reraise { depth: 0 } ) ;
31903173
31913174 // Pop EXCEPTION_HANDLER fblock
31923175 // Pop after RERAISE so the instruction has the correct exception handler
@@ -3200,12 +3183,7 @@ impl Compiler {
32003183 self . switch_to_block ( cleanup_block) ;
32013184 emit ! ( self , Instruction :: Copy { index: 3_u32 } ) ;
32023185 emit ! ( self , Instruction :: PopExcept ) ;
3203- emit ! (
3204- self ,
3205- Instruction :: RaiseVarargs {
3206- kind: bytecode:: RaiseKind :: ReraiseFromStack ,
3207- }
3208- ) ;
3186+ emit ! ( self , Instruction :: Reraise { depth: 1 } ) ;
32093187
32103188 // We successfully ran the try block:
32113189 // else:
@@ -3277,12 +3255,7 @@ impl Compiler {
32773255
32783256 // RERAISE 0: re-raise the original exception to outer handler
32793257 // Stack: [lasti, prev_exc, exc] - exception is on top
3280- emit ! (
3281- self ,
3282- Instruction :: RaiseVarargs {
3283- kind: bytecode:: RaiseKind :: ReraiseFromStack ,
3284- }
3285- ) ;
3258+ emit ! ( self , Instruction :: Reraise { depth: 0 } ) ;
32863259 }
32873260
32883261 // finally cleanup block
@@ -3295,12 +3268,7 @@ impl Compiler {
32953268 // POP_EXCEPT: restore prev_exc as current exception
32963269 emit ! ( self , Instruction :: PopExcept ) ;
32973270 // RERAISE 1: reraise with lasti from stack
3298- emit ! (
3299- self ,
3300- Instruction :: RaiseVarargs {
3301- kind: bytecode:: RaiseKind :: ReraiseFromStack ,
3302- }
3303- ) ;
3271+ emit ! ( self , Instruction :: Reraise { depth: 1 } ) ;
33043272 }
33053273
33063274 // End block - continuation point after try-finally
@@ -3673,23 +3641,13 @@ impl Compiler {
36733641
36743642 emit ! ( self , Instruction :: Copy { index: 2_u32 } ) ;
36753643 emit ! ( self , Instruction :: PopExcept ) ;
3676- emit ! (
3677- self ,
3678- Instruction :: RaiseVarargs {
3679- kind: bytecode:: RaiseKind :: ReraiseFromStack
3680- }
3681- ) ;
3644+ emit ! ( self , Instruction :: Reraise { depth: 0 } ) ;
36823645
36833646 if let Some ( cleanup) = finally_cleanup_block {
36843647 self . switch_to_block ( cleanup) ;
36853648 emit ! ( self , Instruction :: Copy { index: 3_u32 } ) ;
36863649 emit ! ( self , Instruction :: PopExcept ) ;
3687- emit ! (
3688- self ,
3689- Instruction :: RaiseVarargs {
3690- kind: bytecode:: RaiseKind :: ReraiseFromStack
3691- }
3692- ) ;
3650+ emit ! ( self , Instruction :: Reraise { depth: 1 } ) ;
36933651 }
36943652 }
36953653
@@ -4036,7 +3994,7 @@ impl Compiler {
40363994 emit ! ( self , Instruction :: LoadDeref ( idx) ) ;
40373995 } else {
40383996 let cond_annotations_name = self . name ( "__conditional_annotations__" ) ;
4039- emit ! ( self , Instruction :: LoadGlobal ( cond_annotations_name) ) ;
3997+ self . emit_load_global ( cond_annotations_name, false ) ;
40403998 }
40413999 // CONTAINS_OP (in)
40424000 emit ! ( self , Instruction :: ContainsOp ( bytecode:: Invert :: No ) ) ;
@@ -4528,7 +4486,7 @@ impl Compiler {
45284486
45294487 // Load (global) __name__ and store as __module__
45304488 let dunder_name = self . name ( "__name__" ) ;
4531- emit ! ( self , Instruction :: LoadGlobal ( dunder_name) ) ;
4489+ self . emit_load_global ( dunder_name, false ) ;
45324490 let dunder_module = self . name ( "__module__" ) ;
45334491 emit ! ( self , Instruction :: StoreName ( dunder_module) ) ;
45344492
@@ -7994,12 +7952,7 @@ impl Compiler {
79947952 emit ! ( self , Instruction :: StoreFast ( idx) ) ;
79957953 }
79967954 // Re-raise the exception
7997- emit ! (
7998- self ,
7999- Instruction :: RaiseVarargs {
8000- kind: bytecode:: RaiseKind :: ReraiseFromStack
8001- }
8002- ) ;
7955+ emit ! ( self , Instruction :: Reraise { depth: 0 } ) ;
80037956
80047957 // Normal end path
80057958 self . switch_to_block ( end_block) ;
@@ -8118,6 +8071,13 @@ impl Compiler {
81188071 self . emit_arg ( encoded, |arg| Instruction :: LoadAttr { idx : arg } )
81198072 }
81208073
8074+ /// Emit LOAD_GLOBAL.
8075+ /// Encodes: (name_idx << 1) | push_null_bit
8076+ fn emit_load_global ( & mut self , name_idx : u32 , push_null : bool ) {
8077+ let encoded = ( name_idx << 1 ) | u32:: from ( push_null) ;
8078+ self . emit_arg ( encoded, Instruction :: LoadGlobal ) ;
8079+ }
8080+
81218081 /// Emit LOAD_SUPER_ATTR for 2-arg super().attr access.
81228082 /// Encodes: (name_idx << 2) | 0b10 (method=0, class=1)
81238083 fn emit_load_super_attr ( & mut self , name_idx : u32 ) {
0 commit comments