|
| 1 | +//===--- Builder.swift - Building and modifying SIL ----------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +import SILBridging |
| 14 | + |
| 15 | +/// A utility to create new instructions at a given insertion point. |
| 16 | +public struct Builder { |
| 17 | + let insertionPoint: Instruction |
| 18 | + let location: Location |
| 19 | + private let passContext: BridgedPassContext |
| 20 | + |
| 21 | + private var bridgedInsPoint: BridgedInstruction { insertionPoint.bridged } |
| 22 | + |
| 23 | + private func notifyInstructionsChanged() { |
| 24 | + PassContext_notifyChanges(passContext, instructionsChanged) |
| 25 | + } |
| 26 | + |
| 27 | + private func notifyCallsChanged() { |
| 28 | + PassContext_notifyChanges(passContext, callsChanged) |
| 29 | + } |
| 30 | + |
| 31 | + private func notifyBranchesChanged() { |
| 32 | + PassContext_notifyChanges(passContext, branchesChanged) |
| 33 | + } |
| 34 | + |
| 35 | + public init(insertionPoint: Instruction, location: Location, |
| 36 | + passContext: BridgedPassContext) { |
| 37 | + self.insertionPoint = insertionPoint |
| 38 | + self.location = location; |
| 39 | + self.passContext = passContext |
| 40 | + } |
| 41 | + |
| 42 | + public func createBuiltinBinaryFunction(name: String, |
| 43 | + operandType: Type, resultType: Type, arguments: [Value]) -> BuiltinInst { |
| 44 | + notifyInstructionsChanged() |
| 45 | + return arguments.withBridgedValues { valuesRef in |
| 46 | + return name.withBridgedStringRef { nameStr in |
| 47 | + let bi = SILBuilder_createBuiltinBinaryFunction( |
| 48 | + bridgedInsPoint, location.bridgedLocation, nameStr, |
| 49 | + operandType.silType, resultType.silType, valuesRef) |
| 50 | + return bi.getAs(BuiltinInst.self) |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + public func createCondFail(condition: Value, message: String) -> CondFailInst { |
| 56 | + notifyInstructionsChanged() |
| 57 | + return message.withBridgedStringRef { messageStr in |
| 58 | + let cf = SILBuilder_createCondFail( |
| 59 | + bridgedInsPoint, location.bridgedLocation, condition.bridged, messageStr) |
| 60 | + return cf.getAs(CondFailInst.self) |
| 61 | + } |
| 62 | + } |
| 63 | +} |
0 commit comments