* [Qemu-devel] TCG is hard to understand!
@ 2009-12-10 16:44 Jun Koi
2009-12-10 22:21 ` Andreas Färber
0 siblings, 1 reply; 7+ messages in thread
From: Jun Koi @ 2009-12-10 16:44 UTC (permalink / raw)
To: qemu-devel
Hi,
I am trying to understand how TCG works. For example, I look at the
LLDT insn on x86.
In target-i386/translate.c, we translate LLDT to TCG code, like below:
static TCGv_i32 cpu_tmp2_i32; // 1
...
gen_ldst_modrm(s, modrm, OT_WORD, OR_TMP0, 0); // 2
gen_jmp_im(pc_start - s->cs_base); // 3
tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]); // 4
gen_helper_lldt(cpu_tmp2_i32); // 5
This is quite confused. I understand that:
- In line (2), we retrieve the operand and save it into cpu_T[0].
- Line (3) generates jump code, but I dont understand why we need that ????
- Line (4) generate the code to copy cpu_T[0] to the (local) variable
cpu_tmp2_i32.
However, as tcg_gen_trunc_tl_i32() put the *value* of that variable,
but not its *address*, into the generated code, I dont see how next
line (5) can generate code that use the same variable.
Clearly there is no connection between cpu_tmp2_i32 on line (4) and
line (5), so how the generated code works here??
Thanks a lot,
Jun
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [Qemu-devel] TCG is hard to understand!
2009-12-10 16:44 [Qemu-devel] TCG is hard to understand! Jun Koi
@ 2009-12-10 22:21 ` Andreas Färber
2009-12-11 2:34 ` Jun Koi
0 siblings, 1 reply; 7+ messages in thread
From: Andreas Färber @ 2009-12-10 22:21 UTC (permalink / raw)
To: Jun Koi; +Cc: qemu-devel
Hi,
Am 10.12.2009 um 17:44 schrieb Jun Koi:
> I am trying to understand how TCG works. For example, I look at the
> LLDT insn on x86.
>
> In target-i386/translate.c, we translate LLDT to TCG code, like below:
>
>
> static TCGv_i32
> cpu_tmp2_i32; // 1
> ...
> gen_ldst_modrm(s, modrm, OT_WORD, OR_TMP0, 0); // 2
> gen_jmp_im(pc_start - s->cs_base); // 3
> tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]); // 4
> gen_helper_lldt
> (cpu_tmp2_i32); // 5
>
>
> This is quite confused. I understand that:
[...]
> - Line (4) generate the code to copy cpu_T[0] to the (local) variable
> cpu_tmp2_i32.
> However, as tcg_gen_trunc_tl_i32() put the *value* of that variable,
> but not its *address*, into the generated code, I dont see how next
> line (5) can generate code that use the same variable.
> Clearly there is no connection between cpu_tmp2_i32 on line (4) and
> line (5), so how the generated code works here??
Line 4 generates code to truncate the value from the source associated
with cpu_T[0] and put it into the destination associated with
cpu_tmp2_i32, which may be a general-purpose register on PowerPC for
instance (i.e., not an address). The connection between line 4 and 5
is that cpu_tmp2_i32 has as its value the same identifier (sort of a
handle), allowing TCG internal code to lookup its location.
Btw if the code confuses you, cpu_T[n] is actually a leftover from the
dyngen to TCG conversion. Feel free to provide patches replacing the
remaining occurrences by individual local TCG variables if it helps
your understanding. :)
Andreas
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [Qemu-devel] TCG is hard to understand!
2009-12-10 22:21 ` Andreas Färber
@ 2009-12-11 2:34 ` Jun Koi
2009-12-11 3:02 ` Alexander Graf
0 siblings, 1 reply; 7+ messages in thread
From: Jun Koi @ 2009-12-11 2:34 UTC (permalink / raw)
To: Andreas Färber; +Cc: qemu-devel
On Fri, Dec 11, 2009 at 7:21 AM, Andreas Färber <andreas.faerber@web.de> wrote:
> Hi,
>
> Am 10.12.2009 um 17:44 schrieb Jun Koi:
>
>> I am trying to understand how TCG works. For example, I look at the
>> LLDT insn on x86.
>>
>> In target-i386/translate.c, we translate LLDT to TCG code, like below:
>>
>>
>> static TCGv_i32 cpu_tmp2_i32; // 1
>> ...
>> gen_ldst_modrm(s, modrm, OT_WORD, OR_TMP0, 0); // 2
>> gen_jmp_im(pc_start - s->cs_base); // 3
>> tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]); // 4
>> gen_helper_lldt(cpu_tmp2_i32); // 5
>>
>>
>> This is quite confused. I understand that:
>
> [...]
>>
>> - Line (4) generate the code to copy cpu_T[0] to the (local) variable
>> cpu_tmp2_i32.
>> However, as tcg_gen_trunc_tl_i32() put the *value* of that variable,
>> but not its *address*, into the generated code, I dont see how next
>> line (5) can generate code that use the same variable.
>> Clearly there is no connection between cpu_tmp2_i32 on line (4) and
>> line (5), so how the generated code works here??
>
> Line 4 generates code to truncate the value from the source associated with
> cpu_T[0] and put it into the destination associated with cpu_tmp2_i32, which
> may be a general-purpose register on PowerPC for instance (i.e., not an
> address). The connection between line 4 and 5 is that cpu_tmp2_i32 has as
> its value the same identifier (sort of a handle), allowing TCG internal code
> to lookup its location.
>
> Btw if the code confuses you, cpu_T[n] is actually a leftover from the
> dyngen to TCG conversion. Feel free to provide patches replacing the
> remaining occurrences by individual local TCG variables if it helps your
> understanding. :)
>
Thanks a lot for the insight! Now it is quite clear to me.
However, I still dont understand what the line (3) does. Could you
give some hints?
>> static TCGv_i32 cpu_tmp2_i32; // 1
>> ...
>> gen_ldst_modrm(s, modrm, OT_WORD, OR_TMP0, 0); // 2
>> gen_jmp_im(pc_start - s->cs_base); // 3
>> tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]); // 4
>> gen_helper_lldt(cpu_tmp2_i32); // 5
Thanks,
Jun
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [Qemu-devel] TCG is hard to understand!
2009-12-11 2:34 ` Jun Koi
@ 2009-12-11 3:02 ` Alexander Graf
2009-12-11 3:18 ` Jun Koi
0 siblings, 1 reply; 7+ messages in thread
From: Alexander Graf @ 2009-12-11 3:02 UTC (permalink / raw)
To: Jun Koi; +Cc: Andreas Färber, qemu-devel
On 11.12.2009, at 03:34, Jun Koi wrote:
> On Fri, Dec 11, 2009 at 7:21 AM, Andreas Färber <andreas.faerber@web.de> wrote:
>> Hi,
>>
>> Am 10.12.2009 um 17:44 schrieb Jun Koi:
>>
>>> I am trying to understand how TCG works. For example, I look at the
>>> LLDT insn on x86.
>>>
>>> In target-i386/translate.c, we translate LLDT to TCG code, like below:
>>>
>>>
>>> static TCGv_i32 cpu_tmp2_i32; // 1
>>> ...
>>> gen_ldst_modrm(s, modrm, OT_WORD, OR_TMP0, 0); // 2
>>> gen_jmp_im(pc_start - s->cs_base); // 3
>>> tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]); // 4
>>> gen_helper_lldt(cpu_tmp2_i32); // 5
>>>
>>>
>>> This is quite confused. I understand that:
>>
>> [...]
>>>
>>> - Line (4) generate the code to copy cpu_T[0] to the (local) variable
>>> cpu_tmp2_i32.
>>> However, as tcg_gen_trunc_tl_i32() put the *value* of that variable,
>>> but not its *address*, into the generated code, I dont see how next
>>> line (5) can generate code that use the same variable.
>>> Clearly there is no connection between cpu_tmp2_i32 on line (4) and
>>> line (5), so how the generated code works here??
>>
>> Line 4 generates code to truncate the value from the source associated with
>> cpu_T[0] and put it into the destination associated with cpu_tmp2_i32, which
>> may be a general-purpose register on PowerPC for instance (i.e., not an
>> address). The connection between line 4 and 5 is that cpu_tmp2_i32 has as
>> its value the same identifier (sort of a handle), allowing TCG internal code
>> to lookup its location.
>>
>> Btw if the code confuses you, cpu_T[n] is actually a leftover from the
>> dyngen to TCG conversion. Feel free to provide patches replacing the
>> remaining occurrences by individual local TCG variables if it helps your
>> understanding. :)
>>
>
> Thanks a lot for the insight! Now it is quite clear to me.
>
> However, I still dont understand what the line (3) does. Could you
> give some hints?
>
>>> static TCGv_i32 cpu_tmp2_i32; // 1
>>> ...
>>> gen_ldst_modrm(s, modrm, OT_WORD, OR_TMP0, 0); // 2
>>> gen_jmp_im(pc_start - s->cs_base); // 3
This sets the position counter to the current address. That's important in case a helper command calls an exception, because only then the unrolling works and the IP is actually at the instruction we're processing.
Alex
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [Qemu-devel] TCG is hard to understand!
2009-12-11 3:02 ` Alexander Graf
@ 2009-12-11 3:18 ` Jun Koi
2009-12-11 7:36 ` Laurent Desnogues
0 siblings, 1 reply; 7+ messages in thread
From: Jun Koi @ 2009-12-11 3:18 UTC (permalink / raw)
To: Alexander Graf; +Cc: qemu-devel
Hi Alex,
>> However, I still dont understand what the line (3) does. Could you
>> give some hints?
>>
>>>> static TCGv_i32 cpu_tmp2_i32; // 1
>>>> ...
>>>> gen_ldst_modrm(s, modrm, OT_WORD, OR_TMP0, 0); // 2
>>>> gen_jmp_im(pc_start - s->cs_base); // 3
>
> This sets the position counter to the current address. That's important in case a helper command calls an exception, because only then the unrolling works and the IP is actually at the instruction we're processing.
This is excellent, thanks!
Another question: I look at tcg_gen_callN() to see how the helper is
executed. We put the helper opcode into the TCG code buffer, and put
helper's params into gen_opparam_buf.
However, then when TCG generates code to actually call the helper, we
just put the opcode of the host insn into the output buffer, which is
target code at this step, then run it.
Now when the helper is executed, it must get its param from the stack,
which is really the host stack. But as said above, its params are in
gen_opparam_buf, but not in stack?
I searched around, and dont see anywhere we link gen_opparam_buf with
the host stack. So how the helper can get its param??
Surely I missed something, or misunderstand the whole picture. Any hint?
Thanks a lot,
Jun
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [Qemu-devel] TCG is hard to understand!
2009-12-11 3:18 ` Jun Koi
@ 2009-12-11 7:36 ` Laurent Desnogues
2009-12-11 8:40 ` Jun Koi
0 siblings, 1 reply; 7+ messages in thread
From: Laurent Desnogues @ 2009-12-11 7:36 UTC (permalink / raw)
To: Jun Koi; +Cc: Alexander Graf, qemu-devel
On Fri, Dec 11, 2009 at 4:18 AM, Jun Koi <junkoi2004@gmail.com> wrote:
>
> Another question: I look at tcg_gen_callN() to see how the helper is
> executed. We put the helper opcode into the TCG code buffer, and put
> helper's params into gen_opparam_buf.
>
> However, then when TCG generates code to actually call the helper, we
> just put the opcode of the host insn into the output buffer, which is
> target code at this step, then run it.
>
> Now when the helper is executed, it must get its param from the stack,
> which is really the host stack. But as said above, its params are in
> gen_opparam_buf, but not in stack?
> I searched around, and dont see anywhere we link gen_opparam_buf with
> the host stack. So how the helper can get its param??
>
> Surely I missed something, or misunderstand the whole picture. Any hint?
Take a look at tcg.c:tcg_reg_alloc_call
Laurent
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [Qemu-devel] TCG is hard to understand!
2009-12-11 7:36 ` Laurent Desnogues
@ 2009-12-11 8:40 ` Jun Koi
0 siblings, 0 replies; 7+ messages in thread
From: Jun Koi @ 2009-12-11 8:40 UTC (permalink / raw)
To: Laurent Desnogues; +Cc: Alexander Graf, qemu-devel
On Fri, Dec 11, 2009 at 4:36 PM, Laurent Desnogues
<laurent.desnogues@gmail.com> wrote:
> On Fri, Dec 11, 2009 at 4:18 AM, Jun Koi <junkoi2004@gmail.com> wrote:
>>
>> Another question: I look at tcg_gen_callN() to see how the helper is
>> executed. We put the helper opcode into the TCG code buffer, and put
>> helper's params into gen_opparam_buf.
>>
>> However, then when TCG generates code to actually call the helper, we
>> just put the opcode of the host insn into the output buffer, which is
>> target code at this step, then run it.
>>
>> Now when the helper is executed, it must get its param from the stack,
>> which is really the host stack. But as said above, its params are in
>> gen_opparam_buf, but not in stack?
>> I searched around, and dont see anywhere we link gen_opparam_buf with
>> the host stack. So how the helper can get its param??
>>
>> Surely I missed something, or misunderstand the whole picture. Any hint?
>
> Take a look at tcg.c:tcg_reg_alloc_call
>
Now I see how TCG manipulates the stack memory there.
Thanks,
J
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2009-12-11 8:40 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-12-10 16:44 [Qemu-devel] TCG is hard to understand! Jun Koi
2009-12-10 22:21 ` Andreas Färber
2009-12-11 2:34 ` Jun Koi
2009-12-11 3:02 ` Alexander Graf
2009-12-11 3:18 ` Jun Koi
2009-12-11 7:36 ` Laurent Desnogues
2009-12-11 8:40 ` Jun Koi
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).