Skip to content

Commit

Permalink
YJIT: Drop new dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
k0kubun committed Mar 8, 2023
1 parent aba4bc4 commit 57a26a4
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 20 deletions.
14 changes: 0 additions & 14 deletions yjit/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions yjit/Cargo.toml
Expand Up @@ -20,8 +20,6 @@ stats_alloc = { version = "0.1.10", optional = true }

[dev-dependencies]
capstone = "0.10.0"
indoc = "2.0.1"
unindent = "0.2.1"

[features]
# NOTE: Development builds select a set of these via configure.ac
Expand Down
5 changes: 2 additions & 3 deletions yjit/src/backend/x86_64/mod.rs
Expand Up @@ -756,8 +756,7 @@ impl Assembler

#[cfg(test)]
mod tests {
use indoc::indoc;
use crate::disasm::disasm_code_block;
use crate::disasm::{disasm_code_block, unindent, unindent_string};

use super::*;

Expand Down Expand Up @@ -948,7 +947,7 @@ mod tests {
asm.mov(Opnd::mem(64, SP, 0), sp); // should NOT be merged to lea
asm.compile_with_num_regs(&mut cb, 1);

assert_eq!(disasm_code_block(&cb), indoc! {"
assert_eq!(disasm_code_block(&cb), unindent! {"
0x0: lea rax, [rbx + 8]
0x4: mov qword ptr [rbx], rax
"});
Expand Down
60 changes: 59 additions & 1 deletion yjit/src/disasm.rs
Expand Up @@ -202,7 +202,7 @@ pub fn disasm_code_block(cb: &CodeBlock) -> String {
let end_addr = cb.get_write_ptr().raw_ptr() as usize;

let mut disasm = disasm_addr_range(cb, start_addr, end_addr);
disasm = unindent::unindent(&format!("\n{disasm}"));
disasm = unindent_string(&disasm, false);

// Code addresses will be different every time you test it. So this loop replaces
// code addresses that appear in disasm with `start_addr`-origin values.
Expand All @@ -214,6 +214,64 @@ pub fn disasm_code_block(cb: &CodeBlock) -> String {
disasm
}

/// Macro useful for raw string literals
#[cfg(test)]
macro_rules! unindent {
($str:expr) => {
unindent_string($str, true)
};
}
#[cfg(test)]
pub(crate) use unindent;

/// Remove the minimum indent from every line, skipping the first line if `skip_first`.
#[cfg(test)]
pub fn unindent_string(string: &str, trim_lines: bool) -> String {
fn split_lines(string: &str) -> Vec<String> {
let mut result: Vec<String> = vec![];
let mut buf: Vec<u8> = vec![];
for byte in string.as_bytes().iter() {
buf.push(*byte);
if *byte == b'\n' {
result.push(String::from_utf8(buf).unwrap());
buf = vec![];
}
}
if !buf.is_empty() {
result.push(String::from_utf8(buf).unwrap());
}
result
}

// Break up a string into multiple lines
let mut lines = split_lines(string);
if trim_lines { // raw string literals come with extra lines
lines.remove(0);
lines.remove(lines.len() - 1);
}

// Count the minimum number of spaces
let spaces = lines.iter().filter_map(|line| {
for (i, ch) in line.as_bytes().iter().enumerate() {
if *ch != b' ' && *ch != b'\t' {
return Some(i);
}
}
None
}).min().unwrap_or(0);

// Join lines, removing spaces
let mut unindented: Vec<u8> = vec![];
for line in lines.iter() {
if line.len() > spaces {
unindented.extend_from_slice(&line.as_bytes()[spaces..]);
} else {
unindented.extend_from_slice(&line.as_bytes());
}
}
String::from_utf8(unindented).unwrap()
}

/// Primitive called in yjit.rb
/// Produce a list of instructions compiled for an isew
#[no_mangle]
Expand Down

0 comments on commit 57a26a4

Please sign in to comment.