DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (777e60ca8853)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 * vim: set ts=8 sts=4 et sw=4 tw=99:
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifdef DEBUG

#include "jit/C1Spewer.h"

#include <time.h>

#include "jit/LinearScan.h"
#include "jit/LIR.h"
#include "jit/MIRGraph.h"

using namespace js;
using namespace js::jit;

bool
C1Spewer::init(const char* path)
{
    spewout_ = fopen(path, "w");
    return spewout_ != nullptr;
}

void
C1Spewer::beginFunction(MIRGraph* graph, HandleScript script)
{
    if (!spewout_)
        return;

    this->graph  = graph;
    this->script.repoint(script);

    fprintf(spewout_, "begin_compilation\n");
    if (script) {
        fprintf(spewout_, "  name \"%s:%d\"\n", script->filename(), (int)script->lineno());
        fprintf(spewout_, "  method \"%s:%d\"\n", script->filename(), (int)script->lineno());
    } else {
        fprintf(spewout_, "  name \"asm.js compilation\"\n");
        fprintf(spewout_, "  method \"asm.js compilation\"\n");
    }
    fprintf(spewout_, "  date %d\n", (int)time(nullptr));
    fprintf(spewout_, "end_compilation\n");
}

void
C1Spewer::spewPass(const char* pass)
{
    if (!spewout_)
        return;

    fprintf(spewout_, "begin_cfg\n");
    fprintf(spewout_, "  name \"%s\"\n", pass);

    for (MBasicBlockIterator block(graph->begin()); block != graph->end(); block++)
        spewPass(spewout_, *block);

    fprintf(spewout_, "end_cfg\n");
    fflush(spewout_);
}

void
C1Spewer::spewIntervals(const char* pass, LinearScanAllocator* regalloc)
{
    if (!spewout_)
        return;

    fprintf(spewout_, "begin_intervals\n");
    fprintf(spewout_, " name \"%s\"\n", pass);

    size_t nextId = 0x4000;
    for (MBasicBlockIterator block(graph->begin()); block != graph->end(); block++)
        spewIntervals(spewout_, *block, regalloc, nextId);

    fprintf(spewout_, "end_intervals\n");
    fflush(spewout_);
}

void
C1Spewer::endFunction()
{
}

void
C1Spewer::finish()
{
    if (spewout_)
        fclose(spewout_);
}

static void
DumpDefinition(FILE* fp, MDefinition* def)
{
    fprintf(fp, "      ");
    fprintf(fp, "%u %u ", def->id(), unsigned(def->useCount()));
    def->printName(fp);
    fprintf(fp, " ");
    def->printOpcode(fp);
    fprintf(fp, " <|@\n");
}

static void
DumpLIR(FILE* fp, LInstruction* ins)
{
    fprintf(fp, "      ");
    fprintf(fp, "%d ", ins->id());
    ins->dump(fp);
    fprintf(fp, " <|@\n");
}

void
C1Spewer::spewIntervals(FILE* fp, LinearScanAllocator* regalloc, LInstruction* ins, size_t& nextId)
{
    for (size_t k = 0; k < ins->numDefs(); k++) {
        uint32_t id = ins->getDef(k)->virtualRegister();
        VirtualRegister* vreg = &regalloc->vregs[id];

        for (size_t i = 0; i < vreg->numIntervals(); i++) {
            LiveInterval* live = vreg->getInterval(i);
            if (live->numRanges()) {
                fprintf(fp, "%d object \"", (i == 0) ? id : int32_t(nextId++));
                fprintf(fp, "%s", live->getAllocation()->toString());
                fprintf(fp, "\" %d -1", id);
                for (size_t j = 0; j < live->numRanges(); j++) {
                    fprintf(fp, " [%d, %d[", live->getRange(j)->from.pos(),
                            live->getRange(j)->to.pos());
                }
                for (UsePositionIterator usePos(live->usesBegin()); usePos != live->usesEnd(); usePos++)
                    fprintf(fp, " %d M", usePos->pos.pos());
                fprintf(fp, " \"\"\n");
            }
        }
    }
}

void
C1Spewer::spewIntervals(FILE* fp, MBasicBlock* block, LinearScanAllocator* regalloc, size_t& nextId)
{
    LBlock* lir = block->lir();
    if (!lir)
        return;

    for (size_t i = 0; i < lir->numPhis(); i++)
        spewIntervals(fp, regalloc, lir->getPhi(i), nextId);

    for (LInstructionIterator ins = lir->begin(); ins != lir->end(); ins++)
        spewIntervals(fp, regalloc, *ins, nextId);
}

void
C1Spewer::spewPass(FILE* fp, MBasicBlock* block)
{
    fprintf(fp, "  begin_block\n");
    fprintf(fp, "    name \"B%d\"\n", block->id());
    fprintf(fp, "    from_bci -1\n");
    fprintf(fp, "    to_bci -1\n");

    fprintf(fp, "    predecessors");
    for (uint32_t i = 0; i < block->numPredecessors(); i++) {
        MBasicBlock* pred = block->getPredecessor(i);
        fprintf(fp, " \"B%d\"", pred->id());
    }
    fprintf(fp, "\n");

    fprintf(fp, "    successors");
    for (uint32_t i = 0; i < block->numSuccessors(); i++) {
        MBasicBlock* successor = block->getSuccessor(i);
        fprintf(fp, " \"B%d\"", successor->id());
    }
    fprintf(fp, "\n");

    fprintf(fp, "    xhandlers\n");
    fprintf(fp, "    flags\n");

    if (block->lir() && block->lir()->begin() != block->lir()->end()) {
        fprintf(fp, "    first_lir_id %d\n", block->lir()->firstId());
        fprintf(fp, "    last_lir_id %d\n", block->lir()->lastId());
    }

    fprintf(fp, "    begin_states\n");

    if (block->entryResumePoint()) {
        fprintf(fp, "      begin_locals\n");
        fprintf(fp, "        size %d\n", (int)block->numEntrySlots());
        fprintf(fp, "        method \"None\"\n");
        for (uint32_t i = 0; i < block->numEntrySlots(); i++) {
            MDefinition* ins = block->getEntrySlot(i);
            fprintf(fp, "        ");
            fprintf(fp, "%d ", i);
            if (ins->isUnused())
                fprintf(fp, "unused");
            else
                ins->printName(fp);
            fprintf(fp, "\n");
        }
        fprintf(fp, "      end_locals\n");
    }
    fprintf(fp, "    end_states\n");

    fprintf(fp, "    begin_HIR\n");
    for (MPhiIterator phi(block->phisBegin()); phi != block->phisEnd(); phi++)
        DumpDefinition(fp, *phi);
    for (MInstructionIterator i(block->begin()); i != block->end(); i++)
        DumpDefinition(fp, *i);
    fprintf(fp, "    end_HIR\n");

    if (block->lir()) {
        fprintf(fp, "    begin_LIR\n");
        for (size_t i = 0; i < block->lir()->numPhis(); i++)
            DumpLIR(fp, block->lir()->getPhi(i));
        for (LInstructionIterator i(block->lir()->begin()); i != block->lir()->end(); i++)
            DumpLIR(fp, *i);
        fprintf(fp, "    end_LIR\n");
    }

    fprintf(fp, "  end_block\n");
}

#endif /* DEBUG */