Difference between one pass and multi pass compilers?

后端 未结 6 1085
逝去的感伤
逝去的感伤 2021-02-06 03:24

I have seen a lot of posts regarding one pass and multi pass compilers but i dont seem to get the point.

  • What are one

相关标签:
6条回答
  • 2021-02-06 03:39

    A pass in a compiler is nothing but going through the entire program once from top to bottom

    .Often a pass will consist of several phases.Generally one pass is enough for both lexical and syntax analysis to get completed.That means by going through the entire program once ,we can check whether a statement is syntactically correct or not.And then the output of a syntax analysis is an Abstract syntax tree,which is nothing but the source code(condensed) in the form of a tree.The semantic analyser has to go through the AST to do its work and there by effectively going through the entire program once more. That is to complete Semantic analysis two passes are required.(if an explicit AST is required).

    0 讨论(0)
  • 2021-02-06 03:40

    A "pass" reads the source program or the output of the previous pass, makes the transformations according to its phases and writes output into an intermediate file, which may then be read through next pass.

    0 讨论(0)
  • 2021-02-06 03:48

    A single pass compiler is a compiler that scans the program only once and generates equivalent binary program.

    On the other hand a multi pass compiler processes the source code several times(multi pass) and each times the previous generated code works as the input.

    From the above definition, the difference between single pass and multi pass compiler is clear enough.Besides this multi pass makes your code more error free weather the chance of error detection is lower in single pass compiler.A multi pass compiler takes lots of time.That's why a single pass compiler is faster than multi pass compiler.

    0 讨论(0)
  • 2021-02-06 03:52

    A multi-pass compiler is one that separates compilation into multiple passes, where each pass would continue with the result of the previous pass. Such passes could include parsing, type checking, intermediate code generation, various optimization passes and finally code generation. So for example the parser might create a parse tree, which the type checker would then check for type errors and the intermediate code generator could translate into some form of intermediate code. The optimization passes would then each take the current intermediate code and turn it into a more optimized form. Finally the code generation pass would take the optimized intermediate code and produce the target code from it.

    In a single-pass compiler all of the steps happen in one pass. So it reads some of the source code, analyses it, typechecks it, optimizes it and generates code for it, and only then moves on to the next bit of code.

    Single pass compilers consume less memory (because they don't hold the whole AST and/or intermediate code in memory) and generally run faster.

    Some languages, like C, are designed to be compilable in a single pass, but others are not. For example functions in C need to be declared before their first use, so the compiler has already seen the function's type signature before it reads the function call. It can then use that information for type checking. In more modern languages, like Java or C# for example, functions can be called before their definition (and forward-declarations don't exist). Such languages can't be compiled in a single pass because the type checker might know nothing about a function's signature when it encounters the function call, making it impossible to typecheck the program without having first processed the whole file.

    Further a multi-pass compiler can make use of more kinds of optimizations, so even for languages that can be compiled in a single pass, modern compilers usually use multiple passes.

    0 讨论(0)
  • 2021-02-06 03:57
    • A one-pass compiler is a compiler that passes through the source code of each compilation unit only once. A multi-pass compiler is a type of compiler that processes the source code or abstract syntax tree of a program several times.

    • A one-pass compilers is faster than multi-pass compilers

    • A one-pass compiler has limited scope of passes but multi-pass compiler has wide scope of passes.

    • Multi-pass compilers are sometimes called wide compilers where as one-pass compiler are sometimes called narrow compiler.

    • Many programming languages cannot be represented with a single pass compilers, for example Pascal can be implemented with a single pass compiler where as languages like Java require a multi-pass compiler.
    0 讨论(0)
  • 2021-02-06 04:03

    The origin of the term multi-pass comes from a time when computers had a lot less memory. Compilers need a lot of memory, and in a small memory machine, this is hard to manage.

    So the original idea was a compiler ran in multiple passes. The first pass read the source code, and did basic tasks such as syntax checking, maybe building a symbol table, and then wrote its results to a disk file for the second pass. Each successive pass N would read the previous pass's result, and change the program representation to move it further and further towards machine code, and write out its results for pass N+1 to read. This process repeated until the final pass produced the final code. Many compilers could get by a few ("multi") passes; there were reputed compilers with dozens of passes built on really old machines.

    (This same concept applies to so called "two pass assemblers": first pass reads the assembler source code, syntax checks, figures out what location values should be used for label symbols; second pass produces object code using the knowledge of the symbol locations assigned in the first pass).

    Memory is now larger, and it is quite practical for read source code for every very big programs into memory, have the compiler do all its work in the memory of a single process, and write the object code. You still see some analogical remnants of this in the concept of linkers; they glue together multiple object modules ("the first pass") into a single binary.

    If you look at compiler internally, they operate in phases. Typical phases might be:

    *  Parse and syntax check
    *  Build symbol tables
    *  Perform semantic sanity check
    *  Determine control flow
    *  Determine data flow
    *  Generate some "intermediate" language (representing abstract instructions)
    *  Optimize the intermediate language
    *  Generate machine code from the optimized language
    

    What a specific compiler does for phases varies from compiler to compiler. Each of these steps pushes the program representations closer to the final machine code. An N-pass compiler would bundle one or more of these steps into a single pass.

    Back to present times, we have lots of memory; no need for a modern compiler to write the intermediate results to a disk file, so all these phases happen in the memory of a single process. You, the compiler user, don't see them. So you might call modern compilers "one pass" in the original senses of the word. Since nobody now cares, the phrase has simply fallen into disuse.

    In any case, the compilers are still generally multi-phased internally. (There are compilers that do all of these steps in what amounts a single phase; normally, they cannot do a lot of optmization).

    0 讨论(0)
提交回复
热议问题