Compilation error involving comparison and logical operator
David Malcolm
dmalcolm@redhat.com
Thu Jan 1 00:00:00 GMT 2015
On Thu, 2015-06-25 at 23:46 +0100, Dibyendu Majumdar wrote:
> Hi Dave,
>
> I am getting an error in an expression such as this:
>
> int bvalue_0_2;
> bvalue_0_2 = &base[(int)0]->tt_ == (int)1 && &base[(int)0]->value_.b == (int)0;
>
> Of course the expression above really is:
>
> bvalue_0_2 = ((&base[(int)0]->tt_ == (int)1) &&
> (&base[(int)0]->value_.b == (int)0));
>
> The error I get is this:
>
> libgccjit.so: error: gcc_jit_block_add_assignment: mismatching types:
> assignment to bvalue_0_2 (type: int) from &base[(int)0]->tt_ == (int)0
> (type: bool)
Unlike C, you need to explicitly cast from bool to int
using gcc_jit_context_new_cast, see e.g.
https://gcc.gnu.org/onlinedocs/jit/intro/tutorial04.html which has this
for a BINARY_COMPARE_LT bytecode in a stack-based VM, which pops two
ints x and y and pushes (x < y) in int form:
case BINARY_COMPARE_LT:
Y_EQUALS_POP ();
X_EQUALS_POP ();
PUSH_RVALUE (
/* cast of bool to int */
gcc_jit_context_new_cast (
state.ctxt,
loc,
/* (x < y) as a bool */
gcc_jit_context_new_comparison (
state.ctxt,
loc,
GCC_JIT_COMPARISON_LT,
gcc_jit_lvalue_as_rvalue (state.x),
gcc_jit_lvalue_as_rvalue (state.y)),
state.int_type));
break;
> I notice that the error message doesn't show the full expression above
> - is this just an issue with the dump or is there a problem with the
> expression evaluation?
Difficult to say without seeing the code (e.g. via
gcc_jit_context_dump_reproducer_to_file). My guess is that the
expression you've constructed is not what you think it is, and that
that's what's causing the issue.
> Also, I am confused by the error as I am not sure if this means I need
> to store the result of the expression in a bool value. In fact if I do
> that then:
>
> libgccjit.so: error: gcc_jit_block_add_assignment: mismatching types:
> assignment to bvalue_0_2 (type: bool) from &base[(int)0]->tt_ ==
> (int)1 && &base[(int)0]->value_.b == (int)0 (type: int)
>
> Now the full expression is shown but the error is different.
> I will send you dumps tomorrow for this but if you could let me know
> what the rules are for assigning boolean values to variables that
> would be great.
As noted above, you need to use gcc_jit_context_new_cast when assigning
between int and bool types. See:
https://gcc.gnu.org/onlinedocs/jit/topics/expressions.html#gcc_jit_context_new_cast
Hope this makes sense
Dave
More information about the Jit
mailing list