# HG changeset patch # User Christian Humer # Date 1409594898 -7200 # Node ID 8fd42ea95f6495860016a74a917fbf0897572768 # Parent 9716891b73421c92bd83b6fc3a1c1d14dd6243ee SL: added assertTrue and assertFalse builtins to SL. diff -r 9716891b7342 -r 8fd42ea95f64 graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/builtins/SLAssertFalseBuiltin.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/builtins/SLAssertFalseBuiltin.java Mon Sep 01 20:08:18 2014 +0200 @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package com.oracle.truffle.sl.builtins; + +import com.oracle.truffle.api.*; +import com.oracle.truffle.api.dsl.*; +import com.oracle.truffle.api.nodes.*; +import com.oracle.truffle.api.source.*; +import com.oracle.truffle.sl.runtime.*; + +/** + * Asserts a given value to be false and throws an {@link AssertionError} if the value + * was true. + */ +@NodeInfo(shortName = "assertFalse") +public abstract class SLAssertFalseBuiltin extends SLBuiltinNode { + + public SLAssertFalseBuiltin() { + super(new NullSourceSection("SL builtin", "assertFalse")); + } + + @Specialization + public boolean doAssert(boolean value, String message) { + if (value) { + CompilerDirectives.transferToInterpreter(); + if (message == null) { + throw new AssertionError(); + } else { + throw new AssertionError(message); + } + } + return value; + } + + @Specialization + public boolean doAssertNull(boolean value, @SuppressWarnings("unused") SLNull message) { + return doAssert(value, null); + } + +} diff -r 9716891b7342 -r 8fd42ea95f64 graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/builtins/SLAssertTrueBuiltin.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/builtins/SLAssertTrueBuiltin.java Mon Sep 01 20:08:18 2014 +0200 @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package com.oracle.truffle.sl.builtins; + +import com.oracle.truffle.api.*; +import com.oracle.truffle.api.dsl.*; +import com.oracle.truffle.api.nodes.*; +import com.oracle.truffle.api.source.*; +import com.oracle.truffle.sl.runtime.*; + +/** + * Asserts a given value to be true and throws an {@link AssertionError} if the value + * was false. + */ +@NodeInfo(shortName = "assertTrue") +public abstract class SLAssertTrueBuiltin extends SLBuiltinNode { + + public SLAssertTrueBuiltin() { + super(new NullSourceSection("SL builtin", "assertTrue")); + } + + @Specialization + public boolean doAssert(boolean value, String message) { + if (!value) { + CompilerDirectives.transferToInterpreter(); + if (message == null) { + throw new AssertionError(); + } else { + throw new AssertionError(message); + } + } + return value; + } + + @Specialization + public boolean doAssertNull(boolean value, @SuppressWarnings("unused") SLNull message) { + return doAssert(value, null); + } + +} diff -r 9716891b7342 -r 8fd42ea95f64 graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/runtime/SLContext.java --- a/graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/runtime/SLContext.java Mon Sep 01 16:46:59 2014 +0200 +++ b/graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/runtime/SLContext.java Mon Sep 01 20:08:18 2014 +0200 @@ -109,9 +109,11 @@ installBuiltin(SLDefineFunctionBuiltinFactory.getInstance()); installBuiltin(SLStackTraceBuiltinFactory.getInstance()); installBuiltin(SLHelloEqualsWorldBuiltinFactory.getInstance()); + installBuiltin(SLAssertTrueBuiltinFactory.getInstance()); + installBuiltin(SLAssertFalseBuiltinFactory.getInstance()); } - private void installBuiltin(NodeFactory factory) { + public void installBuiltin(NodeFactory factory) { /* * The builtin node factory is a class that is automatically generated by the Truffle DSL. * The signature returned by the factory reflects the signature of the @Specialization