#!/usr/bin/ruby # takes output of avr-objdump and turns it into real assembly code. # Ned Konz # Sat Apr 1 08:55:32 PST 2006 # :beginning, :ccode, :asmcomment, :code $state = :beginning def outputLine(line) case line when /^;;\s/ if $state == :beginning line.sub!(/^;*\s*(.+)/, ";;\t/* \\1 */") else $state = :ccode end when /^;/ $state = :asmcomment when /^\s*$/ if $state == :ccode || $state == :beginning line = ";;" end else $state = :code end line.sub!(/\t;\s*([^;]*)\s*;\s*(.*)/, "\t; \\1 (\\2)") if $state != :beginning line.sub!(/;\s*([a-f0-9]{8})\s*$/, "; (0x\\1)") line.sub!(/;\s*(0x[a-f0-9]{2,8})\s*$/, "; (\\1)") puts line end ARGF.each do | f | f.each_line do | line | line.sub!(/(0x[a-z0-9]{2,4})\s+<([^>]+)>/, "\\2 ; \\1") line.sub!(/\tcall\t0x.... /, "\tcall ") line.sub!(/\tjmp\t0x.... /, "\tjmp ") line.sub!(/; 0x[0-9a-f]+ /, "; ") line.sub!(/^<([^>]+)>:/, "\\1 :") case line # asm directive when /^\s+(\.\w+|vector|XJMP)(\s+.*)?/ outputLine line # asm label when /^\s*(\w+)\s*:$/ outputLine "#{$1} :" # line number (-l option to avr-objdump) when /^(\/.*):([0-9]+)$/ puts "#line #{$2} \"#{$1}\"" $state = :code # label when /^([0-9a-fA-F]{8}) <([^>]+)>/ outputLine "#{$2}: ; #{$1}" # instruction when /^\s+[0-9a-fA-F]{1,4}:\t([0-9a-fA-F]{2}\s){2,} *\t/ outputLine line[22 ... -1] # instruction, no opcodes when /^\s+[0-9a-fA-F]{1,4}:\t*\t/ outputLine line[5 ... -1] # table when /^\s+[0-9a-fA-F]{1,4}:\t([0-9a-fA-F]{2}\s)[^\t]+$/ outputLine line[10 ... -1] # blank line when /^\s*$/ outputLine "" # C code else outputLine ";;\t" + line end end end