• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

benvenutti / hasm / 10864857882

14 Sep 2024 07:09PM UTC coverage: 87.654% (+0.1%) from 87.538%
10864857882

push

github

web-flow
Merge pull request #77 from benvenutti/feature/fix-coverity-scan-issues

Feature/fix coverity scan issues

1 of 8 new or added lines in 2 files covered. (12.5%)

568 of 648 relevant lines covered (87.65%)

35.25 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.74
/hasm/src/hasm/Assembler.cpp
1
#include <hasm/Assembler.hpp>
2

3
#include <hasm/Coder.hpp>
4
#include <hasm/ErrorMessage.hpp>
5

6
#include <bitset>
7
#include <cctype>
8
#include <iomanip>
9
#include <stdexcept>
10

11
namespace Hasm
12
{
13

14
Assembler::Assembler( std::istream& in, std::ostream& out, const Logger& logger )
2✔
15
: m_out( out )
2✔
16
, m_parser( in )
2✔
17
, m_logger( logger )
4✔
18
{
19
    if ( !m_logger )
2✔
20
    {
21
        throw std::logic_error( "invalid logger" );
×
22
    }
23
}
2✔
24

25
bool Assembler::assemble()
2✔
26
{
27
    return firstPass() && m_parser.reset() && secondPass();
2✔
28
}
29

30
const SymbolTable& Assembler::getSymbolTable() const
1✔
31
{
32
    return m_symbolTable;
1✔
33
}
34

35
bool Assembler::firstPass()
2✔
36
{
37
    Hack::word lineCounter{ 0_w };
38

39
    while ( m_parser.advance() )
35✔
40
    {
41
        if ( m_parser.getInstructionType() == Hack::InstructionType::label )
33✔
42
        {
43
            m_symbolTable.addEntry( m_parser.symbol(), lineCounter );
4✔
44
        }
45
        else
46
        {
47
            lineCounter++;
31✔
48
        }
49
    }
50

51
    if ( m_parser.getStatus() == Parser::Status::invalid_instruction )
2✔
52
    {
53
        m_logger( ErrorMessage::invalidInstruction( m_parser.getInstruction(), m_parser.getCurrentLineNumber() ) );
×
54
    }
55

56
    return m_parser.getStatus() == Parser::Status::end_of_file;
2✔
57
}
58

59
bool Assembler::secondPass()
2✔
60
{
61
    bool ok{ true };
62

63
    while ( ok && m_parser.advance() )
35✔
64
    {
65
        if ( const auto commandType = m_parser.getInstructionType() )
33✔
66
        {
67
            ok = assembleInstruction( commandType.value() );
33✔
68
        }
69
        else
70
        {
71
            ok = false;
72
        }
73
    }
74

75
    return ok;
2✔
76
}
77

78
bool Assembler::assembleInstruction( const Hack::InstructionType instructionType )
33✔
79
{
80
    switch ( instructionType )
33✔
81
    {
82
        case Hack::InstructionType::addressing:
15✔
83
            return assembleAddressingInstruction();
15✔
84
        case Hack::InstructionType::computation:
16✔
85
            return assembleComputationInstruction();
16✔
86
        default:
87
            return true;
88
    }
89
}
90

91
bool Assembler::assembleAddressingInstruction()
15✔
92
{
93
    const auto symbol  = m_parser.symbol();
15✔
94
    const auto value   = computeValue( symbol );
15✔
95

96
    if ( isValidValue( value ) )
15✔
97
    {
98
        output( value );
15✔
99

100
        return true;
101
    }
102

NEW
103
    m_logger( ErrorMessage::invalidLoadValue( m_parser.getInstruction(), m_parser.getCurrentLineNumber() ) );
×
104

NEW
105
    return false;
×
106
}
107

108
bool Assembler::assembleComputationInstruction()
16✔
109
{
110
    Hack::word cc{ 0_w };
111

112
    cc = Coder::dest( m_parser.dest() ) | Coder::comp( m_parser.comp() ) | Coder::jump( m_parser.jump() )
16✔
113
         | static_cast< Hack::word >( 0b1110000000000000 );
114

115
    output( cc );
16✔
116

117
    return true;
16✔
118
}
119

120
Hack::word Assembler::computeValue( const std::string& symbol )
15✔
121
{
122
    Hack::word value{ 0_w };
123

124
    if ( std::isdigit( symbol.front() ) != 0 )
15✔
125
    {
126
        value = static_cast< Hack::word >( std::stoi( m_parser.symbol() ) );
10✔
127
    }
128
    else if ( m_symbolTable.contains( symbol ) )
10✔
129
    {
130
        value = m_symbolTable.getAddress( symbol ).value();
16✔
131
    }
132
    else
133
    {
134
        value = m_ramAddress++;
2✔
135
        m_symbolTable.addEntry( symbol, value );
2✔
136
    }
137

138
    return value;
15✔
139
}
140

141
bool Assembler::isValidValue( const Hack::word value ) const
×
142
{
143
    return value <= Hack::max_loadable_value;
15✔
144
}
145

146
void Assembler::output( const Hack::word word )
31✔
147
{
148
    m_out << std::bitset< 16 >( word ).to_string() << std::endl;
31✔
149
}
31✔
150

151
} // namespace Hasm
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc