【FlasCC】 FlasCC and Link Time Optimization

来源:互联网 发布:柏拉图网络专业建网站 编辑:程序博客网 时间:2024/04/28 09:40

The FlasCC toolchain is closely modeled after a typical native C/C++ development toolchain. It contains all of the tools you would expect from such a toolchain, including a compiler (with preprocesser), an assembler, a linker, an nm symbol lister, an ar archiver, etc.

As with many such toolchains, FlasCC uses the preprocess, compile, assemble, link model. Each is an individual step, although the compiler driver may perform multiple steps with a single invocation.

In the following example, the compiler driver is used to go from a simple .c file to a final executable in a single command:

 

 
/* everyone's favorite C program: hello.c */int main(){  printf("Hello, world!\n");  return 0;}

 


# preprocess, compile, assemble, link, resulting in final executable
> flascc/sdk/usr/bin/gcc hello.c –o hello.exe

Under the hood, it’s going through 4 distinct stages.

Stage 1: Preprocessing(预处理)

The preprocessing stage works the same way with FlasCC as with a conventional native toolchain. (Flascc预处理过程同一般原生编译工具类似)

It processes#include directives, expands macros, etc.(他也是处理include语句 和 宏定义等)

# preprocess hello.c resulting in preprocessed C in hello.i
> flascc/sdk/usr/bin/gcc –E hello.c –o hello.i

Stage 2: Compilation(编译)

The compilation stage compiles the preprocessed C code into “assembly code.” (Flascc将C语句编译成“汇编语句”)

In a native toolchain, this would result in x86, arm, etc. assembly code. (原生编译器将生成汇编语句)

For FlasCC, “assembly code” is ActionScript 3 (AS3) code.(Flascc生成的“汇编语句”将是AS3 code)

# compile hello.i resulting in “assembly code” in hello.s
> flascc/sdk/usr/bin/gcc –S hello.i –o hello.s

Stage 3: Assembly(汇编)

The assembly stage “assembles” the ActionScript 3 code generated in the previous stage into “object code.” In actuality, it is doing a full compile of the AS3 code using the ASC2 compiler, resulting in an ActionScript Bytecode (or ABC) file. There is some additional work done to compile the single file into multiple AS3 “scripts” to allow lazy initialization of FlasCC modules at runtime. While the resulting file is actually ABC, the nm and ar utilities have been extended to understand them.

# assemble hello.s resulting in object code in hello.o
> flascc/sdk/usr/bin/gcc –c hello.s –o hello.o

The nm tool can be used to list symbols of an ABC “object file.”

# list C symbols in hello.o
> flascc/sdk/usr/bin/nm hello.o
00000000 T _main
U _puts
00000000 W abort
00000000 W memcpy
00000000 W memmove
00000000 W memset

The ar tool can be used to build an archive containing ABC “object files”

# create an archive containing hello.o
> flascc/sdk/usr/bin/ar cr hello.a hello.o

# list the C symbols in the hello.a archive
> flascc/sdk/usr/bin/nm hello.a

 

hello.o:
00000000 T _main
U _puts
00000000 W abort
00000000 W memcpy
00000000 W memmove
00000000 W memset

Stage 4: Link(链接)

Finally, we can link hello.o into a standalone executable.

# link hello.o into an executable
> flascc/sdk/usr/bin/gcc hello.o –o hello.exe

# execute the exe
> ./hello.exe
Hello, world!

This blog post describes why you might want a standalone executable built using FlasCC. But in most cases, you would want to build a SWF instead.

# link hello.o into a SWF
> flascc/sdk/usr/bin/gcc hello.o –emit-swf –o hello.swf

 


url link:http://blogs.adobe.com/flascc/2013/03/18/flascc-and-link-time-optimization/

原创粉丝点击