aboutsummaryrefslogtreecommitdiffstats
path: root/sound/soc/blackfin/bf5xx-i2s-pcm.c
blob: f1fd95bb6416ce0b44e750e356d4ddc95346f31c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*
 * File:         sound/soc/blackfin/bf5xx-i2s-pcm.c
 * Author:       Cliff Cai <Cliff.Cai@analog.com>
 *
 * Created:      Tue June 06 2008
 * Description:  DMA driver for i2s codec
 *
 * Modified:
 *               Copyright 2008 Analog Devices Inc.
 *
 * Bugs:         Enter bugs at http://blackfin.uclinux.org/
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see the file COPYING, or write
 * to the Free Software Foundation, Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <linux/module.h>
#include <linux/init.h>
#include <linux/platform_device.h>
#include <linux/dma-mapping.h>
#include <linux/gfp.h>

#include <sound/core.h>
#include <sound/pcm.h>
#include <sound/pcm_params.h>
#include <sound/soc.h>

#include <asm/dma.h>

#include "bf5xx-i2s-pcm.h"
#include "bf5xx-sport.h"

static void bf5xx_dma_irq(void *data)
{
	struct snd_pcm_substream *pcm = data;
	snd_pcm_period_elapsed(pcm);
}

static const struct snd_pcm_hardware bf5xx_pcm_hardware = {
	.info			= SNDRV_PCM_INFO_INTERLEAVED |
				   SNDRV_PCM_INFO_MMAP |
				   SNDRV_PCM_INFO_MMAP_VALID |
				   SNDRV_PCM_INFO_BLOCK_TRANSFER,
	.formats		= SNDRV_PCM_FMTBIT_S16_LE |
				   SNDRV_PCM_FMTBIT_S24_LE |
				   SNDRV_PCM_FMTBIT_S32_LE,
	.period_bytes_min	= 32,
	.period_bytes_max	= 0x10000,
	.periods_min		= 1,
	.periods_max		= PAGE_SIZE/32,
	.buffer_bytes_max	= 0x20000, /* 128 kbytes */
	.fifo_size		= 16,
};

static int bf5xx_pcm_hw_params(struct snd_pcm_substream *substream,
	struct snd_pcm_hw_params *params)
{
	size_t size = bf5xx_pcm_hardware.buffer_bytes_max;
	snd_pcm_lib_malloc_pages(substream, size);

	return 0;
}

static int bf5xx_pcm_hw_free(struct snd_pcm_substream *substream)
{
	snd_pcm_lib_free_pages(substream);

	return 0;
}

static int bf5xx_pcm_prepare(struct snd_pcm_substream *substream)
{
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct sport_device *sport = runtime->private_data;
	int period_bytes = frames_to_bytes(runtime, runtime->period_size);

	pr_debug("%s enter\n", __func__);
	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
		sport_set_tx_callback(sport, bf5xx_dma_irq, substream);
		sport_config_tx_dma(sport, runtime->dma_area,
			runtime->periods, period_bytes);
	} else {
		sport_set_rx_callback(sport, bf5xx_dma_irq, substream);
		sport_config_rx_dma(sport, runtime->dma_area,
			runtime->periods, period_bytes);
	}

	return 0;
}

static int bf5xx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
{
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct sport_device *sport = runtime->private_data;
	int ret = 0;

	pr_debug("%s enter\n", __func__);
	switch (cmd) {
	case SNDRV_PCM_TRIGGER_START:
		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
			sport_tx_start(sport);
		else
			sport_rx_start(sport);
		break;
	case SNDRV_PCM_TRIGGER_STOP:
	case SNDRV_PCM_TRIGGER_SUSPEND:
	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
			sport_tx_stop(sport);
		else
			sport_rx_stop(sport);
		break;
	default:
		ret = -EINVAL;
	}

	return ret;
}

static snd_pcm_uframes_t bf5xx_pcm_pointer(struct snd_pcm_substream *substream)
{
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct sport_device *sport = runtime->private_data;
	unsigned int diff;
	snd_pcm_uframes_t frames;
	pr_debug("%s enter\n", __func__);
	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
		diff = sport_curr_offset_tx(sport);
	} else {
		diff = sport_curr_offset_rx(sport);
	}

	/*
	 * TX at least can report one frame beyond the end of the
	 * buffer if we hit the wraparound case - clamp to within the
	 * buffer as the ALSA APIs require.
	 */
	if (diff == snd_pcm_lib_buffer_bytes(substream))
		diff = 0;

	frames = bytes_to_frames(substream->runtime, diff);

	return frames;
}

static int bf5xx_pcm_open(struct snd_pcm_substream *substream)
{
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
	struct sport_device *sport_handle = snd_soc_dai_get_drvdata(cpu_dai);
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct snd_dma_buffer *buf = &substream->dma_buffer;
	int ret;

	pr_debug("%s enter\n", __func__);

	snd_soc_set_runtime_hwparams(substream, &bf5xx_pcm_hardware);

	ret = snd_pcm_hw_constraint_integer(runtime, \
			SNDRV_PCM_HW_PARAM_PERIODS);
	if (ret < 0)
		goto out;

	if (sport_handle != NULL) {
		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
			sport_handle->tx_buf = buf->area;
		else
			sport_handle->rx_buf = buf->area;

		runtime->private_data = sport_handle;
	} else {
		pr_err("sport_handle is NULL\n");
		return -1;
	}
	return 0;

 out:
	return ret;
}

static int bf5xx_pcm_mmap(struct snd_pcm_substream *substream,
	struct vm_area_struct *vma)
{
	struct snd_pcm_runtime *runtime = substream->runtime;
	size_t size = vma->vm_end - vma->vm_start;
	vma->vm_start = (unsigned long)runtime->dma_area;
	vma->vm_end = vma->vm_start + size;
	vma->vm_flags |=  VM_SHARED;

	return 0 ;
}

static struct snd_pcm_ops bf5xx_pcm_i2s_ops = {
	.open		= bf5xx_pcm_open,
	.ioctl		= snd_pcm_lib_ioctl,
	.hw_params	= bf5xx_pcm_hw_params,
	.hw_free	= bf5xx_pcm_hw_free,
	.prepare	= bf5xx_pcm_prepare,
	.trigger	= bf5xx_pcm_trigger,
	.pointer	= bf5xx_pcm_pointer,
	.mmap		= bf5xx_pcm_mmap,
};

static int bf5xx_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
{
	struct snd_pcm_substream *substream = pcm->streams[stream].substream;
	struct snd_dma_buffer *buf = &substream->dma_buffer;
	size_t size = bf5xx_pcm_hardware.buffer_bytes_max;

	buf->dev.type = SNDRV_DMA_TYPE_DEV;
	buf->dev.dev = pcm->card->dev;
	buf->private_data = NULL;
	buf->area = dma_alloc_coherent(pcm->card->dev, size,
			&buf->addr, GFP_KERNEL);
	if (!buf->area) {
		pr_err("Failed to allocate dma memory - Please increase uncached DMA memory region\n");
		return -ENOMEM;
	}
	buf->bytes = size;

	pr_debug("%s, area:%p, size:0x%08lx\n", __func__,
		buf->area, buf->bytes);

	return 0;
}

static void bf5xx_pcm_free_dma_buffers(struct snd_pcm *pcm)
{
	struct snd_pcm_substream *substream;
	struct snd_dma_buffer *buf;
	int stream;

	for (stream = 0; stream < 2; stream++) {
		substream = pcm->streams[stream].substream;
		if (!substream)
			continue;

		buf = &substream->dma_buffer;
		if (!buf->area)
			continue;
		dma_free_coherent(NULL, buf->bytes, buf->area, 0);
		buf->area = NULL;
	}
}

static u64 bf5xx_pcm_dmamask = DMA_BIT_MASK(32);

int bf5xx_pcm_i2s_new(struct snd_card *card, struct snd_soc_dai *dai,
	struct snd_pcm *pcm)
{
	int ret = 0;

	pr_debug("%s enter\n", __func__);
	if (!card->dev->dma_mask)
		card->dev->dma_mask = &bf5xx_pcm_dmamask;
	if (!card->dev->coherent_dma_mask)
		card->dev->coherent_dma_mask = DMA_BIT_MASK(32);

	if (dai->driver->playback.channels_min) {
		ret = bf5xx_pcm_preallocate_dma_buffer(pcm,
			SNDRV_PCM_STREAM_PLAYBACK);
		if (ret)
			goto out;
	}

	if (dai->driver->capture.channels_min) {
		ret = bf5xx_pcm_preallocate_dma_buffer(pcm,
			SNDRV_PCM_STREAM_CAPTURE);
		if (ret)
			goto out;
	}
 out:
	return ret;
}

static struct snd_soc_platform_driver bf5xx_i2s_soc_platform = {
	.ops		= &bf5xx_pcm_i2s_ops,
	.pcm_new	= bf5xx_pcm_i2s_new,
	.pcm_free	= bf5xx_pcm_free_dma_buffers,
};

static int __devinit bfin_i2s_soc_platform_probe(struct platform_device *pdev)
{
	return snd_soc_register_platform(&pdev->dev, &bf5xx_i2s_soc_platform);
}

static int __devexit bfin_i2s_soc_platform_remove(struct platform_device *pdev)
{
	snd_soc_unregister_platform(&pdev->dev);
	return 0;
}

static struct platform_driver bfin_i2s_pcm_driver = {
	.driver = {
			.name = "bfin-i2s-pcm-audio",
			.owner = THIS_MODULE,
	},

	.probe = bfin_i2s_soc_platform_probe,
	.remove = __devexit_p(bfin_i2s_soc_platform_remove),
};

static int __init snd_bfin_i2s_pcm_init(void)
{
	return platform_driver_register(&bfin_i2s_pcm_driver);
}
module_init(snd_bfin_i2s_pcm_init);

static void __exit snd_bfin_i2s_pcm_exit(void)
{
	platform_driver_unregister(&bfin_i2s_pcm_driver);
}
module_exit(snd_bfin_i2s_pcm_exit);

MODULE_AUTHOR("Cliff Cai");
MODULE_DESCRIPTION("ADI Blackfin I2S PCM DMA module");
MODULE_LICENSE("GPL");
n1588' href='#n1588'>1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663
--  Iir to ortho translator.
--  Copyright (C) 2002 - 2014 Tristan Gingold
--
--  This program is free software: you can redistribute it and/or modify
--  it under the terms of the GNU General Public License as published by
--  the Free Software Foundation, either version 2 of the License, or
--  (at your option) any later version.
--
--  This program is distributed in the hope that it will be useful,
--  but WITHOUT ANY WARRANTY; without even the implied warranty of
--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
--  GNU General Public License for more details.
--
--  You should have received a copy of the GNU General Public License
--  along with this program.  If not, see <gnu.org/licenses>.

with Simple_IO;
with Name_Table;
with Str_Table;
with Vhdl.Utils; use Vhdl.Utils;
with Vhdl.Nodes_Utils; use Vhdl.Nodes_Utils;
with Vhdl.Std_Package; use Vhdl.Std_Package;
with Errorout; use Errorout;
with Vhdl.Errors; use Vhdl.Errors;
with Flags; use Flags;
with Vhdl.Canon;
with Vhdl.Evaluation; use Vhdl.Evaluation;
with Trans.Chap3;
with Trans.Chap4;
with Trans.Chap6;
with Trans.Chap8;
with Trans.Chap14;
with Trans.Rtis;
with Trans_Decls; use Trans_Decls;
with Trans.Helpers2; use Trans.Helpers2;
with Trans.Foreach_Non_Composite;

package body Trans.Chap7 is
   use Trans.Helpers;
   procedure Copy_Range (Dest : Mnode; Src : Mnode);

   procedure Create_Operator_Instance (Interfaces : in out O_Inter_List;
                                       Info : Operator_Info_Acc) is
   begin
      Subprgs.Add_Subprg_Instance_Interfaces
        (Interfaces, Info.Operator_Instance);
   end Create_Operator_Instance;

   procedure Start_Operator_Instance_Use (Info : Operator_Info_Acc) is
   begin
      Subprgs.Start_Subprg_Instance_Use (Info.Operator_Instance);
   end Start_Operator_Instance_Use;

   procedure Finish_Operator_Instance_Use (Info : Operator_Info_Acc) is
   begin
      Subprgs.Finish_Subprg_Instance_Use (Info.Operator_Instance);
   end Finish_Operator_Instance_Use;

   function Translate_Static_Implicit_Conv
     (Expr : O_Cnode; Expr_Type : Iir; Res_Type : Iir) return O_Cnode
   is
      Expr_Info : Type_Info_Acc;
      Res_Info  : Type_Info_Acc;
      Val       : Var_Type;
      Res       : O_Cnode;
      List      : O_Record_Aggr_List;
      Layout    : Var_Type;
   begin
      if Res_Type = Expr_Type then
         return Expr;
      end if;

      --  EXPR must be already constrained.
      pragma Assert (Get_Constraint_State (Expr_Type) = Fully_Constrained);
      if Get_Constraint_State (Res_Type) = Fully_Constrained then
         --  constrained to constrained.
         if Chap3.Locally_Types_Match (Expr_Type, Res_Type) /= True then
            --  Sem should have replaced the expression by an overflow.
            raise Internal_Error;
            --  Chap6.Gen_Bound_Error (Loc);
         end if;

         --  Constrained to constrained should be OK, as already checked by
         --  sem.
         return Expr;
      end if;

      --  Handle only constrained to unconstrained conversion.
      pragma Assert (Get_Kind (Res_Type) in Iir_Kinds_Array_Type_Definition);

      Expr_Info := Get_Info (Expr_Type);
      Res_Info := Get_Info (Res_Type);
      Val := Create_Global_Const
        (Create_Uniq_Identifier, Expr_Info.Ortho_Type (Mode_Value),
         O_Storage_Private, Expr);
      Layout := Expr_Info.S.Composite_Layout;
      if Layout = Null_Var then
         Layout := Create_Global_Const
           (Create_Uniq_Identifier, Expr_Info.B.Layout_Type,
            O_Storage_Private,
            Chap3.Create_Static_Composite_Subtype_Layout (Expr_Type));
         Expr_Info.S.Composite_Layout := Layout;
      end if;

      Start_Record_Aggr (List, Res_Info.Ortho_Type (Mode_Value));
      New_Record_Aggr_El
        (List, New_Global_Address (New_Global (Get_Var_Label (Val)),
                                   Res_Info.B.Base_Ptr_Type (Mode_Value)));
      New_Record_Aggr_El
        (List, New_Global_Address (New_Global_Selected_Element
                                     (New_Global (Get_Var_Label (Layout)),
                                      Expr_Info.B.Layout_Bounds),
                                   Expr_Info.B.Bounds_Ptr_Type));
      Finish_Record_Aggr (List, Res);

      return Res;
   end Translate_Static_Implicit_Conv;

   function Is_Static_Constant (Decl : Iir_Constant_Declaration) return Boolean
   is
      Expr  : constant Iir := Get_Default_Value (Decl);
      Atype : Iir;
      Info  : Iir;
   begin
      if Expr = Null_Iir then
         --  Deferred constant.
         return False;
      end if;

      --  Only aggregates are specially handled.
      if not Is_Static_Construct (Expr)
        or else Get_Kind (Expr) /= Iir_Kind_Aggregate
      then
         return False;
      end if;

      Atype := Get_Type (Decl);

      --  Currently, only array aggregates are handled.
      if Get_Kind (Get_Base_Type (Atype)) /= Iir_Kind_Array_Type_Definition
      then
         return False;
      end if;

      Info := Get_Aggregate_Info (Expr);
      while Info /= Null_Iir loop
         if Get_Aggr_Dynamic_Flag (Info) then
            raise Internal_Error;
         end if;

         --  Currently, only positionnal aggregates are handled.
         if Get_Aggr_Named_Flag (Info) then
            return False;
         end if;
         --  Currently, others choice are not handled.
         if Get_Aggr_Others_Flag (Info) then
            return False;
         end if;

         Info := Get_Sub_Aggregate_Info (Info);
      end loop;
      return True;
   end Is_Static_Constant;

   procedure Translate_Static_String_Literal8_Inner
     (List : in out O_Array_Aggr_List;
      Str     : Iir;
      El_Type : Iir)
   is
      Literal_List : constant Iir_Flist :=
        Get_Enumeration_Literal_List (Get_Base_Type (El_Type));
      Len          : constant Nat32 := Get_String_Length (Str);
      Id           : constant String8_Id := Get_String8_Id (Str);
      Lit          : Iir;
   begin
      for I in 1 .. Len loop
         Lit := Get_Nth_Element
           (Literal_List, Natural (Str_Table.Element_String8 (Id, I)));
         New_Array_Aggr_El (List, Get_Ortho_Literal (Lit));
      end loop;
   end Translate_Static_String_Literal8_Inner;

   procedure Translate_Static_Array_Aggregate_1
     (List : in out O_Array_Aggr_List;
      Aggr : Iir;
      Aggr_Type : Iir;
      Dim : Positive)
   is
      Nbr_Dims  : constant Natural := Get_Nbr_Dimensions (Aggr_Type);
      El_Type   : constant Iir := Get_Element_Subtype (Aggr_Type);
   begin
      case Get_Kind (Aggr) is
         when Iir_Kind_Aggregate =>
            declare
               Index_Type : constant Iir :=
                 Get_Index_Type (Aggr_Type, Dim - 1);
               Index_Range : constant Iir := Eval_Static_Range (Index_Type);
               Len : constant Int64 :=
                 Eval_Discrete_Range_Length (Index_Range);
               Assocs : constant Iir := Get_Association_Choices_Chain (Aggr);
               Vect : Iir_Array (0 .. Integer (Len - 1));
            begin
               if Len = 0 then
                  --  Should be automatically handled, but fails with some
                  --  old versions of gnat (gnatgpl 2014 with -O).
                  return;
               end if;

               Build_Array_Choices_Vector (Vect, Index_Range, Assocs);

               if Dim = Nbr_Dims then
                  declare
                     Idx : Natural;
                     Assoc : Iir;
                     Expr : Iir;
                     El : Iir;
                     Assoc_Len : Iir_Index32;
                  begin
                     Idx := 0;
                     while Idx < Natural (Len) loop
                        Assoc := Vect (Idx);
                        Expr  := Get_Associated_Expr (Assoc);
                        if Get_Element_Type_Flag (Assoc) then
                           New_Array_Aggr_El
                             (List,
                              Translate_Static_Expression (Expr, El_Type));
                           Idx := Idx + 1;
                        else
                           Assoc_Len := Iir_Index32
                             (Eval_Discrete_Type_Length
                                (Get_Index_Type (Get_Type (Expr), 0)));
                           for I in 0 .. Assoc_Len - 1 loop
                              El := Eval_Indexed_Name_By_Offset (Expr, I);
                              New_Array_Aggr_El
                                (List,
                                 Translate_Static_Expression (El, El_Type));
                              Idx := Idx + 1;
                           end loop;
                        end if;
                     end loop;
                  end;
               else
                  for I in Vect'Range loop
                     Translate_Static_Array_Aggregate_1
                       (List, Get_Associated_Expr (Vect (I)),
                        Aggr_Type, Dim + 1);
                  end loop;
               end if;
            end;
         when Iir_Kind_String_Literal8 =>
            pragma Assert (Dim = Nbr_Dims);
            Translate_Static_String_Literal8_Inner (List, Aggr, El_Type);
         when others =>
            Error_Kind ("translate_static_array_aggregate_1", Aggr);
      end case;
   end Translate_Static_Array_Aggregate_1;

   function Translate_Static_Aggregate (Aggr : Iir) return O_Cnode
   is
      Aggr_Type : constant Iir := Get_Type (Aggr);
      Res       : O_Cnode;
   begin
      Chap3.Translate_Anonymous_Subtype_Definition (Aggr_Type, False);
      case Get_Kind (Aggr_Type) is
         when Iir_Kind_Array_Subtype_Definition =>
            declare
               List : O_Array_Aggr_List;
            begin
               Start_Array_Aggr
                 (List, Get_Ortho_Type (Aggr_Type, Mode_Value),
                  Unsigned_32 (Chap3.Get_Static_Array_Length (Aggr_Type)));

               Translate_Static_Array_Aggregate_1 (List, Aggr, Aggr_Type, 1);
               Finish_Array_Aggr (List, Res);
            end;
         when Iir_Kind_Record_Type_Definition
           | Iir_Kind_Record_Subtype_Definition =>
            declare
               Btype : constant Iir := Get_Base_Type (Aggr_Type);
               Bels : constant Iir_Flist :=
                 Get_Elements_Declaration_List (Btype);
               Assocs : constant Iir := Get_Association_Choices_Chain (Aggr);
               List : O_Record_Aggr_List;
               Assoc : Iir;
               El : Iir;
               Bel : Iir;
            begin
               Start_Record_Aggr
                 (List, Get_Ortho_Type (Aggr_Type, Mode_Value));
               --  First elements declared with a fully-bounded subtype,
               --  then unbounded elements.
               for Static in reverse Boolean loop
                  Assoc := Assocs;
                  for I in Flist_First .. Flist_Last (Bels) loop
                     pragma Assert
                       (Get_Kind (Assoc) = Iir_Kind_Choice_By_None);
                     Bel := Get_Nth_Element (Bels, I);
                     if Is_Static_Type (Get_Info (Get_Type (Bel))) = Static
                     then
                        El := Get_Associated_Expr (Assoc);
                        New_Record_Aggr_El
                          (List,
                           Translate_Static_Expression (El, Get_Type (El)));
                     end if;
                     Assoc := Get_Chain (Assoc);
                  end loop;
               end loop;
               Finish_Record_Aggr (List, Res);
            end;
         when others =>
            raise Internal_Error;
      end case;
      return Res;
   end Translate_Static_Aggregate;

   function Translate_Static_Simple_Aggregate (Aggr : Iir) return O_Cnode
   is
      Aggr_Type : constant Iir := Get_Type (Aggr);
      El_List   : constant Iir_Flist := Get_Simple_Aggregate_List (Aggr);
      El_Type   : constant Iir := Get_Element_Subtype (Aggr_Type);
      El        : Iir;
      List      : O_Array_Aggr_List;
      Res       : O_Cnode;
   begin
      Chap3.Translate_Anonymous_Subtype_Definition (Aggr_Type, False);
      Start_Array_Aggr (List,
                        Get_Ortho_Type (Aggr_Type, Mode_Value),
                        Unsigned_32 (Get_Nbr_Elements (El_List)));

      for I in Flist_First .. Flist_Last (El_List) loop
         El := Get_Nth_Element (El_List, I);
         New_Array_Aggr_El
           (List, Translate_Static_Expression (El, El_Type));
      end loop;

      Finish_Array_Aggr (List, Res);
      return Res;
   end Translate_Static_Simple_Aggregate;

   function Translate_Static_String_Literal8 (Str : Iir) return O_Cnode
   is
      Lit_Type     : constant Iir := Get_Type (Str);
      Element_Type : constant Iir := Get_Element_Subtype (Lit_Type);
      Arr_Type     : O_Tnode;
      List         : O_Array_Aggr_List;
      Res          : O_Cnode;
   begin
      Chap3.Translate_Anonymous_Subtype_Definition (Lit_Type, False);
      Arr_Type := Get_Ortho_Type (Lit_Type, Mode_Value);

      Start_Array_Aggr
        (List, Arr_Type,
         Unsigned_32 (Chap3.Get_Static_Array_Length (Lit_Type)));

      Translate_Static_String_Literal8_Inner (List, Str, Element_Type);

      Finish_Array_Aggr (List, Res);
      return Res;
   end Translate_Static_String_Literal8;

   --  Create a variable (constant) for string or bit string literal STR.
   --  The type of the literal element is ELEMENT_TYPE, and the ortho type
   --  of the string (a constrained array type) is STR_TYPE.
   function Create_String_Literal_Var_Inner
     (Str : Iir; Element_Type : Iir; Arr_Type : O_Tnode) return Var_Type
   is
      Val_Aggr : O_Array_Aggr_List;
      Res      : O_Cnode;
   begin
      Start_Array_Aggr
        (Val_Aggr, Arr_Type, Unsigned_32 (Get_String_Length (Str)));
      case Get_Kind (Str) is
         when Iir_Kind_String_Literal8 =>
            Translate_Static_String_Literal8_Inner
              (Val_Aggr, Str, Element_Type);
         when others =>
            raise Internal_Error;
      end case;
      Finish_Array_Aggr (Val_Aggr, Res);

      return Create_Global_Const
        (Create_Uniq_Identifier, Arr_Type, O_Storage_Private, Res);
   end Create_String_Literal_Var_Inner;

   --  Create a variable (constant) for string or bit string literal STR.
   function Create_String_Literal_Var (Str : Iir) return Var_Type
   is
      Str_Type : constant Iir := Get_Type (Str);
      El_Type : constant Iir := Get_Element_Subtype (Str_Type);
      Arr_Type : O_Tnode;
      Arr_St   : O_Tnode;
   begin
      --  Create the string value.
      Arr_Type := Get_Info (Str_Type).B.Base_Type (Mode_Value);
      Arr_St := New_Array_Subtype
        (Arr_Type,
         Get_Ortho_Type (El_Type, Mode_Value),
         New_Index_Lit (Unsigned_64 (Get_String_Length (Str))));
      return Create_String_Literal_Var_Inner (Str, El_Type, Arr_St);
   end Create_String_Literal_Var;

   --  Some strings literal have an unconstrained array type,
   --  eg: 'image of constant.  Its type is not constrained
   --  because it is not so in VHDL!
   function Translate_Non_Static_String_Literal (Str : Iir) return O_Enode
   is
      Len             : constant Nat32 := Get_String_Length (Str);
      Lit_Type        : constant Iir := Get_Type (Str);
      Type_Info       : constant Type_Info_Acc := Get_Info (Lit_Type);
      Index_Type      : constant Iir := Get_Index_Type (Lit_Type, 0);
      Index_Type_Info : constant Type_Info_Acc := Get_Info (Index_Type);
      Bound_Aggr      : O_Record_Aggr_List;
      Index_Aggr      : O_Record_Aggr_List;
      Res_Aggr        : O_Record_Aggr_List;
      Res             : O_Cnode;
      Val             : Var_Type;
      Bound           : Var_Type;
      R               : O_Enode;
   begin
      --  Create the string value.
      Val := Create_String_Literal_Var (Str);

      if Type_Info.Type_Mode = Type_Mode_Fat_Array then
         --  Create the string bound.
         Start_Record_Aggr (Bound_Aggr, Type_Info.B.Bounds_Type);
         Start_Record_Aggr (Index_Aggr, Index_Type_Info.B.Range_Type);
         New_Record_Aggr_El
           (Index_Aggr,
            New_Signed_Literal
              (Index_Type_Info.Ortho_Type (Mode_Value), 1));
         New_Record_Aggr_El
           (Index_Aggr,
            New_Signed_Literal (Index_Type_Info.Ortho_Type (Mode_Value),
              Integer_64 (Len)));
         New_Record_Aggr_El
           (Index_Aggr, Ghdl_Dir_To_Node);
         New_Record_Aggr_El
           (Index_Aggr,
            New_Unsigned_Literal (Ghdl_Index_Type, Unsigned_64 (Len)));
         Finish_Record_Aggr (Index_Aggr, Res);
         New_Record_Aggr_El (Bound_Aggr, Res);
         Finish_Record_Aggr (Bound_Aggr, Res);
         Bound := Create_Global_Const
           (Create_Uniq_Identifier, Type_Info.B.Bounds_Type,
            O_Storage_Private, Res);

         --  The descriptor.
         Start_Record_Aggr (Res_Aggr, Type_Info.Ortho_Type (Mode_Value));
         New_Record_Aggr_El
           (Res_Aggr,
            New_Global_Address (New_Global (Get_Var_Label (Val)),
                                Type_Info.B.Base_Ptr_Type (Mode_Value)));
         New_Record_Aggr_El
           (Res_Aggr,
            New_Global_Address (New_Global (Get_Var_Label (Bound)),
                                Type_Info.B.Bounds_Ptr_Type));
         Finish_Record_Aggr (Res_Aggr, Res);

         Val := Create_Global_Const
           (Create_Uniq_Identifier, Type_Info.Ortho_Type (Mode_Value),
            O_Storage_Private, Res);
      elsif Type_Info.Type_Mode in Type_Mode_Bounded_Arrays then
         --  Type of string literal isn't statically known; check the
         --  length.
         Chap6.Check_Bound_Error
           (New_Compare_Op
              (ON_Neq,
               New_Lit (New_Index_Lit (Unsigned_64 (Len))),
               Chap3.Get_Array_Type_Length (Lit_Type),
               Ghdl_Bool_Type),
            Str);
      else
         raise Internal_Error;
      end if;

      R := New_Address (Get_Var (Val),
                        Type_Info.Ortho_Ptr_Type (Mode_Value));
      return R;
   end Translate_Non_Static_String_Literal;

   --  Only for Strings of STD.Character.
   function Translate_Static_String (Str_Type : Iir; Str_Ident : Name_Id)
                                    return O_Cnode
   is
      Img : constant String := Name_Table.Image (Str_Ident);
      Literal_List : constant Iir_Flist :=
        Get_Enumeration_Literal_List (Character_Type_Definition);
      Lit          : Iir;
      List         : O_Array_Aggr_List;
      Res          : O_Cnode;
   begin
      Chap3.Translate_Anonymous_Subtype_Definition (Str_Type, False);

      Start_Array_Aggr
        (List, Get_Ortho_Type (Str_Type, Mode_Value), Img'Length);

      for I in Img'Range loop
         Lit := Get_Nth_Element (Literal_List, Character'Pos (Img (I)));
         New_Array_Aggr_El (List, Get_Ortho_Literal (Lit));
      end loop;

      Finish_Array_Aggr (List, Res);
      return Res;
   end Translate_Static_String;

   function Translate_Composite_Literal (Str : Iir; Res_Type : Iir)
                                        return O_Enode
   is
      Str_Type : constant Iir := Get_Type (Str);
      Is_Array : constant Boolean :=
        Get_Kind (Str_Type) = Iir_Kind_Array_Subtype_Definition;
      Is_Static : Boolean;
      Vtype : Iir;
      Var      : Var_Type;
      Info     : Type_Info_Acc;
      Res      : O_Cnode;
      R        : O_Enode;
   begin
      if Get_Constraint_State (Str_Type) = Fully_Constrained
        and then (not Is_Array
                    or else Are_Array_Indexes_Locally_Static (Str_Type))
      then
         Chap3.Create_Composite_Subtype (Str_Type);
         case Get_Kind (Str) is
            when Iir_Kind_String_Literal8 =>
               Res := Translate_Static_String_Literal8 (Str);
            when Iir_Kind_Simple_Aggregate =>
               Res := Translate_Static_Simple_Aggregate (Str);
            when Iir_Kind_Simple_Name_Attribute =>
               Res := Translate_Static_String
                 (Get_Type (Str), Get_Simple_Name_Identifier (Str));
            when Iir_Kind_Aggregate =>
               Res := Translate_Static_Aggregate (Str);
            when others =>
               raise Internal_Error;
         end case;
         Is_Static := not Is_Array
           or else Are_Array_Indexes_Locally_Static (Res_Type);

         if Is_Static then
            Res := Translate_Static_Implicit_Conv (Res, Str_Type, Res_Type);
            Vtype := Res_Type;
         else
            Vtype := Str_Type;
         end if;
         Info := Get_Info (Vtype);
         Var := Create_Global_Const
           (Create_Uniq_Identifier, Info.Ortho_Type (Mode_Value),
            O_Storage_Private, Res);
         R := New_Address (Get_Var (Var), Info.Ortho_Ptr_Type (Mode_Value));
         if not Is_Static then
            R := Translate_Implicit_Conv
              (R, Str_Type, Res_Type, Mode_Value, Str);
         end if;
         return R;
      else
         return Translate_Implicit_Conv
           (Translate_Non_Static_String_Literal (Str), Str_Type, Res_Type,
            Mode_Value, Str);
      end if;
   end Translate_Composite_Literal;

   function Translate_Enumeration_Literal (Atype : Iir; Pos : Natural)
                                          return O_Cnode
   is
      Lit_List : constant Iir_Flist :=
        Get_Enumeration_Literal_List (Get_Base_Type (Atype));
      Enum : constant Iir := Get_Nth_Element (Lit_List, Pos);
   begin
      return Get_Ortho_Literal (Enum);
   end Translate_Enumeration_Literal;

   function Translate_Numeric_Literal (Expr : Iir; Res_Type : O_Tnode)
                                      return O_Cnode is
   begin
      case Get_Kind (Expr) is
         when Iir_Kind_Integer_Literal =>
            return New_Signed_Literal
              (Res_Type, Integer_64 (Get_Value (Expr)));

         when Iir_Kind_Enumeration_Literal =>
            return Translate_Enumeration_Literal
              (Get_Type (Expr), Natural (Get_Enum_Pos (Expr)));

         when Iir_Kind_Floating_Point_Literal =>
            return New_Float_Literal
              (Res_Type, IEEE_Float_64 (Get_Fp_Value (Expr)));

         when Iir_Kind_Physical_Int_Literal
            | Iir_Kind_Physical_Fp_Literal
            | Iir_Kind_Unit_Declaration =>
            return New_Signed_Literal
              (Res_Type, Integer_64 (Get_Physical_Value (Expr)));

         when others =>
            Error_Kind ("translate_numeric_literal", Expr);
      end case;
   exception
      when Constraint_Error =>
         --  Can be raised by Get_Physical_Value.
         Error_Msg_Elab (Expr, "numeric literal not in range");
         return New_Signed_Literal (Res_Type, 0);
   end Translate_Numeric_Literal;

   function Translate_Numeric_Literal (Expr : Iir; Res_Type : Iir)
                                      return O_Cnode
   is
      Expr_Type  : constant Iir := Get_Type (Expr);
      Expr_Otype : O_Tnode;
      Tinfo      : Type_Info_Acc;
   begin
      Tinfo := Get_Info (Expr_Type);
      if Res_Type /= Null_Iir then
         Expr_Otype := Get_Ortho_Type (Res_Type, Mode_Value);
      else
         if Tinfo = null then
            --  FIXME: this is a working kludge, in the case where EXPR_TYPE
            --  is a subtype which was not yet translated.
            --  (eg: evaluated array attribute)
            Tinfo := Get_Info (Get_Base_Type (Expr_Type));
         end if;
         Expr_Otype := Tinfo.Ortho_Type (Mode_Value);
      end if;
      return Translate_Numeric_Literal (Expr, Expr_Otype);
   end Translate_Numeric_Literal;

   function Translate_Null_Literal (Expr : Iir; Res_Type : Iir)
                                   return O_Cnode
   is
      pragma Unreferenced (Expr);
      Tinfo : constant Type_Info_Acc := Get_Info (Res_Type);
      Otype : constant O_Tnode := Tinfo.Ortho_Type (Mode_Value);
   begin
      return New_Null_Access (Otype);
   end Translate_Null_Literal;

   function Translate_Static_Expression (Expr : Iir; Res_Type : Iir)
                                        return O_Cnode
   is
      Expr_Type : constant Iir := Get_Type (Expr);
   begin
      case Get_Kind (Expr) is
         when Iir_Kind_Null_Literal =>
            return Translate_Null_Literal (Expr, Res_Type);

         when Iir_Kind_Integer_Literal
            | Iir_Kind_Enumeration_Literal
            | Iir_Kind_Floating_Point_Literal
            | Iir_Kind_Physical_Int_Literal
            | Iir_Kind_Unit_Declaration
            | Iir_Kind_Physical_Fp_Literal =>
            return Translate_Numeric_Literal (Expr, Res_Type);

         when Iir_Kind_String_Literal8 =>
            return Translate_Static_Implicit_Conv
              (Translate_Static_String_Literal8 (Expr),
               Expr_Type, Res_Type);
         when Iir_Kind_Simple_Aggregate =>
            return Translate_Static_Implicit_Conv
              (Translate_Static_Simple_Aggregate (Expr),
               Expr_Type, Res_Type);
         when Iir_Kind_Aggregate =>
            return Translate_Static_Implicit_Conv
              (Translate_Static_Aggregate (Expr), Expr_Type, Res_Type);

         when Iir_Kinds_Denoting_Name =>
            return Translate_Static_Expression
              (Get_Named_Entity (Expr), Res_Type);
         when others =>
            Error_Kind ("translate_static_expression", Expr);
      end case;
   end Translate_Static_Expression;

   function Translate_Static_Range_Left
     (Expr : Iir; Range_Type : Iir := Null_Iir) return O_Cnode
   is
      Bound : constant Iir := Get_Left_Limit (Expr);
      Left  : O_Cnode;
   begin
      Left := Chap7.Translate_Static_Expression (Bound, Range_Type);
      --  if Range_Type /= Null_Iir
      --    and then Get_Type (Bound) /= Range_Type then
      --   Left := New_Convert_Ov
      --      (Left, Get_Ortho_Type (Range_Type, Mode_Value));
      --  end if;
      return Left;
   end Translate_Static_Range_Left;

   function Translate_Static_Range_Right
     (Expr : Iir; Range_Type : Iir := Null_Iir) return O_Cnode
   is
      Right : O_Cnode;
   begin
      Right := Chap7.Translate_Static_Expression (Get_Right_Limit (Expr),
                                                  Range_Type);
      --          if Range_Type /= Null_Iir then
      --             Right := New_Convert_Ov
      --               (Right, Get_Ortho_Type (Range_Type, Mode_Value));
      --          end if;
      return Right;
   end Translate_Static_Range_Right;

   function Translate_Static_Range_Dir (Expr : Iir) return O_Cnode is
   begin
      case Get_Direction (Expr) is
         when Dir_To =>
            return Ghdl_Dir_To_Node;
         when Dir_Downto =>
            return Ghdl_Dir_Downto_Node;
      end case;
   end Translate_Static_Range_Dir;

   function Translate_Static_Range_Length (Expr : Iir) return O_Cnode
   is
      Ulen : Unsigned_64;
   begin
      Ulen := Unsigned_64 (Eval_Discrete_Range_Length (Expr));
      return New_Unsigned_Literal (Ghdl_Index_Type, Ulen);
   end Translate_Static_Range_Length;

   function Translate_Range_Expression_Left
     (Expr : Iir; Range_Type : Iir := Null_Iir) return O_Enode
   is
      Left : O_Enode;
   begin
      Left := Chap7.Translate_Expression (Get_Left_Limit (Expr));
      if Range_Type /= Null_Iir then
         Left := New_Convert_Ov (Left,
                                 Get_Ortho_Type (Range_Type, Mode_Value));
      end if;
      return Left;
   end Translate_Range_Expression_Left;

   function Translate_Range_Expression_Right
     (Expr : Iir; Range_Type : Iir := Null_Iir) return O_Enode
   is
      Right : O_Enode;
   begin
      Right := Chap7.Translate_Expression (Get_Right_Limit (Expr));
      if Range_Type /= Null_Iir then
         Right := New_Convert_Ov (Right,
                                  Get_Ortho_Type (Range_Type, Mode_Value));
      end if;
      return Right;
   end Translate_Range_Expression_Right;

   --  Compute the length of LEFT DIR (to/downto) RIGHT.
   function Compute_Range_Length
     (Left : O_Enode; Right : O_Enode; Dir : Direction_Type) return O_Enode
   is
      Rng_Type : constant O_Tnode := Ghdl_I32_Type;
      L        : constant O_Enode := New_Convert_Ov (Left, Rng_Type);
      R        : constant O_Enode := New_Convert_Ov (Right, Rng_Type);
      Val      : O_Enode;
      Tmp      : O_Dnode;
      Res      : O_Dnode;
      If_Blk   : O_If_Block;
   begin
      case Dir is
         when Dir_To =>
            Val := New_Dyadic_Op (ON_Sub_Ov, R, L);
         when Dir_Downto =>
            Val := New_Dyadic_Op (ON_Sub_Ov, L, R);
      end case;

      Res := Create_Temp (Ghdl_Index_Type);
      Open_Temp;
      Tmp := Create_Temp (Rng_Type);
      New_Assign_Stmt (New_Obj (Tmp), Val);
      Start_If_Stmt
        (If_Blk,
         New_Compare_Op (ON_Lt, New_Obj_Value (Tmp),
                         New_Lit (New_Signed_Literal (Rng_Type, 0)),
                         Ghdl_Bool_Type));
      Init_Var (Res);
      New_Else_Stmt (If_Blk);
      Val := New_Convert_Ov (New_Obj_Value (Tmp), Ghdl_Index_Type);
      Val := New_Dyadic_Op (ON_Add_Ov, Val, New_Lit (Ghdl_Index_1));
      New_Assign_Stmt (New_Obj (Res), Val);
      Finish_If_Stmt (If_Blk);
      Close_Temp;
      return New_Obj_Value (Res);
   end Compute_Range_Length;

   function Translate_Range_Expression_Length (Expr : Iir) return O_Enode
   is
      Left, Right : O_Enode;
   begin
      if Get_Expr_Staticness (Expr) = Locally then
         return New_Lit (Translate_Static_Range_Length (Expr));
      else
         Left := Chap7.Translate_Expression (Get_Left_Limit (Expr));
         Right := Chap7.Translate_Expression (Get_Right_Limit (Expr));

         return Compute_Range_Length (Left, Right, Get_Direction (Expr));
      end if;
   end Translate_Range_Expression_Length;

   function Translate_Range_Length (Expr : Iir) return O_Enode is
   begin
      case Get_Kind (Expr) is
         when Iir_Kind_Range_Expression =>
            return Translate_Range_Expression_Length (Expr);
         when Iir_Kind_Range_Array_Attribute =>
            return Chap14.Translate_Length_Array_Attribute (Expr, Null_Iir);
         when others =>
            Error_Kind ("translate_range_length", Expr);
      end case;
   end Translate_Range_Length;

   function Translate_Operator_Function_Call
     (Call : Iir; Left : Iir;  Right : Iir; Res_Type : Iir) return O_Enode
   is
      Imp : constant Iir := Get_Implementation (Call);

      function Create_Assoc (Actual : Iir) return Iir
      is
         R : Iir;
      begin
         R := Create_Iir (Iir_Kind_Association_Element_By_Expression);
         Location_Copy (R, Actual);
         Set_Actual (R, Actual);
         return R;
      end Create_Assoc;

      El_L  : Iir;
      El_R  : Iir;
      Res   : O_Enode;
   begin
      El_L := Create_Assoc (Left);
      if Right /= Null_Iir then
         El_R := Create_Assoc (Right);
         Set_Chain (El_L, El_R);
      end if;

      Res := Chap8.Translate_Subprogram_Call (Call, El_L, Null_Iir);

      Free_Iir (El_L);
      if Right /= Null_Iir then
         Free_Iir (El_R);
      end if;

      return Translate_Implicit_Conv
        (Res, Get_Return_Type (Imp), Res_Type, Mode_Value, Left);
   end Translate_Operator_Function_Call;

   procedure Convert_Constrained_To_Unconstrained
     (Res : in out Mnode; Expr : Mnode)
   is
      Type_Info   : constant Type_Info_Acc := Get_Type_Info (Res);
      Kind        : constant Object_Kind_Type := Get_Object_Kind (Expr);
      Stable_Expr : Mnode;
   begin
      Stable_Expr := Stabilize (Expr);
      New_Assign_Stmt
        (M2Lp (Chap3.Get_Composite_Base (Res)),
         New_Convert_Ov (M2Addr (Chap3.Get_Composite_Base (Stable_Expr)),
           Type_Info.B.Base_Ptr_Type (Kind)));
      New_Assign_Stmt
        (M2Lp (Chap3.Get_Composite_Bounds (Res)),
         M2Addr (Chap3.Get_Composite_Bounds (Stable_Expr)));
   end Convert_Constrained_To_Unconstrained;

   function Convert_Constrained_To_Unconstrained
     (Expr : Mnode; Res_Tinfo : Type_Info_Acc) return Mnode
   is
      Mode : constant Object_Kind_Type := Get_Object_Kind (Expr);
      Res  : Mnode;
   begin
      Res := Create_Temp (Res_Tinfo, Mode);
      Convert_Constrained_To_Unconstrained (Res, Expr);
      return Res;
   end Convert_Constrained_To_Unconstrained;

   --  Innert procedure for Convert_Unconstrained_To_Constrained.
   procedure Convert_To_Constrained_Check
     (Bounds : Mnode; Expr_Type : Iir; Atype : Iir; Failure_Label : O_Snode)
   is
      Stable_Bounds : Mnode;
   begin
      Open_Temp;
      Stable_Bounds := Stabilize (Bounds);
      case Get_Kind (Expr_Type) is
         when Iir_Kind_Array_Type_Definition
           | Iir_Kind_Array_Subtype_Definition =>
            declare
               Expr_Indexes  : constant Iir_Flist :=
                 Get_Index_Subtype_List (Expr_Type);
            begin
               for I in 1 .. Get_Nbr_Elements (Expr_Indexes) loop
                  Gen_Exit_When
                    (Failure_Label,
                     New_Compare_Op
                       (ON_Neq,
                        M2E (Chap3.Range_To_Length
                               (Chap3.Bounds_To_Range
                                  (Stable_Bounds, Expr_Type, I))),
                        Chap6.Get_Array_Bound_Length
                          (T2M (Atype, Mode_Value), Atype, I),
                        Ghdl_Bool_Type));
               end loop;
            end;
         when Iir_Kind_Record_Type_Definition
           | Iir_Kind_Record_Subtype_Definition =>
            declare
               Expr_Els : constant Iir_Flist :=
                 Get_Elements_Declaration_List (Expr_Type);
               Atype_Els : constant Iir_Flist :=
                 Get_Elements_Declaration_List (Atype);
               Expr_El, Atype_El : Iir;
               Expr_El_Type, Atype_El_Type : Iir;
            begin
               for I in Flist_First .. Flist_Last (Expr_Els) loop
                  Expr_El := Get_Nth_Element (Expr_Els, I);
                  Atype_El := Get_Nth_Element (Atype_Els, I);
                  Expr_El_Type := Get_Type (Expr_El);
                  Atype_El_Type := Get_Type (Atype_El);
                  if Expr_El_Type /= Atype_El_Type then
                     Convert_To_Constrained_Check
                       (Chap3.Record_Bounds_To_Element_Bounds
                          (Stable_Bounds, Expr_El),
                        Expr_El_Type, Atype_El_Type, Failure_Label);
                  end if;
               end loop;
            end;
         when others =>
            Error_Kind ("convert_unconstrained_to_constrained_check",
                        Expr_Type);
      end case;
      Close_Temp;
   end Convert_To_Constrained_Check;

   function Convert_To_Constrained
     (Expr : Mnode; Expr_Type : Iir; Atype : Iir; Loc : Iir) return Mnode
   is
      Parent_Type : Iir;
      Expr_Stable   : Mnode;
      Success_Label : O_Snode;
      Failure_Label : O_Snode;
   begin
      --  If ATYPE is a parent type of EXPR_TYPE, then all the constrained
      --  are inherited and there is nothing to check.
      Parent_Type := Expr_Type;
      loop
         if Parent_Type = Atype then
            return Expr;
         end if;
         exit when (Get_Kind (Parent_Type)
                    not in Iir_Kinds_Composite_Subtype_Definition);
         Parent_Type := Get_Parent_Type (Parent_Type);
      end loop;

      Expr_Stable := Stabilize (Expr);

      Open_Temp;
      --  Check each dimension.
      Start_Loop_Stmt (Success_Label);
      Start_Loop_Stmt (Failure_Label);

      Convert_To_Constrained_Check
        (Chap3.Get_Composite_Bounds (Expr_Stable), Expr_Type,
         Atype, Failure_Label);

      New_Exit_Stmt (Success_Label);

      Finish_Loop_Stmt (Failure_Label);
      Chap6.Gen_Bound_Error (Loc);
      Finish_Loop_Stmt (Success_Label);
      Close_Temp;

      declare
         Ainfo : constant Type_Info_Acc := Get_Info (Atype);
         Kind : constant Object_Kind_Type := Get_Object_Kind (Expr);
         Nptr : O_Enode;
      begin
         --  Pointer to the array.
         Nptr := M2E (Chap3.Get_Composite_Base (Expr_Stable));
         --  Convert it to pointer to the constrained type.
         Nptr := New_Convert_Ov (Nptr, Ainfo.Ortho_Ptr_Type (Kind));
         return E2M (Nptr, Ainfo, Kind);
      end;
   end Convert_To_Constrained;

   function Translate_Implicit_Array_Conversion
     (Expr : Mnode; Expr_Type : Iir; Res_Type : Iir; Loc : Iir) return Mnode
   is
      Res_Tinfo : Type_Info_Acc;
      Einfo : Type_Info_Acc;
      Mode  : Object_Kind_Type;
   begin
      pragma Assert
        (Get_Kind (Expr_Type) in Iir_Kinds_Array_Type_Definition);

      if Res_Type = Expr_Type then
         return Expr;
      end if;

      Res_Tinfo := Get_Info (Res_Type);
      Einfo := Get_Info (Expr_Type);
      case Res_Tinfo.Type_Mode is
         when Type_Mode_Unbounded_Array =>
            --  X to unconstrained.
            case Einfo.Type_Mode is
               when Type_Mode_Unbounded_Array =>
                  --  unconstrained to unconstrained.
                  return Expr;
               when Type_Mode_Bounded_Arrays =>
                  --  constrained to unconstrained.
                  return Convert_Constrained_To_Unconstrained
                    (Expr, Res_Tinfo);
               when others =>
                  raise Internal_Error;
            end case;
         when Type_Mode_Static_Array =>
            if Einfo.Type_Mode = Type_Mode_Static_Array then
               --  FIXME: optimize static vs non-static
               --  constrained to constrained.
               if Chap3.Locally_Types_Match (Expr_Type, Res_Type) /= True then
                  --  FIXME: generate a bound error ?
                  --  Even if this is caught at compile-time,
                  --  the code is not required to run.
                  Chap6.Gen_Bound_Error (Loc);
               end if;
               --  Convert.  For subtypes of arrays with unbounded elements,
               --  the subtype can be the same but the ortho type can be
               --  different.
               Mode := Get_Object_Kind (Expr);
               return E2M (New_Convert_Ov (M2Addr (Expr),
                                           Res_Tinfo.Ortho_Ptr_Type (Mode)),
                           Res_Tinfo, Mode);
            else
               --  Unbounded/bounded array to bounded array.
               return Convert_To_Constrained (Expr, Expr_Type, Res_Type, Loc);
            end if;
         when Type_Mode_Complex_Array =>
            return Convert_To_Constrained (Expr, Expr_Type, Res_Type, Loc);
         when others =>
            raise Internal_Error;
      end case;
   end Translate_Implicit_Array_Conversion;

   function Translate_Implicit_Record_Conversion
     (Expr : Mnode; Expr_Type : Iir; Res_Type : Iir; Loc : Iir) return Mnode
   is
      Res_Tinfo : Type_Info_Acc;
      Einfo : Type_Info_Acc;
   begin
      if Res_Type = Expr_Type then
         return Expr;
      end if;

      Res_Tinfo := Get_Info (Res_Type);
      Einfo := Get_Info (Expr_Type);
      case Res_Tinfo.Type_Mode is
         when Type_Mode_Unbounded_Record =>
            --  X to unbounded.
            case Einfo.Type_Mode is
               when Type_Mode_Unbounded_Record =>
                  --  unbounded to unbounded
                  return Expr;
               when Type_Mode_Bounded_Records =>
                  --  bounded to unconstrained.
                  return Convert_Constrained_To_Unconstrained
                    (Expr, Res_Tinfo);
               when others =>
                  raise Internal_Error;
            end case;
         when Type_Mode_Bounded_Records =>
            --  X to bounded
            return Convert_To_Constrained (Expr, Expr_Type, Res_Type, Loc);
         when others =>
            raise Internal_Error;
      end case;
   end Translate_Implicit_Record_Conversion;

   --  Convert (if necessary) EXPR translated from EXPR_ORIG to type ATYPE.
   function Translate_Implicit_Conv (Expr      : O_Enode;
                                     Expr_Type : Iir;
                                     Atype     : Iir;
                                     Is_Sig    : Object_Kind_Type;
                                     Loc       : Iir)
                                    return O_Enode is
   begin
      --  Same type: nothing to do.
      if Atype = Expr_Type then
         return Expr;
      end if;

      if Expr_Type = Universal_Integer_Type_Definition then
         return New_Convert_Ov (Expr, Get_Ortho_Type (Atype, Mode_Value));
      elsif Expr_Type = Universal_Real_Type_Definition then
         return New_Convert_Ov (Expr, Get_Ortho_Type (Atype, Mode_Value));
      else
         case Get_Kind (Expr_Type) is
            when Iir_Kinds_Array_Type_Definition =>
               return M2E (Translate_Implicit_Array_Conversion
                             (E2M (Expr, Get_Info (Expr_Type), Is_Sig),
                              Expr_Type, Atype, Loc));
            when Iir_Kind_Record_Type_Definition
              | Iir_Kind_Record_Subtype_Definition =>
               return M2E (Translate_Implicit_Record_Conversion
                             (E2M (Expr, Get_Info (Expr_Type), Is_Sig),
                              Expr_Type, Atype, Loc));
            when others =>
               return Expr;
         end case;
      end if;
   end Translate_Implicit_Conv;

   type Predefined_To_Onop_Type is
     array (Iir_Predefined_Functions) of ON_Op_Kind;
   Predefined_To_Onop : constant Predefined_To_Onop_Type :=
     (Iir_Predefined_Boolean_Or => ON_Or,
      Iir_Predefined_Boolean_Not => ON_Not,
      Iir_Predefined_Boolean_And => ON_And,
      Iir_Predefined_Boolean_Xor => ON_Xor,

      Iir_Predefined_Bit_Not => ON_Not,
      Iir_Predefined_Bit_And => ON_And,
      Iir_Predefined_Bit_Or => ON_Or,
      Iir_Predefined_Bit_Xor => ON_Xor,

      Iir_Predefined_Integer_Equality => ON_Eq,
      Iir_Predefined_Integer_Inequality => ON_Neq,
      Iir_Predefined_Integer_Less_Equal => ON_Le,
      Iir_Predefined_Integer_Less => ON_Lt,
      Iir_Predefined_Integer_Greater => ON_Gt,
      Iir_Predefined_Integer_Greater_Equal => ON_Ge,
      Iir_Predefined_Integer_Plus => ON_Add_Ov,
      Iir_Predefined_Integer_Minus => ON_Sub_Ov,
      Iir_Predefined_Integer_Mul => ON_Mul_Ov,
      Iir_Predefined_Integer_Rem => ON_Rem_Ov,
      Iir_Predefined_Integer_Mod => ON_Mod_Ov,
      Iir_Predefined_Integer_Div => ON_Div_Ov,
      Iir_Predefined_Integer_Absolute => ON_Abs_Ov,
      Iir_Predefined_Integer_Negation => ON_Neg_Ov,

      Iir_Predefined_Enum_Equality => ON_Eq,
      Iir_Predefined_Enum_Inequality => ON_Neq,
      Iir_Predefined_Enum_Greater_Equal => ON_Ge,
      Iir_Predefined_Enum_Greater => ON_Gt,
      Iir_Predefined_Enum_Less => ON_Lt,
      Iir_Predefined_Enum_Less_Equal => ON_Le,

      Iir_Predefined_Physical_Equality => ON_Eq,
      Iir_Predefined_Physical_Inequality => ON_Neq,
      Iir_Predefined_Physical_Less => ON_Lt,
      Iir_Predefined_Physical_Less_Equal => ON_Le,
      Iir_Predefined_Physical_Greater => ON_Gt,
      Iir_Predefined_Physical_Greater_Equal => ON_Ge,
      Iir_Predefined_Physical_Negation => ON_Neg_Ov,
      Iir_Predefined_Physical_Absolute => ON_Abs_Ov,
      Iir_Predefined_Physical_Minus => ON_Sub_Ov,
      Iir_Predefined_Physical_Plus => ON_Add_Ov,
      Iir_Predefined_Physical_Rem => ON_Rem_Ov,
      Iir_Predefined_Physical_Mod => ON_Mod_Ov,

      Iir_Predefined_Floating_Greater => ON_Gt,
      Iir_Predefined_Floating_Greater_Equal => ON_Ge,
      Iir_Predefined_Floating_Less => ON_Lt,
      Iir_Predefined_Floating_Less_Equal => ON_Le,
      Iir_Predefined_Floating_Equality => ON_Eq,
      Iir_Predefined_Floating_Inequality => ON_Neq,
      Iir_Predefined_Floating_Minus => ON_Sub_Ov,
      Iir_Predefined_Floating_Plus => ON_Add_Ov,
      Iir_Predefined_Floating_Mul => ON_Mul_Ov,
      Iir_Predefined_Floating_Div => ON_Div_Ov,
      Iir_Predefined_Floating_Negation => ON_Neg_Ov,
      Iir_Predefined_Floating_Absolute => ON_Abs_Ov,

      others => ON_Nil);

   function Translate_Shortcircuit_Operator
     (Imp : Iir_Function_Declaration; Left, Right : Iir) return O_Enode
   is
      Rtype    : Iir;
      Res      : O_Dnode;
      Res_Type : O_Tnode;
      If_Blk   : O_If_Block;
      Val      : Integer;
      V        : O_Cnode;
      Kind     : Iir_Predefined_Functions;
      Invert   : Boolean;
   begin
      Rtype := Get_Return_Type (Imp);
      Res_Type := Get_Ortho_Type (Rtype, Mode_Value);
      Res := Create_Temp (Res_Type);
      Open_Temp;
      New_Assign_Stmt (New_Obj (Res), Chap7.Translate_Expression (Left));
      Close_Temp;
      Kind := Get_Implicit_Definition (Imp);

      --  Short cut: RIGHT is the result (and must be evaluated) iff
      --  LEFT is equal to VAL (ie '0' or false for 0, '1' or true for 1).
      case Kind is
         when Iir_Predefined_Bit_And
            | Iir_Predefined_Boolean_And =>
            Invert := False;
            Val := 1;
         when Iir_Predefined_Bit_Nand
            | Iir_Predefined_Boolean_Nand =>
            Invert := True;
            Val := 1;
         when Iir_Predefined_Bit_Or
            | Iir_Predefined_Boolean_Or =>
            Invert := False;
            Val := 0;
         when Iir_Predefined_Bit_Nor
            | Iir_Predefined_Boolean_Nor =>
            Invert := True;
            Val := 0;
         when others =>
            Error_Kind ("translate_shortcircuit_operator", Kind);
      end case;

      V := Get_Ortho_Literal
        (Get_Nth_Element (Get_Enumeration_Literal_List (Rtype), Val));
      Start_If_Stmt (If_Blk,
                     New_Compare_Op (ON_Eq,
                       New_Obj_Value (Res), New_Lit (V),
                       Ghdl_Bool_Type));
      Open_Temp;
      New_Assign_Stmt (New_Obj (Res), Chap7.Translate_Expression (Right));
      Close_Temp;
      Finish_If_Stmt (If_Blk);
      if Invert then
         return New_Monadic_Op (ON_Not, New_Obj_Value (Res));
      else
         return New_Obj_Value (Res);
      end if;
   end Translate_Shortcircuit_Operator;

   function Translate_Lib_Operator (Left, Right : O_Enode; Func : O_Dnode)
                                   return O_Enode
   is
      Constr : O_Assoc_List;
   begin
      Start_Association (Constr, Func);
      New_Association (Constr, Left);
      if Right /= O_Enode_Null then
         New_Association (Constr, Right);
      end if;
      return New_Function_Call (Constr);
   end Translate_Lib_Operator;

   function Translate_Predefined_Lib_Operator
     (Left, Right : O_Enode; Func : Iir_Function_Declaration) return O_Enode
   is
      Info   : constant Operator_Info_Acc := Get_Info (Func);
      Constr : O_Assoc_List;
   begin
      Start_Association (Constr, Info.Operator_Node);
      Subprgs.Add_Subprg_Instance_Assoc (Constr, Info.Operator_Instance);
      New_Association (Constr, Left);
      if Right /= O_Enode_Null then
         New_Association (Constr, Right);
      end if;
      return New_Function_Call (Constr);
   end Translate_Predefined_Lib_Operator;

   function Translate_Predefined_Array_Operator
     (Left, Right : O_Enode; Func : Iir) return O_Enode
   is
      Info      : constant Type_Info_Acc := Get_Info (Get_Return_Type (Func));
      Func_Info : constant Operator_Info_Acc := Get_Info (Func);
      Res       : O_Dnode;
      Constr    : O_Assoc_List;
   begin
      Create_Temp_Stack2_Mark;
      Res := Create_Temp (Info.Ortho_Type (Mode_Value));
      Start_Association (Constr, Func_Info.Operator_Node);
      Subprgs.Add_Subprg_Instance_Assoc (Constr, Func_Info.Operator_Instance);
      New_Association (Constr,
                       New_Address (New_Obj (Res),
                                    Info.Ortho_Ptr_Type (Mode_Value)));
      New_Association (Constr, Left);
      if Right /= O_Enode_Null then
         New_Association (Constr, Right);
      end if;
      New_Procedure_Call (Constr);
      return New_Address (New_Obj (Res), Info.Ortho_Ptr_Type (Mode_Value));
   end Translate_Predefined_Array_Operator;

   function Translate_Predefined_Array_Operator_Convert
     (Left, Right : O_Enode; Func : Iir; Res_Type : Iir) return O_Enode
   is
      Ret_Type : constant Iir := Get_Return_Type (Func);
      Res      : O_Enode;
   begin
      Res := Translate_Predefined_Array_Operator (Left, Right, Func);
      return Translate_Implicit_Conv
        (Res, Ret_Type, Res_Type, Mode_Value, Func);
   end Translate_Predefined_Array_Operator_Convert;

   --  A somewhat complex operation...
   --
   --  Previously, concatenation was handled like any other operator.  This
   --  is not efficient as for a serie of concatenation (like A & B & C & D),
   --  this resulted in O(n**2) copies.  The current implementation handles
   --  many concatenations in a raw.
   function Translate_Concatenation
     (Concat_Imp : Iir; Left, Right : Iir; Res_Type : Iir) return O_Enode
   is
      Expr_Type  : constant Iir := Get_Return_Type (Concat_Imp);
      Index_Type : constant Iir := Get_Index_Type (Expr_Type, 0);
      El_Type    : constant Iir := Get_Element_Subtype (Expr_Type);
      Info       : constant Type_Info_Acc := Get_Info (Expr_Type);
      Is_Unbounded_El : constant Boolean :=
        not Is_Fully_Constrained_Type (El_Type);
      Static_Length : Int64 := 0;
      Nbr_Dyn_Expr : Natural := 0;

      type Handle_Acc is access procedure (E : Iir; Is_First : Boolean);
      type Handlers_Type is record
         Handle_El : Handle_Acc;
         Handle_Arr : Handle_Acc;
      end record;

      --  Call handlers for each leaf of LEFT CONCAT_IMP RIGHT.
      --  Handlers.Handle_Arr is called for array leaves, and
      --  Handlers.Handle_El for element leaves.
      procedure Walk (Handlers : Handlers_Type)
      is
         Walk_Handlers : Handlers_Type;
         Is_First : Boolean;

         --  Call handlers for each leaf of L IMP R.
         procedure Walk_Concat (Imp : Iir; L, R : Iir);

         --  Call handlers for each leaf of E (an array expression).  First
         --  check whether E is also a concatenation.
         procedure Walk_Arr (E : Iir)
         is
            Imp : Iir;
            Assocs : Iir;
         begin
            if Get_Kind (E) = Iir_Kind_Concatenation_Operator then
               Imp := Get_Implementation (E);
               if (Get_Implicit_Definition (Imp)
                     in Iir_Predefined_Concat_Functions)
                 and then Get_Return_Type (Imp) = Expr_Type
               then
                  Walk_Concat (Imp, Get_Left (E), Get_Right (E));
                  return;
               end if;
            elsif Get_Kind (E) = Iir_Kind_Function_Call then
               --  Also handle "&" (A, B)
               --  Note that associations are always 'simple': no formal, no
               --  default expression in implicit declarations.
               Imp := Get_Implementation (E);
               if (Get_Implicit_Definition (Imp)
                     in Iir_Predefined_Concat_Functions)
                 and then Get_Return_Type (Imp) = Expr_Type
               then
                  Assocs := Get_Parameter_Association_Chain (E);
                  Walk_Concat
                    (Imp,
                     Get_Actual (Assocs), Get_Actual (Get_Chain (Assocs)));
                  return;
               end if;
            end if;

            Walk_Handlers.Handle_Arr (E, Is_First);
            Is_First := False;
         end Walk_Arr;

         procedure Walk_Concat (Imp : Iir; L, R : Iir) is
         begin
            case Get_Implicit_Definition (Imp) is
               when Iir_Predefined_Array_Array_Concat =>
                  Walk_Arr (L);
                  Walk_Arr (R);
               when Iir_Predefined_Array_Element_Concat =>
                  Walk_Arr (L);
                  Walk_Handlers.Handle_El (R, False);
               when Iir_Predefined_Element_Array_Concat =>
                  Walk_Handlers.Handle_El (L, Is_First);
                  Is_First := False;
                  Walk_Arr (R);
               when Iir_Predefined_Element_Element_Concat =>
                  Walk_Handlers.Handle_El (L, Is_First);
                  Is_First := False;
                  Walk_Handlers.Handle_El (R, False);
               when others =>
                  raise Internal_Error;
            end case;
         end Walk_Concat;
      begin
         Walk_Handlers := Handlers;
         Is_First := True;
         Walk_Concat (Concat_Imp, Left, Right);
      end Walk;

      --  Return TRUE if the bounds of E are known at analysis time.
      function Is_Static_Arr (E : Iir) return Boolean
      is
         Etype : constant Iir := Get_Type (E);
      begin
         pragma Assert (Get_Base_Type (Etype) = Expr_Type);
         return Is_Fully_Constrained_Type (Etype)
           and then Get_Type_Staticness (Get_Index_Type (Etype, 0)) = Locally;
      end Is_Static_Arr;

      --  Pre_Walk: compute known static length and number of dynamic arrays.
      procedure Pre_Walk_El (E : Iir; Is_First : Boolean)
      is
         pragma Unreferenced (E);
      begin
         if Is_First and Is_Unbounded_El then
            --  Force evaluation of the first expression to get element bounds.
            Nbr_Dyn_Expr := Nbr_Dyn_Expr + 1;
         end if;
         Static_Length := Static_Length + 1;
      end Pre_Walk_El;

      procedure Pre_Walk_Arr (E : Iir; Is_First : Boolean)
      is
         Idx_Type : Iir;
      begin
         --  Three possibilities:
         --  * type is fully constrained, range is static, length is known
         --  * type is fully constrained, range is not static, length isn't
         --  * type is not constrained
         if Is_First and Is_Unbounded_El then
            --  Force evaluation of the first expression to get element bounds.
            Nbr_Dyn_Expr := Nbr_Dyn_Expr + 1;
         elsif Is_Static_Arr (E) then
            Idx_Type := Get_Index_Type (Get_Type (E), 0);
            Static_Length := Static_Length
              + Eval_Discrete_Range_Length (Get_Range_Constraint (Idx_Type));
         else
            Nbr_Dyn_Expr := Nbr_Dyn_Expr + 1;
         end if;
      end Pre_Walk_Arr;

      --  In order to declare Dyn_Mnodes (below), create a function that can
      --  be called now (not possible with procedures).
      function Call_Pre_Walk return Natural is
      begin
         Walk ((Pre_Walk_El'Access, Pre_Walk_Arr'Access));
         return Nbr_Dyn_Expr;
      end Call_Pre_Walk;

      --  Compute now the number of dynamic expressions.
      Nbr_Dyn_Expr1 : constant Natural := Call_Pre_Walk;
      pragma Assert (Nbr_Dyn_Expr1 = Nbr_Dyn_Expr);

      Var_Bounds : Mnode;
      Arr_Ptr : O_Dnode;
      Var_Arr : Mnode;
      Var_Length : O_Dnode;

      Var_Res : O_Dnode;
      Res : Mnode;

      --  Common subexpression: get the range of the result as a Mnode.
      function Get_Res_Range return Mnode is
      begin
         return Chap3.Bounds_To_Range (Var_Bounds, Expr_Type, 1);
      end Get_Res_Range;

      type Mnode_Array is array (1 .. Nbr_Dyn_Expr) of Mnode;
      Dyn_Mnodes : Mnode_Array;
      Dyn_I : Natural;
      E_Length : O_Enode;

      procedure Nil_El (E : Iir; Is_First : Boolean) is
      begin
         null;
      end Nil_El;

      procedure Eval_One (E : Iir; Res_Type : Iir; Copy_El_Layout : Boolean)
      is
         E_Val : O_Enode;
         Bnd : Mnode;
      begin
         Dyn_I := Dyn_I + 1;
         --  First, translate expression.
         E_Val := Translate_Expression (E, Res_Type);
         --  Then create Mnode (type info may be computed by
         --  translate_expression).
         Dyn_Mnodes (Dyn_I) :=
           Stabilize (E2M (E_Val, Get_Info (Res_Type), Mode_Value));

         if Copy_El_Layout then
            --  Copy layout.
            pragma Assert (Dyn_I = 1);
            Bnd := Chap3.Get_Composite_Bounds (Dyn_Mnodes (1));
            if Res_Type = Expr_Type then
               Bnd := Chap3.Array_Bounds_To_Element_Layout (Bnd, Expr_Type);
            end if;
            Gen_Memcpy
              (M2Addr (Chap3.Array_Bounds_To_Element_Layout
                         (Var_Bounds, Expr_Type)),
               M2Addr (Bnd),
               New_Lit (New_Sizeof (Get_Info (El_Type).B.Layout_Type,
                                    Ghdl_Index_Type)));
         end if;
      end Eval_One;

      procedure Eval_First_El (E : Iir; Is_First : Boolean) is
      begin
         if Is_First and then Is_Unbounded_El then
            Eval_One (E, El_Type, True);
         end if;
      end Eval_First_El;

      --  Evaluate a dynamic parameter.
      procedure Eval_Dyn_Arr (E : Iir; Is_First : Boolean) is
      begin
         if (Is_First and Is_Unbounded_El)
           or else not Is_Static_Arr (E)
         then
            Eval_One (E, Expr_Type, Is_First and then Is_Unbounded_El);
         end if;
      end Eval_Dyn_Arr;

      procedure Len_El (E : Iir; Is_First : Boolean)
      is
         pragma Unreferenced (E);
      begin
         if Is_First and Is_Unbounded_El then
            --  The first param is evaluated to get the element bounds, but
            --  its length is known.
            Dyn_I := Dyn_I + 1;
         end if;
      end Len_El;

      --  Add contribution to length of result from a dynamic parameter.
      procedure Len_Dyn_Arr (E : Iir; Is_First : Boolean)
      is
         Elen : O_Enode;
      begin
         if not Is_Static_Arr (E)
           or else (Is_First and Is_Unbounded_El)
         then
            Dyn_I := Dyn_I + 1;
            Elen := Chap3.Get_Array_Length (Dyn_Mnodes (Dyn_I), Get_Type (E));
            if E_Length = O_Enode_Null then
               E_Length := Elen;
            else
               E_Length := New_Dyadic_Op (ON_Add_Ov, E_Length, Elen);
            end if;
         end if;
      end Len_Dyn_Arr;

      --  Offset in the result.
      Var_Off : O_Dnode;

      --  Return the stride of the result array, if the element subtype is
      --  unbounded.
      function Get_Stride return O_Enode is
      begin
         if Is_Unbounded_El then
            return New_Value
              (Chap3.Layout_To_Size
                 (Chap3.Array_Bounds_To_Element_Layout (Var_Bounds, Expr_Type),
                  Mode_Value));
         else
            return O_Enode_Null;
         end if;
      end Get_Stride;

      --  Assign: write values to the result array.
      procedure Assign_El (E : Iir; Is_First : Boolean)
      is
         Dest : Mnode;
         Src : Mnode;
      begin
         Dest := Chap3.Index_Base
           (Var_Arr, Expr_Type, New_Obj_Value (Var_Off), Get_Stride);

         if Is_First and Is_Unbounded_El then
            Dyn_I := Dyn_I + 1;
            Src := Dyn_Mnodes (Dyn_I);
         else
            Src := Translate_Expression (E, El_Type);
         end if;
         if Is_Unbounded_El then
            Gen_Memcpy (M2Addr (Dest),
                        M2Addr (Chap3.Get_Composite_Base (Src)),
                        New_Value (Chap3.Layout_To_Size
                                     (Chap3.Array_Bounds_To_Element_Layout
                                        (Var_Bounds, Expr_Type),
                                      Mode_Value)));
         else
            Chap3.Translate_Object_Copy (Dest, Src, El_Type);
         end if;

         Inc_Var (Var_Off);
      end Assign_El;

      procedure Assign_Arr (E : Iir; Is_First : Boolean)
      is
         E_Val : O_Enode;
         M : Mnode;
         V_Arr   : O_Dnode;
         Var_Sub_Arr : Mnode;
      begin
         Open_Temp;
         if Is_Static_Arr (E)
           and then not (Is_First and Is_Unbounded_El)
         then
            --  First, translate expression.
            E_Val := Translate_Expression (E, Expr_Type);
            --  Then create Mnode (type info may be computed by
            --  translate_expression).
            M := E2M (E_Val, Get_Info (Expr_Type), Mode_Value);
            Stabilize (M);
         else
            Dyn_I := Dyn_I + 1;
            M := Dyn_Mnodes (Dyn_I);
         end if;

         --  Create a slice of the result
         V_Arr := Create_Temp (Info.Ortho_Type (Mode_Value));
         Var_Sub_Arr := Dv2M (V_Arr, Info, Mode_Value);
         New_Assign_Stmt
           (M2Lp (Chap3.Get_Composite_Bounds (Var_Sub_Arr)),
            M2Addr (Chap3.Get_Composite_Bounds (M)));
         New_Assign_Stmt
           (M2Lp (Chap3.Get_Composite_Base (Var_Sub_Arr)),
            New_Convert_Ov
              (M2Addr (Chap3.Slice_Base (Var_Arr,
                                         Expr_Type,
                                         New_Obj_Value (Var_Off),
                                         Get_Stride)),
               Info.B.Base_Ptr_Type (Mode_Value)));

         --  Copy
         Chap3.Translate_Object_Copy (Var_Sub_Arr, M, Expr_Type);

         --  Increase offset
         New_Assign_Stmt
           (New_Obj (Var_Off),
            New_Dyadic_Op (ON_Add_Ov,
                           New_Obj_Value (Var_Off),
                           Chap3.Get_Array_Length (M, Expr_Type)));
         Close_Temp;
      end Assign_Arr;

      --  Find last expression.  This is used to get the bounds in the case of
      --  a null-range result.
      Last_Expr : Iir;
      Last_Dyn_Expr : Natural;

      procedure Find_Last_Arr (E : Iir; Is_First : Boolean)
      is
         pragma Unreferenced (Is_First);
      begin
         Last_Expr := E;
         if Is_Static_Arr (E) then
            Last_Dyn_Expr := 0;
         else
            Dyn_I := Dyn_I + 1;
            Last_Dyn_Expr := Dyn_I;
         end if;
      end Find_Last_Arr;

      --  Copy Left and Dir from SRC to the result.  Used for v87.
      procedure Copy_Bounds_V87 (Src : Mnode)
      is
         Src1 : Mnode;
      begin
         Open_Temp;
         Src1 := Stabilize (Src);
         New_Assign_Stmt (M2Lv (Chap3.Range_To_Left (Get_Res_Range)),
                          M2E (Chap3.Range_To_Left (Src1)));
         New_Assign_Stmt (M2Lv (Chap3.Range_To_Dir (Get_Res_Range)),
                          M2E (Chap3.Range_To_Dir (Src1)));
         Close_Temp;
      end Copy_Bounds_V87;

      --  Vhdl 87 bounds: find the first non-null expression and assign
      --  left and dir to the result.
      Assign_Bounds_V87_Done : Boolean;
      type O_If_Block_Array is array
        (1 .. Nbr_Dyn_Expr * Boolean'Pos (Flags.Vhdl_Std = Vhdl_87))
        of O_If_Block;
      Assign_Bounds_Ifs : O_If_Block_Array;

      procedure Assign_Bounds_El_V87 (E : Iir; Is_First : Boolean)
      is
         pragma Unreferenced (Is_First);
         pragma Unreferenced (E);
      begin
         if Assign_Bounds_V87_Done then
            return;
         end if;

         Copy_Bounds_V87 (Chap3.Type_To_Range (Get_Index_Type (Expr_Type, 0)));
         Assign_Bounds_V87_Done := True;
      end Assign_Bounds_El_V87;

      procedure Assign_Bounds_Arr_V87 (E : Iir; Is_First : Boolean)
      is
         pragma Unreferenced (Is_First);
         Idx_Rng : Iir;
      begin
         if Assign_Bounds_V87_Done then
            return;
         end if;

         if Is_Static_Arr (E) then
            Idx_Rng := Get_Range_Constraint
              (Get_Index_Type (Get_Type (E), 0));
            if Eval_Discrete_Range_Length (Idx_Rng) = 0 then
               return;
            end if;
            New_Assign_Stmt
              (M2Lv (Chap3.Range_To_Left (Get_Res_Range)),
               New_Lit (Translate_Static_Range_Left (Idx_Rng, Index_Type)));
            New_Assign_Stmt
              (M2Lv (Chap3.Range_To_Dir (Get_Res_Range)),
               New_Lit (Translate_Static_Range_Dir (Idx_Rng)));
            Assign_Bounds_V87_Done := True;
         else
            Dyn_I := Dyn_I + 1;
            Start_If_Stmt
              (Assign_Bounds_Ifs (Dyn_I),
               New_Compare_Op (ON_Neq,
                               Chap3.Get_Array_Length (Dyn_Mnodes (Dyn_I),
                                                       Expr_Type),
                               New_Lit (Ghdl_Index_0),
                               Ghdl_Bool_Type));
            Copy_Bounds_V87 (Chap3.Bounds_To_Range
                               (Chap3.Get_Composite_Bounds
                                  (Dyn_Mnodes (Dyn_I)), Expr_Type, 1));
            New_Else_Stmt (Assign_Bounds_Ifs (Dyn_I));
         end if;
      end Assign_Bounds_Arr_V87;

   begin
      --  Bounds
      Var_Bounds := Dv2M
        (Create_Temp (Info.B.Bounds_Type), Info, Mode_Value,
         Info.B.Bounds_Type, Info.B.Bounds_Ptr_Type);

      --  Base
      Arr_Ptr := Create_Temp (Info.B.Base_Ptr_Type (Mode_Value));
      Var_Arr := Dp2M (Arr_Ptr, Info, Mode_Value,
                       Info.B.Base_Type (Mode_Value),
                       Info.B.Base_Ptr_Type (Mode_Value));

      --  Result
      Var_Res := Create_Temp (Info.Ortho_Type (Mode_Value));
      Res := Dv2M (Var_Res, Info, Mode_Value);

      --  Set result bounds.
      New_Assign_Stmt
        (M2Lp (Chap3.Get_Composite_Bounds (Res)), M2Addr (Var_Bounds));

      --  Evaluate all dynamic expressions
      Dyn_I := 0;
      Walk ((Eval_First_El'Access, Eval_Dyn_Arr'Access));
      --  Check that all dynamic expressions have been handled.
      pragma Assert (Dyn_I = Dyn_Mnodes'Last);

      --  Compute length
      if Static_Length /= 0 then
         E_Length := New_Lit (New_Index_Lit (Unsigned_64 (Static_Length)));
      else
         E_Length := O_Enode_Null;
      end if;
      Dyn_I := 0;
      Walk ((Len_El'Access, Len_Dyn_Arr'Access));
      pragma Assert (Dyn_I = Dyn_Mnodes'Last);
      pragma Assert (E_Length /= O_Enode_Null);
      Var_Length := Create_Temp_Init (Ghdl_Index_Type, E_Length);

      --  Compute bounds.
      declare
         If_Blk : O_If_Block;
      begin
         if Static_Length = 0 then
            --  The result may have null bounds.  Note: we haven't optimize
            --  the case when the result is known to have null bounds.
            Start_If_Stmt
              (If_Blk, New_Compare_Op (ON_Neq, New_Obj_Value (Var_Length),
                                       New_Lit (Ghdl_Index_0),
                                       Ghdl_Bool_Type));
         end if;

         --  For a non-null bounds result.
         if Flags.Vhdl_Std > Vhdl_87 or Flag_Relaxed_Rules then
            --  Vhdl 93 case: lean and simple.
            Chap3.Create_Range_From_Length
              (Index_Type, Var_Length, Get_Res_Range, Left);
         else
            --  Vhdl 87 rules are error-prone and not very efficient:

            --  LRM87 7.2.4
            --  The left bound of this result is the left bound of the left
            --  operand, unless the left operand is a null array, in which
            --  case the result of the concatenation is the right operand.
            --  The direction of the result is the direction of the left
            --  operand, unless the left operand is a null array, in which
            --  case the direction of the result is that of the right operand.

            --  Assign length.
            New_Assign_Stmt
              (M2Lv (Chap3.Range_To_Length (Get_Res_Range)),
               New_Obj_Value (Var_Length));

            --  Left and direction are copied from the first expressions with
            --  non-null range.
            Dyn_I := 0;
            Assign_Bounds_V87_Done := False;
            Walk ((Assign_Bounds_El_V87'Access, Assign_Bounds_Arr_V87'Access));
            for I in reverse 1 .. Dyn_I  loop
               Finish_If_Stmt (Assign_Bounds_Ifs (I));
            end loop;

            --  Set right bound.
            declare
               Idx_Info : constant Type_Info_Acc := Get_Info (Index_Type);
               Idx_Otype : constant O_Tnode :=
                 Idx_Info.Ortho_Type (Mode_Value);
               Var_Length1 : O_Dnode;
               Var_Right   : O_Dnode;
               If_Blk2 : O_If_Block;
            begin
               Open_Temp;
               Var_Length1 := Create_Temp (Ghdl_Index_Type);
               Var_Right := Create_Temp (Idx_Otype);

               --  Note this substraction cannot overflow, since LENGTH >= 1.
               New_Assign_Stmt
                 (New_Obj (Var_Length1),
                  New_Dyadic_Op (ON_Sub_Ov,
                                 New_Obj_Value (Var_Length),
                                 New_Lit (Ghdl_Index_1)));

               --  Compute right bound of result:
               --    if dir = dir_to then
               --        right := left + length_1;
               --    else
               --        right := left - length_1;
               --    end if;
               Start_If_Stmt
                 (If_Blk2,
                  New_Compare_Op (ON_Eq,
                                  M2E (Chap3.Range_To_Dir (Get_Res_Range)),
                                  New_Lit (Ghdl_Dir_To_Node),
                                  Ghdl_Bool_Type));
               New_Assign_Stmt
                 (New_Obj (Var_Right),
                  New_Dyadic_Op (ON_Add_Ov,
                                 M2E (Chap3.Range_To_Left (Get_Res_Range)),
                                 New_Convert_Ov (New_Obj_Value (Var_Length1),
                                                 Idx_Otype)));
               New_Else_Stmt (If_Blk2);
               New_Assign_Stmt
                 (New_Obj (Var_Right),
                  New_Dyadic_Op (ON_Sub_Ov,
                                 M2E (Chap3.Range_To_Left (Get_Res_Range)),
                                 New_Convert_Ov (New_Obj_Value (Var_Length1),
                                                 Idx_Otype)));
               Finish_If_Stmt (If_Blk2);

               --   Check the right bounds is inside the bounds of the
               --   index type.
               Chap3.Check_Range (Var_Right, Null_Iir, Index_Type, Left);
               New_Assign_Stmt
                 (M2Lv (Chap3.Range_To_Right (Get_Res_Range)),
                  New_Obj_Value (Var_Right));
               Close_Temp;
            end;
         end if;

         if Static_Length = 0 then
            New_Else_Stmt (If_Blk);
            --  For a null bound result.  Same rules for v87 and v93.
            --  Find last expression.
            Last_Expr := Null_Iir;
            Last_Dyn_Expr := 0;
            Dyn_I := 0;
            Walk ((Nil_El'Access, Find_Last_Arr'Access));
            pragma Assert (Dyn_I = Dyn_Mnodes'Last);

            if Last_Dyn_Expr = 0 then
               --  The last expression is not dynamic.
               Translate_Discrete_Range
                 (Get_Res_Range, Get_Index_Type (Get_Type (Last_Expr), 0));
            else
               Copy_Range
                 (Get_Res_Range,
                  Chap3.Bounds_To_Range
                    (Chap3.Get_Composite_Bounds (Dyn_Mnodes (Last_Dyn_Expr)),
                     Expr_Type, 1));
            end if;

            Finish_If_Stmt (If_Blk);
         end if;
      end;

      --  Allocate result.
      New_Assign_Stmt
        (New_Obj (Arr_Ptr),
         Gen_Alloc (Alloc_Stack,
                    Chap3.Get_Object_Size (Res, Expr_Type),
                    Info.B.Base_Ptr_Type (Mode_Value)));
      New_Assign_Stmt
        (M2Lp (Chap3.Get_Composite_Base (Res)), M2Addr (Var_Arr));

      --  Assign expressions
      Open_Temp;
      Var_Off := Create_Temp_Init (Ghdl_Index_Type, New_Lit (Ghdl_Index_0));
      Dyn_I := 0;
      Walk ((Assign_El'Access, Assign_Arr'Access));
      pragma Assert (Dyn_I = Dyn_Mnodes'Last);
      Close_Temp;

      return Translate_Implicit_Conv
        (M2E (Res), Expr_Type, Res_Type, Mode_Value, Left);
   end Translate_Concatenation;

   function Translate_Scalar_Min_Max
     (Op : ON_Op_Kind; Left, Right : Iir; Res_Type : Iir) return O_Enode
   is
      Res_Otype : constant O_Tnode := Get_Ortho_Type (Res_Type, Mode_Value);
      Res, L, R : O_Dnode;
      If_Blk    : O_If_Block;
   begin
      --  Create a variable for the result.
      Res := Create_Temp (Res_Otype);

      Open_Temp;
      L := Create_Temp_Init
        (Res_Otype, Translate_Expression (Left, Res_Type));
      R := Create_Temp_Init
        (Res_Otype, Translate_Expression (Right, Res_Type));

      Start_If_Stmt (If_Blk, New_Compare_Op (Op,
                                             New_Obj_Value (L),
                                             New_Obj_Value (R),
                                             Ghdl_Bool_Type));
      New_Assign_Stmt (New_Obj (Res), New_Obj_Value (L));
      New_Else_Stmt (If_Blk);
      New_Assign_Stmt (New_Obj (Res), New_Obj_Value (R));
      Finish_If_Stmt (If_Blk);
      Close_Temp;

      return New_Obj_Value (Res);
   end Translate_Scalar_Min_Max;

   function Translate_Predefined_Vector_Min_Max
     (Is_Min : Boolean; Left : Iir; Res_Type : Iir) return O_Enode
   is
      Res_Otype    : constant O_Tnode := Get_Ortho_Type (Res_Type, Mode_Value);
      Left_Type    : constant Iir := Get_Type (Left);
      Res, El, Len : O_Dnode;
      Arr          : Mnode;
      If_Blk       : O_If_Block;
      Label        : O_Snode;
      Op           : ON_Op_Kind;
   begin
      --  Create a variable for the result.
      Res := Create_Temp (Res_Otype);

      Open_Temp;
      if Is_Min then
         Op := ON_Lt;
      else
         Op := ON_Gt;
      end if;
      New_Assign_Stmt
        (New_Obj (Res),
         Chap14.Translate_High_Low_Type_Attribute (Res_Type, Is_Min));

      El := Create_Temp (Res_Otype);
      Arr := Stabilize (E2M (Translate_Expression (Left),
                             Get_Info (Left_Type), Mode_Value));
      Len := Create_Temp_Init
        (Ghdl_Index_Type,
         M2E (Chap3.Range_To_Length
                (Chap3.Get_Array_Range (Arr, Left_Type, 1))));

      --  Create:
      --    loop
      --      exit when LEN = 0;
      --      LEN := LEN - 1;
      --      if ARR[LEN] </> RES then
      --         RES := ARR[LEN];
      --      end if;
      --    end loop;
      Start_Loop_Stmt (Label);
      Gen_Exit_When (Label, New_Compare_Op (ON_Eq, New_Obj_Value (Len),
                                            New_Lit (Ghdl_Index_0),
                                            Ghdl_Bool_Type));
      Dec_Var (Len);
      New_Assign_Stmt
        (New_Obj (El),
         M2E (Chap3.Index_Base (Chap3.Get_Composite_Base (Arr),
                                Left_Type, New_Obj_Value (Len))));
      Start_If_Stmt (If_Blk, New_Compare_Op (Op,
                                             New_Obj_Value (El),
                                             New_Obj_Value (Res),
                                             Ghdl_Bool_Type));
      New_Assign_Stmt (New_Obj (Res), New_Obj_Value (El));
      Finish_If_Stmt (If_Blk);
      Finish_Loop_Stmt (Label);

      Close_Temp;

      return New_Obj_Value (Res);
   end Translate_Predefined_Vector_Min_Max;

   function Translate_Std_Ulogic_Match
     (Func : O_Dnode; L, R : O_Enode; Res_Type : O_Tnode) return O_Enode
   is
      Constr : O_Assoc_List;
   begin
      Start_Association (Constr, Func);
      New_Association (Constr, New_Convert_Ov (L, Ghdl_I32_Type));
      New_Association (Constr, New_Convert_Ov (R, Ghdl_I32_Type));
      return New_Convert_Ov (New_Function_Call (Constr), Res_Type);
   end Translate_Std_Ulogic_Match;

   function Translate_To_String (Subprg   : O_Dnode;
                                 Res_Type : Iir;
                                 Loc      : Iir;
                                 Val      : O_Enode;
                                 Arg2     : O_Enode := O_Enode_Null;
                                 Arg3     : O_Enode := O_Enode_Null)
                                return O_Enode
   is
      Val_Type : constant Iir := Get_Base_Type (Res_Type);
      Res      : O_Dnode;
      Assoc    : O_Assoc_List;
   begin
      Res := Create_Temp (Std_String_Node);
      Create_Temp_Stack2_Mark;
      Start_Association (Assoc, Subprg);
      New_Association (Assoc,
                       New_Address (New_Obj (Res), Std_String_Ptr_Node));
      New_Association (Assoc, Val);
      if Arg2 /= O_Enode_Null then
         New_Association (Assoc, Arg2);
         if Arg3 /= O_Enode_Null then
            New_Association (Assoc, Arg3);
         end if;
      end if;
      New_Procedure_Call (Assoc);
      return M2E (Translate_Implicit_Array_Conversion
                  (Dv2M (Res, Get_Info (Val_Type), Mode_Value),
                   Val_Type, Res_Type, Loc));
   end Translate_To_String;

   function Translate_Bv_To_String (Subprg   : O_Dnode;
                                    Val      : O_Enode;
                                    Val_Type : Iir;
                                    Res_Type : Iir;
                                    Loc      : Iir)
                                   return O_Enode
   is
      Arr : Mnode;
   begin
      Arr := Stabilize (E2M (Val, Get_Info (Val_Type), Mode_Value));
      return Translate_To_String
        (Subprg, Res_Type, Loc,
         M2E (Chap3.Get_Composite_Base (Arr)),
         M2E (Chap3.Range_To_Length
                (Chap3.Get_Array_Range (Arr, Val_Type, 1))));
   end Translate_Bv_To_String;

   subtype Predefined_Boolean_Logical is Iir_Predefined_Functions range
     Iir_Predefined_Boolean_And .. Iir_Predefined_Boolean_Xnor;

   function Translate_Predefined_Logical
     (Op : Predefined_Boolean_Logical; Left, Right : O_Enode) return O_Enode is
   begin
      case Op is
         when Iir_Predefined_Boolean_And =>
            return New_Dyadic_Op (ON_And, Left, Right);
         when Iir_Predefined_Boolean_Or =>
            return New_Dyadic_Op (ON_Or, Left, Right);
         when Iir_Predefined_Boolean_Nand =>
            return New_Monadic_Op
              (ON_Not, New_Dyadic_Op (ON_And, Left, Right));
         when Iir_Predefined_Boolean_Nor =>
            return New_Monadic_Op
              (ON_Not, New_Dyadic_Op (ON_Or, Left, Right));
         when Iir_Predefined_Boolean_Xor =>
            return New_Dyadic_Op (ON_Xor, Left, Right);
         when Iir_Predefined_Boolean_Xnor =>
            return New_Monadic_Op
              (ON_Not, New_Dyadic_Op (ON_Xor, Left, Right));
      end case;
   end Translate_Predefined_Logical;

   function Translate_Predefined_TF_Array_Element
     (Op : Predefined_Boolean_Logical;
      Left, Right : Iir;
      Res_Type : Iir;
      Loc : Iir)
     return O_Enode
   is
      Arr_Type      : constant Iir := Get_Base_Type (Get_Type (Left));
      Res_Btype     : constant Iir := Get_Base_Type (Res_Type);
      Res_Info      : constant Type_Info_Acc := Get_Info (Res_Btype);
      Base_Ptr_Type : constant O_Tnode :=
        Res_Info.B.Base_Ptr_Type (Mode_Value);
      Arr           : Mnode;
      El            : O_Dnode;
      Base          : O_Dnode;
      Len           : O_Dnode;
      Label         : O_Snode;
      Res           : Mnode;
   begin
      --  Translate the array.
      --  Need to convert to the base type as the subtype may not be
      --  translated (for strings).
      Arr := Stabilize (E2M (Translate_Expression (Left, Arr_Type),
                             Get_Info (Arr_Type), Mode_Value));

      --  Extract its length.
      Len := Create_Temp_Init
        (Ghdl_Index_Type,
         M2E (Chap3.Range_To_Length
                (Chap3.Get_Array_Range (Arr, Arr_Type, 1))));

      --  Allocate the result array.
      Base := Create_Temp_Init
        (Base_Ptr_Type,
         Gen_Alloc (Alloc_Stack, New_Obj_Value (Len), Base_Ptr_Type));

      Open_Temp;
      --  Translate the element.
      El := Create_Temp_Init (Get_Ortho_Type (Get_Type (Right), Mode_Value),
                              Translate_Expression (Right));
      --  Create:
      --    loop
      --      exit when LEN = 0;
      --      LEN := LEN - 1;
      --      BASE[LEN] := EL op ARR[LEN];
      --    end loop;
      Start_Loop_Stmt (Label);
      Gen_Exit_When (Label, New_Compare_Op (ON_Eq, New_Obj_Value (Len),
                                            New_Lit (Ghdl_Index_0),
                                            Ghdl_Bool_Type));
      Dec_Var (Len);
      New_Assign_Stmt
        (New_Indexed_Acc_Value (New_Obj (Base),
                                New_Obj_Value (Len)),
         Translate_Predefined_Logical
           (Op,
            New_Obj_Value (El),
            M2E (Chap3.Index_Base (Chap3.Get_Composite_Base (Arr),
                                   Arr_Type, New_Obj_Value (Len)))));
      Finish_Loop_Stmt (Label);
      Close_Temp;

      Res := Create_Temp (Res_Info, Mode_Value);
      New_Assign_Stmt (M2Lp (Chap3.Get_Composite_Base (Res)),
                       New_Obj_Value (Base));
      New_Assign_Stmt (M2Lp (Chap3.Get_Composite_Bounds (Res)),
                       M2Addr (Chap3.Get_Composite_Bounds (Arr)));

      return Translate_Implicit_Conv (M2E (Res), Res_Btype, Res_Type,
                                      Mode_Value, Loc);
   end Translate_Predefined_TF_Array_Element;

   function Translate_Predefined_TF_Reduction
     (Op : ON_Op_Kind; Operand : Iir; Res_Type : Iir) return O_Enode
   is
      Arr_Type  : constant Iir := Get_Type (Operand);
      Enums     : constant Iir_Flist :=
        Get_Enumeration_Literal_List (Get_Base_Type (Res_Type));
      Init_Enum : Iir;

      Res      : O_Dnode;
      Arr_Expr : O_Enode;
      Arr      : Mnode;
      Len      : O_Dnode;
      Label    : O_Snode;
   begin
      if Op = ON_And then
         Init_Enum := Get_Nth_Element (Enums, 1);
      else
         Init_Enum := Get_Nth_Element (Enums, 0);
      end if;

      Res := Create_Temp_Init (Get_Ortho_Type (Res_Type, Mode_Value),
                               New_Lit (Get_Ortho_Literal (Init_Enum)));

      Open_Temp;
      --  Translate the array.  Note that Translate_Expression may create
      --  the info for the array type, so be sure to call it before calling
      --  Get_Info.
      Arr_Expr := Translate_Expression (Operand);
      Arr := Stabilize (E2M (Arr_Expr, Get_Info (Arr_Type), Mode_Value));

      --  Extract its length.
      Len := Create_Temp_Init
        (Ghdl_Index_Type,
         M2E (Chap3.Range_To_Length
                (Chap3.Get_Array_Range (Arr, Arr_Type, 1))));

      --  Create:
      --    loop
      --      exit when LEN = 0;
      --      LEN := LEN - 1;
      --      RES := RES op ARR[LEN];
      --    end loop;
      Start_Loop_Stmt (Label);
      Gen_Exit_When (Label, New_Compare_Op (ON_Eq, New_Obj_Value (Len),
                                            New_Lit (Ghdl_Index_0),
                                            Ghdl_Bool_Type));
      Dec_Var (Len);
      New_Assign_Stmt
        (New_Obj (Res),
         New_Dyadic_Op
           (Op,
            New_Obj_Value (Res),
            M2E (Chap3.Index_Base (Chap3.Get_Composite_Base (Arr),
                                   Arr_Type, New_Obj_Value (Len)))));
      Finish_Loop_Stmt (Label);
      Close_Temp;

      return New_Obj_Value (Res);
   end Translate_Predefined_TF_Reduction;

   function Translate_Predefined_Array_Min_Max
     (Is_Min                : Boolean;
      Left, Right           : O_Enode;
      Left_Type, Right_Type : Iir;
      Res_Type              : Iir;
      Imp                   : Iir;
      Loc                   : Iir)
     return O_Enode
   is
      Arr_Type : constant Iir := Get_Base_Type (Left_Type);
      Arr_Info : constant Type_Info_Acc := Get_Info (Arr_Type);
      L, R     : Mnode;
      If_Blk   : O_If_Block;
      Res      : Mnode;
   begin
      Res := Create_Temp (Arr_Info, Mode_Value);
      L := Stabilize (E2M (Left, Get_Info (Left_Type), Mode_Value));
      R := Stabilize (E2M (Right, Get_Info (Right_Type), Mode_Value));
      Start_If_Stmt
        (If_Blk,
         New_Compare_Op
           (ON_Eq,
            Translate_Predefined_Lib_Operator (M2E (L), M2E (R), Imp),
            New_Lit (Ghdl_Compare_Lt),
            Std_Boolean_Type_Node));
      if Is_Min then
         Copy_Fat_Pointer (Res, Translate_Implicit_Array_Conversion
                           (L, Left_Type, Arr_Type, Loc));
      else
         Copy_Fat_Pointer (Res, Translate_Implicit_Array_Conversion
                           (R, Right_Type, Arr_Type, Loc));
      end if;
      New_Else_Stmt (If_Blk);
      if Is_Min then
         Copy_Fat_Pointer (Res, Translate_Implicit_Array_Conversion
                           (R, Right_Type, Arr_Type, Loc));
      else
         Copy_Fat_Pointer (Res, Translate_Implicit_Array_Conversion
                           (L, Left_Type, Arr_Type, Loc));
      end if;
      Finish_If_Stmt (If_Blk);

      return M2E (Translate_Implicit_Array_Conversion
                  (Res, Arr_Type, Res_Type, Loc));
   end Translate_Predefined_Array_Min_Max;

   function Translate_Predefined_TF_Edge (Is_Rising : Boolean; Left : Iir)
                                         return O_Enode
   is
      Enums : constant Iir_Flist :=
        Get_Enumeration_Literal_List (Get_Base_Type (Get_Type (Left)));
      Sig  : Mnode;
      Val  : Mnode;
   begin
      Chap6.Translate_Signal_Name (Left, Sig, Val);
      return New_Dyadic_Op
        (ON_And,
         New_Value (Chap14.Get_Signal_Field (Sig, Ghdl_Signal_Event_Field)),
         New_Compare_Op
           (ON_Eq,
            M2E (Val),
            New_Lit (Get_Ortho_Literal
                       (Get_Nth_Element (Enums, Boolean'Pos (Is_Rising)))),
            Std_Boolean_Type_Node));
   end Translate_Predefined_TF_Edge;

   function Translate_Predefined_Std_Ulogic_Array_Match
     (Subprg : O_Dnode; Left, Right : Iir; Res_Type : Iir) return O_Enode
   is
      Res_Otype      : constant O_Tnode :=
        Get_Ortho_Type (Res_Type, Mode_Value);
      L_Type         : constant Iir := Get_Type (Left);
      R_Type         : constant Iir := Get_Type (Right);
      L_Expr, R_Expr : O_Enode;
      L, R           : Mnode;
      Assoc          : O_Assoc_List;

      Res : O_Dnode;
   begin
      Res := Create_Temp (Ghdl_I32_Type);

      Open_Temp;
      --  Translate the arrays.  Note that Translate_Expression may create
      --  the info for the array type, so be sure to call it before calling
      --  Get_Info.
      L_Expr := Translate_Expression (Left);
      L := Stabilize (E2M (L_Expr, Get_Info (L_Type), Mode_Value));

      R_Expr := Translate_Expression (Right);
      R := Stabilize (E2M (R_Expr, Get_Info (R_Type), Mode_Value));

      Start_Association (Assoc, Subprg);
      New_Association
        (Assoc,
         New_Convert_Ov (M2E (Chap3.Get_Composite_Base (L)), Ghdl_Ptr_Type));
      New_Association
        (Assoc,
         M2E (Chap3.Range_To_Length (Chap3.Get_Array_Range (L, L_Type, 1))));

      New_Association
        (Assoc,
         New_Convert_Ov (M2E (Chap3.Get_Composite_Base (R)), Ghdl_Ptr_Type));
      New_Association
        (Assoc,
         M2E (Chap3.Range_To_Length (Chap3.Get_Array_Range (R, R_Type, 1))));

      New_Assign_Stmt (New_Obj (Res), New_Function_Call (Assoc));

      Close_Temp;

      return New_Convert_Ov (New_Obj_Value (Res), Res_Otype);
   end Translate_Predefined_Std_Ulogic_Array_Match;

   function Translate_Predefined_Operator
     (Expr : Iir_Function_Declaration; Left, Right : Iir; Res_Type : Iir)
     return O_Enode
   is
      Imp : constant Iir := Get_Implementation (Expr);
      Kind : constant Iir_Predefined_Functions :=
        Get_Implicit_Definition (Imp);
      Left_Tree  : O_Enode;
      Right_Tree : O_Enode;
      Left_Type  : Iir;
      Right_Type : Iir;
      Res_Otype  : O_Tnode;
      Op         : ON_Op_Kind;
      Inter      : Iir;
      Res        : O_Enode;
   begin
      case Kind is
         when Iir_Predefined_Bit_And
            | Iir_Predefined_Bit_Or
            | Iir_Predefined_Bit_Nand
            | Iir_Predefined_Bit_Nor
            | Iir_Predefined_Boolean_And
            | Iir_Predefined_Boolean_Or
            | Iir_Predefined_Boolean_Nand
            | Iir_Predefined_Boolean_Nor =>
            --  Right operand of shortcircuit operators may not be evaluated.
            return Translate_Shortcircuit_Operator (Imp, Left, Right);

         when Iir_Predefined_Array_Array_Concat
           | Iir_Predefined_Element_Array_Concat
           | Iir_Predefined_Array_Element_Concat
           | Iir_Predefined_Element_Element_Concat =>
            return Translate_Concatenation (Imp, Left, Right, Res_Type);

            --  Operands of min/max are evaluated in a declare block.
         when Iir_Predefined_Enum_Minimum
            | Iir_Predefined_Integer_Minimum
            | Iir_Predefined_Floating_Minimum
            | Iir_Predefined_Physical_Minimum =>
            return Translate_Scalar_Min_Max (ON_Le, Left, Right, Res_Type);
         when Iir_Predefined_Enum_Maximum
            | Iir_Predefined_Integer_Maximum
            | Iir_Predefined_Floating_Maximum
            | Iir_Predefined_Physical_Maximum =>
            return Translate_Scalar_Min_Max (ON_Ge, Left, Right, Res_Type);

            --  Avoid implicit conversion of the array parameters to the
            --  unbounded type for optimizing purpose.  FIXME: should do the
            --  same for the result.
         when Iir_Predefined_TF_Array_Element_And =>
            return Translate_Predefined_TF_Array_Element
              (Iir_Predefined_Boolean_And, Left, Right, Res_Type, Expr);
         when Iir_Predefined_TF_Element_Array_And =>
            return Translate_Predefined_TF_Array_Element
              (Iir_Predefined_Boolean_And, Right, Left, Res_Type, Expr);
         when Iir_Predefined_TF_Array_Element_Or =>
            return Translate_Predefined_TF_Array_Element
              (Iir_Predefined_Boolean_Or, Left, Right, Res_Type, Expr);
         when Iir_Predefined_TF_Element_Array_Or =>
            return Translate_Predefined_TF_Array_Element
              (Iir_Predefined_Boolean_Or, Right, Left, Res_Type, Expr);
         when Iir_Predefined_TF_Array_Element_Nand =>
            return Translate_Predefined_TF_Array_Element
              (Iir_Predefined_Boolean_Nand, Left, Right, Res_Type, Expr);
         when Iir_Predefined_TF_Element_Array_Nand =>
            return Translate_Predefined_TF_Array_Element
              (Iir_Predefined_Boolean_Nand, Right, Left, Res_Type, Expr);
         when Iir_Predefined_TF_Array_Element_Nor =>
            return Translate_Predefined_TF_Array_Element
              (Iir_Predefined_Boolean_Nor, Left, Right, Res_Type, Expr);
         when Iir_Predefined_TF_Element_Array_Nor =>
            return Translate_Predefined_TF_Array_Element
              (Iir_Predefined_Boolean_Nor, Right, Left, Res_Type, Expr);
         when Iir_Predefined_TF_Array_Element_Xor =>
            return Translate_Predefined_TF_Array_Element
              (Iir_Predefined_Boolean_Xor, Left, Right, Res_Type, Expr);
         when Iir_Predefined_TF_Element_Array_Xor =>
            return Translate_Predefined_TF_Array_Element
              (Iir_Predefined_Boolean_Xor, Right, Left, Res_Type, Expr);
         when Iir_Predefined_TF_Array_Element_Xnor =>
            return Translate_Predefined_TF_Array_Element
              (Iir_Predefined_Boolean_Xnor, Left, Right, Res_Type, Expr);
         when Iir_Predefined_TF_Element_Array_Xnor =>
            return Translate_Predefined_TF_Array_Element
              (Iir_Predefined_Boolean_Xnor, Right, Left, Res_Type, Expr);

            --  Avoid implicit conversion of the array parameters to the
            --  unbounded type for optimizing purpose.
         when Iir_Predefined_TF_Reduction_And =>
            return Translate_Predefined_TF_Reduction
              (ON_And, Left, Res_Type);
         when Iir_Predefined_TF_Reduction_Or =>
            return Translate_Predefined_TF_Reduction
              (ON_Or, Left, Res_Type);
         when Iir_Predefined_TF_Reduction_Nand =>
            return New_Monadic_Op
              (ON_Not,
               Translate_Predefined_TF_Reduction (ON_And, Left, Res_Type));
         when Iir_Predefined_TF_Reduction_Nor =>
            return New_Monadic_Op
              (ON_Not,
               Translate_Predefined_TF_Reduction (ON_Or, Left, Res_Type));
         when Iir_Predefined_TF_Reduction_Xor =>
            return Translate_Predefined_TF_Reduction
              (ON_Xor, Left, Res_Type);
         when Iir_Predefined_TF_Reduction_Xnor =>
            return New_Monadic_Op
              (ON_Not,
               Translate_Predefined_TF_Reduction (ON_Xor, Left, Res_Type));

         when Iir_Predefined_Vector_Minimum =>
            return Translate_Predefined_Vector_Min_Max
              (True, Left, Get_Type (Expr));
         when Iir_Predefined_Vector_Maximum =>
            return Translate_Predefined_Vector_Min_Max
              (False, Left, Get_Type (Expr));

         when Iir_Predefined_Bit_Rising_Edge
            | Iir_Predefined_Boolean_Rising_Edge =>
            return Translate_Predefined_TF_Edge (True, Left);
         when Iir_Predefined_Bit_Falling_Edge
            | Iir_Predefined_Boolean_Falling_Edge =>
            return Translate_Predefined_TF_Edge (False, Left);

         when Iir_Predefined_Std_Ulogic_Array_Match_Equality =>
            return Translate_Predefined_Std_Ulogic_Array_Match
              (Ghdl_Std_Ulogic_Array_Match_Eq, Left, Right, Res_Type);
         when Iir_Predefined_Std_Ulogic_Array_Match_Inequality =>
            return Translate_Predefined_Std_Ulogic_Array_Match
              (Ghdl_Std_Ulogic_Array_Match_Ne, Left, Right, Res_Type);

         when others =>
            null;
      end case;

      --  Evaluate parameters.
      Res_Otype := Get_Ortho_Type (Res_Type, Mode_Value);
      Inter := Get_Interface_Declaration_Chain (Imp);
      if Left = Null_Iir then
         Left_Tree := O_Enode_Null;
      else
         Left_Type := Get_Type (Inter);
         Left_Tree := Translate_Expression (Left, Left_Type);
      end if;

      if Right = Null_Iir then
         Right_Tree := O_Enode_Null;
      else
         Right_Type := Get_Type (Get_Chain (Inter));
         Right_Tree := Translate_Expression (Right, Right_Type);
      end if;

      Op := Predefined_To_Onop (Kind);
      if Op /= ON_Nil then
         case Op is
            when ON_Eq
               | ON_Neq
               | ON_Ge
               | ON_Gt
               | ON_Le
               | ON_Lt =>
               Res := New_Compare_Op (Op, Left_Tree, Right_Tree,
                                      Std_Boolean_Type_Node);
            when ON_Add_Ov
               | ON_Sub_Ov
               | ON_Mul_Ov
               | ON_Div_Ov
               | ON_Rem_Ov
               | ON_Mod_Ov
               | ON_Xor =>
               Res := New_Dyadic_Op (Op, Left_Tree, Right_Tree);
            when ON_Abs_Ov
               | ON_Neg_Ov
               | ON_Not =>
               Res := New_Monadic_Op (Op, Left_Tree);
            when others =>
               Simple_IO.Put_Line_Err
                 ("translate_predefined_operator: cannot handle "
                  & ON_Op_Kind'Image (Op));
               raise Internal_Error;
         end case;
         Res := Translate_Implicit_Conv
           (Res, Get_Return_Type (Imp), Res_Type, Mode_Value, Expr);
         return Res;
      end if;

      case Kind is
         when Iir_Predefined_Bit_Xnor
            | Iir_Predefined_Boolean_Xnor =>
            return Translate_Predefined_Logical
              (Iir_Predefined_Boolean_Xnor, Left_Tree, Right_Tree);
         when Iir_Predefined_Bit_Match_Equality =>
            return New_Compare_Op (ON_Eq, Left_Tree, Right_Tree,
                                   Get_Ortho_Type (Res_Type, Mode_Value));
         when Iir_Predefined_Bit_Match_Inequality =>
            return New_Compare_Op (ON_Neq, Left_Tree, Right_Tree,
                                   Get_Ortho_Type (Res_Type, Mode_Value));

         when Iir_Predefined_Bit_Condition =>
            return New_Compare_Op
              (ON_Eq, Left_Tree, New_Lit (Get_Ortho_Literal (Bit_1)),
               Std_Boolean_Type_Node);

         when Iir_Predefined_Integer_Identity
            | Iir_Predefined_Floating_Identity
            | Iir_Predefined_Physical_Identity =>
            return Translate_Implicit_Conv
              (Left_Tree, Left_Type, Res_Type, Mode_Value, Expr);

         when Iir_Predefined_Access_Equality
            | Iir_Predefined_Access_Inequality =>
            if Is_Composite (Get_Info (Left_Type)) then
               --  a fat pointer.
               declare
                  T        : Type_Info_Acc;
                  B        : Type_Info_Acc;
                  L, R     : O_Dnode;
                  V1, V2   : O_Enode;
                  Op1, Op2 : ON_Op_Kind;
               begin
                  if Kind = Iir_Predefined_Access_Equality then
                     Op1 := ON_Eq;
                     Op2 := ON_And;
                  else
                     Op1 := ON_Neq;
                     Op2 := ON_Or;
                  end if;
                  T := Get_Info (Left_Type);
                  B := Get_Info (Get_Designated_Type (Left_Type));
                  L := Create_Temp (T.Ortho_Ptr_Type (Mode_Value));
                  R := Create_Temp (T.Ortho_Ptr_Type (Mode_Value));
                  New_Assign_Stmt (New_Obj (L), Left_Tree);
                  New_Assign_Stmt (New_Obj (R), Right_Tree);
                  V1 := New_Compare_Op
                    (Op1,
                     New_Value_Selected_Acc_Value
                       (New_Obj (L), B.B.Base_Field (Mode_Value)),
                     New_Value_Selected_Acc_Value
                       (New_Obj (R), B.B.Base_Field (Mode_Value)),
                     Std_Boolean_Type_Node);
                  V2 := New_Compare_Op
                    (Op1,
                     New_Value_Selected_Acc_Value
                       (New_Obj (L), B.B.Bounds_Field (Mode_Value)),
                     New_Value_Selected_Acc_Value
                       (New_Obj (R), B.B.Bounds_Field (Mode_Value)),
                     Std_Boolean_Type_Node);
                  return New_Dyadic_Op (Op2, V1, V2);
               end;
            else
               --  a thin pointer.
               if Kind = Iir_Predefined_Access_Equality then
                  return New_Compare_Op
                    (ON_Eq, Left_Tree, Right_Tree, Std_Boolean_Type_Node);
               else
                  return New_Compare_Op
                    (ON_Neq, Left_Tree, Right_Tree, Std_Boolean_Type_Node);
               end if;
            end if;

         when Iir_Predefined_Physical_Integer_Div =>
            return New_Dyadic_Op (ON_Div_Ov, Left_Tree,
                                  New_Convert_Ov (Right_Tree, Res_Otype));
         when Iir_Predefined_Physical_Physical_Div =>
            return New_Convert_Ov
              (New_Dyadic_Op (ON_Div_Ov, Left_Tree, Right_Tree), Res_Otype);

            --  LRM 7.2.6
            --  Multiplication of a value P of a physical type Tp by a
            --  value I of type INTEGER is equivalent to the following
            --  computation: Tp'Val (Tp'Pos (P) * I)
            --  FIXME: this is not what is really done...
         when Iir_Predefined_Integer_Physical_Mul =>
            return New_Dyadic_Op (ON_Mul_Ov,
                                  New_Convert_Ov (Left_Tree, Res_Otype),
                                  Right_Tree);
         when Iir_Predefined_Physical_Integer_Mul =>
            return New_Dyadic_Op (ON_Mul_Ov, Left_Tree,
                                  New_Convert_Ov (Right_Tree, Res_Otype));

            --  LRM 7.2.6
            --  Multiplication of a value P of a physical type Tp by a
            --  value F of type REAL is equivalten to the following
            --  computation: Tp'Val (INTEGER (REAL (Tp'Pos (P)) * F))
            --  FIXME: we do not restrict with INTEGER.
         when Iir_Predefined_Physical_Real_Mul =>
            declare
               Right_Otype : O_Tnode;
            begin
               Right_Otype := Get_Ortho_Type (Right_Type, Mode_Value);
               return New_Convert_Ov
                 (New_Dyadic_Op (ON_Mul_Ov,
                  New_Convert_Ov (Left_Tree, Right_Otype),
                  Right_Tree),
                  Res_Otype);
            end;
         when Iir_Predefined_Physical_Real_Div =>
            declare
               Right_Otype : O_Tnode;
            begin
               Right_Otype := Get_Ortho_Type (Right_Type, Mode_Value);
               return New_Convert_Ov
                 (New_Dyadic_Op (ON_Div_Ov,
                  New_Convert_Ov (Left_Tree, Right_Otype),
                  Right_Tree),
                  Res_Otype);
            end;
         when Iir_Predefined_Real_Physical_Mul =>
            declare
               Left_Otype : O_Tnode;
            begin
               Left_Otype := Get_Ortho_Type (Left_Type, Mode_Value);
               return New_Convert_Ov
                 (New_Dyadic_Op (ON_Mul_Ov,
                  Left_Tree,
                  New_Convert_Ov (Right_Tree, Left_Otype)),
                  Res_Otype);
            end;

         when Iir_Predefined_Universal_R_I_Mul =>
            return New_Dyadic_Op (ON_Mul_Ov,
                                  Left_Tree,
                                  New_Convert_Ov (Right_Tree, Res_Otype));
         when Iir_Predefined_Universal_I_R_Mul =>
            return New_Dyadic_Op (ON_Mul_Ov,
                                  New_Convert_Ov (Left_Tree, Res_Otype),
                                  Right_Tree);

         when Iir_Predefined_Floating_Exp =>
            Res := Translate_Lib_Operator
              (New_Convert_Ov (Left_Tree, Std_Real_Otype),
               Right_Tree, Ghdl_Real_Exp);
            return New_Convert_Ov (Res, Res_Otype);
         when Iir_Predefined_Integer_Exp =>
            declare
               Expr_Tinfo : constant Type_Info_Acc :=
                 Get_Info (Get_Type (Expr));
               Opr : O_Dnode;
               Etype : O_Tnode;
            begin
               case Type_Mode_Integers (Expr_Tinfo.Type_Mode) is
                  when Type_Mode_I32 =>
                     Opr := Ghdl_I32_Exp;
                     Etype := Ghdl_I32_Type;
                  when Type_Mode_I64 =>
                     Opr := Ghdl_I64_Exp;
                     Etype := Ghdl_I64_Type;
               end case;
               Res := Translate_Lib_Operator
                 (New_Convert_Ov (Left_Tree, Etype), Right_Tree, Opr);
               return New_Convert_Ov (Res, Res_Otype);
            end;

         when Iir_Predefined_Array_Inequality
            | Iir_Predefined_Record_Inequality =>
            return New_Monadic_Op
              (ON_Not, Translate_Predefined_Lib_Operator
                 (Left_Tree, Right_Tree, Imp));
         when Iir_Predefined_Array_Equality
            | Iir_Predefined_Record_Equality =>
            return Translate_Predefined_Lib_Operator
              (Left_Tree, Right_Tree, Imp);

         when Iir_Predefined_Array_Greater =>
            return New_Compare_Op
              (ON_Eq,
               Translate_Predefined_Lib_Operator (Left_Tree, Right_Tree,
                 Imp),
               New_Lit (Ghdl_Compare_Gt),
               Std_Boolean_Type_Node);
         when Iir_Predefined_Array_Greater_Equal =>
            return New_Compare_Op
              (ON_Ge,
               Translate_Predefined_Lib_Operator (Left_Tree, Right_Tree,
                 Imp),
               New_Lit (Ghdl_Compare_Eq),
               Std_Boolean_Type_Node);
         when Iir_Predefined_Array_Less =>
            return New_Compare_Op
              (ON_Eq,
               Translate_Predefined_Lib_Operator (Left_Tree, Right_Tree,
                 Imp),
               New_Lit (Ghdl_Compare_Lt),
               Std_Boolean_Type_Node);
         when Iir_Predefined_Array_Less_Equal =>
            return New_Compare_Op
              (ON_Le,
               Translate_Predefined_Lib_Operator (Left_Tree, Right_Tree,
                 Imp),
               New_Lit (Ghdl_Compare_Eq),
               Std_Boolean_Type_Node);

         when Iir_Predefined_TF_Array_And
            | Iir_Predefined_TF_Array_Or
            | Iir_Predefined_TF_Array_Nand
            | Iir_Predefined_TF_Array_Nor
            | Iir_Predefined_TF_Array_Xor
            | Iir_Predefined_TF_Array_Xnor
            | Iir_Predefined_TF_Array_Not
            | Iir_Predefined_Array_Srl
            | Iir_Predefined_Array_Sra
            | Iir_Predefined_Array_Ror =>
            return Translate_Predefined_Array_Operator_Convert
              (Left_Tree, Right_Tree, Imp, Res_Type);

         when Iir_Predefined_Array_Sll
            | Iir_Predefined_Array_Sla
            | Iir_Predefined_Array_Rol =>
            Right_Tree := New_Monadic_Op (ON_Neg_Ov, Right_Tree);
            return Translate_Predefined_Array_Operator_Convert
              (Left_Tree, Right_Tree, Imp, Res_Type);

         when Iir_Predefined_Array_Array_Concat
            | Iir_Predefined_Element_Array_Concat
            | Iir_Predefined_Array_Element_Concat
            | Iir_Predefined_Element_Element_Concat =>
            raise Internal_Error;

         when Iir_Predefined_Endfile =>
            return Translate_Lib_Operator
              (Left_Tree, O_Enode_Null, Ghdl_File_Endfile);

         when Iir_Predefined_Now_Function =>
            return New_Obj_Value (Ghdl_Now);

         when Iir_Predefined_Std_Ulogic_Match_Equality =>
            return Translate_Std_Ulogic_Match
              (Ghdl_Std_Ulogic_Match_Eq,
               Left_Tree, Right_Tree, Res_Otype);
         when Iir_Predefined_Std_Ulogic_Match_Inequality =>
            return Translate_Std_Ulogic_Match
              (Ghdl_Std_Ulogic_Match_Ne,
               Left_Tree, Right_Tree, Res_Otype);
         when Iir_Predefined_Std_Ulogic_Match_Less =>
            return Translate_Std_Ulogic_Match
              (Ghdl_Std_Ulogic_Match_Lt,
               Left_Tree, Right_Tree, Res_Otype);
         when Iir_Predefined_Std_Ulogic_Match_Less_Equal =>
            return Translate_Std_Ulogic_Match
              (Ghdl_Std_Ulogic_Match_Le,
               Left_Tree, Right_Tree, Res_Otype);
         when Iir_Predefined_Std_Ulogic_Match_Greater =>
            return Translate_Std_Ulogic_Match
              (Ghdl_Std_Ulogic_Match_Gt,
               Left_Tree, Right_Tree, Res_Otype);
         when Iir_Predefined_Std_Ulogic_Match_Greater_Equal =>
            return Translate_Std_Ulogic_Match
              (Ghdl_Std_Ulogic_Match_Ge,
               Left_Tree, Right_Tree, Res_Otype);

         when Iir_Predefined_Bit_Array_Match_Equality =>
            return New_Compare_Op
              (ON_Eq,
               Translate_Predefined_Lib_Operator
                 (Left_Tree, Right_Tree, Imp),
               New_Lit (Std_Boolean_True_Node),
               Res_Otype);
         when Iir_Predefined_Bit_Array_Match_Inequality =>
            return New_Compare_Op
              (ON_Eq,
               Translate_Predefined_Lib_Operator
                 (Left_Tree, Right_Tree, Imp),
               New_Lit (Std_Boolean_False_Node),
               Res_Otype);

         when Iir_Predefined_Array_Minimum =>
            return Translate_Predefined_Array_Min_Max
              (True, Left_Tree, Right_Tree, Left_Type, Right_Type,
               Res_Type, Imp, Expr);
         when Iir_Predefined_Array_Maximum =>
            return Translate_Predefined_Array_Min_Max
              (False, Left_Tree, Right_Tree, Left_Type, Right_Type,
               Res_Type, Imp, Expr);

         when Iir_Predefined_Integer_To_String =>
            case Get_Info (Left_Type).Type_Mode is
               when Type_Mode_I32 =>
                  return Translate_To_String
                    (Ghdl_To_String_I32, Res_Type, Expr,
                     New_Convert_Ov (Left_Tree, Ghdl_I32_Type));
               when Type_Mode_I64 =>
                  return Translate_To_String
                    (Ghdl_To_String_I64, Res_Type, Expr,
                     New_Convert_Ov (Left_Tree, Ghdl_I64_Type));
               when others =>
                  raise Internal_Error;
            end case;
         when Iir_Predefined_Enum_To_String =>
            --  LRM08 5.7 String representations
            --  - For a given value of type CHARACTER, [...]
            --
            --  So special case for character.
            if Get_Base_Type (Left_Type) = Character_Type_Definition then
               return Translate_To_String
                 (Ghdl_To_String_Char, Res_Type, Expr, Left_Tree);
            end if;

            --  LRM08 5.7 String representations
            --  - For a given value of type other than CHARACTER, [...]
            declare
               Conv   : O_Tnode;
               Subprg : O_Dnode;
            begin
               case Get_Info (Left_Type).Type_Mode is
                  when Type_Mode_B1 =>
                     Subprg := Ghdl_To_String_B1;
                     Conv := Ghdl_Bool_Type;
                  when Type_Mode_E8 =>
                     Subprg := Ghdl_To_String_E8;
                     Conv := Ghdl_I32_Type;
                  when Type_Mode_E32 =>
                     Subprg := Ghdl_To_String_E32;
                     Conv := Ghdl_I32_Type;
                  when others =>
                     raise Internal_Error;
               end case;
               return Translate_To_String
                 (Subprg, Res_Type, Expr,
                  New_Convert_Ov (Left_Tree, Conv),
                  Rtis.New_Rti_Address (Get_Info (Left_Type).Type_Rti));
            end;
         when Iir_Predefined_Floating_To_String =>
            return Translate_To_String
              (Ghdl_To_String_F64, Res_Type, Expr,
               New_Convert_Ov (Left_Tree, Ghdl_Real_Type));
         when Iir_Predefined_Real_To_String_Digits =>
            return Translate_To_String
              (Ghdl_To_String_F64_Digits, Res_Type, Expr,
               New_Convert_Ov (Left_Tree, Ghdl_Real_Type),
               New_Convert_Ov (Right_Tree, Ghdl_I32_Type));
         when Iir_Predefined_Real_To_String_Format =>
            return Translate_To_String
              (Ghdl_To_String_F64_Format, Res_Type, Expr,
               New_Convert_Ov (Left_Tree, Ghdl_Real_Type),
               Right_Tree);
         when Iir_Predefined_Physical_To_String =>
            declare
               Conv   : O_Tnode;
               Subprg : O_Dnode;
            begin
               case Get_Info (Left_Type).Type_Mode is
                  when Type_Mode_P32 =>
                     Subprg := Ghdl_To_String_P32;
                     Conv := Ghdl_I32_Type;
                  when Type_Mode_P64 =>
                     Subprg := Ghdl_To_String_P64;
                     Conv := Ghdl_I64_Type;
                  when others =>
                     raise Internal_Error;
               end case;
               return Translate_To_String
                 (Subprg, Res_Type, Expr,
                  New_Convert_Ov (Left_Tree, Conv),
                  Rtis.New_Rti_Address (Get_Info (Left_Type).Type_Rti));
            end;
         when Iir_Predefined_Time_To_String_Unit =>
            return Translate_To_String
              (Ghdl_Time_To_String_Unit, Res_Type, Expr,
               Left_Tree, Right_Tree,
               Rtis.New_Rti_Address (Get_Info (Left_Type).Type_Rti));
         when Iir_Predefined_Bit_Vector_To_Ostring =>
            return Translate_Bv_To_String
              (Ghdl_BV_To_Ostring, Left_Tree, Left_Type, Res_Type, Expr);
         when Iir_Predefined_Bit_Vector_To_Hstring =>
            return Translate_Bv_To_String
              (Ghdl_BV_To_Hstring, Left_Tree, Left_Type, Res_Type, Expr);
         when Iir_Predefined_Array_Char_To_String =>
            declare
               El_Type : constant Iir := Get_Element_Subtype (Left_Type);
               Subprg  : O_Dnode;
               Arg     : Mnode;
            begin
               Arg := Stabilize
                 (E2M (Left_Tree, Get_Info (Left_Type), Mode_Value));
               case Get_Info (El_Type).Type_Mode is
                  when Type_Mode_B1 =>
                     Subprg := Ghdl_Array_Char_To_String_B1;
                  when Type_Mode_E8 =>
                     Subprg := Ghdl_Array_Char_To_String_E8;
                  when Type_Mode_E32 =>
                     Subprg := Ghdl_Array_Char_To_String_E32;
                  when others =>
                     raise Internal_Error;
               end case;
               return Translate_To_String
                 (Subprg, Res_Type, Expr,
                  New_Convert_Ov (M2E (Chap3.Get_Composite_Base (Arg)),
                    Ghdl_Ptr_Type),
                  Chap3.Get_Array_Length (Arg, Left_Type),
                  Rtis.New_Rti_Address (Get_Info (El_Type).Type_Rti));
            end;

         when others =>
            Error_Kind ("translate_predefined_operator(2)", Kind);
      end case;
   end Translate_Predefined_Operator;

   --  Assign EXPR to TARGET.
   procedure Translate_Assign
     (Target : Mnode; Val : O_Enode; Expr : Iir; Target_Type : Iir; Loc : Iir)
   is
      T_Info : constant Type_Info_Acc := Get_Info (Target_Type);
   begin
      case T_Info.Type_Mode is
         when Type_Mode_Scalar =>
            New_Assign_Stmt
              (M2Lv (Target),
               Chap3.Maybe_Insert_Scalar_Check (Val, Expr, Target_Type));
         when Type_Mode_Acc
           | Type_Mode_Bounds_Acc
           | Type_Mode_File =>
            New_Assign_Stmt (M2Lv (Target), Val);
         when Type_Mode_Unbounded_Array
           | Type_Mode_Unbounded_Record =>
            declare
               T : Mnode;
               E : O_Dnode;
               EM : Mnode;
            begin
               T := Stabilize (Target);
               E := Create_Temp_Init
                 (T_Info.Ortho_Ptr_Type (Mode_Value), Val);
               EM := Dp2M (E, T_Info, Mode_Value);
               Chap3.Check_Composite_Match
                 (Target_Type, T, Get_Type (Expr), EM, Loc);
               Chap3.Translate_Object_Copy (T, EM, Target_Type);
            end;
         when Type_Mode_Bounded_Arrays
           | Type_Mode_Bounded_Records =>
            --  Source is of type TARGET_TYPE, so no length check is
            --  necessary.
            Chap3.Translate_Object_Copy
              (Target, E2M (Val, T_Info, Mode_Value), Target_Type);
         when Type_Mode_Unknown
            | Type_Mode_Protected =>
            raise Internal_Error;
      end case;
   end Translate_Assign;

   procedure Translate_Assign (Target : Mnode; Expr : Iir; Target_Type : Iir)
   is
      Val : O_Enode;
   begin
      if Get_Kind (Expr) = Iir_Kind_Aggregate then
         --  FIXME: handle overlap between TARGET and EXPR.
         Translate_Aggregate (Target, Target_Type, Expr);
      else
         Open_Temp;
         Val := Chap7.Translate_Expression (Expr, Target_Type);
         Translate_Assign (Target, Val, Expr, Target_Type, Expr);
         Close_Temp;
      end if;
   end Translate_Assign;

   --  If AGGR is of the form (others => (others => EXPR)) (where the
   --   number of (others => ) sub-aggregate is at least 1, return EXPR
   --   otherwise return NULL_IIR.
   function Is_Aggregate_Others (Aggr : Iir_Aggregate) return Iir
   is
      Chain : Iir;
      Aggr1 : Iir;
   begin
      Aggr1 := Aggr;
      loop
         Chain := Get_Association_Choices_Chain (Aggr1);
         if not Is_Chain_Length_One (Chain) then
            return Null_Iir;
         end if;
         if Get_Kind (Chain) /= Iir_Kind_Choice_By_Others then
            return Null_Iir;
         end if;
         Aggr1 := Get_Associated_Expr (Chain);
         case Get_Kind (Aggr1) is
            when Iir_Kind_Aggregate =>
               if Get_Type (Aggr1) /= Null_Iir then
                  --  Stop when a sub-aggregate is in fact an aggregate.
                  return Aggr1;
               end if;
            when Iir_Kind_String_Literal8 =>
               return Null_Iir;
               --Error_Kind ("is_aggregate_others", Aggr1);
            when others =>
               return Aggr1;
         end case;
      end loop;
   end Is_Aggregate_Others;

   --  Generate code for (others => EL).
   procedure Translate_Aggregate_Others
     (Target : Mnode; Target_Type : Iir; El : Iir)
   is
      El_Type : constant Iir := Get_Element_Subtype (Target_Type);
      It       : O_Dnode;
      Len      : O_Dnode;
      Len_Val  : O_Enode;
      Label    : O_Snode;
      Arr_Var  : Mnode;
      El_Node  : Mnode;
   begin
      Open_Temp;

      Arr_Var := Stabilize (Target);
      Len_Val := Chap3.Get_Array_Length (Arr_Var, Target_Type);

      --  FIXME: use this (since this use one variable instead of two):
      --  I := length;
      --  loop
      --    exit when I = 0;
      --    I := I - 1;
      --    A[I] := xxx;
      --  end loop;
      Len := Create_Temp_Init (Ghdl_Index_Type, Len_Val);
      if True then
         It := Create_Temp (Ghdl_Index_Type);
      else
         New_Var_Decl (It, Wki_I, O_Storage_Local, Ghdl_Index_Type);
      end if;
      Init_Var (It);
      Start_Loop_Stmt (Label);
      Gen_Exit_When
        (Label, New_Compare_Op (ON_Eq,
                                New_Obj_Value (It), New_Obj_Value (Len),
                                Ghdl_Bool_Type));
      El_Node := Chap6.Translate_Indexed_Name_By_Offset
        (Arr_Var, Target_Type, It);
      Translate_Assign (El_Node, El, El_Type);
      Inc_Var (It);
      Finish_Loop_Stmt (Label);

      Close_Temp;
   end Translate_Aggregate_Others;

   procedure Translate_Array_Aggregate_Gen_String
     (Targ : Mnode; Aggr : Iir; Aggr_Type : Iir; Var_Index : O_Dnode)
   is
      Expr_Type  : constant Iir := Get_Element_Subtype (Aggr_Type);
      Len : constant Nat32 := Get_String_Length (Aggr);

      --  Type of the unconstrained array type.
      Arr_Type : O_Tnode;

      Cst   : Var_Type;
      Var_I : O_Dnode;
      Label : O_Snode;
   begin
      --  FIXME: check length is matching ?

      --  Create a constant for the string.
      --  First, create its type, because the literal has no
      --  type (subaggregate).
      Arr_Type := New_Array_Type
        (Get_Ortho_Type (Expr_Type, Mode_Value), Ghdl_Index_Type);
      New_Type_Decl (Create_Uniq_Identifier, Arr_Type);
      Cst := Create_String_Literal_Var_Inner (Aggr, Expr_Type, Arr_Type);

      --  Copy it.
      Open_Temp;
      Var_I := Create_Temp (Ghdl_Index_Type);
      Init_Var (Var_I);
      Start_Loop_Stmt (Label);
      Gen_Exit_When (Label,
                     New_Compare_Op (ON_Eq,
                                     New_Obj_Value (Var_I),
                                     New_Lit (New_Index_Lit (Nat32'Pos (Len))),
                                     Ghdl_Bool_Type));
      New_Assign_Stmt
        (M2Lv (Chap6.Translate_Indexed_Name_By_Offset
                 (Targ, Aggr_Type, Var_Index)),
         New_Value (New_Indexed_Element (Get_Var (Cst),
                                         New_Obj_Value (Var_I))));
      Inc_Var (Var_I);
      Inc_Var (Var_Index);
      Finish_Loop_Stmt (Label);
      Close_Temp;
   end Translate_Array_Aggregate_Gen_String;

   procedure Translate_Array_Aggregate_Gen (Targ       : Mnode;
                                            Aggr       : Iir;
                                            Aggr_Type  : Iir;
                                            Dim        : Natural;
                                            Var_Index  : O_Dnode)
   is
      Index_List : Iir_Flist;
      Aggr_El_Type  : Iir;
      Final      : Boolean;

      --  Assign EXPR to current position (defined by index VAR_INDEX), and
      --  update VAR_INDEX.  Handles sub-aggregates.
      procedure Do_Assign_El (Expr : Iir; Assoc_Len : out Int64)
      is
         Dest : Mnode;
      begin
         Dest := Chap6.Translate_Indexed_Name_By_Offset
           (Targ, Aggr_Type, Var_Index);
         Translate_Assign (Dest, Expr, Aggr_El_Type);
         Assoc_Len := 1;
         Inc_Var (Var_Index);
      end Do_Assign_El;

      procedure Do_Assign_Vec (Assoc : Iir; Expr : Iir; Assoc_Len : out Int64)
      is
         Dest : Mnode;
         Src : Mnode;
         Expr_Type : Iir;
         Idx_Type : Iir;
         El_Len : O_Enode;
         Bnd : Mnode;
      begin
         Expr_Type := Get_Type (Expr);
         Dest := Chap3.Slice_Base
           (Chap3.Get_Composite_Base (Targ), Aggr_Type,
            New_Obj_Value (Var_Index), O_Enode_Null);
         Src := Translate_Expression (Expr, Expr_Type);
         Stabilize (Src);
         --  FIXME: check bounds ?
         Gen_Memcpy (M2Addr (Dest),
                     M2Addr (Chap3.Get_Composite_Base (Src)),
                     Chap3.Get_Object_Size (Src, Expr_Type));
         --  FIXME: handle non-static expression type (at least for
         --  choice by range).
         if Get_Kind (Assoc) = Iir_Kind_Choice_By_Range then
            --  If there is a choice by range, then the range is static
            --  (dynamic aggregate are not handled here).
            pragma Assert (Get_Choice_Staticness (Assoc) = Locally);
            Idx_Type := Get_Choice_Range (Assoc);
         else
            --  Try to get the range from the expression (if it is static).
            pragma Assert (Get_Kind (Assoc) = Iir_Kind_Choice_By_None);
            Idx_Type := Get_Index_Type (Expr_Type, 0);
            if Get_Type_Staticness (Idx_Type) /= Locally then
               Idx_Type := Null_Iir;
            end if;
         end if;
         if Idx_Type /= Null_Iir then
            Idx_Type := Get_Range_From_Discrete_Range (Idx_Type);
            Assoc_Len := Eval_Discrete_Range_Length (Idx_Type);
            El_Len := New_Lit (New_Index_Lit (Unsigned_64 (Assoc_Len)));
         else
            Bnd := Chap3.Get_Composite_Type_Bounds (Expr_Type);
            El_Len := M2E
              (Chap3.Range_To_Length
                 (Chap3.Bounds_To_Range (Bnd, Expr_Type, 1)));
            Assoc_Len := 0;
         end if;
         New_Assign_Stmt
           (New_Obj (Var_Index),
            New_Dyadic_Op (ON_Add_Ov,
                           New_Obj_Value (Var_Index), El_Len));
      end Do_Assign_Vec;

      procedure Do_Assign (Assoc : Iir; Expr : Iir; Assoc_Len : out Int64) is
      begin
         if Final then
            if Get_Element_Type_Flag (Assoc) then
               Do_Assign_El (Expr, Assoc_Len);
            else
               Do_Assign_Vec (Assoc, Expr, Assoc_Len);
            end if;
         else
            Translate_Array_Aggregate_Gen
              (Targ, Expr, Aggr_Type, Dim + 1, Var_Index);
            Assoc_Len := 1;
         end if;
      end Do_Assign;

      procedure Translate_Array_Aggregate_Gen_Positional
      is
         P  : Natural;
         El : Iir;
         Assoc_Len : Int64;
      begin
         --  First, assign positionnal association.
         --  FIXME: count the number of positionnal association and generate
         --   an error if there is more positionnal association than elements
         --   in the array.
         El := Get_Association_Choices_Chain (Aggr);
         P := 0;
         loop
            exit when El = Null_Iir;
            exit when Get_Kind (El) /= Iir_Kind_Choice_By_None;
            Do_Assign (El, Get_Associated_Expr (El), Assoc_Len);
            P := P + Natural (Assoc_Len);
            El := Get_Chain (El);
         end loop;

         --  End of chain.
         if El = Null_Iir then
            return;
         end if;

         pragma Assert (Get_Kind (El) = Iir_Kind_Choice_By_Others);

         --  Handle others.
         declare
            Var_Len    : O_Dnode;
            Range_Ptr  : Mnode;
            Label      : O_Snode;
            Len_Tmp    : O_Enode;
         begin
            Open_Temp;
            --  Create a loop from P to len.
            Var_Len := Create_Temp (Ghdl_Index_Type);

            Range_Ptr := Chap3.Bounds_To_Range
              (Chap3.Get_Composite_Bounds (Targ), Aggr_Type, Dim);
            Len_Tmp := M2E (Chap3.Range_To_Length (Range_Ptr));
            if P /= 0 then
               Len_Tmp := New_Dyadic_Op
                 (ON_Sub_Ov,
                  Len_Tmp, New_Lit (New_Index_Lit (Unsigned_64 (P))));
            end if;
            New_Assign_Stmt (New_Obj (Var_Len), Len_Tmp);

            --  Start loop.
            Start_Loop_Stmt (Label);
            --  Check if end of loop.
            Gen_Exit_When
              (Label,
               New_Compare_Op (ON_Eq,
                               New_Obj_Value (Var_Len),
                               New_Lit (Ghdl_Index_0),
                               Ghdl_Bool_Type));

            Do_Assign (El, Get_Associated_Expr (El), Assoc_Len);
            pragma Assert (Assoc_Len = 1);
            Dec_Var (Var_Len);
            Finish_Loop_Stmt (Label);
            Close_Temp;
         end;
      end Translate_Array_Aggregate_Gen_Positional;

      procedure Translate_Array_Aggregate_Gen_Named
      is
         El : Iir;
         Assoc_Len : Int64;
      begin
         El := Get_Association_Choices_Chain (Aggr);

         --  Then, assign named or others association.
         if Is_Chain_Length_One (El) then
            pragma Assert (Get_Info (El) = null);
            --  There is only one choice
            case Get_Kind (El) is
               when Iir_Kind_Choice_By_Others =>
                  --  Handled by positional.
                  raise Internal_Error;
               when Iir_Kind_Choice_By_Expression =>
                  Do_Assign (El, Get_Associated_Expr (El), Assoc_Len);
                  return;
               when Iir_Kind_Choice_By_Range =>
                  if Get_Element_Type_Flag (El) then
                     declare
                        Var_Length : O_Dnode;
                        Var_I      : O_Dnode;
                        Label      : O_Snode;
                     begin
                        Open_Temp;
                        Var_Length := Create_Temp_Init
                          (Ghdl_Index_Type,
                           Chap7.Translate_Range_Length
                             (Get_Choice_Range (El)));
                        Var_I := Create_Temp (Ghdl_Index_Type);
                        Init_Var (Var_I);
                        Start_Loop_Stmt (Label);
                        Gen_Exit_When
                          (Label,
                           New_Compare_Op (ON_Eq,
                                           New_Obj_Value (Var_I),
                                           New_Obj_Value (Var_Length),
                                           Ghdl_Bool_Type));
                        Do_Assign (El, Get_Associated_Expr (El), Assoc_Len);
                        Inc_Var (Var_I);
                        Finish_Loop_Stmt (Label);
                        Close_Temp;
                     end;
                  else
                     Do_Assign (El, Get_Associated_Expr (El), Assoc_Len);
                  end if;
                  return;
               when others =>
                  Error_Kind ("translate_array_aggregate_gen", El);
            end case;
         end if;

         --  Several choices..
         declare
            Range_Type : constant Iir :=
              Get_Base_Type (Get_Index_Type (Index_List, Dim - 1));
            Rtinfo     : constant Type_Info_Acc := Get_Info (Range_Type);
            Var_Pos    : O_Dnode;
            Var_Len    : O_Dnode;
            Var_Alen   : O_Dnode;
            Range_Ptr  : Mnode;
            If_Blk     : O_If_Block;
            Case_Blk   : O_Case_Block;
            Label      : O_Snode;
            Len_Tmp    : O_Enode;
            Expr       : Iir;
         begin
            Open_Temp;
            --  Create a loop from left +- number of positionnals associations
            --   to/downto right.
            Var_Pos := Create_Temp (Rtinfo.Ortho_Type (Mode_Value));
            Range_Ptr := Stabilize
              (Chap3.Bounds_To_Range
                 (Chap3.Get_Composite_Bounds (Targ), Aggr_Type, Dim));
            New_Assign_Stmt (New_Obj (Var_Pos),
                             M2E (Chap3.Range_To_Left (Range_Ptr)));

            Var_Len := Create_Temp (Ghdl_Index_Type);
            Len_Tmp := M2E (Chap3.Range_To_Length (Range_Ptr));
            New_Assign_Stmt (New_Obj (Var_Len), Len_Tmp);

            Var_Alen := Create_Temp (Ghdl_Index_Type);

            --  Start loop.
            Start_Loop_Stmt (Label);
            --  Check if end of loop.
            Gen_Exit_When (Label,
                           New_Compare_Op (ON_Eq,
                                           New_Obj_Value (Var_Len),
                                           New_Lit (Ghdl_Index_0),
                                           Ghdl_Bool_Type));

            --  convert aggr into a case statement.
            Start_Case_Stmt (Case_Blk, New_Obj_Value (Var_Pos));
            while El /= Null_Iir loop
               --  No Expr_Eval.
               pragma Assert (Get_Info (El) = null);

               Start_Choice (Case_Blk);
               Chap8.Translate_Case_Choice (El, Range_Type, Case_Blk);
               Finish_Choice (Case_Blk);
               if not Get_Same_Alternative_Flag (El) then
                  Expr := Get_Associated_Expr (El);
               end if;
               Do_Assign (El, Expr, Assoc_Len);
               New_Assign_Stmt
                 (New_Obj (Var_Alen),
                  New_Lit (New_Index_Lit (Unsigned_64 (Assoc_Len))));
               El := Get_Chain (El);
            end loop;
            Finish_Case_Stmt (Case_Blk);
            --  Update var_pos
            Start_If_Stmt
              (If_Blk,
               New_Compare_Op (ON_Eq,
                               M2E (Chap3.Range_To_Dir (Range_Ptr)),
                               New_Lit (Ghdl_Dir_To_Node),
                               Ghdl_Bool_Type));
            New_Assign_Stmt
              (New_Obj (Var_Pos),
               New_Dyadic_Op
                 (ON_Add_Ov,
                  New_Obj_Value (Var_Pos),
                  New_Convert_Ov (New_Obj_Value (Var_Alen),
                                  Rtinfo.Ortho_Type (Mode_Value))));
            New_Else_Stmt (If_Blk);
            New_Assign_Stmt
              (New_Obj (Var_Pos),
               New_Dyadic_Op
                 (ON_Sub_Ov,
                  New_Obj_Value (Var_Pos),
                  New_Convert_Ov (New_Obj_Value (Var_Alen),
                                  Rtinfo.Ortho_Type (Mode_Value))));
            Finish_If_Stmt (If_Blk);
            --  Update var_len.
            New_Assign_Stmt (New_Obj (Var_Len),
                             New_Dyadic_Op (ON_Sub_Ov,
                                            New_Obj_Value (Var_Len),
                                            New_Obj_Value (Var_Alen)));
            Finish_Loop_Stmt (Label);
            Close_Temp;
         end;
      end Translate_Array_Aggregate_Gen_Named;

      Assocs : Iir;
   begin
      if Get_Kind (Aggr) = Iir_Kind_String_Literal8 then
         Translate_Array_Aggregate_Gen_String
           (Targ, Aggr, Aggr_Type, Var_Index);
         return;
      end if;

      pragma Assert (Get_Kind (Aggr) = Iir_Kind_Aggregate);

      Index_List := Get_Index_Subtype_List (Aggr_Type);

      --  FINAL is true if the elements of the aggregate are elements of
      --  the array.
      if Get_Nbr_Elements (Index_List) = Dim then
         Aggr_El_Type := Get_Element_Subtype (Aggr_Type);
         Final:= True;
      else
         Final := False;
      end if;

      Assocs := Get_Association_Choices_Chain (Aggr);

      case Get_Kind (Assocs) is
         when Iir_Kind_Choice_By_None
           | Iir_Kind_Choice_By_Others =>
            Translate_Array_Aggregate_Gen_Positional;
         when others =>
            Translate_Array_Aggregate_Gen_Named;
      end case;
   end Translate_Array_Aggregate_Gen;

   procedure Translate_Record_Aggregate
     (Target : Mnode; Target_Type : Iir; Aggr : Iir)
   is
      El_List  : constant Iir_Flist :=
        Get_Elements_Declaration_List (Target_Type);
      El_Index : Natural;
      Nbr_El   : constant Natural := Get_Nbr_Elements (El_List);

      --  Record which elements of the record have been set.  The 'others'
      --  clause applies to all elements not already set.
      type Bool_Array_Type is array (0 .. Nbr_El - 1) of Boolean;
      pragma Pack (Bool_Array_Type);
      Set_Array : Bool_Array_Type := (others => False);

      --  The expression associated.
      El_Expr : Iir;
      Assoc   : Iir;
      Targ    : Mnode;

      --  Set an elements.
      procedure Set_El (El : Iir_Element_Declaration)
      is
         Info : constant Ortho_Info_Acc := Get_Info (Assoc);
         El_Type : constant Iir := Get_Type (El);
         Dest : Mnode;
      begin
         Dest := Chap6.Translate_Selected_Element (Targ, El);
         if Info /= null then
            --  The expression was already evaluated to compute the bounds.
            --  Just copy it.
            Chap3.Translate_Object_Copy (Dest, Info.Expr_Eval, El_Type);
            Clear_Info (Assoc);
         else
            Translate_Assign (Dest, El_Expr, El_Type);
         end if;
         Set_Array (Natural (Get_Element_Position (El))) := True;
      end Set_El;

      N_El_Expr : Iir;
   begin
      Open_Temp;
      Targ := Stabilize (Target);

      El_Index := 0;
      Assoc := Get_Association_Choices_Chain (Aggr);
      while Assoc /= Null_Iir loop
         --  Get the associated expression, possibly from the first choice
         --  in a lidt of choices.
         N_El_Expr := Get_Associated_Expr (Assoc);
         if N_El_Expr /= Null_Iir then
            El_Expr := N_El_Expr;
         end if;

         case Get_Kind (Assoc) is
            when Iir_Kind_Choice_By_None =>
               Set_El (Get_Nth_Element (El_List, El_Index));
               El_Index := El_Index + 1;
            when Iir_Kind_Choice_By_Name =>
               El_Index := Natural
                 (Get_Element_Position
                    (Get_Named_Entity (Get_Choice_Name (Assoc))));
               Set_El (Get_Nth_Element (El_List, El_Index));
               El_Index := Natural'Last;
            when Iir_Kind_Choice_By_Others =>
               for J in Set_Array'Range loop
                  if not Set_Array (J) then
                     Set_El (Get_Nth_Element (El_List, J));
                  end if;
               end loop;
            when others =>
               Error_Kind ("translate_record_aggregate", Assoc);
         end case;
         Assoc := Get_Chain (Assoc);
      end loop;
      Close_Temp;
   end Translate_Record_Aggregate;

   procedure Translate_Array_Aggregate
     (Target : Mnode; Target_Type : Iir; Aggr : Iir)
   is
      Aggr_Type       : constant Iir := Get_Type (Aggr);
      Index_List      : constant Iir_Flist :=
        Get_Index_Subtype_List (Aggr_Type);
      Targ_Index_List : constant Iir_Flist :=
        Get_Index_Subtype_List (Target_Type);

      Aggr_Info : Iir_Aggregate_Info;
      Bounds    : Mnode;
      Var_Index : O_Dnode;
      Targ      : Mnode;

      Rinfo : Type_Info_Acc;
      Bt    : Iir;

      --  Generate code for: (LVAL lop RNG.left) or (RVAL rop RNG.right)
      function Check_Value (Lval : Iir;
                            Lop  : ON_Op_Kind;
                            Rval : Iir;
                            Rop  : ON_Op_Kind;
                            Rng  : Mnode)
                               return O_Enode
      is
         L, R : O_Enode;
      begin
         L := New_Compare_Op
           (Lop,
            New_Lit (Translate_Static_Expression (Lval, Bt)),
            M2E (Chap3.Range_To_Left (Rng)),
            Ghdl_Bool_Type);
         R := New_Compare_Op
           (Rop,
            New_Lit (Translate_Static_Expression (Rval, Bt)),
            M2E (Chap3.Range_To_Right (Rng)),
            Ghdl_Bool_Type);
         return New_Dyadic_Op (ON_Or, L, R);
      end Check_Value;

      Range_Ptr    : Mnode;
      Subtarg_Type : Iir;
      Subaggr_Type : Iir;
      L, H         : Iir;
      Min          : Iir_Int32;
      Has_Others   : Boolean;

      Var_Err : O_Dnode;
      E       : O_Enode;
      If_Blk  : O_If_Block;
      Op      : ON_Op_Kind;
   begin
      Open_Temp;
      Targ := Stabilize (Target);
      Bounds := Stabilize (Chap3.Get_Composite_Bounds (Targ));
      Aggr_Info := Get_Aggregate_Info (Aggr);

      --  Check type
      for I in Flist_First .. Flist_Last (Index_List) loop
         Subaggr_Type := Get_Index_Type (Index_List, I);
         Subtarg_Type := Get_Index_Type (Targ_Index_List, I);

         Bt := Get_Base_Type (Subaggr_Type);
         Rinfo := Get_Info (Bt);

         if Get_Aggr_Dynamic_Flag (Aggr_Info) then
            --  Dynamic range, must evaluate it.
            Open_Temp;
            declare
               A_Range : Mnode;
            begin
               --  Evaluate the range.
               Chap3.Translate_Anonymous_Subtype_Definition
                 (Subaggr_Type, False);

               A_Range :=
                 Dv2M (Create_Temp (Rinfo.B.Range_Type), Rinfo, Mode_Value,
                       Rinfo.B.Range_Type, Rinfo.B.Range_Ptr_Type);
               Chap7.Translate_Range
                 (A_Range, Get_Range_Constraint (Subaggr_Type), Subaggr_Type);

               --  Check range length VS target length.
               Chap6.Check_Bound_Error
                 (New_Compare_Op
                    (ON_Neq,
                     M2E (Chap3.Range_To_Length (A_Range)),
                     M2E (Chap3.Range_To_Length
                            (Chap3.Bounds_To_Range
                               (Bounds, Target_Type, I + 1))),
                     Ghdl_Bool_Type),
                  Aggr);
            end;
            Close_Temp;
         elsif Get_Type_Staticness (Subaggr_Type) /= Locally
           or else Subaggr_Type /= Subtarg_Type
         then
            --  Note: if the aggregate has no others, then the bounds
            --  must be the same, otherwise, aggregate bounds must be
            --  inside type bounds.
            Has_Others := Get_Aggr_Others_Flag (Aggr_Info);
            Min := Get_Aggr_Min_Length (Aggr_Info);
            L := Get_Aggr_Low_Limit (Aggr_Info);

            if Min > 0 or L /= Null_Iir then
               Open_Temp;

               --  Pointer to the range.
               Range_Ptr := Stabilize
                 (Chap3.Bounds_To_Range (Bounds, Target_Type, I + 1));
               Var_Err := Create_Temp (Ghdl_Bool_Type);
               H := Get_Aggr_High_Limit (Aggr_Info);

               if L /= Null_Iir then
                  --  Check the index range of the aggregrate is equal
                  --  (or within in presence of 'others') the index range
                  --  of the target.
                  Start_If_Stmt
                    (If_Blk,
                     New_Compare_Op (ON_Eq,
                       M2E (Chap3.Range_To_Dir (Range_Ptr)),
                       New_Lit (Ghdl_Dir_To_Node),
                       Ghdl_Bool_Type));
                  if Has_Others then
                     E := Check_Value (L, ON_Lt, H, ON_Gt, Range_Ptr);
                  else
                     E := Check_Value (L, ON_Neq, H, ON_Neq, Range_Ptr);
                  end if;
                  New_Assign_Stmt (New_Obj (Var_Err), E);
                  New_Else_Stmt (If_Blk);
                  if Has_Others then
                     E := Check_Value (H, ON_Gt, L, ON_Lt, Range_Ptr);
                  else
                     E := Check_Value (H, ON_Neq, L, ON_Neq, Range_Ptr);
                  end if;
                  New_Assign_Stmt (New_Obj (Var_Err), E);
                  Finish_If_Stmt (If_Blk);
                  -- If L and H are greather than the minimum length,
                  -- then there is no need to check with min.
                  if Iir_Int32 (Eval_Pos (H) - Eval_Pos (L) + 1) >= Min then
                     Min := 0;
                  end if;
               end if;

               if Min > 0 then
                  --  Check the number of elements is equal (or less in
                  --  presence of 'others') than the length of the index
                  --  range of the target.
                  if Has_Others then
                     Op := ON_Lt;
                  else
                     Op := ON_Neq;
                  end if;
                  E := New_Compare_Op
                    (Op,
                     M2E (Chap3.Range_To_Length (Range_Ptr)),
                     New_Lit (New_Unsigned_Literal (Ghdl_Index_Type,
                       Unsigned_64 (Min))),
                     Ghdl_Bool_Type);
                  if L /= Null_Iir then
                     E := New_Dyadic_Op (ON_Or, E, New_Obj_Value (Var_Err));
                  end if;
                  New_Assign_Stmt (New_Obj (Var_Err), E);
               end if;
               Chap6.Check_Bound_Error (New_Obj_Value (Var_Err), Aggr);
               Close_Temp;
            end if;
         end if;

         --  Next dimension.
         Aggr_Info := Get_Sub_Aggregate_Info (Aggr_Info);
      end loop;

      Var_Index := Create_Temp_Init
        (Ghdl_Index_Type, New_Lit (Ghdl_Index_0));
      Translate_Array_Aggregate_Gen (Targ, Aggr, Target_Type, 1, Var_Index);
      Close_Temp;

      --  FIXME: creating aggregate subtype is expensive and rarely used.
      --  (one of the current use - only ? - is check_array_match).
      Chap3.Translate_Anonymous_Subtype_Definition (Aggr_Type, False);
   end Translate_Array_Aggregate;

   procedure Translate_Aggregate
     (Target : Mnode; Target_Type : Iir; Aggr : Iir) is
   begin
      case Iir_Kinds_Composite_Type_Definition (Get_Kind (Target_Type)) is
         when Iir_Kind_Array_Subtype_Definition
            | Iir_Kind_Array_Type_Definition =>
            declare
               El : Iir;
            begin
               El := Is_Aggregate_Others (Aggr);
               if El /= Null_Iir then
                  Translate_Aggregate_Others (Target, Target_Type, El);
               else
                  Translate_Array_Aggregate (Target, Target_Type, Aggr);
               end if;
            end;
         when Iir_Kind_Record_Type_Definition
            | Iir_Kind_Record_Subtype_Definition =>
            Translate_Record_Aggregate (Target, Target_Type, Aggr);
      end case;
   end Translate_Aggregate;

   procedure Translate_Aggregate_Sub_Bounds
     (Bounds : Mnode; Aggr : Iir; Mode : Object_Kind_Type);

   procedure Translate_Array_Aggregate_Bounds
     (Bounds : Mnode; Aggr : Iir; Mode : Object_Kind_Type)
   is
      Aggr_Type : constant Iir := Get_Base_Type (Get_Type (Aggr));
      El_Type : constant Iir := Get_Element_Subtype (Aggr_Type);
      Assoc : Iir;
      Static_Len : Int64;
      Var_Len : O_Dnode;
      Expr_Type : Iir;
      Range_Type : Iir;
      El_Bounds_Copied : Boolean;
   begin
      pragma Assert (Is_Stable (Bounds));
      Static_Len := 0;

      --  If the element subtype is fully constrained, there is no bounds to
      --  be copied.
      El_Bounds_Copied := Is_Fully_Constrained_Type (El_Type);

      --  First pass: static length.
      Assoc := Get_Association_Choices_Chain (Aggr);
      while Assoc /= Null_Iir loop
         pragma Assert (Get_Kind (Assoc) = Iir_Kind_Choice_By_None);
         if Get_Element_Type_Flag (Assoc) then
            Static_Len := Static_Len + 1;
            if not El_Bounds_Copied then
               declare
                  Expr : constant Iir := Get_Associated_Expr (Assoc);
                  Expr_Bnd : Mnode;
                  El_Layout : Mnode;
                  Info : Ortho_Info_Acc;
                  Obj : Mnode;
               begin
                  Expr_Type := Get_Type (Expr);
                  if Is_Fully_Constrained_Type (Expr_Type) then
                     Expr_Bnd := Chap3.Get_Composite_Type_Bounds (Expr_Type);
                  else
                     Obj := Chap6.Translate_Name (Expr, Mode);
                     Stabilize (Obj);
                     Info := Add_Info (Assoc, Kind_Expr_Eval);
                     Info.Expr_Eval := Obj;
                     Expr_Bnd := Chap3.Get_Composite_Bounds (Obj);
                  end if;
                  El_Layout := Chap3.Array_Bounds_To_Element_Bounds
                    (Bounds, Aggr_Type);
                  Chap3.Copy_Bounds (El_Layout, Expr_Bnd, El_Type);
                  --  Compute size.
                  --  TODO: this is just a multiplication, could be done
                  --  inline.
                  Chap3.Gen_Call_Type_Builder
                    (Chap3.Array_Bounds_To_Element_Layout (Bounds, Aggr_Type),
                     Expr_Type, Mode);
                  if Mode = Mode_Signal then
                     Chap3.Gen_Call_Type_Builder
                       (Chap3.Array_Bounds_To_Element_Layout (Bounds,
                                                              Aggr_Type),
                        Expr_Type, Mode_Value);
                  end if;
               end;
            end if;
         else
            Expr_Type := Get_Type (Get_Associated_Expr (Assoc));
            pragma Assert (Is_One_Dimensional_Array_Type (Expr_Type));
            if Get_Constraint_State (Expr_Type) = Fully_Constrained then
               Range_Type := Get_Index_Type (Expr_Type, 0);
               if Get_Type_Staticness (Range_Type) = Locally then
                  Static_Len :=
                    Static_Len + Eval_Discrete_Type_Length (Range_Type);
               end if;
            else
               --  TODO
               raise Internal_Error;
            end if;
         end if;
         Assoc := Get_Chain (Assoc);
      end loop;

      --  Second pass: non-static length.
      Var_Len := Create_Temp (Ghdl_Index_Type);
      New_Assign_Stmt (New_Obj (Var_Len),
                       New_Lit (New_Index_Lit (Unsigned_64 (Static_Len))));
      Assoc := Get_Association_Choices_Chain (Aggr);
      while Assoc /= Null_Iir loop
         pragma Assert (Get_Kind (Assoc) = Iir_Kind_Choice_By_None);
         if not Get_Element_Type_Flag (Assoc) then
            Expr_Type := Get_Type (Get_Associated_Expr (Assoc));
            if Get_Constraint_State (Expr_Type) = Fully_Constrained then
               Range_Type := Get_Index_Type (Expr_Type, 0);
               if Get_Type_Staticness (Range_Type) /= Locally then
                  declare
                     Bnd : Mnode;
                     L : Mnode;
                  begin
                     Bnd := Chap3.Get_Composite_Type_Bounds (Expr_Type);

                     L := Chap3.Range_To_Length
                       (Chap3.Bounds_To_Range (Bnd, Expr_Type, 1));
                     New_Assign_Stmt
                       (New_Obj (Var_Len),
                        New_Dyadic_Op (ON_Add_Ov,
                                       New_Obj_Value (Var_Len), M2E (L)));
                  end;
               end if;
            else
               --  TODO
               raise Internal_Error;
            end if;
         end if;
         Assoc := Get_Chain (Assoc);
      end loop;

      --  FIXME: what about the other ranges: no need to compute, but extract
      --   and check match.
      Chap3.Create_Range_From_Length
        (Get_Index_Type (Aggr_Type, 0), Var_Len,
         Chap3.Bounds_To_Range (Bounds, Aggr_Type, 1), Aggr);
   end Translate_Array_Aggregate_Bounds;

   procedure Translate_Record_Aggregate_Bounds
     (Bounds : Mnode; Aggr : Iir; Mode : Object_Kind_Type)
   is
      Stable_Bounds : Mnode;
      Aggr_Type : constant Iir := Get_Type (Aggr);
      Base_El_List : constant Iir_Flist :=
        Get_Elements_Declaration_List (Get_Base_Type (Aggr_Type));

      Pos : Natural;
      Base_El : Iir;
      Base_El_Type : Iir;

      Others_Assoc : Iir;
      Assoc : Iir;

      Expr : Iir;
      Expr_Type : Iir;
      Val : Mnode;
      Info : Ortho_Info_Acc;
   begin
      Stable_Bounds := Stabilize (Bounds);

      Others_Assoc := Null_Iir;
      Pos := 0;
      Assoc := Get_Association_Choices_Chain (Aggr);
      while Assoc /= Null_Iir loop
         case Iir_Kinds_Record_Choice (Get_Kind (Assoc)) is
            when Iir_Kind_Choice_By_Others =>
               Others_Assoc := Assoc;
               pragma Assert (Get_Chain (Assoc) = Null_Iir);
               exit;
            when Iir_Kind_Choice_By_None =>
               null;
            when Iir_Kind_Choice_By_Name =>
               pragma Assert
                 (Get_Element_Position
                    (Get_Named_Entity
                       (Get_Choice_Name (Assoc))) = Iir_Index32 (Pos));
               null;
         end case;
         Base_El := Get_Nth_Element (Base_El_List, Pos);
         Base_El_Type := Get_Type (Base_El);
         if Is_Unbounded_Type (Get_Info (Base_El_Type)) then
            --  There are corresponding bounds.
            Expr := Get_Associated_Expr (Assoc);
            Expr_Type := Get_Type (Expr);
            if False
              and then Get_Constraint_State (Expr_Type) = Fully_Constrained
            then
               --  Translate subtype, and copy bounds.
               raise Internal_Error;
            else
               if Get_Kind (Expr) = Iir_Kind_Aggregate then
                  --  Just translate bounds.
                  Translate_Aggregate_Sub_Bounds
                    (Chap3.Record_Bounds_To_Element_Bounds (Stable_Bounds,
                                                            Base_El),
                     Expr, Mode);
               else
                  --  Eval expr
                  Val := Translate_Expression (Expr);
                  Val := Stabilize (Val);
                  Info := Add_Info (Assoc, Kind_Expr_Eval);
                  Info.Expr_Eval := Val;

                  --  Copy bounds.
                  Chap3.Copy_Bounds
                    (Chap3.Record_Bounds_To_Element_Bounds
                       (Stable_Bounds, Base_El),
                     Chap3.Get_Composite_Bounds (Val), Expr_Type);
               end if;
            end if;
         end if;

         Pos := Pos + 1;
         Assoc := Get_Chain (Assoc);
      end loop;
      pragma Assert (Others_Assoc = Null_Iir);  --  TODO
   end Translate_Record_Aggregate_Bounds;

   --  Just create the bounds from AGGR.
   procedure Translate_Aggregate_Sub_Bounds
     (Bounds : Mnode; Aggr : Iir; Mode : Object_Kind_Type)
   is
      Aggr_Type : constant Iir := Get_Type (Aggr);
   begin
      case Iir_Kinds_Composite_Type_Definition (Get_Kind (Aggr_Type)) is
         when Iir_Kind_Array_Type_Definition
           | Iir_Kind_Array_Subtype_Definition =>
            Translate_Array_Aggregate_Bounds (Bounds, Aggr, Mode);
         when Iir_Kind_Record_Type_Definition
           | Iir_Kind_Record_Subtype_Definition =>
            Translate_Record_Aggregate_Bounds (Bounds, Aggr, Mode);
      end case;
   end Translate_Aggregate_Sub_Bounds;

   --  Create the bounds and build the type (set size).
   procedure Translate_Aggregate_Bounds
     (Bounds : Mnode; Aggr : Iir; Mode : Object_Kind_Type)
   is
      Aggr_Type : constant Iir := Get_Type (Aggr);
   begin
      case Iir_Kinds_Composite_Type_Definition (Get_Kind (Aggr_Type)) is
         when Iir_Kind_Array_Type_Definition
           | Iir_Kind_Array_Subtype_Definition =>
            Translate_Array_Aggregate_Bounds (Bounds, Aggr, Mode);
            declare
               El_Type : constant Iir := Get_Element_Subtype (Aggr_Type);
            begin
               --  The array aggregate may be unbounded simply because the
               --  indexes are not known but its element is bounded.
               if Is_Unbounded_Type (Get_Info (El_Type)) then
                  Chap3.Gen_Call_Type_Builder
                    (Chap3.Array_Bounds_To_Element_Layout (Bounds, Aggr_Type),
                     El_Type, Mode);
               end if;
            end;
         when Iir_Kind_Record_Type_Definition
           | Iir_Kind_Record_Subtype_Definition =>
            Translate_Record_Aggregate_Bounds (Bounds, Aggr, Mode);
            Chap3.Gen_Call_Type_Builder (Bounds, Aggr_Type, Mode);
      end case;
   end Translate_Aggregate_Bounds;

   function Translate_Allocator_By_Expression (Expr : Iir) return O_Enode
   is
      --  TODO: the constraint from an access subtype is ignored.
      A_Type : constant Iir := Get_Base_Type (Get_Type (Expr));
      A_Info : constant Type_Info_Acc := Get_Info (A_Type);
      D_Type : constant Iir := Get_Designated_Type (A_Type);
      D_Info : constant Type_Info_Acc := Get_Info (D_Type);
      Val    : O_Enode;
      R      : Mnode;
   begin
      --  Compute the expression.
      Val := Translate_Expression (Get_Expression (Expr), D_Type);

      --  Allocate memory for the object.
      case A_Info.Type_Mode is
         when Type_Mode_Bounds_Acc =>
            declare
               Res : O_Dnode;
               Val_Size : O_Dnode;
               Bounds_Size : O_Cnode;
               Val_M  : Mnode;
            begin
               Res := Create_Temp (A_Info.Ortho_Type (Mode_Value));
               Val_M := Stabilize (E2M (Val, D_Info, Mode_Value));

               --  Size of the value (object without the bounds).
               Val_Size := Create_Temp_Init
                 (Ghdl_Index_Type,
                  Chap3.Get_Subtype_Size
                    (D_Type, Chap3.Get_Composite_Bounds (Val_M), Mode_Value));

               --  Size of the bounds.
               Bounds_Size :=
                 New_Sizeof (D_Info.B.Bounds_Type, Ghdl_Index_Type);

               --  Allocate the object.
               New_Assign_Stmt
                 (New_Obj (Res),
                  Gen_Alloc (Alloc_Heap,
                             New_Dyadic_Op
                               (ON_Add_Ov,
                                New_Lit (Bounds_Size),
                                New_Obj_Value (Val_Size)),
                             A_Info.Ortho_Type (Mode_Value)));

               --  Copy bounds.
               Gen_Memcpy
                 (New_Obj_Value (Res),
                  M2Addr (Chap3.Get_Composite_Bounds (Val_M)),
                  New_Lit (Bounds_Size));

               --  Copy values.
               Gen_Memcpy
                 (Chap3.Get_Bounds_Acc_Base (New_Obj_Value (Res), D_Type),
                  M2Addr (Chap3.Get_Composite_Base (Val_M)),
                  New_Obj_Value (Val_Size));

               return New_Obj_Value (Res);
            end;
         when Type_Mode_Acc =>
            R := Dp2M (Create_Temp (D_Info.Ortho_Ptr_Type (Mode_Value)),
                       D_Info, Mode_Value);
            Chap3.Translate_Object_Allocation
              (R, Alloc_Heap, D_Type, Mnode_Null);
            Chap3.Translate_Object_Copy
              (R, E2M (Val, D_Info, Mode_Value), D_Type);
            return New_Convert_Ov (M2Addr (R), A_Info.Ortho_Type (Mode_Value));
         when others =>
            raise Internal_Error;
      end case;
   end Translate_Allocator_By_Expression;

   function Bounds_Acc_To_Fat_Pointer (Ptr : O_Dnode; Acc_Type : Iir)
                                      return Mnode
   is
      D_Type   : constant Iir :=
        Get_Designated_Type (Get_Base_Type (Acc_Type));
      D_Info   : constant Type_Info_Acc := Get_Info (D_Type);
      Res : Mnode;
   begin
      Res := Dv2M (Create_Temp (D_Info.Ortho_Type (Mode_Value)),
                   D_Info, Mode_Value);

      New_Assign_Stmt
        (M2Lp (Chap3.Get_Composite_Bounds (Res)),
         New_Convert_Ov (New_Obj_Value (Ptr), D_Info.B.Bounds_Ptr_Type));
      New_Assign_Stmt
        (M2Lp (Chap3.Get_Composite_Base (Res)),
         Chap3.Get_Bounds_Acc_Base (New_Obj_Value (Ptr), D_Type));
      return Res;
   end Bounds_Acc_To_Fat_Pointer;

   function Translate_Allocator_By_Subtype (Expr : Iir) return O_Enode
   is
      A_Type   : constant Iir := Get_Type (Expr);
      A_Info   : constant Type_Info_Acc := Get_Info (A_Type);
      D_Type   : constant Iir := Get_Designated_Type (A_Type);
      D_Info   : constant Type_Info_Acc := Get_Info (D_Type);
      Bounds   : Mnode;
      Res      : Mnode;
   begin
      case A_Info.Type_Mode is
         when Type_Mode_Bounds_Acc =>
            declare
               Sub_Type : Iir;
               Ptr : O_Dnode;
               Val_Size : O_Dnode;
               Bounds_Size : O_Cnode;
            begin
               Sub_Type := Get_Subtype_Indication (Expr);
               Sub_Type := Get_Type_Of_Subtype_Indication (Sub_Type);
               Chap3.Create_Composite_Subtype (Sub_Type);

               Ptr := Create_Temp (A_Info.Ortho_Type (Mode_Value));

               --  Size of the value (object without the bounds).
               Val_Size := Create_Temp_Init
                 (Ghdl_Index_Type,
                  Chap3.Get_Subtype_Size
                    (D_Type, Chap3.Get_Composite_Type_Bounds (Sub_Type),
                     Mode_Value));

               --  Size of the bounds.
               Bounds_Size :=
                 New_Sizeof (D_Info.B.Bounds_Type, Ghdl_Index_Type);

               --  Allocate the object.
               New_Assign_Stmt
                 (New_Obj (Ptr),
                  Gen_Alloc (Alloc_Heap,
                             New_Dyadic_Op
                               (ON_Add_Ov,
                                New_Lit (Bounds_Size),
                                New_Obj_Value (Val_Size)),
                             A_Info.Ortho_Type (Mode_Value)));

               --  Copy bounds.
               Gen_Memcpy (New_Obj_Value (Ptr),
                           M2Addr (Chap3.Get_Composite_Type_Bounds (Sub_Type)),
                           New_Lit (Bounds_Size));

               --  Create a fat pointer to initialize the object.
               Res := Bounds_Acc_To_Fat_Pointer (Ptr, A_Type);
               Chap4.Init_Object (Res, D_Type);

               return New_Obj_Value (Ptr);
            end;
         when Type_Mode_Acc =>
            Res := Dp2M (Create_Temp (D_Info.Ortho_Ptr_Type (Mode_Value)),
                         D_Info, Mode_Value);
            Bounds := Mnode_Null;
            Chap3.Translate_Object_Allocation
              (Res, Alloc_Heap, D_Type, Bounds);
            Chap4.Init_Object (Res, D_Type);
            return New_Convert_Ov
              (M2Addr (Res), A_Info.Ortho_Type (Mode_Value));
         when others =>
            raise Internal_Error;
      end case;
   end Translate_Allocator_By_Subtype;

   function Translate_Fat_Array_Type_Conversion
     (Expr : O_Enode; Expr_Type : Iir; Res_Type : Iir; Loc : Iir)
     return O_Enode;

   function Translate_Array_Subtype_Conversion
     (Expr : O_Enode; Expr_Type : Iir; Res_Type : Iir; Loc : Iir)
     return O_Enode
   is
      Res_Info  : constant Type_Info_Acc := Get_Info (Res_Type);
      Expr_Info : constant Type_Info_Acc := Get_Info (Expr_Type);
      E         : Mnode;
   begin
      E := Stabilize (E2M (Expr, Expr_Info, Mode_Value));
      case Res_Info.Type_Mode is
         when Type_Mode_Bounded_Arrays =>
            Chap3.Check_Composite_Match
              (Res_Type, T2M (Res_Type, Mode_Value),
               Expr_Type, E,
               Loc);
            return New_Convert_Ov
              (M2Addr (Chap3.Get_Composite_Base (E)),
               Res_Info.Ortho_Ptr_Type (Mode_Value));
         when Type_Mode_Unbounded_Array =>
            declare
               Res : Mnode;
            begin
               Res := Create_Temp (Res_Info);
               Copy_Fat_Pointer (Res, E);
               Chap3.Check_Composite_Match (Res_Type, Res, Expr_Type, E, Loc);
               return M2Addr (Res);
            end;
         when others =>
            Error_Kind ("translate_array_subtype_conversion", Res_Type);
      end case;
   end Translate_Array_Subtype_Conversion;

   function Translate_Type_Conversion
     (Expr : O_Enode; Expr_Type : Iir; Res_Type : Iir; Loc : Iir)
     return O_Enode
   is
      Res_Info : constant Type_Info_Acc := Get_Info (Res_Type);
      Res      : O_Enode;
   begin
      case Get_Kind (Res_Type) is
         when Iir_Kinds_Scalar_Type_And_Subtype_Definition =>
            Res := New_Convert_Ov (Expr, Res_Info.Ortho_Type (Mode_Value));
            if Chap3.Need_Range_Check (Null_Iir, Res_Type) then
               Res := Chap3.Insert_Scalar_Check
                 (Res, Null_Iir, Res_Type, Loc);
            end if;
            return Res;
         when Iir_Kinds_Array_Type_Definition =>
            if Get_Constraint_State (Res_Type) = Fully_Constrained then
               return Translate_Array_Subtype_Conversion
                 (Expr, Expr_Type, Res_Type, Loc);
            else
               return Translate_Fat_Array_Type_Conversion
                 (Expr, Expr_Type, Res_Type, Loc);
            end if;
         when Iir_Kind_Record_Type_Definition
            | Iir_Kind_Record_Subtype_Definition =>
            return Expr;
         when others =>
            Error_Kind ("translate_type_conversion", Res_Type);
      end case;
   end Translate_Type_Conversion;

   procedure Translate_Type_Conversion_Bounds
     (Res : Mnode; Src : Mnode; Res_Type : Iir; Src_Type : Iir; Loc : Iir)
   is
      Res_Indexes  : constant Iir_Flist := Get_Index_Subtype_List (Res_Type);
      Src_Indexes  : constant Iir_Flist := Get_Index_Subtype_List (Src_Type);
      Res_Base_Type    : constant Iir := Get_Base_Type (Res_Type);
      Src_Base_Type    : constant Iir := Get_Base_Type (Src_Type);
      Res_Base_Indexes : constant Iir_Flist :=
        Get_Index_Subtype_List (Res_Base_Type);
      Src_Base_Indexes : constant Iir_Flist :=
        Get_Index_Subtype_List (Src_Base_Type);

      R_El              : Iir;
      S_El              : Iir;
   begin
      --  Convert bounds.
      for I in Flist_First .. Flist_Last (Src_Indexes) loop
         R_El := Get_Index_Type (Res_Indexes, I);
         S_El := Get_Index_Type (Src_Indexes, I);
         declare
            Rb_Ptr          : Mnode;
            Sb_Ptr          : Mnode;
            Ee              : O_Enode;
            Same_Index_Type : constant Boolean :=
              (Get_Index_Type (Res_Base_Indexes, I)
               = Get_Index_Type (Src_Base_Indexes, I));
         begin
            Open_Temp;
            Rb_Ptr := Stabilize (Chap3.Bounds_To_Range (Res, Res_Type, I + 1));
            Sb_Ptr := Stabilize (Chap3.Bounds_To_Range (Src, Src_Type, I + 1));
            --  Convert left and right (unless they have the same type -
            --  this is an optimization but also this deals with null
            --  array in common cases).
            Ee := M2E (Chap3.Range_To_Left (Sb_Ptr));
            if not Same_Index_Type then
               Ee := Translate_Type_Conversion (Ee, S_El, R_El, Loc);
            end if;
            New_Assign_Stmt (M2Lv (Chap3.Range_To_Left (Rb_Ptr)), Ee);
            Ee := M2E (Chap3.Range_To_Right (Sb_Ptr));
            if not Same_Index_Type then
               Ee := Translate_Type_Conversion (Ee, S_El, R_El, Loc);
            end if;
            New_Assign_Stmt (M2Lv (Chap3.Range_To_Right (Rb_Ptr)), Ee);
            --  Copy Dir and Length.
            New_Assign_Stmt (M2Lv (Chap3.Range_To_Dir (Rb_Ptr)),
                             M2E (Chap3.Range_To_Dir (Sb_Ptr)));
            New_Assign_Stmt (M2Lv (Chap3.Range_To_Length (Rb_Ptr)),
                             M2E (Chap3.Range_To_Length (Sb_Ptr)));
            Close_Temp;
         end;
      end loop;
   end Translate_Type_Conversion_Bounds;

   function Translate_Fat_Array_Type_Conversion
     (Expr : O_Enode; Expr_Type : Iir; Res_Type : Iir; Loc : Iir)
     return O_Enode
   is
      Res_Info  : constant Type_Info_Acc := Get_Info (Res_Type);
      Expr_Info : constant Type_Info_Acc := Get_Info (Expr_Type);

      Res       : Mnode;
      E         : Mnode;
      Bounds    : O_Dnode;
   begin
      Res := Create_Temp (Res_Info, Mode_Value);
      Bounds := Create_Temp (Res_Info.B.Bounds_Type);

      Open_Temp;
      E := Stabilize (E2M (Expr, Expr_Info, Mode_Value));

      --  Set base.
      New_Assign_Stmt
        (M2Lp (Chap3.Get_Composite_Base (Res)),
         New_Convert_Ov (M2Addr (Chap3.Get_Composite_Base (E)),
           Res_Info.B.Base_Ptr_Type (Mode_Value)));
      --  Set bounds.
      New_Assign_Stmt
        (M2Lp (Chap3.Get_Composite_Bounds (Res)),
         New_Address (New_Obj (Bounds), Res_Info.B.Bounds_Ptr_Type));

      --  Convert bounds.
      Translate_Type_Conversion_Bounds
        (Dv2M (Bounds, Res_Info, Mode_Value,
               Res_Info.B.Bounds_Type, Res_Info.B.Bounds_Ptr_Type),
         Stabilize (Chap3.Get_Composite_Bounds (E)),
         Res_Type, Expr_Type, Loc);

      Close_Temp;
      return M2E (Res);
   end Translate_Fat_Array_Type_Conversion;

   function Sig2val_Prepare_Composite
     (Targ : Mnode; Targ_Type : Iir; Data : Mnode) return Mnode
   is
      pragma Unreferenced (Targ, Targ_Type);
   begin
      if Get_Type_Info (Data).Type_Mode in Type_Mode_Unbounded then
         return Stabilize (Chap3.Get_Composite_Base (Data));
      else
         return Stabilize (Data);
      end if;
   end Sig2val_Prepare_Composite;

   function Sig2val_Update_Data_Array
     (Val : Mnode; Targ_Type : Iir; Index : O_Dnode) return Mnode is
   begin
      return Chap3.Index_Base (Val, Targ_Type, New_Obj_Value (Index));
   end Sig2val_Update_Data_Array;

   function Sig2val_Update_Data_Record
     (Val : Mnode; Targ_Type : Iir; El : Iir_Element_Declaration) return Mnode
   is
      pragma Unreferenced (Targ_Type);
   begin
      return Chap6.Translate_Selected_Element (Val, El);
   end Sig2val_Update_Data_Record;

   procedure Translate_Signal_Assign_Driving_Non_Composite
     (Targ : Mnode; Targ_Type : Iir; Data: Mnode) is
   begin
      New_Assign_Stmt
        (Chap14.Get_Signal_Value_Field (M2E (Targ), Targ_Type,
                                        Ghdl_Signal_Driving_Value_Field),
         M2E (Data));
   end Translate_Signal_Assign_Driving_Non_Composite;

   procedure Translate_Signal_Assign_Driving is new Foreach_Non_Composite
     (Data_Type => Mnode,
      Composite_Data_Type => Mnode,
      Do_Non_Composite => Translate_Signal_Assign_Driving_Non_Composite,
      Prepare_Data_Array => Sig2val_Prepare_Composite,
      Update_Data_Array => Sig2val_Update_Data_Array,
      Prepare_Data_Record => Sig2val_Prepare_Composite,
      Update_Data_Record => Sig2val_Update_Data_Record);

   function Allocate_Value_From_Signal (Sig : Mnode; Sig_Type : Iir)
                                       return Mnode
   is
      Tinfo : constant Type_Info_Acc := Get_Info (Sig_Type);
      Res     : Mnode;
   begin
      if Tinfo.Type_Mode in Type_Mode_Unbounded then
         Res := Create_Temp (Tinfo);

         --  Copy bounds.
         New_Assign_Stmt
           (M2Lp (Chap3.Get_Composite_Bounds (Res)),
            M2Addr (Chap3.Get_Composite_Bounds (Sig)));

         --  Allocate base.
         Chap3.Allocate_Unbounded_Composite_Base (Alloc_Stack, Res, Sig_Type);
      elsif Is_Complex_Type (Tinfo) then
         Res := Create_Temp (Tinfo);
         Chap4.Allocate_Complex_Object (Sig_Type, Alloc_Stack, Res);
      else
         Res := Create_Temp (Tinfo);
      end if;

      return Res;
   end Allocate_Value_From_Signal;

   function Translate_Signal_Value (Sig : Mnode; Sig_Type : Iir) return Mnode
   is
      procedure Translate_Signal_Non_Composite
        (Targ      : Mnode;
         Targ_Type : Iir;
         Data      : Mnode) is
      begin
         New_Assign_Stmt (M2Lv (Targ),
                          Read_Value (M2E (Data), Targ_Type));
      end Translate_Signal_Non_Composite;

      procedure Translate_Signal_Target is new Foreach_Non_Composite
        (Data_Type => Mnode,
         Composite_Data_Type => Mnode,
         Do_Non_Composite => Translate_Signal_Non_Composite,
         Prepare_Data_Array => Sig2val_Prepare_Composite,
         Update_Data_Array => Sig2val_Update_Data_Array,
         Prepare_Data_Record => Sig2val_Prepare_Composite,
         Update_Data_Record => Sig2val_Update_Data_Record);

      Tinfo : constant Type_Info_Acc := Get_Info (Sig_Type);
      Sig2 : Mnode;
      Res : Mnode;
   begin
      if Tinfo.Type_Mode in Type_Mode_Scalar then
         return E2M (Read_Value (M2E (Sig), Sig_Type), Tinfo, Mode_Value);
      else
         Sig2 := Stabilize (Sig);
         pragma Unreferenced (Sig);

         Res := Allocate_Value_From_Signal (Sig2, Sig_Type);

         Open_Temp;
         Translate_Signal_Target (Res, Sig_Type, Sig2);
         Close_Temp;

         return Res;
      end if;
   end Translate_Signal_Value;

   function Read_Signal_Driving_Value (Sig : O_Enode; Sig_Type : Iir)
                                      return O_Enode is
   begin
      return New_Value (Chap14.Get_Signal_Value_Field
                        (Sig, Sig_Type, Ghdl_Signal_Driving_Value_Field));
   end Read_Signal_Driving_Value;

   function Translate_Signal_Driving_Value_1 is new Translate_Signal_Value
     (Read_Value => Read_Signal_Driving_Value);

   function Translate_Signal_Driving_Value
     (Sig : Mnode; Sig_Type : Iir) return Mnode
         renames Translate_Signal_Driving_Value_1;

   procedure Set_Driving_Value
     (Sig : Mnode; Sig_Type : Iir; Val : Mnode)
         renames Translate_Signal_Assign_Driving;

   function Translate_Overflow_Literal (Expr : Iir) return O_Enode
   is
      Expr_Type : constant Iir := Get_Type (Expr);
      Tinfo : Type_Info_Acc;
      Otype : O_Tnode;
      L     : O_Dnode;
   begin
      Chap3.Translate_Anonymous_Subtype_Definition (Expr_Type, False);

      --  Generate the error message
      Chap6.Gen_Bound_Error (Expr);

      --  Create a dummy value, for type checking.  But never
      --  executed.
      Tinfo := Get_Info (Expr_Type);
      Otype := Tinfo.Ortho_Type (Mode_Value);
      L := Create_Temp (Otype);
      if Tinfo.Type_Mode in Type_Mode_Fat then
         --  For fat pointers or arrays.
         return New_Address (New_Obj (L),
                             Tinfo.Ortho_Ptr_Type (Mode_Value));
      else
         return New_Obj_Value (L);
      end if;
   end Translate_Overflow_Literal;

   function Translate_Aggregate_Expression (Expr : Iir; Rtype : Iir)
                                            return  O_Enode
   is
      Expr_Type : constant Iir := Get_Type (Expr);
      Aggr_Type : Iir;
      Tinfo     : Type_Info_Acc;
      Bounds    : Mnode;
      Mres      : Mnode;
      Res       : O_Enode;
   begin
      --  Extract the type of the aggregate.  Use the type of the
      --  context if it is fully constrained.
      Aggr_Type := Expr_Type;
      if Rtype /= Null_Iir
        and then Is_Fully_Constrained_Type (Rtype)
      then
         Aggr_Type := Rtype;
      end if;

      if Get_Constraint_State (Aggr_Type) /= Fully_Constrained then
         Tinfo := Get_Info (Aggr_Type);
         if Tinfo = null then
            --  AGGR_TYPE may be a subtype that has not been
            --  translated.  Use the base type in that case.
            Aggr_Type := Get_Base_Type (Aggr_Type);
            Tinfo := Get_Info (Aggr_Type);
         end if;

         Mres := Create_Temp (Tinfo);
         Bounds := Create_Temp_Bounds (Tinfo);
         New_Assign_Stmt (M2Lp (Chap3.Get_Composite_Bounds (Mres)),
                          M2Addr (Bounds));
         --  Build bounds from aggregate.
         Chap7.Translate_Aggregate_Bounds (Bounds, Expr, Mode_Value);
         Chap3.Allocate_Unbounded_Composite_Base
           (Alloc_Stack, Mres, Aggr_Type);
      else
         Chap3.Create_Composite_Subtype (Aggr_Type);

         --  FIXME: this may be not necessary
         Tinfo := Get_Info (Aggr_Type);

         --  The result area has to be created
         if Is_Complex_Type (Tinfo) then
            Mres := Create_Temp (Tinfo);
            Chap4.Allocate_Complex_Object (Aggr_Type, Alloc_Stack, Mres);
         else
            --  if thin array/record:
            --    create result
            Mres := Create_Temp (Tinfo);
         end if;
      end if;

      Translate_Aggregate (Mres, Aggr_Type, Expr);
      Res := M2E (Mres);

      if Rtype /= Null_Iir and then Aggr_Type /= Rtype then
         Res := Translate_Implicit_Conv
           (Res, Aggr_Type, Rtype, Mode_Value, Expr);
      end if;
      return Res;
   end Translate_Aggregate_Expression;

   function Translate_Expression (Expr : Iir; Rtype : Iir := Null_Iir)
                                 return Mnode
   is
      Res_Type : Iir;
      Res : O_Enode;
   begin
      if Rtype = Null_Iir then
         Res_Type := Get_Type (Expr);
      else
         Res_Type := Rtype;
      end if;
      Res := Translate_Expression (Expr, Res_Type);
      return E2M (Res, Get_Info (Res_Type), Mode_Value);
   end Translate_Expression;

   function Translate_Expression (Expr : Iir; Rtype : Iir := Null_Iir)
                                 return O_Enode
   is
      Imp       : Iir;
      Expr_Type : Iir;
      Res_Type  : Iir;
      Res       : O_Enode;
   begin
      Expr_Type := Get_Type (Expr);
      if Rtype = Null_Iir then
         Res_Type := Expr_Type;
      else
         Res_Type := Rtype;
      end if;
      case Get_Kind (Expr) is
         when Iir_Kind_Integer_Literal
            | Iir_Kind_Enumeration_Literal
            | Iir_Kind_Floating_Point_Literal =>
            return New_Lit (Translate_Static_Expression (Expr, Rtype));

         when Iir_Kind_Physical_Int_Literal
           | Iir_Kind_Physical_Fp_Literal
           | Iir_Kind_Unit_Declaration =>
            declare
               Otype : constant O_Tnode :=
                 Get_Ortho_Type (Expr_Type, Mode_Value);
               Val : Int64;
            begin
               --  Get the value now, as it may generate a constraint_error.
               Val := Get_Physical_Value (Expr);
               return New_Lit (New_Signed_Literal (Otype, Integer_64 (Val)));
            exception
               when Constraint_Error =>
                  Warning_Msg_Elab (Warnid_Runtime_Error, Expr,
                                    "physical literal out of range");
                  return Translate_Overflow_Literal (Expr);
            end;

         when Iir_Kind_String_Literal8
            | Iir_Kind_Simple_Aggregate
            | Iir_Kind_Simple_Name_Attribute =>
            return Translate_Composite_Literal (Expr, Res_Type);

         when Iir_Kind_Aggregate =>
            if Get_Aggregate_Expand_Flag (Expr) then
               return Translate_Composite_Literal (Expr, Res_Type);
            else
               return Translate_Aggregate_Expression (Expr, Rtype);
            end if;

         when Iir_Kind_Null_Literal =>
            return New_Lit (Translate_Null_Literal (Expr, Res_Type));

         when Iir_Kind_Overflow_Literal =>
            return Translate_Overflow_Literal (Expr);

         when Iir_Kind_Parenthesis_Expression =>
            return Translate_Expression (Get_Expression (Expr), Rtype);

         when Iir_Kind_Allocator_By_Expression =>
            return Translate_Allocator_By_Expression (Expr);
         when Iir_Kind_Allocator_By_Subtype =>
            return Translate_Allocator_By_Subtype (Expr);

         when Iir_Kind_Qualified_Expression =>
            --  FIXME: check type.
            Res := Translate_Expression (Get_Expression (Expr), Expr_Type);

         when Iir_Kind_Constant_Declaration
            | Iir_Kind_Variable_Declaration
            | Iir_Kind_Signal_Declaration
            | Iir_Kind_File_Declaration
            | Iir_Kind_Object_Alias_Declaration
            | Iir_Kind_Interface_Constant_Declaration
            | Iir_Kind_Interface_Variable_Declaration
            | Iir_Kind_Interface_Signal_Declaration
            | Iir_Kind_Interface_File_Declaration
            | Iir_Kind_Indexed_Name
            | Iir_Kind_Slice_Name
            | Iir_Kind_Selected_Element
            | Iir_Kind_Dereference
            | Iir_Kind_Implicit_Dereference
            | Iir_Kind_Stable_Attribute
            | Iir_Kind_Quiet_Attribute
            | Iir_Kind_Delayed_Attribute
            | Iir_Kind_Transaction_Attribute
            | Iir_Kind_Guard_Signal_Declaration
            | Iir_Kind_Attribute_Value
            | Iir_Kind_Attribute_Name =>
            Res := M2E (Chap6.Translate_Name (Expr, Mode_Value));

         when Iir_Kind_Iterator_Declaration =>
            declare
               Expr_Info : Ortho_Info_Acc;
            begin
               Expr_Info := Get_Info (Expr);
               Res := New_Value (Get_Var (Expr_Info.Iterator_Var));
               if Rtype /= Null_Iir then
                  Res := New_Convert_Ov
                    (Res, Get_Ortho_Type (Rtype, Mode_Value));
               end if;
               return Res;
            end;

         when Iir_Kinds_Dyadic_Operator =>
            Imp := Get_Implementation (Expr);
            if Is_Implicit_Subprogram (Imp) then
               return Translate_Predefined_Operator
                 (Expr, Get_Left (Expr), Get_Right (Expr), Res_Type);
            else
               return Translate_Operator_Function_Call
                 (Expr, Get_Left (Expr), Get_Right (Expr), Res_Type);
            end if;
         when Iir_Kinds_Monadic_Operator =>
            Imp := Get_Implementation (Expr);
            if Is_Implicit_Subprogram (Imp) then
               return Translate_Predefined_Operator
                 (Expr, Get_Operand (Expr), Null_Iir, Res_Type);
            else
               return Translate_Operator_Function_Call
                 (Expr, Get_Operand (Expr), Null_Iir, Res_Type);
            end if;
         when Iir_Kind_Function_Call =>
            Imp := Get_Implementation (Expr);
            declare
               Assoc_Chain : Iir;
            begin
               if Is_Implicit_Subprogram (Imp) then
                  declare
                     Left, Right : Iir;
                  begin
                     Assoc_Chain := Get_Parameter_Association_Chain (Expr);
                     if Assoc_Chain = Null_Iir then
                        Left := Null_Iir;
                        Right := Null_Iir;
                     else
                        Left := Get_Actual (Assoc_Chain);
                        Assoc_Chain := Get_Chain (Assoc_Chain);
                        if Assoc_Chain = Null_Iir then
                           Right := Null_Iir;
                        else
                           Right := Get_Actual (Assoc_Chain);
                        end if;
                     end if;
                     return Translate_Predefined_Operator
                       (Expr, Left, Right, Res_Type);
                  end;
               else
                  Vhdl.Canon.Canon_Subprogram_Call (Expr);
                  Trans.Update_Node_Infos;
                  Assoc_Chain := Get_Parameter_Association_Chain (Expr);
                  Res := Chap8.Translate_Subprogram_Call
                    (Expr, Assoc_Chain, Get_Method_Object (Expr));
                  Expr_Type := Get_Return_Type (Imp);
               end if;
            end;

         when Iir_Kind_Type_Conversion =>
            declare
               Conv_Expr : constant Iir := Get_Expression (Expr);
            begin
               Res := Translate_Type_Conversion
                 (Translate_Expression (Conv_Expr), Get_Type (Conv_Expr),
                  Expr_Type, Expr);
            end;

         when Iir_Kind_Length_Array_Attribute =>
            return Chap14.Translate_Length_Array_Attribute
              (Expr, Res_Type);
         when Iir_Kind_Low_Array_Attribute =>
            return Chap14.Translate_Low_Array_Attribute (Expr);
         when Iir_Kind_High_Array_Attribute =>
            return Chap14.Translate_High_Array_Attribute (Expr);
         when Iir_Kind_Left_Array_Attribute =>
            return Chap14.Translate_Left_Array_Attribute (Expr);
         when Iir_Kind_Right_Array_Attribute =>
            return Chap14.Translate_Right_Array_Attribute (Expr);
         when Iir_Kind_Ascending_Array_Attribute =>
            return Chap14.Translate_Ascending_Array_Attribute (Expr);

         when Iir_Kind_Val_Attribute =>
            return Chap14.Translate_Val_Attribute (Expr);
         when Iir_Kind_Pos_Attribute =>
            return Chap14.Translate_Pos_Attribute (Expr, Res_Type);

         when Iir_Kind_Succ_Attribute
           | Iir_Kind_Pred_Attribute
           | Iir_Kind_Leftof_Attribute
           | Iir_Kind_Rightof_Attribute =>
            return Chap14.Translate_Succ_Pred_Attribute (Expr);

         when Iir_Kind_Image_Attribute =>
            Res := Chap14.Translate_Image_Attribute (Expr);

         when Iir_Kind_Value_Attribute =>
            return Chap14.Translate_Value_Attribute (Expr);

         when Iir_Kind_Event_Attribute =>
            return Chap14.Translate_Event_Attribute (Expr);
         when Iir_Kind_Active_Attribute =>
            return Chap14.Translate_Active_Attribute (Expr);
         when Iir_Kind_Last_Value_Attribute =>
            Res := Chap14.Translate_Last_Value_Attribute (Expr);

         when Iir_Kind_High_Type_Attribute =>
            return Chap14.Translate_High_Low_Type_Attribute
              (Get_Type (Expr), True);
         when Iir_Kind_Low_Type_Attribute =>
            return Chap14.Translate_High_Low_Type_Attribute
              (Get_Type (Expr), False);
         when Iir_Kind_Left_Type_Attribute =>
            return M2E
              (Chap3.Range_To_Left
                 (Lv2M (Translate_Range (Get_Prefix (Expr), Expr_Type),
                  Get_Info (Get_Base_Type (Expr_Type)), Mode_Value)));
         when Iir_Kind_Right_Type_Attribute =>
            return M2E
              (Chap3.Range_To_Right
                 (Lv2M (Translate_Range (Get_Prefix (Expr), Expr_Type),
                  Get_Info (Get_Base_Type (Expr_Type)), Mode_Value)));

         when Iir_Kind_Last_Event_Attribute =>
            return Chap14.Translate_Last_Time_Attribute
              (Get_Prefix (Expr), Ghdl_Signal_Last_Event_Field);
         when Iir_Kind_Last_Active_Attribute =>
            return Chap14.Translate_Last_Time_Attribute
              (Get_Prefix (Expr), Ghdl_Signal_Last_Active_Field);

         when Iir_Kind_Driving_Value_Attribute =>
            Res := Chap14.Translate_Driving_Value_Attribute (Expr);
         when Iir_Kind_Driving_Attribute =>
            Res := Chap14.Translate_Driving_Attribute (Expr);

         when Iir_Kind_Path_Name_Attribute
            | Iir_Kind_Instance_Name_Attribute =>
            Res := Chap14.Translate_Path_Instance_Name_Attribute (Expr);

         when Iir_Kind_Simple_Name
            | Iir_Kind_Character_Literal
            | Iir_Kind_Selected_Name =>
            return Translate_Expression (Get_Named_Entity (Expr), Rtype);

         when Iir_Kind_Psl_Endpoint_Declaration =>
            declare
               Info : constant Psl_Info_Acc := Get_Info (Expr);
            begin
               return New_Value (Get_Var (Info.Psl_Finish_Count_Var));
            end;

         when others =>
            Error_Kind ("translate_expression", Expr);
      end case;

      --  Quick test to avoid useless calls.
      if Expr_Type /= Res_Type then
         Res := Translate_Implicit_Conv
           (Res, Expr_Type, Res_Type, Mode_Value, Expr);
      end if;

      return Res;
   end Translate_Expression;

   --  Check if RNG is of the form:
   --     1 to T'length
   --  or T'Length downto 1
   --  or 0 to T'length - 1
   --  or T'Length - 1 downto 0
   --  In either of these cases, return T'Length
   function Is_Length_Range_Expression (Rng : Iir_Range_Expression) return Iir