Project Stage 1: Preparation(part-2)
Yukti Manoj Mulani
Posted on June 3, 2024
Hiiii peeps!! I am back with another part for our project stage -1 called preparation and in this stage we will continue with Argument Parsing. So without wasting any time let's get started!!
Task: Find the code that controls the argument parsing. Add a dummy argument and experiment with messages for the user that describe how and when that argument can be used.
Argument parsing in GCC is the process where the compiler interprets the command-line options provided by the user to control the compilation process. This involves reading the arguments, validating them, and setting internal flags or configurations based on these options. Now how are we going to h=find the code that controls argument parsing. Remember where the gcc code base is? After installing and building gcc you will se a directory in the home directory called gcc and in that directory we have another directory called gcc , here is the code base and also the key file needed for argument parsing called "common.opt".
So the address of the file will be gcc/gcc/common.opt
.
Below are the steps to add a dummy argument.
- Define a dummy argument in common.opt The common.opt file defines the command-line options available to GCC. Each option has a specific format and fields that describe its behavior. Open common.opt and add a new line for your dummy argument.
fdummy-arg
Common
Report
Dummy argument for testing
The fields here are:
- fdummy-arg: The name of the argument (e.g., -fdummy-arg).
- Common: Indicates that this option is available across different languages.
- Report: The category of the option.
- The last line provides a brief description.
And this is were I started doubting my skills and knowledge. I felt like there is biiiig hole in my knowledge somewhere because the things after this step everything started going wrong. According to my research you need to implement your argument in the gcc.cc
and I did the same but all my builds kept on failing. So, for now I will keep this task on the side to rethink it with a fresh mind and move on to our other tasks. 😔
Task:Find out where argument information is stored and how it can be accessed in code.
Argument information is typically stored in structures like
cl_decoded_option
and managed in arrays such as decoded_options
.You can access the argument information in your pass or other parts of the GCC codebase by iterating over these arrays. In my opinion the below code should work if the your new dummy options are working unlike mine. 😞
for (int i = 0; i < decoded_options_count; i++)
{
if (strcmp(decoded_options[i].opt, "my-dummy-option") == 0)
{
// Handle the option
}
}
Task: Find out how dumps are produced during the compilation passes (in particular, during the tree passes). Become familiar with producing and reviewing these dumps. Create a dummy pass that produces a useful diagnostic dump.
Dumps are usually produced by functions that print the intermediate representation of the code to files. These functions are often called within passes.Dumps in GCC are a powerful feature used for debugging and understanding the compilation process. They provide intermediate representations of the code at various stages of compilation.
Enabling Dumps
To enable dumps when compiling a program, use the -fdump options. These options control the generation of dump files for various stages.
Basic Usage
gcc -fdump-tree-all -c your_program.c
Specific Dumps:
Tree Dumps:
-fdump-tree-original: Dump the original AST.
-fdump-tree-gimple: Dump the GIMPLE representation.
-fdump-tree-optimized: Dump the optimized AST.
RTL Dumps:
-fdump-rtl-all: Dump all RTL stages.
-fdump-rtl-expand: Dump RTL after expansion.
-fdump-rtl-optimized: Dump the optimized RTL.
Control Flow Graph Dumps:
-fdump-tree-cfg: Dump the control flow graph for trees.
-fdump-rtl-cfg: Dump the control flow graph for RTL.
Reflections
What I Learned
Embarking on the project to understand and modify the GCC codebase has been a profound learning experience. Despite the setbacks, I gained valuable insights into how GCC handles argument parsing and compilation passes. The intricate nature of a compiler like GCC, with its vast and complex codebase, highlighted the importance of having a structured approach when making modifications.
Challenges
The primary challenge I faced was in modifying the argument parsing mechanism. While adding a dummy argument to common.opt seemed straightforward, implementing the actual handling of this argument in gcc.cc proved difficult. The build process failed multiple times, indicating deeper issues with my approach or possibly gaps in my understanding of the code structure.
Complexity of GCC Codebase:
The sheer size and complexity of GCC's codebase made navigation and pinpointing the exact locations for modifications challenging. Each file and function has interdependencies that are not immediately apparent.
Build Failures:
The build failures were particularly frustrating as they often stemmed from small, seemingly insignificant mistakes. Error messages were sometimes cryptic, requiring extensive research to decode and understand.
#Understanding GCC Internals:
While I had a theoretical understanding of compilers, delving into GCC’s internals was a different ball game. The use of various data structures and the flow of data between them was more complex than anticipated.
Knowledge Gaps
This project highlighted several gaps in my knowledge:
Deep Understanding of Compiler Theory:
Although I had a basic understanding of compiler theory, I realized I needed a deeper comprehension, especially regarding GCC's specific implementation of these theories.
Debugging and Troubleshooting Build Issues:
My skills in debugging complex build issues need improvement. I often found myself stuck, unsure how to proceed after encountering a build error.
Navigating Large Codebases:
Efficiently navigating and understanding large codebases like GCC is a skill I need to develop further. This includes being able to track down the relationships between different parts of the code and understanding their interactions.
How I Plan to Address These Gaps
Studying Compiler Theory:
I plan to delve deeper into compiler theory through academic resources and online courses. Understanding the foundational principles at a deeper level should help me grasp GCC's implementation better.
Improving Debugging Skills:
I will practice debugging in different environments and with different types of software projects. Additionally, learning to use advanced debugging tools and techniques will be crucial.
Exploring Smaller Compiler Projects:
Before jumping back into GCC, I will explore smaller, simpler compiler projects. This will allow me to build my skills and confidence in a less overwhelming environment.
Engaging with the Community:
I will become more active in the GCC community, seeking advice and learning from experienced contributors. This can provide practical insights and support as I continue to work on GCC.
Tasks I Found Most Attractive
Despite the challenges, I found the process of attempting to add new features to be the most engaging. Specifically, I enjoyed:
Coding and Experimentation:
Writing and experimenting with code, even when it didn't work out, was a rewarding challenge. The process of debugging and understanding errors, though difficult, was intellectually stimulating.
Understanding the Compilation Process:
Learning about the different passes in the compilation process and how GCC manages these was fascinating. It provided a clearer picture of how source code is transformed into executable programs.
Conclusion
While this part of the project didn't yield the results I hoped for, it was a significant learning experience. I now have a clearer idea of my strengths and the areas I need to improve. Moving forward, I am better prepared to tackle similar challenges, armed with the knowledge and experience gained from this attempt.
So that is all for this failed series of Project Stage-1.
Below are some sources that might help you when you get stuck up like me. I hope you can make a good use of them.
Resources
GCC github-mirror
GCC Documentation
GCC Internals Mannual
Installing GCC
Thank you for reading even if it was a failed attempt.
Until next time Happy Coding!!! 🙋♀️
Posted on June 3, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.