Skip to content

Commit

Permalink
[EraVM][AsmParser] Migrate to new asm syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
atrosinenko authored and akiramenai committed Aug 13, 2024
1 parent 8bf1e55 commit 3c4bdc6
Show file tree
Hide file tree
Showing 92 changed files with 1,750 additions and 1,660 deletions.
16 changes: 8 additions & 8 deletions lld/test/ELF/eravm-binary-layout.s
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
; RUN: llvm-objdump --no-leading-addr --disassemble-all --disassemble-zeroes --reloc --syms %t | FileCheck --check-prefix=OUTPUT-HASH %s

.text
nop stack+=[2 + r0]
incsp 2
add code[@glob_initializer], r0, stack[@glob]

.globl get_glob
Expand All @@ -16,11 +16,11 @@ get_glob:
ret

DEFAULT_UNWIND:
ret.panic.to_label @DEFAULT_UNWIND
pncl @DEFAULT_UNWIND
DEFAULT_FAR_RETURN:
ret.ok.to_label r1, @DEFAULT_FAR_RETURN
retl r1, @DEFAULT_FAR_RETURN
DEFAULT_FAR_REVERT:
ret.revert.to_label r1, @DEFAULT_FAR_REVERT
revl r1, @DEFAULT_FAR_REVERT

.data
.globl glob ; @glob
Expand All @@ -45,7 +45,7 @@ glob_initializer:
; OUTPUT-NEXT: 00000010 g .code 00000000 get_glob
; OUTPUT-EMPTY:
; OUTPUT-LABEL: <.code>:
; OUTPUT-NEXT: 00 02 00 00 00 00 00 02 nop stack+=[2 + r0]
; OUTPUT-NEXT: 00 02 00 00 00 00 00 02 incsp 2
; OUTPUT-NEXT: 00 00 00 02 00 00 00 47 add code[2], r0, stack[r0]
; OUTPUT-EMPTY:
; OUTPUT-NEXT: <get_glob>:
Expand All @@ -54,13 +54,13 @@ glob_initializer:
; OUTPUT-NEXT: 00 00 00 00 00 01 04 2d ret
; OUTPUT-EMPTY:
; OUTPUT-NEXT: <DEFAULT_UNWIND>:
; OUTPUT-NEXT: 00 00 00 05 00 00 04 32 ret.panic.to_label 5
; OUTPUT-NEXT: 00 00 00 05 00 00 04 32 pncl 5
; OUTPUT-EMPTY:
; OUTPUT-NEXT: <DEFAULT_FAR_RETURN>:
; OUTPUT-NEXT: 00 00 00 06 00 01 04 2e ret.ok.to_label r1, 6
; OUTPUT-NEXT: 00 00 00 06 00 01 04 2e retl 6
; OUTPUT-EMPTY:
; OUTPUT-NEXT: <DEFAULT_FAR_REVERT>:
; OUTPUT-NEXT: 00 00 00 07 00 01 04 30 ret.revert.to_label r1, 7
; OUTPUT-NEXT: 00 00 00 07 00 01 04 30 revl 7
; OUTPUT-EMPTY:
; OUTPUT-NEXT: <glob_initializer>:
; OUTPUT-NEXT: 00 00 00 00 00 00 00 00
Expand Down
60 changes: 30 additions & 30 deletions lld/test/ELF/eravm-code-reloc.s
Original file line number Diff line number Diff line change
Expand Up @@ -66,51 +66,51 @@ foo_local:
.type caller_g,@function
.globl caller_g
caller_g:
near_call r1, @foo, @handler
far_call r3, r4, @foo
call r1, @foo, @handler
callf r3, r4, @foo
add @.OUTLINED_FUNCTION_RET0, r0, stack-[1]
jump @foo_local
.OUTLINED_FUNCTION_RET0:
jump @label
ret.ok.to_label r1, @label
ret.revert.to_label r1, @label
ret.panic.to_label @label
retl r1, @label
revl r1, @label
pncl @label
.globl label
label:
jump code[@jump_table + 1]
ret
; INPUT-LABEL: <caller_g>:
; INPUT-NEXT: 00 00 00 00 00 01 04 0f near_call r1, 0, 0
; INPUT-NEXT: 00 00 00 00 00 01 04 0f call r1, 0, 0
; INPUT-NEXT: R_ERAVM_16_SCALE_8 handler
; INPUT-NEXT: R_ERAVM_16_SCALE_8 foo
; INPUT-NEXT: 00 00 00 00 00 43 04 21 far_call r3, r4, 0
; INPUT-NEXT: 00 00 00 00 00 43 04 21 callf r3, r4, 0
; INPUT-NEXT: R_ERAVM_16_SCALE_8 foo
; INPUT-NEXT: 00 01 00 00 00 00 00 3d add 0, r0, stack-[1 + r0]
; INPUT-NEXT: R_ERAVM_16_SCALE_8 .text+0x40
; INPUT-NEXT: 00 00 00 00 00 00 01 3d jump 0
; INPUT-NEXT: R_ERAVM_16_SCALE_8 .text+0x18
; INPUT-NEXT: 00 00 00 00 00 00 01 3d jump 0
; INPUT-NEXT: R_ERAVM_16_SCALE_8 label
; INPUT-NEXT: 00 00 00 00 00 01 04 2e ret.ok.to_label r1, 0
; INPUT-NEXT: 00 00 00 00 00 01 04 2e retl 0
; INPUT-NEXT: R_ERAVM_16_SCALE_8 label
; INPUT-NEXT: 00 00 00 00 00 01 04 30 ret.revert.to_label r1, 0
; INPUT-NEXT: 00 00 00 00 00 01 04 30 revl 0
; INPUT-NEXT: R_ERAVM_16_SCALE_8 label
; INPUT-NEXT: 00 00 00 00 00 00 04 32 ret.panic.to_label 0
; INPUT-NEXT: 00 00 00 00 00 00 04 32 pncl 0
; INPUT-NEXT: R_ERAVM_16_SCALE_8 label
; INPUT-LABEL: <label>:
; INPUT-NEXT: 00 00 00 01 00 00 01 3e jump code[1]
; INPUT-NEXT: R_ERAVM_16_SCALE_32 jump_table
; INPUT-NEXT: 00 00 00 00 00 01 04 2d ret

; OUTPUT-LABEL: <caller_g>:
; OUTPUT-NEXT: 00 12 00 13 00 01 04 0f near_call r1, 19, 18
; OUTPUT-NEXT: 00 00 00 13 00 43 04 21 far_call r3, r4, 19
; OUTPUT-NEXT: 00 12 00 13 00 01 04 0f call r1, 19, 18
; OUTPUT-NEXT: 00 00 00 13 00 43 04 21 callf r3, r4, 19
; OUTPUT-NEXT: 00 01 00 18 00 00 00 3d add 24, r0, stack-[1 + r0]
; OUTPUT-NEXT: 00 00 00 13 00 00 01 3d jump 19
; OUTPUT-NEXT: 00 00 00 1c 00 00 01 3d jump 28
; OUTPUT-NEXT: 00 00 00 1c 00 01 04 2e ret.ok.to_label r1, 28
; OUTPUT-NEXT: 00 00 00 1c 00 01 04 30 ret.revert.to_label r1, 28
; OUTPUT-NEXT: 00 00 00 1c 00 00 04 32 ret.panic.to_label 28
; OUTPUT-NEXT: 00 00 00 1c 00 01 04 2e retl 28
; OUTPUT-NEXT: 00 00 00 1c 00 01 04 30 revl 28
; OUTPUT-NEXT: 00 00 00 1c 00 00 04 32 pncl 28
; OUTPUT-LABEL: <label>:
; OUTPUT-NEXT: 00 00 00 02 00 00 01 3e jump code[2]
; OUTPUT-NEXT: 00 00 00 00 00 01 04 2d ret
Expand All @@ -119,43 +119,43 @@ label:
.type caller_l,@function
.globl caller_l
caller_l:
near_call r1, @foo_local, @handler_local
far_call r3, r4, @foo_local
call r1, @foo_local, @handler_local
callf r3, r4, @foo_local
jump @label_local
ret.ok.to_label r1, @label_local
ret.revert.to_label r1, @label_local
ret.panic.to_label @label_local
retl r1, @label_local
revl r1, @label_local
pncl @label_local
.local label_local
label_local:
jump code[@jump_table_local + 1]
ret

; INPUT-LABEL: <caller_l>:
; INPUT-NEXT: 00 00 00 00 00 01 04 0f near_call r1, 0, 0
; INPUT-NEXT: 00 00 00 00 00 01 04 0f call r1, 0, 0
; INPUT-NEXT: R_ERAVM_16_SCALE_8 .text+0x10
; INPUT-NEXT: R_ERAVM_16_SCALE_8 .text+0x18
; INPUT-NEXT: 00 00 00 00 00 43 04 21 far_call r3, r4, 0
; INPUT-NEXT: 00 00 00 00 00 43 04 21 callf r3, r4, 0
; INPUT-NEXT: R_ERAVM_16_SCALE_8 .text+0x18
; INPUT-NEXT: 00 00 00 00 00 00 01 3d jump 0
; INPUT-NEXT: R_ERAVM_16_SCALE_8 .text+0xa0
; INPUT-NEXT: 00 00 00 00 00 01 04 2e ret.ok.to_label r1, 0
; INPUT-NEXT: 00 00 00 00 00 01 04 2e retl 0
; INPUT-NEXT: R_ERAVM_16_SCALE_8 .text+0xa0
; INPUT-NEXT: 00 00 00 00 00 01 04 30 ret.revert.to_label r1, 0
; INPUT-NEXT: 00 00 00 00 00 01 04 30 revl 0
; INPUT-NEXT: R_ERAVM_16_SCALE_8 .text+0xa0
; INPUT-NEXT: 00 00 00 00 00 00 04 32 ret.panic.to_label 0
; INPUT-NEXT: 00 00 00 00 00 00 04 32 pncl 0
; INPUT-NEXT: R_ERAVM_16_SCALE_8 .text+0xa0
; INPUT-LABEL: <label_local>:
; INPUT-NEXT: 00 00 00 01 00 00 01 3e jump code[1]
; INPUT-NEXT: R_ERAVM_16_SCALE_32 .rodata+0x20
; INPUT-NEXT: 00 00 00 00 00 01 04 2d ret

; OUTPUT-LABEL: <caller_l>:
; OUTPUT-NEXT: 00 12 00 13 00 01 04 0f near_call r1, 19, 18
; OUTPUT-NEXT: 00 00 00 13 00 43 04 21 far_call r3, r4, 19
; OUTPUT-NEXT: 00 12 00 13 00 01 04 0f call r1, 19, 18
; OUTPUT-NEXT: 00 00 00 13 00 43 04 21 callf r3, r4, 19
; OUTPUT-NEXT: 00 00 00 24 00 00 01 3d jump 36
; OUTPUT-NEXT: 00 00 00 24 00 01 04 2e ret.ok.to_label r1, 36
; OUTPUT-NEXT: 00 00 00 24 00 01 04 30 ret.revert.to_label r1, 36
; OUTPUT-NEXT: 00 00 00 24 00 00 04 32 ret.panic.to_label 36
; OUTPUT-NEXT: 00 00 00 24 00 01 04 2e retl 36
; OUTPUT-NEXT: 00 00 00 24 00 01 04 30 revl 36
; OUTPUT-NEXT: 00 00 00 24 00 00 04 32 pncl 36
; OUTPUT-LABEL: <label_local>:
; OUTPUT-NEXT: 00 00 00 02 00 00 01 3e jump code[2]
; OUTPUT-NEXT: 00 00 00 00 00 01 04 2d ret
36 changes: 36 additions & 0 deletions llvm/lib/Target/EraVM/AsmParser/EraVMAsmParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ class EraVMAsmParser : public MCTargetAsmParser {
OperandMatchResultTy tryParseStackOperand(OperandVector &Operands);
OperandMatchResultTy tryParseCodeOperand(OperandVector &Operands);

bool parseAdjSP(OperandVector &Operands);

bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
SMLoc NameLoc, OperandVector &Operands) override;

Expand Down Expand Up @@ -687,6 +689,36 @@ EraVMAsmParser::tryParseCodeOperand(OperandVector &Operands) {
return MatchOperand_Success;
}

bool EraVMAsmParser::parseAdjSP(OperandVector &Operands) {
SMLoc S, E;
MCRegister Reg;
int Addend = 0;

while (!parseOptionalToken(AsmToken::EndOfStatement)) {
switch (getTok().getKind()) {
default:
return TokError("unexpected token");
case AsmToken::Identifier:
if (tryParseRegister(Reg, S, E))
return Error(getTok().getLoc(), "cannot parse register");
Operands.push_back(EraVMOperand::CreateReg(Reg, S, E));
break;
case AsmToken::Plus:
Operands.push_back(EraVMOperand::CreateToken("+", getTok().getLoc()));
Lex(); // eat "+" token
break;
case AsmToken::Integer:
Addend = getTok().getIntVal();
S = getTok().getLoc();
E = getTok().getEndLoc();
Operands.push_back(EraVMOperand::CreateImm(createConstant(Addend), S, E));
Lex(); // eat integer token
break;
}
}
return false;
}

bool EraVMAsmParser::ParseInstruction(ParseInstructionInfo &Info,
StringRef Name, SMLoc NameLoc,
OperandVector &Operands) {
Expand All @@ -695,6 +727,10 @@ bool EraVMAsmParser::ParseInstruction(ParseInstructionInfo &Info,

StringRef Mnemonic = static_cast<EraVMOperand &>(*Operands[0]).getToken();
applyMnemonicAliases(Mnemonic, getAvailableFeatures(), /*VariantID=*/0);

if (Mnemonic == "incsp" || Mnemonic == "decsp")
return parseAdjSP(Operands);

auto ParseOne = [this, Mnemonic, &Operands]() {
return parseOperand(Mnemonic, Operands);
};
Expand Down
3 changes: 2 additions & 1 deletion llvm/lib/Target/EraVM/EraVMAsmPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,8 @@ void EraVMAsmPrinter::emitDefaultLandingPads() {

// Creates instruction:
//
// nop stack+=[StackSize + r0]
// incsp StackSize
//
static MCInst createIncSPInst(int64_t StackSize) {
assert(StackSize >= 0);
MCInst MCI;
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Target/EraVM/EraVMInstrFormats.td
Original file line number Diff line number Diff line change
Expand Up @@ -767,8 +767,8 @@ class IFarCall<EraVMOpcode opcode,

string Modifiers =
!cond(!eq({is_static, is_shard}, {0, 0}): "",
!eq({is_static, is_shard}, {1, 0}): ".static",
!eq({is_static, is_shard}, {0, 1}): ".shard",
!eq({is_static, is_shard}, {1, 0}): ".st",
!eq({is_static, is_shard}, {0, 1}): ".sh",
!eq({is_static, is_shard}, {1, 1}): ".st.sh",
true: ".INVALID");
let AsmString = !strconcat(opcode.Name, Modifiers, "${cc}", "\t", asmstr);
Expand Down
25 changes: 14 additions & 11 deletions llvm/lib/Target/EraVM/EraVMInstrInfo.td
Original file line number Diff line number Diff line change
Expand Up @@ -401,9 +401,15 @@ def NOPsrs : Isr_s<OpNoOp, NoSwap, PreserveFlags, []>;

def : Pat<(EraVMadd_to_sp GR256:$reg), (NOPrrs R0, R0, R0, $reg, 0, 0)>;

def : InstAlias<"nop${cc}\t${dst0}", (NOPrrs R0, R0, stackout:$dst0, pred:$cc)>;
def : InstAlias<"nop${cc}\t${src0}", (NOPsrr R0, stackin:$src0, R0, pred:$cc)>;
def : InstAlias<"nop${cc}", (NOPrrr R0, R0, R0, pred:$cc)>;
def : InstAlias<"incsp${cc}\t${reg}", (NOPrrs R0, R0, R0, GR256:$reg, 0, pred:$cc)>;
def : InstAlias<"incsp${cc}\t${imm}", (NOPrrs R0, R0, R0, R0, i16imm:$imm, pred:$cc)>;
def : InstAlias<"incsp${cc}\t${reg}+${imm}", (NOPrrs R0, R0, R0, GR256:$reg, i16imm:$imm, pred:$cc)>;

def : InstAlias<"decsp${cc}\t${reg}", (NOPsrr R0, R0, GR256:$reg, 0, R0, pred:$cc)>;
def : InstAlias<"decsp${cc}\t${imm}", (NOPsrr R0, R0, R0, i16imm:$imm, R0, pred:$cc)>;
def : InstAlias<"decsp${cc}\t${reg}+${imm}", (NOPsrr R0, R0, GR256:$reg, i16imm:$imm, R0, pred:$cc)>;

def : InstAlias<"nop${cc}", (NOPrrr R0, R0, R0, pred:$cc)>;

// Note that while technically commutative arithmetic instructions are not swappable,
// it is not a problem as there will be no corresponding instruction defined with SwapOperand = 1
Expand Down Expand Up @@ -945,13 +951,10 @@ def THROW : Pseudo<(outs), (ins GR256:$rs0, pred:$cc), [(EraVMthrow GR256:$rs0)]
PseudoInstExpansion<(REVERTr GR256:$rs0, pred:$cc)>;

def : InstAlias<"ret${cc}", (RETr R1, pred:$cc)>;
def : InstAlias<"revert${cc}", (REVERTr R1, pred:$cc)>;
def : InstAlias<"rev${cc}", (REVERTr R1, pred:$cc)>;

// TODO Make these aliases preferred for printing after switching to the new syntax.
let EmitPriority = 0 in {
def : InstAlias<"ret.ok.to_label${cc} $dest", (RETrl R1, jmptarget:$dest, pred:$cc)>;
def : InstAlias<"ret.revert.to_label${cc} $dest", (REVERTrl R1, jmptarget:$dest, pred:$cc)>;
}
def : InstAlias<"retl${cc} $dest", (RETrl R1, jmptarget:$dest, pred:$cc)>;
def : InstAlias<"revl${cc} $dest", (REVERTrl R1, jmptarget:$dest, pred:$cc)>;

def : Pat<(EraVMreturn GR256:$rs0), (RETrl GR256:$rs0, (default_far_return 0))>;
def : Pat<(EraVMrevert GR256:$rs0), (REVERTrl GR256:$rs0, (default_far_revert 0))>;
Expand Down Expand Up @@ -1034,7 +1037,7 @@ let Defs = [R1, R2, R3, R4, R5, R6, R7, R8, R9, R10, R11, R12, R13, R14, R15, Fl

// Unfortunately, we cannot encode @DEFAULT_UNWIND_DEST here, so we have to alias to pseudo
def NEAR_CALL_default_unwind : AsmParserPseudo<(outs), (ins GR256:$in1, jmptarget:$callee),
"near_call", "${in1} ${callee}">;
"call", "${in1} ${callee}">;

defm FAR_CALL : FarCallInst<OpFarcall>;
defm DELEGATE_CALL : FarCallInst<OpDelegate>;
Expand All @@ -1049,7 +1052,7 @@ foreach Op = [OpFarcall, OpDelegate, OpMimic] in {
!strconcat(Op.Name, ".st.sh")>;
}

def : InstAlias<"near_call${cc} ${callee}",
def : InstAlias<"call${cc} ${callee}",
(NEAR_CALL_default_unwind R0, jmptarget:$callee, pred:$cc)>;

def : Pat<(EraVMfarcall bb:$unwind), (FAR_CALLrrl R1, R2, bb:$unwind)>;
Expand Down
Loading

0 comments on commit 3c4bdc6

Please sign in to comment.