Tag Archive for 'c++'

Compiling Java and C# to SWF

At the end of my Leaving the Sandbox session I showed two projects I have been working on without telling anyone. The first was a compiler that could compile C# to SWF. The second was another compiling being able to compile Java to SWF. First of all I have to say that both tools are far from being finished. They are a so called proof-of-concept. For me it is enough to know that this is possible. I am also not sure if I will finish one of those tools.

So how does this work? Because I am now working on the AudioTool backend for our highly anticipated launch I had to switch to fulltime Java development. In order to understand the JVM better I started to search for the optimizations that the HotSpot VM performs and stumbled upon a framework called Soot. VoilĂ . Reading about Soot made me question why nobody is working on such a framework for ActionScript. Probably because universities see Flash just as a toy. For us developers being dependent on the Flash Platform it is of course more than that.

I will quickly explain what Soot does. It reads compiled Java class files and converts the machine code they contain into a three address code representaiton. This code is then heavily optimized. TAAS is basically doing the same. I also want to note that Soot is by far more complex and complete than TAAS right now. It would be megalomania saying that TAAS is Soot for ActionScript. But you get the idea. The one framework converts Java to three address code and the other framework converts ActionScript to three address code. Maybe it is possible to connect them.

This is what I did for the Java compiler. The whole pipeline looks like this:

  1. Compile Java sourcecode with the Java compiler of your choise
  2. Use Soot to get the three address code representation of a compiled binary
  3. Create TAAS expressions for the expressions that Soot uses
  4. Connect the TAAS graph the same way the Soot graph is connected
  5. Compile the TAAS graph to a SWF

This is about it. There is some glue-code involved here and there and I did not implement all Java expressions yet. I did also not bother about threads. Basically one could do what Scott has done for Alchemy. The cool thing is that we get highly optimized Java code because of the Java compiler and Soot. Then the TAAS compiler can run a second time after linking is done and perform platform specific optimizations like inlining and loop invariant code motion for certain expressions.

I already wrote a couple of classes and packed them in a SWC. Those mimic the behaviour of the Java classes like java.lang.System or java.util.LinkedList. On the Java side I implemented the Flash classes like flash.display.Sprite and flash.events.EventDispatcher. The interesting part is that all the methods those classes contain are marked native which means they have no implementation since they are native to the Flash Player.
This is also a great advantage. It is slow in Alchemy to call Flash methods and to communicate with Flash classes. The Java approach does not have this tradeoff. I also do not have to trigger the ActionScript compiler. The conversion form class to swf is entirely done using Soot and TAAS.

So far for the Java compiler. But in order to show a little bit more of the potential I wanted to have a C# compiler working as well. The great thing about advanced languages and a large community is that people have already built a lot of tools. It did not take too long to find an advanced framework that converts C# to Java. Actually this is not be the best approach. But I could not find the “Soot for C#” the evening before my FOTB session.

So I am using net2java to convert .NET code like C# or VisualBasic to Java. That code is then compiled using the Java compiler and I am using Soot again to convert the code to TAAS without much stress.

For me the most important part is to know that it is possible to compile .NET source code and Java binaries to SWF. To complete those tools one just has to implement all the missing expressions and the standard Java library in Flash. Everything could also work the other way around. Compiling from TAAS to .NET, Silverlight and Java is another option.

Flirting With Silverlight

Lately a lot of discussion had the ActionScript language as its focus. People start complaining about complex language features but I think they are great because the end user will benefit from that. Yesterday evening I had my very first test with Silverlight — and I am really impressed.

It took me a short amount of time to port the strange attractor to Silverlight. I agree that this is may be not a fair comparison because I know C# already but have a look at the source code. I make heavy use of type inference and the Matrix4x4 class has the plus, multiply and array-index operator overloaded. The code is more readable. And besides: it executes really fast. Faster than my heavily optimized ActionScript version. Imagine I would write var bleh = 1.0 in ActionScript. Framerate would drop to something like zero. But this is sad since there is no reason for me to write var bleh: Number = 1.0. A modern compiler should be able to figure this out. haXe can do it, C# can do it, OCaml can do it and lots of others as well.

Remember: This was my first time using Silverlight. To achieve the same result with Flash you have to be kind of an expert in strange player and language “features”. Now tell me again that the end user will not benefit.

AS3C — take a look inside

I have started working on AS3C at the end of last year. After a quick prototype the development stagnated and I added just several fixes and tests to the code. Basically I started AS3C as a complete C# newcommer and because of that the code is very ugly.

Due to the fact that I do not have much free time to continue developing AS3C I think it is the right time to release the source-code on the one hand and to let people experiment with it on the other hand.

You can either download the sources and build AS3C manually (you will need zlib.net) or download a binary from trunk/bin/.

When using AS3C you will need the ActionScript from the SVN. Remember that you write real ActionScript code which gets translated by AS3C. There is also one undocumented and very experimental feature existing. If you run as3c.exe -optimize main.swf you could get some speed improvements if you have heavy loops using the Math class. But it could also destroy the SWF so do not forget to make a backup :o)

FlashHunter Deluxe

FlashHunter has been used by actually one person: Michael Baczynski. He asked me at the FFK08 in Germany if I could modify the plugin to give him a global keyboard hook which will kill all instances of the Flash Player including the debugger.

So here it is: The FlashHunter Deluxe. Just for you Michael :)

FlashHunter

FlashHunter

FlashHunter is a neat Eclipse plug-in I wrote that allows you to kill all running Flash player instances with a single click. Two days ago I was implementing the fast Fourier transformation in Flash and I had a stupid error that caused an endless loop. Since I am testing with the standalone player the only way to get rid of it was Ctrl+Alt+Del and killing it. This is not really a nice workflow so I wrote the plug-in.

I am not a Java developer and definitly not an Eclpise platform developer so the plugin itself is very basic. I experimented a little bit with JNI and wrote most of the plugin in C++ because it still is my language of choice.

The plugin is Windows only. I think you could also do something like that as an external tool for OS X (“killall Flash\ Player”).

Imageprocessing results

Maybe you remember me some days ago talking about research and object recognition. I finished it and had to present the theoretical background and practical working example. What can I say? I worked the night before my examination until seven o’clock in the morning to get it working. Without a written speech, sleep or something else helpful I talked about my program. The result was 14 of 15 possible points which is quiet good.

Now about the Imageprocessing part. I wrote a program that gets structural information from an obstacle you hold into the camera. The title is “Objectrecognition of presented obstacles” and this is done using a motion-detection for the movement. A scene background-substraction is done to get only the moved obstacle. Then there is a median-colour lookup after a lof of filters have been applied. Closing, Retinex and Blobcounter to mention only some of them.
The last final part which I like most is the artificial neuronal network that recognizes the shape of the obstacle. I used a simple feed forward net and the results were also quiet good.

You can read the full article here. I am sorry but it is not written in English so you can only read if you understand German.

AS3 Documentation Tool AS3toXML

There was one great tool to create a documentation for AS2. It was called BLDoc from Bline-Express. The only problem with this tool is that it does not work with Actionscript3. So I started working on a AS3 documentation tool called AS3toXML because it is only analyzing the code and creates a Xml without any nice visual output. That will be the next tool I will create. XMLtoHTML or something like that. Now back to the first application which is nearly finished.

I need your whishes and suggestions!

Yeah, thats true! I want you to help me to have a powerful tool to create Actionscript3 documentations. So here we go. I will post feature lists and Xml output. I wish you to use the comment function to tell me about possible mistakes since I do not know very much about Xml. I want you to tell me how things can be done better and feature requests.

Updated:

  • Tags like @deceprated. I should have got all common tags now. If you know a tag you would like go ahead and leave a comment
  • Support for the new namespace feature that AS3 offers (access of class members can be public/private/internal/protected or defined by namespace)
  • Design changes. @see, @link, @throws and @req (requires) are now using same layout as multi-tags that can be used several times
  • Fixed code-analyzer to work with very screwed code and multiline declarations/values

Pending:

  • XSLT to format the output since the XMLtoHTML application is also finished now
  • Support for meta-tags (still need a list of all that tags)

Continue reading ‘AS3 Documentation Tool AS3toXML’

Free Visual C# 2005

This is kind of old news but maybe you are as outdated as I am and you are happy about the news that there is a free version of Visual C# available at MSDN. Get it here!.