Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add projected bit fields #27

Merged
merged 1 commit into from
Nov 8, 2023
Merged

Add projected bit fields #27

merged 1 commit into from
Nov 8, 2023

Conversation

rauhul
Copy link
Collaborator

@rauhul rauhul commented Nov 8, 2023

Removes raw access to bit fields from generated Register Read and Write views. Users will now need to use the raw computed property of the Read and Write views to access bit fields as the underlying register storage integer type.

For example, if a programmer previously had a register with the following field and mutation

@Register(bitWidth: 32)
struct R32 {
  @ReadWrite(bits: 0..<1)
  var en: EN
}

r32.modify { $0.en = 1 }

The modify closure must be updated to: $0.raw.en = 1


Introduces the BitFieldProjectable protocol which describes how types can be encoded to or decoded from raw bit fields. Bit field macros now have an optional as: argument which take a type conforming to the BitFieldProjectable protocol and expose the field as that type. Extending the above example with this new functionality we get the following snippet:

@Register(bitWidth: 32)
struct R32 {
  @ReadWrite(bits: 0..<1, as: Bool.self)
  var en: EN
}

r32.modify { $0.en = true }

Field and projected type bit widths cannot be statically validated to match due to limitations with the Swift language. However MMIO guarantees that such uses compile to trap if the projection is used.

Removes raw access to bit fields from generated Register Read and Write
views. Users will now need to use the `raw` computed property of the
Read and Write views to access bit fields as the underlying register
storage integer type.

For example, if a programmer previously had a register with the
following field and mutation

```swift
@register(bitWidth: 32)
struct R32 {
  @readwrite(bits: 0..<1)
  var en: EN
}

r32.modify { $0.en = 1 }
```

The modify closure must be updated to: `$0.raw.en = 1`

---

Introduces the `BitFieldProjectable` protocol which describes how types
can be encoded to or decoded from raw bit fields. Bit field macros now
have an optional `as:` argument which take a type conforming to the
`BitFieldProjectable` protocol and expose the field as that type.
Extending the above example with this new functionality we get the
following snippet:

```swift
@register(bitWidth: 32)
struct R32 {
  @readwrite(bits: 0..<1, as: Bool.self)
  var en: EN
}

r32.modify { $0.en = true }
```

Field and projected type bit widths cannot be statically validated to
match due to limitations with the Swift language. However MMIO
guarantees that such uses compile to trap if the projection is used.
@rauhul rauhul merged commit 4dd2bc2 into main Nov 8, 2023
@rauhul rauhul deleted the type-safe-bit-field-projections branch November 8, 2023 01:29
rauhul added a commit that referenced this pull request Nov 17, 2023
Removes raw access to bit fields from generated Register Read and Write
views. Users will now need to use the `raw` computed property of the
Read and Write views to access bit fields as the underlying register
storage integer type.

For example, if a programmer previously had a register with the
following field and mutation

```swift
@register(bitWidth: 32)
struct R32 {
  @readwrite(bits: 0..<1)
  var en: EN
}

r32.modify { $0.en = 1 }
```

The modify closure must be updated to: `$0.raw.en = 1`

---

Introduces the `BitFieldProjectable` protocol which describes how types
can be encoded to or decoded from raw bit fields. Bit field macros now
have an optional `as:` argument which take a type conforming to the
`BitFieldProjectable` protocol and expose the field as that type.
Extending the above example with this new functionality we get the
following snippet:

```swift
@register(bitWidth: 32)
struct R32 {
  @readwrite(bits: 0..<1, as: Bool.self)
  var en: EN
}

r32.modify { $0.en = true }
```

Field and projected type bit widths cannot be statically validated to
match due to limitations with the Swift language. However MMIO
guarantees that such uses compile to trap if the projection is used.
rauhul added a commit that referenced this pull request Nov 17, 2023
Removes raw access to bit fields from generated Register Read and Write
views. Users will now need to use the `raw` computed property of the
Read and Write views to access bit fields as the underlying register
storage integer type.

For example, if a programmer previously had a register with the
following field and mutation

```swift
@register(bitWidth: 32)
struct R32 {
  @readwrite(bits: 0..<1)
  var en: EN
}

r32.modify { $0.en = 1 }
```

The modify closure must be updated to: `$0.raw.en = 1`

---

Introduces the `BitFieldProjectable` protocol which describes how types
can be encoded to or decoded from raw bit fields. Bit field macros now
have an optional `as:` argument which take a type conforming to the
`BitFieldProjectable` protocol and expose the field as that type.
Extending the above example with this new functionality we get the
following snippet:

```swift
@register(bitWidth: 32)
struct R32 {
  @readwrite(bits: 0..<1, as: Bool.self)
  var en: EN
}

r32.modify { $0.en = true }
```

Field and projected type bit widths cannot be statically validated to
match due to limitations with the Swift language. However MMIO
guarantees that such uses compile to trap if the projection is used.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant