SPIN variable shadowing suprise (flexspin)
I just scratched my head over a rather stupid mistake that I would have hoped the compiler to prevent me from making.
I pass the address of an instruction buffer to another cog. I called it mux (it's the MUX-register of an ADS1256 that I move through to acquire data). The expectation is that this buffer does not change during the other cogs operation, to avoid a needless copy.
I then observed changes in this buffer that where puzzling to me. Until I found that I also created a local variable of the same name
Excerpts from the code below:
INSTRUCTION_BUFFER_SIZE = 2 BUFFER_SIZE = 100 var BYTE mux[INSTRUCTION_BUFFER_SIZE] LONG read LONG write LONG buffer[BUFFER_SIZE * 2] obj term : "jm_fullduplexserial" adc : "buffered_ads1256" pub main | a, b, c, channel, timestamp, mux, value, rpos, wpos term.start(RX1, TX1, %0000, BR_TERM) read := BUFFER_SIZE adc.start(ADC_SDI, ADC_SDO, ADC_CLK, ADC_CS, ADC_DRDY, ADC_PDWN, @read) pinlow(TRIGGER) pinhigh(TRIGGER) adc.wreg(adc.DRATE, %00000011) ' 2.5SPS a := adc.rreg(adc.DRATE, 1) 'term.fstr1(string("ADC DRATE \%%8.8b\r\n"), a) ', b, c) mux[0] := (0 << 4) | (1 << 3) ' AIN-channel and AINCOM mux[1] := (1 << 4) | (1 << 3) ' AIN-channel and AINCOM adc.start_continuous_read(@mux, INSTRUCTION_BUFFER_SIZE) repeat timestamp, mux, value := adc.read_continuous term.fstr3(string("ICDAC %8.8x MUX 0x%2.2x: %8.8x\r\n"), timestamp, mux, value) 'term.fstr2(string("\r\nrpos: %d wpos: %d\r\n\r\n"), rpos, wpos) term.fstr3(string("MUX0 0x%2.2x MUX1 0x%2.2x, %d\r\n"), instruction_buffer[0], instruction_buffer[1], adc.available)
The problem is easily solved of course, I just renamed the buffer to instruction_buffer
and that's it.
However given that I'm not allowed to e.g. have variable and method of the same name, I wonder if this would be worth an error? Or at least warning, if it would break existing code.
Comments
If you pass -Wall on the flexspin command line you'll get a warning for this.
Neat! Found quite a few other questionable things I actually had wondered about (using multiple return values for example w/o declaring them).