Mercurial > hg > truffle
changeset 17003:8fd42ea95f64
SL: added assertTrue and assertFalse builtins to SL.
author | Christian Humer <christian.humer@gmail.com> |
---|---|
date | Mon, 01 Sep 2014 20:08:18 +0200 |
parents | 9716891b7342 |
children | 158c9ba66e45 |
files | graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/builtins/SLAssertFalseBuiltin.java graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/builtins/SLAssertTrueBuiltin.java graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/runtime/SLContext.java |
diffstat | 3 files changed, 123 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- /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 <code>false</code> and throws an {@link AssertionError} if the value + * was <code>true</code>. + */ +@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); + } + +}
--- /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 <code>true</code> and throws an {@link AssertionError} if the value + * was <code>false</code>. + */ +@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); + } + +}
--- 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<? extends SLBuiltinNode> factory) { + public void installBuiltin(NodeFactory<? extends SLBuiltinNode> 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