Files
carbon-lang/explorer/interpreter/exec_program.cpp
T
Prabhat SachdevaandRichard Smith 04d9a7b533 Improved trace output selection using program phase filtering (#2851)
By implementing these improvements, users will have the ability to choose specific parts of the trace output.

Currently, when executing a file using the explorer with the --trace_file=- or --trace_file=filename.txt flag, the resulting output is an extensive and verbose log containing all the information.

In this PR, I have introduced the `ProgramPhase` enum class, which have distinct phases encountered during the compilation of a program in the explorer. Each member of this enum class corresponds to a specific phase, signifying the relevant information to be included in the trace output.

The phases covered by the `ProgramPhase` enum class are as follows:

1. Printing the source program
2. Name resolution
3. Control flow resolution
4. Type checking
5. Unformed variable resolution
6. Printing declarations
7. Printing the timings
8. Printing whole output.

These phases can be selected by passing the following compiler flags along with `--trace_file=-`.
`-trace_source_program`, `-trace_name_resolution`, `-trace_control_flow_resolution`, `-trace_type_checking`, `-trace_unformed_variables_resolution`, `-trace_declarations`, `-trace_execution`, `-trace_timing` and `-trace_all`. If none of these flags is passed only execution trace will be added to the output.

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2023-06-08 10:49:13 -07:00

96 lines
3.5 KiB
C++

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include "explorer/interpreter/exec_program.h"
#include <variant>
#include "common/check.h"
#include "common/error.h"
#include "common/ostream.h"
#include "explorer/common/arena.h"
#include "explorer/common/trace_stream.h"
#include "explorer/interpreter/interpreter.h"
#include "explorer/interpreter/resolve_control_flow.h"
#include "explorer/interpreter/resolve_names.h"
#include "explorer/interpreter/resolve_unformed.h"
#include "explorer/interpreter/type_checker.h"
#include "llvm/Support/Error.h"
namespace Carbon {
auto AnalyzeProgram(Nonnull<Arena*> arena, AST ast,
Nonnull<TraceStream*> trace_stream,
Nonnull<llvm::raw_ostream*> print_stream) -> ErrorOr<AST> {
trace_stream->set_current_phase(ProgramPhase::SourceProgram);
if (trace_stream->is_enabled()) {
*trace_stream << "********** source program **********\n";
for (int i = ast.num_prelude_declarations;
i < static_cast<int>(ast.declarations.size()); ++i) {
*trace_stream << *ast.declarations[i];
}
}
SourceLocation source_loc("<Main()>", 0);
ast.main_call = arena->New<CallExpression>(
source_loc, arena->New<IdentifierExpression>(source_loc, "Main"),
arena->New<TupleLiteral>(source_loc));
// Although name resolution is currently done once, generic programming
// (particularly templates) may require more passes.
trace_stream->set_current_phase(ProgramPhase::NameResolution);
if (trace_stream->is_enabled()) {
*trace_stream << "********** resolving names **********\n";
}
CARBON_RETURN_IF_ERROR(ResolveNames(ast));
trace_stream->set_current_phase(ProgramPhase::ControlFlowResolution);
if (trace_stream->is_enabled()) {
*trace_stream << "********** resolving control flow **********\n";
}
CARBON_RETURN_IF_ERROR(ResolveControlFlow(ast));
trace_stream->set_current_phase(ProgramPhase::TypeChecking);
if (trace_stream->is_enabled()) {
*trace_stream << "********** type checking **********\n";
}
CARBON_RETURN_IF_ERROR(
TypeChecker(arena, trace_stream, print_stream).TypeCheck(ast));
trace_stream->set_current_phase(ProgramPhase::UnformedVariableResolution);
if (trace_stream->is_enabled()) {
*trace_stream << "********** resolving unformed variables **********\n";
}
CARBON_RETURN_IF_ERROR(ResolveUnformed(ast));
trace_stream->set_current_phase(ProgramPhase::Declarations);
if (trace_stream->is_enabled()) {
*trace_stream << "********** printing declarations **********\n";
for (int i = ast.num_prelude_declarations;
i < static_cast<int>(ast.declarations.size()); ++i) {
*trace_stream << *ast.declarations[i];
}
}
trace_stream->set_current_phase(ProgramPhase::Unknown);
return ast;
}
auto ExecProgram(Nonnull<Arena*> arena, AST ast,
Nonnull<TraceStream*> trace_stream,
Nonnull<llvm::raw_ostream*> print_stream) -> ErrorOr<int> {
trace_stream->set_current_phase(ProgramPhase::Execution);
if (trace_stream->is_enabled()) {
*trace_stream << "********** starting execution **********\n";
}
CARBON_ASSIGN_OR_RETURN(
auto interpreter_result,
InterpProgram(ast, arena, trace_stream, print_stream));
if (trace_stream->is_enabled()) {
*trace_stream << "interpreter result: " << interpreter_result << "\n";
}
trace_stream->set_current_phase(ProgramPhase::Unknown);
return interpreter_result;
}
} // namespace Carbon