Difference between one pass and multi pass compilers?

后端 未结 6 1092
逝去的感伤
逝去的感伤 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: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.

提交回复
热议问题