fix lsearch and lfind to pass key as first arg to the compar callback
[musl.git] / tools / add-cfi.x86_64.awk
blobbbc90daa3198abe260241e760c97925106684270
1 # Insert GAS CFI directives ("control frame information") into x86-64 asm input
3 BEGIN {
4 # don't put CFI data in the .eh_frame ELF section (which we don't keep)
5 print ".cfi_sections .debug_frame"
7 # only emit CFI directives inside a function
8 in_function = 0
10 # emit .loc directives with line numbers from original source
11 printf ".file 1 \"%s\"\n", ARGV[1]
12 line_number = 0
14 # used to detect "call label; label:" trick
15 called = ""
18 function get_const1() {
19 # for instructions with 2 operands, get 1st operand (assuming it is constant)
20 match($0, /-?(0x[0-9a-fA-F]+|[0-9]+),/)
21 return parse_const(substr($0, RSTART, RLENGTH-1))
24 function canonicalize_reg(register) {
25 if (match(register, /^r/))
26 return register
27 else if (match(register, /^e/))
28 return "r" substr(register, 2, length(register)-1)
29 else if (match(register, /[hl]$/)) # AH, AL, BH, BL, etc
30 return "r" substr(register, 1, 1) "x"
31 else # AX, BX, CX, etc
32 return "r" register
34 function get_reg() {
35 # only use if you already know there is 1 and only 1 register
36 match($0, /%[er]?([abcd][xlh]|si|di|bp|8|9|10|11|12|13|14|15)/)
37 return canonicalize_reg(substr($0, RSTART+1, RLENGTH-1))
39 function get_reg1() {
40 # for instructions with 2 operands, get 1st operand (assuming it is register)
41 match($0, /%[er]?([abcd][xlh]|si|di|bp|8|9|10|11|12|13|14|15),/)
42 return canonicalize_reg(substr($0, RSTART+1, RLENGTH-2))
44 function get_reg2() {
45 # for instructions with 2 operands, get 2nd operand (assuming it is register)
46 match($0, /,%[er]?([abcd][xlh]|si|di|bp|8|9|10|11|12|13|14|15)/)
47 return canonicalize_reg(substr($0, RSTART+2, RLENGTH-2))
50 function adjust_sp_offset(delta) {
51 if (in_function)
52 printf ".cfi_adjust_cfa_offset %d\n", delta
56 line_number = line_number + 1
58 # clean the input up before doing anything else
59 # delete comments
60 gsub(/(#|\/\/).*/, "")
62 # canonicalize whitespace
63 gsub(/[ \t]+/, " ") # mawk doesn't understand \s
64 gsub(/ *, */, ",")
65 gsub(/ *: */, ": ")
66 gsub(/ $/, "")
67 gsub(/^ /, "")
70 # check for assembler directives which we care about
71 /^\.(section|data|text)/ {
72 # a .cfi_startproc/.cfi_endproc pair should be within the same section
73 # otherwise, clang will choke when generating ELF output
74 if (in_function) {
75 print ".cfi_endproc"
76 in_function = 0
79 /^\.type [a-zA-Z0-9_]+,\@function/ {
80 functions[substr($2, 1, length($2)-10)] = 1
82 # not interested in assembler directives beyond this, just pass them through
83 /^\./ {
84 print
85 next
88 /^[a-zA-Z0-9_]+:/ {
89 label = substr($1, 1, length($1)-1) # drop trailing :
91 if (called == label) {
92 # note adjustment of stack pointer from "call label; label:"
93 adjust_sp_offset(8)
96 if (functions[label]) {
97 if (in_function)
98 print ".cfi_endproc"
100 in_function = 1
101 print ".cfi_startproc"
103 for (register in saved)
104 delete saved[register]
105 for (register in dirty)
106 delete dirty[register]
109 # an instruction may follow on the same line, so continue processing
112 /^$/ { next }
115 called = ""
116 printf ".loc 1 %d\n", line_number
117 print
120 # KEEPING UP WITH THE STACK POINTER
121 # %rsp should only be adjusted by pushing/popping or adding/subtracting constants
123 /pushl?/ {
124 adjust_sp_offset(8)
126 /popl?/ {
127 adjust_sp_offset(-8)
129 /addl? \$-?(0x[0-9a-fA-F]+|[0-9]+),%rsp/ { adjust_sp_offset(-get_const1()) }
130 /subl? \$-?(0x[0-9a-fA-F]+|[0-9]+),%rsp/ { adjust_sp_offset(get_const1()) }
132 /call/ {
133 if (match($0, /call [0-9]+f/)) # "forward" label
134 called = substr($0, RSTART+5, RLENGTH-6)
135 else if (match($0, /call [0-9a-zA-Z_]+/))
136 called = substr($0, RSTART+5, RLENGTH-5)
139 # TRACKING REGISTER VALUES FROM THE PREVIOUS STACK FRAME
141 /pushl? %r(ax|bx|cx|dx|si|di|bp|8|9|10|11|12|13|14|15)/ { # don't match "push (%reg)"
142 # if a register is being pushed, and its value has not changed since the
143 # beginning of this function, the pushed value can be used when printing
144 # local variables at the next level up the stack
145 # emit '.cfi_rel_offset' for that
147 if (in_function) {
148 register = get_reg()
149 if (!saved[register] && !dirty[register]) {
150 printf ".cfi_rel_offset %s,0\n", register
151 saved[register] = 1
156 /movl? %r(ax|bx|cx|dx|si|di|bp|8|9|10|11|12|13|14|15),-?(0x[0-9a-fA-F]+|[0-9]+)?\(%rsp\)/ {
157 if (in_function) {
158 register = get_reg()
159 if (match($0, /-?(0x[0-9a-fA-F]+|[0-9]+)\(%rsp\)/)) {
160 offset = parse_const(substr($0, RSTART, RLENGTH-6))
161 } else {
162 offset = 0
164 if (!saved[register] && !dirty[register]) {
165 printf ".cfi_rel_offset %s,%d\n", register, offset
166 saved[register] = 1
171 # IF REGISTER VALUES ARE UNCEREMONIOUSLY TRASHED
172 # ...then we want to know about it.
174 function trashed(register) {
175 if (in_function && !saved[register] && !dirty[register]) {
176 printf ".cfi_undefined %s\n", register
178 dirty[register] = 1
180 # this does NOT exhaustively check for all possible instructions which could
181 # overwrite a register value inherited from the caller (just the common ones)
182 /mov.*,%[er]?([abcd][xlh]|si|di|bp|8|9|10|11|12|13|14|15)$/ { trashed(get_reg2()) }
183 /(add|addl|sub|subl|and|or|xor|lea|sal|sar|shl|shr).*,%[er]?([abcd][xlh]|si|di|bp|8|9|10|11|12|13|14|15)$/ {
184 trashed(get_reg2())
186 /^i?mul [^,]*$/ { trashed("rax"); trashed("rdx") }
187 /^i?mul.*,%[er]?([abcd][xlh]|si|di|bp|8|9|10|11|12|13|14|15)$/ { trashed(get_reg2()) }
188 /^i?div/ { trashed("rax"); trashed("rdx") }
190 /(dec|inc|not|neg|pop) %[er]?([abcd][xlh]|si|di|bp|8|9|10|11|12|13|14|15)/ { trashed(get_reg()) }
191 /cpuid/ { trashed("rax"); trashed("rbx"); trashed("rcx"); trashed("rdx") }
193 END {
194 if (in_function)
195 print ".cfi_endproc"