-- version control marker $Id: cgtest18.occ,v 1.4 1995/10/10 12:58:06 djb1 Exp $

PROC cgtest18 (CHAN OF BYTE keyb, scr, err)

  #INCLUDE "cgmain.occ"

  SEQ
    init ("CG test 18", "Alias and Usage checks")
    title("Usage checks")
    --{{{  pragmas
    PROC usage.pragmas()
      SEQ
        --{{{  SHARED x       U000 - U009
        INT x :
        #PRAGMA SHARED x
        SEQ
          x := 0
          CHAN OF INT c :
          PAR
            SEQ
              x := 99
              c ! 0
              c ! 0
              check(x, -99, "U001")
            INT any :
            SEQ
              c ? any
              x := -99
              c ? any
        --}}}
        --{{{  SHARED x, y    U010 - U019
        INT x,y :
        #PRAGMA SHARED x,y
        INT yy :
        SEQ
          x,y := 0,0
          CHAN OF INT c :
          PAR
            INT z :
            SEQ
              z := 0
              x := 99
              c ! 0
              z := y
              c ! 0
              y := -1
              c ! 0
              check(x, -99, "U010")
              check(z,  27, "U011")
            INT any :
            SEQ
              y := 27
              c ? any
              x := -99
              yy := y
              c ? any
              c ? any
          check(y,  -1, "U012")
          check(yy, 27, "U013")
        --}}}
        --{{{  SHARED channel U020 - U029
        CHAN OF INT c :
        #PRAGMA SHARED c
        CHAN OF INT d :
        PAR
          SEQ
            c ! 99
            d ! 0
            d ! 0
            c ! 88
          INT any :
          SEQ
            d ? any
            c ! 77
            d ? any
          INT x :
          SEQ
            c ? x
            check(x, 99, "U020")
            c ? x
            check(x, 77, "U021")
            c ? x
            check(x, 88, "U022")
        
        --}}}
        --{{{  SHARED array   U030 - U039
        [10]INT array :
        #PRAGMA SHARED array
        SEQ
          CHAN OF INT c :
          PAR
            SEQ
              SEQ i = 0 FOR SIZE array
                array[i] := i
              c ! 2
              c ! 2 -- synchronise
              array[2] := 99
              c ! 2
              c ! 2 -- synchronise
            INT i :
            SEQ
              c ? i
              check(array[i],  2, "U030")
              c ? i -- synchronise
              c ? i
              check(array[i], 99, "U031")
              c ? i -- synchronise
        
        --}}}
    :
    usage.pragmas()
    --}}}
    --{{{  bug 646
    PROC bug.646()
      PROC p ([]CHAN OF INT c)
        PAR i = 0 FOR 5
          c[i] ! i
      :
      PROC q ([5]CHAN OF INT c)
        PAR i = 0 FOR 5
          c[i] ! i
      :
      SEQ pass = 0 FOR 2
        [5]CHAN OF INT d :
        PAR
          CASE pass
            0
              p (d)
            1
              q (d)
          SEQ i = 0 FOR 5
            INT x :
            SEQ
              d[i] ? x
              check(x, i, "B646a")
    
    :
    bug.646()
    --}}}
    --{{{  bug 683
    PROC bug.683()
      PROC bounce(CHAN OF INT in, out)
        PAR
          out ! 87
          INT x :
          SEQ
            in ? x
            check(x, 87, "B683a")
      :
      CHAN OF INT c :
      CHAN OF INT d, e :
      INT y :
      SEQ
        y := 1
        bounce(c, c)
        ins IS [d, c] :
        outs IS [e, c] :
        bounce(ins[y], outs[y])
    :
    bug.683()
    --}}}
    --{{{  bug 1025
    PROC bug.1025()
      [2][2]INT data :
      SEQ
        SEQ i = 0 FOR SIZE data
          SEQ j = 0 FOR SIZE data[0]
            data[i][j] := (i*j) + 99
        [SIZE data][SIZE data[0]] CHAN OF INT c :
        PAR
          PAR i = 0 FOR SIZE data
            SEQ j = 0 FOR SIZE data
              c[i][j] ! (i*j) + 101
          PAR i = 0 FOR SIZE data
            SEQ j = 0 FOR SIZE data
              c[i][j] ? data[i][j]
        SEQ i = 0 FOR SIZE data
          SEQ j = 0 FOR SIZE data[0]
            check(data[i][j], (i*j) + 101, "B1025A")
    :
    bug.1025()
    --}}}
    --{{{  bug 1032
    PROC bug.1032()
      [2][2]INT a:
      INT x, y :
      SEQ
        SEQ i = 0 FOR SIZE a
          SEQ j = 0 FOR SIZE a[0]
            a[i][j] := 99+(i+j)
        x, y := 0, 1
        this IS a[x][0] :
        that IS a[y][0] :
        SEQ
          check(this,  99, "B1032a")
          check(that, 100, "B1032b")
    :
    bug.1032()
    --}}}
    --{{{  bug 1380
    PROC bug.1380()
      PROC p ([]CHAN OF INT c)
        PAR
          c[0] ! 0
          c[1] ! 1
      :
      [2]CHAN OF INT c :
      PAR
        p(c)
        INT x0, x1 :
        SEQ
          c[0] ? x0
          c[1] ? x1
          check(x0, 0, "B1380a")
          check(x1, 1, "B1380b")
    :
    bug.1380()
    --}}}
    title("Alias checks")
    --{{{  pragmas
    PROC alias.pragmas()
      SEQ
        --{{{  VAL        A000 - A009
        [5]INT a :
        SEQ
          SEQ j = 0 FOR SIZE a
            a[j] := j
          INT i :
          SEQ
            i := 0
            WHILE i < (SIZE a)
              VAL x IS a[i] :
              #PRAGMA PERMITALIASES x
              SEQ
                check(x, i, "A000")
                i := i + 1
        
        --}}}
        --{{{  VAL        A010 - A019
        [5]INT a :
        SEQ
          SEQ j = 0 FOR SIZE a
            a[j] := j
          INT i :
          SEQ
            i := 0
            WHILE i < (SIZE a)
              VAL x IS a[i] :
              #PRAGMA PERMITALIASES x
              SEQ
                check(x, i, "A010")
                a[i] := a[i] + 1
                i := i + 1
          INT i :
          SEQ
            i := 0
            WHILE i < (SIZE a)
              VAL x IS a[i] :
              #PRAGMA PERMITALIASES x
              SEQ
                check(x, i+1, "A011")
                i := i + 1
        --}}}
        --{{{  non-VAL    A020 - A029
        [5]INT a :
        SEQ
          INT i :
          SEQ
            i := 0
            WHILE i < (SIZE a)
              x IS a[i] :
              #PRAGMA PERMITALIASES x
              SEQ
                x := i
                i := i + 1
          INT i :
          SEQ
            i := 0
            WHILE i < (SIZE a)
              x IS a[i] :
              #PRAGMA PERMITALIASES x
              SEQ
                check(x, i, "A020")
                i := i + 1
        
        --}}}
        --{{{  non-VAL    A030 - A039
        [5]INT a :
        #PRAGMA PERMITALIASES a
        INT i, j :
        SEQ
          SEQ k = 0 FOR SIZE a
            a[k] := k
          i := 3
          j := a[i]
          x IS a[i] :
          SEQ
            check(x,     3, "A030")
            check(a[i],  3, "A031")
            a[j] := 27  -- modify array through a 'clashing' alias
            check(x,    27, "A032")
            check(a[i], 27, "A033")
            x := 99     -- modify array through the abbreviation
            check(x,    99, "A034")
            check(a[i], 99, "A035")
            check(a[j], 99, "A036")
            x := j      -- reset array back to original values
            SEQ k = 0 FOR SIZE a
              check(a[k], k, "A037")
            x := 27     -- make sure copyout is detected
            a[i] := j   -- reset array back to original values
          SEQ k = 0 FOR SIZE a
            check(a[k], k, "A038")
        
        --}}}
        --{{{  'pointer'  A040 - A049
        PROC local(INT x, y)
          #PRAGMA PERMITALIASES x, y
          SEQ
            x := 99
            y := 101
            check(x, 101, "A040")
        :
        [10]INT a :
        #PRAGMA PERMITALIASES a
        INT i, j :
        SEQ
          i := 8
          j := (i+i) - i
          local(a[i], a[j])
        --}}}
    :
    alias.pragmas()
    --}}}
    --{{{  bug 1563
    PROC bug.1563()
      SEQ
        INT32, INT32 FUNCTION swap.INT32s(VAL INT32 a, b) IS b, a :
        PROC swap.PROC1(INT32 res1, res2, VAL INT32 in1, in2)
          #PRAGMA PERMITALIASES res1, res2, in1, in2
          -- NOTE that in this example, res1 and res2 are aliased with in1, and in2
          -- ON PURPOSE!
          res1, res2 := swap.INT32s(in1, in2)
        :
        INT32 a, b :
        #PRAGMA PERMITALIASES a, b
        SEQ
          a, b := -1(INT32), 0(INT32)
    
          swap.PROC1(a, b, a, b)
    
          --ASSERT(a =   0(INT32) )
          --ASSERT(b = (-1(INT32)))
          check(INT a,  0, "B1563aa")
          check(INT b, -1, "B1563ab")
        INT64, INT64 FUNCTION swap.INT64s(VAL INT64 a, b) IS b, a :
        PROC swap.PROC1(INT64 res1, res2, VAL INT64 in1, in2)
          #PRAGMA PERMITALIASES res1, res2, in1, in2
          -- NOTE that in this example, res1 and res2 are aliased with in1, and in2
          -- ON PURPOSE!
          res1, res2 := swap.INT64s(in1, in2)
        :
        INT64 a, b :
        #PRAGMA PERMITALIASES a, b
        SEQ
          a, b := -1(INT64), 0(INT64)
    
          swap.PROC1(a, b, a, b)
    
          --ASSERT(a =   0(INT64) )
          --ASSERT(b = (-1(INT64)))
          check(INT a,  0, "B1563ac")
          check(INT b, -1, "B1563ad")
    :
    bug.1563()
    
    --}}}
    --{{{  bug INSdi02414
    PROC bug.INSdi02414()
      SEQ
        --{{{  basic test, REAL64
        PROC test1 (REAL64 verf, VAL INT32 running.time)
          VAL real.run IS REAL64 ROUND running.time:
          #PRAGMA PERMITALIASES real.run
          verf := real.run
        :
        INT32 running.time :
        REAL64 verf :
        SEQ
          running.time := id.INT32(99(INT32))
          test1(verf, running.time)
          check(INT ROUND verf, 99, "di02414a")
        --}}}
        --{{{  basic test, REAL32
        PROC test1 (REAL32 verf, VAL INT32 running.time)
          VAL real.run IS REAL32 ROUND running.time:
          #PRAGMA PERMITALIASES real.run
          verf := real.run
        :
        INT32 running.time :
        REAL32 verf :
        SEQ
          running.time := id.INT32(99(INT32))
          test1(verf, running.time)
          check(INT ROUND verf, 99, "di02414b")
        --}}}
        --{{{  user test, REAL64
        PROC test1 (REAL64 verf, VAL INT32 running.time)
          REAL64 FUNCTION f(VAL INT32 x) IS REAL64 ROUND (x+1(INT32)) :
          VAL real.run IS f(running.time):
          #PRAGMA PERMITALIASES real.run
          verf := real.run
        :
        INT32 running.time :
        REAL64 verf :
        SEQ
          running.time := id.INT32(99(INT32))
          test1(verf, running.time)
          check(INT ROUND verf, 100, "di02414c")
        --}}}
        --{{{  user test, REAL32
        PROC test1 (REAL32 verf, VAL INT32 running.time)
          REAL32 FUNCTION f(VAL INT32 x) IS REAL32 ROUND (x+1(INT32)) :
          VAL real.run IS f(running.time):
          #PRAGMA PERMITALIASES real.run
          verf := real.run
        :
        INT32 running.time :
        REAL32 verf :
        SEQ
          running.time := id.INT32(99(INT32))
          test1(verf, running.time)
          check(INT ROUND verf, 100, "di02414d")
        --}}}
    :
    bug.INSdi02414()
    --}}}
    finish ()
:
