Merge remote-tracking branch 'origin/xdccore-H-sasha' into master_ggentree
diff --git a/src/packages/xdc/services/intern/gen/Config.java b/src/packages/xdc/services/intern/gen/Config.java
index 637edc7..b436482 100644
--- a/src/packages/xdc/services/intern/gen/Config.java
+++ b/src/packages/xdc/services/intern/gen/Config.java
@@ -184,7 +184,7 @@
                 continue;
             }
             /* If a function pointer table is disabled in a module, we don't
-             * need to be able to follow its inheritance chain. A longer 
+             * need to be able to follow its inheritance chain. A longer
              * description of the purpose of 'fxntab' is in Types.xdc.
              */
             if (commonGeti(m, "fxntab") != 1) {
@@ -233,7 +233,7 @@
                 genInternals(m);
                 /* modset keeps track of the modules whose typedefs and externs
                  * for instances are already generated so as to not generate
-                 * them again in the same file. We clear it between files
+                 * them again in the same file. We clear it between the files
                  * because each of the files must be selfsufficient.
                  */
                 modset.clear();
@@ -242,8 +242,8 @@
         fm.switchCommon();
 
         if (isRom) {
-            /* The constants and the module states that are kept in two large
-             * structures are read from Program.$$rom.romStruct.
+            /* The constants and the module states that are kept in the two
+             * large structures are read from Program.$$rom.romStruct.
              */
             Scriptable romobj = (Scriptable)(prog.find("$$rom"));
             NativeArray members =
@@ -262,7 +262,7 @@
 
         /* We also need to generate internals for the common C file, and all
          * modules need to contribute to that one, even the modules with the
-         * separate C files. Their types and definitions can be used in the
+         * separate C files. Their types and definitions might be used in the
          * common C file.
          */
         for (Value.Obj m : modvec) {
@@ -450,7 +450,8 @@
                     genPreds((Value.Obj)oval, ps, xs + '_' + i, isConst);
                 }
                 else if (oval instanceof Value.Arr) {
-                    genArr((Value.Arr)oval, cname, an, xs + '_' + i, ps, isConst);
+                    genArr((Value.Arr)oval, cname, an, xs + '_' + i, ps,
+                        isConst);
                 }
                 else if (oval instanceof Extern) {
                     genExt((Extern)oval);
@@ -596,12 +597,14 @@
         String cs = "xdc_runtime_Core_";
 
         glob.genTitleD("Object__DESC__C");
-        glob.out.printf("%ttypedef struct { %1Object2__ s0; char c; } %1__S1;\n", glob.cname);
+        glob.out.printf(
+            "%ttypedef struct { %1Object2__ s0; char c; } %1__S1;\n",
+            glob.cname);
 
         /* If this const is to be a part of a large structure referenced
          * by ROM, its global name will not exist. However, ROM builds can
          * include code that references such consts, so we define global names
-         * in the linker command files as offsets within ROM structures.
+         * in the linker command files as offsets within the ROM structures.
          * We need to record the type and value for this consts to be able to
          * create a type definition, and to lay down the initializer for the
          * structure.
@@ -618,7 +621,9 @@
         /* now the value for Object__DESC__C is generated */
         if (unit.isHeir()) {
             if (commonGeti(mod, "fxntab") == 1) {
-                glob.out.printf("%t(xdc_CPtr)&%1Module__FXNS__C, /* fxnTab */\n", glob.cname);
+                glob.out.printf(
+                    "%t(xdc_CPtr)&%1Module__FXNS__C, /* fxnTab */\n",
+                    glob.cname);
             }
             else {
                 glob.out.printf("%t(xdc_CPtr)NULL, /* fxnTab */\n");
@@ -633,7 +638,8 @@
          * not exist, or if it does, it contains only the diagnostic mask.
          */
         if (commonGeti(mod, "memoryPolicy") != STATIC_POLICY) {
-            glob.out.printf("%t&%1Module__root__V.link, /* modLink */\n", glob.cname);
+            glob.out.printf("%t&%1Module__root__V.link, /* modLink */\n",
+                glob.cname);
         }
         else {
             glob.out.printf("%t(xdc_Ptr)NULL, /* modLink */\n");
@@ -2493,14 +2499,22 @@
         glob.out.printf("#ifdef __ti__sect\n");
         glob.out.printf("#pragma DATA_SECTION(ROM_stateStruct,"
             + " \".data:ROM_stateStruct\");\n");
-        glob.out.printf("#else\n");
-        glob.out.printf("#ifdef __GNUC__\n");
+        /* section placement for GCC except MacOS (see genArr comments) */
+        glob.out.printf("#elif defined(__GNUC__) && !(defined(__MACH__) && defined(__APPLE__))%+\n");
+        glob.out.printf("%t#ifndef __ti__\n");
+        /* this is for non-TI gcc compilers */
         glob.out.printf("__FAR__ TROM__stateStruct ROM_stateStruct "
             + "__attribute__ ((section(\".data_ROM_stateStruct\")));\n");
-        glob.out.printf("#endif\n");
+        glob.out.printf("%t#elif defined(__clang__)\n");
+        /* this is for the TI clang compiler */
+        glob.out.printf("__FAR__ TROM__stateStruct ROM_stateStruct "
+            + "__attribute__ ((section(\".data:ROM_stateStruct\")));\n");
+        glob.out.printf("%t#endif\n%-#endif\n");
+
         glob.out.printf("#if defined (__IAR_SYSTEMS_ICC__)\n");
         glob.out.printf("#pragma location = \".data_ROM_stateStruct\"\n");
-        glob.out.printf("#endif\n#endif\n");
+        glob.out.printf("#endif\n");
+
         glob.out.printf("__FAR__ TROM__stateStruct ROM_stateStruct = {%+\n");
         for (String state: romModStates.keySet()) {
             String value = romModStates.get(state).get(1);
@@ -2516,14 +2530,19 @@
         glob.out.printf("#ifdef __ti__sect\n");
         glob.out.printf("#pragma DATA_SECTION(xdcRomConstPtr,"
             + " \".data:xdcRomConstPtr\");\n");
-        glob.out.printf("#else\n");
-        glob.out.printf("#ifdef __GNUC__\n");
+        glob.out.printf("#elif defined(__GNUC__) && !(defined(__MACH__) && defined(__APPLE__))%+\n");
+        glob.out.printf("%t#ifndef __ti__\n");
         glob.out.printf("const xdc_UInt8 * xdcRomConstPtr __attribute__"
             + " ((section(\".data_xdcRomConstPtr\")));\n");
-        glob.out.printf("#endif\n");
+        glob.out.printf("%t#elif defined(__clang__)\n");
+        glob.out.printf("const xdc_UInt8 * xdcRomConstPtr __attribute__"
+            + " ((section(\".data:xdcRomConstPtr\")));\n");
+        glob.out.printf("%t#endif\n%-#endif\n");
+
         glob.out.printf("#if defined (__IAR_SYSTEMS_ICC__)\n");
         glob.out.printf("#pragma location = \".data_xdcRomConstPtr\"\n");
-        glob.out.printf("#endif\n#endif\n");
+        glob.out.printf("#endif\n");
+
         glob.out.printf("const xdc_UInt8 * xdcRomConstPtr"
             + " = (xdc_UInt8 *)&ROM_constStruct;\n");
 
@@ -2531,14 +2550,18 @@
         glob.out.printf("#ifdef __ti__sect\n");
         glob.out.printf("#pragma DATA_SECTION(xdcRomStatePtr,"
             + " \".data:xdcRomStatePtr\");\n");
-        glob.out.printf("#else\n");
-        glob.out.printf("#ifdef __GNUC__\n");
+        glob.out.printf("#elif defined(__GNUC__) && !(defined(__MACH__) && defined(__APPLE__))%+\n");
+        glob.out.printf("%t#ifndef __ti__\n");
         glob.out.printf("xdc_UInt8 * xdcRomStatePtr __attribute__"
             + " ((section(\".data_xdcRomStatePtr\")));\n");
-        glob.out.printf("#endif\n");
+        glob.out.printf("%t#elif defined(__clang__)\n");
+        glob.out.printf("xdc_UInt8 * xdcRomStatePtr __attribute__"
+            + " ((section(\".data:xdcRomStatePtr\")));\n");
+        glob.out.printf("%t#endif\n%-#endif\n");
+
         glob.out.printf("#if defined (__IAR_SYSTEMS_ICC__)\n");
         glob.out.printf("#pragma location = \".data_xdcRomStatePtr\"\n");
-        glob.out.printf("#endif\n#endif\n");
+        glob.out.printf("#endif\n");
         glob.out.printf("xdc_UInt8 * xdcRomStatePtr = (xdc_UInt8 *)"
             + "&ROM_stateStruct;\n");
     }
@@ -2944,16 +2967,17 @@
             glob.out.printf("%treturn 1;\n");
         }
         else {
-            /* When generating startupDone__S function for modules that require
-             * a separate config C file, we have to create a complete body of
-             * the function here. The normal flow would be to generate a call to
-             * startupDone__F, and then Startup template would generate
-             * startupDone__F. However, we can't extract only startupDone__F
-             * function for one module from the Startup template's output and
-             * redirect it somewhere else. Therefore, the Startup template is
-             * changed to not generate at all startupDone__F functions for
-             * modules that require separate files. What it does is to create
-             * a target function that maps module IDs to stateTab indices, and
+            /* When generating startupDone__S function for the modules that
+             * require a separate config C file, we have to create a complete
+             * body of the function here. The normal flow would be to generate a
+             * call to startupDone__F, and then the Startup template would
+             * generate startupDone__F. However, we can't extract only
+             * startupDone__F function for one module from the Startup
+             * template's output and redirect it to a different C file.
+             * Therefore, the Startup template is changed as to not generate at
+             * all the startupDone__F functions for the modules that require
+             * separate files. What it does instead is to create a target
+             * function that maps module IDs to stateTab indices, and
              * that's the function invoked below.
              */
             if (fm.switchMod(mod) == FileManager.SEPARATE_FILE) {