1 /* Copyright JS Foundation and other contributors, http://js.foundation
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "ecma-alloc.h"
17 #include "ecma-literal-storage.h"
18 #include "ecma-helpers.h"
19 #include "jcontext.h"
20 #include "jerry_literal_cache.h"
21
InitJerryLiteralCache(void)22 void InitJerryLiteralCache(void) {}
23
ClearJerryLiteralCache(void)24 void ClearJerryLiteralCache(void) {}
25
AddJerryLiteralCache(int32_t hash,void * p)26 void AddJerryLiteralCache(int32_t hash, void *p) {}
27
DelJerryLiteralCache(int32_t hash)28 void DelJerryLiteralCache(int32_t hash) {}
29
GetJerryLiteralCache(int32_t hash)30 void *GetJerryLiteralCache(int32_t hash)
31 {
32 return (void*)UINT32_MAX;
33 }
34
35 /** \addtogroup ecma ECMA
36 * @{
37 *
38 * \addtogroup ecmalitstorage Literal storage
39 * @{
40 */
41
42 #if ENABLED (JERRY_ES2015)
43 /**
44 * Free symbol list
45 */
46 static void
ecma_free_symbol_list(jmem_cpointer_t symbol_list_cp)47 ecma_free_symbol_list (jmem_cpointer_t symbol_list_cp) /**< symbol list */
48 {
49 while (symbol_list_cp != JMEM_CP_NULL)
50 {
51 ecma_lit_storage_item_t *symbol_list_p = JMEM_CP_GET_NON_NULL_POINTER (ecma_lit_storage_item_t, symbol_list_cp);
52
53 for (int i = 0; i < ECMA_LIT_STORAGE_VALUE_COUNT; i++)
54 {
55 if (symbol_list_p->values[i] != JMEM_CP_NULL)
56 {
57 ecma_string_t *string_p = JMEM_CP_GET_NON_NULL_POINTER (ecma_string_t,
58 symbol_list_p->values[i]);
59
60 JERRY_ASSERT (ECMA_STRING_IS_REF_EQUALS_TO_ONE (string_p));
61 ecma_deref_ecma_string (string_p);
62 }
63 }
64
65 jmem_cpointer_t next_item_cp = symbol_list_p->next_cp;
66 jmem_pools_free (symbol_list_p, sizeof (ecma_lit_storage_item_t));
67 symbol_list_cp = next_item_cp;
68 }
69 } /* ecma_free_symbol_list */
70 #endif /* ENABLED (JERRY_ES2015) */
71
72 /**
73 * Free string list
74 */
75 static void
ecma_free_string_list(jmem_cpointer_t string_list_cp)76 ecma_free_string_list (jmem_cpointer_t string_list_cp) /**< string list */
77 {
78 while (string_list_cp != JMEM_CP_NULL)
79 {
80 ecma_lit_storage_item_t *string_list_p = JMEM_CP_GET_NON_NULL_POINTER (ecma_lit_storage_item_t, string_list_cp);
81
82 for (int i = 0; i < ECMA_LIT_STORAGE_VALUE_COUNT; i++)
83 {
84 if (string_list_p->values[i] != JMEM_CP_NULL)
85 {
86 ecma_string_t *string_p = JMEM_CP_GET_NON_NULL_POINTER (ecma_string_t,
87 string_list_p->values[i]);
88
89 JERRY_ASSERT (ECMA_STRING_IS_REF_EQUALS_TO_ONE (string_p));
90 DelJerryLiteralCache(string_p->u.hash);
91 ecma_destroy_ecma_string (string_p);
92 }
93 }
94
95 jmem_cpointer_t next_item_cp = string_list_p->next_cp;
96 jmem_pools_free (string_list_p, sizeof (ecma_lit_storage_item_t));
97 string_list_cp = next_item_cp;
98 }
99 } /* ecma_free_string_list */
100
101 /**
102 * Free number list
103 */
104 static void
ecma_free_number_list(jmem_cpointer_t number_list_cp)105 ecma_free_number_list (jmem_cpointer_t number_list_cp) /**< string list */
106 {
107 while (number_list_cp != JMEM_CP_NULL)
108 {
109 ecma_number_storage_item_t *number_list_p = JMEM_CP_GET_NON_NULL_POINTER (ecma_number_storage_item_t,
110 number_list_cp);
111
112 for (int i = 0; i < ECMA_LIT_STORAGE_VALUE_COUNT; i++)
113 {
114 if (number_list_p->values[i] != JMEM_CP_NULL)
115 {
116 ecma_number_t *num_p = JMEM_CP_GET_NON_NULL_POINTER (ecma_number_t, number_list_p->values[i]);
117 ecma_dealloc_number (num_p);
118 }
119 }
120
121 jmem_cpointer_t next_item_cp = number_list_p->next_cp;
122 jmem_pools_free (number_list_p, sizeof (ecma_number_storage_item_t));
123 number_list_cp = next_item_cp;
124 }
125 } /* ecma_free_number_list */
126
127 /**
128 * Finalize literal storage
129 */
130 void
ecma_finalize_lit_storage(void)131 ecma_finalize_lit_storage (void)
132 {
133 #if ENABLED (JERRY_ES2015)
134 ecma_free_symbol_list (JERRY_CONTEXT (symbol_list_first_cp));
135 #endif /* ENABLED (JERRY_ES2015) */
136 ecma_free_string_list (JERRY_CONTEXT (string_list_first_cp));
137 ecma_free_number_list (JERRY_CONTEXT (number_list_first_cp));
138 } /* ecma_finalize_lit_storage */
139
140 /**
141 * Find or create a literal string.
142 *
143 * @return ecma_string_t compressed pointer
144 */
145 ecma_value_t
ecma_find_or_create_literal_string(const lit_utf8_byte_t * chars_p,lit_utf8_size_t size)146 ecma_find_or_create_literal_string (const lit_utf8_byte_t *chars_p, /**< string to be searched */
147 lit_utf8_size_t size) /**< size of the string */
148 {
149 ecma_string_t *string_desc_p = ecma_find_special_string (chars_p,size);
150 if ((string_desc_p != NULL) && ECMA_IS_DIRECT_STRING (string_desc_p))
151 {
152 return ecma_make_string_value (string_desc_p);
153 }
154
155 ecma_string_t *nonref_string_p = ecma_new_nonref_ecma_string_from_utf8(chars_p, size);
156 ecma_string_t *cached_literal = (ecma_string_t *)GetJerryLiteralCache(nonref_string_p->u.hash);
157 if ((cached_literal != NULL) && (cached_literal != (void*)UINT32_MAX)) {
158 if (ecma_compare_ecma_strings_with_literal (nonref_string_p, cached_literal,chars_p)) {
159 return ecma_make_string_value (cached_literal);
160 }
161 }
162
163 ecma_string_t *string_p = ecma_new_ecma_string_from_utf8 (chars_p, size);
164
165 jmem_cpointer_t string_list_cp = JERRY_CONTEXT (string_list_first_cp);
166 jmem_cpointer_t *empty_cpointer_p = NULL;
167
168 while (string_list_cp != JMEM_CP_NULL)
169 {
170 ecma_lit_storage_item_t *string_list_p = JMEM_CP_GET_NON_NULL_POINTER (ecma_lit_storage_item_t, string_list_cp);
171
172 for (int i = 0; i < ECMA_LIT_STORAGE_VALUE_COUNT; i++)
173 {
174 if (string_list_p->values[i] == JMEM_CP_NULL)
175 {
176 if (empty_cpointer_p == NULL)
177 {
178 empty_cpointer_p = string_list_p->values + i;
179 }
180
181 if ((cached_literal == NULL) && empty_cpointer_p != NULL) {
182 break;
183 }
184 }
185 else if (cached_literal != NULL)
186 {
187 ecma_string_t *value_p = JMEM_CP_GET_NON_NULL_POINTER (ecma_string_t,
188 string_list_p->values[i]);
189
190 if (ecma_compare_ecma_strings (string_p, value_p))
191 {
192 /* Return with string if found in the list. */
193 ecma_deref_ecma_string (string_p);
194 return ecma_make_string_value (value_p);
195 }
196 }
197 }
198
199 if ((cached_literal == NULL) && (empty_cpointer_p != NULL)) {
200 break;
201 }
202 string_list_cp = string_list_p->next_cp;
203 }
204
205 ECMA_SET_STRING_AS_STATIC (string_p);
206 jmem_cpointer_t result;
207 JMEM_CP_SET_NON_NULL_POINTER (result, string_p);
208
209 if (empty_cpointer_p != NULL)
210 {
211 *empty_cpointer_p = result;
212 AddJerryLiteralCache(string_p->u.hash, string_p);
213 return ecma_make_string_value (string_p);
214 }
215
216 ecma_lit_storage_item_t *new_item_p;
217 new_item_p = (ecma_lit_storage_item_t *) jmem_pools_alloc (sizeof (ecma_lit_storage_item_t));
218
219 new_item_p->values[0] = result;
220 for (int i = 1; i < ECMA_LIT_STORAGE_VALUE_COUNT; i++)
221 {
222 new_item_p->values[i] = JMEM_CP_NULL;
223 }
224
225 new_item_p->next_cp = JERRY_CONTEXT (string_list_first_cp);
226 JMEM_CP_SET_NON_NULL_POINTER (JERRY_CONTEXT (string_list_first_cp), new_item_p);
227
228 AddJerryLiteralCache(string_p->u.hash, string_p);
229 return ecma_make_string_value (string_p);
230 } /* ecma_find_or_create_literal_string */
231
232 /**
233 * Find or create a literal number.
234 *
235 * @return ecma_string_t compressed pointer
236 */
237 ecma_value_t
ecma_find_or_create_literal_number(ecma_number_t number_arg)238 ecma_find_or_create_literal_number (ecma_number_t number_arg) /**< number to be searched */
239 {
240 ecma_value_t num = ecma_make_number_value (number_arg);
241
242 if (ecma_is_value_integer_number (num))
243 {
244 return num;
245 }
246
247 JERRY_ASSERT (ecma_is_value_float_number (num));
248
249 jmem_cpointer_t number_list_cp = JERRY_CONTEXT (number_list_first_cp);
250 jmem_cpointer_t *empty_cpointer_p = NULL;
251
252 while (number_list_cp != JMEM_CP_NULL)
253 {
254 ecma_number_storage_item_t *number_list_p = JMEM_CP_GET_NON_NULL_POINTER (ecma_number_storage_item_t,
255 number_list_cp);
256
257 for (int i = 0; i < ECMA_LIT_STORAGE_VALUE_COUNT; i++)
258 {
259 if (number_list_p->values[i] == JMEM_CP_NULL)
260 {
261 if (empty_cpointer_p == NULL)
262 {
263 empty_cpointer_p = number_list_p->values + i;
264 }
265 }
266 else
267 {
268 ecma_number_t *number_p = JMEM_CP_GET_NON_NULL_POINTER (ecma_number_t,
269 number_list_p->values[i]);
270
271 if (*number_p == number_arg)
272 {
273 ecma_free_value (num);
274 return ecma_make_float_value (number_p);
275 }
276 }
277 }
278
279 number_list_cp = number_list_p->next_cp;
280 }
281
282 ecma_number_t *num_p = ecma_get_pointer_from_float_value (num);
283
284 jmem_cpointer_t result;
285 JMEM_CP_SET_NON_NULL_POINTER (result, num_p);
286
287 if (empty_cpointer_p != NULL)
288 {
289 *empty_cpointer_p = result;
290 return num;
291 }
292
293 ecma_number_storage_item_t *new_item_p;
294 new_item_p = (ecma_number_storage_item_t *) jmem_pools_alloc (sizeof (ecma_number_storage_item_t));
295
296 new_item_p->values[0] = result;
297 for (int i = 1; i < ECMA_LIT_STORAGE_VALUE_COUNT; i++)
298 {
299 new_item_p->values[i] = JMEM_CP_NULL;
300 }
301
302 new_item_p->next_cp = JERRY_CONTEXT (number_list_first_cp);
303 JMEM_CP_SET_NON_NULL_POINTER (JERRY_CONTEXT (number_list_first_cp), new_item_p);
304
305 return num;
306 } /* ecma_find_or_create_literal_number */
307
308 /**
309 * Log2 of snapshot literal alignment.
310 */
311 #define JERRY_SNAPSHOT_LITERAL_ALIGNMENT_LOG 1
312
313 /**
314 * Snapshot literal alignment.
315 */
316 #define JERRY_SNAPSHOT_LITERAL_ALIGNMENT (1u << JERRY_SNAPSHOT_LITERAL_ALIGNMENT_LOG)
317
318 /**
319 * Literal offset shift.
320 */
321 #define JERRY_SNAPSHOT_LITERAL_SHIFT (ECMA_VALUE_SHIFT + 1)
322
323 /**
324 * Literal value is number.
325 */
326 #define JERRY_SNAPSHOT_LITERAL_IS_NUMBER (1u << ECMA_VALUE_SHIFT)
327
328 #if ENABLED (JERRY_SNAPSHOT_SAVE)
329
330 /**
331 * Append the value at the end of the appropriate list if it is not present there.
332 */
ecma_save_literals_append_value(ecma_value_t value,ecma_collection_t * lit_pool_p)333 void ecma_save_literals_append_value (ecma_value_t value, /**< value to be appended */
334 ecma_collection_t *lit_pool_p) /**< list of known values */
335 {
336 /* Unlike direct numbers, direct strings are converted to character literals. */
337 if (!ecma_is_value_string (value) && !ecma_is_value_float_number (value))
338 {
339 return;
340 }
341
342 ecma_value_t *buffer_p = lit_pool_p->buffer_p;
343
344 for (uint32_t i = 0; i < lit_pool_p->item_count; i++)
345 {
346 /* Strings / numbers are direct strings or stored in the literal storage.
347 * Therefore direct comparison is enough to find the same strings / numbers. */
348 if (buffer_p[i] == value)
349 {
350 return;
351 }
352 }
353
354 ecma_collection_push_back (lit_pool_p, value);
355 } /* ecma_save_literals_append_value */
356
357 /**
358 * Add names from a byte-code data to a list.
359 */
360 void
ecma_save_literals_add_compiled_code(const ecma_compiled_code_t * compiled_code_p,ecma_collection_t * lit_pool_p)361 ecma_save_literals_add_compiled_code (const ecma_compiled_code_t *compiled_code_p, /**< byte-code data */
362 ecma_collection_t *lit_pool_p) /**< list of known values */
363 {
364 ecma_value_t *literal_p;
365 uint32_t argument_end = 0;
366 uint32_t register_end;
367 uint32_t const_literal_end;
368 uint32_t literal_end;
369
370 JERRY_ASSERT (compiled_code_p->status_flags & CBC_CODE_FLAGS_FUNCTION);
371
372 if (compiled_code_p->status_flags & CBC_CODE_FLAGS_UINT16_ARGUMENTS)
373 {
374 cbc_uint16_arguments_t *args_p = (cbc_uint16_arguments_t *) compiled_code_p;
375 uint8_t *byte_p = (uint8_t *) compiled_code_p;
376
377 literal_p = (ecma_value_t *) (byte_p + sizeof (cbc_uint16_arguments_t));
378 register_end = args_p->register_end;
379 const_literal_end = args_p->const_literal_end - register_end;
380 literal_end = args_p->literal_end - register_end;
381
382 if (compiled_code_p->status_flags & CBC_CODE_FLAGS_MAPPED_ARGUMENTS_NEEDED)
383 {
384 argument_end = args_p->argument_end;
385 }
386 }
387 else
388 {
389 cbc_uint8_arguments_t *args_p = (cbc_uint8_arguments_t *) compiled_code_p;
390 uint8_t *byte_p = (uint8_t *) compiled_code_p;
391
392 literal_p = (ecma_value_t *) (byte_p + sizeof (cbc_uint8_arguments_t));
393 register_end = args_p->register_end;
394 const_literal_end = args_p->const_literal_end - register_end;
395 literal_end = args_p->literal_end - register_end;
396
397 if (compiled_code_p->status_flags & CBC_CODE_FLAGS_MAPPED_ARGUMENTS_NEEDED)
398 {
399 argument_end = args_p->argument_end;
400 }
401 }
402
403 for (uint32_t i = 0; i < argument_end; i++)
404 {
405 ecma_save_literals_append_value (literal_p[i], lit_pool_p);
406 }
407
408 for (uint32_t i = 0; i < const_literal_end; i++)
409 {
410 ecma_save_literals_append_value (literal_p[i], lit_pool_p);
411 }
412
413 for (uint32_t i = const_literal_end; i < literal_end; i++)
414 {
415 ecma_compiled_code_t *bytecode_p = ECMA_GET_INTERNAL_VALUE_POINTER (ecma_compiled_code_t,
416 literal_p[i]);
417
418 if ((bytecode_p->status_flags & CBC_CODE_FLAGS_FUNCTION)
419 && bytecode_p != compiled_code_p)
420 {
421 ecma_save_literals_add_compiled_code (bytecode_p, lit_pool_p);
422 }
423 }
424
425 if (argument_end != 0)
426 {
427 uint8_t *byte_p = (uint8_t *) compiled_code_p;
428 byte_p += ((size_t) compiled_code_p->size) << JMEM_ALIGNMENT_LOG;
429 literal_p = ((ecma_value_t *) byte_p) - argument_end;
430
431 for (uint32_t i = 0; i < argument_end; i++)
432 {
433 ecma_save_literals_append_value (literal_p[i], lit_pool_p);
434 }
435 }
436 } /* ecma_save_literals_add_compiled_code */
437
438 /**
439 * Save literals to specified snapshot buffer.
440 *
441 * Note:
442 * Frees 'lit_pool_p' regardless of success.
443 *
444 * @return true - if save was performed successfully (i.e. buffer size is sufficient),
445 * false - otherwise
446 */
447 bool
ecma_save_literals_for_snapshot(ecma_collection_t * lit_pool_p,uint32_t * buffer_p,size_t buffer_size,size_t * in_out_buffer_offset_p,lit_mem_to_snapshot_id_map_entry_t ** out_map_p,uint32_t * out_map_len_p)448 ecma_save_literals_for_snapshot (ecma_collection_t *lit_pool_p, /**< list of known values */
449 uint32_t *buffer_p, /**< [out] output snapshot buffer */
450 size_t buffer_size, /**< size of the buffer */
451 size_t *in_out_buffer_offset_p, /**< [in,out] write position in the buffer */
452 lit_mem_to_snapshot_id_map_entry_t **out_map_p, /**< [out] map from literal identifiers
453 * to the literal offsets
454 * in snapshot */
455 uint32_t *out_map_len_p) /**< [out] number of literals */
456 {
457 if (lit_pool_p->item_count == 0)
458 {
459 *out_map_p = NULL;
460 *out_map_len_p = 0;
461 }
462
463 uint32_t lit_table_size = 0;
464 size_t max_lit_table_size = buffer_size - *in_out_buffer_offset_p;
465
466 if (max_lit_table_size > (UINT32_MAX >> JERRY_SNAPSHOT_LITERAL_SHIFT))
467 {
468 max_lit_table_size = (UINT32_MAX >> JERRY_SNAPSHOT_LITERAL_SHIFT);
469 }
470
471 ecma_value_t *lit_buffer_p = lit_pool_p->buffer_p;
472
473 /* Compute the size of the literal pool. */
474 for (uint32_t i = 0; i < lit_pool_p->item_count; i++)
475 {
476 if (ecma_is_value_float_number (lit_buffer_p[i]))
477 {
478 lit_table_size += (uint32_t) sizeof (ecma_number_t);
479 }
480 else
481 {
482 ecma_string_t *string_p = ecma_get_string_from_value (lit_buffer_p[i]);
483
484 lit_table_size += (uint32_t) JERRY_ALIGNUP (sizeof (uint16_t) + ecma_string_get_size (string_p),
485 JERRY_SNAPSHOT_LITERAL_ALIGNMENT);
486 }
487
488 /* Check whether enough space is available and the maximum size is not reached. */
489 if (lit_table_size > max_lit_table_size)
490 {
491 ecma_collection_destroy (lit_pool_p);
492 return false;
493 }
494 }
495
496 lit_mem_to_snapshot_id_map_entry_t *map_p;
497 ecma_length_t total_count = lit_pool_p->item_count;
498
499 map_p = jmem_heap_alloc_block (total_count * sizeof (lit_mem_to_snapshot_id_map_entry_t));
500
501 /* Set return values (no error is possible from here). */
502 JERRY_ASSERT ((*in_out_buffer_offset_p % sizeof (uint32_t)) == 0);
503
504 uint8_t *destination_p = (uint8_t *) (buffer_p + (*in_out_buffer_offset_p / sizeof (uint32_t)));
505 uint32_t literal_offset = 0;
506
507 *in_out_buffer_offset_p += lit_table_size;
508 *out_map_p = map_p;
509 *out_map_len_p = total_count;
510
511 lit_buffer_p = lit_pool_p->buffer_p;
512
513 /* Generate literal pool data. */
514 for (uint32_t i = 0; i < lit_pool_p->item_count; i++)
515 {
516 map_p->literal_id = lit_buffer_p[i];
517 map_p->literal_offset = (literal_offset << JERRY_SNAPSHOT_LITERAL_SHIFT) | ECMA_TYPE_SNAPSHOT_OFFSET;
518
519 ecma_length_t length;
520
521 if (ecma_is_value_float_number (lit_buffer_p[i]))
522 {
523 map_p->literal_offset |= JERRY_SNAPSHOT_LITERAL_IS_NUMBER;
524
525 ecma_number_t num = ecma_get_float_from_value (lit_buffer_p[i]);
526 memcpy (destination_p, &num, sizeof (ecma_number_t));
527
528 length = JERRY_ALIGNUP (sizeof (ecma_number_t), JERRY_SNAPSHOT_LITERAL_ALIGNMENT);
529 }
530 else
531 {
532 ecma_string_t *string_p = ecma_get_string_from_value (lit_buffer_p[i]);
533 length = ecma_string_get_size (string_p);
534
535 *(uint16_t *) destination_p = (uint16_t) length;
536
537 ecma_string_to_utf8_bytes (string_p, destination_p + sizeof (uint16_t), length);
538
539 length = JERRY_ALIGNUP (sizeof (uint16_t) + length, JERRY_SNAPSHOT_LITERAL_ALIGNMENT);
540 }
541
542 JERRY_ASSERT ((length % sizeof (uint16_t)) == 0);
543 destination_p += length;
544 literal_offset += length;
545
546 map_p++;
547 }
548
549 ecma_collection_destroy (lit_pool_p);
550 return true;
551 } /* ecma_save_literals_for_snapshot */
552
553 #endif /* ENABLED (JERRY_SNAPSHOT_SAVE) */
554
555 #if ENABLED (JERRY_SNAPSHOT_EXEC) || ENABLED (JERRY_SNAPSHOT_SAVE)
556
557 /**
558 * Get the compressed pointer of a given literal.
559 *
560 * @return literal compressed pointer
561 */
562 ecma_value_t
ecma_snapshot_get_literal(const uint8_t * literal_base_p,ecma_value_t literal_value)563 ecma_snapshot_get_literal (const uint8_t *literal_base_p, /**< literal start */
564 ecma_value_t literal_value) /**< string / number offset */
565 {
566 JERRY_ASSERT ((literal_value & ECMA_VALUE_TYPE_MASK) == ECMA_TYPE_SNAPSHOT_OFFSET);
567
568 const uint8_t *literal_p = literal_base_p + (literal_value >> JERRY_SNAPSHOT_LITERAL_SHIFT);
569
570 if (literal_value & JERRY_SNAPSHOT_LITERAL_IS_NUMBER)
571 {
572 ecma_number_t num;
573 memcpy (&num, literal_p, sizeof (ecma_number_t));
574 return ecma_find_or_create_literal_number (num);
575 }
576
577 uint16_t length = *(const uint16_t *) literal_p;
578
579 return ecma_find_or_create_literal_string (literal_p + sizeof (uint16_t), length);
580 } /* ecma_snapshot_get_literal */
581
582 #endif /* ENABLED (JERRY_SNAPSHOT_EXEC) || ENABLED (JERRY_SNAPSHOT_SAVE) */
583
584 /**
585 * @}
586 * @}
587 */
588