Jump to page: 1 2
Thread overview
mixin assembler does not work?
Jul 20, 2014
Foo
Jul 20, 2014
bearophile
Jul 20, 2014
bearophile
Jul 20, 2014
Foo
Jul 20, 2014
bearophile
Jul 20, 2014
sigod
Jul 20, 2014
bearophile
Jul 20, 2014
Foo
Jul 20, 2014
Foo
Jul 20, 2014
Foo
Jul 20, 2014
bearophile
Jul 20, 2014
Foo
Jul 20, 2014
bearophile
Jul 20, 2014
sigod
Jul 20, 2014
Nicolas Sicard
Jul 21, 2014
Foo
July 20, 2014
Hey guys. Can someone explain me, why this code does only works
with the inline assembler version but not with the mixin?
Thanks in advance!

Code:
import std.stdio : writeln;
import std.conv : to;

template Vala(uint count, alias arr) {
	immutable string c = to!string(count);

	enum Vala = "asm { sub ESP, " ~ c ~ "; mov " ~ arr.stringof ~ ",
" ~ c ~ "; mov " ~ arr.stringof ~ " + 4, ESP; }";
}

void main() {
	ubyte[] a;
	writeln(a.length);
	
	static if (false) {
		asm {
			sub ESP, 1000;	// Reserve 1000 bytes
			mov a, 1000;	// Set a.length = 1000
			mov a + 4, ESP;	// Set &a[0] to reserved bytes
		}
	} else {
		mixin Vala!(1000, a);
	}
	
	writeln(a.length);
}
July 20, 2014
Foo:

> Hey guys. Can someone explain me, why this code does only works
> with the inline assembler version but not with the mixin?

You need mixin(), it's unfortunate this gives no error messages:


import std.string: format;

enum Vala(uint count, alias arr) = format("
    asm {
        sub ESP, %d;     // Reserve 'count' bytes
        mov %s, %d;      // Set a.length = 'count'
        mov %s + 4, ESP; // Set &a[0] to reserved bytes
    }", count, arr.stringof, count, arr.stringof);

void main() {
    import std.stdio: writeln;

    ubyte[] a;
    a.length.writeln;

    static if (false) {
        asm {
            sub ESP, 1000;  // Reserve 1000 bytes
            mov a, 1000;    // Set a.length = 1000
            mov a + 4, ESP; // Set &a[0] to reserved bytes
        }
    } else {
        mixin(Vala!(1000, a));
    }

    a.length.writeln;
}

Bye,
bearophile
July 20, 2014
On Sunday, 20 July 2014 at 14:18:58 UTC, Foo wrote:
> template Vala(uint count, alias arr) {
> 	immutable string c = to!string(count);
>
> 	enum Vala = "asm { sub ESP, " ~ c ~ "; mov " ~ arr.stringof ~ ",
> " ~ c ~ "; mov " ~ arr.stringof ~ " + 4, ESP; }";
> }
> ...
> 		mixin Vala!(1000, a);

I'm not sure how to do it, but I see few mistakes in your code:

1. You declaring it as a string. (Or your intend to use `mixin()`?)
2. You trying to use `template` as a [`mixin template`][0].

[0]: http://dlang.org/template-mixin
July 20, 2014
> enum Vala(uint count, alias arr) = format("
>     asm {
>         sub ESP, %d;     // Reserve 'count' bytes
>         mov %s, %d;      // Set a.length = 'count'
>         mov %s + 4, ESP; // Set &a[0] to reserved bytes
>     }", count, arr.stringof, count, arr.stringof);

I'd like to write that more like this:


enum Vala(uint count, alias arr) = format("
    asm {
        sub ESP, %%(count);              // Reserve 'count' bytes
        mov %%(arr.stringof), %%(count); // Set a.length = 'count'
        mov %%(arr.stringof) + 4, ESP;   // Set &a[0] to reserved bytes
    }");


See:
http://fslang.uservoice.com/forums/245727-f-language/suggestions/6002107-steal-nice-println-syntax-from-swift

Bye,
bearophile
July 20, 2014
sigod:

> , but I see few mistakes in your code:

What's unfortunate is the silence of the compiler about that programmer mistake :-)

Bye,
bearophile
July 20, 2014
On Sunday, 20 July 2014 at 14:46:32 UTC, bearophile wrote:
>> enum Vala(uint count, alias arr) = format("
>>    asm {
>>        sub ESP, %d;     // Reserve 'count' bytes
>>        mov %s, %d;      // Set a.length = 'count'
>>        mov %s + 4, ESP; // Set &a[0] to reserved bytes
>>    }", count, arr.stringof, count, arr.stringof);
>
> I'd like to write that more like this:
>
>
> enum Vala(uint count, alias arr) = format("
>     asm {
>         sub ESP, %%(count);              // Reserve 'count' bytes
>         mov %%(arr.stringof), %%(count); // Set a.length = 'count'
>         mov %%(arr.stringof) + 4, ESP;   // Set &a[0] to reserved bytes
>     }");
>
>
> See:
> http://fslang.uservoice.com/forums/245727-f-language/suggestions/6002107-steal-nice-println-syntax-from-swift
>
> Bye,
> bearophile
It seems that your code doesn't work with 2.065.
July 20, 2014
Foo:

> It seems that your code doesn't work with 2.065.

Right. For 2.065 you have to replace the enum expression with a template.

If you can I suggest to update your compiler to the latest beta. There are tons of bugfixes and changes.

Bye,
bearophile
July 20, 2014
On Sunday, 20 July 2014 at 14:44:07 UTC, sigod wrote:
> On Sunday, 20 July 2014 at 14:18:58 UTC, Foo wrote:
>> template Vala(uint count, alias arr) {
>> 	immutable string c = to!string(count);
>>
>> 	enum Vala = "asm { sub ESP, " ~ c ~ "; mov " ~ arr.stringof ~ ",
>> " ~ c ~ "; mov " ~ arr.stringof ~ " + 4, ESP; }";
>> }
>> ...
>> 		mixin Vala!(1000, a);
>
> I'm not sure how to do it, but I see few mistakes in your code:
>
> 1. You declaring it as a string. (Or your intend to use `mixin()`?)
> 2. You trying to use `template` as a [`mixin template`][0].
>
> [0]: http://dlang.org/template-mixin

Yeah, it now works with mixin(Vala!(1000, a)); I thought that both are the same.
July 20, 2014
For clarification: how would that work without mixin + string?
July 20, 2014
On Sunday, 20 July 2014 at 14:55:00 UTC, Foo wrote:
> For clarification: how would that work without mixin + string?

I tried this:

mixin template Vala2(uint count, alias arr) {
	asm {
		sub ESP, count;
		mov arr, count;
		mov arr + 4, ESP;
	}
}

but I get several errors. Unfortunately it seems that asm cannot be used in mixin templates?!
« First   ‹ Prev
1 2