cranelift_codegen/isa/x64/encoding/
mod.rs

1//! Contains the encoding machinery for the various x64 instruction formats.
2use crate::{isa::x64, machinst::MachBuffer};
3use std::vec::Vec;
4
5pub mod evex;
6pub mod rex;
7pub mod vex;
8
9/// The encoding formats in this module all require a way of placing bytes into
10/// a buffer.
11pub trait ByteSink {
12    /// Add 1 byte to the code section.
13    fn put1(&mut self, _: u8);
14
15    /// Add 2 bytes to the code section.
16    fn put2(&mut self, _: u16);
17
18    /// Add 4 bytes to the code section.
19    fn put4(&mut self, _: u32);
20
21    /// Add 8 bytes to the code section.
22    fn put8(&mut self, _: u64);
23}
24
25impl ByteSink for MachBuffer<x64::inst::Inst> {
26    fn put1(&mut self, value: u8) {
27        self.put1(value)
28    }
29
30    fn put2(&mut self, value: u16) {
31        self.put2(value)
32    }
33
34    fn put4(&mut self, value: u32) {
35        self.put4(value)
36    }
37
38    fn put8(&mut self, value: u64) {
39        self.put8(value)
40    }
41}
42
43/// Provide a convenient implementation for testing.
44impl ByteSink for Vec<u8> {
45    fn put1(&mut self, v: u8) {
46        self.extend_from_slice(&[v])
47    }
48
49    fn put2(&mut self, v: u16) {
50        self.extend_from_slice(&v.to_le_bytes())
51    }
52
53    fn put4(&mut self, v: u32) {
54        self.extend_from_slice(&v.to_le_bytes())
55    }
56
57    fn put8(&mut self, v: u64) {
58        self.extend_from_slice(&v.to_le_bytes())
59    }
60}