3 # Feed this script the output of objdump -M raw --no-show-raw-insn ppc-prog
5 # It will look for insns that can be represented in compressed mode,
6 # according to the encoding rules in the copcond dictionary below.
7 # It's just a skeleton for testing of the logic,
8 # the encoding rules are yet to be filled in.
10 # Nothing is assumed as to the actual bit-encoding of the insns, this
11 # is just to experiment with insn selection and get a quick feedback
12 # loop for the encoding options in compressed mode.
14 # In this script, the computations of encoding modes and transitions
15 # are tuned for the simpler model that uses 1-byte nops for
16 # transitions in and out of compressed mode, placing compressed-mode
17 # insns at odd addresses. At (visible) entry points, mode is forced
18 # to return to uncompressed mode.
20 # The entire code stream is printed, without any attempt to modify the
21 # addresses that go along with or in them; we only insert markers for
22 # the transition points, and for the compressed instructions.
24 # The really useful information is printed at the end: a summary of
25 # transition and compressed-insn counts, and the achieved compression
31 insn
= re
.compile('\s+(?P<addr>[0-9a-f]+):\s+(?P<opcode>[^ ]+) *(?P<operands>.*)')
33 opkind
= re
.compile('(?P<reg>(?P<regkind>[cf]?r)(?P<regnum>[0-9]+))|(?P<immediate>-?[0-9]+)|(?P<branch>[0-9a-f]+)(?: <.*>)?|(?P<offset>-?[0-9]+)\((?P<basereg>r[0-9]+)\)')
36 match
= opkind
.fullmatch(op
)
40 elif match
['reg'] is not None:
41 op
= (match
['regkind'], int(match
['regnum']))
42 elif match
['immediate'] is not None:
43 op
= ('imm', int (op
).bit_length ())
44 elif match
['branch'] is not None:
45 op
= ('pcoff', (int (match
['branch'], 16)
46 - int (addr
, 16)).bit_length ())
47 elif match
['offset'] is not None:
48 op
= ('ofst', mapop(match
['offset']), mapop(match
['basereg']))
50 raise "unrecognized operand kind"
57 if mop
[0] in { 'r', 'fr', 'cr' }:
60 raise "operand is not a register"
66 raise "operand is not an immediate"
68 # Following are predicates to be used in copcond, to tell the mode in
69 # which opcode with ops as operands is to be represented
71 # Any occurrence of the opcode can be compressed.
72 def anyops(opcode
, ops
):
75 # Compress iff first and second operands are the same.
76 def same01(opcode
, ops
):
82 # Registers representable in a made-up 3-bit mapping.
83 cregs2
= { 1, 2, 3, 4, 5, 6, 7, 31 }
85 # Return true iff mop is a regular register present in cregs2
87 return opclass(mop
) is 'r' and regno(mop
) in cregs2
89 # Return true iff mop is an immediate of at most 8 bits.
91 return opclass(mop
) is 'imm' and immbits(mop
) <= 8
93 # Compress binary opcodes iff the first two operands (output and first
94 # input operand) are registers representable in 3 bits in compressed
95 # mode, and the immediate operand can be represented in 8 bits.
96 def bin2regs3imm8(opcode
, ops
):
97 if bin2regs3(ops
[0]) and bin2regs3(ops
[1]) and imm8(ops
[2]):
102 # Map opcodes that might be compressed to a function that returns the
103 # mode (index into mode_list below) in which the insn is to be
104 # represented. Those not mentioned in copcond are assumed
107 # Pretending anything goes, just for demonstration purposes.
111 # Output and first input operand must coincide for these.
114 # Limiting register and operand range:
115 'addi': bin2regs3imm8
116 # Anything else is uncompressed.
122 count_uncompressed
= 0
124 mode_list
= ['Uncompressed', 'Compressed'] # for documentation purposes only
126 for line
in sys
.stdin
:
130 match
= insn
.fullmatch(line
)
133 # Switch to uncompressed mode at function boundaries
134 if current_mode
is not 0:
135 print('<leave compressed mode>')
137 leave_compressed
+= 1
141 opcode
= match
['opcode']
142 operands
= match
['operands']
144 if opcode
in copcond
:
145 this_mode
= copcond
[opcode
](opcode
,
146 [mapop(op
) for op
in operands
.split(',')])
151 if current_mode
is not 1:
154 enter_compressed
+= 1
156 count_compressed
+= 1
158 if current_mode
is not 0:
159 print('\t\tcout.nop')
161 leave_compressed
+= 1
163 count_uncompressed
+= 1
165 transition_bytes
= 1 * enter_compressed
+ 1 * leave_compressed
166 compressed_bytes
= 2 * count_compressed
167 uncompressed_bytes
= 4 * count_uncompressed
168 total_bytes
= transition_bytes
+ compressed_bytes
+ uncompressed_bytes
169 original_bytes
= 2 * compressed_bytes
+ uncompressed_bytes
173 print('Compressed instructions: %i' % count_compressed
)
174 print('Uncompressed instructions: %i' % count_uncompressed
)
175 print('Transitions into compressed mode: %i' % enter_compressed
)
176 print('Transitions out of compressed mode: %i' % leave_compressed
)
177 print('Compressed size estimate: %i' % total_bytes
)
178 print('Original size: %i' % original_bytes
)
179 print('Compressed/original ratio: %f' % (total_bytes
/ original_bytes
))