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

rieske / trans / 28379938466

29 Jun 2026 02:35PM UTC coverage: 90.44% (-0.2%) from 90.635%
28379938466

push

github

web-flow
Merge pull request #41 from rieske/feature/file-scope-globals

Add file-scope global variables

300 of 358 new or added lines in 19 files covered. (83.8%)

8 existing lines in 2 files now uncovered.

5052 of 5586 relevant lines covered (90.44%)

242657.94 hits per line

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

86.82
/src/codegen/IntelInstructionSet.cpp
1
#include "IntelInstructionSet.h"
2

3
#include "Register.h"
4

5
#include <iostream>
6
#include <sstream>
7

8
namespace {
9

10
std::string memoryOffsetMnemonic(const codegen::Register& memoryBase, int memoryOffset) {
7,940✔
11
    return "[" + memoryBase.getName() + (memoryOffset ? " + " + std::to_string(memoryOffset) : "") + "]";
8,208✔
12
}
13

14
std::string memoryReference(const codegen::MemoryOperand& operand) {
8,142✔
15
    if (operand.isGlobal()) {
8,142✔
16
        return "[rel " + operand.label() + "]";
202✔
17
    }
18
    return memoryOffsetMnemonic(operand.baseRegister(), operand.offset());
7,940✔
19
}
20

21
} // namespace
22

23
namespace codegen {
24

25
IntelInstructionSet::~IntelInstructionSet() = default;
696✔
26

27
// TODO: this needs to be rethought, expanded and unit tested separately
28
// currently just a spike for handling newlines and driven by functional tests
29
// - needs to handle all kinds of escape sequences
30
// - needs to handle single quotes - will break now if constant contains a single quote
31
std::string toConstantDeclaration(std::string escapedConstant) {
770✔
32
    auto constantValue = escapedConstant.substr(1, escapedConstant.length()-2); // strip "
770✔
33
    std::stringstream declaration;
770✔
34
    declaration << "db '";
770✔
35
    for (auto it = escapedConstant.cbegin()+1; it != escapedConstant.cend()-1; ++it) {
3,462✔
36
        if (*it == '\\' && *(it+1) == 'n') {
2,692✔
37
            declaration << "', 10, '";
102✔
38
            ++it;
102✔
39
        } else {
40
            declaration << *it;
2,590✔
41
        }
42
    }
43
    declaration << "', 0";
770✔
44
    return declaration.str();
1,540✔
45
}
770✔
46

47
std::string IntelInstructionSet::preamble(const std::map<std::string, std::string>& constants,
348✔
48
        const std::vector<GlobalVariable>& globalVariables) const {
49
    std::stringstream preamble;
348✔
50
    preamble << "default rel\n"
51
            "extern scanf\n"
52
            "extern printf\n\n"
53
            "section .data\n";
348✔
54
    for (const auto& constant : constants) {
1,118✔
55
        preamble << "\t" << constant.first << " " << toConstantDeclaration(constant.second) << "\n";
770✔
56
    }
57
    // One qword per global for now (sizeInBytes is tracked for StackMachine homes only).
58
    for (const auto& global : globalVariables) {
432✔
59
        preamble << "\t" << global.name << " dq " << global.initializerLiteral << "\n";
84✔
60
    }
61
    preamble << "\n"
62
            "section .text\n"
63
            "\tglobal main\n\n";
348✔
64
    return preamble.str();
696✔
65
}
348✔
66

67
std::string IntelInstructionSet::label(std::string name) const {
1,014✔
68
    return name + ":";
1,014✔
69
}
70

71
std::string IntelInstructionSet::push(const Register& reg) const {
2,640✔
72
    return "push " + reg.getName();
2,640✔
73
}
74

75
std::string IntelInstructionSet::pop(const Register& reg) const {
2,260✔
76
    return "pop " + reg.getName();
2,260✔
77
}
78

79
std::string IntelInstructionSet::add(const Register& reg, int constant) const {
14✔
80
    return "add " + reg.getName() + ", " + std::to_string(constant);
14✔
81
}
82

83
std::string IntelInstructionSet::sub(const Register& reg, int constant) const {
438✔
84
    return "sub " + reg.getName() + ", " + std::to_string(constant);
438✔
85
}
86

87
std::string IntelInstructionSet::lea(const MemoryOperand& source, const Register& target) const {
318✔
88
    return "lea " + target.getName() + ", " + memoryReference(source);
318✔
89
}
90

91
std::string IntelInstructionSet::not_(const Register& reg) const {
×
92
    return "not " + reg.getName();
×
93
}
94

95
std::string IntelInstructionSet::mov(const Register& from, const MemoryOperand& destination) const {
2,286✔
96
    return "mov " + memoryReference(destination) + ", " + from.getName();
2,286✔
97
}
98

99
std::string IntelInstructionSet::mov(const Register& from, const Register& to) const {
924✔
100
    if (&from == &to) {
924✔
101
        return "";
×
102
    }
103
    return "mov " + to.getName() + ", " + from.getName();
924✔
104
}
105

106
std::string IntelInstructionSet::mov(const MemoryOperand& source, const Register& to) const {
2,748✔
107
    return "mov " + to.getName() + ", " + memoryReference(source);
2,748✔
108
}
109

110
std::string IntelInstructionSet::mov(std::string constant, const MemoryOperand& destination) const {
2,332✔
111
    return "mov qword " + memoryReference(destination) + ", " + constant;
2,332✔
112
}
113

114
std::string IntelInstructionSet::mov(std::string constant, const Register& to) const {
×
115
    return "mov " + to.getName() + ", " + constant;
×
116
}
117

118
std::string IntelInstructionSet::cmp(const Register& leftArgument, const MemoryOperand& rightArgument) const {
20✔
119
    return "cmp " + leftArgument.getName() + ", " + "qword " + memoryReference(rightArgument);
20✔
120
}
121

122
std::string IntelInstructionSet::cmp(const Register& leftArgument, const Register& rightArgument) const {
20✔
123
    return "cmp " + leftArgument.getName() + ", " + rightArgument.getName();
20✔
124
}
125

126
std::string IntelInstructionSet::cmp(const MemoryOperand& leftArgument, const Register& rightArgument) const {
138✔
127
    return "cmp qword " + memoryReference(leftArgument) + ", " + rightArgument.getName();
138✔
128
}
129

130
std::string IntelInstructionSet::cmp(const Register& argument, int constant) const {
10✔
131
    return "cmp " + argument.getName() + ", " + std::to_string(constant);
10✔
132
}
133

134
std::string IntelInstructionSet::cmp(const MemoryOperand& leftArgument, int constant) const {
130✔
135
    return "cmp qword " + memoryReference(leftArgument) + ", " + std::to_string(constant);
130✔
136
}
137

138
std::string IntelInstructionSet::call(std::string procedureName) const {
870✔
139
    return "call " + procedureName;
870✔
140
}
141

142
std::string IntelInstructionSet::jmp(std::string label) const {
274✔
143
    return "jmp " + label;
274✔
144
}
145

146
std::string IntelInstructionSet::je(std::string label) const {
152✔
147
    return "je " + label;
152✔
148
}
149

150
std::string IntelInstructionSet::jne(std::string label) const {
22✔
151
    return "jne " + label;
22✔
152
}
153

154
std::string IntelInstructionSet::jg(std::string label) const {
36✔
155
    return "jg " + label;
36✔
156
}
157

158
std::string IntelInstructionSet::jl(std::string label) const {
56✔
159
    return "jl " + label;
56✔
160
}
161

162
std::string IntelInstructionSet::jge(std::string label) const {
24✔
163
    return "jge " + label;
24✔
164
}
165

166
std::string IntelInstructionSet::jle(std::string label) const {
28✔
167
    return "jle " + label;
28✔
168
}
169

170
std::string IntelInstructionSet::syscall() const {
×
171
    return "syscall";
×
172
}
173

174
std::string IntelInstructionSet::leave() const {
452✔
175
    return "leave";
904✔
176
}
177

178
std::string IntelInstructionSet::ret() const {
452✔
179
    return "ret";
904✔
180
}
181

182
std::string IntelInstructionSet::xor_(const Register& operand, const Register& result) const {
890✔
183
    return "xor " + result.getName() + ", " + operand.getName();
890✔
184
}
185

186
std::string IntelInstructionSet::xor_(const MemoryOperand& operand, const Register& result) const {
10✔
187
    return "xor " + result.getName() + ", " + memoryReference(operand);
10✔
188
}
189

UNCOV
190
std::string IntelInstructionSet::or_(const Register& operand, const Register& result) const {
×
UNCOV
191
    return "or " + result.getName() + ", " + operand.getName();
×
192
}
193

194
std::string IntelInstructionSet::or_(const MemoryOperand& operand, const Register& result) const {
10✔
195
    return "or " + result.getName() + ", " + memoryReference(operand);
10✔
196
}
197

UNCOV
198
std::string IntelInstructionSet::and_(const Register& operand, const Register& result) const {
×
UNCOV
199
    return "and " + result.getName() + ", " + operand.getName();
×
200
}
201

202
std::string IntelInstructionSet::and_(const MemoryOperand& operand, const Register& result) const {
10✔
203
    return "and " + result.getName() + ", " + memoryReference(operand);
10✔
204
}
205

206
std::string IntelInstructionSet::shl(const Register& result) const {
20✔
207
    return "shl " + result.getName() + ", cl";
20✔
208
}
209

210
//std::string IntelInstructionSet::shl(std::string constant, const Register& result) const {
211
//}
212

213
std::string IntelInstructionSet::shr(const Register& result) const {
18✔
214
    return "shr " + result.getName() + ", cl";
18✔
215
}
216

217
//std::string IntelInstructionSet::shr(std::string constant, const Register& result) const {
218
//}
219

220
std::string IntelInstructionSet::add(const Register& operand, const Register& result) const {
12✔
221
    return "add " + result.getName() + ", " + operand.getName();
12✔
222
}
223

224
std::string IntelInstructionSet::add(const MemoryOperand& operand, const Register& result) const {
66✔
225
    return "add " + result.getName() + ", " + memoryReference(operand);
66✔
226
}
227

228
std::string IntelInstructionSet::sub(const Register& operand, const Register& result) const {
×
229
    return "sub " + result.getName() + ", " + operand.getName();
×
230
}
231

232
std::string IntelInstructionSet::sub(const MemoryOperand& operand, const Register& result) const {
22✔
233
    return "sub " + result.getName() + ", " + memoryReference(operand);
22✔
234
}
235

236
std::string IntelInstructionSet::imul(const Register& operand) const {
×
237
    return "imul " + operand.getName();
×
238
}
239

240
std::string IntelInstructionSet::imul(const MemoryOperand& operand) const {
16✔
241
    return "imul qword " + memoryReference(operand);
16✔
242
}
243

244
std::string IntelInstructionSet::idiv(const Register& operand) const {
×
245
    return "idiv " + operand.getName();
×
246
}
247

248
std::string IntelInstructionSet::idiv(const MemoryOperand& operand) const {
20✔
249
    return "idiv qword " + memoryReference(operand);
20✔
250
}
251

252
std::string IntelInstructionSet::inc(const Register& operand) const {
28✔
253
    return "inc " + operand.getName();
28✔
254
}
255

256
std::string IntelInstructionSet::inc(const MemoryOperand& operand) const {
12✔
257
    return "inc qword " + memoryReference(operand);
12✔
258
}
259

260
std::string IntelInstructionSet::dec(const Register& operand) const {
8✔
261
    return "dec " + operand.getName();
8✔
262
}
263

264
std::string IntelInstructionSet::dec(const MemoryOperand& operand) const {
4✔
265
    return "dec qword " + memoryReference(operand);
4✔
266
}
267

268
std::string IntelInstructionSet::neg(const Register& operand) const {
88✔
269
    return "neg " + operand.getName();
88✔
270
}
271

272
} // namespace codegen
273

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