02 - How Compiler Works

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 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