1 // This is unused when no platforms with the new backend are enabled. 2 #![allow(dead_code)] 3 4 use crate::binemit::{Addend, CodeOffset, CodeSink, Reloc}; 5 use crate::ir::Value; 6 use crate::ir::{ConstantOffset, ExternalName, Function, JumpTable, Opcode, SourceLoc, TrapCode}; 7 use crate::isa::TargetIsa; 8 9 use alloc::vec::Vec; 10 use std::string::String; 11 12 pub struct TestCodeSink { 13 bytes: Vec<u8>, 14 } 15 16 impl TestCodeSink { 17 /// Create a new TestCodeSink. new() -> TestCodeSink18 pub fn new() -> TestCodeSink { 19 TestCodeSink { bytes: vec![] } 20 } 21 22 /// Return the code emitted to this sink as a hex string. stringify(&self) -> String23 pub fn stringify(&self) -> String { 24 // This is pretty lame, but whatever .. 25 use std::fmt::Write; 26 let mut s = String::with_capacity(self.bytes.len() * 2); 27 for b in &self.bytes { 28 write!(&mut s, "{:02X}", b).unwrap(); 29 } 30 s 31 } 32 } 33 34 impl CodeSink for TestCodeSink { offset(&self) -> CodeOffset35 fn offset(&self) -> CodeOffset { 36 self.bytes.len() as CodeOffset 37 } 38 put1(&mut self, x: u8)39 fn put1(&mut self, x: u8) { 40 self.bytes.push(x); 41 } 42 put2(&mut self, x: u16)43 fn put2(&mut self, x: u16) { 44 self.bytes.push((x >> 0) as u8); 45 self.bytes.push((x >> 8) as u8); 46 } 47 put4(&mut self, mut x: u32)48 fn put4(&mut self, mut x: u32) { 49 for _ in 0..4 { 50 self.bytes.push(x as u8); 51 x >>= 8; 52 } 53 } 54 put8(&mut self, mut x: u64)55 fn put8(&mut self, mut x: u64) { 56 for _ in 0..8 { 57 self.bytes.push(x as u8); 58 x >>= 8; 59 } 60 } 61 reloc_block(&mut self, _rel: Reloc, _block_offset: CodeOffset)62 fn reloc_block(&mut self, _rel: Reloc, _block_offset: CodeOffset) {} 63 reloc_external( &mut self, _srcloc: SourceLoc, _rel: Reloc, _name: &ExternalName, _addend: Addend, )64 fn reloc_external( 65 &mut self, 66 _srcloc: SourceLoc, 67 _rel: Reloc, 68 _name: &ExternalName, 69 _addend: Addend, 70 ) { 71 } 72 reloc_constant(&mut self, _rel: Reloc, _constant_offset: ConstantOffset)73 fn reloc_constant(&mut self, _rel: Reloc, _constant_offset: ConstantOffset) {} 74 reloc_jt(&mut self, _rel: Reloc, _jt: JumpTable)75 fn reloc_jt(&mut self, _rel: Reloc, _jt: JumpTable) {} 76 trap(&mut self, _code: TrapCode, _srcloc: SourceLoc)77 fn trap(&mut self, _code: TrapCode, _srcloc: SourceLoc) {} 78 begin_jumptables(&mut self)79 fn begin_jumptables(&mut self) {} 80 begin_rodata(&mut self)81 fn begin_rodata(&mut self) {} 82 end_codegen(&mut self)83 fn end_codegen(&mut self) {} 84 add_stackmap(&mut self, _val_list: &[Value], _func: &Function, _isa: &dyn TargetIsa)85 fn add_stackmap(&mut self, _val_list: &[Value], _func: &Function, _isa: &dyn TargetIsa) {} 86 add_call_site(&mut self, _opcode: Opcode, _srcloc: SourceLoc)87 fn add_call_site(&mut self, _opcode: Opcode, _srcloc: SourceLoc) {} 88 } 89