Skip to content

Commit 324f392

Browse files
authored
Merge pull request #32 from Pigrecos/main
Refactor extractBytes function to handle various input sizes correctly
2 parents 674297a + add4af8 commit 324f392

File tree

1 file changed

+13
-22
lines changed

1 file changed

+13
-22
lines changed

lifter/GEPTracker.cpp

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -234,35 +234,26 @@ class lifterMemoryBuffer {
234234
}
235235

236236
private:
237-
Value* extractBytes(IRBuilder<>& builder, Value* value, uint64_t startOffset,
238-
uint64_t endOffset) {
237+
Value* extractBytes(IRBuilder<>& builder, Value* value, uint64_t startOffset, uint64_t endOffset) {
239238
LLVMContext& context = builder.getContext();
240-
241-
if (!value) {
242-
return ConstantInt::get(
243-
Type::getIntNTy(context, (endOffset - startOffset) * 8), 0);
244-
}
245-
246239
uint64_t byteCount = endOffset - startOffset;
240+
Type* resultType = Type::getIntNTy(context, byteCount * 8);
247241

248-
uint64_t shiftAmount = startOffset * 8;
249-
250-
printvalue2(endOffset);
242+
printvalue(value);
251243

252-
printvalue2(startOffset);
253-
printvalue2(byteCount);
254-
printvalue2(shiftAmount);
244+
// Shift right if needed
245+
if (startOffset > 0) {
246+
uint64_t shiftAmount = startOffset * 8;
247+
value = createLShrFolder(builder, value, APInt(value->getType()->getIntegerBitWidth(), shiftAmount), "shiftedValue");
248+
}
255249

256-
Value* shiftedValue = createLShrFolder(
257-
builder, value,
258-
APInt(value->getType()->getIntegerBitWidth(), shiftAmount),
259-
"extractbytes");
260250
printvalue(value);
261-
printvalue(shiftedValue);
262251

263-
Value* truncatedValue = createTruncFolder(
264-
builder, shiftedValue, Type::getIntNTy(context, byteCount * 8));
265-
return truncatedValue;
252+
// Extend or truncate to the required size
253+
value = createZExtOrTruncFolder(builder, value, resultType);
254+
255+
printvalue(value);
256+
return value;
266257
}
267258
};
268259

0 commit comments

Comments
 (0)