File: static_objects.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (61 lines) | stat: -rw-r--r-- 2,629 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// RUN: %target-swift-frontend -target %target-future-triple -parse-as-library %s -O -sil-verify-all -Xllvm -sil-disable-pass=FunctionSignatureOpts -module-name=test -emit-sil | %FileCheck %s
// RUN: %target-swift-frontend -target %target-future-triple -parse-as-library %s -O -sil-verify-all -Xllvm -sil-disable-pass=FunctionSignatureOpts -module-name=test -emit-ir | %FileCheck %s -check-prefix=CHECK-LLVM

// Also do an end-to-end test to check all components, including IRGen.
// RUN: %empty-directory(%t) 
// RUN: %target-build-swift -parse-as-library -O -Xllvm -sil-disable-pass=FunctionSignatureOpts -module-name=test %s -o %t/a.out
// RUN: %target-run %t/a.out | %FileCheck %s -check-prefix=CHECK-OUTPUT
// REQUIRES: executable_test,swift_stdlib_no_asserts,optimized_stdlib
// REQUIRES: CPU=arm64 || CPU=x86_64
// REQUIRES: swift_in_compiler

public class C {
  var x: Int

  init(x: Int) {
    self.x = x
  }
}

public class D: C {}

// CHECK-LABEL: sil_global private @$s4test1c_WZTv_ : $C = {
// CHECK-NEXT:    %0 = integer_literal $Builtin.Int64, 27         // user: %1
// CHECK-NEXT:    %1 = struct $Int (%0 : $Builtin.Int64)          // user: %2
// CHECK-NEXT:    %initval = object $C (%1 : $Int)
// CHECK-NEXT:  }

// CHECK-LLVM-LABEL: @"$s4test1c_WZTv_" ={{.*}} global %{{[a-zA-Z_0-9]*}}c { [1 x i64] zeroinitializer, %{{[a-zA-Z_0-9]*}} <{ %swift.refcounted zeroinitializer, %TSi <{ i64 27 }> }

public let c = C(x: 27)


// CHECK-LABEL: sil [noinline] @$s4test6testitAA1CCyF : $@convention(thin) () -> @owned C {
// CHECK:         [[C:%.*]] = global_value @$s4test1c_WZTv_ : $C
// CHECK-NEXT:    return [[C]]
// CHECK:       } // end sil function '$s4test6testitAA1CCyF'

// CHECK-LLVM-LABEL: define {{.*}} @"$s4test6testitAA1CCyF"
// CHECK-LLVM:         [[C:%.*]] = tail call ptr @swift_initStaticObject({{.*}} getelementptr {{.*}}, ptr @"$s4test1c_WZTv_", i64 0, i32 1))
// CHECK-LLVM-NEXT:    ret ptr [[C]]
// CHECK-LLVM:       }
@inline(never)
public func testit() -> C {
  return c
}

// Cannot allocate a ManagedBuffer in a data section, because it calls malloc_size on the class instance.
// CHECK-LABEL: sil private [global_init_once_fn] @$s4test10managedBuf_WZ :
// CHECK:         alloc_ref [tail_elems $UInt8 * %{{[0-9]*}} : $Builtin.Word] $ManagedBuffer<(), UInt8>
// CHECK:       } // end sil function '$s4test10managedBuf_WZ'
public let managedBuf = ManagedBuffer<Void, UInt8>.create(minimumCapacity: 0, makingHeaderWith: { _ in })

@main struct Main {
  static func main() {
    // CHECK-OUTPUT: c.x=27
    print("c.x=\(testit().x)")
    // CHECK-OUTPUT: c=test.C
    print("c=\(testit())")
  }
}