SpenceKonde / ATTinyCore

Arduino core for ATtiny 1634, 828, x313, x4, x41, x5, x61, x7 and x8
Other
1.53k stars 301 forks source link

Problem on compilation with malloc #855

Open FroggyCorp opened 2 months ago

FroggyCorp commented 2 months ago

Hi,

I'm using the 1.5.2 ATTinyCore, i saw that there is a 2.0 coming, and didn't try with it. I have actually no time to check my code on the 2.0 (i apologize). I wanted to lower the size of my program for an attiny84 and got that problem (minimalist) (i use attiny84 without bootloader) :

This simple code is 898B size :

uint8_t * send = NULL;
void Function(int val)
  {
    if (!send)
          send = (uint8_t*) malloc((val) * sizeof(uint8_t));
      send[3] = val;
  }

void setup() {}

void loop() {
  Function(5);
}

but this simple code is 246B :

uint8_t * send = NULL;

void Function(int val)
  {

      send = (uint8_t*) malloc((val) * sizeof(uint8_t));
      send[3] = val;
  }

void setup() {}

void loop() {
  Function(5);
}

So, if i use "send" before malloc, i have a full sketch compiled, but if i use "send" after malloc, the program is "free" of code :

`Disassembly of section .text:

00000000 <__vectors>:
__vectors():
   0:   10 c0           rjmp    .+32        ; 0x22 <__ctors_end>
   2:   1f c0           rjmp    .+62        ; 0x42 <__bad_interrupt>
   4:   1e c0           rjmp    .+60        ; 0x42 <__bad_interrupt>
   6:   1d c0           rjmp    .+58        ; 0x42 <__bad_interrupt>
   8:   1c c0           rjmp    .+56        ; 0x42 <__bad_interrupt>
   a:   1b c0           rjmp    .+54        ; 0x42 <__bad_interrupt>
   c:   1a c0           rjmp    .+52        ; 0x42 <__bad_interrupt>
   e:   19 c0           rjmp    .+50        ; 0x42 <__bad_interrupt>
  10:   18 c0           rjmp    .+48        ; 0x42 <__bad_interrupt>
  12:   17 c0           rjmp    .+46        ; 0x42 <__bad_interrupt>
  14:   16 c0           rjmp    .+44        ; 0x42 <__bad_interrupt>
  16:   16 c0           rjmp    .+44        ; 0x44 <__vector_11>
  18:   14 c0           rjmp    .+40        ; 0x42 <__bad_interrupt>
  1a:   13 c0           rjmp    .+38        ; 0x42 <__bad_interrupt>
  1c:   12 c0           rjmp    .+36        ; 0x42 <__bad_interrupt>
  1e:   11 c0           rjmp    .+34        ; 0x42 <__bad_interrupt>
  20:   10 c0           rjmp    .+32        ; 0x42 <__bad_interrupt>

00000022 <__ctors_end>:
__trampolines_start():
  22:   11 24           eor r1, r1
  24:   1f be           out 0x3f, r1    ; 63
  26:   cf e5           ldi r28, 0x5F   ; 95
  28:   d2 e0           ldi r29, 0x02   ; 2
  2a:   de bf           out 0x3e, r29   ; 62
  2c:   cd bf           out 0x3d, r28   ; 61

0000002e <__do_clear_bss>:
__do_clear_bss():
/home/jenkins-mingw32/workspace/avr-gcc-staging/label/Ubuntu14.04x64-mingw32/gcc-build/avr/avr25/libgcc/../../../../gcc/libgcc/config/avr/lib1funcs.S:2441
  2e:   20 e0           ldi r18, 0x00   ; 0
/home/jenkins-mingw32/workspace/avr-gcc-staging/label/Ubuntu14.04x64-mingw32/gcc-build/avr/avr25/libgcc/../../../../gcc/libgcc/config/avr/lib1funcs.S:2442
  30:   a0 e6           ldi r26, 0x60   ; 96
/home/jenkins-mingw32/workspace/avr-gcc-staging/label/Ubuntu14.04x64-mingw32/gcc-build/avr/avr25/libgcc/../../../../gcc/libgcc/config/avr/lib1funcs.S:2443
  32:   b0 e0           ldi r27, 0x00   ; 0
/home/jenkins-mingw32/workspace/avr-gcc-staging/label/Ubuntu14.04x64-mingw32/gcc-build/avr/avr25/libgcc/../../../../gcc/libgcc/config/avr/lib1funcs.S:2444
  34:   01 c0           rjmp    .+2         ; 0x38 <.do_clear_bss_start>

00000036 <.do_clear_bss_loop>:
/home/jenkins-mingw32/workspace/avr-gcc-staging/label/Ubuntu14.04x64-mingw32/gcc-build/avr/avr25/libgcc/../../../../gcc/libgcc/config/avr/lib1funcs.S:2446
  36:   1d 92           st  X+, r1

00000038 <.do_clear_bss_start>:
/home/jenkins-mingw32/workspace/avr-gcc-staging/label/Ubuntu14.04x64-mingw32/gcc-build/avr/avr25/libgcc/../../../../gcc/libgcc/config/avr/lib1funcs.S:2448
  38:   a9 36           cpi r26, 0x69   ; 105
/home/jenkins-mingw32/workspace/avr-gcc-staging/label/Ubuntu14.04x64-mingw32/gcc-build/avr/avr25/libgcc/../../../../gcc/libgcc/config/avr/lib1funcs.S:2449
  3a:   b2 07           cpc r27, r18
/home/jenkins-mingw32/workspace/avr-gcc-staging/label/Ubuntu14.04x64-mingw32/gcc-build/avr/avr25/libgcc/../../../../gcc/libgcc/config/avr/lib1funcs.S:2450
  3c:   e1 f7           brne    .-8         ; 0x36 <.do_clear_bss_loop>
.do_clear_bss_start():
  3e:   4c d0           rcall   .+152       ; 0xd8 <main>
  40:   58 c0           rjmp    .+176       ; 0xf2 <_exit>

00000042 <__bad_interrupt>:
__vector_1():
  42:   de cf           rjmp    .-68        ; 0x0 <__vectors>

00000044 <__vector_11>:
__vector_11():
\Arduino15\packages\ATTinyCore\hardware\avr\1.5.2\cores\tiny/wiring.c:308
      #error "cannot find Millis() timer overflow vector"
    #endif
  #else
    #error "Millis() timer not defined!"
  #endif
  {
  44:   1f 92           push    r1
  46:   0f 92           push    r0
  48:   0f b6           in  r0, 0x3f    ; 63
  4a:   0f 92           push    r0
  4c:   11 24           eor r1, r1
  4e:   2f 93           push    r18
  50:   3f 93           push    r19
  52:   8f 93           push    r24
  54:   9f 93           push    r25
  56:   af 93           push    r26
  58:   bf 93           push    r27
\Arduino15\packages\ATTinyCore\hardware\avr\1.5.2\cores\tiny/wiring.c:311
    // copy these to local variables so they can be stored in registers
    // (volatile variables must be read from memory on every access)
    unsigned long m = millis_timer_millis;
  5a:   80 91 65 00     lds r24, 0x0065 ; 0x800065 <millis_timer_millis>
  5e:   90 91 66 00     lds r25, 0x0066 ; 0x800066 <millis_timer_millis+0x1>
  62:   a0 91 67 00     lds r26, 0x0067 ; 0x800067 <millis_timer_millis+0x2>
  66:   b0 91 68 00     lds r27, 0x0068 ; 0x800068 <millis_timer_millis+0x3>
\Arduino15\packages\ATTinyCore\hardware\avr\1.5.2\cores\tiny/wiring.c:312
    unsigned char f = millis_timer_fract;
  6a:   30 91 64 00     lds r19, 0x0064 ; 0x800064 <millis_timer_fract>
\Arduino15\packages\ATTinyCore\hardware\avr\1.5.2\cores\tiny/wiring.c:319
    static unsigned char correct_exact = 0;     // rollover intended
    if (++correct_exact < CORRECT_EXACT_MANY) {
      ++f;
    }
#endif
    f += FRACT_INC;
  6e:   26 e0           ldi r18, 0x06   ; 6
  70:   23 0f           add r18, r19
\Arduino15\packages\ATTinyCore\hardware\avr\1.5.2\cores\tiny/wiring.c:321

    if (f >= FRACT_MAX)
  72:   2d 37           cpi r18, 0x7D   ; 125
  74:   68 f1           brcs    .+90        ; 0xd0 <__vector_11+0x8c>
\Arduino15\packages\ATTinyCore\hardware\avr\1.5.2\cores\tiny/wiring.c:323
    {
      f -= FRACT_MAX;
  76:   29 e8           ldi r18, 0x89   ; 137
  78:   23 0f           add r18, r19
\Arduino15\packages\ATTinyCore\hardware\avr\1.5.2\cores\tiny/wiring.c:324
      m += MILLIS_INC + 1;
  7a:   03 96           adiw    r24, 0x03   ; 3
  7c:   a1 1d           adc r26, r1
  7e:   b1 1d           adc r27, r1
\Arduino15\packages\ATTinyCore\hardware\avr\1.5.2\cores\tiny/wiring.c:331
    else
    {
      m += MILLIS_INC;
    }

    millis_timer_fract = f;
  80:   20 93 64 00     sts 0x0064, r18 ; 0x800064 <millis_timer_fract>
\Arduino15\packages\ATTinyCore\hardware\avr\1.5.2\cores\tiny/wiring.c:332
    millis_timer_millis = m;
  84:   80 93 65 00     sts 0x0065, r24 ; 0x800065 <millis_timer_millis>
  88:   90 93 66 00     sts 0x0066, r25 ; 0x800066 <millis_timer_millis+0x1>
  8c:   a0 93 67 00     sts 0x0067, r26 ; 0x800067 <millis_timer_millis+0x2>
  90:   b0 93 68 00     sts 0x0068, r27 ; 0x800068 <millis_timer_millis+0x3>
\Arduino15\packages\ATTinyCore\hardware\avr\1.5.2\cores\tiny/wiring.c:334
#ifndef CORRECT_EXACT_MICROS
    millis_timer_overflow_count++;
  94:   80 91 60 00     lds r24, 0x0060 ; 0x800060 <__DATA_REGION_ORIGIN__>
  98:   90 91 61 00     lds r25, 0x0061 ; 0x800061 <__DATA_REGION_ORIGIN__+0x1>
  9c:   a0 91 62 00     lds r26, 0x0062 ; 0x800062 <__DATA_REGION_ORIGIN__+0x2>
  a0:   b0 91 63 00     lds r27, 0x0063 ; 0x800063 <__DATA_REGION_ORIGIN__+0x3>
  a4:   01 96           adiw    r24, 0x01   ; 1
  a6:   a1 1d           adc r26, r1
  a8:   b1 1d           adc r27, r1
  aa:   80 93 60 00     sts 0x0060, r24 ; 0x800060 <__DATA_REGION_ORIGIN__>
  ae:   90 93 61 00     sts 0x0061, r25 ; 0x800061 <__DATA_REGION_ORIGIN__+0x1>
  b2:   a0 93 62 00     sts 0x0062, r26 ; 0x800062 <__DATA_REGION_ORIGIN__+0x2>
  b6:   b0 93 63 00     sts 0x0063, r27 ; 0x800063 <__DATA_REGION_ORIGIN__+0x3>
\Arduino15\packages\ATTinyCore\hardware\avr\1.5.2\cores\tiny/wiring.c:336
#endif
  }
  ba:   bf 91           pop r27
  bc:   af 91           pop r26
  be:   9f 91           pop r25
  c0:   8f 91           pop r24
  c2:   3f 91           pop r19
  c4:   2f 91           pop r18
  c6:   0f 90           pop r0
  c8:   0f be           out 0x3f, r0    ; 63
  ca:   0f 90           pop r0
  cc:   1f 90           pop r1
  ce:   18 95           reti
\Arduino15\packages\ATTinyCore\hardware\avr\1.5.2\cores\tiny/wiring.c:328
      f -= FRACT_MAX;
      m += MILLIS_INC + 1;
    }
    else
    {
      m += MILLIS_INC;
  d0:   02 96           adiw    r24, 0x02   ; 2
  d2:   a1 1d           adc r26, r1
  d4:   b1 1d           adc r27, r1
  d6:   d4 cf           rjmp    .-88        ; 0x80 <__vector_11+0x3c>

000000d8 <main>:
main():
\Arduino15\packages\ATTinyCore\hardware\avr\1.5.2\cores\tiny/wiring.c:1138
*/

  // Use the Millis Timer for fast PWM (unless it doesn't have an output).
  #if (TIMER_TO_USE_FOR_MILLIS == 0)
    #if defined(WGM01) // if Timer0 has PWM
      TCCR0A = (1<<WGM01) | (1<<WGM00);
  d8:   83 e0           ldi r24, 0x03   ; 3
  da:   80 bf           out 0x30, r24   ; 48
\Arduino15\packages\ATTinyCore\hardware\avr\1.5.2\cores\tiny/wiring.c:1141
    #endif
    #if defined(TCCR0B) //The x61 has a wacky Timer0!
      TCCR0B = (MillisTimer_Prescale_Index << CS00);
  dc:   83 bf           out 0x33, r24   ; 51
\Arduino15\packages\ATTinyCore\hardware\avr\1.5.2\cores\tiny/wiring.c:1162
    TCCR1A = 1<<WGM10;
    TCCR1B = (1<<WGM12) | (MillisTimer_Prescale_Index << CS10);
  #endif

  // this needs to be called before setup() or some functions won't work there
  sei();
  de:   78 94           sei
\Arduino15\packages\ATTinyCore\hardware\avr\1.5.2\cores\tiny/wiring.c:1169
  #ifndef DISABLEMILLIS
    // Enable the overflow interrupt (this is the basic system tic-toc for millis)
    #if defined(TIMSK) && defined(TOIE0) && (TIMER_TO_USE_FOR_MILLIS == 0)
      sbi(TIMSK, TOIE0);
    #elif defined(TIMSK0) && defined(TOIE0) && (TIMER_TO_USE_FOR_MILLIS == 0)
      sbi(TIMSK0, TOIE0);
  e0:   99 b7           in  r25, 0x39   ; 57
  e2:   91 60           ori r25, 0x01   ; 1
  e4:   99 bf           out 0x39, r25   ; 57
initToneTimerInternal():
\Arduino15\packages\ATTinyCore\hardware\avr\1.5.2\cores\tiny/wiring.c:972
    #elif (TIMER_TO_USE_FOR_TONE == 1 ) && defined(__AVR_ATtinyX7__)
      TCCR1A = (1<<COM1A1)|(1<<COM1B1)|(1<<WGM10);
      TCCR1B = (ToneTimer_Prescale_Index << CS10);
    #elif (TIMER_TO_USE_FOR_TONE == 1) // x4, x8, x313,
      // Use the Tone Timer for phase correct PWM
      TCCR1A = (1<<WGM10);
  e6:   91 e0           ldi r25, 0x01   ; 1
  e8:   9f bd           out 0x2f, r25   ; 47
\Arduino15\packages\ATTinyCore\hardware\avr\1.5.2\cores\tiny/wiring.c:973
      TCCR1B = (0<<WGM12) | (0<<WGM13) | (ToneTimer_Prescale_Index << CS10); //set the clock
  ea:   8e bd           out 0x2e, r24   ; 46
main():
\Arduino15\packages\ATTinyCore\hardware\avr\1.5.2\cores\tiny/wiring.c:1214
  #if defined( INITIALIZE_ANALOG_TO_DIGITAL_CONVERTER ) && INITIALIZE_ANALOG_TO_DIGITAL_CONVERTER
    #if defined(ADCSRA)
      // set a2d prescale factor
      // ADCSRA = (ADCSRA & ~((1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0))) | (ADC_ARDUINO_PRESCALER << ADPS0) | (1<<ADEN);
      // dude, this is being called on startup. We know that ADCSRA is 0! Why add a RMW cycle?!
      ADCSRA = (ADC_ARDUINO_PRESCALER << ADPS0) | (1<<ADEN);
  ec:   86 e8           ldi r24, 0x86   ; 134
  ee:   86 b9           out 0x06, r24   ; 6
  f0:   ff cf           rjmp    .-2         ; 0xf0 <main+0x18>

000000f2 <_exit>:
exit():
/home/jenkins-mingw32/workspace/avr-gcc-staging/label/Ubuntu14.04x64-mingw32/gcc-build/avr/avr25/libgcc/../../../../gcc/libgcc/config/avr/lib1funcs.S:2278
  f2:   f8 94           cli

000000f4 <__stop_program>:
__stop_program():
/home/jenkins-mingw32/workspace/avr-gcc-staging/label/Ubuntu14.04x64-mingw32/gcc-build/avr/avr25/libgcc/../../../../gcc/libgcc/config/avr/lib1funcs.S:2280
  f4:   ff cf           rjmp    .-2         ; 0xf4 <__stop_program>`

If i try with "arduino nano" board, the size is good in each case, it's why i open that issue here and not for arduino core.

Regards