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
use std::fmt;
use std::fmt::Formatter;

use id_arena::Id;

use crate::core::{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)?;
                }
            }
        }
        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>,
    ),
}