Skip to content

Commit

Permalink
refactor: Refactor of attribute lookahead processing (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
asmelko authored Sep 15, 2020
1 parent 027b9f9 commit ce8e59d
Show file tree
Hide file tree
Showing 88 changed files with 1,481 additions and 984 deletions.
2 changes: 1 addition & 1 deletion parser_library/src/analyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ analyzer::analyzer(const std::string& text,
*parser_,
tracer)
{
parser_->initialize(&hlasm_ctx_ref_, &lsp_proc_);
parser_->initialize(&hlasm_ctx_ref_, &lsp_proc_, &lib_provider, &mngr_);
parser_->setErrorHandler(std::make_shared<error_strategy>());
parser_->removeErrorListeners();
parser_->addErrorListener(&listener_);
Expand Down
62 changes: 57 additions & 5 deletions parser_library/src/context/hlasm_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include "ebcdic_encoding.h"
#include "expressions/conditional_assembly/terms/ca_constant.h"
#include "expressions/conditional_assembly/terms/ca_symbol_attribute.h"
#include "instruction.h"

namespace hlasm_plugin::parser_library::context {
Expand Down Expand Up @@ -262,6 +263,45 @@ void hlasm_context::set_source_indices(size_t begin_index, size_t end_index, siz
source_stack_.back().end_line = end_line;
}

std::pair<source_position, source_snapshot> hlasm_context::get_begin_snapshot(bool ignore_macros) const
{
context::source_position statement_position;

bool is_in_macros = ignore_macros ? false : scope_stack_.size() > 1;

if (!is_in_macros && current_copy_stack().empty())
{
statement_position.file_offset = current_source().begin_index;
statement_position.file_line = current_source().current_instruction.pos.line;
}
else
{
statement_position.file_offset = current_source().end_index;
statement_position.file_line = current_source().end_line + 1;
}

context::source_snapshot snapshot = current_source().create_snapshot();

if (snapshot.copy_frames.size() && is_in_macros)
++snapshot.copy_frames.back().statement_offset;

return std::make_pair(std::move(statement_position), std::move(snapshot));
}

std::pair<source_position, source_snapshot> hlasm_context::get_end_snapshot() const
{
context::source_position statement_position;
statement_position.file_offset = current_source().end_index;
statement_position.file_line = current_source().end_line + 1;

context::source_snapshot snapshot = current_source().create_snapshot();

if (snapshot.copy_frames.size())
++snapshot.copy_frames.back().statement_offset;

return std::make_pair(std::move(statement_position), std::move(snapshot));
}

void hlasm_context::push_statement_processing(const processing::processing_kind kind)
{
assert(!proc_stack_.empty());
Expand Down Expand Up @@ -322,6 +362,11 @@ const std::deque<code_scope>& hlasm_context::scope_stack() const { return scope_

const source_context& hlasm_context::current_source() const { return source_stack_.back(); }

const std::vector<copy_member_invocation>& hlasm_context::current_copy_stack() const
{
return source_stack_.back().copy_stack;
}

std::vector<copy_member_invocation>& hlasm_context::current_copy_stack() { return source_stack_.back().copy_stack; }

std::vector<id_index> hlasm_context::whole_copy_stack() const
Expand Down Expand Up @@ -366,11 +411,8 @@ var_sym_ptr hlasm_context::get_var_sym(id_index name)

void hlasm_context::add_sequence_symbol(sequence_symbol_ptr seq_sym)
{
if (curr_scope()->is_in_macro())
throw std::runtime_error("adding sequence symbols to macro definition not allowed");

if (curr_scope()->sequence_symbols.find(seq_sym->name) == curr_scope()->sequence_symbols.end())
curr_scope()->sequence_symbols.emplace(seq_sym->name, std::move(seq_sym));
auto& opencode = scope_stack_.front();
opencode.sequence_symbols.try_emplace(seq_sym->name, std::move(seq_sym));
}

const sequence_symbol* hlasm_context::get_sequence_symbol(id_index name) const
Expand All @@ -394,6 +436,14 @@ const sequence_symbol* hlasm_context::get_sequence_symbol(id_index name) const
return nullptr;
}

const sequence_symbol* hlasm_context::get_opencode_sequence_symbol(id_index name) const
{
if (const auto& sym = scope_stack_.front().sequence_symbols.find(name);
sym != scope_stack_.front().sequence_symbols.end())
return sym->second.get();
return nullptr;
}

void hlasm_context::set_branch_counter(A_t value)
{
curr_scope()->branch_counter = value;
Expand Down Expand Up @@ -541,6 +591,8 @@ C_t hlasm_context::get_type_attr(var_sym_ptr var_symbol, const std::vector<size_
if (value.empty())
return "O";

expressions::ca_symbol_attribute::try_extract_leading_symbol(value);

auto res = expressions::ca_constant::try_self_defining_term(value);
if (res)
return "N";
Expand Down
9 changes: 8 additions & 1 deletion parser_library/src/context/hlasm_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ class hlasm_context
// sets current source file indices
void set_source_indices(size_t begin_index, size_t end_index, size_t end_line);

std::pair<source_position, source_snapshot> get_begin_snapshot(bool ignore_macros) const;
std::pair<source_position, source_snapshot> get_end_snapshot() const;

// pushes new kind of statement processing
void push_statement_processing(const processing::processing_kind kind);
// pushes new kind of statement processing as well as new source
Expand All @@ -107,6 +110,7 @@ class hlasm_context
// gets macro nest
const std::deque<code_scope>& scope_stack() const;
// gets copy nest of current statement processing
const std::vector<copy_member_invocation>& current_copy_stack() const;
std::vector<copy_member_invocation>& current_copy_stack();
// gets names of whole copy nest
std::vector<id_index> whole_copy_stack() const;
Expand Down Expand Up @@ -139,6 +143,9 @@ class hlasm_context
// return sequence symbol in current scope
// returns nullptr if there is none in the current scope
const sequence_symbol* get_sequence_symbol(id_index name) const;
// return opencode sequence symbol
// returns nullptr if there is none
const sequence_symbol* get_opencode_sequence_symbol(id_index name) const;

void set_branch_counter(A_t value);
A_t get_branch_counter() const;
Expand Down Expand Up @@ -205,7 +212,7 @@ class hlasm_context
return glob->second;
}

set_sym_ptr val(std::make_shared<set_symbol<T>>(id, is_scalar, true));
auto val = std::make_shared<set_symbol<T>>(id, is_scalar, true);

globals_.insert({ id, val });
curr_scope()->variables.insert({ id, val });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@

#include "alignment.h"

using namespace hlasm_plugin::parser_library;
using namespace hlasm_plugin::parser_library::context;
namespace hlasm_plugin::parser_library::context {

void ordinary_assembly_context::create_private_section()
{
Expand Down Expand Up @@ -55,6 +54,15 @@ bool ordinary_assembly_context::create_symbol(
return ok;
}

void ordinary_assembly_context::add_symbol_reference(symbol sym) { symbol_refs_.try_emplace(sym.name, std::move(sym)); }

const symbol* ordinary_assembly_context::get_symbol_reference(context::id_index name) const
{
auto tmp = symbol_refs_.find(name);

return tmp == symbol_refs_.end() ? nullptr : &tmp->second;
}

const symbol* ordinary_assembly_context::get_symbol(id_index name) const
{
auto tmp = symbols_.find(name);
Expand Down Expand Up @@ -255,4 +263,6 @@ std::pair<address, space_ptr> ordinary_assembly_context::reserve_storage_area_sp
return std::make_pair(ret_addr, sp);
}
return std::make_pair(curr_section_->current_location_counter().reserve_storage_area(length, align).first, nullptr);
}
}

} // namespace hlasm_plugin::parser_library::context
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@
#include "symbol_dependency_tables.h"


namespace hlasm_plugin {
namespace parser_library {
namespace context {
namespace hlasm_plugin::parser_library::context {

// class holding complete information about the 'ordinary assembly' (assembler and machine instructions)
// it contains 'sections' ordinary 'symbols' and all dependencies between them
Expand All @@ -37,6 +35,8 @@ class ordinary_assembly_context : public dependency_solver
std::vector<std::unique_ptr<section>> sections_;
// list of visited symbols
std::unordered_map<id_index, symbol> symbols_;
// list of lookaheaded symbols
std::unordered_map<id_index, symbol> symbol_refs_;

section* curr_section_;

Expand All @@ -57,6 +57,9 @@ class ordinary_assembly_context : public dependency_solver
[[nodiscard]] bool create_symbol(
id_index name, symbol_value value, symbol_attributes attributes, location symbol_location);

void add_symbol_reference(symbol sym);
const symbol* get_symbol_reference(context::id_index name) const;

// gets symbol by name
virtual const symbol* get_symbol(id_index name) const override;
symbol* get_symbol(id_index name);
Expand Down Expand Up @@ -111,7 +114,6 @@ class ordinary_assembly_context : public dependency_solver
std::pair<address, space_ptr> reserve_storage_area_space(size_t length, alignment align);
};

} // namespace context
} // namespace parser_library
} // namespace hlasm_plugin
} // namespace hlasm_plugin::parser_library::context

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ enum class symbol_origin
SECT,
MACH,
EQU,
DAT
DAT,
UNKNOWN
};

// structure wrapping attributes of the symbol
Expand Down
27 changes: 21 additions & 6 deletions parser_library/src/context/source_snapshot.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@
#include "id_storage.h"
#include "range.h"

namespace hlasm_plugin {
namespace parser_library {
namespace context {
namespace hlasm_plugin::parser_library::context {

// helper structure representing position in source file
struct source_position
Expand Down Expand Up @@ -69,6 +67,24 @@ struct source_snapshot
size_t end_line;
std::vector<copy_frame> copy_frames;

source_snapshot()
: begin_index(0)
, end_index(0)
, end_line(0)
{}

source_snapshot(location instruction,
size_t begin_index,
size_t end_index,
size_t end_line,
std::vector<copy_frame> copy_frames)
: instruction(std::move(instruction))
, begin_index(begin_index)
, end_index(end_index)
, end_line(end_line)
, copy_frames(std::move(copy_frames))
{}

bool operator==(const source_snapshot& oth) const
{
if (!(end_line == oth.end_line && begin_index == oth.begin_index && end_index == oth.end_index
Expand All @@ -83,7 +99,6 @@ struct source_snapshot
}
};

} // namespace context
} // namespace parser_library
} // namespace hlasm_plugin
} // namespace hlasm_plugin::parser_library::context

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class ca_expression : public diagnosable_op_impl
ca_expression(context::SET_t_enum expr_kind, range expr_range);

// retrieves set of attributed symbols that are not yet defined
virtual undef_sym_set get_undefined_attributed_symbols(const context::dependency_solver& solver) const = 0;
virtual undef_sym_set get_undefined_attributed_symbols(const evaluation_context& eval_ctx) const = 0;

// builds parts of the expression tree that could not be built during parsing
virtual void resolve_expression_tree(context::SET_t_enum kind) = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ ca_binary_operator::ca_binary_operator(
, right_expr(std::move(right_expr))
{}

undef_sym_set ca_binary_operator::get_undefined_attributed_symbols(const context::dependency_solver& solver) const
undef_sym_set ca_binary_operator::get_undefined_attributed_symbols(const evaluation_context& eval_ctx) const
{
auto tmp = left_expr->get_undefined_attributed_symbols(solver);
tmp.merge(right_expr->get_undefined_attributed_symbols(solver));
auto tmp = left_expr->get_undefined_attributed_symbols(eval_ctx);
tmp.merge(right_expr->get_undefined_attributed_symbols(eval_ctx));
return tmp;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class ca_binary_operator : public ca_expression

ca_binary_operator(ca_expr_ptr left_expr, ca_expr_ptr right_expr, context::SET_t_enum expr_kind, range expr_range);

virtual undef_sym_set get_undefined_attributed_symbols(const context::dependency_solver& solver) const override;
virtual undef_sym_set get_undefined_attributed_symbols(const evaluation_context& eval_ctx) const override;

virtual void resolve_expression_tree(context::SET_t_enum kind) override;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ ca_unary_operator::ca_unary_operator(ca_expr_ptr expr, context::SET_t_enum expr_
, expr(std::move(expr))
{}

undef_sym_set ca_unary_operator::get_undefined_attributed_symbols(const context::dependency_solver& solver) const
undef_sym_set ca_unary_operator::get_undefined_attributed_symbols(const evaluation_context& eval_ctx) const
{
return expr->get_undefined_attributed_symbols(solver);
return expr->get_undefined_attributed_symbols(eval_ctx);
}

void ca_unary_operator::resolve_expression_tree(context::SET_t_enum kind)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class ca_unary_operator : public ca_expression

ca_unary_operator(ca_expr_ptr expr, context::SET_t_enum expr_kind, range expr_range);

virtual undef_sym_set get_undefined_attributed_symbols(const context::dependency_solver& solver) const override;
virtual undef_sym_set get_undefined_attributed_symbols(const evaluation_context& eval_ctx) const override;

virtual void resolve_expression_tree(context::SET_t_enum kind) override;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ ca_constant::ca_constant(context::A_t value, range expr_range)
, value(value)
{}

undef_sym_set ca_constant::get_undefined_attributed_symbols(const context::dependency_solver&) const
{
return undef_sym_set();
}
undef_sym_set ca_constant::get_undefined_attributed_symbols(const evaluation_context&) const { return undef_sym_set(); }

void ca_constant::resolve_expression_tree(context::SET_t_enum kind)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class ca_constant : public ca_expression

ca_constant(context::A_t value, range expr_range);

virtual undef_sym_set get_undefined_attributed_symbols(const context::dependency_solver& solver) const override;
virtual undef_sym_set get_undefined_attributed_symbols(const evaluation_context& eval_ctx) const override;

virtual void resolve_expression_tree(context::SET_t_enum kind) override;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ ca_expr_list::ca_expr_list(std::vector<ca_expr_ptr> expr_list, range expr_range)
, expr_list(std::move(expr_list))
{}

undef_sym_set ca_expr_list::get_undefined_attributed_symbols(const context::dependency_solver& solver) const
undef_sym_set ca_expr_list::get_undefined_attributed_symbols(const evaluation_context& eval_ctx) const
{
undef_sym_set tmp;
for (auto&& expr : expr_list)
tmp.merge(expr->get_undefined_attributed_symbols(solver));
tmp.merge(expr->get_undefined_attributed_symbols(eval_ctx));
return tmp;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class ca_expr_list : public ca_expression

ca_expr_list(std::vector<ca_expr_ptr> expr_list, range expr_range);

virtual undef_sym_set get_undefined_attributed_symbols(const context::dependency_solver& solver) const override;
virtual undef_sym_set get_undefined_attributed_symbols(const evaluation_context& eval_ctx) const override;

virtual void resolve_expression_tree(context::SET_t_enum kind) override;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ ca_function::ca_function(context::id_index function_name,
, duplication_factor(std::move(duplication_factor))
{}

undef_sym_set ca_function::get_undefined_attributed_symbols(const context::dependency_solver& solver) const
undef_sym_set ca_function::get_undefined_attributed_symbols(const evaluation_context& eval_ctx) const
{
undef_sym_set ret;
for (auto&& expr : parameters)
ret.merge(expr->get_undefined_attributed_symbols(solver));
ret.merge(expr->get_undefined_attributed_symbols(eval_ctx));
if (duplication_factor)
ret.merge(duplication_factor->get_undefined_attributed_symbols(solver));
ret.merge(duplication_factor->get_undefined_attributed_symbols(eval_ctx));
return ret;
}

Expand Down
Loading

0 comments on commit ce8e59d

Please sign in to comment.