Compile time macro trouble.

Dan Olson via digitalmars-d-ldc digitalmars-d-ldc at puremagic.com
Sun Mar 13 07:28:56 PDT 2016


Taylor Hillegeist <taylorh140 at gmail.com> writes:

> So here: https://dlang.org/pretod.html
> there is a lovely section about how doing normal functions is
> basically a processor macro.
>
> The C Preprocessor Way:
> #define X(i)	((i) = (i) / 3)
>
> The D Way:
> int X(ref int i) { return i = i / 3; }
>
> However I am running into troubles with the truthfulness of this on
> LDC for thumb cortex-m0plus.
>
> MKL25Z.d
> line 5243:
> uint32_t SIM_SOPT2_TPMSRC(uint32_t x)  {return ((x <<
> SIM_SOPT2_TPMSRC_SHIFT) & SIM_SOPT2_TPMSRC_MASK);}
>
> start.d
> line 28:     int value = SIM_SOPT2_TPMSRC(1);
> 0x0000046E 2001      MOVS     r0,#0x01
> 0x00000470 9004      STR      r0,[sp,#0x10]
> 0x00000472 F000F8ED  BL.W     MKL25Z4.SIM_SOPT2_TPMSRC (0x00000650)
> //branch to crap
> 0x00000476 9005      STR      r0,[sp,#0x14]
> ...
> 0x00000650 B082      DCW      0xB082 < the crap
> 0x00000652 4601      DCW      0x4601
> 0x00000654 9001      DCW      0x9001
> 0x00000656 0600      DCW      0x0600
> 0x00000658 2203      DCW      0x2203
>
> Is this an error on ldc's part or do i need to do something more to
> get the correct result?
>
> I compiled with:
> ldc2.exe -output-o -march=thumb -mcpu=cortex-m0plus -mtriple=arm-linux
> -g -c main.d start.d MKL25Z4.d
> and linked with:
> arm-unknown-linux-gnueabi-ld.exe -T mkl25z4.ld --gc-sections  start.o
> MKL25Z4.o main.o  -o ldoutput.elf
>
>
> And yes there is a little bit of information missing i've uploaded
> some files here.
>
> https://github.com/taylorh140/DLANG_FRDM_KL25Z
>
> I'm just trying to get more comfortable with working on
> microcontrollers with d. The good news is that I got a simple example
> running. An led blinking three colors!  I would like advice on the
> best way to handle direct interaction with registers, but maybe that's
> best for another post.

Taylor, this should help:

Change your function to a template, and then it will get inlined (add the
extra, empty parens).

uint32_t SIM_SOPT2_TPMSRC()(uint32_t x)  {
  return ((x << SIM_SOPT2_TPMSRC_SHIFT) & SIM_SOPT2_TPMSRC_MASK);
}

I think LDC only inlines regular functions when they are in the same
module.  But templates get inlined across modules.

Make sure you are using LDC ltsmaster branch since it has some needed
improvements for ARM, although thumb is not well tested except on iOS
(but that is a different fork).
--
Dan


More information about the digitalmars-d-ldc mailing list