Annotation of OpenXM_contrib2/asir2000/parse/struct.c, Revision 1.1
1.1 ! noro 1: /* $OpenXM: OpenXM/src/asir99/parse/struct.c,v 1.1.1.1 1999/11/10 08:12:34 noro Exp $ */
! 2: #include "ca.h"
! 3: #include "parse.h"
! 4: #include "comp.h"
! 5:
! 6: struct oSS oLSS;
! 7: SS LSS = &oLSS;
! 8:
! 9: void structdef(name,member)
! 10: char *name;
! 11: NODE member;
! 12: {
! 13: int i,j,k,type;
! 14: SDEF sdef,s;
! 15: NODE n,ms;
! 16: char *mname,*sname;
! 17:
! 18: for ( s = LSS->sa, i = 0; i < LSS->n; i++ )
! 19: if ( !strcmp(s[i].name,name) ) {
! 20: fprintf(stderr,"redeclaration of %s\n",name); break;
! 21: }
! 22: if ( !LSS->sa || ((i == LSS->n)&&(LSS->n==LSS->asize)) )
! 23: reallocarray((char **)&LSS->sa,(int *)&LSS->asize,(int *)&LSS->n,(int)sizeof(struct oSDEF));
! 24: sdef = &LSS->sa[i];
! 25: if ( i == LSS->n )
! 26: LSS->n++;
! 27: for ( i = 0, n = member; n; n = NEXT(n), i++ );
! 28: sdef->name = (char *)MALLOC(strlen(name)+1);
! 29: bcopy(name,sdef->name,strlen(name)+1);
! 30: for ( j = 0, n = member; n; n = NEXT(n) )
! 31: for ( ms = NEXT((NODE)BDY(n)); ms; ms = NEXT(ms), j++ );
! 32: sdef->n = j;
! 33: sdef->f = (struct oFIELD *)MALLOC(sdef->n*sizeof(struct oFIELD));
! 34: for ( j = 0, n = member; n; n = NEXT(n) ) {
! 35: ms = (NODE)BDY(n);
! 36: if ( sname = (char *)BDY(ms) ) {
! 37: for ( s = LSS->sa, k = 0; k < LSS->n; k++ )
! 38: if ( !strcmp(s[k].name,sname) )
! 39: break;
! 40: if ( k == LSS->n ) {
! 41: fprintf(stderr,"%s: undefined structure\n",sname); return;
! 42: } else
! 43: type = k;
! 44: } else
! 45: type = -1;
! 46: for ( ms = NEXT(ms); ms; ms = NEXT(ms), j++ ) {
! 47: mname = (char *)MALLOC(strlen(BDY(ms))+1);
! 48: bcopy(BDY(ms),mname,strlen(BDY(ms))+1);
! 49: sdef->f[j].name = mname; sdef->f[j].type = type;
! 50: }
! 51: }
! 52: }
! 53:
! 54: void setstruct(name,vars)
! 55: char *name;
! 56: NODE vars;
! 57: {
! 58: SDEF s;
! 59: int i;
! 60: NODE n;
! 61:
! 62: for ( s = LSS->sa, i = 0; i < LSS->n; i++ )
! 63: if ( !strcmp(s[i].name,name) )
! 64: break;
! 65: if ( i == LSS->n ) {
! 66: fprintf(stderr,"%s: undefined structure\n",name); return;
! 67: }
! 68: s = &LSS->sa[i];
! 69: for ( n = vars; n; n = NEXT(n) )
! 70: CPVS->va[(int)BDY(n)].type = i;
! 71: }
! 72:
! 73: void newstruct(type,rp)
! 74: int type;
! 75: COMP *rp;
! 76: {
! 77: NEWCOMP(*rp,LSS->sa[type].n); (*rp)->type = type;
! 78: }
! 79:
! 80: int structtoindex(name)
! 81: char *name;
! 82: {
! 83: SDEF s;
! 84: int i;
! 85:
! 86: for ( s = LSS->sa, i = 0; i < LSS->n; i++ )
! 87: if ( !strcmp(s[i].name,name) )
! 88: break;
! 89: if ( i == LSS->n ) {
! 90: fprintf(stderr,"%s: undefined structure\n",name); return -1; /* XXX */
! 91: } else
! 92: return i;
! 93: }
! 94:
! 95: int membertoindex(type,name)
! 96: int type;
! 97: char *name;
! 98: {
! 99: SDEF s;
! 100: FIELD f;
! 101: int i;
! 102:
! 103: s = &LSS->sa[type];
! 104: for ( f = s->f, i = 0; i < s->n; i++ )
! 105: if ( !strcmp(f[i].name,name) )
! 106: break;
! 107: if ( i == s->n ) {
! 108: fprintf(stderr,"structure has no member named `%s'\n",name);
! 109: return -1;
! 110: } else
! 111: return i;
! 112: }
! 113:
! 114: int indextotype(type,index)
! 115: int type,index;
! 116: {
! 117: return LSS->sa[type].f[index].type;
! 118: }
! 119:
! 120: int gettype(index)
! 121: unsigned int index;
! 122: {
! 123: if ( MSB & index )
! 124: return (int)GPVS->va[index&~MSB].type;
! 125: else
! 126: return (int)CPVS->va[index].type;
! 127: }
! 128:
! 129: int getcompsize(type)
! 130: int type;
! 131: {
! 132: return LSS->sa[type].n;
! 133: }
! 134:
! 135: void getmember(expr,memp)
! 136: FNODE expr;
! 137: Obj *memp;
! 138: {
! 139: int i;
! 140: FNODE root;
! 141: COMP t;
! 142: PV v0;
! 143: NODE2 mchain;
! 144:
! 145: root = (FNODE)FA1(expr); mchain = (NODE2)FA2(expr);
! 146: if ( ID(root) == I_PVAR ) {
! 147: i = (int)FA0(root);
! 148: v0 = i>=0?&CPVS->va[(unsigned int)i]:&GPVS->va[((unsigned int)i)&~MSB];
! 149: t = (COMP)v0->priv;
! 150: } else
! 151: t = (COMP)eval(root);
! 152: for ( ; mchain; mchain = NEXT(mchain) )
! 153: t = (COMP)(t->member[(int)BDY1(mchain)]);
! 154: *memp = (Obj)t;
! 155: }
! 156:
! 157: void getmemberp(expr,addrp)
! 158: FNODE expr;
! 159: Obj **addrp;
! 160: {
! 161: int i;
! 162: FNODE root;
! 163: COMP t;
! 164: PV v0;
! 165: COMP *addr;
! 166: NODE2 mchain;
! 167:
! 168: root = (FNODE)FA1(expr); mchain = (NODE2)FA2(expr);
! 169: if ( ID(root) == I_PVAR ) {
! 170: i = (int)FA0(root);
! 171: v0 = i>=0?&CPVS->va[(unsigned int)i]:&GPVS->va[((unsigned int)i)&~MSB];
! 172: addr = (COMP *)&v0->priv;
! 173: } else {
! 174: t = (COMP)eval(root);
! 175: addr = (COMP *)&t;
! 176: }
! 177: for ( ; mchain; mchain = NEXT(mchain) )
! 178: addr = (COMP *)&((*addr)->member[(int)BDY1(mchain)]);
! 179: *addrp = (Obj *)addr;
! 180: }
! 181:
! 182: void getarrayp(a,ind,addrp)
! 183: Obj a;
! 184: NODE ind;
! 185: Obj **addrp;
! 186: {
! 187: Obj len,row,col;
! 188: Obj *addr;
! 189:
! 190: switch ( OID(a) ) {
! 191: case O_VECT:
! 192: len = (Obj)BDY(ind);
! 193: if ( !rangecheck(len,((VECT)a)->len) )
! 194: error("getarrayp : Out of range");
! 195: else
! 196: addr = (Obj *)&(BDY((VECT)a)[QTOS((Q)len)]);
! 197: break;
! 198: case O_MAT:
! 199: row = (Obj)BDY(ind); col = (Obj)BDY(NEXT(ind));
! 200: if ( !rangecheck(row,((MAT)a)->row)
! 201: || !rangecheck(col,((MAT)a)->col) )
! 202: error("getarrayp : Out of range");
! 203: else
! 204: addr = (Obj *)&(BDY((MAT)a)[QTOS((Q)row)][QTOS((Q)col)]);
! 205: break;
! 206: default:
! 207: addr = 0;
! 208: break;
! 209: }
! 210: *addrp = addr;
! 211: }
! 212:
! 213: void memberofstruct(pexpr,name,exprp)
! 214: FNODE pexpr;
! 215: char *name;
! 216: FNODE *exprp;
! 217: {
! 218: NODE2 n,*np;
! 219: int type,ind;
! 220:
! 221: if ( pexpr->id != I_CAST ) {
! 222: if ( pexpr->id == I_PVAR )
! 223: pexpr = (FNODE)mkfnode(3,I_CAST,gettype((unsigned int)pexpr->arg[0]),pexpr,0);
! 224: else
! 225: error("memberofstruct : ???");
! 226: }
! 227: if ( !pexpr->arg[2] ) {
! 228: np = (NODE2 *)&(pexpr->arg[2]); type = (int)pexpr->arg[0];
! 229: } else {
! 230: for ( n = (NODE2)pexpr->arg[2]; NEXT(n); n = NEXT(n) );
! 231: np = &NEXT(n); type = (int)BDY2(n);
! 232: }
! 233: ind = membertoindex(type,name);
! 234: MKNODE2(*np,ind,indextotype(type,ind),0);
! 235: *exprp = pexpr;
! 236: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>