Skip to content

Commit 674d177

Browse files
committed
Add ConstantOperand tests for fallback and LocalSetters
1 parent 2cb9945 commit 674d177

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

truffle/src/com.oracle.truffle.api.bytecode.test/src/com/oracle/truffle/api/bytecode/test/ConstantOperandTest.java

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@
5252
import com.oracle.truffle.api.TruffleLanguage;
5353
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
5454
import com.oracle.truffle.api.bytecode.BytecodeConfig;
55+
import com.oracle.truffle.api.bytecode.BytecodeLocal;
56+
import com.oracle.truffle.api.bytecode.BytecodeNode;
5557
import com.oracle.truffle.api.bytecode.BytecodeParser;
5658
import com.oracle.truffle.api.bytecode.BytecodeRootNode;
5759
import com.oracle.truffle.api.bytecode.ConstantOperand;
@@ -60,12 +62,14 @@
6062
import com.oracle.truffle.api.bytecode.EpilogReturn;
6163
import com.oracle.truffle.api.bytecode.GenerateBytecode;
6264
import com.oracle.truffle.api.bytecode.Instrumentation;
65+
import com.oracle.truffle.api.bytecode.LocalSetter;
6366
import com.oracle.truffle.api.bytecode.Operation;
6467
import com.oracle.truffle.api.bytecode.Prolog;
6568
import com.oracle.truffle.api.bytecode.test.ConstantOperandTestRootNode.ReplaceValue;
6669
import com.oracle.truffle.api.bytecode.test.error_tests.ExpectError;
6770
import com.oracle.truffle.api.bytecode.test.error_tests.ExpectWarning;
6871
import com.oracle.truffle.api.dsl.Bind;
72+
import com.oracle.truffle.api.dsl.Fallback;
6973
import com.oracle.truffle.api.dsl.Specialization;
7074
import com.oracle.truffle.api.exception.AbstractTruffleException;
7175
import com.oracle.truffle.api.frame.FrameDescriptor;
@@ -179,6 +183,41 @@ public void testInstrumentationWithConstantAndYield() {
179183
assertEquals(123, cont.getResult());
180184
}
181185

186+
@Test
187+
public void testConstantWithFallback() {
188+
ConstantOperandTestRootNode root = parse(b -> {
189+
b.beginRoot(LANGUAGE);
190+
b.beginReturn();
191+
b.beginCheckValue(42);
192+
b.emitLoadArgument(0);
193+
b.endCheckValue();
194+
b.endReturn();
195+
b.endRoot();
196+
});
197+
assertEquals(true, root.getCallTarget().call(42));
198+
assertEquals(false, root.getCallTarget().call(43));
199+
assertEquals(false, root.getCallTarget().call("foo"));
200+
}
201+
202+
@Test
203+
public void testLocalSetter() {
204+
ConstantOperandTestRootNode root = parse(b -> {
205+
b.beginRoot(LANGUAGE);
206+
BytecodeLocal local = b.createLocal();
207+
b.beginSetCheckValue(42, local);
208+
b.emitLoadArgument(0);
209+
b.endSetCheckValue();
210+
211+
b.beginReturn();
212+
b.emitLoadLocal(local);
213+
b.endReturn();
214+
b.endRoot();
215+
});
216+
assertEquals(true, root.getCallTarget().call(42));
217+
assertEquals(false, root.getCallTarget().call(43));
218+
assertEquals(false, root.getCallTarget().call("foo"));
219+
}
220+
182221
@Test
183222
public void testConstantOperandsInProlog() {
184223
ConstantOperandsInPrologTestRootNode root = ConstantOperandsInPrologTestRootNodeGen.create(BytecodeConfig.DEFAULT, b -> {
@@ -276,6 +315,41 @@ public static int doInt(int constantOperand) {
276315
}
277316
}
278317

318+
@Operation
319+
@ConstantOperand(type = int.class)
320+
@SuppressWarnings("unused")
321+
public static final class CheckValue {
322+
@Specialization(guards = "arg == constantOperand")
323+
public static boolean doMatch(int constantOperand, int arg) {
324+
return true;
325+
}
326+
327+
@Fallback
328+
public static boolean doNoMatch(int constantOperand, Object arg) {
329+
return false;
330+
}
331+
}
332+
333+
@Operation
334+
@ConstantOperand(type = int.class)
335+
@ConstantOperand(type = LocalSetter.class)
336+
@SuppressWarnings("unused")
337+
public static final class SetCheckValue {
338+
@Specialization(guards = "arg == constantOperand")
339+
public static void doMatch(VirtualFrame frame, int constantOperand, LocalSetter setter, int arg,
340+
@Bind("$bytecode") BytecodeNode bytecode,
341+
@Bind("$bci") int bci) {
342+
setter.setBoolean(bytecode, bci, frame, true);
343+
}
344+
345+
@Fallback
346+
public static void doNoMatch(VirtualFrame frame, int constantOperand, LocalSetter setter, Object arg,
347+
@Bind("$bytecode") BytecodeNode bytecode,
348+
@Bind("$bci") int bci) {
349+
setter.setBoolean(bytecode, bci, frame, false);
350+
}
351+
}
352+
279353
@Instrumentation
280354
@ConstantOperand(type = int.class, specifyAtEnd = true)
281355
public static final class ReplaceValue {

0 commit comments

Comments
 (0)