digitalmars.D.learn - mixin assembler does not work?
- Foo (27/27) Jul 20 2014 Hey guys. Can someone explain me, why this code does only works
- bearophile (26/28) Jul 20 2014 You need mixin(), it's unfortunate this gives no error messages:
- bearophile (12/18) Jul 20 2014 I'd like to write that more like this:
- Foo (2/22) Jul 20 2014 It seems that your code doesn't work with 2.065.
- bearophile (7/8) Jul 20 2014 Right. For 2.065 you have to replace the enum expression with a
- sigod (6/14) Jul 20 2014 I'm not sure how to do it, but I see few mistakes in your code:
- bearophile (5/6) Jul 20 2014 What's unfortunate is the silence of the compiler about that
- Foo (3/18) Jul 20 2014 Yeah, it now works with mixin(Vala!(1000, a)); I thought that
- Foo (1/1) Jul 20 2014 For clarification: how would that work without mixin + string?
- Foo (11/12) Jul 20 2014 I tried this:
- bearophile (4/5) Jul 20 2014 What about disallowing "mixin templatename" unless you add
- Foo (2/7) Jul 20 2014 I do not understand?
- bearophile (4/5) Jul 20 2014 Sorry, mine was a language design question (to catch your bug).
- sigod (2/7) Jul 20 2014 I thought it's disallowed.
- Nicolas Sicard (4/16) Jul 20 2014 The reason may be that mixin templates are just for inserting
- Foo (2/21) Jul 21 2014 But what's the reason for that?
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
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
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
On Sunday, 20 July 2014 at 14:46:32 UTC, bearophile wrote:It seems that your code doesn't work with 2.065.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
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
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
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
On Sunday, 20 July 2014 at 14:44:07 UTC, sigod wrote:On Sunday, 20 July 2014 at 14:18:58 UTC, Foo wrote:Yeah, it now works with mixin(Vala!(1000, a)); I thought that both are the same.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
For clarification: how would that work without mixin + string?
Jul 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?!
Jul 20 2014
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
On Sunday, 20 July 2014 at 15:54:15 UTC, bearophile wrote:I do not understand?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
Foo:I do not understand?Sorry, mine was a language design question (to catch your bug). Bye, bearophile
Jul 20 2014
On Sunday, 20 July 2014 at 15:54:15 UTC, bearophile wrote:I thought it's disallowed.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
On Sunday, 20 July 2014 at 15:02:58 UTC, Foo wrote:On Sunday, 20 July 2014 at 14:55:00 UTC, Foo wrote:The reason may be that mixin templates are just for inserting declarations, which asm blocks aren't. This limitation isn't specific to asm.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
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:But what's the reason for that?On Sunday, 20 July 2014 at 14:55:00 UTC, Foo wrote:The reason may be that mixin templates are just for inserting declarations, which asm blocks aren't. This limitation isn't specific to asm.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 21 2014