These are chat archives for learnclang/general

31st
Dec 2014
Nysuatro
@Nysuatro
Dec 31 2014 02:06

If you want to learn more about compilers ( and in depth) check out this free course :)

https://www.coursera.org/course/compilers

Liju Kunnummal
@ljkart
Dec 31 2014 02:33

More About Compiler

The Compiler design consists of the following phases.

  1. Lexical Analyzer<br>

  • The source code is converted into stream of tokens
  • Removes white spaces and comments
  • eg: x = a + b * c /* source code */ --> Lexical Analyzer --> id = id + id * id
  • This is achieved by using patterns which is known to the lexical analyzer

  2. Syntax Analyzer(Parser)<br>

  • It takes the token produced by lexical analysis as input and generates a parse tree (or syntax tree)
  • token arrangements are checked against the source code grammar, i.e. the parser checks if the expression made by the tokens is syntactically correct.

  3. Semantic Analyzer<br>

  • Semantic analysis checks whether the parse tree constructed follows the rules of language.
  • For example, assignment of values is between compatible data types, and adding string to an integer
  • Also, the semantic analyzer keeps track of identifiers, their types and expressions; whether identifiers are declared before use or not etc
  • The output of semantic analyzer will be a meaningfully verified parse tree.

  4. Intermediate Code Generator<br>

  • After semantic analysis the compiler generates an intermediate code of the source code for the target machine
  • It represents a program for some abstract machine. It is in between the high-level language and the machine language.
  • There are various kinds of intermediate code and most popular one is three address code
eg: 
 x = a + b * c - three address code representation is,
 t1 = b * c
 t2 = t1 + a
 x = t2

  5. Code Optimizer<br>

  • Optimization can be assumed as something that removes unnecessary code lines, and arranges the sequence of statements in order to speed up the program execution without wasting resources (CPU, memory).
    eg: 
    the optimized code will be,
    t1 = b * c
    x = a + t1
      6. Target Code Generator
  • Target code generator will generate codes that assembler can understand
  • This will write the assembler code according to type of assembler used in different platform.

    Summary

  • The phases of a compiler are collected into front end and back end.
  • front-end - all analysis phase(1-3) including intermediate code generation
  • back-end - includes the code optimization phase and final code generation phase.
  • You don't need to rebuild the whole phase to design a new compiler for a new platform, you can take the front-end and change the back-end according to the platform.
  • That's why we have to recompile the code according to platform they run because each platform uses different assembler. And even this applicable while we porting codes from computer to mobile platform.
  • Clang - Clang is a compiler front end for the C, C++, Objective-C and Objective-C++ programming languages. It uses LLVM as its back end and has been part of the LLVM release cycle since LLVM 2.6.
  • LLVM(Low Level Virtual Machine) - LLVM Project is a collection of modular and reusable compiler and toolchain technologies. More at LLVM Wiki
Marcus Ottosson
@mottosso
Dec 31 2014 09:37

If you want to learn more about compilers ( and in depth) check out this free course

That looks like a great resource, thanks for sharing!

@ljkart Perfect! Short and sweet, also saw you added it to the Basic Workflow section in the Wiki, great work!
David Martinez
@davidmartinezanim
Dec 31 2014 11:19
Appreciated. thanks guys!
Liju Kunnummal
@ljkart
Dec 31 2014 12:09
thanks @mottosso
Marcus Ottosson
@mottosso
Dec 31 2014 12:13
That's a good graph, now I get what Front-end means. So LLVM must be the Back end then?

What do you guys think about either next or next-next project being to "Open a window"? And that we'll do it using whichever platform we're at or would like to try it on, following up with tutorials for each platform, like Windows using the windows.h like in the Handmade Hero videos, and Linux using X or Wayland and so forth.


No drawing or anything, just to get a window opened.

I would like to get visual asap, as I think it will make a lot of things more clear and interesting.
David Martinez
@davidmartinezanim
Dec 31 2014 12:22
This message was deleted
This message was deleted
This message was deleted
Liju Kunnummal
@ljkart
Dec 31 2014 12:24
I think LLVM started as a backend..! but now it's grown to be an umbrella project contains many reusable libraries and many other tools is well and which is currently known as LLVM Project.
sure we can try opening a window..!
Marcus Ottosson
@mottosso
Dec 31 2014 12:26
I wonder what is involved in writing a new language
Maybe we'll make that the next-next-next project? :)
Seeing as everyone's doing that now, Fabric with it's KL and Flowbox with their Luna, along with Julia, Halide and a number of other new languages popping up.
Cesar Saez
@csaez
Dec 31 2014 12:31
Jonathan Blow has done a series of videos on his new language (jai, gamedev oriented), it's an interesting project.
Liju Kunnummal
@ljkart
Dec 31 2014 12:34
link?
Liju Kunnummal
@ljkart
Dec 31 2014 12:43
thanks @csaez
Marcus Ottosson
@mottosso
Dec 31 2014 12:54
Looks interesting, thanks for sharing