blob: cdc92d739bbc30cdfee7ff02ed4dd5f7a85aaaae [file] [log] [blame]
// Copyright Microsoft and CHERIoT Contributors.
// SPDX-License-Identifier: MIT
#pragma once
/**
* Helpers for exposing field offsets and structure sizes into assembly. This
* file provides macros that should be included in both C++ and assembly files.
* In C++, they will check (at compile time) that the values are correct and
* print a helpful error message if they do not. In assembly, they will simply
* expose the values as assembler symbol definitions.
*/
#ifdef __cplusplus
/**
* Helper class for checking the size of a structure. Used in static
* asserts so that the expected size shows up in compiler error messages.
*/
template<auto Real, auto Expected>
struct CheckSize
{
static constexpr bool value = Real == Expected;
};
/**
* Export a macro into assembly of the form `{structure}_offset_{field}`. The
* value of this macro will be `value`. In C++, this macro will report an error
* if the provided value does not match the compiler's understanding of the
* field offset. The error message will contain the correct value.
*
* This macros also defines a static constexpr value of the same name as the
* assembly definition.
*/
# define EXPORT_ASSEMBLY_OFFSET(structure, field, val) \
static constexpr size_t structure##_offset_##field = val; \
static_assert(CheckSize<offsetof(structure, field), val>::value, \
"Offset provided for assembly is incorrect");
/**
* Variant of `EXPORT_ASSEMBLY_OFFSET` that can be used to specify the name of
* the exported offset. This is useful for providing the offset of fields in
* nested structures when assembly code does not need to know the shape of the
* overall structure, only the offset of various fields.
*
* This macros also defines a static constexpr value of the same name as the
* assembly definition.
*/
# define EXPORT_ASSEMBLY_OFFSET_NAMED(structure, field, value, name) \
static constexpr size_t name = value; \
EXPORT_ASSEMBLY_OFFSET(structure, field, value)
/**
* Export a macro into assembly of the form `{structure}_size`. The value of
* this macro will be `value`. In C++, this macro will report an error if the
* provided value does not match the compiler's understanding of the structure
* size. The error message will contain the correct value.
*
* This macros also defines a static constexpr value of the same name as the
* assembly definition.
*/
# define EXPORT_ASSEMBLY_SIZE(structure, val) \
static constexpr size_t structure##_size = val; \
static_assert(CheckSize<sizeof(structure), val>::value, \
"Size provided for assembly is incorrect");
#elif defined(__ASSEMBLER__)
# define EXPORT_ASSEMBLY_OFFSET_NAMED(structure, field, value, name) \
.set name value
# define EXPORT_ASSEMBLY_OFFSET(structure, field, value) \
.set structure##_offset_##field, value
# define EXPORT_ASSEMBLY_SIZE(structure, value) .set structure##_size, value
#else
# define EXPORT_ASSEMBLY_OFFSET(structure, field, name, value)
# define EXPORT_ASSEMBLY_SIZE(structure, name, value)
# define EXPORT_ASSEMBLY_OFFSET_NAMED(structure, field, value, name)
#endif