Skip to content

Commit

Permalink
Encapsulated behaviour into smaller static functions
Browse files Browse the repository at this point in the history
  • Loading branch information
cheng-alvin committed Dec 31, 2023
1 parent 77e321a commit be3b7ef
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions libjas/adc.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
#include <stdint.h>
#include <stdlib.h>

static void encodeOpcode(jasTaggedOperand_t op1, jasTaggedOperand_t op2, jasTaggedOperand_t op3, jasTaggedOperand_t op4, jasInstance_t *instance, jasModrmMode_t mode, signed long long indexOfRex);

static void encodeOperands(jasTaggedOperand_t op1, jasTaggedOperand_t op2, jasTaggedOperand_t op3, jasTaggedOperand_t op4, jasInstance_t *instance, jasModrmMode_t mode);

// TODO create new unit tests for `adc`

jasErrorCode_t adc(jasTaggedOperand_t op1, jasTaggedOperand_t op2, jasTaggedOperand_t op3, jasTaggedOperand_t op4, jasInstance_t *instance) {
Expand All @@ -23,11 +27,15 @@ jasErrorCode_t adc(jasTaggedOperand_t op1, jasTaggedOperand_t op2, jasTaggedOper

jasModrmMode_t mode;

// TODO encapsulate behaviour
// Note: If there's no REX prefix, therefore the `indexOfRex` will be 0 >
signed long long indexOfRex = jasRexExpectedInRegisterEncoding(op1) ? instance->bufferLen : -1;
CONDITIONAL_WRITE(jasRexExpectedInRegisterEncoding(op1), jasRexConstructPrefix(NULL, JAS_REX_B))

encodeOpcode(op1, op2, op3, op4, instance, mode, indexOfRex);
encodeOperands(op1, op2, op3, op4, instance, mode);
}

static void encodeOpcode(jasTaggedOperand_t op1, jasTaggedOperand_t op2, jasTaggedOperand_t op3, jasTaggedOperand_t op4, jasInstance_t *instance, jasModrmMode_t mode, signed long long indexOfRex) {
switch (op1.type) {
case JAS_INDIRECT_8:
mode = JAS_MODRM_INDIRECT;
Expand Down Expand Up @@ -76,9 +84,7 @@ jasErrorCode_t adc(jasTaggedOperand_t op1, jasTaggedOperand_t op2, jasTaggedOper

NO_LONG_MODE

// TODO Encapsulate behaviour
CONDITIONAL_WRITE(indexOfRex < 0, jasRexConstructPrefix(NULL, JAS_REX_W))
else WRITE(jasRexConstructPrefix(instance->buffer[indexOfRex], JAS_REX_W));
WRITE(jasRexConstructPrefix(indexOfRex > 0 ? instance->buffer[indexOfRex] : NULL, JAS_REX_W))

WRITE_IF_TRUE_THEN_BREAK(op1.type == JAS_REG_OPERAND_64 && (op2.type == JAS_REG_OPERAND_64 || op2.type == JAS_INDIRECT_64), 0x13)
WRITE_IF_TRUE_THEN_BREAK(op1.operand.reg.reg64 == JAS_REG_RAX, 0x15)
Expand All @@ -88,9 +94,10 @@ jasErrorCode_t adc(jasTaggedOperand_t op1, jasTaggedOperand_t op2, jasTaggedOper
WRITE(0x81)
break;
}
}

static void encodeOperands(jasTaggedOperand_t op1, jasTaggedOperand_t op2, jasTaggedOperand_t op3, jasTaggedOperand_t op4, jasInstance_t *instance, jasModrmMode_t mode) {

// TODO encapsulate behaviour - Which is stupid how I did not encapsulate this behaviour in the first place.
// Operand encoder:
switch (instance->buffer[instance->bufferLen - 1]) {
case 0x14:
case 0x15:
Expand Down

0 comments on commit be3b7ef

Please sign in to comment.