@@ -87,6 +87,9 @@ pub struct CodeBlock
87
87
// References to labels
88
88
label_refs : Vec < LabelRef > ,
89
89
90
+ // Comments for assembly instructions, if that feature is enabled
91
+ asm_comments : Vec < ( usize , String ) > ,
92
+
90
93
// Keep track of the current aligned write position.
91
94
// Used for changing protection when writing to the JIT buffer
92
95
current_aligned_write_pos : usize ,
@@ -116,6 +119,7 @@ impl CodeBlock
116
119
label_addrs : Vec :: new ( ) ,
117
120
label_names : Vec :: new ( ) ,
118
121
label_refs : Vec :: new ( ) ,
122
+ asm_comments : Vec :: new ( ) ,
119
123
current_aligned_write_pos : ALIGNED_WRITE_POSITION_NONE ,
120
124
page_size : 4096 ,
121
125
dropped_bytes : false
@@ -131,6 +135,7 @@ impl CodeBlock
131
135
label_addrs : Vec :: new ( ) ,
132
136
label_names : Vec :: new ( ) ,
133
137
label_refs : Vec :: new ( ) ,
138
+ asm_comments : Vec :: new ( ) ,
134
139
current_aligned_write_pos : ALIGNED_WRITE_POSITION_NONE ,
135
140
page_size,
136
141
dropped_bytes : false
@@ -142,6 +147,34 @@ impl CodeBlock
142
147
self . write_pos + num_bytes < self . mem_size
143
148
}
144
149
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
+
145
178
pub fn get_write_pos ( & self ) -> usize {
146
179
self . write_pos
147
180
}
0 commit comments