FX,

You're likely correct that the creation of an expression for
c_null_char is overkill.  I copied the code from gfc_conv_concat_op()
in trans-expr.cc to get the 'f_c_string(s) -> trim(s) // c_null_char'.
This was the motivation of creating a string.  If I look at the
-fdump-tree-orginal for

   s1 = 'abc   '
   s2 = f_c_string(s1)

I see (slightly edited to reduce noise)

    _string_trim (&tlen.0, (void **) &tstr.1, 6, &s1);
    D.4785 = (void *) __builtin_malloc (MAX_EXPR <(tlen.0 + 1), 1>);
    pstr.2 = (character(kind=1) *) D.4785;
    _concat_string (tlen.0+1, pstr.2, tlen.0, tstr.1, 1, &"z"[1]{lb: 1 sz: 1});

so the c_null_char (currently 'z') simply appears as a constant.

I keep your comment in mind when I polish the code.

-- 
steve



 
On Tue, Dec 10, 2024 at 11:15:10PM +0100, FX Coudert wrote:
> Hi Steve,
> 
> I think it’d be simpler not to create a front-end expression for the 
> c_null_char. Unless I’m mistaken, you only need it as an argument to pass to 
> build_call_expr_loc (input_location, gfor_fndecl_concat_string, …). You can 
> pass a pointer to the constant character, and the length is one. The variable 
> for length would be build_int_cst (gfc_charlen_type_node, 1). For the pointer 
> to the constant character, you would do:
> 
>   tree nulchar = gfc_character1_type_node(gfc_character1_type_node, 
> “nulchar”);
>   gfc_add_modify (&block, nulchar, build_zero_cst (gfc_character1_type_node));
>   tree pnulchar = gfc_build_addr_expr (NULL_TREE, nulchar);
> 
> Since the variable in this case is a known constant, there’s probably even a 
> better way. I just don’t remember enough, but others may help?
> 
> ---
> 
> Regarding your question, you are right: you want to build the two code blocks 
> (you already have the content), then use:   fold_build3_loc (input_location, 
> COND_EXPR, return_type, boolexpression, iftrue, iffalse)
> 
> You will find some logic like that in gfc_conv_intrinsic_minmaxloc() near the 
> comment "We switch to > or >= depending on the value of the BACK argument”. 
> You create ifblock and elseblock, once you have the code inside them, you 
> make them into trees with gfc_finish_block (becoming ifbody2 and elsebody2). 
> Then:
> 
>           tmp = fold_build3_loc (input_location, COND_EXPR, logical_type_node,
>                                  back, ifbody2, elsebody2);
> 
> 
> Hope this helps?
> 
> 
> Best,
> FX

-- 
Steve

Reply via email to