1 /* -*- Mode: C++; c-basic-offset: 4; indent-tabs-mode: t; tab-width: 4 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is [Open Source Virtual Machine].
17 * The Initial Developer of the Original Code is
18 * Adobe System Incorporated.
19 * Portions created by the Initial Developer are Copyright (C) 2004-2007
20 * the Initial Developer. All Rights Reserved.
24 * Mozilla TraceMonkey Team
25 * Asko Tontti <atontti@cc.hut.fi>
27 * Alternatively, the contents of this file may be used under the terms of
28 * either the GNU General Public License Version 2 or later (the "GPL"), or
29 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 * in which case the provisions of the GPL or the LGPL are applicable instead
31 * of those above. If you wish to allow use of your version of this file only
32 * under the terms of either the GPL or the LGPL, and not to allow others to
33 * use your version of this file under the terms of the MPL, indicate your
34 * decision by deleting the provisions above and replace them with the notice
35 * and other provisions required by the GPL or the LGPL. If you do not delete
36 * the provisions above, a recipient may use your version of this file under
37 * the terms of any one of the MPL, the GPL or the LGPL.
39 * ***** END LICENSE BLOCK ***** */
42 #ifndef __nanojit_Fragmento__
43 #define __nanojit_Fragmento__
45 #ifdef AVMPLUS_VERBOSE
46 extern void drawTraceTrees(Fragmento
*frago
, FragmentMap
* _frags
, avmplus::AvmCore
*core
, char *fileName
);
58 struct Page
: public PageHeader
61 LIns lir
[(NJ_PAGE_SIZE
-sizeof(PageHeader
))/sizeof(LIns
)];
62 NIns code
[(NJ_PAGE_SIZE
-sizeof(PageHeader
))/sizeof(NIns
)];
65 struct AllocEntry
: public avmplus::GCObject
70 typedef avmplus::List
<AllocEntry
*,avmplus::LIST_NonGCObjects
> AllocList
;
72 typedef avmplus::GCSortedMap
<const void*, uint32_t, avmplus::LIST_NonGCObjects
> BlockSortedMap
;
73 class BlockHist
: public BlockSortedMap
76 BlockHist(avmplus::GC
*gc
) : BlockSortedMap(gc
)
79 uint32_t count(const void *p
) {
80 uint32_t c
= 1+get(p
);
89 * This is the main control center for creating and managing fragments.
91 class Fragmento
: public avmplus::GCFinalizedObject
94 Fragmento(AvmCore
* core
, uint32_t cacheSizeLog2
);
97 void addMemory(void* firstPage
, uint32_t pageCount
); // gives memory to the Assembler
101 void pageFree(Page
* page
);
102 void pagesRelease(PageList
& list
);
104 Fragment
* getLoop(const void* ip
);
105 Fragment
* getAnchor(const void* ip
);
106 // Remove one fragment. The caller is responsible for making sure
107 // that this does not destroy any resources shared with other
108 // fragments (such as a LirBuffer or this fragment itself as a
110 void clearFrag(const void* ip
);
111 void clearFrags(); // clear all fragments from the cache
112 Fragment
* getMerge(GuardRecord
*lr
, const void* ip
);
113 Fragment
* createBranch(SideExit
*exit
, const void* ip
);
114 Fragment
* newFrag(const void* ip
);
115 Fragment
* newBranch(Fragment
*from
, const void* ip
);
117 verbose_only ( uint32_t pageCount(); )
118 verbose_only ( void dumpStats(); )
119 verbose_only ( void dumpRatio(const char*, BlockHist
*);)
120 verbose_only ( void dumpFragStats(Fragment
*, int level
, fragstats
&); )
121 verbose_only ( void countBlock(BlockHist
*, const void* pc
); )
122 verbose_only ( void countIL(uint32_t il
, uint32_t abc
); )
123 verbose_only( void addLabel(Fragment
* f
, const char *prefix
, int id
); )
128 uint32_t pages
; // pages consumed
129 uint32_t maxPageUse
; // highwater mark of (pages-freePages)
130 uint32_t flushes
, ilsize
, abcsize
, compiles
, totalCompiles
;
134 verbose_only( DWB(BlockHist
*) enterCounts
; )
135 verbose_only( DWB(BlockHist
*) mergeCounts
; )
136 verbose_only( DWB(LabelMap
*) labels
; )
138 #ifdef AVMPLUS_VERBOSE
139 void drawTrees(char *fileName
);
142 uint32_t cacheUsed() const { return (_stats
.pages
-_freePages
.size())<<NJ_LOG2_PAGE_SIZE
; }
143 uint32_t cacheUsedMax() const { return (_stats
.maxPageUse
)<<NJ_LOG2_PAGE_SIZE
; }
145 void clearFragment(Fragment
*f
);
146 void pagesGrow(int32_t count
);
150 DWB(Assembler
*) _assm
;
151 FragmentMap _frags
; /* map from ip -> Fragment ptr */
155 AllocList _allocList
;
156 avmplus::GCHeap
* _gcHeap
;
158 const uint32_t _max_pages
;
159 uint32_t _pagesGrowth
;
169 * Fragments are linear sequences of native code that have a single entry
170 * point at the start of the fragment and may have one or more exit points
172 * It may turn out that that this arrangement causes too much traffic
173 * between d and i-caches and that we need to carve up the structure differently.
175 class Fragment
: public avmplus::GCFinalizedObject
178 Fragment(const void*);
181 NIns
* code() { return _code
; }
182 void setCode(NIns
* codee
, Page
* pages
) { _code
= codee
; _pages
= pages
; }
183 GuardRecord
* links() { return _links
; }
184 int32_t& hits() { return _hits
; }
187 bool isBlacklisted() { return _hits
< 0; }
188 debug_only( bool hasOnlyTreeLinks(); )
189 void releaseLirBuffer();
190 void releaseCode(Fragmento
* frago
);
191 void releaseTreeMem(Fragmento
* frago
);
192 bool isAnchor() { return anchor
== this; }
193 bool isRoot() { return root
== this; }
196 verbose_only( uint32_t _called
; )
197 verbose_only( uint32_t _native
; )
198 verbose_only( uint32_t _exitNative
; )
199 verbose_only( uint32_t _lir
; )
200 verbose_only( uint32_t _lirbytes
; )
201 verbose_only( const char* _token
; )
202 verbose_only( uint64_t traceTicks
; )
203 verbose_only( uint64_t interpTicks
; )
204 verbose_only( DWB(Fragment
*) eot_target
; )
205 verbose_only( uint32_t sid
;)
206 verbose_only( uint32_t compileNbr
;)
208 DWB(Fragment
*) treeBranches
;
209 DWB(Fragment
*) branches
;
210 DWB(Fragment
*) nextbranch
;
211 DWB(Fragment
*) anchor
;
213 DWB(Fragment
*) parent
;
214 DWB(Fragment
*) first
;
216 DWB(LirBuffer
*) lirbuf
;
218 SideExit
* spawnedFrom
;
224 uint32_t recordAttempts
;
225 int32_t blacklistLevel
;
231 NIns
* _code
; // ptr to start of code
232 GuardRecord
* _links
; // code which is linked (or pending to be) to this fragment
234 Page
* _pages
; // native code pages
237 #endif // __nanojit_Fragmento__