当前位置:网站首页>Four processes of program operation
Four processes of program operation
2022-07-06 17:53:00 【TABE_】
Here's the catalog title
The program needs to run , There are four steps to go through : Preprocessing 、 compile 、 Compilations and links , As shown in the figure below :
The above options need to be explained .
Use gcc If the command is not followed by any options , Preprocessing will be performed by default 、 compile 、 assembly 、 Link the whole process , If the program is correct , You'll get an executable , The default is a.out.
-E Options : Prompt the compiler to stop after preprocessing , Later compilation 、 assembly 、 The link will not be executed first .
-S Options : Prompt the compiler to stop after compiling , Don't perform assembly and linking .
-c Options : Prompt the compiler to stop after executing the assembly .
therefore , These three options are equivalent to limiting the stop time of compiler execution , Instead of carrying out a step alone .
Preprocessing
The operation of the pretreatment process :
- Will all “#define” Delete , And expand all the macro definitions
- Handle all conditional compilation instructions , such as “#if”、“#ifdef”、“#elif”、“#else”、“#endif”
- Handle “#include” Precompiling instructions , Insert the included header file into the location of the compilation instruction .( This process is recursive , Because the included files may also contain other files )
- Delete all comments “//” and “/* */”.
- Add line number and filename ID , It is convenient for the compiler to generate the line number for debugging when compiling later, and to display the line number when compiling errors or warnings .
- Keep all #pragma Compile instructions , Because the compiler needs to use them .
Use a simple program to verify that the facts are the same as mentioned above . Write a simple program , And then use -E Option to perform the preprocessing process , Open generated .i Compare the file with the source file , The result is clear at a glance , As shown in the figure below :
For adding line numbers to the code, this is not demonstrated here , We don't add line numbers manually when writing code , The line numbers we see are automatically added by the editing tool we use , These line number compilation systems are invisible , But what? , We found that if we had a problem with one line of code , When compiling, you will be prompted that there is something wrong with the line of code , This has proved that , The compiler will automatically add line numbers .
compile
Use -S Options , Indicates that the compilation operation ends after execution . Corresponding to generate a .s file .
The compilation process is the core part of the whole program construction , Compile successfully , Will convert the source code from text form to machine language , The compilation process is to perform lexical analysis on the preprocessed files 、 Syntax analysis 、 Semantic analysis 、 Intermediate code generation and object code generation and optimization , Finally, the assembly code file is formed .
Lexical analysis
Lexical analysis uses a method called lex The program realizes lexical scanning , It will divide the input string into marks according to the lexical rules described by the user . The generated marks are generally divided into : keyword 、 identifier 、 Literal ( Contains figures 、 String, etc. ) And special symbols ( Operator 、 Equal sign, etc ), Then they put them in the corresponding table .
Syntax analysis
The parser is based on the syntax rules given by the user , Parse the token sequence generated by lexical analysis , Then they form a syntax tree . For different languages , But its grammar rules are different . There is also a ready-made tool for parsing , be called :yacc.
Semantic analysis
Syntax analysis completes the analysis of expression syntax , But it doesn't know if this statement really makes sense . Some statements are grammatically legal , But it has no practical significance , For example, two pointers do multiplication , At this time, semantic analysis is needed , But the compiler can only analyze static semantics .
Static semantics : The semantics can be determined at compile time . It usually includes declaring a match with the type 、 Conversion of type . For example, when a floating-point expression is assigned to an integer expression , It implies a conversion from floating-point to integer , Semantic analysis needs to complete this transformation , Another example , Assign a floating-point expression to a pointer , This is definitely not going to work , During semantic analysis, it will be found that the two types do not match , The compiler will report an error .
Dynamic semantics : Semantics that can only be determined at run time . For example, divide two integers , Grammatically, no problem , The type also matches , Sounds all right , however , If the divisor is 0 Words , That's a problem , And this problem is unknown in advance , Only when it is running can it be found that there is a problem , This is dynamic semantics .
Intermediate code generation
Our code can be optimized , For some values that can be determined during compilation , It will be optimized , For example, in the above example 2+6, During compilation, its value can be determined as 8 了 , But it is difficult to optimize the grammar directly , At this time, the optimizer will turn the syntax tree into the middle code first . Intermediate code is generally independent of the target machine and running environment .( Dimensions without data 、 Variable address and register name, etc ). Intermediate code has different forms in different compilers , There are three common address codes and P- Code .
Intermediate code allows the compiler to be divided into front-end and back-end . The compiler front end is responsible for generating machine independent intermediate code , The compiler back end replaces intermediate code with machine code .
Target code generation and optimization
The code generator converts intermediate code into machine code , This process depends on the target machine , Because different machines have different word lengths 、 register 、 Data type, etc .
Finally, the object code optimizer optimizes the object code , For example, select the appropriate addressing mode 、 Use uniqueness instead of multiplication and division 、 Delete redundant instructions, etc .
assembly
The assembly procedure calls the assembler as To complete , Is used to convert assembly code into machine executable instructions , Almost every assembly statement corresponds to a machine instruction .
Use command as hello.s -o hello.o Or use gcc -c hello.s -o hello.o To execute until the end of the assembly process , The corresponding generated file is .o file .
link
The main content of the link is to correctly connect the parts referenced by each module . Its job is to modify the references of some instructions to other symbolic addresses . The link process mainly includes address and space allocation 、 Symbol resolution and relocation .
Symbol resolution
Sometimes called symbolic binding 、 Name binding 、 Name resolution 、 Or address binding , In fact, it means using symbols to identify an address . for instance int a = 6, Such a code , use a To identify a block 4 Bytes of space , The content stored in the space is 6.
relocation
The process of recalculating the address of each target is called relocation , Relocation is divided into the following two steps :
- Relocate section and symbol definitions : Merge sections of the same type , Then the linker assigns the memory address of the runtime to the new aggregation section , Assign to each section defined by the input module , And each symbol assigned to the input module definition . This step is complete , Each instruction and global variable in the program has a unique runtime memory address . That is, the instruction segment and the global in the code segment 、 Static variables 、 The function entry and the runtime memory address of the instruction have been determined .
- Relocate symbol references in a section : The linker modifies the reference to each symbol in the code section and data section , Make them point to the correct runtime address . In this step, the linker depends on the data structure called relocation entry in the relocatable target module . When the link is not completed, the location of global variables or functions of other modules in one module is unknown , Use the relocation entry to tell the linker how to modify this reference when merging the target file into an executable
边栏推荐
- The art of Engineering
- Distinguish between basic disk and dynamic disk RAID disk redundant array
- Smart street lamp based on stm32+ Huawei cloud IOT design
- Is it meaningful for 8-bit MCU to run RTOS?
- 偷窃他人漏洞报告变卖成副业,漏洞赏金平台出“内鬼”
- 视频融合云平台EasyCVR增加多级分组,可灵活管理接入设备
- 1700C - Helping the Nature
- 《ASP.NET Core 6框架揭秘》样章发布[200页/5章]
- Growth of operation and maintenance Xiaobai - week 7
- Summary of study notes for 2022 soft exam information security engineer preparation
猜你喜欢
Interview shock 62: what are the precautions for group by?
RB157-ASEMI整流桥RB157
Kivy tutorial: support Chinese in Kivy to build cross platform applications (tutorial includes source code)
学 SQL 必须了解的 10 个高级概念
FMT开源自驾仪 | FMT中间件:一种高实时的分布式日志模块Mlog
SQL statement optimization, order by desc speed optimization
Reppoints: advanced order of deformable convolution
Zen integration nails, bugs, needs, etc. are reminded by nails
Open source and safe "song of ice and fire"
李書福為何要親自掛帥造手機?
随机推荐
MarkDown语法——更好地写博客
Pourquoi Li shufu a - t - il construit son téléphone portable?
Kali2021 installation and basic configuration
Essai de pénétration du Code à distance - essai du module b
容器里用systemctl运行服务报错:Failed to get D-Bus connection: Operation not permitted(解决方法)
Fleet tutorial 13 basic introduction to listview's most commonly used scroll controls (tutorial includes source code)
Zen integration nails, bugs, needs, etc. are reminded by nails
Cool Lehman has a variety of AI digital human images to create a vr virtual exhibition hall with a sense of technology
一体化实时 HTAP 数据库 StoneDB,如何替换 MySQL 并实现近百倍性能提升
node の SQLite
MySQL stored procedure
中移动、蚂蚁、顺丰、兴盛优选技术专家,带你了解架构稳定性保障
Interview assault 63: how to remove duplication in MySQL?
The shell generates JSON arrays and inserts them into the database
OpenCV中如何使用滚动条动态调整参数
Selected technical experts from China Mobile, ant, SF, and Xingsheng will show you the guarantee of architecture stability
ASEMI整流桥DB207的导通时间与参数选择
Pytest learning ----- detailed explanation of the request for interface automation test
Interview shock 62: what are the precautions for group by?
Unity粒子特效系列-闪星星的宝箱