Files
swift-mirror/libswift/Sources/SIL/Builder.swift
Erik Eckstein 27dc5afb10 libswift: add a few utilities
* add the IntegerLiteralInst + the Builder function to create it
* add Value.nonDebugUses
* add a general utility Sequence.isEmpty
* add PassContext.replaceAllUses
* add a function to erase all `debug_value` uses in PassContext.erase
2021-11-29 09:41:05 +01:00

71 lines
2.5 KiB
Swift

//===--- Builder.swift - Building and modifying SIL ----------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import SILBridging
/// A utility to create new instructions at a given insertion point.
public struct Builder {
let insertionPoint: Instruction
let location: Location
private let passContext: BridgedPassContext
private var bridgedInsPoint: BridgedInstruction { insertionPoint.bridged }
private func notifyInstructionsChanged() {
PassContext_notifyChanges(passContext, instructionsChanged)
}
private func notifyCallsChanged() {
PassContext_notifyChanges(passContext, callsChanged)
}
private func notifyBranchesChanged() {
PassContext_notifyChanges(passContext, branchesChanged)
}
public init(insertionPoint: Instruction, location: Location,
passContext: BridgedPassContext) {
self.insertionPoint = insertionPoint
self.location = location;
self.passContext = passContext
}
public func createBuiltinBinaryFunction(name: String,
operandType: Type, resultType: Type, arguments: [Value]) -> BuiltinInst {
notifyInstructionsChanged()
return arguments.withBridgedValues { valuesRef in
return name.withBridgedStringRef { nameStr in
let bi = SILBuilder_createBuiltinBinaryFunction(
bridgedInsPoint, location.bridgedLocation, nameStr,
operandType.bridged, resultType.bridged, valuesRef)
return bi.getAs(BuiltinInst.self)
}
}
}
public func createCondFail(condition: Value, message: String) -> CondFailInst {
notifyInstructionsChanged()
return message.withBridgedStringRef { messageStr in
let cf = SILBuilder_createCondFail(
bridgedInsPoint, location.bridgedLocation, condition.bridged, messageStr)
return cf.getAs(CondFailInst.self)
}
}
public func createIntegerLiteral(_ value: Int, type: Type) -> IntegerLiteralInst {
notifyInstructionsChanged()
let literal = SILBuilder_createIntegerLiteral(
bridgedInsPoint, location.bridgedLocation, type.bridged, value)
return literal.getAs(IntegerLiteralInst.self)
}
}