Skip to content

Commit

Permalink
[SPARC][IAS] Add named V9 ASI tag constants for memory instructions
Browse files Browse the repository at this point in the history
This adds named ASI tag constants (such as #ASI_P and #ASI_P_L) for memory
accesses.

This patch adds 64-bit/V9 tag names, given that currently the majority of SPARC
software targets that arch.

Support for 32-bit/V8 tag names will be added in a future patch.

Reviewed By: barannikov88

Differential Revision: https://reviews.llvm.org/D157235
  • Loading branch information
koachan authored and brad0 committed Sep 5, 2023
1 parent 8037deb commit bc45acb
Show file tree
Hide file tree
Showing 15 changed files with 398 additions and 125 deletions.
40 changes: 28 additions & 12 deletions llvm/lib/Target/Sparc/AsmParser/SparcAsmParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1113,17 +1113,33 @@ OperandMatchResultTy SparcAsmParser::parseASITag(OperandVector &Operands) {
SMLoc E = Parser.getTok().getEndLoc();
int64_t ASIVal = 0;

if (getParser().parseAbsoluteExpression(ASIVal)) {
Error(
S,
is64Bit()
? "malformed ASI tag, must be %asi or a constant integer expression"
: "malformed ASI tag, must be a constant integer expression");
return MatchOperand_ParseFail;
}
if (is64Bit() && (getLexer().getKind() == AsmToken::Hash)) {
// For now we only support named tags for 64-bit/V9 systems.
// TODO: add support for 32-bit/V8 systems.
SMLoc TagStart = getLexer().peekTok(false).getLoc();
Parser.Lex(); // Eat the '#'.
auto ASIName = Parser.getTok().getString();
auto ASITag = SparcASITag::lookupASITagByName(ASIName);
if (!ASITag)
ASITag = SparcASITag::lookupASITagByAltName(ASIName);
Parser.Lex(); // Eat the identifier token.

if (!isUInt<8>(ASIVal)) {
Error(S, "invalid ASI number, must be between 0 and 255");
if (!ASITag) {
Error(TagStart, "unknown ASI tag");
return MatchOperand_ParseFail;
}

ASIVal = ASITag->Encoding;
} else if (!getParser().parseAbsoluteExpression(ASIVal)) {
if (!isUInt<8>(ASIVal)) {
Error(S, "invalid ASI number, must be between 0 and 255");
return MatchOperand_ParseFail;
}
} else {
Error(S, is64Bit()
? "malformed ASI tag, must be %asi, a constant integer "
"expression, or a named tag"
: "malformed ASI tag, must be a constant integer expression");
return MatchOperand_ParseFail;
}

Expand Down Expand Up @@ -1239,8 +1255,8 @@ SparcAsmParser::parseOperand(OperandVector &Operands, StringRef Mnemonic) {
return MatchOperand_Success;
}

Error(S,
"malformed ASI tag, must be %asi or a constant integer expression");
Error(S, "malformed ASI tag, must be %asi, a constant integer "
"expression, or a named tag");
return MatchOperand_ParseFail;
}

Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Target/Sparc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ tablegen(LLVM SparcGenDisassemblerTables.inc -gen-disassembler)
tablegen(LLVM SparcGenInstrInfo.inc -gen-instr-info)
tablegen(LLVM SparcGenMCCodeEmitter.inc -gen-emitter)
tablegen(LLVM SparcGenRegisterInfo.inc -gen-register-info)
tablegen(LLVM SparcGenSearchableTables.inc -gen-searchable-tables)
tablegen(LLVM SparcGenSubtargetInfo.inc -gen-subtarget)

add_public_tablegen_target(SparcCommonTableGen)
Expand Down
6 changes: 5 additions & 1 deletion llvm/lib/Target/Sparc/MCTargetDesc/SparcInstPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,5 +255,9 @@ void SparcInstPrinter::printMembarTag(const MCInst *MI, int opNum,
void SparcInstPrinter::printASITag(const MCInst *MI, int opNum,
const MCSubtargetInfo &STI, raw_ostream &O) {
unsigned Imm = MI->getOperand(opNum).getImm();
O << Imm;
auto ASITag = SparcASITag::lookupASITagByEncoding(Imm);
if (isV9(STI) && ASITag)
O << '#' << ASITag->Name;
else
O << Imm;
}
7 changes: 7 additions & 0 deletions llvm/lib/Target/Sparc/MCTargetDesc/SparcMCTargetDesc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@
#include "llvm/MC/TargetRegistry.h"
#include "llvm/Support/ErrorHandling.h"

namespace llvm {
namespace SparcASITag {
#define GET_ASITagsList_IMPL
#include "SparcGenSearchableTables.inc"
} // end namespace SparcASITag
} // end namespace llvm

using namespace llvm;

#define GET_INSTRINFO_MC_DESC
Expand Down
13 changes: 13 additions & 0 deletions llvm/lib/Target/Sparc/MCTargetDesc/SparcMCTargetDesc.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#ifndef LLVM_LIB_TARGET_SPARC_MCTARGETDESC_SPARCMCTARGETDESC_H
#define LLVM_LIB_TARGET_SPARC_MCTARGETDESC_SPARCMCTARGETDESC_H

#include "llvm/ADT/StringRef.h"
#include "llvm/Support/DataTypes.h"

#include <memory>
Expand All @@ -35,6 +36,18 @@ MCAsmBackend *createSparcAsmBackend(const Target &T, const MCSubtargetInfo &STI,
const MCTargetOptions &Options);
std::unique_ptr<MCObjectTargetWriter> createSparcELFObjectWriter(bool Is64Bit,
uint8_t OSABI);

// Defines symbolic names for Sparc v9 ASI tag names.
namespace SparcASITag {
struct ASITag {
const char *Name;
const char *AltName;
unsigned Encoding;
};

#define GET_ASITagsList_DECL
#include "SparcGenSearchableTables.inc"
} // end namespace SparcASITag
} // End llvm namespace

// Defines symbolic names for Sparc registers. This defines a mapping from
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Target/Sparc/Sparc.td
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ include "LeonFeatures.td"
// Register File, Calling Conv, Instruction Descriptions
//===----------------------------------------------------------------------===//

include "SparcASITags.td"
include "SparcRegisterInfo.td"
include "SparcCallingConv.td"
include "SparcSchedule.td"
Expand Down
54 changes: 54 additions & 0 deletions llvm/lib/Target/Sparc/SparcASITags.td
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//===- SparcASITags.td -------------------------------------*- tablegen -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file defines the symbolic operands permitted for various kinds of
// SPARCv9 ASI.
//
//===----------------------------------------------------------------------===//

include "llvm/TableGen/SearchableTable.td"

class ASITag<string name, string alt_name, bits<8> op> {
string Name = name;
// A maximum of one alias is supported right now.
string AltName = alt_name;
bits<8> Encoding = op;
}

def ASITagsList : GenericTable {
let FilterClass = "ASITag";
let Fields = ["Name", "AltName", "Encoding"];

let PrimaryKey = [ "Encoding" ];
let PrimaryKeyName = "lookupASITagByEncoding";
}

def lookupASITagByName : SearchIndex {
let Table = ASITagsList;
let Key = [ "Name" ];
}

def lookupASITagByAltName : SearchIndex {
let Table = ASITagsList;
let Key = [ "AltName" ];
}

def : ASITag<"ASI_N", "ASI_NUCLEUS", 0x4>;
def : ASITag<"ASI_N_L", "ASI_NUCLEUS_LITTLE", 0xC>;
def : ASITag<"ASI_AIUP", "ASI_AS_IF_USER_PRIMARY", 0x10>;
def : ASITag<"ASI_AIUS", "ASI_AS_IF_USER_SECONDARY", 0x11>;
def : ASITag<"ASI_AIUP_L", "ASI_AS_IF_USER_PRIMARY_LITTLE", 0x18>;
def : ASITag<"ASI_AIUS_L", "ASI_AS_IF_USER_SECONDARY_LITTLE", 0x19>;
def : ASITag<"ASI_P", "ASI_PRIMARY", 0x80>;
def : ASITag<"ASI_S", "ASI_SECONDARY", 0x81>;
def : ASITag<"ASI_PNF", "ASI_PRIMARY_NOFAULT", 0x82>;
def : ASITag<"ASI_SNF", "ASI_SECONDARY_NOFAULT", 0x83>;
def : ASITag<"ASI_P_L", "ASI_PRIMARY_LITTLE", 0x88>;
def : ASITag<"ASI_S_L", "ASI_SECONDARY_LITTLE", 0x89>;
def : ASITag<"ASI_PNF_L", "ASI_PRIMARY_NOFAULT_LITTLE", 0x8A>;
def : ASITag<"ASI_SNF_L", "ASI_SECONDARY_NOFAULT_LITTLE", 0x8B>;
34 changes: 17 additions & 17 deletions llvm/test/MC/Disassembler/Sparc/sparc-mem.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# CHECK: ldsb [%g1], %o4
0xd8 0x48 0x40 0x00

# CHECK: ldsba [%i0+%l6] 131, %o2
# CHECK: ldsba [%i0+%l6] #ASI_SNF, %o2
0xd4 0xce 0x10 0x76

# CHECK: ldsh [%i0+%l6], %o2
Expand All @@ -27,7 +27,7 @@
# CHECK: ldsh [%g1], %o4
0xd8 0x50 0x40 0x00

# CHECK: ldsha [%i0+%l6] 131, %o2
# CHECK: ldsha [%i0+%l6] #ASI_SNF, %o2
0xd4 0xd6 0x10 0x76

# CHECK: ldub [%i0+%l6], %o2
Expand All @@ -42,7 +42,7 @@
# CHECK: ldub [%g1], %o2
0xd4 0x08 0x40 0x00

# CHECK: lduba [%i0+%l6] 131, %o2
# CHECK: lduba [%i0+%l6] #ASI_SNF, %o2
0xd4 0x8e 0x10 0x76

# CHECK: lduh [%i0+%l6], %o2
Expand All @@ -57,7 +57,7 @@
# CHECK: lduh [%g1], %o2
0xd4 0x10 0x40 0x00

# CHECK: lduha [%i0+%l6] 131, %o2
# CHECK: lduha [%i0+%l6] #ASI_SNF, %o2
0xd4 0x96 0x10 0x76

# CHECK: ld [%i0+%l6], %o2
Expand All @@ -72,7 +72,7 @@
# CHECK: ld [%g1], %o2
0xd4 0x00 0x40 0x00

# CHECK: lda [%i0+%l6] 131, %o2
# CHECK: lda [%i0+%l6] #ASI_SNF, %o2
0xd4 0x86 0x10 0x76

# CHECK: ld [%i0+%l6], %f2
Expand All @@ -87,7 +87,7 @@
# CHECK: ld [%g1], %f2
0xc5 0x00 0x40 0x00

# CHECK: lda [%i0+%l6] 131, %f2
# CHECK: lda [%i0+%l6] #ASI_SNF, %f2
0xc5 0x86 0x10 0x76

# CHECK: ldd [%i0+%l6], %f2
Expand All @@ -102,7 +102,7 @@
# CHECK: ldd [%g1], %f2
0xc5 0x18 0x40 0x00

# CHECK: ldda [%i0+%l6] 131, %f2
# CHECK: ldda [%i0+%l6] #ASI_SNF, %f2
0xc5 0x9e 0x10 0x76

# CHECK: ldq [%i0+%l6], %f4
Expand Down Expand Up @@ -153,7 +153,7 @@
# CHECK: stb %o2, [%g1]
0xd4 0x28 0x40 0x00

# CHECK: stba %o2, [%i0+%l6] 131
# CHECK: stba %o2, [%i0+%l6] #ASI_SNF
0xd4 0xae 0x10 0x76

# CHECK: sth %o2, [%i0+%l6]
Expand All @@ -168,7 +168,7 @@
# CHECK: sth %o2, [%g1]
0xd4 0x30 0x40 0x00

# CHECK: stha %o2, [%i0+%l6] 131
# CHECK: stha %o2, [%i0+%l6] #ASI_SNF
0xd4 0xb6 0x10 0x76

# CHECK: st %o2, [%i0+%l6]
Expand All @@ -183,7 +183,7 @@
# CHECK: st %o2, [%g1]
0xd4 0x20 0x40 0x00

# CHECK: sta %o2, [%i0+%l6] 131
# CHECK: sta %o2, [%i0+%l6] #ASI_SNF
0xd4 0xa6 0x10 0x76

# CHECK: st %f2, [%i0+%l6]
Expand All @@ -198,7 +198,7 @@
# CHECK: st %f2, [%g1]
0xc5 0x20 0x40 0x00

# CHECK: sta %f2, [%i0+%l6] 131
# CHECK: sta %f2, [%i0+%l6] #ASI_SNF
0xc5 0xa6 0x10 0x76

# CHECK: std %f2, [%i0+%l6]
Expand All @@ -213,7 +213,7 @@
# CHECK: std %f2, [%g1]
0xc5 0x38 0x40 0x00

# CHECK: stda %f2, [%i0+%l6] 131
# CHECK: stda %f2, [%i0+%l6] #ASI_SNF
0xc5 0xbe 0x10 0x76

# CHECK: stq %f4, [%i0+%l6]
Expand Down Expand Up @@ -252,10 +252,10 @@
# CHECK: swap [%g1], %o2
0xd4 0x78 0x40 0x00

# CHECK: swapa [%i0+%l6] 131, %o2
# CHECK: swapa [%i0+%l6] #ASI_SNF, %o2
0xd4 0xfe 0x10 0x76

# CHECK: swapa [%g1] 131, %o2
# CHECK: swapa [%g1] #ASI_SNF, %o2
0xd4 0xf8 0x50 0x60

# CHECK: ldd [%i0+%l6], %o2
Expand All @@ -282,7 +282,7 @@
# CHECK: std %o2, [%g1]
0xd4 0x38 0x40 0x00

# CHECK: stda %o2, [%i0+%l6] 131
# CHECK: stda %o2, [%i0+%l6] #ASI_SNF
0xd4 0xbe 0x10 0x76

# CHECK: ldstub [%i0+%l6], %o2
Expand All @@ -297,10 +297,10 @@
# CHECK: ldstub [%g1], %o2
0xd4 0x68 0x40 0x00

# CHECK: ldstuba [%i0+%l6] 131, %o2
# CHECK: ldstuba [%i0+%l6] #ASI_SNF, %o2
0xd4 0xee 0x10 0x76

# CHECK: ldstuba [%g1] 131, %o2
# CHECK: ldstuba [%g1] #ASI_SNF, %o2
0xd4 0xe8 0x50 0x60

# CHECK: flush %g1+%g2
Expand Down
34 changes: 34 additions & 0 deletions llvm/test/MC/Disassembler/Sparc/sparc-v9-asi.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# RUN: llvm-mc --disassemble %s -triple=sparcv9-unknown-linux | FileCheck %s --check-prefix=V9

## Disassembly prefers alternate mnemonic over explicit ASI tags
## and short over long ASI tag names.
# V9: casxa [%i0] #ASI_N, %l6, %o2
0xd5 0xf6 0x00 0x96
# V9: casxa [%i0] #ASI_N_L, %l6, %o2
0xd5 0xf6 0x01 0x96
# V9: casxa [%i0] #ASI_AIUP, %l6, %o2
0xd5 0xf6 0x02 0x16
# V9: casxa [%i0] #ASI_AIUS, %l6, %o2
0xd5 0xf6 0x02 0x36
# V9: casxa [%i0] #ASI_AIUP_L, %l6, %o2
0xd5 0xf6 0x03 0x16
# V9: casxa [%i0] #ASI_AIUS_L, %l6, %o2
0xd5 0xf6 0x03 0x36
## casx == casxa #ASI_P
# V9: casx [%i0], %l6, %o2
0xd5 0xf6 0x10 0x16
# V9: casxa [%i0] #ASI_S, %l6, %o2
0xd5 0xf6 0x10 0x36
# V9: casxa [%i0] #ASI_PNF, %l6, %o2
0xd5 0xf6 0x10 0x56
# V9: casxa [%i0] #ASI_SNF, %l6, %o2
0xd5 0xf6 0x10 0x76
## casxl == casxa #ASI_L
# V9: casxl [%i0], %l6, %o2
0xd5 0xf6 0x11 0x16
# V9: casxa [%i0] #ASI_S_L, %l6, %o2
0xd5 0xf6 0x11 0x36
# V9: casxa [%i0] #ASI_PNF_L, %l6, %o2
0xd5 0xf6 0x11 0x56
# V9: casxa [%i0] #ASI_SNF_L, %l6, %o2
0xd5 0xf6 0x11 0x76
Loading

0 comments on commit bc45acb

Please sign in to comment.