1

Here is a minimal reproducible example:

/*
 * Example for Xtensa xt-xcc compiler "error: attempt to move .org backwards".
 */

struct k_dict
{
    char *m_name_dict;
    char *p_name_dict;
    union
    {
        float f_value;
        int i_value;
        char* s_value;
    };
};

struct k_mode
{
    const char*  m_name;
          int    p_number;
    const struct k_dict *p;
    const struct k_dict pars[];
};

struct k_dict P1[] =
{
    {.m_name_dict="M1", .p_name_dict="P1", .s_value="string"},
    {.m_name_dict="M2", .p_name_dict="P2", .i_value=5       },
    {.m_name_dict="M3", .p_name_dict="P3", .f_value=48.0    }
};

struct k_mode mode_default =
{
    .m_name   = "default",
    .p_number = 1,
    .p        = P1,
    .pars =
    {
        {.m_name_dict = "m", .p_name_dict="p", .s_value="s"}
    }
};


int main( int argc, char **argv )
{
    return 0;
}

Compiling this using Tensilica Xtensa xt-xcc compiler gives assembler error:

"xt-xcc" -c -g -O0 -std=c11 -fmessage-length=0 -DPROC_hifi3_tv_car_5 -DCONFIG_hifi3_tv_car_5 --xtensa-system=hifi3_tv_car_5/config --xtensa-core=hifi3_tv_car_5 --xtensa-params= "\"HelloWorld/main.c\"" -o "\"HelloWorld/bin/hifi3_tv_car_5/Debug/main.o\"" 
/Temp/cc0s#6c7c.a32040: Assembler messages:
/Temp/cc0s#6c7c.a32040:39: Error: attempt to move .org backwards
xt-xcc ERROR: XtensaTools/bin/xt-as.exe returned non-zero status 1   

Where could this come from?

Note: GCC works fine.

Danijel
  • 8,198
  • 18
  • 69
  • 133
  • Is `xt-as` a build of GAS (from GNU Binutils) as a cross-assembler, or is it Xtensa's own assembler? – Peter Cordes Jul 23 '20 at 16:36
  • As the manual says: "it's GNU assembler configured for Xtensa architecture.". – Danijel Jul 23 '20 at 16:41
  • And also: "Unlike older assemblers, `xt-as` is designed to assemble a source program in one pass of the source file. This has a subtle impact on the `.org` directive." – Danijel Jul 23 '20 at 16:42
  • GAS in general has always been a one-pass assembler; I think they're comparing it earlier non-GAS assemblers (for other ISAs if there aren't others for Xtensa). You didn't link anything about the xcc / xt-as toolchain, so I still don't know which manual you're quoting, for this ISA I've never used. But anyway, now we know it's normal GAS. I forget if GAS usually allows seeking backwards with `.org`, I only remember NASM. I don't know why the C compiler would be emitting multiple `.org` directives in the first place. – Peter Cordes Jul 23 '20 at 16:52
  • Clearly this isn't quite a [mcve], there's only one `#include` in the whole thing, so no way for one header to include the others. But presumably it compiles if you just put everything in one `.c` file. – Peter Cordes Jul 23 '20 at 16:53
  • What is `struct mode`? Is that a typo? – Erik Eidt Jul 23 '20 at 18:13
  • 1
    Many compiler systems use intermediate files including a temporary assembly source file. Commonly you can provide options to keep these temporary files, please try this. Then look into the intermediate assembly source to investigate further. – the busybee Jul 24 '20 at 06:26
  • Added a minimal example. – Danijel Jul 24 '20 at 09:55
  • Seems like C11 is not supported even there is no warning reported by the compiler when the `c11`flag is added. From the manual: "Support for the C11 standard can be enabled with the `-std=c11` option only with the Clang front end. ... Starting with the RG-2017.7 release, XCC includes an alternative compiler front end based on Clang version 3.4 from the LLVM project. In future versions, the Clang front end will replace GCC. The Clang front end is selected by using the `-clang` compiler option." Using clang the above code compiles fine. – Danijel Jul 24 '20 at 12:00

2 Answers2

3

Seems like C11 is not supported by xt-xcc, even there is no warning reported by the compiler when the -std=c11 flag is added.

From the Xtensa manual: "Support for the C11 standard can be enabled with the -std=c11 option only with the Clang front end. (...) Starting with the RG-2017.7 release, XCC includes an alternative compiler front end based on Clang version 3.4 from the LLVM project. In future versions, the Clang front end will replace GCC. The Clang front end is selected by using the -clang compiler option."

Using Clang the above code compiles fine.

Danijel
  • 8,198
  • 18
  • 69
  • 133
2

The issue comes from the code generated for initialization of mode_default:

        .data
        .org 0x0
        .align  16
        .global mode_default
        .type   mode_default, @object
        .size   mode_default, 12
mode_default:   # 0x0
        .long   .L_g_7
        # offset 4
        .long   1
        .long   P1 +0
        .long   .L_g_8
        .long   .L_g_9
        .long   .L_g_10
        # end of initialization for mode_default
        .org 0x10

and it looks like a bug in the compiler. Interestingly, I observe the same issue with both xt-xcc and xt-xcc -clang.

jcmvbkbc
  • 723
  • 1
  • 4
  • 5
  • Thanks. I haven't used `xt-xcc -clang`, instead I use `xt-clang` directly. – Danijel Jul 28 '20 at 06:28
  • `xt-xcc -clang` and `xt-clang` are different: first uses the old xcc backend based on Open64, second is a new development based on clang + LLVM. – jcmvbkbc Jul 29 '20 at 17:12