diff util/serve_arm_as.py @ 347:742588fb8cd6 devel

Merge into devel branch
author Windel Bouwman
date Fri, 07 Mar 2014 17:10:21 +0100
parents 3bb7dcfe5529
children
line wrap: on
line diff
--- a/util/serve_arm_as.py	Sat Mar 01 16:27:52 2014 +0100
+++ b/util/serve_arm_as.py	Fri Mar 07 17:10:21 2014 +0100
@@ -4,23 +4,38 @@
 """
 
 import subprocess
-import tornado.web
-import tornado.ioloop
+try:
+    import tornado.web
+    import tornado.ioloop
+    from tornado.web import RequestHandler
+except Exception as e:
+    print(e)
+    RequestHandler = object
+
 import io
 
+prefix = 'arm-none-eabi-'
+AS = prefix + 'as'
+objdump = prefix + 'objdump'
+
 def mangle(inp, outstream):
     print('assembling...', file=outstream)
-    p_as = subprocess.Popen(['arm-elf-as'], stdin=subprocess.PIPE)
+    p_as = subprocess.Popen([AS], stdin=subprocess.PIPE)
     p_as.communicate(input=inp.encode('ascii'))
+    if p_as.returncode != 0:
+        raise Exception('{}'.format(p_as.returncode))
 
-    p_objdump = subprocess.Popen(['arm-elf-objdump', '-d'], stdout=subprocess.PIPE)
+    p_objdump = subprocess.Popen([objdump, '-d'], stdout=subprocess.PIPE)
     output = p_objdump.communicate()[0].decode('ascii')
+    if p_objdump.returncode != 0:
+        raise Exception('{}'.format(p_objdump.returncode))
     print(output, file=outstream)
 
-    p_objdump = subprocess.Popen(['arm-elf-objdump', '-s'], stdout=subprocess.PIPE)
+    p_objdump = subprocess.Popen([objdump, '-s', '-j', '.text'], stdout=subprocess.PIPE)
     output = p_objdump.communicate()[0].decode('ascii')
+    if p_objdump.returncode != 0:
+        raise Exception('{}'.format(p_objdump.returncode))
     print(output, file=outstream)
-    print('Done!', file=outstream)
 
 page = """
 <html>
@@ -44,10 +59,11 @@
 </html>
 """
 
-class MainHandler(tornado.web.RequestHandler):
+class MainHandler(RequestHandler):
     def get(self):
         src="sub sp,sp,#8"
         self.write(page.replace('%result%', '').replace('%src%', src))
+
     def post(self):
         #print(self.request.body)
         src = self.get_argument('source')
@@ -58,9 +74,45 @@
         self.write(page.replace('%result%', txt).replace('%src%', src))
 
 
-application = tornado.web.Application([(r"/", MainHandler)])
-
-if __name__ == '__main__':
+def serve_tornado():
+    application = tornado.web.Application([(r"/", MainHandler)])
     application.listen(8888)
     tornado.ioloop.IOLoop.instance().start()
 
+def extract_bytes(txt):
+    lines = list(filter(None, (line.strip() for line in txt.split('\n'))))
+    idx = lines.index('Contents of section .text:')
+
+    byte_txt = ''
+    for line in lines[idx+1:]:
+        parts = list(filter(None, line.split(' ')))
+        byte_txt += ' '.join(parts[1:-1]) + ' '
+    byte_txt = byte_txt.strip()
+    return byte_txt
+
+def run_on_file():
+    with open('test_patterns.txt', 'r') as f:
+        data = f.read()
+
+    outfile = open('tests.pygen', 'w')
+    snippets = data.split('===')
+    for snippet in snippets:
+        make_test(snippet, outfile)
+    outfile.close()
+
+def make_test(src, output_file):
+    out = io.StringIO()
+    try:
+        mangle(src, out)
+    except Exception as e:
+        return
+    txt = out.getvalue()
+    byte_txt = extract_bytes(txt)
+    print("    def testTODO(self):", file=output_file)
+    for line in filter(None, src.split('\n')):
+        print("        self.feed('{}')".format(line), file=output_file)
+    print("        self.check('{}')".format(byte_txt), file=output_file)
+
+if __name__ == '__main__':
+    #serve_tornado()
+    run_on_file()