| 1 |
/* |
| 2 |
* FCML - Free Code Manipulation Library. |
| 3 |
* Copyright (C) 2010-2020 Slawomir Wojtasiak |
| 4 |
* |
| 5 |
* This library is free software; you can redistribute it and/or |
| 6 |
* modify it under the terms of the GNU Lesser General Public |
| 7 |
* License as published by the Free Software Foundation; either |
| 8 |
* version 2.1 of the License, or (at your option) any later version. |
| 9 |
* |
| 10 |
* This library is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
* Lesser General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU Lesser General Public |
| 16 |
* License along with this library; if not, write to the Free Software |
| 17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 |
*/ |
| 19 |
|
| 20 |
#include "fcml_rend_utils.h" |
| 21 |
|
| 22 |
#include <ctype.h> |
| 23 |
#include <stdarg.h> |
| 24 |
|
| 25 |
#include <fcml_types.h> |
| 26 |
#include <fcml_common.h> |
| 27 |
#include <fcml_dialect.h> |
| 28 |
|
| 29 |
#include "fcml_env_int.h" |
| 30 |
#include "fcml_ceh.h" |
| 31 |
#include "fcml_stream.h" |
| 32 |
|
| 33 |
static fcml_string rounding_modes[4] = { |
| 34 |
"{rn-sae}", "{rd-sae}", |
| 35 |
"{ru-sae}", "{rz-sae}" |
| 36 |
}; |
| 37 |
|
| 38 |
void fcml_fn_rend_utils_format_printf(fcml_st_memory_stream *stream, |
| 39 |
const fcml_string format, ...) { |
| 40 |
|
| 41 |
/* We'll never reach this limit.*/ |
| 42 |
fcml_char local_buffer[512]; |
| 43 |
|
| 44 |
va_list arg_list; |
| 45 |
va_start(arg_list, format); |
| 46 |
|
| 47 |
fcml_fn_env_str_vsnprintf(local_buffer, sizeof(local_buffer), format, |
| 48 |
arg_list); |
| 49 |
int part_size = fcml_fn_env_str_strlen(local_buffer); |
| 50 |
if (part_size > stream->size - stream->offset - 1) { |
| 51 |
part_size = stream->size - stream->offset - 1; |
| 52 |
} |
| 53 |
|
| 54 |
fcml_fn_env_memory_copy( |
| 55 |
&(((fcml_char*) stream->base_address)[stream->offset]), |
| 56 |
local_buffer, part_size); |
| 57 |
|
| 58 |
stream->offset += part_size; |
| 59 |
} |
| 60 |
|
| 61 |
void fcml_fn_rend_utils_format_append_hex_byte(fcml_st_memory_stream *stream, |
| 62 |
fcml_uint8_t hex_byte) { |
| 63 |
fcml_fn_rend_utils_format_printf(stream, FCML_PRI_INT8_HEX, hex_byte); |
| 64 |
} |
| 65 |
|
| 66 |
void fcml_fn_rend_utils_format_append_code(const fcml_st_render_config *config, |
| 67 |
fcml_st_memory_stream *stream, const fcml_uint8_t *instrunction_code, |
| 68 |
const fcml_usize instruction_code_size) { |
| 69 |
fcml_int c_pad = 0; |
| 70 |
for (int i = 0; i < instruction_code_size; i++) { |
| 71 |
fcml_fn_rend_utils_format_append_hex_byte(stream, instrunction_code[i]); |
| 72 |
} |
| 73 |
if (config->render_flags & FCML_REND_FLAG_CODE_PADDING) { |
| 74 |
int padding = FCML_REND_DEFAULT_CODE_PADDING; |
| 75 |
if (config->prefered_code_padding > 0) { |
| 76 |
padding = config->prefered_code_padding; |
| 77 |
} |
| 78 |
c_pad = (padding - instruction_code_size) * 2; |
| 79 |
if (c_pad > 0) { |
| 80 |
fcml_fn_rend_utils_format_repeat_char(stream, ' ', c_pad); |
| 81 |
} |
| 82 |
} |
| 83 |
if (c_pad <= 0) { |
| 84 |
fcml_fn_rend_utils_format_append_str(stream, FCML_TEXT(" ")); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
fcml_int fcml_fn_rend_utils_format_append_str_if(fcml_st_memory_stream *stream, |
| 89 |
const fcml_string string, fcml_bool condition) { |
| 90 |
if (condition) { |
| 91 |
return fcml_fn_rend_utils_format_append_str(stream, string); |
| 92 |
} |
| 93 |
return 0; |
| 94 |
} |
| 95 |
|
| 96 |
void fcml_fn_rend_utils_format_finish_str(fcml_st_memory_stream *stream) { |
| 97 |
int size = sizeof(FCML_TEXT("\0")); |
| 98 |
if (fcml_fn_stream_size(stream) >= size) { |
| 99 |
fcml_fn_env_str_strncpy( |
| 100 |
&(((fcml_char*) stream->base_address)[stream->offset]), |
| 101 |
FCML_TEXT("\0"), size); |
| 102 |
stream->offset += size; |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
void fcml_fn_rend_utils_format_append_printf(fcml_st_memory_stream *stream, |
| 107 |
fcml_string format, ...) { |
| 108 |
fcml_char buffer[256]; |
| 109 |
va_list args; |
| 110 |
va_start(args, format); |
| 111 |
fcml_fn_env_str_vsnprintf(buffer, sizeof(buffer), format, args); |
| 112 |
va_end(args); |
| 113 |
fcml_fn_rend_utils_format_append_str(stream, buffer); |
| 114 |
} |
| 115 |
|
| 116 |
fcml_int fcml_fn_rend_utils_format_append_str(fcml_st_memory_stream *stream, |
| 117 |
const fcml_string source) { |
| 118 |
|
| 119 |
fcml_int source_size; |
| 120 |
if (source == NULL || (source_size = fcml_fn_env_str_strlen(source)) == 0) { |
| 121 |
return 0; |
| 122 |
} |
| 123 |
|
| 124 |
fcml_int destination_size = stream->size - stream->offset; |
| 125 |
fcml_int n = |
| 126 |
(destination_size < source_size) ? destination_size : source_size; |
| 127 |
if (n > 0) { |
| 128 |
fcml_fn_env_str_strncpy( |
| 129 |
&(((fcml_char*) stream->base_address)[stream->offset]), source, |
| 130 |
n); |
| 131 |
stream->offset += n; |
| 132 |
} else { |
| 133 |
n = 0; |
| 134 |
} |
| 135 |
|
| 136 |
return n; |
| 137 |
} |
| 138 |
|
| 139 |
fcml_int fcml_fn_rend_utils_format_append_str_padding( |
| 140 |
fcml_st_memory_stream *stream, const fcml_string source, |
| 141 |
fcml_int16_t padding) { |
| 142 |
|
| 143 |
fcml_usize str_len = fcml_fn_env_str_strlen(source); |
| 144 |
fcml_int len = 0; |
| 145 |
|
| 146 |
len += fcml_fn_rend_utils_format_append_str(stream, source); |
| 147 |
|
| 148 |
if (str_len < padding) { |
| 149 |
padding -= str_len; |
| 150 |
while (padding > 0) { |
| 151 |
len += fcml_fn_rend_utils_format_append_str(stream, " "); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
return len; |
| 156 |
} |
| 157 |
|
| 158 |
void fcml_fn_rend_utils_format_append_stream( |
| 159 |
fcml_st_memory_stream *destination_stream, |
| 160 |
const fcml_st_memory_stream *source_stream) { |
| 161 |
|
| 162 |
if (source_stream->offset == 0) { |
| 163 |
return; |
| 164 |
} |
| 165 |
|
| 166 |
fcml_int destination_size = |
| 167 |
destination_stream->size - destination_stream->offset; |
| 168 |
fcml_int n = (destination_size < source_stream->offset) ? |
| 169 |
destination_size : source_stream->offset; |
| 170 |
if (n > 0) { |
| 171 |
fcml_fn_env_str_strncpy( |
| 172 |
(fcml_string) &(((fcml_char*) destination_stream->base_address)[destination_stream->offset]), |
| 173 |
(fcml_string) source_stream->base_address, n); |
| 174 |
destination_stream->offset += n; |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
fcml_ceh_error fcml_fn_rend_utils_format_append_integer( |
| 179 |
fcml_string patterns[6][4], fcml_st_memory_stream *stream, |
| 180 |
const fcml_st_integer *integer, fcml_bool is_hex, |
| 181 |
fcml_bool remove_leading_zeros) { |
| 182 |
|
| 183 |
fcml_string *format; |
| 184 |
|
| 185 |
fcml_int delta = integer->is_signed ? 0 : 1; |
| 186 |
|
| 187 |
if (is_hex) { |
| 188 |
if (remove_leading_zeros) { |
| 189 |
format = &(patterns[4 + delta][0]); |
| 190 |
} else { |
| 191 |
format = &(patterns[2 + delta][0]); |
| 192 |
} |
| 193 |
} else { |
| 194 |
format = &(patterns[0 + delta][0]); |
| 195 |
} |
| 196 |
|
| 197 |
fcml_char local_buffer[32]; |
| 198 |
|
| 199 |
switch (integer->size) { |
| 200 |
case 8: |
| 201 |
fcml_fn_env_str_snprintf(local_buffer, sizeof(local_buffer), format[0], |
| 202 |
integer->is_signed & !is_hex ? |
| 203 |
integer->int8 : (fcml_uint8_t) integer->int8); |
| 204 |
break; |
| 205 |
case 16: |
| 206 |
fcml_fn_env_str_snprintf(local_buffer, sizeof(local_buffer), format[1], |
| 207 |
integer->is_signed & !is_hex ? |
| 208 |
integer->int16 : (fcml_uint16_t) integer->int16); |
| 209 |
break; |
| 210 |
case 32: |
| 211 |
fcml_fn_env_str_snprintf(local_buffer, sizeof(local_buffer), format[2], |
| 212 |
integer->is_signed & !is_hex ? |
| 213 |
integer->int32 : (fcml_uint32_t) integer->int32); |
| 214 |
break; |
| 215 |
case 64: |
| 216 |
fcml_fn_env_str_snprintf(local_buffer, sizeof(local_buffer), format[3], |
| 217 |
integer->is_signed & !is_hex ? |
| 218 |
integer->int64 : (fcml_uint64_t) integer->int64); |
| 219 |
break; |
| 220 |
default: |
| 221 |
return FCML_CEH_GEC_INVALID_INPUT; |
| 222 |
} |
| 223 |
|
| 224 |
/* Currently this logic desn't collide with AT&T dialect, so parameterization is not needed.*/ |
| 225 |
if (is_hex && !isdigit(local_buffer[0])) { |
| 226 |
fcml_fn_rend_utils_format_append_str(stream, "0"); |
| 227 |
} |
| 228 |
|
| 229 |
fcml_fn_rend_utils_format_append_str(stream, (fcml_string) local_buffer); |
| 230 |
|
| 231 |
return FCML_CEH_GEC_NO_ERROR; |
| 232 |
} |
| 233 |
|
| 234 |
void fcml_fn_rend_utils_format_append_reg( |
| 235 |
const fcml_st_dialect_context_int *dialect_context, |
| 236 |
fcml_st_memory_stream *output_stream, const fcml_st_register *reg, |
| 237 |
fcml_bool is_rex) { |
| 238 |
fcml_char printable_reg[64]; |
| 239 |
dialect_context->get_register(reg, printable_reg, sizeof(printable_reg), |
| 240 |
is_rex); |
| 241 |
fcml_fn_rend_utils_format_append_str(output_stream, printable_reg); |
| 242 |
} |
| 243 |
|
| 244 |
void fcml_fn_rend_utils_format_append_operand_decorators( |
| 245 |
const fcml_st_dialect_context_int *dialect_context, |
| 246 |
fcml_st_memory_stream *output_stream, |
| 247 |
const fcml_st_operand_decorators *decorators) { |
| 248 |
|
| 249 |
/* Operand mask register attribute. */ |
| 250 |
if (decorators->operand_mask_reg.type != FCML_REG_UNDEFINED) { |
| 251 |
fcml_fn_rend_utils_format_append_str(output_stream, "{"); |
| 252 |
fcml_fn_rend_utils_format_append_reg(dialect_context, output_stream, |
| 253 |
&(decorators->operand_mask_reg), FCML_FALSE); |
| 254 |
fcml_fn_rend_utils_format_append_str(output_stream, "}"); |
| 255 |
} |
| 256 |
|
| 257 |
/* Broadcasting. */ |
| 258 |
if (decorators->bcast.is_not_null) { |
| 259 |
fcml_fn_rend_utils_format_append_printf(output_stream, "{1to%d}", |
| 260 |
(fcml_int32_t) decorators->bcast.value); |
| 261 |
} |
| 262 |
|
| 263 |
/* Zeroing. */ |
| 264 |
if (decorators->z) { |
| 265 |
fcml_fn_rend_utils_format_append_str(output_stream, "{z}"); |
| 266 |
} |
| 267 |
|
| 268 |
/* Embedded rounding control. */ |
| 269 |
if (decorators->er.is_not_null) { |
| 270 |
fcml_fn_rend_utils_format_append_str(output_stream, |
| 271 |
rounding_modes[decorators->er.value]); |
| 272 |
} |
| 273 |
|
| 274 |
/* Indicates support for SAE (Suppress All Exceptions). */ |
| 275 |
if (decorators->sae) { |
| 276 |
fcml_fn_rend_utils_format_append_str(output_stream, "{sae}"); |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
fcml_int fcml_fn_rend_utils_padding(fcml_st_memory_stream *output_stream, |
| 281 |
fcml_int len, fcml_int padding, fcml_int default_padding) { |
| 282 |
if (padding <= 0) { |
| 283 |
padding = default_padding; |
| 284 |
} |
| 285 |
return fcml_fn_rend_utils_format_repeat_char(output_stream, ' ', |
| 286 |
padding - len); |
| 287 |
} |
| 288 |
|
| 289 |
fcml_int fcml_fn_rend_utils_format_repeat_char(fcml_st_memory_stream *stream, |
| 290 |
fcml_char character, fcml_int repeats) { |
| 291 |
if (repeats > 0) { |
| 292 |
fcml_int size = fcml_fn_stream_size(stream); |
| 293 |
if (size < repeats) { |
| 294 |
repeats = size; |
| 295 |
} |
| 296 |
if (repeats > 0) { |
| 297 |
fcml_char *buffer = |
| 298 |
&(((fcml_char*) stream->base_address)[stream->offset]); |
| 299 |
stream->offset += repeats; |
| 300 |
fcml_int counter = repeats; |
| 301 |
while (counter > 0) { |
| 302 |
buffer[--counter] = character; |
| 303 |
} |
| 304 |
} |
| 305 |
} else { |
| 306 |
repeats = 0; |
| 307 |
} |
| 308 |
return repeats; |
| 309 |
} |
| 310 |
|
| 311 |
fcml_int fcml_ifn_rend_utils_print_prefixes( |
| 312 |
fcml_st_memory_stream *output_stream, |
| 313 |
const fcml_st_prefixes_details *prefixes, fcml_uint32_t flags) { |
| 314 |
fcml_int len = 0; |
| 315 |
if (prefixes->is_xacquire) { |
| 316 |
len += fcml_fn_rend_utils_format_append_str(output_stream, |
| 317 |
FCML_TEXT("xacquire ")); |
| 318 |
} |
| 319 |
if (prefixes->is_xrelease) { |
| 320 |
len += fcml_fn_rend_utils_format_append_str(output_stream, |
| 321 |
FCML_TEXT("xrelease ")); |
| 322 |
} |
| 323 |
if (prefixes->is_lock) { |
| 324 |
len += fcml_fn_rend_utils_format_append_str(output_stream, |
| 325 |
FCML_TEXT("lock ")); |
| 326 |
} |
| 327 |
if (prefixes->is_branch) { |
| 328 |
len += fcml_fn_rend_utils_format_append_str(output_stream, |
| 329 |
FCML_TEXT("branch ")); |
| 330 |
} |
| 331 |
if (prefixes->is_nobranch) { |
| 332 |
len += fcml_fn_rend_utils_format_append_str(output_stream, |
| 333 |
FCML_TEXT("nobranch ")); |
| 334 |
} |
| 335 |
if (prefixes->is_rep) { |
| 336 |
if (flags & FCML_REND_FLAG_REP_PREFIX_GROUP_1) { |
| 337 |
len += fcml_fn_rend_utils_format_append_str(output_stream, |
| 338 |
FCML_TEXT("repe ")); |
| 339 |
} else if (flags & FCML_REND_FLAG_REP_PREFIX_GROUP_2) { |
| 340 |
len += fcml_fn_rend_utils_format_append_str(output_stream, |
| 341 |
FCML_TEXT("repz ")); |
| 342 |
} else { |
| 343 |
len += fcml_fn_rend_utils_format_append_str(output_stream, |
| 344 |
FCML_TEXT("rep ")); |
| 345 |
} |
| 346 |
} |
| 347 |
if (prefixes->is_repne) { |
| 348 |
if (flags & FCML_REND_FLAG_REP_PREFIX_GROUP_2) { |
| 349 |
len += fcml_fn_rend_utils_format_append_str(output_stream, |
| 350 |
FCML_TEXT("repnz ")); |
| 351 |
} else { |
| 352 |
len += fcml_fn_rend_utils_format_append_str(output_stream, |
| 353 |
FCML_TEXT("repne ")); |
| 354 |
} |
| 355 |
} |
| 356 |
return len; |
| 357 |
} |
| 358 |
|
| 359 |
fcml_bool fcml_fn_rend_utils_render_operand_separator( |
| 360 |
const fcml_st_render_config *config, |
| 361 |
fcml_st_memory_stream *output_stream, fcml_int mnemonic_length, |
| 362 |
fcml_bool first_operand) { |
| 363 |
if (!first_operand) { |
| 364 |
fcml_fn_rend_utils_format_append_str(output_stream, FCML_TEXT(",")); |
| 365 |
} else { |
| 366 |
/* Deferred mnemonic part padding. */ |
| 367 |
fcml_int padding_len = 0; |
| 368 |
if (config->render_flags & FCML_REND_FLAG_MNEMONIC_PADDING) { |
| 369 |
padding_len = fcml_fn_rend_utils_padding(output_stream, |
| 370 |
mnemonic_length, config->prefered_mnemonic_padding, |
| 371 |
FCML_REND_DEFAULT_MNEMONIC_PADDING); |
| 372 |
} |
| 373 |
if (padding_len <= 0) { |
| 374 |
fcml_fn_rend_utils_format_append_str(output_stream, FCML_TEXT(" ")); |
| 375 |
} |
| 376 |
first_operand = FCML_FALSE; |
| 377 |
} |
| 378 |
return FCML_FALSE; |
| 379 |
} |