| 1 |
/* Subroutines for log output for AVR 8-bit microcontrollers. |
| 2 |
Copyright (C) 2011-2025 Free Software Foundation, Inc. |
| 3 |
Contributed by Georg-Johann Lay ([email protected]) |
| 4 |
|
| 5 |
This file is part of GCC. |
| 6 |
|
| 7 |
GCC is free software; you can redistribute it and/or modify |
| 8 |
it under the terms of the GNU General Public License as published by |
| 9 |
the Free Software Foundation; either version 3, or (at your option) |
| 10 |
any later version. |
| 11 |
|
| 12 |
GCC is distributed in the hope that it will be useful, |
| 13 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
GNU General Public License for more details. |
| 16 |
|
| 17 |
You should have received a copy of the GNU General Public License |
| 18 |
along with GCC; see the file COPYING3. If not see |
| 19 |
<http://www.gnu.org/licenses/>. */ |
| 20 |
|
| 21 |
#define IN_TARGET_CODE 1 |
| 22 |
|
| 23 |
#include "config.h" |
| 24 |
#include "system.h" |
| 25 |
#include "coretypes.h" |
| 26 |
#include "tm.h" |
| 27 |
#include "function.h" |
| 28 |
#include "rtl.h" |
| 29 |
#include "tree.h" |
| 30 |
#include "tree-pass.h" /* for current_pass */ |
| 31 |
#include "memmodel.h" |
| 32 |
#include "tm_p.h" |
| 33 |
#include "print-tree.h" |
| 34 |
|
| 35 |
/* This file supplies some functions for AVR back-end developers |
| 36 |
with a printf-like interface. The functions are called through |
| 37 |
macros `avr_dump', `avr_edump' or `avr_fdump' from avr-protos.h: |
| 38 |
|
| 39 |
avr_fdump (FILE *stream, const char *fmt, ...); |
| 40 |
avr_edump (fmt, ...) is a shortcut for avr_fdump (stderr, fmt, ...) |
| 41 |
avr_dump (fmt, ...) is a shortcut for avr_fdump (dump_file, fmt, ...) |
| 42 |
|
| 43 |
== known %-codes == |
| 44 |
|
| 45 |
b: bool |
| 46 |
r: rtx |
| 47 |
t: tree |
| 48 |
T: tree (brief) |
| 49 |
C: enum rtx_code |
| 50 |
m: machine_mode |
| 51 |
R: enum reg_class |
| 52 |
L: insn list |
| 53 |
H: location_t |
| 54 |
|
| 55 |
== no arguments == |
| 56 |
|
| 57 |
A: call abort() |
| 58 |
f: current_function_name() |
| 59 |
F: caller (via __FUNCTION__) |
| 60 |
P: Pass name and number |
| 61 |
?: Print caller, current function and pass info |
| 62 |
!: Ditto, but only print if in a pass with static pass number, |
| 63 |
else return. |
| 64 |
|
| 65 |
== same as printf == |
| 66 |
|
| 67 |
%: % |
| 68 |
c: char |
| 69 |
s: string |
| 70 |
d: int (decimal) |
| 71 |
x: int (hex) |
| 72 |
*/ |
| 73 |
|
| 74 |
/* Set according to -mlog= option. */ |
| 75 |
avr_log_t avr_log; |
| 76 |
|
| 77 |
/* The worker function implementing the %-codes */ |
| 78 |
static void avr_log_vadump (FILE*, const char*, va_list); |
| 79 |
|
| 80 |
/* Forward to fprintf for convenience. Return the number of consumed format |
| 81 |
chars after a %-code, or 0 if unrecognized and nothing consumed. */ |
| 82 |
static int avr_forward_to_printf (FILE*, const char*, va_list); |
| 83 |
|
| 84 |
/* Wrapper for avr_log_vadump. If STREAM is NULL we are called by avr_dump, |
| 85 |
i.e. output to dump_file if available. The 2nd argument is __FUNCTION__. |
| 86 |
The 3rd argument is the format string. */ |
| 87 |
|
| 88 |
int |
| 89 |
avr_vdump (FILE *stream, const char *caller, ...) |
| 90 |
{ |
| 91 |
va_list ap; |
| 92 |
|
| 93 |
if (stream == NULL && dump_file) |
| 94 |
stream = dump_file; |
| 95 |
|
| 96 |
va_start (ap, caller); |
| 97 |
if (stream) |
| 98 |
avr_log_vadump (stream, caller, ap); |
| 99 |
va_end (ap); |
| 100 |
|
| 101 |
return 1; |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
/* Worker function implementing the %-codes and forwarding to |
| 106 |
respective print/dump function. */ |
| 107 |
|
| 108 |
static void |
| 109 |
avr_log_vadump (FILE *file, const char *caller, va_list ap) |
| 110 |
{ |
| 111 |
char bs[3] = {'\\', '?', '\0'}; |
| 112 |
|
| 113 |
/* 3rd proper argument is always the format string. */ |
| 114 |
const char *fmt = va_arg (ap, const char*); |
| 115 |
|
| 116 |
while (*fmt) |
| 117 |
{ |
| 118 |
switch (*fmt++) |
| 119 |
{ |
| 120 |
default: |
| 121 |
fputc (*(fmt-1), file); |
| 122 |
break; |
| 123 |
|
| 124 |
case '\\': |
| 125 |
bs[1] = *fmt++; |
| 126 |
fputs (bs, file); |
| 127 |
break; |
| 128 |
|
| 129 |
case '%': |
| 130 |
switch (*fmt++) |
| 131 |
{ |
| 132 |
case '%': |
| 133 |
fputc ('%', file); |
| 134 |
break; |
| 135 |
|
| 136 |
case 't': |
| 137 |
{ |
| 138 |
tree t = va_arg (ap, tree); |
| 139 |
if (NULL_TREE == t) |
| 140 |
fprintf (file, "<NULL-TREE>"); |
| 141 |
else |
| 142 |
{ |
| 143 |
if (stderr == file) |
| 144 |
debug_tree (t); |
| 145 |
else |
| 146 |
{ |
| 147 |
print_node (file, "", t, 0); |
| 148 |
putc ('\n', file); |
| 149 |
} |
| 150 |
} |
| 151 |
break; |
| 152 |
} |
| 153 |
|
| 154 |
case 'T': |
| 155 |
{ |
| 156 |
tree t = va_arg (ap, tree); |
| 157 |
if (NULL_TREE == t) |
| 158 |
fprintf (file, "<NULL-TREE>"); |
| 159 |
else |
| 160 |
print_node_brief (file, "", t, 3); |
| 161 |
} |
| 162 |
break; |
| 163 |
|
| 164 |
case 'b': |
| 165 |
fprintf (file, "%s", va_arg (ap, int) ? "true" : "false"); |
| 166 |
break; |
| 167 |
|
| 168 |
case 'c': |
| 169 |
fputc (va_arg (ap, int), file); |
| 170 |
break; |
| 171 |
|
| 172 |
case 'r': |
| 173 |
print_inline_rtx (file, va_arg (ap, rtx), 0); |
| 174 |
break; |
| 175 |
|
| 176 |
case 'L': |
| 177 |
{ |
| 178 |
rtx_insn *insn = safe_as_a <rtx_insn *> (va_arg (ap, rtx)); |
| 179 |
|
| 180 |
while (insn) |
| 181 |
{ |
| 182 |
print_inline_rtx (file, insn, 0); |
| 183 |
fprintf (file, "\n"); |
| 184 |
insn = NEXT_INSN (insn); |
| 185 |
} |
| 186 |
break; |
| 187 |
} |
| 188 |
|
| 189 |
case 'f': |
| 190 |
if (cfun && cfun->decl) |
| 191 |
fputs (current_function_name(), file); |
| 192 |
break; |
| 193 |
|
| 194 |
case 's': |
| 195 |
{ |
| 196 |
const char *str = va_arg (ap, char*); |
| 197 |
fputs (str ? str : "(null)", file); |
| 198 |
} |
| 199 |
break; |
| 200 |
|
| 201 |
case 'm': |
| 202 |
fputs (GET_MODE_NAME ((machine_mode) va_arg (ap, int)), |
| 203 |
file); |
| 204 |
break; |
| 205 |
|
| 206 |
case 'C': |
| 207 |
fputs (rtx_name[va_arg (ap, int)], file); |
| 208 |
break; |
| 209 |
|
| 210 |
case 'R': |
| 211 |
fputs (reg_class_names[va_arg (ap, int)], file); |
| 212 |
break; |
| 213 |
|
| 214 |
case 'F': |
| 215 |
fputs (caller, file); |
| 216 |
break; |
| 217 |
|
| 218 |
case 'H': |
| 219 |
{ |
| 220 |
location_t loc = va_arg (ap, location_t); |
| 221 |
|
| 222 |
if (BUILTINS_LOCATION == loc) |
| 223 |
fprintf (file, "<BUILTIN-LOCATION>"); |
| 224 |
else if (UNKNOWN_LOCATION == loc) |
| 225 |
fprintf (file, "<UNKNOWN-LOCATION>"); |
| 226 |
else |
| 227 |
fprintf (file, "%s:%d", |
| 228 |
LOCATION_FILE (loc), LOCATION_LINE (loc)); |
| 229 |
|
| 230 |
break; |
| 231 |
} |
| 232 |
|
| 233 |
case '!': |
| 234 |
if (!current_pass) |
| 235 |
return; |
| 236 |
/* FALLTHRU */ |
| 237 |
|
| 238 |
case '?': |
| 239 |
avr_vdump (file, caller, "%F[%f:%P]"); |
| 240 |
break; |
| 241 |
|
| 242 |
case 'P': |
| 243 |
if (current_pass) |
| 244 |
fprintf (file, "%s(%d)", |
| 245 |
current_pass->name, |
| 246 |
current_pass->static_pass_number); |
| 247 |
else |
| 248 |
fprintf (file, "pass=?"); |
| 249 |
|
| 250 |
break; |
| 251 |
|
| 252 |
case 'A': |
| 253 |
fflush (file); |
| 254 |
abort(); |
| 255 |
|
| 256 |
default: |
| 257 |
int n_used = avr_forward_to_printf (file, fmt - 1, ap); |
| 258 |
if (n_used > 0) |
| 259 |
{ |
| 260 |
// "-1" due to "*fmt++" above. |
| 261 |
fmt += n_used - 1; |
| 262 |
} |
| 263 |
else |
| 264 |
{ |
| 265 |
// Unknown %-code: Stop printing. |
| 266 |
fprintf (file, "??? %%%c ???%s\n", *(fmt-1), fmt); |
| 267 |
fmt = ""; |
| 268 |
} |
| 269 |
break; |
| 270 |
} |
| 271 |
break; /* % */ |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
fflush (file); |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
#define IS_INTC(c) (c == 'd' || c == 'x' || c == 'X') |
| 280 |
|
| 281 |
// Consume FMTs like: %x %4x %04x %*x %0*x |
| 282 |
// and similar for 'd' or 'ld' or 'X' instead of 'x'. |
| 283 |
|
| 284 |
static int |
| 285 |
avr_forward_to_printf (FILE *file, const char* const fmt, va_list ap) |
| 286 |
{ |
| 287 |
const char *p = fmt; |
| 288 |
bool len_p = false; |
| 289 |
|
| 290 |
// Optional fill |
| 291 |
p += p[0] == '0' || p[0] == ' '; |
| 292 |
|
| 293 |
// optional length |
| 294 |
if (p[0] >= '1' && p[0] <= '9') |
| 295 |
++p; |
| 296 |
else if (p[0] == '*') |
| 297 |
++p, len_p = true; |
| 298 |
|
| 299 |
// Type |
| 300 |
const bool long_p = p[0] == 'l'; |
| 301 |
|
| 302 |
if (IS_INTC (p[0]) |
| 303 |
|| (long_p && IS_INTC (p[1]))) |
| 304 |
{ |
| 305 |
p += 1 + long_p; |
| 306 |
|
| 307 |
const int n_used = (int) (p - fmt); |
| 308 |
|
| 309 |
char xfm[10] = { '%' }; |
| 310 |
memcpy (xfm + 1, fmt, n_used); |
| 311 |
xfm[1 + n_used] = '\0'; |
| 312 |
|
| 313 |
if (len_p) |
| 314 |
{ |
| 315 |
const int len = va_arg (ap, int); |
| 316 |
if (long_p) |
| 317 |
fprintf (file, xfm, len, va_arg (ap, long)); |
| 318 |
else |
| 319 |
fprintf (file, xfm, len, va_arg (ap, int)); |
| 320 |
} |
| 321 |
else |
| 322 |
{ |
| 323 |
if (long_p) |
| 324 |
fprintf (file, xfm, va_arg (ap, long)); |
| 325 |
else |
| 326 |
fprintf (file, xfm, va_arg (ap, int)); |
| 327 |
} |
| 328 |
|
| 329 |
return n_used; |
| 330 |
} |
| 331 |
|
| 332 |
return 0; |
| 333 |
} |
| 334 |
|
| 335 |
|
| 336 |
/* Called from avr.cc:avr_option_override(). |
| 337 |
Parse argument of -mlog= and set respective fields in avr_log. */ |
| 338 |
|
| 339 |
void |
| 340 |
avr_log_set_avr_log (void) |
| 341 |
{ |
| 342 |
bool all = TARGET_ALL_DEBUG != 0; |
| 343 |
|
| 344 |
if (all) |
| 345 |
avropt_log_details = "all"; |
| 346 |
|
| 347 |
if (all || avropt_log_details) |
| 348 |
{ |
| 349 |
/* Adding , at beginning and end of string makes searching easier. */ |
| 350 |
|
| 351 |
char *str = (char*) alloca (3 + strlen (avropt_log_details)); |
| 352 |
bool info; |
| 353 |
|
| 354 |
str[0] = ','; |
| 355 |
strcat (stpcpy (str+1, avropt_log_details), ","); |
| 356 |
|
| 357 |
all |= strstr (str, ",all,") != NULL; |
| 358 |
info = strstr (str, ",?,") != NULL; |
| 359 |
|
| 360 |
if (info) |
| 361 |
fprintf (stderr, "\n-mlog="); |
| 362 |
|
| 363 |
#define SET_DUMP_DETAIL(S) \ |
| 364 |
do { \ |
| 365 |
avr_log.S = (all || strstr (str, "," #S ",") != NULL); \ |
| 366 |
if (info) \ |
| 367 |
fprintf (stderr, #S ","); \ |
| 368 |
} while (0) |
| 369 |
|
| 370 |
SET_DUMP_DETAIL (address_cost); |
| 371 |
SET_DUMP_DETAIL (builtin); |
| 372 |
SET_DUMP_DETAIL (constraints); |
| 373 |
SET_DUMP_DETAIL (insn_addresses); |
| 374 |
SET_DUMP_DETAIL (legitimate_address_p); |
| 375 |
SET_DUMP_DETAIL (legitimize_address); |
| 376 |
SET_DUMP_DETAIL (legitimize_reload_address); |
| 377 |
SET_DUMP_DETAIL (progmem); |
| 378 |
SET_DUMP_DETAIL (rtx_costs); |
| 379 |
|
| 380 |
#undef SET_DUMP_DETAIL |
| 381 |
|
| 382 |
if (info) |
| 383 |
fprintf (stderr, "?\n\n"); |
| 384 |
} |
| 385 |
} |