From e9cd3e5d0157f145dc0c3fea59f434fdd5ae55c6 Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Sat, 1 Aug 2026 20:03:34 +0200 Subject: Evaluate strings at compile time --- gcc/gcc/elna-generic.cc | 19 ++----------------- gcc/gcc/elna-tree.cc | 38 +++++++++++++++++++------------------- 2 files changed, 21 insertions(+), 36 deletions(-) (limited to 'gcc') diff --git a/gcc/gcc/elna-generic.cc b/gcc/gcc/elna-generic.cc index 969e929..e59a438 100644 --- a/gcc/gcc/elna-generic.cc +++ b/gcc/gcc/elna-generic.cc @@ -515,24 +515,9 @@ namespace elna::gcc void generic_visitor::visit(boot::literal *string) { - tree index_constant = build_int_cstu(elna_word_type_node, string->value.size()); - tree char_array_type = build_array_type(elna_char_type_node, build_index_type(index_constant)); + tree slice_type = get_inner_alias(string->type_decoration, symbols); - tree string_literal = build_string(string->value.size(), string->value.c_str()); - TREE_TYPE(string_literal) = char_array_type; - TREE_CONSTANT(string_literal) = 1; - TREE_READONLY(string_literal) = 1; - TREE_STATIC(string_literal) = 1; - - tree slice_type = get_inner_alias(string->type_decoration, this->symbols); - tree ptr_field = TYPE_FIELDS(slice_type); - - tree ptr_type = TREE_TYPE(ptr_field); - string_literal = build4(ARRAY_REF, elna_char_type_node, - string_literal, integer_zero_node, NULL_TREE, NULL_TREE); - string_literal = build1(ADDR_EXPR, ptr_type, string_literal); - - this->current_expression = build_slice(slice_type, string_literal, index_constant); + this->current_expression = constant_to_tree(boot::constant_value{ string->value }, this->symbols, slice_type); } void generic_visitor::visit(boot::traits_expression *trait) diff --git a/gcc/gcc/elna-tree.cc b/gcc/gcc/elna-tree.cc index 6aaf6f9..addb686 100644 --- a/gcc/gcc/elna-tree.cc +++ b/gcc/gcc/elna-tree.cc @@ -63,17 +63,6 @@ namespace elna::gcc return RECORD_OR_UNION_TYPE_P(type) || TREE_CODE(type) == ENUMERAL_TYPE; } - bool is_void_type(tree type) - { - return type == NULL_TREE || type == void_type_node; - } - - bool is_castable_type(tree type) - { - gcc_assert(TYPE_P(type)); - return INTEGRAL_TYPE_P(type) || POINTER_TYPE_P(type) || TREE_CODE(type) == REAL_TYPE; - } - tree prepare_rvalue(tree rvalue) { if (DECL_P(rvalue) && TREE_CODE(TREE_TYPE(rvalue)) == FUNCTION_TYPE) @@ -328,15 +317,26 @@ namespace elna::gcc { return null_pointer_node; } - else if (std::holds_alternative(constant_value)) + else if (std::holds_alternative(constant_value)) { - const auto& address = std::get(constant_value); - tree decl = symbols->lookup(address.name); - if (decl == NULL_TREE) - { - return NULL_TREE; - } - return build1(ADDR_EXPR, build_pointer_type(TREE_TYPE(decl)), decl); + const auto& string_value = std::get(constant_value); + tree index_constant = build_int_cstu(elna_word_type_node, string_value.size()); + tree char_array_type = build_array_type(elna_char_type_node, build_index_type(index_constant)); + + tree string_literal = build_string(string_value.size(), string_value.c_str()); + TREE_TYPE(string_literal) = char_array_type; + TREE_CONSTANT(string_literal) = 1; + TREE_READONLY(string_literal) = 1; + TREE_STATIC(string_literal) = 1; + + tree ptr_field = TYPE_FIELDS(type); + + tree ptr_type = TREE_TYPE(ptr_field); + string_literal = build4(ARRAY_REF, elna_char_type_node, + string_literal, integer_zero_node, NULL_TREE, NULL_TREE); + string_literal = build1(ADDR_EXPR, ptr_type, string_literal); + + return build_slice(type, string_literal, index_constant); } else if (std::holds_alternative>(constant_value)) { -- cgit v1.2.3