llvm-scratch 0.1.15

Free Standing library in Rust
Documentation
use std::fmt;
use std::fmt::Formatter;

use id_arena::Id;

use crate::core::{
    function::{FunctionType, ParameterSet, ReturnType},
    llvm_string::LLVMString,
    llvm_type, llvm_value,
};

pub type InstructionId = Id<Instruction>;

/// An representation of LLVMInstruction.
/// LLVMInstruction を表す構造体
#[derive(Eq, PartialEq, PartialOrd, Ord, Hash)]
pub struct Instruction {
    pub kind: InstKind,
    dst_value: Option<llvm_value::LLVMValue>,
}

impl Instruction {
    pub fn new(inst_kind: InstKind, dst_value: Option<llvm_value::LLVMValue>) -> Self {
        Self {
            kind: inst_kind,
            dst_value,
        }
    }

    pub fn is_terminator(&self) -> bool {
        match &self.kind {
            InstKind::RETVOID => true,
            _ => false,
        }
    }
}

impl fmt::Display for Instruction {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match &self.kind {
            InstKind::ALLOCA(alloc_type, elements_opt, align_opt, addr_opt) => {
                if let Some(dst) = &self.dst_value {
                    write!(f, "{} = ", dst)?;
                }
                write!(f, "alloca {}", alloc_type)?;

                if let Some(elements) = elements_opt {
                    write!(f, ", {} {}", elements.0, elements.1)?;
                }

                if let Some(align) = align_opt {
                    write!(f, ", align {}", align)?;
                }

                if let Some(addr) = addr_opt {
                    write!(f, ", addrspace({})", addr)?;
                }
            }
            InstKind::CALL(tail_opt, flags_opt, ret_ty_res, callee_name, args) => {
                if let Some(dst) = &self.dst_value {
                    write!(f, "{} = ", dst)?;
                }

                if let Some(tail) = tail_opt {
                    write!(f, "{} ", tail)?;
                }

                write!(f, "call ")?;

                if let Some(flag) = flags_opt {
                    write!(f, "{} ", flag)?;
                }

                match ret_ty_res {
                    Ok(ret_ty) => {
                        write!(f, "{} ", ret_ty)?;
                    }
                    Err(fn_ty) => {
                        write!(f, "{} ", fn_ty)?;
                    }
                }

                write!(f, "@{}{}", callee_name, args)?;
            }
            InstKind::GETELEMENTPTR(inbounds, res_ty, src_value, indexes) => {
                if let Some(dst) = &self.dst_value {
                    write!(f, "{} = ", dst)?;
                }

                write!(f, "getelementptr ")?;

                if *inbounds {
                    write!(f, "inbounds ")?;
                }

                write!(f, "{}, {}* {}, ", res_ty, res_ty, src_value)?;

                for (i, (idx_ty, idx_v)) in indexes.iter().enumerate() {
                    if i == indexes.len() - 1 {
                        write!(f, "{} {}", idx_ty, idx_v)?;
                    } else {
                        write!(f, "{} {}, ", idx_ty, idx_v)?;
                    }
                }
            }
            InstKind::RETVOID => {
                write!(f, "ret void")?;
            }
        }
        Ok(())
    }
}

type NumElements = u32;
type Alignment = u32;
type AddrSpace = u32;
type Inbounds = bool;

/// LLVMInstruction の種類
#[derive(Eq, PartialEq, PartialOrd, Ord, Hash)]
pub enum InstKind {
    ALLOCA(
        llvm_type::LLVMType,
        Option<(llvm_type::LLVMType, NumElements)>,
        Option<Alignment>,
        Option<AddrSpace>,
    ),
    CALL(
        Option<TailMarker>,
        Option<FastMathFlags>,
        Result<ReturnType, FunctionType>,
        LLVMString,
        ParameterSet,
    ),
    GETELEMENTPTR(
        Inbounds,
        llvm_type::LLVMType,
        llvm_value::LLVMValue,
        Vec<(llvm_type::LLVMType, llvm_value::LLVMValue)>,
    ),
    RETVOID,
}

#[derive(Eq, PartialEq, PartialOrd, Ord, Hash)]
pub enum TailMarker {
    TAIL,
    MUSTTAIL,
    NOTAIL,
}

impl fmt::Display for TailMarker {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        let tail_str = match self {
            Self::TAIL => "tail",
            Self::MUSTTAIL => "musttail",
            Self::NOTAIL => "notail",
        };

        write!(f, "{}", tail_str)
    }
}

#[derive(Eq, PartialEq, PartialOrd, Ord, Hash)]
pub enum FastMathFlags {
    NNAN,
    NINF,
    NSZ,
    ARCP,
    CONTRACT,
    AFN,
    REASSOC,
    FAST,
}

impl fmt::Display for FastMathFlags {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        let flag_str = match self {
            Self::NNAN => "nnan",
            Self::NINF => "ninf",
            Self::NSZ => "nsz",
            Self::ARCP => "arcp",
            Self::CONTRACT => "contract",
            Self::AFN => "afn",
            Self::REASSOC => "reassoc",
            Self::FAST => "fast",
        };

        write!(f, "{}", flag_str)
    }
}

#[derive(Eq, PartialEq, PartialOrd, Ord, Hash)]
pub enum Linkage {
    PRIVATE,
    INTERNAL,
    EXTERNAL,
}

impl fmt::Display for Linkage {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        let linkage_str = match self {
            Self::PRIVATE => "private",
            Self::INTERNAL => "internal",
            Self::EXTERNAL => "external",
        };

        write!(f, "{}", linkage_str)
    }
}