rxgen: emit opcode defines in header
[openafs.git] / src / rxgen / rpc_parse.c
index 0d87c5f..3eff6a3 100644 (file)
@@ -68,6 +68,7 @@ int combinepackages = 0;
 int PackageIndex = -1;
 int PerProcCounter = 0;
 int Multi_Init = 0;
+int StatIndex = -1;
 
 /*
  * Character arrays to keep list of function names as we process the file
@@ -131,12 +132,12 @@ static void ucs_ProcParams_setup(definition * defp, int split_flag);
 static void ucs_ProcTail_setup(definition * defp, int split_flag);
 static void ss_Proc_CodeGeneration(definition * defp);
 static void ss_ProcName_setup(definition * defp);
-static void ss_ProcParams_setup(definition * defp, int *somefrees);
-static void ss_ProcSpecial_setup(definition * defp, int *somefrees);
+static void ss_ProcParams_setup(definition * defp);
+static void ss_ProcSpecial_setup(definition * defp);
 static void ss_ProcUnmarshallInParams_setup(definition * defp);
 static void ss_ProcCallRealProc_setup(definition * defp);
 static void ss_ProcMarshallOutParams_setup(definition * defp);
-static void ss_ProcTail_setup(definition * defp, int somefrees);
+static void ss_ProcTail_setup(definition * defp);
 static int opcode_holes_exist(void);
 static void er_ProcDeclExterns_setup(void);
 static void er_ProcProcsArray_setup(void);
@@ -622,6 +623,7 @@ def_statindex(definition * defp)
     defp->def_name = name;
     defp->def.co = tok.str;
     PackageStatIndex[PackageIndex] = name;
+    StatIndex = atoi(tok.str);
 }
 
 static void
@@ -929,7 +931,7 @@ generate_code(definition * defp, int proc_split_flag, int multi_flag)
        if (Sflag || cflag)
            ss_Proc_CodeGeneration(defp);
     }
-    if (Sflag || (cflag && xflag && !proc_split_flag))
+    if (Sflag || (cflag && xflag && !proc_split_flag) || hflag)
        STOREVAL(&proc_defined[PackageIndex], defp);
 }
 
@@ -1147,16 +1149,21 @@ cs_ProcName_setup(definition * defp, char *procheader, int split_flag)
     if (!cflag) {
        for (plist = defp->pc.plists; plist; plist = plist->next) {
            if (plist->component_kind == DEF_PARAM) {
+               f_print(fout, ",");
                if (ansic_flag) {
+                   if (plist->pl.param_kind == DEF_INPARAM &&
+                       strcmp(plist->pl.param_type, "char *") == 0) {
+                       f_print(fout, "const ");
+                   }
                    if (plist->pl.param_flag & OUT_STRING) {
-                       f_print(fout, ",%s *%s", plist->pl.param_type,
+                       f_print(fout, "%s *%s", plist->pl.param_type,
                                plist->pl.param_name);
                    } else {
-                       f_print(fout, ",%s %s", plist->pl.param_type,
+                       f_print(fout, "%s %s", plist->pl.param_type,
                                plist->pl.param_name);
                    }
                } else {
-                   f_print(fout, ", %s", plist->pl.param_name);
+                   f_print(fout, " %s", plist->pl.param_name);
                    plist->pl.param_flag &= ~PROCESSED_PARAM;
                }
            }
@@ -1343,17 +1350,15 @@ cs_ProcTail_setup(definition * defp, int split_flag)
 static void
 ss_Proc_CodeGeneration(definition * defp)
 {
-    int somefrees = 0;
-
     defp->can_fail = 0;
     ss_ProcName_setup(defp);
     if (!cflag) {
-       ss_ProcParams_setup(defp, &somefrees);
-       ss_ProcSpecial_setup(defp, &somefrees);
+       ss_ProcParams_setup(defp);
+       ss_ProcSpecial_setup(defp);
        ss_ProcUnmarshallInParams_setup(defp);
        ss_ProcCallRealProc_setup(defp);
        ss_ProcMarshallOutParams_setup(defp);
-       ss_ProcTail_setup(defp, somefrees);
+       ss_ProcTail_setup(defp);
     }
 }
 
@@ -1384,7 +1389,7 @@ ss_ProcName_setup(definition * defp)
 
 
 static void
-ss_ProcParams_setup(definition * defp, int *somefrees)
+ss_ProcParams_setup(definition * defp)
 {
     proc1_list *plist, *plist1;
     list *listp;
@@ -1408,7 +1413,6 @@ ss_ProcParams_setup(definition * defp, int *somefrees)
                        plist->pl.param_name);
            } else {
                plist->pl.param_flag |= FREETHIS_PARAM;
-               *somefrees = 1;
                f_print(fout, "\t%s %s=(%s)0", plist->pl.param_type,
                        plist->pl.param_name, plist->pl.param_type);
            }
@@ -1423,7 +1427,6 @@ ss_ProcParams_setup(definition * defp, int *somefrees)
                        f_print(fout, ", %s", plist1->pl.param_name);
                    } else {
                        plist1->pl.param_flag |= FREETHIS_PARAM;
-                       *somefrees = 1;
                        f_print(fout, ", *%s=(%s)0", plist1->pl.param_name,
                                plist1->pl.param_type);
                    }
@@ -1456,7 +1459,7 @@ ss_ProcParams_setup(definition * defp, int *somefrees)
 
 
 static void
-ss_ProcSpecial_setup(definition * defp, int *somefrees)
+ss_ProcSpecial_setup(definition * defp)
 {
     proc1_list *plist;
     definition *defp1;
@@ -1475,22 +1478,18 @@ ss_ProcSpecial_setup(definition * defp, int *somefrees)
                if (streq(string, structname(plist->pl.param_type))) {
                    plist->pl.string_name = spec->sdef.string_name;
                    plist->pl.param_flag |= FREETHIS_PARAM;
-                   *somefrees = 1;
                    fprintf(fout, "\n\t%s.%s = 0;", plist->pl.param_name,
                            spec->sdef.string_name);
                }
            }
        }
     }
-    if (!*somefrees)
-       fprintf(fout, "\n");
     for (listp = typedef_defined; listp != NULL; listp = listp->next) {
        defp1 = (definition *) listp->val;
        for (plist = defp->pc.plists; plist; plist = plist->next) {
            if (plist->component_kind == DEF_PARAM) {
                if (streq(defp1->def_name, structname(plist->pl.param_type))) {
                    plist->pl.param_flag |= FREETHIS_PARAM;
-                   *somefrees = 1;
                    switch (defp1->pc.rel) {
                    case REL_ARRAY:
                        plist->pl.string_name = alloc(40);
@@ -1526,7 +1525,6 @@ ss_ProcSpecial_setup(definition * defp, int *somefrees)
            if (plist->component_kind == DEF_PARAM) {
                if (streq(defp1->def_name, structname(plist->pl.param_type))) {
                    plist->pl.param_flag |= FREETHIS_PARAM;
-                   *somefrees = 1;
                    fprintf(fout, "\n\tmemset(&%s, 0, sizeof(%s));",
                                 plist->pl.param_name, defp1->def_name);
                }
@@ -1629,30 +1627,36 @@ ss_ProcMarshallOutParams_setup(definition * defp)
     }
 }
 
+static void
+ss_ProcTail_frees(char *xdrfunc, int *somefrees) {
+    if (!*somefrees) {
+       f_print(fout, "\tz_xdrs->x_op = XDR_FREE;\n");
+        f_print(fout, "\tif ((!%s)", xdrfunc);
+       *somefrees = 1;
+    } else {
+       f_print(fout, "\n\t    || (!%s)", xdrfunc);
+    }
+}
+
 
 static void
-ss_ProcTail_setup(definition * defp, int somefrees)
+ss_ProcTail_setup(definition * defp)
 {
     proc1_list *plist;
     definition *defp1;
     list *listp;
-    int firsttime = 0;
+    int somefrees = 0;
 
     if (defp->can_fail) {
        f_print(fout, "fail:\n");
     }
-    for (plist = defp->pc.plists; plist; plist = plist->next) {
-       if (plist->component_kind == DEF_PARAM
-           && (plist->pl.param_flag & FREETHIS_PARAM))
-           somefrees = 1;
-    }
-    if (somefrees)
-       f_print(fout, "\tz_xdrs->x_op = XDR_FREE;\n");
+
     for (plist = defp->pc.plists; plist; plist = plist->next) {
        if (plist->component_kind == DEF_PARAM
                && (plist->pl.param_flag & FREETHIS_PARAM))
-           f_print(fout, "\tif (!%s) goto fail1;\n", plist->scode);
+           ss_ProcTail_frees(plist->scode, &somefrees);
     }
+
     for (listp = typedef_defined; listp != NULL; listp = listp->next) {
        defp1 = (definition *) listp->val;
        for (plist = defp->pc.plists; plist; plist = plist->next) {
@@ -1664,13 +1668,7 @@ ss_ProcTail_setup(definition * defp, int somefrees)
                    switch (defp1->pc.rel) {
                    case REL_ARRAY:
                    case REL_POINTER:
-                       if (!somefrees && !firsttime) {
-                           firsttime = 1;
-                           f_print(fout, "\tz_xdrs->x_op = XDR_FREE;\n");
-                       }
-                       somefrees = 1;
-                       f_print(fout, "\tif (!%s) goto fail1;\n",
-                               plist->scode);
+                       ss_ProcTail_frees(plist->scode, &somefrees);
                        break;
                    default:
                        break;
@@ -1679,6 +1677,7 @@ ss_ProcTail_setup(definition * defp, int somefrees)
            }
        }
     }
+
     for (listp = uniondef_defined; listp != NULL; listp = listp->next) {
        defp1 = (definition *) listp->val;
        for (plist = defp->pc.plists; plist; plist = plist->next) {
@@ -1688,19 +1687,18 @@ ss_ProcTail_setup(definition * defp, int somefrees)
                && !(plist->pl.param_flag & FREETHIS_PARAM)) {
                if (streq(defp1->def_name, structname(plist->pl.param_type))) {
                    if (plist->pl.param_flag & INDIRECT_PARAM) {
-                       if (!somefrees && !firsttime) {
-                           firsttime = 1;
-                           f_print(fout, "\tz_xdrs->x_op = XDR_FREE;\n");
-                       }
-                       somefrees = 1;
-                       f_print(fout, "\tif (!%s) goto fail1;\n",
-                               plist->scode);
+                       ss_ProcTail_frees(plist->scode, &somefrees);
                    }
                }
            }
        }
     }
 
+    if (somefrees) {
+       f_print(fout, ")\n");
+       f_print(fout, "\t\tz_result = RXGEN_SS_XDRFREE;\n\n");
+    }
+
     if (xflag) {
        f_print(fout, "\tif (rx_enable_stats) {\n");
        f_print(fout, "\t    rx_RecordCallStatistics(z_call,");
@@ -1717,29 +1715,7 @@ ss_ProcTail_setup(definition * defp, int somefrees)
     }
 
     f_print(fout, "\treturn z_result;\n");
-    if (somefrees) {
-       f_print(fout, "fail1:\n");
-
-       if (xflag) {
-           f_print(fout, "\tif (rx_enable_stats) {\n");
-           f_print(fout,
-                   "\t    rx_RecordCallStatistics(z_call,");
-           if (PackageStatIndex[PackageIndex]) {
-               f_print(fout, " %s,\n", PackageStatIndex[PackageIndex]);
-           } else {
-               f_print(fout,
-                       "\n\t\t(((afs_uint32)(ntohs(rx_ServiceIdOf(rx_ConnectionOf(z_call))) << 16)) |\n"
-                       "\t\t((afs_uint32)ntohs(rx_ServiceOf(rx_ConnectionOf(z_call))->servicePort))),\n");
-           }
-           f_print(fout, "\t\t%d, %sNO_OF_STAT_FUNCS, 0);\n",
-                   no_of_stat_funcs, PackagePrefix[PackageIndex]);
-           f_print(fout, "\t}\n\n");
-       }
-
-       f_print(fout, "\treturn RXGEN_SS_XDRFREE;\n}\n\n");
-    } else {
-       f_print(fout, "}\n\n");
-    }
+    f_print(fout, "}\n\n");
 }
 
 
@@ -1766,17 +1742,22 @@ ucs_ProcName_setup(definition * defp, char *procheader, int split_flag)
     if (!cflag) {
        for (plist = defp->pc.plists; plist; plist = plist->next) {
            if (plist->component_kind == DEF_PARAM) {
+               f_print(fout, ",");
                if (ansic_flag) {
+                   if (plist->pl.param_kind == DEF_INPARAM &&
+                       strcmp(plist->pl.param_type, "char *") == 0) {
+                       f_print(fout, "const ");
+                   }
                    if (plist->pl.param_flag & OUT_STRING) {
-                       f_print(fout, ",%s *%s", plist->pl.param_type,
+                       f_print(fout, "%s *%s", plist->pl.param_type,
                                plist->pl.param_name);
                    } else {
-                       f_print(fout, ",%s %s", plist->pl.param_type,
+                       f_print(fout, "%s %s", plist->pl.param_type,
                                plist->pl.param_name);
                    }
                } else {
                    plist->pl.param_flag &= ~PROCESSED_PARAM;
-                   f_print(fout, ", %s", plist->pl.param_name);
+                   f_print(fout, " %s", plist->pl.param_name);
                }
            }
        }
@@ -2191,6 +2172,7 @@ h_HeadofOldStyleProc_setup(void)
     f_print(fout,"\nextern int %s%sExecuteRequest(struct rx_call *);\n",
            prefix,
            (combinepackages ? MasterPrefix : PackagePrefix[PackageIndex]));
+    f_print(fout,"\nextern int %sOpCodeIndex(int op);\n", PackagePrefix[PackageIndex]);
 }
 
 void
@@ -2211,45 +2193,52 @@ h_Proc_CodeGeneration(void)
     PackageIndex = temp;
 }
 
+static void
+proc_h_case(definition * defp)
+{
+    f_print(fout, "#define opcode_%s%s \t((afs_uint64)((%uLL << 32) + %sOpCodeIndex(%u)))\n",
+           defp->pc.proc_prefix, defp->pc.proc_name, StatIndex,
+           defp->pc.proc_prefix, defp->pc.proc_opcodenum);
+}
+
+void
+h_opcode_stats_pkg(char *pprefix, int lowest, int highest, int nops,
+                 int statfuncs, char *ptype, list *proclist)
+{
+    list *listp;
+
+    if (!pprefix)
+       return;
+
+    f_print(fout,
+            "\n/* Opcode-related useful stats for %spackage: %s */\n",
+            ptype, pprefix);
+    f_print(fout, "#define %sLOWEST_OPCODE   %d\n", pprefix, lowest);
+    f_print(fout, "#define %sHIGHEST_OPCODE     %d\n", pprefix, highest);
+    f_print(fout, "#define %sNUMBER_OPCODES     %d\n\n", pprefix, nops);
+
+    for (listp = proclist; listp != NULL;
+         listp = listp->next)
+        proc_h_case((definition *) listp->val);
+
+    if (xflag) {
+        f_print(fout, "#define %sNO_OF_STAT_FUNCS\t%d\n\n",
+                pprefix, statfuncs);
+        f_print(fout, "AFS_RXGEN_EXPORT\n");
+        f_print(fout, "extern const char *%sfunction_names[];\n\n",
+                pprefix);
+    }
+}
+
 void
 h_opcode_stats(void)
 {
     if (combinepackages) {
-       f_print(fout,
-               "\n/* Opcode-related useful stats for Master package: %s */\n",
-               MasterPrefix);
-       f_print(fout, "#define %sLOWEST_OPCODE   %d\n", MasterPrefix,
-               master_lowest_opcode);
-       f_print(fout, "#define %sHIGHEST_OPCODE %d\n", MasterPrefix,
-               master_highest_opcode);
-       f_print(fout, "#define %sNUMBER_OPCODES %d\n\n", MasterPrefix,
-               master_no_of_opcodes);
-       if (xflag) {
-           f_print(fout, "#define %sNO_OF_STAT_FUNCS\t%d\n\n", MasterPrefix,
-                   no_of_stat_funcs_header[0]);
-           f_print(fout, "AFS_RXGEN_EXPORT\n");
-           f_print(fout, "extern const char *%sfunction_names[];\n\n",
-                   MasterPrefix);
-       }
+       h_opcode_stats_pkg(MasterPrefix, master_lowest_opcode, master_highest_opcode, master_no_of_opcodes, no_of_stat_funcs_header[0], "Master ", proc_defined[0]);
     } else {
        int i;
        for (i = 0; i <= PackageIndex; i++) {
-           f_print(fout,
-                   "\n/* Opcode-related useful stats for package: %s */\n",
-                   PackagePrefix[i]);
-           f_print(fout, "#define %sLOWEST_OPCODE   %d\n", PackagePrefix[i],
-                   lowest_opcode[i]);
-           f_print(fout, "#define %sHIGHEST_OPCODE     %d\n",
-                   PackagePrefix[i], highest_opcode[i]);
-           f_print(fout, "#define %sNUMBER_OPCODES     %d\n\n",
-                   PackagePrefix[i], no_of_opcodes[i]);
-           if (xflag) {
-               f_print(fout, "#define %sNO_OF_STAT_FUNCS\t%d\n\n",
-                       PackagePrefix[i], no_of_stat_funcs_header[i]);
-               f_print(fout, "AFS_RXGEN_EXPORT\n");
-               f_print(fout, "extern const char *%sfunction_names[];\n\n",
-                       PackagePrefix[i]);
-           }
+           h_opcode_stats_pkg(PackagePrefix[i], lowest_opcode[i], highest_opcode[i], no_of_opcodes[i], no_of_stat_funcs_header[i], "", proc_defined[i]);
        }
     }
 }