How does the Compiler of C?



  • I need a description of the work of the compiler in the natural language, prepared for someone who doesn't know the language. I mean, it's like, "Imagine you're a compiler, we're moving on."

    I've been teaching that language since yesterday. Syntaxis seems to be chaos, I don't understand the list of actions that the compiler conducts in the selection process, I don't understand what subsystem of language refers to, and that as a result, the elements of language and functions are transmitted. All those symbols that don't know how to work. It's very different from what I'm used to in tcl, where there's a locker, commanders and their parameters.



  • You should think the next way.

    1. A common picture. C-Files are individually computerized, the computer doesn't know anything about other files unless it's clearly in the file. Other files are " retracted " by the pre-processor. (For purists, yes, I can feed and...h compilator through Makefile, we won't complicate the painting without need.)

    2. Preprocessor. It passes by code and produces blunt text macro machines. #define X(Y, Z) for (int i = 0; i < Y; i = i * Z) Makes it. X(10, 2 + 1) Turn into for (int i = 0; i < 10; i = i * 2 + 1)♪ The Preprocessor, however, knows about the lines and does not conduct macro sub-standards within them. He also uses it. #include by mechanical insertion of the file.

    3. Preprocessor's a string. Inside rigid and symbolic literature, some symbol sequences are replaced by others. For example, \n is replaced a symbol with code 10. Also, for the literals of large lines.wchar_t*) The character set'a character may be recoded in UCS-2 or UCS-4 depending on the compiler.

    4. It's a compilator. There's no magic in the compilator. There are key words.forif, etc.) and functions. For example, printf - it's a function (from the standard library) printf("%d\n", 15); produces, in compiled code, a function printf and its transmission "%d\n" and 15♪ Same challenge. printf("%d\n", ""); performs functions printf with parameters "%d\n" and "" (This challenge will end with the wrong time of implementation). Compiler knows the exact semantics of the format line printf and right To give a clue if he sees that the parameters are not suitable for the format line.

    5. Optimizer. It has the right within to replace any design with a more effective rule as if, from the point of view of the final withdrawal and the visible value to the user, the conversion is permissible. Example: If you have a long calculation without side effects that you do not use (i.e., don't get it out), the optimist has the right to throw it away. And also has the right not to throw away. For example, how to calculate the value added A() + B() not defined, and even if the functions A and B have side effects, the optimist has the right to calculate them in any way, maybe even a cross-section. If you want to guarantee that A() to be determined before B()use a clear additional variable.

    6. Undefined behaviour. Here be dragons. There is a fairly large set of early stage-situations (e.g., the deregulation of a zero-indicator, the way out of the area (!) or the sign-off) when the compiler ceases to be responsible for the result. The compiler has the right to assume that this will never happen, to do this non-trivial design and to use it to simplify the code. For example:

      int m[1];
      if (cond)
      {
          printf("Хе-хе\n");
          return;
      }
      for (int i = 0; i < 2; i++)
      {
          m[i] = 0;
      }
      

      Compiler has the right to assume that the treatment m[1] never occurs, so the cycle is not met, so the code has to go early. returnThat's why cond necessarily. trueWhich means you can't figure it out, and you can simplify the whole function to the point.

      printf("Хе-хе\n");
      



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2