www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - mixin assembler does not work?

reply "Foo" <test foo.bar> writes:
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);
}
Jul 20 2014
next sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
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
Jul 20 2014
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
 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
Jul 20 2014
parent reply "Foo" <test foo.bar> writes:
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.
Jul 20 2014
parent "bearophile" <bearophileHUGS lycos.com> writes:
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
Jul 20 2014
prev sibling parent reply "sigod" <sigod.mail gmail.com> writes:
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
Jul 20 2014
next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
sigod:

 , but I see few mistakes in your code:
What's unfortunate is the silence of the compiler about that programmer mistake :-) Bye, bearophile
Jul 20 2014
prev sibling parent reply "Foo" <test foo.bar> writes:
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.
Jul 20 2014
parent reply "Foo" <test foo.bar> writes:
For clarification: how would that work without mixin + string?
Jul 20 2014
parent reply "Foo" <test foo.bar> writes:
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?!
Jul 20 2014
next sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
 mixin template Vala2(uint count, alias arr) {
What about disallowing "mixin templatename" unless you add "mixin" before the "template" keyword? Bye, bearophile
Jul 20 2014
next sibling parent reply "Foo" <test foo.bar> writes:
On Sunday, 20 July 2014 at 15:54:15 UTC, bearophile wrote:
 mixin template Vala2(uint count, alias arr) {
What about disallowing "mixin templatename" unless you add "mixin" before the "template" keyword? Bye, bearophile
I do not understand?
Jul 20 2014
parent "bearophile" <bearophileHUGS lycos.com> writes:
Foo:

 I do not understand?
Sorry, mine was a language design question (to catch your bug). Bye, bearophile
Jul 20 2014
prev sibling parent "sigod" <sigod.mail gmail.com> writes:
On Sunday, 20 July 2014 at 15:54:15 UTC, bearophile wrote:
 mixin template Vala2(uint count, alias arr) {
What about disallowing "mixin templatename" unless you add "mixin" before the "template" keyword? Bye, bearophile
I thought it's disallowed.
Jul 20 2014
prev sibling parent reply "Nicolas Sicard" <dransic gmail.com> writes:
On Sunday, 20 July 2014 at 15:02:58 UTC, Foo wrote:
 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?!
The reason may be that mixin templates are just for inserting declarations, which asm blocks aren't. This limitation isn't specific to asm.
Jul 20 2014
parent "Foo" <test foo.bar> writes:
On Sunday, 20 July 2014 at 17:50:10 UTC, Nicolas Sicard wrote:
 On Sunday, 20 July 2014 at 15:02:58 UTC, Foo wrote:
 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?!
The reason may be that mixin templates are just for inserting declarations, which asm blocks aren't. This limitation isn't specific to asm.
But what's the reason for that?
Jul 21 2014