summaryrefslogtreecommitdiffstats
path: root/third_party/rust/cranelift-codegen/src/isa/test_utils.rs
blob: 01c500d6caf5e668ddd355546d99980da7ddf021 (plain)
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
// This is unused when no platforms with the new backend are enabled.
#![allow(dead_code)]

use crate::binemit::{Addend, CodeOffset, CodeSink, Reloc};
use crate::ir::Value;
use crate::ir::{ConstantOffset, ExternalName, Function, JumpTable, Opcode, SourceLoc, TrapCode};
use crate::isa::TargetIsa;

use alloc::vec::Vec;
use std::string::String;

pub struct TestCodeSink {
    bytes: Vec<u8>,
}

impl TestCodeSink {
    /// Create a new TestCodeSink.
    pub fn new() -> TestCodeSink {
        TestCodeSink { bytes: vec![] }
    }

    /// Return the code emitted to this sink as a hex string.
    pub fn stringify(&self) -> String {
        // This is pretty lame, but whatever ..
        use std::fmt::Write;
        let mut s = String::with_capacity(self.bytes.len() * 2);
        for b in &self.bytes {
            write!(&mut s, "{:02X}", b).unwrap();
        }
        s
    }
}

impl CodeSink for TestCodeSink {
    fn offset(&self) -> CodeOffset {
        self.bytes.len() as CodeOffset
    }

    fn put1(&mut self, x: u8) {
        self.bytes.push(x);
    }

    fn put2(&mut self, x: u16) {
        self.bytes.push((x >> 0) as u8);
        self.bytes.push((x >> 8) as u8);
    }

    fn put4(&mut self, mut x: u32) {
        for _ in 0..4 {
            self.bytes.push(x as u8);
            x >>= 8;
        }
    }

    fn put8(&mut self, mut x: u64) {
        for _ in 0..8 {
            self.bytes.push(x as u8);
            x >>= 8;
        }
    }

    fn reloc_external(
        &mut self,
        _srcloc: SourceLoc,
        _rel: Reloc,
        _name: &ExternalName,
        _addend: Addend,
    ) {
    }

    fn reloc_constant(&mut self, _rel: Reloc, _constant_offset: ConstantOffset) {}

    fn reloc_jt(&mut self, _rel: Reloc, _jt: JumpTable) {}

    fn trap(&mut self, _code: TrapCode, _srcloc: SourceLoc) {}

    fn begin_jumptables(&mut self) {}

    fn begin_rodata(&mut self) {}

    fn end_codegen(&mut self) {}

    fn add_stack_map(&mut self, _val_list: &[Value], _func: &Function, _isa: &dyn TargetIsa) {}

    fn add_call_site(&mut self, _opcode: Opcode, _srcloc: SourceLoc) {}
}