Imported Upstream version 1.0.0
[platform/upstream/js.git] / js / src / methodjit / Retcon.h
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  * vim: set ts=4 sw=4 et tw=99:
3  *
4  * ***** BEGIN LICENSE BLOCK *****
5  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * The Original Code is Mozilla Jaegermonkey.
18  *
19  * The Initial Developer of the Original Code is the Mozilla Foundation.
20  *
21  * Portions created by the Initial Developer are Copyright (C) 2010
22  * the Initial Developer. All Rights Reserved.
23  *
24  * Contributor(s):
25  *   Andrew Drake <drakedevel@gmail.com>
26  *
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.
38  *
39  * ***** END LICENSE BLOCK ***** */
40
41 /*
42  * Retroactive continuity ("retcon") refers to the retroactive modification
43  * or reinterpretation of established facts. 
44  */
45
46 #if !defined jsjaeger_retcon_h__ && defined JS_METHODJIT
47 #define jsjaeger_retcon_h__
48
49 #include "jscntxt.h"
50 #include "jsscript.h"
51 #include "MethodJIT.h"
52 #include "Compiler.h"
53
54 namespace js {
55 namespace mjit {
56
57 /*
58  * A problem often arises where, for one reason or another, a piece of code
59  * wants to touch the script->code, but isn't expecting JSOP_TRAP. This allows
60  * one to temporarily remove JSOP_TRAPs from the instruction stream (without
61  * copying) and automatically re-add them on scope exit.
62  */
63 class AutoScriptRetrapper
64 {
65   public:
66     AutoScriptRetrapper(JSContext *cx1, JSScript *script1) :
67         cx(cx1), script(script1), traps(cx) {};
68     ~AutoScriptRetrapper();
69
70     bool untrap(jsbytecode *pc);
71
72   private:
73     JSContext *cx;
74     JSScript *script;
75     Vector<jsbytecode*> traps;
76 };
77
78 /*
79  * This class is responsible for sanely re-JITing a script and fixing up
80  * the world. If you ever change the code associated with a JSScript, or
81  * otherwise would cause existing JITed code to be incorrect, you /must/ use
82  * this to invalidate and potentially re-compile the existing JITed code,
83  * fixing up the stack in the process.
84  */
85 class Recompiler {
86     struct PatchableAddress {
87         void **location;
88         CallSite callSite;
89     };
90     
91 public:
92     Recompiler(JSContext *cx, JSScript *script);
93     
94     bool recompile();
95
96 private:
97     JSContext *cx;
98     JSScript *script;
99     
100     PatchableAddress findPatch(JITScript *jit, void **location);
101     void applyPatch(Compiler& c, PatchableAddress& toPatch);
102     bool recompile(JSStackFrame *fp, Vector<PatchableAddress> &patches,
103                    Vector<CallSite> &sites);
104     bool saveTraps(JITScript *jit, Vector<CallSite> *sites);
105 };
106
107 } /* namespace mjit */
108 } /* namespace js */
109
110 #endif
111