TritonVM / tasm-lang

Writing tasm with Rust syntax
15 stars 2 forks source link

Add support for inlining functions #32

Open Sword-Smith opened 11 months ago

Sword-Smith commented 11 months ago

Currently we are calling a function each time read from public input, or print to it. We should just inline the function body instead of calling a function.

I suggest that e.g. CompiledFunction gets a field inline which defaults to false. The inlining should happen after code generation, since the code generation step is already too big. We want to avoid making the most complicated step (intermediate AST -> TASM) more complicated than it already is. It's already too complicated.

Sword-Smith commented 11 months ago

I think this should be an optimization step performed directly on TASM code -- that way we do not make the already very complicated code generation step even bigger. I suggest to implement function inlining like this.

impl OuterFunctionTasmCode {
    /// Return the names of the subroutines that should be inlined, those whose function body is shorter or equal to the input length 
    fn get_subroutines_for_inling(&self, subroutine_length_threshold: usize) -> Vec<String> {
        todo!()
    }

    /// Perform the function inlining that mutates the `CompiledTasm` data structure
    fn inline(&mut self, subroutine_names: Vec<String>) {
        todo!()
    }

The subroutines that contain recurse cannot be inlined. We should probably only inline the subroutines that

  1. start with a label
  2. contain no recurse instructions
  3. contain only one return instruction
  4. The last instruction in the return instruction

So for this, we would also want a method

impl SubRoutine {
    pub(crate) fn can_be_inlined(&self) -> bool {
        todo!()
    }
}
Sword-Smith commented 11 months ago

@junkicide