275
|
1
|
|
2 """
|
|
3 Tornado webserver interface to arm-elf-as.
|
|
4 """
|
263
|
5
|
|
6 import subprocess
|
346
|
7 try:
|
|
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
|
275
|
15 import io
|
263
|
16
|
346
|
17 prefix = 'arm-none-eabi-'
|
|
18 AS = prefix + 'as'
|
|
19 objdump = prefix + 'objdump'
|
|
20
|
275
|
21 def mangle(inp, outstream):
|
|
22 print('assembling...', file=outstream)
|
346
|
23 p_as = subprocess.Popen([AS], stdin=subprocess.PIPE)
|
263
|
24 p_as.communicate(input=inp.encode('ascii'))
|
346
|
25 if p_as.returncode != 0:
|
|
26 raise Exception('{}'.format(p_as.returncode))
|
263
|
27
|
346
|
28 p_objdump = subprocess.Popen([objdump, '-d'], stdout=subprocess.PIPE)
|
263
|
29 output = p_objdump.communicate()[0].decode('ascii')
|
346
|
30 if p_objdump.returncode != 0:
|
|
31 raise Exception('{}'.format(p_objdump.returncode))
|
275
|
32 print(output, file=outstream)
|
263
|
33
|
346
|
34 p_objdump = subprocess.Popen([objdump, '-s', '-j', '.text'], stdout=subprocess.PIPE)
|
263
|
35 output = p_objdump.communicate()[0].decode('ascii')
|
346
|
36 if p_objdump.returncode != 0:
|
|
37 raise Exception('{}'.format(p_objdump.returncode))
|
275
|
38 print(output, file=outstream)
|
|
39
|
|
40 page = """
|
|
41 <html>
|
|
42 <head>
|
|
43 </head>
|
|
44 <body>
|
|
45 <h1>
|
|
46
|
|
47 </h1>
|
|
48 <form action="" method="post">
|
|
49 <textarea name="source" rows="25" cols="80">
|
|
50 %src%
|
|
51 </textarea>
|
|
52 <br>
|
|
53 <input type="submit" value="Assemble!">
|
|
54 </form>
|
|
55 <div>
|
|
56 %result%
|
|
57 </div>
|
|
58 </body>
|
|
59 </html>
|
|
60 """
|
263
|
61
|
346
|
62 class MainHandler(RequestHandler):
|
263
|
63 def get(self):
|
275
|
64 src="sub sp,sp,#8"
|
|
65 self.write(page.replace('%result%', '').replace('%src%', src))
|
346
|
66
|
275
|
67 def post(self):
|
|
68 #print(self.request.body)
|
|
69 src = self.get_argument('source')
|
|
70 out = io.StringIO()
|
|
71 mangle(src, out)
|
|
72 txt = out.getvalue()
|
|
73 txt = txt.replace('\n', '<br>')
|
|
74 self.write(page.replace('%result%', txt).replace('%src%', src))
|
|
75
|
|
76
|
346
|
77 def serve_tornado():
|
|
78 application = tornado.web.Application([(r"/", MainHandler)])
|
275
|
79 application.listen(8888)
|
|
80 tornado.ioloop.IOLoop.instance().start()
|
263
|
81
|
346
|
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()
|