@@ -99,6 +99,40 @@ bits match
99
99
case _ => y := x
100
100
```
101
101
102
+ 6 . ** Bit Extractor Patterns** :
103
+ ``` scala
104
+ // Match and extract a 32-bit section between DEAD and BEEF
105
+ y match
106
+ case h " DEAD ${secret : B [32 ]}BEEF " =>
107
+ // Use extracted secret bits
108
+
109
+ // Extract multiple sections
110
+ y match
111
+ case h " DE ${part1 : B [16 ]}AD ${part2 : B [16 ]}BEEF " =>
112
+ // Use part1 and part2 bits
113
+
114
+ // Store extracted bits in variables
115
+ val h " DEAD ${extracted : B [32 ]}BEEF " = input : @ unchecked
116
+
117
+ // Extract multiple sections into variables
118
+ val h " DE ${first : B [16 ]}ADBE ${second : B [16 ]}EF " = input : @ unchecked
119
+
120
+ // Using guards with extracted bit fields
121
+ y match
122
+ case h " DEAD ${secret : B [32 ]}BEEF " if secret > h " 20000000 " =>
123
+ // Match when the secret section is greater than 0x20000000
124
+ case h " DE ${part1 : B [16 ]}AD ${part2 : B [16 ]}BEEF " if part1 == h " FFFF " =>
125
+ // Match when part1 is all ones
126
+ ```
127
+
128
+ Bit extractor patterns allow you to:
129
+ - Match specific bit patterns while extracting variable sections
130
+ - Use hex or binary notation for the fixed parts
131
+ - Specify the width of extracted sections using ` B[width] ` syntax
132
+ - Store extracted bits in variables for later use
133
+ - Extract multiple sections in a single pattern match
134
+ - Use guards to add conditions on extracted bit fields
135
+
102
136
### Match to If Conversion
103
137
104
138
During compilation, match expressions are typically converted to if-else chains for hardware implementation. For example:
0 commit comments