aboutsummaryrefslogtreecommitdiffstats
path: root/frontends/verilog/verilog_lexer.l
diff options
context:
space:
mode:
Diffstat (limited to 'frontends/verilog/verilog_lexer.l')
0 files changed, 0 insertions, 0 deletions
f='#n55'>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 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 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
--  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 Std_Names;
with Vhdl.Std_Package; use Vhdl.Std_Package;
with Vhdl.Errors; use Vhdl.Errors;
with Vhdl.Sem_Inst;
with Vhdl.Nodes_Meta;
with Vhdl.Utils; use Vhdl.Utils;
with Vhdl.Back_End;
with Trans.Chap3;
with Trans.Chap4;
with Trans.Chap5;
with Trans.Chap6;
with Trans.Chap8;
with Trans.Rtis;
with Trans.Helpers2; use Trans.Helpers2;
with Trans_Decls; use Trans_Decls;
with Translation; use Translation;

package body Trans.Chap2 is
   use Trans.Subprgs;
   use Trans.Helpers;

   procedure Elab_Package_Internal (Spec : Iir; Header : Iir);

   type Name_String_Xlat_Array is array (Name_Id range <>) of String (1 .. 4);

   --  Ortho function names are only composed of [A-Za-z0-9_].  For VHDL
   --  functions whose name is an operator symbol, we need to create a name
   --  with letters.
   Operator_String_Xlat : constant
     Name_String_Xlat_Array (Std_Names.Name_Id_Operators) :=
     (Std_Names.Name_Op_Equality => "OPEq",
      Std_Names.Name_Op_Inequality => "OPNe",
      Std_Names.Name_Op_Less => "OPLt",
      Std_Names.Name_Op_Less_Equal => "OPLe",
      Std_Names.Name_Op_Greater => "OPGt",
      Std_Names.Name_Op_Greater_Equal => "OPGe",
      Std_Names.Name_Op_Plus => "OPPl",
      Std_Names.Name_Op_Minus => "OPMi",
      Std_Names.Name_Op_Mul => "OPMu",
      Std_Names.Name_Op_Div => "OPDi",
      Std_Names.Name_Op_Exp => "OPEx",
      Std_Names.Name_Op_Concatenation => "OPCc",
      Std_Names.Name_Op_Condition => "OPCd",
      Std_Names.Name_Op_Match_Equality => "OPQe",
      Std_Names.Name_Op_Match_Inequality => "OPQi",
      Std_Names.Name_Op_Match_Less => "OPQL",
      Std_Names.Name_Op_Match_Less_Equal => "OPQl",
      Std_Names.Name_Op_Match_Greater => "OPQG",
      Std_Names.Name_Op_Match_Greater_Equal => "OPQg");

   --  Set the identifier prefix with the subprogram identifier and
   --  overload number if any.
   procedure Push_Subprg_Identifier (Spec : Iir; Mark : out Id_Mark_Type)
   is
      Id : constant Name_Id := Get_Identifier (Spec);
   begin
      --  FIXME: name_shift_operators, name_logical_operators,
      --   name_word_operators, name_mod, name_rem
      if Id in Std_Names.Name_Id_Operators then
         Push_Identifier_Prefix
           (Mark, Operator_String_Xlat (Id), Get_Overload_Number (Spec));
      else
         Push_Identifier_Prefix (Mark, Id, Get_Overload_Number (Spec));
      end if;
   end Push_Subprg_Identifier;

   --  Return the type of a subprogram interface.
   procedure Translate_Interface_Mechanism (Inter : Iir)
   is
      Spec : constant Iir := Get_Parent (Inter);
      pragma Assert (Get_Kind (Spec) in Iir_Kinds_Subprogram_Declaration);
      Info : constant Interface_Info_Acc := Get_Info (Inter);
      Tinfo : constant Type_Info_Acc := Get_Info (Get_Type (Inter));
      Mech : Call_Mechanism;
   begin
      --  Mechanism.
      case Type_Mode_Valid (Tinfo.Type_Mode) is
         when Type_Mode_Pass_By_Copy =>
            Mech := Pass_By_Copy;
         when Type_Mode_Pass_By_Address =>
            Mech := Pass_By_Address;
      end case;

      case Iir_Kinds_Interface_Object_Declaration (Get_Kind (Inter)) is
         when Iir_Kind_Interface_Constant_Declaration
            | Iir_Kind_Interface_File_Declaration =>
            Info.Interface_Mechanism (Mode_Value) := Mech;
         when Iir_Kind_Interface_Variable_Declaration =>
            if Get_Foreign_Flag (Spec)
              and then Get_Mode (Inter) in Iir_Out_Modes
            then
               Mech := Pass_By_Address;
            end if;
            Info.Interface_Mechanism (Mode_Value) := Mech;
         when Iir_Kind_Interface_Signal_Declaration =>
            Info.Interface_Mechanism (Mode_Signal) := Mech;
            --  Values are always passed by address.
            if Get_Kind (Spec) = Iir_Kind_Procedure_Declaration then
               Mech := Pass_By_Address;
            end if;
            Info.Interface_Mechanism (Mode_Value) := Mech;
         when Iir_Kind_Interface_Quantity_Declaration =>
            raise Internal_Error;
      end case;
   end Translate_Interface_Mechanism;

   function Translate_Interface_Type (Inter : Iir; Mode : Object_Kind_Type)
                                     return O_Tnode
   is
      Info : constant Interface_Info_Acc := Get_Info (Inter);
      Tinfo : constant Type_Info_Acc := Get_Info (Get_Type (Inter));
   begin
      case Info.Interface_Mechanism (Mode) is
         when Pass_By_Address =>
            return Tinfo.Ortho_Ptr_Type (Mode);
         when Pass_By_Copy =>
            return Tinfo.Ortho_Type (Mode);
      end case;
   end Translate_Interface_Type;

   procedure Translate_Subprogram_Interfaces (Spec : Iir)
   is
      Inter : Iir;
      Mark  : Id_Mark_Type;
      Info  : Subprg_Info_Acc;
      El_List : O_Element_List;
      Param_Info : Ortho_Info_Acc;
   begin
      --  Set the identifier prefix with the subprogram identifier and
      --  overload number if any.
      Push_Subprg_Identifier (Spec, Mark);

      --  Translate interface types.
      Inter := Get_Interface_Declaration_Chain (Spec);
      while Inter /= Null_Iir loop
         Chap3.Translate_Object_Subtype_Indication (Inter);
         Inter := Get_Chain (Inter);
      end loop;

      if Get_Kind (Spec) = Iir_Kind_Procedure_Declaration then
         --  Create the param record (except for foreign subprogram).
         Info := Get_Info (Spec);
         Inter := Get_Interface_Declaration_Chain (Spec);
         if (Inter /= Null_Iir or else Get_Suspend_Flag (Spec))
           and then not Get_Foreign_Flag (Spec)
         then
            Start_Record_Type (El_List);

            --  Create fields for interfaces.
            while Inter /= Null_Iir loop
               Param_Info := Add_Info (Inter, Kind_Interface);
               Translate_Interface_Mechanism (Inter);

               New_Record_Field
                 (El_List, Param_Info.Interface_Field (Mode_Value),
                  Create_Identifier_Without_Prefix (Inter),
                  Translate_Interface_Type (Inter, Mode_Value));

               if Get_Kind (Inter) = Iir_Kind_Interface_Signal_Declaration
               then
                  New_Record_Field
                    (El_List, Param_Info.Interface_Field (Mode_Signal),
                     Create_Identifier_Without_Prefix (Inter, "SIG"),
                     Translate_Interface_Type (Inter, Mode_Signal));
               end if;
               Inter := Get_Chain (Inter);
            end loop;

            if Get_Suspend_Flag (Spec) then
               New_Record_Field (El_List, Info.Subprg_State_Field,
                                 Get_Identifier ("STATE"), Ghdl_Index_Type);
               New_Record_Field (El_List, Info.Subprg_Locvars_Field,
                                 Get_Identifier ("FRAME"), Ghdl_Ptr_Type);
            end if;

            --  Declare the record type and an access to the record.
            Finish_Record_Type (El_List, Info.Subprg_Params_Type);
            New_Type_Decl (Create_Identifier ("PARAMSTYPE"),
                           Info.Subprg_Params_Type);
            Info.Subprg_Params_Ptr :=
              New_Access_Type (Info.Subprg_Params_Type);
            New_Type_Decl (Create_Identifier ("PARAMSPTR"),
                           Info.Subprg_Params_Ptr);
         else
            Info.Subprg_Params_Type := O_Tnode_Null;
            Info.Subprg_Params_Ptr := O_Tnode_Null;
         end if;
      end if;
      Pop_Identifier_Prefix (Mark);
   end Translate_Subprogram_Interfaces;

   procedure Elab_Subprogram_Interfaces (Spec : Iir)
   is
      Inter : Iir;
   begin
      --  Translate interface types.
      Inter := Get_Interface_Declaration_Chain (Spec);
      while Inter /= Null_Iir loop
         Chap3.Elab_Object_Subtype_Indication (Inter);
         Inter := Get_Chain (Inter);
      end loop;
   end Elab_Subprogram_Interfaces;

   procedure Translate_Subprogram_Declaration (Spec : Iir)
   is
      use Vhdl.Back_End;
      Info : constant Subprg_Info_Acc := Get_Info (Spec);
      Is_Func : constant Boolean :=
        Get_Kind (Spec) = Iir_Kind_Function_Declaration;
      Is_Foreign : constant Boolean := Get_Foreign_Flag (Spec);
      Inter : Iir;
      Param_Info : Ortho_Info_Acc;
      Arg_Type : O_Tnode;
      Tinfo : Type_Info_Acc;
      Interface_List : O_Inter_List;
      Mark : Id_Mark_Type;
      Rtype : Iir;
      Id : O_Ident;
      Storage : O_Storage;
      Foreign : Foreign_Info_Type;
   begin
      --  Set the identifier prefix with the subprogram identifier and
      --  overload number if any.
      Push_Subprg_Identifier (Spec, Mark);

      --  Create the subprogram identifier.
      if Is_Foreign then
         --  Special handling for foreign subprograms.
         Foreign := Translate_Foreign_Id (Spec);
         case Foreign.Kind is
            when Foreign_Unknown =>
               Id := Create_Identifier;
            when Foreign_Intrinsic =>
               Id := Create_Identifier;
            when Foreign_Vhpidirect =>
               Id := Get_Identifier
                 (Foreign.Subprg_Name (1 .. Foreign.Subprg_Len));
         end case;
         Storage := O_Storage_External;
      else
         Foreign := Foreign_Bad;
         Id := Create_Identifier;
         Storage := Global_Storage;
      end if;

      if Is_Func then
         --  If the result of a function is a composite type for ortho,
         --  the result is allocated by the caller and an access to it is
         --  given to the function.
         Rtype := Get_Return_Type (Spec);
         Info.Use_Stack2 := False;
         Tinfo := Get_Info (Rtype);

         if Is_Composite (Tinfo) then
            Start_Procedure_Decl (Interface_List, Id, Storage);
            New_Interface_Decl
              (Interface_List, Info.Res_Interface,
               Get_Identifier ("RESULT"),
               Tinfo.Ortho_Ptr_Type (Mode_Value));
            --  Furthermore, if the result type is unconstrained, the
            --  function will allocate it on a secondary stack.
            if not Is_Fully_Constrained_Type (Rtype) then
               Info.Use_Stack2 := True;
            end if;
         else
            --  Normal function.
            Start_Function_Decl
              (Interface_List, Id, Storage, Tinfo.Ortho_Type (Mode_Value));
            Info.Res_Interface := O_Dnode_Null;
         end if;
      else
         Start_Procedure_Decl (Interface_List, Id, Storage);

         if Info.Subprg_Params_Type /= O_Tnode_Null then
            New_Interface_Decl (Interface_List, Info.Res_Interface,
                                Get_Identifier ("PARAMS"),
                                Info.Subprg_Params_Ptr);
         else
            Info.Res_Interface := O_Dnode_Null;
         end if;
      end if;

      --  Instance parameter if any.
      if not Is_Foreign then
         Subprgs.Create_Subprg_Instance (Interface_List, Spec);
      end if;

      --  Translate interfaces.
      if Is_Func or else Is_Foreign then
         Inter := Get_Interface_Declaration_Chain (Spec);
         while Inter /= Null_Iir loop
            --  Create the info.
            Param_Info := Add_Info (Inter, Kind_Interface);
            Translate_Interface_Mechanism (Inter);

            Arg_Type := Translate_Interface_Type (Inter, Mode_Value);
            New_Interface_Decl
              (Interface_List, Param_Info.Interface_Decl (Mode_Value),
               Create_Identifier_Without_Prefix (Inter), Arg_Type);

            if Get_Kind (Inter) = Iir_Kind_Interface_Signal_Declaration then
               Arg_Type := Translate_Interface_Type (Inter, Mode_Signal);
               New_Interface_Decl
                 (Interface_List, Param_Info.Interface_Decl (Mode_Signal),
                  Create_Identifier_Without_Prefix (Inter, "SIG"),
                  Arg_Type);
            end if;
            Inter := Get_Chain (Inter);
         end loop;
      end if;
      Finish_Subprogram_Decl (Interface_List, Info.Subprg_Node);

      --  Call the hook for foreign subprograms.
      if Is_Foreign and then Foreign_Hook /= null then
         Foreign_Hook.all (Spec, Foreign, Info.Subprg_Node);
      end if;

      Save_Local_Identifier (Info.Subprg_Local_Id);
      Pop_Identifier_Prefix (Mark);
   end Translate_Subprogram_Declaration;

   --  Return TRUE iff subprogram specification SPEC is translated in an
   --  ortho function.
   function Is_Subprogram_Ortho_Function (Spec : Iir) return Boolean
   is
   begin
      if Get_Kind (Spec) = Iir_Kind_Procedure_Declaration then
         return False;
      end if;
      if Get_Info (Spec).Res_Interface /= O_Dnode_Null then
         return False;
      end if;
      return True;
   end Is_Subprogram_Ortho_Function;

   --  Return TRUE iif SUBPRG_BODY declares explicitly or implicitely
   --  (or even implicitely by translation) a subprogram.
   function Has_Nested_Subprograms (Subprg_Body : Iir) return Boolean
   is
      Decl  : Iir;
      Atype : Iir;
   begin
      Decl := Get_Declaration_Chain (Subprg_Body);
      while Decl /= Null_Iir loop
         case Get_Kind (Decl) is
            when Iir_Kind_Function_Declaration
               | Iir_Kind_Procedure_Declaration =>
               return True;
            when Iir_Kind_Function_Body
               | Iir_Kind_Procedure_Body =>
               --  The declaration preceed the body.
               raise Internal_Error;
            when Iir_Kind_Type_Declaration
               | Iir_Kind_Anonymous_Type_Declaration =>
               Atype := Get_Type_Definition (Decl);
               case Iir_Kinds_Type_And_Subtype_Definition (Get_Kind (Atype)) is
                  when Iir_Kinds_Scalar_Type_And_Subtype_Definition =>
                     null;
                  when Iir_Kind_Access_Type_Definition
                     | Iir_Kind_Access_Subtype_Definition =>
                     null;
                  when Iir_Kind_File_Type_Definition =>
                     return True;
                  when Iir_Kind_File_Subtype_Definition =>
                     null;
                  when Iir_Kind_Protected_Type_Declaration =>
                     --  We suppose there is at least one method.
                     return True;
                  when Iir_Kinds_Composite_Type_Definition =>
                     --  At least for "=".
                     return True;
                  when Iir_Kind_Incomplete_Type_Definition
                    | Iir_Kind_Interface_Type_Definition =>
                     null;
               end case;
            when Iir_Kind_Package_Declaration
              | Iir_Kind_Package_Body =>
               if Has_Nested_Subprograms (Decl) then
                  return True;
               end if;
            when others =>
               null;
         end case;
         Decl := Get_Chain (Decl);
      end loop;
      return False;
   end Has_Nested_Subprograms;

   procedure Translate_Subprogram_Body (Subprg : Iir)
   is
      Spec : constant Iir := Get_Subprogram_Specification (Subprg);
      Info : constant Ortho_Info_Acc := Get_Info (Spec);

      --  True if the subprogram is suspendable (can be true only for
      --  procedures).
      Has_Suspend : constant Boolean :=
        Get_Kind (Spec) = Iir_Kind_Procedure_Declaration
        and then Get_Suspend_Flag (Spec);

      --  True if the subprogram is translated to a function in ortho.
      Is_Ortho_Func  : constant Boolean := Is_Subprogram_Ortho_Function (Spec);

      Old_Subprogram : Iir;
      Mark           : Id_Mark_Type;
      Final          : Boolean;

      --  Set for a public method.  In this case, the lock must be acquired
      --  and retained.
      Is_Prot : Boolean := False;

      --  True if the body has local (nested) subprograms.
      Has_Nested : Boolean;

      Frame_Ptr_Type : O_Tnode;
      Upframe_Field  : O_Fnode;
      Upframe_Scope  : Var_Scope_Acc;

      Frame     : O_Dnode;
      Frame_Ptr : O_Dnode;

      Has_Return : Boolean;

      Prev_Subprg_Instances : Subprgs.Subprg_Instance_Stack;
   begin
      --  Do not translate body for foreign subprograms.
      if Get_Foreign_Flag (Spec) then
         return;
      end if;

      --  Check if there are nested subprograms to unnest.  In that case,
      --  a frame record is created, which is less efficient than the
      --  use of local variables.
      if Flag_Unnest_Subprograms then
         Has_Nested := Has_Nested_Subprograms (Subprg);
      else
         Has_Nested := False;
      end if;

      --  Set the identifier prefix with the subprogram identifier and
      --  overload number if any.
      Push_Subprg_Identifier (Spec, Mark);
      Restore_Local_Identifier (Info.Subprg_Local_Id);

      if Has_Nested or else Has_Suspend then
         --  Unnest subprograms.
         --  Create an instance for the local declarations.
         Push_Frame_Factory (Info.Subprg_Frame_Scope'Access, Has_Suspend);
         Add_Subprg_Instance_Field (Upframe_Field, Upframe_Scope);

         if Info.Subprg_Params_Ptr /= O_Tnode_Null then
            --  Field for the parameters structure
            Info.Subprg_Params_Var :=
              Create_Var (Create_Var_Identifier ("PARAMS"),
                          Info.Subprg_Params_Ptr);
         else
            --  Create fields for parameters.
            --  FIXME: do it only if they are referenced in nested
            --  subprograms.
            declare
               Inter      : Iir;
               Inter_Type : O_Tnode;
               Inter_Info : Inter_Info_Acc;
            begin
               Inter := Get_Interface_Declaration_Chain (Spec);
               while Inter /= Null_Iir loop
                  Inter_Info := Get_Info (Inter);
                  if Inter_Info.Interface_Decl (Mode_Value) /= O_Dnode_Null
                  then
                     Inter_Type :=
                       Translate_Interface_Type (Inter, Mode_Value);
                     Inter_Info.Interface_Field (Mode_Value) :=
                       Add_Instance_Factory_Field
                       (Create_Identifier_Without_Prefix (Inter), Inter_Type);

                     if Get_Kind (Inter)
                       = Iir_Kind_Interface_Signal_Declaration
                     then
                        Inter_Type :=
                          Translate_Interface_Type (Inter, Mode_Signal);
                        Inter_Info.Interface_Field (Mode_Signal) :=
                          Add_Instance_Factory_Field
                          (Create_Identifier_Without_Prefix (Inter, "SIG"),
                           Inter_Type);
                     end if;
                  end if;
                  Inter := Get_Chain (Inter);
               end loop;
            end;
         end if;

         Chap4.Translate_Declaration_Chain (Subprg);

         if Has_Suspend then
            --  Add declarations for statements (iterator, call) and state.
            Chap4.Translate_Statements_Chain_State_Declaration
              (Get_Sequential_Statement_Chain (Subprg),
               Info.Subprg_Locvars_Scope'Access);
            Add_Scope_Field (Wki_Locvars, Info.Subprg_Locvars_Scope);
         end if;

         Pop_Frame_Factory (Info.Subprg_Frame_Scope'Access);

         New_Type_Decl (Create_Identifier ("_FRAMETYPE"),
                        Get_Scope_Type (Info.Subprg_Frame_Scope));
         Declare_Scope_Acc
           (Info.Subprg_Frame_Scope,
            Create_Identifier ("_FRAMEPTR"), Frame_Ptr_Type);

         Rtis.Generate_Subprogram_Body (Subprg);

         --  Local frame
         Subprgs.Push_Subprg_Instance
           (Info.Subprg_Frame_Scope'Access, Frame_Ptr_Type,
            Wki_Upframe, Prev_Subprg_Instances);
         --  Link to previous frame
         Subprgs.Start_Prev_Subprg_Instance_Use_Via_Field
           (Upframe_Scope, Upframe_Field);

         Chap4.Translate_Declaration_Chain_Subprograms
           (Subprg, Subprg_Translate_Spec_And_Body);

         --  Link to previous frame
         Subprgs.Finish_Prev_Subprg_Instance_Use_Via_Field
           (Upframe_Scope, Upframe_Field);
         --  Local frame
         Subprgs.Pop_Subprg_Instance (Wki_Upframe, Prev_Subprg_Instances);
      end if;

      --  Create the body.  Add a line very early, before any statement.

      Start_Subprogram_Body (Info.Subprg_Node);
      New_Debug_Line_Stmt (Get_Line_Number (Subprg));

      Start_Subprg_Instance_Use (Spec);

      --  Variables will be created on the stack.
      Push_Local_Factory;

      --  Code has access to local (and outer) variables.
      --  FIXME: this is not necessary if Has_Nested is set
      Subprgs.Clear_Subprg_Instance (Prev_Subprg_Instances);

      --  There is a local scope for temporaries.
      Open_Local_Temp;

      if not Has_Suspend and not Has_Nested then
         Chap4.Translate_Declaration_Chain (Subprg);
         Rtis.Generate_Subprogram_Body (Subprg);
         Chap4.Translate_Declaration_Chain_Subprograms
           (Subprg, Subprg_Translate_Spec_And_Body);
      else
         New_Var_Decl (Frame_Ptr, Get_Identifier ("FRAMEPTR"),
                       O_Storage_Local, Frame_Ptr_Type);

         if Has_Suspend then
            New_Assign_Stmt
              (New_Obj (Frame_Ptr),
               New_Convert_Ov (New_Value_Selected_Acc_Value
                                 (New_Obj (Info.Res_Interface),
                                  Info.Subprg_Locvars_Field),
                               Frame_Ptr_Type));

            Chap8.State_Entry (Info);

            --  Initial state: allocate frame.
            New_Assign_Stmt
              (New_Obj (Frame_Ptr),
               Gen_Alloc
                 (Alloc_Return,
                  New_Lit
                    (New_Sizeof (Get_Scope_Type (Info.Subprg_Frame_Scope),
                                 Ghdl_Index_Type)),
                  Frame_Ptr_Type));
            New_Assign_Stmt
              (New_Selected_Acc_Value (New_Obj (Info.Res_Interface),
                                       Info.Subprg_Locvars_Field),
               New_Convert_Ov (New_Obj_Value (Frame_Ptr),
                               Ghdl_Ptr_Type));

            --  Allocate the return state.  This IS NOT AN ASSERTION as the
            --  State_Allocate function has a side-effect.
            if Chap8.State_Allocate /= Chap8.State_Return then
               raise Internal_Error;
            end if;
         else
            --  Allocate the frame by declaring a local variable.
            New_Var_Decl (Frame, Wki_Frame, O_Storage_Local,
                          Get_Scope_Type (Info.Subprg_Frame_Scope));

            New_Assign_Stmt (New_Obj (Frame_Ptr),
                             New_Address (New_Obj (Frame), Frame_Ptr_Type));
         end if;

         --  FIXME: use direct reference (ie Frame instead of Frame_Ptr)
         Set_Scope_Via_Param_Ptr (Info.Subprg_Frame_Scope, Frame_Ptr);

         --  Set UPFRAME.
         Subprgs.Set_Subprg_Instance_Field
           (Frame_Ptr, Upframe_Field, Info.Subprg_Instance);

         if Info.Subprg_Params_Type /= O_Tnode_Null then
            --  Initialize the PARAMS field
            New_Assign_Stmt (Get_Var (Info.Subprg_Params_Var),
                             New_Obj_Value (Info.Res_Interface));
            --  Do not reference the RESULT field in the subprogram body,
            --  directly reference the RESULT parameter.
            --  FIXME: has a flag (see below for parameters).
            Info.Subprg_Params_Var := Null_Var;
         end if;

         --  Copy parameters to FRAME.
         if Info.Subprg_Params_Ptr = O_Tnode_Null then
            declare
               Inter      : Iir;
               Inter_Info : Inter_Info_Acc;
            begin
               Inter := Get_Interface_Declaration_Chain (Spec);
               while Inter /= Null_Iir loop
                  Inter_Info := Get_Info (Inter);
                  for Mode in Object_Kind_Type loop
                     if Inter_Info.Interface_Decl (Mode) /= O_Dnode_Null then
                        New_Assign_Stmt
                          (New_Selected_Element
                             (New_Obj (Frame),
                              Inter_Info.Interface_Field (Mode)),
                           New_Obj_Value (Inter_Info.Interface_Decl (Mode)));

                        --  Forget the reference to the field in FRAME, so that
                        --  this subprogram will directly reference the
                        --  parameter (and not its copy in the FRAME).
                        Inter_Info.Interface_Field (Mode) := O_Fnode_Null;
                     end if;
                  end loop;
                  Inter := Get_Chain (Inter);
               end loop;
            end;
         end if;
      end if;

      Is_Prot := Is_Subprogram_Method (Spec);
      if Is_Prot then
         --  Lock the object.
         Chap3.Call_Ghdl_Protected_Procedure (Get_Method_Type (Spec),
                                              Ghdl_Protected_Enter);
      end if;

      Chap4.Elab_Declaration_Chain (Subprg, Final);

      if not Has_Suspend then
         Stack2_Release;
      end if;
      --  If finalization is required and if the subprogram is a function,
      --  create a variable for the result.
      if (Final or Is_Prot) and Is_Ortho_Func then
         New_Var_Decl
           (Info.Subprg_Result, Get_Identifier ("RESULT"),
            O_Storage_Local,
            Get_Ortho_Type (Get_Return_Type (Spec), Mode_Value));
      end if;

      --  If finalization is required, create a dummy loop around the
      --  body and convert returns into exit out of this loop.
      if not Has_Suspend and then (Final or Is_Prot) then
         Start_Loop_Stmt (Info.Subprg_Exit);
      end if;

      Old_Subprogram := Current_Subprogram;
      Current_Subprogram := Spec;
      Has_Return := Chap8.Translate_Statements_Chain_Has_Return
        (Get_Sequential_Statement_Chain (Subprg));
      Current_Subprogram := Old_Subprogram;

      if Has_Suspend or Final or Is_Prot then
         --  Create a barrier to catch missing return statement.
         if Get_Kind (Spec) = Iir_Kind_Procedure_Declaration then
            if Has_Suspend then
               Chap8.State_Jump (Chap8.State_Return);
            else
               New_Exit_Stmt (Info.Subprg_Exit);
            end if;
         else
            if not Has_Return then
               --  Missing return
               Chap6.Gen_Program_Error
                 (Subprg, Chap6.Prg_Err_Missing_Return);
            end if;
         end if;
         if Has_Suspend then
            Chap8.State_Start (Chap8.State_Return);
         else
            Finish_Loop_Stmt (Info.Subprg_Exit);
         end if;
         Chap4.Final_Declaration_Chain (Subprg, False);

         if Is_Prot then
            --  Unlock the object.
            Chap3.Call_Ghdl_Protected_Procedure (Get_Method_Type (Spec),
                                                 Ghdl_Protected_Leave);
         end if;

         if Has_Suspend then
            Chap8.State_Suspend (Chap8.State_Return);
            Chap8.State_Leave (Spec);
         end if;

         if Is_Ortho_Func then
            New_Return_Stmt (New_Obj_Value (Info.Subprg_Result));
         end if;
      else
         if Get_Kind (Spec) = Iir_Kind_Function_Declaration
           and then not Has_Return
         then
            --  Missing return
            Chap6.Gen_Program_Error
              (Subprg, Chap6.Prg_Err_Missing_Return);
         end if;
      end if;

      if Has_Nested then
         Clear_Scope (Info.Subprg_Frame_Scope);
      end if;

      Subprgs.Pop_Subprg_Instance (O_Ident_Nul, Prev_Subprg_Instances);
      Close_Local_Temp;
      Pop_Local_Factory;

      Finish_Subprg_Instance_Use (Spec);

      Finish_Subprogram_Body;

      Pop_Identifier_Prefix (Mark);
   end Translate_Subprogram_Body;

   procedure Push_Package_Instance_Factory (Spec : Iir)
   is
      Info : constant Ortho_Info_Acc := Get_Info (Spec);
   begin
      Push_Instance_Factory (Info.Package_Body_Scope'Access);
      Info.Package_Spec_Field := Add_Instance_Factory_Field
        (Get_Identifier ("SPEC"),
         Get_Scope_Type (Info.Package_Spec_Scope));
   end Push_Package_Instance_Factory;

   procedure Pop_Package_Instance_Factory (Spec : Iir)
   is
      Info : constant Ortho_Info_Acc := Get_Info (Spec);
   begin
      Pop_Instance_Factory (Info.Package_Body_Scope'Access);
   end Pop_Package_Instance_Factory;

   --  Declare elaboration routines for a package.
   procedure Create_Package_Elaborator (Info : Ortho_Info_Acc)
   is
      Interface_List : O_Inter_List;
   begin
      --  Declare elaborator for the spec.
      Start_Procedure_Decl
        (Interface_List, Create_Identifier ("ELAB_SPEC"), Global_Storage);
      Subprgs.Add_Subprg_Instance_Interfaces
        (Interface_List, Info.Package_Elab_Spec_Instance);
      Finish_Subprogram_Decl
        (Interface_List, Info.Package_Elab_Spec_Subprg);

      --  Declare elaborator for the body.
      Start_Procedure_Decl
        (Interface_List, Create_Identifier ("ELAB_BODY"), Global_Storage);
      Subprgs.Add_Subprg_Instance_Interfaces
        (Interface_List, Info.Package_Elab_Body_Instance);
      Finish_Subprogram_Decl
        (Interface_List, Info.Package_Elab_Body_Subprg);
   end Create_Package_Elaborator;

   --  Translate a non-uninstantiated package declaration.
   --  HEADER is the node containing generic and generic_map.
   procedure Translate_Package_Concrete_Common (Decl : Iir; Header : Iir)
   is
      Info : Ortho_Info_Acc;
   begin
      Info := Add_Info (Decl, Kind_Package);

      if Header /= Null_Iir then
         Chap4.Translate_Generic_Association_Chain (Header);
      end if;

      Chap4.Translate_Declaration_Chain (Decl);

      Save_Local_Identifier (Info.Package_Local_Id);
   end Translate_Package_Concrete_Common;

   procedure Translate_Package_Concrete_Unit (Decl : Iir; Header : Iir)
   is
      Info : constant Ortho_Info_Acc := Get_Info (Decl);
   begin
      Info.Package_Elab_Var := Create_Var
        (Create_Var_Identifier ("ELABORATED"), Ghdl_Bool_Type);

      --  For nested package, this will be translated when translating
      --  subprograms.
      Chap4.Translate_Declaration_Chain_Subprograms_Spec_Body (Decl);

      Create_Package_Elaborator (Info);

      if Flag_Rti then
         --  Generate RTI.
         Rtis.Generate_Unit (Decl);
      end if;

      if Global_Storage /= O_Storage_External then
         --  Create elaboration procedure for the spec
         Elab_Package_Internal (Decl, Header);
      end if;

      --  Overwrite the value written by Translate_Package_Concrete_Common.
      Save_Local_Identifier (Info.Package_Local_Id);
   end Translate_Package_Concrete_Unit;

   --  Translate a package declaration or a macro-expanded package
   --  instantiation.  HEADER is the node containing generic and generic_map.
   procedure Translate_Package_Uninst_Common (Decl : Iir; Header : Iir)
   is
      Add_Body : constant Boolean :=
        not Get_Need_Body (Decl) and then Get_Package_Body (Decl) = Null_Iir;
      Info : Ortho_Info_Acc;
   begin
      Info := Add_Info (Decl, Kind_Package);

      --  Create an instance for the spec.
      Push_Instance_Factory (Info.Package_Spec_Scope'Access);
      Chap4.Translate_Generic_Chain (Header);
      Chap4.Translate_Declaration_Chain (Decl);
      if not Is_Nested_Package (Decl) then
         Info.Package_Elab_Var := Create_Var
           (Create_Var_Identifier ("ELABORATED"), Ghdl_Bool_Type);
      end if;
      Pop_Instance_Factory (Info.Package_Spec_Scope'Access);

      --  Name the spec instance and create a pointer.
      New_Type_Decl (Create_Identifier ("SPECINSTTYPE"),
                     Get_Scope_Type (Info.Package_Spec_Scope));
      Declare_Scope_Acc (Info.Package_Spec_Scope,
                         Create_Identifier ("SPECINSTPTR"),
                         Info.Package_Spec_Ptr_Type);

      --  Create an instance and its pointer for the body.
      Chap2.Declare_Inst_Type_And_Ptr
        (Info.Package_Body_Scope'Access, Info.Package_Body_Ptr_Type);

      if Add_Body then
         --  Generic package without a body.
         --  Create an empty body instance.
         Push_Package_Instance_Factory (Decl);
         Pop_Package_Instance_Factory (Decl);
      end if;
      Save_Local_Identifier (Info.Package_Local_Id);
   end Translate_Package_Uninst_Common;

   procedure Translate_Package_Uninst_Unit (Decl : Iir; Header : Iir)
   is
      Info : constant Ortho_Info_Acc := Get_Info (Decl);
      Add_Body : constant Boolean :=
        not Get_Need_Body (Decl) and then Get_Package_Body (Decl) = Null_Iir;
      Prev_Subprg_Instance : Subprgs.Subprg_Instance_Stack;
   begin
      --  Each subprogram has a body instance argument (because subprograms
      --  body can access body declarations).
      Subprgs.Push_Subprg_Instance
        (Info.Package_Body_Scope'Access, Info.Package_Body_Ptr_Type,
         Wki_Instance, Prev_Subprg_Instance);

      --  For nested package, this will be translated when translating
      --  subprograms.
      Chap4.Translate_Declaration_Chain_Subprograms
        (Decl, Subprg_Translate_Only_Spec);

      Create_Package_Elaborator (Info);

      Subprgs.Pop_Subprg_Instance (Wki_Instance, Prev_Subprg_Instance);

      if Flag_Rti then
         --  Generate RTI.
         Rtis.Generate_Unit (Decl);
      end if;

      if Add_Body and then Global_Storage /= O_Storage_External then
         --  For nested package, this will be translated when translating
         --  subprograms.
         Set_Scope_Via_Field (Info.Package_Spec_Scope,
                              Info.Package_Spec_Field,
                              Info.Package_Body_Scope'Access);

         Chap4.Translate_Declaration_Chain_Subprograms
           (Decl, Subprg_Translate_Only_Body);

         --  Create elaboration procedure for the spec
         Elab_Package_Internal (Decl, Header);

         Clear_Scope (Info.Package_Spec_Scope);
      end if;

      Save_Local_Identifier (Info.Package_Local_Id);
   end Translate_Package_Uninst_Unit;

   procedure Translate_Package_Declaration_Subprograms
     (Decl : Iir_Package_Declaration; What : Subprg_Translate_Kind)
   is
      Info : constant Ortho_Info_Acc := Get_Info (Decl);
      Is_Uninst : constant Boolean := Is_Uninstantiated_Package (Decl);
      Prev_Subprg_Instance : Subprgs.Subprg_Instance_Stack;
      Mark  : Id_Mark_Type;
   begin
      if Is_Uninst and then Get_Macro_Expanded_Flag (Decl) then
         --  Nothing to do for macro-expanded packages.
         return;
      end if;

      Push_Identifier_Prefix (Mark, Get_Identifier (Decl));

      if Is_Uninst then
         --  An extra parameter for the package instance needs to be added
         --  to the subprograms.
         Subprgs.Push_Subprg_Instance
           (Info.Package_Body_Scope'Access, Info.Package_Body_Ptr_Type,
            Wki_Instance, Prev_Subprg_Instance);
      end if;

      Chap4.Translate_Declaration_Chain_Subprograms (Decl, What);

      if Is_Uninst then
         if What in Subprg_Translate_Spec then
            --  Also declare elaborator subprograms.
            Create_Package_Elaborator (Info);
         end if;

         Subprgs.Pop_Subprg_Instance (Wki_Instance, Prev_Subprg_Instance);

         if What in Subprg_Translate_Body then
            --  For nested package, this will be translated when translating
            --  subprograms.
            Set_Scope_Via_Field (Info.Package_Spec_Scope,
                                 Info.Package_Spec_Field,
                                 Info.Package_Body_Scope'Access);

            Elab_Package_Internal (Decl, Get_Package_Header (Decl));

            Clear_Scope (Info.Package_Spec_Scope);

            if not Get_Need_Body (Decl)
              and then Get_Package_Body (Decl) = Null_Iir
            then
               Elab_Package_Body (Decl, Null_Iir);
            end if;
         end if;
      end if;

      Pop_Identifier_Prefix (Mark);
   end Translate_Package_Declaration_Subprograms;

   procedure Translate_Package_Body_Subprograms
     (Bod : Iir_Package_Body; What : Subprg_Translate_Kind)
   is
      Spec : constant Iir := Get_Package (Bod);
      Is_Uninst : constant Boolean := Is_Uninstantiated_Package (Spec);
      Info : constant Ortho_Info_Acc := Get_Info (Spec);
      Prev_Subprg_Instance : Subprgs.Subprg_Instance_Stack;
      Mark  : Id_Mark_Type;
   begin
      if Is_Uninst and then Get_Macro_Expanded_Flag (Spec) then
         --  Nothing to do for macro-expanded packages.
         return;
      end if;

      Push_Identifier_Prefix (Mark, Get_Identifier (Spec));

      if Is_Uninst then
         --  An extra parameter for the package instance needs to be added
         --  to the subprograms.
         Subprgs.Push_Subprg_Instance
           (Info.Package_Body_Scope'Access, Info.Package_Body_Ptr_Type,
            Wki_Instance, Prev_Subprg_Instance);

         --  For nested package, this will be translated when translating
         --  subprograms.
         Set_Scope_Via_Field (Info.Package_Spec_Scope,
                              Info.Package_Spec_Field,
                              Info.Package_Body_Scope'Access);
      end if;

      Chap4.Translate_Declaration_Chain_Subprograms (Bod, What);

      if Is_Uninst then
         Clear_Scope (Info.Package_Spec_Scope);
         Subprgs.Pop_Subprg_Instance (Wki_Instance, Prev_Subprg_Instance);

         if What in Subprg_Translate_Body then
            Elab_Package_Body (Spec, Bod);
         end if;
      end if;

      Pop_Identifier_Prefix (Mark);
   end Translate_Package_Body_Subprograms;

   procedure Translate_Package_Declaration (Decl : Iir_Package_Declaration)
   is
      Mark : Id_Mark_Type;
   begin
      --  Skip uninstantiated package that have to be macro-expanded.
      if Get_Macro_Expanded_Flag (Decl) then
         return;
      end if;

      Push_Identifier_Prefix (Mark, Get_Identifier (Decl));

      if Is_Uninstantiated_Package (Decl) then
         Translate_Package_Uninst_Common (Decl, Get_Package_Header (Decl));
      else
         Translate_Package_Concrete_Common (Decl, Get_Package_Header (Decl));
      end if;

      Pop_Identifier_Prefix (Mark);
   end Translate_Package_Declaration;

   procedure Translate_Package_Declaration_Unit
     (Decl : Iir_Package_Declaration)
   is
      Header : Iir;
   begin
      --  Skip uninstantiated package that have to be macro-expanded.
      if Get_Macro_Expanded_Flag (Decl) then
         return;
      end if;

      Header := Get_Package_Header (Decl);
      if Is_Uninstantiated_Package (Decl) then
         Translate_Package_Uninst_Common (Decl, Header);
         Translate_Package_Uninst_Unit (Decl, Header);
      else
         Translate_Package_Concrete_Common (Decl, Header);
         Translate_Package_Concrete_Unit (Decl, Header);
      end if;
   end Translate_Package_Declaration_Unit;

   procedure Translate_Package_Body_Internal (Bod : Iir_Package_Body)
   is
      Is_Nested : constant Boolean := Is_Nested_Package (Bod);
      Spec      : constant Iir_Package_Declaration := Get_Package (Bod);

      --  True if the package spec is a package declaration.  It could be a
      --  package instantiation declaration.
      Is_Spec_Decl : constant Boolean :=
        Get_Kind (Spec) = Iir_Kind_Package_Declaration;

      Info      : constant Ortho_Info_Acc := Get_Info (Spec);
      Prev_Storage : constant O_Storage := Global_Storage;
      Prev_Subprg_Instance : Subprgs.Subprg_Instance_Stack;
   begin
      if Is_Spec_Decl and then Get_Macro_Expanded_Flag (Spec) then
         return;
      end if;

      --  Translate declarations.
      if Is_Spec_Decl and then Is_Uninstantiated_Package (Spec) then
         Push_Package_Instance_Factory (Spec);

         --  Translate the specifications.
         Chap4.Translate_Declaration_Chain (Bod);

         Pop_Package_Instance_Factory (Spec);
      else
         Restore_Local_Identifier (Info.Package_Local_Id);

         Chap4.Translate_Declaration_Chain (Bod);
      end if;

      --  May be called during elaboration to generate RTI.
      if Global_Storage = O_Storage_External then
         return;
      end if;

      Global_Storage := O_Storage_Private;

      --  Generate RTI, but not for nested packages (RTI will be generated as
      --  a declaration by the parent).
      if not Is_Nested and then Flag_Rti then
         Rtis.Generate_Unit (Bod);
      end if;

      if Is_Spec_Decl and then Is_Uninstantiated_Package (Spec) then
         --  Add access to the specs.
         Subprgs.Push_Subprg_Instance
           (Info.Package_Body_Scope'Access, Info.Package_Body_Ptr_Type,
            Wki_Instance, Prev_Subprg_Instance);
         Set_Scope_Via_Field (Info.Package_Spec_Scope,
                              Info.Package_Spec_Field,
                              Info.Package_Body_Scope'Access);
      end if;

      if not Is_Nested then
         --  Translate subprograms.  For nested package, this has to be called
         --  when translating subprograms.
         Chap4.Translate_Declaration_Chain_Subprograms
           (Bod, Subprg_Translate_Spec_And_Body);
      end if;

      if Is_Spec_Decl and then Is_Uninstantiated_Package (Spec) then
         Subprgs.Pop_Subprg_Instance (Wki_Instance, Prev_Subprg_Instance);
         if not Is_Nested then
            Chap4.Translate_Declaration_Chain_Subprograms
              (Spec, Subprg_Translate_Only_Body);
            Elab_Package_Internal (Spec, Get_Package_Header (Spec));
         end if;
         Clear_Scope (Info.Package_Spec_Scope);
      end if;

      if not Is_Nested and Flag_Elaboration then
         Elab_Package_Body (Spec, Bod);
      end if;

      Global_Storage := Prev_Storage;
   end Translate_Package_Body_Internal;

   --  For a nested package body or for a nested package instantiation body.
   procedure Translate_Package_Body (Bod : Iir_Package_Body)
   is
      Spec : constant Iir_Package_Declaration := Get_Package (Bod);
      Mark : Id_Mark_Type;
   begin
      Push_Identifier_Prefix (Mark, Get_Identifier (Spec));

      Translate_Package_Body_Internal (Bod);

      Pop_Identifier_Prefix (Mark);
   end Translate_Package_Body;

   procedure Translate_Package_Body_Unit (Bod : Iir_Package_Body) is
   begin
      if not Flag_Elaboration then
         return;
      end if;

      Translate_Package_Body_Internal (Bod);
   end Translate_Package_Body_Unit;

   --  Elaborate a package or a package instantiation.
   procedure Elab_Package_Internal (Spec : Iir; Header : Iir)
   is
      Is_Nested : constant Boolean := Is_Nested_Package (Spec);
      Is_Uninst : constant Boolean :=
        Get_Kind (Spec) = Iir_Kind_Package_Declaration
        and then Is_Uninstantiated_Package (Spec);
      Info   : constant Ortho_Info_Acc := Get_Info (Spec);
      Final  : Boolean;
      Constr : O_Assoc_List;
   begin
      if not Flag_Elaboration and not Is_Nested then
         return;
      end if;

      if (not Is_Nested) or else Is_Uninst then
         Start_Subprogram_Body (Info.Package_Elab_Spec_Subprg);
         Push_Local_Factory;
         Subprgs.Start_Subprg_Instance_Use (Info.Package_Elab_Spec_Instance);

         if not Is_Nested then
            Elab_Dependence (Get_Design_Unit (Spec));
         end if;

         if not Is_Uninst then
            --  Register the top level package.  This is done dynamically, as
            --  we know only during elaboration that the design depends on a
            --  package (a package maybe referenced by an entity which is never
            --  instantiated due to generate statements).
            Start_Association (Constr, Ghdl_Rti_Add_Package);
            New_Association
              (Constr, Rtis.New_Rti_Address (Info.Package_Rti_Const));
            New_Procedure_Call (Constr);
         end if;

         Open_Temp;
      end if;

      if Is_Valid (Header)
        and then Is_Valid (Get_Generic_Map_Aspect_Chain (Header))
      then
         Chap5.Elab_Generic_Map_Aspect
           (Header, Header,
            (Info.Package_Spec_Scope'Access, Info.Package_Spec_Scope));
      end if;
      Chap4.Elab_Declaration_Chain (Spec, Final);
      pragma Unreferenced (Final);

      if (not Is_Nested) or else Is_Uninst then
         Close_Temp;

         Subprgs.Finish_Subprg_Instance_Use (Info.Package_Elab_Spec_Instance);
         Pop_Local_Factory;
         Finish_Subprogram_Body;
      end if;
   end Elab_Package_Internal;

   procedure Elab_Package_Declaration (Spec : Iir) is
   begin
      Elab_Package_Internal (Spec, Get_Package_Header (Spec));
   end Elab_Package_Declaration;

   procedure Elab_Package_Body (Spec : Iir_Package_Declaration; Bod : Iir)
   is
      --  SPEC can be a package declaration or a package instantiation.
      Is_Spec_Decl : constant Boolean :=
        Get_Kind (Spec) = Iir_Kind_Package_Declaration;
      Is_Uninst : constant Boolean :=
        Is_Spec_Decl and then Is_Uninstantiated_Package (Spec);

      Info   : constant Ortho_Info_Acc := Get_Info (Spec);
      If_Blk : O_If_Block;
      Constr : O_Assoc_List;
      Final  : Boolean;
   begin
      --  Macro-expanded packages are skipped.
      pragma Assert
        (not (Is_Spec_Decl and then Get_Macro_Expanded_Flag (Spec)));

      --  No elaboration code generated, except for nested packages
      --  (could be within a subprogram).
      pragma Assert (Flag_Elaboration or else Is_Nested_Package (Spec));

      if Is_Uninst then
         --  Make spec reachable.
         Set_Scope_Via_Field (Info.Package_Spec_Scope,
                              Info.Package_Spec_Field,
                              Info.Package_Body_Scope'Access);
      end if;

      Start_Subprogram_Body (Info.Package_Elab_Body_Subprg);
      Push_Local_Factory;
      Subprgs.Start_Subprg_Instance_Use (Info.Package_Elab_Body_Instance);

      --  If the package was already elaborated, return now,
      --  else mark the package as elaborated.
      if Info.Package_Elab_Var /= Null_Var then
         Start_If_Stmt (If_Blk, New_Value (Get_Var (Info.Package_Elab_Var)));
         New_Return_Stmt;
         New_Else_Stmt (If_Blk);
         New_Assign_Stmt (Get_Var (Info.Package_Elab_Var),
                          New_Lit (Ghdl_Bool_True_Node));
         Finish_If_Stmt (If_Blk);
      end if;

      --  Elab Spec.
      Start_Association (Constr, Info.Package_Elab_Spec_Subprg);
      Add_Subprg_Instance_Assoc (Constr, Info.Package_Elab_Spec_Instance);
      New_Procedure_Call (Constr);

      if Bod /= Null_Iir then
         if not Is_Nested_Package (Bod) then
            Elab_Dependence (Get_Design_Unit (Bod));
         end if;

         Open_Temp;
         Chap4.Elab_Declaration_Chain (Bod, Final);
         Close_Temp;
      end if;

      Subprgs.Finish_Subprg_Instance_Use (Info.Package_Elab_Body_Instance);
      Pop_Local_Factory;
      Finish_Subprogram_Body;

      if Is_Uninst then
         Clear_Scope (Info.Package_Spec_Scope);
      end if;
   end Elab_Package_Body;

   procedure Elab_Package_Unit_Without_Body (Spec : Iir) is
   begin
      if Get_Macro_Expanded_Flag (Spec) then
         return;
      end if;

      Elab_Package_Body (Spec, Null_Iir);
   end Elab_Package_Unit_Without_Body;

   procedure Instantiate_Iir_Info (N : Iir);

   procedure Instantiate_Iir_Chain_Info (Chain : Iir)
   is
      N : Iir;
   begin
      N := Chain;
      while N /= Null_Iir loop
         Instantiate_Iir_Info (N);
         N := Get_Chain (N);
      end loop;
   end Instantiate_Iir_Chain_Info;

   procedure Instantiate_Iir_List_Info (L : Iir_List)
   is
      It : List_Iterator;
   begin
      case L is
         when Null_Iir_List
            | Iir_List_All =>
            return;
         when others =>
            It := List_Iterate (L);
            while Is_Valid (It) loop
               Instantiate_Iir_Info (Get_Element (It));
               Next (It);
            end loop;
      end case;
   end Instantiate_Iir_List_Info;

   procedure Instantiate_Iir_Flist_Info (L : Iir_Flist)