# Some Troubleshooting: On the way

### Troubleshooting Process

Debugging with approach be:

1. **Initial Error Analysis**: When my first build attempt failed, I examined the error logs carefully:
    
    ```plaintext
    ./pass-instances.def:47:23: error: expected ';' at end of member declaration
    47 | NEXT_PASS_WITH_ARG (pass_mypass, 1, 1)
    ```
    
    This suggested an issue with my pass registration syntax.
    
2. **Investigating Pass Declaration Conventions**: I looked at existing passes in the GCC source code by running:
    
    ```plaintext
    cd ~/git/gcc/gcc
    grep -r "NEXT_PASS" passes.def
    ```
    
    This revealed that the correct format was `NEXT_PASS (pass_name, number)` rather than `NEXT_PASS_WITH_ARG`.
    
3. **Function Naming Pattern Analysis**: I investigated the naming conventions by examining other pass implementations:
    
    ```plaintext
    cd ~/git/gcc/gcc
    find . -name "tree-*.cc" | xargs grep "make_pass_"
    ```
    
    This showed me that GCC expects factory functions to follow the pattern `make_pass_X` where X is the pass name without the "pass\_" prefix.
    
4. **Makefile Formatting Investigation**: To understand the Makefile formatting issue, I examined the original and my modified version:
    
    ```plaintext
    cd ~/git/gcc/gcc
    grep -A 10 -B 10 "OBJS" Makefile.in > original_makefile_section.txt
    # After comparing my changes with the original format
    ```
    
    This revealed that I needed to maintain the exact same indentation (using tabs) and add a backslash at the end of my object file entry.
    
5. **Rebuilding with Verbose Output**: I attempted to rebuild with more verbose output to see exactly where the build was failing:
    
    ```plaintext
    cd ~/gcc-build-001
    make V=1 -j 4 |& tee verbose_build.log
    ```
    
    Provided more detailed error information: issue was with the pass registration syntax.
    
6. **Isolating Components**: To narrow down the issue, I tried building just the compiler component:
    
    ```plaintext
    cd ~/gcc-build-001/gcc
    make -j 1 |& tee gcc_build.log
    ```
    
    This made the error messages easier to identify and address.
    
7. **Comparing with Reference Implementations**: I studied successful GCC pass implementations:
    
    ```plaintext
    cd ~/git/gcc/gcc
    # Example: Looking at tree-ssa-pre.cc as a reference
    less tree-ssa-pre.cc
    ```
    
    Helped me in understanding the correct structure and naming conventions for GCC passes.
    
8. **Version-Specific Documentation**: Just to be sure: checked if there were version-specific requirements for the GCC version I was working with:
    
    ```plaintext
    cd ~/git/gcc
    git log -1 --pretty=format:"%h %s" # To identify the specific GCC version
    ```
    

### Specific Troubleshooting

1. **Naming Convention Inconsistencies**: My initial implementation used inconsistent naming:
    
    ```plaintext
    // In tree-my-pass.cc
    gimple_opt_pass *make_tree_my_pass(gcc::context *ctxt)
    
    // In tree-pass.h declaration
    extern gimple_opt_pass *make_tree_my_pass(gcc::context *ctxt);
    
    // In passes.def
    NEXT_PASS (pass_my_pass, 1)
    ```
    
    I later discovered GCC requires a specific pattern:
    
    * Factory function must be named `make_pass_X`
        
    * Pass registration must use `pass_X`
        
    * These names must be consistent across all files
        
2. **Makefile Syntax Errors**: The error message `missing separator (did you mean TAB instead of 8 spaces?)`occurred because:
    
    ```plaintext
    # I initially added:
    tree_my_pass.o \  # With spaces instead of tabs
    
    # Should have been:
    [TAB]tree-my-pass.o \  # With a tab character for indentation
    ```
    
    I confirmed this by examining the exact whitespace characters in the Makefile:
    
    ```plaintext
    cat -A Makefile.in | grep -A 3 -B 3 "tree-my-pass"
    ```
    
    This showed spaces (represented as `^I`) instead of tabs where required.
    
3. **Pass Registration Syntax Error**: When I tried using `NEXT_PASS_WITH_ARG`, I encountered compilation errors because:
    
    ```plaintext
    # I tried:
    NEXT_PASS_WITH_ARG (pass_my_pass, 1, 1)
    NEXT_PASS_WITH_ARG (pass_my_pass, 0)
    
    # But examining other passes showed I should use:
    NEXT_PASS (pass_my_pass, 1)
    ```
    
    I discovered through source investigation that `NEXT_PASS_WITH_ARG` is used differently in the GCC codebase than I initially understood.
    
4. **Header Inclusion Order**: I found that the order of header inclusions could affect compilation:
    
    ```plaintext
    // Original order
    #include "tree.h"
    #include "gimple.h"
    #include "tree-pass.h"
    
    // After examining compiler warnings, I tried:
    #include "tree-pass.h"
    #include "tree.h"
    #include "gimple.h"
    ```
