Skip to content

Commit 0dfa9c1

Browse files
committed
Change Truffle::Graal.assert_constant into a TrufflePrimitive.assert_compilation_constant
1 parent b902c98 commit 0dfa9c1

File tree

10 files changed

+44
-81
lines changed

10 files changed

+44
-81
lines changed

doc/contributor/testing.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ The compiler tests include a sort of test suite for compilation, called the
117117
partial evaluation (*PE*) tests, as well as tests for more subtle things like
118118
on-stack-replacement.
119119

120-
Special methods such as `Truffle::Graal.assert_constant` are used to implement
120+
Special methods such as `TrufflePrimitive.assert_compilation_constant` are used to implement
121121
this.
122122

123123
Compiler tests are in `test/truffle/compiler`.

doc/samples/can-we-fold-yet.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919

2020
test_thread = Thread.new do
2121
begin
22-
eval "loop { Truffle::Graal.assert_constant #{code}; Truffle::Graal.assert_not_compiled; Thread.pass }"
22+
eval "loop { TrufflePrimitive.assert_compilation_constant #{code}; Truffle::Graal.assert_not_compiled; Thread.pass }"
2323
rescue Truffle::GraalError => e
2424
if e.message.include? 'Truffle::Graal.assert_not_compiled'
2525
puts "Yes! Truffle can constant fold this to #{eval(code).inspect}"
26-
elsif e.message.include? 'Truffle::Graal.assert_constant'
26+
elsif e.message.include? 'TrufflePrimitive.assert_compilation_constant'
2727
puts "No :( Truffle can't constant fold that"
2828
else
2929
puts 'There was an error executing that :('

spec/truffle/graal/assert_constant_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88

99
require_relative '../../ruby/spec_helper'
1010

11-
describe "Truffle::Graal.assert_constant" do
11+
describe "TrufflePrimitive.assert_compilation_constant" do
1212

1313
it "raises a RuntimeError when called dynamically" do
1414
-> { Truffle::Graal.send(:assert_constant, 14 + 2) }.should raise_error(RuntimeError)
1515
end
1616

1717
guard -> { !TruffleRuby.jit? } do
1818
it "returns the value of the argument" do
19-
Truffle::Graal.assert_constant(14 + 2).should == 16
19+
TrufflePrimitive.assert_compilation_constant(14 + 2).should == 16
2020
end
2121
end
2222

src/main/java/org/truffleruby/core/exception/CoreExceptions.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -243,10 +243,6 @@ public DynamicObject frozenError(String message, Node currentNode) {
243243

244244
// RuntimeError
245245

246-
public DynamicObject runtimeErrorNotConstant(Node currentNode) {
247-
return runtimeError("Truffle::Graal.assert_constant can only be called lexically", currentNode);
248-
}
249-
250246
public DynamicObject runtimeErrorCompiled(Node currentNode) {
251247
return runtimeError("Truffle::Graal.assert_not_compiled can only be called lexically", currentNode);
252248
}
@@ -964,7 +960,7 @@ public DynamicObject rangeError(String message, Node currentNode) {
964960
// Truffle::GraalError
965961

966962
public DynamicObject graalErrorAssertConstantNotConstant(Node currentNode) {
967-
return graalError("value in Truffle::Graal.assert_constant was not constant", currentNode);
963+
return graalError("value in TrufflePrimitive.assert_compilation_constant was not constant", currentNode);
968964
}
969965

970966
public DynamicObject graalErrorAssertNotCompiledCompiled(Node currentNode) {

src/main/java/org/truffleruby/extra/TruffleGraalNodes.java

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,14 @@
99
*/
1010
package org.truffleruby.extra;
1111

12+
import com.oracle.truffle.api.CompilerDirectives;
13+
import com.oracle.truffle.api.dsl.NodeChild;
1214
import org.truffleruby.Layouts;
1315
import org.truffleruby.builtins.CoreModule;
1416
import org.truffleruby.builtins.CoreMethod;
1517
import org.truffleruby.builtins.CoreMethodArrayArgumentsNode;
18+
import org.truffleruby.builtins.Primitive;
19+
import org.truffleruby.builtins.PrimitiveNode;
1620
import org.truffleruby.core.proc.ProcType;
1721
import org.truffleruby.language.RubyNode;
1822
import org.truffleruby.language.RubyRootNode;
@@ -34,17 +38,6 @@
3438
@CoreModule("Truffle::Graal")
3539
public abstract class TruffleGraalNodes {
3640

37-
@CoreMethod(names = "assert_constant", onSingleton = true, required = 1)
38-
public abstract static class AssertConstantNode extends CoreMethodArrayArgumentsNode {
39-
40-
@TruffleBoundary
41-
@Specialization
42-
protected DynamicObject assertConstant(Object value) {
43-
throw new RaiseException(getContext(), coreExceptions().runtimeErrorNotConstant(this));
44-
}
45-
46-
}
47-
4841
@CoreMethod(names = "assert_not_compiled", onSingleton = true)
4942
public abstract static class AssertNotCompiledNode extends CoreMethodArrayArgumentsNode {
5043

@@ -170,4 +163,24 @@ protected DynamicObject copyCapturedLocals(DynamicObject proc) {
170163

171164
}
172165

166+
@NodeChild(value = "value", type = RubyNode.class)
167+
@Primitive(name = "assert_compilation_constant")
168+
public abstract static class AssertConstantNode extends PrimitiveNode {
169+
170+
@Specialization
171+
protected Object assertCompilationConstant(Object value) {
172+
if (!CompilerDirectives.isCompilationConstant(value)) {
173+
notConstantBoundary();
174+
}
175+
176+
return value;
177+
}
178+
179+
@TruffleBoundary
180+
private void notConstantBoundary() {
181+
throw new RaiseException(getContext(), coreExceptions().graalErrorAssertConstantNotConstant(this), true);
182+
}
183+
}
184+
185+
173186
}

src/main/java/org/truffleruby/parser/BodyTranslator.java

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -504,13 +504,9 @@ public RubyNode visitCallNode(CallParseNode node) {
504504
final Rope rope = context.getRopeCache().getRope(nodeRope, codeRange);
505505
final DynamicObject frozenString = context.getFrozenStringLiteral(rope);
506506

507-
return addNewlineIfNeeded(
508-
node,
509-
withSourceSection(
510-
sourceSection,
511-
new DefinedWrapperNode(
512-
context.getCoreStrings().METHOD,
513-
new ObjectLiteralNode(frozenString))));
507+
return addNewlineIfNeeded(node, withSourceSection(
508+
sourceSection,
509+
new DefinedWrapperNode(context.getCoreStrings().METHOD, new ObjectLiteralNode(frozenString))));
514510
}
515511

516512
if (receiver instanceof ConstParseNode &&
@@ -523,12 +519,7 @@ public RubyNode visitCallNode(CallParseNode node) {
523519
&& ((Colon2ConstParseNode) receiver).getLeftNode() instanceof ConstParseNode &&
524520
((ConstParseNode) ((Colon2ConstParseNode) receiver).getLeftNode()).getName().equals("Truffle") &&
525521
((Colon2ConstParseNode) receiver).getName().equals("Graal")) {
526-
if (methodName.equals("assert_constant")) {
527-
final RubyNode ret = AssertConstantNodeGen
528-
.create(((ArrayParseNode) node.getArgsNode()).get(0).accept(this));
529-
ret.unsafeSetSourceSection(sourceSection);
530-
return addNewlineIfNeeded(node, ret);
531-
} else if (methodName.equals("assert_not_compiled")) {
522+
if (methodName.equals("assert_not_compiled")) {
532523
final RubyNode ret = AssertNotCompiledNodeGen.create();
533524
ret.unsafeSetSourceSection(sourceSection);
534525
return addNewlineIfNeeded(node, ret);

src/main/java/org/truffleruby/platform/AssertConstantNode.java

Lines changed: 0 additions & 37 deletions
This file was deleted.

test/truffle/compiler/pe/pe.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
# do:
2020
#
2121
# loop do
22-
# Truffle::Graal.assert_constant expression
22+
# TrufflePrimitive.assert_compilation_constant expression
2323
# end
2424
#
2525
# Run with:
@@ -89,11 +89,11 @@ def counter(example)
8989
require_relative 'interop/interop_pe'
9090
require_relative 'truffle/engine_pe.rb'
9191
require_relative 'macro/pushing_pixels_pe.rb'
92-
92+
9393
if Truffle::Interop.mime_type_supported?('application/javascript')
9494
require_relative 'interop/js.rb'
9595
end
96-
96+
9797
if Truffle::Interop.mime_type_supported?('application/x-r')
9898
require_relative 'interop/r.rb'
9999
end
@@ -114,13 +114,13 @@ def report(status, code, message = nil)
114114
next if example.tagged
115115

116116
finished = false
117-
117+
118118
runner = proc do
119119
begin
120120
tested += 1
121121
eval "
122122
def test_pe_code
123-
value = Truffle::Graal.assert_constant(begin; #{example.code}; end)
123+
value = TrufflePrimitive.assert_compilation_constant(begin; #{example.code}; end)
124124
Truffle::Graal.assert_not_compiled
125125
value
126126
end"
@@ -130,7 +130,7 @@ def test_pe_code
130130
rescue Truffle::GraalError => e
131131
if e.message.include? 'Truffle::Graal.assert_not_compiled'
132132
constant = true
133-
elsif e.message.include? 'Truffle::Graal.assert_constant'
133+
elsif e.message.include? 'TrufflePrimitive.assert_compilation_constant'
134134
constant = false
135135
else
136136
constant = nil

test/truffle/compiler/stf-optimises/stf-optimises.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ def foo
2323
loop do
2424
x = foo
2525
raise 'value not correct' unless x == 200
26-
Truffle::Graal.assert_constant x
26+
TrufflePrimitive.assert_compilation_constant x
2727
Truffle::Graal.assert_not_compiled
2828
end
2929
rescue Truffle::GraalError => e
3030
if e.message.include? 'Truffle::Graal.assert_not_compiled'
3131
puts 'STF optimising'
3232
exit 0
33-
elsif e.message.include? 'Truffle::Graal.assert_constant'
33+
elsif e.message.include? 'TrufflePrimitive.assert_compilation_constant'
3434
puts 'STF not optimising'
3535
exit 1
3636
else

test/truffle/compiler/tp-optimises/tp-optimises.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ def foo
2525
loop do
2626
x = foo
2727
raise 'value not correct' unless x == 200
28-
Truffle::Graal.assert_constant x
28+
TrufflePrimitive.assert_compilation_constant x
2929
Truffle::Graal.assert_not_compiled
3030
end
3131
rescue Truffle::GraalError => e
3232
if e.message.include? 'Truffle::Graal.assert_not_compiled'
3333
puts 'TP optimising'
3434
exit 0
35-
elsif e.message.include? 'Truffle::Graal.assert_constant'
35+
elsif e.message.include? 'TrufflePrimitive.assert_compilation_constant'
3636
puts 'TP not optimising'
3737
exit 1
3838
else

0 commit comments

Comments
 (0)