Skip to content

Commit

Permalink
feat: Literal support (#207)
Browse files Browse the repository at this point in the history
(closes #193, closes #75)
  • Loading branch information
slavek-kucera authored Dec 10, 2021
1 parent b3dac83 commit 44a93f2
Show file tree
Hide file tree
Showing 80 changed files with 3,093 additions and 831 deletions.
2 changes: 1 addition & 1 deletion language_server/test/telemetry_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ TEST(telemetry, telemetry_broker)

//"textDocument/hover",R"#({"textDocument":{"uri":"file:///c%3A/test/stability.hlasm"},"position":{"line":0,"character":7}})#"_json),

std::thread lsp_thread([&broker, &lsp_server]() {
std::thread lsp_thread([&lsp_server]() {
lsp_server.message_received(
R"({"jsonrpc":"2.0","id":48,"method":"textDocument/hover","params":{"textDocument":{"uri":"file:///test_file"},"position":{"line":0,"character":2} }})"_json);
});
Expand Down
2 changes: 2 additions & 0 deletions parser_library/src/context/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ target_sources(parser_library PRIVATE
instruction.cpp
instruction.h
instruction_type.h
literal_pool.cpp
literal_pool.h
macro.cpp
macro.h
macro_param_data.cpp
Expand Down
4 changes: 3 additions & 1 deletion parser_library/src/context/hlasm_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,8 @@ hlasm_context::hlasm_context(std::string file_name, asm_option asm_options, std:
add_global_system_vars();
}

hlasm_context::~hlasm_context() = default;

void hlasm_context::set_source_position(position pos) { source_stack_.back().current_instruction.pos = pos; }

void hlasm_context::set_source_indices(size_t begin_index, size_t end_index, size_t end_line)
Expand Down Expand Up @@ -647,7 +649,7 @@ 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);
value = expressions::ca_symbol_attribute::try_extract_leading_symbol(value);

auto res = expressions::ca_constant::try_self_defining_term(value);
if (res)
Expand Down
3 changes: 2 additions & 1 deletion parser_library/src/context/hlasm_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
#include "ordinary_assembly/ordinary_assembly_context.h"
#include "processing_context.h"


namespace hlasm_plugin::parser_library::context {

// class helping to perform semantic analysis of hlasm source code
Expand Down Expand Up @@ -83,6 +82,7 @@ class hlasm_context
// last AINSERT virtual file id
size_t m_ainsert_id = 0;
bool m_end_reached = false;

void add_system_vars_to_scope();
void add_global_system_vars();

Expand All @@ -92,6 +92,7 @@ class hlasm_context
hlasm_context(std::string file_name = "",
asm_option asm_opts = {},
std::shared_ptr<id_storage> init_ids = std::make_shared<id_storage>());
~hlasm_context();

// gets name of file where is open-code located
const std::string& opencode_file_name() const;
Expand Down
2 changes: 1 addition & 1 deletion parser_library/src/context/id_storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ class id_storage
const std::string* AGO;
const std::string* ACTR;
const std::string* AREAD;
const std::string* END;
const std::string* ALIAS;
const std::string* END;
well_known_strings(std::unordered_set<std::string>& ptr);

} const well_known;
Expand Down
54 changes: 53 additions & 1 deletion parser_library/src/context/instruction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "instruction.h"

#include <algorithm>
#include <limits>

using namespace hlasm_plugin::parser_library::context;
using namespace hlasm_plugin::parser_library::checking;
Expand Down Expand Up @@ -179,7 +180,6 @@ const std::map<std::string, assembler_instruction> instruction::assembler_instru
{ "XATTR", assembler_instruction(1, -1, false, "attribute+") },
};


bool hlasm_plugin::parser_library::context::machine_instruction::check_nth_operand(
size_t place, const checking::machine_operand* operand)
{
Expand Down Expand Up @@ -2670,3 +2670,55 @@ const std::map<std::string, machine_instruction> instruction::machine_instructio

const std::map<std::string, mnemonic_code> instruction::mnemonic_codes =
instruction::get_mnemonic_codes(machine_instructions);

// Generates a bitmask for an arbitrary machine instruction indicating which operands
// are of the RI type (and therefore are modified by transform_reloc_imm_operands)
unsigned char machine_instruction::generate_reladdr_bitmask(
const std::vector<checking::machine_operand_format>& operands)
{
unsigned char result = 0;

assert(operands.size() <= std::numeric_limits<decltype(result)>::digits);

decltype(result) top_bit = 1 << (std::numeric_limits<decltype(result)>::digits - 1);

for (const auto& op : operands)
{
if (op.identifier.type == checking::machine_operand_type::RELOC_IMM)
result |= top_bit;
top_bit >>= 1;
}

return result;
}

// Generates a bitmask for an arbitrary mnemonit indicating which operands
// are of the RI type (and therefore are modified by transform_reloc_imm_operands)
unsigned char mnemonic_code::generate_reladdr_bitmask(
const machine_instruction* instruction, const std::vector<std::pair<size_t, size_t>>& replaced)
{
unsigned char result = 0;

decltype(result) top_bit = 1 << (std::numeric_limits<decltype(result)>::digits - 1);

const std::pair<size_t, size_t>* replaced_b = replaced.data();
const std::pair<size_t, size_t>* const replaced_e = replaced.data() + replaced.size();

size_t position = 0;
for (const auto& op : instruction->operands)
{
if (replaced_b != replaced_e && position == replaced_b->first)
{
++replaced_b;
++position;
continue;
}

if (op.identifier.type == checking::machine_operand_type::RELOC_IMM)
result |= top_bit;
top_bit >>= 1;

++position;
}
return result;
}
32 changes: 29 additions & 3 deletions parser_library/src/context/instruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,28 @@ const checking::machine_operand_format rel_addr_imm_24_S =
const checking::machine_operand_format rel_addr_imm_32_S =
checking::machine_operand_format(reladdr_imm_32s, empty, empty);

class reladdr_transform_mask
{
unsigned char m_mask;

public:
reladdr_transform_mask(unsigned char m)
: m_mask(m)
{}
unsigned char mask() const { return m_mask; }

friend bool operator==(const reladdr_transform_mask& l, const reladdr_transform_mask& r)
{
return l.m_mask == r.m_mask;
}
friend bool operator!=(const reladdr_transform_mask& l, const reladdr_transform_mask& r) { return !(l == r); }
};

// machine instruction representation for checking
class machine_instruction
{
static unsigned char generate_reladdr_bitmask(const std::vector<checking::machine_operand_format>& operands);

public:
std::string instr_name;
mach_format format;
Expand All @@ -190,18 +208,20 @@ class machine_instruction
int no_optional;
size_t page_no;

reladdr_transform_mask reladdr_mask;

machine_instruction(const std::string& name,
mach_format format,
std::vector<checking::machine_operand_format> operands,
int no_optional,

size_t page_no)
: instr_name(name)
, format(format)
, operands(operands)
, size_for_alloc(get_length_by_format(format))
, no_optional(no_optional)
, page_no(page_no) {};
, page_no(page_no)
, reladdr_mask(generate_reladdr_bitmask(operands)) {};
machine_instruction(const std::string& name,
mach_format format,
std::vector<checking::machine_operand_format> operands,
Expand Down Expand Up @@ -237,16 +257,22 @@ struct ca_instruction
// representation of mnemonic codes for machine instructions
struct mnemonic_code
{
static unsigned char generate_reladdr_bitmask(
const machine_instruction* instruction, const std::vector<std::pair<size_t, size_t>>& replaced);

public:
mnemonic_code(const machine_instruction* instr, std::vector<std::pair<size_t, size_t>> replaced)
: instruction(instr)
, replaced(replaced) {};
, replaced(replaced)
, reladdr_mask(generate_reladdr_bitmask(instr, replaced)) {};

const machine_instruction* instruction;

// first goes place, then value
std::vector<std::pair<size_t, size_t>> replaced;

reladdr_transform_mask reladdr_mask;

size_t operand_count() const { return instruction->operands.size() + instruction->no_optional - replaced.size(); }
};

Expand Down
Loading

0 comments on commit 44a93f2

Please sign in to comment.