Coverage Report

Created: 2017-10-03 07:32

/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/llvm/lib/Target/X86/X86AsmPrinter.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- X86AsmPrinter.cpp - Convert X86 LLVM code to AT&T assembly --------===//
2
//
3
//                     The LLVM Compiler Infrastructure
4
//
5
// This file is distributed under the University of Illinois Open Source
6
// License. See LICENSE.TXT for details.
7
//
8
//===----------------------------------------------------------------------===//
9
//
10
// This file contains a printer that converts from our internal representation
11
// of machine-dependent LLVM code to X86 machine code.
12
//
13
//===----------------------------------------------------------------------===//
14
15
#include "X86AsmPrinter.h"
16
#include "InstPrinter/X86ATTInstPrinter.h"
17
#include "MCTargetDesc/X86BaseInfo.h"
18
#include "X86InstrInfo.h"
19
#include "X86MachineFunctionInfo.h"
20
#include "llvm/BinaryFormat/COFF.h"
21
#include "llvm/CodeGen/MachineConstantPool.h"
22
#include "llvm/CodeGen/MachineModuleInfoImpls.h"
23
#include "llvm/CodeGen/MachineValueType.h"
24
#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
25
#include "llvm/IR/DebugInfo.h"
26
#include "llvm/IR/DerivedTypes.h"
27
#include "llvm/IR/Mangler.h"
28
#include "llvm/IR/Module.h"
29
#include "llvm/IR/Type.h"
30
#include "llvm/MC/MCAsmInfo.h"
31
#include "llvm/MC/MCCodeEmitter.h"
32
#include "llvm/MC/MCContext.h"
33
#include "llvm/MC/MCExpr.h"
34
#include "llvm/MC/MCSectionCOFF.h"
35
#include "llvm/MC/MCSectionMachO.h"
36
#include "llvm/MC/MCStreamer.h"
37
#include "llvm/MC/MCSymbol.h"
38
#include "llvm/Support/Debug.h"
39
#include "llvm/Support/ErrorHandling.h"
40
#include "llvm/Support/TargetRegistry.h"
41
using namespace llvm;
42
43
//===----------------------------------------------------------------------===//
44
// Primitive Helper Functions.
45
//===----------------------------------------------------------------------===//
46
47
/// runOnMachineFunction - Emit the function body.
48
///
49
72.7k
bool X86AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
50
72.7k
  Subtarget = &MF.getSubtarget<X86Subtarget>();
51
72.7k
52
72.7k
  SMShadowTracker.startFunction(MF);
53
72.7k
  CodeEmitter.reset(TM.getTarget().createMCCodeEmitter(
54
72.7k
      *MF.getSubtarget().getInstrInfo(), *MF.getSubtarget().getRegisterInfo(),
55
72.7k
      MF.getContext()));
56
72.7k
57
72.7k
  SetupMachineFunction(MF);
58
72.7k
59
72.7k
  if (
Subtarget->isTargetCOFF()72.7k
) {
60
1.99k
    bool Local = MF.getFunction()->hasLocalLinkage();
61
1.99k
    OutStreamer->BeginCOFFSymbolDef(CurrentFnSym);
62
1.99k
    OutStreamer->EmitCOFFSymbolStorageClass(
63
1.99k
        Local ? 
COFF::IMAGE_SYM_CLASS_STATIC56
:
COFF::IMAGE_SYM_CLASS_EXTERNAL1.94k
);
64
1.99k
    OutStreamer->EmitCOFFSymbolType(COFF::IMAGE_SYM_DTYPE_FUNCTION
65
1.99k
                                               << COFF::SCT_COMPLEX_TYPE_SHIFT);
66
1.99k
    OutStreamer->EndCOFFSymbolDef();
67
1.99k
  }
68
72.7k
69
72.7k
  // Emit the rest of the function body.
70
72.7k
  EmitFunctionBody();
71
72.7k
72
72.7k
  // Emit the XRay table for this function.
73
72.7k
  emitXRayTable();
74
72.7k
75
72.7k
  // We didn't modify anything.
76
72.7k
  return false;
77
72.7k
}
78
79
/// printSymbolOperand - Print a raw symbol reference operand.  This handles
80
/// jump tables, constant pools, global address and external symbols, all of
81
/// which print to a label with various suffixes for relocation types etc.
82
static void printSymbolOperand(X86AsmPrinter &P, const MachineOperand &MO,
83
78
                               raw_ostream &O) {
84
78
  switch (MO.getType()) {
85
0
  
default: 0
llvm_unreachable0
("unknown symbol type!");
86
7
  case MachineOperand::MO_ConstantPoolIndex:
87
7
    P.GetCPISymbol(MO.getIndex())->print(O, P.MAI);
88
7
    P.printOffset(MO.getOffset(), O);
89
7
    break;
90
71
  case MachineOperand::MO_GlobalAddress: {
91
71
    const GlobalValue *GV = MO.getGlobal();
92
71
93
71
    MCSymbol *GVSym;
94
71
    if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY ||
95
71
        MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE)
96
0
      GVSym = P.getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
97
71
    else
98
71
      GVSym = P.getSymbol(GV);
99
71
100
71
    // Handle dllimport linkage.
101
71
    if (MO.getTargetFlags() == X86II::MO_DLLIMPORT)
102
0
      GVSym =
103
0
          P.OutContext.getOrCreateSymbol(Twine("__imp_") + GVSym->getName());
104
71
105
71
    if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY ||
106
71
        
MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE71
) {
107
0
      MCSymbol *Sym = P.getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
108
0
      MachineModuleInfoImpl::StubValueTy &StubSym =
109
0
          P.MMI->getObjFileInfo<MachineModuleInfoMachO>().getGVStubEntry(Sym);
110
0
      if (!StubSym.getPointer())
111
0
        StubSym = MachineModuleInfoImpl::
112
0
          StubValueTy(P.getSymbol(GV), !GV->hasInternalLinkage());
113
0
    }
114
71
115
71
    // If the name begins with a dollar-sign, enclose it in parens.  We do this
116
71
    // to avoid having it look like an integer immediate to the assembler.
117
71
    if (GVSym->getName()[0] != '$')
118
71
      GVSym->print(O, P.MAI);
119
0
    else {
120
0
      O << '(';
121
0
      GVSym->print(O, P.MAI);
122
0
      O << ')';
123
0
    }
124
71
    P.printOffset(MO.getOffset(), O);
125
71
    break;
126
78
  }
127
78
  }
128
78
129
78
  switch (MO.getTargetFlags()) {
130
0
  default:
131
0
    llvm_unreachable("Unknown target flag on GV operand");
132
78
  case X86II::MO_NO_FLAG:    // No flag.
133
78
    break;
134
0
  case X86II::MO_DARWIN_NONLAZY:
135
0
  case X86II::MO_DLLIMPORT:
136
0
    // These affect the name of the symbol, not any suffix.
137
0
    break;
138
0
  case X86II::MO_GOT_ABSOLUTE_ADDRESS:
139
0
    O << " + [.-";
140
0
    P.MF->getPICBaseSymbol()->print(O, P.MAI);
141
0
    O << ']';
142
0
    break;
143
0
  case X86II::MO_PIC_BASE_OFFSET:
144
0
  case X86II::MO_DARWIN_NONLAZY_PIC_BASE:
145
0
    O << '-';
146
0
    P.MF->getPICBaseSymbol()->print(O, P.MAI);
147
0
    break;
148
0
  case X86II::MO_TLSGD:     O << "@TLSGD";     break;
149
0
  case X86II::MO_TLSLD:     O << "@TLSLD";     break;
150
0
  case X86II::MO_TLSLDM:    O << "@TLSLDM";    break;
151
0
  case X86II::MO_GOTTPOFF:  O << "@GOTTPOFF";  break;
152
0
  case X86II::MO_INDNTPOFF: O << "@INDNTPOFF"; break;
153
0
  case X86II::MO_TPOFF:     O << "@TPOFF";     break;
154
0
  case X86II::MO_DTPOFF:    O << "@DTPOFF";    break;
155
0
  case X86II::MO_NTPOFF:    O << "@NTPOFF";    break;
156
0
  case X86II::MO_GOTNTPOFF: O << "@GOTNTPOFF"; break;
157
0
  case X86II::MO_GOTPCREL:  O << "@GOTPCREL";  break;
158
0
  case X86II::MO_GOT:       O << "@GOT";       break;
159
0
  case X86II::MO_GOTOFF:    O << "@GOTOFF";    break;
160
0
  case X86II::MO_PLT:       O << "@PLT";       break;
161
0
  case X86II::MO_TLVP:      O << "@TLVP";      break;
162
0
  case X86II::MO_TLVP_PIC_BASE:
163
0
    O << "@TLVP" << '-';
164
0
    P.MF->getPICBaseSymbol()->print(O, P.MAI);
165
0
    break;
166
0
  case X86II::MO_SECREL:    O << "@SECREL32";  break;
167
78
  }
168
78
}
169
170
static void printOperand(X86AsmPrinter &P, const MachineInstr *MI,
171
                         unsigned OpNo, raw_ostream &O,
172
                         const char *Modifier = nullptr, unsigned AsmVariant = 0);
173
174
/// printPCRelImm - This is used to print an immediate value that ends up
175
/// being encoded as a pc-relative value.  These print slightly differently, for
176
/// example, a $ is not emitted.
177
static void printPCRelImm(X86AsmPrinter &P, const MachineInstr *MI,
178
5
                          unsigned OpNo, raw_ostream &O) {
179
5
  const MachineOperand &MO = MI->getOperand(OpNo);
180
5
  switch (MO.getType()) {
181
0
  
default: 0
llvm_unreachable0
("Unknown pcrel immediate operand");
182
2
  case MachineOperand::MO_Register:
183
2
    // pc-relativeness was handled when computing the value in the reg.
184
2
    printOperand(P, MI, OpNo, O);
185
2
    return;
186
1
  case MachineOperand::MO_Immediate:
187
1
    O << MO.getImm();
188
1
    return;
189
2
  case MachineOperand::MO_GlobalAddress:
190
2
    printSymbolOperand(P, MO, O);
191
2
    return;
192
0
  }
193
0
}
194
195
static void printOperand(X86AsmPrinter &P, const MachineInstr *MI,
196
                         unsigned OpNo, raw_ostream &O, const char *Modifier,
197
1.16k
                         unsigned AsmVariant) {
198
1.16k
  const MachineOperand &MO = MI->getOperand(OpNo);
199
1.16k
  switch (MO.getType()) {
200
0
  
default: 0
llvm_unreachable0
("unknown operand type!");
201
1.10k
  case MachineOperand::MO_Register: {
202
1.10k
    // FIXME: Enumerating AsmVariant, so we can remove magic number.
203
1.10k
    if (
AsmVariant == 01.10k
)
O << '%'1.08k
;
204
1.10k
    unsigned Reg = MO.getReg();
205
1.10k
    if (
Modifier && 1.10k
strncmp(Modifier, "subreg", strlen("subreg")) == 02
) {
206
0
      unsigned Size = (strcmp(Modifier+6,"64") == 0) ? 64 :
207
0
                      
(strcmp(Modifier+6,"32") == 0) ? 0
320
:
208
0
                      
(strcmp(Modifier+6,"16") == 0) ? 0
160
:
80
;
209
0
      Reg = getX86SubSuperRegister(Reg, Size);
210
0
    }
211
1.10k
    O << X86ATTInstPrinter::getRegisterName(Reg);
212
1.10k
    return;
213
1.16k
  }
214
1.16k
215
43
  case MachineOperand::MO_Immediate:
216
43
    if (
AsmVariant == 043
)
O << '$'43
;
217
43
    O << MO.getImm();
218
43
    return;
219
1.16k
220
20
  case MachineOperand::MO_GlobalAddress: {
221
20
    if (
AsmVariant == 020
)
O << '$'12
;
222
20
    printSymbolOperand(P, MO, O);
223
20
    break;
224
20
  }
225
20
  }
226
20
}
227
228
static void printLeaMemReference(X86AsmPrinter &P, const MachineInstr *MI,
229
                                 unsigned Op, raw_ostream &O,
230
167
                                 const char *Modifier = nullptr) {
231
167
  const MachineOperand &BaseReg  = MI->getOperand(Op+X86::AddrBaseReg);
232
167
  const MachineOperand &IndexReg = MI->getOperand(Op+X86::AddrIndexReg);
233
167
  const MachineOperand &DispSpec = MI->getOperand(Op+X86::AddrDisp);
234
167
235
167
  // If we really don't want to print out (rip), don't.
236
167
  bool HasBaseReg = BaseReg.getReg() != 0;
237
167
  if (
HasBaseReg && 167
Modifier120
&&
!strcmp(Modifier, "no-rip")2
&&
238
1
      BaseReg.getReg() == X86::RIP)
239
0
    HasBaseReg = false;
240
167
241
167
  // HasParenPart - True if we will print out the () part of the mem ref.
242
158
  bool HasParenPart = IndexReg.getReg() || HasBaseReg;
243
167
244
167
  switch (DispSpec.getType()) {
245
0
  default:
246
0
    llvm_unreachable("unknown operand type!");
247
122
  case MachineOperand::MO_Immediate: {
248
122
    int DispVal = DispSpec.getImm();
249
122
    if (
DispVal || 122
!HasParenPart66
)
250
63
      O << DispVal;
251
122
    break;
252
167
  }
253
45
  case MachineOperand::MO_GlobalAddress:
254
45
  case MachineOperand::MO_ConstantPoolIndex:
255
45
    printSymbolOperand(P, DispSpec, O);
256
167
  }
257
167
258
167
  
if (167
Modifier && 167
strcmp(Modifier, "H") == 02
)
259
1
    O << "+8";
260
167
261
167
  if (
HasParenPart167
) {
262
120
    assert(IndexReg.getReg() != X86::ESP &&
263
120
           "X86 doesn't allow scaling by ESP");
264
120
265
120
    O << '(';
266
120
    if (HasBaseReg)
267
120
      printOperand(P, MI, Op+X86::AddrBaseReg, O, Modifier);
268
120
269
120
    if (
IndexReg.getReg()120
) {
270
9
      O << ',';
271
9
      printOperand(P, MI, Op+X86::AddrIndexReg, O, Modifier);
272
9
      unsigned ScaleVal = MI->getOperand(Op+X86::AddrScaleAmt).getImm();
273
9
      if (ScaleVal != 1)
274
3
        O << ',' << ScaleVal;
275
9
    }
276
120
    O << ')';
277
120
  }
278
167
}
279
280
static void printMemReference(X86AsmPrinter &P, const MachineInstr *MI,
281
                              unsigned Op, raw_ostream &O,
282
167
                              const char *Modifier = nullptr) {
283
167
  assert(isMem(*MI, Op) && "Invalid memory reference!");
284
167
  const MachineOperand &Segment = MI->getOperand(Op+X86::AddrSegmentReg);
285
167
  if (
Segment.getReg()167
) {
286
0
    printOperand(P, MI, Op+X86::AddrSegmentReg, O, Modifier);
287
0
    O << ':';
288
0
  }
289
167
  printLeaMemReference(P, MI, Op, O, Modifier);
290
167
}
291
292
static void printIntelMemReference(X86AsmPrinter &P, const MachineInstr *MI,
293
                                   unsigned Op, raw_ostream &O,
294
                                   const char *Modifier = nullptr,
295
25
                                   unsigned AsmVariant = 1) {
296
25
  const MachineOperand &BaseReg  = MI->getOperand(Op+X86::AddrBaseReg);
297
25
  unsigned ScaleVal = MI->getOperand(Op+X86::AddrScaleAmt).getImm();
298
25
  const MachineOperand &IndexReg = MI->getOperand(Op+X86::AddrIndexReg);
299
25
  const MachineOperand &DispSpec = MI->getOperand(Op+X86::AddrDisp);
300
25
  const MachineOperand &SegReg   = MI->getOperand(Op+X86::AddrSegmentReg);
301
25
302
25
  // If this has a segment register, print it.
303
25
  if (
SegReg.getReg()25
) {
304
0
    printOperand(P, MI, Op+X86::AddrSegmentReg, O, Modifier, AsmVariant);
305
0
    O << ':';
306
0
  }
307
25
308
25
  O << '[';
309
25
310
25
  bool NeedPlus = false;
311
25
  if (
BaseReg.getReg()25
) {
312
17
    printOperand(P, MI, Op+X86::AddrBaseReg, O, Modifier, AsmVariant);
313
17
    NeedPlus = true;
314
17
  }
315
25
316
25
  if (
IndexReg.getReg()25
) {
317
0
    if (
NeedPlus0
)
O << " + "0
;
318
0
    if (ScaleVal != 1)
319
0
      O << ScaleVal << '*';
320
0
    printOperand(P, MI, Op+X86::AddrIndexReg, O, Modifier, AsmVariant);
321
0
    NeedPlus = true;
322
0
  }
323
25
324
25
  if (
!DispSpec.isImm()25
) {
325
8
    if (
NeedPlus8
)
O << " + "0
;
326
8
    printOperand(P, MI, Op+X86::AddrDisp, O, Modifier, AsmVariant);
327
25
  } else {
328
17
    int64_t DispVal = DispSpec.getImm();
329
17
    if (
DispVal || 17
(!IndexReg.getReg() && 6
!BaseReg.getReg()6
)) {
330
11
      if (
NeedPlus11
) {
331
11
        if (DispVal > 0)
332
11
          O << " + ";
333
0
        else {
334
0
          O << " - ";
335
0
          DispVal = -DispVal;
336
0
        }
337
11
      }
338
11
      O << DispVal;
339
11
    }
340
17
  }
341
25
  O << ']';
342
25
}
343
344
static bool printAsmMRegister(X86AsmPrinter &P, const MachineOperand &MO,
345
7
                              char Mode, raw_ostream &O) {
346
7
  unsigned Reg = MO.getReg();
347
7
  switch (Mode) {
348
0
  default: return true;  // Unknown mode.
349
2
  case 'b': // Print QImode register
350
2
    Reg = getX86SubSuperRegister(Reg, 8);
351
2
    break;
352
1
  case 'h': // Print QImode high register
353
1
    Reg = getX86SubSuperRegister(Reg, 8, true);
354
1
    break;
355
3
  case 'w': // Print HImode register
356
3
    Reg = getX86SubSuperRegister(Reg, 16);
357
3
    break;
358
0
  case 'k': // Print SImode register
359
0
    Reg = getX86SubSuperRegister(Reg, 32);
360
0
    break;
361
1
  case 'q':
362
1
    // Print 64-bit register names if 64-bit integer registers are available.
363
1
    // Otherwise, print 32-bit register names.
364
1
    Reg = getX86SubSuperRegister(Reg, P.getSubtarget().is64Bit() ? 
640
:
321
);
365
1
    break;
366
7
  }
367
7
368
7
  O << '%' << X86ATTInstPrinter::getRegisterName(Reg);
369
7
  return false;
370
7
}
371
372
/// PrintAsmOperand - Print out an operand for an inline asm expression.
373
///
374
bool X86AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
375
                                    unsigned AsmVariant,
376
1.04k
                                    const char *ExtraCode, raw_ostream &O) {
377
1.04k
  // Does this asm operand have a single letter operand modifier?
378
1.04k
  if (
ExtraCode && 1.04k
ExtraCode[0]39
) {
379
39
    if (
ExtraCode[1] != 039
)
return true0
; // Unknown modifier.
380
39
381
39
    const MachineOperand &MO = MI->getOperand(OpNo);
382
39
383
39
    switch (ExtraCode[0]) {
384
0
    default:
385
0
      // See if this is a generic print operand
386
0
      return AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, O);
387
5
    case 'a': // This is an address.  Currently only 'i' and 'r' are expected.
388
5
      switch (MO.getType()) {
389
0
      default:
390
0
        return true;
391
1
      case MachineOperand::MO_Immediate:
392
1
        O << MO.getImm();
393
1
        return false;
394
0
      case MachineOperand::MO_ConstantPoolIndex:
395
0
      case MachineOperand::MO_JumpTableIndex:
396
0
      case MachineOperand::MO_ExternalSymbol:
397
0
        llvm_unreachable("unexpected operand type!");
398
2
      case MachineOperand::MO_GlobalAddress:
399
2
        printSymbolOperand(*this, MO, O);
400
2
        if (Subtarget->isPICStyleRIPRel())
401
1
          O << "(%rip)";
402
2
        return false;
403
2
      case MachineOperand::MO_Register:
404
2
        O << '(';
405
2
        printOperand(*this, MI, OpNo, O);
406
2
        O << ')';
407
2
        return false;
408
0
      }
409
0
410
19
    case 'c': // Don't print "$" before a global var name or constant.
411
19
      switch (MO.getType()) {
412
0
      default:
413
0
        printOperand(*this, MI, OpNo, O);
414
0
        break;
415
10
      case MachineOperand::MO_Immediate:
416
10
        O << MO.getImm();
417
10
        break;
418
0
      case MachineOperand::MO_ConstantPoolIndex:
419
0
      case MachineOperand::MO_JumpTableIndex:
420
0
      case MachineOperand::MO_ExternalSymbol:
421
0
        llvm_unreachable("unexpected operand type!");
422
9
      case MachineOperand::MO_GlobalAddress:
423
9
        printSymbolOperand(*this, MO, O);
424
9
        break;
425
19
      }
426
19
      return false;
427
19
428
0
    case 'A': // Print '*' before a register (it must be a register)
429
0
      if (
MO.isReg()0
) {
430
0
        O << '*';
431
0
        printOperand(*this, MI, OpNo, O);
432
0
        return false;
433
0
      }
434
0
      return true;
435
0
436
8
    case 'b': // Print QImode register
437
8
    case 'h': // Print QImode high register
438
8
    case 'w': // Print HImode register
439
8
    case 'k': // Print SImode register
440
8
    case 'q': // Print DImode register
441
8
      if (MO.isReg())
442
7
        return printAsmMRegister(*this, MO, ExtraCode[0], O);
443
1
      printOperand(*this, MI, OpNo, O);
444
1
      return false;
445
1
446
5
    case 'P': // This is the operand of a call, treat specially.
447
5
      printPCRelImm(*this, MI, OpNo, O);
448
5
      return false;
449
1
450
2
    case 'n':  // Negate the immediate or print a '-' before the operand.
451
2
      // Note: this is a temporary solution. It should be handled target
452
2
      // independently as part of the 'MC' work.
453
2
      if (
MO.isImm()2
) {
454
2
        O << -MO.getImm();
455
2
        return false;
456
2
      }
457
0
      O << '-';
458
39
    }
459
39
  }
460
1.04k
461
1.00k
  printOperand(*this, MI, OpNo, O, /*Modifier*/ nullptr, AsmVariant);
462
1.00k
  return false;
463
1.04k
}
464
465
bool X86AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
466
                                          unsigned OpNo, unsigned AsmVariant,
467
                                          const char *ExtraCode,
468
192
                                          raw_ostream &O) {
469
192
  if (
AsmVariant192
) {
470
25
    printIntelMemReference(*this, MI, OpNo, O);
471
25
    return false;
472
25
  }
473
167
474
167
  
if (167
ExtraCode && 167
ExtraCode[0]3
) {
475
3
    if (
ExtraCode[1] != 03
)
return true0
; // Unknown modifier.
476
3
477
3
    switch (ExtraCode[0]) {
478
0
    default: return true;  // Unknown modifier.
479
1
    case 'b': // Print QImode register
480
1
    case 'h': // Print QImode high register
481
1
    case 'w': // Print HImode register
482
1
    case 'k': // Print SImode register
483
1
    case 'q': // Print SImode register
484
1
      // These only apply to registers, ignore on mem.
485
1
      break;
486
1
    case 'H':
487
1
      printMemReference(*this, MI, OpNo, O, "H");
488
1
      return false;
489
1
    case 'P': // Don't print @PLT, but do print as memory.
490
1
      printMemReference(*this, MI, OpNo, O, "no-rip");
491
1
      return false;
492
165
    }
493
165
  }
494
165
  printMemReference(*this, MI, OpNo, O);
495
165
  return false;
496
165
}
497
498
8.52k
void X86AsmPrinter::EmitStartOfAsmFile(Module &M) {
499
8.52k
  const Triple &TT = TM.getTargetTriple();
500
8.52k
501
8.52k
  if (TT.isOSBinFormatMachO())
502
3.78k
    OutStreamer->SwitchSection(getObjFileLowering().getTextSection());
503
8.52k
504
8.52k
  if (
TT.isOSBinFormatCOFF()8.52k
) {
505
536
    // Emit an absolute @feat.00 symbol.  This appears to be some kind of
506
536
    // compiler features bitfield read by link.exe.
507
536
    if (
TT.getArch() == Triple::x86536
) {
508
233
      MCSymbol *S = MMI->getContext().getOrCreateSymbol(StringRef("@feat.00"));
509
233
      OutStreamer->BeginCOFFSymbolDef(S);
510
233
      OutStreamer->EmitCOFFSymbolStorageClass(COFF::IMAGE_SYM_CLASS_STATIC);
511
233
      OutStreamer->EmitCOFFSymbolType(COFF::IMAGE_SYM_DTYPE_NULL);
512
233
      OutStreamer->EndCOFFSymbolDef();
513
233
      // According to the PE-COFF spec, the LSB of this value marks the object
514
233
      // for "registered SEH".  This means that all SEH handler entry points
515
233
      // must be registered in .sxdata.  Use of any unregistered handlers will
516
233
      // cause the process to terminate immediately.  LLVM does not know how to
517
233
      // register any SEH handlers, so its object files should be safe.
518
233
      OutStreamer->EmitSymbolAttribute(S, MCSA_Global);
519
233
      OutStreamer->EmitAssignment(
520
233
          S, MCConstantExpr::create(int64_t(1), MMI->getContext()));
521
233
    }
522
536
  }
523
8.52k
  OutStreamer->EmitSyntaxDirective();
524
8.52k
525
8.52k
  // If this is not inline asm and we're in 16-bit
526
8.52k
  // mode prefix assembly with .code16.
527
8.52k
  bool is16 = TT.getEnvironment() == Triple::CODE16;
528
8.52k
  if (
M.getModuleInlineAsm().empty() && 8.52k
is168.32k
)
529
1
    OutStreamer->EmitAssemblerFlag(MCAF_Code16);
530
8.52k
}
531
532
static void
533
emitNonLazySymbolPointer(MCStreamer &OutStreamer, MCSymbol *StubLabel,
534
405
                         MachineModuleInfoImpl::StubValueTy &MCSym) {
535
405
  // L_foo$stub:
536
405
  OutStreamer.EmitLabel(StubLabel);
537
405
  //   .indirect_symbol _foo
538
405
  OutStreamer.EmitSymbolAttribute(MCSym.getPointer(), MCSA_IndirectSymbol);
539
405
540
405
  if (MCSym.getInt())
541
405
    // External to current translation unit.
542
405
    OutStreamer.EmitIntValue(0, 4/*size*/);
543
405
  else
544
405
    // Internal to current translation unit.
545
405
    //
546
405
    // When we place the LSDA into the TEXT section, the type info
547
405
    // pointers need to be indirect and pc-rel. We accomplish this by
548
405
    // using NLPs; however, sometimes the types are local to the file.
549
405
    // We need to fill in the value for the NLP in those cases.
550
0
    OutStreamer.EmitValue(
551
0
        MCSymbolRefExpr::create(MCSym.getPointer(), OutStreamer.getContext()),
552
0
        4 /*size*/);
553
405
}
554
555
27.9k
MCSymbol *X86AsmPrinter::GetCPISymbol(unsigned CPID) const {
556
27.9k
  if (
Subtarget->isTargetKnownWindowsMSVC()27.9k
) {
557
154
    const MachineConstantPoolEntry &CPE =
558
154
        MF->getConstantPool()->getConstants()[CPID];
559
154
    if (
!CPE.isMachineConstantPoolEntry()154
) {
560
154
      const DataLayout &DL = MF->getDataLayout();
561
154
      SectionKind Kind = CPE.getSectionKind(&DL);
562
154
      const Constant *C = CPE.Val.ConstVal;
563
154
      unsigned Align = CPE.Alignment;
564
154
      if (const MCSectionCOFF *S = dyn_cast<MCSectionCOFF>(
565
154
              getObjFileLowering().getSectionForConstant(DL, Kind, C, Align))) {
566
154
        if (MCSymbol *
Sym154
= S->getCOMDATSymbol()) {
567
152
          if (Sym->isUndefined())
568
57
            OutStreamer->EmitSymbolAttribute(Sym, MCSA_Global);
569
152
          return Sym;
570
152
        }
571
27.8k
      }
572
154
    }
573
154
  }
574
27.8k
575
27.8k
  return AsmPrinter::GetCPISymbol(CPID);
576
27.8k
}
577
578
8.49k
void X86AsmPrinter::EmitEndOfAsmFile(Module &M) {
579
8.49k
  const Triple &TT = TM.getTargetTriple();
580
8.49k
581
8.49k
  if (
TT.isOSBinFormatMachO()8.49k
) {
582
3.77k
    // All darwin targets use mach-o.
583
3.77k
    MachineModuleInfoMachO &MMIMacho =
584
3.77k
        MMI->getObjFileInfo<MachineModuleInfoMachO>();
585
3.77k
586
3.77k
    // Output stubs for dynamically-linked functions.
587
3.77k
    MachineModuleInfoMachO::SymbolListTy Stubs;
588
3.77k
589
3.77k
    // Output stubs for external and common global variables.
590
3.77k
    Stubs = MMIMacho.GetGVStubList();
591
3.77k
    if (
!Stubs.empty()3.77k
) {
592
176
      MCSection *TheSection = OutContext.getMachOSection(
593
176
          "__IMPORT", "__pointers", MachO::S_NON_LAZY_SYMBOL_POINTERS,
594
176
          SectionKind::getMetadata());
595
176
      OutStreamer->SwitchSection(TheSection);
596
176
597
176
      for (auto &Stub : Stubs)
598
405
        emitNonLazySymbolPointer(*OutStreamer, Stub.first, Stub.second);
599
176
600
176
      Stubs.clear();
601
176
      OutStreamer->AddBlankLine();
602
176
    }
603
3.77k
604
3.77k
    SM.serializeToStackMapSection();
605
3.77k
    FM.serializeToFaultMapSection();
606
3.77k
607
3.77k
    // Funny Darwin hack: This flag tells the linker that no global symbols
608
3.77k
    // contain code that falls through to other global symbols (e.g. the obvious
609
3.77k
    // implementation of multiple entry points).  If this doesn't occur, the
610
3.77k
    // linker can safely perform dead code stripping.  Since LLVM never
611
3.77k
    // generates code that does this, it is always safe to set.
612
3.77k
    OutStreamer->EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
613
3.77k
  }
614
8.49k
615
8.49k
  if (
TT.isKnownWindowsMSVCEnvironment() && 8.49k
MMI->usesVAFloatArgument()427
) {
616
9
    StringRef SymbolName =
617
9
        (TT.getArch() == Triple::x86_64) ? 
"_fltused"4
:
"__fltused"5
;
618
9
    MCSymbol *S = MMI->getContext().getOrCreateSymbol(SymbolName);
619
9
    OutStreamer->EmitSymbolAttribute(S, MCSA_Global);
620
9
  }
621
8.49k
622
8.49k
  if (
TT.isOSBinFormatCOFF()8.49k
) {
623
532
    const TargetLoweringObjectFileCOFF &TLOFCOFF =
624
532
        static_cast<const TargetLoweringObjectFileCOFF&>(getObjFileLowering());
625
532
626
532
    std::string Flags;
627
532
    raw_string_ostream FlagsOS(Flags);
628
532
629
532
    for (const auto &Function : M)
630
3.52k
      TLOFCOFF.emitLinkerFlagsForGlobal(FlagsOS, &Function);
631
532
    for (const auto &Global : M.globals())
632
513
      TLOFCOFF.emitLinkerFlagsForGlobal(FlagsOS, &Global);
633
532
    for (const auto &Alias : M.aliases())
634
58
      TLOFCOFF.emitLinkerFlagsForGlobal(FlagsOS, &Alias);
635
532
636
532
    FlagsOS.flush();
637
532
638
532
    // Output collected flags.
639
532
    if (
!Flags.empty()532
) {
640
18
      OutStreamer->SwitchSection(TLOFCOFF.getDrectveSection());
641
18
      OutStreamer->EmitBytes(Flags);
642
18
    }
643
532
644
532
    SM.serializeToStackMapSection();
645
532
  }
646
8.49k
647
8.49k
  if (
TT.isOSBinFormatELF()8.49k
) {
648
4.18k
    SM.serializeToStackMapSection();
649
4.18k
    FM.serializeToFaultMapSection();
650
4.18k
  }
651
8.49k
}
652
653
//===----------------------------------------------------------------------===//
654
// Target Registry Stuff
655
//===----------------------------------------------------------------------===//
656
657
// Force static initialization.
658
62.1k
extern "C" void LLVMInitializeX86AsmPrinter() {
659
62.1k
  RegisterAsmPrinter<X86AsmPrinter> X(getTheX86_32Target());
660
62.1k
  RegisterAsmPrinter<X86AsmPrinter> Y(getTheX86_64Target());
661
62.1k
}