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

Initial Register and BitField macros #4

Merged
merged 6 commits into from
Oct 6, 2023
Merged

Initial Register and BitField macros #4

merged 6 commits into from
Oct 6, 2023

Conversation

rauhul
Copy link
Collaborator

@rauhul rauhul commented Sep 27, 2023

adds initial macros for declaring symmetric and asymmetric registers composed of bitfields.

adds initial macros for declaring symmetric and asymmetric registers
composed of bitfields.
@rauhul rauhul marked this pull request as draft September 27, 2023 19:46
@rauhul rauhul self-assigned this Sep 27, 2023
@rauhul rauhul added the enhancement New feature or request label Sep 27, 2023
@rauhul
Copy link
Collaborator Author

rauhul commented Sep 27, 2023

This doesn't yet have complete tests, nor does it incorporate @jckarter's suggestions. These may come in a future PR, right now I'm trying to land on a good enough API for initial users, before polishing up the internals.

@rauhul
Copy link
Collaborator Author

rauhul commented Sep 27, 2023

The protocol hierarchy also feels like a mess...

\(declAccessLevel) init(unsafeAddress: UInt) { self.unsafeAddress = unsafeAddress }
""",
"\(declAccessLevel) var unsafeAddress: UInt",
"\(declAccessLevel) init(unsafeAddress: UInt) { self.unsafeAddress = unsafeAddress }",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this can be uniformly UInt some registers will be defined as UInt8, UInt16, UInt32 or Uint64

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: This is the address of the register, not the value of the register itself

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should it then contain the size in the initializer?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean the macro arguments?

case reserved = "Reserved"
case readWrite = "ReadWrite"
case readOnly = "ReadOnly"
case writeOnly = "WriteOnly"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about writeOnce and readWriteOnce types?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forthcoming...

- Rewrite diagnostics system
- Add `require` helpers methods which throw on error and emit
  diagnostics
- Refactors macros to leverage `require` methods
- Expands MacroArgumentParser to emit better diagnostics and fixits
@rauhul rauhul marked this pull request as ready for review October 5, 2023 22:44
@rauhul rauhul changed the title WIP: Register and bitfield macro Initial Register and BitField macros Oct 5, 2023
- make swift-format happier (not 100%)
- file some bugs and update fixmes to reference them
@rauhul rauhul merged commit ea0e282 into main Oct 6, 2023
@rauhul rauhul deleted the register-macros branch October 6, 2023 20:39
rauhul added a commit that referenced this pull request Nov 17, 2023
Add initial Register and BitField macros

Adds an initial set of macros for declaring symmetric and asymmetric
registers composed of bit fields. The new `@Register` macro can be
applied to a struct only containing stored properties marked with one of
the new bit field macros: `@Reserved`, `@ReadWrite`, `@ReadOnly`, or
`@WriteOnly`.

The bit field macros take bit range argument which the register macro
uses to create three views of the register. The first is a raw view
which provides untyped access to the bits of the register. The second is
a read view which only provides typed getters for the readable bit
fields. The last is a write view which provides setters.

Additional bit field macros such as `@Write1Clear` will be introduced in
a future PR.

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

var example = Register<Example>(...)
example.modify { $0.en = 0x1 }
```

Miscellaneous changes

Rewrites diagnostics system leveraging a new `MacroContext` type which
handles prefixing diagnostics with the name of the macro which produced
them.

Adds helper methods prefixed by "require" to swift-syntax types which
take a macro context, emit a diagnostic and throw `ExpansionError` if
the requirement is not met.

Adds MMIOKindMacro protocol wrappers which suppresses thrown
`ExpansionErrors` to allow macros to be written as simpler straight line
code e.g. `try value.requireSomething(context)`.

Replaces `MacroArgumentParser` with `ParsableMacro` leveraging
`MacroContext` which emits better diagnostics and fix-its.
rauhul added a commit that referenced this pull request Nov 17, 2023
Add initial Register and BitField macros

Adds an initial set of macros for declaring symmetric and asymmetric
registers composed of bit fields. The new `@Register` macro can be
applied to a struct only containing stored properties marked with one of
the new bit field macros: `@Reserved`, `@ReadWrite`, `@ReadOnly`, or
`@WriteOnly`.

The bit field macros take bit range argument which the register macro
uses to create three views of the register. The first is a raw view
which provides untyped access to the bits of the register. The second is
a read view which only provides typed getters for the readable bit
fields. The last is a write view which provides setters.

Additional bit field macros such as `@Write1Clear` will be introduced in
a future PR.

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

var example = Register<Example>(...)
example.modify { $0.en = 0x1 }
```

Miscellaneous changes

Rewrites diagnostics system leveraging a new `MacroContext` type which
handles prefixing diagnostics with the name of the macro which produced
them.

Adds helper methods prefixed by "require" to swift-syntax types which
take a macro context, emit a diagnostic and throw `ExpansionError` if
the requirement is not met.

Adds MMIOKindMacro protocol wrappers which suppresses thrown
`ExpansionErrors` to allow macros to be written as simpler straight line
code e.g. `try value.requireSomething(context)`.

Replaces `MacroArgumentParser` with `ParsableMacro` leveraging
`MacroContext` which emits better diagnostics and fix-its.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants