Skip to content

Commit db03a55

Browse files
committed
Swift SIL: add some Builder APIs
* `createCheckedCastAddrBranch` * `createUnconditionalCheckedCastAddr` * `createDebugValue` * `createWitnessMethod` * `createInitEnumDataAddr`
1 parent f7466ea commit db03a55

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed

SwiftCompilerSources/Sources/SIL/Builder.swift

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,38 @@ public struct Builder {
175175
let cast = bridged.createUpcast(value.bridged, type.bridged)
176176
return notifyNew(cast.getAs(UpcastInst.self))
177177
}
178+
179+
@discardableResult
180+
public func createCheckedCastAddrBranch(
181+
source: Value, sourceFormalType: CanonicalType,
182+
destination: Value, targetFormalType: CanonicalType,
183+
consumptionKind: CheckedCastAddrBranchInst.CastConsumptionKind,
184+
successBlock: BasicBlock,
185+
failureBlock: BasicBlock
186+
) -> CheckedCastAddrBranchInst {
187+
188+
let bridgedConsumption: BridgedInstruction.CastConsumptionKind
189+
switch consumptionKind {
190+
case .TakeAlways: bridgedConsumption = .TakeAlways
191+
case .TakeOnSuccess: bridgedConsumption = .TakeOnSuccess
192+
case .CopyOnSuccess: bridgedConsumption = .CopyOnSuccess
193+
}
194+
let cast = bridged.createCheckedCastAddrBranch(source.bridged, sourceFormalType.bridged,
195+
destination.bridged, targetFormalType.bridged,
196+
bridgedConsumption,
197+
successBlock.bridged, failureBlock.bridged)
198+
return notifyNew(cast.getAs(CheckedCastAddrBranchInst.self))
199+
}
200+
201+
@discardableResult
202+
public func createUnconditionalCheckedCastAddr(
203+
source: Value, sourceFormalType: CanonicalType,
204+
destination: Value, targetFormalType: CanonicalType
205+
) -> UnconditionalCheckedCastAddrInst {
206+
let cast = bridged.createUnconditionalCheckedCastAddr(source.bridged, sourceFormalType.bridged,
207+
destination.bridged, targetFormalType.bridged)
208+
return notifyNew(cast.getAs(UnconditionalCheckedCastAddrInst.self))
209+
}
178210

179211
public func createLoad(fromAddress: Value, ownership: LoadInst.LoadOwnership) -> LoadInst {
180212
let load = bridged.createLoad(fromAddress.bridged, ownership.rawValue)
@@ -285,6 +317,11 @@ public struct Builder {
285317
return notifyNew(bridged.createEndLifetime(value.bridged).getAs(EndLifetimeInst.self))
286318
}
287319

320+
@discardableResult
321+
public func createDebugValue(value: Value, debugVariable: DebugVariableInstruction.DebugVariable) -> DebugValueInst {
322+
return notifyNew(bridged.createDebugValue(value.bridged, debugVariable).getAs(DebugValueInst.self))
323+
}
324+
288325
@discardableResult
289326
public func createDebugStep() -> DebugStepInst {
290327
return notifyNew(bridged.createDebugStep().getAs(DebugStepInst.self))
@@ -324,6 +361,14 @@ public struct Builder {
324361
return notifyNew(apply.getAs(TryApplyInst.self))
325362
}
326363

364+
public func createWitnessMethod(lookupType: CanonicalType,
365+
conformance: Conformance,
366+
member: DeclRef,
367+
methodType: Type) -> WitnessMethodInst {
368+
return notifyNew(bridged.createWitnessMethod(lookupType.bridged, conformance.bridged,
369+
member.bridged, methodType.bridged).getAs(WitnessMethodInst.self))
370+
}
371+
327372
@discardableResult
328373
public func createReturn(of value: Value) -> ReturnInst {
329374
return notifyNew(bridged.createReturn(value.bridged).getAs(ReturnInst.self))
@@ -347,6 +392,11 @@ public struct Builder {
347392
return notifyNew(uteda.getAs(UncheckedTakeEnumDataAddrInst.self))
348393
}
349394

395+
public func createInitEnumDataAddr(enumAddress: Value, caseIndex: Int, type: Type) -> InitEnumDataAddrInst {
396+
let uteda = bridged.createInitEnumDataAddr(enumAddress.bridged, caseIndex, type.bridged)
397+
return notifyNew(uteda.getAs(InitEnumDataAddrInst.self))
398+
}
399+
350400
public func createEnum(caseIndex: Int, payload: Value?, enumType: Type) -> EnumInst {
351401
let enumInst = bridged.createEnum(caseIndex, payload.bridged, enumType.bridged)
352402
return notifyNew(enumInst.getAs(EnumInst.self))

include/swift/SIL/SILBridging.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,6 +1135,14 @@ struct BridgedBuilder{
11351135
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedInstruction createUncheckedAddrCast(BridgedValue op,
11361136
BridgedType type) const;
11371137
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedInstruction createUpcast(BridgedValue op, BridgedType type) const;
1138+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedInstruction createCheckedCastAddrBranch(
1139+
BridgedValue source, BridgedCanType sourceFormalType,
1140+
BridgedValue destination, BridgedCanType targetFormalType,
1141+
BridgedInstruction::CastConsumptionKind consumptionKind,
1142+
BridgedBasicBlock successBlock, BridgedBasicBlock failureBlock) const;
1143+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedInstruction createUnconditionalCheckedCastAddr(
1144+
BridgedValue source, BridgedCanType sourceFormalType,
1145+
BridgedValue destination, BridgedCanType targetFormalType) const;
11381146
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedInstruction createLoad(BridgedValue op, SwiftInt ownership) const;
11391147
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedInstruction createLoadBorrow(BridgedValue op) const;
11401148
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedInstruction createBeginDeallocRef(BridgedValue reference,
@@ -1162,6 +1170,8 @@ struct BridgedBuilder{
11621170
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedInstruction createDestroyValue(BridgedValue op) const;
11631171
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedInstruction createDestroyAddr(BridgedValue op) const;
11641172
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedInstruction createEndLifetime(BridgedValue op) const;
1173+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedInstruction createDebugValue(BridgedValue src,
1174+
BridgedSILDebugVariable var) const;
11651175
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedInstruction createDebugStep() const;
11661176
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedInstruction createApply(BridgedValue function,
11671177
BridgedSubstitutionMap subMap,
@@ -1173,6 +1183,9 @@ struct BridgedBuilder{
11731183
BridgedBasicBlock normalBB, BridgedBasicBlock errorBB,
11741184
bool isNonAsync,
11751185
BridgedGenericSpecializationInformation specInfo) const;
1186+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedInstruction createWitnessMethod(BridgedCanType lookupType,
1187+
BridgedConformance conformance,
1188+
BridgedDeclRef member, BridgedType methodType) const;
11761189
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedInstruction createReturn(BridgedValue op) const;
11771190
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedInstruction createThrow(BridgedValue op) const;
11781191
SWIFT_IMPORT_UNSAFE BridgedInstruction createSwitchEnumInst(BridgedValue enumVal,
@@ -1185,6 +1198,9 @@ struct BridgedBuilder{
11851198
SwiftInt caseIdx, BridgedType resultType) const;
11861199
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedInstruction createUncheckedTakeEnumDataAddr(BridgedValue enumAddr,
11871200
SwiftInt caseIdx) const;
1201+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedInstruction createInitEnumDataAddr(BridgedValue enumAddr,
1202+
SwiftInt caseIdx,
1203+
BridgedType type) const;
11881204
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedInstruction createEnum(SwiftInt caseIdx, OptionalBridgedValue payload,
11891205
BridgedType resultType) const;
11901206
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedInstruction createThinToThickFunction(BridgedValue fn,

include/swift/SIL/SILBridgingImpl.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2128,6 +2128,28 @@ BridgedInstruction BridgedBuilder::createUpcast(BridgedValue op, BridgedType typ
21282128
type.unbridged())};
21292129
}
21302130

2131+
BridgedInstruction BridgedBuilder::createCheckedCastAddrBranch(
2132+
BridgedValue source, BridgedCanType sourceFormalType,
2133+
BridgedValue destination, BridgedCanType targetFormalType,
2134+
BridgedInstruction::CastConsumptionKind consumptionKind,
2135+
BridgedBasicBlock successBlock, BridgedBasicBlock failureBlock) const
2136+
{
2137+
return {unbridged().createCheckedCastAddrBranch(
2138+
regularLoc(), (swift::CastConsumptionKind)consumptionKind,
2139+
source.getSILValue(), sourceFormalType.unbridged(),
2140+
destination.getSILValue(), targetFormalType.unbridged(),
2141+
successBlock.unbridged(), failureBlock.unbridged())};
2142+
}
2143+
2144+
BridgedInstruction BridgedBuilder::createUnconditionalCheckedCastAddr(
2145+
BridgedValue source, BridgedCanType sourceFormalType,
2146+
BridgedValue destination, BridgedCanType targetFormalType) const
2147+
{
2148+
return {unbridged().createUnconditionalCheckedCastAddr(
2149+
regularLoc(), source.getSILValue(), sourceFormalType.unbridged(),
2150+
destination.getSILValue(), targetFormalType.unbridged())};
2151+
}
2152+
21312153
BridgedInstruction BridgedBuilder::createLoad(BridgedValue op, SwiftInt ownership) const {
21322154
return {unbridged().createLoad(regularLoc(), op.getSILValue(),
21332155
(swift::LoadOwnershipQualifier)ownership)};
@@ -2226,6 +2248,11 @@ BridgedInstruction BridgedBuilder::createEndLifetime(BridgedValue op) const {
22262248
return {unbridged().createEndLifetime(regularLoc(), op.getSILValue())};
22272249
}
22282250

2251+
BridgedInstruction BridgedBuilder::createDebugValue(BridgedValue op,
2252+
BridgedSILDebugVariable var) const {
2253+
return {unbridged().createDebugValue(regularLoc(), op.getSILValue(), var.unbridge())};
2254+
}
2255+
22292256
BridgedInstruction BridgedBuilder::createDebugStep() const {
22302257
return {unbridged().createDebugStep(regularLoc())};
22312258
}
@@ -2257,6 +2284,14 @@ BridgedInstruction BridgedBuilder::createTryApply(BridgedValue function, Bridged
22572284
arguments.getValues(argValues), normalBB.unbridged(), errorBB.unbridged(), applyOpts, specInfo.data)};
22582285
}
22592286

2287+
BridgedInstruction BridgedBuilder::createWitnessMethod(BridgedCanType lookupType,
2288+
BridgedConformance conformance,
2289+
BridgedDeclRef member, BridgedType methodType) const {
2290+
return {unbridged().createWitnessMethod(regularLoc(), lookupType.unbridged(), conformance.unbridged(),
2291+
member.unbridged(), methodType.unbridged())};
2292+
}
2293+
2294+
22602295
BridgedInstruction BridgedBuilder::createReturn(BridgedValue op) const {
22612296
return {unbridged().createReturn(regularLoc(), op.getSILValue())};
22622297
}
@@ -2278,6 +2313,14 @@ BridgedInstruction BridgedBuilder::createUncheckedTakeEnumDataAddr(BridgedValue
22782313
return {unbridged().createUncheckedTakeEnumDataAddr(regularLoc(), en, en->getType().getEnumElement(caseIdx))};
22792314
}
22802315

2316+
BridgedInstruction BridgedBuilder::createInitEnumDataAddr(BridgedValue enumAddr,
2317+
SwiftInt caseIdx,
2318+
BridgedType type) const {
2319+
swift::SILValue en = enumAddr.getSILValue();
2320+
return {unbridged().createInitEnumDataAddr(regularLoc(), en, en->getType().getEnumElement(caseIdx),
2321+
type.unbridged())};
2322+
}
2323+
22812324
BridgedInstruction BridgedBuilder::createEnum(SwiftInt caseIdx, OptionalBridgedValue payload,
22822325
BridgedType resultType) const {
22832326
swift::EnumElementDecl *caseDecl =

0 commit comments

Comments
 (0)