Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 31 additions & 13 deletions doc/source/lower_il.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1991,28 +1991,46 @@ See ``lower_array_new`` and ``lower_array_delete``.
Constructors and Destructors for Arrays
---------------------------------------

Construction or destruction of an entire (static or automatic) array is done by
calling runtime routines. For the cfront-like ABI, ``__vec_new`` or
``__vec_delete`` are used (see previous section). The address of the array is
passed into ``__vec_new``, thus suppressing allocation, and ``0`` for the flag
``free`` is passed into ``__vec_delete``, thus suppressing freeing of the
storage. For the IA-64 ABI, there are specific runtime routines for these
functions:
For construction of an entire (static or automatic) array, lowering generates
nested loops that call the element constructor when no exception cleanup is
needed, the array is not variably sized, and nested bounds are known and
nonzero. The loops zero each element first when value-initialization requires
it. When construction needs
exception cleanup, or for other construction cases handled by the runtime,
the cfront-like ABI calls ``__edg_vec_ctor`` for preallocated storage.
Array destruction uses ``__edg_vec_dtor``. These entry points are
implemented separately from the allocation-capable vector runtime, so a
program that is compiled without exception handling and does not use array
new does not link that runtime. With exception handling, the unwinder's
array cleanup (``__cleanup_vec_new_or_delete``) is part of the allocating
vector runtime, which is then always linked. Newly generated calls require a
matching runtime library; the existing vector entry points remain available
for previously generated code and dynamic allocation.

The runtime traverses preallocated arrays using byte offsets from the supplied
address. For nested arrays, strict ISO C pointer-bounds portability of this
traversal has not been established.

Separating array construction from the allocation-capable vector runtime does
not make exception handling allocation-free: the exception runtime can still
allocate storage for thrown objects and its own bookkeeping.

For the IA-64 ABI, the standard runtime routines for these functions are:

| ``__cxa_vec_ctor(``\ *array, num_elems, elem_size, ctor_routine,*
*dtor_routine*\ ``)``
| ``__cxa_vec_dtor(``\ *array, num_elems, elem_size, dtor_routine*\ ``)``

Copying of an entire array using a copy constructor is done in the cfront-like
ABI by calling ``__vec_cctor``, passing the array address, the number of
elements, the element size, the address of the routine to call, and the source
array address. [#f4]_ In the IA-64 ABI, the runtime routine is
``__cxa_vec_cctor.``
ABI by calling ``__vec_cctor``, or ``__edg_vec_cctor`` when exception cleanup
is needed. The call passes the array address, the number of elements, the
element size, the routine to call, and the source array address. [#f4]_ In
the IA-64 ABI, the runtime routine is ``__cxa_vec_cctor``.

If the constructor involved has default arguments, a wrapper routine is created
that requires no arguments other than the "``this``" parameter and calls the
constructor with the proper default arguments. The wrapper routine is then
passed to the runtime routine, which can call it like a normal constructor.
constructor with the proper default arguments. The generated loop or runtime
routine can then call the wrapper like a normal constructor.

See

Expand Down
2 changes: 1 addition & 1 deletion lib_src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ set(BARE_FILE_NAMES array_del array_del_aligned array_new
newnothrow newnothrow_aligned placedel placenew pure_virt rtti
set_new sized_array_del sized_array_del_aligned sized_del
sized_del_aligned static_init stdexcept thread_dtor throw
typeinfo vars vec_cctor vec_newdel vla_alloc)
typeinfo vars vec_cctor vec_newdel vec_preallocated vla_alloc)

# Load the specified macro configuration for the runtime library (i.e.,
# cpprtlib).
Expand Down
7 changes: 7 additions & 0 deletions lib_src/pending_changes/gh-561-static-array-construction.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[GH #561]
Runtime support for constructing preallocated arrays

The construction and destruction of arrays that are not allocated by array
new (__edg_vec_ctor, __edg_vec_dtor, and related routines) is now in
vec_preallocated.c, separate from the array new and delete support in
vec_newdel.c. Exception cleanup for such arrays always uses __edg_vec_dtor.
15 changes: 6 additions & 9 deletions lib_src/throw.c
Original file line number Diff line number Diff line change
Expand Up @@ -831,14 +831,12 @@ requires cleanup.
element_addr = (a_sizeof_t*)(obj_addr_array[(ehrdp + 1)->handle]);
elements = (an_element_count)*element_addr;
} /* if */
#ifndef __EDG_IA64_ABI
__vec_delete(obj_addr, (an_element_count_param)elements,
ehasp->element_size, dtor_ptr, /*delete_flag=*/FALSE,
/*unused_arg=*/0);
#else /* ifdef __EDG_IA64_ABI */
ABI_NAMESPACE::__cxa_vec_dtor(obj_addr, elements,
ehasp->element_size, dtor_ptr);
#endif /* ifdef __EDG_IA64_ABI */
/* Use the EDG-specific name in both ABIs: it cannot be resolved
by another runtime (e.g., libstdc++) and therefore links in
this library's preallocated-array routines whenever exception
handling is used. */
__edg_vec_dtor(obj_addr, (size_t)elements,
ehasp->element_size, dtor_ptr);
} /* if */
} else if (has_vtbl_ptr) {
/* A non-array object for which a special destructor must be called
Expand Down Expand Up @@ -2047,4 +2045,3 @@ built without exception handling support. This version does nothing.
} /* __eh_exit_processing */

#endif /* EXCEPTION_HANDLING */

200 changes: 41 additions & 159 deletions lib_src/vec_newdel.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,50 +59,6 @@ EXTERN_C void _array_pointer_not_from_vec_new();
to one of the vector handling routines. */
#endif /* ifndef __EDG_IA64_ABI */

struct an_array_alloc_eh_info {
void* array_ptr;
/* Pointer to the memory allocated for the array. */
ptrdiff_t
number_of_elements;
/* Total number of elements in the array. */
size_t
element_size;
/* Size of each element. */
size_t
prefix_size;
/* Size of the array prefix. */
size_t
elements_processed;
/* Number of elements constructed or destructed so
far. */
a_boolean
is_vec_new;
/* TRUE if this is a vec_new operation, FALSE if this
is a vec_delete. */
a_boolean
free_memory_on_cleanup;
/* TRUE if the memory for the array was allocated by
new and should be freed during object cleanup. */
a_destructor_ptr
destructor;
/* Pointer to the destructor to be called for each
element of the array. */
a_delete_ptr
delete_routine;
/* Pointer to the delete routine to be called to
deallocate an array. */

a_boolean is_two_arg;
/* TRUE if the delete routine is the two argument
form. FALSE if it is the single argument form. */
#ifdef __EDG_IA64_ABI
a_boolean terminate_immediately;
/* TRUE if any exception during the destruction of the
array elements should result in termination. */
#endif /* defined(__EDG_IA64_ABI) */
};


/*
Increment a void* pointer by a given value.
*/
Expand Down Expand Up @@ -811,106 +767,8 @@ operators where the delete operator, if any, takes two arguments.
} /* __cxa_vec_new3 */


EXTERN_C void ABI_NAMESPACE::__cxa_vec_ctor(
void *array_ptr,
size_t number_of_elements,
size_t element_size,
a_constructor_ptr ctor,
a_destructor_ptr dtor)
/*
The entry point used for constructing an array of objects where the memory has
already been allocated.
*/
{
(void)(array_new_general(array_ptr, (ptrdiff_t)number_of_elements,
element_size, /*prefix_size=*/0, (void *)NULL,
ctor, dtor, (a_new_ptr)NULL, (a_delete_ptr)NULL,
/*is_two_arg=*/FALSE, /*zero_init=*/FALSE));
} /* __cxa_vec_ctor */


EXTERN_C void ABI_NAMESPACE::__cxa_vec_cctor(
void *array_ptr,
void *src_array_ptr,
size_t number_of_elements,
size_t element_size,
a_copy_constructor_ptr ctor,
a_destructor_ptr dtor)
/*
The entry point used for copying an array of objects.
*/
{
(void)(array_new_general(array_ptr, (ptrdiff_t)number_of_elements,
element_size, /*prefix_size=*/0, src_array_ptr,
(a_constructor_ptr)ctor, dtor, (a_new_ptr)NULL,
(a_delete_ptr)NULL, /*is_two_arg=*/FALSE,
/*zero_init=*/FALSE));
} /* __cxa_vec_cctor */

#endif /* defined(__EDG_IA64_ABI) */

#if EXCEPTION_HANDLING
EXTERN_C void __cleanup_vec_new_or_delete(an_eh_stack_entry_ptr ehsep)
/*
Called by the exception handling cleanup routine to do the cleanup
processing for a vec_new or vec_delete operation that was interrupted by
an exception.
*/
{
/* Call the destructor, if specified, on each element in the array, in
reverse order. */
an_array_alloc_eh_info_ptr aaehip = ehsep->variant.array_alloc_eh_info;
a_destructor_ptr dtor = aaehip->destructor;
size_t number_of_elements;
size_t element_size;
void* arr_ptr;
void* array_ptr;
size_t i;
size_t first_element;

#ifdef __EDG_IA64_ABI
if (aaehip->terminate_immediately) {
__call_terminate();
} /* if */
#endif /* defined(__EDG_IA64_ABI) */
array_ptr = (void *)aaehip->array_ptr;
element_size = aaehip->element_size;
if (aaehip->is_vec_new) {
/* Cleaning up a vec_new. Destroy the fully constructed elements of
the array in reverse order. */
number_of_elements = aaehip->elements_processed;
first_element = number_of_elements - 1;
} else {
first_element = aaehip->number_of_elements -
aaehip->elements_processed - 1;
number_of_elements = first_element + 1;
} /* if */
if (dtor != NULL) {
/* If there is a destructor, destroy the objects. */
for (i = 0,
arr_ptr = (void *)(((char *)array_ptr) +
first_element * element_size);
i < number_of_elements;
i++, increment_ptr(arr_ptr, -(int)(element_size))) {
#ifndef __EDG_IA64_ABI
/* Call the destructor with 0x2 - whole object = TRUE
0x1 - delete object = FALSE. */
(*dtor)(arr_ptr, 0x2 /*whole object = TRUE, delete = FALSE*/);
#else /* ifdef __EDG_IA64_ABI */
(*dtor)(arr_ptr);
#endif /* ifdef __EDG_IA64_ABI */
} /* for */
} /* if */
if (aaehip->free_memory_on_cleanup) {
/* Call the routine to free the memory. */
size_t size = element_size * aaehip->number_of_elements;
free_array(array_ptr, size, aaehip->prefix_size,
aaehip->delete_routine, aaehip->is_two_arg);
} /* if */
} /* __cleanup_vec_new_or_delete */
#endif /* EXCEPTION_HANDLING */


#ifndef __EDG_IA64_ABI
/*ARGSUSED*/ /* terminate_immediately is used only in the IA-64 ABI. */
#endif /* ifndef __EDG_IA64_ABI */
Expand Down Expand Up @@ -1055,22 +913,6 @@ requires two arguments.
} /* __array_delete */
#endif /* ABI_CHANGES_FOR_ARRAY_NEW_AND_DELETE */
#else /* defined(__EDG_IA64_ABI) */
EXTERN_C void ABI_NAMESPACE::__cxa_vec_dtor(
void *array_ptr,
size_t number_of_elements,
size_t element_size,
a_destructor_ptr dtor)
/*
Run the destructors for an array of objects.
*/
{
array_delete_general(array_ptr, number_of_elements, element_size,
/*prefix_size=*/0, dtor, /*delete_flag=*/FALSE,
(a_delete_ptr)NULL, /*is_two_arg=*/FALSE,
/*terminate_immediately=*/FALSE);
} /* __cxa_vec_dtor */


EXTERN_C void ABI_NAMESPACE::__cxa_vec_delete(void *array_ptr,
size_t element_size,
size_t prefix_size,
Expand Down Expand Up @@ -1156,6 +998,47 @@ some reason (i.e., overflow, less than zero, too small for initializer list).

#endif /* defined(__EDG_IA64_ABI) */

#if EXCEPTION_HANDLING
EXTERN_C void __cleanup_vec_new_or_delete(an_eh_stack_entry_ptr ehsep)
/*
Use ehsep to clean up an interrupted array construction or destruction.
This is kept with the allocating vector routines so that it links
vec_newdel.o, and hence these definitions of the allocating vector entry
points, into every program that uses exception handling.
*/
{
an_array_alloc_eh_info_ptr info = ehsep->variant.array_alloc_eh_info;
size_t count;
size_t i;

#ifdef __EDG_IA64_ABI
if (info->terminate_immediately) {
__call_terminate();
} /* if */
#endif /* defined(__EDG_IA64_ABI) */
if (info->is_vec_new) {
count = info->elements_processed;
} else {
count = info->number_of_elements - info->elements_processed;
} /* if */
if (info->destructor != NULL) {
for (i = count; i != 0; --i) {
void *element = (char *)info->array_ptr + (i - 1) * info->element_size;
#ifndef __EDG_IA64_ABI
(*info->destructor)(element, 0x2);
#else /* defined(__EDG_IA64_ABI) */
(*info->destructor)(element);
#endif /* defined(__EDG_IA64_ABI) */
} /* for */
} /* if */
if (info->free_memory_on_cleanup) {
free_array(info->array_ptr,
info->element_size * info->number_of_elements,
info->prefix_size, info->delete_routine, info->is_two_arg);
} /* if */
} /* __cleanup_vec_new_or_delete */
#endif /* EXCEPTION_HANDLING */

#ifndef __EDG_IA64_ABI
EXTERN_C void _array_pointer_not_from_vec_new()
/*
Expand All @@ -1168,4 +1051,3 @@ The name is intended to describe the nature of the problem to the user
__abort_execution(ec_array_not_from_vec_new);
}
#endif /* ifndef __EDG_IA64_ABI */

44 changes: 43 additions & 1 deletion lib_src/vec_newdel.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,47 @@ operations that are in process.
*/
typedef struct an_array_alloc_eh_info *an_array_alloc_eh_info_ptr;

#endif /* ifndef VEC_NEWDEL_H */
struct an_array_alloc_eh_info {
void* array_ptr; /* Pointer to the memory allocated for the array. */
ptrdiff_t number_of_elements;
/* Total number of elements in the array. */
size_t element_size; /* Size of each element. */
size_t prefix_size; /* Size of the array prefix. */
size_t elements_processed;
/* Number of elements constructed or destructed. */
a_boolean is_vec_new; /* TRUE for construction, FALSE for destruction. */
a_boolean free_memory_on_cleanup;
/* TRUE if cleanup must free the array storage. */
a_destructor_ptr destructor;
/* Destructor for each element of the array. */
a_delete_ptr delete_routine;
/* Deallocation routine, or NULL for the default. */
a_boolean is_two_arg; /* TRUE for a two-argument deallocation routine. */
#ifdef __EDG_IA64_ABI
a_boolean terminate_immediately;
/* TRUE if a destructor throw requires termination. */
#endif /* defined(__EDG_IA64_ABI) */
};

EXTERN_C void __edg_vec_dtor(void *array_ptr,
size_t number_of_elements,
size_t element_size,
a_destructor_ptr dtor);

EXTERN_C void *__edg_vec_ctor(void *array_ptr,
size_t number_of_elements,
size_t element_size,
a_constructor_ptr ctor,
a_destructor_ptr dtor,
int zero_init);

#ifndef __EDG_IA64_ABI
EXTERN_C void __edg_vec_cctor(void *array_ptr,
size_t number_of_elements,
size_t element_size,
a_copy_constructor_ptr ctor,
void *src_array_ptr,
a_destructor_ptr dtor);
#endif /* ifndef __EDG_IA64_ABI */

#endif /* ifndef VEC_NEWDEL_H */
Loading
Loading