comparison graal/com.oracle.truffle.api/src/com/oracle/truffle/api/utilities/AssumedValue.java @ 17056:0e7894989f37

Truffle: fix AssumedValue.
author Chris Seaton <chris.seaton@oracle.com>
date Sat, 06 Sep 2014 17:14:00 +0100
parents f28ea693056f
children 935de03661c1
comparison
equal deleted inserted replaced
17055:3e4d3be0b6bf 17056:0e7894989f37
1 /* 1 /*
2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. 2 * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 * 4 *
5 * This code is free software; you can redistribute it and/or modify it 5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as 6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this 7 * published by the Free Software Foundation. Oracle designates this
33 * Note that you should be careful that modifications to this value do not cause deoptimization 33 * Note that you should be careful that modifications to this value do not cause deoptimization
34 * loops. This could be by using a value that is monotonic. 34 * loops. This could be by using a value that is monotonic.
35 */ 35 */
36 public class AssumedValue<T> { 36 public class AssumedValue<T> {
37 37
38 private final String name;
39
38 @CompilationFinal private T value; 40 @CompilationFinal private T value;
39 private final CyclicAssumption assumption; 41 @CompilationFinal private Assumption assumption;
42
43 public AssumedValue(T initialValue) {
44 this(null, initialValue);
45 }
40 46
41 public AssumedValue(String name, T initialValue) { 47 public AssumedValue(String name, T initialValue) {
42 assumption = new CyclicAssumption(name); 48 this.name = name;
43 value = initialValue; 49 value = initialValue;
50 assumption = Truffle.getRuntime().createAssumption(name);
44 } 51 }
45 52
46 /** 53 /**
47 * Get the current value, updating it if it has been {@link #set}. The compiler may be able to 54 * Get the current value, updating it if it has been {@link #set}. The compiler may be able to
48 * make this method return a constant value, but still accommodate mutation. 55 * make this method return a constant value, but still accommodate mutation.
49 */ 56 */
50 public T get() { 57 public T get() {
51 try { 58 try {
52 assumption.getAssumption().check(); 59 assumption.check();
53 } catch (InvalidAssumptionException e) { 60 } catch (InvalidAssumptionException e) {
54 // No need to rewrite anything - just pick up the new value 61 // No need to rewrite anything - just pick up the new values
55 } 62 }
56 63
57 return value; 64 return value;
58 } 65 }
59 66
60 /** 67 /**
61 * Set a new value, which will be picked up the next time {@link #get} is called. 68 * Set a new value, which will be picked up the next time {@link #get} is called.
62 */ 69 */
63 public void set(T newValue) { 70 public void set(T newValue) {
71 CompilerDirectives.transferToInterpreter();
72
64 value = newValue; 73 value = newValue;
65 assumption.invalidate(); 74 final Assumption oldAssumption = assumption;
75 assumption = Truffle.getRuntime().createAssumption(name);
76 oldAssumption.invalidate();
66 } 77 }
67 78
68 } 79 }