aboutsummaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorAlon Zakai <azakai@mozilla.com>2011-03-02 19:12:13 -0800
committerAlon Zakai <azakai@mozilla.com>2011-03-02 19:12:13 -0800
commitc3408af2696ab4c0649495b63ec912f55156890b (patch)
tree5ff7d4a9faf843069fc6f2ec8aa4823197afcc8f /tools
parent841d890872d0e4ff480d81dad909b31772c0f98d (diff)
autodebugger tool
Diffstat (limited to 'tools')
-rw-r--r--tools/autodebugger.py49
1 files changed, 49 insertions, 0 deletions
diff --git a/tools/autodebugger.py b/tools/autodebugger.py
new file mode 100644
index 00000000..b90501cc
--- /dev/null
+++ b/tools/autodebugger.py
@@ -0,0 +1,49 @@
+'''
+Processes an LLVM assembly (.ll) file, adding debugging information.
+
+You can then run the .ll file in the LLVM interpreter (lli) and
+compare that to the output when compiled using emscripten.
+'''
+
+import os, sys, re
+
+POSTAMBLE = '''
+@.emscripten.autodebug.str = private constant [9 x i8] c"%d : %d\\0A\\00", align 1 ; [#uses=1]
+
+; [#uses=1]
+define void @emscripten_autodebug(i32 %line, i32 %value) {
+entry:
+ %0 = call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([9 x i8]* @.emscripten.autodebug.str, i32 0, i32 0), i32 %line, i32 %value) ; [#uses=0]
+ br label %return
+
+return: ; preds = %entry
+ ret void
+}
+'''
+
+filename, ofilename = sys.argv[1], sys.argv[2]
+f = open(filename, 'r')
+data = f.read()
+f.close()
+
+if 'declare i32 @printf(' not in data:
+ POSTAMBLE += '''
+; [#uses=1]
+declare i32 @printf(i8*, ...)
+'''
+
+lines = data.split('\n')
+for i in range(len(lines)):
+ #if i == 5:
+ # lines[i] += '\n
+
+ m = re.match(' store (?P<type>i64|i32|i16|i8) %(?P<var>[\w.]+), .*', lines[i])
+ if m and m.group('type') == 'i32': # TODO: Other types
+ lines[i] += '\n call void @emscripten_autodebug(i32 %d, i32 %%%s)' % (i+1, m.group('var'))
+
+f = open(ofilename, 'w')
+f.write('\n'.join(lines) + '\n' + POSTAMBLE + '\n')
+f.close()
+
+print 'Success.'
+