Azriel Fasten

November 25, 2008

Contact: fst911@gmail.com



My Experience With Alchemy



The following is my understanding of what the Alchemy does ( mostly inside Alchemy's gcc script ). This is for creating an executable swf, not a library. There may be some minor differences. Using these tools, one can compile C/C++ programs to Actionscript, to be used in the Actionscript Virtual Machine found in Adobe Flash Player.





LLVM-GCC

llvm-gcc/llvm-g++ is a C/C++ compiler which targets LLVM Intermediate Representation ( IR ). The bytecode format of the IR is normally stored in *.bc ( bytecode ) files. These bytecode files can then be operated upon by the rest of LLVM's tools such as opt ( for optimization ). For a full list of LLVM tools see: http://llvm.org/docs/CommandGuide/index.html.


Example:

llvm-g++ -emit-llvm -c -nostdinc -nostdinc++ -I"../avm2-libc/include/" -I"../avm2-libc/include/c++/3.4/" --include "../avm2-libc/avm2/AVM2Env.h"

Note:

The C and C++ standard library header files are provided by Alchemy. Also, AVM2Env.h must be included to set up the environment correctly. Arguments:



OPT

opt is LLVM's optimizer. It takes bytecode files as input, and outputs bytecode files.


Example:

opt -f "main.bc" -lowerallocs -o "main.opted.bc"

Note:

Alchemy's gcc script uses -lowerallocs, if it isn't used, then asc.jar will be unable to compile the Actionscript file. Arguments:




LLVM-LD

llvm-ld can be used to combine bytecode files, which can then be further optimized with opt ( inter-procedural optimizations ). For Alchemy, the C and C++ standard libraries are pre-compiled into LLVM bytecode files. They should be combined with the project ( and optionally re-optimized ) before using llc.


Example:

llvm-ld "main.bc" "avm2-libc/lib/avm2-libc.l.bc" "avm2-libc/lib/avm2-libstdc++.l.bc" -O3 -o "main.bc" -internalize-public-api-list="$(internsyms)"

Notes:

Alchemy provides the standard libraries in bytecode format. They are linked in with llvm-ld, together with your bytecode file ( main.bc ). Arguments:



_start, malloc, free, __adddi3, __anddi3, __ashldi3, __ashrdi3, __cmpdi2, __divdi3, __fixdfdi, __fixsfdi, __fixunsdfdi, __fixunssfdi, __floatdidf, __floatdisf, __floatunsdidf, __iordi3, __lshldi3, __lshrdi3, __moddi3, __muldi3, __negdi2, __one_cmpldi2, __qdivrem, __adddi3, __anddi3, __ashldi3, __ashrdi3, __cmpdi2, __divdi3, __qdivrem, __fixdfdi, __fixsfdi, __fixunsdfdi, __fixunssfdi, __floatdidf, __floatdisf, __floatunsdidf, __iordi3, __lshldi3, __lshrdi3, __moddi3, __muldi3, __negdi2, __one_cmpldi2, __subdi3, __ucmpdi2, __udivdi3, __umoddi3, __xordi3, __subdi3, __ucmpdi2, __udivdi3, __umoddi3, __xordi3, __error



LLC

llc is used to convert bytecode files to a target back-end. LLVM has many back-ends. Adobe ( Peterson ) has made their own custom back-end called avm2, which targets the Actionscript Virtual Machine version 2. llc, using this back-end, will produce *.as files ( Actionscript ). These are not ordinary *.as files as they use special extensions not supported by the Actionscript compiler. Thus, alchemy provides a custom Actionscript compiler asc.jar.


Example:

llc -f -march=avm2 -o "main.as" "main.bc” -avm2-package-name=cmodule

Notes:

Arguments:




ASC.JAR

asc.jar is the custom Actionscript compiler. It converts the *.as files to *.swc, *.swf, *.abc files. The compiler must link in the flash libraries provided by Alchemy ( presumably to provide the runtime ). It is a Java application and thus requires Java.


Example:

java -Xms16M -Xmx1024M -jar "bin/asc.jar" -AS3 -strict -import "flashlibs/global.abc" -import "flashlibs/playerglobal.abc" -config "Alchemy::Shell=false" -config "Alchemy::NoShell=true" -config "Alchemy::LogLevel=2" -config "Alchemy::Vector=true" -config "Alchemy::NoVector=false" -config "Alchemy::SetjmpAbuse=true" -swf "cmodule.ConSprite,800,600,60"

Notes:

The following is an explanation of the arguments used. The quoted text is from running from running “java -jar asc.jar --help”.





My environment:



Problems I've encountered using Alchemy:


#include <iostream>
int main(){
 std::cout << "testing\n";
 return 0;
}







1http://llvm.org/cmds/llvmgcc.html

2From running “llvm-ld --help”

3From running Alchemy's “llc --help”

4http://labs.adobe.com/wiki/index.php/Alchemy:Documentation:Developing_with_Alchemy:Tools#Tools_in_.24ALCHEMY_HOME.2Fbin