Anomily with XC8 - Page 1
Forum section: electronics / general

author: bxdobs
author_rank: Newbie
timestamp: August 06, 2023, 12:53:59 am
content: W7pro 64b using MPLAB v5.3 with XC8 v2.10Project using a PIC12F629The following C code, specifically lCase(), isn't being compiled properly by XC8:Code:char uCase(char cC){return (cC & ccUCMask); // 0xDF}char lCase(char cC){return (cC | ccLCMask); // 0x20}results in:Code:02D81283BCF0x03,502D900B4MOVWF0x34,F02DA0834MOVF0x34,W02DB39DFANDLW0xDF02DC0008RETURNCode:02DD 0008 RETURNThis was expected to provide:Code:02D81283BCF0x03,502D900B4MOVWF0x34,F02DA0834MOVF0x34,W02DB39DFANDLW0xDF02DC0008RETURNCode:02DD1283BCF0x03,502DE00B4MOVWF0x34,F02DF0834MOVF0x34,W02C039DFIORLW0x2002C10008RETURN2 attempts at forcing the compiler to provide a working solution by explicitly using a local variableThis one failsCode:char lCase(char cC){char cX = cC;return (cX | ccLCMask);}This C Code works but returns way too much machine codeCode:char lCase(char cC){char cX = cC;cX = cX | ccLCMask;return (cX);}Code:02C11283BCF0x03,502C200B5MOVWF0x35,F02C30835MOVF0x35,W02C400B4MOVWF0x34,F02C50834MOVF0x34,W02C600B6MOVWF0x36,F02C70836MOVF0x36,W02C83820IORLW0x2002C900B4MOVWF0x34,F02CA0834MOVF0x34,W02CB00B6MOVWF0x36,F02CC0008RETURNAt a high level, it would appear the "|" isn't being compiled properly if included within the return() statementI could forcibly use asm("IORLW 0x20"); but wasn't expecting to 2nd guess the compiler.Thoughts?

author: bxdobs
author_rank: Newbie
timestamp: August 06, 2023, 01:25:41 am
content: So I have resolved this issue using Predefined Macros ... rather than try and understand the Compiler failureCode:#define uCase(x) (x = x & ccUCMask)#define lCase(x) (x = x | ccLCMask)I have confirmed my code is now compiling with what is expected ... PLUS ... not wasting time and space with Calls/Returns

author: Shonky
author_rank: Frequent Contributor
timestamp: August 06, 2023, 01:57:00 am
content: Works for me here with xc8 2.41Code:_lCase:;incstack = 0callstack 7; Regs used in _lCase: [wreg+status,2];lCase@cC stored from wregbcfstatus, 5;RP0=0, select bank0movwf(lCase@cC)line12l623:;xc8test.c: 10: char lCase(char cC);xc8test.c: 11: {;xc8test.c: 12: return (cC | (0x20));movf(lCase@cC),wiorlw020hline13l6:returnI'm not a fan of this though:Code:#define uCase(x) (x = x & ccUCMask)#define lCase(x) (x = x | ccLCMask)Making a define that looks like a function but changes parameters is a no no in my mind. Also you should always parenthesise macro parameters IMO even if you're sure you won't ever cause a problem.Code:#define uCase(x) ((x) & ccUCMask)#define lCase(x) ((x) | ccLCMask)x = lCase(x);x = uCase(x);

author: DavidAlfa
author_rank: Super Contributor
timestamp: August 06, 2023, 03:20:29 am
content: What optimization level? Remember O2 is free.Os will produce the optimized result you would expect with manual assembly, but it's no longer free.Also what values are you feeding to the function?The compiler migh be smart enough to detect the variable needs zero processing, for example if you're or'ing 0xF0 with 0x20.You could skip this optimization by forcing the variable to be treated as volatile, then check the output asm.Code:char lCase(char cC){return ((volatile char) cC | ccLCMask); // 0x20}

author: Shonky
author_rank: Frequent Contributor
timestamp: August 06, 2023, 03:35:17 am
content: -Os and -O3 both produce the correct (and the same) code in 2.41

author: JPortici
author_rank: Super Contributor
timestamp: August 06, 2023, 04:34:01 am
content: Quote from: DavidAlfa on August 06, 2023, 03:20:29 am-Os wasneverfreei wonder if the "problem" is integer promotion that could probably be solved by casting the result or the arguments. I don't have XC8 on this machine to test. Doesn't the compiler nag you about fitting a short into a char?Anyway a better way to look at the disassembly is to post the disassembly listing file (build for debug and go to window->debug->output->disassembly listing file) because there is zero context here

author: DavidAlfa
author_rank: Super Contributor
timestamp: August 06, 2023, 04:46:18 am
content: Quote from: JPortici on August 06, 2023, 04:34:01 amI know, I know.I mean it's no longer free (After you set Os), in the context that O2 is.Yeah using chars seems wrong, I only use them for char..acters!But I guess it works anyways if using hex literals.