diff --git a/tools/qfcc/include/target.h b/tools/qfcc/include/target.h index 2e9de6ce1..ce871da3e 100644 --- a/tools/qfcc/include/target.h +++ b/tools/qfcc/include/target.h @@ -51,6 +51,7 @@ typedef struct { const expr_t *(*proc_address) (const expr_t *expr, rua_ctx_t *ctx); // for both vector and quaternion types const expr_t *(*vector_compare)(int op, const expr_t *e1, const expr_t *e2); + const expr_t *(*shift_op)(int op, const expr_t *e1, const expr_t *e2); bool (*setup_intrinsic_symtab) (symtab_t *symtab); diff --git a/tools/qfcc/source/expr_binary.c b/tools/qfcc/source/expr_binary.c index 38e4e9c08..c7c1a7640 100644 --- a/tools/qfcc/source/expr_binary.c +++ b/tools/qfcc/source/expr_binary.c @@ -160,6 +160,12 @@ entity_compare (int op, const expr_t *e1, const expr_t *e2) return e; } +static const expr_t * +target_shift_op (int op, const expr_t *e1, const expr_t *e2) +{ + return current_target.shift_op (op, e1, e2); +} + static const type_t * promote_type (const type_t *dst, const type_t *src) { @@ -504,7 +510,8 @@ static expr_type_t compare_ops[] = { }; static expr_type_t shift_ops[] = { - { .match_a = is_math, .match_b = is_math, }, + { .match_a = is_math, .match_b = is_math, + .process = target_shift_op }, {} }; diff --git a/tools/qfcc/source/target_rua.c b/tools/qfcc/source/target_rua.c index b83dfe5fc..1ce2a2be6 100644 --- a/tools/qfcc/source/target_rua.c +++ b/tools/qfcc/source/target_rua.c @@ -370,6 +370,42 @@ ruamoko_vector_compare (int op, const expr_t *e1, const expr_t *e2) return e; } +static const expr_t * +ruamoko_shift_op (int op, const expr_t *e1, const expr_t *e2) +{ + auto t1 = get_type (e1); + auto t2 = get_type (e2); + + if (is_matrix (t1) || is_matrix (t2)) { + return error (e1, "invalid operands for %s", get_op_string (op)); + } + if (is_real (t1)) { + warning (e1, "shift of floating point value"); + } + if (is_real (t2)) { + warning (e2, "shift by floating point value"); + } + if (is_double (t1)) { + t1 = vector_type (&type_long, type_width (t1)); + t2 = vector_type (&type_long, type_width (t1)); + } + if (is_float (t1)) { + t1 = vector_type (&type_int, type_width (t1)); + t2 = vector_type (&type_int, type_width (t1)); + } + if (is_uint (t1) || is_int (t1)) { + t2 = vector_type (&type_int, type_width (t1)); + } + if (is_ulong (t1) || is_long (t1)) { + t2 = vector_type (&type_long, type_width (t1)); + } + e1 = cast_expr (t1, e1); + e2 = cast_expr (t2, e2); + auto e = new_binary_expr (op, e1, e2); + e->expr.type = t1; + return fold_constants (e); +} + target_t ruamoko_target = { .value_too_large = ruamoko_value_too_large, .build_scope = ruamoko_build_scope, @@ -381,4 +417,5 @@ target_t ruamoko_target = { .proc_caselabel = ruamoko_proc_caselabel, .proc_address = ruamoko_proc_address, .vector_compare = ruamoko_vector_compare, + .shift_op = ruamoko_shift_op, }; diff --git a/tools/qfcc/source/target_spirv.c b/tools/qfcc/source/target_spirv.c index 1447a5e31..893106e7e 100644 --- a/tools/qfcc/source/target_spirv.c +++ b/tools/qfcc/source/target_spirv.c @@ -2216,6 +2216,31 @@ spirv_vector_compare (int op, const expr_t *e1, const expr_t *e2) return e; } +static const expr_t * +spirv_shift_op (int op, const expr_t *e1, const expr_t *e2) +{ + auto t1 = get_type (e1); + auto t2 = get_type (e2); + + if (is_matrix (t1) || is_matrix (t2)) { + return error (e1, "invalid operands for %s", get_op_string (op)); + } + if (!is_integral (t1) || !is_integral (t2)) { + return error (e1, "invalid operands for %s", get_op_string (op)); + } + if (is_uint (t1)) { + t2 = vector_type (&type_int, type_width (t1)); + } + if (is_ulong (t1)) { + t2 = vector_type (&type_long, type_width (t1)); + } + e1 = cast_expr (t1, e1); + e2 = cast_expr (t2, e2); + auto e = new_binary_expr (op, e1, e2); + e->expr.type = t1; + return fold_constants (e); +} + target_t spirv_target = { .value_too_large = spirv_value_too_large, .build_scope = spirv_build_scope, @@ -2225,4 +2250,5 @@ target_t spirv_target = { .assign_vector = spirv_assign_vector, .setup_intrinsic_symtab = spirv_setup_intrinsic_symtab, .vector_compare = spirv_vector_compare, + .shift_op = spirv_shift_op, }; diff --git a/tools/qfcc/source/target_v6.c b/tools/qfcc/source/target_v6.c index 74fd63f64..278ced755 100644 --- a/tools/qfcc/source/target_v6.c +++ b/tools/qfcc/source/target_v6.c @@ -219,6 +219,41 @@ v6p_vector_compare (int op, const expr_t *e1, const expr_t *e2) return do_vector_compare (op, e1, e2, &type_int); } +static const expr_t * +v6_shift_op (int op, const expr_t *e1, const expr_t *e2) +{ + return error (e1, "shift ops not supported"); +} + +static const expr_t * +v6p_shift_op (int op, const expr_t *e1, const expr_t *e2) +{ + auto t1 = get_type (e1); + auto t2 = get_type (e2); + + if (!is_scalar (t1) || !is_scalar (t2)) { + return error (e1, "invalid operands for %s", get_op_string (op)); + } + if (is_double (t1) && !e1->implicit) { + warning (e1, "shift of double"); + } + if (is_double (t2) && !e2->implicit) { + warning (e2, "shift by double"); + } + const type_t *type; + if (is_real (t1)) { + e1 = cast_expr (&type_float, e1); + e2 = cast_expr (&type_float, e2); + type = &type_float; + } else { + e2 = cast_expr (&type_int, e2); + type = t1; + } + auto e = new_binary_expr (op, e1, e2); + e->expr.type = type; + return fold_constants (e); +} + target_t v6_target = { .value_too_large = v6_value_too_large, .build_scope = v6p_build_scope, @@ -231,6 +266,7 @@ target_t v6_target = { .proc_caselabel = ruamoko_proc_caselabel, .proc_address = ruamoko_proc_address, .vector_compare = v6_vector_compare, + .shift_op = v6_shift_op, }; target_t v6p_target = { @@ -245,4 +281,5 @@ target_t v6p_target = { .proc_caselabel = ruamoko_proc_caselabel, .proc_address = ruamoko_proc_address, .vector_compare = v6p_vector_compare, + .shift_op = v6p_shift_op, };