1
1
use std:: fmt;
2
+ use std:: ops:: Deref ;
2
3
use std:: str:: FromStr ;
3
4
4
5
use itertools:: Itertools as _;
@@ -102,102 +103,52 @@ pub struct IntrinsicType {
102
103
pub target : String ,
103
104
}
104
105
105
- pub trait BaseIntrinsicTypeDefinition {
106
- /// Get the TypeKind for this type, recursing into pointers.
107
- fn kind ( & self ) -> TypeKind ;
108
-
109
- /// Get the size of a single element inside this type, recursing into
110
- /// pointers, i.e. a pointer to a u16 would be 16 rather than the size
111
- /// of a pointer.
112
- fn inner_size ( & self ) -> u32 ;
113
-
114
- fn num_lanes ( & self ) -> u32 ;
115
-
116
- fn num_vectors ( & self ) -> u32 ;
117
-
118
- /// Determine if the type is a simd type, this will treat a type such as
119
- /// `uint64x1` as simd.
120
- fn is_simd ( & self ) -> bool ;
121
-
122
- fn is_ptr ( & self ) -> bool ;
123
-
124
- fn c_scalar_type ( & self ) -> String ;
125
-
126
- fn rust_scalar_type ( & self ) -> String ;
127
-
128
- /// Gets a cast for this type if needs promotion.
129
- /// This is required for 8 bit types due to printing as the 8 bit types use
130
- /// a char and when using that in `std::cout` it will print as a character,
131
- /// which means value of 0 will be printed as a null byte.
132
- ///
133
- /// This is also needed for polynomial types because we want them to be
134
- /// printed as unsigned integers to match Rust's `Debug` impl.
135
- fn c_promotion ( & self ) -> & str ;
136
-
137
- /// Generates an initialiser for an array, which can be used to initialise an argument for the
138
- /// intrinsic call.
139
- ///
140
- /// This is determistic based on the pass number.
141
- ///
142
- /// * `loads`: The number of values that need to be loaded from the argument array
143
- /// * e.g for argument type uint32x2, loads=2 results in a string representing 4 32-bit values
144
- ///
145
- /// Returns a string such as
146
- /// * `{0x1, 0x7F, 0xFF}` if `language` is `Language::C`
147
- /// * `[0x1 as _, 0x7F as _, 0xFF as _]` if `language` is `Language::Rust`
148
- fn populate_random ( & self , indentation : Indentation , loads : u32 , language : & Language ) -> String ;
149
-
150
- fn is_rust_vals_array_const ( & self ) -> bool ;
151
-
152
- fn as_call_param_c ( & self , name : & String ) -> String ;
153
- }
154
-
155
- impl BaseIntrinsicTypeDefinition for IntrinsicType {
156
- fn kind ( & self ) -> TypeKind {
106
+ impl IntrinsicType {
107
+ pub fn kind ( & self ) -> TypeKind {
157
108
self . kind
158
109
}
159
110
160
- fn inner_size ( & self ) -> u32 {
111
+ pub fn inner_size ( & self ) -> u32 {
161
112
if let Some ( bl) = self . bit_len {
162
113
bl
163
114
} else {
164
115
unreachable ! ( "" )
165
116
}
166
117
}
167
118
168
- fn num_lanes ( & self ) -> u32 {
119
+ pub fn num_lanes ( & self ) -> u32 {
169
120
if let Some ( sl) = self . simd_len { sl } else { 1 }
170
121
}
171
122
172
- fn num_vectors ( & self ) -> u32 {
123
+ pub fn num_vectors ( & self ) -> u32 {
173
124
if let Some ( vl) = self . vec_len { vl } else { 1 }
174
125
}
175
126
176
- fn is_simd ( & self ) -> bool {
127
+ pub fn is_simd ( & self ) -> bool {
177
128
self . simd_len . is_some ( ) || self . vec_len . is_some ( )
178
129
}
179
130
180
- fn is_ptr ( & self ) -> bool {
131
+ pub fn is_ptr ( & self ) -> bool {
181
132
self . ptr
182
133
}
183
134
184
- fn c_scalar_type ( & self ) -> String {
135
+ pub fn c_scalar_type ( & self ) -> String {
185
136
format ! (
186
137
"{prefix}{bits}_t" ,
187
138
prefix = self . kind( ) . c_prefix( ) ,
188
139
bits = self . inner_size( )
189
140
)
190
141
}
191
142
192
- fn rust_scalar_type ( & self ) -> String {
143
+ pub fn rust_scalar_type ( & self ) -> String {
193
144
format ! (
194
145
"{prefix}{bits}" ,
195
146
prefix = self . kind( ) . rust_prefix( ) ,
196
147
bits = self . inner_size( )
197
148
)
198
149
}
199
150
200
- fn c_promotion ( & self ) -> & str {
151
+ pub fn c_promotion ( & self ) -> & str {
201
152
match * self {
202
153
IntrinsicType {
203
154
kind,
@@ -225,7 +176,12 @@ impl BaseIntrinsicTypeDefinition for IntrinsicType {
225
176
}
226
177
}
227
178
228
- fn populate_random ( & self , indentation : Indentation , loads : u32 , language : & Language ) -> String {
179
+ pub fn populate_random (
180
+ & self ,
181
+ indentation : Indentation ,
182
+ loads : u32 ,
183
+ language : & Language ,
184
+ ) -> String {
229
185
match self {
230
186
IntrinsicType {
231
187
bit_len : Some ( bit_len @ ( 8 | 16 | 32 | 64 ) ) ,
@@ -293,7 +249,7 @@ impl BaseIntrinsicTypeDefinition for IntrinsicType {
293
249
}
294
250
}
295
251
296
- fn is_rust_vals_array_const ( & self ) -> bool {
252
+ pub fn is_rust_vals_array_const ( & self ) -> bool {
297
253
match self {
298
254
// Floats have to be loaded at runtime for stable NaN conversion.
299
255
IntrinsicType {
@@ -308,7 +264,7 @@ impl BaseIntrinsicTypeDefinition for IntrinsicType {
308
264
}
309
265
}
310
266
311
- fn as_call_param_c ( & self , name : & String ) -> String {
267
+ pub fn as_call_param_c ( & self , name : & String ) -> String {
312
268
if self . ptr {
313
269
format ! ( "&{}" , name)
314
270
} else {
@@ -317,92 +273,24 @@ impl BaseIntrinsicTypeDefinition for IntrinsicType {
317
273
}
318
274
}
319
275
320
- pub trait IntrinsicTypeDefinition : BaseIntrinsicTypeDefinition {
276
+ pub trait IntrinsicTypeDefinition : Deref < Target = IntrinsicType > {
321
277
/// Determines the load function for this type.
322
278
/// can be implemented in an `impl` block
323
- fn get_load_function ( & self , _language : Language ) -> String {
324
- unimplemented ! ( "Different architectures must implement get_load_function!" )
325
- }
279
+ fn get_load_function ( & self , _language : Language ) -> String ;
326
280
327
281
/// can be implemented in an `impl` block
328
- fn get_lane_function ( & self ) -> String {
329
- unimplemented ! ( "Different architectures must implement get_lane_function!" )
330
- }
282
+ fn get_lane_function ( & self ) -> String ;
331
283
332
284
/// can be implemented in an `impl` block
333
- fn from_c ( _s : & str , _target : & String ) -> Result < Box < Self > , String > {
334
- unimplemented ! ( "Different architectures must implement from_c!" )
335
- }
285
+ fn from_c ( _s : & str , _target : & String ) -> Result < Box < Self > , String > ;
336
286
337
287
/// Gets a string containing the typename for this type in C format.
338
288
/// can be directly defined in `impl` blocks
339
- fn c_type ( & self ) -> String {
340
- unimplemented ! ( "Different architectures must implement c_type!" )
341
- }
289
+ fn c_type ( & self ) -> String ;
342
290
343
291
/// can be directly defined in `impl` blocks
344
- fn c_single_vector_type ( & self ) -> String {
345
- unimplemented ! ( "Different architectures must implement c_single_vector_type!" )
346
- }
292
+ fn c_single_vector_type ( & self ) -> String ;
347
293
348
294
/// can be defined in `impl` blocks
349
- fn rust_type ( & self ) -> String {
350
- unimplemented ! ( "Different architectures must implement rust_type!" )
351
- }
352
- }
353
-
354
- /// Defines the basic structure of achitecture-specific derivatives
355
- /// of IntrinsicType.
356
- #[ macro_export]
357
- macro_rules! base_intrinsictype_trait_def_macro {
358
- ( $T: ident) => {
359
- use crate :: common:: intrinsic_helpers:: IntrinsicType ;
360
-
361
- #[ derive( Debug , Clone , PartialEq ) ]
362
- pub struct $T( pub IntrinsicType ) ;
363
-
364
- impl BaseIntrinsicTypeDefinition for $T {
365
- fn kind( & self ) -> TypeKind {
366
- self . 0 . kind( )
367
- }
368
- fn inner_size( & self ) -> u32 {
369
- self . 0 . inner_size( )
370
- }
371
- fn num_lanes( & self ) -> u32 {
372
- self . 0 . num_lanes( )
373
- }
374
- fn num_vectors( & self ) -> u32 {
375
- self . 0 . num_vectors( )
376
- }
377
- fn is_simd( & self ) -> bool {
378
- self . 0 . is_simd( )
379
- }
380
- fn is_ptr( & self ) -> bool {
381
- self . 0 . is_ptr( )
382
- }
383
- fn c_scalar_type( & self ) -> String {
384
- self . 0 . c_scalar_type( )
385
- }
386
- fn rust_scalar_type( & self ) -> String {
387
- self . 0 . rust_scalar_type( )
388
- }
389
- fn c_promotion( & self ) -> & str {
390
- self . 0 . c_promotion( )
391
- }
392
- fn populate_random(
393
- & self ,
394
- indentation: Indentation ,
395
- loads: u32 ,
396
- language: & Language ,
397
- ) -> String {
398
- self . 0 . populate_random( indentation, loads, language)
399
- }
400
- fn is_rust_vals_array_const( & self ) -> bool {
401
- self . 0 . is_rust_vals_array_const( )
402
- }
403
- fn as_call_param_c( & self , name: & String ) -> String {
404
- self . 0 . as_call_param_c( name)
405
- }
406
- }
407
- } ;
295
+ fn rust_type ( & self ) -> String ;
408
296
}
0 commit comments