comparison 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
comparison
equal deleted inserted replaced
343:11c5a8a70c02 347:742588fb8cd6
2 """ 2 """
3 Tornado webserver interface to arm-elf-as. 3 Tornado webserver interface to arm-elf-as.
4 """ 4 """
5 5
6 import subprocess 6 import subprocess
7 import tornado.web 7 try:
8 import tornado.ioloop 8 import tornado.web
9 import tornado.ioloop
10 from tornado.web import RequestHandler
11 except Exception as e:
12 print(e)
13 RequestHandler = object
14
9 import io 15 import io
16
17 prefix = 'arm-none-eabi-'
18 AS = prefix + 'as'
19 objdump = prefix + 'objdump'
10 20
11 def mangle(inp, outstream): 21 def mangle(inp, outstream):
12 print('assembling...', file=outstream) 22 print('assembling...', file=outstream)
13 p_as = subprocess.Popen(['arm-elf-as'], stdin=subprocess.PIPE) 23 p_as = subprocess.Popen([AS], stdin=subprocess.PIPE)
14 p_as.communicate(input=inp.encode('ascii')) 24 p_as.communicate(input=inp.encode('ascii'))
25 if p_as.returncode != 0:
26 raise Exception('{}'.format(p_as.returncode))
15 27
16 p_objdump = subprocess.Popen(['arm-elf-objdump', '-d'], stdout=subprocess.PIPE) 28 p_objdump = subprocess.Popen([objdump, '-d'], stdout=subprocess.PIPE)
17 output = p_objdump.communicate()[0].decode('ascii') 29 output = p_objdump.communicate()[0].decode('ascii')
30 if p_objdump.returncode != 0:
31 raise Exception('{}'.format(p_objdump.returncode))
18 print(output, file=outstream) 32 print(output, file=outstream)
19 33
20 p_objdump = subprocess.Popen(['arm-elf-objdump', '-s'], stdout=subprocess.PIPE) 34 p_objdump = subprocess.Popen([objdump, '-s', '-j', '.text'], stdout=subprocess.PIPE)
21 output = p_objdump.communicate()[0].decode('ascii') 35 output = p_objdump.communicate()[0].decode('ascii')
36 if p_objdump.returncode != 0:
37 raise Exception('{}'.format(p_objdump.returncode))
22 print(output, file=outstream) 38 print(output, file=outstream)
23 print('Done!', file=outstream)
24 39
25 page = """ 40 page = """
26 <html> 41 <html>
27 <head> 42 <head>
28 </head> 43 </head>
42 </div> 57 </div>
43 </body> 58 </body>
44 </html> 59 </html>
45 """ 60 """
46 61
47 class MainHandler(tornado.web.RequestHandler): 62 class MainHandler(RequestHandler):
48 def get(self): 63 def get(self):
49 src="sub sp,sp,#8" 64 src="sub sp,sp,#8"
50 self.write(page.replace('%result%', '').replace('%src%', src)) 65 self.write(page.replace('%result%', '').replace('%src%', src))
66
51 def post(self): 67 def post(self):
52 #print(self.request.body) 68 #print(self.request.body)
53 src = self.get_argument('source') 69 src = self.get_argument('source')
54 out = io.StringIO() 70 out = io.StringIO()
55 mangle(src, out) 71 mangle(src, out)
56 txt = out.getvalue() 72 txt = out.getvalue()
57 txt = txt.replace('\n', '<br>') 73 txt = txt.replace('\n', '<br>')
58 self.write(page.replace('%result%', txt).replace('%src%', src)) 74 self.write(page.replace('%result%', txt).replace('%src%', src))
59 75
60 76
61 application = tornado.web.Application([(r"/", MainHandler)]) 77 def serve_tornado():
62 78 application = tornado.web.Application([(r"/", MainHandler)])
63 if __name__ == '__main__':
64 application.listen(8888) 79 application.listen(8888)
65 tornado.ioloop.IOLoop.instance().start() 80 tornado.ioloop.IOLoop.instance().start()
66 81
82 def extract_bytes(txt):
83 lines = list(filter(None, (line.strip() for line in txt.split('\n'))))
84 idx = lines.index('Contents of section .text:')
85
86 byte_txt = ''
87 for line in lines[idx+1:]:
88 parts = list(filter(None, line.split(' ')))
89 byte_txt += ' '.join(parts[1:-1]) + ' '
90 byte_txt = byte_txt.strip()
91 return byte_txt
92
93 def run_on_file():
94 with open('test_patterns.txt', 'r') as f:
95 data = f.read()
96
97 outfile = open('tests.pygen', 'w')
98 snippets = data.split('===')
99 for snippet in snippets:
100 make_test(snippet, outfile)
101 outfile.close()
102
103 def make_test(src, output_file):
104 out = io.StringIO()
105 try:
106 mangle(src, out)
107 except Exception as e:
108 return
109 txt = out.getvalue()
110 byte_txt = extract_bytes(txt)
111 print(" def testTODO(self):", file=output_file)
112 for line in filter(None, src.split('\n')):
113 print(" self.feed('{}')".format(line), file=output_file)
114 print(" self.check('{}')".format(byte_txt), file=output_file)
115
116 if __name__ == '__main__':
117 #serve_tornado()
118 run_on_file()