summaryrefslogtreecommitdiff
path: root/src/vm/interpreter.hpp
blob: 49f419d424cbeb5f4e4b847e3020a8e2cdfa010f (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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
// 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.



// This file contains bodies of inline methods.

#ifndef INTERPRETER_HPP_DEFINED
#define INTERPRETER_HPP_DEFINED 1

#include "interpreter.h"
#if INTERP_ILCYCLE_PROFILE
#include "cycletimer.h"
#endif // INTERP_ILCYCLE_PROFILE

#if INTERP_TRACING
// static 
FILE* Interpreter::GetLogFile()
{
    if (s_InterpreterLogFile == NULL)
    {
        static ConfigString fileName;
        LPWSTR fn = fileName.val(CLRConfig::INTERNAL_InterpreterLogFile);
        if (fn == NULL)
        {
            s_InterpreterLogFile = stdout;
        }
        else
        {
            s_InterpreterLogFile = _wfopen(fn, W("a"));
        }
    }
    return s_InterpreterLogFile;
}
#endif // INTERP_TRACING

inline void Interpreter::LdFromMemAddr(void* addr, InterpreterType tp)
{
    CONTRACTL {
        SO_TOLERANT;
        THROWS;
        GC_NOTRIGGER;
        MODE_COOPERATIVE;
    } CONTRACTL_END;

    unsigned stackHt = m_curStackHt;

    OpStackTypeSet(stackHt, tp.StackNormalize());

    size_t sz = tp.Size(&m_interpCeeInfo);
    if (tp.IsStruct())
    {
        if (tp.IsLargeStruct(&m_interpCeeInfo))
        {
            // Large struct case.
            void* ptr = LargeStructOperandStackPush(sz);
            memcpy(ptr, addr, sz);
            OpStackSet<void*>(stackHt, ptr);
        }
        else
        {
            OpStackSet<INT64>(stackHt, GetSmallStructValue(addr, sz));
        }
        m_curStackHt = stackHt + 1;
        return;
    }

    // Otherwise...

    // The compiler seems to compile this switch statement into an "if
    // cascade" anyway, but in a non-optimal order (one that's good for
    // code density, but doesn't match the actual usage frequency,
    // which, in fairness, it would have no clue about.  So we might
    // as well do our own "if cascade" in the order we believe is
    // likely to be optimal (at least on 32-bit systems).
    if (sz == 4)
    {
        OpStackSet<INT32>(stackHt, *reinterpret_cast<INT32*>(addr));
    }
    else if (sz == 1)
    {
        CorInfoType cit = tp.ToCorInfoType();
        if (CorInfoTypeIsUnsigned(cit))
        {
            OpStackSet<UINT32>(stackHt, *reinterpret_cast<UINT8*>(addr));
        }
        else
        {
            OpStackSet<INT32>(stackHt, *reinterpret_cast<INT8*>(addr));
        }
    }
    else if (sz == 8)
    {
        OpStackSet<INT64>(stackHt, *reinterpret_cast<INT64*>(addr));
    }
    else
    {
        assert(sz == 2); // only remaining case.
        CorInfoType cit = tp.ToCorInfoType();
        if (CorInfoTypeIsUnsigned(cit))
        {
            OpStackSet<UINT32>(stackHt, *reinterpret_cast<UINT16*>(addr));
        }
        else
        {
            OpStackSet<INT32>(stackHt, *reinterpret_cast<INT16*>(addr));
        }
    }
    m_curStackHt = stackHt + 1;
}

inline void Interpreter::LdLoc(int locNum)
{
    CONTRACTL {
        SO_TOLERANT;
        THROWS;
        GC_TRIGGERS;
        MODE_COOPERATIVE;
    } CONTRACTL_END;

    if (locNum >= m_methInfo->m_numLocals)
    {
        COMPlusThrow(kVerificationException);
    }

    unsigned stackHt = m_curStackHt;
    GCX_FORBID();
    OpStackSet<INT64>(stackHt, *FixedSizeLocalSlot(locNum));
    InterpreterType tp = m_methInfo->m_localDescs[locNum].m_typeStackNormal;
    OpStackTypeSet(stackHt, tp);
    m_curStackHt = stackHt + 1;
    m_orOfPushedInterpreterTypes |= static_cast<size_t>(tp.AsRaw());
}

void Interpreter::StLoc(int locNum)
{
    CONTRACTL {
        SO_TOLERANT;
        THROWS;
        GC_TRIGGERS;
        MODE_COOPERATIVE;
    } CONTRACTL_END;

    assert(m_curStackHt >= 1);

    if (locNum >= m_methInfo->m_numLocals)
    {
        COMPlusThrow(kVerificationException);
    }

    // Don't decrement "m_curStackHt" early -- if we do, then we'll have a potential GC hole, if 
    // the top-of-stack value is a GC ref.
    unsigned ind = m_curStackHt - 1;
    InterpreterType tp = m_methInfo->m_localDescs[locNum].m_typeStackNormal;

#ifdef _DEBUG
    if (!tp.Matches(OpStackTypeGet(ind), &m_interpCeeInfo))
    {
        if (!s_InterpreterLooseRules || 
            // We copy a 64 bit value, otherwise some of the below conditions should end up as casts.
            !((tp.ToCorInfoTypeShifted() == CORINFO_TYPE_SHIFTED_NATIVEINT && OpStackTypeGet(ind).ToCorInfoTypeShifted() == CORINFO_TYPE_SHIFTED_INT) ||
            (tp.ToCorInfoTypeShifted() == CORINFO_TYPE_SHIFTED_INT && OpStackTypeGet(ind).ToCorInfoTypeShifted() == CORINFO_TYPE_SHIFTED_NATIVEINT) ||
            (tp.ToCorInfoTypeShifted() == CORINFO_TYPE_SHIFTED_INT && OpStackTypeGet(ind).ToCorInfoTypeShifted() == CORINFO_TYPE_SHIFTED_LONG) ||
            (tp.ToCorInfoTypeShifted() == CORINFO_TYPE_SHIFTED_LONG && OpStackTypeGet(ind).ToCorInfoTypeShifted() == CORINFO_TYPE_SHIFTED_INT) ||
            (tp.ToCorInfoTypeShifted() == CORINFO_TYPE_SHIFTED_LONG && OpStackTypeGet(ind).ToCorInfoTypeShifted() == CORINFO_TYPE_SHIFTED_BYREF) ||
            (tp.ToCorInfoTypeShifted() == CORINFO_TYPE_SHIFTED_INT && OpStackTypeGet(ind).ToCorInfoTypeShifted() == CORINFO_TYPE_SHIFTED_BYREF) ||
            (tp.ToCorInfoTypeShifted() == CORINFO_TYPE_SHIFTED_NATIVEINT && OpStackTypeGet(ind).ToCorInfoTypeShifted() == CORINFO_TYPE_SHIFTED_BYREF) ||
            (tp.ToCorInfoTypeShifted() == CORINFO_TYPE_SHIFTED_BYREF && OpStackTypeGet(ind).ToCorInfoTypeShifted() == CORINFO_TYPE_SHIFTED_LONG) ||
            (tp.ToCorInfoTypeShifted() == CORINFO_TYPE_SHIFTED_BYREF && OpStackTypeGet(ind).ToCorInfoTypeShifted() == CORINFO_TYPE_SHIFTED_CLASS) ||
            (tp.ToCorInfoTypeShifted() == CORINFO_TYPE_SHIFTED_FLOAT && OpStackTypeGet(ind).ToCorInfoTypeShifted() == CORINFO_TYPE_SHIFTED_DOUBLE) ||
            (tp.ToCorInfoTypeShifted() == CORINFO_TYPE_SHIFTED_DOUBLE && OpStackTypeGet(ind).ToCorInfoTypeShifted() == CORINFO_TYPE_SHIFTED_FLOAT)))
        {
            VerificationError("StLoc requires types to match.");
        }
    }
#endif

    if (tp.IsLargeStruct(&m_interpCeeInfo))
    {
        size_t sz = tp.Size(&m_interpCeeInfo); // TODO: note that tp.IsLargeStruct() above just called tp.Size(), so this is duplicate work unless the optimizer inlines and CSEs the calls.

        // The operand stack entry is a pointer to a corresponding entry in the large struct stack.
        // There will be a large struct location for the local as well.
        BYTE* addr = LargeStructLocalSlot(locNum);

        // Now, before we copy from the large struct stack to "addr", we have a problem.
        // We've optimized "ldloc" to just copy the fixed size entry for the local onto the ostack.
        // But this might mean that there are pointers to "addr" already on the stack, as stand-ins for
        // the value they point to.  If we overwrite that value, we've inadvertently modified the ostack.
        // So we first "normalize" the ostack wrt "addr", ensuring that any entries containing addr
        // have large-struct slots allocated for them, and the values are copied there.
        OpStackNormalize();

        // Now we can do the copy.
        void* srcAddr = OpStackGet<void*>(ind);
        memcpy(addr, srcAddr, sz);
        LargeStructOperandStackPop(sz, srcAddr);
    }
    else
    {
        // Otherwise, we just copy the full stack entry.
        *FixedSizeLocalSlot(locNum) = OpStackGet<INT64>(ind);
    }

    m_curStackHt = ind;

#if INTERP_TRACING
    // The value of the locals has changed; print them.
    if (s_TraceInterpreterILFlag.val(CLRConfig::INTERNAL_TraceInterpreterIL))
    {
        PrintLocals();
    }
#endif // _DEBUG
}

void Interpreter::StToLocalMemAddr(void* addr, InterpreterType tp)
{
    CONTRACTL {
        SO_TOLERANT;
        THROWS;
        GC_TRIGGERS;
        MODE_COOPERATIVE;
    } CONTRACTL_END;

    assert(m_curStackHt >= 1);
    m_curStackHt--;

    size_t sz = tp.Size(&m_interpCeeInfo);
    if (tp.IsStruct())
    {
        if (tp.IsLargeStruct(&m_interpCeeInfo))
        {
            // Large struct case.
            void* srcAddr = OpStackGet<void*>(m_curStackHt);
            memcpy(addr, srcAddr, sz);
            LargeStructOperandStackPop(sz, srcAddr);
        }
        else
        {
            memcpy(addr, OpStackGetAddr(m_curStackHt, sz), sz);
        }
        return;
    }

    // Note: this implementation assumes a little-endian architecture.
    if (sz == 4)
    {
        *reinterpret_cast<INT32*>(addr) = OpStackGet<INT32>(m_curStackHt);
    }
    else if (sz == 1)
    {
        *reinterpret_cast<INT8*>(addr) = OpStackGet<INT8>(m_curStackHt);
    }
    else if (sz == 8)
    {
        *reinterpret_cast<INT64*>(addr) = OpStackGet<INT64>(m_curStackHt);
    }
    else
    {
        assert(sz == 2);
        *reinterpret_cast<INT16*>(addr) = OpStackGet<INT16>(m_curStackHt);
    }
}

template<int op, typename T, bool IsIntType, CorInfoType cit, bool TypeIsUnchanged>
void Interpreter::BinaryArithOpWork(T val1, T val2)
{
    T res;
    if (op == BA_Add)
    {
        res = val1 + val2;
    }
    else if (op == BA_Sub)
    {
        res = val1 - val2;
    }
    else if (op == BA_Mul)
    {
        res = val1 * val2;
    }
    else
    {
        assert(op == BA_Div || op == BA_Rem);
        if (IsIntType)
        {
            if (val2 == 0)
            {
                ThrowDivideByZero();
            }
            else if (val2 == -1 && val1 == static_cast<T>(((UINT64)1) << (sizeof(T)*8 - 1))) // min int / -1 is not representable.
            {
                ThrowSysArithException();
            }
        }
        // Otherwise...
        if (op == BA_Div)
        {
            res = val1 / val2;
        }
        else 
        {
            res = RemFunc(val1, val2);
        }
    }

    unsigned residx = m_curStackHt - 2;
    OpStackSet<T>(residx, res);
    if (!TypeIsUnchanged)
    {
        OpStackTypeSet(residx, InterpreterType(cit));
    }
}

void Interpreter::BrOnValueTakeBranch(bool shouldBranch, int targetLen)
{
    if  (shouldBranch)
    {
        int offset;
        if (targetLen == 1)
        {
            // BYTE is unsigned...
            offset = getI1(m_ILCodePtr + 1);
        }
        else
        {
            offset = getI4LittleEndian(m_ILCodePtr + 1);
        }
        // 1 is the size of the current instruction; offset is relative to start of next.
        if (offset < 0)
        {
            // Backwards branch; enable caching.
            BackwardsBranchActions(offset);
        }
        ExecuteBranch(m_ILCodePtr + 1 + targetLen + offset);
    }
    else
    {
        m_ILCodePtr += targetLen + 1;
    }
}

extern size_t CorInfoTypeSizeArray[];

size_t CorInfoTypeSize(CorInfoType cit)
{
    LIMITED_METHOD_CONTRACT;

    _ASSERTE_MSG(cit != CORINFO_TYPE_VALUECLASS, "Precondition");

    size_t res = CorInfoTypeSizeArray[cit];

    _ASSERTE_MSG(res != 0, "Other illegal input");

    return res;
}

void Interpreter::StaticFldAddr(CORINFO_ACCESS_FLAGS accessFlgs, 
                                /*out (byref)*/void** pStaticFieldAddr,
                                /*out*/InterpreterType* pit, /*out*/UINT* pFldSize, /*out*/bool* pManagedMem)
{
    unsigned ilOffset = CurOffset();

#if INTERP_TRACING
    InterlockedIncrement(&s_tokenResolutionOpportunities[RTK_SFldAddr]);
#endif // INTERP_TRACING

    StaticFieldCacheEntry* cacheEntry = NULL;
    if (s_InterpreterUseCaching) cacheEntry = GetCachedStaticField(ilOffset);
    if (cacheEntry == NULL)
    {
        bool doCaching = StaticFldAddrWork(accessFlgs, pStaticFieldAddr, pit, pFldSize, pManagedMem);
        if (s_InterpreterUseCaching && doCaching)
        {
            cacheEntry = new StaticFieldCacheEntry(*pStaticFieldAddr, *pFldSize, *pit);
            CacheStaticField(ilOffset, cacheEntry);
        }
    }
    else
    {
        // Reenable this if you want to check this (#ifdef _DEBUG).  Was interfering with some statistics
        // gathering I was doing in debug builds.
#if 0
        // Make sure the caching works correctly.
        StaticFldAddrWork(accessFlgs, pStaticFieldAddr, pit, pFldSize, pManagedMem);
        assert(*pStaticFieldAddr == cacheEntry->m_srcPtr && *pit == cacheEntry->m_it && *pFldSize == cacheEntry->m_sz);
#else
        // If we do the call above, it takes care of this.
        m_ILCodePtr += 5;  // In the case above, the call to StaticFldAddr increments the code pointer.
#endif
        *pStaticFieldAddr = cacheEntry->m_srcPtr;
        *pit = cacheEntry->m_it;
        *pFldSize = cacheEntry->m_sz;
        *pManagedMem = true; // Or else it wouldn't have been cached.
    }
}

void Interpreter::ResolveToken(CORINFO_RESOLVED_TOKEN* resTok, mdToken token, CorInfoTokenKind tokenType InterpTracingArg(ResolveTokenKind rtk))
{
    resTok->tokenContext = GetPreciseGenericsContext();
    resTok->tokenScope = m_methInfo->m_module;
    resTok->token = token;
    resTok->tokenType = tokenType;
#if INTERP_ILCYCLE_PROFILE
    unsigned __int64 startCycles;
    bool b = CycleTimer::GetThreadCyclesS(&startCycles); assert(b);
#endif // INTERP_ILCYCLE_PROFILE
    m_interpCeeInfo.resolveToken(resTok);
#if 1
    if (resTok->tokenType == CORINFO_TOKENKIND_Method)
    {
        MethodDesc* pMD = reinterpret_cast<MethodDesc*>(resTok->hMethod);
        MethodTable* pMT = GetMethodTableFromClsHnd(resTok->hClass);

        if (pMD->GetMethodTable() != pMT)
        {
            // Find the method on exactClass corresponding to methToCall.
            pMD = MethodDesc::FindOrCreateAssociatedMethodDesc(
                        pMD,             // pPrimaryMD
                        pMT,             // pExactMT
                        FALSE,           // forceBoxedEntryPoint
                        pMD->GetMethodInstantiation(),  // methodInst
                        FALSE,           // allowInstParam
                        TRUE);           // forceRemotableMethod (to get maximally specific).          
            resTok->hMethod = reinterpret_cast<CORINFO_METHOD_HANDLE>(pMD);
        }
    }
#endif
#if INTERP_ILCYCLE_PROFILE
    unsigned __int64 endCycles;
    b = CycleTimer::GetThreadCyclesS(&endCycles); assert(b);
    m_exemptCycles += (endCycles - startCycles);
#endif // INTERP_ILCYCLE_PROFILE

#if INTERP_TRACING
    InterlockedIncrement(&s_tokenResolutionCalls[rtk]);
#endif // INTERP_TRACING
}

FieldDesc* Interpreter::FindField(unsigned metaTok InterpTracingArg(ResolveTokenKind rtk))
{
    CORINFO_RESOLVED_TOKEN fldTok;
    ResolveToken(&fldTok, metaTok, CORINFO_TOKENKIND_Field InterpTracingArg(rtk));
    return (FieldDesc*)fldTok.hField;
}

CORINFO_CLASS_HANDLE Interpreter::FindClass(unsigned metaTok InterpTracingArg(ResolveTokenKind rtk))
{
    CORINFO_RESOLVED_TOKEN clsTok;
    ResolveToken(&clsTok, metaTok, CORINFO_TOKENKIND_Class InterpTracingArg(rtk));
    return clsTok.hClass;
}

void Interpreter::ThrowOnInvalidPointer(void* ptr)
{
    if (ptr == NULL)
        ThrowNullPointerException();

    BOOL good = TRUE;

    EX_TRY
    {
        AVInRuntimeImplOkayHolder AVOkay;
        good = *(BOOL*)ptr;

        // This conditional forces the dereference to occur; it also
        // ensures that good == TRUE if the dereference succeeds.
        if (!good)
            good = TRUE;
    }
    EX_CATCH
    {
        good = FALSE;
    }
    EX_END_CATCH(SwallowAllExceptions)

    if (!good)
        ThrowNullPointerException();
}

#endif // INTERPRETER_HPP_DEFINED