Files
swift-mirror/SwiftCompilerSources/Sources/SIL/Builder.swift
Erik Eckstein 24b62d4d7c Swift SIL: add a few utilities and instructions
* instructions: function_ref, mark_dependence
* add `BuiltinInst.id`
* add isObjC and canAllocOnStack for alloc_ref and alloc_ref_dynamic
* add `ApplySite::referencedFunction`
* add `Builder.createDeallocStackRef`
* add == and != operators for `Function`
* add `List.first` and `ReverseList.first`
2022-01-12 15:47:15 +01:00

77 lines
2.7 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)
}
public func createDeallocStackRef(_ operand: Value) -> DeallocStackRefInst {
notifyInstructionsChanged()
let dr = SILBuilder_createDeallocStackRef(bridgedInsPoint, location.bridgedLocation, operand.bridged)
return dr.getAs(DeallocStackRefInst.self)
}
}