<div dir="ltr"><div>Thanks for the detailed explanation, and sorry for the delayed reply.</div><div><br></div><div>The fact that %lo and %hi (VK_Mips_ABS_{HI,LO}) can only be attached to a MCSymbolRefExpr does seem to be a bit wrong.  They are really functions, which should be able to wrap any MCExpr.  The fact that "%hi(x - y)" seems to be converted to "%hi(x) - %hi(y)" by the parser adds to the confusion.  More comments below...</div>
<div class="gmail_extra"><br><div class="gmail_quote">On 24 December 2013 12:39, Sasa Stankovic <span dir="ltr"><<a href="mailto:Sasa.Stankovic@rt-rk.com" target="_blank">Sasa.Stankovic@rt-rk.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div class="im">> 2) Why is it that you handle cases like:<br>

><br>
> .set diff, label2 - label1<br>
> lui $2, %lo(diff)<br>
><br>
> but not cases where that is written more directly as:<br>
><br>
> lui $2, %lo(label2 - label1)<br>
><br>
> (which still gives the UNREACHABLE error I described earlier in the<br>
> thread)?  Is it just because the former is easier to implement?  If so, is<br>
> that just a quirk of how these cases are handled via different code paths<br>
> in MC?<br>
<br>
</div>Implementing %lo(label2 - label1) was first thing that I tried. The<br>
problem was that no MCBinaryExpr constructor takes relocation kind as<br>
argument; relocations can only be passed to MCSymbolRefExpr. Currently,<br>
when MipsAsmParser parses %lo(label2 - label1), it creates MCBinaryExpr<br>
where both operands are MCSymbolRefExpr, and both operands have relocation<br>
kind for %lo relocation (VK_Mips_ABS_LO). This is the reason for<br>
UNREACHABLE error, because there are two identical relocations for the<br>
same offset in .o file.<br>
<br>
I then tried to pass VK_Mips_None instead of VK_Mips_ABS_LO to second<br>
operand, but this also produced couple of UNREACHABLE errors, this time in<br>
Mips specific code.<br>
<br>
Finally, I created target-specific MCExpr subclass, as a subclass of<br>
MCTargetExpr. The implementation was similar to<br>
lib/Target/ARM/MCTargetDesc/ARMMCExpr.h. This class has members for<br>
expression and relocation kind, so associating relocation to expression as<br>
a whole was easy.<br></blockquote><div><br></div><div><div>Thanks for pointing out ARMMCExpr.h.  Taking a similar approach to ARMMCExpr.h does seem like it would be a lot cleaner.</div><div><br></div><div>Grepping for EvaluateAsRelocatableImpl() shows that this does seem to be the normal way to implement hi/lo functions in MC.  The following targets define their own MCExpr subclasses:</div>
<div><br></div><div><div> * lib/Target/AArch64/MCTargetDesc/AArch64MCExpr.h</div><div> * lib/Target/ARM/MCTargetDesc/ARMMCExpr.h<br></div><div> * lib/Target/NVPTX/NVPTXMCExpr.h</div><div> * lib/Target/PowerPC/MCTargetDesc/PPCMCExpr.h (confusingly, this defines VK_PPC_LO, but there's another VK_PPC_LO defined in include/llvm/MC/MCExpr.h)</div>
<div> * lib/Target/Sparc/MCTargetDesc/SparcMCExpr.h</div></div><div><br></div><div>The ARM and NVPTX implementations of EvaluateAsRelocatableImpl() don't delegate to their subexpressions, but the rest do, calling getSubExpr()->EvaluateAsRelocatable(), which is what you'd want to do for a MIPS version so that %lo/%hi work in any context that can accept a constant.</div>
</div><div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">The patch worked, but then people at Mips noted that gas supports<br>

assigning symbol difference to temp symbol and applying %hi and %lo on<br>
that symbol. That seemed much simpler.</blockquote><div><br></div><div>The temp symbol approach might be a smaller patch, but it does seem like a hack that's working around limitations elsewhere.  Can you post the earlier patch you had for adding a Mips MCExpr subclass?</div>
<div><br></div><div>Cheers,</div><div>Mark</div><div><br></div></div></div></div>