Skip to content

Commit cd6aa4b

Browse files
authored
Merge pull request ruby#198 from Shopify/noah_asm_comments
Assembly comments on CodeBlocks
2 parents 3d9fab3 + 126f549 commit cd6aa4b

File tree

3 files changed

+51
-17
lines changed

3 files changed

+51
-17
lines changed

yjit/src/asm/mod.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ pub struct CodeBlock
8787
// References to labels
8888
label_refs: Vec<LabelRef>,
8989

90+
// Comments for assembly instructions, if that feature is enabled
91+
asm_comments: Vec<(usize, String)>,
92+
9093
// Keep track of the current aligned write position.
9194
// Used for changing protection when writing to the JIT buffer
9295
current_aligned_write_pos: usize,
@@ -116,6 +119,7 @@ impl CodeBlock
116119
label_addrs: Vec::new(),
117120
label_names: Vec::new(),
118121
label_refs: Vec::new(),
122+
asm_comments: Vec::new(),
119123
current_aligned_write_pos: ALIGNED_WRITE_POSITION_NONE,
120124
page_size: 4096,
121125
dropped_bytes: false
@@ -131,6 +135,7 @@ impl CodeBlock
131135
label_addrs: Vec::new(),
132136
label_names: Vec::new(),
133137
label_refs: Vec::new(),
138+
asm_comments: Vec::new(),
134139
current_aligned_write_pos: ALIGNED_WRITE_POSITION_NONE,
135140
page_size,
136141
dropped_bytes: false
@@ -142,6 +147,34 @@ impl CodeBlock
142147
self.write_pos + num_bytes < self.mem_size
143148
}
144149

150+
/// Add an assembly comment if the feature is on.
151+
/// If not, this becomes an inline no-op.
152+
#[inline]
153+
pub fn add_comment(&mut self, comment: &str) {
154+
#[cfg(feature="asm_comments")]
155+
{
156+
let is_dup = if let Some((last_pos, last_comment)) = self.asm_comments.last() {
157+
*last_pos == self.write_pos && *last_comment == comment
158+
} else { false };
159+
if !is_dup {
160+
self.asm_comments.push((self.write_pos, String::from(comment)));
161+
}
162+
}
163+
}
164+
165+
/// Add an assembly comment at a specific byte offset if the feature is on.
166+
/// If not, this becomes an inline no-op.
167+
#[inline]
168+
pub fn add_comment_at(&mut self, pos:usize, comment: &str) {
169+
#[cfg(feature="asm_comments")]
170+
self.asm_comments.push((pos, String::from(comment)));
171+
}
172+
173+
/// Get a slice (readonly ref) of assembly comments - if the feature is off, this will be empty.
174+
pub fn get_comments(&self) -> &[(usize, String)] {
175+
return self.asm_comments.as_slice();
176+
}
177+
145178
pub fn get_write_pos(&self) -> usize {
146179
self.write_pos
147180
}

yjit/src/asm/x86_64/tests.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,3 +420,20 @@ fn basic_capstone_usage() -> std::result::Result<(), capstone::Error> {
420420
)),
421421
}
422422
}
423+
424+
#[test]
425+
#[cfg(feature = "asm_comments")]
426+
fn block_comments() {
427+
let mut cb = super::CodeBlock::new_dummy(4096);
428+
429+
cb.add_comment("Beginning");
430+
xor(&mut cb, EAX, EAX); // 2 bytes long
431+
cb.add_comment("Two bytes in");
432+
cb.add_comment("Two bytes in"); // Duplicate, should be ignored
433+
test(&mut cb, mem_opnd(64, RSI, 64), imm_opnd(!0x08)); // 8 bytes long
434+
cb.add_comment("Ten bytes in");
435+
436+
let comments = cb.get_comments();
437+
let expected:Vec<(usize,String)> = vec!( (0, "Beginning".to_string()), (2, "Two bytes in".to_string()), (10, "Ten bytes in".to_string()) );
438+
assert_eq!(expected, comments);
439+
}

yjit/src/codegen.rs

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -239,23 +239,7 @@ fn add_comment(cb: &mut CodeBlock, comment_str: &str)
239239
{
240240
#[cfg(feature = "asm_comments")]
241241
{
242-
/*
243-
// We can't add comments to the outlined code block
244-
if (cb == ocb)
245-
return;
246-
247-
// Avoid adding duplicate comment strings (can happen due to deferred codegen)
248-
size_t num_comments = rb_darray_size(yjit_code_comments);
249-
if (num_comments > 0) {
250-
struct yjit_comment last_comment = rb_darray_get(yjit_code_comments, num_comments - 1);
251-
if (last_comment.offset == cb->write_pos && strcmp(last_comment.comment, comment_str) == 0) {
252-
return;
253-
}
254-
}
255-
256-
struct yjit_comment new_comment = (struct yjit_comment){ cb->write_pos, comment_str };
257-
rb_darray_append(&yjit_code_comments, new_comment);
258-
*/
242+
cb.add_comment(comment_str);
259243
}
260244
}
261245

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy