{"action":"created","issue":{"url":"https://api.github.com/repos/mbedmicro/mbed/issues/863","labels_url":"https://api.github.com/repos/mbedmicro/mbed/issues/863/labels{/name}","comments_url":"https://api.github.com/repos/mbedmicro/mbed/issues/863/comments","events_url":"https://api.github.com/repos/mbedmicro/mbed/issues/863/events","html_url":"https://github.com/mbedmicro/mbed/issues/863","id":55460841,"number":863,"title":"The code is optimized when use operator= for DigitalOut","user":{"login":"leibin2014","id":9861776,"avatar_url":"https://avatars.githubusercontent.com/u/9861776?v=3","gravatar_id":"","url":"https://api.github.com/users/leibin2014","html_url":"https://github.com/leibin2014","followers_url":"https://api.github.com/users/leibin2014/followers","following_url":"https://api.github.com/users/leibin2014/following{/other_user}","gists_url":"https://api.github.com/users/leibin2014/gists{/gist_id}","starred_url":"https://api.github.com/users/leibin2014/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/leibin2014/subscriptions","organizations_url":"https://api.github.com/users/leibin2014/orgs","repos_url":"https://api.github.com/users/leibin2014/repos","events_url":"https://api.github.com/users/leibin2014/events{/privacy}","received_events_url":"https://api.github.com/users/leibin2014/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/mbedmicro/mbed/labels/question","name":"question","color":"cc317c"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":8,"created_at":"2015-01-26T09:20:52Z","updated_at":"2015-01-27T05:46:12Z","closed_at":null,"body":"According to the handbook I use a lot of operator= for setting chipselect, reset pins. The code works if I set optimization flag to -O0 or -O1, but sometimes can't work if set to -O2. Take below code as an example:\r\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r\nclass ILI9341: public SPI\r\n{\r\npublic:\r\n\tILI9341(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset, PinName dc, PinName backlight);\r\n\t~ILI9341();\r\n\tvoid configureRectangle(uint32_t x1, uint32_t y1, uint32_t x2, uint32_t y2);\r\n\r\nprivate:\r\n\tDigitalOut cs;\r\n\tDigitalOut reset;\r\n\tDigitalOut dc;\r\n};\r\n\r\nvoid ILI9341::configureRectangle(uint32_t x1, uint32_t y1, uint32_t x2, uint32_t y2)\r\n{\r\n\tcs = 0;\r\n\tADDRESS_SET(x1,y1,x2,y2);   \r\n\tdc = 1;\r\n}\r\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r\n\r\nThe code cs = 0; and dc = 1 in function configureRectangle is optimized if using -O2 as compiler flag.\r\nIs there a way to avoid this problem if I want to use -O2 flag and also the operator= for DigitalOut?\r\n\r\nMy compiler is arm-none-eabi-gcc version 4.9.3."},"comment":{"url":"https://api.github.com/repos/mbedmicro/mbed/issues/comments/71592982","html_url":"https://github.com/mbedmicro/mbed/issues/863#issuecomment-71592982","issue_url":"https://api.github.com/repos/mbedmicro/mbed/issues/863","id":71592982,"user":{"login":"adamgreen","id":1060664,"avatar_url":"https://avatars.githubusercontent.com/u/1060664?v=3","gravatar_id":"","url":"https://api.github.com/users/adamgreen","html_url":"https://github.com/adamgreen","followers_url":"https://api.github.com/users/adamgreen/followers","following_url":"https://api.github.com/users/adamgreen/following{/other_user}","gists_url":"https://api.github.com/users/adamgreen/gists{/gist_id}","starred_url":"https://api.github.com/users/adamgreen/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/adamgreen/subscriptions","organizations_url":"https://api.github.com/users/adamgreen/orgs","repos_url":"https://api.github.com/users/adamgreen/repos","events_url":"https://api.github.com/users/adamgreen/events{/privacy}","received_events_url":"https://api.github.com/users/adamgreen/received_events","type":"User","site_admin":false},"created_at":"2015-01-27T05:46:12Z","updated_at":"2015-01-27T05:46:12Z","body":"I built your sample code with the following g++ command line:\r\n```\r\narm-none-eabi-g++ -mcpu=cortex-m0 -mthumb -c -g -fno-common -fmessage-length=0 -Wall -fno-exceptions -ffunction-sections -fdata-sections -fomit-frame-pointer -MMD -MP -DNDEBUG -O2 -DTARGET_NRF51822 -DTARGET_M0 -DTARGET_CORTEX_M -DTARGET_NORDIC -DTARGET_NRF51822_MKIT -DTARGET_MCU_NRF51822 -DTARGET_MCU_NORDIC_16K -DTOOLCHAIN_GCC_ARM -DTOOLCHAIN_GCC -D__CORTEX_M0 -DARM_MATH_CM0 -DMBED_BUILD_TIMESTAMP=1422336272.34 -D__MBED__=1  -std=gnu++98 -fno-rtti -I. -I./mbed -I./mbed/TARGET_NRF51822 -I./mbed/TARGET_NRF51822/TARGET_NORDIC -I./mbed/TARGET_NRF51822/TARGET_NORDIC/TARGET_MCU_NRF51822 -I./mbed/TARGET_NRF51822/TARGET_NORDIC/TARGET_MCU_NRF51822/TARGET_NRF51822_MKIT -I./mbed/TARGET_NRF51822/TARGET_NORDIC/TARGET_MCU_NRF51822/Lib -I./mbed/TARGET_NRF51822/TARGET_NORDIC/TARGET_MCU_NRF51822/Lib/s110_nrf51822_7_1_0 -I./mbed/TARGET_NRF51822/TARGET_NORDIC/TARGET_MCU_NRF51822/Lib/s110_nrf51822_7_1_0/s110_nrf51822_7.1.0_API -I./mbed/TARGET_NRF51822/TARGET_NORDIC/TARGET_MCU_NRF51822/Lib/s110_nrf51822_7_1_0/s110_nrf51822_7.1.0_API/include -I./mbed/TARGET_NRF51822/TARGET_NORDIC/TARGET_MCU_NRF51822/Lib/nrf-sdk -I./mbed/TARGET_NRF51822/TARGET_NORDIC/TARGET_MCU_NRF51822/Lib/nrf-sdk/app_common -I./mbed/TARGET_NRF51822/TARGET_NORDIC/TARGET_MCU_NRF51822/Lib/nrf-sdk/sd_common -I./mbed/TARGET_NRF51822/TOOLCHAIN_GCC_ARM  -o main.o main.cpp\r\n```\r\n\r\nThat produced the expected assembly code for main where setup was actually inlined:\r\n```\r\n00016198 <main>:\r\n   16198:\t4b02      \tldr\tr3, [pc, #8]\t; (161a4 <main+0xc>)\r\n   1619a:\t68da      \tldr\tr2, [r3, #12]\r\n   1619c:\t685b      \tldr\tr3, [r3, #4]\r\n   1619e:\t6013      \tstr\tr3, [r2, #0]\r\n   161a0:\te7fe      \tb.n\t161a0 <main+0x8>\r\n   161a2:\t46c0      \tnop\t\t\t; (mov r8, r8)\r\n   161a4:\t200020fc \t.word\t0x200020fc\r\n```\r\n\r\nWhere did you get your compiler flags?  You should look for support from whomever gave you those options to use for the NRF51822 device with GCC.  They aren't the recommended ones.\r\n\r\nThe flags I used above were from a project exported from the mbed online compiler for GCC_ARM so they are the recommended ones.  I did change the -Os optimization flag to -O2.\r\n"}}