annotate util/serve_arm_as.py @ 405:f381cea07fec

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