Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
/* Copyright (C) 2009-2020 Free Software Foundation, Inc.
   Contributed by Anatoly Sokolov (aesok@post.ru)

   This file is part of GCC.

   GCC is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.
   
   GCC is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with GCC; see the file COPYING3.  If not see
   <http://www.gnu.org/licenses/>.  */

/* Not included in avr.c since this requires C front end.  */

#define IN_TARGET_CODE 1

#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "target.h"
#include "c-family/c-common.h"
#include "stor-layout.h"
#include "langhooks.h"
#include "memmodel.h"
#include "tm_p.h"

/* IDs for all the AVR builtins.  */

enum avr_builtin_id
  {
#define DEF_BUILTIN(NAME, N_ARGS, TYPE, CODE, LIBNAME)  \
    AVR_BUILTIN_ ## NAME,
#include "builtins.def"
#undef DEF_BUILTIN

    AVR_BUILTIN_COUNT
  };


/* Implement `TARGET_RESOLVE_OVERLOADED_PLUGIN'.  */

static tree
avr_resolve_overloaded_builtin (unsigned int iloc, tree fndecl, void *vargs)
{
  tree type0, type1, fold = NULL_TREE;
  enum avr_builtin_id id = AVR_BUILTIN_COUNT;
  location_t loc = (location_t) iloc;
  vec<tree, va_gc> &args = * (vec<tree, va_gc>*) vargs;

  switch (DECL_MD_FUNCTION_CODE (fndecl))
    {
    default:
      break;

    case AVR_BUILTIN_ABSFX:
      if (args.length() != 1)
        {
          error_at (loc, "%qs expects 1 argument but %d given",
                    "absfx", (int) args.length());

          fold = error_mark_node;
          break;
        }

      type0 = TREE_TYPE (args[0]);

      if (!FIXED_POINT_TYPE_P (type0))
        {
          error_at (loc, "%qs expects a fixed-point value as argument",
                    "absfx");

          fold = error_mark_node;
        }

      switch (TYPE_MODE (type0))
        {
        case E_QQmode: id = AVR_BUILTIN_ABSHR; break;
        case E_HQmode: id = AVR_BUILTIN_ABSR; break;
        case E_SQmode: id = AVR_BUILTIN_ABSLR; break;
        case E_DQmode: id = AVR_BUILTIN_ABSLLR; break;

        case E_HAmode: id = AVR_BUILTIN_ABSHK; break;
        case E_SAmode: id = AVR_BUILTIN_ABSK; break;
        case E_DAmode: id = AVR_BUILTIN_ABSLK; break;
        case E_TAmode: id = AVR_BUILTIN_ABSLLK; break;

        case E_UQQmode:
        case E_UHQmode:
        case E_USQmode:
        case E_UDQmode:
        case E_UHAmode:
        case E_USAmode:
        case E_UDAmode:
        case E_UTAmode:
          warning_at (loc, 0, "using %qs with unsigned type has no effect",
                      "absfx");
          return args[0];

        default:
          error_at (loc, "no matching fixed-point overload found for %qs",
                    "absfx");

          fold = error_mark_node;
          break;
        }

      fold = targetm.builtin_decl (id, true);

      if (fold != error_mark_node)
        fold = build_function_call_vec (loc, vNULL, fold, &args, NULL);

      break; // absfx

    case AVR_BUILTIN_ROUNDFX:
      if (args.length() != 2)
        {
          error_at (loc, "%qs expects 2 arguments but %d given",
                    "roundfx", (int) args.length());

          fold = error_mark_node;
          break;
        }

      type0 = TREE_TYPE (args[0]);
      type1 = TREE_TYPE (args[1]);

      if (!FIXED_POINT_TYPE_P (type0))
        {
          error_at (loc, "%qs expects a fixed-point value as first argument",
                    "roundfx");

          fold = error_mark_node;
        }

      if (!INTEGRAL_TYPE_P (type1))
        {
          error_at (loc, "%qs expects an integer value as second argument",
                    "roundfx");

          fold = error_mark_node;
        }

      switch (TYPE_MODE (type0))
        {
        case E_QQmode: id = AVR_BUILTIN_ROUNDHR; break;
        case E_HQmode: id = AVR_BUILTIN_ROUNDR; break;
        case E_SQmode: id = AVR_BUILTIN_ROUNDLR; break;
        case E_DQmode: id = AVR_BUILTIN_ROUNDLLR; break;

        case E_UQQmode: id = AVR_BUILTIN_ROUNDUHR; break;
        case E_UHQmode: id = AVR_BUILTIN_ROUNDUR; break;
        case E_USQmode: id = AVR_BUILTIN_ROUNDULR; break;
        case E_UDQmode: id = AVR_BUILTIN_ROUNDULLR; break;

        case E_HAmode: id = AVR_BUILTIN_ROUNDHK; break;
        case E_SAmode: id = AVR_BUILTIN_ROUNDK; break;
        case E_DAmode: id = AVR_BUILTIN_ROUNDLK; break;
        case E_TAmode: id = AVR_BUILTIN_ROUNDLLK; break;

        case E_UHAmode: id = AVR_BUILTIN_ROUNDUHK; break;
        case E_USAmode: id = AVR_BUILTIN_ROUNDUK; break;
        case E_UDAmode: id = AVR_BUILTIN_ROUNDULK; break;
        case E_UTAmode: id = AVR_BUILTIN_ROUNDULLK; break;

        default:
          error_at (loc, "no matching fixed-point overload found for %qs",
                    "roundfx");

          fold = error_mark_node;
          break;
        }

      fold = targetm.builtin_decl (id, true);

      if (fold != error_mark_node)
        fold = build_function_call_vec (loc, vNULL, fold, &args, NULL);

      break; // roundfx

    case AVR_BUILTIN_COUNTLSFX:
      if (args.length() != 1)
        {
          error_at (loc, "%qs expects 1 argument but %d given",
                    "countlsfx", (int) args.length());

          fold = error_mark_node;
          break;
        }

      type0 = TREE_TYPE (args[0]);

      if (!FIXED_POINT_TYPE_P (type0))
        {
          error_at (loc, "%qs expects a fixed-point value as first argument",
                    "countlsfx");

          fold = error_mark_node;
        }

      switch (TYPE_MODE (type0))
        {
        case E_QQmode: id = AVR_BUILTIN_COUNTLSHR; break;
        case E_HQmode: id = AVR_BUILTIN_COUNTLSR; break;
        case E_SQmode: id = AVR_BUILTIN_COUNTLSLR; break;
        case E_DQmode: id = AVR_BUILTIN_COUNTLSLLR; break;

        case E_UQQmode: id = AVR_BUILTIN_COUNTLSUHR; break;
        case E_UHQmode: id = AVR_BUILTIN_COUNTLSUR; break;
        case E_USQmode: id = AVR_BUILTIN_COUNTLSULR; break;
        case E_UDQmode: id = AVR_BUILTIN_COUNTLSULLR; break;

        case E_HAmode: id = AVR_BUILTIN_COUNTLSHK; break;
        case E_SAmode: id = AVR_BUILTIN_COUNTLSK; break;
        case E_DAmode: id = AVR_BUILTIN_COUNTLSLK; break;
        case E_TAmode: id = AVR_BUILTIN_COUNTLSLLK; break;

        case E_UHAmode: id = AVR_BUILTIN_COUNTLSUHK; break;
        case E_USAmode: id = AVR_BUILTIN_COUNTLSUK; break;
        case E_UDAmode: id = AVR_BUILTIN_COUNTLSULK; break;
        case E_UTAmode: id = AVR_BUILTIN_COUNTLSULLK; break;

        default:
          error_at (loc, "no matching fixed-point overload found for %qs",
                    "countlsfx");

          fold = error_mark_node;
          break;
        }

      fold = targetm.builtin_decl (id, true);

      if (fold != error_mark_node)
        fold = build_function_call_vec (loc, vNULL, fold, &args, NULL);

      break; // countlsfx
    }

  return fold;
}
  

/* Implement `REGISTER_TARGET_PRAGMAS'.  */

void
avr_register_target_pragmas (void)
{
  gcc_assert (ADDR_SPACE_GENERIC == ADDR_SPACE_RAM);

  /* Register address spaces.  The order must be the same as in the respective
     enum from avr.h (or designated initializers must be used in avr.c).
     We always register all address spaces even if some of them make no
     sense for some targets.  Diagnose for non-supported spaces will be
     emit by TARGET_ADDR_SPACE_DIAGNOSE_USAGE.  */

  for (int i = 0; i < ADDR_SPACE_COUNT; i++)
    {
      gcc_assert (i == avr_addrspace[i].id);

      if (!ADDR_SPACE_GENERIC_P (i))
        c_register_addr_space (avr_addrspace[i].name, avr_addrspace[i].id);
    }

  targetm.resolve_overloaded_builtin = avr_resolve_overloaded_builtin;
}


/* Transform LO into uppercase and write the result to UP.
   You must provide enough space for UP.  Return UP.  */

static char*
avr_toupper (char *up, const char *lo)
{
  char *up0 = up;

  for (; *lo; lo++, up++)
    *up = TOUPPER (*lo);

  *up = '\0';

  return up0;
}

/* Worker function for TARGET_CPU_CPP_BUILTINS.  */

void
avr_cpu_cpp_builtins (struct cpp_reader *pfile)
{
  builtin_define_std ("AVR");

  /* __AVR_DEVICE_NAME__ and  avr_mcu_types[].macro like __AVR_ATmega8__
     are defined by -D command option, see device-specs file.  */

  if (avr_arch->macro)
    cpp_define_formatted (pfile, "__AVR_ARCH__=%s", avr_arch->macro);
  if (AVR_HAVE_RAMPD)    cpp_define (pfile, "__AVR_HAVE_RAMPD__");
  if (AVR_HAVE_RAMPX)    cpp_define (pfile, "__AVR_HAVE_RAMPX__");
  if (AVR_HAVE_RAMPY)    cpp_define (pfile, "__AVR_HAVE_RAMPY__");
  if (AVR_HAVE_RAMPZ)    cpp_define (pfile, "__AVR_HAVE_RAMPZ__");
  if (AVR_HAVE_ELPM)     cpp_define (pfile, "__AVR_HAVE_ELPM__");
  if (AVR_HAVE_ELPMX)    cpp_define (pfile, "__AVR_HAVE_ELPMX__");
  if (AVR_HAVE_MOVW)     cpp_define (pfile, "__AVR_HAVE_MOVW__");
  if (AVR_HAVE_LPMX)     cpp_define (pfile, "__AVR_HAVE_LPMX__");

  if (avr_arch->asm_only)
    cpp_define (pfile, "__AVR_ASM_ONLY__");
  if (AVR_HAVE_MUL)
    {
      cpp_define (pfile, "__AVR_ENHANCED__");
      cpp_define (pfile, "__AVR_HAVE_MUL__");
    }

  if (AVR_HAVE_JMP_CALL)
    cpp_define (pfile, "__AVR_HAVE_JMP_CALL__");

  if (avr_arch->have_jmp_call)
    cpp_define (pfile, "__AVR_MEGA__");

  if (AVR_SHORT_CALLS)
    cpp_define (pfile, "__AVR_SHORT_CALLS__");

  if (AVR_XMEGA)
    cpp_define (pfile, "__AVR_XMEGA__");

  if (AVR_TINY)
    {
      cpp_define (pfile, "__AVR_TINY__");

      /* Define macro "__AVR_TINY_PM_BASE_ADDRESS__" with mapped program memory
         start address.  This macro shall be used where mapped program
         memory is accessed, eg. copying data section (__do_copy_data)
         contents to data memory region.
         NOTE:
         Program memory of AVR_TINY devices cannot be accessed directly,
         it has been mapped to the data memory.  For AVR_TINY devices
         (ATtiny4/5/9/10/20 and 40) mapped program memory starts at 0x4000. */

      cpp_define_formatted (pfile, "__AVR_TINY_PM_BASE_ADDRESS__=0x%x",
                            avr_arch->flash_pm_offset);
    }

  if (avr_arch->flash_pm_offset)
    cpp_define_formatted (pfile, "__AVR_PM_BASE_ADDRESS__=0x%x",
                          avr_arch->flash_pm_offset);

  if (AVR_HAVE_EIJMP_EICALL)
    {
      cpp_define (pfile, "__AVR_HAVE_EIJMP_EICALL__");
      cpp_define (pfile, "__AVR_3_BYTE_PC__");
    }
  else
    {
      cpp_define (pfile, "__AVR_2_BYTE_PC__");
    }

  if (AVR_HAVE_8BIT_SP)
    cpp_define (pfile, "__AVR_HAVE_8BIT_SP__");
  else
    cpp_define (pfile, "__AVR_HAVE_16BIT_SP__");

  if (AVR_HAVE_SPH)
    cpp_define (pfile, "__AVR_HAVE_SPH__");
  else
    cpp_define (pfile, "__AVR_SP8__");

  if (TARGET_NO_INTERRUPTS)
    cpp_define (pfile, "__NO_INTERRUPTS__");

  if (TARGET_SKIP_BUG)
    {
      cpp_define (pfile, "__AVR_ERRATA_SKIP__");

      if (AVR_HAVE_JMP_CALL)
        cpp_define (pfile, "__AVR_ERRATA_SKIP_JMP_CALL__");
    }

  if (TARGET_RMW)
    cpp_define (pfile, "__AVR_ISA_RMW__");

  cpp_define_formatted (pfile, "__AVR_SFR_OFFSET__=0x%x",
                        avr_arch->sfr_offset);

#ifdef WITH_AVRLIBC
  cpp_define (pfile, "__WITH_AVRLIBC__");
#endif /* WITH_AVRLIBC */

  // From configure --with-libf7={|libgcc|math|math-symbols|yes|no}

#ifdef WITH_LIBF7_LIBGCC
  cpp_define (pfile, "__WITH_LIBF7_LIBGCC__");
#endif /* WITH_LIBF7_LIBGCC */

#ifdef WITH_LIBF7_MATH
  cpp_define (pfile, "__WITH_LIBF7_MATH__");
#endif /* WITH_LIBF7_MATH */

#ifdef WITH_LIBF7_MATH_SYMBOLS
  cpp_define (pfile, "__WITH_LIBF7_MATH_SYMBOLS__");
#endif /* WITH_LIBF7_MATH_SYMBOLS */

  // From configure --with-double={|32|32,64|64,32|64}

#ifdef HAVE_DOUBLE_MULTILIB
  cpp_define (pfile, "__HAVE_DOUBLE_MULTILIB__");
#endif

#ifdef HAVE_DOUBLE64
  cpp_define (pfile, "__HAVE_DOUBLE64__");
#endif

#ifdef HAVE_DOUBLE32
  cpp_define (pfile, "__HAVE_DOUBLE32__");
#endif

#if defined (WITH_DOUBLE64)
  cpp_define (pfile, "__DEFAULT_DOUBLE__=64");
#elif defined (WITH_DOUBLE32)
  cpp_define (pfile, "__DEFAULT_DOUBLE__=32");
#else
#error "align this with config.gcc"
#endif

  // From configure --with-long-double={|32|32,64|64,32|64|double}

#ifdef HAVE_LONG_DOUBLE_MULTILIB
  cpp_define (pfile, "__HAVE_LONG_DOUBLE_MULTILIB__");
#endif

#ifdef HAVE_LONG_DOUBLE64
  cpp_define (pfile, "__HAVE_LONG_DOUBLE64__");
#endif

#ifdef HAVE_LONG_DOUBLE32
  cpp_define (pfile, "__HAVE_LONG_DOUBLE32__");
#endif

#ifdef HAVE_LONG_DOUBLE_IS_DOUBLE
  cpp_define (pfile, "__HAVE_LONG_DOUBLE_IS_DOUBLE__");
#endif

#if defined (WITH_LONG_DOUBLE64)
  cpp_define (pfile, "__DEFAULT_LONG_DOUBLE__=64");
#elif defined (WITH_LONG_DOUBLE32)
  cpp_define (pfile, "__DEFAULT_LONG_DOUBLE__=32");
#else
#error "align this with config.gcc"
#endif

  // From configure --with-double-comparison={2|3} --with-libf7.

#if defined (WITH_DOUBLE_COMPARISON)
#if WITH_DOUBLE_COMPARISON == 2 || WITH_DOUBLE_COMPARISON == 3
  /* The number of states a DFmode comparison libcall might take and
     reflects what avr.c:FLOAT_LIB_COMPARE_RETURNS_BOOL returns for
     DFmode.  GCC's default is 3-state, but some libraries like LibF7
     implement true / false (2-state).  */
  cpp_define_formatted (pfile, "__WITH_DOUBLE_COMPARISON__=%d",
			WITH_DOUBLE_COMPARISON);
#else
#error "align this with config.gcc"
#endif
#else
#error "align this with config.gcc"
#endif

  /* Define builtin macros so that the user can easily query whether
     non-generic address spaces (and which) are supported or not.
     This is only supported for C.  For C++, a language extension is needed
     (as mentioned in ISO/IEC DTR 18037; Annex F.2) which is not
     implemented in GCC up to now.  */

  if (lang_GNU_C ())
    {
      for (int i = 0; i < ADDR_SPACE_COUNT; i++)
        if (!ADDR_SPACE_GENERIC_P (i)
            /* Only supply __FLASH<n> macro if the address space is reasonable
               for this target.  The address space qualifier itself is still
               supported, but using it will throw an error.  */
            && avr_addr_space_supported_p ((addr_space_t) i))
          {
            const char *name = avr_addrspace[i].name;
            char *Name = (char*) alloca (1 + strlen (name));

            cpp_define (pfile, avr_toupper (Name, name));
          }
    }

  /* Define builtin macros so that the user can easily query whether or
     not a specific builtin is available. */

#define DEF_BUILTIN(NAME, N_ARGS, TYPE, CODE, LIBNAME)  \
  cpp_define (pfile, "__BUILTIN_AVR_" #NAME);
#include "builtins.def"
#undef DEF_BUILTIN

  /* Builtin macros for the __int24 and __uint24 type.  */

  cpp_define_formatted (pfile, "__INT24_MAX__=8388607%s",
                        INT_TYPE_SIZE == 8 ? "LL" : "L");
  cpp_define (pfile, "__INT24_MIN__=(-__INT24_MAX__-1)");
  cpp_define_formatted (pfile, "__UINT24_MAX__=16777215%s",
                        INT_TYPE_SIZE == 8 ? "ULL" : "UL");
}