Skip to content

Commit 68c3b24

Browse files
committed
handle sign extension of constants of various lengths
This is a generalization of 9918ea6 which handles 8-bit and 16-bit as well as 32-bit values.
1 parent d8729a7 commit 68c3b24

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

src/codegen/target/x86/encode.cpp

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,24 @@
2424

2525
using namespace avian::util;
2626

27+
namespace {
28+
29+
int64_t
30+
signExtend(unsigned size, int64_t v)
31+
{
32+
if (size == 4) {
33+
return static_cast<int32_t>(v);
34+
} else if (size == 2) {
35+
return static_cast<int16_t>(v);
36+
} else if (size == 1) {
37+
return static_cast<int8_t>(v);
38+
} else {
39+
return v;
40+
}
41+
}
42+
43+
} // namespace
44+
2745
namespace avian {
2846
namespace codegen {
2947
namespace x86 {
@@ -328,11 +346,7 @@ void moveCR2(Context* c, UNUSED unsigned aSize, lir::Constant* a,
328346
UNUSED unsigned bSize, lir::Register* b, unsigned promiseOffset)
329347
{
330348
if (vm::TargetBytesPerWord == 4 and bSize == 8) {
331-
int64_t v = a->value->value();
332-
333-
if (aSize == 4) {
334-
v = static_cast<int32_t>(v);
335-
}
349+
int64_t v = signExtend(aSize, a->value->value());
336350

337351
ResolvedPromise high((v >> 32) & 0xFFFFFFFF);
338352
lir::Constant ah(&high);
@@ -348,13 +362,7 @@ void moveCR2(Context* c, UNUSED unsigned aSize, lir::Constant* a,
348362
maybeRex(c, vm::TargetBytesPerWord, b);
349363
opcode(c, 0xb8 + regCode(b));
350364
if (a->value->resolved()) {
351-
int64_t v = a->value->value();
352-
353-
if (aSize == 4 and bSize == 8) {
354-
v = static_cast<int32_t>(v);
355-
}
356-
357-
c->code.appendTargetAddress(v);
365+
c->code.appendTargetAddress(signExtend(aSize, a->value->value()));
358366
} else {
359367
expect(c, aSize == vm::TargetBytesPerWord);
360368

0 commit comments

Comments
 (0)