Skip to content

Commit

Permalink
cleaned up detection of user code, made recompatible with nodes outsi…
Browse files Browse the repository at this point in the history
…de of components
  • Loading branch information
Henry Cook authored and Henry Cook committed May 23, 2013
1 parent 1b08309 commit 2c93b2d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 38 deletions.
59 changes: 26 additions & 33 deletions src/main/scala/ChiselError.scala
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,8 @@ object ChiselError {
def apply(mf: => String, n: Node): ChiselError =
new ChiselError(() => mf, n.line)
def apply(m: String, stack: Array[StackTraceElement]): ChiselError = {
new ChiselError(() => m, findFirstUserLine(stack))
}
def apply(m: String, stack: Array[StackTraceElement]): ChiselError =
new ChiselError(() => m, findFirstUserLine(stack) getOrElse stack(0))
def apply(mf: => String, stack: Array[StackTraceElement]): ChiselError =
new ChiselError(() => mf, findFirstUserLine(stack))
Expand All @@ -64,52 +63,46 @@ object ChiselError {
}

def error(m: String) {
error(m, findFirstUserLine(Thread.currentThread().getStackTrace))
val stack = Thread.currentThread().getStackTrace
error(m, findFirstUserLine(stack) getOrElse stack(0))
}

/** emit a warning message */
def warning(m: String) {
val stack = Thread.currentThread().getStackTrace
ChiselErrors += new ChiselError(() => m,
findFirstUserLine(Thread.currentThread().getStackTrace), 1)
findFirstUserLine(stack) getOrElse stack(0), 1)
}

def findFirstUserLine(stack: Array[StackTraceElement]): StackTraceElement = {
val index = findFirstUserInd(stack)
if( index > 0 ) stack(index) else stack(0)
}
def findFirstUserLine(stack: Array[StackTraceElement]): Option[StackTraceElement] =
findFirstUserInd(stack) map { stack(_) }

def findFirstUserInd(stack: Array[StackTraceElement]): Int = {
/* Starts at one because item 0 is java.lang.Thread.getStackTrace */
for(i <- 1 until stack.length) {
val ste = stack(i)
def findFirstUserInd(stack: Array[StackTraceElement]): Option[Int] = {
def isUserCode(ste: StackTraceElement): Boolean = {
val className = ste.getClassName()
try {
val cls = Class.forName(className)
val supercls = cls.getSuperclass()
if( supercls == classOf[Component] ) {
return i
if( cls.getSuperclass() == classOf[Component] ) true
else {
/* XXX Do it the old way until we figure if it is safe
to remove from Node.scala
var line: StackTraceElement = findFirstUserLine(Thread.currentThread().getStackTrace)
*/
val dotPos = className.lastIndexOf('.')
if( dotPos > 0 )
(className.subSequence(0, dotPos) != "Chisel") && !className.contains("scala")
else false
}
} catch {
case e: java.lang.ClassNotFoundException => {}
case e: java.lang.ClassNotFoundException => false
}
}
/* XXX Do it the old way until we figure if it is safe
to remove from Node.scala
var line: StackTraceElement = findFirstUserLine(Thread.currentThread().getStackTrace)
*/
for(i <- 1 until stack.length) {
val ste = stack(i)
val classname = ste.getClassName
val dotPos = classname.lastIndexOf('.')
if( dotPos > 0 ) {
val pkg = classname.subSequence(0, dotPos)
if (pkg != "Chisel" && !classname.contains("scala")) {
return i
}
}
val idx = stack.indexWhere(isUserCode)
if(idx < 0) {
println("COULDN'T FIND LINE NUMBER (" + stack(1) + ")")
None
}
println("COULDN'T FIND LINE NUMBER (" + stack(1) + ")")
0
else Some(idx)
}

/** Prints error messages generated by Chisel at runtime. */
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/Node.scala
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ abstract class Node extends nameable {
var inferCount = 0;
var genError = false;
var stack: Array[StackTraceElement] = null;
var line: StackTraceElement = findFirstUserLine(Thread.currentThread().getStackTrace)
var line: StackTraceElement = findFirstUserLine(Thread.currentThread().getStackTrace) getOrElse Thread.currentThread().getStackTrace()(0)
var isScanArg = false
var isPrintArg = false
def isMemOutput = false
Expand Down
5 changes: 1 addition & 4 deletions src/main/scala/hcl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,7 @@ object chiselMain {
object throwException {
def apply(s: String) = {
val xcpt = new Exception(s)
val st = xcpt.getStackTrace
val usrStart = findFirstUserInd(st)
val usrEnd = if(usrStart == 0) st.length else usrStart + 1
xcpt.setStackTrace(st.slice(usrStart, usrEnd))
findFirstUserLine(xcpt.getStackTrace) map { u => xcpt.setStackTrace(Array(u)) }
throw xcpt
}
}
Expand Down

0 comments on commit 2c93b2d

Please sign in to comment.