-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpatmos-hex2bin
executable file
·98 lines (80 loc) · 3.1 KB
/
patmos-hex2bin
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/python
from __future__ import print_function
import argparse
import sys
import os.path
import re
from binascii import unhexlify
"""
This script reads the hexadecimal instruction encoding in Patmos .s files
generated by patmos-clang or llc -show-mc-encoding, and adds them in
binary encoding to the comments.
Author: Stefan Hepp <[email protected]>
"""
parser = argparse.ArgumentParser(description='Reformat assembler encoding comments.',
usage='%(prog)s [-b|-f] [-o <outfile.s>|-i] <asmfile>')
parser.add_argument('asmfile', help='assembler file containing instruction encoding comments')
parser.add_argument('-b', '--binary', action='store_true', help='Produce binary file containing all instructions (use with -o)')
parser.add_argument('-f', '--bytes', action='store_true', help='Group binary representation per byte, not per field')
parser.add_argument('-i', '--inplace', action='store_true', help='Perform replace in place on the input file.')
parser.add_argument('-o', '--output', help='Write output to file instead of stdout')
args = parser.parse_args()
if args.inplace and args.output:
print("Error: -i not allowed with -o")
sys.exit(1)
if args.binary:
if args.inplace or args.asmfile == args.output:
print("You probably do not want to replace your .s file with the binary, cowardly backing out.")
sys.exit(2)
if args.output is None:
print("Error: -b must be used with -o")
sys.exit(1)
if not os.path.isfile(args.asmfile):
print("Input " + args.asmfile + " not found or not a file.")
sys.exit(1)
try:
infile = open(args.asmfile, 'r')
lines = infile.readlines()
infile.close()
except IOError as e:
print("Cannot read input file " + args.asmfile + ": " + e)
sys.exit(1)
outfile = None
if args.inplace or args.output:
try:
outfile = open(args.asmfile if args.inplace else args.output, 'w')
except IOError as e:
print("Cannot open output file for writing: " + e)
sys.exit(2)
pat = re.compile("# encoding: \[( ?([0-9a-fx,A]*) *(\| [01x ]*)?)\]")
for line in lines:
m = pat.search(line)
if m:
hex = m.group(2)
bny = ""
pad = ""
for val in hex.split(','):
if val == "A":
bny += "00" if args.binary else "xxxxxxxx"
pad += " "
continue
if args.binary:
bny += val[2:4]
else:
bny += bin(int(val[2:4], 16))[2:].zfill(8)
bny += "" if args.bytes else " "
if args.binary:
line = unhexlify(bin)
else:
if args.bytes:
bny = (bny[0:1] + " " + bny[1:5] + " " + bny[5:10] + " " +
bny[10:15] + " " + bny[15:20] + " " + bny[20:25] + " " +
bny[25:32] + " " + bny[32:] +
(" " if len(bny) >= 32 else ""))
line = line.replace(m.group(1), " %s %s| %s" % (hex, pad, bny))
if outfile:
outfile.write(line)
else:
print(line, end='')
if outfile:
outfile.close()