C-script hybrid programming method based on tiny C Compiler

Source: Internet
Author: User

TCC Brief Introduction:The most interesting feature of TCC is that it can be common on UNIX systems #! The/usr/bin/TCC method is used to execute the source program written in the ansi c language, omitting the steps for compiling and linking on the command line, you can directly run the source program written in C language. In this way, we can significantly speed up development like any other scripting language, such as Perl or Python. You can use the C language just like writing shell scripts. It is a wonderful thing to think about. However, TCC has some other features!
  • The size of TCC is very small. The size of all source code packages is less than 200 kb after compression. The size of the compiled TCC executable program is less than 80 KB. This means that we can use TCC to provide us with the ability to write c-language scripts in almost any scenario. This includes, of course, environments with tight hard disk space, such as embedded systems and boot floppy disks.
  • In the source program for TCC, you can use any dynamic link library that can be used in the source program for GCC. TCC not only supports the Standard ansi c language, but also supports the ISO c99 standard and some extensions made to the C language from GCC.
  • TCC directly generates Partially Optimized x86 machine code. You do not need to generate binary code for any virtual machine. According to the data provided by the authors of TCC, TCC Compilation speed is faster than that of the gnu c compiler without any code optimization (GCC-O0. Of course, if GCC is used for code optimization, the Compilation speed will be even worse than that of TCC.
  • The libtcc library allows you to use TCC as the backend for dynamic code generation. In libtcc. h, there is an API description. libtcc_test.c is a simple example of using libtcc. Libtcc contains the idea that you can include a program in a string and compile it directly. Then you can access global symbols (functions and variables ).
Generate libtcc: Since libtcc is based on Linux, in order to generate a dynamic connection library under the window, we must first use mingw/msys to compile and then use the following statement to generate a DLL. To facilitate msvc usage, you also need to generate a lib library for link. The entire process is as follows: 1) generate dllgcc-O2-shared-wall-wl, -- export-all-symbols-mpreferred-Stack-boundary = 2-March = i386-falign-functions = 0-fno-strict-aliasing-dtcc_target_pe-dlibtcc-O libtcc. dll tcc. c2) generate def pexports libtcc.dll | sed "s/^_//" > libtcc.def3) generate lib from def lib /machine:i386 /def:libtcc.def     Because C programming requires header files and libraries, we also need to include the include directory and lib directory when releasing the program, and then specify the path in the main program.  TCC Cooperation with the main program: In our program, we mainly put the fixed logic in the main program, and put the changed things in the TCC script. The basic process of this program is as follows: Open the packet capture according to the user input parameters, send each appropriate array segment of the datagram to the user-written TCC program according to the specified type, the TCC program transmits the processed data to the main function for further processing, such as writing data to a file. Note that we may need to process RTP packets or packets without RTP headers. During output, some bitstreams also need to be added with additional processing, for example, h261 and h263 need to call functions in the main program to splice sbit and ebit; for example, h263 + is simple to process, such as adding h264 stream headers, such as direct audio output. Therefore, variables and functions must be passed between the main program and the TCC program. The following is a rough code framework and explanation. Main Program:Tccstate * s; Create a New TCC status S = tcc_new (); If (! S) {fprintf (stderr, "cocould not create TCC State/N"); exit (1);} Add path: tcc_add_include_path (S, fullinc); tcc_add_library_path (S, fulllib); set the output mode to memory: tcc_set_output_type (S, tcc_output_memory); compile a file to TCC (actually, read the file to the buffer, and then call tcc_compile_string) tcc_compile_file, fullpath); Add the variables and function symbols to the TCC environment, so that the tcc_add_symbol (S, "encodeflag", (unsigned long) can be directly used in TCC) & encodeflag); tcc_add_symbol (S, "F", (unsigned Lo NG) & F); tcc_add_symbol (S, "enter_h263decode", (unsigned long) & enter_h263decode); tcc_add_symbol (S, "writefile", (unsigned long) & writefile ); to do all the relocation work, tcc_relocate (s) must be called before tcc_get_symbol; tcc_relocate (s) can be used to obtain functions and variable symbols tcc_get_symbol (S, & Val, "process") in TCC; process =) val; tcc_get_symbol (S, & Val, "withrtp"); withrtp = (INT) (* (int *) Val); tcc_get_symbol (S, & Val, "offset "); offset = (INT) (* (int *) Val); cyclically processes each packet whi Le (RES = pcap_next_ex (FP, & header, & pkt_data)> = 0 ){... Call the processing function process (& pkt_data [offset], totallen);} in TCC to clear tcc_delete (s ); TCC Program# Include <stdio. h> # include <stdlib. h> int withrtp = 1; int offset = 54; unsigned char zero [2] = {0, 0}; unsigned char hsf-head [4] = {0, 0, 0, 1 }; extern file * F; extern unsigned char encodeflag; extern int writefile (unsigned char * Buf, int Len); int process (unsigned char * PT, int Len) {If (encodeflag = 0 | encodeflag = 1) {/* h261 or h263 */If (PT [0]) & 0x7 = 0) {enter_h263decode (Len, PT, 1);} else {enter_h263decode (Len, PT, 0) ;}} Else if (encodeflag = 2) {/* h263 + */If (PT [0] = 0x04) & (PT [1] = 0x00) {writefile (zero, 2); writefile (& PT [2], len-2 );} else if (PT [0] = 0x00) & (PT [1] = 0x00) {writefile (& PT [2], len-2);} else {printf ("not correct 263 + format/N");} else if (encodeflag = 3) {/* Raw Data */fwrite (PT, 1, Len, f);} else if (encodeflag = 4) {/* MP3 */If (PT [0] = 0xff & PT [1] = 0xfb) {writefile (& PT [4], len-4 );} else {P Rintf ("not a MP3 header, % 02x % 02x/N", PT [0], PT [1]) ;}} else if (encodeflag = 5) {/* h264 */writefile (hsf-head, 4); writefile (PT, Len) ;}else {;} return 0 ;} as shown in the preceding example, TCC is used, it is easy to process arrays. After all, if you use other scripting languages, such as Lua and Perl, It is very troublesome to easily perform bitwise operations to process arrays, C language, after all, is a language familiar to most people and is easy to use.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.