0% found this document useful (0 votes)
71 views4 pages

02 - How Compiler Works

The document describes the main stages of how a compiler works: 1) Preprocessing evaluates all preprocessor statements and tokenizes the source code. 2) Parsing sorts the tokens into an understandable format by creating an abstract syntax tree. 3) Code generation produces machine code that the computer can execute and data storage from the abstract syntax tree.

Uploaded by

22194
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views4 pages

02 - How Compiler Works

The document describes the main stages of how a compiler works: 1) Preprocessing evaluates all preprocessor statements and tokenizes the source code. 2) Parsing sorts the tokens into an understandable format by creating an abstract syntax tree. 3) Code generation produces machine code that the computer can execute and data storage from the abstract syntax tree.

Uploaded by

22194
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

how compiler works

• functions
– preprocessing
• all preprocessor statements are evaluated
• then and there
– tokenising
– parsing
• sort english into an understandable format
– creating
• abstract syntax tree creation
– representation of the code
– but as abstract syntax tree
– scope: convert code into
• constant data
• instructions
– created the abstract syntax tree
– generate code
• machine code
– that computer can execute
• other data
– place to store constant variables
• what has compiler done
– generated obj file for each for each cpp file
• (for each translation unit)
• cpp file = translation unit
• files don not exist in c++
• file is a way to feed compiler with source code
– tel compile what kind of file it is
– how should it treat is
• .cpp
• .c
• .h
– default conventions
– it will copile it as a translation unit
• common to create one big cpp file
– one traslation unit
– one object file
• stages
– generated files for each translation unit
• cpp files are translation names
• a way to feed the compiler with the surce code
– files do not matter
– it will copile it as a translation unit
• smth that is compiled into object file
• into object file
• in java
– class name has to be tied to the file name
– folder hierarchy had to be tied to a package
• preprocessor statement
– include
• which file you want to include
– define
– if
– ifdef
– pragma statements
• tell compiler exactly what to do
• .i files
– see what the preprocessor actually generated
#line 1 "D:\\C++\\01P\\Project1\\Project1\\math.cpp"
int Multiply(int a, int b)
{
int result = a * b;
return result;
#line 1 "D:\\C++\\01P\\Project1\\Project1\\EndBrace.h"
}-(it included it insead of the endbrace)
#line 6 "D:\\C++\\01P\\Project1\\Project1\\math.cpp"

definte INTEGER int


#if 0

int Multiply(int a, int b)


{
int result = a * b;
return result;
}

#endif

disable preprocessor to be able to build the cpp file

output properties assempler only


without result = a * b
int Multiply(int a, int b)
{
int result = a * b;
return result;
}

; Line 5
mov eax, DWORD PTR _a$[ebp]
<!-- load the a variable into eax register -->
imul eax, DWORD PTR _b$[ebp]
<!-- imul instruction = multiplication instruction on -->
<!-- a variable and b variable -->
mov DWORD PTR _result$[ebp], eax
<!-- store the result of that into a variable called result -->
; Line 6
mov eax, DWORD PTR _result$[ebp]
<!-- move it back to eax to return it -->

without result = a * b
int Multiply(int a, int b)
{
return a * b;
}

; Line 5
mov eax, DWORD PTR _a$[ebp]
imul eax, DWORD PTR _b$[ebp]

optimization
• optimization
– under debut
– max speed O2
! incompatible with RTC
• code generation
– basic runtime checks
• RTC
• defalut [x]
• constant folding
– 5x2
– everything constant workd out at compile time
mov eax, DWORD PTR _message$[ebp]
• move message pointer into eax
– which is a return register
• call to log
– call ?Log@@YAPBDPBD@Z
• @ functions signature
– uniquely define a function
– for linking

You might also like