An old patch: [Fwd: Re: [G95-develop] Expression reordering]

Tobias Schlüter tobias.schlueter@physik.uni-muenchen.de
Fri Mar 26 17:27:00 GMT 2004


Hi all,

I've seen that the subject of reassociation is being heavily discussed 
on the gcc mailing list. I'd like to remind you of a patch I posted 1.5 
years ago, which makes gfortran frontend aware of parentheses. Back then 
Andy didn't like it, but I think it might appear in a different light 
given the ongoing discussion. (Some people have already remarked that it 
would be a severe short-coming for a Fortran compiler not to be able to 
perform reassociation) There had been some discussion back then in which 
I elaborated why I think this is necessary, I'll reiterate briefly:
Currently gfortran parse an expression like
    C = A + B + C
as:
    (assign c (+ (+ a b) c))
which is of course the same parse tree as for
    C = (A + B) + C

Since the compiler can't tell the difference, it can't apply the set of 
transformations which is legal for the former, but not the latter 
expression.
My patch cures this shortcoming by explicitly recording if an expression 
is in parentheses.

A more elaborate discussion can be found in the thread titled 
"expression reordering" which took place around Oct 18th 2002 on the 
g95-develop list.

Below is my old mail with the then-correct patch, I guess it won't apply 
cleanly to today's sources, due to formatting changes. I don't have a 
copyright assignment in place for gcc, so I'm not changing this, as this 
old code, being a contribution to g95, is covered by my g95 copyright 
assignment, and I'd rather be safe than sorry.

If you think it's safe, I'll gladly post a version updated to apply 
cleanly to today's sources.

A few remarks about the patch:
1) it creates a new expression type, INTRINSIC_PARENTHESES (rth 
suggested a name along the lines of INTRINSIC_NON_ASSOC back then, I 
guess I like his suggestion better)
2) the middleend parts are missing. Originally I thought translating 
INTRINSIC_PARENTHESES to a BIND_EXPR would do the trick, but the 
discussion on the gcc list makes it look like gcc doesn't really have 
any functionality geared towards this

Enjoy,
- Tobi

-------- Original Message --------
Subject: Re: [G95-develop] Expression reordering
Date: Fri, 18 Oct 2002 22:22:38 +0200
From: Tobias Schlüter <Tobias.Schlueter@physik.uni-muenchen.de>
To: Tobias Schlüter <Tobias.Schlueter@physik.uni-muenchen.de>
CC: g95-develop@lists.sourceforge.net
References: <3DB0223F.24233.2CD683D1@localhost>


A small update to my patch. Applying the appended patch to matchexp.c
instead of the one before will make g95 remember parentheses only for
numerical expressions, as mandated by 7.1.7.2 in the Oct 97 draft
standard.

- Tobi

--- matchexp.c	23 Jun 2002 22:46:32 -0000	1.18
+++ matchexp.c	18 Oct 2002 20:17:26 -0000
@@ -113,6 +113,8 @@

  static match match_primary(g95_expr **result) {
  match m;
+g95_expr *e1, *e2;
+locus where;

    m = g95_match_literal_constant(result, 0);
    if (m != MATCH_NO) return m;
@@ -125,15 +127,35 @@

  /* Match an expression in parenthesis */

+  e1 = e2 = NULL;
+  where = *g95_current_locus();
    if (g95_match_char('(') != MATCH_YES) return MATCH_NO;

-  m = g95_match_expr(result);
+  m = g95_match_expr(&e1);
    if (m == MATCH_NO) goto syntax;
    if (m == MATCH_ERROR) return m;

    m = g95_match_char(')');
    if (m == MATCH_NO)
      g95_error("Expected a right parenthesis in expression at %C");
+
+  /* now we have the expression inside the parentheses, build the
+   * expression pointing to it. By 7.1.7.2 the integrity of
+   * parentheses is only conserved in numerical calculations, so we
+   * don't bother to keep the parentheses otherwise */
+  if(!g95_numeric_ts(&e1->ts))
+    *result = e1;
+  else {
+    e2 = g95_get_expr();
+    e2->expr_type = EXPR_OP;
+    e2->ts        = e1->ts;
+    e2->rank      = e1->rank;
+    e2->operator  = INTRINSIC_PARENTHESES;
+    e2->where     = where;
+    e2->op1       = e1;
+    e2->op2       = NULL;
+    *result = e2;
+  }

    if (m != MATCH_YES) {
      g95_free_expr(*result);



-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf
_______________________________________________
G95-develop mailing list
G95-develop@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/g95-develop



More information about the Fortran mailing list