📄 cache.c
字号:
/*0001*//*
/*0002./ * Copyright (c) 1998-2001 Sun Microsystems, Inc. All Rights Reserved.
/*0003./ *
/*0004./ * This software is the confidential and proprietary information of Sun
/*0005./ * Microsystems, Inc. ("Confidential Information"). You shall not
/*0006./ * disclose such Confidential Information and shall use it only in
/*0007./ * accordance with the terms of the license agreement you entered into
/*0008./ * with Sun.
/*0009./ *
/*0010./ * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
/*0011./ * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
/*0012./ * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
/*0013./ * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
/*0014./ * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
/*0015./ * THIS SOFTWARE OR ITS DERIVATIVES.
/*0016./ *
/*0017./ */
/*0018*/
/*0019*//*=========================================================================
/*0020./ * KVM Virtual Machine
/*0021./ *=========================================================================
/*0022./ * SYSTEM: KVM
/*0023./ * SUBSYSTEM: Internal runtime structures
/*0024./ * FILE: cache.c
/*0025./ * OVERVIEW: Inline caching support (see Cache.h).
/*0026./ * AUTHOR: Antero Taivalsaari, Sun Labs
/*0027./ *=======================================================================*/
/*0028*/
/*0029*//*=========================================================================
/*0030./ * Include files
/*0031./ *=======================================================================*/
/*0032*/
/*0033*/#include <global.h>
/*0034*/
/*0035*//*=========================================================================
/*0036./ * Everything that follows is included in the system only if
/*0037./ * bytecode optimization is turned on
/*0038./ *=======================================================================*/
/*0039*/
/*0040*/#if ENABLEFASTBYTECODES
/*0041*/
/*0042*//*=========================================================================
/*0043./ * Global variables and definitions
/*0044./ *=======================================================================*/
/*0045*/
/*0046*//* The master inline cache in the system (see Cache.h) */
/*0047*/ICACHE InlineCache;
/*0048*/
/*0049*//* Index of the next inline cache entry to be used */
/*0050*/int InlineCachePointer;
/*0051*/
/*0052*//* Flag telling whether inline cache area is full or not */
/*0053*/int InlineCacheAreaFull;
/*0054*/
/*0055*/static void releaseInlineCacheEntry(int index);
/*0056*/
/*0057*//*=========================================================================
/*0058./ * Constructor/destructors for (re)initializing inline caching
/*0059./ *=======================================================================*/
/*0060*/
/*0061*//*=========================================================================
/*0062./ * FUNCTION: InitializeInlineCaching()
/*0063./ * TYPE: constructor
/*0064./ * OVERVIEW: Create the master inline cache area of the system and
/*0065./ * initialize it properly.
/*0066./ * INTERFACE:
/*0067./ * parameters: <none>
/*0068./ * returns: <nothing>
/*0069./ *=======================================================================*/
/*0070*/
/*0071*/void
/*0072*/InitializeInlineCaching(void)
/*0073*/{
/*0074*/ /* Align the cache area so that accessing static variables is safe
/*0075./ * regardless of the alignment settings of the compiler
/*0076./ */
/*0077*/ InlineCache =
/*0078*/ (ICACHE)callocPermanentObject(SIZEOF_ICACHE*INLINECACHESIZE+1);
/*0079*/ InlineCachePointer = 0;
/*0080*/ InlineCacheAreaFull = FALSE;
/*0081*/ memset(InlineCache, 0, (SIZEOF_ICACHE*INLINECACHESIZE+1)*sizeof(CELL));
/*0082*/}
/*0083*/
/*0084*//*=========================================================================
/*0085./ * FUNCTION: FinalizeInlineCaching()
/*0086./ * TYPE: destructor (reconstructor, actually)
/*0087./ * OVERVIEW: Flush all the existing inline cache entries from the
/*0088./ * master inline cache area, patching all the affected
/*0089./ * methods with original code.
/*0090./ * INTERFACE:
/*0091./ * parameters: <none>
/*0092./ * returns: <nothing>
/*0093./ *=======================================================================*/
/*0094*/
/*0095*/void
/*0096*/FinalizeInlineCaching(void)
/*0097*/{
/*0098*/ int last = InlineCacheAreaFull ? INLINECACHESIZE : InlineCachePointer;
/*0099*/ while (--last >= 0) {
/*0100*/ releaseInlineCacheEntry(last);
/*0101*/ }
/*0102*/ InlineCachePointer = 0;
/*0103*/ InlineCacheAreaFull = FALSE;
/*0104*/}
/*0105*/
/*0106*//*=========================================================================
/*0107./ * Constructors/destructors for individual icache entries
/*0108./ *=======================================================================*/
/*0109*/
/*0110*//*=========================================================================
/*0111./ * FUNCTION: releaseInlineCacheEntry()
/*0112./ * TYPE: private destructor
/*0113./ * OVERVIEW: Release the requested inline cache entry and patch
/*0114./ * code with original bytecode and parameters.
/*0115./ * INTERFACE:
/*0116./ * parameters: inline cache entry index
/*0117./ * returns: <nothing>
/*0118./ * NOTE: This operation should only be called internally
/*0119./ * by createICacheEntry() below
/*0120./ *=======================================================================*/
/*0121*/
/*0122*/static void
/*0123*/releaseInlineCacheEntry(int index)
/*0124*/{
/*0125*/ ICACHE thisICache = &InlineCache[index];
/*0126*/
/*0127*/ /* Read the pointer to the code location */
/*0128*/ /* referring to this inline cache entry */
/*0129*/ BYTE* codeLoc = (BYTE*)thisICache->codeLoc;
/*0130*/
/*0131*/ /* Restore original bytecodes */
/*0137*/ *codeLoc = thisICache->origInst;
/*0141*/ putShort(codeLoc+1, thisICache->origParam);
/*0142*/}
/*0143*/
/*0144*//*=========================================================================
/*0145./ * FUNCTION: createInlineCacheEntry()
/*0146./ * TYPE: constructor
/*0147./ * OVERVIEW: Create or reallocate an inline cache entry,
/*0148./ * storing the given parameter and the original
/*0149./ * bytecode sequence in the cache.
/*0150./ * INTERFACE:
/*0151./ * parameters: user-supplied contents, location of the original
/*0152./ * bytecode sequence
/*0153./ * returns: index of the icache entry
/*0154./ *=======================================================================*/
/*0155*/
/*0156*/int
/*0157*/createInlineCacheEntry(cell* contents, BYTE* originalCode)
/*0158*/{
/*0159*/ ICACHE thisICache;
/*0160*/ int index;
/*0161*/
/*0162*/ /*
/*0163./ * Check first if inline cache is already full
/*0164./ */
/*0165*/ if (InlineCacheAreaFull) {
/*0166*/ releaseInlineCacheEntry(InlineCachePointer);
/*0167*/ }
/*0168*/
/*0169*/ /* Allocate new entry / reallocate old one */
/*0170*/ thisICache = &InlineCache[InlineCachePointer];
/*0171*/ index = InlineCachePointer++;
/*0172*/
/*0173*/ /* Check whether the icache area is full now */
/*0174*/ if (InlineCachePointer == INLINECACHESIZE) {
/*0175*/ InlineCacheAreaFull = TRUE;
/*0176*/ InlineCachePointer = 0;
/*0177*/ }
/*0178*/
/*0179*/ /* Initialize icache values */
/*0180*/ thisICache->contents = contents;
/*0181*/ thisICache->codeLoc = originalCode;
/*0182*/ thisICache->origInst = *originalCode;
/*0183*/ thisICache->origParam = getShort(originalCode+1);
/*0184*/
/*0185*/ return index;
/*0186*/}
/*0187*/
/*0188*//*=========================================================================
/*0189./ * Operations on individual icache entries
/*0190./ *=======================================================================*/
/*0191*/
/*0192*//*=========================================================================
/*0193./ * FUNCTION: getInlineCache()
/*0194./ * TYPE: public accessor function
/*0195./ * OVERVIEW: Given an index, get the address of the corresponding
/*0196./ * inline cache entry in the master inline cache area.
/*0197./ * INTERFACE:
/*0198./ * parameters: inline cache entry index
/*0199./ * returns: inline cache entry address
/*0200./ *=======================================================================*/
/*0201*/
/*0202*/ICACHE getInlineCache(int index)
/*0203*/{
/*0204*/ return &InlineCache[index];
/*0205*/}
/*0206*/
/*0207*//*=========================================================================
/*0208./ * End of conditional compilation (ENABLEFASTBYTECODES)
/*0209./ *=======================================================================*/
/*0210*/
/*0211*/#endif /* ENABLEFASTBYTECODES */
/*0212*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -