void gendo(SNODE *stmt) /* * generate code for a do - while loop. */ { int oldcont, oldbreak; oldcont = contlab; oldbreak = breaklab; contlab = nextlabel++; gen_label(contlab); if( stmt->s1 != 0 && stmt->s1->next != 0 ) { breaklab = nextlabel++; genstmt(stmt->s1); /* generate body */ initstack(); truejp(stmt->exp,contlab); gen_label(breaklab); } else { genstmt(stmt->s1); initstack(); truejp(stmt->exp,contlab); } breaklab = oldbreak; contlab = oldcont; }
void genwhile(SNODE *stmt) /* * generate code to evaluate a while statement. */ { int lab1, lab2, lab3; initstack(); /* initialize temp registers */ lab1 = contlab; /* save old continue label */ contlab = nextlabel++; /* new continue label */ if( stmt->s1 != 0 ) /* has block */ { lab2 = breaklab; /* save old break label */ breaklab = nextlabel++; gen_code(op_jmp,0,make_label(contlab),0); lab3 = nextlabel++; gen_label(lab3); genstmt(stmt->s1); gen_label(contlab); if (stmt->lst) gen_line(stmt->lst); initstack(); truejp(stmt->exp,lab3); gen_label(breaklab); breaklab = lab2; /* restore old break label */ } else /* no loop code */ { if (stmt->lst) gen_line(stmt->lst); gen_label(contlab); initstack(); truejp(stmt->exp,contlab); } contlab = lab1; /* restore old continue label */ }
// generate code to evaluate a for loop // void GenerateFor(struct snode *stmt) { int old_break, old_cont, exit_label, loop_label; AMODE *ap; old_break = breaklab; old_cont = contlab; loop_label = nextlabel++; exit_label = nextlabel++; contlab = nextlabel++; initstack(); if( stmt->initExpr != NULL ) ReleaseTempRegister(GenerateExpression(stmt->initExpr,F_ALL | F_NOVALUE ,GetNaturalSize(stmt->initExpr))); GenerateLabel(loop_label); initstack(); if( stmt->exp != NULL ) GenerateFalseJump(stmt->exp,exit_label,stmt->predreg); if( stmt->s1 != NULL ) { breaklab = exit_label; GenerateStatement(stmt->s1); } GenerateLabel(contlab); initstack(); if( stmt->incrExpr != NULL ) ReleaseTempRegister(GenerateExpression(stmt->incrExpr,F_ALL | F_NOVALUE,GetNaturalSize(stmt->incrExpr))); GenerateMonadic(isThor ? op_br:op_bra,0,make_clabel(loop_label)); breaklab = old_break; contlab = old_cont; GenerateLabel(exit_label); }
/* * generate code to evaluate an until statement. */ void GenerateUntil(Statement *stmt) { int lab1, lab2; initstack(); /* initialize temp registers */ lab1 = contlab; /* save old continue label */ lab2 = breaklab; /* save old break label */ contlab = nextlabel++; /* new continue label */ GenerateLabel(contlab); if( stmt->s1 != NULL ) /* has block */ { breaklab = nextlabel++; initstack(); GenerateTrueJump(stmt->exp,breaklab,stmt->predreg); GenerateStatement(stmt->s1); GenerateMonadic(isThor ? op_br:op_bra,0,make_clabel(contlab)); GenerateLabel(breaklab); breaklab = lab2; /* restore old break label */ } else /* no loop code */ { initstack(); GenerateFalseJump(stmt->exp,contlab,stmt->predreg); } contlab = lab1; /* restore old continue label */ }
void Interpret(char expr[]) { SetIntSignal(True); if(commandline(expr)) ; else if(seterror()==0) { initstack(); restoretemplates(); CloseAllIOFiles(); interrupted = False; if(parseinput(expr)) checkexpression(top(), True); else { checkexpression(top(), False); settop(modify_expression(top())); starttiming(); toplevel(); stoptiming(); } } SetIntSignal(False); initstack(); restoretemplates(); CloseAllIOFiles(); interrupted = False; Write("\n"); }
void gencompactswitch(SNODE *stmt, int deflab) { int tablab,curlab,i, size = natural_size(stmt->exp); AMODE *ap,*ap2; long switchbottom=gswitchbottom, switchcount=gswitchcount; long switchtop=gswitchtop; int *switchlabels=0; tablab = nextlabel++; curlab = nextlabel++; initstack(); ap = gen_expr(stmt->exp,F_DREG | F_VOL,4); initstack(); if (switchbottom) gen_code(op_sub,4,ap,make_immed(switchbottom)); if (size < 0) gen_code(op_jl,0,make_label(deflab),0); else gen_code(op_jb,0,make_label(deflab),0); gen_code(op_cmp,4,ap,make_immed(switchtop-switchbottom)); if (size < 0) gen_code(op_jge,0,make_label(deflab),0); else gen_code(op_jnc,0,make_label(deflab),0); gen_code(op_shl,4,ap,make_immed(2)); ap2 = xalloc(sizeof(AMODE)); ap->mode = am_indisp; ap2->preg = ap->preg; ap->offset = makenode(en_labcon,(char *)tablab,0); gen_code(op_jmp,4,ap,0); initstack(); align(4); gen_label(tablab); switchlabels = xalloc((switchtop-switchbottom) * sizeof(int)); for (i=switchbottom; i < switchtop; i++) { switchlabels[i-switchbottom] = deflab; } stmt = stmt->s1; while (stmt) { if( stmt->s2 ) /* default case ? */ { stmt->label = (SNODE *)deflab; diddef = TRUE; } else { switchlabels[(int)stmt->label-switchbottom] = curlab; stmt->label = (SNODE *)curlab; } if(stmt->next != 0 ) curlab = nextlabel++; stmt = stmt->next; } for (i=0; i < switchtop-switchbottom; i++) gen_code(op_dd,4,make_label(switchlabels[i]),0); }
main() { struct stack s; int check; int item; initstack(&s); printf("Enter no of elemnts less than 100"); fflush(stdin); scanf("%d",&check); printf("enter elements \n"); do { fflush(stdin); scanf("%d",&item); push(&s,item); check --; } while(check!=0); printf("original stack \n"); display(&s); printf("\n reversed stack \n"); reverse(&s); display(&s); scanf("%d",&item); }
int main() { struct stack p; int data,ch, data1, m; printf("Enter the maximum size of the stack\n"); scanf("%d",&m); initstack(&p,m); do { printMenu(); printf("Enter your choice\n"); scanf("%d",&ch); switch(ch) { case 1: printf("Enter the element to be pushed\n"); scanf("%d",&data); push(&p, data); break; case 2: data1 = pop(&p); if(data1 != -1000) printf("The popped element is %d\n",data1); break; case 3: printf("The contents of the stack are"); display(p); printf("\n"); break; default: return 0; } } while(1); return 0; }
void GenerateSpinUnlock(Statement *stmt) { AMODE *ap; if( stmt != NULL && stmt->exp != NULL ) { initstack(); ap = GenerateExpression(stmt->exp,F_REG|F_IMMED,8); // Force return value into register 1 if( ap->preg != 1 ) { if (ap->mode == am_immed) { if (isFISA64) FISA64_GenLdi(makereg(1),ap,8); else GenerateTriadic(op_ori, 0, makereg(1),makereg(0),ap); } else GenerateDiadic(op_mov, 0, makereg(1),ap); if (isRaptor64) GenerateDiadic(op_outb, 0, makereg(0),make_indexed((int64_t)stmt->incrExpr,1)); else if (isFISA64) { GenerateMonadic(op_bsr, 0, make_string("_UnlockSema")); } else GenerateDiadic(op_sb, 0, makereg(0),make_indexed((int64_t)stmt->incrExpr,1)); } ReleaseTempRegister(ap); } }
void GenerateThrow(Statement *stmt) { AMODE *ap; if( stmt != NULL && stmt->exp != NULL ) { initstack(); ap = GenerateExpression(stmt->exp,F_ALL,8); if (ap->mode==am_immed) { if (isFISA64) FISA64_GenLdi(makereg(1),ap,8); else GenerateTriadic(op_ori,0,makereg(1),makereg(0),ap); } else if( ap->mode != am_reg) GenerateDiadic(op_lw,0,makereg(1),ap); else if (ap->preg != 1 ) GenerateTriadic(op_or,0,makereg(1),ap,makereg(0)); ReleaseTempRegister(ap); if (isFISA64) FISA64_GenLdi(makereg(2),make_immed((int64_t)stmt->label),8); else GenerateTriadic(op_ori,0,makereg(2),makereg(0),make_immed((int64_t)stmt->label)); } GenerateMonadic(isThor?op_br:op_bra,0,make_clabel(throwlab)); }
/* * generate code to evaluate an if statement. */ void GenerateIf(Statement *stmt) { int lab1, lab2, oldbreak; lab1 = nextlabel++; /* else label */ lab2 = nextlabel++; /* exit label */ oldbreak = breaklab; /* save break label */ initstack(); /* clear temps */ if (gCpu!=888) if (stmt->predreg==70) GenerateFalseJump(stmt->exp,lab1,stmt->predreg); GenerateFalseJump(stmt->exp,lab1,stmt->predreg); //if( stmt->s1 != 0 && stmt->s1->next != 0 ) // if( stmt->s2 != 0 ) // breaklab = lab2; // else // breaklab = lab1; GenerateStatement(stmt->s1); if( stmt->s2 != 0 ) /* else part exists */ { GenerateDiadic(isThor ? op_br:op_bra,0,make_clabel(lab2),0); if (mixedSource) GenerateMonadic(op_rem,0,make_string("; else")); GenerateLabel(lab1); //if( stmt->s2 == 0 || stmt->s2->next == 0 ) // breaklab = oldbreak; //else // breaklab = lab2; GenerateStatement(stmt->s2); GenerateLabel(lab2); } else /* no else code */ GenerateLabel(lab1); breaklab = oldbreak; }
int createmap(step *ps) { initstack(ps); srand(time(NULL)); randmap(); while(move(ps)==-1) { randmap(); initstack(ps); } del_23(); }
boolean newundostack (hdlundostack *hstack) { if (!initstack (sizeof (tyundostack), sizeof (tyundorecord), (hdlstack *) hstack)) return (false); (***hstack).ixaction = noaction; return (true); } /*newundostack*/
bool Load(const char *filename) { static char fileName[stringsize] = ""; if(filename) strncat(strcpy(fileName, ""), filename, stringsize-1); if(strlen(fileName) == 0) return False; else if(seterror()==0) { initstack(); unlockmem(); inithashtable(); lockmem(); initlex(); initlib(); initsyslib(); initmodify(); parsefile(inipath); parsefile(fileName); checkdefinitions(); modify_definitions(); lockmem(); if (gettemplate("main")->tag == FUNC) { Interpret("main"); } return True; } else { initstack(); unlockmem(); inithashtable(); lockmem(); initlex(); initlib(); initsyslib(); initmodify(); parsefile(inipath); checkdefinitions(); modify_definitions(); lockmem(); return False; } }
bool InitRemote(void) { initstack(); restoretemplates(); CloseAllIOFiles(); interrupted = False; SetIntSignal(True); ObjectCount = 0; RemoteOk = True; return True; }
int main(int argc, const char * argv[]) { int n, i; scanf("%d ", &n); int a[n]; struct stack t; struct stack *s = &t; for(i = 0; i<n; i++) scanf("%d", &a[i]); initstack(2*n, s); qs(a, 0, n-1, s); for(i = 0; i<n; i++) printf("%d ", a[i]); free(s->tsk); return 0; }
void gen_for(SNODE *stmt) /* * generate code to evaluate a for loop */ { int old_break, old_cont, exit_label, loop_label; old_break = breaklab; old_cont = contlab; loop_label = nextlabel++; exit_label = nextlabel++; contlab = nextlabel++; initstack(); if( stmt->label != 0 ) gen_expr(stmt->label,F_ALL | F_NOVALUE ,natural_size(stmt->label)); gen_code(op_jmp,0,make_label(contlab),0); gen_label(loop_label); if( stmt->s1 != 0 ) { breaklab = exit_label; genstmt(stmt->s1); } initstack(); if( stmt->s2 != 0 ) gen_expr(stmt->s2,F_ALL | F_NOVALUE,natural_size(stmt->s2)); gen_label(contlab); if (stmt->lst) gen_line(stmt->lst); initstack(); if( stmt->exp != 0 ) truejp(stmt->exp,loop_label); else gen_code(op_jmp,0,make_label(loop_label),0); gen_label(exit_label); breaklab = old_break; contlab = old_cont; }
/* * generate code for a do - while loop. */ void GenerateDoUntil(struct snode *stmt) { int oldcont, oldbreak; oldcont = contlab; oldbreak = breaklab; contlab = nextlabel++; GenerateLabel(contlab); breaklab = nextlabel++; GenerateStatement(stmt->s1); /* generate body */ initstack(); GenerateFalseJump(stmt->exp,contlab,stmt->predreg); GenerateLabel(breaklab); breaklab = oldbreak; contlab = oldcont; }
void genbinaryswitch(SNODE *stmt, int deflab) { int curlab, i=0,j,size = natural_size(stmt->exp); AMODE *ap1; long switchbottom=gswitchbottom, switchcount=gswitchcount; long switchtop=gswitchtop; int *switchlabels=0; long *switchids=0; int *switchbinlabels=0; curlab = nextlabel++; initstack(); ap1 = gen_expr(stmt->exp,F_DREG,4); global_flag++; switchlabels = xalloc((switchcount) * sizeof(int)); switchbinlabels = xalloc((switchcount) * sizeof(int)); switchids = xalloc((switchcount) * sizeof(long)); global_flag--; stmt = stmt->s1; while (stmt) { if( stmt->s2 ) /* default case ? */ { stmt->label = (SNODE *) deflab; diddef = TRUE; } else { switchlabels[i] = curlab; switchbinlabels[i] = -1; switchids[i++] = (int)stmt->label; stmt->label = (SNODE *)curlab; } if( stmt->next != 0 ) curlab = nextlabel++; stmt = stmt->next; } for (i=0; i < switchcount; i++) for (j=i+1; j < switchcount; j++) if (switchids[j] < switchids[i]) { int temp = switchids[i]; switchids[i] = switchids[j]; switchids[j] = temp; temp = switchlabels[i]; switchlabels[i] = switchlabels[j]; switchlabels[j] = temp; } bingen(0,(switchcount)/2,switchcount,ap1,deflab,size,switchids,switchlabels,switchbinlabels); freeop(ap1); }
/* inorder traversal * LVR */ int inordertl(bintree tree) { /* tr represents tree */ lstack trstack = initstack(); elemtype top; if (tree->root) { btnode tr = tree->root; top = malloc(sizeof(*top)); top->node = tr; top->isrecprob = (tr->lsubtr || tr->rsubtr) ? 1 : 0; push(top, trstack); } btnode tmp; elemtype lprob; /* left problem */ elemtype rprob; /* right problem */ while(isempty_ls(trstack)){ top = pop(trstack); tmp = top->node; if (!top->isrecprob) { visit(tmp); free(top); } /* push R first, V second, L last, just as the inverse of LVR */ else { if (tmp->rsubtr) { rprob = malloc(sizeof(*rprob)); rprob->node = tmp->rsubtr; rprob->isrecprob = (tmp->rsubtr->lsubtr || tmp->rsubtr->rsubtr) ? 1 : 0; push(rprob, trstack); } top->isrecprob = 0; // next time we just vist it and done push(top, trstack); if (tmp->lsubtr) { lprob = malloc(sizeof(*rprob)); lprob->node = tmp->lsubtr; lprob->isrecprob = (tmp->lsubtr->lsubtr || tmp->lsubtr->rsubtr) ? 1 : 0; push(lprob, trstack); } } } }
void main() { struct stack s; // int i; clrscr(); initstack(&s); push(&s,11); push(&s,23); push(&s,5); push(&s,8); push(&s,1); pop(&s); pop(&s); print(&s); getch(); }
void intraverse2(TREE T){ TREE p; Sqstack S; initstack(&S);p=T; while(p||stackempty(S)){//节点不为空或者栈不空时循环 if(p) { push(&S,p); p=p->left; } else{ pop(&S,&p); print(p->data); p=p->right; } } }
main() { initstack(); r1 = 7; r2 = 13; r3 = 100; printString ("Now is the time for all good people ...\n"); L1: r2 = r1 * r2; *(fp + 4) = r2; printInt (r2); printString (" "); if( r3 > r2 ) goto L1; printline (); pushi(r2); }
// The same as generating a compound statement but leaves out the generation of // the prolog and epilog clauses. void GenerateFuncbody(Statement *stmt) { Statement *st; SYM *sp; sp = stmt->ssyms.head; while (sp) { if (sp->initexp) { initstack(); ReleaseTempRegister(GenerateExpression(sp->initexp,F_ALL,8)); } sp = sp->next; } // Generate statement will process the entire list of statements in // the block. GenerateStatement(stmt->s1); }
void GenerateCheck(Statement * stmt) { AMODE *ap1, *ap2, *ap3, *ap4; ENODE *node, *ep; int size; initstack(); ep = node = stmt->exp; if (ep->p[0]->nodetype==en_lt && ep->p[1]->nodetype==en_ge && equalnode(ep->p[0]->p[0],ep->p[1]->p[0])) { ep->nodetype = en_chk; if (ep->p[0]) ep->p[2] = ep->p[0]->p[1]; else ep->p[2] = NULL; ep->p[1] = ep->p[1]->p[1]; ep->p[0] = ep->p[0]->p[0]; } else if (ep->p[0]->nodetype==en_ge && ep->p[1]->nodetype==en_lt && equalnode(ep->p[0]->p[0],ep->p[1]->p[0])) { ep->nodetype = en_chk; if (ep->p[1]) ep->p[2] = ep->p[1]->p[1]; else ep->p[2] = NULL; ep->p[1] = ep->p[0]->p[1]; ep->p[0] = ep->p[0]->p[0]; } if (ep->nodetype != en_chk) { error(ERR_CHECK); return; } size = GetNaturalSize(node); ap1 = GenerateExpression(node->p[0],F_REG,size); ap2 = GenerateExpression(node->p[1],F_REG|F_IMM0,size); ap3 = GenerateExpression(node->p[2],F_REG|F_IMMED,size); if (ap2->mode == am_immed) { ap2->mode = am_reg; ap2->preg = 0; } GenerateTriadic(op_chk,0,ap1,ap2,ap3); ReleaseTempRegister(ap3); ReleaseTempRegister(ap2); ReleaseTempRegister(ap1); }
int combination(int n,int m){ int sum=0; elemtype x; struct stack stk; initstack(&stk); x.n=n;x.m=m; push(&stk,x); while(!stackempty(&stk)){ x=pop(&stk); if(x.n==x.m||!x.m)sum++; else{ x.n--; push(&stk,x); x.m--; push(&stk,x); } } return sum; }
void genif(SNODE *stmt) /* * generate code to evaluate an if statement. */ { int lab1, lab2; lab1 = nextlabel++; /* else label */ lab2 = nextlabel++; /* exit label */ initstack(); /* clear temps */ falsejp(stmt->exp,lab1); genstmt(stmt->s1); if( stmt->s2 != 0 ) /* else part exists */ { gen_code(op_jmp,0,make_label(lab2),0); gen_label(lab1); genstmt(stmt->s2); gen_label(lab2); } else /* no else code */ gen_label(lab1); }
void main() { int a[7][8] = { 1,1,1,1,1,1,1,1, 1,0,0,0,1,1,1,1, 1,0,1,0,1,0,1,1, 1,1,0,0,1,0,1,1, 1,1,0,1,0,0,0,1, 1,0,0,0,0,1,0,1, 1,1,1,1,1,1,1,1}; int i, j, k; stack *s; initstack(&s); for (i = 0; i < 7; i++) { for (j = 0; j < 8; j++) { printf("%3d",a[i][j]); } printf("\n"); } search(s,a,5,6); printf("*****************************\n"); for (i = 0; i < 7; i++) { for (j = 0; j < 8; j++) { printf("%3d",a[i][j]); } printf("\n"); } printf("\n"); while (!empty(&s)) { pop(&s,&i,&j,&k); printf("i = %d,j = %d\n",i,j); } free(s); }
int main( ) { struct stack s ; int i ; initstack ( &s ) ; push ( &s, 11 ) ; push ( &s, 23 ) ; push ( &s, -8 ) ; push ( &s, 16 ) ; push ( &s, 27 ) ; push ( &s, 14 ) ; push ( &s, 20 ) ; push ( &s, 39 ) ; push ( &s, 2 ) ; push ( &s, 15 ) ; push ( &s, 7 ) ; i = pop ( &s ) ; if ( i != '\0' ) printf ( "Item popped: %d\n", i ) ; i = pop ( &s ) ; if ( i != '\0' ) printf ( "Item popped: %d\n", i ) ; i = pop ( &s ) ; if ( i != '\0' ) printf ( "Item popped: %d\n", i ) ; i = pop ( &s ) ; if ( i != '\0' ) printf ( "Item popped: %d\n", i ) ; i = pop ( &s ) ; if ( i != '\0' ) printf ( "Item popped: %d\n\n", i ) ; return 0 ; }
int combination(int n, int m){ struct stack s; initstack(&s);//初期化 comb a={n,m}; int ans=0; push(&s,a); while(!stackempty(&s)){ a=pop(&s); if(a.n==a.m||a.m==0||a.n==1){ return ans+1; }else{ a.n=a.n-1; push(&s,a); a.m=a.m-1; push(&s,a); } } return ans; }