#!/bin/sh
#
# Copyright 1997, Silicon Graphics, Inc.
# ALL RIGHTS RESERVED
# 
# UNPUBLISHED -- Rights reserved under the copyright laws of the United
# States.   Use of a copyright notice is precautionary only and does not
# imply publication or disclosure.
# 
# U.S. GOVERNMENT RESTRICTED RIGHTS LEGEND:
# Use, duplication or disclosure by the Government is subject to restrictions
# as set forth in FAR 52.227.19(c)(2) or subparagraph (c)(1)(ii) of the Rights
# in Technical Data and Computer Software clause at DFARS 252.227-7013 and/or
# in similar or successor clauses in the FAR, or the DOD or NASA FAR
# Supplement.  Contractor/manufacturer is Silicon Graphics, Inc.,
# 2011 N. Shoreline Blvd. Mountain View, CA 94039-7311.
# 
# THE CONTENT OF THIS WORK CONTAINS CONFIDENTIAL AND PROPRIETARY
# INFORMATION OF SILICON GRAPHICS, INC. ANY DUPLICATION, MODIFICATION,
# DISTRIBUTION, OR DISCLOSURE IN ANY FORM, IN WHOLE, OR IN PART, IS STRICTLY
# PROHIBITED WITHOUT THE PRIOR EXPRESS WRITTEN PERMISSION OF SILICON
# GRAPHICS, INC.
#
# Generate evaluator functions from skeletons.
#
# $Id: meta,v 1.1 1999/04/28 10:06:17 kenmcd Exp $

##############
# procedures #
##############

_fetch()
{
fin=fetch.sk
cat $fin >> $fout
}

_misc()
{
fin=misc.sk
sed -e "s/@ITYPE/$itype/g" \
    -e "s/@OTYPE/$otype/g" \
    $fin >> $fout
}

_aggr()
{
fin=aggregate.sk
sed -e "s/@FUN/$fun/g" \
    -e "s/@ITYPE/$itype/g" \
    -e "s/@OTYPE/$otype/g" \
    -e "s/@TOP/$top/g" \
    -e "s/@LOOP/$loop/g" \
    -e "s/@BOT/$bot/g" \
    -e "s/@NOTVALID/$notvalid/g" \
    $fin >> $fout
}

_unary()
{
fin=unary.sk
sed -e "s/@FUN/$fun/g" \
    -e "s/@ITYPE/$itype/g" \
    -e "s/@OTYPE/$otype/g" \
    -e "s/@OP/$op/g" \
    $fin >> $fout
}

_binary()
{
fin=binary.sk
sed -e "s/@FUN/$fun/g" \
    -e "s/@ITYPE/$itype/g" \
    -e "s/@OTYPE/$otype/g" \
    -e "s/@OP/$op/g" \
    $fin >> $fout
}

_merge()
{
fin=merge.sk
if [ -z "$scale" ]
then
    sed -e '/RealTime/d' $fin
else
    cat $fin
fi \
| sed -e "s/@FUN/$fun/g" \
    -e "s/@ITYPE/$itype/g" \
    -e "s/@OTYPE/$otype/g" \
    -e "s/@OP/$op/g" \
    -e "s/@DELTA/$delta/g" \
    -e "s/@SCALE/$scale/g" \
    >> $fout
}

_act()
{
fin=act.sk
cat $fin >> $fout
}



########
# main #
########

fout=fun.c
rm -f $fout
cat hdr.sk > $fout


#
# fetch
#
_fetch

#
# rule and delay
#
itype=double
otype=double

_misc

#
# aggregation operators
#
itype=double
otype=double
notvalid="x->valid = 0;"

fun=cndSum
top="a = *ip;"
loop="a += *ip;"
bot="*op++ = a;"
_aggr

fun=cndAvg
top="a = *ip;"
loop="a += *ip;"
bot="*op++ = a \/ n;"
_aggr

fun=cndMax
top="a = *ip;"
loop="if (*ip > a) a = *ip;"
bot="*op++ = a;"
_aggr

fun=cndMin
top="a = *ip;"
loop="if (*ip < a) a = *ip;"
bot="*op++ = a;"
_aggr

#
# arithmetic operators
#
itype=double
otype=double

fun=cndNeg
op="OP(x) -(x)"
_unary

fun=cndAdd
op="OP(x,y) ((x) + (y))"
_binary

fun=cndSub
op="OP(x,y) ((x) - (y))"
_binary

fun=cndMul
op="OP(x,y) ((x) * (y))"
_binary

fun=cndDiv
op="OP(x,y) ((x) \/ (y))"
_binary

fun=cndRate
delta="delta = is1->stamp - is2->stamp;"
op="-"
scale="*op = *op \\/ delta;"
_merge

#
# relational operators
#
itype=double
otype=Truth

fun=cndEq
op="OP(x,y) ((x) == (y))"
_binary

fun=cndNeq
op="OP(x,y) ((x) != (y))"
_binary

fun=cndLt
op="OP(x,y) ((x) < (y))"
_binary

fun=cndLte
op="OP(x,y) ((x) <= (y))"
_binary

fun=cndGt
op="OP(x,y) ((x) > (y))"
_binary

fun=cndGte
op="OP(x,y) ((x) >= (y))"
_binary

#
# boolean connectives
#
itype=Truth
otype=Truth

fun=cndNot
op="OP(x) (((x) == TRUE || (x) == FALSE) ? !(x) : DUNNO)"
_unary

fun=cndAnd
op="OP(x,y) (((x) == TRUE \\&\\& (y) == TRUE) ? TRUE : (((x) == FALSE || (y) == FALSE) ?  FALSE : DUNNO))"
_binary

fun=cndOr
op="OP(x,y) (((x) == TRUE || (y) == TRUE) ? TRUE : (((x) == FALSE \\&\\& (y) == FALSE) ?  FALSE : DUNNO))"
_binary

fun=cndRise
delta=""
op=">"
scale=""
_merge

fun=cndFall
delta=""
op="<"
scale=""
_merge

#
# quantifiers
#
itype=Truth
otype=Truth

fun=cndAll
top="a = *ip;"
loop="if (*ip == FALSE) a = FALSE;\\
		else if (*ip == DUNNO \\&\\& a != DUNNO) a = DUNNO;"
bot="*op++ = a;"
notvalid="*op++ = DUNNO; os->stamp = is->stamp; x->valid++;"
_aggr

fun=cndSome
top="a = *ip;"
loop="if (*ip == TRUE) a = TRUE;\\
		else if (*ip == DUNNO \\&\\& a != DUNNO) a = DUNNO;"
bot="*op++ = a;"
notvalid="*op++ = DUNNO; os->stamp = is->stamp; x->valid++;"
_aggr

fun=cndPcnt
top="a = *ip;"
loop="a += *ip;"
bot="*op++ = (a >= *(double *)x->arg2->ring * n) ? TRUE : FALSE;"
notvalid="*op++ = DUNNO; os->stamp = is->stamp; x->valid++;"
_aggr

#
# truth counter
#
itype=Truth
otype=double
notvalid="x->valid = 0;"

fun=cndCount
top="a = *ip;"
loop="a += *ip;"
bot="*op++ = a;"
_aggr

#
# actions
#
_act

# discourage changes to fun.c
#
chmod 444 $fout
