package org.mozartoz.truffle.nodes.local;

import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.frame.Frame;
import com.oracle.truffle.api.frame.FrameSlot;
import com.oracle.truffle.api.frame.FrameSlotTypeException;
import com.oracle.truffle.api.nodes.Node;

public abstract class ReadFrameSlotNode extends Node implements FrameSlotNode {

	private final FrameSlot slot;

	public ReadFrameSlotNode(FrameSlot slot) {
		this.slot = slot;
	}

	public FrameSlot getSlot() {
		return slot;
	}

	public abstract Object executeRead(Frame frame);

	@Specialization(rewriteOn = FrameSlotTypeException.class)
	protected long readLong(Frame frame) throws FrameSlotTypeException {
		return frame.getLong(slot);
	}

	@Specialization(rewriteOn = FrameSlotTypeException.class)
	protected double readDouble(Frame frame) throws FrameSlotTypeException {
		return frame.getDouble(slot);
	}

	@Specialization(rewriteOn = FrameSlotTypeException.class)
	protected boolean readBoolean(Frame frame) throws FrameSlotTypeException {
		return frame.getBoolean(slot);
	}

	@Specialization(rewriteOn = FrameSlotTypeException.class)
	protected Object readObject(Frame frame) throws FrameSlotTypeException {
		return frame.getObject(slot);
	}

	@Specialization(replaces = { "readLong", "readDouble", "readBoolean", "readObject" })
	protected Object read(Frame frame) {
		return frame.getValue(slot);
	}

	@Override
	public String toString() {
		return super.toString() + " " + slot.getIdentifier();
	}

}

@@ -36,16 +36,64 @@
 local;%0A%0A
+import com.oracle.truffle.api.dsl.ImportStatic;%0A
 import c
@@ -269,29 +269,67 @@
 rameSlot
-TypeException
+Kind;%0Aimport com.oracle.truffle.api.frame.FrameUtil
 ;%0Aimport
@@ -365,16 +365,51 @@
 .Node;%0A%0A
+@ImportStatic(FrameSlotKind.class)%0A
 public a
@@ -702,48 +702,38 @@
 ion(
-rewriteOn = FrameSlotTypeException.class
+guards = %22isKind(frame, Long)%22
 )%0A%09p
@@ -771,38 +771,8 @@
 ame)
- throws FrameSlotTypeException
  %7B%0A%09
@@ -783,21 +783,25 @@
 urn 
-f
+F
 rame
+Util
 .getLong
 (slo
@@ -796,17 +796,28 @@
 .getLong
-(
+Safe(frame, 
 slot);%0A%09
@@ -840,48 +840,40 @@
 ion(
-rewriteOn = FrameSlotTypeException.class
+guards = %22isKind(frame, Double)%22
 )%0A%09p
@@ -915,38 +915,8 @@
 ame)
- throws FrameSlotTypeException
  %7B%0A%09
@@ -919,29 +919,33 @@
  %7B%0A%09%09return 
-f
+F
 rame
+Util
 .getDouble(s
@@ -942,17 +942,28 @@
 etDouble
-(
+Safe(frame, 
 slot);%0A%09
@@ -986,48 +986,41 @@
 ion(
-rewriteOn = FrameSlotTypeException.class
+guards = %22isKind(frame, Boolean)%22
 )%0A%09p
@@ -1064,38 +1064,8 @@
 ame)
- throws FrameSlotTypeException
  %7B%0A%09
@@ -1068,29 +1068,33 @@
  %7B%0A%09%09return 
-f
+F
 rame
+Util
 .getBoolean(
@@ -1092,17 +1092,28 @@
 tBoolean
-(
+Safe(frame, 
 slot);%0A%09
@@ -1136,48 +1136,40 @@
 ion(
-rewriteOn = FrameSlotTypeException.class
+guards = %22isKind(frame, Object)%22
 )%0A%09p
@@ -1211,38 +1211,8 @@
 ame)
- throws FrameSlotTypeException
  %7B%0A%09
@@ -1215,29 +1215,33 @@
  %7B%0A%09%09return 
-f
+F
 rame
+Util
 .getObject(s
@@ -1238,17 +1238,28 @@
 etObject
-(
+Safe(frame, 
 slot);%0A%09
@@ -1421,16 +1421,150 @@
 t);%0A%09%7D%0A%0A
+%09protected boolean isKind(Frame frame, FrameSlotKind kind) %7B%0A%09%09return frame.getFrameDescriptor().getFrameSlotKind(slot) == kind;%0A%0A%09%7D%0A%0A
 %09@Overri

Rewrite ReadFrameSlotNode to avoid too many exceptions