Electronic – MPLAB XC8 + PIC18 + external memory = Error 712 (can’t generate code for this expression)

picxc8

I'm currently having a bad time trying to use an external RAM chip (512 kB) with a PIC18F8720 and the Microchip XC8 compiler (v1.35, PRO mode).

Whenever I try to write some code to modify a variable in the external memory, the linker throws an error (712): "can't generate code for this expression". This doesn't happen when reading far variables: only when writing into them.

This error is reproducible, even in this small example program:

#include <xc.h>
#include <stdio.h>

far int intArray[10];
far int intVariable;

void main(void) {
    intVariable = 2; // --> ERROR 712
    intArray[1] = 3; // --> ERROR 712
    printf("%d\n", intVariable);
    printf("%d\n", intArray[0]);
}

The compilation+linking commands are generated by MPLAB X v3.20:

"/opt/microchip/xc8/v1.35/bin/xc8" –pass1 –chip=18F8720 -Q -G –double=24 –float=24 –emi=byteselect –ram=default,+20000-9FFFF –opt=default,+asm,+asmfile,-speed,+space,-debug –addrqual=require –mode=pro -P -N255 –warn=-3 –asmlist –summary=default,-psect,-class,+mem,-hex,-file –output=default,-inhx032 –runtime=default,+clear,+init,-keep,-no_startup,-download,+config,+clib,-plib –output=-mcof,+elf:multilocs –stack=compiled:auto:auto:auto "–errformat=%f:%l: error: (%n) %s" "–warnformat=%f:%l: warning: (%n) %s" "–msgformat=%f:%l: advisory: (%n) %s" -obuild/default/production/main.p1 main.c

"/opt/microchip/xc8/v1.35/bin/xc8" –chip=18F8720 -G -mdist/default/production/Test01.X.production.map –double=24 –float=24 –emi=byteselect –ram=default,+20000-9FFFF –opt=default,+asm,+asmfile,-speed,+space,-debug –addrqual=require –mode=pro -P -N255 –warn=-3 –asmlist –summary=default,-psect,-class,+mem,-hex,-file –output=default,-inhx032 –runtime=default,+clear,+init,-keep,-no_startup,-download,+config,+clib,-plib –output=-mcof,+elf:multilocs –stack=compiled:auto:auto:auto "–errformat=%f:%l: error: (%n) %s" "–warnformat=%f:%l: warning: (%n) %s" "–msgformat=%f:%l: advisory: (%n) %s" –memorysummary dist/default/production/memoryfile.xml -odist/default/production/Test01.X.production.elf build/default/production/main.p1

Compilation output:

main.c:9: error: (712) can't generate code for this expression
main.c:10: error: (712) can't generate code for this expression
Microchip MPLAB XC8 C Compiler (PRO Mode) V1.35
Build date: Jul 7 2015
Part Support Version: 1.35
Copyright (C) 2015 Microchip Technology Inc.
License type: Node Configuration

What am I doing wrong? The compiler output doesn't help much, as the error is way too generic to give any clue.

Best Answer

I was just having a play around with this and it seems like something you should report to Microchip but I have found a workaround that might be useful in the interim. The problem only seems to occur with multi-byte variables outside an array or when trying to access the first element of an array. The following code compiles OK and seems to generate plausible assembler output although I didn't have a chip with external RAM to test:

#include <xc.h>
#include <stdio.h>
#include <stdint.h>

far uint8_t intVariable;
far int intArray[1000];

void main(void) {
    intVariable = 1;
//    intArray[0] = 3; <<- Causes an error
    intArray[1] = 3;
    intArray[999] = 3;
    printf("%d\n", intVariable);
    printf("%d\n", intArray[1]);
}

I suspect based on that it tries to perform some optimization on writing to a multi-byte variable that falls down, but when inside an array it generates the full code to calculate the offset that seems to work OK. Note that I only have the free compiler so the pro edition may optimize out the constant array access, but I'd imagine changing the index to a variable would fix that if it does happen.

When I compared my compiler / linker options to yours under the XC8 compiler optimizations I also had to enable "Instruction Invariant Mode" for it to work. Hopefully that might get you up and running and it might be a good additional test case to submit to Microchip.