www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Placing variable/array in a particular section

reply "Jens Bauer" <doctor who.no> writes:
Today I finally succeeded in building my first "Hello World" D 
program (after fixing the endian problem).

Is there a way of setting the target section for a variable or an 
array ?

Eg. the equivalent way of doing this using gcc is:

__attribute__((section(".isr_vector"))) VectorFunc g_pfnVectors[] 
= { ... };

-I need this functionality, because on microcontrollers, it's 
necessary to control where in RAM / flash memory the data is 
written.

I could of course do this using C or assembly language, but I'd 
like to use pure D only, if possible.
Apr 03 2015
parent reply Rikki Cattermole <alphaglosined gmail.com> writes:
On 4/04/2015 2:12 a.m., Jens Bauer wrote:
 Today I finally succeeded in building my first "Hello World" D program
 (after fixing the endian problem).

 Is there a way of setting the target section for a variable or an array ?

 Eg. the equivalent way of doing this using gcc is:

 __attribute__((section(".isr_vector"))) VectorFunc g_pfnVectors[] = {
 ... };

 -I need this functionality, because on microcontrollers, it's necessary
 to control where in RAM / flash memory the data is written.

 I could of course do this using C or assembly language, but I'd like to
 use pure D only, if possible.
Supposedly gdc supports it. [0] http://wiki.dlang.org/GDC/Using_GDC Extensions->Attributes [1] https://gcc.gnu.org/onlinedocs/gcc/Variable-Attributes.html#Variable-Attributes
Apr 03 2015
parent reply "Jens Bauer" <doctor who.no> writes:
On Friday, 3 April 2015 at 13:37:50 UTC, Rikki Cattermole wrote:
 On 4/04/2015 2:12 a.m., Jens Bauer wrote:
 Is there a way of setting the target section for a variable or 
 an array ?
Supposedly gdc supports it. [0] http://wiki.dlang.org/GDC/Using_GDC Extensions->Attributes [1] https://gcc.gnu.org/onlinedocs/gcc/Variable-Attributes.html#Variable-Attributes
I tried to use some of the examples, but I got the following error: src/start.d:7:10: error: module attribute is in file 'gcc/attribute.d' which cannot be read import gcc.attribute; ^ -Am I missing some files somewhere ?
Apr 03 2015
parent reply "Jens Bauer" <doctor who.no> writes:
On Friday, 3 April 2015 at 13:58:21 UTC, Jens Bauer wrote:
 On Friday, 3 April 2015 at 13:37:50 UTC, Rikki Cattermole wrote:
 On 4/04/2015 2:12 a.m., Jens Bauer wrote:
 Is there a way of setting the target section for a variable 
 or an array ?
Supposedly gdc supports it. [0] http://wiki.dlang.org/GDC/Using_GDC Extensions->Attributes [1] https://gcc.gnu.org/onlinedocs/gcc/Variable-Attributes.html#Variable-Attributes
I tried to use some of the examples, but I got the following error: src/start.d:7:10: error: module attribute is in file 'gcc/attribute.d' which cannot be read import gcc.attribute; ^
Uhm, it seems that druntime is required for that; unfortunately it's not ported to the Cortex-M platform. I found the attribute.d file and tried a quick copy-and-paste, but no luck there.
Apr 03 2015
parent reply Rikki Cattermole <alphaglosined gmail.com> writes:
On 4/04/2015 3:08 a.m., Jens Bauer wrote:
 On Friday, 3 April 2015 at 13:58:21 UTC, Jens Bauer wrote:
 On Friday, 3 April 2015 at 13:37:50 UTC, Rikki Cattermole wrote:
 On 4/04/2015 2:12 a.m., Jens Bauer wrote:
 Is there a way of setting the target section for a variable or an
 array ?
Supposedly gdc supports it. [0] http://wiki.dlang.org/GDC/Using_GDC Extensions->Attributes [1] https://gcc.gnu.org/onlinedocs/gcc/Variable-Attributes.html#Variable-Attributes
I tried to use some of the examples, but I got the following error: src/start.d:7:10: error: module attribute is in file 'gcc/attribute.d' which cannot be read import gcc.attribute; ^
Uhm, it seems that druntime is required for that; unfortunately it's not ported to the Cortex-M platform. I found the attribute.d file and tried a quick copy-and-paste, but no luck there.
Yeah, contact the GDC guys, they should be able to help you. Either the GDC newsgroup or as an issue on the compiler's project. Atleast at this point, keep an open mind. Think of what you are doing as testing the current state :)
Apr 03 2015
parent reply "Jens Bauer" <doctor who.no> writes:
On Saturday, 4 April 2015 at 02:57:22 UTC, Rikki Cattermole wrote:
 On 4/04/2015 3:08 a.m., Jens Bauer wrote:
 src/start.d:7:10: error: module attribute is in file 
 'gcc/attribute.d' which cannot be read
   import gcc.attribute;
          ^
Uhm, it seems that druntime is required for that; unfortunately it's not ported to the Cortex-M platform. I found the attribute.d file and tried a quick copy-and-paste, but no luck there.
Correction: minlibd *is* a druntime port; just a minimal one. However, I'm not sure this feature is supported yet. Timo Sintonen did a lot of great work; perhaps getting this detail supported might not require much sweat. ;)
 Yeah, contact the GDC guys, they should be able to help you.
 Either the GDC newsgroup or as an issue on the compiler's 
 project.
A-ha! I just discovered this page... <http://www.digitalmars.com/NewsGroup.html>
 Atleast at this point, keep an open mind. Think of what you are 
 doing as testing the current state :)
I will, though my impression until now, is that the compiler is quite mature. (I accept that there will always be some minor issues when moving to a new system, but that does not drag down the quality of the compiler).
Apr 04 2015
parent reply Johannes Pfau <nospam example.com> writes:
Am Sat, 04 Apr 2015 10:38:44 +0000
schrieb "Jens Bauer" <doctor who.no>:

 On Saturday, 4 April 2015 at 02:57:22 UTC, Rikki Cattermole wrote:
 On 4/04/2015 3:08 a.m., Jens Bauer wrote:
 src/start.d:7:10: error: module attribute is in file 
 'gcc/attribute.d' which cannot be read
   import gcc.attribute;
          ^
Uhm, it seems that druntime is required for that; unfortunately it's not ported to the Cortex-M platform. I found the attribute.d file and tried a quick copy-and-paste, but no luck there.
Correction: minlibd *is* a druntime port; just a minimal one. However, I'm not sure this feature is supported yet. Timo Sintonen did a lot of great work; perhaps getting this detail supported might not require much sweat. ;)
 Yeah, contact the GDC guys, they should be able to help you.
 Either the GDC newsgroup or as an issue on the compiler's 
 project.
A-ha! I just discovered this page... <http://www.digitalmars.com/NewsGroup.html>
 Atleast at this point, keep an open mind. Think of what you are 
 doing as testing the current state :)
I will, though my impression until now, is that the compiler is quite mature. (I accept that there will always be some minor issues when moving to a new system, but that does not drag down the quality of the compiler).
It's possible to use gcc.attribute with custom mini-runtimes. You need the gcc/attribute.d file but you can simply copy/paste it from druntime[1], there are no dependencies. I'll push support for the section attribute in 1~2 hours. (waiting for the testsuite ;-) [1] https://github.com/D-Programming-GDC/GDC/blob/master/libphobos/libdruntime/gcc/attribute.d
Apr 05 2015
next sibling parent "Jens Bauer" <doctor who.no> writes:
On Sunday, 5 April 2015 at 10:16:10 UTC, Johannes Pfau wrote:
 On 4/04/2015 3:08 a.m., Jens Bauer wrote:
It's possible to use gcc.attribute with custom mini-runtimes. You need the gcc/attribute.d file but you can simply copy/paste it from druntime[1], there are no dependencies.
After having done a number of attribute tests for a few hours yesterday, I think minlibd has all the attribute support that it possibly can. It's attribute.d is identical to the standard runtime file.
 I'll push support for the section attribute in 1~2 hours. 
 (waiting for the testsuite ;-)
That will be absolutely awesome - and also a big breakthrough. :) I will test and provide feedback as soon as I can. I've added a comment to [Bug 172] explaining the importance of having the weak alias support as well. Of course, section is the most important, but weak alias is also widely used with exception vectors. If attribute support for this is also implemented, I think there's no excuse anymore for using D on microcontrollers. ;)
Apr 05 2015
prev sibling parent "Jens Bauer" <doctor who.no> writes:
On Sunday, 5 April 2015 at 10:16:10 UTC, Johannes Pfau wrote:
 I'll push support for the section attribute in 1~2 hours. 
 (waiting for the testsuite ;-)

 [1] 
 https://github.com/D-Programming-GDC/GDC/blob/master/libphobos/libdruntime/gcc/attribute.d
I've made a couple of tests and it all works as expected... The snippet below builds without problems (except for the expected error; confirming that I have gotten the immutable part right). In addition, I've checked the disassembly and it all checks out fine. import gcc.attribute; //alias extern(C) void function() VectorFunc; // attribute("section",(".isr_vector")) immutable VectorFunc [100] functions = [ cast(VectorFunc) 0x10001000, &Reset_Handler, cast(VectorFunc)0xfadeface ]; /* works */ // attribute("section",".isr_vector") immutable VectorFunc [100] functions = [ cast(VectorFunc) 0x10001000, &Reset_Handler, cast(VectorFunc)0xfeedface ]; /* works */ alias extern(C) immutable void function() VectorFunc; attribute("section",".isr_vector") VectorFunc [100] g_pfnVectors = [ cast(VectorFunc) 0x10001000, &Reset_Handler, cast(VectorFunc)0xfeedfeed ]; /* works */ extern(C) void Reset_Handler() { // g_pfnVectors[4] = cast(VectorFunc)0x1000; /* error: cannot modify immutable expression g_pfnVectors[4] (correct behaviour) */ while(1){} } ... 08000000 <_D5start12g_pfnVectorsyG100PUZv>: 8000000: 10001000 andne r1, r0, r0 8000004: 08000235 stmdaeq r0, {r0, r2, r4, r5, r9} 8000008: feedface cdp2 10, 14, cr15, cr13, cr14, {6} [snipped some frame_dummy, tm_clones and dtors junk] 08000234 <Reset_Handler>: 8000234: e7fe b.n 8000234 <Reset_Handler>
Apr 05 2015