Skip to content

Instantly share code, notes, and snippets.

@ktemkin
Created July 18, 2013 20:41
Show Gist options
  • Save ktemkin/6032893 to your computer and use it in GitHub Desktop.
Save ktemkin/6032893 to your computer and use it in GitHub Desktop.
Quick script to colorize the output of avr-objdump. Install smart_colored first: gem install smart_colored
#!/usr/bin/env ruby
require 'smart_colored/extend'
module Rules
extend self
Rules = []
#
# Quick meta-method which adds a given rule to the internal
# list of rules to be applied.
#
def add(&block)
Rules << block
end
#
# Meta-method which adss a simple substitution rule.
#
def add_substitution_rule(find, replace)
add { |line| line.gsub(find, replace) }
end
#
# Applies each of the defined rules to a given line.
#
def apply_all(line)
Rules.each { |rule| line = rule[line] }
line
end
#
# Helper regular expressions.
#
#Assembly-language comment.
COMMENT = /(;.*)$/
#Section header
SECTION_HEADER = /^([0-9a-fA-F]+)\s+<(.*)>:$/
#Register names
REGISTER_NAME = /(r[0-9]+)/
#Code which is likely C source code.
C_SOURCE = /^([0-9a-f\s]*[^0-9a-f\s:]+.*)$/
#
# List of all AVR opcodes.
#
Mnemonics =
%w{
nop movw muls mulsu fmul fmuls fmulsu cpc sbc add cpse cp sub adc and eor or mov cpi sbci subi
ori andi ld ldd st std lpm elpm pop sts push com neg swap inc asr lsr ror dec bset bclr ijmp
eijmp ret icall reti eicall sleep break wdr lpm elpm spm espm jmp call adiw sbiw cbi sbic sbi sbis
mul in out rjmp rcall ldi brbs brbc bld bst sbrc sbrs brcs brcc cli
}
#
# Rule definitions.
#
#Colorize comments in green.
add_substitution_rule COMMENT, '\1'.green
#Colorize section headers in yellow.
add_substitution_rule SECTION_HEADER, "\\1 <#{'\2'.bold}>:".yellow
#Colorize opcodes in bold and blue.
Mnemonics.each { |mnemonic| add_substitution_rule(/(\s#{mnemonic}\s)/, '\1'.bold.blue) }
#Colorize register names in blue...
add_substitution_rule REGISTER_NAME, '\1'.cyan
#Try to colorize lines of C in red
add_substitution_rule C_SOURCE, '\1'.magenta
end
#Apply each of the rules to the command line
ARGF.each_line { |line| print Rules::apply_all(line) }
#vim: filetype=ruby
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment