summaryrefslogtreecommitdiff
path: root/src/jit/sm.cpp
blob: b016899761984a9def23fd43d90b27eb71bedb6b (plain)
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

/*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XX                                                                           XX
XX                 State machine used in the JIT                             XX
XX                                                                           XX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
*/

#include "jitpch.h"
#ifdef _MSC_VER
#pragma hdrstop
#endif

#include "smcommon.cpp"

//
// The array to map from EE opcodes (i.e. CEE_ ) to state machine opcodes (i.e. SM_ )
//
const SM_OPCODE smOpcodeMap[] = {
#define OPCODEMAP(eename, eestring, smname) smname,
#include "smopcodemap.def"
#undef OPCODEMAP
};

// ????????? How to make this method inlinable, since it refers to smOpcodeMap????
/* static */ SM_OPCODE CodeSeqSM::MapToSMOpcode(OPCODE opcode)
{
    assert(opcode < CEE_COUNT);

    SM_OPCODE smOpcode = smOpcodeMap[opcode];
    assert(smOpcode < SM_COUNT);
    return smOpcode;
}

void CodeSeqSM::Start(Compiler* comp)
{
    pComp          = comp;
    States         = gp_SMStates;
    JumpTableCells = gp_SMJumpTableCells;
    StateWeights   = gp_StateWeights;
    NativeSize     = 0;

    Reset();
}

void CodeSeqSM::Reset()
{
    curState = SM_STATE_ID_START;
}

void CodeSeqSM::End()
{
    if (States[curState].term)
    {
        TermStateMatch(curState DEBUGARG(pComp->verbose));
    }
}

void CodeSeqSM::Run(SM_OPCODE opcode DEBUGARG(int level))
{
    SM_STATE_ID nextState;
    SM_STATE_ID rollbackState;

    SM_OPCODE opcodesToRevisit[MAX_CODE_SEQUENCE_LENGTH];

    assert(level <= MAX_CODE_SEQUENCE_LENGTH);

_Next:
    nextState = GetDestState(curState, opcode);

    if (nextState != 0)
    {
        // This is easy, Just go to the next state.
        curState = nextState;
        return;
    }

    assert(curState != SM_STATE_ID_START);

    if (States[curState].term)
    {
        TermStateMatch(curState DEBUGARG(pComp->verbose));
        curState = SM_STATE_ID_START;
        goto _Next;
    }

    // This is hard. We need to rollback to the longest matched term state and restart from there.

    rollbackState = States[curState].longestTermState;
    TermStateMatch(rollbackState DEBUGARG(pComp->verbose));

    assert(States[curState].length > States[rollbackState].length);

    unsigned numOfOpcodesToRevisit = States[curState].length - States[rollbackState].length + 1;
    assert(numOfOpcodesToRevisit > 1 &&
           numOfOpcodesToRevisit <= MAX_CODE_SEQUENCE_LENGTH); // So it can fit in the local array opcodesToRevisit[]

    SM_OPCODE* p = opcodesToRevisit + (numOfOpcodesToRevisit - 1);

    *p = opcode;

    // Fill in the local array:
    for (unsigned i = 0; i < numOfOpcodesToRevisit - 1; ++i)
    {
        *(--p)   = States[curState].opc;
        curState = States[curState].prevState;
    }

    assert(curState == rollbackState);

    // Now revisit these opcodes, starting from SM_STATE_ID_START.
    curState = SM_STATE_ID_START;
    for (p = opcodesToRevisit; p < opcodesToRevisit + numOfOpcodesToRevisit; ++p)
    {
        Run(*p DEBUGARG(level + 1));
    }
}

SM_STATE_ID CodeSeqSM::GetDestState(SM_STATE_ID srcState, SM_OPCODE opcode)
{
    assert(opcode < SM_COUNT);

    JumpTableCell* pThisJumpTable = (JumpTableCell*)(((PBYTE)JumpTableCells) + States[srcState].jumpTableByteOffset);

    JumpTableCell* cell = pThisJumpTable + opcode;

    if (cell->srcState != srcState)
    {
        assert(cell->srcState == 0 ||
               cell->srcState != srcState); // Either way means there is not outgoing edge from srcState.
        return 0;
    }
    else
    {
        return cell->destState;
    }
}

#ifdef DEBUG

const char* CodeSeqSM::StateDesc(SM_STATE_ID stateID)
{
    static char      s_StateDesc[500];
    static SM_OPCODE s_StateDescOpcodes[MAX_CODE_SEQUENCE_LENGTH];

    if (stateID == 0)
    {
        return "invalid";
    }
    if (stateID == SM_STATE_ID_START)
    {
        return "start";
    }
    unsigned i = 0;

    SM_STATE_ID b = stateID;

    while (States[b].prevState != 0)
    {
        s_StateDescOpcodes[i] = States[b].opc;
        b                     = States[b].prevState;
        ++i;
    }

    assert(i == States[stateID].length && i > 0);

    *s_StateDesc = 0;

    while (--i > 0)
    {
        strcat(s_StateDesc, smOpcodeNames[s_StateDescOpcodes[i]]);
        strcat(s_StateDesc, " -> ");
    }

    strcat(s_StateDesc, smOpcodeNames[s_StateDescOpcodes[0]]);

    return s_StateDesc;
}

#endif // DEBUG