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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
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)
    }
}