From 3acb75147668942c535fb34c6335a18c2ca7c5c9 Mon Sep 17 00:00:00 2001 From: Melody Horn Date: Tue, 22 Dec 2020 19:21:41 -0700 Subject: define fragile union --- language/type-definition.rst | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/language/type-definition.rst b/language/type-definition.rst index 7b90934..04cc858 100644 --- a/language/type-definition.rst +++ b/language/type-definition.rst @@ -86,6 +86,26 @@ Defining Types However, there is no internal indication of which type of data is actually being stored in the union. As such, in non-trivial cases no compiler can predict which field is or is not valid, and any statement which reads a field of a fragile union must itself be a :crowbar:ref:`FragileStatement`. - .. todo:: + The size of a fragile union is the largest size of any of its members. + The address of each member is the address of the union object itself. + The member which was most recently set will retain its value. + Reading another member with size no larger than the most recently set member will interpret the first bytes of the most recently set member as a value of the type of the member being read. + + For example, the functions ``test1`` and ``test2`` are equivalent:: + + fragile union Example { + float32 float_data; + uint32 uint_data; + } - explain memory layout of fragile unions + uint32 test1(float32 arg) { + union Example temp; + temp.float_data = arg; + fragile return temp.uint_data; + } + + uint32 test2(float32 arg) { + float32* temp = &arg; + fragile uint32* temp_casted = (uint32*)temp; + return *temp_casted; + } -- cgit v1.2.3