Fix comparison with integer literals
This commit is contained in:
parent
e2ddd23b55
commit
81316e0d97
@ -2630,19 +2630,21 @@ CORD compile(env_t *env, ast_t *ast)
|
|||||||
type_t *lhs_t = get_type(env, cmp.lhs);
|
type_t *lhs_t = get_type(env, cmp.lhs);
|
||||||
type_t *rhs_t = get_type(env, cmp.rhs);
|
type_t *rhs_t = get_type(env, cmp.rhs);
|
||||||
type_t *operand_t;
|
type_t *operand_t;
|
||||||
CORD lhs, rhs;
|
if (cmp.lhs->tag == Int && is_numeric_type(rhs_t)) {
|
||||||
if (can_compile_to_type(env, cmp.rhs, lhs_t)) {
|
operand_t = rhs_t;
|
||||||
lhs = compile(env, cmp.lhs);
|
} else if (cmp.rhs->tag == Int && is_numeric_type(lhs_t)) {
|
||||||
rhs = compile_to_type(env, cmp.rhs, lhs_t);
|
operand_t = lhs_t;
|
||||||
|
} else if (can_compile_to_type(env, cmp.rhs, lhs_t)) {
|
||||||
operand_t = lhs_t;
|
operand_t = lhs_t;
|
||||||
} else if (can_compile_to_type(env, cmp.lhs, rhs_t)) {
|
} else if (can_compile_to_type(env, cmp.lhs, rhs_t)) {
|
||||||
rhs = compile(env, cmp.rhs);
|
|
||||||
lhs = compile_to_type(env, cmp.lhs, rhs_t);
|
|
||||||
operand_t = rhs_t;
|
operand_t = rhs_t;
|
||||||
} else {
|
} else {
|
||||||
code_err(ast, "I can't do comparisons between ", type_to_str(lhs_t), " and ", type_to_str(rhs_t));
|
code_err(ast, "I can't do comparisons between ", type_to_str(lhs_t), " and ", type_to_str(rhs_t));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CORD lhs = compile_to_type(env, cmp.lhs, operand_t);
|
||||||
|
CORD rhs = compile_to_type(env, cmp.rhs, operand_t);
|
||||||
|
|
||||||
if (ast->tag == Compare)
|
if (ast->tag == Compare)
|
||||||
return CORD_all("generic_compare(stack(", lhs, "), stack(", rhs, "), ",
|
return CORD_all("generic_compare(stack(", lhs, "), stack(", rhs, "), ",
|
||||||
compile_type_info(operand_t), ")");
|
compile_type_info(operand_t), ")");
|
||||||
|
@ -1149,22 +1149,14 @@ type_t *get_type(env_t *env, ast_t *ast)
|
|||||||
}
|
}
|
||||||
code_err(ast, "I couldn't figure out how to do `xor` between ", type_to_str(lhs_t), " and ", type_to_str(rhs_t));
|
code_err(ast, "I couldn't figure out how to do `xor` between ", type_to_str(lhs_t), " and ", type_to_str(rhs_t));
|
||||||
}
|
}
|
||||||
case Compare: {
|
case Compare:
|
||||||
binary_operands_t binop = BINARY_OPERANDS(ast);
|
|
||||||
type_t *lhs_t = get_type(env, binop.lhs);
|
|
||||||
type_t *rhs_t = get_type(env, binop.rhs);
|
|
||||||
|
|
||||||
if (can_promote(rhs_t, lhs_t) || can_promote(lhs_t, rhs_t))
|
|
||||||
return Type(IntType, .bits=TYPE_IBITS32);
|
|
||||||
|
|
||||||
code_err(ast, "I don't know how to compare ", type_to_str(lhs_t), " and ", type_to_str(rhs_t));
|
|
||||||
}
|
|
||||||
case Equals: case NotEquals: case LessThan: case LessThanOrEquals: case GreaterThan: case GreaterThanOrEquals: {
|
case Equals: case NotEquals: case LessThan: case LessThanOrEquals: case GreaterThan: case GreaterThanOrEquals: {
|
||||||
binary_operands_t binop = BINARY_OPERANDS(ast);
|
binary_operands_t binop = BINARY_OPERANDS(ast);
|
||||||
type_t *lhs_t = get_type(env, binop.lhs);
|
type_t *lhs_t = get_type(env, binop.lhs);
|
||||||
type_t *rhs_t = get_type(env, binop.rhs);
|
type_t *rhs_t = get_type(env, binop.rhs);
|
||||||
|
|
||||||
if (can_promote(rhs_t, lhs_t) || can_promote(lhs_t, rhs_t))
|
if (can_promote(rhs_t, lhs_t) || can_promote(lhs_t, rhs_t))
|
||||||
return Type(BoolType);
|
return ast->tag == Compare ? Type(IntType, .bits=TYPE_IBITS32) : Type(BoolType);
|
||||||
|
|
||||||
code_err(ast, "I don't know how to compare ", type_to_str(lhs_t), " and ", type_to_str(rhs_t));
|
code_err(ast, "I don't know how to compare ", type_to_str(lhs_t), " and ", type_to_str(rhs_t));
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user