From 386036e23b67d6aa6d10ff166a1904f18c304a8a Mon Sep 17 00:00:00 2001 From: Lars Knoll Date: Mon, 2 Jul 2018 13:24:03 +0200 Subject: Change the instruction encoding for the bytecode Change the encoding, so that even instructions are short ones, odd instructions long. This implies that the lowest bit encodes whether an instruction is short or long (1 vs 4 byte encoded). This prepares for allowing us to extend the totoal number of instructions beyond 128. Change-Id: I4732e7241d3593b24ad25cd69555edc25f38d2f6 Reviewed-by: Simon Hausmann --- src/qml/compiler/qv4bytecodegenerator.cpp | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) (limited to 'src/qml/compiler/qv4bytecodegenerator.cpp') diff --git a/src/qml/compiler/qv4bytecodegenerator.cpp b/src/qml/compiler/qv4bytecodegenerator.cpp index 1d1e98ea58..6efeac2a31 100644 --- a/src/qml/compiler/qv4bytecodegenerator.cpp +++ b/src/qml/compiler/qv4bytecodegenerator.cpp @@ -70,10 +70,9 @@ int BytecodeGenerator::newRegisterArray(int n) void BytecodeGenerator::packInstruction(I &i) { - uchar type = *reinterpret_cast(i.packed); - Q_ASSERT(type >= MOTH_NUM_INSTRUCTIONS()); - if (type >= MOTH_NUM_INSTRUCTIONS()) - type -= MOTH_NUM_INSTRUCTIONS(); + Instr::Type type = Instr::unpack(i.packed); + Q_ASSERT(int(type) < MOTH_NUM_INSTRUCTIONS()); + type = Instr::narrowInstructionType(type); int instructionsAsInts[sizeof(Instr)/sizeof(int)] = {}; int nMembers = Moth::InstrInfo::argumentCount[static_cast(i.type)]; for (int j = 0; j < nMembers; ++j) { @@ -89,10 +88,10 @@ void BytecodeGenerator::packInstruction(I &i) break; } } - char *code = i.packed; + uchar *code = i.packed; switch (width) { case Normal: - *reinterpret_cast(code) = type; + Instr::pack(code, type); ++code; for (int n = 0; n < nMembers; ++n) { qint8 v = static_cast(instructionsAsInts[n]); @@ -122,7 +121,7 @@ void BytecodeGenerator::adjustJumpOffsets() // qDebug() << "adjusting jump offset for instruction" << index << i.position << i.size << "offsetForJump" << i.offsetForJump << "target" // << labels.at(i.linkedLabel) << linkedInstruction.position << "jumpOffset" << jumpOffset; uchar type = *reinterpret_cast(i.packed); - if (type >= MOTH_NUM_INSTRUCTIONS()) { + if (Instr::isWide(Instr::Type(type))) { Q_ASSERT(i.offsetForJump == i.size - 4); qToLittleEndian(jumpOffset, c); } else { @@ -177,7 +176,7 @@ void BytecodeGenerator::finalize(Compiler::Context *context) entry.line = currentLine; lineNumbers.append(entry); } - code.append(i.packed, i.size); + code.append(reinterpret_cast(i.packed), i.size); } context->code = code; @@ -228,10 +227,10 @@ QT_WARNING_POP if (offsetOfOffset != -1) offsetOfOffset += 1; I instr{type, static_cast(s + 1), 0, currentLine, offsetOfOffset, -1, "\0\0" }; - char *code = instr.packed; - *reinterpret_cast(code) = static_cast(MOTH_NUM_INSTRUCTIONS() + static_cast(type)); + uchar *code = instr.packed; + Instr::pack(code, Instr::wideInstructionType(type)); ++code; - Q_ASSERT(MOTH_NUM_INSTRUCTIONS() + static_cast(type) < 256); + Q_ASSERT(static_cast(Instr::wideInstructionType(type)) < 256); for (int j = 0; j < argCount; ++j) { qToLittleEndian(i.argumentsAsInts[j], code); -- cgit v1.2.1