TritonVM / tasm-lang

Writing tasm with Rust syntax
15 stars 2 forks source link

Constant-folding on intermediate language #46

Open Sword-Smith opened 9 months ago

Sword-Smith commented 9 months ago

Constant folding in the intermediate language should reduce expressions such as BFieldElement::new(100) * BFieldElement::new(100) to BFieldElement::new(10_000).

The compiler will parse and graft let a: BFieldElement = BFieldElement::new(100) * BFieldElement::new(100); to this intermediate expression:

Let(
            LetStmt {
                var_name: "a",
                mutable: false,
                data_type: BFE,
                expr: Binop(
                    Lit(
                        BFE(
                            BFieldElement(
                                429496729500,
                            ),
                        ),
                    ),
                    Mul,
                    Lit(
                        BFE(
                            BFieldElement(
                                429496729500,
                            ),
                        ),
                    ),
                    KnownType(
                        BFE,
                    ),
                ),
            },
        ),

After the constant folding the BinOp of type Mul should be removed and only one BFE element should remain.

Constant folding on the intermediate language should be handled for these data types: