Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,24 @@ DEBUG_TARGET = opt6502-debug
PROFILE_TARGET = opt6502-profile

# Source files
SOURCES = opt6502.c
SOURCES = src/main.c \
src/types.c \
src/ast/ast.c \
src/ast/parser.c \
src/analysis/analysis.c \
src/analysis/registers.c \
src/optimizations/optimizer.c \
src/optimizations/peephole.c \
src/optimizations/deadcode.c \
src/optimizations/jumps.c \
src/optimizations/loadstore.c \
src/optimizations/regusage.c \
src/optimizations/constant.c \
src/optimizations/cpu65c02.c \
src/optimizations/cpu45gs02.c \
src/optimizations/inline.c \
src/output/output.c \
src/program/program.c
OBJECTS = $(SOURCES:.c=.o)

# Installation directory (can be overridden)
Expand Down Expand Up @@ -94,6 +111,7 @@ clean:
rm -f $(TARGET) $(DEBUG_TARGET) $(PROFILE_TARGET)
rm -f $(OBJECTS)
rm -f *.o
find src -name "*.o" -type f -delete 2>/dev/null || true
rm -f gmon.out
rm -f test_output.asm
@echo "Clean complete."
Expand Down Expand Up @@ -137,7 +155,7 @@ test: $(TARGET)
dist: clean
@echo "Creating distribution tarball..."
@mkdir -p opt6502-1.0
@cp opt6502.c opt6502-1.0/
@cp -r src opt6502-1.0/
@cp Makefile opt6502-1.0/
@if [ -f README.md ]; then cp README.md opt6502-1.0/; fi
@if [ -f LICENSE ]; then cp LICENSE opt6502-1.0/; fi
Expand Down
46 changes: 46 additions & 0 deletions src/analysis/analysis.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* @file analysis.c
* @brief Code flow and branch target analysis implementation
*
* Implements control flow analysis functions for identifying branch
* targets and analyzing program structure.
*/

#include "analysis.h"

/**
* @brief Mark all branch target labels in the AST
*
* Iterates through the AST and marks all label nodes as branch targets.
* Labels are potential targets for jumps, branches, and subroutine calls,
* so they must be preserved during optimization.
*
* @param prog Program to analyze
*/
void mark_branch_targets_ast(Program *prog) {
AstNode *node = prog->root;
while (node) {
if (node->type == NODE_LABEL) {
node->is_branch_target = true;
}
node = node->next;
}
}

/**
* @brief Analyze call flow and control flow patterns
*
* Wrapper function that performs all necessary control flow analysis.
* Currently calls mark_branch_targets_ast() to identify all labels
* that can be branched to.
*
* Future enhancements could include:
* - Dead code detection based on reachability
* - Loop detection
* - Subroutine call graph construction
*
* @param prog Program to analyze
*/
void analyze_call_flow_ast(Program *prog) {
mark_branch_targets_ast(prog);
}
36 changes: 36 additions & 0 deletions src/analysis/analysis.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* @file analysis.h
* @brief Code flow and branch target analysis
*
* Provides analysis functions for identifying branch targets and
* analyzing control flow in the program AST.
*/

#ifndef ANALYSIS_H
#define ANALYSIS_H

#include "../types.h"

/**
* @brief Mark all branch target labels in the AST
*
* Scans the AST and marks all label nodes that can be jumped or
* branched to. This information is used by optimizers to preserve
* labels that are referenced by control flow instructions.
*
* @param prog Program to analyze
*/
void mark_branch_targets_ast(Program *prog);

/**
* @brief Analyze call flow and control flow patterns
*
* Performs control flow analysis including branch target identification.
* This is typically called before optimization passes to ensure that
* control flow is properly understood.
*
* @param prog Program to analyze
*/
void analyze_call_flow_ast(Program *prog);

#endif // ANALYSIS_H
Loading
Loading