@@ -7,7 +7,7 @@ use itertools::Itertools;
77use num_bigint:: BigInt ;
88use num_complex:: Complex64 ;
99use serde:: { Deserialize , Serialize } ;
10- use std:: collections:: { BTreeMap , HashSet } ;
10+ use std:: collections:: BTreeSet ;
1111use std:: fmt;
1212
1313/// Sourcecode location.
@@ -94,8 +94,6 @@ impl ConstantBag for BasicBag {
9494#[ derive( Clone , PartialEq , Serialize , Deserialize ) ]
9595pub struct CodeObject < C : Constant = ConstantData > {
9696 pub instructions : Vec < Instruction > ,
97- /// Jump targets.
98- pub label_map : BTreeMap < Label , usize > ,
9997 pub locations : Vec < Location > ,
10098 pub flags : CodeFlags ,
10199 pub posonlyarg_count : usize , // Number of positional-only arguments
@@ -146,11 +144,13 @@ impl CodeFlags {
146144}
147145
148146#[ derive( Serialize , Debug , Deserialize , Clone , Copy , PartialEq , Eq , Hash , Ord , PartialOrd ) ]
149- pub struct Label ( usize ) ;
150-
151- impl Label {
152- pub fn new ( label : usize ) -> Self {
153- Label ( label)
147+ #[ repr( transparent) ]
148+ // XXX: if you add a new instruction that stores a Label, make sure to add it in
149+ // compile::CodeInfo::finalize_code
150+ pub struct Label ( pub usize ) ;
151+ impl fmt:: Display for Label {
152+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
153+ self . 0 . fmt ( f)
154154 }
155155}
156156
@@ -551,7 +551,6 @@ impl<C: Constant> CodeObject<C> {
551551 ) -> Self {
552552 CodeObject {
553553 instructions : Vec :: new ( ) ,
554- label_map : BTreeMap :: new ( ) ,
555554 locations : Vec :: new ( ) ,
556555 flags,
557556 posonlyarg_count,
@@ -602,9 +601,31 @@ impl<C: Constant> CodeObject<C> {
602601 expand_codeobjects : bool ,
603602 level : usize ,
604603 ) -> fmt:: Result {
605- let label_targets: HashSet < & usize > = self . label_map . values ( ) . collect ( ) ;
604+ let mut label_targets = BTreeSet :: new ( ) ;
605+ for instruction in & self . instructions {
606+ let label = match instruction {
607+ Jump { target } => target,
608+ JumpIfTrue { target } => target,
609+ JumpIfFalse { target } => target,
610+ JumpIfTrueOrPop { target } => target,
611+ JumpIfFalseOrPop { target } => target,
612+ ForIter { target } => target,
613+ SetupLoop { start, end } => {
614+ label_targets. insert ( * start) ;
615+ label_targets. insert ( * end) ;
616+ continue ;
617+ }
618+ SetupFinally { handler } => handler,
619+ SetupExcept { handler } => handler,
620+ SetupWith { end } => end,
621+ SetupAsyncWith { end } => end,
622+ _ => continue ,
623+ } ;
624+ label_targets. insert ( * label) ;
625+ }
626+
606627 for ( offset, instruction) in self . instructions . iter ( ) . enumerate ( ) {
607- let arrow = if label_targets. contains ( & offset) {
628+ let arrow = if label_targets. contains ( & Label ( offset) ) {
608629 ">>"
609630 } else {
610631 " "
@@ -613,14 +634,7 @@ impl<C: Constant> CodeObject<C> {
613634 write ! ( f, " " ) ?;
614635 }
615636 write ! ( f, "{} {:5} " , arrow, offset) ?;
616- instruction. fmt_dis (
617- f,
618- & self . label_map ,
619- & self . constants ,
620- & self . names ,
621- expand_codeobjects,
622- level,
623- ) ?;
637+ instruction. fmt_dis ( f, & self . constants , & self . names , expand_codeobjects, level) ?;
624638 }
625639 Ok ( ( ) )
626640 }
@@ -649,7 +663,6 @@ impl<C: Constant> CodeObject<C> {
649663 . collect ( ) ,
650664
651665 instructions : self . instructions ,
652- label_map : self . label_map ,
653666 locations : self . locations ,
654667 flags : self . flags ,
655668 posonlyarg_count : self . posonlyarg_count ,
@@ -675,7 +688,6 @@ impl<C: Constant> CodeObject<C> {
675688 . collect ( ) ,
676689
677690 instructions : self . instructions . clone ( ) ,
678- label_map : self . label_map . clone ( ) ,
679691 locations : self . locations . clone ( ) ,
680692 flags : self . flags ,
681693 posonlyarg_count : self . posonlyarg_count ,
@@ -723,7 +735,6 @@ impl Instruction {
723735 fn fmt_dis < C : Constant > (
724736 & self ,
725737 f : & mut fmt:: Formatter ,
726- label_map : & BTreeMap < Label , usize > ,
727738 constants : & [ C ] ,
728739 names : & [ C :: Name ] ,
729740 expand_codeobjects : bool ,
@@ -803,28 +814,28 @@ impl Instruction {
803814 GetIter => w ! ( GetIter ) ,
804815 Continue => w ! ( Continue ) ,
805816 Break => w ! ( Break ) ,
806- Jump { target } => w ! ( Jump , label_map [ target] ) ,
807- JumpIfTrue { target } => w ! ( JumpIfTrue , label_map [ target] ) ,
808- JumpIfFalse { target } => w ! ( JumpIfFalse , label_map [ target] ) ,
809- JumpIfTrueOrPop { target } => w ! ( JumpIfTrueOrPop , label_map [ target] ) ,
810- JumpIfFalseOrPop { target } => w ! ( JumpIfFalseOrPop , label_map [ target] ) ,
817+ Jump { target } => w ! ( Jump , target) ,
818+ JumpIfTrue { target } => w ! ( JumpIfTrue , target) ,
819+ JumpIfFalse { target } => w ! ( JumpIfFalse , target) ,
820+ JumpIfTrueOrPop { target } => w ! ( JumpIfTrueOrPop , target) ,
821+ JumpIfFalseOrPop { target } => w ! ( JumpIfFalseOrPop , target) ,
811822 MakeFunction => w ! ( MakeFunction ) ,
812823 CallFunction { typ } => w ! ( CallFunction , format!( "{:?}" , typ) ) ,
813- ForIter { target } => w ! ( ForIter , label_map [ target] ) ,
824+ ForIter { target } => w ! ( ForIter , target) ,
814825 ReturnValue => w ! ( ReturnValue ) ,
815826 YieldValue => w ! ( YieldValue ) ,
816827 YieldFrom => w ! ( YieldFrom ) ,
817828 SetupAnnotation => w ! ( SetupAnnotation ) ,
818- SetupLoop { start, end } => w ! ( SetupLoop , label_map [ start] , label_map [ end] ) ,
819- SetupExcept { handler } => w ! ( SetupExcept , label_map [ handler] ) ,
820- SetupFinally { handler } => w ! ( SetupFinally , label_map [ handler] ) ,
829+ SetupLoop { start, end } => w ! ( SetupLoop , start, end) ,
830+ SetupExcept { handler } => w ! ( SetupExcept , handler) ,
831+ SetupFinally { handler } => w ! ( SetupFinally , handler) ,
821832 EnterFinally => w ! ( EnterFinally ) ,
822833 EndFinally => w ! ( EndFinally ) ,
823- SetupWith { end } => w ! ( SetupWith , label_map [ end] ) ,
834+ SetupWith { end } => w ! ( SetupWith , end) ,
824835 WithCleanupStart => w ! ( WithCleanupStart ) ,
825836 WithCleanupFinish => w ! ( WithCleanupFinish ) ,
826837 BeforeAsyncWith => w ! ( BeforeAsyncWith ) ,
827- SetupAsyncWith { end } => w ! ( SetupAsyncWith , label_map [ end] ) ,
838+ SetupAsyncWith { end } => w ! ( SetupAsyncWith , end) ,
828839 PopBlock => w ! ( PopBlock ) ,
829840 Raise { argc } => w ! ( Raise , argc) ,
830841 BuildString { size } => w ! ( BuildString , size) ,
0 commit comments