/* * getVarName - extract a variable name from a command */ static bool getVarName( char *str, char *tmp1, vlist *vl ) { if( NextWord1( str, tmp1 ) <= 0 ) { return( FALSE ); } if( !VarName( tmp1, vl ) ) { return( FALSE ); } return( TRUE ); } /* getVarName */
/* * getVarName - extract a variable name from a command */ static bool getVarName( const char **str, char *tmp1, vlist *vl ) { char tmp[MAX_INPUT_LINE]; *str = GetNextWord1( *str, tmp ); if( *tmp == '\0' ) { return( false ); } if( !VarName( tmp1, tmp, vl ) ) { return( false ); } return( true ); } /* getVarName */
/* * SrcAssign - assign a value to a variable */ vi_rc SrcAssign( const char *data, vlist *vl ) { int i, j, k, l; long val; bool rxflag = false; bool envflag = false; bool setflag = false; bool expflag = false; bool timeflag = false; bool lnumflag = false; char tmp[MAX_SRC_LINE], tmp1[MAX_SRC_LINE], name[MAX_SRC_LINE]; const char *v1; char *t; vars *v; jmp_buf jmpaddr; time_t tod; bool check_end; char tmpstr[MAX_STR]; /* * get assign syntax : * ASSIGN %v = /v1/(r)($)(@)(x)(l)(t) * possible v1: * strlen %a * strchr %a ch * substr %a n1 n2 */ data = GetNextWord1( data, tmp ); if( *tmp == '\0' ) { return( ERR_SRC_INVALID_ASSIGN ); } if( !VarName( name, tmp, vl ) ) { return( ERR_SRC_INVALID_ASSIGN ); } data = GetNextWord1( data, tmp ); if( *tmp == '\0' ) { return( ERR_SRC_INVALID_ASSIGN ); } if( stricmp( tmp, "=" ) != 0 ) { return( ERR_SRC_INVALID_ASSIGN ); } data = SkipLeadingSpaces( data ); if( data[0] == '/' || data[0] == '"' ) { check_end = false; if( data[0] == '"' ) { data = GetNextWord( data, tmp, SingleQuote ); if( data[0] == '"' ) { check_end = true; } } else { data = GetNextWord( data, tmp, SingleSlash ); if( data[0] == '/' ) { check_end = true; } } if( check_end ) { for( ++data; data[0] != '\0'; data++ ) { switch( data[0] ) { case 't': timeflag = true; break; case 'r': rxflag = true; break; case '$': envflag = true; break; case '@': setflag = true; break; case 'x': expflag = true; break; case 'l': lnumflag = true; break; } } } Expand( tmp1, tmp, vl ); } else { data = GetNextWord1( data, tmp ); if( *tmp == '\0' ) { return( ERR_SRC_INVALID_ASSIGN ); } j = Tokenize( StrTokens, tmp, false ); if( j != TOK_INVALID ) { data = GetNextWord1( data, tmp ); if( *tmp == '\0' ) { return( ERR_SRC_INVALID_ASSIGN ); } if( !VarName( tmp1, tmp, vl ) ) { return( ERR_SRC_INVALID_ASSIGN ); } v = VarFind( tmp1, vl ); switch( j ) { case STR_T_STRLEN: if( v != NULL ) { sprintf( tmp1, "%d", v->len ); } else { strcpy( tmp1, "0" ); } break; case STR_T_SUBSTR: data = GetNextWord1( data, tmp ); if( *tmp == '\0' ) { return( ERR_SRC_INVALID_ASSIGN ); } Expand( tmp1, tmp, vl ); i = atoi( tmp1 ) - 1; data = GetNextWord1( data, tmp ); if( *tmp == '\0' ) { return( ERR_SRC_INVALID_ASSIGN ); } Expand( tmp1, tmp, vl ); j = atoi( tmp1 ) - 1; if( v == NULL ) { tmp1[0] = '\0'; break; } if( j >= v->len || i < 0 ) { tmp1[0] = '\0'; } else { l = 0; for( k = i; k <= j; k++ ) { tmp1[l++] = v->value[k]; } tmp1[l] = '\0'; } break; case STR_T_STRCHR: data = GetNextWord1( data, tmp ); if( *tmp == '\0' ) { return( ERR_SRC_INVALID_ASSIGN ); } Expand( tmp1, tmp, vl ); if( v == NULL ) { j = -1; } else { t = strchr( v->value, tmp1[0] ); if( t != NULL ) { j = t - v->value; } else { j = -1; } j++; } sprintf( tmp1, "%d", j ); break; } } else { Expand( tmp1, tmp, vl ); } } /* * regular expression subs. */ if( rxflag && CurrentRegularExpression != NULL ) { RegSub( CurrentRegularExpression, tmp1, tmp, CurrentPos.line ); strcpy( tmp1, tmp ); } /* * special processing */ if( envflag ) { v1 = getenv( tmp1 ); if( v1 == NULL ) { v1 = ""; } } else if( setflag ) { v1 = GetASetVal( tmp1, tmpstr ); } else if( timeflag ) { tod = time( NULL ); strftime( tmp, sizeof( tmp ), tmp1, localtime( &tod ) ); v1 = tmp; } else if( expflag || lnumflag ) { i = setjmp( jmpaddr ); if( i != 0 ) { return( (vi_rc)i ); } StartExprParse( tmp1, jmpaddr ); val = GetConstExpr(); if( lnumflag ) { fcb *cfcb; line *cline; vi_rc rc; rc = CGimmeLinePtr( val, &cfcb, &cline ); v1 = cline->data; if( rc != ERR_NO_ERR ) { v1 = ""; } } else { v1 = ltoa( val, tmp1, EditVars.Radix ); } } else { v1 = tmp1; } VarAddStr( name, v1, vl ); return( ERR_NO_ERR ); } /* SrcAssign */
void ProcTranslate(Var * proc) /* Purpose: Translate generic instructions to instructions directly translatable to processor instructions. */ { Instr * i, * first_i, * next_i; InstrBlock * blk; UInt8 step = 0; Bool in_assert; UInt8 color; Loc loc; loc.proc = proc; VERBOSE_NOW = false; if (Verbose(proc)) { VERBOSE_NOW = true; PrintHeader(2, VarName(proc)); } // As first step, we translate all variables stored on register addresses to actual registers for(blk = proc->instr; blk != NULL; blk = blk->next) { for(i = blk->first; i != NULL; i = i->next) { if (i->op == INSTR_LINE) continue; i->result = VarReg(i->result); i->arg1 = VarReg(i->arg1); i->arg2 = VarReg(i->arg2); } } // We perform as many translation steps as necessary // Some translation rules may use other translation rules, so more than one step may be necessary. // Translation ends either when there has not been any modification in last step or after // defined number of steps (to prevent infinite loop in case of invalid set of translation rules). in_assert = false; for(blk = proc->instr; blk != NULL; blk = blk->next) { if (Verbose(proc)) { PrintBlockHeader(blk); } loc.blk = blk; loc.n = 1; // The translation is done by using procedures for code generating. // We detach the instruction list from block and set the block as destination for instruction generator. // In this moment, the code generating stack must be empty anyways. first_i = blk->first; blk->first = blk->last = NULL; GenSetDestination(blk, NULL); i = first_i; while(i != NULL) { loc.i = i; if (ASSERTS_OFF) { if (i->op == INSTR_ASSERT_BEGIN) { in_assert = true; goto next; } else if (i->op == INSTR_ASSERT_END) { in_assert = false; goto next; } else { if (in_assert) goto next; } } if (VERBOSE_NOW) { color = PrintColor(RED+BLUE); PrintInstrLine(loc.n); InstrPrint(i); PrintColor(color); } if (!InstrTranslate3(i->op, i->result, i->arg1, i->arg2, GENERATE)) { InternalErrorLoc("Unsupported instruction", &loc); InstrPrint(i); } next: next_i = i->next; // InstrFree(i); i = next_i; loc.n++; } // Free the instructions i = first_i; while (i!=NULL) { next_i = i->next; InstrFree(i); i = next_i; } } // block }