jitcraft/Opcodes.cpp
changeset 70 1aec996cde2d
parent 68 b40b0e18a918
child 72 40746ab0a30b
     1.1 --- a/jitcraft/Opcodes.cpp	Wed Jul 11 01:12:54 2012 -0700
     1.2 +++ b/jitcraft/Opcodes.cpp	Thu Jul 12 00:38:00 2012 -0700
     1.3 @@ -23,6 +23,7 @@
     1.4  #include "Zone.h"
     1.5  #include "Array.h"
     1.6  #include "Strings.h"
     1.7 +#include "CompactBuffer.h"
     1.8  #include "Heap-inl.h"
     1.9  
    1.10  using namespace ke;
    1.11 @@ -38,7 +39,8 @@
    1.12  Code *
    1.13  Code::New(Zone *zone, Handle<ByteArray> bytecode, Handle<FixedArray> locals,
    1.14            Handle<FixedArray> objects, unsigned maxStackDepth, Handle<Module> module,
    1.15 -	  Handle<ByteArray> safepoints)
    1.16 +	      Handle<ByteArray> safepoints, Handle<ByteArray> sourcemap,
    1.17 +          unsigned lineNumber)
    1.18  {
    1.19      Local<Code> code(zone, Code::cast(zone->allocate(MapKind_Code, sizeof(Code), Heap::Tenure_Old)));
    1.20      if (!code)
    1.21 @@ -50,6 +52,8 @@
    1.22      code->maxStackDepth_ = maxStackDepth;
    1.23      code->module_ = module;
    1.24      code->safepoints_ = safepoints;
    1.25 +    code->sourcemap_ = sourcemap;
    1.26 +    code->lineNumber_ = lineNumber;
    1.27      return code;
    1.28  }
    1.29  
    1.30 @@ -98,6 +102,28 @@
    1.31      return Address(result) + sizeof(unsigned);
    1.32  }
    1.33  
    1.34 +SourcePosition
    1.35 +Code::pcToSource(unsigned pc)
    1.36 +{
    1.37 +    assert(pc < bytecode_->length());
    1.38 +
    1.39 +    CompactBufferReader reader(sourcemap_->raw(), sourcemap_->raw() + sourcemap_->length());
    1.40 +
    1.41 +    unsigned line = 1;
    1.42 +    unsigned col = 0;
    1.43 +    unsigned curPc = 0;
    1.44 +    while (!reader.done()) {
    1.45 +        curPc += reader.readUint32();
    1.46 +        if (curPc > pc)
    1.47 +            break;
    1.48 +
    1.49 +        line = reader.readUint32();
    1.50 +        col = reader.readUint32();
    1.51 +    }
    1.52 +
    1.53 +    return SourcePosition(lineNumber_ + line, col);
    1.54 +}
    1.55 +
    1.56  // All jumps must be the same length.
    1.57  STATIC_ASSERT(OP_JL_LENGTH == OP_JUMP_LENGTH);
    1.58  STATIC_ASSERT(OP_JLE_LENGTH == OP_JUMP_LENGTH);