diff --git a/Cargo.lock b/Cargo.lock index 727d459b3..24bd26697 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -755,26 +755,6 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e88a8acf291dafb59c2d96e8f59828f3838bb1a70398823ade51a84de6a6deed" -[[package]] -name = "fehler" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d5729fe49ba028cd550747b6e62cd3d841beccab5390aa398538c31a2d983635" -dependencies = [ - "fehler-macros", -] - -[[package]] -name = "fehler-macros" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ccb5acb1045ebbfa222e2c50679e392a71dd77030b78fb0189f2d9c5974400f9" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - [[package]] name = "fixedbitset" version = "0.2.0" @@ -1129,16 +1109,13 @@ dependencies = [ ] [[package]] -name = "lz-fear" -version = "0.1.1" +name = "lz4_flex" +version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "06aad1ce45e4ccf7a8d7d43e0c3ad38dc5d2255174a5f29a3c39d961fbc6181d" +checksum = "2e7bea5a3a7bb3c040adc89eadadee33c3d39371b20dd980c952dd2642867b99" dependencies = [ - "bitflags", "byteorder", - "fehler", - "thiserror", - "twox-hash", + "quick-error", ] [[package]] @@ -1769,7 +1746,7 @@ dependencies = [ "bitflags", "bstr", "itertools", - "lz-fear", + "lz4_flex", "num-bigint", "num-complex", "serde", @@ -1814,6 +1791,7 @@ dependencies = [ "itertools", "log", "num-complex", + "num-traits", "rustpython-ast", "rustpython-bytecode", "rustpython-parser", @@ -2394,16 +2372,6 @@ dependencies = [ "serde", ] -[[package]] -name = "twox-hash" -version = "1.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "04f8ab788026715fa63b31960869617cba39117e520eb415b0139543e325ab59" -dependencies = [ - "cfg-if 0.1.10", - "static_assertions", -] - [[package]] name = "typenum" version = "1.12.0" diff --git a/bytecode/Cargo.toml b/bytecode/Cargo.toml index a1c431d4e..303f429d5 100644 --- a/bytecode/Cargo.toml +++ b/bytecode/Cargo.toml @@ -11,7 +11,7 @@ license = "MIT" [dependencies] bincode = "1.1" bitflags = "1.1" -lz-fear = "0.1" +lz4_flex = "0.4" num-bigint = { version = "0.3", features = ["serde"] } num-complex = { version = "0.3", features = ["serde"] } serde = { version = "1.0", features = ["derive"] } diff --git a/bytecode/src/bytecode.rs b/bytecode/src/bytecode.rs index 778455581..8051fa73d 100644 --- a/bytecode/src/bytecode.rs +++ b/bytecode/src/bytecode.rs @@ -117,15 +117,12 @@ pub struct CodeObject { bitflags! { #[derive(Serialize, Deserialize)] pub struct CodeFlags: u16 { - const HAS_DEFAULTS = 0x01; - const HAS_KW_ONLY_DEFAULTS = 0x02; - const HAS_ANNOTATIONS = 0x04; - const NEW_LOCALS = 0x08; - const IS_GENERATOR = 0x10; - const IS_COROUTINE = 0x20; - const HAS_VARARGS = 0x40; - const HAS_VARKEYWORDS = 0x80; - const IS_OPTIMIZED = 0x0100; + const NEW_LOCALS = 0x01; + const IS_GENERATOR = 0x02; + const IS_COROUTINE = 0x04; + const HAS_VARARGS = 0x08; + const HAS_VARKEYWORDS = 0x10; + const IS_OPTIMIZED = 0x20; } } @@ -145,8 +142,8 @@ impl CodeFlags { #[derive(Serialize, Debug, Deserialize, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)] #[repr(transparent)] // XXX: if you add a new instruction that stores a Label, make sure to add it in -// compile::CodeInfo::finalize_code and CodeObject::label_targets -pub struct Label(pub usize); +// Instruction::label_arg{,_mut} +pub struct Label(pub u32); impl fmt::Display for Label { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { self.0.fmt(f) @@ -156,6 +153,8 @@ impl fmt::Display for Label { /// Transforms a value prior to formatting it. #[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)] pub enum ConversionFlag { + /// No conversion + None, /// Converts by calling `str()`. Str, /// Converts by calling `ascii()`. @@ -164,16 +163,22 @@ pub enum ConversionFlag { Repr, } -pub type NameIdx = usize; +#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)] +pub enum RaiseKind { + Reraise, + Raise, + RaiseCause, +} + +pub type NameIdx = u32; /// A Single bytecode instruction. #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub enum Instruction { - Import { - name_idx: Option, - symbols_idx: Vec, - level: usize, + ImportName { + idx: NameIdx, }, + ImportNameless, ImportStar, ImportFrom { idx: NameIdx, @@ -203,14 +208,16 @@ pub enum Instruction { }, LoadConst { /// index into constants vec - idx: usize, + idx: u32, }, UnaryOperation { op: UnaryOperator, }, BinaryOperation { op: BinaryOperator, - inplace: bool, + }, + BinaryOperationInplace { + op: BinaryOperator, }, LoadAttr { idx: NameIdx, @@ -220,11 +227,13 @@ pub enum Instruction { }, Pop, Rotate { - amount: usize, + amount: u32, }, Duplicate, GetIter, - Continue, + Continue { + target: Label, + }, Break, Jump { target: Label, @@ -247,9 +256,15 @@ pub enum Instruction { JumpIfFalseOrPop { target: Label, }, - MakeFunction, - CallFunction { - typ: CallType, + MakeFunction(MakeFunctionFlags), + CallFunctionPositional { + nargs: u32, + }, + CallFunctionKeyword { + nargs: u32, + }, + CallFunctionEx { + has_kwargs: bool, }, ForIter { target: Label, @@ -259,7 +274,6 @@ pub enum Instruction { YieldFrom, SetupAnnotation, SetupLoop { - start: Label, end: Label, }, @@ -292,56 +306,57 @@ pub enum Instruction { WithCleanupFinish, PopBlock, Raise { - argc: usize, + kind: RaiseKind, }, BuildString { - size: usize, + size: u32, }, BuildTuple { - size: usize, unpack: bool, + size: u32, }, BuildList { - size: usize, unpack: bool, + size: u32, }, BuildSet { - size: usize, unpack: bool, + size: u32, }, BuildMap { - size: usize, unpack: bool, for_call: bool, + size: u32, }, BuildSlice { - size: usize, + /// whether build a slice with a third step argument + step: bool, }, ListAppend { - i: usize, + i: u32, }, SetAdd { - i: usize, + i: u32, }, MapAdd { - i: usize, + i: u32, }, PrintExpr, LoadBuildClass, UnpackSequence { - size: usize, + size: u32, }, UnpackEx { - before: usize, - after: usize, + before: u8, + after: u8, }, FormatValue { - conversion: Option, + conversion: ConversionFlag, }, PopException, Reverse { - amount: usize, + amount: u32, }, GetAwaitable, BeforeAsyncWith, @@ -355,17 +370,20 @@ pub enum Instruction { /// required to support named expressions of Python 3.8 in dict comprehension /// today (including Py3.9) only required in dict comprehension. MapAddRev { - i: usize, + i: u32, }, } use self::Instruction::*; -#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] -pub enum CallType { - Positional(usize), - Keyword(usize), - Ex(bool), +bitflags! { + #[derive(Serialize, Deserialize)] + pub struct MakeFunctionFlags: u8 { + const CLOSURE = 0x01; + const ANNOTATIONS = 0x02; + const KW_ONLY_DEFAULTS = 0x04; + const DEFAULTS = 0x08; + } } #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] @@ -452,7 +470,7 @@ impl BorrowedConstant<'_, C> { } } -#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +#[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)] pub enum ComparisonOperator { Greater, GreaterOrEqual, @@ -467,7 +485,7 @@ pub enum ComparisonOperator { ExceptionMatch, } -#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +#[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)] pub enum BinaryOperator { Power, Multiply, @@ -484,7 +502,7 @@ pub enum BinaryOperator { Or, } -#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +#[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)] pub enum UnaryOperator { Not, Invert, @@ -588,39 +606,8 @@ impl CodeObject { pub fn label_targets(&self) -> BTreeSet