[Translate] Compiler (10)-Compile to C

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Here's the original.

———— Translation Divider Line ————

Compiler (10)-Compile to C

The first part: Introduction
Part II: compilation, Translation and interpretation
Part III: Compiler Design Overview
Part IV: Language Design Overview
Part V: Calc 1 Language Specification sheet
Part VI: Identifiers
Part VII: Scanning
Part VIII: Abstract syntax tree
Ninth part: Parsing

Finally to the last step!

Our language specification is so simple that you can actually skip the C Direct output assembly. I have two reasons not to do so. First of all, portability. In this guideline, I do not need to write C code for any particular architecture. C has been ported to a variety of different systems, so you can let the C compiler do the work for us.

Second, for many programmers, a compilation is a lot more unfamiliar than C. Even if you never use C to write anything, it's much easier to understand than a compilation.

Compile

This is going to look a little familiar. We will traverse the AST and generate the C code and calculate it.

The compiler generates and outputs a file with the same name as the original file with the extension ". C". Compilefile creates the output file, parses the source code, and initiates output processing.

As with the parser, the first element of the AST will be the File object. However, the File object itself does not contain anything useful except the root expression, which provides a mechanism for outputting the content.

There is nothing exciting about this part. Just a series of templates. We provide an entry for the program, an exit status, and a required printf statement and C standard input/output header file.

Optimization

At the beginning of this series, I said that I would not discuss much about optimization, as it is. From another perspective, our language is suitable for an optimization: precomputed.

We can view the corresponding code for each element in the tree, but what about the result? It can be so complicated and useless. In other words, why not perform this step and make the final output simple and fast?

If the root expression is a number, pass this number to printf. If it's a two-value expression, there's some other fun.

The first stop is the general function Compnode, which is used to judge what we have: Basiclit or binaryexpr.

If it is a basic syntax element, which is known as an integer, simply convert it to the actual integer and return the result.

Two-value expressions are equally simple. The first element in the expression list is always a starting point. The order of the operands is important for operations such as division and subtraction. The first element of the expression list is saved as a starting point in a temporary variable (in other words, this is somewhat similar to using the EAX or Rax registers in the assembly).

The results are computed for each operand and saved back to the same variable based on the operator. Returns the result after completion. This process is done recursively, knowing that everything is complete.

Final link

After the compiler has done the work, there is something that needs to be done. First, you need to create a command to read the source code for Calc 1, and call the compiler's library. Second, the C compiler, such as GCC or clang, works on the output of the compiler commands. Finally, given how the C compiler works, you may also need to execute the linker on the target file of the C compiler output.

All of this is handled by a program called CALCC, an optional Calc compiler. This is another validation of my super smart naming technique.

There is nothing special about this program. It opens an input file, verifies that it has a. calc extension, and then calls Comp.compilefile. Then use the Exec.command from the Go standard library to execute the C compiler and linker.

There are also several command-line arguments to control the C compiler.

Conclusion

I hope this is helpful for those who want to learn and learn something about compiler knowledge. It's a pretty big topic and it's almost impossible to navigate.

For some people who feel this series may be too simple, I'm sorry. I hope you'll be free to join me and move on to Calc 2.

I skipped a lot of content, so I wanted to be able to wave around these flaws in the next series. The content that will be included in Calc 2 is:

    • Symbol table
    • Scope
    • function definition
    • Variable definition
    • Contrast and branching
    • Assigning values to variables
    • Type
    • Memory stacks

I'll implement Calc 2 based on the Calc 1 code, so anything you've learned here, I think, will apply to the next series.

If Calc 3 is finished, I really hope so, and I'm bound to get entangled in the assembly. If so, you may need to include some articles on the assembly itself, or as a separate guidance series for Calc 3. There are other ideas: objects, methods, multiple assignments, multiple files, and libraries.

Thanks for reading!

Good luck to you, bye!

Related Article

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.