18c2ecf20Sopenharmony_ci#!/bin/awk -f
28c2ecf20Sopenharmony_ci# SPDX-License-Identifier: GPL-2.0
38c2ecf20Sopenharmony_ci# Usage: objdump -d a.out | awk -f objdump_reformat.awk | ./insn_decoder_test
48c2ecf20Sopenharmony_ci# Reformats the disassembly as follows:
58c2ecf20Sopenharmony_ci# - Removes all lines except the disassembled instructions.
68c2ecf20Sopenharmony_ci# - For instructions that exceed 1 line (7 bytes), crams all the hex bytes
78c2ecf20Sopenharmony_ci# into a single line.
88c2ecf20Sopenharmony_ci# - Remove bad(or prefix only) instructions
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ciBEGIN {
118c2ecf20Sopenharmony_ci	prev_addr = ""
128c2ecf20Sopenharmony_ci	prev_hex = ""
138c2ecf20Sopenharmony_ci	prev_mnemonic = ""
148c2ecf20Sopenharmony_ci	bad_expr = "(\\(bad\\)|^rex|^.byte|^rep(z|nz)$|^lock$|^es$|^cs$|^ss$|^ds$|^fs$|^gs$|^data(16|32)$|^addr(16|32|64))"
158c2ecf20Sopenharmony_ci	fwait_expr = "^9b "
168c2ecf20Sopenharmony_ci	fwait_str="9b\tfwait"
178c2ecf20Sopenharmony_ci}
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci/^ *[0-9a-f]+ <[^>]*>:/ {
208c2ecf20Sopenharmony_ci	# Symbol entry
218c2ecf20Sopenharmony_ci	printf("%s%s\n", $2, $1)
228c2ecf20Sopenharmony_ci}
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci/^ *[0-9a-f]+:/ {
258c2ecf20Sopenharmony_ci	if (split($0, field, "\t") < 3) {
268c2ecf20Sopenharmony_ci		# This is a continuation of the same insn.
278c2ecf20Sopenharmony_ci		prev_hex = prev_hex field[2]
288c2ecf20Sopenharmony_ci	} else {
298c2ecf20Sopenharmony_ci		# Skip bad instructions
308c2ecf20Sopenharmony_ci		if (match(prev_mnemonic, bad_expr))
318c2ecf20Sopenharmony_ci			prev_addr = ""
328c2ecf20Sopenharmony_ci		# Split fwait from other f* instructions
338c2ecf20Sopenharmony_ci		if (match(prev_hex, fwait_expr) && prev_mnemonic != "fwait") {
348c2ecf20Sopenharmony_ci			printf "%s\t%s\n", prev_addr, fwait_str
358c2ecf20Sopenharmony_ci			sub(fwait_expr, "", prev_hex)
368c2ecf20Sopenharmony_ci		}
378c2ecf20Sopenharmony_ci		if (prev_addr != "")
388c2ecf20Sopenharmony_ci			printf "%s\t%s\t%s\n", prev_addr, prev_hex, prev_mnemonic
398c2ecf20Sopenharmony_ci		prev_addr = field[1]
408c2ecf20Sopenharmony_ci		prev_hex = field[2]
418c2ecf20Sopenharmony_ci		prev_mnemonic = field[3]
428c2ecf20Sopenharmony_ci	}
438c2ecf20Sopenharmony_ci}
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ciEND {
468c2ecf20Sopenharmony_ci	if (prev_addr != "")
478c2ecf20Sopenharmony_ci		printf "%s\t%s\t%s\n", prev_addr, prev_hex, prev_mnemonic
488c2ecf20Sopenharmony_ci}
49