|
| 1 | +############## |
| 2 | +Layout Strings |
| 3 | +############## |
| 4 | + |
| 5 | +Introduction |
| 6 | +*********** |
| 7 | +Layout strings encode the structure of a type into a bytestring that can be |
| 8 | +interpreted by a runtime function to achieve a destroy or copy. Rather than |
| 9 | +generating ir for a destroy/assignWithCopy/etc, we instead generate a layout |
| 10 | +string which encodes enough information for a called runtime function to |
| 11 | +perform the operation for us. Value witness functions tend to be quite large, |
| 12 | +so this allows us to replace them with a single call instead. This gives us |
| 13 | +the option of making a codesize/runtime cost trade off. |
| 14 | + |
| 15 | +NB: Integers in this format are read and written as big endian. There's no |
| 16 | +strong reason for this. Just that it's slightly easier to read/debug the |
| 17 | +string when dumping it from memory. |
| 18 | + |
| 19 | +The Runtime Functions |
| 20 | +******************** |
| 21 | +There are 5 runtime functions which follow from the 5 similar value witness functions: |
| 22 | + |
| 23 | +Destroy a given value at addr |
| 24 | +:: |
| 25 | + swift_generic_destroy(void* addr, Metadata* metadata, const char* layoutStr); |
| 26 | + |
| 27 | +Retain src if we are not taking, release dest, then copy a value from src to dest. |
| 28 | +:: |
| 29 | + swift_generic_assign(void* dest, void* src, Metadata* metadata, const char* layoutStr, bool isTake); |
| 30 | + |
| 31 | +Retain src if we are not taking, then copy a value from src to dest. |
| 32 | +:: |
| 33 | + swift_generic_initialize(void* dest, void* src, Metadata* metadata, const char* layoutStr, bool isTake); |
| 34 | + |
| 35 | +Each currently takes the layout string as an additional arugment. Ideally the |
| 36 | +layout string is stored in the metadata so that the extra arguments are not |
| 37 | +needed. This would additionally allow us to dynamically create strings at |
| 38 | +metadata initialization time to do things like runtime generic specialization |
| 39 | +for the layout string. |
| 40 | + |
| 41 | +Value types |
| 42 | +******* |
| 43 | + |
| 44 | +VALUE := SCALAR | ALIGNED_GROUP | SINGLE_ENUM | MULTI_ENUM | ARCHETYPE | RESILIENT_TYPE |
| 45 | + |
| 46 | +Scalars |
| 47 | +****** |
| 48 | + |
| 49 | +SCALAR := 'c'|'s'|'l'|'L'|'C'|'r'|'N'|'n'|'W'|'u'|'w'|'b'|'B'|'o'|'f'|'x' |
| 50 | +
|
| 51 | +For our purposes, scalars are types that are not composed of other types. In |
| 52 | +swift, this is mostly POD types such as ints, and reference types. |
| 53 | + |
| 54 | +We define POD types for types that just represent data. These do need to be |
| 55 | +copied, but do not need to be released or retained. |
| 56 | +:: |
| 57 | + I8 = 'c', |
| 58 | + I16 = 's', |
| 59 | + I32 = 'l', |
| 60 | + I64 = 'L', |
| 61 | + |
| 62 | +We also have reference types. While they are all 64bit sized, we need to |
| 63 | +differentiate between them because they have different ways of being |
| 64 | +released/retained. |
| 65 | + |
| 66 | +:: |
| 67 | + ErrorReference = 'r', |
| 68 | + NativeStrongReference = 'N', |
| 69 | + NativeUnownedReference = 'n', |
| 70 | + NativeWeakReference = 'W', |
| 71 | + UnknownUnownedReference = 'u', |
| 72 | + UnknownWeakReference = 'w', |
| 73 | + BlockReference = 'b', |
| 74 | + BridgeReference = 'B', |
| 75 | + ObjCReference = 'o', |
| 76 | + ExistentialReference = 'x', |
| 77 | + |
| 78 | +Aligned Group |
| 79 | +************* |
| 80 | +Structs are expressed as a group of values that have required alignments. |
| 81 | +:: |
| 82 | + ALIGNED_GROUP:= 'a' UINT32 (ALIGNMENT,UINT32,VALUE)+ |
| 83 | + // ALIGNED_GROUP:= 'a' numFields (alignment,fieldLength,field)+ |
| 84 | + ALIGNMENT := '0'|'1'|'2'|'3' |
| 85 | + |
| 86 | +The Alignment attached to the structs indicates the field should be aligned on |
| 87 | +2^(ALIGNMENT) bytes |
| 88 | + |
| 89 | +Enums |
| 90 | +******* |
| 91 | + |
| 92 | +We distinguish between the less complex single enums, and the more complex |
| 93 | +multi payload enums. Note the no payload enums are lowered to a POD scalar |
| 94 | +rather than an enum. |
| 95 | + |
| 96 | +Single Enums |
| 97 | +------------- |
| 98 | +:: |
| 99 | + SIZE := uint32 |
| 100 | + |
| 101 | + // e numEmptyPayloads lengthOfPayload payload |
| 102 | + SINGLEENUM := 'e' SIZE SIZE VALUE |
| 103 | + |
| 104 | +For single payload enums we need enough information to determine the overall |
| 105 | +size of the enum and how to release/retain it. For example, to release an |
| 106 | +single payload enum, we need to do the following: |
| 107 | + |
| 108 | +:: |
| 109 | + destroy SINGLEENUM: |
| 110 | + compute extra inhabitants of PAYLOAD |
| 111 | + determine if numEmptyPayloads fits in extra inhabitants |
| 112 | + if they don't fit, add extra tag bits |
| 113 | + check if any extra inhabitant bits or extra tag bits are set |
| 114 | + if not, we have a value: |
| 115 | + destroy value |
| 116 | + |
| 117 | +Multi Enums |
| 118 | +----------- |
| 119 | +:: |
| 120 | + // E numEmptyPayloads numPayloads lengthOfEachPayload payloads |
| 121 | + MULTIENUM := 'E' SIZE SIZE SIZE+ VALUE+ |
| 122 | + |
| 123 | +For multi payload enums we need enough information to determine the overall |
| 124 | +size of the enum from each paylaod and how to release/retain each payload. For |
| 125 | +example to release a multi enum, we need to do the following: |
| 126 | + |
| 127 | +:: |
| 128 | + destroy MULTIENUM: |
| 129 | + compute and merge the extra inhabitants of each possible payload |
| 130 | + compute the overall size of the enum (size of largest payload plus any extra tag bits) |
| 131 | + use the extra inhabitants and extra tag bits to get the encoded enum case |
| 132 | + if the case < numPayloads: |
| 133 | + destroy the indicated payload |
| 134 | + |
| 135 | +Examples |
| 136 | +******** |
| 137 | + |
| 138 | +Struct |
| 139 | +------ |
| 140 | +:: |
| 141 | + struct { |
| 142 | + let a : Int8 |
| 143 | + let b : Int16 |
| 144 | + let c : Int16 |
| 145 | + let d : SomeClass |
| 146 | + } |
| 147 | + |
| 148 | +byte aligned int8 |
| 149 | +2 byte aligned int16 |
| 150 | +2 byte aligned int16 |
| 151 | +8 byte aligned Native Pointer |
| 152 | +:: |
| 153 | + '1c2s2s8N' |
| 154 | + |
| 155 | +Single Enum |
| 156 | +---- |
| 157 | +:: |
| 158 | + enum { |
| 159 | + case a(c: SomeClasee) |
| 160 | + case b |
| 161 | + case c |
| 162 | + case d |
| 163 | + } |
| 164 | + |
| 165 | +A single enum with 3 no payload cases, a payload length of 1, and a payload of |
| 166 | +a single Native Pointer |
| 167 | +:: |
| 168 | + 'e<0x0><0x0><0x0><0x3><0x0><0x0><0x0><0x1>N' |
| 169 | + |
| 170 | +Multi Enum |
| 171 | +---- |
| 172 | +:: |
| 173 | + struct MyStruct { |
| 174 | + let a: SomeClass |
| 175 | + let b: SomeClass |
| 176 | + } |
| 177 | + enum { |
| 178 | + case a(c: SomeClass) |
| 179 | + case b(c: MyStruct) |
| 180 | + case c |
| 181 | + case d |
| 182 | + case e |
| 183 | + } |
| 184 | + |
| 185 | +A Multi enum with 3 no payload cases, two payloads, one of a struct, the other of just a Native pointer |
| 186 | +:: |
| 187 | + 'E<0x0><0x0><0x0><0x3><0x0><0x0><0x0><0x2><0x0><0x0><0x0><0x4><0x0><0x0><0x0><0x1>N4N4N' |
| 188 | + ^| Num no payloads | num payloads | strlength payload1 |strlen payload2 |^| MyStruct |
| 189 | + |----+--------Multi Enum Indicator |--SomeClass |
0 commit comments