This is an archive of the discontinued LLVM Phabricator instance.

[M68k] Add `TRAP`, `TRAPV`, `BKPT`, `ILLEGAL` instructions
ClosedPublic

Authored by ids1024 on Mar 28 2023, 8:17 PM.

Details

Summary

This makes it possible to use TRAP to make Linux system calls using inline assembly for instance.

This does not validate that the 4 and 3 bit immediates taken by TRAP and BKPT are in range. But it seems the backend isn't validating the range of immediates in general.

Diff Detail

Event Timeline

ids1024 created this revision.Mar 28 2023, 8:17 PM
Herald added a project: Restricted Project. · View Herald TranscriptMar 28 2023, 8:17 PM
Herald added a subscriber: hiraditya. · View Herald Transcript
ids1024 requested review of this revision.Mar 28 2023, 8:17 PM
Herald added a project: Restricted Project. · View Herald TranscriptMar 28 2023, 8:17 PM
myhsu added a comment.EditedApr 1 2023, 7:31 PM

Thank you for the patch!
First, could you add MC tests? If you're not familiar, I believe test/MC/M68k/Bits/Classes/MxBTST_RR.s and test/MC/Disassembler/M68k/bits.txt are good examples.

This does not validate that the 4 and 3 bit immediates taken by TRAP and BKPT are in range. But it seems the backend isn't validating the range of immediates in general.

This can be done by adding a new immediate Operand with a different AsmOperandClass, instead of using MxImm. In this new AsmOperandClass you can use different PredicateMethod / RenderMethod / ParserMethod to validate the range.

myhsu added inline comments.Apr 1 2023, 7:36 PM
llvm/lib/Target/M68k/M68kInstrControl.td
356

nit: could you add a simple banner here like

//===------------===//
// Trap / Breakpoint
//===------------===//

This does not validate that the 4 and 3 bit immediates taken by TRAP and BKPT are in range. But it seems the backend isn't validating the range of immediates in general.

This can be done by adding a new immediate Operand with a different AsmOperandClass, instead of using MxImm. In this new AsmOperandClass you can use different PredicateMethod / RenderMethod / ParserMethod to validate the range.

Ah, so this would be best done with another AsmOperandClass? And not somehow using MxImm and MxOp. I'm still trying to understand how these sorts of things are handled in tablegen.

With the current version here, it accepts __asm__ ("bkpt #6666666666666666"); without error. So Mxi8imm doesn't seem to be correctly validating that the immediate fits in 8 bits.

myhsu added a comment.Apr 1 2023, 8:42 PM

This does not validate that the 4 and 3 bit immediates taken by TRAP and BKPT are in range. But it seems the backend isn't validating the range of immediates in general.

This can be done by adding a new immediate Operand with a different AsmOperandClass, instead of using MxImm. In this new AsmOperandClass you can use different PredicateMethod / RenderMethod / ParserMethod to validate the range.

Ah, so this would be best done with another AsmOperandClass? And not somehow using MxImm and MxOp. I'm still trying to understand how these sorts of things are handled in tablegen.

I don't think it's necessary to have a MxOp alternative because you can do something like:

// New AsmOperandClass for 3-bit vector
def MxVec3Imm : AsmOperandClass { ... }
...
let ParserMatchClass = MxVec3Imm in
def MxVec3imm  : MxOp<i8,  MxSize8,  "i">;

With the current version here, it accepts __asm__ ("bkpt #6666666666666666"); without error. So Mxi8imm doesn't seem to be correctly validating that the immediate fits in 8 bits.

Right, this is something we need to fix in another patch, thanks for pointing it out :-)

ids1024 added a comment.EditedApr 3 2023, 8:45 PM

Looks like the Imm union member is currently not set anywhere. It's only used in M68kOperand::print. M68kOperand::createImm sets the Expr union variant.

Edit: But it looks like Expr->evaluateAsAbsolute(imm)) works to get the immediate value to validate it. So not a problem here, but the definition of print looks wrong.

ids1024 updated this revision to Diff 510684.Apr 3 2023, 9:39 PM

This now validates that the immediates are in range. This also adds tests, and a banner at the start of this section of the .td file.

myhsu added inline comments.Apr 3 2023, 9:59 PM
llvm/lib/Target/M68k/AsmParser/M68kAsmParser.cpp
54

is this used anywhere?

181

could you add some simple comments for these two new functions?

359
364

isInt<4>(imm) might be better, see https://llvm.org/doxygen/MathExtras_8h.html.

llvm/lib/Target/M68k/M68kInstrControl.td
359–371

RenderMethod and ParserMethod can be factored out:

let RenderMethod = "...", ParserMethod = "..." in {
  def MxTrapImm : AsmOperandClass {
    let Name = "MxtrapImm";
    let PredicateMethod = "isTrapImm";
  }

  def MxBkptImm : AsmOperandClass {
    let Name = "MxBkptImm";
    let PredicateMethod = "isBkptImm";
  }
}
360

super nit: MxTrapImm?

myhsu added a comment.Apr 3 2023, 10:37 PM

Another thing I just found out is the absent of surrounding code in the patch ("Context not available."). Please use git show -U999999 ... > patch.diff if you manually generated the patch. See https://llvm.org/docs/Phabricator.html#requesting-a-review-via-the-web-interface

ids1024 updated this revision to Diff 510697.Apr 3 2023, 10:53 PM

Code style, comment, and unused function declaration.

myhsu accepted this revision.Apr 4 2023, 9:18 AM

LGTM Thank you!
I will commit the patch on your behalf, please tell me your preferred name and email.

This revision is now accepted and ready to land.Apr 4 2023, 9:18 AM

LGTM Thank you!
I will commit the patch on your behalf, please tell me your preferred name and email.

Ian Douglas Scott <ian@iandouglasscott.com>

This revision was automatically updated to reflect the committed changes.