@ -298,7 +298,7 @@ static always_inline struct stack_entry *pop_stack(struct stack *stack,
# define BINOP(i) inst->u.binop[i]
# define DEFINE_BINOP_HELPER(opc, OP, W0, W1, W2, W3, W4) \
# define DEFINE_BINOP_BC_ HELPER(opc, OP, W0, W1, W2, W3, W4) \
case opc * 5 : { \
uint8_t op0 , op1 , res ; \
int8_t sop0 , sop1 ; \
@ -350,8 +350,8 @@ static always_inline struct stack_entry *pop_stack(struct stack *stack,
break ; \
}
# define DEFINE_BINOP(opc, OP) DEFINE_BINOP_HELPER(opc, OP, WRITE8, WRITE8, WRITE16, WRITE32, WRITE64)
# define DEFINE_ICMPOP(opc, OP) DEFINE_BINOP_HELPER(opc, OP, WRITE8, WRITE8, WRITE8, WRITE8, WRITE8)
# define DEFINE_BINOP(opc, OP) DEFINE_BINOP_BC_ HELPER(opc, OP, WRITE8, WRITE8, WRITE16, WRITE32, WRITE64)
# define DEFINE_ICMPOP(opc, OP) DEFINE_BINOP_BC_ HELPER(opc, OP, WRITE8, WRITE8, WRITE8, WRITE8, WRITE8)
# define CHECK_OP(cond, msg) if((cond)) { cli_dbgmsg(msg); stop = CL_EBYTECODE; break;}
@ -409,7 +409,7 @@ static always_inline struct stack_entry *pop_stack(struct stack *stack,
default : CHECK_UNREACHABLE ; \
}
# define DEFINE_OP_RET_N(OP, T, R0, W0) \
# define DEFINE_OP_BC_ RET_N(OP, T, R0, W0) \
case OP : { \
T tmp ; \
R0 ( tmp , inst - > u . unaryop ) ; \
@ -447,77 +447,77 @@ int cli_vm_execute(const struct cli_bc *bc, struct cli_bc_ctx *ctx, const struct
do {
pc + + ;
switch ( inst - > interp_op ) {
DEFINE_BINOP ( OP_ADD , res = op0 + op1 ) ;
DEFINE_BINOP ( OP_SUB , res = op0 - op1 ) ;
DEFINE_BINOP ( OP_MUL , res = op0 * op1 ) ;
DEFINE_BINOP ( OP_BC_ ADD , res = op0 + op1 ) ;
DEFINE_BINOP ( OP_BC_ SUB , res = op0 - op1 ) ;
DEFINE_BINOP ( OP_BC_ MUL , res = op0 * op1 ) ;
DEFINE_BINOP ( OP_UDIV , CHECK_OP ( op1 = = 0 , " bytecode attempted to execute udiv#0 \n " ) ;
DEFINE_BINOP ( OP_BC_ UDIV , CHECK_OP ( op1 = = 0 , " bytecode attempted to execute udiv#0 \n " ) ;
res = op0 / op1 ) ;
DEFINE_BINOP ( OP_SDIV , CHECK_OP ( check_sdivops ( sop0 , sop1 ) , " bytecode attempted to execute sdiv#0 \n " ) ;
DEFINE_BINOP ( OP_BC_ SDIV , CHECK_OP ( check_sdivops ( sop0 , sop1 ) , " bytecode attempted to execute sdiv#0 \n " ) ;
res = sop0 / sop1 ) ;
DEFINE_BINOP ( OP_UREM , CHECK_OP ( op1 = = 0 , " bytecode attempted to execute urem#0 \n " ) ;
DEFINE_BINOP ( OP_BC_ UREM , CHECK_OP ( op1 = = 0 , " bytecode attempted to execute urem#0 \n " ) ;
res = op0 % op1 ) ;
DEFINE_BINOP ( OP_SREM , CHECK_OP ( check_sdivops ( sop0 , sop1 ) , " bytecode attempted to execute urem#0 \n " ) ;
DEFINE_BINOP ( OP_BC_ SREM , CHECK_OP ( check_sdivops ( sop0 , sop1 ) , " bytecode attempted to execute urem#0 \n " ) ;
res = sop0 % sop1 ) ;
DEFINE_BINOP ( OP_SHL , CHECK_OP ( op1 > inst - > type , " bytecode attempted to execute shl greater than bitwidth \n " ) ;
DEFINE_BINOP ( OP_BC_ SHL , CHECK_OP ( op1 > inst - > type , " bytecode attempted to execute shl greater than bitwidth \n " ) ;
res = op0 < < op1 ) ;
DEFINE_BINOP ( OP_LSHR , CHECK_OP ( op1 > inst - > type , " bytecode attempted to execute lshr greater than bitwidth \n " ) ;
DEFINE_BINOP ( OP_BC_ LSHR , CHECK_OP ( op1 > inst - > type , " bytecode attempted to execute lshr greater than bitwidth \n " ) ;
res = op0 > > op1 ) ;
DEFINE_BINOP ( OP_ASHR , CHECK_OP ( op1 > inst - > type , " bytecode attempted to execute ashr greater than bitwidth \n " ) ;
DEFINE_BINOP ( OP_BC_ ASHR , CHECK_OP ( op1 > inst - > type , " bytecode attempted to execute ashr greater than bitwidth \n " ) ;
res = CLI_SRS ( sop0 , op1 ) ) ;
DEFINE_BINOP ( OP_AND , res = op0 & op1 ) ;
DEFINE_BINOP ( OP_OR , res = op0 | op1 ) ;
DEFINE_BINOP ( OP_XOR , res = op0 ^ op1 ) ;
DEFINE_BINOP ( OP_BC_ AND , res = op0 & op1 ) ;
DEFINE_BINOP ( OP_BC_ OR , res = op0 | op1 ) ;
DEFINE_BINOP ( OP_BC_ XOR , res = op0 ^ op1 ) ;
DEFINE_CASTOP ( OP_SEXT ,
DEFINE_CASTOP ( OP_BC_ SEXT ,
CHOOSE ( READ1 ( sres , inst - > u . cast . source ) ; res = sres ? ~ 0ull : 0 ,
READ8 ( sres , inst - > u . cast . source ) ; res = sres = SIGNEXT ( sres , inst - > u . cast . mask ) ,
READ16 ( sres , inst - > u . cast . source ) ; res = sres = SIGNEXT ( sres , inst - > u . cast . mask ) ,
READ32 ( sres , inst - > u . cast . source ) ; res = sres = SIGNEXT ( sres , inst - > u . cast . mask ) ,
READ64 ( sres , inst - > u . cast . source ) ; res = sres = SIGNEXT ( sres , inst - > u . cast . mask ) ) ) ;
DEFINE_CASTOP ( OP_ZEXT ,
DEFINE_CASTOP ( OP_BC_ ZEXT ,
CHOOSE ( READ1 ( res , inst - > u . cast . source ) ,
READ8 ( res , inst - > u . cast . source ) ,
READ16 ( res , inst - > u . cast . source ) ,
READ32 ( res , inst - > u . cast . source ) ,
READ64 ( res , inst - > u . cast . source ) ) ) ;
DEFINE_CASTOP ( OP_TRUNC ,
DEFINE_CASTOP ( OP_BC_ TRUNC ,
CHOOSE ( READ1 ( res , inst - > u . cast . source ) ,
READ8 ( res , inst - > u . cast . source ) ,
READ16 ( res , inst - > u . cast . source ) ,
READ32 ( res , inst - > u . cast . source ) ,
READ64 ( res , inst - > u . cast . source ) ) ) ;
DEFINE_OP ( OP_BRANCH )
DEFINE_OP ( OP_BC_B RANCH )
stop = jump ( func , ( values [ inst - > u . branch . condition ] & 1 ) ?
inst - > u . branch . br_true : inst - > u . branch . br_false ,
& bb , & inst , & bb_inst ) ;
continue ;
DEFINE_OP ( OP_JMP )
DEFINE_OP ( OP_BC_ JMP )
stop = jump ( func , inst - > u . jump , & bb , & inst , & bb_inst ) ;
continue ;
DEFINE_OP_RET_N ( OP_RET * 5 , uint8_t , READ1 , WRITE8 ) ;
DEFINE_OP_RET_N ( OP_RET * 5 + 1 , uint8_t , READ8 , WRITE8 ) ;
DEFINE_OP_RET_N ( OP_RET * 5 + 2 , uint16_t , READ16 , WRITE16 ) ;
DEFINE_OP_RET_N ( OP_RET * 5 + 3 , uint32_t , READ32 , WRITE32 ) ;
DEFINE_OP_RET_N ( OP_RET * 5 + 4 , uint64_t , READ64 , WRITE64 ) ;
DEFINE_ICMPOP ( OP_ICMP_EQ , res = ( op0 = = op1 ) ) ;
DEFINE_ICMPOP ( OP_ICMP_NE , res = ( op0 ! = op1 ) ) ;
DEFINE_ICMPOP ( OP_ICMP_UGT , res = ( op0 > op1 ) ) ;
DEFINE_ICMPOP ( OP_ICMP_UGE , res = ( op0 > = op1 ) ) ;
DEFINE_ICMPOP ( OP_ICMP_ULT , res = ( op0 < op1 ) ) ;
DEFINE_ICMPOP ( OP_ICMP_ULE , res = ( op0 < = op1 ) ) ;
DEFINE_ICMPOP ( OP_ICMP_SGT , res = ( sop0 > sop1 ) ) ;
DEFINE_ICMPOP ( OP_ICMP_SGE , res = ( sop0 > = sop1 ) ) ;
DEFINE_ICMPOP ( OP_ICMP_SLE , res = ( sop0 < = sop1 ) ) ;
DEFINE_ICMPOP ( OP_ICMP_SLT , res = ( sop0 < sop1 ) ) ;
case OP_SELECT * 5 :
DEFINE_OP_BC_ RET_N ( OP_BC _RET * 5 , uint8_t , READ1 , WRITE8 ) ;
DEFINE_OP_BC_ RET_N ( OP_BC _RET * 5 + 1 , uint8_t , READ8 , WRITE8 ) ;
DEFINE_OP_BC_ RET_N ( OP_BC _RET * 5 + 2 , uint16_t , READ16 , WRITE16 ) ;
DEFINE_OP_BC_ RET_N ( OP_BC _RET * 5 + 3 , uint32_t , READ32 , WRITE32 ) ;
DEFINE_OP_BC_ RET_N ( OP_BC _RET * 5 + 4 , uint64_t , READ64 , WRITE64 ) ;
DEFINE_ICMPOP ( OP_BC_ ICMP_EQ , res = ( op0 = = op1 ) ) ;
DEFINE_ICMPOP ( OP_BC_ ICMP_NE , res = ( op0 ! = op1 ) ) ;
DEFINE_ICMPOP ( OP_BC_ ICMP_UGT , res = ( op0 > op1 ) ) ;
DEFINE_ICMPOP ( OP_BC_ ICMP_UGE , res = ( op0 > = op1 ) ) ;
DEFINE_ICMPOP ( OP_BC_ ICMP_ULT , res = ( op0 < op1 ) ) ;
DEFINE_ICMPOP ( OP_BC_ ICMP_ULE , res = ( op0 < = op1 ) ) ;
DEFINE_ICMPOP ( OP_BC_ ICMP_SGT , res = ( sop0 > sop1 ) ) ;
DEFINE_ICMPOP ( OP_BC_ ICMP_SGE , res = ( sop0 > = sop1 ) ) ;
DEFINE_ICMPOP ( OP_BC_ ICMP_SLE , res = ( sop0 < = sop1 ) ) ;
DEFINE_ICMPOP ( OP_BC_ ICMP_SLT , res = ( sop0 < sop1 ) ) ;
case OP_BC_ SELECT * 5 :
{
uint8_t t0 , t1 , t2 ;
READ1 ( t0 , inst - > u . three [ 0 ] ) ;
@ -526,7 +526,7 @@ int cli_vm_execute(const struct cli_bc *bc, struct cli_bc_ctx *ctx, const struct
WRITE8 ( inst - > dest , t0 ? t1 : t2 ) ;
break ;
}
case OP_SELECT * 5 + 1 :
case OP_BC_ SELECT * 5 + 1 :
{
uint8_t t0 , t1 , t2 ;
READ1 ( t0 , inst - > u . three [ 0 ] ) ;
@ -535,7 +535,7 @@ int cli_vm_execute(const struct cli_bc *bc, struct cli_bc_ctx *ctx, const struct
WRITE8 ( inst - > dest , t0 ? t1 : t2 ) ;
break ;
}
case OP_SELECT * 5 + 2 :
case OP_BC_ SELECT * 5 + 2 :
{
uint8_t t0 ;
uint16_t t1 , t2 ;
@ -545,7 +545,7 @@ int cli_vm_execute(const struct cli_bc *bc, struct cli_bc_ctx *ctx, const struct
WRITE16 ( inst - > dest , t0 ? t1 : t2 ) ;
break ;
}
case OP_SELECT * 5 + 3 :
case OP_BC_ SELECT * 5 + 3 :
{
uint8_t t0 ;
uint32_t t1 , t2 ;
@ -555,7 +555,7 @@ int cli_vm_execute(const struct cli_bc *bc, struct cli_bc_ctx *ctx, const struct
WRITE32 ( inst - > dest , t0 ? t1 : t2 ) ;
break ;
}
case OP_SELECT * 5 + 4 :
case OP_BC_ SELECT * 5 + 4 :
{
uint8_t t0 ;
uint64_t t1 , t2 ;
@ -566,7 +566,7 @@ int cli_vm_execute(const struct cli_bc *bc, struct cli_bc_ctx *ctx, const struct
break ;
}
DEFINE_OP ( OP_CALL_API ) {
DEFINE_OP ( OP_BC_ CALL_API ) {
const struct cli_apicall * api = & cli_apicalls [ inst - > u . ops . funcid ] ;
int32_t res ;
CHECK_APIID ( inst - > u . ops . funcid ) ;
@ -594,7 +594,7 @@ int cli_vm_execute(const struct cli_bc *bc, struct cli_bc_ctx *ctx, const struct
break ;
}
DEFINE_OP ( OP_CALL_DIRECT )
DEFINE_OP ( OP_BC_ CALL_DIRECT )
CHECK_FUNCID ( inst - > u . ops . funcid ) ;
func2 = & bc - > funcs [ inst - > u . ops . funcid ] ;
CHECK_EQ ( func2 - > numArgs , inst - > u . ops . numOps ) ;
@ -657,35 +657,35 @@ int cli_vm_execute(const struct cli_bc *bc, struct cli_bc_ctx *ctx, const struct
stack_depth + + ;
continue ;
case OP_COPY * 5 :
case OP_BC_ COPY * 5 :
{
uint8_t op ;
READ1 ( op , BINOP ( 0 ) ) ;
WRITE8 ( BINOP ( 1 ) , op ) ;
break ;
}
case OP_COPY * 5 + 1 :
case OP_BC_ COPY * 5 + 1 :
{
uint8_t op ;
READ8 ( op , BINOP ( 0 ) ) ;
WRITE8 ( BINOP ( 1 ) , op ) ;
break ;
}
case OP_COPY * 5 + 2 :
case OP_BC_ COPY * 5 + 2 :
{
uint16_t op ;
READ16 ( op , BINOP ( 0 ) ) ;
WRITE16 ( BINOP ( 1 ) , op ) ;
break ;
}
case OP_COPY * 5 + 3 :
case OP_BC_ COPY * 5 + 3 :
{
uint32_t op ;
READ32 ( op , BINOP ( 0 ) ) ;
WRITE32 ( BINOP ( 1 ) , op ) ;
break ;
}
case OP_COPY * 5 + 4 :
case OP_BC_ COPY * 5 + 4 :
{
uint64_t op ;
READ32 ( op , BINOP ( 0 ) ) ;
@ -693,29 +693,29 @@ int cli_vm_execute(const struct cli_bc *bc, struct cli_bc_ctx *ctx, const struct
break ;
}
case OP_LOAD * 5 :
case OP_LOAD * 5 + 1 :
case OP_BC_ LOAD * 5 :
case OP_BC_ LOAD * 5 + 1 :
{
uint8_t * ptr ;
READP ( ptr , inst - > u . unaryop ) ;
WRITE8 ( inst - > dest , ( * ptr ) ) ;
break ;
}
case OP_LOAD * 5 + 2 :
case OP_BC_ LOAD * 5 + 2 :
{
const union unaligned_16 * ptr ;
READP ( ptr , inst - > u . unaryop ) ;
WRITE16 ( inst - > dest , ( ptr - > una_u16 ) ) ;
break ;
}
case OP_LOAD * 5 + 3 :
case OP_BC_ LOAD * 5 + 3 :
{
const union unaligned_32 * ptr ;
READP ( ptr , inst - > u . unaryop ) ;
WRITE32 ( inst - > dest , ( ptr - > una_u32 ) ) ;
break ;
}
case OP_LOAD * 5 + 4 :
case OP_BC_ LOAD * 5 + 4 :
{
const union unaligned_64 * ptr ;
READP ( ptr , inst - > u . unaryop ) ;
@ -723,7 +723,7 @@ int cli_vm_execute(const struct cli_bc *bc, struct cli_bc_ctx *ctx, const struct
break ;
}
case OP_STORE * 5 :
case OP_BC_ STORE * 5 :
{
uint8_t * ptr ;
uint8_t v ;
@ -732,7 +732,7 @@ int cli_vm_execute(const struct cli_bc *bc, struct cli_bc_ctx *ctx, const struct
* ptr = v ;
break ;
}
case OP_STORE * 5 + 1 :
case OP_BC_ STORE * 5 + 1 :
{
uint8_t * ptr ;
uint8_t v ;
@ -741,7 +741,7 @@ int cli_vm_execute(const struct cli_bc *bc, struct cli_bc_ctx *ctx, const struct
* ptr = v ;
break ;
}
case OP_STORE * 5 + 2 :
case OP_BC_ STORE * 5 + 2 :
{
union unaligned_16 * ptr ;
uint16_t v ;
@ -750,7 +750,7 @@ int cli_vm_execute(const struct cli_bc *bc, struct cli_bc_ctx *ctx, const struct
ptr - > una_s16 = v ;
break ;
}
case OP_STORE * 5 + 3 :
case OP_BC_ STORE * 5 + 3 :
{
union unaligned_32 * ptr ;
uint32_t v ;
@ -759,7 +759,7 @@ int cli_vm_execute(const struct cli_bc *bc, struct cli_bc_ctx *ctx, const struct
ptr - > una_u32 = v ;
break ;
}
case OP_STORE * 5 + 4 :
case OP_BC_ STORE * 5 + 4 :
{
union unaligned_64 * ptr ;
uint64_t v ;
@ -768,7 +768,7 @@ int cli_vm_execute(const struct cli_bc *bc, struct cli_bc_ctx *ctx, const struct
ptr - > una_u64 = v ;
break ;
}
/* TODO: implement OP_GEP1, OP_GEP2, OP_GEPN */
/* TODO: implement OP_BC_ GEP1, OP_BC_ GEP2, OP_BC _GEPN */
default :
cli_errmsg ( " Opcode %u of type %u is not implemented yet! \n " ,
inst - > interp_op / 5 , inst - > interp_op % 5 ) ;