@@ -113,6 +113,7 @@ pub struct CodeObject<C: Constant = ConstantData> {
113
113
pub kwonlyarg_count : usize ,
114
114
pub source_path : C :: Name ,
115
115
pub first_line_number : usize ,
116
+ pub max_stacksize : u32 ,
116
117
pub obj_name : C :: Name , // Name of the object that created this code object
117
118
pub cell2arg : Option < Box < [ isize ] > > ,
118
119
pub constants : Box < [ C ] > ,
@@ -246,7 +247,9 @@ pub enum Instruction {
246
247
Continue {
247
248
target : Label ,
248
249
} ,
249
- Break ,
250
+ Break {
251
+ target : Label ,
252
+ } ,
250
253
Jump {
251
254
target : Label ,
252
255
} ,
@@ -285,9 +288,7 @@ pub enum Instruction {
285
288
YieldValue ,
286
289
YieldFrom ,
287
290
SetupAnnotation ,
288
- SetupLoop {
289
- end : Label ,
290
- } ,
291
+ SetupLoop ,
291
292
292
293
/// Setup a finally handler, which will be called whenever one of this events occurs:
293
294
/// - the block is popped
@@ -377,6 +378,7 @@ pub enum Instruction {
377
378
} ,
378
379
GetAIter ,
379
380
GetANext ,
381
+ EndAsyncFor ,
380
382
381
383
/// Reverse order evaluation in MapAdd
382
384
/// required to support named expressions of Python 3.8 in dict comprehension
@@ -556,34 +558,6 @@ impl<N: AsRef<str>> fmt::Debug for Arguments<'_, N> {
556
558
}
557
559
558
560
impl < C : Constant > CodeObject < C > {
559
- pub fn new (
560
- flags : CodeFlags ,
561
- posonlyarg_count : usize ,
562
- arg_count : usize ,
563
- kwonlyarg_count : usize ,
564
- source_path : C :: Name ,
565
- first_line_number : usize ,
566
- obj_name : C :: Name ,
567
- ) -> Self {
568
- CodeObject {
569
- instructions : Box :: new ( [ ] ) ,
570
- locations : Box :: new ( [ ] ) ,
571
- flags,
572
- posonlyarg_count,
573
- arg_count,
574
- kwonlyarg_count,
575
- source_path,
576
- first_line_number,
577
- obj_name,
578
- cell2arg : None ,
579
- constants : Box :: new ( [ ] ) ,
580
- names : Box :: new ( [ ] ) ,
581
- varnames : Box :: new ( [ ] ) ,
582
- cellvars : Box :: new ( [ ] ) ,
583
- freevars : Box :: new ( [ ] ) ,
584
- }
585
- }
586
-
587
561
// like inspect.getargs
588
562
pub fn arg_names ( & self ) -> Arguments < C :: Name > {
589
563
let nargs = self . arg_count ;
@@ -696,6 +670,7 @@ impl<C: Constant> CodeObject<C> {
696
670
arg_count : self . arg_count ,
697
671
kwonlyarg_count : self . kwonlyarg_count ,
698
672
first_line_number : self . first_line_number ,
673
+ max_stacksize : self . max_stacksize ,
699
674
cell2arg : self . cell2arg ,
700
675
}
701
676
}
@@ -727,6 +702,7 @@ impl<C: Constant> CodeObject<C> {
727
702
arg_count : self . arg_count ,
728
703
kwonlyarg_count : self . kwonlyarg_count ,
729
704
first_line_number : self . first_line_number ,
705
+ max_stacksize : self . max_stacksize ,
730
706
cell2arg : self . cell2arg . clone ( ) ,
731
707
}
732
708
}
@@ -802,7 +778,7 @@ impl Instruction {
802
778
| SetupExcept { handler : l }
803
779
| SetupWith { end : l }
804
780
| SetupAsyncWith { end : l }
805
- | SetupLoop { end : l }
781
+ | Break { target : l }
806
782
| Continue { target : l } => Some ( l) ,
807
783
_ => None ,
808
784
}
@@ -822,12 +798,115 @@ impl Instruction {
822
798
| SetupExcept { handler : l }
823
799
| SetupWith { end : l }
824
800
| SetupAsyncWith { end : l }
825
- | SetupLoop { end : l }
801
+ | Break { target : l }
826
802
| Continue { target : l } => Some ( l) ,
827
803
_ => None ,
828
804
}
829
805
}
830
806
807
+ pub fn unconditional_branch ( & self ) -> bool {
808
+ matches ! ( self , Jump { .. } | Continue { .. } | Break { .. } | ReturnValue | Raise { .. } )
809
+ }
810
+
811
+ pub fn stack_effect ( & self , jump : bool ) -> i32 {
812
+ match self {
813
+ ImportName { .. } | ImportNameless => -1 ,
814
+ ImportStar => -1 ,
815
+ ImportFrom { .. } => 1 ,
816
+ LoadFast ( _) | LoadNameAny ( _) | LoadGlobal ( _) | LoadDeref ( _) | LoadClassDeref ( _) => 1 ,
817
+ StoreFast ( _) | StoreLocal ( _) | StoreGlobal ( _) | StoreDeref ( _) => -1 ,
818
+ DeleteFast ( _) | DeleteLocal ( _) | DeleteGlobal ( _) | DeleteDeref ( _) => 0 ,
819
+ LoadClosure ( _) => 1 ,
820
+ Subscript => -1 ,
821
+ StoreSubscript => -3 ,
822
+ DeleteSubscript => -2 ,
823
+ LoadAttr { .. } => 0 ,
824
+ StoreAttr { .. } => -2 ,
825
+ DeleteAttr { .. } => -1 ,
826
+ LoadConst { .. } => 1 ,
827
+ UnaryOperation { .. } => 0 ,
828
+ BinaryOperation { .. } | BinaryOperationInplace { .. } | CompareOperation { .. } => -1 ,
829
+ Pop => -1 ,
830
+ Rotate { .. } => 0 ,
831
+ Duplicate => 1 ,
832
+ GetIter => 0 ,
833
+ Continue { .. } => 0 ,
834
+ Break { .. } => 0 ,
835
+ Jump { .. } => 0 ,
836
+ JumpIfTrue { .. } | JumpIfFalse { .. } => -1 ,
837
+ JumpIfTrueOrPop { .. } | JumpIfFalseOrPop { .. } => {
838
+ if jump {
839
+ 0
840
+ } else {
841
+ -1
842
+ }
843
+ }
844
+ MakeFunction ( flags) => {
845
+ -2 - flags. contains ( MakeFunctionFlags :: CLOSURE ) as i32
846
+ - flags. contains ( MakeFunctionFlags :: ANNOTATIONS ) as i32
847
+ - flags. contains ( MakeFunctionFlags :: KW_ONLY_DEFAULTS ) as i32
848
+ - flags. contains ( MakeFunctionFlags :: DEFAULTS ) as i32
849
+ + 1
850
+ }
851
+ CallFunctionPositional { nargs } => -1 - ( * nargs as i32 ) + 1 ,
852
+ CallFunctionKeyword { nargs } => -2 - ( * nargs as i32 ) + 1 ,
853
+ CallFunctionEx { has_kwargs } => -2 - * has_kwargs as i32 + 1 ,
854
+ ForIter { .. } => {
855
+ if jump {
856
+ -1
857
+ } else {
858
+ 1
859
+ }
860
+ }
861
+ ReturnValue => -1 ,
862
+ YieldValue => 0 ,
863
+ YieldFrom => -1 ,
864
+ SetupAnnotation | SetupLoop | SetupFinally { .. } | EnterFinally | EndFinally => 0 ,
865
+ SetupExcept { .. } => {
866
+ if jump {
867
+ 1
868
+ } else {
869
+ 0
870
+ }
871
+ }
872
+ SetupWith { .. } => {
873
+ if jump {
874
+ 0
875
+ } else {
876
+ 1
877
+ }
878
+ }
879
+ WithCleanupStart => 0 ,
880
+ WithCleanupFinish => -1 ,
881
+ PopBlock => 0 ,
882
+ Raise { kind } => -( * kind as u8 as i32 ) ,
883
+ BuildString { size }
884
+ | BuildTuple { size, .. }
885
+ | BuildList { size, .. }
886
+ | BuildSet { size, .. } => -( * size as i32 ) + 1 ,
887
+ BuildMap { unpack, size, .. } => {
888
+ let nargs = if * unpack { * size } else { * size * 2 } ;
889
+ -( nargs as i32 ) + 1
890
+ }
891
+ BuildSlice { step } => -2 - ( * step as i32 ) + 1 ,
892
+ ListAppend { .. } | SetAdd { .. } => -1 ,
893
+ MapAdd { .. } | MapAddRev { .. } => -2 ,
894
+ PrintExpr => -1 ,
895
+ LoadBuildClass => 1 ,
896
+ UnpackSequence { size } => -1 + * size as i32 ,
897
+ UnpackEx { before, after } => -1 + * before as i32 + 1 + * after as i32 ,
898
+ FormatValue { .. } => -1 ,
899
+ PopException => 0 ,
900
+ Reverse { .. } => 0 ,
901
+ GetAwaitable => 0 ,
902
+ BeforeAsyncWith => 1 ,
903
+ SetupAsyncWith { .. } => 0 ,
904
+ GetAIter => 0 ,
905
+ GetANext => 1 ,
906
+ EndAsyncFor => -1 ,
907
+ }
908
+ }
909
+
831
910
#[ allow( clippy:: too_many_arguments) ]
832
911
fn fmt_dis < C : Constant > (
833
912
& self ,
@@ -922,7 +1001,7 @@ impl Instruction {
922
1001
Duplicate => w ! ( Duplicate ) ,
923
1002
GetIter => w ! ( GetIter ) ,
924
1003
Continue { target } => w ! ( Continue , target) ,
925
- Break => w ! ( Break ) ,
1004
+ Break { target } => w ! ( Break , target ) ,
926
1005
Jump { target } => w ! ( Jump , target) ,
927
1006
JumpIfTrue { target } => w ! ( JumpIfTrue , target) ,
928
1007
JumpIfFalse { target } => w ! ( JumpIfFalse , target) ,
@@ -937,7 +1016,7 @@ impl Instruction {
937
1016
YieldValue => w ! ( YieldValue ) ,
938
1017
YieldFrom => w ! ( YieldFrom ) ,
939
1018
SetupAnnotation => w ! ( SetupAnnotation ) ,
940
- SetupLoop { end } => w ! ( SetupLoop , end ) ,
1019
+ SetupLoop => w ! ( SetupLoop ) ,
941
1020
SetupExcept { handler } => w ! ( SetupExcept , handler) ,
942
1021
SetupFinally { handler } => w ! ( SetupFinally , handler) ,
943
1022
EnterFinally => w ! ( EnterFinally ) ,
@@ -972,6 +1051,7 @@ impl Instruction {
972
1051
GetAwaitable => w ! ( GetAwaitable ) ,
973
1052
GetAIter => w ! ( GetAIter ) ,
974
1053
GetANext => w ! ( GetANext ) ,
1054
+ EndAsyncFor => w ! ( EndAsyncFor ) ,
975
1055
MapAdd { i } => w ! ( MapAdd , i) ,
976
1056
}
977
1057
}
0 commit comments