📄 match.c
字号:
/* resources are available */#ifndef BUG1 if (qdebug) { /* vdp003- print out code template matched - * it's not pretty, but it helps */ printf("Template matched: "); printf("%s", opst[q->op]); printf(" visit: "); /*, q->visit);*/ prcook(q->visit); printf("\nleft shape - %o left type - %o\n", q->lshape, q->ltype); printf("right shape - %o right type - %o\n", q->rshape, q->rtype); printf("needs - %o rewrite -%o\n",q->rewrite); printf("cstring - %s\n", q->cstring); }#endif BUG1 expand( p, cookie, q->cstring ); /* generate code */ reclaim( p, q->rewrite, cookie ); return(MDONE); } return(MNOPE); }int rtyflg = 0;expand( p, cookie, cp ) NODE *p; register char *cp; { /* generate code by interpreting table entry */ register char c; CONSZ val; rtyflg = 0; for( ; *cp; ++cp ){ switch( *cp ){ default: PUTCHAR( *cp ); continue; /* this is the usual case... */ case 'R': /* RAP002 Put out the asm information that was saved in scan.c if this is a call to asm. */ if ( !strcmp (p->in.left->in.name,"_asm") ){ outasm(p->in.left->in.asminfo); return; } continue; case 'T': /* rewrite register type is suppressed */ rtyflg = 1; continue; case 'Z': /* special machine dependent operations */# ifdef NEWZZZ switch( c = *++cp ) { case '1': case '2': case '3': case 'R': case 'L': /* get down first */ zzzcode( getlr( p, c ), *++cp ); break; default: /* normal zzzcode processing otherwise */ zzzcode( p, c ); break; }# else zzzcode( p, *++cp );# endif continue; case 'F': /* this line deleted if FOREFF is active */ if( cookie & FOREFF ) while( *++cp != '\n' ) ; /* VOID */ continue; case 'S': /* field size */ printf( "%d", fldsz ); continue; case 'H': /* field shift */ printf( "%d", fldshf ); continue; case 'M': /* field mask */ case 'N': /* complement of field mask */ val = 1; val <<= fldsz; --val; val <<= fldshf; adrcon( *cp=='M' ? val : ~val ); continue; case 'L': /* output special label field */ printf( "%d", p->bn.label ); continue; case 'O': /* opcode string */ hopcode( *++cp, p->in.op ); continue; case 'B': /* byte offset in word */ val = getlr(p,*++cp)->tn.lval; val = BYTEOFF(val); printf( CONFMT, val ); continue; case 'C': /* for constant value only */ conput( getlr( p, *++cp ) ); continue; case 'I': /* in instruction */ insput( getlr( p, *++cp ) ); continue; case 'A': /* address of */ adrput( getlr( p, *++cp ) ); continue; case 'U': /* for upper half of address, only */ upput( getlr( p, *++cp ) ); continue; } } }NODE *getlr( p, c ) NODE *p; { /* return the pointer to the left or right side of p, or p itself, depending on the optype of p */ switch( c ) { case '1': case '2': case '3': return( &resc[c-'1'] ); case 'L': return( optype( p->in.op ) == LTYPE ? p : p->in.left ); case 'R': return( optype( p->in.op ) != BITYPE ? p : p->in.right ); } cerror( "bad getlr: %c", c ); /* NOTREACHED */ }# ifdef MULTILEVELunion mltemplate{ struct ml_head{ int tag; /* identifies class of tree */ int subtag; /* subclass of tree */ union mltemplate * nexthead; /* linked by mlinit() */ } mlhead; struct ml_node{ int op; /* either an operator or op description */ int nshape; /* shape of node */ /* both op and nshape must match the node. * where the work is to be done entirely by * op, nshape can be SANY, visa versa, op can * be OPANY. */ int ntype; /* type descriptor from mfile2 */ } mlnode; };# define MLSZ 30extern union mltemplate mltree[];int mlstack[MLSZ];int *mlsp; /* pointing into mlstack */NODE * ststack[MLSZ];NODE **stp; /* pointing into ststack */mlinit(){ union mltemplate **lastlink; register union mltemplate *n; register mlop; lastlink = &(mltree[0].nexthead); n = &mltree[0]; for( ; (n++)->mlhead.tag != 0; *lastlink = ++n, lastlink = &(n->mlhead.nexthead) ){# ifndef BUG3 if( vdebug )printf("mlinit: %d\n",(n-1)->mlhead.tag);# endif /* wander thru a tree with a stack finding * its structure so the next header can be located. */ mlsp = mlstack; for( ;; ++n ){ if( (mlop = n->mlnode.op) < OPSIMP ){ switch( optype(mlop) ){ default: cerror("(1)unknown opcode: %o",mlop); case BITYPE: goto binary; case UTYPE: break; case LTYPE: goto leaf; } } else{ if( mamask[mlop-OPSIMP] & (SIMPFLG|COMMFLG|MULFLG|DIVFLG|LOGFLG|FLOFLG|SHFFLG) ){ binary: *mlsp++ = BITYPE; } else if( ! (mamask[mlop-OPSIMP] & UTYPE) ){/* includes OPANY */ leaf: if( mlsp == mlstack ) goto tree_end; else if ( *--mlsp != BITYPE ) cerror("(1)bad multi-level tree descriptor around mltree[%d]", n-mltree); } } } tree_end: /* n points to final leaf */ ; }# ifndef BUG3 if( vdebug > 3 ){ printf("mltree={\n"); for( n= &(mltree[0]); n->mlhead.tag != 0; ++n) printf("%o: %d, %d, %o,\n",n, n->mlhead.tag,n->mlhead.subtag,n->mlhead.nexthead); printf(" }\n"); }# endif }mlmatch( subtree, target, subtarget ) NODE * subtree; int target,subtarget;{ /* * does subtree match a multi-level tree with * tag "target"? Return zero on failure, * non-zero subtag on success (or MDONE if * there is a zero subtag field). */ union mltemplate *head; /* current template header */ register union mltemplate *n; /* node being matched */ NODE * st; /* subtree being matched */ register int mlop;# ifndef BUG3 if( vdebug ) printf("mlmatch(%o,%d)\n",subtree,target);# endif for( head = &(mltree[0]); head->mlhead.tag != 0; head=head->mlhead.nexthead){# ifndef BUG3 if( vdebug > 1 )printf("mlmatch head(%o) tag(%d)\n", head->mlhead.tag);# endif if( head->mlhead.tag != target )continue; if( subtarget && head->mlhead.subtag != subtarget)continue;# ifndef BUG3 if( vdebug ) printf("mlmatch for %d\n",target);# endif /* potential for match */ n = head + 1; st = subtree; stp = ststack; mlsp = mlstack; /* compare n->op, ->nshape, ->ntype to * the subtree node st */ for( ;; ++n ){ /* for each node in multi-level template */ /* opmatch */ if( n->op < OPSIMP ){ if( st->op != n->op )break; } else { register opmtemp; if((opmtemp=mamask[n->op-OPSIMP])&SPFLG){ if(st->op!=NAME && st->op!=ICON && st->op!=OREG && ! shltype(st->op,st)) break; } else if((dope[st->op]&(opmtemp|ASGFLG))!=opmtemp) break; } /* check shape and type */ if( ! tshape( st, n->mlnode.nshape ) ) break; if( ! ttype( st->type, n->mlnode.ntype ) ) break; /* that node matched, let's try another */ /* must advance both st and n and halt at right time */ if( (mlop = n->mlnode.op) < OPSIMP ){ switch( optype(mlop) ){ default: cerror("(2)unknown opcode: %o",mlop); case BITYPE: goto binary; case UTYPE: st = st->left; break; case LTYPE: goto leaf; } } else{ if( mamask[mlop - OPSIMP] & (SIMPFLG|COMMFLG|MULFLG|DIVFLG|LOGFLG|FLOFLG|SHFFLG) ){ binary: *mlsp++ = BITYPE; *stp++ = st; st = st->left; } else if( ! (mamask[mlop-OPSIMP] & UTYPE) ){/* includes OPANY */ leaf: if( mlsp == mlstack ) goto matched; else if ( *--mlsp != BITYPE ) cerror("(2)bad multi-level tree descriptor around mltree[%d]", n-mltree); st = (*--stp)->right; } else /* UNARY */ st = st->left; } continue; matched: /* complete multi-level match successful */# ifndef BUG3 if( vdebug ) printf("mlmatch() success\n");# endif if( head->mlhead.subtag == 0 ) return( MDONE ); else {# ifndef BUG3 if( vdebug )printf("\treturns %d\n", head->mlhead.subtag );# endif return( head->mlhead.subtag ); } } } return( 0 ); }# endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -