002-Swift进阶-编译简介、sil文件的分析

2020-12-15  本文已影响0人  Stronger_J

一、swift编译简介

IOS开发的语言不管是OC还是swift,后端都是通过LLVM进行编译的,如下:


image.png

可以看到OC是clang编译器,编译IR,然后在生成可执行文件.0(机器码),Swift是通过swift编译器,编译成IR,然后生成可执行文件。


image.png

二、SIL

swift使用的编译器为swiftc,OC使用的为Clang。
swiftc命令查询:swiftc -h
参考视频:链接
参考文档:SIL参考文档

编译成SIL文件:

swiftc -emit-sil main.swift >> main.sil && open main.sil 
//带转译
swiftc -emit-sil main.swift | xcrun swift-demangle >> main.sil && open main.sil 

//IR
swiftc -emit-ir main.swift | xcrun swift-demangle >> main.ll && open main.ll

//UIKit
 swiftc -emit-sil -target x86_64-apple-ios13.5-simulator -sdk $(xcrun --show-sdk-path --sdk iphonesimulator) ViewController.swift > ViewController.sil

swift文件的入口函数


image.png

转译命令xcrun swift-demangle s4main2t1AA6LJTestCvp
寄存器读取:register read register read x8

xcode添加符号断点


image.png

三、HeapObject.cpp中alloc_Object方法

image.png

1、初始化调度

2、打印占用空间大小

import Foundation
class LJTest{
    var age = 18
    var name: String = "swift"
}
var t1 = LJTest()
print(MemoryLayout<String>.stride)          //步长
print(MemoryLayout<String>.size)            //大小
print(class_getInstanceSize(LJTest.self))   //大小

3、类结构探索

static HeapObject *_swift_allocObject_(HeapMetadata const *metadata,
                                       size_t requiredSize,
                                       size_t requiredAlignmentMask) {
  assert(isAlignmentMask(requiredAlignmentMask));
  auto object = reinterpret_cast<HeapObject *>(
      swift_slowAlloc(requiredSize, requiredAlignmentMask));

  // NOTE: this relies on the C++17 guaranteed semantics of no null-pointer
  // check on the placement new allocator which we have observed on Windows,
  // Linux, and macOS.
  new (object) HeapObject(metadata);

  // If leak tracking is enabled, start tracking this object.
  SWIFT_LEAKS_START_TRACKING_OBJECT(object);

  SWIFT_RT_TRACK_INVOCATION(object, swift_allocObject);

  return object;
}

获取当前kind

/// Get the metadata kind.
  MetadataKind getKind() const {
    return getEnumeratedMetadataKind(Kind);
  }

获取类对象
const TargetClassMetadata<Runtime> *getClassObject() const;
getClassObject()方法获取的就是kind类型,通过getKind()方法获取类型(MetadataKind::Class),

template<> inline const ClassMetadata *
  Metadata::getClassObject() const {
    switch (getKind()) {
    case MetadataKind::Class: {
      // Native Swift class metadata is also the class object.
      return static_cast<const ClassMetadata *>(this);
    }
    case MetadataKind::ObjCClassWrapper: {
      // Objective-C class objects are referenced by their Swift metadata wrapper.
      auto wrapper = static_cast<const ObjCClassWrapperMetadata *>(this);
      return wrapper->Class;
    }
    // Other kinds of types don't have class objects.
    default:
      return nullptr;
    }
  }

Metadata结构体:

 struct swift_class_t: NSObject{
    void *kind; //isa, kind(unsigned long) 
    void *superClass;
    void *cacheData 
    void *data 
    uint32_t flags; //4 
    uint32_t instanceAddressOffset; //4 
    uint32_t instanceSize;//4 
    uint16_t instanceAlignMask; //2 
    uint16_t reserved; //2 
    uint32_t classSize; //4  
    uint32_t classAddressOffset; //4 
    void *description;  // ... 
};
上一篇 下一篇

猜你喜欢

热点阅读