llvm/lib/MC/MCSectionWasm.cpp
Dan Gohman d660a5d68c [WebAssembly] Add skeleton MC support for the Wasm container format
This just adds the basic skeleton for supporting a new object file format.
All of the actual encoding will be implemented in followup patches.

Differential Revision: https://reviews.llvm.org/D26722


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@295803 91177308-0d34-0410-b5e6-96231b3b80d8
2017-02-22 01:23:18 +00:00

98 lines
2.5 KiB
C++

//===- lib/MC/MCSectionWasm.cpp - Wasm Code Section Representation --------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "llvm/MC/MCSectionWasm.h"
#include "llvm/MC/MCAsmInfo.h"
#include "llvm/MC/MCContext.h"
#include "llvm/MC/MCExpr.h"
#include "llvm/MC/MCSymbol.h"
#include "llvm/Support/raw_ostream.h"
using namespace llvm;
MCSectionWasm::~MCSectionWasm() {} // anchor.
// Decides whether a '.section' directive
// should be printed before the section name.
bool MCSectionWasm::ShouldOmitSectionDirective(StringRef Name,
const MCAsmInfo &MAI) const {
return MAI.shouldOmitSectionDirective(Name);
}
static void printName(raw_ostream &OS, StringRef Name) {
if (Name.find_first_not_of("0123456789_."
"abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ") == Name.npos) {
OS << Name;
return;
}
OS << '"';
for (const char *B = Name.begin(), *E = Name.end(); B < E; ++B) {
if (*B == '"') // Unquoted "
OS << "\\\"";
else if (*B != '\\') // Neither " or backslash
OS << *B;
else if (B + 1 == E) // Trailing backslash
OS << "\\\\";
else {
OS << B[0] << B[1]; // Quoted character
++B;
}
}
OS << '"';
}
void MCSectionWasm::PrintSwitchToSection(const MCAsmInfo &MAI, const Triple &T,
raw_ostream &OS,
const MCExpr *Subsection) const {
if (ShouldOmitSectionDirective(SectionName, MAI)) {
OS << '\t' << getSectionName();
if (Subsection) {
OS << '\t';
Subsection->print(OS, &MAI);
}
OS << '\n';
return;
}
OS << "\t.section\t";
printName(OS, getSectionName());
OS << ",\"";
// TODO: Print section flags.
OS << '"';
OS << ',';
// If comment string is '@', e.g. as on ARM - use '%' instead
if (MAI.getCommentString()[0] == '@')
OS << '%';
else
OS << '@';
// TODO: Print section type.
if (isUnique())
OS << ",unique," << UniqueID;
OS << '\n';
if (Subsection) {
OS << "\t.subsection\t";
Subsection->print(OS, &MAI);
OS << '\n';
}
}
bool MCSectionWasm::UseCodeAlign() const { return false; }
bool MCSectionWasm::isVirtualSection() const { return false; }