<html><head><meta http-equiv="Content-Type" content="text/html; charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">BTW: If you work on the MI level, then I recommend to use a debug build of llvm and to pass -verify-machineinstrs to llc and it should catch you using registers that are not part of the instructions register classes.<div class=""><br class=""></div><div class="">- Matthias<br class=""><div><br class=""><blockquote type="cite" class=""><div class="">On Jun 26, 2018, at 1:13 PM, Craig Topper via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" class="">llvm-dev@lists.llvm.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">This shouldn't have parsed. <div class=""><br class=""></div><div class="">

<span style="background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline" class="">movq    (%gs), %r14</span> </div><div class=""><br class=""></div><div class="">

<span style="background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline" class="">That's trying to use%gs as a base register which isn't valid. GNU assembler rejects it. And coincidentally llvm-mc started rejecting it on trunk late last week.</span>  That's probably why it printed as %ebp.</div><div class=""><br class=""></div><div class="">I don't know if there is an instruction to read the base of %gs directly. Maybe rdgsbase, but that's only available on Ivy Bridge and later CPUs.. But ussing %gs as part of the memory address for any other instruction is automatically relative to the base of %gs.</div><div class=""><br class=""></div><div class=""> 
<br clear="all" class=""><div class=""><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature">~Craig</div></div><br class=""></div></div><br class=""><div class="gmail_quote"><div dir="ltr" class="">On Tue, Jun 26, 2018 at 12:57 PM K Jelesnianski <<a href="mailto:kjski@vt.edu" class="">kjski@vt.edu</a>> wrote:<br class=""></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Dear Craig,<br class="">
<br class="">
Thanks for the help so far. I have rewritten my assembly to comply<br class="">
with user-land not being able to directly modify the segment registers<br class="">
%GS/%FS. I used llvm-mc with -show-inst to get the equivalent LLVM<br class="">
instruction + operands. Now I am working backwards to actually code<br class="">
this assembly into my MachineFunctionPass and got the easy assembly<br class="">
implemented, however my more complicated asm is still struggling as I<br class="">
am still seeing 0x0(%rbp) instead of (%gs) or errors.<br class="">
Core question here being: how do I properly create BuildMI statements<br class="">
for assembly dealing with offsets?<br class="">
-------------------------------------------------------------------------------------------------<br class="">
Assembly I want to translate:<br class="">
mov   (%gs), %r14                  //get value off %GS base addresss<br class="">
mov %r15, %gs:0x0(%r14)     //put value in R15 into R14:(%GS)  [ (%GS) + R14 ]<br class="">
--------------------------------------------------------------------------------------------------<br class="">
LLVM-MC -show inst gives:<br class="">
movq    (%gs), %r14          # <MCInst #1810 MOV64rm<br class="">
                                        #  <MCOperand Reg:117><br class="">
                                        #  <MCOperand Reg:33><br class="">
                                        #  <MCOperand Imm:1><br class="">
                                        #  <MCOperand Reg:0><br class="">
                                        #  <MCOperand Imm:0><br class="">
                                        #  <MCOperand Reg:0>><br class="">
movq    %r15, %gs:(%r14)        # <MCInst #1803 MOV64mr<br class="">
                                        #  <MCOperand Reg:117><br class="">
                                        #  <MCOperand Imm:1><br class="">
                                        #  <MCOperand Reg:0><br class="">
                                        #  <MCOperand Imm:0><br class="">
                                        #  <MCOperand Reg:33><br class="">
                                        #  <MCOperand Reg:118>><br class="">
-------------------------------------------------------------------------------------------------------<br class="">
I'll be honest and say I don't really know how to add the operands<br class="">
properly to BuildMI. I figured out the following so far<br class="">
MachineInstrBuilder thing = BuildMI(MachineBB, Position in MBB ,<br class="">
DebugLoc(not sure what this accomplishes), TII->get( X86 instruction I<br class="">
want), where instruction result goes)<br class="">
<br class="">
this has .add(MachineOperand)<br class="">
            .addReg(X86::a reg macro)<br class="">
            .addIMM(a constant like 0x8)<br class="">
            and a few more I dont think apply to me.<br class="">
<br class="">
but I am not sure I must follow a specific order? I am assuming yes<br class="">
and it has something to do with X86InstrInfo.td definitions, but not<br class="">
sure.<br class="">
--------------------------------------------------------------------------------------------------------<br class="">
LLVM C++ code I tried to translate this to:<br class="">
/* 1 mov   (%gs), %r14 */<br class="">
    MachineInstrBuilder e1 =<br class="">
BuildMI(MBB,MBB.end(),DL,TII->get(X86::MOV64rm),X86::R14)<br class="">
       .addReg(X86::GS);<br class="">
/* 2 mov %r15, %gs:0x0(%r14) */<br class="">
    MachineOperand baseReg = MachineOperand::CreateReg(X86::GS,false);<br class="">
    MachineOperand scaleAmt = MachineOperand::CreateImm(0x1);<br class="">
    MachineOperand indexReg = MachineOperand::CreateReg(X86::R14,false);<br class="">
    MachineOperand disp = MachineOperand::CreateImm(0x0);<br class="">
<br class="">
    BuildMI(MBB, MBB.end(), DL, TII->get(X86::MOV64mr))<br class="">
      .add(baseReg)<br class="">
      .add(scaleAmt)<br class="">
      .add(indexReg);<br class="">
<br class="">
/* both instructions give the following error<br class="">
<br class="">
clang-6.0: /LLVM6.0.0/llvm/include/llvm/ADT/SmallVector.h:154: const<br class="">
T& llvm::SmallVectorTemplateCommon<T, <template-parameter-1-2><br class="">
>::operator[](llvm::SmallVectorTemplateCommon<T,<br class="">
<template-parameter-1-2> >::size_type) const [with T =<br class="">
llvm::MCOperand; <template-parameter-1-2> = void;<br class="">
llvm::SmallVectorTemplateCommon<T, <template-parameter-1-2><br class="">
>::const_reference = const llvm::MCOperand&;<br class="">
llvm::SmallVectorTemplateCommon<T, <template-parameter-1-2><br class="">
>::size_type = long unsigned int]: Assertion `idx < size()' failed.<br class="">
<br class="">
I saw this function in the code base but not sure what it does<br class="">
"addDirectMem(MachineInstructionBuilder_thing, register you want to<br class="">
use);"<br class="">
<br class="">
<br class="">
This is be the last bit of information I think I need to finish up<br class="">
this implementation. Thanks again for your help!<br class="">
<br class="">
Sincerely,<br class="">
<br class="">
Chris Jelesnianski<br class="">
<br class="">
On Sat, Jun 23, 2018 at 11:32 PM, Craig Topper <<a href="mailto:craig.topper@gmail.com" target="_blank" class="">craig.topper@gmail.com</a>> wrote:<br class="">
> The size suffix thing is a weird quirk in our assembler I should look into<br class="">
> fixing. Instructions in at&t syntax usually have a size suffix that is often<br class="">
> optional<br class="">
><br class="">
> For example:<br class="">
>   add %ax, %bx<br class="">
> and<br class="">
>   addw %ax, %bx<br class="">
><br class="">
> Are equivalent because the register name indicates the size.<br class="">
><br class="">
> but for an instruction like this<br class="">
>   addw $1, (%ax)<br class="">
><br class="">
> There is nothing to infer the size from so an explicit suffix is required.<br class="">
><br class="">
> So for an instruction like "add %ax, %bx" from above, we try to guess the<br class="">
> size suffix from the register. In your case, you used a segment register<br class="">
> which we couldn't guess the size from. And then we printed a bad error<br class="">
> message.<br class="">
><br class="">
> There's no quick reference as such for the meaning of the various<br class="">
> X86::XXXXXX names. But the complete list of them is in<br class="">
> lib/Target/X86/X86GenInstrInfo.inc in your build area. The names are meant<br class="">
> to be fairly straight forward to understand. The first part of the name<br class="">
> should almost always be the instruction name from the Intel/AMD manuals. The<br class="">
> lower case letters at the end sort of convey operand types, but often not<br class="">
> the number of operands even though it looks that way. The most common<br class="">
> letters are 'r' for register, 'm' for memory and 'i' for immediate. Numbers<br class="">
> after 'i' specify the size of the immediate if its important to distinguish<br class="">
> from other sizes or different than the size of the instruction. The lower<br class="">
> case letters are most useful to distinguish different instructions from each<br class="">
> other. So for example, if two instructions only differ in the lower case<br class="">
> letters and one says "rr" and one says "rm", the first is the register form<br class="">
> and the second is the memory form of the same instruction.<br class="">
><br class="">
> ~Craig<br class="">
><br class="">
><br class="">
> On Sat, Jun 23, 2018 at 7:55 PM K Jelesnianski <<a href="mailto:kjski@vt.edu" target="_blank" class="">kjski@vt.edu</a>> wrote:<br class="">
>><br class="">
>> Dear Craig,<br class="">
>><br class="">
>> Thank you super much for the quick reply! Yea I'm still new to working<br class="">
>> on the back-end and that sounds great. I already have the raw assembly<br class="">
>> of what I want to accomplish so this is perfect. I just tried it and<br class="">
>> yea, I will have to break down my assembly even further to more<br class="">
>> simpler operations. You're right about my assembly dealing with<br class="">
>> segment registers as I'm getting the following error:<br class="">
>> "error: unknown use of instruction mnemonic without a size suffix"<br class="">
>><br class="">
>> Just curious, what does it mean by size suffix??<br class="">
>><br class="">
>> It's super cool to see the equivalent with "-show-inst"!!! Thank you<br class="">
>> so much for this help!<br class="">
>><br class="">
>> Last note, I know that the definitions (e.g. def SUB32ri) of the<br class="">
>> various instructions can be found in the various ****.td, but is there<br class="">
>> documentation where the meaning or quick reference of every<br class="">
>> X86::XXXXXX llvm instruction macro can found, so I can quickly pick<br class="">
>> and choose which actual macro I need to use, to "work forwards" rather<br class="">
>> than working backwards by writing the assembly first and using llvm-mc<br class="">
>> -show-inst  ??<br class="">
>><br class="">
>> Thanks super much again.<br class="">
>><br class="">
>> Sincerely,<br class="">
>><br class="">
>> Chris Jelesnianski<br class="">
>> Graduate Research Assistant<br class="">
>> Virginia Tech<br class="">
>><br class="">
>> On Sat, Jun 23, 2018 at 8:45 PM, Craig Topper <<a href="mailto:craig.topper@gmail.com" target="_blank" class="">craig.topper@gmail.com</a>><br class="">
>> wrote:<br class="">
>> > More specifically there is no instruction that can add/subtract segment<br class="">
>> > registers. They can only be updated my the mov segment register<br class="">
>> > instructions, opcodes 0x8c and 0x8e in x86 assembly.<br class="">
>> ><br class="">
>> > I suggest you write the text version of the assembly you want to<br class="">
>> > generate<br class="">
>> > and assemble it with llvm-mc. This will tell you if its even valid.<br class="">
>> > After<br class="">
>> > that you can use -show-inst to print the names of the instructions that<br class="">
>> > X86<br class="">
>> > uses that you can give to BuildMI.<br class="">
>> ><br class="">
>> > ~Craig<br class="">
>> ><br class="">
>> ><br class="">
>> > On Sat, Jun 23, 2018 at 5:36 PM Craig Topper <<a href="mailto:craig.topper@gmail.com" target="_blank" class="">craig.topper@gmail.com</a>><br class="">
>> > wrote:<br class="">
>> >><br class="">
>> >> The SUB32ri can't instruction can't operate on segment registers. It<br class="">
>> >> operates on EAX/EBX/EDX/ECX/EBP, etc. When it gets encoded only 3 or 4<br class="">
>> >> bits<br class="">
>> >> of the register value make it into the binary encoding. Objdump just<br class="">
>> >> extracts those 3 or 4 bits back out and prints one of the<br class="">
>> >> EAX/EBX/EDX/ECX/EBP registers that those bits correspond to.<br class="">
>> >><br class="">
>> >> ~Craig<br class="">
>> >><br class="">
>> >><br class="">
>> >> On Sat, Jun 23, 2018 at 5:28 PM K Jelesnianski via llvm-dev<br class="">
>> >> <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank" class="">llvm-dev@lists.llvm.org</a>> wrote:<br class="">
>> >>><br class="">
>> >>> Dear All,<br class="">
>> >>><br class="">
>> >>> Currently I am trying to inject custom x86-64 assembly into a<br class="">
>> >>> functions entry basic block. More specifically, I am trying to build<br class="">
>> >>> assembly in a machine function pass from scratch.<br class="">
>> >>><br class="">
>> >>> While the dumped machine function instruction info displays that %gs<br class="">
>> >>> will be used, when I perform objdump -d on my executable I am see that<br class="">
>> >>> %gs is replaced by %ebp? Why is this happening?<br class="">
>> >>><br class="">
>> >>> I know it probably has something to do with me not specifying operands<br class="">
>> >>> properly, but I cannot find enough documentation on this besides<br class="">
>> >>> looking through code comments such as X86BaseInfo.cpp. I feel there<br class="">
>> >>> isn't enough for me to be able to connect the dots.<br class="">
>> >>><br class="">
>> >>> Below I have sample code: %gs holds a base address to a memory<br class="">
>> >>> location where I am trying to store information. I am trying to update<br class="">
>> >>> the %gs register pointer location before saving more values, etc.<br class="">
>> >>><br class="">
>> >>> LLVM C++ codeMachine Function pass code:<br class="">
>> >>> MachineInstrBuilder sss = BuildMI(MBB, MBB.begin(), DL,<br class="">
>> >>> TII->get(X86::SUB32ri),X86::GS)<br class="">
>> >>>                     .addReg(X86::GS)<br class="">
>> >>>                     .addImm(0x8);<br class="">
>> >>><br class="">
>> >>> machine function pass dump:<br class="">
>> >>>  %gs = SUB32ri %gs, 8, implicit-def %eflags<br class="">
>> >>><br class="">
>> >>> Objdump -d assembly from executable<br class="">
>> >>>   400510:   81 ed 04 00 00 00       sub    $0x8,%ebp<br class="">
>> >>><br class="">
>> >>><br class="">
>> >>> TLDR: I am trying to create custom assembly via BuildMI() and<br class="">
>> >>> manipulate<br class="">
>> >>> segment<br class="">
>> >>> registers via a MachineFunctionPass.<br class="">
>> >>><br class="">
>> >>> I have looked at LLVMs safestack implementation, but they are taking a<br class="">
>> >>> fairly complicated hybrid approach between an IR Function pass with<br class="">
>> >>> Backend support. I would like to stay as a single machinefunction<br class="">
>> >>> pass.<br class="">
>> >>><br class="">
>> >>> Believe me I would do this at the IR level if I didnt need to<br class="">
>> >>> specifically use the segment registers.<br class="">
>> >>><br class="">
>> >>> Thanks for the help in advance!<br class="">
>> >>><br class="">
>> >>> Sincerely,<br class="">
>> >>><br class="">
>> >>> Christopher Jelesnianski<br class="">
>> >>> Graduate Research Assistant<br class="">
>> >>> Virginia Tech<br class="">
>> >>> _______________________________________________<br class="">
>> >>> LLVM Developers mailing list<br class="">
>> >>> <a href="mailto:llvm-dev@lists.llvm.org" target="_blank" class="">llvm-dev@lists.llvm.org</a><br class="">
>> >>> <a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank" class="">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br class="">
</blockquote></div>
_______________________________________________<br class="">LLVM Developers mailing list<br class=""><a href="mailto:llvm-dev@lists.llvm.org" class="">llvm-dev@lists.llvm.org</a><br class="">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev<br class=""></div></blockquote></div><br class=""></div></body></html>