ArticleslgStudy

biology

Code generation (compiler)

Code generation (compiler) is a biology topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Code generation (compiler) rather than just read about it. In short: In computing, code generation is part of the process chain of a compiler, in which an intermediate representation of source code is converted into a form (e.g., machine code) that the target system can readily execute. Sophisticated compilers typically perform multiple passes over various intermediate forms.

Key takeaways

  • Code generation (compiler) belongs to biology; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Code generation (compiler) to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Code generation (compiler) from memory before moving on to harder problems.

Reference excerpt

In computing, code generation is part of the process chain of a compiler, in which an intermediate representation of source code is converted into a form (e.g., machine code) that the target system can readily execute. Sophisticated compilers typically perform multiple passes over various intermediate forms. This multi-stage process is used because many algorithms for code optimization are easier to apply one at a time, or because the input to one optimization relies on the completed processing performed by another optimization. This organization also facilitates the creation of a single compiler that can target multiple architectures, as only the last of the code generation stages (the backend) needs to change from target to target. (For more information on compiler design, see Compiler.) The input to the code generator typically consists of a parse tree or an abstract syntax tree. The tree is converted into a linear sequence of instructions, usually in an intermediate language such as three-address code. Further stages of compilation may or may not be referred to as "code generation", depending on whether they involve a significant change in the representation of the program. (For example, a peephole optimization pass would not likely be called "code generation", although a code generator might incorporate a peephole optimization pass.)

Major tasks In addition to converting an intermediate representation into a linear sequence of machine instructions, a typical code generator tries to optimize the generated code. Tasks which are typically part of a sophisticated compiler's "code generation" phase include:

Instruction selection: which instructions to use. Instruction scheduling: in which order to put those instructions. Scheduling is a speed optimization that can critically affect pipelined machines. Register allocation: the allocation of variables to processor registers Debug data generation if required, so that the code can be debugged. Instruction selection is typically carried out by doing a recursive postorder traversal on the abstract syntax tree, matching particular tree configurations against templates; for example, the tree W := ADD(X,MUL(Y,Z)) might be transformed into a linear sequence of instructions by recursively generating the sequences for t1 := X and t2 := MUL(Y,Z), and then emitting the instruction ADD W, t1, t2. In a compiler that uses an intermediate language, there may be two instruction selection stages—one to convert the parse tree into intermediate code, and a second phase, much later, to convert the intermediate code into instructions from the instruction set of the target machine. This second phase does not require a tree traversal; it can be done linearly, and typically involves a simple replacement of intermediate-language operations with their corresponding opcodes. However, suppose the compiler is a language translator (for example, one that converts Java to C++). In that case, the second code-generation phase may involve building a tree from the linear intermediate code.

Runtime code generation When code generation occurs at runtime, as in just-in-time compilation (JIT), the entire process must be efficient with respect to space and time. For example, when regular expressions are interpreted and used to generate code at runtime, a non-deterministic finite-state machine is often generated instead of a deterministic one because the former can usually be created more quickly and occupies less memory space than the latter. Despite generally generating less efficient code, JIT code generation can take advantage of profiling information that is available only at runtime.

Related concepts The fundamental task of taking input in one language and producing output in a non-trivially different language can be understood in terms of the core transformational operations of formal language theory. Consequently, some techniques initially developed for use in compilers have come to be employed in other ways. For example, YACC (Yet Another Compiler-Compiler) takes input in Backus–Naur form and converts it to a parser in C. Though it was initially created for automatic generation of a parser for a compiler, yacc is also often used to automate writing code that needs to be modified each time specifications are changed. Many integrated development environments (IDEs) support some form of automatic source-code generation, often using algorithms in common with compiler code generators, although commonly less complicated. (See also: Program transformation and Data transformation.)

Reflection In general, a syntax and semantic analyzer tries to retrieve the program's structure from the source code, while a code generator uses this structural information (e.g., data types) to produce code. In other words, the former adds information while the latter loses some. One consequence of this information loss is that reflection becomes difficult or even impossible. To counter this problem, code generators often embed syntactic and semantic information and the code necessary for execution.

See also Automatic programming Comparison of code generation tools Source-to-source compilation: automatic translation of a computer program from one programming language to another

References

Worked examples

Example 1 — a first encounter with Code generation (compiler)

Start with the simplest possible case. Write down what Code generation (compiler) claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In biology, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Code generation (compiler) before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Code generation (compiler) ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Code generation (compiler)

In research
Code generation (compiler) appears in biology research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Code generation (compiler) in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Code generation (compiler) is common in secondary-school and first-year university syllabi. It links to neighbouring topics Compiler construction, Machine code, so understanding it makes those chapters shorter.
In everyday life
Look for Code generation (compiler) outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Code generation (compiler)” →

How to study Code generation (compiler) in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Code generation (compiler) means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Code generation (compiler) out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Code generation (compiler) in simple terms?

In computing, code generation is part of the process chain of a compiler, in which an intermediate representation of source code is converted into a form (e.g., machine code) that the target system can readily execute. Sophisticated compilers typically perform multiple passes over various intermedi…

Why does Code generation (compiler) matter?

Because it connects several biology ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Code generation (compiler)?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Code generation (compiler).

Tags

  • Compiler construction
  • Machine code

Keep exploring