1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
|
/*
* Stack-less Just-In-Time compiler
*
* Copyright Zoltan Herczeg (hzmester@freemail.hu). All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) AND CONTRIBUTORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE COPYRIGHT HOLDER(S) OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_jump_has_label(struct sljit_jump *jump)
{
return !(jump->flags & JUMP_ADDR) && (jump->u.label != NULL);
}
SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_jump_has_target(struct sljit_jump *jump)
{
return (jump->flags & JUMP_ADDR) != 0;
}
SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_jump_is_mov_addr(struct sljit_jump *jump)
{
return (jump->flags & JUMP_MOV_ADDR) != 0;
}
#define SLJIT_SERIALIZE_DEBUG ((sljit_u16)0x1)
struct sljit_serialized_compiler {
sljit_u32 signature;
sljit_u16 version;
sljit_u16 cpu_type;
sljit_uw buf_segment_count;
sljit_uw label_count;
sljit_uw jump_count;
sljit_uw const_count;
sljit_s32 options;
sljit_s32 scratches;
sljit_s32 saveds;
sljit_s32 fscratches;
sljit_s32 fsaveds;
sljit_s32 local_size;
sljit_uw size;
#if (defined SLJIT_HAS_STATUS_FLAGS_STATE && SLJIT_HAS_STATUS_FLAGS_STATE)
sljit_s32 status_flags_state;
#endif /* SLJIT_HAS_STATUS_FLAGS_STATE */
#if (defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32)
sljit_s32 args_size;
#endif /* SLJIT_CONFIG_X86_32 */
#if ((defined SLJIT_CONFIG_ARM_32 && SLJIT_CONFIG_ARM_32) && (defined __SOFTFP__)) \
|| (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
sljit_uw args_size;
#endif /* (SLJIT_CONFIG_ARM_32 && __SOFTFP__) || SLJIT_CONFIG_MIPS_32 */
#if (defined SLJIT_CONFIG_ARM_V6 && SLJIT_CONFIG_ARM_V6)
sljit_uw cpool_diff;
sljit_uw cpool_fill;
sljit_uw patches;
#endif /* SLJIT_CONFIG_ARM_V6 */
#if (defined SLJIT_CONFIG_MIPS && SLJIT_CONFIG_MIPS)
sljit_s32 delay_slot;
#endif /* SLJIT_CONFIG_MIPS */
};
struct sljit_serialized_debug_info {
sljit_sw last_flags;
sljit_s32 last_return;
sljit_s32 logical_local_size;
};
struct sljit_serialized_label {
sljit_uw size;
};
struct sljit_serialized_jump {
sljit_uw addr;
sljit_uw flags;
sljit_uw value;
};
struct sljit_serialized_const {
sljit_uw addr;
};
#define SLJIT_SERIALIZE_ALIGN(v) (((v) + sizeof(sljit_uw) - 1) & ~(sljit_uw)(sizeof(sljit_uw) - 1))
#if (defined SLJIT_LITTLE_ENDIAN && SLJIT_LITTLE_ENDIAN)
#define SLJIT_SERIALIZE_SIGNATURE 0x534c4a54
#else /* !SLJIT_LITTLE_ENDIAN */
#define SLJIT_SERIALIZE_SIGNATURE 0x544a4c53
#endif /* SLJIT_LITTLE_ENDIAN */
#define SLJIT_SERIALIZE_VERSION 1
SLJIT_API_FUNC_ATTRIBUTE sljit_uw* sljit_serialize_compiler(struct sljit_compiler *compiler,
sljit_s32 options, sljit_uw *size)
{
sljit_uw serialized_size = sizeof(struct sljit_serialized_compiler);
struct sljit_memory_fragment *buf;
struct sljit_label *label;
struct sljit_jump *jump;
struct sljit_const *const_;
struct sljit_serialized_compiler *serialized_compiler;
struct sljit_serialized_label *serialized_label;
struct sljit_serialized_jump *serialized_jump;
struct sljit_serialized_const *serialized_const;
#if (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS) \
|| (defined SLJIT_DEBUG && SLJIT_DEBUG)
struct sljit_serialized_debug_info *serialized_debug_info;
#endif /* SLJIT_ARGUMENT_CHECKS || SLJIT_DEBUG */
sljit_uw counter, used_size;
sljit_u8 *result;
sljit_u8 *ptr;
SLJIT_UNUSED_ARG(options);
if (size != NULL)
*size = 0;
PTR_FAIL_IF(compiler->error);
#if (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS) \
|| (defined SLJIT_DEBUG && SLJIT_DEBUG)
if (!(options & SLJIT_SERIALIZE_IGNORE_DEBUG))
serialized_size += sizeof(struct sljit_serialized_debug_info);
#endif /* SLJIT_ARGUMENT_CHECKS || SLJIT_DEBUG */
#if (defined SLJIT_CONFIG_ARM_V6 && SLJIT_CONFIG_ARM_V6)
serialized_size += SLJIT_SERIALIZE_ALIGN(compiler->cpool_fill * (sizeof(sljit_uw) + 1));
#endif /* SLJIT_CONFIG_ARM_V6 */
/* Compute the size of the data. */
buf = compiler->buf;
while (buf != NULL) {
serialized_size += sizeof(sljit_uw) + SLJIT_SERIALIZE_ALIGN(buf->used_size);
buf = buf->next;
}
serialized_size += compiler->label_count * sizeof(struct sljit_serialized_label);
jump = compiler->jumps;
while (jump != NULL) {
serialized_size += sizeof(struct sljit_serialized_jump);
jump = jump->next;
}
const_ = compiler->consts;
while (const_ != NULL) {
serialized_size += sizeof(struct sljit_serialized_const);
const_ = const_->next;
}
result = (sljit_u8*)SLJIT_MALLOC(serialized_size, compiler->allocator_data);
PTR_FAIL_IF_NULL(result);
if (size != NULL)
*size = serialized_size;
ptr = result;
serialized_compiler = (struct sljit_serialized_compiler*)ptr;
ptr += sizeof(struct sljit_serialized_compiler);
serialized_compiler->signature = SLJIT_SERIALIZE_SIGNATURE;
serialized_compiler->version = SLJIT_SERIALIZE_VERSION;
serialized_compiler->cpu_type = 0;
serialized_compiler->label_count = compiler->label_count;
serialized_compiler->options = compiler->options;
serialized_compiler->scratches = compiler->scratches;
serialized_compiler->saveds = compiler->saveds;
serialized_compiler->fscratches = compiler->fscratches;
serialized_compiler->fsaveds = compiler->fsaveds;
serialized_compiler->local_size = compiler->local_size;
serialized_compiler->size = compiler->size;
#if (defined SLJIT_HAS_STATUS_FLAGS_STATE && SLJIT_HAS_STATUS_FLAGS_STATE)
serialized_compiler->status_flags_state = compiler->status_flags_state;
#endif /* SLJIT_HAS_STATUS_FLAGS_STATE */
#if (defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32) \
|| ((defined SLJIT_CONFIG_ARM_32 && SLJIT_CONFIG_ARM_32) && (defined __SOFTFP__)) \
|| (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
serialized_compiler->args_size = compiler->args_size;
#endif /* SLJIT_CONFIG_X86_32 || (SLJIT_CONFIG_ARM_32 && __SOFTFP__) || SLJIT_CONFIG_MIPS_32 */
#if (defined SLJIT_CONFIG_ARM_V6 && SLJIT_CONFIG_ARM_V6)
serialized_compiler->cpool_diff = compiler->cpool_diff;
serialized_compiler->cpool_fill = compiler->cpool_fill;
serialized_compiler->patches = compiler->patches;
SLJIT_MEMCPY(ptr, compiler->cpool, compiler->cpool_fill * sizeof(sljit_uw));
SLJIT_MEMCPY(ptr + compiler->cpool_fill * sizeof(sljit_uw), compiler->cpool_unique, compiler->cpool_fill);
ptr += SLJIT_SERIALIZE_ALIGN(compiler->cpool_fill * (sizeof(sljit_uw) + 1));
#endif /* SLJIT_CONFIG_ARM_V6 */
#if (defined SLJIT_CONFIG_MIPS && SLJIT_CONFIG_MIPS)
serialized_compiler->delay_slot = compiler->delay_slot;
#endif /* SLJIT_CONFIG_MIPS */
buf = compiler->buf;
counter = 0;
while (buf != NULL) {
used_size = buf->used_size;
*(sljit_uw*)ptr = used_size;
ptr += sizeof(sljit_uw);
SLJIT_MEMCPY(ptr, buf->memory, used_size);
ptr += SLJIT_SERIALIZE_ALIGN(used_size);
buf = buf->next;
counter++;
}
serialized_compiler->buf_segment_count = counter;
label = compiler->labels;
while (label != NULL) {
serialized_label = (struct sljit_serialized_label*)ptr;
serialized_label->size = label->size;
ptr += sizeof(struct sljit_serialized_label);
label = label->next;
}
jump = compiler->jumps;
counter = 0;
while (jump != NULL) {
serialized_jump = (struct sljit_serialized_jump*)ptr;
serialized_jump->addr = jump->addr;
serialized_jump->flags = jump->flags;
if (jump->flags & JUMP_ADDR)
serialized_jump->value = jump->u.target;
else if (jump->u.label != NULL)
serialized_jump->value = jump->u.label->u.index;
else
serialized_jump->value = SLJIT_MAX_ADDRESS;
ptr += sizeof(struct sljit_serialized_jump);
jump = jump->next;
counter++;
}
serialized_compiler->jump_count = counter;
const_ = compiler->consts;
counter = 0;
while (const_ != NULL) {
serialized_const = (struct sljit_serialized_const*)ptr;
serialized_const->addr = const_->addr;
ptr += sizeof(struct sljit_serialized_const);
const_ = const_->next;
counter++;
}
serialized_compiler->const_count = counter;
#if (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS) \
|| (defined SLJIT_DEBUG && SLJIT_DEBUG)
if (!(options & SLJIT_SERIALIZE_IGNORE_DEBUG)) {
serialized_debug_info = (struct sljit_serialized_debug_info*)ptr;
serialized_debug_info->last_flags = compiler->last_flags;
serialized_debug_info->last_return = compiler->last_return;
serialized_debug_info->logical_local_size = compiler->logical_local_size;
serialized_compiler->cpu_type |= SLJIT_SERIALIZE_DEBUG;
#if (defined SLJIT_DEBUG && SLJIT_DEBUG)
ptr += sizeof(struct sljit_serialized_debug_info);
#endif /* SLJIT_DEBUG */
}
#endif /* SLJIT_ARGUMENT_CHECKS || SLJIT_DEBUG */
SLJIT_ASSERT((sljit_uw)(ptr - result) == serialized_size);
return (sljit_uw*)result;
}
SLJIT_API_FUNC_ATTRIBUTE struct sljit_compiler *sljit_deserialize_compiler(sljit_uw* buffer, sljit_uw size,
sljit_s32 options, void *allocator_data)
{
struct sljit_compiler *compiler;
struct sljit_serialized_compiler *serialized_compiler;
struct sljit_serialized_label *serialized_label;
struct sljit_serialized_jump *serialized_jump;
struct sljit_serialized_const *serialized_const;
#if (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS) \
|| (defined SLJIT_DEBUG && SLJIT_DEBUG)
struct sljit_serialized_debug_info *serialized_debug_info;
#endif /* SLJIT_ARGUMENT_CHECKS || SLJIT_DEBUG */
struct sljit_memory_fragment *buf;
struct sljit_memory_fragment *last_buf;
struct sljit_label *label;
struct sljit_label *last_label;
struct sljit_label **label_list = NULL;
struct sljit_jump *jump;
struct sljit_jump *last_jump;
struct sljit_const *const_;
struct sljit_const *last_const;
sljit_u8 *ptr = (sljit_u8*)buffer;
sljit_u8 *end = ptr + size;
sljit_uw i, used_size, aligned_size, label_count;
SLJIT_UNUSED_ARG(options);
if (size < sizeof(struct sljit_serialized_compiler) || (size & (sizeof(sljit_uw) - 1)) != 0)
return NULL;
serialized_compiler = (struct sljit_serialized_compiler*)ptr;
if (serialized_compiler->signature != SLJIT_SERIALIZE_SIGNATURE || serialized_compiler->version != SLJIT_SERIALIZE_VERSION)
return NULL;
compiler = sljit_create_compiler(allocator_data);
PTR_FAIL_IF(compiler == NULL);
compiler->label_count = serialized_compiler->label_count;
compiler->options = serialized_compiler->options;
compiler->scratches = serialized_compiler->scratches;
compiler->saveds = serialized_compiler->saveds;
compiler->fscratches = serialized_compiler->fscratches;
compiler->fsaveds = serialized_compiler->fsaveds;
compiler->local_size = serialized_compiler->local_size;
compiler->size = serialized_compiler->size;
#if (defined SLJIT_HAS_STATUS_FLAGS_STATE && SLJIT_HAS_STATUS_FLAGS_STATE)
compiler->status_flags_state = serialized_compiler->status_flags_state;
#endif /* SLJIT_HAS_STATUS_FLAGS_STATE */
#if (defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32) \
|| ((defined SLJIT_CONFIG_ARM_32 && SLJIT_CONFIG_ARM_32) && (defined __SOFTFP__)) \
|| (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
compiler->args_size = serialized_compiler->args_size;
#endif /* SLJIT_CONFIG_X86_32 || (SLJIT_CONFIG_ARM_32 && __SOFTFP__) || SLJIT_CONFIG_MIPS_32 */
#if (defined SLJIT_CONFIG_ARM_V6 && SLJIT_CONFIG_ARM_V6)
used_size = serialized_compiler->cpool_fill;
aligned_size = SLJIT_SERIALIZE_ALIGN(used_size * (sizeof(sljit_uw) + 1));
compiler->cpool_diff = serialized_compiler->cpool_diff;
compiler->cpool_fill = used_size;
compiler->patches = serialized_compiler->patches;
if ((sljit_uw)(end - ptr) < aligned_size)
goto error;
SLJIT_MEMCPY(compiler->cpool, ptr, used_size * sizeof(sljit_uw));
SLJIT_MEMCPY(compiler->cpool_unique, ptr + used_size * sizeof(sljit_uw), used_size);
ptr += aligned_size;
#endif /* SLJIT_CONFIG_ARM_V6 */
#if (defined SLJIT_CONFIG_MIPS && SLJIT_CONFIG_MIPS)
compiler->delay_slot = serialized_compiler->delay_slot;
#endif /* SLJIT_CONFIG_MIPS */
#if (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS) \
|| (defined SLJIT_DEBUG && SLJIT_DEBUG)
if (!(serialized_compiler->cpu_type & SLJIT_SERIALIZE_DEBUG))
goto error;
#endif /* SLJIT_ARGUMENT_CHECKS || SLJIT_DEBUG */
ptr += sizeof(struct sljit_serialized_compiler);
i = serialized_compiler->buf_segment_count;
last_buf = NULL;
while (i > 0) {
if ((sljit_uw)(end - ptr) < sizeof(sljit_uw))
goto error;
used_size = *(sljit_uw*)ptr;
aligned_size = SLJIT_SERIALIZE_ALIGN(used_size);
ptr += sizeof(sljit_uw);
if ((sljit_uw)(end - ptr) < aligned_size)
goto error;
if (last_buf == NULL) {
SLJIT_ASSERT(compiler->buf != NULL && compiler->buf->next == NULL);
buf = compiler->buf;
} else {
buf = (struct sljit_memory_fragment*)SLJIT_MALLOC(BUF_SIZE, allocator_data);
if (!buf)
goto error;
buf->next = NULL;
}
buf->used_size = used_size;
SLJIT_MEMCPY(buf->memory, ptr, used_size);
if (last_buf != NULL)
last_buf->next = buf;
last_buf = buf;
ptr += aligned_size;
i--;
}
last_label = NULL;
label_count = serialized_compiler->label_count;
if ((sljit_uw)(end - ptr) < label_count * sizeof(struct sljit_serialized_label))
goto error;
label_list = (struct sljit_label **)SLJIT_MALLOC(label_count * sizeof(struct sljit_label*), allocator_data);
if (label_list == NULL)
goto error;
for (i = 0; i < label_count; i++) {
label = (struct sljit_label*)ensure_abuf(compiler, sizeof(struct sljit_label));
if (label == NULL)
goto error;
serialized_label = (struct sljit_serialized_label*)ptr;
label->next = NULL;
label->u.index = i;
label->size = serialized_label->size;
if (last_label != NULL)
last_label->next = label;
else
compiler->labels = label;
last_label = label;
label_list[i] = label;
ptr += sizeof(struct sljit_serialized_label);
}
compiler->last_label = last_label;
last_jump = NULL;
i = serialized_compiler->jump_count;
if ((sljit_uw)(end - ptr) < i * sizeof(struct sljit_serialized_jump))
goto error;
while (i > 0) {
jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
if (jump == NULL)
goto error;
serialized_jump = (struct sljit_serialized_jump*)ptr;
jump->next = NULL;
jump->addr = serialized_jump->addr;
jump->flags = serialized_jump->flags;
if (!(serialized_jump->flags & JUMP_ADDR)) {
if (serialized_jump->value != SLJIT_MAX_ADDRESS) {
if (serialized_jump->value >= label_count)
goto error;
jump->u.label = label_list[serialized_jump->value];
} else
jump->u.label = NULL;
} else
jump->u.target = serialized_jump->value;
if (last_jump != NULL)
last_jump->next = jump;
else
compiler->jumps = jump;
last_jump = jump;
ptr += sizeof(struct sljit_serialized_jump);
i--;
}
compiler->last_jump = last_jump;
SLJIT_FREE(label_list, allocator_data);
label_list = NULL;
last_const = NULL;
i = serialized_compiler->const_count;
if ((sljit_uw)(end - ptr) < i * sizeof(struct sljit_serialized_const))
goto error;
while (i > 0) {
const_ = (struct sljit_const*)ensure_abuf(compiler, sizeof(struct sljit_const));
if (const_ == NULL)
goto error;
serialized_const = (struct sljit_serialized_const*)ptr;
const_->next = NULL;
const_->addr = serialized_const->addr;
if (last_const != NULL)
last_const->next = const_;
else
compiler->consts = const_;
last_const = const_;
ptr += sizeof(struct sljit_serialized_const);
i--;
}
compiler->last_const = last_const;
#if (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS) \
|| (defined SLJIT_DEBUG && SLJIT_DEBUG)
if ((sljit_uw)(end - ptr) < sizeof(struct sljit_serialized_debug_info))
goto error;
serialized_debug_info = (struct sljit_serialized_debug_info*)ptr;
compiler->last_flags = (sljit_s32)serialized_debug_info->last_flags;
compiler->last_return = serialized_debug_info->last_return;
compiler->logical_local_size = serialized_debug_info->logical_local_size;
#endif /* SLJIT_ARGUMENT_CHECKS || SLJIT_DEBUG */
return compiler;
error:
sljit_free_compiler(compiler);
if (label_list != NULL)
SLJIT_FREE(label_list, allocator_data);
return NULL;
}
|