This is an archive of the discontinued LLVM Phabricator instance.

Patch that implements %hi(sym1 - sym2) and %lo(sym1 - sym2) expressions for MIPS.
ClosedPublic

Authored by sstankovic on Jan 22 2014, 9:09 AM.

Details

Summary

Patch implements %hi(sym1 - sym2) and %lo(sym1 - sym2) expressions for MIPS by creating target expression class MipsMCExpr.

Diff Detail

Event Timeline

Thanks for uploading this via Phabricator. Sorry for being slow to review this again this time around -- I wanted to take the time to try the patch locally and try modifying it.

lib/Target/Mips/AsmParser/MipsAsmParser.cpp
11 ↗(On Diff #6573)

Nit: make sure the #include list stays sorted

1319 ↗(On Diff #6573)

In your previous e-mail, you wrote:

If evaluateRelocExpr() is removed, then following instruction won't assemble: ...

I see what you mean. However, what I mean is that evaluateRelocExpr() shouldn't recurse when creating a MipsMCExpr.

So it would be cleaner to change these two lines to:

const MCSymbolRefExpr *LSRE = dyn_cast<MCSymbolRefExpr>(BE->getLHS());
const MCSymbolRefExpr *RSRE = dyn_cast<MCSymbolRefExpr>(BE->getRHS());

With that, the tests still pass.

1331 ↗(On Diff #6573)

With the change above, you don't need to recreate the two MCSymbolRefExprs here. You can just pass LSRE and RSRE to MCBinaryExpr::Create().

1344 ↗(On Diff #6573)

So then, for clarity, these two lines:

const MCExpr *LExp = evaluateRelocExpr(BE->getLHS(), RelocStr);
const MCExpr *RExp = evaluateRelocExpr(BE->getRHS(), RelocStr);

could be moved down to just before this line.

1374 ↗(On Diff #6573)

This 'default' case can be removed now, after adding the MCExpr::Target case, otherwise it produces the warning "default label in switch which covers all enumeration values [-Wcovered-switch-default]".

lib/Target/Mips/MCTargetDesc/MipsMCCodeEmitter.cpp
377 ↗(On Diff #6573)

Note that this needs to be updated to isMicroMips(STI) on trunk now.

lib/Target/Mips/MCTargetDesc/MipsMCExpr.cpp
35 ↗(On Diff #6573)

This check shouldn't be needed because this function can handle any subexpression.

lib/Target/Mips/MCTargetDesc/MipsMCExpr.h
31 ↗(On Diff #6573)

Nit: you don't need a "_" prefix. You can write "Kind(Kind)" below.

sstankovic updated this revision to Unknown Object (????).Jan 30 2014, 11:04 AM

Patch is updated.

sstankovic updated this revision to Unknown Object (????).Jan 31 2014, 2:14 AM

Fixed the comment.

Thanks for addressing my comments. Just one more thing, about EvaluateAsRelocatableImpl(). I didn't understand that part when I reviewed the change on Wednesday, so I didn't comment on that part then, but having investigated I understand it now.

lib/Target/Mips/AsmParser/MipsAsmParser.cpp
1318 ↗(On Diff #6788)

Nit: With the change below, you could simplify this a little by using isa<> rather than dyn_cast<>.

1324 ↗(On Diff #6788)

Minor issue: You can simplify this further: you don't need to recreate the same MCBinaryExpr. CreateHi() and CreateLo() can be passed Expr rather than Res.

lib/Target/Mips/MCTargetDesc/MipsMCExpr.cpp
37 ↗(On Diff #6788)

In your earlier version of the patch, you had EvaluateAsRelocatableImpl() doing the shifting/masking for hi16. I didn't understand how it worked to remove this part, until I investigated further.

I had a look at the other *MCExpr.{h,cpp} files, and it seems there are there are two approaches to this:

  1. Have EvaluateAsRelocatableImpl() do the shifting/masking. There's only one architecture that does this, PowerPC (see lib/Target/PowerPC/MCTargetDesc/PPCMCExpr.cpp).

This approach would work for MIPS if you:

  • change MipsMCCodeEmitter.cpp's MCExpr::Target handling to push a FK_Data_2 fixup instead of LO16/HI16 fixup;
  • change adjustFixupValue() in MipsAsmBackend.cpp to accept FK_Data_2 in addition to FK_Data_4.

(This works -- I tried it.)

  1. Do the shifting/masking elsewhere, leaving a trivial implementation of EvaluateAsRelocatableImpl().

For two architectures, EvaluateAsRelocatableImpl() just does "return false":
lib/Target/ARM/MCTargetDesc/ARMMCExpr.cpp
lib/Target/NVPTX/NVPTXMCExpr.h

For another two, EvaluateAsRelocatableImpl() delegates trivially to the subexpression:
lib/Target/AArch64/MCTargetDesc/AArch64MCExpr.cpp
lib/Target/Sparc/MCTargetDesc/SparcMCExpr.cpp
i.e.

AArch64MCExpr::EvaluateAsRelocatableImpl(MCValue &Res,
                                         const MCAsmLayout *Layout) const {
  return getSubExpr()->EvaluateAsRelocatable(Res, *Layout);
}

Using approach 2 seems preferable since it's the most common in the codebase. It's what you're doing, except that your EvaluateAsRelocatableImpl() is a lot more complex and is closer to the PPC version.

Based on the AArch64/Sparc versions, you can simplify this to:

MipsMCExpr::EvaluateAsRelocatableImpl(MCValue &Res,
                                      const MCAsmLayout *Layout) const {
  if (!Layout)
    return false;
  return getSubExpr()->EvaluateAsRelocatable(Res, *Layout);
}

So can you change it to that, please?

(The extra Layout check is necessary because getExprOpValue()'s call to EvaluateAsAbsolute() in MipsMCCodeEmitter.cpp causes this to be called with Layout==NULL.)

sstankovic updated this revision to Unknown Object (????).Feb 3 2014, 5:34 AM

Patch is updated.

LGTM. When you commit this, you can put "Differential Revision: http://llvm-reviews.chandlerc.com/D2592" in the commit message to link back to the review.

lib/Target/Mips/AsmParser/MipsAsmParser.cpp
1325 ↗(On Diff #6825)

Nit: you could do "return MipsMCExpr::CreateHi(...)" here

test/MC/Mips/hilo-addressing.s
7 ↗(On Diff #6825)

Can you make this llvm-mc invocation consistent with the previous one? i.e. "%s" at end, and consistently use or omit "-o -".

sstankovic updated this revision to Unknown Object (????).Feb 3 2014, 11:46 AM

Patch is updated.

petarj closed this revision.Feb 4 2014, 10:48 AM

Closed by commit rL200783 (authored by @petarj).