1- //! High level intermediary representation.
1+ //! High- level intermediary representation (IR) in static single-assignment (SSA) form .
22
33// We use the YARV bytecode constants which have a CRuby-style name
44#![ allow( non_upper_case_globals) ]
@@ -20,6 +20,7 @@ use std::{
2020} ;
2121use crate :: hir_type:: { Type , types} ;
2222
23+ /// An index of an [`Insn`] in a [`Function`]. See also: [`Function::find`].
2324#[ derive( Copy , Clone , Ord , PartialOrd , Eq , PartialEq , Hash , Debug ) ]
2425pub struct InsnId ( pub usize ) ;
2526
@@ -35,6 +36,7 @@ impl std::fmt::Display for InsnId {
3536 }
3637}
3738
39+ /// Reference to a [`Block`]. It's an index to help with circular reference.
3840#[ derive( Copy , Clone , Eq , PartialEq , Hash , Debug ) ]
3941pub struct BlockId ( pub usize ) ;
4042
@@ -309,11 +311,14 @@ impl PtrPrintMap {
309311 }
310312}
311313
314+ /// An instruction in the SSA IR. The output of an instruction is referred to by the index of
315+ /// the instruction ([`InsnId`]). SSA form enables this, and [`UnionFind`] ([`Function::find`])
316+ /// helps with editing.
312317#[ derive( Debug , Clone ) ]
313318pub enum Insn {
314319 PutSelf ,
315320 Const { val : Const } ,
316- // SSA block parameter. Also used for function parameters in the function's entry block.
321+ /// SSA block parameter. Also used for function parameters in the function's entry block.
317322 Param { idx : usize } ,
318323
319324 StringCopy { val : InsnId } ,
@@ -324,8 +329,8 @@ pub enum Insn {
324329 ArrayDup { val : InsnId , state : InsnId } ,
325330 ArrayMax { elements : Vec < InsnId > , state : InsnId } ,
326331
327- // Check if the value is truthy and "return" a C boolean. In reality, we will likely fuse this
328- // with IfTrue/IfFalse in the backend to generate jcc.
332+ /// Check if the value is truthy and "return" a C boolean. In reality, we will likely fuse this
333+ /// with IfTrue/IfFalse in the backend to generate jcc.
329334 Test { val : InsnId } ,
330335 Defined { op_type : usize , obj : VALUE , pushval : VALUE , v : InsnId } ,
331336 GetConstantPath { ic : * const iseq_inline_constant_cache } ,
@@ -334,29 +339,29 @@ pub enum Insn {
334339 //SetIvar {},
335340 //GetIvar {},
336341
337- // Own a FrameState so that instructions can look up their dominating FrameState when
338- // generating deopt side-exits and frame reconstruction metadata. Does not directly generate
339- // any code.
342+ /// Own a FrameState so that instructions can look up their dominating FrameState when
343+ /// generating deopt side-exits and frame reconstruction metadata. Does not directly generate
344+ /// any code.
340345 Snapshot { state : FrameState } ,
341346
342- // Unconditional jump
347+ /// Unconditional jump
343348 Jump ( BranchEdge ) ,
344349
345- // Conditional branch instructions
350+ /// Conditional branch instructions
346351 IfTrue { val : InsnId , target : BranchEdge } ,
347352 IfFalse { val : InsnId , target : BranchEdge } ,
348353
349- // Call a C function
350- // `name` is for printing purposes only
354+ /// Call a C function
355+ /// `name` is for printing purposes only
351356 CCall { cfun : * const u8 , args : Vec < InsnId > , name : ID , return_type : Type , elidable : bool } ,
352357
353- // Send without block with dynamic dispatch
354- // Ignoring keyword arguments etc for now
358+ /// Send without block with dynamic dispatch
359+ /// Ignoring keyword arguments etc for now
355360 SendWithoutBlock { self_val : InsnId , call_info : CallInfo , cd : * const rb_call_data , args : Vec < InsnId > , state : InsnId } ,
356361 Send { self_val : InsnId , call_info : CallInfo , cd : * const rb_call_data , blockiseq : IseqPtr , args : Vec < InsnId > , state : InsnId } ,
357362 SendWithoutBlockDirect { self_val : InsnId , call_info : CallInfo , cd : * const rb_call_data , iseq : IseqPtr , args : Vec < InsnId > , state : InsnId } ,
358363
359- // Control flow instructions
364+ /// Control flow instructions
360365 Return { val : InsnId } ,
361366
362367 /// Fixnum +, -, *, /, %, ==, !=, <, <=, >, >=
@@ -530,6 +535,7 @@ impl std::fmt::Display for Insn {
530535 }
531536}
532537
538+ /// A basic block in a [`Function`].
533539#[ derive( Default , Debug ) ]
534540pub struct Block {
535541 params : Vec < InsnId > ,
@@ -548,6 +554,7 @@ impl Block {
548554 }
549555}
550556
557+ /// Pretty printer for [`Function`].
551558pub struct FunctionPrinter < ' a > {
552559 fun : & ' a Function ,
553560 display_snapshot : bool ,
@@ -660,6 +667,7 @@ impl<T: Copy + Into<usize> + PartialEq> UnionFind<T> {
660667 }
661668}
662669
670+ /// A group of [`Insn`]s organized as a graph of [`Block`]s.
663671#[ derive( Debug ) ]
664672pub struct Function {
665673 // ISEQ this function refers to
0 commit comments