Help with GCC issue
Not for the first time, changing the version of GCC I use to compile my code has broken something. The specific code is this:
esi = oldesi - 1; esi = argue((signed char *)accs - 1, (void *)(esp + 2), 1);where esi is declared as a global register variable thus:
register signed char *esi asm ("esi"); // Program pointerThe code always used to work, but now it doesn't; seemingly the compiler doesn't realise that esi is used inside argue(). I can fix it by inserting an artificial sequence point:
esi = oldesi - 1; printf(""); esi = argue((signed char *)accs - 1, (void *)(esp + 2), 1);Although this fix is effective, obviously I don't want to leave it in the code. What is the best way for me to workaround what appears to be a compiler bug?
0
Comments
-
Richard_Russell wrote: »I can fix it by inserting an artificial sequence point:
esi = oldesi - 1; printf(""); esi = argue((signed char *)accs - 1, (void *)(esp + 2), 1);
Although this fix is effective, obviously I don't want to leave it in the code. What is the best way for me to workaround what appears to be a compiler bug?volatile int seq; esi = oldesi - 1; for (seq=0; seq<1; seq++); esi = argue((signed char *)accs - 1, (void *)(esp + 2), 1);
although, interestingly, replacing the for statement with seq++; doesn't work.
Generally when I report this kind of behaviour at programming forums I get the response that my code is invoking Undefined Behaviour so I shouldn't expect it to work, but if that is the case why?
0 -
Richard_Russell wrote: »Generally when I report this kind of behaviour at programming forums I get the response that my code is invoking Undefined Behaviour so I shouldn't expect it to work, but if that is the case why?
I can leave the crude 'fix' in the code and hope for the best, despite not understanding either the cause or why the fix works. But it's not very satisfactory, especially in published Open Source software.
0 -
There's a tool called cppcheck which might possibly give an explanation when it finds code which it finds troublesome.0
-
There's a tool called cppcheck which might possibly give an explanation when it finds code which it finds troublesome.
So I wouldn't be at all surprised if cppcheck simply says what's this global register variable thing? That's not legal C!
0 -
I have kept quiet, because I don't understand what the snippet is trying to do.
You're setting ESI, then setting it again with no reference to what it was before, so ordinarily I would see the first line as completely superfluous - but I am expecting there is some hidden magic going on.0 -
Another possible issue is that you'll get heaps of warnings, which you'd prefer not to be bombarded with. Many will be false positives, I think, that being the nature of such static analyses. (The other modern open source C compiler, clang, also has some static analysis competence, and again it's possible the warnings will be helpfully informative.)
There's a good chance you're already aware of Compiler Explorer at godbolt.org which offers many versions of many compilers with many targets. It might be useful in exploring what you can do, or what diagnostics you can get, if you have a small enough test case to suit that tool. (I don't know what the capacity is.)0 -
if you have a small enough test case to suit that tool.
0 -
You're setting ESI, then setting it again with no reference to what it was before, so ordinarily I would see the first line as completely superfluous - but I am expecting there is some hidden magic going on.
The GCC docs refer to global register variables being "useful in programs such as programming language interpreters that have a couple of global variables that are accessed very often" which is exactly the case in BBC BASIC
I haven't reproduced here my entire post to the BBC BASIC forum, but there I also stated that "This code normally runs fine (on multiple platforms and CPU types)", to which I might have added "and multiple compilers". Neither GCC nor Clang (in multiple incarnations, for example built into tools like Android Studio and Apple's Xcode) issue any warnings.
Is there something in my code that you're concerned about, for example in possibly resulting in UB?
1 -
Richard_Russell wrote: »
As a professional C programmer you may disapprove of globals, as do I in ordinary circumstances, but as the GCC docs confirm, their use is quite normal in language interpreters. I wouldn't be at all surprised if Brandy uses globals for the same reason.
In any case, as you know the C code in question (like all of BBCSDL) was created semi-mechanically from the assembly language code of BB4W, and in assembly language everything is a global (I know that some 'high level' assemblers do support function calls with parameters and the like, but that's different from the machine-level programming I used to do).0 -
Fair enough. I completely missed that when I read your original post late at night, so half asleep I got the wrong impression. Sorry about that. My mind is also somewhat elsewhere, my big focus at the moment is doing my best to assist my son getting him ready to try for a choir school, so unsurprisingly very little has happened with Matrix Brandy (aside writing the Note Quiz, again to help with his music).1
-
very little has happened with Matrix Brandy...
I don't have any regrets: there are no burning ambitions for my software that I'm not going to be able to fulfil, nor anything crucial left in the 'to do' list.
0