fixes for compiling vector operations to C++

This commit is contained in:
Vern Paxson 2021-12-12 12:33:38 -08:00
parent 4ea5785908
commit 52ed9351a9
3 changed files with 46 additions and 62 deletions

View file

@ -580,13 +580,13 @@ string CPPCompile::GenArithCoerceExpr(const Expr* e, GenType gt)
if ( same_type(t, op->GetType()) ) if ( same_type(t, op->GetType()) )
return GenExpr(op, gt); return GenExpr(op, gt);
bool is_vec = t->Tag() == TYPE_VECTOR; if ( t->Tag() == TYPE_VECTOR )
return string("vector_coerce_to__CPP(") + GenExpr(op, GEN_NATIVE) + ", " + GenTypeName(t) +
auto coerce_t = is_vec ? t->Yield() : t; ")";
string cast_name; string cast_name;
switch ( coerce_t->InternalType() ) switch ( t->InternalType() )
{ {
case TYPE_INTERNAL_INT: case TYPE_INTERNAL_INT:
cast_name = "bro_int_t"; cast_name = "bro_int_t";
@ -602,10 +602,6 @@ string CPPCompile::GenArithCoerceExpr(const Expr* e, GenType gt)
reporter->InternalError("bad type in arithmetic coercion"); reporter->InternalError("bad type in arithmetic coercion");
} }
if ( is_vec )
return string("vec_coerce_to_") + cast_name + "__CPP(" + GenExpr(op, GEN_NATIVE) + ", " +
GenTypeName(t) + ")";
return NativeToGT(cast_name + "(" + GenExpr(op, GEN_NATIVE) + ")", t, gt); return NativeToGT(cast_name + "(" + GenExpr(op, GEN_NATIVE) + ")", t, gt);
} }
@ -1115,10 +1111,12 @@ string CPPCompile::GenListAssign(const ExprPtr& lhs, const ExprPtr& rhs)
string CPPCompile::GenVectorOp(const Expr* e, string op, const char* vec_op) string CPPCompile::GenVectorOp(const Expr* e, string op, const char* vec_op)
{ {
auto gen = string("vec_op_") + vec_op + "__CPP(" + op + ")"; auto t = e->GetType();
auto gen_t = GenTypeName(t);
auto gen = string("vec_op_") + vec_op + "__CPP(" + op + ", " + gen_t + ")";
if ( ! IsArithmetic(e->GetType()->Yield()->Tag()) ) if ( ! IsArithmetic(t->Yield()->Tag()) )
gen = string("vector_coerce_to__CPP(") + gen + ", " + GenTypeName(e->GetType()) + ")"; gen = string("vector_coerce_to__CPP(") + gen + ", " + gen_t + ")";
return gen; return gen;
} }

View file

@ -47,8 +47,9 @@ static VectorTypePtr base_vector_type__CPP(const VectorTypePtr& vt)
#define VEC_OP1_KERNEL(accessor, type, op) \ #define VEC_OP1_KERNEL(accessor, type, op) \
for ( unsigned int i = 0; i < v->Size(); ++i ) \ for ( unsigned int i = 0; i < v->Size(); ++i ) \
{ \ { \
auto v_i = v->ValAt(i)->accessor(); \ auto v_i = v->ValAt(i); \
v_result->Assign(i, make_intrusive<type>(op v_i)); \ if ( v_i ) \
v_result->Assign(i, make_intrusive<type>(op v_i->accessor())); \
} }
// A macro (since it's beyond my templating skillz to deal with the // A macro (since it's beyond my templating skillz to deal with the
@ -58,9 +59,9 @@ static VectorTypePtr base_vector_type__CPP(const VectorTypePtr& vt)
// is "double". It needs to be optional because C++ will (rightfully) // is "double". It needs to be optional because C++ will (rightfully)
// complain about applying certain C++ unary operations to doubles. // complain about applying certain C++ unary operations to doubles.
#define VEC_OP1(name, op, double_kernel) \ #define VEC_OP1(name, op, double_kernel) \
VectorValPtr vec_op_##name##__CPP(const VectorValPtr& v) \ VectorValPtr vec_op_##name##__CPP(const VectorValPtr& v, const TypePtr& t) \
{ \ { \
auto vt = base_vector_type__CPP(v->GetType<VectorType>()); \ auto vt = base_vector_type__CPP(cast_intrusive<VectorType>(t)); \
auto v_result = make_intrusive<VectorVal>(vt); \ auto v_result = make_intrusive<VectorVal>(vt); \
\ \
switch ( vt->Yield()->InternalType() ) \ switch ( vt->Yield()->InternalType() ) \
@ -105,9 +106,10 @@ VEC_OP1(comp, ~, )
#define VEC_OP2_KERNEL(accessor, type, op) \ #define VEC_OP2_KERNEL(accessor, type, op) \
for ( unsigned int i = 0; i < v1->Size(); ++i ) \ for ( unsigned int i = 0; i < v1->Size(); ++i ) \
{ \ { \
auto v1_i = v1->ValAt(i)->accessor(); \ auto v1_i = v1->ValAt(i); \
auto v2_i = v2->ValAt(i)->accessor(); \ auto v2_i = v2->ValAt(i); \
v_result->Assign(i, make_intrusive<type>(v1_i op v2_i)); \ if ( v1_i && v2_i ) \
v_result->Assign(i, make_intrusive<type>(v1_i->accessor() op v2_i->accessor())); \
} }
// Analogous to VEC_OP1, instantiates a function for a given binary operation, // Analogous to VEC_OP1, instantiates a function for a given binary operation,
@ -387,27 +389,42 @@ VectorValPtr vector_coerce_to__CPP(const VectorValPtr& v, const TypePtr& targ)
for ( unsigned int i = 0; i < n; ++i ) for ( unsigned int i = 0; i < n; ++i )
{ {
ValPtr v_i = v->ValAt(i); ValPtr v_i = v->ValAt(i);
if ( ! v_i )
continue;
ValPtr r_i; ValPtr r_i;
switch ( ytag ) switch ( ytag )
{ {
case TYPE_BOOL: case TYPE_BOOL:
r_i = val_mgr->Bool(v_i->AsBool()); r_i = val_mgr->Bool(v_i->CoerceToInt() != 0);
break;
case TYPE_INT:
r_i = val_mgr->Int(v_i->CoerceToInt());
break;
case TYPE_COUNT:
r_i = val_mgr->Count(v_i->CoerceToUnsigned());
break; break;
case TYPE_ENUM: case TYPE_ENUM:
r_i = yt->AsEnumType()->GetEnumVal(v_i->AsInt()); r_i = yt->AsEnumType()->GetEnumVal(v_i->CoerceToInt());
break; break;
case TYPE_PORT: case TYPE_PORT:
r_i = make_intrusive<PortVal>(v_i->AsCount()); r_i = make_intrusive<PortVal>(v_i->CoerceToUnsigned());
break;
case TYPE_DOUBLE:
r_i = make_intrusive<DoubleVal>(v_i->CoerceToDouble());
break; break;
case TYPE_INTERVAL: case TYPE_INTERVAL:
r_i = make_intrusive<IntervalVal>(v_i->AsDouble()); r_i = make_intrusive<IntervalVal>(v_i->CoerceToDouble());
break; break;
case TYPE_TIME: case TYPE_TIME:
r_i = make_intrusive<TimeVal>(v_i->AsDouble()); r_i = make_intrusive<TimeVal>(v_i->CoerceToDouble());
break; break;
default: default:
@ -420,40 +437,10 @@ VectorValPtr vector_coerce_to__CPP(const VectorValPtr& v, const TypePtr& targ)
return v_result; return v_result;
} }
VectorValPtr vec_coerce_to_bro_int_t__CPP(const VectorValPtr& v, TypePtr targ) VectorValPtr vec_scalar_mixed_with_vector()
{ {
auto res_t = cast_intrusive<VectorType>(targ); reporter->CPPRuntimeError("vector-mixed-with-scalar operations not supported");
auto v_result = make_intrusive<VectorVal>(move(res_t)); return nullptr;
auto n = v->Size();
for ( unsigned int i = 0; i < n; ++i )
v_result->Assign(i, val_mgr->Int(v->IntAt(i)));
return v_result;
}
VectorValPtr vec_coerce_to_bro_uint_t__CPP(const VectorValPtr& v, TypePtr targ)
{
auto res_t = cast_intrusive<VectorType>(targ);
auto v_result = make_intrusive<VectorVal>(move(res_t));
auto n = v->Size();
for ( unsigned int i = 0; i < n; ++i )
v_result->Assign(i, val_mgr->Count(v->CountAt(i)));
return v_result;
}
VectorValPtr vec_coerce_to_double__CPP(const VectorValPtr& v, TypePtr targ)
{
auto res_t = cast_intrusive<VectorType>(targ);
auto v_result = make_intrusive<VectorVal>(move(res_t));
auto n = v->Size();
for ( unsigned int i = 0; i < n; ++i )
v_result->Assign(i, make_intrusive<DoubleVal>(v->DoubleAt(i)));
return v_result;
} }
} // namespace zeek::detail } // namespace zeek::detail

View file

@ -22,10 +22,10 @@ inline ValPtr vector_append__CPP(VectorValPtr v1, ValPtr v2)
} }
// Unary vector operations. // Unary vector operations.
extern VectorValPtr vec_op_pos__CPP(const VectorValPtr& v); extern VectorValPtr vec_op_pos__CPP(const VectorValPtr& v, const TypePtr& t);
extern VectorValPtr vec_op_neg__CPP(const VectorValPtr& v); extern VectorValPtr vec_op_neg__CPP(const VectorValPtr& v, const TypePtr& t);
extern VectorValPtr vec_op_not__CPP(const VectorValPtr& v); extern VectorValPtr vec_op_not__CPP(const VectorValPtr& v, const TypePtr& t);
extern VectorValPtr vec_op_comp__CPP(const VectorValPtr& v); extern VectorValPtr vec_op_comp__CPP(const VectorValPtr& v, const TypePtr& t);
// Binary vector operations. // Binary vector operations.
extern VectorValPtr vec_op_add__CPP(const VectorValPtr& v1, const VectorValPtr& v2); extern VectorValPtr vec_op_add__CPP(const VectorValPtr& v1, const VectorValPtr& v2);
@ -81,8 +81,7 @@ extern VectorValPtr vec_coerce_to_bro_uint_t__CPP(const VectorValPtr& v, TypePtr
extern VectorValPtr vec_coerce_to_double__CPP(const VectorValPtr& v, TypePtr targ); extern VectorValPtr vec_coerce_to_double__CPP(const VectorValPtr& v, TypePtr targ);
// A dummy function used during code generation for unsupported operations // A dummy function used during code generation for unsupported operations
// that mix vector and scalar arguments. We don't define it in RuntimeVec.cc // that mix vector and scalar arguments.
// so that it'll generate a linking error.
extern VectorValPtr vec_scalar_mixed_with_vector(); extern VectorValPtr vec_scalar_mixed_with_vector();
} // namespace zeek::detail } // namespace zeek::detail