use of com.oracle.truffle.api.source.SourceSection in project graal by oracle.
the class SLException method typeError.
/**
* Provides a user-readable message for run-time type errors. SL is strongly typed, i.e., there
* are no automatic type conversions of values.
*/
@TruffleBoundary
public static SLException typeError(Node operation, Object... values) {
StringBuilder result = new StringBuilder();
result.append("Type error");
if (operation != null) {
SourceSection ss = operation.getEncapsulatingSourceSection();
if (ss != null && ss.isAvailable()) {
result.append(" at ").append(ss.getSource().getName()).append(" line ").append(ss.getStartLine()).append(" col ").append(ss.getStartColumn());
}
}
result.append(": operation");
if (operation != null) {
NodeInfo nodeInfo = SLContext.lookupNodeInfo(operation.getClass());
if (nodeInfo != null) {
result.append(" \"").append(nodeInfo.shortName()).append("\"");
}
}
result.append(" not defined for");
String sep = " ";
for (int i = 0; i < values.length; i++) {
Object value = values[i];
result.append(sep);
sep = ", ";
if (value instanceof Long || value instanceof SLBigNumber) {
result.append("Number ").append(value);
} else if (value instanceof Boolean) {
result.append("Boolean ").append(value);
} else if (value instanceof String) {
result.append("String \"").append(value).append("\"");
} else if (value instanceof SLFunction) {
result.append("Function ").append(value);
} else if (value == SLNull.SINGLETON) {
result.append("NULL");
} else if (value == null) {
// value is not evaluated because of short circuit evaluation
result.append("ANY");
} else {
result.append(value);
}
}
return new SLException(result.toString(), operation);
}
use of com.oracle.truffle.api.source.SourceSection in project graal by oracle.
the class NodeSnippets method getEncapsulatingSourceSection.
/**
* Retrieves the segment of guest language source code that is represented by this Node, if
* present; otherwise retrieves the segment represented by the nearest AST ancestor that has
* this information. Can be called on any thread and without a language context.
*
* @return an approximation of the source code represented by this Node
* @since 0.8 or earlier
*/
@ExplodeLoop
public SourceSection getEncapsulatingSourceSection() {
Node current = this;
while (current != null) {
final SourceSection currentSection = current.getSourceSection();
if (currentSection != null) {
return currentSection;
}
current = current.parent;
}
return null;
}
use of com.oracle.truffle.api.source.SourceSection in project graal by oracle.
the class NodeUtil method printSourceAttributionTree.
private static void printSourceAttributionTree(PrintWriter p, Node parent, Node node, int level) {
if (node == null) {
return;
}
if (parent == null) {
// Add some preliminary information before starting with the root node
final SourceSection sourceSection = node.getSourceSection();
if (sourceSection != null) {
final String txt = sourceSection.getSource().getCharacters().toString();
p.println("Full source len=(" + txt.length() + ") ___" + txt + "___");
p.println("AST source attribution:");
}
}
final StringBuilder sb = new StringBuilder();
for (int i = 0; i < level; i++) {
sb.append("| ");
}
if (parent != null) {
sb.append(getNodeFieldName(parent, node, ""));
}
sb.append(" (" + node.getClass().getSimpleName() + ") ");
sb.append(printSyntaxTags(node));
sb.append(displaySourceAttribution(node));
p.println(sb.toString());
for (Node child : node.getChildren()) {
printSourceAttributionTree(p, node, child, level + 1);
}
p.flush();
}
use of com.oracle.truffle.api.source.SourceSection in project graal by oracle.
the class ProfilerTest method testRootName.
@Test
public void testRootName() throws IOException {
profiler.setCollecting(true);
profiler.setTiming(false);
Map<SourceSection, Profiler.Counter> counters = profiler.getCounters();
Assert.assertEquals(0, counters.size());
Assert.assertTrue(profiler.isCollecting());
Assert.assertFalse(profiler.isTiming());
Assert.assertFalse(profiler.hasData());
Assert.assertNull(profiler.getMimeTypes());
run(source);
counters = profiler.getCounters();
final com.oracle.truffle.api.source.Source sourceImpl = getSourceImpl(source);
final SourceSection rootSection = sourceImpl.createSection(0, 140);
final SourceSection leafSection = sourceImpl.createSection(17, 16);
final SourceSection callfooSection = sourceImpl.createSection(47, 27);
final SourceSection callbarSection = sourceImpl.createSection(88, 27);
Profiler.Counter root = counters.get(rootSection);
Profiler.Counter leaf = counters.get(leafSection);
Profiler.Counter callfoo = counters.get(callfooSection);
Profiler.Counter callbar = counters.get(callbarSection);
Assert.assertEquals("", root.getName());
Assert.assertEquals("foo", leaf.getName());
Assert.assertEquals("bar", callfoo.getName());
Assert.assertEquals("baz", callbar.getName());
}
use of com.oracle.truffle.api.source.SourceSection in project graal by oracle.
the class ProfilerTest method testInvocationCounts.
@Test
public void testInvocationCounts() throws IOException {
profiler.setCollecting(true);
profiler.setTiming(false);
Map<SourceSection, Profiler.Counter> counters = profiler.getCounters();
Assert.assertEquals(0, counters.size());
Assert.assertTrue(profiler.isCollecting());
Assert.assertFalse(profiler.isTiming());
Assert.assertFalse(profiler.hasData());
Assert.assertNull(profiler.getMimeTypes());
assertEvalOut(source, "");
counters = profiler.getCounters();
Assert.assertEquals(4, counters.size());
Assert.assertTrue(profiler.isCollecting());
Assert.assertFalse(profiler.isTiming());
Assert.assertTrue(profiler.hasData());
final com.oracle.truffle.api.source.Source sourceImpl = getSourceImpl(source);
final SourceSection rootSection = sourceImpl.createSection(0, 140);
final SourceSection leafSection = sourceImpl.createSection(17, 16);
final SourceSection callfooSection = sourceImpl.createSection(47, 27);
final SourceSection callbarSection = sourceImpl.createSection(88, 27);
Profiler.Counter root = counters.get(rootSection);
Profiler.Counter leaf = counters.get(leafSection);
Profiler.Counter callfoo = counters.get(callfooSection);
Profiler.Counter callbar = counters.get(callbarSection);
Assert.assertNotNull(root);
Assert.assertNotNull(leaf);
Assert.assertNotNull(callfoo);
Assert.assertNotNull(callbar);
final Profiler.Counter.TimeKind testTimeKind = Profiler.Counter.TimeKind.INTERPRETED_AND_COMPILED;
Assert.assertEquals(1L, root.getInvocations(testTimeKind));
Assert.assertEquals(200L, leaf.getInvocations(testTimeKind));
Assert.assertEquals(20L, callfoo.getInvocations(testTimeKind));
Assert.assertEquals(2L, callbar.getInvocations(testTimeKind));
profiler.setCollecting(false);
Assert.assertFalse(profiler.isCollecting());
Assert.assertFalse(profiler.isTiming());
Assert.assertTrue(profiler.hasData());
assertEvalOut(source, "");
Assert.assertFalse(profiler.isCollecting());
Assert.assertFalse(profiler.isTiming());
Assert.assertTrue(profiler.hasData());
Assert.assertEquals(1L, root.getInvocations(testTimeKind));
Assert.assertEquals(200L, leaf.getInvocations(testTimeKind));
Assert.assertEquals(20L, callfoo.getInvocations(testTimeKind));
Assert.assertEquals(2L, callbar.getInvocations(testTimeKind));
profiler.clearData();
Assert.assertFalse(profiler.isCollecting());
Assert.assertFalse(profiler.isTiming());
Assert.assertFalse(profiler.hasData());
Assert.assertEquals(0L, root.getInvocations(testTimeKind));
Assert.assertEquals(0L, leaf.getInvocations(testTimeKind));
Assert.assertEquals(0L, callfoo.getInvocations(testTimeKind));
Assert.assertEquals(0L, callbar.getInvocations(testTimeKind));
profiler.setCollecting(true);
Assert.assertTrue(profiler.isCollecting());
Assert.assertFalse(profiler.isTiming());
Assert.assertFalse(profiler.hasData());
assertEvalOut(source, "");
Assert.assertTrue(profiler.isCollecting());
Assert.assertFalse(profiler.isTiming());
Assert.assertTrue(profiler.hasData());
Assert.assertEquals(1L, root.getInvocations(testTimeKind));
Assert.assertEquals(200L, leaf.getInvocations(testTimeKind));
Assert.assertEquals(20L, callfoo.getInvocations(testTimeKind));
Assert.assertEquals(2L, callbar.getInvocations(testTimeKind));
profiler.clearData();
Assert.assertTrue(profiler.isCollecting());
Assert.assertFalse(profiler.isTiming());
Assert.assertFalse(profiler.hasData());
counters = profiler.getCounters();
Assert.assertEquals(4, counters.size());
for (int i = 0; i < 10000; i++) {
assertEvalOut(source, "");
}
Assert.assertTrue(profiler.isCollecting());
Assert.assertFalse(profiler.isTiming());
Assert.assertTrue(profiler.hasData());
root = counters.get(rootSection);
leaf = counters.get(leafSection);
callfoo = counters.get(callfooSection);
callbar = counters.get(callbarSection);
Assert.assertEquals(10000L, root.getInvocations(testTimeKind));
Assert.assertEquals(2000000L, leaf.getInvocations(testTimeKind));
Assert.assertEquals(200000L, callfoo.getInvocations(testTimeKind));
Assert.assertEquals(20000L, callbar.getInvocations(testTimeKind));
profiler.printHistograms(new PrintStream(out));
String o = getOut();
Assert.assertTrue(o != null && o.trim().length() > 0);
engine.close();
engine = null;
}
Aggregations