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[] =
26 #define OPCODEMAP(eename,eestring,smname) smname,
27 #include "smopcodemap.def"
32 // ????????? How to make this method inlinable, since it refers to smOpcodeMap????
33 /* static */ SM_OPCODE CodeSeqSM::MapToSMOpcode(OPCODE opcode)
35 assert(opcode < CEE_COUNT);
37 SM_OPCODE smOpcode = smOpcodeMap[opcode];
38 assert(smOpcode < SM_COUNT);
42 void CodeSeqSM::Start(Compiler * comp)
46 JumpTableCells = gp_SMJumpTableCells;
47 StateWeights = gp_StateWeights;
53 void CodeSeqSM::Reset()
55 curState = SM_STATE_ID_START;
58 // Reset the state occurence counts
59 memset(StateMatchedCounts, 0, sizeof(StateMatchedCounts));
66 if (States[curState].term)
68 TermStateMatch(curState DEBUGARG(pComp->verbose));
72 void CodeSeqSM::Run(SM_OPCODE opcode DEBUGARG(int level))
74 SM_STATE_ID nextState;
75 SM_STATE_ID rollbackState;
77 SM_OPCODE opcodesToRevisit[MAX_CODE_SEQUENCE_LENGTH];
79 assert(level<=MAX_CODE_SEQUENCE_LENGTH);
82 nextState = GetDestState(curState, opcode);
86 // This is easy, Just go to the next state.
91 assert(curState != SM_STATE_ID_START);
93 if (States[curState].term)
95 TermStateMatch(curState DEBUGARG(pComp->verbose));
96 curState = SM_STATE_ID_START;
100 // This is hard. We need to rollback to the longest matched term state and restart from there.
102 rollbackState = States[curState].longestTermState;
103 TermStateMatch(rollbackState DEBUGARG(pComp->verbose));
105 assert(States[curState].length > States[rollbackState].length);
107 unsigned numOfOpcodesToRevisit = States[curState].length - States[rollbackState].length + 1;
108 assert(numOfOpcodesToRevisit > 1 &&
109 numOfOpcodesToRevisit <= MAX_CODE_SEQUENCE_LENGTH); // So it can fit in the local array opcodesToRevisit[]
111 SM_OPCODE * p = opcodesToRevisit + (numOfOpcodesToRevisit - 1);
115 // Fill in the local array:
116 for (unsigned i = 0; i<numOfOpcodesToRevisit-1; ++i)
118 * (--p) = States[curState].opc;
119 curState = States[curState].prevState;
122 assert(curState == rollbackState);
124 // Now revisit these opcodes, starting from SM_STATE_ID_START.
125 curState = SM_STATE_ID_START;
126 for (p = opcodesToRevisit; p< opcodesToRevisit + numOfOpcodesToRevisit; ++p)
128 Run(*p DEBUGARG(level+1));
133 SM_STATE_ID CodeSeqSM::GetDestState(SM_STATE_ID srcState, SM_OPCODE opcode)
135 assert(opcode < SM_COUNT);
137 JumpTableCell * pThisJumpTable = (JumpTableCell * )(((PBYTE)JumpTableCells) + States[srcState].jumpTableByteOffset);
139 JumpTableCell * cell = pThisJumpTable+opcode;
141 if (cell->srcState != srcState)
143 assert(cell->srcState == 0 || cell->srcState != srcState); // Either way means there is not outgoing edge from srcState.
148 return cell->destState;
154 const char * CodeSeqSM::StateDesc(SM_STATE_ID stateID)
156 static char s_StateDesc[500];
157 static SM_OPCODE s_StateDescOpcodes[MAX_CODE_SEQUENCE_LENGTH];
162 if (stateID == SM_STATE_ID_START)
167 SM_STATE_ID b = stateID;
169 while (States[b].prevState != 0)
171 s_StateDescOpcodes[i] = States[b].opc;
172 b = States[b].prevState;
176 assert(i == States[stateID].length && i>0);
182 strcat(s_StateDesc, smOpcodeNames[s_StateDescOpcodes[i]]);
183 strcat(s_StateDesc, " -> ");
186 strcat(s_StateDesc, smOpcodeNames[s_StateDescOpcodes[0]]);