/* writeValue - write a value */
bool write_ctx::writeValue(value v)
{
    if (v == UNDEFINED_VALUE)
        return s->put(CsFaslTagUndefined);
    else if (v == NULL_VALUE)
        return s->put(CsFaslTagNull);
    else if (v == TRUE_VALUE)
        return s->put(CsFaslTagTrue);
    else if (v == FALSE_VALUE)
        return s->put(CsFaslTagFalse);
    else if (CsCompiledCodeP(v))
        return writeCodeValue(v);
    else if (CsVectorP(v))
    {
        uint n = 0;
        if(object2id.find(v,n))
          return writeProxyValue(n);
        else
        {
          n = object2id.size();
          object2id[v] = n;
          return writeVectorValue(v);
        }
    }
    else if (CsObjectP(v))
    {
        uint n = 0;
        if(object2id.find(v,n))
          return writeProxyValue(n);
        else
        {
          n = object2id.size();
          object2id[v] = n;
          return writeObjectValue(v);
        }
    }
    else if (CsSymbolP(v))
        return writeSymbolValue(v);
    else if (CsStringP(v))
        return writeStringValue(v);
    else if (CsIntegerP(v))
        return writeIntegerValue(v);
    else if (CsFloatP(v))
        return writeFloatValue(v);
    else if (CsTupleP(v))
        return writeTupleValue(v);
    else if (CsByteVectorP(v))
        return writeByteVectorValue(v);
    else if (CsDateP(c,v))
        return writeDateValue(v);
    else if (CsColorP(v))
        return writeColorValue(v);
    else if (CsLengthP(v))
        return writeLengthValue(v);
    else if (CsAngleP(v))
        return writeAngleValue(v);
    else if (CsDurationP(v))
        return writeDurationValue(v);
    else {
#ifdef _DEBUG
        dispatch* pd = CsGetDispatch(v);
#endif
        assert(false);
        return false;
    }
}
Пример #2
0
/* CsDecodeInstruction - decode a single bytecode instruction */
int CsDecodeInstruction(VM *c,value code,int lc,stream *s)
{
    char buf[100];
    value name;
    byte *cp;
    int i,cnt,n=1;
    OTDEF *op;

    /* get bytecode pointer for this instruction and the method name */
    cp = CsByteVectorAddress(CsCompiledCodeBytecodes(code)) + lc;
    name = CsCompiledCodeName(code);
    
    /* show the address and opcode */
    if (CsStringP(name)) {
        //char *data = CsStringAddress(name);
        //long size = CsStringSize(name);
        //if (size > 32) size = 32;
        //strncpy(buf,(char *)data,(size_t)size);
        //sprintf(&buf[size],":%04x %02x ",lc,*cp);
        s->printf(L"%S:%04x %02x",CsStringAddress(name),lc,*cp);
    }
    else
        s->printf(L"%08lx:%04x %02x ",(long)code,lc,*cp);
    //s->put_str(buf);

    /* display the operands */
    for (op = otab; op->ot_name; ++op)
        if (*cp == op->ot_code) {
            switch (op->ot_fmt) {
            case FMT_NONE:
                sprintf(buf,"      %s\n",op->ot_name);
                s->put_str(buf);
                break;
            case FMT_BYTE:
                sprintf(buf,"%02x    %s %02x\n",cp[1],op->ot_name,cp[1]);
                s->put_str(buf);
                n += 1;
                break;
            case FMT_2BYTE:
                sprintf(buf,"%02x %02x %s %02x %02x\n",cp[1],cp[2],
                        op->ot_name,cp[1],cp[2]);
                s->put_str(buf);
                n += 2;
                break;
            case FMT_WORD:
                sprintf(buf,"%02x %02x %s %02x%02x\n",cp[1],cp[2],
                        op->ot_name,cp[2],cp[1]);
                s->put_str(buf);
                n += 2;
                break;
            case FMT_LIT:
                sprintf(buf,"%02x %02x %s %02x%02x ; ",cp[1],cp[2],
                        op->ot_name,cp[2],cp[1]);
                s->put_str(buf);
                CsPrint(c,CsCompiledCodeLiteral(code,(cp[2] << 8) | cp[1]),s);
                s->put('\n');
                n += 2;
                break;
            case FMT_SWITCH:
                sprintf(buf,"%02x %02x %s %02x%02x\n",cp[1],cp[2],
                        op->ot_name,cp[2],cp[1]);
                s->put_str(buf);
                cnt = cp[2] << 8 | cp[1];
                n += 2 + cnt * 4 + 2;
                i = 3;
                while (--cnt >= 0) {
                    sprintf(buf,"                 %02x%02x %02x%02x ; ",cp[i+1],cp[i],cp[i+3],cp[i+2]);
                    s->put_str(buf);
                    CsPrint(c,CsCompiledCodeLiteral(code,(cp[i+1] << 8) | cp[i]),s);
                    s->put('\n');
                    i += 4;
                }
                sprintf(buf,"                 %02x%02x\n",cp[i+1],cp[i]);
                s->put_str(buf);
                break;
            }
            return n;
        }
    
    /* unknown opcode */
    sprintf(buf,"      <UNKNOWN>\n");
    s->put_str(buf);
    return 1;
}