File: SwiftSILManipulator.cpp

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 (133 lines) | stat: -rw-r--r-- 5,196 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
//===-- SwiftSILManipulator.cpp ---------------------------------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

#include "SwiftSILManipulator.h"

#include "SwiftASTManipulator.h"

#include "lldb/Symbol/CompilerType.h"
#include "lldb/Utility/LLDBLog.h"
#include "lldb/Utility/Log.h"

#include "swift/SIL/SILArgument.h"
#include "swift/SIL/SILBasicBlock.h"
#include "swift/SIL/SILBuilder.h"
#include "swift/SIL/SILFunction.h"
#include "swift/SIL/SILLocation.h"
#include "swift/SIL/SILModule.h"
#include "swift/SIL/SILType.h"
#include "swift/SIL/SILValue.h"
#include "swift/SIL/TypeLowering.h"

using namespace lldb_private;

SwiftSILManipulator::SwiftSILManipulator(swift::SILBuilder &builder)
    : m_builder(builder), m_log(GetLog(LLDBLog::Expressions)) {}

swift::SILValue SwiftSILManipulator::emitLValueForVariable(
    swift::VarDecl *var, SwiftExpressionParser::SILVariableInfo &info) {
  swift::SILFunction &function = m_builder.getFunction();

  swift::SILBasicBlock &entry_block = *function.getEntryBlock();

  swift::SILArgument *struct_argument = nullptr;

  for (swift::SILArgument *argument : entry_block.getArguments()) {
    swift::Identifier argument_name = argument->getDecl()->getBaseName()
                                        .getIdentifier();

    if (!strcmp(argument_name.get(), SwiftASTManipulator::GetArgumentName())) {
      struct_argument = argument;
      break;
    }
  }

  if (!struct_argument)
    return swift::SILValue();

  assert(struct_argument->getType().getAsString().find(
             "UnsafeMutablePointer") != std::string::npos);

  swift::CanType unsafe_mutable_pointer_can_type =
      struct_argument->getType().getSwiftRValueType();

  swift::BoundGenericStructType *unsafe_mutable_pointer_struct_type =
      llvm::cast<swift::BoundGenericStructType>(
          unsafe_mutable_pointer_can_type.getPointer());
  swift::StructDecl *unsafe_mutable_pointer_struct_decl =
      unsafe_mutable_pointer_struct_type->getDecl();

  swift::VarDecl *value_member_decl = nullptr;

  for (swift::Decl *member : unsafe_mutable_pointer_struct_decl->getMembers()) {
    if (swift::VarDecl *member_var = llvm::dyn_cast<swift::VarDecl>(member)) {
      if (member_var->getName().str().equals("_rawValue")) {
        value_member_decl = member_var;
        break;
      }
    }
  }

  if (!value_member_decl)
    return swift::SILValue();

  swift::ASTContext &ast_ctx = m_builder.getASTContext();

  // FIXME: Creating a new TypeLowering every time is wasteful.
  swift::Lowering::TypeConverter converter(*m_builder.getModule().getSwiftModule());

  swift::SILLocation null_loc((swift::Decl *)nullptr);
  swift::SILType raw_pointer_type = swift::SILType::getRawPointerType(ast_ctx);

  swift::StructExtractInst *struct_extract = m_builder.createStructExtract(
      null_loc, struct_argument, value_member_decl, raw_pointer_type);
  swift::IntegerLiteralInst *integer_literal = m_builder.createIntegerLiteral(
      null_loc, swift::SILType::getBuiltinIntegerType(64, ast_ctx),
      (intmax_t)info.offset);
  swift::IndexRawPointerInst *index_raw_pointer =
      m_builder.createIndexRawPointer(null_loc, struct_extract,
                                      integer_literal);
  swift::PointerToAddressInst *pointer_to_return_slot =
      m_builder.createPointerToAddress(null_loc, index_raw_pointer,
                                       raw_pointer_type.getAddressType(),
                                       /*isStrict*/ true);
  swift::LoadInst *pointer_to_variable =
      m_builder.createLoad(null_loc, pointer_to_return_slot,
                           swift::LoadOwnershipQualifier::Trivial);
  auto type = var->getDeclContext()->mapTypeIntoContext(
      var->getInterfaceType());
  auto loweredType = converter.getLoweredRValueType(
      swift::TypeExpansionContext::minimal(), type);

  // The fact that self is unowned is not preserved in the inner
  // lldb_expr function. By casting the value to $*@sil_unowned ...
  // SILGen emits a load_unowned instead of a load and the unwrapped
  // self is passed to the lldb_expr.
  swift::CanType var_type = loweredType;
  if (info.is_unowned_self)
    var_type = swift::ReferenceStorageType::get(
                   loweredType, swift::ReferenceOwnership::Unowned, ast_ctx)
                   ->getCanonicalType();

  swift::PointerToAddressInst *address_of_variable =
      m_builder.createPointerToAddress(
          null_loc, pointer_to_variable,
          swift::SILType::getPrimitiveAddressType(var_type),
          /*isStrict*/ true);

  if (info.needs_init) {
    info.needs_init = false;
    return swift::SILValue(m_builder.createMarkUninitialized(
        null_loc, address_of_variable, swift::MarkUninitializedInst::Var));
  } else
    return swift::SILValue(address_of_variable);
}