1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
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,
        }
    }
}

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)?;
            }
        }
        Ok(())
    }
}

type NumElements = u32;
type Alignment = u32;
type AddrSpace = u32;

/// 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,
    ),
}

#[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)
    }
}