1156230Smux//===- MCAsmInfo.cpp - Asm Info -------------------------------------------===//
2156230Smux//
3156230Smux// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4156230Smux// See https://llvm.org/LICENSE.txt for license information.
5156230Smux// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6156230Smux//
7156230Smux//===----------------------------------------------------------------------===//
8156230Smux//
9156230Smux// This file defines target asm properties related what form asm statements
10156230Smux// should take.
11156230Smux//
12156230Smux//===----------------------------------------------------------------------===//
13156230Smux
14156230Smux#include "llvm/MC/MCAsmInfo.h"
15156230Smux#include "llvm/BinaryFormat/Dwarf.h"
16156230Smux#include "llvm/MC/MCContext.h"
17156230Smux#include "llvm/MC/MCExpr.h"
18156230Smux#include "llvm/MC/MCStreamer.h"
19156230Smux#include "llvm/Support/CommandLine.h"
20156230Smux
21156230Smuxusing namespace llvm;
22156230Smux
23156230Smuxenum DefaultOnOff { Default, Enable, Disable };
24156230Smuxstatic cl::opt<DefaultOnOff> DwarfExtendedLoc(
25156230Smux    "dwarf-extended-loc", cl::Hidden,
26156230Smux    cl::desc("Disable emission of the extended flags in .loc directives."),
27156230Smux    cl::values(clEnumVal(Default, "Default for platform"),
28156230Smux               clEnumVal(Enable, "Enabled"), clEnumVal(Disable, "Disabled")),
29156230Smux    cl::init(Default));
30156230Smux
31156230SmuxMCAsmInfo::MCAsmInfo() {
32156230Smux  SeparatorString = ";";
33156230Smux  CommentString = "#";
34156230Smux  LabelSuffix = ":";
35156230Smux  PrivateGlobalPrefix = "L";
36156230Smux  PrivateLabelPrefix = PrivateGlobalPrefix;
37186781Slulf  LinkerPrivateGlobalPrefix = "";
38186781Slulf  InlineAsmStart = "APP";
39156230Smux  InlineAsmEnd = "NO_APP";
40156230Smux  Code16Directive = ".code16";
41156230Smux  Code32Directive = ".code32";
42186781Slulf  Code64Directive = ".code64";
43156230Smux  ZeroDirective = "\t.zero\t";
44156230Smux  AsciiDirective = "\t.ascii\t";
45156230Smux  AscizDirective = "\t.asciz\t";
46156230Smux  Data8bitsDirective = "\t.byte\t";
47156230Smux  Data16bitsDirective = "\t.short\t";
48156230Smux  Data32bitsDirective = "\t.long\t";
49156230Smux  Data64bitsDirective = "\t.quad\t";
50156230Smux  GlobalDirective = "\t.globl\t";
51156230Smux  WeakDirective = "\t.weak\t";
52156230Smux  if (DwarfExtendedLoc != Default)
53186781Slulf    SupportsExtendedDwarfLocDirective = DwarfExtendedLoc == Enable;
54186781Slulf
55186781Slulf  // FIXME: Clang's logic should be synced with the logic used to initialize
56186781Slulf  //        this member and the two implementations should be merged.
57186781Slulf  // For reference:
58156230Smux  // - Solaris always enables the integrated assembler by default
59156230Smux  //   - SparcELFMCAsmInfo and X86ELFMCAsmInfo are handling this case
60156230Smux  // - Windows always enables the integrated assembler by default
61156230Smux  //   - MCAsmInfoCOFF is handling this case, should it be MCAsmInfoMicrosoft?
62186781Slulf  // - MachO targets always enables the integrated assembler by default
63156230Smux  //   - MCAsmInfoDarwin is handling this case
64156230Smux  // - Generic_GCC toolchains enable the integrated assembler on a per
65156230Smux  //   architecture basis.
66186781Slulf  //   - The target subclasses for AArch64, ARM, and X86 handle these cases
67156230Smux  UseIntegratedAssembler = false;
68156230Smux  PreserveAsmComments = true;
69156230Smux}
70156230Smux
71156230SmuxMCAsmInfo::~MCAsmInfo() = default;
72156230Smux
73156230Smuxvoid MCAsmInfo::addInitialFrameState(const MCCFIInstruction &Inst) {
74186781Slulf  InitialFrameState.push_back(Inst);
75156230Smux}
76156230Smux
77156230Smuxbool MCAsmInfo::isSectionAtomizableBySymbols(const MCSection &Section) const {
78156230Smux  return false;
79156230Smux}
80156230Smux
81156230Smuxconst MCExpr *
82186781SlulfMCAsmInfo::getExprForPersonalitySymbol(const MCSymbol *Sym,
83186781Slulf                                       unsigned Encoding,
84156230Smux                                       MCStreamer &Streamer) const {
85  return getExprForFDESymbol(Sym, Encoding, Streamer);
86}
87
88const MCExpr *
89MCAsmInfo::getExprForFDESymbol(const MCSymbol *Sym,
90                               unsigned Encoding,
91                               MCStreamer &Streamer) const {
92  if (!(Encoding & dwarf::DW_EH_PE_pcrel))
93    return MCSymbolRefExpr::create(Sym, Streamer.getContext());
94
95  MCContext &Context = Streamer.getContext();
96  const MCExpr *Res = MCSymbolRefExpr::create(Sym, Context);
97  MCSymbol *PCSym = Context.createTempSymbol();
98  Streamer.EmitLabel(PCSym);
99  const MCExpr *PC = MCSymbolRefExpr::create(PCSym, Context);
100  return MCBinaryExpr::createSub(Res, PC, Context);
101}
102
103bool MCAsmInfo::isAcceptableChar(char C) const {
104  return (C >= 'a' && C <= 'z') || (C >= 'A' && C <= 'Z') ||
105         (C >= '0' && C <= '9') || C == '_' || C == '$' || C == '.' || C == '@';
106}
107
108bool MCAsmInfo::isValidUnquotedName(StringRef Name) const {
109  if (Name.empty())
110    return false;
111
112  // If any of the characters in the string is an unacceptable character, force
113  // quotes.
114  for (char C : Name) {
115    if (!isAcceptableChar(C))
116      return false;
117  }
118
119  return true;
120}
121
122bool MCAsmInfo::shouldOmitSectionDirective(StringRef SectionName) const {
123  // FIXME: Does .section .bss/.data/.text work everywhere??
124  return SectionName == ".text" || SectionName == ".data" ||
125        (SectionName == ".bss" && !usesELFSectionDirectiveForBSS());
126}
127