Skip to content

Commit

Permalink
[RISCV] Support .variant_cc directive for the assembler.
Browse files Browse the repository at this point in the history
The patch is split from D103435. The patch supported a new directive .variant_cc
that annotates function with STO_RISCV_VARIANT_CC. Symbols marked with
STO_RISCV_VARIANT_CC do not use standard calling conversion or use parameter not
passed in GPR/FPR.

Related: riscv-non-isa/riscv-elf-psabi-doc#190

Initial authored by: HsiangKai

Reviewed By: MaskRay

Differential Revision: https://reviews.llvm.org/D138352
  • Loading branch information
yetingk committed Dec 5, 2022
1 parent 89fae41 commit 3a88121
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 0 deletions.
16 changes: 16 additions & 0 deletions llvm/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp
Expand Up @@ -180,6 +180,7 @@ class RISCVAsmParser : public MCTargetAsmParser {
bool parseDirectiveOption();
bool parseDirectiveAttribute();
bool parseDirectiveInsn(SMLoc L);
bool parseDirectiveVariantCC();

void setFeatureBits(uint64_t Feature, StringRef FeatureString) {
if (!(getSTI().getFeatureBits()[Feature])) {
Expand Down Expand Up @@ -2035,6 +2036,8 @@ bool RISCVAsmParser::ParseDirective(AsmToken DirectiveID) {
return parseDirectiveAttribute();
if (IDVal == ".insn")
return parseDirectiveInsn(DirectiveID.getLoc());
if (IDVal == ".variant_cc")
return parseDirectiveVariantCC();

return true;
}
Expand Down Expand Up @@ -2297,6 +2300,19 @@ bool RISCVAsmParser::parseDirectiveInsn(SMLoc L) {
/*MatchingInlineAsm=*/false);
}

/// parseDirectiveVariantCC
/// ::= .variant_cc symbol
bool RISCVAsmParser::parseDirectiveVariantCC() {
StringRef Name;
if (getParser().parseIdentifier(Name))
return TokError("expected symbol name");
if (parseEOL())
return false;
getTargetStreamer().emitDirectiveVariantCC(
*getContext().getOrCreateSymbol(Name));
return false;
}

void RISCVAsmParser::emitToStreamer(MCStreamer &S, const MCInst &Inst) {
MCInst CInst;
bool Res = compressInst(CInst, Inst, getSTI(), S.getContext());
Expand Down
5 changes: 5 additions & 0 deletions llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.cpp
Expand Up @@ -187,6 +187,11 @@ void RISCVTargetELFStreamer::reset() {
Contents.clear();
}

void RISCVTargetELFStreamer::emitDirectiveVariantCC(MCSymbol &Symbol) {
getStreamer().getAssembler().registerSymbol(Symbol);
cast<MCSymbolELF>(Symbol).setOther(ELF::STO_RISCV_VARIANT_CC);
}

namespace {
class RISCVELFStreamer : public MCELFStreamer {
static std::pair<unsigned, unsigned> getRelocPairForSize(unsigned Size) {
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.h
Expand Up @@ -106,6 +106,7 @@ class RISCVTargetELFStreamer : public RISCVTargetStreamer {
void emitDirectiveOptionNoRVC() override;
void emitDirectiveOptionRelax() override;
void emitDirectiveOptionNoRelax() override;
void emitDirectiveVariantCC(MCSymbol &Symbol) override;

void finish() override;
};
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Target/RISCV/MCTargetDesc/RISCVTargetStreamer.cpp
Expand Up @@ -32,6 +32,7 @@ void RISCVTargetStreamer::emitDirectiveOptionRVC() {}
void RISCVTargetStreamer::emitDirectiveOptionNoRVC() {}
void RISCVTargetStreamer::emitDirectiveOptionRelax() {}
void RISCVTargetStreamer::emitDirectiveOptionNoRelax() {}
void RISCVTargetStreamer::emitDirectiveVariantCC(MCSymbol &Symbol) {}
void RISCVTargetStreamer::emitAttribute(unsigned Attribute, unsigned Value) {}
void RISCVTargetStreamer::finishAttributeSection() {}
void RISCVTargetStreamer::emitTextAttribute(unsigned Attribute,
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Target/RISCV/MCTargetDesc/RISCVTargetStreamer.h
Expand Up @@ -33,6 +33,7 @@ class RISCVTargetStreamer : public MCTargetStreamer {
virtual void emitDirectiveOptionNoRVC();
virtual void emitDirectiveOptionRelax();
virtual void emitDirectiveOptionNoRelax();
virtual void emitDirectiveVariantCC(MCSymbol &Symbol);
virtual void emitAttribute(unsigned Attribute, unsigned Value);
virtual void finishAttributeSection();
virtual void emitTextAttribute(unsigned Attribute, StringRef String);
Expand Down
42 changes: 42 additions & 0 deletions llvm/test/MC/RISCV/directive-variant_cc.s
@@ -0,0 +1,42 @@
// RUN: llvm-mc -triple riscv64 -filetype obj -o - %s | llvm-readobj --symbols - | FileCheck %s
// RUN: llvm-mc -triple riscv64 -filetype obj -defsym=OBJ=1 -o - %s | llvm-readelf -s - | FileCheck %s --check-prefix=OBJ
// RUN: not llvm-mc -triple riscv64 -filetype asm -defsym=ERR=1 -o - %s 2>&1 | FileCheck %s --check-prefix=ERR

.text
.variant_cc local
local:

// CHECK: Name: local
// CHECK: Other [ (0x80)

.ifdef OBJ
/// Binding directive before .variant_cc.
.global def1
.variant_cc def1
def1:

/// Binding directive after .variant_cc.
.variant_cc def2
.weak def2
def2:

.globl alias_def1
.set alias_def1, def1

.variant_cc undef

// OBJ: NOTYPE LOCAL DEFAULT [VARIANT_CC] [[#]] local
// OBJ-NEXT: NOTYPE GLOBAL DEFAULT [VARIANT_CC] [[#]] def1
// OBJ-NEXT: NOTYPE WEAK DEFAULT [VARIANT_CC] [[#]] def2
// OBJ-NEXT: NOTYPE GLOBAL DEFAULT [[#]] alias_def1
// OBJ-NEXT: NOTYPE GLOBAL DEFAULT [VARIANT_CC] UND undef
.endif

.ifdef ERR
.variant_cc
// ERR: [[#@LINE-1]]:12: error: expected symbol name

.global fox
.variant_cc fox bar
// ERR: [[#@LINE-1]]:17: error: expected newline
.endif

0 comments on commit 3a88121

Please sign in to comment.