Am Sun, 01 Feb 2015 21:48:40 -0800 schrieb Walter Bright <newshou...@digitalmars.com>:
> On 2/1/2015 9:21 PM, Daniel Murphy wrote: > > "Walter Bright" wrote in message > > news:mam6qe$15nu$1...@digitalmars.com... > >> > We also need a pragma(address) to complement pragma(mangle). > >> > >> What would that do? > > > > It would allow naming a memory address, similar to using .org in > > assembly. > > > > eg > > pragma(address, 0x0025) > > shared ubyte PORTB; > > static assert(&PORTB == cast(ubyte*)0x0025); > > > > This is a much nicer version of C's > > #define PORTB (*(volatile unsigned char *)0x0025) > > That's what I suspected :-) > > struct Ports { > static ubyte B() { return volatileLoad(cast(ubyte *)0x0025); } > static void B(ubyte value) { volatileStore(cast(ubyte *)0x0025, > value); } } > > ... > Ports.B = 7; > foo(Ports.B); > > gets the job done. No, it doesn't even come close. * Ports.B += 7 doesn't work. In order to implement it you need a Volatile!ubyte wrapper, return by ref and avoid some compiler bugs * You do need force-inline to produce halfway decent code * You also need to enable backend optimization to produce decent code This has been discussed before and the best way to express this in the language is @property ref @attribute("inlineonly") Volatile!ubyte PORTB() {return cast((Volatile!ubyte)*)(0x0025)} You need cross-module inlining, a way to avoid actually generating a callable function (inlineonly =/= forceinline) to avoid bloat and optimization must be enabled. Cross-module inlining is not supported in GDC and not trivial to implement. Also the code looks ugly. pragma address is: * Easy to implement (https://github.com/D-Programming-microD/GDC/commit/a4027b6d9c53a186c142244553861af8cce5492f) * A logical, consistent paradigm (if there are extern variables with specific names, why no variables with specific address) and extension of pragma(mangle) * Easy to use * Enforces that the address is a compile time constant, produce perfect ASM code even without optimization * No need to define a wrapper function, with all the consequences that hack requires (inlineonly) * Apparently already in other languages Given that we can implement pragmas as compiler backend vendors the bar to include pragmas into dmd also shouldn't be too high. Otherwise it'll just be implemented as a compiler dependent pragma.