principles of compiler design
play

Principles of Compiler Design - The Brainf*ck Compiler - Clifford - PowerPoint PPT Presentation

Principles of Compiler Design - The Brainf*ck Compiler - Clifford Wolf - www.clifford.at http://www.clifford.at/papers/2004/compiler/ Clifford Wolf, December 22, 2004 http://www.clifford.at/papers/2004/compiler/ p. 1/56 Introduction


  1. Principles of Compiler Design - The Brainf*ck Compiler - Clifford Wolf - www.clifford.at http://www.clifford.at/papers/2004/compiler/ Clifford Wolf, December 22, 2004 http://www.clifford.at/papers/2004/compiler/ – p. 1/56

  2. Introduction ● Introduction ● Overview (1/2) ● Overview (2/2) ● Aim Brainf*ck Introduction Lexer and Parser Code Generators Tools Complex Code Generators The BF Compiler Stack Machines The SPL Project LL(regex) parsers URLs and References Clifford Wolf, December 22, 2004 http://www.clifford.at/papers/2004/compiler/ – p. 2/56

  3. Introduction ■ My presentation at 20C3 about CPU design featuring a Introduction ● Introduction Brainf*ck CPU was a big success ● Overview (1/2) ● Overview (2/2) ● Aim Brainf*ck ■ My original plan for 21C3 was to build a Brainf*ck CPU with Lexer and Parser tubes.. Code Generators Tools ■ But: Complex Code Generators The only thing more dangerous than a hardware guy with a The BF Compiler code patch is a programmer with a soldering iron. Stack Machines The SPL Project ■ So this is a presentation about compiler design featuring a LL(regex) parsers Brainf*ck Compiler. URLs and References Clifford Wolf, December 22, 2004 http://www.clifford.at/papers/2004/compiler/ – p. 3/56

  4. Overview (1/2) In this presentation I will discuss: Introduction ● Introduction ● Overview (1/2) ● Overview (2/2) ■ A little introduction to Brainf*ck ● Aim Brainf*ck Lexer and Parser ■ Components of a compiler, overview Code Generators Tools ■ Designing and implementing lexers Complex Code Generators The BF Compiler Stack Machines ■ Designing and implementing parsers The SPL Project LL(regex) parsers ■ Designing and implementing code generators URLs and References ■ Tools (flex, bison, iburg, etc.) Clifford Wolf, December 22, 2004 http://www.clifford.at/papers/2004/compiler/ – p. 4/56

  5. Overview (2/2) ■ Overview of more complex code generators Introduction ● Introduction ◆ Abstract syntax trees ● Overview (1/2) ● Overview (2/2) ◆ Intermediate representations ● Aim ◆ Basic block analysis Brainf*ck ◆ Backpatching Lexer and Parser ◆ Dynamic programming Code Generators ◆ Optimizations Tools Complex Code Generators The BF Compiler ■ Design and implementation of the Brainf*ck Compiler Stack Machines The SPL Project ■ Implementation of and code generation for stack machines LL(regex) parsers URLs and References ■ Design and implementation of the SPL Project ■ Design and implementation of LL(regex) parsers Clifford Wolf, December 22, 2004 http://www.clifford.at/papers/2004/compiler/ – p. 5/56

  6. Aim ■ After this presentation, the auditors .. Introduction ● Introduction ● Overview (1/2) ● Overview (2/2) ● Aim ■ .. should have a rough idea of how compilers are working. Brainf*ck Lexer and Parser ■ .. should be able to implement parsers for complex Code Generators configuration files. Tools Complex Code Generators ■ .. should be able to implement code-generators for stack The BF Compiler machines. Stack Machines The SPL Project LL(regex) parsers ■ .. should have a rough idea of code-generation for register URLs and References machines. Clifford Wolf, December 22, 2004 http://www.clifford.at/papers/2004/compiler/ – p. 6/56

  7. Introduction Brainf*ck ● Overview ● Instructions ● Implementing "while" ● Implementing "x=y" Brainf*ck ● Implementing "if" ● Functions Lexer and Parser Code Generators Tools Complex Code Generators The BF Compiler Stack Machines The SPL Project LL(regex) parsers URLs and References Clifford Wolf, December 22, 2004 http://www.clifford.at/papers/2004/compiler/ – p. 7/56

  8. Overview ■ Brainf*ck is a very simple turing-complete programming Introduction language. Brainf*ck ● Overview ● Instructions ● Implementing "while" ■ It has only 8 instructions and no instruction parameters. ● Implementing "x=y" ● Implementing "if" ● Functions ■ Each instruction is represented by one character: Lexer and Parser < > + - . , [ ] Code Generators Tools ■ All other characters in the input are ignored. Complex Code Generators The BF Compiler ■ A Brainfuck program has an implicit byte pointer which is free Stack Machines to move around within an array of 30000 bytes, initially all set The SPL Project to zero. The pointer itself is initialized to point to the LL(regex) parsers beginning of this array. URLs and References Some languages are designed to solve a problem. Others are designed to prove a point. Clifford Wolf, December 22, 2004 http://www.clifford.at/papers/2004/compiler/ – p. 8/56

  9. Instructions Introduction Increment the pointer. > ++p; Brainf*ck ● Overview Decrement the pointer. < --p; ● Instructions ● Implementing "while" ● Implementing "x=y" Increment the byte at the pointer. + ++*p; ● Implementing "if" ● Functions Decrement the byte at the pointer. - ++*p; Lexer and Parser Output the byte at the pointer. . Code Generators Tools putchar(*p); Complex Code Generators Input a byte and store it in the byte at the pointer. , The BF Compiler *p = getchar(); Stack Machines Jump forward past the matching ] if the byte at the pointer is zero. The SPL Project [ LL(regex) parsers while (*p) { URLs and References Jump backward to the matching [ unless the byte at the pointer is zero. ] } Clifford Wolf, December 22, 2004 http://www.clifford.at/papers/2004/compiler/ – p. 9/56

  10. Implementing "while" ■ Implementing a while statement is easy, because the Introduction Brainf*ck [ .. ] statement is a while loop. Brainf*ck ● Overview ● Instructions ● Implementing "while" ● Implementing "x=y" ■ So while (x) { <foobar> } becomes: ● Implementing "if" ● Functions Lexer and Parser <move pointer to a> Code Generators [ Tools <foobar> Complex Code Generators <move pointer to a> The BF Compiler ] Stack Machines The SPL Project LL(regex) parsers URLs and References Clifford Wolf, December 22, 2004 http://www.clifford.at/papers/2004/compiler/ – p. 10/56

  11. Implementing "x=y" ■ Implementing assignment (copy) instructions is a bit more Introduction complex. Brainf*ck ● Overview ● Instructions ● Implementing "while" ■ The straight forward way of doing that resets y to zero: ● Implementing "x=y" ● Implementing "if" ● Functions <move pointer to y> [ - Lexer and Parser <move pointer to x> + Code Generators <move pointer to y> ] Tools ■ So, a temporary variable t is needed: Complex Code Generators The BF Compiler <move pointer to y> [ - Stack Machines <move pointer to t> + The SPL Project <move pointer to y> ] LL(regex) parsers URLs and References <move pointer to t> [ - <move pointer to x> + <move pointer to y> + <move pointer to t> ] Clifford Wolf, December 22, 2004 http://www.clifford.at/papers/2004/compiler/ – p. 11/56

  12. Implementing "if" ■ The if statement is like a while-loop, but it should run its Introduction block only once. Again, a temporary variable is needed to Brainf*ck ● Overview implement if (x) { <foobar> } : ● Instructions ● Implementing "while" ● Implementing "x=y" <move pointer to x> [ - ● Implementing "if" ● Functions <move pointer to t> + Lexer and Parser <move pointer to x> ] Code Generators Tools <move pointer to t> [ Complex Code Generators The BF Compiler [ - Stack Machines <move pointer to x> + The SPL Project <move pointer to t> ] LL(regex) parsers URLs and References <foobar> <move pointer to t> ] Clifford Wolf, December 22, 2004 http://www.clifford.at/papers/2004/compiler/ – p. 12/56

  13. Functions ■ Brainf*ck has no construct for functions. Introduction Brainf*ck ● Overview ● Instructions ■ The compiler has support for macros which are always ● Implementing "while" ● Implementing "x=y" inlined. ● Implementing "if" ● Functions Lexer and Parser ■ The generated code may become huge if macros are used Code Generators intensively. Tools Complex Code Generators ■ So recursions must be implemented using explicit stacks. The BF Compiler Stack Machines The SPL Project LL(regex) parsers URLs and References Clifford Wolf, December 22, 2004 http://www.clifford.at/papers/2004/compiler/ – p. 13/56

  14. Introduction Brainf*ck Lexer and Parser ● Lexer ● Parser Lexer and Parser ● BNF ● Reduce Functions ● Algorithms ● Conflicts Code Generators Tools Complex Code Generators The BF Compiler Stack Machines The SPL Project LL(regex) parsers URLs and References Clifford Wolf, December 22, 2004 http://www.clifford.at/papers/2004/compiler/ – p. 14/56

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend