LCC Compiler source program analysis (3) Choose a different target code interface

Source: Internet
Author: User
Tags error handling int size
One of the most important features in LCC is the ability to output different target codes. For example, the same C program, can generate MIPS, X86 and other assembly code, only need to choose a different target parameters. Only the code that generates X86 is parsed here, so the parameters of the command line are as follows: Rcc.exe-target=x86/nasm hello.i hello.asm parameter-target=x86/nasm is the X86 assembler code that lets the C compiler choose to generate NASM. The parameter hello.i is an intermediate file that has been previously described, and it is preprocessed. The parameter hello.asm is the generated target file. The   C compiled entry function is main, which is defined in the file main.c as follows:////c compiler entry. Cai Junsheng  2007/05/13 shenzhen//int main (int argc, char *argv[])   at the beginning of the main function is the code that handles the parameters as follows: #001  int main (int ar GC, Char *argv[]) #002  {#003   int i, J; #004   #005   for (i = argc-1; i > 0; i--) #006 & nbsp; {    #007          if (strncmp (Argv[i], "-target= ", 8) = = 0) #008          {#009                 break; #010         }    #011   } #012   #013    if (i > 0) #014   {#015          char *s = STRCHR (Argv[i], '//'); #016          if (s! = NULL) #017           {#018                *s = '/'; #019         }    #020   #021           for (j = 0; Bindings[j].name && bindings[j].ir; j + +) #022           {#023                 if (strcmp (&argv[i][8], bindings[j].name) = = 0) #024                 {#025                      IR = bindings[j].ir; #026              &Nbsp;      break; #027               } #028           #029   #030          if (s! = NULL) #031          {#032                 *s = '//'; #033         }    #034   } #035    #036   if (! IR) #037   {#038          fprint (stderr, "%s:unknown target", argv[ 0]); #039          if (i > 0) #040           {#041                fprint (stderr, " '%s ', &argv[i][8]); #042         }    #043  #044          fprint (stderr, "; Must specify one of/n "); #045   #046          for (i = 0; bindings[i].name; i++) #047           {#048                 Fprint (stderr, "/t-target=%s/n", bindings[i].name); #049         }    #050   #051           exit (exit_failure); #052   }   the 5th line to line 11th of the program is to find the target code parameters, that is, identify the-target= parameter. If found, jump out of the loop. Lines 13th through 34th of the program are the interfaces that find the corresponding build target code. The C compiler has already defined an array of code that can be generated, so just simply compare the names to find the appropriate interface for the target code. The structure of the interface definition is as follows: typedef struct Binding { char *name;  interface *ir;} Binding; Name saves the target code names, and IR saves the interface. The interface is defined as follows: #001  typedef struct Interface {#002   metrics charmetric; #003   metrics shortmetric; #004   metrics intmetric; #005   metrics Longmetric; #006   metrics longlongmetric; #007   metrics floatmetric; #008   metrics doublemetric; #009   metrics longdoublemetric; #010   metrics ptrmetric; #011   metrics structmetric; #012   unsigned Little_endian:1; #013   unsigned mulops_calls:1; #014   unsigned wants_callb:1; #015   unsigned wants_argb:1; #016   unsigned left_to_right:1; #017   unsigned wants_dag:1; #018   unsigned unsigned_char:1; #019  void (*address) (symbol p, symbol Q, long n); #020  void (*blockbeg) (ENV *); #021  void (*blockend) (ENV *); #022  void (*defaddress) (Symbol); #023  void (*defconst)   (int suffix, int size, Value v); #024  void (*defstring) (int n, char *s); #025  void (*defsymbol) (Symbol); #026  void (*emit)     (Node); #027  void (*export) (Symbol); #028  void (*function) (Symbol, symbol[], symbol[], int); #029 &NBsp Node (*gen)      (node); #030  void (*global) (Symbol); #031  void (*import) (Symbol); #032  void (*local) (Symbol); #033  void (*progbeg) (int argc, char *argv[]); #034  void (*progend) (void); #035  void (*segment) (int); #036  void (*space) (int); #037  void (*stabblock) (int, int, symbol*); #038  void (*stabend)   (coordinate *, symbol, coordinate * *, symbol *, symbol *); #039  void (*stabfend) (Symbol, int); #040  void (*stabinit) (char *, int, char *[]); #041  void (*stabline) (coordinate *); #042  void (*stabsym)   (Symbol); #043  void (*stabtype) (Symbol); #044   xinterface x; #045  } Interface; The interface defines the functions and data members required for the output assembly target code. Later again a function is introduced in one function. Continue parsing the main function, lines 36th through 52nd are unable to find error handling to generate the target code interface. The command line parameter is-target=x86/nasm, so the interface found is the X86/name interface, which is stored in the IR global variable.             here, choose a different target code generation interface for the analysis to complete.  

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.