Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ extension LoadInst : OnoneSimplifiable, SILCombineSimplifiable {
index < stringLiteral.value.count {

let builder = Builder(before: self, context)
let charLiteral = builder.createIntegerLiteral(Int(stringLiteral.value[index]), type: type)
let charLiteral = builder.createIntegerLiteral(stringLiteral.value[index], type: type)
uses.replaceAll(with: charLiteral, context)
context.erase(instruction: self)
return true
Expand Down
35 changes: 27 additions & 8 deletions SwiftCompilerSources/Sources/SIL/Builder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -190,19 +190,38 @@ public struct Builder {
}
}

private func createIntegerLiteral(_ value: Int, type: Type, treatAsSigned: Bool) -> IntegerLiteralInst {
let literal = bridged.createIntegerLiteral(type.bridged, value, treatAsSigned)
return notifyNew(literal.getAs(IntegerLiteralInst.self))
}
/// Creates a integer literal instruction with the given integer value and
/// type. If an extension is necessary, the value is extended in accordance
/// with the signedness of `Value`.
public func createIntegerLiteral<Value: FixedWidthInteger>(
_ value: Value,
type: Type
) -> IntegerLiteralInst {
precondition(
Value.bitWidth <= Int.bitWidth,
"Cannot fit \(Value.bitWidth)-bit integer into \(Int.bitWidth)-bit 'Swift.Int'"
)
// Extend the value based on its signedness to the bit width of `Int` and
// reinterpret it as an `Int`.
let extendedValue: Int =
if Value.isSigned {
Int(value)
} else {
// NB: This initializer is effectively a generic equivalent
// of `Int(bitPattern:)`
Int(truncatingIfNeeded: value)
}

public func createIntegerLiteral(_ value: Int, type: Type) -> IntegerLiteralInst {
createIntegerLiteral(value, type: type, treatAsSigned: true)
let literal = bridged.createIntegerLiteral(type.bridged, extendedValue, Value.isSigned)
return notifyNew(literal.getAs(IntegerLiteralInst.self))
}

/// Creates a `Builtin.Int1` integer literal instruction with the given value.
/// Creates a `Builtin.Int1` integer literal instruction with a value
/// corresponding to the given Boolean.
public func createBoolLiteral(_ value: Bool) -> IntegerLiteralInst {
let boolType = notificationHandler.getBuiltinIntegerType(1).type
return createIntegerLiteral(value ? 1 : 0, type: boolType, treatAsSigned: false)
let integerValue: UInt = value ? 1 : 0
return createIntegerLiteral(integerValue, type: boolType)
}

public func createAllocRef(_ type: Type, isObjC: Bool = false, canAllocOnStack: Bool = false, isBare: Bool = false,
Expand Down