AS3V For Ant Released

Since I started the development of some other library I completly put AS3V aside because the Eclipse integration with FDT is currently not possible. But since AS3V is working already I decided to release a version really quick that allows you to use AS3V as an Ant Task or simply from command line.

I know it would be better to have it as an Eclipse Plug-in with nice little markers etc. but for that I need some more extension points in FDT. I do not know about FlexBuilder but it would be probably the same.

The file includes an example build and some test cases that show you how AS3V can detect common errors and mistakes. There is currently not that much documentation available for the rules and parameters you can specify. Putting a list online with all rules and nice descriptions is of course something I would like to do but currently the time for that is missing. I hope everything is self-explaining if you have a look at the readme and example files.

Update: The AS3V package has been updated with one bug fix and some modifications so that AS3V will produce a more interesting log output if it fails for some reason. Using AS3V with Eclipse works only if you specify fork="true" like in the Flex SDK example from the build file that comes with the zip.

A Simple Method And Taas

I just want to share with you how Taas can actually optimize code and what happens behind the scenes. So in my last post I talked about stackless code and optimizations that are possible but how does it work at all?
Imagine you have got this ActionScript code:

var x: Number = 1.0;
var y: Number;

if(true)
{
	y = 3.0;
}
else
{
	y = 2.0;
}

x += y;

return x;

For the sake of simplicity I will not use local variables in the bytecode. But it will be easier to figure out what happens in the bytecode.

      PushDouble           1.0
      PushTrue
      IfTrue               L0

      PushDouble           2.0
      Jump                 L1

L0:   PushDouble           3.0
L1:   Add
      ReturnValue

This bytecode is nearly the same as the method but without local variables. This should be fairly simple to understand.

When converting bytecode to Taas, its corresponding control flow graph is converted into a control flow graph of Taas expressions. I do not want to go into much detail how the stack based code is converted but here is the graph before and after the transformation from bytecode to Taas. It looks quite similar but there is a major difference. All those constant values in the Taas graph are not values pushed on a stack but assignments to virtual variables which exist only in theory. You can see also that the Add in the bytecode has known operands and type in the Taas version. Since it is currently not know if 2.0 or 3.0 has been used there is a Φ function that says “This value is either 2.0 or 3.0 depending on the path taken at runtime”.

So as I said this code can be optimized much better than stack based code. This graph is very redundant. There are three utilities to compact and simplify the current graph of Taas expressions. The algorithm has been developed for Java bytecode but works with ActionScript very well tool. The concept is quite simple. Perform copy propagation, constant folding and dead code elimination until the graph stops changing. Applying these techniques to the Taas graph yields the following results.

Copy propagation:

Most of the theoretical variables have been eliminated using copy propagation.

Constant folding:

Known constants have been replaced. Even the if condition is no longer needed and the false branch has been removed. The dead code elimination will clean up this code afterwards.

Dead code elimination:

Dead code elimination has removed the dead Jump statement and also the value 2.0 from the Φ expression. Afterwards constant folding replaced the Φ expression with its one constant value. Then another iteration of constant folding replaced the Add expression with the constant value 4.0. Afterwards copy propagation has put the result into the Return statement and voilá.

This result may have seemed quite obvious from the beginning and you may ask who writes such code. Probably nobody. But once you start inlining methods this makes a lot of sense. A lot of preconditions are known in that case and unnecessary branches can be removed. Since inlining methods bloats up the code it is very important to compact it afterwards as much as possible. I hope you see now how interesting all of this might be in the future.

Leaving The Sandbox

If you have been following me on Twitter you might have figured out that I am working on a new project. During the last couple of months I have learned a lot in terms of code analysis and optimizations. And I am not talking about ActionScript optimizations — this is as interesting as a piece of cake. I mean stuff like sparse conditional constant propagation or loop-invariant code motion. This is where it gets interesting.

In order to perform such optimizations considering ActionScript there are two options:

  • Extend the existing ActionScript Compiler
  • Write a compiler that does not take ActionScript as the input but ActionScript Bytecode like AS3C

Extending the ASC for this task is not worh considering in my opinion and since AS3C is really buggy I decided to start from scratch. The result is high-level framework to deal with SWF, SWC and ABC files including abstract structures for control flow analysis or bytecode permutations. The idea was to make manipulations as easy as possible by hiding the complex nature of ABC files which contain ActionScript bytecode and the description of classes, their visibility etc.
Since this basic framework is now complete I started with the next step: transforming the bytecode into a stack-less representation. The reason is quite simple. The bytecode and AVM+ are using a stack-based form for various good reasons. But optimizing stack-based code is hard because the stack plays such an important role since nearly all instructions depend on the stack’s state, thus on the preceding operations.

The idea is to transform the stack-based bytecode into a stack-less three-address-code. This is why I started working on TAAS, Three-Address-ActionScript. TAAS is a stackless representation of ActionScript bytecode and typed as long as the type can be determined at compile-time. This means also that method calls are solved and that it is possible to have an optimization step to inline those for instance.
Unfortunately it is absolutely not trivial to convert bytecode to three-address-code since the control flow of a method has to be considered as well for instance. This and many other things caused me a lot of headaches during the last week. Most problems are solved but I have not implemented all instructions of the AVM+ yet. Although I can already transform the 3D lorenz attractor to TAAS for instance and all types are solved correct.

Now what is the next step? Of course converting TAAS to a static-single-assignment form which is perfect for optimizations.
Having a powerful framework opens up a lot of possibilities. There are frameworks available for Java which convert Java bytecode to SSA as well which could be converted to TAAS and finally to ActionScript bytecode. One could also start implementing great features like Code Contracts.

I will talk about my work and results also at FOTB this year.

Flash on Tap and Flashbelt

Tomorrow I will leave for my trip to the USA. First conference will be the Flash on Tap conference which is happening for the first time. Then shortly afterwards I will speak at flashbelt. This is now the 3rd 6th flashbelt conference but for me the first time speaking there.

I have never been to the States before so I am really looking forward to it. At my session “AudioTool’s Private Parts” which had been completely renewed for the FFK09 in Germany I will talk of course about the technologies that our tool uses and show some examples using Hobnox Open Source. I will also show a live preview of AS3V running in Eclipse and how it integrates into the daily workflow.

AS3V Preview

AS3V Here is the first preview of what AS3V looks like in Eclipse. I have written about 30 rules which include cyclomatic complexity checks and unreachable code detection. Most of the time was getting AS3V to link against the asc.jar from the Flex SDK inside Eclipse which is a real nightmare if you ever tried that one for yourself. However — it works. And hopefully I can forget about all the code I had to write pretty soon.

You can see in the screenshot that AS3V places markers in your Eclipse code. It works as an incremental project builder which means it is pretty fast since it only evaluates all your sources that have changed independently. If you have any suggestions for possible rules, please tell me. The Flex SDK coding conventions could be interesting for instance. Right now the Eclipse plug-in has still some problems but I think that a first version might be released in the next few weeks.

The return of AS3V

In September 2008 I started working on a tool called AS3V. The goal of AS3V was to parse source code and to check the code against a set of rules. My main problem with the first attempt of AS3V was that I used ANTLR. ANTLR is a great tool to create a compiler but for ActionScript 3 and its various strange behaviours you have to do a lot of hacking. Parsing ActionScript, XML and regular expressions with the optional semicolon in ECMAScript is not really fun using such a tool. metaas went that path and unfortunately their parser fails very often.

This is the reason why I started writing a parser manually from scratch. But this is also not a good idea. What about Adobe changing the language or adding features? I would have to change my parser. This is for instance a problem you have with FDT. Adobe releases a feature and it will take a while until you can adopt that. Maybe that feature is only beta but you want to use it. So are you going to implement it or not? Since the Flex SDK is open source I decided to take a completly different approach. Option one is to modify the original ASC. I think this is a bad idea. On every update from the Adobe guys you would have to patch the compiler again, and release a compiled version of that. This is what flexcover does. But I do not want to replace core parts of the Flex SDK with a modified compiler. So this is the reason why AS3V links only against the ASC. This means basically I do not care about what Adobe does with the compiler as long as the API stays consistent. This means also AS3V knows only as much as the ASC. This is sometimes a problem but in most cases not so important. ASC drops for instance braces every time. AS3V does not know if you write if(true) trace('foo'); or if(true) { trace('foo'); }. But in the end AS3V is able to analyze the real compiler output which is huge; theoretically every implicit cast can be watched!

Now enough theory. Why bother using AS3V and what is it good for? Imagine a loop like this:


for( var y: int = 0; y < height; ++y )
  for( var x: int = 0; x < width; ++x )
    index = y * width + x;

AS3V is currently smart enough to analyze the statement y * width + x and to figure out that y and width do not change in the inner loop. AS3V is therefore able to warn you about unneccessary calculations. There are lot of other rules I have already implemented which help you writing better code. For instance the pattern if( expr0 ) { if( expr1 ) { foo(); } } can be reduced to if( expr0 && expr1 ) { foo(); }.

There is a lot of room for other rules. I am happy to see some progress and hope that other people will find AS3V as useful as I. In the current state I have to implement more rules and a little bit more complex ones like dead code detection with constant folding. A release is targeted once I am happy with the rule set.

Not to forget: An Eclipse plugin which will show you appropriate warnings as neat little icons is of course also planned.

MetaLaunch For Eclipse

MetaLaunch Plug-inEver wanted to start multiple launchers with one single click in Eclipse? MetaLaunch will solve this issue. Just select the launchers you want to start and then change the order to fit your needs. You can also select to launch those tasks sequential or in a one shot.

Unfortunately I have not figured out yet how I could abort the launch process if mxmlc fails for instance. This is due to the fact that the fcsh process is persistent and does not return for instance a non-zero exit code when mxmlc reported an error. However, the MetaLaunch plug-in can help you in a lot of cases. For instance if you are developing a multi-client application and you want to launch every time four instances of the Flash Player etc.

The address of the Eclipse update site:

http://metalaunch.joa-ebert.com/update

Semantic Highlightning For PBDT

Semantic highlightningPBDT has now support for semantic highlightning. Thanks to the FDT guys again for answering a lot of Eclipse releated questions. Semantic highlightning works currently with parameters, input and output declarations and for defined preprocessor directives. Just have a look at the screenshot. I have also integrated complete color preferences support. If you go to Preferences > PBDT > Colors you can change everything the way you like.

PBDT with basic refactoring

PBDT RefactoringPBDT has a new feature which is basic refactoring. Simply select the variable you want to rename and press Alt+Shift+R or select “Rename” from the context menu. There are currently two issues with refactoring. If the refactoring entry in the context menu is disabled please select the variable and try again. The same for the shortcut. If you want to refactor something, just select it first in the text. Once I figure out how Eclipse handles changes of the caret position I will fix that.

The second is about scopes. If you redefine a variable in a different scope the refactoring is not correct. But I guess 99% of the developers will not get in touch with this issue.
In this example i would be refactored in the outer and inner scope which is not correct.

void evaluatePixel()
{
        int i;

        {
        	int i;
        	i;
        }

        i;

        out = sampleNearest(src,outCoord());
}

Hobnox Open Source

I am proud to announce that we are finally able to release some of our frameworks as open source. It has been a while and we talked very often about this topic. Today at the FlashForum Conference in Cologne I had the honor to show our open source hub to the public for the first time.

We have currently the UIEvent framework including full source code online. I can think of a lot of other frameworks we could release in the near future. Including for instance our DOM, Pogo, Tween or Graph framework. I really hope that we get the go for all those other libraries in the next few days as well.