Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

307 rindas
13KB

  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-2020 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 6.5, http://www.gnu.org/software/texinfo/ -->
  18. <head>
  19. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  20. <title>Named Address Spaces (Using the GNU Compiler Collection (GCC))</title>
  21. <meta name="description" content="Named Address Spaces (Using the GNU Compiler Collection (GCC))">
  22. <meta name="keywords" content="Named Address Spaces (Using the GNU Compiler Collection (GCC))">
  23. <meta name="resource-type" content="document">
  24. <meta name="distribution" content="global">
  25. <meta name="Generator" content="makeinfo">
  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.indentedblock {margin-right: 0em}
  36. blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
  37. blockquote.smallquotation {font-size: smaller}
  38. div.display {margin-left: 3.2em}
  39. div.example {margin-left: 3.2em}
  40. div.lisp {margin-left: 3.2em}
  41. div.smalldisplay {margin-left: 3.2em}
  42. div.smallexample {margin-left: 3.2em}
  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.nolinebreak {white-space: nowrap}
  54. span.roman {font-family: initial; font-weight: normal}
  55. span.sansserif {font-family: sans-serif; font-weight: normal}
  56. ul.no-bullet {list-style: none}
  57. -->
  58. </style>
  59. </head>
  60. <body lang="en">
  61. <a name="Named-Address-Spaces"></a>
  62. <div class="header">
  63. <p>
  64. 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>
  65. </div>
  66. <hr>
  67. <a name="Named-Address-Spaces-1"></a>
  68. <h3 class="section">6.17 Named Address Spaces</h3>
  69. <a name="index-Named-Address-Spaces"></a>
  70. <p>As an extension, GNU C supports named address spaces as
  71. defined in the N1275 draft of ISO/IEC DTR 18037. Support for named
  72. address spaces in GCC will evolve as the draft technical report
  73. changes. Calling conventions for any target might also change. At
  74. present, only the AVR, M32C, RL78, and x86 targets support
  75. address spaces other than the generic address space.
  76. </p>
  77. <p>Address space identifiers may be used exactly like any other C type
  78. qualifier (e.g., <code>const</code> or <code>volatile</code>). See the N1275
  79. document for more details.
  80. </p>
  81. <a name="AVR-Named-Address-Spaces"></a><a name="AVR-Named-Address-Spaces-1"></a>
  82. <h4 class="subsection">6.17.1 AVR Named Address Spaces</h4>
  83. <p>On the AVR target, there are several address spaces that can be used
  84. in order to put read-only data into the flash memory and access that
  85. data by means of the special instructions <code>LPM</code> or <code>ELPM</code>
  86. needed to read from flash.
  87. </p>
  88. <p>Devices belonging to <code>avrtiny</code> and <code>avrxmega3</code> can access
  89. flash memory by means of <code>LD*</code> instructions because the flash
  90. memory is mapped into the RAM address space. There is <em>no need</em>
  91. for language extensions like <code>__flash</code> or attribute
  92. <a href="AVR-Variable-Attributes.html#AVR-Variable-Attributes"><code>progmem</code></a>.
  93. The default linker description files for these devices cater for that
  94. feature and <code>.rodata</code> stays in flash: The compiler just generates
  95. <code>LD*</code> instructions, and the linker script adds core specific
  96. offsets to all <code>.rodata</code> symbols: <code>0x4000</code> in the case of
  97. <code>avrtiny</code> and <code>0x8000</code> in the case of <code>avrxmega3</code>.
  98. See <a href="AVR-Options.html#AVR-Options">AVR Options</a> for a list of respective devices.
  99. </p>
  100. <p>For devices not in <code>avrtiny</code> or <code>avrxmega3</code>,
  101. any data including read-only data is located in RAM (the generic
  102. address space) because flash memory is not visible in the RAM address
  103. space. In order to locate read-only data in flash memory <em>and</em>
  104. to generate the right instructions to access this data without
  105. using (inline) assembler code, special address spaces are needed.
  106. </p>
  107. <dl compact="compact">
  108. <dt><code>__flash</code></dt>
  109. <dd><a name="index-_005f_005fflash-AVR-Named-Address-Spaces"></a>
  110. <p>The <code>__flash</code> qualifier locates data in the
  111. <code>.progmem.data</code> section. Data is read using the <code>LPM</code>
  112. instruction. Pointers to this address space are 16 bits wide.
  113. </p>
  114. </dd>
  115. <dt><code>__flash1</code></dt>
  116. <dt><code>__flash2</code></dt>
  117. <dt><code>__flash3</code></dt>
  118. <dt><code>__flash4</code></dt>
  119. <dt><code>__flash5</code></dt>
  120. <dd><a name="index-_005f_005fflash1-AVR-Named-Address-Spaces"></a>
  121. <a name="index-_005f_005fflash2-AVR-Named-Address-Spaces"></a>
  122. <a name="index-_005f_005fflash3-AVR-Named-Address-Spaces"></a>
  123. <a name="index-_005f_005fflash4-AVR-Named-Address-Spaces"></a>
  124. <a name="index-_005f_005fflash5-AVR-Named-Address-Spaces"></a>
  125. <p>These are 16-bit address spaces locating data in section
  126. <code>.progmem<var>N</var>.data</code> where <var>N</var> refers to
  127. address space <code>__flash<var>N</var></code>.
  128. The compiler sets the <code>RAMPZ</code> segment register appropriately
  129. before reading data by means of the <code>ELPM</code> instruction.
  130. </p>
  131. </dd>
  132. <dt><code>__memx</code></dt>
  133. <dd><a name="index-_005f_005fmemx-AVR-Named-Address-Spaces"></a>
  134. <p>This is a 24-bit address space that linearizes flash and RAM:
  135. If the high bit of the address is set, data is read from
  136. RAM using the lower two bytes as RAM address.
  137. If the high bit of the address is clear, data is read from flash
  138. with <code>RAMPZ</code> set according to the high byte of the address.
  139. See <a href="AVR-Built_002din-Functions.html#AVR-Built_002din-Functions"><code>__builtin_avr_flash_segment</code></a>.
  140. </p>
  141. <p>Objects in this address space are located in <code>.progmemx.data</code>.
  142. </p></dd>
  143. </dl>
  144. <p><b>Example</b>
  145. </p>
  146. <div class="smallexample">
  147. <pre class="smallexample">char my_read (const __flash char ** p)
  148. {
  149. /* p is a pointer to RAM that points to a pointer to flash.
  150. The first indirection of p reads that flash pointer
  151. from RAM and the second indirection reads a char from this
  152. flash address. */
  153. return **p;
  154. }
  155. /* Locate array[] in flash memory */
  156. const __flash int array[] = { 3, 5, 7, 11, 13, 17, 19 };
  157. int i = 1;
  158. int main (void)
  159. {
  160. /* Return 17 by reading from flash memory */
  161. return array[array[i]];
  162. }
  163. </pre></div>
  164. <p>For each named address space supported by avr-gcc there is an equally
  165. named but uppercase built-in macro defined.
  166. The purpose is to facilitate testing if respective address space
  167. support is available or not:
  168. </p>
  169. <div class="smallexample">
  170. <pre class="smallexample">#ifdef __FLASH
  171. const __flash int var = 1;
  172. int read_var (void)
  173. {
  174. return var;
  175. }
  176. #else
  177. #include &lt;avr/pgmspace.h&gt; /* From AVR-LibC */
  178. const int var PROGMEM = 1;
  179. int read_var (void)
  180. {
  181. return (int) pgm_read_word (&amp;var);
  182. }
  183. #endif /* __FLASH */
  184. </pre></div>
  185. <p>Notice that attribute <a href="AVR-Variable-Attributes.html#AVR-Variable-Attributes"><code>progmem</code></a>
  186. locates data in flash but
  187. accesses to these data read from generic address space, i.e.
  188. from RAM,
  189. so that you need special accessors like <code>pgm_read_byte</code>
  190. from <a href="http://nongnu.org/avr-libc/user-manual/"><span class="nolinebreak">AVR-LibC</span></a><!-- /@w -->
  191. together with attribute <code>progmem</code>.
  192. </p>
  193. <p><b>Limitations and caveats</b>
  194. </p>
  195. <ul>
  196. <li> Reading across the 64&nbsp;KiB section boundary of
  197. the <code>__flash</code> or <code>__flash<var>N</var></code> address spaces
  198. shows undefined behavior. The only address space that
  199. supports reading across the 64&nbsp;KiB flash segment boundaries is
  200. <code>__memx</code>.
  201. </li><li> If you use one of the <code>__flash<var>N</var></code> address spaces
  202. you must arrange your linker script to locate the
  203. <code>.progmem<var>N</var>.data</code> sections according to your needs.
  204. </li><li> Any data or pointers to the non-generic address spaces must
  205. be qualified as <code>const</code>, i.e. as read-only data.
  206. This still applies if the data in one of these address
  207. spaces like software version number or calibration lookup table are intended to
  208. be changed after load time by, say, a boot loader. In this case
  209. the right qualification is <code>const</code> <code>volatile</code> so that the compiler
  210. must not optimize away known values or insert them
  211. as immediates into operands of instructions.
  212. </li><li> The following code initializes a variable <code>pfoo</code>
  213. located in static storage with a 24-bit address:
  214. <div class="smallexample">
  215. <pre class="smallexample">extern const __memx char foo;
  216. const __memx void *pfoo = &amp;foo;
  217. </pre></div>
  218. </li><li> On the reduced Tiny devices like ATtiny40, no address spaces are supported.
  219. Just use vanilla C / C++ code without overhead as outlined above.
  220. Attribute <code>progmem</code> is supported but works differently,
  221. see <a href="AVR-Variable-Attributes.html#AVR-Variable-Attributes">AVR Variable Attributes</a>.
  222. </li></ul>
  223. <a name="M32C-Named-Address-Spaces"></a>
  224. <h4 class="subsection">6.17.2 M32C Named Address Spaces</h4>
  225. <a name="index-_005f_005ffar-M32C-Named-Address-Spaces"></a>
  226. <p>On the M32C target, with the R8C and M16C CPU variants, variables
  227. qualified with <code>__far</code> are accessed using 32-bit addresses in
  228. order to access memory beyond the first 64&nbsp;Ki bytes. If
  229. <code>__far</code> is used with the M32CM or M32C CPU variants, it has no
  230. effect.
  231. </p>
  232. <a name="RL78-Named-Address-Spaces"></a>
  233. <h4 class="subsection">6.17.3 RL78 Named Address Spaces</h4>
  234. <a name="index-_005f_005ffar-RL78-Named-Address-Spaces"></a>
  235. <p>On the RL78 target, variables qualified with <code>__far</code> are accessed
  236. with 32-bit pointers (20-bit addresses) rather than the default 16-bit
  237. addresses. Non-far variables are assumed to appear in the topmost
  238. 64&nbsp;KiB of the address space.
  239. </p>
  240. <a name="x86-Named-Address-Spaces"></a>
  241. <h4 class="subsection">6.17.4 x86 Named Address Spaces</h4>
  242. <a name="index-x86-named-address-spaces"></a>
  243. <p>On the x86 target, variables may be declared as being relative
  244. to the <code>%fs</code> or <code>%gs</code> segments.
  245. </p>
  246. <dl compact="compact">
  247. <dt><code>__seg_fs</code></dt>
  248. <dt><code>__seg_gs</code></dt>
  249. <dd><a name="index-_005f_005fseg_005ffs-x86-named-address-space"></a>
  250. <a name="index-_005f_005fseg_005fgs-x86-named-address-space"></a>
  251. <p>The object is accessed with the respective segment override prefix.
  252. </p>
  253. <p>The respective segment base must be set via some method specific to
  254. the operating system. Rather than require an expensive system call
  255. to retrieve the segment base, these address spaces are not considered
  256. to be subspaces of the generic (flat) address space. This means that
  257. explicit casts are required to convert pointers between these address
  258. spaces and the generic address space. In practice the application
  259. should cast to <code>uintptr_t</code> and apply the segment base offset
  260. that it installed previously.
  261. </p>
  262. <p>The preprocessor symbols <code>__SEG_FS</code> and <code>__SEG_GS</code> are
  263. defined when these address spaces are supported.
  264. </p></dd>
  265. </dl>
  266. <hr>
  267. <div class="header">
  268. <p>
  269. 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>
  270. </div>
  271. </body>
  272. </html>