How to view assembly generated from Swift


Xcode currently does not provide any way to view the generated assembly code from Swift source files within Xcode itself1. You can manually generate the assembly using the xcrun command:

xcrun -sdk $(xcrun --show-sdk-path --sdk iphoneos9.0) swiftc -target arm64-apple-ios9.0 -emit-assembly source.swift > output.s

You will need to determine the values for the –sdk (note that this is the switch within the parentheses) and -target switches. To determine which value to use for –sdk use the xcodebuild command:

xcodebuild -showsdks

You determine the appropriate -target argument value using the <architecture>-<vendor>-<abi> triple e.g.:

armv7-apple-ios9.0

arm64-apple-ios9.0

You can determine this value by looking at the build output from the CompileSwift command’s -target argument. You do this by going to the Report Navigator in Xcode and looking at one of the build reports. You’ll need to click on the expand transcript button on the far right:

Xcode Report Navigator annotated

and then look for the -target argument:

CompileSwift  target

Note that this target is not the name of the target in Xcode.

You can also use xcrun to emit IR (bytecode):

xcrun -sdk $(xcrun --show-sdk-path --sdk iphoneos9.0) swiftc -target arm64-apple-ios9.0 -emit-ir source.swift > output.ir

1: rdar://23399706

The ARM64 (AARCH64) stack
Do we really want an iPad Xcode?

Leave a Reply

Your email address will not be published / Required fields are marked *