/* * Allocate off bits on the stack. p is a tree that when evaluated * is the multiply count for off, t is a storeable node where to write * the allocated address. */ void spalloc(NODE *t, NODE *p, OFFSZ off) { NODE *sp; p = buildtree(MUL, p, bcon(off/SZCHAR)); p = buildtree(PLUS, p, bcon(30)); p = buildtree(AND, p, xbcon(-16, NULL, UNSIGNED)); p = cast(p, UNSIGNED, 0); /* sub the size from sp */ sp = block(REG, NIL, NIL, UNSIGNED+PTR, 0, 0); slval(sp, 0); sp->n_rval = STKREG; p = (buildtree(MINUSEQ, sp, p)); ecomp(p); /* save the address of sp */ sp = block(REG, NIL, NIL, PTR+UNSIGNED, t->n_df, t->n_ap); slval(sp, 0); sp->n_rval = STKREG; t->n_type = sp->n_type; p = (buildtree(ASSIGN, t, sp)); /* Emit! */ ecomp(p); }
void myp2tree(NODE *p) { struct symtab *sp; NODE *l; if (cdope(p->n_op) & CALLFLG) { if (p->n_left->n_op == ADDROF && p->n_left->n_left->n_op == NAME) { p->n_left = nfree(p->n_left); l = p->n_left; l->n_op = ICON; if (l->n_sp->sclass != STATIC && l->n_sp->sclass != USTATIC) l->n_sp = picsymtab(l->n_sp->sname, "@PLTPC", ""); } } if (p->n_op != FCON) return; sp = IALLOC(sizeof(struct symtab)); sp->sclass = STATIC; sp->sap = 0; sp->slevel = 1; /* fake numeric label */ sp->soffset = getlab(); sp->sflags = 0; sp->stype = p->n_type; sp->squal = (CON >> TSHIFT); sp->sname = NULL; locctr(DATA, sp); defloc(sp); ninval(0, tsize(sp->stype, sp->sdf, sp->sap), p); p->n_op = NAME; slval(p, 0); p->n_sp = sp; }
/* clocal() is called to do local transformations on * an expression tree preparitory to its being * written out in intermediate code. * * the major essential job is rewriting the * automatic variables and arguments in terms of * REG and OREG nodes * conversion ops which are not necessary are also clobbered here * in addition, any special features (such as rewriting * exclusive or) are easily handled here as well */ NODE * clocal(NODE *p) { register struct symtab *q; register NODE *r, *l; register int o; register int m; TWORD t; #ifdef PCC_DEBUG if (xdebug) { printf("clocal: %p\n", p); fwalk(p, eprint, 0); } #endif switch( o = p->n_op ){ case NAME: if ((q = p->n_sp) == NULL) return p; /* Nothing to care about */ switch (q->sclass) { case PARAM: case AUTO: /* fake up a structure reference */ r = block(REG, NIL, NIL, PTR+STRTY, 0, 0); slval(r, 0); r->n_rval = FPREG; p = stref(block(STREF, r, p, 0, 0, 0)); break; case USTATIC: if (kflag == 0) break; /* FALLTHROUGH */ case STATIC: #ifdef TLS if (q->sflags & STLS) { p = tlsref(p); break; } #endif break; case REGISTER: p->n_op = REG; slval(p, 0); p->n_rval = q->soffset; break; case EXTERN: case EXTDEF: if (q->sflags & STLS) { p = tlsref(p); break; } if (kflag == 0 || statinit) break; if (blevel > 0) p = picext(p); break; } break; case UCALL: case USTCALL: /* For now, always clear eax */ l = block(REG, NIL, NIL, INT, 0, 0); regno(l) = RAX; p->n_right = clocal(buildtree(ASSIGN, l, bcon(0))); p->n_op -= (UCALL-CALL); break; case SCONV: /* Special-case shifts */ if (p->n_type == LONG && (l = p->n_left)->n_op == LS && l->n_type == INT && l->n_right->n_op == ICON) { p->n_left = l->n_left; p = buildtree(LS, p, l->n_right); nfree(l); break; } l = p->n_left; /* Float conversions may need extra casts */ if (p->n_type == FLOAT || p->n_type == DOUBLE || p->n_type == LDOUBLE) { if (l->n_type < INT || l->n_type == BOOL) { p->n_left = block(SCONV, l, NIL, ISUNSIGNED(l->n_type) ? UNSIGNED : INT, l->n_df, l->n_ap); break; } } if (p->n_type == l->n_type) { nfree(p); return l; } if ((p->n_type & TMASK) == 0 && (l->n_type & TMASK) == 0 && tsize(p->n_type, p->n_df, p->n_ap) == tsize(l->n_type, l->n_df, l->n_ap)) { if (p->n_type != FLOAT && p->n_type != DOUBLE && l->n_type != FLOAT && l->n_type != DOUBLE && l->n_type != LDOUBLE && p->n_type != LDOUBLE) { if (l->n_op == NAME || l->n_op == UMUL || l->n_op == TEMP) { l->n_type = p->n_type; nfree(p); return l; } } } if (DEUNSIGN(p->n_type) == INT && DEUNSIGN(l->n_type) == INT && coptype(l->n_op) == BITYPE && l->n_op != COMOP && l->n_op != QUEST && l->n_op != ASSIGN) { l->n_type = p->n_type; nfree(p); return l; } o = l->n_op; m = p->n_type; if (o == ICON) { CONSZ val = glval(l); /* if named constant and pointer, allow cast to long/ulong */ if (!nncon(l) && (l->n_type & TMASK) && (m == LONG || m == ULONG)) { l->n_type = m; l->n_ap = 0; return nfree(p); } if (ISPTR(l->n_type) && !nncon(l)) break; /* cannot convert named pointers */ if (!ISPTR(m)) /* Pointers don't need to be conv'd */ switch (m) { case BOOL: val = nncon(l) ? (val != 0) : 1; slval(l, val); l->n_sp = NULL; break; case CHAR: slval(l, (char)val); break; case UCHAR: slval(l, val & 0377); break; case SHORT: slval(l, (short)val); break; case USHORT: slval(l, val & 0177777); break; case UNSIGNED: slval(l, val & 0xffffffff); break; case INT: slval(l, (int)val); break; case LONG: case LONGLONG: slval(l, (long long)val); break; case ULONG: case ULONGLONG: slval(l, val); break; case VOID: break; case LDOUBLE: case DOUBLE: case FLOAT: l->n_op = FCON; l->n_dcon = fltallo(); FCAST(l->n_dcon)->fp = val; break; default: cerror("unknown type %d", m); } l->n_type = m; l->n_ap = NULL; nfree(p); return l; } else if (l->n_op == FCON) { CONSZ lv; if (p->n_type == BOOL) lv = !FLOAT_ISZERO(FCAST(l->n_dcon)); else { FLOAT_FP2INT(lv, FCAST(l->n_dcon), m); } slval(l, lv); l->n_sp = NULL; l->n_op = ICON; l->n_type = m; l->n_ap = NULL; nfree(p); return clocal(l); } if ((p->n_type == CHAR || p->n_type == UCHAR || p->n_type == SHORT || p->n_type == USHORT) && (l->n_type == FLOAT || l->n_type == DOUBLE || l->n_type == LDOUBLE)) { p = block(SCONV, p, NIL, p->n_type, p->n_df, p->n_ap); p->n_left->n_type = INT; return p; } break; case MOD: case DIV: if (o == DIV && p->n_type != CHAR && p->n_type != SHORT) break; if (o == MOD && p->n_type != CHAR && p->n_type != SHORT) break; /* make it an int division by inserting conversions */ p->n_left = makety(p->n_left, INT, 0, 0, 0); p->n_right = makety(p->n_right, INT, 0, 0, 0); p = makety(p, p->n_type, 0, 0, 0); p->n_left->n_type = INT; break; case FORCE: /* put return value in return reg */ p->n_op = ASSIGN; p->n_right = p->n_left; p->n_left = block(REG, NIL, NIL, p->n_type, 0, 0); t = p->n_type; if (ISITY(t)) t = t - (FIMAG-FLOAT); p->n_left->n_rval = p->n_left->n_type == BOOL ? RETREG(CHAR) : RETREG(t); break; case LS: case RS: /* shift count must be in a char */ if (p->n_right->n_type == CHAR || p->n_right->n_type == UCHAR) break; p->n_right = makety(p->n_right, CHAR, 0, 0, 0); break; } #ifdef PCC_DEBUG if (xdebug) { printf("clocal end: %p\n", p); fwalk(p, eprint, 0); } #endif return(p); }
/* clocal() is called to do local transformations on * an expression tree preparitory to its being * written out in intermediate code. * * the major essential job is rewriting the * automatic variables and arguments in terms of * REG and OREG nodes * conversion ops which are not necessary are also clobbered here * in addition, any special features (such as rewriting * exclusive or) are easily handled here as well */ P1ND * clocal(P1ND *p) { struct attr *ap; register struct symtab *q; register P1ND *r, *l, *n, *s; register int o; register int m; #ifdef PCC_DEBUG if (xdebug) { printf("clocal: %p\n", p); p1fwalk(p, eprint, 0); } #endif switch( o = p->n_op ){ case NAME: if ((q = p->n_sp) == NULL) return p; /* Nothing to care about */ switch (q->sclass) { case PARAM: case AUTO: /* fake up a structure reference */ r = block(REG, NIL, NIL, PTR+STRTY, 0, 0); slval(r, 0); r->n_rval = FPREG; p = stref(block(STREF, r, p, 0, 0, 0)); break; case USTATIC: if (kflag == 0) break; /* FALLTHROUGH */ case STATIC: #ifdef TLS if (q->sflags & STLS) { p = tlsref(p); break; } #endif if (kflag == 0) { if (q->slevel == 0) break; } else if (kflag && !statinit && blevel > 0 && #ifdef GCC_COMPAT attr_find(q->sap, GCC_ATYP_WEAKREF)) { #else 0) { #endif /* extern call */ p = picext(p); } else if (blevel > 0 && !statinit) p = picstatic(p); break; case REGISTER: p->n_op = REG; slval(p, 0); p->n_rval = q->soffset; break; case EXTERN: case EXTDEF: #ifdef TLS if (q->sflags & STLS) { p = tlsref(p); break; } #endif #ifdef PECOFFABI if (q->sflags & SDLLINDIRECT) p = import(p); #endif #ifdef GCC_COMPAT if ((ap = attr_find(q->sap, GCC_ATYP_VISIBILITY)) != NULL && strcmp(ap->sarg(0), "hidden") == 0) printf(PRTPREF "\t.hidden %s\n", getsoname(q)); #endif if (kflag == 0) break; if (blevel > 0 && !statinit) p = picext(p); break; } break; case ADDROF: if (kflag == 0 || blevel == 0 || statinit) break; /* char arrays may end up here */ l = p->n_left; if (l->n_op != NAME || (l->n_type != ARY+CHAR && l->n_type != ARY+WCHAR_TYPE)) break; l = p; p = picstatic(p->n_left); p1nfree(l); if (p->n_op != UMUL) cerror("ADDROF error"); l = p; p = p->n_left; p1nfree(l); break; case UCALL: if (kflag == 0) break; l = block(REG, NIL, NIL, INT, 0, 0); l->n_rval = EBX; p->n_right = buildtree(ASSIGN, l, tempnode(gotnr, INT, 0, 0)); p->n_op -= (UCALL-CALL); break; case USTCALL: #if defined(os_openbsd) ap = strattr(p->n_left->n_ap); if (ap->amsize == SZCHAR || ap->amsize == SZSHORT || ap->amsize == SZINT || ap->amsize == SZLONGLONG) #else if (attr_find(p->n_left->n_ap, ATTR_COMPLEX) && (ap = strattr(p->n_left->n_ap)) && ap->amsize == SZLONGLONG) #endif { /* float complex */ /* fake one arg to make pass2 happy */ p->n_right = block(FUNARG, bcon(0), NIL, INT, 0, 0); p->n_op -= (UCALL-CALL); break; } /* Add hidden arg0 */ r = block(REG, NIL, NIL, INCREF(VOID), 0, 0); regno(r) = EBP; #ifdef GCC_COMPAT if ((ap = attr_find(p->n_ap, GCC_ATYP_REGPARM)) != NULL && ap->iarg(0) > 0) { l = block(REG, NIL, NIL, INCREF(VOID), 0, 0); regno(l) = EAX; p->n_right = buildtree(ASSIGN, l, r); } else #endif p->n_right = block(FUNARG, r, NIL, INCREF(VOID), 0, 0); p->n_op -= (UCALL-CALL); if (kflag == 0) break; l = block(REG, NIL, NIL, INT, 0, 0); regno(l) = EBX; r = buildtree(ASSIGN, l, tempnode(gotnr, INT, 0, 0)); p->n_right = block(CM, r, p->n_right, INT, 0, 0); break; /* FALLTHROUGH */ #if defined(MACHOABI) case CALL: case STCALL: if (p->n_type == VOID) break; r = tempnode(0, p->n_type, p->n_df, p->n_ap); l = tcopy(r); p = buildtree(COMOP, buildtree(ASSIGN, r, p), l); #endif break; #ifdef notyet /* XXX breaks sometimes */ case CBRANCH: l = p->n_left; /* * Remove unnecessary conversion ops. */ if (!clogop(l->n_op) || l->n_left->n_op != SCONV) break; if (coptype(l->n_op) != BITYPE) break; if (l->n_right->n_op != ICON) break; r = l->n_left->n_left; if (r->n_type >= FLOAT) break; if (toolarge(r->n_type, l->n_right->n_lval)) break; l->n_right->n_type = r->n_type; if (l->n_op >= ULE && l->n_op <= UGT) l->n_op -= (UGT-ULE); p->n_left = buildtree(l->n_op, r, l->n_right); p1nfree(l->n_left); p1nfree(l); break; #endif case PCONV: l = p->n_left; /* Make int type before pointer */ if (l->n_type < INT || l->n_type == LONGLONG || l->n_type == ULONGLONG || l->n_type == BOOL) { /* float etc? */ p->n_left = block(SCONV, l, NIL, UNSIGNED, 0, 0); } break; case SCONV: if (p->n_left->n_op == COMOP) break; /* may propagate wrong type later */ l = p->n_left; if (p->n_type == l->n_type) { p1nfree(p); return l; } if ((p->n_type & TMASK) == 0 && (l->n_type & TMASK) == 0 && tsize(p->n_type, p->n_df, p->n_ap) == tsize(l->n_type, l->n_df, l->n_ap)) { if (p->n_type != FLOAT && p->n_type != DOUBLE && l->n_type != FLOAT && l->n_type != DOUBLE && l->n_type != LDOUBLE && p->n_type != LDOUBLE) { if (l->n_op == NAME || l->n_op == UMUL || l->n_op == TEMP) { l->n_type = p->n_type; p1nfree(p); return l; } } } if (DEUNSIGN(p->n_type) == INT && DEUNSIGN(l->n_type) == INT && coptype(l->n_op) == BITYPE && l->n_op != COMOP && l->n_op != QUEST && l->n_op != ASSIGN) { l->n_type = p->n_type; p1nfree(p); return l; } o = l->n_op; m = p->n_type; if (o == ICON) { /* * Can only end up here if o is an address, * and in that case the only compile-time conversion * possible is to int. */ if ((TMASK & l->n_type) == 0 && l->n_sp == NULL) cerror("SCONV ICON"); if (l->n_sp == 0) { p->n_type = UNSIGNED; concast(l, m); } else if (m != INT && m != UNSIGNED) break; l->n_type = m; l->n_ap = 0; p1nfree(p); return l; } else if (l->n_op == FCON) cerror("SCONV FCON"); if ((p->n_type == CHAR || p->n_type == UCHAR || p->n_type == SHORT || p->n_type == USHORT) && (l->n_type == FLOAT || l->n_type == DOUBLE || l->n_type == LDOUBLE)) { p = block(SCONV, p, NIL, p->n_type, p->n_df, p->n_ap); p->n_left->n_type = INT; return p; } break; case MOD: case DIV: if (o == DIV && p->n_type != CHAR && p->n_type != SHORT) break; if (o == MOD && p->n_type != CHAR && p->n_type != SHORT) break; /* make it an int division by inserting conversions */ p->n_left = makety(p->n_left, INT, 0, 0, 0); p->n_right = makety(p->n_right, INT, 0, 0, 0); o = p->n_type; p->n_type = INT; p = makety(p, o, 0, 0, 0); break; case FORCE: /* put return value in return reg */ p->n_op = ASSIGN; p->n_right = p->n_left; p->n_left = block(REG, NIL, NIL, p->n_type, 0, 0); p->n_left->n_rval = p->n_left->n_type == BOOL ? RETREG(CHAR) : RETREG(p->n_type); break; case LS: case RS: /* shift count must be in a char */ if (p->n_right->n_type == CHAR || p->n_right->n_type == UCHAR) break; p->n_right = block(SCONV, p->n_right, NIL, CHAR, 0, 0); break; /* If not using pcc struct return */ case STASG: r = p->n_right; if (r->n_op != STCALL && r->n_op != USTCALL) break; m = tsize(BTYPE(r->n_type), r->n_df, r->n_ap); if (m == SZCHAR) m = CHAR; else if (m == SZSHORT) m = SHORT; else if (m == SZINT) m = INT; else if (m == SZLONGLONG) m = LONGLONG; else break; #if !defined(os_openbsd) if (attr_find(r->n_ap, ATTR_COMPLEX) == 0) break; /* float _Complex always in regs */ #endif l = buildtree(ADDROF, p->n_left, NIL); p1nfree(p); r->n_op -= (STCALL-CALL); r->n_type = m; /* r = long, l = &struct */ n = tempnode(0, m, r->n_df, r->n_ap); r = buildtree(ASSIGN, p1tcopy(n), r); s = tempnode(0, l->n_type, l->n_df, l->n_ap); l = buildtree(ASSIGN, p1tcopy(s), l); p = buildtree(COMOP, r, l); l = buildtree(CAST, block(NAME, NIL, NIL, m|PTR, 0, 0), p1tcopy(s)); r = l->n_right; p1nfree(l->n_left); p1nfree(l); r = buildtree(ASSIGN, buildtree(UMUL, r, NIL), n); p = buildtree(COMOP, p, r); p = buildtree(COMOP, p, s); break; }
/* clocal() is called to do local transformations on * an expression tree preparitory to its being * written out in intermediate code. * * the major essential job is rewriting the * automatic variables and arguments in terms of * REG and OREG nodes * conversion ops which are not necessary are also clobbered here * in addition, any special features (such as rewriting * exclusive or) are easily handled here as well */ NODE * clocal(NODE *p) { register struct symtab *q; register NODE *r, *l; register int o; TWORD t; #ifdef PCC_DEBUG if (xdebug) { printf("clocal: %p\n", p); fwalk(p, eprint, 0); } #endif switch( o = p->n_op ){ case NAME: if ((q = p->n_sp) == NULL) return p; /* Nothing to care about */ switch (q->sclass) { case PARAM: case AUTO: /* fake up a structure reference */ r = block(REG, NIL, NIL, PTR+STRTY, 0, 0); slval(r, 0); r->n_rval = FPREG; p = stref(block(STREF, r, p, 0, 0, 0)); break; case REGISTER: p->n_op = REG; slval(p, 0); p->n_rval = q->soffset; break; case USTATIC: case STATIC: if (kflag == 0) break; if (blevel > 0 && !statinit) p = picstatic(p); break; case EXTERN: case EXTDEF: if (kflag == 0) break; if (blevel > 0 && !statinit) p = picext(p); break; } break; case ADDROF: if (kflag == 0 || blevel == 0 || statinit) break; /* char arrays may end up here */ l = p->n_left; if (l->n_op != NAME || (l->n_type != ARY+CHAR && l->n_type != ARY+WCHAR_TYPE)) break; l = p; p = picstatic(p->n_left); nfree(l); if (p->n_op != UMUL) cerror("ADDROF error"); l = p; p = p->n_left; nfree(l); break; case STASG: /* convert struct assignment to call memcpy */ l = p->n_left; if (l->n_op == NAME && ISFTN(l->n_sp->stype)) break; /* struct return, do nothing */ /* first construct arg list */ p->n_left = buildtree(ADDROF, p->n_left, 0); r = bcon(tsize(STRTY, p->n_df, p->n_ap)/SZCHAR); p->n_left = buildtree(CM, p->n_left, p->n_right); p->n_right = r; p->n_op = CM; p->n_type = INT; r = block(NAME, NIL, NIL, INT, 0, 0); r->n_sp = lookup(addname("memcpy"), SNORMAL); if (r->n_sp->sclass == SNULL) { r->n_sp->sclass = EXTERN; r->n_sp->stype = INCREF(VOID+PTR)+(FTN-PTR); } r->n_type = r->n_sp->stype; p = buildtree(CALL, r, p); break; case SCONV: l = p->n_left; if (l->n_op == ICON && ISPTR(l->n_type)) { /* Do immediate cast here */ /* Should be common code */ q = l->n_sp; l->n_sp = NULL; l->n_type = UNSIGNED; if (concast(l, p->n_type) == 0) cerror("clocal"); p = nfree(p); p->n_sp = q; } break; case FORCE: /* put return value in return reg */ p->n_op = ASSIGN; p->n_right = p->n_left; p->n_left = block(REG, NIL, NIL, p->n_type, 0, 0); t = p->n_type; if (ISITY(t)) t = t - (FIMAG-FLOAT); p->n_left->n_rval = RETREG(t); break; } #ifdef PCC_DEBUG if (xdebug) { printf("clocal end: %p\n", p); fwalk(p, eprint, 0); } #endif return(p); }
std::unique_ptr<Value> ArithmeticValueNode::traceValue(std::unique_ptr<Value> lval, std::unique_ptr<Value> rval, std::ostream& out) const { switch (_operator) { case ADD: { if (lval->getType() == Value::String && rval->getType() == Value::String) { StringValue& slval(static_cast<StringValue&>(*lval)); StringValue& srval(static_cast<StringValue&>(*rval)); std::unique_ptr<Value> result(new StringValue( slval.getValue() + srval.getValue())); out << "Appended strings '" << slval << "' + '" << srval << "' -> '" << *result << "'.\n"; return result; } } [[fallthrough]]; case SUB: case MUL: case DIV: { if (lval->getType() == Value::Integer && rval->getType() == Value::Integer) { IntegerValue& ilval(static_cast<IntegerValue&>(*lval)); IntegerValue& irval(static_cast<IntegerValue&>(*rval)); IntegerValue::ValueType res = 0; switch (_operator) { case ADD: res = ilval.getValue() + irval.getValue(); break; case SUB: res = ilval.getValue() - irval.getValue(); break; case MUL: res = ilval.getValue() * irval.getValue(); break; case DIV: res = ilval.getValue() / irval.getValue(); break; case MOD: assert(0); } std::unique_ptr<Value> result(new IntegerValue(res, false)); out << "Performed integer operation " << ilval << " " << getOperatorName() << " " << irval << " = " << *result << "\n"; return result; } NumberValue* nlval(dynamic_cast<NumberValue*>(lval.get())); NumberValue* nrval(dynamic_cast<NumberValue*>(lval.get())); if (nlval != 0 && nrval != 0) { NumberValue::CommonValueType res = 0; switch (_operator) { case ADD: res = nlval->getCommonValue() + nrval->getCommonValue(); break; case SUB: res = nlval->getCommonValue() - nrval->getCommonValue(); break; case MUL: res = nlval->getCommonValue() * nrval->getCommonValue(); break; case DIV: res = nlval->getCommonValue() / nrval->getCommonValue(); break; case MOD: assert(0); } std::unique_ptr<Value> result(new FloatValue(res)); out << "Performed float operation " << nlval << " " << getOperatorName() << " " << nrval << " = " << *result << "\n"; return result; } } break; case MOD: { if (lval->getType() == Value::Integer && rval->getType() == Value::Integer) { IntegerValue& ilval(static_cast<IntegerValue&>(*lval)); IntegerValue& irval(static_cast<IntegerValue&>(*rval)); std::unique_ptr<Value> result(new IntegerValue( ilval.getValue() % irval.getValue(), false)); out << "Performed integer operation " << ilval << " " << getOperatorName() << " " << irval << " = " << *result << "\n"; return result; } } break; } out << "Failed to do operation " << getOperatorName() << " on values of type " << lval->getType() << " and " << rval->getType() << ". Resolving invalid.\n"; return std::unique_ptr<Value>(new InvalidValue); }
std::unique_ptr<Value> ArithmeticValueNode::getValue(std::unique_ptr<Value> lval, std::unique_ptr<Value> rval) const { switch (_operator) { case ADD: { if (lval->getType() == Value::String && rval->getType() == Value::String) { StringValue& slval(static_cast<StringValue&>(*lval)); StringValue& srval(static_cast<StringValue&>(*rval)); return std::unique_ptr<Value>(new StringValue( slval.getValue() + srval.getValue())); } } [[fallthrough]]; case SUB: case MUL: case DIV: { if (lval->getType() == Value::Integer && rval->getType() == Value::Integer) { IntegerValue& ilval(static_cast<IntegerValue&>(*lval)); IntegerValue& irval(static_cast<IntegerValue&>(*rval)); IntegerValue::ValueType res = 0; switch (_operator) { case ADD: res = ilval.getValue() + irval.getValue(); break; case SUB: res = ilval.getValue() - irval.getValue(); break; case MUL: res = ilval.getValue() * irval.getValue(); break; case DIV: if (irval.getValue() != 0) { res = ilval.getValue() / irval.getValue(); } else { throw vespalib::IllegalArgumentException("Division by zero"); } break; case MOD: assert(0); } return std::unique_ptr<Value>(new IntegerValue(res, false)); } NumberValue* nlval(dynamic_cast<NumberValue*>(lval.get())); NumberValue* nrval(dynamic_cast<NumberValue*>(rval.get())); if (nlval != 0 && nrval != 0) { NumberValue::CommonValueType res = 0; switch (_operator) { case ADD: res = nlval->getCommonValue() + nrval->getCommonValue(); break; case SUB: res = nlval->getCommonValue() - nrval->getCommonValue(); break; case MUL: res = nlval->getCommonValue() * nrval->getCommonValue(); break; case DIV: if (nrval->getCommonValue() != 0) { res = nlval->getCommonValue() / nrval->getCommonValue(); } else { throw vespalib::IllegalArgumentException("Division by zero"); } break; case MOD: assert(0); } return std::unique_ptr<Value>(new FloatValue(res)); } } break; case MOD: { if (lval->getType() == Value::Integer && rval->getType() == Value::Integer) { IntegerValue& ilval(static_cast<IntegerValue&>(*lval)); IntegerValue& irval(static_cast<IntegerValue&>(*rval)); if (irval.getValue() != 0) { return std::unique_ptr<Value>(new IntegerValue(ilval.getValue() % irval.getValue(), false)); } else { throw vespalib::IllegalArgumentException("Division by zero"); } } } break; } return std::unique_ptr<Value>(new InvalidValue); }