Type* UnaryOperation::type() { if (opr() == DEREFERENCE) { auto t = operand()->type(); if (PointerType* pt = dynamic_cast<PointerType*>(t)) { auto bt = pt->baseType()->clone(); SAFE_DELETE(t); return bt; } else { SAFE_DELETE(t); return new ErrorType("dereferencing a value that is not a pointer"); } } else if (opr() == ADDRESSOF) return new PointerType(operand()->type(), false); else return operand()->type(); }
void ArccosineExpression::evaluateJacobianImpl( const MatrixID<const double>& parameterID, const MatrixID<double>& resultID, ExpressionCompiler<double>& expressionCompiler ) const { expressionCompiler.evaluateJacobian(operand(), parameterID, resultID); expressionCompiler.compute( expressionCompiler.evaluate(operand(), parameterID), resultID, [] (ConstMatrixViewXd operandValues, MatrixViewXd results) { double operandValue = operandValues.value(); double temp = 1 - operandValue * operandValue; if (temp > 0.0) { results *= -1.0 / opensolid::sqrt(temp); } else { throw Error(new PlaceholderError()); } } ); }
Expression * ArcSine::shallowReduce(Context& context, AngleUnit angleUnit) { Expression * e = Expression::shallowReduce(context, angleUnit); if (e != this) { return e; } #if MATRIX_EXACT_REDUCING if (operand(0)->type() == Type::Matrix) { return SimplificationEngine::map(this, context, angleUnit); } #endif return Trigonometry::shallowReduceInverseFunction(this, context, angleUnit); }
Expression *Typecast::rewrite() { rewriteChild(operand_); /* Convert cast of pointer to a structure to a cast of pointer to its first field. */ if (type_->isPointer() && !type_->isStructurePointer()) { if (const PointerType *pointerType = operand()->getType()->as<PointerType>()) { if (const StructType *structType = pointerType->pointeeType()->as<StructType>()) { if (const MemberDeclaration *member = structType->getMember(0)) { setOperand(std::make_unique<UnaryOperator>(tree(), UnaryOperator::REFERENCE, std::make_unique<MemberAccessOperator>(tree(), MemberAccessOperator::ARROW, releaseOperand(), member))); } } } } /* * (int32_t*)(int64_t)expr -> (int32_t*)expr */ if (type_->isScalar()) { if (Typecast *typecast = operand()->as<Typecast>()) { const Type *operandType = typecast->operand()->getType(); if (typecast->type()->isScalar() && operandType->isScalar() && type_->size() == typecast->type()->size() && typecast->type()->size() == operandType->size()) { setOperand(typecast->releaseOperand()); } } } /* This really must be the last rule. */ if (type_ == operand()->getType()) { return releaseOperand().release(); } return this; }
//命令行循环 void cmd_ln_loop(const objfile& file){ char cmd[51] = {0}; int quit = 0; int length; while (1){ std::cout<<"<-"; std::cin.getline(cmd,50); length = strlen(cmd); if (!cmd[0]){ continue; } std::string opr = strtok(cmd," "); if (opr == "la"){ const char* opr1_chp = strtok(NULL," "); const char* fn_chp = strtok(NULL, " "); if (!(opr1_chp && fn_chp)){ printf("format: la [line] [file]\n"); continue; } std::string operand1(opr1_chp); std::string filename(fn_chp); short line = (short)atoi(operand1.c_str()); int filenum = file.f_name_to_num(filename); if (filenum == -1){ printf("no file named %s found\n",filename.c_str()); continue; } try{ int addr = file.l_to_a(line,filenum); printf("%d\n",addr); }catch (std::string s){ std::cout<<s<<std::endl; } }else if (opr == "al"){ const char* addr_chp = strtok(NULL," "); if (!addr_chp){ printf("format: al [addr]\n"); continue; } std::string operand (addr_chp); int addr = (short)atoi(operand.c_str()); int filenum = file.a_to_f(addr); int line = file.a_to_l(addr,filenum); std::string filename(file.f_to_n(filenum)); printf("line %d of %s\n",line,filename.c_str()); std::cout<< getLineFromFile(filename, line)<<std::endl; }else if (opr == "q") break; Sleep(50); } }
void ScalingExpression::evaluateJacobianImpl( const MatrixID<const Interval>& parameterID, const MatrixID<Interval>& resultID, ExpressionCompiler<Interval>& expressionCompiler ) const { expressionCompiler.evaluateJacobian(operand(), parameterID, resultID); double scale = this->scale(); expressionCompiler.compute( resultID, [scale] (IntervalMatrixViewXd results) { results *= scale; } ); }
const Type *UnaryOperator::getType() const { const Type *operandType = operand()->getType(); switch (operatorKind()) { case REFERENCE: { return tree().makePointerType(operandType); } case DEREFERENCE: if (const PointerType *pointerType = operandType->as<PointerType>()) { return pointerType->pointeeType(); } else { return tree().makeErroneousType(); } break; case BITWISE_NOT: if (operandType->isInteger()) { return tree().integerPromotion(operandType); } else { return tree().makeErroneousType(); } case LOGICAL_NOT: if (operandType->isScalar()) { return tree().makeIntegerType(tree().intSize(), false); } else { return tree().makeErroneousType(); } break; case NEGATION: { if (operandType->isInteger() || operandType->isFloat()) { return operandType; } else { return tree().makeErroneousType(); } break; } case PREFIX_INCREMENT: case PREFIX_DECREMENT: if (operandType->isScalar()) { return operandType; } else { return tree().makeErroneousType(); } break; default: unreachable(); break; } return NULL; }
Expression * MatrixInverse::templatedApproximate(Context& context, AngleUnit angleUnit) const { Expression * input = operand(0)->approximate<T>(context, angleUnit); Expression * result = nullptr; if (input->type() == Type::Complex) { Complex<T> * c = static_cast<Complex<T> *>(input); result = new Complex<T>(Division::compute(Complex<T>::Cartesian(1, 0), *c)); } else { assert(input->type() == Type::Matrix); result = static_cast<Matrix *>(input)->createInverse<T>(); } if (result == nullptr) { result = new Complex<T>(Complex<T>::Float(NAN)); } delete input; return result; }
void draw(SkCanvas* canvas) { SkRegion operand({35, 35, 85, 85}); const char* labels[] = {"difference", "intersect", "union", "xor", "reverse diff", "replace"}; int index = 0; SkPaint paint; for (auto op : { SkRegion::kDifference_Op, SkRegion::kIntersect_Op, SkRegion::kUnion_Op, SkRegion::kXOR_Op, SkRegion::kReverseDifference_Op, SkRegion::kReplace_Op } ) { SkRegion target({10, 10, 60, 60}); target.op(operand, op); canvas->drawRegion(target, paint); canvas->drawString(labels[index++], 40, 100, paint); canvas->translate(80, 0); if (SkRegion::kUnion_Op == op) { canvas->translate(-240, 120); } } }
void TranslationExpression::evaluateImpl( const MatrixID<const Interval>& parameterID, const MatrixID<Interval>& resultID, ExpressionCompiler<Interval>& expressionCompiler ) const { ColumnMatrixXd columnMatrix = this->columnMatrix(); expressionCompiler.evaluate(operand(), parameterID, resultID); expressionCompiler.compute( resultID, [columnMatrix] (IntervalMatrixViewXd results) { for (int columnIndex = 0; columnIndex < results.numColumns(); ++columnIndex) { results.column(columnIndex) += columnMatrix; } } ); }
void SquareRootExpression::evaluateImpl( const MatrixID<const Interval>& parameterID, const MatrixID<Interval>& resultID, ExpressionCompiler<Interval>& expressionCompiler ) const { expressionCompiler.evaluate(operand(), parameterID, resultID); expressionCompiler.compute( resultID, [] (IntervalMatrixViewXd results) { results.setMap( results, [] (Interval value) { return opensolid::sqrt(value); } ); } ); }
void ArccosineExpression::evaluateImpl( const MatrixID<const Interval>& parameterID, const MatrixID<Interval>& resultID, ExpressionCompiler<Interval>& expressionCompiler ) const { expressionCompiler.evaluate(operand(), parameterID, resultID); expressionCompiler.compute( resultID, [] (IntervalMatrixViewXd results) { results.setMap( results, [] (Interval value) { Interval domain(-1, 1); return opensolid::acos(value.intersection(domain)); } ); } ); }
void SquaredNormExpression::evaluateImpl( const MatrixID<const Interval>& parameterID, const MatrixID<Interval>& resultID, ExpressionCompiler<Interval>& expressionCompiler ) const { expressionCompiler.compute( expressionCompiler.evaluate(operand(), parameterID), resultID, [] (ConstIntervalMatrixViewXd operandValues, IntervalMatrixViewXd results) { for (int columnIndex = 0; columnIndex < results.numColumns(); ++columnIndex) { results(0, columnIndex) = operandValues.column(columnIndex).fold( Interval(0.0), [] (Interval result, Interval value) { return result + value * value; } ); } } ); }
void convert(char prefix[],char postfix[]) { int i,j=0; char symbol, infix[50]; stack[index]='#'; //convert to infix for(i=0;i<strlen(prefix);i++) { symbol=prefix[i]; if(isoperator(symbol)==0) { infix[j]=symbol; infix[j+1]=pop(); j+=2; } else push(symbol); } infix[j]='\0'; j=0; //convert to postfix for(i=0;i<strlen(infix);i++) { symbol=infix[i]; if(isoperator(symbol)==0) { postfix[j]=symbol; j++; } else { if(symbol=='(') push(symbol); else if(symbol==')') { while(stack[index]!='(') { postfix[j]=pop(); j++; } pop(); } else { if(operand(symbol)>operand(stack[index])) push(symbol); else { while(operand(symbol)<=operand(stack[index])) { postfix[j]=pop(); j++; } push(symbol); } } } } while(stack[index]!='#') { postfix[j]=pop(); j++; } postfix[j]='\0'; }
Module* parsemod(char *path, uchar *code, ulong length, Dir *dir) { Heap *h; Inst *ip; Type *pt; String *s; Module *m; Array *ary; ulong ul[2]; WORD lo, hi; int lsize, id, v, entry, entryt, tnp, tsz, siglen; int de, pc, i, n, isize, dsize, hsize, dasp; uchar *mod, sm, *istream, **isp, *si, *addr, *dastack[DADEPTH]; Link *l; istream = code; isp = &istream; m = malloc(sizeof(Module)); if(m == nil) return nil; m->dev = dir->dev; m->dtype = dir->type; m->qid = dir->qid; m->mtime = dir->mtime; m->origmp = H; m->pctab = nil; switch(operand(isp)) { default: kwerrstr("bad magic"); goto bad; case SMAGIC: siglen = operand(isp); n = length-(*isp-code); if(n < 0 || siglen > n){ kwerrstr("corrupt signature"); goto bad; } if(verifysigner(*isp, siglen, *isp+siglen, n-siglen) == 0) { kwerrstr("security violation"); goto bad; } *isp += siglen; break; case XMAGIC: if(mustbesigned(path, code, length, dir)){ kwerrstr("security violation: not signed"); goto bad; } break; } m->rt = operand(isp); m->ss = operand(isp); isize = operand(isp); dsize = operand(isp); hsize = operand(isp); lsize = operand(isp); entry = operand(isp); entryt = operand(isp); if(isize < 0 || dsize < 0 || hsize < 0 || lsize < 0) { kwerrstr("implausible Dis file"); goto bad; } m->nprog = isize; m->prog = mallocz(isize*sizeof(Inst), 0); if(m->prog == nil) { kwerrstr(exNomem); goto bad; } m->ref = 1; ip = m->prog; for(i = 0; i < isize; i++) { ip->op = *istream++; ip->add = *istream++; ip->reg = 0; ip->s.imm = 0; ip->d.imm = 0; switch(ip->add & ARM) { case AXIMM: case AXINF: case AXINM: ip->reg = operand(isp); break; } switch(UXSRC(ip->add)) { case SRC(AFP): case SRC(AMP): case SRC(AIMM): ip->s.ind = operand(isp); break; case SRC(AIND|AFP): case SRC(AIND|AMP): ip->s.i.f = operand(isp); ip->s.i.s = operand(isp); break; } switch(UXDST(ip->add)) { case DST(AFP): case DST(AMP): ip->d.ind = operand(isp); break; case DST(AIMM): ip->d.ind = operand(isp); if(brpatch(ip, m) == 0) { kwerrstr("bad branch addr"); goto bad; } break; case DST(AIND|AFP): case DST(AIND|AMP): ip->d.i.f = operand(isp); ip->d.i.s = operand(isp); break; } ip++; } m->ntype = hsize; m->type = malloc(hsize*sizeof(Type*)); if(m->type == nil) { kwerrstr(exNomem); goto bad; } for(i = 0; i < hsize; i++) { id = operand(isp); if(id > hsize) { kwerrstr("heap id range"); goto bad; } tsz = operand(isp); tnp = operand(isp); if(tsz < 0 || tnp < 0 || tnp > 128*1024){ kwerrstr("implausible Dis file"); goto bad; } pt = dtype(freeheap, tsz, istream, tnp); if(pt == nil) { kwerrstr(exNomem); goto bad; } istream += tnp; m->type[id] = pt; } if(dsize != 0) { pt = m->type[0]; if(pt == 0 || pt->size != dsize) { kwerrstr("bad desc for mp"); goto bad; } h = heapz(pt); m->origmp = H2D(uchar*, h); }
size_t ygen_emit::opgen( ygen_program* p, size_t index ) { yssa_function* function = p->ssafunc; yssa_opinst* op = function->ops.at( index ); switch ( op->opcode ) { case YL_NOP: { p->ops.emplace_back( YL_NOP, 0, 0, 0 ); p->slocs.push_back( op->sloc ); return 1; } case YL_MOV: { assert( op->result_count == 1 ); assert( op->operand_count == 1 ); if ( op->r != yl_opinst::NOVAL && op->r != op->operand[ 0 ]->r ) { unsigned a = op->operand[ 0 ]->r; assert( a != yl_opinst::NOVAL ); p->ops.emplace_back( (yl_opcode)op->opcode, op->r, a, 0 ); p->slocs.push_back( op->sloc ); p->stackcount = std::max( p->stackcount, (size_t)op->r + 1 ); } return 1; } case YL_NULL: case YL_NEG: case YL_BITNOT: case YL_MUL: case YL_DIV: case YL_MOD: case YL_INTDIV: case YL_ADD: case YL_SUB: case YL_LSL: case YL_LSR: case YL_ASR: case YL_BITAND: case YL_BITXOR: case YL_BITOR: case YL_CONCAT: case YL_EQ: case YL_NE: case YL_LT: case YL_GT: case YL_LE: case YL_GE: case YL_LNOT: case YL_LXOR: case YL_SUPER: case YL_INKEY: case YL_INDEX: case YL_RESPONDS: case YL_IS: { assert( op->result_count == 1 ); if ( op->r != yl_opinst::NOVAL ) { unsigned a = operand( op, 0 ); unsigned b = operand( op, 1 ); p->ops.emplace_back( (yl_opcode)op->opcode, op->r, a, b ); p->slocs.push_back( op->sloc ); p->stackcount = std::max( p->stackcount, (size_t)op->r + 1 ); } return 1; } case YL_BOOL: { assert( op->result_count == 1 ); if ( op->r != yl_opinst::NOVAL ) { unsigned a = op->boolean ? 1 : 0; p->ops.emplace_back( YL_BOOL, op->r, a, 0 ); p->slocs.push_back( op->sloc ); p->stackcount = std::max( p->stackcount, (size_t)op->r + 1 ); } return 1; } case YL_NUMBER: { assert( op->result_count == 1 ); if ( op->r != yl_opinst::NOVAL ) { unsigned c = (unsigned)p->numvals.at( op->number ); p->ops.emplace_back( YL_NUMBER, op->r, c ); p->slocs.push_back( op->sloc ); p->stackcount = std::max( p->stackcount, (size_t)op->r + 1 ); } return 1; } case YL_STRING: { assert( op->result_count == 1 ); if ( op->r != yl_opinst::NOVAL ) { symkey k( op->string->string, op->string->length ); unsigned c = (unsigned)p->strvals.at( k ); p->ops.emplace_back( YL_STRING, op->r, c ); p->slocs.push_back( op->sloc ); p->stackcount = std::max( p->stackcount, (size_t)op->r + 1 ); } return 1; } case YL_GLOBAL: { assert( op->result_count == 1 ); if ( op->r != yl_opinst::NOVAL ) { unsigned b = (unsigned)p->strvals.at( op->key ); p->ops.emplace_back( YL_GLOBAL, op->r, 0, b ); p->slocs.push_back( op->sloc ); p->ops.emplace_back( YL_ILCACHE, 0, (unsigned)( p->ilcount++ ) ); p->slocs.push_back( op->sloc ); p->stackcount = std::max( p->stackcount, (size_t)op->r + 1 ); } return 1; } case YL_SETGLOBAL: { assert( op->result_count == 0 ); unsigned r = operand( op, 0 ); unsigned b = (unsigned)p->strvals.at( op->key ); p->ops.emplace_back( YL_SETGLOBAL, r, 0, b ); p->slocs.push_back( op->sloc ); p->ops.emplace_back( YL_ILCACHE, 0, (unsigned)( p->ilcount++ ) ); p->slocs.push_back( op->sloc ); return 1; } case YL_FUNCTION: { if ( op->r != yl_opinst::NOVAL ) { unsigned c = (unsigned)p->funvals.at( op->function ); p->ops.emplace_back( YL_FUNCTION, op->r, c ); p->slocs.push_back( op->sloc ); p->stackcount = std::max( p->stackcount, (size_t)op->r + 1 ); } size_t n; for ( n = 1; index + n < function->ops.size(); ++n ) { yssa_opinst* up = function->ops.at( index + n ); if ( up->opcode == YL_UPLOCAL ) { if ( op->r != yl_opinst::NOVAL ) { unsigned b = operand( up, 0 ); p->ops.emplace_back( YL_UPLOCAL, up->r, up->a, b ); p->slocs.push_back( op->sloc ); p->localupcount = std::max( p->localupcount, (size_t)up->a + 1 ); } } else if ( up->opcode == YL_UPUPVAL ) { if ( op->r != yl_opinst::NOVAL ) { p->ops.emplace_back( YL_UPUPVAL, up->r, up->a, 0 ); p->slocs.push_back( op->sloc ); } } else { break; } } return n; } case YL_VARARG: case YL_CALL: case YL_YCALL: case YL_YIELD: case YL_RETURN: case YL_NEXT: case YL_EXTEND: case YL_UNPACK: { assert( op->is_call() ); // These ops can either produce or consume multiple values. We must // treat chains of multivals as a single operation on top of stack. yssa_opinst* multival = op; yssa_opinst* firstop = op; yssa_opinst* finalop = op; size_t ilast = index + 1; for ( ; ilast < function->ops.size(); ++ilast ) { yssa_opinst* op = function->ops.at( ilast ); if ( op->opcode == YSSA_IMPLICIT ) { continue; } if ( op->is_call() && op->multival == multival ) { // Follow the multival chain. multival = op; finalop = op; continue; } // Multival results must be consumed by the next instruction. assert( ! op->has_multival() || ! op->multival ); break; } // Get all operands into the correct registers. ygen_movgraph arguments; for ( size_t iop = index; iop < ilast; ++iop ) { op = function->ops.at( iop ); if ( op->opcode == YSSA_IMPLICIT ) { continue; } // First argument of UNPACK and EXTEND can be any register. unsigned first = 0; if ( op->opcode == YL_UNPACK || op->opcode == YL_EXTEND ) { unsigned a = operand( op, first++ ); if ( a >= multival->stacktop ) { arguments.move( op->stacktop, a ); op->operand[ 0 ]->r = op->stacktop; } } // Move remaining operands. for ( unsigned iarg = first; iarg < op->operand_count; ++iarg ) { unsigned a = operand( op, iarg ); unsigned r = op->stacktop + iarg - first; arguments.move( r, a ); } } arguments.emit( p, firstop->sloc ); // Emit instructions. for ( size_t iop = index; iop < ilast; ++iop ) { op = function->ops.at( iop ); if ( op->opcode == YSSA_IMPLICIT ) { continue; } assert( op->is_call() ); assert( op->stacktop != yl_opinst::NOVAL ); switch ( op->opcode ) { case YL_VARARG: case YL_CALL: case YL_YCALL: case YL_YIELD: case YL_RETURN: { unsigned a = op->multival ? yl_opinst::MARK : op->operand_count; unsigned b = op->result_count; p->ops.emplace_back( (yl_opcode)op->opcode, op->stacktop, a, b ); p->slocs.push_back( op->sloc ); stack( p, op->stacktop, op->result_count ); break; } case YL_NEXT: { p->ops.emplace_back( YL_NEXT, op->stacktop, op->a, op->result_count ); p->slocs.push_back( op->sloc ); stack( p, op->stacktop, op->result_count ); break; } case YL_EXTEND: { unsigned a = op->multival ? yl_opinst::MARK : op->operand_count; unsigned b = operand( op, 0 ); p->ops.emplace_back( YL_EXTEND, op->stacktop, a, b ); p->slocs.push_back( op->sloc ); break; } case YL_UNPACK: { unsigned a = operand( op, 0 ); p->ops.emplace_back( YL_UNPACK, op->stacktop, a, op->result_count ); p->slocs.push_back( op->sloc ); stack( p, op->stacktop, op->result_count ); break; } default: assert( ! "unexpected multival operation" ); break; } } // Results of final op. op = finalop; assert( op->is_call() ); // RETURN and EXTEND don't return any results. if ( op->opcode == YL_RETURN || op->opcode == YL_EXTEND ) { assert( op->result_count == 0 ); return ilast - index; } // Move all results into the correct registers. ygen_movgraph results; if ( op->r != yl_opinst::NOVAL ) { results.move( op->r, op->stacktop ); } for ( ; ilast < function->ops.size(); ++ilast ) { yssa_opinst* sel = function->ops.at( ilast ); if ( sel->opcode == YSSA_IMPLICIT ) { continue; } if ( sel->opcode == YSSA_SELECT ) { assert( sel->operand_count == 1 ); assert( sel->operand[ 0 ] == op ); if ( sel->r != yl_opinst::NOVAL ) { results.move( sel->r, op->stacktop + sel->select ); } continue; } break; } results.emit( p, finalop->sloc ); return ilast - index; } case YL_ITER: case YL_ITERKEY: { unsigned a = operand( op, 0 ); p->ops.emplace_back( (yl_opcode)op->opcode, op->r, a, 0 ); p->slocs.push_back( op->sloc ); p->itercount = std::max( p->itercount, (size_t)op->r + 1 ); return 1; } case YL_NEXT1: { if ( op->r != yl_opinst::NOVAL ) { p->ops.emplace_back( YL_NEXT1, op->r, op->a ); p->slocs.push_back( op->sloc ); p->stackcount = std::max( p->stackcount, (size_t)op->r + 1 ); } return 1; } case YL_NEXT2: { // Find selects. unsigned r = yl_opinst::NOVAL; unsigned b = yl_opinst::NOVAL; size_t n; for ( n = 1; index + n < function->ops.size(); ++n ) { yssa_opinst* sel = function->ops.at( index + n ); if ( sel->opcode == YSSA_SELECT ) { assert( sel->operand_count == 1 ); assert( sel->operand[ 0 ] == op ); if ( sel->select == 0 ) { r = sel->r; } else if ( sel->select == 1 ) { b = sel->r; } else { assert( ! "invalid select index" ); } } else { break; } } assert( r != yl_opinst::NOVAL ); assert( b != yl_opinst::NOVAL ); p->ops.emplace_back( YL_NEXT2, r, op->a, b ); p->slocs.push_back( op->sloc ); p->stackcount = std::max( p->stackcount, (size_t)r + 1 ); p->stackcount = std::max( p->stackcount, (size_t)b + 1 ); return n; } case YL_GETUP: { if ( op->r != yl_opinst::NOVAL ) { p->ops.emplace_back( YL_GETUP, op->r, op->a, 0 ); p->slocs.push_back( op->sloc ); p->stackcount = std::max( p->stackcount, (size_t)op->r + 1 ); } return 1; } case YL_SETUP: { p->ops.emplace_back( YL_SETUP, operand( op, 0 ), op->a, 0 ); p->slocs.push_back( op->sloc ); return 1; } case YL_OBJECT: { assert( op->result_count == 1 ); if ( op->r != yl_opinst::NOVAL ) { unsigned a = op->operand_count ? operand( op, 0 ) : yl_opinst::NOVAL; p->ops.emplace_back( (yl_opcode)op->opcode, op->r, a, 0 ); p->slocs.push_back( op->sloc ); p->stackcount = std::max( p->stackcount, (size_t)op->r + 1 ); } return 1; } case YL_CLOSE: { p->ops.emplace_back( YL_CLOSE, 0, op->a, op->b ); p->slocs.push_back( op->sloc ); return 1; } case YL_ARRAY: case YL_TABLE: { if ( op->r != yl_opinst::NOVAL ) { p->ops.emplace_back( (yl_opcode)op->opcode, op->r, op->c ); p->slocs.push_back( op->sloc ); p->stackcount = std::max( p->stackcount, (size_t)op->r + 1 ); } return 1; } case YL_KEY: { if ( op->r != yl_opinst::NOVAL ) { unsigned b = (unsigned)p->strvals.at( op->key ); p->ops.emplace_back( YL_KEY, op->r, operand( op, 0 ), b ); p->slocs.push_back( op->sloc ); p->ops.emplace_back( YL_ILCACHE, 0, (unsigned)( p->ilcount++ ) ); p->slocs.push_back( op->sloc ); p->stackcount = std::max( p->stackcount, (size_t)op->r + 1 ); } return 1; } case YL_SETKEY: { unsigned r = operand( op, 0 ); unsigned a = operand( op, 1 ); unsigned b = (unsigned)p->strvals.at( op->key ); p->ops.emplace_back( YL_SETKEY, r, a, b ); p->slocs.push_back( op->sloc ); p->ops.emplace_back( YL_ILCACHE, 0, (unsigned)( p->ilcount++ ) ); p->slocs.push_back( op->sloc ); return 1; } case YL_SETINKEY: case YL_SETINDEX: { assert( op->operand_count == 3 ); unsigned r = operand( op, 0 ); unsigned a = operand( op, 1 ); unsigned b = operand( op, 2 ); p->ops.emplace_back( (yl_opcode)op->opcode, r, a, b ); p->slocs.push_back( op->sloc ); return 1; } case YL_DELKEY: { unsigned a = operand( op, 0 ); unsigned b = (unsigned)p->strvals.at( op->key ); p->ops.emplace_back( YL_DELKEY, 0, a, b ); p->slocs.push_back( op->sloc ); return 1; } case YL_DELINKEY: { unsigned a = operand( op, 0 ); unsigned b = operand( op, 1 ); p->ops.emplace_back( YL_DELINKEY, 0, a, b ); p->slocs.push_back( op->sloc ); return 1; } case YL_APPEND: { unsigned a = operand( op, 0 ); unsigned r = operand( op, 1 ); p->ops.emplace_back( YL_APPEND, r, a, 0 ); p->slocs.push_back( op->sloc ); return 1; } case YL_THROW: { p->ops.emplace_back( YL_THROW, operand( op, 0 ), 0, 0 ); p->slocs.push_back( op->sloc ); return 1; } case YL_EXCEPT: { if ( op->r != yl_opinst::NOVAL ) { p->ops.emplace_back( YL_EXCEPT, op->r, 0, 0 ); p->slocs.push_back( op->sloc ); p->stackcount = std::max( p->stackcount, (size_t)op->r + 1 ); } return 1; } case YL_HANDLE: { p->ops.emplace_back( YL_HANDLE, 0, 0, 0 ); p->slocs.push_back( op->sloc ); return 1; } case YL_UNWIND: { p->ops.emplace_back( YL_UNWIND, 0, 0, 0 ); p->slocs.push_back( op->sloc ); return 1; } case YSSA_PARAM: { // Get all parameters into the correct registers. ygen_movgraph params; size_t n; for ( n = 0; index + n < function->ops.size(); ++n ) { yssa_opinst* param = function->ops.at( index + n ); if ( param->opcode == YSSA_PARAM ) { unsigned a = param->select + 1; p->stackcount = std::max( p->stackcount, (size_t)a + 1 ); if ( param->r != yl_opinst::NOVAL ) { params.move( param->r, a ); } } else { break; } } params.emit( p, op->sloc ); return n; } case YSSA_IMPLICIT: case YSSA_ITEREACH: case YSSA_PHI: case YSSA_VAR: { // Ignore these ops. return 1; } case YL_SWP: case YL_JMP: case YL_JMPT: case YL_JMPF: case YL_JMPV: case YL_JMPN: case YL_UPLOCAL: case YL_UPUPVAL: case YSSA_SELECT: case YSSA_REF: default: { assert( ! "unexpected SSA op" ); return 1; } } }
void Typecast::visitChildNodes(Visitor<TreeNode> &visitor) { visitor(operand()); }
Abakus::Number BuiltinFunction::derivative() const { Abakus::Number du = operand()->derivative(); Abakus::Number value = operand()->value(); Abakus::Number one(1), zero(0); if(du == zero) return du; // In case these functions get added later, these derivatives may // be useful: // d/dx(asinh u) = (du/dx * 1 / sqrt(x^2 + 1)) // d/dx(acosh u) = (du/dx * 1 / sqrt(x^2 - 1)) // d/dx(atanh u) = (du/dx * 1 / (1 - x^2)) // This is very unfortunate duplication. if(name() == "sin") return value.cos() * du; else if(name() == "cos") return -value.sin() * du; else if(name() == "tan") { Abakus::Number cosResult; cosResult = value.cos(); cosResult = cosResult * cosResult; return one / cosResult; } else if(name() == "asinh") { value = value * value + one; return du / value.sqrt(); } else if(name() == "acosh") { value = value * value - one; return du / value.sqrt(); } else if(name() == "atanh") { value = one - value * value; return du / value; } else if(name() == "sinh") { return du * value.cosh(); } else if(name() == "cosh") { return du * value.sinh(); // Yes the sign is correct. } else if(name() == "tanh") { Abakus::Number tanh = value.tanh(); return du * (one - tanh * tanh); } else if(name() == "atan") { return one * du / (one + value * value); } else if(name() == "acos") { // Same as asin but with inverted sign. return -(one / (value * value - one).sqrt() * du); } else if(name() == "asin") { return one / (value * value - one).sqrt() * du; } else if(name() == "ln") { return du / value; } else if(name() == "exp") { return du * value.exp(); } else if(name() == "log") { return du / value / Abakus::Number(10).ln(); } else if(name() == "sqrt") { Abakus::Number half("0.5"); return half * value.pow(-half) * du; } else if(name() == "abs") { return (value / value.abs()) * du; } // Approximate it. Abakus::Number epsilon("1e-15"); Abakus::Number fxh = evaluateFunction(function(), value + epsilon); Abakus::Number fx = evaluateFunction(function(), value); return (fxh - fx) / epsilon; }
void UnaryOperator::visitChildNodes(Visitor<TreeNode> &visitor) { visitor(operand()); }
CalcChecker::CalcChecker() : ANTLR_USE_NAMESPACE(antlr)TreeParser() { } void CalcChecker::program(ANTLR_USE_NAMESPACE(antlr)RefAST _t) { ANTLR_USE_NAMESPACE(antlr)RefAST program_AST_in = (_t == ANTLR_USE_NAMESPACE(antlr)RefAST(ASTNULL)) ? ANTLR_USE_NAMESPACE(antlr)nullAST : _t; try { // for error handling ANTLR_USE_NAMESPACE(antlr)RefAST __t71 = _t; ANTLR_USE_NAMESPACE(antlr)RefAST tmp1_AST_in = _t; match(_t,PROGRAM_AST); _t = _t->getFirstChild(); { // ( ... )+ int _cnt73=0; for (;;) { if (_t == ANTLR_USE_NAMESPACE(antlr)nullAST ) _t = ASTNULL; switch ( _t->getType()) { case DECL: { declaration(_t); _t = _retTree; break; } case PRINT: case BECOMES: { statement(_t); _t = _retTree; break; } default: { if ( _cnt73>=1 ) { goto _loop73; } else {throw ANTLR_USE_NAMESPACE(antlr)NoViableAltException(_t);} } } _cnt73++; } _loop73:; } // ( ... )+ _t = __t71; _t = _t->getNextSibling(); } catch (ANTLR_USE_NAMESPACE(antlr)RecognitionException& ex) { reportError(ex); if ( _t != ANTLR_USE_NAMESPACE(antlr)nullAST ) _t = _t->getNextSibling(); } _retTree = _t; } void CalcChecker::declaration(ANTLR_USE_NAMESPACE(antlr)RefAST _t) { ANTLR_USE_NAMESPACE(antlr)RefAST declaration_AST_in = (_t == ANTLR_USE_NAMESPACE(antlr)RefAST(ASTNULL)) ? ANTLR_USE_NAMESPACE(antlr)nullAST : _t; ANTLR_USE_NAMESPACE(antlr)RefAST id = ANTLR_USE_NAMESPACE(antlr)nullAST; try { // for error handling ANTLR_USE_NAMESPACE(antlr)RefAST __t75 = _t; ANTLR_USE_NAMESPACE(antlr)RefAST tmp2_AST_in = _t; match(_t,DECL); _t = _t->getFirstChild(); type(_t); _t = _retTree; id = _t; match(_t,IDENTIFIER); _t = _t->getNextSibling(); _t = __t75; _t = _t->getNextSibling(); #line 231 "Calc.g" if (isDeclared(id->getText())){ cerr << id->getText() << " is already declared" << endl; exit(-1); } else declare(id->getText()); #line 88 "CalcChecker.cpp" } catch (ANTLR_USE_NAMESPACE(antlr)RecognitionException& ex) { reportError(ex); if ( _t != ANTLR_USE_NAMESPACE(antlr)nullAST ) _t = _t->getNextSibling(); } _retTree = _t; } void CalcChecker::statement(ANTLR_USE_NAMESPACE(antlr)RefAST _t) { ANTLR_USE_NAMESPACE(antlr)RefAST statement_AST_in = (_t == ANTLR_USE_NAMESPACE(antlr)RefAST(ASTNULL)) ? ANTLR_USE_NAMESPACE(antlr)nullAST : _t; ANTLR_USE_NAMESPACE(antlr)RefAST id = ANTLR_USE_NAMESPACE(antlr)nullAST; try { // for error handling if (_t == ANTLR_USE_NAMESPACE(antlr)nullAST ) _t = ASTNULL; switch ( _t->getType()) { case BECOMES: { ANTLR_USE_NAMESPACE(antlr)RefAST __t77 = _t; ANTLR_USE_NAMESPACE(antlr)RefAST tmp3_AST_in = _t; match(_t,BECOMES); _t = _t->getFirstChild(); id = _t; match(_t,IDENTIFIER); _t = _t->getNextSibling(); expr(_t); _t = _retTree; _t = __t77; _t = _t->getNextSibling(); #line 243 "Calc.g" if (!isDeclared(id->getText())) { cerr << id->getText() << " is not declared" << endl; exit(-1); } //TODO: type checking #line 130 "CalcChecker.cpp" break; } case PRINT: { ANTLR_USE_NAMESPACE(antlr)RefAST __t78 = _t; ANTLR_USE_NAMESPACE(antlr)RefAST tmp4_AST_in = _t; match(_t,PRINT); _t = _t->getFirstChild(); expr(_t); _t = _retTree; _t = __t78; _t = _t->getNextSibling(); break; } default: { throw ANTLR_USE_NAMESPACE(antlr)NoViableAltException(_t); } } } catch (ANTLR_USE_NAMESPACE(antlr)RecognitionException& ex) { reportError(ex); if ( _t != ANTLR_USE_NAMESPACE(antlr)nullAST ) _t = _t->getNextSibling(); } _retTree = _t; } void CalcChecker::type(ANTLR_USE_NAMESPACE(antlr)RefAST _t) { ANTLR_USE_NAMESPACE(antlr)RefAST type_AST_in = (_t == ANTLR_USE_NAMESPACE(antlr)RefAST(ASTNULL)) ? ANTLR_USE_NAMESPACE(antlr)nullAST : _t; try { // for error handling if (_t == ANTLR_USE_NAMESPACE(antlr)nullAST ) _t = ASTNULL; switch ( _t->getType()) { case INT: { ANTLR_USE_NAMESPACE(antlr)RefAST tmp5_AST_in = _t; match(_t,INT); _t = _t->getNextSibling(); break; } case FLOAT: { ANTLR_USE_NAMESPACE(antlr)RefAST tmp6_AST_in = _t; match(_t,FLOAT); _t = _t->getNextSibling(); break; } default: { throw ANTLR_USE_NAMESPACE(antlr)NoViableAltException(_t); } } } catch (ANTLR_USE_NAMESPACE(antlr)RecognitionException& ex) { reportError(ex); if ( _t != ANTLR_USE_NAMESPACE(antlr)nullAST ) _t = _t->getNextSibling(); } _retTree = _t; } void CalcChecker::expr(ANTLR_USE_NAMESPACE(antlr)RefAST _t) { ANTLR_USE_NAMESPACE(antlr)RefAST expr_AST_in = (_t == ANTLR_USE_NAMESPACE(antlr)RefAST(ASTNULL)) ? ANTLR_USE_NAMESPACE(antlr)nullAST : _t; try { // for error handling if (_t == ANTLR_USE_NAMESPACE(antlr)nullAST ) _t = ASTNULL; switch ( _t->getType()) { case INT: case FLOAT: case IDENTIFIER: { operand(_t); _t = _retTree; break; } case PLUS: { ANTLR_USE_NAMESPACE(antlr)RefAST __t80 = _t; ANTLR_USE_NAMESPACE(antlr)RefAST tmp7_AST_in = _t; match(_t,PLUS); _t = _t->getFirstChild(); expr(_t); _t = _retTree; expr(_t); _t = _retTree; _t = __t80; _t = _t->getNextSibling(); #line 258 "Calc.g" /*TODO: type checking*/ #line 223 "CalcChecker.cpp" break; } case MINUS: { ANTLR_USE_NAMESPACE(antlr)RefAST __t81 = _t; ANTLR_USE_NAMESPACE(antlr)RefAST tmp8_AST_in = _t; match(_t,MINUS); _t = _t->getFirstChild(); expr(_t); _t = _retTree; expr(_t); _t = _retTree; _t = __t81; _t = _t->getNextSibling(); #line 259 "Calc.g" /*TODO: type checking*/ #line 240 "CalcChecker.cpp" break; } case MUL: { ANTLR_USE_NAMESPACE(antlr)RefAST __t82 = _t; ANTLR_USE_NAMESPACE(antlr)RefAST tmp9_AST_in = _t; match(_t,MUL); _t = _t->getFirstChild(); expr(_t); _t = _retTree; expr(_t); _t = _retTree; _t = __t82; _t = _t->getNextSibling(); #line 260 "Calc.g" /*TODO: type checking*/ #line 257 "CalcChecker.cpp" break; } case DIV: { ANTLR_USE_NAMESPACE(antlr)RefAST __t83 = _t; ANTLR_USE_NAMESPACE(antlr)RefAST tmp10_AST_in = _t; match(_t,DIV); _t = _t->getFirstChild(); expr(_t); _t = _retTree; expr(_t); _t = _retTree; _t = __t83; _t = _t->getNextSibling(); #line 261 "Calc.g" /*TODO: type checking*/ #line 274 "CalcChecker.cpp" break; } case POW: { ANTLR_USE_NAMESPACE(antlr)RefAST __t84 = _t; ANTLR_USE_NAMESPACE(antlr)RefAST tmp11_AST_in = _t; match(_t,POW); _t = _t->getFirstChild(); expr(_t); _t = _retTree; expr(_t); _t = _retTree; _t = __t84; _t = _t->getNextSibling(); #line 262 "Calc.g" /*TODO: type checking*/ #line 291 "CalcChecker.cpp" break; } case SIGN_MINUS: { ANTLR_USE_NAMESPACE(antlr)RefAST __t85 = _t; ANTLR_USE_NAMESPACE(antlr)RefAST tmp12_AST_in = _t; match(_t,SIGN_MINUS); _t = _t->getFirstChild(); expr(_t); _t = _retTree; _t = __t85; _t = _t->getNextSibling(); #line 263 "Calc.g" /*TODO: type checking*/ #line 306 "CalcChecker.cpp" break; } default: { throw ANTLR_USE_NAMESPACE(antlr)NoViableAltException(_t); } } } catch (ANTLR_USE_NAMESPACE(antlr)RecognitionException& ex) { reportError(ex); if ( _t != ANTLR_USE_NAMESPACE(antlr)nullAST ) _t = _t->getNextSibling(); } _retTree = _t; } void CalcChecker::operand(ANTLR_USE_NAMESPACE(antlr)RefAST _t) { ANTLR_USE_NAMESPACE(antlr)RefAST operand_AST_in = (_t == ANTLR_USE_NAMESPACE(antlr)RefAST(ASTNULL)) ? ANTLR_USE_NAMESPACE(antlr)nullAST : _t; ANTLR_USE_NAMESPACE(antlr)RefAST id = ANTLR_USE_NAMESPACE(antlr)nullAST; try { // for error handling if (_t == ANTLR_USE_NAMESPACE(antlr)nullAST ) _t = ASTNULL; switch ( _t->getType()) { case IDENTIFIER: { id = _t; match(_t,IDENTIFIER); _t = _t->getNextSibling(); #line 268 "Calc.g" if (!isDeclared(id->getText())){ cerr << id->getText() << " is not declared" << endl; exit(-1); } #line 342 "CalcChecker.cpp" break; } case INT: { ANTLR_USE_NAMESPACE(antlr)RefAST tmp13_AST_in = _t; match(_t,INT); _t = _t->getNextSibling(); break; } case FLOAT: { ANTLR_USE_NAMESPACE(antlr)RefAST tmp14_AST_in = _t; match(_t,FLOAT); _t = _t->getNextSibling(); break; } default: { throw ANTLR_USE_NAMESPACE(antlr)NoViableAltException(_t); } } } catch (ANTLR_USE_NAMESPACE(antlr)RecognitionException& ex) { reportError(ex); if ( _t != ANTLR_USE_NAMESPACE(antlr)nullAST ) _t = _t->getNextSibling(); } _retTree = _t; } void CalcChecker::initializeASTFactory( ANTLR_USE_NAMESPACE(antlr)ASTFactory& ) { } const char* CalcChecker::tokenNames[] = { "<0>", "EOF", "<2>", "NULL_TREE_LOOKAHEAD", "\"print\"", "\"int\"", "\"float\"", "BECOMES", "PLUS", "MINUS", "MUL", "DIV", "MOD", "POW", "COLON", "SEMICOLON", "LPAREN", "RPAREN", "LBRACKET", "RBRACKET", "COMMA", "LOWER", "UPPER", "DIGIT", "IDENTIFIER", "NUMBER", "COMMENT", "WS", "PROGRAM_AST", "DECL", "SIGN_MINUS", "SIGN_PLUS", 0 };
EXPORT_C TInteger& TInteger::operator*=(TInt aOperand) { TStackInteger64 operand(aOperand); *this *= operand; return *this; }
Expression *UnaryOperator::rewrite() { rewriteChild(operand_); if (operatorKind() == BITWISE_NOT && operand()->getType()->size() == 1) { setOperatorKind(LOGICAL_NOT); } switch (operatorKind()) { case DEREFERENCE: { if (UnaryOperator *unary = operand()->as<UnaryOperator>()) { if (unary->operatorKind() == REFERENCE) { return unary->releaseOperand().release(); } } return this; } case LOGICAL_NOT: { while (Typecast *typecast = operand()->as<Typecast>()) { if (typecast->type()->isScalar() && typecast->operand()->getType()->isScalar()) { setOperand(typecast->releaseOperand()); } else { break; } } if (BinaryOperator *binary = operand()->as<BinaryOperator>()) { switch (binary->operatorKind()) { case BinaryOperator::EQ: binary->setOperatorKind(BinaryOperator::NEQ); return releaseOperand().release(); case BinaryOperator::NEQ: binary->setOperatorKind(BinaryOperator::EQ); return releaseOperand().release(); case BinaryOperator::LT: binary->setOperatorKind(BinaryOperator::GEQ); return releaseOperand().release(); case BinaryOperator::LEQ: binary->setOperatorKind(BinaryOperator::GT); return releaseOperand().release(); case BinaryOperator::GT: binary->setOperatorKind(BinaryOperator::LEQ); return releaseOperand().release(); case BinaryOperator::GEQ: binary->setOperatorKind(BinaryOperator::LT); return releaseOperand().release(); default: break; } } if (UnaryOperator *unary = operand()->as<UnaryOperator>()) { if (unary->operatorKind() == LOGICAL_NOT) { if (unary->operand()->getType()->size() == 1) { return unary->releaseOperand().release(); } } } return this; } default: return this; } }
void UnaryOperator::doCallOnChildren(const std::function<void(TreeNode *)> &fun) { fun(operand()); }
void Parser::_bool() { operand(); rop(); operand(); }
int main(int argc, char **argv) { setlocale(LC_NUMERIC, "C"); program_name = argv[0]; register FILE *fp; register int k; register char c; register int gfil = 0; char *file[50]; char *operand(int *argcp, char ***argvp); while (--argc) { if (**++argv != '-') file[gfil++] = *argv; else switch (c = (*argv)[1]) { case 0: file[gfil++] = NULL; break; case 'C': /* compatibility mode */ compatibility_flag = TRUE; break; case 'F': /* font path to find DESC */ font::command_line_font_dir(operand(&argc, &argv)); break; case 'T': /* final output typesetter name */ device = operand(&argc, &argv); break; case 'M': /* set library directory */ macro_path.command_line_dir(operand(&argc, &argv)); break; case 's': /* preserve order of elements */ sflag = 1; break; case '-': if (strcmp(*argv,"--version")==0) { case 'v': printf("GNU grn (groff) version %s\n", Version_string); exit(0); break; } if (strcmp(*argv,"--help")==0) { case '?': usage(stdout); exit(0); break; } // fallthrough default: error("unknown switch: %1", c); usage(stderr); exit(1); } } getres(); /* set the resolution for an output device */ if (gfil == 0) { /* no filename, use standard input */ file[0] = NULL; gfil++; } for (k = 0; k < gfil; k++) { if (file[k] != NULL) { if ((fp = fopen(file[k], "r")) == NULL) fatal("can't open %1", file[k]); } else fp = stdin; while (doinput(fp)) { if (*c1 == '.' && *c2 == 'G' && *c3 == 'S') { if (compatibility_flag || *c4 == '\n' || *c4 == ' ' || *c4 == '\0') conv(fp, linenum); else fputs(inputline, stdout); } else fputs(inputline, stdout); } } return 0; }
int Division::polynomialDegree(char symbolName) const { if (operand(1)->polynomialDegree(symbolName) != 0) { return -1; } return operand(0)->polynomialDegree(symbolName); }
ObjectBoolean *ObjectInteger::operator_boolean_not(void) { std::unique_ptr<ObjectBoolean> operand(as_boolean()); return operand->operator_boolean_not(); }
void UnaryFunction::applyMap(NodeFunctor &fn) const { fn(operand()); fn(this); }
ExpressionLayout * AbsoluteValue::privateCreateLayout(PrintFloat::Mode floatDisplayMode, ComplexFormat complexFormat) const { assert(floatDisplayMode != PrintFloat::Mode::Default); assert(complexFormat != ComplexFormat::Default); return new AbsoluteValueLayout(operand(0)->createLayout(floatDisplayMode, complexFormat), false); }
QString UnaryFunction::infixString() const { return QString("%1(%2)").arg(name(), operand()->infixString()); }