blob: fec563817e6c31489f02ebe85d2896ef60449b82 [file] [log] [blame]
Daniel Dunbar12783d12010-02-21 21:54:14 +00001//===-- X86AsmBackend.cpp - X86 Assembler Backend -------------------------===//
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#include "llvm/Target/TargetAsmBackend.h"
11#include "X86.h"
Daniel Dunbar87190c42010-03-19 09:28:12 +000012#include "X86FixupKinds.h"
13#include "llvm/MC/MCAssembler.h"
Daniel Dunbard6e59082010-03-15 21:56:50 +000014#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbar12783d12010-02-21 21:54:14 +000015#include "llvm/Target/TargetRegistry.h"
16#include "llvm/Target/TargetAsmBackend.h"
17using namespace llvm;
18
19namespace {
20
Daniel Dunbar87190c42010-03-19 09:28:12 +000021static unsigned getFixupKindLog2Size(unsigned Kind) {
22 switch (Kind) {
23 default: assert(0 && "invalid fixup kind!");
24 case X86::reloc_pcrel_1byte:
25 case FK_Data_1: return 0;
26 case FK_Data_2: return 1;
27 case X86::reloc_pcrel_4byte:
28 case X86::reloc_riprel_4byte:
29 case X86::reloc_riprel_4byte_movq_load:
30 case FK_Data_4: return 2;
31 case FK_Data_8: return 3;
32 }
33}
34
Daniel Dunbar12783d12010-02-21 21:54:14 +000035class X86AsmBackend : public TargetAsmBackend {
36public:
Daniel Dunbar6c27f5e2010-03-11 01:34:16 +000037 X86AsmBackend(const Target &T)
Daniel Dunbar12783d12010-02-21 21:54:14 +000038 : TargetAsmBackend(T) {}
Daniel Dunbar87190c42010-03-19 09:28:12 +000039
40 void ApplyFixup(const MCAsmFixup &Fixup, MCDataFragment &DF,
41 uint64_t Value) const {
42 unsigned Size = 1 << getFixupKindLog2Size(Fixup.Kind);
43
44 assert(Fixup.Offset + Size <= DF.getContents().size() &&
45 "Invalid fixup offset!");
46 for (unsigned i = 0; i != Size; ++i)
47 DF.getContents()[Fixup.Offset + i] = uint8_t(Value >> (i * 8));
48 }
Daniel Dunbar12783d12010-02-21 21:54:14 +000049};
50
Daniel Dunbar23ac7c72010-03-11 01:34:21 +000051class DarwinX86AsmBackend : public X86AsmBackend {
52public:
53 DarwinX86AsmBackend(const Target &T)
Daniel Dunbar06829512010-03-18 00:58:53 +000054 : X86AsmBackend(T) {
55 HasAbsolutizedSet = true;
56 HasScatteredSymbols = true;
57 }
Daniel Dunbar23ac7c72010-03-11 01:34:21 +000058};
59
Daniel Dunbard6e59082010-03-15 21:56:50 +000060class DarwinX86_32AsmBackend : public DarwinX86AsmBackend {
61public:
62 DarwinX86_32AsmBackend(const Target &T)
63 : DarwinX86AsmBackend(T) {}
64};
65
66class DarwinX86_64AsmBackend : public DarwinX86AsmBackend {
67public:
68 DarwinX86_64AsmBackend(const Target &T)
Daniel Dunbar06829512010-03-18 00:58:53 +000069 : DarwinX86AsmBackend(T) {
70 HasReliableSymbolDifference = true;
71 }
Daniel Dunbard6e59082010-03-15 21:56:50 +000072
73 virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
74 // Temporary labels in the string literals sections require symbols. The
75 // issue is that the x86_64 relocation format does not allow symbol +
76 // offset, and so the linker does not have enough information to resolve the
77 // access to the appropriate atom unless an external relocation is used. For
78 // non-cstring sections, we expect the compiler to use a non-temporary label
79 // for anything that could have an addend pointing outside the symbol.
80 //
81 // See <rdar://problem/4765733>.
82 const MCSectionMachO &SMO = static_cast<const MCSectionMachO&>(Section);
83 return SMO.getType() == MCSectionMachO::S_CSTRING_LITERALS;
84 }
85};
86
Daniel Dunbar12783d12010-02-21 21:54:14 +000087}
88
89TargetAsmBackend *llvm::createX86_32AsmBackend(const Target &T,
Daniel Dunbar6c27f5e2010-03-11 01:34:16 +000090 const std::string &TT) {
Daniel Dunbar23ac7c72010-03-11 01:34:21 +000091 switch (Triple(TT).getOS()) {
92 case Triple::Darwin:
Daniel Dunbard6e59082010-03-15 21:56:50 +000093 return new DarwinX86_32AsmBackend(T);
Daniel Dunbar23ac7c72010-03-11 01:34:21 +000094 default:
95 return new X86AsmBackend(T);
96 }
Daniel Dunbar12783d12010-02-21 21:54:14 +000097}
98
99TargetAsmBackend *llvm::createX86_64AsmBackend(const Target &T,
Daniel Dunbar6c27f5e2010-03-11 01:34:16 +0000100 const std::string &TT) {
Daniel Dunbar23ac7c72010-03-11 01:34:21 +0000101 switch (Triple(TT).getOS()) {
102 case Triple::Darwin:
Daniel Dunbard6e59082010-03-15 21:56:50 +0000103 return new DarwinX86_64AsmBackend(T);
Daniel Dunbar23ac7c72010-03-11 01:34:21 +0000104 default:
105 return new X86AsmBackend(T);
106 }
Daniel Dunbar12783d12010-02-21 21:54:14 +0000107}