Apparat RC6: Say Hello To An Old Friend

Patrick Le Clec’h is an active committer to the Apparat project and recently we just merged his work into the main branch. With a couple of other changes this is now a good time for another release candidate. Patrick added a good old friend to Apparat: The __asm function. You might remember __asm from my work on the now deprecated AS3C project.

However the Apparat version is much better. First of all we have put a lot of work into Apparat to make such transformations rock-solid. AS3C had its issues and was never a reliable tool. But there are a lot of new great features Patrick implemented. You can mix AS3 in your bytecode as well. __as3 is the best friend of __asm. Because sometimes writing pure bytecode is very verbose and not necessary.

A simple trace('Hello World!') with pure bytecode would look like this. Please note the FindPropStrict and CallPropVoid operations which reference trace.

__asm(
  FindPropStrict(AbcQName('trace', AbcNamespace(NamespaceKind.PACKAGE, ''))),
  PushString('Hello World!'),
  CallPropVoid(AbcQName('trace', AbcNamespace(NamespaceKind.PACKAGE, '')), 1)
);

Finding the object in the correct namespace is often a very cumbersome task. Thanks to __as3 we can also write this in a much more conciese way.

__asm(
  FindPropStrict(__as3(trace)),
  PushString('Hello World!'),
  CallPropVoid(__as3(trace), 1)
);

Note that the ASM compiler will try to guess the required name once it is requested by an operation. You can use __as3 also for other tasks.

var x: int = 1;
__asm(
  FindPropStrict(__as3(trace)),
  __as3(x < 10),
  CallPropVoid(__as3(trace), 1)
);

This would trace "true" for instance. If you are curious about the ASM syntax I can recommend you using the dump tool. It produces code which is nearly __asm-ready. We will probably write another output so you can directly transform existing code to __asm calls.

If you are interested in some more examples the Apparat Math replacements make use of __asm now as well. IntMath is a good example for an inlined class where you are using maybe a simple method like IntMath.abs and the heavy lifting is done behind the scenes using inline assembler. To use the ASM expansion you have to process your SWF file with TDSI. It is by default turned on.

16 Comments

  1. pleclech
    Posted Jul 28, 2010 at 7:14 pm | Permalink

    Thank you Joa.

    It’s a pleasure to work with you.
    Hope i will be able to help more with Apparat, as it’s a really interesting and challenging project :)

    Patrick.

  2. Rezmason
    Posted Jul 28, 2010 at 7:31 pm | Permalink

    How does this work? The tools in Apparat RC5 were all based on the concept of compiling a working SWF that used dummy classes, which would then be replaced with opcodes. But won’t writing straight-up opcodes in a .as file prevent the project from compiling?

  3. Posted Jul 28, 2010 at 7:45 pm | Permalink

    It is exaclty the same concept. __asm and all the operations are valid AS3 code. You compile your file and done. However one problem would still remain: if you do not process the file with TDSI it would not replace the __asm code, so nothing happens.

    The apparat-asm.swc contains all the AS3 code for inline assembler.

  4. wvxvw
    Posted Jul 29, 2010 at 11:39 am | Permalink

    Hi. Thanks a bunch. I was like since forever trying to find a way to resolve scope in nested E4X constructions, it was too involved to modify that by hand. But this seems like the tool for it. I’ll test it and report back if I could get it working.
    Appreciate the effort very much!

  5. Sebas
    Posted Aug 3, 2010 at 1:45 pm | Permalink

    It looks like it doesn’t work with scala 2.8.0 final since it looks for scala 2.8.0.rc7, can you confirm this? Thank you!

  6. Posted Aug 3, 2010 at 3:50 pm | Permalink

    I cannot confirm this. The builds on GoogleCode make use of Scala 2.8.0.final. The POMs are using 2.8.0.final and the runtime check is also looking for a Scala version string that equals 2.8.0.final

  7. E.a.p.
    Posted Aug 4, 2010 at 4:25 pm | Permalink

    @ Sebas,

    I had this at first too, but if u were folowing the tutorial by webdevotion and downloaded apparat using the link, you probably have the wrong fersion, make sure u get the latest version from the downloads section. Best of Luck

    E

  8. Sebas
    Posted Aug 5, 2010 at 11:07 am | Permalink

    thank you both!

  9. Sebas
    Posted Aug 5, 2010 at 11:31 am | Permalink

    Hello,

    I have last question if you don’t mind. I noticed that the particle system example uses specific functions from the apparat library. I wonder if apparat optimize just those functions or it actually optimize the whole swf regardless if apparat functions are used or not. Thank you.

  10. Posted Aug 5, 2010 at 2:33 pm | Permalink

    Sebas: Those specific functions are an example for Apparat’s inline expansion features. What does this mean? You can make use of it too. You need to have a class that extends apparat.inline.Inline and all its static methods will be inlined. So it is not specific to those methods that come with Apparat.

  11. E.a.p.
    Posted Aug 6, 2010 at 9:48 am | Permalink

    Hey Joa,

    I have a question myself too, do you know of anyone who has accomplished to run apparat with AIR? I am trying to use apparat with an AIR project by using the airglobal swc and adl, but cant get it fully to work yet. Some guidelines would be appreciated, Thanks!

    E

  12. Posted Aug 6, 2010 at 6:09 pm | Permalink

    What do you mean run Apparat with AIR? You want to modify an AIR project or do you want to launch Apparat with AIR?

  13. Posted Aug 8, 2010 at 2:11 pm | Permalink

    Great Joa,

    we can do the Math to local var trick now:

    var sqrt : Number;
    var math;
    __asm(
    __as3(Math),
    SetLocal(math)
    );

    var i : int = 2000000;
    var t : int = getTimer();
    while(i–)
    {
    __asm(
    GetLocal(math),
    PushByte(20),
    CallProperty(__as3(Math.sqrt), 1),
    ConvertDouble,
    ConvertDouble,
    SetLocal(sqrt)
    );
    }

    Math.sqrt takes about 80ms, and from local var about 50ms…

    Thanks, Nico.

  14. Posted Aug 8, 2010 at 2:44 pm | Permalink

    Also works when wrapping into a apparat macro.

    public static function init(math : *) : void
    {
    __asm(
    __as3(Math),
    SetLocal(math)
    );
    }

    public static function acos(math : *, phi : Number) : void
    {
    __asm(
    GetLocal(math),
    GetLocal(phi),
    CallProperty(__as3(Math.acos), 1),
    ConvertDouble,
    SetLocal(phi)
    );
    }

    By using:
    var math : Class;
    MathMacro.init(math);

    var phi : Number = …
    MathMacro.acos(math, phi);

    I love it ;)

  15. Posted Aug 8, 2010 at 10:20 pm | Permalink

    Oh nice. By the way I think you do not need a ConvertDouble after CallProperty for the Math methods since they are already typed double.

  16. Posted Aug 9, 2010 at 12:05 am | Permalink

    I thought that, too. But for some reasons – it’s slower if you do not… I cross checked with the haxe generated bytecode – Nicolas inserts even 2 ConvertDouble, but that does increase speed any further. I am pretty sure, that this ConvertDouble is no performance voodoo, however – I cannot explain it.

5 Trackbacks

  1. By joa (Joa Ebert) on Jul 28, 2010 at 4:46 pm

    Apparat RC6: Say Hello To An Old Friend http://blog.joa-ebert.com/2010/07/28/apparat-rc6-say-hello-to-an-old-friend/

  2. By nodename (Alan Shaw) on Jul 28, 2010 at 4:58 pm

    Great to see even more progress on this! RT @joa: Apparat RC6:http://blog.joa-ebert.com/2010/07/28/apparat-rc6-say-hello-to-an-old-friend/

  3. [...] This post was mentioned on Twitter by Joa Ebert and JP Bader, vic cekvenich. vic cekvenich said: RT @joa: Apparat RC6: Say Hello To An Old Friend http://blog.joa-ebert.com/2010/07/28/apparat-rc6-say-hello-to-an-old-friend/ [...]

  4. By Harley davidson dealer texas on May 2, 2011 at 6:53 am

    [...] Apparat RC6: Say Hello To An Old Friend « blog.joa-ebert.com 28 Jul 2010. Patrick added a good old friend to Apparat: The __asm function.. A simple trace('Hello World!') with pure bytecode would look like this. Apparat RC6: Say Hello To An Old Friend « blog.joa-ebert.com [...]

  5. [...] book about puberty. Just the other day my daughter told me her 10 yr old friend got her period. A Parent asks, I have a girl who's 10 and a boy who's 11. And i want to give them a good book about … WordPress › [...]

Post a Comment

Your email is never shared. Required fields are marked *

*
*