1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
5 /*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
6 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
8 XX State machine used in the JIT XX
10 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
11 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
19 #include "smcommon.cpp"
22 // The array to map from EE opcodes (i.e. CEE_ ) to state machine opcodes (i.e. SM_ )
24 const SM_OPCODE smOpcodeMap[] = {
25 #define OPCODEMAP(eename, eestring, smname) smname,
26 #include "smopcodemap.def"
30 // ????????? How to make this method inlinable, since it refers to smOpcodeMap????
31 /* static */ SM_OPCODE CodeSeqSM::MapToSMOpcode(OPCODE opcode)
33 assert(opcode < CEE_COUNT);
35 SM_OPCODE smOpcode = smOpcodeMap[opcode];
36 assert(smOpcode < SM_COUNT);
40 void CodeSeqSM::Start(Compiler* comp)
44 JumpTableCells = gp_SMJumpTableCells;
45 StateWeights = gp_StateWeights;
51 void CodeSeqSM::Reset()
53 curState = SM_STATE_ID_START;
58 if (States[curState].term)
60 TermStateMatch(curState DEBUGARG(pComp->verbose));
64 void CodeSeqSM::Run(SM_OPCODE opcode DEBUGARG(int level))
66 SM_STATE_ID nextState;
67 SM_STATE_ID rollbackState;
69 SM_OPCODE opcodesToRevisit[MAX_CODE_SEQUENCE_LENGTH];
71 assert(level <= MAX_CODE_SEQUENCE_LENGTH);
74 nextState = GetDestState(curState, opcode);
78 // This is easy, Just go to the next state.
83 assert(curState != SM_STATE_ID_START);
85 if (States[curState].term)
87 TermStateMatch(curState DEBUGARG(pComp->verbose));
88 curState = SM_STATE_ID_START;
92 // This is hard. We need to rollback to the longest matched term state and restart from there.
94 rollbackState = States[curState].longestTermState;
95 TermStateMatch(rollbackState DEBUGARG(pComp->verbose));
97 assert(States[curState].length > States[rollbackState].length);
99 unsigned numOfOpcodesToRevisit = States[curState].length - States[rollbackState].length + 1;
100 assert(numOfOpcodesToRevisit > 1 &&
101 numOfOpcodesToRevisit <= MAX_CODE_SEQUENCE_LENGTH); // So it can fit in the local array opcodesToRevisit[]
103 SM_OPCODE* p = opcodesToRevisit + (numOfOpcodesToRevisit - 1);
107 // Fill in the local array:
108 for (unsigned i = 0; i < numOfOpcodesToRevisit - 1; ++i)
110 *(--p) = States[curState].opc;
111 curState = States[curState].prevState;
114 assert(curState == rollbackState);
116 // Now revisit these opcodes, starting from SM_STATE_ID_START.
117 curState = SM_STATE_ID_START;
118 for (p = opcodesToRevisit; p < opcodesToRevisit + numOfOpcodesToRevisit; ++p)
120 Run(*p DEBUGARG(level + 1));
124 SM_STATE_ID CodeSeqSM::GetDestState(SM_STATE_ID srcState, SM_OPCODE opcode)
126 assert(opcode < SM_COUNT);
128 JumpTableCell* pThisJumpTable = (JumpTableCell*)(((PBYTE)JumpTableCells) + States[srcState].jumpTableByteOffset);
130 JumpTableCell* cell = pThisJumpTable + opcode;
132 if (cell->srcState != srcState)
134 assert(cell->srcState == 0 ||
135 cell->srcState != srcState); // Either way means there is not outgoing edge from srcState.
140 return cell->destState;
146 const char* CodeSeqSM::StateDesc(SM_STATE_ID stateID)
148 static char s_StateDesc[500];
149 static SM_OPCODE s_StateDescOpcodes[MAX_CODE_SEQUENCE_LENGTH];
155 if (stateID == SM_STATE_ID_START)
161 SM_STATE_ID b = stateID;
163 while (States[b].prevState != 0)
165 s_StateDescOpcodes[i] = States[b].opc;
166 b = States[b].prevState;
170 assert(i == States[stateID].length && i > 0);
176 strcat(s_StateDesc, smOpcodeNames[s_StateDescOpcodes[i]]);
177 strcat(s_StateDesc, " -> ");
180 strcat(s_StateDesc, smOpcodeNames[s_StateDescOpcodes[0]]);