Thread overview | |||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
January 09, 2017 Mixin in Inline Assembly | ||||
---|---|---|---|---|
| ||||
Right now I'm working on a project where I'm implementing a VM in D. I'm on the rotate instructions, and realized I could *almost* abstract the ror and rol instructions with the following function private void rot(string ins)(int *op1, int op2) { int tmp = *op1; asm { mov EAX, tmp; // I'd also like to know if I could just load *op1 directly into EAX mov ECX, op2[EBP]; mixin(ins ~ " EAX, CL;"); // Issue here mov tmp, EAX; } *op1 = tmp; } However, the inline assembler doesn't like me trying to do a mixin. Is there a way around this? (There is a reason op1 is a pointer instead of a ref int, please don't ask about it) |
January 09, 2017 Re: Mixin in Inline Assembly | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chris M. | On Monday, 9 January 2017 at 02:31:42 UTC, Chris M. wrote:
> Right now I'm working on a project where I'm implementing a VM in D. I'm on the rotate instructions, and realized I could *almost* abstract the ror and rol instructions with the following function
>
> private void rot(string ins)(int *op1, int op2)
> {
> int tmp = *op1;
> asm
> {
> mov EAX, tmp; // I'd also like to know if I could just load *op1 directly into EAX
> mov ECX, op2[EBP];
> mixin(ins ~ " EAX, CL;"); // Issue here
> mov tmp, EAX;
> }
> *op1 = tmp;
> }
>
> However, the inline assembler doesn't like me trying to do a mixin. Is there a way around this?
>
> (There is a reason op1 is a pointer instead of a ref int, please don't ask about it)
Yes make the whole inline asm a mixin.
|
January 09, 2017 Re: Mixin in Inline Assembly | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chris M. | On Monday, 9 January 2017 at 02:31:42 UTC, Chris M. wrote:
> However, the inline assembler doesn't like me trying to do a mixin.
yep. iasm is completely independent from other fronted, it has it's own lexer, parser and so on. don't expect those things to work. the only way is to mixin the whole iasm block, including `asm{}`.
|
January 09, 2017 Re: Mixin in Inline Assembly | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stefan Koch | On Monday, 9 January 2017 at 02:38:01 UTC, Stefan Koch wrote:
> On Monday, 9 January 2017 at 02:31:42 UTC, Chris M. wrote:
>> [...]
>
> Yes make the whole inline asm a mixin.
Awesome, got it working. Thanks to both replies.
|
January 09, 2017 Re: Mixin in Inline Assembly | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chris M. | On Monday, 9 January 2017 at 02:31:42 UTC, Chris M. wrote:
> asm
> {
> mov EAX, tmp; // I'd also like to know if I could just load *op1 directly into EAX
> mov ECX, op2[EBP];
> mixin(ins ~ " EAX, CL;"); // Issue here
> mov tmp, EAX;
> }
> *op1 = tmp;
> }
>
> However, the inline assembler doesn't like me trying to do a mixin. Is there a way around this?
'
You should be able to break it up too
asm {
mov EAX, tmp;
}
mixin("asm { "~ ins ~ "EAX, CL;" }");
asm {
move tmp, EAX;
}
you get the idea. It should compile to the same thing.
|
January 10, 2017 Re: Mixin in Inline Assembly | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chris M. | On Monday, 9 January 2017 at 02:31:42 UTC, Chris M. wrote:
> Right now I'm working on a project where I'm implementing a VM in D. I'm on the rotate instructions, and realized I could *almost* abstract the ror and rol instructions with the following function
>
> private void rot(string ins)(int *op1, int op2)
> {
> int tmp = *op1;
> asm
> {
> mov EAX, tmp; // I'd also like to know if I could just load *op1 directly into EAX
> mov ECX, op2[EBP];
> mixin(ins ~ " EAX, CL;"); // Issue here
> mov tmp, EAX;
> }
> *op1 = tmp;
> }
don't forget to flag
asm pure nothrow {}
otherwise it's slow.
|
January 10, 2017 Re: Mixin in Inline Assembly | ||||
---|---|---|---|---|
| ||||
Posted in reply to Basile B. | On Tuesday, 10 January 2017 at 10:41:54 UTC, Basile B. wrote:
>
> don't forget to flag
>
> asm pure nothrow {}
>
> otherwise it's slow.
Why?
|
January 10, 2017 Re: Mixin in Inline Assembly | ||||
---|---|---|---|---|
| ||||
Posted in reply to Guillaume Piolat | On Tuesday, 10 January 2017 at 11:38:43 UTC, Guillaume Piolat wrote: > On Tuesday, 10 January 2017 at 10:41:54 UTC, Basile B. wrote: >> >> don't forget to flag >> >> asm pure nothrow {} >> >> otherwise it's slow. > > Why? It's an empirical observation. In september I tried to get why an inline asm function was slow. What happened was that I didn't mark the asm block as nothrow https://forum.dlang.org/post/xznocpxtalpayvkrwxey@forum.dlang.org I opened an issue asking the specifications to explain that clearly. |
January 10, 2017 Re: Mixin in Inline Assembly | ||||
---|---|---|---|---|
| ||||
Posted in reply to Basile B. | On Tuesday, 10 January 2017 at 13:13:17 UTC, Basile B. wrote:
> On Tuesday, 10 January 2017 at 11:38:43 UTC, Guillaume Piolat wrote:
>> On Tuesday, 10 January 2017 at 10:41:54 UTC, Basile B. wrote:
>>>
>>> don't forget to flag
>>>
>>> asm pure nothrow {}
>>>
>>> otherwise it's slow.
>>
>> Why?
>
> It's an empirical observation. In september I tried to get why an inline asm function was slow. What happened was that I didn't mark the asm block as nothrow
>
> https://forum.dlang.org/post/xznocpxtalpayvkrwxey@forum.dlang.org
>
> I opened an issue asking the specifications to explain that clearly.
Interesting, thanks.
|
January 11, 2017 Re: Mixin in Inline Assembly | ||||
---|---|---|---|---|
| ||||
Posted in reply to Basile B. | On Tuesday, 10 January 2017 at 13:13:17 UTC, Basile B. wrote:
> On Tuesday, 10 January 2017 at 11:38:43 UTC, Guillaume Piolat wrote:
>> On Tuesday, 10 January 2017 at 10:41:54 UTC, Basile B. wrote:
>>>
>>> don't forget to flag
>>>
>>> asm pure nothrow {}
>>>
>>> otherwise it's slow.
>>
>> Why?
>
> It's an empirical observation. In september I tried to get why an inline asm function was slow. What happened was that I didn't mark the asm block as nothrow
>
> https://forum.dlang.org/post/xznocpxtalpayvkrwxey@forum.dlang.org
>
> I opened an issue asking the specifications to explain that clearly.
Huh, that's really interesting, thanks for posting. I guess my other question would be how do I determine if a block of assembly is pure?
I also figured out moving *op1 directly into RAX, guess it makes sense that a 64-bit value would need a 64-bit register :)
private void rot(string ins)(int *op1, int op2)
{
mixin("
asm
{
mov RAX, op1;
mov ECX, op2[EBP];" ~
ins ~ " [RAX], CL;
}
");
}
|
Copyright © 1999-2021 by the D Language Foundation