[XCOFF] handle string constants generation for AIX

This follows https://www.ibm.com/docs/en/aix/7.2?topic=constants-string

Reviewed By: hubert.reinterpretcast

Differential Revision: https://reviews.llvm.org/D101280
This commit is contained in:
Chen Zheng 2021-05-07 06:19:29 +00:00
parent 26e916334e
commit a95473c563
5 changed files with 78 additions and 23 deletions

View File

@ -357,9 +357,15 @@ protected:
LCOMM::LCOMMType LCOMMDirectiveAlignmentType = LCOMM::NoAlignment; LCOMM::LCOMMType LCOMMDirectiveAlignmentType = LCOMM::NoAlignment;
/// True if the target only has basename for .file directive. False if the /// True if the target only has basename for .file directive. False if the
/// target also needs the directory along with the basename. Default to true. /// target also needs the directory along with the basename. Defaults to true.
bool HasBasenameOnlyForFileDirective = true; bool HasBasenameOnlyForFileDirective = true;
/// True if the target represents string constants as mostly raw characters in
/// paired double quotation with paired double quotation marks as the escape
/// mechanism to represent a double quotation mark within the string. Defaults
/// to false.
bool HasPairedDoubleQuoteStringConstants = false;
// True if the target allows .align directives on functions. This is true for // True if the target allows .align directives on functions. This is true for
// most targets, so defaults to true. // most targets, so defaults to true.
bool HasFunctionAlignment = true; bool HasFunctionAlignment = true;
@ -697,6 +703,9 @@ public:
bool hasBasenameOnlyForFileDirective() const { bool hasBasenameOnlyForFileDirective() const {
return HasBasenameOnlyForFileDirective; return HasBasenameOnlyForFileDirective;
} }
bool hasPairedDoubleQuoteStringConstants() const {
return HasPairedDoubleQuoteStringConstants;
}
bool hasFunctionAlignment() const { return HasFunctionAlignment; } bool hasFunctionAlignment() const { return HasFunctionAlignment; }
bool hasDotTypeDotSizeDirective() const { return HasDotTypeDotSizeDirective; } bool hasDotTypeDotSizeDirective() const { return HasDotTypeDotSizeDirective; }
bool hasSingleParameterDotFile() const { return HasSingleParameterDotFile; } bool hasSingleParameterDotFile() const { return HasSingleParameterDotFile; }

View File

@ -20,6 +20,11 @@ MCAsmInfoXCOFF::MCAsmInfoXCOFF() {
IsLittleEndian = false; IsLittleEndian = false;
HasVisibilityOnlyWithLinkage = true; HasVisibilityOnlyWithLinkage = true;
HasBasenameOnlyForFileDirective = false; HasBasenameOnlyForFileDirective = false;
// For XCOFF, string constant consists of any number of characters enclosed in
// "" (double quotation marks).
HasPairedDoubleQuoteStringConstants = true;
PrivateGlobalPrefix = "L.."; PrivateGlobalPrefix = "L..";
PrivateLabelPrefix = "L.."; PrivateLabelPrefix = "L..";
SupportsQuotedNames = false; SupportsQuotedNames = false;

View File

@ -60,6 +60,13 @@ class MCAsmStreamer final : public MCStreamer {
unsigned UseDwarfDirectory : 1; unsigned UseDwarfDirectory : 1;
void EmitRegisterName(int64_t Register); void EmitRegisterName(int64_t Register);
void PrintQuotedString(StringRef Data, raw_ostream &OS) const;
void printDwarfFileDirective(unsigned FileNo, StringRef Directory,
StringRef Filename,
Optional<MD5::MD5Result> Checksum,
Optional<StringRef> Source,
bool UseDwarfDirectory,
raw_svector_ostream &OS) const;
void emitCFIStartProcImpl(MCDwarfFrameInfo &Frame) override; void emitCFIStartProcImpl(MCDwarfFrameInfo &Frame) override;
void emitCFIEndProcImpl(MCDwarfFrameInfo &Frame) override; void emitCFIEndProcImpl(MCDwarfFrameInfo &Frame) override;
@ -1039,33 +1046,53 @@ static void PrintByteList(StringRef Data, raw_ostream &OS,
llvm_unreachable("Invalid AsmCharLiteralSyntax value!"); llvm_unreachable("Invalid AsmCharLiteralSyntax value!");
} }
static void PrintQuotedString(StringRef Data, raw_ostream &OS) { void MCAsmStreamer::PrintQuotedString(StringRef Data, raw_ostream &OS) const {
OS << '"'; OS << '"';
for (unsigned i = 0, e = Data.size(); i != e; ++i) { if (MAI->hasPairedDoubleQuoteStringConstants()) {
unsigned char C = Data[i]; for (unsigned i = 0, e = Data.size(); i != e; ++i) {
if (C == '"' || C == '\\') { unsigned char C = Data[i];
OS << '\\' << (char)C; if (C == '"')
continue; OS << "\"\"";
else
OS << (char)C;
} }
} else {
for (unsigned i = 0, e = Data.size(); i != e; ++i) {
unsigned char C = Data[i];
if (C == '"' || C == '\\') {
OS << '\\' << (char)C;
continue;
}
if (isPrint((unsigned char)C)) { if (isPrint((unsigned char)C)) {
OS << (char)C; OS << (char)C;
continue; continue;
} }
switch (C) { switch (C) {
case '\b': OS << "\\b"; break; case '\b':
case '\f': OS << "\\f"; break; OS << "\\b";
case '\n': OS << "\\n"; break; break;
case '\r': OS << "\\r"; break; case '\f':
case '\t': OS << "\\t"; break; OS << "\\f";
break;
case '\n':
OS << "\\n";
break;
case '\r':
OS << "\\r";
break;
case '\t':
OS << "\\t";
break;
default: default:
OS << '\\'; OS << '\\';
OS << toOctal(C >> 6); OS << toOctal(C >> 6);
OS << toOctal(C >> 3); OS << toOctal(C >> 3);
OS << toOctal(C >> 0); OS << toOctal(C >> 0);
break; break;
}
} }
} }
@ -1390,12 +1417,10 @@ void MCAsmStreamer::emitFileDirective(StringRef Filename) {
EmitEOL(); EmitEOL();
} }
static void printDwarfFileDirective(unsigned FileNo, StringRef Directory, void MCAsmStreamer::printDwarfFileDirective(
StringRef Filename, unsigned FileNo, StringRef Directory, StringRef Filename,
Optional<MD5::MD5Result> Checksum, Optional<MD5::MD5Result> Checksum, Optional<StringRef> Source,
Optional<StringRef> Source, bool UseDwarfDirectory, raw_svector_ostream &OS) const {
bool UseDwarfDirectory,
raw_svector_ostream &OS) {
SmallString<128> FullPathName; SmallString<128> FullPathName;
if (!UseDwarfDirectory && !Directory.empty()) { if (!UseDwarfDirectory && !Directory.empty()) {

View File

@ -0,0 +1,8 @@
; RUN: llc -verify-machineinstrs -mtriple powerpc-ibm-aix-xcoff < %s \
; RUN: | FileCheck %s
; RUN: llc -verify-machineinstrs -mtriple powerpc64-ibm-aix-xcoff < %s \
; RUN: | FileCheck %s
; CHECK: .file "1""2.c"
source_filename = "1\222.c"

View File

@ -0,0 +1,8 @@
; RUN: llc -verify-machineinstrs -mtriple powerpc-ibm-aix-xcoff < %s \
; RUN: | FileCheck %s
; RUN: llc -verify-machineinstrs -mtriple powerpc64-ibm-aix-xcoff < %s \
; RUN: | FileCheck %s
; CHECK: .file "1'2.c"
source_filename = "1'2.c"