Named-Address-Spaces.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!-- Copyright (C) 1988-2017 Free Software Foundation, Inc.
  4. Permission is granted to copy, distribute and/or modify this document
  5. under the terms of the GNU Free Documentation License, Version 1.3 or
  6. any later version published by the Free Software Foundation; with the
  7. Invariant Sections being "Funding Free Software", the Front-Cover
  8. Texts being (a) (see below), and with the Back-Cover Texts being (b)
  9. (see below). A copy of the license is included in the section entitled
  10. "GNU Free Documentation License".
  11. (a) The FSF's Front-Cover Text is:
  12. A GNU Manual
  13. (b) The FSF's Back-Cover Text is:
  14. You have freedom to copy and modify this GNU Manual, like GNU
  15. software. Copies published by the Free Software Foundation raise
  16. funds for GNU development. -->
  17. <!-- Created by GNU Texinfo 5.2, http://www.gnu.org/software/texinfo/ -->
  18. <head>
  19. <title>Using the GNU Compiler Collection (GCC): Named Address Spaces</title>
  20. <meta name="description" content="Using the GNU Compiler Collection (GCC): Named Address Spaces">
  21. <meta name="keywords" content="Using the GNU Compiler Collection (GCC): Named Address Spaces">
  22. <meta name="resource-type" content="document">
  23. <meta name="distribution" content="global">
  24. <meta name="Generator" content="makeinfo">
  25. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  26. <link href="index.html#Top" rel="start" title="Top">
  27. <link href="Option-Index.html#Option-Index" rel="index" title="Option Index">
  28. <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
  29. <link href="C-Extensions.html#C-Extensions" rel="up" title="C Extensions">
  30. <link href="Zero-Length.html#Zero-Length" rel="next" title="Zero Length">
  31. <link href="Fixed_002dPoint.html#Fixed_002dPoint" rel="prev" title="Fixed-Point">
  32. <style type="text/css">
  33. <!--
  34. a.summary-letter {text-decoration: none}
  35. blockquote.smallquotation {font-size: smaller}
  36. div.display {margin-left: 3.2em}
  37. div.example {margin-left: 3.2em}
  38. div.indentedblock {margin-left: 3.2em}
  39. div.lisp {margin-left: 3.2em}
  40. div.smalldisplay {margin-left: 3.2em}
  41. div.smallexample {margin-left: 3.2em}
  42. div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
  43. div.smalllisp {margin-left: 3.2em}
  44. kbd {font-style:oblique}
  45. pre.display {font-family: inherit}
  46. pre.format {font-family: inherit}
  47. pre.menu-comment {font-family: serif}
  48. pre.menu-preformatted {font-family: serif}
  49. pre.smalldisplay {font-family: inherit; font-size: smaller}
  50. pre.smallexample {font-size: smaller}
  51. pre.smallformat {font-family: inherit; font-size: smaller}
  52. pre.smalllisp {font-size: smaller}
  53. span.nocodebreak {white-space:nowrap}
  54. span.nolinebreak {white-space:nowrap}
  55. span.roman {font-family:serif; font-weight:normal}
  56. span.sansserif {font-family:sans-serif; font-weight:normal}
  57. ul.no-bullet {list-style: none}
  58. -->
  59. </style>
  60. </head>
  61. <body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
  62. <a name="Named-Address-Spaces"></a>
  63. <div class="header">
  64. <p>
  65. Next: <a href="Zero-Length.html#Zero-Length" accesskey="n" rel="next">Zero Length</a>, Previous: <a href="Fixed_002dPoint.html#Fixed_002dPoint" accesskey="p" rel="prev">Fixed-Point</a>, Up: <a href="C-Extensions.html#C-Extensions" accesskey="u" rel="up">C Extensions</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
  66. </div>
  67. <hr>
  68. <a name="Named-Address-Spaces-1"></a>
  69. <h3 class="section">6.16 Named Address Spaces</h3>
  70. <a name="index-Named-Address-Spaces"></a>
  71. <p>As an extension, GNU C supports named address spaces as
  72. defined in the N1275 draft of ISO/IEC DTR 18037. Support for named
  73. address spaces in GCC will evolve as the draft technical report
  74. changes. Calling conventions for any target might also change. At
  75. present, only the AVR, SPU, M32C, RL78, and x86 targets support
  76. address spaces other than the generic address space.
  77. </p>
  78. <p>Address space identifiers may be used exactly like any other C type
  79. qualifier (e.g., <code>const</code> or <code>volatile</code>). See the N1275
  80. document for more details.
  81. </p>
  82. <a name="AVR-Named-Address-Spaces"></a><a name="AVR-Named-Address-Spaces-1"></a>
  83. <h4 class="subsection">6.16.1 AVR Named Address Spaces</h4>
  84. <p>On the AVR target, there are several address spaces that can be used
  85. in order to put read-only data into the flash memory and access that
  86. data by means of the special instructions <code>LPM</code> or <code>ELPM</code>
  87. needed to read from flash.
  88. </p>
  89. <p>Per default, any data including read-only data is located in RAM
  90. (the generic address space) so that non-generic address spaces are
  91. needed to locate read-only data in flash memory
  92. <em>and</em> to generate the right instructions to access this data
  93. without using (inline) assembler code.
  94. </p>
  95. <dl compact="compact">
  96. <dt><code>__flash</code></dt>
  97. <dd><a name="index-_005f_005fflash-AVR-Named-Address-Spaces"></a>
  98. <p>The <code>__flash</code> qualifier locates data in the
  99. <code>.progmem.data</code> section. Data is read using the <code>LPM</code>
  100. instruction. Pointers to this address space are 16 bits wide.
  101. </p>
  102. </dd>
  103. <dt><code>__flash1</code></dt>
  104. <dt><code>__flash2</code></dt>
  105. <dt><code>__flash3</code></dt>
  106. <dt><code>__flash4</code></dt>
  107. <dt><code>__flash5</code></dt>
  108. <dd><a name="index-_005f_005fflash1-AVR-Named-Address-Spaces"></a>
  109. <a name="index-_005f_005fflash2-AVR-Named-Address-Spaces"></a>
  110. <a name="index-_005f_005fflash3-AVR-Named-Address-Spaces"></a>
  111. <a name="index-_005f_005fflash4-AVR-Named-Address-Spaces"></a>
  112. <a name="index-_005f_005fflash5-AVR-Named-Address-Spaces"></a>
  113. <p>These are 16-bit address spaces locating data in section
  114. <code>.progmem<var>N</var>.data</code> where <var>N</var> refers to
  115. address space <code>__flash<var>N</var></code>.
  116. The compiler sets the <code>RAMPZ</code> segment register appropriately
  117. before reading data by means of the <code>ELPM</code> instruction.
  118. </p>
  119. </dd>
  120. <dt><code>__memx</code></dt>
  121. <dd><a name="index-_005f_005fmemx-AVR-Named-Address-Spaces"></a>
  122. <p>This is a 24-bit address space that linearizes flash and RAM:
  123. If the high bit of the address is set, data is read from
  124. RAM using the lower two bytes as RAM address.
  125. If the high bit of the address is clear, data is read from flash
  126. with <code>RAMPZ</code> set according to the high byte of the address.
  127. See <a href="AVR-Built_002din-Functions.html#AVR-Built_002din-Functions"><code>__builtin_avr_flash_segment</code></a>.
  128. </p>
  129. <p>Objects in this address space are located in <code>.progmemx.data</code>.
  130. </p></dd>
  131. </dl>
  132. <p><b>Example</b>
  133. </p>
  134. <div class="smallexample">
  135. <pre class="smallexample">char my_read (const __flash char ** p)
  136. {
  137. /* p is a pointer to RAM that points to a pointer to flash.
  138. The first indirection of p reads that flash pointer
  139. from RAM and the second indirection reads a char from this
  140. flash address. */
  141. return **p;
  142. }
  143. /* Locate array[] in flash memory */
  144. const __flash int array[] = { 3, 5, 7, 11, 13, 17, 19 };
  145. int i = 1;
  146. int main (void)
  147. {
  148. /* Return 17 by reading from flash memory */
  149. return array[array[i]];
  150. }
  151. </pre></div>
  152. <p>For each named address space supported by avr-gcc there is an equally
  153. named but uppercase built-in macro defined.
  154. The purpose is to facilitate testing if respective address space
  155. support is available or not:
  156. </p>
  157. <div class="smallexample">
  158. <pre class="smallexample">#ifdef __FLASH
  159. const __flash int var = 1;
  160. int read_var (void)
  161. {
  162. return var;
  163. }
  164. #else
  165. #include &lt;avr/pgmspace.h&gt; /* From AVR-LibC */
  166. const int var PROGMEM = 1;
  167. int read_var (void)
  168. {
  169. return (int) pgm_read_word (&amp;var);
  170. }
  171. #endif /* __FLASH */
  172. </pre></div>
  173. <p>Notice that attribute <a href="AVR-Variable-Attributes.html#AVR-Variable-Attributes"><code>progmem</code></a>
  174. locates data in flash but
  175. accesses to these data read from generic address space, i.e.
  176. from RAM,
  177. so that you need special accessors like <code>pgm_read_byte</code>
  178. from <a href="http://nongnu.org/avr-libc/user-manual/"><span class="nolinebreak">AVR-LibC</span></a><!-- /@w -->
  179. together with attribute <code>progmem</code>.
  180. </p>
  181. <p><b>Limitations and caveats</b>
  182. </p>
  183. <ul>
  184. <li> Reading across the 64&nbsp;KiB section boundary of
  185. the <code>__flash</code> or <code>__flash<var>N</var></code> address spaces
  186. shows undefined behavior. The only address space that
  187. supports reading across the 64&nbsp;KiB flash segment boundaries is
  188. <code>__memx</code>.
  189. </li><li> If you use one of the <code>__flash<var>N</var></code> address spaces
  190. you must arrange your linker script to locate the
  191. <code>.progmem<var>N</var>.data</code> sections according to your needs.
  192. </li><li> Any data or pointers to the non-generic address spaces must
  193. be qualified as <code>const</code>, i.e. as read-only data.
  194. This still applies if the data in one of these address
  195. spaces like software version number or calibration lookup table are intended to
  196. be changed after load time by, say, a boot loader. In this case
  197. the right qualification is <code>const</code> <code>volatile</code> so that the compiler
  198. must not optimize away known values or insert them
  199. as immediates into operands of instructions.
  200. </li><li> The following code initializes a variable <code>pfoo</code>
  201. located in static storage with a 24-bit address:
  202. <div class="smallexample">
  203. <pre class="smallexample">extern const __memx char foo;
  204. const __memx void *pfoo = &amp;foo;
  205. </pre></div>
  206. <p>Such code requires at least binutils 2.23, see
  207. <a href="https://sourceware.org/PR13503">PR13503</a><!-- /@w -->.
  208. </p>
  209. </li><li> On the reduced Tiny devices like ATtiny40, no address spaces are supported.
  210. Data can be put into and read from flash memory by means of
  211. attribute <code>progmem</code>, see <a href="AVR-Variable-Attributes.html#AVR-Variable-Attributes">AVR Variable Attributes</a>.
  212. </li></ul>
  213. <a name="M32C-Named-Address-Spaces"></a>
  214. <h4 class="subsection">6.16.2 M32C Named Address Spaces</h4>
  215. <a name="index-_005f_005ffar-M32C-Named-Address-Spaces"></a>
  216. <p>On the M32C target, with the R8C and M16C CPU variants, variables
  217. qualified with <code>__far</code> are accessed using 32-bit addresses in
  218. order to access memory beyond the first 64&nbsp;Ki bytes. If
  219. <code>__far</code> is used with the M32CM or M32C CPU variants, it has no
  220. effect.
  221. </p>
  222. <a name="RL78-Named-Address-Spaces"></a>
  223. <h4 class="subsection">6.16.3 RL78 Named Address Spaces</h4>
  224. <a name="index-_005f_005ffar-RL78-Named-Address-Spaces"></a>
  225. <p>On the RL78 target, variables qualified with <code>__far</code> are accessed
  226. with 32-bit pointers (20-bit addresses) rather than the default 16-bit
  227. addresses. Non-far variables are assumed to appear in the topmost
  228. 64&nbsp;KiB of the address space.
  229. </p>
  230. <a name="SPU-Named-Address-Spaces"></a>
  231. <h4 class="subsection">6.16.4 SPU Named Address Spaces</h4>
  232. <a name="index-_005f_005fea-SPU-Named-Address-Spaces"></a>
  233. <p>On the SPU target variables may be declared as
  234. belonging to another address space by qualifying the type with the
  235. <code>__ea</code> address space identifier:
  236. </p>
  237. <div class="smallexample">
  238. <pre class="smallexample">extern int __ea i;
  239. </pre></div>
  240. <p>The compiler generates special code to access the variable <code>i</code>.
  241. It may use runtime library
  242. support, or generate special machine instructions to access that address
  243. space.
  244. </p>
  245. <a name="x86-Named-Address-Spaces"></a>
  246. <h4 class="subsection">6.16.5 x86 Named Address Spaces</h4>
  247. <a name="index-x86-named-address-spaces"></a>
  248. <p>On the x86 target, variables may be declared as being relative
  249. to the <code>%fs</code> or <code>%gs</code> segments.
  250. </p>
  251. <dl compact="compact">
  252. <dt><code>__seg_fs</code></dt>
  253. <dt><code>__seg_gs</code></dt>
  254. <dd><a name="index-_005f_005fseg_005ffs-x86-named-address-space"></a>
  255. <a name="index-_005f_005fseg_005fgs-x86-named-address-space"></a>
  256. <p>The object is accessed with the respective segment override prefix.
  257. </p>
  258. <p>The respective segment base must be set via some method specific to
  259. the operating system. Rather than require an expensive system call
  260. to retrieve the segment base, these address spaces are not considered
  261. to be subspaces of the generic (flat) address space. This means that
  262. explicit casts are required to convert pointers between these address
  263. spaces and the generic address space. In practice the application
  264. should cast to <code>uintptr_t</code> and apply the segment base offset
  265. that it installed previously.
  266. </p>
  267. <p>The preprocessor symbols <code>__SEG_FS</code> and <code>__SEG_GS</code> are
  268. defined when these address spaces are supported.
  269. </p></dd>
  270. </dl>
  271. <hr>
  272. <div class="header">
  273. <p>
  274. Next: <a href="Zero-Length.html#Zero-Length" accesskey="n" rel="next">Zero Length</a>, Previous: <a href="Fixed_002dPoint.html#Fixed_002dPoint" accesskey="p" rel="prev">Fixed-Point</a>, Up: <a href="C-Extensions.html#C-Extensions" accesskey="u" rel="up">C Extensions</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
  275. </div>
  276. </body>
  277. </html>