summaryrefslogtreecommitdiffstats
path: root/vendor/gimli/src/test_util.rs
blob: 706aaf934633f5e64edfb5240b6d45b0cfaaf5b1 (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
#![allow(missing_docs)]

use crate::Format;
use test_assembler::{Label, Section};

pub trait GimliSectionMethods {
    fn sleb(self, val: i64) -> Self;
    fn uleb(self, val: u64) -> Self;
    fn initial_length(self, format: Format, length: &Label, start: &Label) -> Self;
    fn word(self, size: u8, val: u64) -> Self;
    fn word_label(self, size: u8, val: &Label) -> Self;
}

impl GimliSectionMethods for Section {
    fn sleb(mut self, mut val: i64) -> Self {
        while val & !0x3f != 0 && val | 0x3f != -1 {
            self = self.D8(val as u8 | 0x80);
            val >>= 7;
        }
        self.D8(val as u8 & 0x7f)
    }

    fn uleb(mut self, mut val: u64) -> Self {
        while val & !0x7f != 0 {
            self = self.D8(val as u8 | 0x80);
            val >>= 7;
        }
        self.D8(val as u8)
    }

    fn initial_length(self, format: Format, length: &Label, start: &Label) -> Self {
        match format {
            Format::Dwarf32 => self.D32(length).mark(start),
            Format::Dwarf64 => self.D32(0xffff_ffff).D64(length).mark(start),
        }
    }

    fn word(self, size: u8, val: u64) -> Self {
        match size {
            4 => self.D32(val as u32),
            8 => self.D64(val),
            _ => panic!("unsupported word size"),
        }
    }

    fn word_label(self, size: u8, val: &Label) -> Self {
        match size {
            4 => self.D32(val),
            8 => self.D64(val),
            _ => panic!("unsupported word size"),
        }
    }
}