From 60a3da27fef24089ae0b77cc2a006c46419882a5 Mon Sep 17 00:00:00 2001 From: Ian Lance Taylor Date: Thu, 16 Feb 2012 22:10:26 +0000 Subject: compiler: Lower constant string comparisons. From-SVN: r184316 --- gcc/go/gofrontend/expressions.cc | 39 +++++++++++++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 4 deletions(-) (limited to 'gcc') diff --git a/gcc/go/gofrontend/expressions.cc b/gcc/go/gofrontend/expressions.cc index 0b53f72..20c49f1 100644 --- a/gcc/go/gofrontend/expressions.cc +++ b/gcc/go/gofrontend/expressions.cc @@ -5824,15 +5824,46 @@ Binary_expression::do_lower(Gogo* gogo, Named_object*, } // String constant expressions. - if (op == OPERATOR_PLUS - && left->type()->is_string_type() - && right->type()->is_string_type()) + if (left->type()->is_string_type() && right->type()->is_string_type()) { std::string left_string; std::string right_string; if (left->string_constant_value(&left_string) && right->string_constant_value(&right_string)) - return Expression::make_string(left_string + right_string, location); + { + if (op == OPERATOR_PLUS) + return Expression::make_string(left_string + right_string, + location); + else if (is_comparison) + { + int cmp = left_string.compare(right_string); + bool r; + switch (op) + { + case OPERATOR_EQEQ: + r = cmp == 0; + break; + case OPERATOR_NOTEQ: + r = cmp != 0; + break; + case OPERATOR_LT: + r = cmp < 0; + break; + case OPERATOR_LE: + r = cmp <= 0; + break; + case OPERATOR_GT: + r = cmp > 0; + break; + case OPERATOR_GE: + r = cmp >= 0; + break; + default: + go_unreachable(); + } + return Expression::make_boolean(r, location); + } + } } // Special case for shift of a floating point constant. -- cgit v1.1