Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save norio-nomura/9a27288e1d6b0b5f2a35878d40e3ee1e to your computer and use it in GitHub Desktop.
Save norio-nomura/9a27288e1d6b0b5f2a35878d40e3ee1e to your computer and use it in GitHub Desktop.
How is `StaticString` created?
$ echo 'func f(_ s: StaticString) { print(s) }; f("test")'|swiftc - -emit-assembly -O|swift demangle
.section __TEXT,__text,regular,pure_instructions
.macosx_version_min 10, 9
.section __TEXT,__literal16,16byte_literals
.p2align 4
LCPI0_0:
.quad 1
.quad 2
.section __TEXT,__text,regular,pure_instructions
.globl _main
.p2align 4, 0x90
_main:
.cfi_startproc
pushq %rbp
Ltmp0:
.cfi_def_cfa_offset 16
Ltmp1:
.cfi_offset %rbp, -16
movq %rsp, %rbp
Ltmp2:
.cfi_def_cfa_register %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $16, %rsp
Ltmp3:
.cfi_offset %rbx, -48
Ltmp4:
.cfi_offset %r12, -40
Ltmp5:
.cfi_offset %r14, -32
Ltmp6:
.cfi_offset %r15, -24
movq _lazy cache variable for type metadata for Swift._ContiguousArrayStorage<Any>(%rip), %rdi
testq %rdi, %rdi
jne LBB0_4
movq _lazy cache variable for type metadata for Any(%rip), %rdi
testq %rdi, %rdi
jne LBB0_3
leaq -40(%rbp), %rsi
xorl %edi, %edi
callq _swift_rt_swift_getExistentialTypeMetadata
movq %rax, %rdi
movq %rdi, _lazy cache variable for type metadata for Any(%rip)
LBB0_3:
callq _type metadata accessor for Swift._ContiguousArrayStorage
movq %rax, %rdi
movq %rdi, _lazy cache variable for type metadata for Swift._ContiguousArrayStorage<Any>(%rip)
LBB0_4:
movl $64, %esi
movl $7, %edx
callq _swift_rt_swift_allocObject
movq %rax, %rbx
movaps LCPI0_0(%rip), %xmm0
movups %xmm0, 16(%rbx)
movq _type metadata for Swift.StaticString@GOTPCREL(%rip), %rax
movq %rax, 56(%rbx)
leaq L___unnamed_1(%rip), %rax
movd %rax, %xmm0
movl $4, %eax
movd %rax, %xmm1
punpcklqdq %xmm1, %xmm0
movdqu %xmm0, 32(%rbx)
movb $2, 48(%rbx)
callq _Swift.(print ([Any], separator : Swift.String, terminator : Swift.String) -> ()).(default argument 1)
movq %rax, %r14
movq %rdx, %r15
movq %rcx, %r12
callq _Swift.(print ([Any], separator : Swift.String, terminator : Swift.String) -> ()).(default argument 2)
movq %rdx, %r9
movq %rcx, (%rsp)
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
movq %r12, %rcx
movq %rax, %r8
callq _Swift.print ([Any], separator : Swift.String, terminator : Swift.String) -> ()
xorl %eax, %eax
addq $16, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
.cfi_endproc
.private_extern _swift_rt_swift_getExistentialTypeMetadata
.globl _swift_rt_swift_getExistentialTypeMetadata
.weak_def_can_be_hidden _swift_rt_swift_getExistentialTypeMetadata
.p2align 4, 0x90
_swift_rt_swift_getExistentialTypeMetadata:
movq __swift_getExistentialTypeMetadata@GOTPCREL(%rip), %rax
movq (%rax), %rax
jmpq *%rax
.private_extern _swift_rt_swift_allocObject
.globl _swift_rt_swift_allocObject
.weak_def_can_be_hidden _swift_rt_swift_allocObject
.p2align 4, 0x90
_swift_rt_swift_allocObject:
movq __swift_allocObject@GOTPCREL(%rip), %rax
movq (%rax), %rax
jmpq *%rax
.section __TEXT,__literal16,16byte_literals
.p2align 4
LCPI3_0:
.quad 1
.quad 2
.section __TEXT,__text,regular,pure_instructions
.private_extern _main.f (Swift.StaticString) -> ()
.globl _main.f (Swift.StaticString) -> ()
.p2align 4, 0x90
_main.f (Swift.StaticString) -> ():
.cfi_startproc
pushq %rbp
Ltmp7:
.cfi_def_cfa_offset 16
Ltmp8:
.cfi_offset %rbp, -16
movq %rsp, %rbp
Ltmp9:
.cfi_def_cfa_register %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $16, %rsp
Ltmp10:
.cfi_offset %rbx, -48
Ltmp11:
.cfi_offset %r12, -40
Ltmp12:
.cfi_offset %r14, -32
Ltmp13:
.cfi_offset %r15, -24
movl %edx, %r14d
movq %rsi, %r15
movq %rdi, %r12
movq _lazy cache variable for type metadata for Swift._ContiguousArrayStorage<Any>(%rip), %rdi
testq %rdi, %rdi
jne LBB3_4
movq _lazy cache variable for type metadata for Any(%rip), %rdi
testq %rdi, %rdi
jne LBB3_3
leaq -40(%rbp), %rsi
xorl %edi, %edi
callq _swift_rt_swift_getExistentialTypeMetadata
movq %rax, %rdi
movq %rdi, _lazy cache variable for type metadata for Any(%rip)
LBB3_3:
callq _type metadata accessor for Swift._ContiguousArrayStorage
movq %rax, %rdi
movq %rdi, _lazy cache variable for type metadata for Swift._ContiguousArrayStorage<Any>(%rip)
LBB3_4:
movl $64, %esi
movl $7, %edx
callq _swift_rt_swift_allocObject
movq %rax, %rbx
movaps LCPI3_0(%rip), %xmm0
movups %xmm0, 16(%rbx)
movq _type metadata for Swift.StaticString@GOTPCREL(%rip), %rax
movq %rax, 56(%rbx)
movq %r12, 32(%rbx)
movq %r15, 40(%rbx)
movb %r14b, 48(%rbx)
callq _Swift.(print ([Any], separator : Swift.String, terminator : Swift.String) -> ()).(default argument 1)
movq %rax, %r14
movq %rdx, %r15
movq %rcx, %r12
callq _Swift.(print ([Any], separator : Swift.String, terminator : Swift.String) -> ()).(default argument 2)
movq %rdx, %r9
movq %rcx, (%rsp)
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
movq %r12, %rcx
movq %rax, %r8
callq _Swift.print ([Any], separator : Swift.String, terminator : Swift.String) -> ()
addq $16, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
.cfi_endproc
.section __TEXT,__cstring,cstring_literals
L___unnamed_1:
.asciz "test"
.private_extern _lazy cache variable for type metadata for Swift._ContiguousArrayStorage<Any>
.section __DATA,__data
.globl _lazy cache variable for type metadata for Swift._ContiguousArrayStorage<Any>
.weak_definition _lazy cache variable for type metadata for Swift._ContiguousArrayStorage<Any>
.p2align 3
_lazy cache variable for type metadata for Swift._ContiguousArrayStorage<Any>:
.quad 0
.private_extern _lazy cache variable for type metadata for Any
.globl _lazy cache variable for type metadata for Any
.weak_definition _lazy cache variable for type metadata for Any
.p2align 3
_lazy cache variable for type metadata for Any:
.quad 0
.private_extern ___swift_reflection_version
.section __TEXT,__const
.globl ___swift_reflection_version
.weak_definition ___swift_reflection_version
.p2align 1
___swift_reflection_version:
.short 1
.no_dead_strip ___swift_reflection_version
.linker_option "-lswiftCore"
.linker_option "-lobjc"
.section __DATA,__objc_imageinfo,regular,no_dead_strip
L_OBJC_IMAGE_INFO:
.long 0
.long 1088
.subsections_via_symbols
$ echo 'let s: StaticString = "test"'|swiftc - -emit-sil|swift demangle
sil_stage canonical
import Builtin
import Swift
import SwiftShims
// s
sil_global hidden [let] @main.s : Swift.StaticString : $StaticString
sil_scope 1 { parent @main : $@convention(c) (Int32, UnsafeMutablePointer<Optional<UnsafeMutablePointer<Int8>>>) -> Int32 }
// main
sil @main : $@convention(c) (Int32, UnsafeMutablePointer<Optional<UnsafeMutablePointer<Int8>>>) -> Int32 {
bb0(%0 : $Int32, %1 : $UnsafeMutablePointer<Optional<UnsafeMutablePointer<Int8>>>):
alloc_global @main.s : Swift.StaticString, loc "<stdin>":1:5, scope 1 // id: %2
%3 = global_addr @main.s : Swift.StaticString : $*StaticString, loc "<stdin>":1:5, scope 1 // user: %12
%4 = string_literal utf8 "test", loc "<stdin>":1:23, scope 1 // user: %9
%5 = integer_literal $Builtin.Word, 4, loc "<stdin>":1:23, scope 1 // user: %10
br bb1, loc "<stdin>":1:23, scope 1 // id: %6
bb1: // Preds: bb0
%7 = integer_literal $Builtin.Int8, 2, loc "<stdin>":1:23, scope 1 // user: %10
br bb2, loc "<stdin>":1:23, scope 1 // id: %8
bb2: // Preds: bb1
%9 = builtin "ptrtoint_Word"(%4 : $Builtin.RawPointer) : $Builtin.Word, loc "<stdin>":1:23, scope 1 // user: %10
%10 = struct $StaticString (%9 : $Builtin.Word, %5 : $Builtin.Word, %7 : $Builtin.Int8), loc "<stdin>":1:23, scope 1 // user: %12
br bb3, loc "<stdin>":1:23, scope 1 // id: %11
bb3: // Preds: bb2
store %10 to %3 : $*StaticString, loc "<stdin>":1:23, scope 1 // id: %12
%13 = integer_literal $Builtin.Int32, 0, scope 1 // user: %14
%14 = struct $Int32 (%13 : $Builtin.Int32), scope 1 // user: %15
return %14 : $Int32, scope 1 // id: %15
} // end sil function 'main'
sil_scope 2 { parent @Swift.StaticString.init (_builtinStringLiteral : Builtin.RawPointer, utf8CodeUnitCount : Builtin.Word, isASCII : Builtin.Int1) -> Swift.StaticString : $@convention(method) (Builtin.RawPointer, Builtin.Word, Builtin.Int1, @thin StaticString.Type) -> StaticString }
// StaticString.init(_builtinStringLiteral : Builtin.RawPointer, utf8CodeUnitCount : Builtin.Word, isASCII : Builtin.Int1) -> StaticString
sil public_external [transparent] [fragile] [readonly] @Swift.StaticString.init (_builtinStringLiteral : Builtin.RawPointer, utf8CodeUnitCount : Builtin.Word, isASCII : Builtin.Int1) -> Swift.StaticString : $@convention(method) (Builtin.RawPointer, Builtin.Word, Builtin.Int1, @thin StaticString.Type) -> StaticString {
// %0 // user: %10
// %1 // user: %11
// %2 // user: %4
bb0(%0 : $Builtin.RawPointer, %1 : $Builtin.Word, %2 : $Builtin.Int1, %3 : $@thin StaticString.Type):
cond_br %2, bb2, bb1, scope 2 // id: %4
bb1: // Preds: bb0
%5 = integer_literal $Builtin.Int8, 0, scope 2 // user: %6
br bb3(%5 : $Builtin.Int8), scope 2 // id: %6
bb2: // Preds: bb0
%7 = integer_literal $Builtin.Int8, 2, scope 2 // user: %8
br bb3(%7 : $Builtin.Int8), scope 2 // id: %8
// %9 // user: %11
bb3(%9 : $Builtin.Int8): // Preds: bb2 bb1
%10 = builtin "ptrtoint_Word"(%0 : $Builtin.RawPointer) : $Builtin.Word, scope 2 // user: %11
%11 = struct $StaticString (%10 : $Builtin.Word, %1 : $Builtin.Word, %9 : $Builtin.Int8), scope 2 // user: %12
return %11 : $StaticString, scope 2 // id: %12
} // end sil function 'Swift.StaticString.init (_builtinStringLiteral : Builtin.RawPointer, utf8CodeUnitCount : Builtin.Word, isASCII : Builtin.Int1) -> Swift.StaticString'
$ echo 'let s: StaticString = "test"'|swiftc - -emit-sil -O|swift demangle
sil_stage canonical
import Builtin
import Swift
import SwiftShims
// s
sil_global hidden [let] @main.s : Swift.StaticString : $StaticString
sil_scope 1 { parent @main : $@convention(c) (Int32, UnsafeMutablePointer<Optional<UnsafeMutablePointer<Int8>>>) -> Int32 }
// main
sil @main : $@convention(c) (Int32, UnsafeMutablePointer<Optional<UnsafeMutablePointer<Int8>>>) -> Int32 {
bb0(%0 : $Int32, %1 : $UnsafeMutablePointer<Optional<UnsafeMutablePointer<Int8>>>):
alloc_global @main.s : Swift.StaticString, loc "<stdin>":1:5, scope 1 // id: %2
%3 = global_addr @main.s : Swift.StaticString : $*StaticString, loc "<stdin>":1:5, scope 1 // user: %9
%4 = string_literal utf8 "test", loc "<stdin>":1:23, scope 1 // user: %7
%5 = integer_literal $Builtin.Word, 4, loc "<stdin>":1:23, scope 1 // user: %8
%6 = integer_literal $Builtin.Int8, 2, loc "<stdin>":1:23, scope 1 // user: %8
%7 = builtin "ptrtoint_Word"(%4 : $Builtin.RawPointer) : $Builtin.Word, loc "<stdin>":1:23, scope 1 // user: %8
%8 = struct $StaticString (%7 : $Builtin.Word, %5 : $Builtin.Word, %6 : $Builtin.Int8), loc "<stdin>":1:23, scope 1 // user: %9
store %8 to %3 : $*StaticString, loc "<stdin>":1:23, scope 1 // id: %9
%10 = integer_literal $Builtin.Int32, 0, scope 1 // user: %11
%11 = struct $Int32 (%10 : $Builtin.Int32), scope 1 // user: %12
return %11 : $Int32, scope 1 // id: %12
} // end sil function 'main'
$ echo 'let s: StaticString = "test"'|swiftc - -emit-assembly|swift demangle
.section __TEXT,__text,regular,pure_instructions
.macosx_version_min 10, 9
.globl _main
.p2align 4, 0x90
_main:
.cfi_startproc
pushq %rbp
Ltmp0:
.cfi_def_cfa_offset 16
Ltmp1:
.cfi_offset %rbp, -16
movq %rsp, %rbp
Ltmp2:
.cfi_def_cfa_register %rbp
movl %edi, -4(%rbp)
movq %rsi, -16(%rbp)
jmp LBB0_2
LBB0_2:
jmp LBB0_3
LBB0_3:
xorl %eax, %eax
leaq L___unnamed_1(%rip), %rcx
movq %rcx, _main.s : Swift.StaticString(%rip)
movq $4, _main.s : Swift.StaticString+8(%rip)
movb $2, _main.s : Swift.StaticString+16(%rip)
popq %rbp
retq
.cfi_endproc
.private_extern _main.s : Swift.StaticString
.globl _main.s : Swift.StaticString
.zerofill __DATA,__common,_main.s : Swift.StaticString,17,3
.section __TEXT,__cstring,cstring_literals
L___unnamed_1:
.asciz "test"
.private_extern ___swift_reflection_version
.section __TEXT,__const
.globl ___swift_reflection_version
.weak_definition ___swift_reflection_version
.p2align 1
___swift_reflection_version:
.short 1
.no_dead_strip ___swift_reflection_version
.linker_option "-lswiftCore"
.linker_option "-lobjc"
.section __DATA,__objc_imageinfo,regular,no_dead_strip
L_OBJC_IMAGE_INFO:
.long 0
.long 1088
.subsections_via_symbols
$ echo 'let s: StaticString = "test"'|swiftc - -emit-assembly -O|swift demangle
.section __TEXT,__text,regular,pure_instructions
.macosx_version_min 10, 9
.globl _main
.p2align 4, 0x90
_main:
pushq %rbp
movq %rsp, %rbp
leaq L___unnamed_1(%rip), %rax
movd %rax, %xmm0
movl $4, %eax
movd %rax, %xmm1
punpcklqdq %xmm1, %xmm0
movdqa %xmm0, _main.s : Swift.StaticString(%rip)
movb $2, _main.s : Swift.StaticString+16(%rip)
xorl %eax, %eax
popq %rbp
retq
.private_extern _main.s : Swift.StaticString
.globl _main.s : Swift.StaticString
.zerofill __DATA,__common,_main.s : Swift.StaticString,17,4
.section __TEXT,__cstring,cstring_literals
L___unnamed_1:
.asciz "test"
.private_extern ___swift_reflection_version
.section __TEXT,__const
.globl ___swift_reflection_version
.weak_definition ___swift_reflection_version
.p2align 1
___swift_reflection_version:
.short 1
.no_dead_strip ___swift_reflection_version
.linker_option "-lswiftCore"
.linker_option "-lobjc"
.section __DATA,__objc_imageinfo,regular,no_dead_strip
L_OBJC_IMAGE_INFO:
.long 0
.long 1088
.subsections_via_symbols
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment