35
35
import com .oracle .graal .python .builtins .objects .type .LazyPythonClass ;
36
36
import com .oracle .graal .python .nodes .SpecialMethodNames ;
37
37
import com .oracle .graal .python .nodes .generator .GeneratorFunctionRootNode ;
38
+ import com .oracle .truffle .api .Assumption ;
38
39
import com .oracle .truffle .api .CompilerAsserts ;
40
+ import com .oracle .truffle .api .CompilerDirectives ;
39
41
import com .oracle .truffle .api .CompilerDirectives .CompilationFinal ;
40
42
import com .oracle .truffle .api .CompilerDirectives .TruffleBoundary ;
41
43
import com .oracle .truffle .api .RootCallTarget ;
44
+ import com .oracle .truffle .api .Truffle ;
42
45
import com .oracle .truffle .api .nodes .RootNode ;
43
46
import com .oracle .truffle .api .object .DynamicObject ;
44
47
import com .oracle .truffle .api .source .SourceSection ;
45
- import com .oracle .truffle .api .utilities .CyclicAssumption ;
46
48
47
49
public class PFunction extends PythonObject {
48
50
private static final Object [] EMPTY_DEFAULTS = new Object [0 ];
49
51
private final String name ;
50
52
private final String enclosingClassName ;
51
- private final CyclicAssumption codeStableAssumption = new CyclicAssumption ( "function code unchanged" );
52
- private final CyclicAssumption defaultsStableAssumption = new CyclicAssumption ( "function defaults unchanged" );
53
+ private final Assumption codeStableAssumption = Truffle . getRuntime (). createAssumption ( "function code unchanged for " + toString () );
54
+ private final Assumption defaultsStableAssumption = Truffle . getRuntime (). createAssumption ( "function defaults unchanged " + toString () );
53
55
private final PythonObject globals ;
54
56
private final PCell [] closure ;
55
57
private final boolean isStatic ;
56
58
@ CompilationFinal private PCode code ;
59
+ private PCode uncachedCode ;
57
60
@ CompilationFinal (dimensions = 1 ) private Object [] defaultValues ;
61
+ private Object [] uncachedDefaultValues ;
58
62
@ CompilationFinal (dimensions = 1 ) private PKeyword [] kwDefaultValues ;
63
+ private PKeyword [] uncachedKwDefaultValues ;
59
64
60
65
public PFunction (LazyPythonClass clazz , String name , String enclosingClassName , RootCallTarget callTarget , PythonObject globals , PCell [] closure ) {
61
66
this (clazz , name , enclosingClassName , callTarget , globals , EMPTY_DEFAULTS , PKeyword .EMPTY_KEYWORDS , closure );
@@ -65,12 +70,12 @@ public PFunction(LazyPythonClass clazz, String name, String enclosingClassName,
65
70
PCell [] closure ) {
66
71
super (clazz );
67
72
this .name = name ;
68
- this .code = new PCode (PythonBuiltinClassType .PCode , callTarget );
73
+ this .code = this . uncachedCode = new PCode (PythonBuiltinClassType .PCode , callTarget );
69
74
this .isStatic = name .equals (SpecialMethodNames .__NEW__ );
70
75
this .enclosingClassName = enclosingClassName ;
71
76
this .globals = globals ;
72
- this .defaultValues = defaultValues == null ? EMPTY_DEFAULTS : defaultValues ;
73
- this .kwDefaultValues = kwDefaultValues == null ? PKeyword .EMPTY_KEYWORDS : kwDefaultValues ;
77
+ this .defaultValues = this . uncachedDefaultValues = defaultValues == null ? EMPTY_DEFAULTS : defaultValues ;
78
+ this .kwDefaultValues = this . uncachedKwDefaultValues = kwDefaultValues == null ? PKeyword .EMPTY_KEYWORDS : kwDefaultValues ;
74
79
this .closure = closure ;
75
80
addDefaultConstants (this .getStorage (), name , enclosingClassName );
76
81
}
@@ -86,14 +91,14 @@ public boolean isStatic() {
86
91
}
87
92
88
93
public RootCallTarget getCallTarget () {
89
- return code .getRootCallTarget ();
94
+ return getCode () .getRootCallTarget ();
90
95
}
91
96
92
- public CyclicAssumption getCodeStableAssumption () {
97
+ public Assumption getCodeStableAssumption () {
93
98
return codeStableAssumption ;
94
99
}
95
100
96
- public CyclicAssumption getDefaultsStableAssumption () {
101
+ public Assumption getDefaultsStableAssumption () {
97
102
return defaultsStableAssumption ;
98
103
}
99
104
@@ -110,7 +115,7 @@ public String getName() {
110
115
}
111
116
112
117
public Arity getArity () {
113
- return code .getArity ();
118
+ return getCode () .getArity ();
114
119
}
115
120
116
121
public PCell [] getClosure () {
@@ -136,31 +141,52 @@ public final String toString() {
136
141
}
137
142
138
143
public PCode getCode () {
139
- return code ;
144
+ Assumption assumption = this .codeStableAssumption ;
145
+ if (CompilerDirectives .isCompilationConstant (this ) && CompilerDirectives .isCompilationConstant (assumption )) {
146
+ if (assumption .isValid ()) {
147
+ return code ;
148
+ }
149
+ }
150
+ return uncachedCode ;
140
151
}
141
152
142
153
public void setCode (PCode code ) {
143
- this .code = code ;
154
+ codeStableAssumption .invalidate ("code changed for function " + getName ());
155
+ this .code = this .uncachedCode = code ;
144
156
}
145
157
146
158
public String getEnclosingClassName () {
147
159
return enclosingClassName ;
148
160
}
149
161
150
162
public Object [] getDefaults () {
151
- return defaultValues ;
163
+ Assumption assumption = this .defaultsStableAssumption ;
164
+ if (CompilerDirectives .isCompilationConstant (this ) && CompilerDirectives .isCompilationConstant (assumption )) {
165
+ if (assumption .isValid ()) {
166
+ return defaultValues ;
167
+ }
168
+ }
169
+ return uncachedDefaultValues ;
152
170
}
153
171
154
172
public void setDefaults (Object [] defaults ) {
155
- this .defaultValues = defaults ;
173
+ this .defaultsStableAssumption .invalidate ("defaults changed for function " + getName ());
174
+ this .defaultValues = this .uncachedDefaultValues = defaults ;
156
175
}
157
176
158
177
public PKeyword [] getKwDefaults () {
159
- return kwDefaultValues ;
178
+ Assumption assumption = this .defaultsStableAssumption ;
179
+ if (CompilerDirectives .isCompilationConstant (this ) && CompilerDirectives .isCompilationConstant (assumption )) {
180
+ if (assumption .isValid ()) {
181
+ return kwDefaultValues ;
182
+ }
183
+ }
184
+ return uncachedKwDefaultValues ;
160
185
}
161
186
162
187
public void setKwDefaults (PKeyword [] defaults ) {
163
- this .kwDefaultValues = defaults ;
188
+ this .defaultsStableAssumption .invalidate ("kw defaults changed for function " + getName ());
189
+ this .kwDefaultValues = this .uncachedKwDefaultValues = defaults ;
164
190
}
165
191
166
192
@ TruffleBoundary
0 commit comments