@@ -13,6 +13,8 @@ public enum Unsigned: IntegralConstantRepresentation {}
1313public enum Signed : IntegralConstantRepresentation { }
1414/// Represents floating types and operations.
1515public enum Floating : ConstantRepresentation { }
16+ /// Represents struct types and operations.
17+ public enum Struct : ConstantRepresentation { }
1618
1719/// A `Constant` represents a value initialized to a constant. Constant values
1820/// may be manipulated with standard Swift arithmetic operations and used with
@@ -28,6 +30,7 @@ public struct Constant<Repr: ConstantRepresentation>: IRValue {
2830 case unsigned
2931 case signed
3032 case floating
33+ case `struct`
3134 }
3235 fileprivate let repr : Representation
3336
@@ -43,6 +46,8 @@ public struct Constant<Repr: ConstantRepresentation>: IRValue {
4346 self . repr = . signed
4447 } else if reprID == ObjectIdentifier ( Floating . self) {
4548 self . repr = . floating
49+ } else if reprID == ObjectIdentifier ( Struct . self) {
50+ self . repr = . struct
4651 } else {
4752 fatalError ( " Invalid representation \( type ( of: Repr . self) ) " )
4853 }
@@ -71,6 +76,8 @@ public struct Constant<Repr: ConstantRepresentation>: IRValue {
7176 return Constant < T > ( llvm: LLVMConstIntCast ( val, type. asLLVM ( ) , /*signed:*/ false . llvm) )
7277 case . floating:
7378 return Constant < T > ( llvm: LLVMConstFPToUI ( val, type. asLLVM ( ) ) )
79+ case . struct:
80+ fatalError ( " Cannot cast struct type to integer type " )
7481 }
7582 } else if destID == ObjectIdentifier ( Signed . self) {
7683 switch self . repr {
@@ -79,6 +86,8 @@ public struct Constant<Repr: ConstantRepresentation>: IRValue {
7986 return Constant < T > ( llvm: LLVMConstIntCast ( val, type. asLLVM ( ) , /*signed:*/ true . llvm) )
8087 case . floating:
8188 return Constant < T > ( llvm: LLVMConstFPToSI ( val, type. asLLVM ( ) ) )
89+ case . struct:
90+ fatalError ( " Cannot cast struct type to integer type " )
8291 }
8392 } else {
8493 fatalError ( " Invalid representation \( type ( of: T . self) ) " )
@@ -100,6 +109,8 @@ public struct Constant<Repr: ConstantRepresentation>: IRValue {
100109 return Constant < Floating > ( llvm: LLVMConstSIToFP ( val, type. asLLVM ( ) ) )
101110 case . floating:
102111 return Constant < Floating > ( llvm: LLVMConstFPCast ( val, type. asLLVM ( ) ) )
112+ case . struct:
113+ fatalError ( " Cannot cast struct type to float type " )
103114 }
104115 }
105116
@@ -152,6 +163,8 @@ public struct Constant<Repr: ConstantRepresentation>: IRValue {
152163 }
153164 case . floating:
154165 return Constant ( llvm: LLVMConstFAdd ( lhsVal, rhsVal) )
166+ case . struct:
167+ fatalError ( " Operation undefined on struct type " )
155168 }
156169 }
157170
@@ -170,6 +183,8 @@ public struct Constant<Repr: ConstantRepresentation>: IRValue {
170183 return Constant ( llvm: LLVMConstAdd ( lhs. llvm, rhs. llvm) )
171184 case . floating:
172185 return Constant ( llvm: LLVMConstFAdd ( lhs. llvm, rhs. llvm) )
186+ case . struct:
187+ fatalError ( " Operation undefined on struct type " )
173188 }
174189 }
175190
@@ -199,6 +214,8 @@ public struct Constant<Repr: ConstantRepresentation>: IRValue {
199214 }
200215 case . floating:
201216 return lhs - rhs
217+ case . struct:
218+ fatalError ( " Operation undefined on struct type " )
202219 }
203220 }
204221
@@ -217,6 +234,8 @@ public struct Constant<Repr: ConstantRepresentation>: IRValue {
217234 return Constant ( llvm: LLVMConstSub ( lhs. llvm, rhs. llvm) )
218235 case . floating:
219236 return Constant ( llvm: LLVMConstFSub ( lhs. llvm, rhs. llvm) )
237+ case . struct:
238+ fatalError ( " Operation undefined on struct type " )
220239 }
221240 }
222241
@@ -246,6 +265,8 @@ public struct Constant<Repr: ConstantRepresentation>: IRValue {
246265 }
247266 case . floating:
248267 return lhs * rhs
268+ case . struct:
269+ fatalError ( " Operation undefined on struct type " )
249270 }
250271 }
251272
@@ -264,6 +285,8 @@ public struct Constant<Repr: ConstantRepresentation>: IRValue {
264285 return Constant ( llvm: LLVMConstMul ( lhs. llvm, rhs. llvm) )
265286 case . floating:
266287 return Constant ( llvm: LLVMConstFMul ( lhs. llvm, rhs. llvm) )
288+ case . struct:
289+ fatalError ( " Operation undefined on struct type " )
267290 }
268291 }
269292
@@ -285,6 +308,8 @@ public struct Constant<Repr: ConstantRepresentation>: IRValue {
285308 return Constant ( llvm: LLVMConstUDiv ( lhs. llvm, rhs. llvm) )
286309 case . floating:
287310 return Constant ( llvm: LLVMConstFDiv ( lhs. llvm, rhs. llvm) )
311+ case . struct:
312+ fatalError ( " Operation undefined on struct type " )
288313 }
289314 }
290315
@@ -306,6 +331,8 @@ public struct Constant<Repr: ConstantRepresentation>: IRValue {
306331 return Constant ( llvm: LLVMConstURem ( lhs. llvm, rhs. llvm) )
307332 case . floating:
308333 return Constant ( llvm: LLVMConstFRem ( lhs. llvm, rhs. llvm) )
334+ case . struct:
335+ fatalError ( " Operation undefined on struct type " )
309336 }
310337 }
311338
@@ -328,6 +355,8 @@ public struct Constant<Repr: ConstantRepresentation>: IRValue {
328355 return Constant < Signed > ( llvm: LLVMConstICmp ( IntPredicate . equal. llvm, lhs. llvm, rhs. llvm) )
329356 case . floating:
330357 return Constant < Signed > ( llvm: LLVMConstFCmp ( RealPredicate . orderedEqual. llvm, lhs. llvm, rhs. llvm) )
358+ case . struct:
359+ fatalError ( " Operation undefined on struct type " )
331360 }
332361 }
333362
@@ -348,6 +377,8 @@ public struct Constant<Repr: ConstantRepresentation>: IRValue {
348377 return Constant < Signed > ( llvm: LLVMConstICmp ( IntPredicate . unsignedLessThan. llvm, lhs. llvm, rhs. llvm) )
349378 case . floating:
350379 return Constant < Signed > ( llvm: LLVMConstFCmp ( RealPredicate . orderedLessThan. llvm, lhs. llvm, rhs. llvm) )
380+ case . struct:
381+ fatalError ( " Operation undefined on struct type " )
351382 }
352383 }
353384
@@ -368,6 +399,8 @@ public struct Constant<Repr: ConstantRepresentation>: IRValue {
368399 return Constant < Signed > ( llvm: LLVMConstICmp ( IntPredicate . unsignedGreaterThan. llvm, lhs. llvm, rhs. llvm) )
369400 case . floating:
370401 return Constant < Signed > ( llvm: LLVMConstFCmp ( RealPredicate . orderedGreaterThan. llvm, lhs. llvm, rhs. llvm) )
402+ case . struct:
403+ fatalError ( " Operation undefined on struct type " )
371404 }
372405 }
373406
@@ -388,6 +421,8 @@ public struct Constant<Repr: ConstantRepresentation>: IRValue {
388421 return Constant < Signed > ( llvm: LLVMConstICmp ( IntPredicate . unsignedLessThanOrEqual. llvm, lhs. llvm, rhs. llvm) )
389422 case . floating:
390423 return Constant < Signed > ( llvm: LLVMConstFCmp ( RealPredicate . orderedLessThanOrEqual. llvm, lhs. llvm, rhs. llvm) )
424+ case . struct:
425+ fatalError ( " Operation undefined on struct type " )
391426 }
392427 }
393428
@@ -408,6 +443,8 @@ public struct Constant<Repr: ConstantRepresentation>: IRValue {
408443 return Constant < Signed > ( llvm: LLVMConstICmp ( IntPredicate . unsignedGreaterThanOrEqual. llvm, lhs. llvm, rhs. llvm) )
409444 case . floating:
410445 return Constant < Signed > ( llvm: LLVMConstFCmp ( RealPredicate . orderedGreaterThanOrEqual. llvm, lhs. llvm, rhs. llvm) )
446+ case . struct:
447+ fatalError ( " Operation undefined on struct type " )
411448 }
412449 }
413450}
@@ -521,3 +558,16 @@ extension Constant where Repr: IntegralConstantRepresentation {
521558 return Constant < T > ( llvm: LLVMConstSelect ( cond. llvm, then. llvm, `else`. llvm) )
522559 }
523560}
561+
562+ // MARK: Struct Operations
563+
564+ extension Constant where Repr == Struct {
565+
566+ public func getElement( indices: [ Int ] ) -> IRValue {
567+ assert ( repr == . struct)
568+ var indices = indices. map ( { UInt32 ( $0) } )
569+ return indices. withUnsafeMutableBufferPointer { buf in
570+ return LLVMConstExtractValue ( asLLVM ( ) , buf. baseAddress, UInt32 ( buf. count) )
571+ }
572+ }
573+ }
0 commit comments