Added enums and bitfields

WerWolv
2021-01-07 23:59:27 +01:00
parent ab5108ce06
commit 7d5b1be975

@@ -91,4 +91,55 @@ union Color {
Color color @ 0x100;
```
![Union](https://puu.sh/H4LY2/0d40d2ac34.png)
![Union](https://puu.sh/H4LY2/0d40d2ac34.png)
## Enums
Enums are types whose value is restricted to a distinct number of values. When placed in memory, the Pattern Data View will show the relevant enum entry name instead of the numerical value.
Every enum has an underlying type which is used to specify the size of the enum when placed in memory. `u32` will create a 4 byte enum, `s8` will create a 1 byte enum.
Every enum entry can be set to a distinct value using the `<identifier> = <expression>` syntax as seen below. If no value is specified for an entry, it's value will be the value of the last entry plus one. Counting starts at zero.
```cpp
enum <typeName> : <builtinTypeName> {
<enumEntry>
...
};
// Example
enum Architecture : u8 {
x86 = 0x20,
x64, // Value 0x21
ARM32 = 0x35,
ARM64 // Value 0x36
};
Architecture arch @ 0x100;
```
![Enum](https://puu.sh/H4Mx6/c1761deddd.png)
## Bitfields
If you're trying to parse a region of memory that is not aligned to the usual 8 bit boundaries or has variables that are smaller than one 8 bits (such as bit flags), a bitfield can be used.
Bitfields allow variables to be specified with a custom number bits used. This is done by using the `<identifier> : <expression>` syntax where the identifier before the colon specifies the field name and the expression after the colon the size of the field in bits.
There is no padding inserted between members, however the size of the entire bitfield will be rounded up to the next 8 bit boundary.
```cpp
bitfield <typeName> {
<bitfieldEntry>
...
};
// Example
bitfield Permission {
r : 1;
w : 1;
x : 1;
};
Permission perm @ 0x20;
```
![Bitfields](https://puu.sh/H4MC9/24980dcaae.png)