17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*a2f144d1SJordan Brown * Common Development and Distribution License (the "License").
6*a2f144d1SJordan Brown * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
2161961e0fSrobinson
227c478bd9Sstevel@tonic-gate /*
23*a2f144d1SJordan Brown * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
277c478bd9Sstevel@tonic-gate /* All Rights Reserved */
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988
307c478bd9Sstevel@tonic-gate * The Regents of the University of California
317c478bd9Sstevel@tonic-gate * All Rights Reserved
327c478bd9Sstevel@tonic-gate *
337c478bd9Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from
347c478bd9Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its
357c478bd9Sstevel@tonic-gate * contributors.
367c478bd9Sstevel@tonic-gate */
377c478bd9Sstevel@tonic-gate
387c478bd9Sstevel@tonic-gate /*
397c478bd9Sstevel@tonic-gate * rpc_cout.c, XDR routine outputter for the RPC protocol compiler
407c478bd9Sstevel@tonic-gate */
417c478bd9Sstevel@tonic-gate #include <stdio.h>
4261961e0fSrobinson #include <stdlib.h>
437c478bd9Sstevel@tonic-gate #include <string.h>
4461961e0fSrobinson #include <ctype.h>
457c478bd9Sstevel@tonic-gate #include "rpc_parse.h"
467c478bd9Sstevel@tonic-gate #include "rpc_util.h"
477c478bd9Sstevel@tonic-gate
4861961e0fSrobinson extern void crash(void);
4961961e0fSrobinson
5061961e0fSrobinson static void print_header(definition *);
5161961e0fSrobinson static void print_trailer(void);
5261961e0fSrobinson static void emit_enum(definition *);
5361961e0fSrobinson static void emit_program(definition *);
5461961e0fSrobinson static void emit_union(definition *);
5561961e0fSrobinson static void emit_struct(definition *);
5661961e0fSrobinson static void emit_typedef(definition *);
5761961e0fSrobinson static void print_stat(int, declaration *);
5861961e0fSrobinson static void emit_inline(int, declaration *, int);
5961961e0fSrobinson static void emit_inline64(int, declaration *, int);
6061961e0fSrobinson static void emit_single_in_line(int, declaration *, int, relation);
6161961e0fSrobinson static void emit_single_in_line64(int, declaration *, int, relation);
6261961e0fSrobinson static char *upcase(char *);
6361961e0fSrobinson
647c478bd9Sstevel@tonic-gate /*
657c478bd9Sstevel@tonic-gate * Emit the C-routine for the given definition
667c478bd9Sstevel@tonic-gate */
677c478bd9Sstevel@tonic-gate void
emit(definition * def)6861961e0fSrobinson emit(definition *def)
697c478bd9Sstevel@tonic-gate {
7061961e0fSrobinson if (def->def_kind == DEF_CONST)
717c478bd9Sstevel@tonic-gate return;
727c478bd9Sstevel@tonic-gate if (def->def_kind == DEF_PROGRAM) {
737c478bd9Sstevel@tonic-gate emit_program(def);
747c478bd9Sstevel@tonic-gate return;
757c478bd9Sstevel@tonic-gate }
767c478bd9Sstevel@tonic-gate if (def->def_kind == DEF_TYPEDEF) {
777c478bd9Sstevel@tonic-gate /*
787c478bd9Sstevel@tonic-gate * now we need to handle declarations like
797c478bd9Sstevel@tonic-gate * struct typedef foo foo;
807c478bd9Sstevel@tonic-gate * since we dont want this to be expanded into 2 calls
817c478bd9Sstevel@tonic-gate * to xdr_foo
827c478bd9Sstevel@tonic-gate */
837c478bd9Sstevel@tonic-gate
847c478bd9Sstevel@tonic-gate if (strcmp(def->def.ty.old_type, def->def_name) == 0)
857c478bd9Sstevel@tonic-gate return;
867c478bd9Sstevel@tonic-gate };
877c478bd9Sstevel@tonic-gate print_header(def);
887c478bd9Sstevel@tonic-gate switch (def->def_kind) {
897c478bd9Sstevel@tonic-gate case DEF_UNION:
907c478bd9Sstevel@tonic-gate emit_union(def);
917c478bd9Sstevel@tonic-gate break;
927c478bd9Sstevel@tonic-gate case DEF_ENUM:
937c478bd9Sstevel@tonic-gate emit_enum(def);
947c478bd9Sstevel@tonic-gate break;
957c478bd9Sstevel@tonic-gate case DEF_STRUCT:
967c478bd9Sstevel@tonic-gate emit_struct(def);
977c478bd9Sstevel@tonic-gate break;
987c478bd9Sstevel@tonic-gate case DEF_TYPEDEF:
997c478bd9Sstevel@tonic-gate emit_typedef(def);
1007c478bd9Sstevel@tonic-gate break;
1017c478bd9Sstevel@tonic-gate }
1027c478bd9Sstevel@tonic-gate print_trailer();
1037c478bd9Sstevel@tonic-gate }
1047c478bd9Sstevel@tonic-gate
10561961e0fSrobinson static int
findtype(definition * def,char * type)10661961e0fSrobinson findtype(definition *def, char *type)
1077c478bd9Sstevel@tonic-gate {
1087c478bd9Sstevel@tonic-gate
10961961e0fSrobinson if (def->def_kind == DEF_PROGRAM || def->def_kind == DEF_CONST)
1107c478bd9Sstevel@tonic-gate return (0);
1117c478bd9Sstevel@tonic-gate return (streq(def->def_name, type));
1127c478bd9Sstevel@tonic-gate }
1137c478bd9Sstevel@tonic-gate
11461961e0fSrobinson static int
undefined(char * type)11561961e0fSrobinson undefined(char *type)
1167c478bd9Sstevel@tonic-gate {
1177c478bd9Sstevel@tonic-gate definition *def;
1187c478bd9Sstevel@tonic-gate
1197c478bd9Sstevel@tonic-gate def = (definition *)FINDVAL(defined, type, findtype);
1207c478bd9Sstevel@tonic-gate return (def == NULL);
1217c478bd9Sstevel@tonic-gate }
1227c478bd9Sstevel@tonic-gate
1237c478bd9Sstevel@tonic-gate
12461961e0fSrobinson static void
print_generic_header(char * procname,int pointerp)12561961e0fSrobinson print_generic_header(char *procname, int pointerp)
1267c478bd9Sstevel@tonic-gate {
1277c478bd9Sstevel@tonic-gate f_print(fout, "\n");
1287c478bd9Sstevel@tonic-gate f_print(fout, "bool_t\n");
1297c478bd9Sstevel@tonic-gate if (Cflag) {
1307c478bd9Sstevel@tonic-gate f_print(fout, "xdr_%s(", procname);
13161961e0fSrobinson f_print(fout, "XDR *xdrs, ");
1327c478bd9Sstevel@tonic-gate f_print(fout, "%s ", procname);
1337c478bd9Sstevel@tonic-gate if (pointerp)
1347c478bd9Sstevel@tonic-gate f_print(fout, "*");
1357c478bd9Sstevel@tonic-gate f_print(fout, "objp)\n{\n\n");
1367c478bd9Sstevel@tonic-gate } else {
1377c478bd9Sstevel@tonic-gate f_print(fout, "xdr_%s(xdrs, objp)\n", procname);
13861961e0fSrobinson f_print(fout, "\tXDR *xdrs;\n");
1397c478bd9Sstevel@tonic-gate f_print(fout, "\t%s ", procname);
1407c478bd9Sstevel@tonic-gate if (pointerp)
1417c478bd9Sstevel@tonic-gate f_print(fout, "*");
1427c478bd9Sstevel@tonic-gate f_print(fout, "objp;\n{\n\n");
1437c478bd9Sstevel@tonic-gate }
1447c478bd9Sstevel@tonic-gate }
1457c478bd9Sstevel@tonic-gate
14661961e0fSrobinson static void
print_header(definition * def)14761961e0fSrobinson print_header(definition *def)
1487c478bd9Sstevel@tonic-gate {
1497c478bd9Sstevel@tonic-gate print_generic_header(def->def_name,
1507c478bd9Sstevel@tonic-gate def->def_kind != DEF_TYPEDEF ||
151*a2f144d1SJordan Brown !isvectordef(def->def.ty.old_type, def->def.ty.rel));
1527c478bd9Sstevel@tonic-gate /* Now add Inline support */
1537c478bd9Sstevel@tonic-gate
1547c478bd9Sstevel@tonic-gate if (inlinelen == 0)
1557c478bd9Sstevel@tonic-gate return;
1567c478bd9Sstevel@tonic-gate /* May cause lint to complain. but ... */
15761961e0fSrobinson f_print(fout, "\trpc_inline_t *buf;\n\n");
1587c478bd9Sstevel@tonic-gate }
1597c478bd9Sstevel@tonic-gate
16061961e0fSrobinson static void
print_prog_header(proc_list * plist)16161961e0fSrobinson print_prog_header(proc_list *plist)
1627c478bd9Sstevel@tonic-gate {
1637c478bd9Sstevel@tonic-gate print_generic_header(plist->args.argname, 1);
1647c478bd9Sstevel@tonic-gate }
1657c478bd9Sstevel@tonic-gate
16661961e0fSrobinson static void
print_trailer(void)16761961e0fSrobinson print_trailer(void)
1687c478bd9Sstevel@tonic-gate {
1697c478bd9Sstevel@tonic-gate f_print(fout, "\treturn (TRUE);\n");
1707c478bd9Sstevel@tonic-gate f_print(fout, "}\n");
1717c478bd9Sstevel@tonic-gate }
1727c478bd9Sstevel@tonic-gate
1737c478bd9Sstevel@tonic-gate
17461961e0fSrobinson static void
print_ifopen(int indent,char * name)17561961e0fSrobinson print_ifopen(int indent, char *name)
1767c478bd9Sstevel@tonic-gate {
1777c478bd9Sstevel@tonic-gate tabify(fout, indent);
1787c478bd9Sstevel@tonic-gate if (streq(name, "rpcprog_t") ||
1797c478bd9Sstevel@tonic-gate streq(name, "rpcvers_t") ||
1807c478bd9Sstevel@tonic-gate streq(name, "rpcproc_t") ||
1817c478bd9Sstevel@tonic-gate streq(name, "rpcprot_t") ||
1827c478bd9Sstevel@tonic-gate streq(name, "rpcport_t"))
18361961e0fSrobinson (void) strtok(name, "_");
1847c478bd9Sstevel@tonic-gate f_print(fout, "if (!xdr_%s(xdrs", name);
1857c478bd9Sstevel@tonic-gate }
1867c478bd9Sstevel@tonic-gate
18761961e0fSrobinson static void
print_ifarg(char * arg)18861961e0fSrobinson print_ifarg(char *arg)
1897c478bd9Sstevel@tonic-gate {
1907c478bd9Sstevel@tonic-gate f_print(fout, ", %s", arg);
1917c478bd9Sstevel@tonic-gate }
1927c478bd9Sstevel@tonic-gate
19361961e0fSrobinson static void
print_ifsizeof(int indent,char * prefix,char * type)19461961e0fSrobinson print_ifsizeof(int indent, char *prefix, char *type)
1957c478bd9Sstevel@tonic-gate {
1967c478bd9Sstevel@tonic-gate if (indent) {
1977c478bd9Sstevel@tonic-gate f_print(fout, ",\n");
1987c478bd9Sstevel@tonic-gate tabify(fout, indent);
1997c478bd9Sstevel@tonic-gate } else {
2007c478bd9Sstevel@tonic-gate f_print(fout, ", ");
2017c478bd9Sstevel@tonic-gate }
2027c478bd9Sstevel@tonic-gate if (streq(type, "bool")) {
2037c478bd9Sstevel@tonic-gate f_print(fout, "sizeof (bool_t), (xdrproc_t)xdr_bool");
2047c478bd9Sstevel@tonic-gate } else {
2057c478bd9Sstevel@tonic-gate f_print(fout, "sizeof (");
2067c478bd9Sstevel@tonic-gate if (undefined(type) && prefix) {
2077c478bd9Sstevel@tonic-gate f_print(fout, "%s ", prefix);
2087c478bd9Sstevel@tonic-gate }
2097c478bd9Sstevel@tonic-gate f_print(fout, "%s), (xdrproc_t)xdr_%s", type, type);
2107c478bd9Sstevel@tonic-gate }
2117c478bd9Sstevel@tonic-gate }
2127c478bd9Sstevel@tonic-gate
21361961e0fSrobinson static void
print_ifclose(int indent)21461961e0fSrobinson print_ifclose(int indent)
2157c478bd9Sstevel@tonic-gate {
2167c478bd9Sstevel@tonic-gate f_print(fout, "))\n");
2177c478bd9Sstevel@tonic-gate tabify(fout, indent);
2187c478bd9Sstevel@tonic-gate f_print(fout, "\treturn (FALSE);\n");
2197c478bd9Sstevel@tonic-gate }
2207c478bd9Sstevel@tonic-gate
22161961e0fSrobinson static void
print_ifstat(int indent,char * prefix,char * type,relation rel,char * amax,char * objname,char * name)22261961e0fSrobinson print_ifstat(int indent, char *prefix, char *type, relation rel,
22361961e0fSrobinson char *amax, char *objname, char *name)
2247c478bd9Sstevel@tonic-gate {
2257c478bd9Sstevel@tonic-gate char *alt = NULL;
2267c478bd9Sstevel@tonic-gate
2277c478bd9Sstevel@tonic-gate switch (rel) {
2287c478bd9Sstevel@tonic-gate case REL_POINTER:
2297c478bd9Sstevel@tonic-gate print_ifopen(indent, "pointer");
2307c478bd9Sstevel@tonic-gate print_ifarg("(char **)");
2317c478bd9Sstevel@tonic-gate f_print(fout, "%s", objname);
2327c478bd9Sstevel@tonic-gate print_ifsizeof(0, prefix, type);
2337c478bd9Sstevel@tonic-gate break;
2347c478bd9Sstevel@tonic-gate case REL_VECTOR:
23561961e0fSrobinson if (streq(type, "string"))
2367c478bd9Sstevel@tonic-gate alt = "string";
23761961e0fSrobinson else if (streq(type, "opaque"))
2387c478bd9Sstevel@tonic-gate alt = "opaque";
2397c478bd9Sstevel@tonic-gate if (alt) {
2407c478bd9Sstevel@tonic-gate print_ifopen(indent, alt);
2417c478bd9Sstevel@tonic-gate print_ifarg(objname);
2427c478bd9Sstevel@tonic-gate } else {
2437c478bd9Sstevel@tonic-gate print_ifopen(indent, "vector");
2447c478bd9Sstevel@tonic-gate print_ifarg("(char *)");
2457c478bd9Sstevel@tonic-gate f_print(fout, "%s", objname);
2467c478bd9Sstevel@tonic-gate }
2477c478bd9Sstevel@tonic-gate print_ifarg(amax);
24861961e0fSrobinson if (!alt)
2497c478bd9Sstevel@tonic-gate print_ifsizeof(indent + 1, prefix, type);
2507c478bd9Sstevel@tonic-gate break;
2517c478bd9Sstevel@tonic-gate case REL_ARRAY:
25261961e0fSrobinson if (streq(type, "string"))
2537c478bd9Sstevel@tonic-gate alt = "string";
25461961e0fSrobinson else if (streq(type, "opaque"))
2557c478bd9Sstevel@tonic-gate alt = "bytes";
2567c478bd9Sstevel@tonic-gate if (streq(type, "string")) {
2577c478bd9Sstevel@tonic-gate print_ifopen(indent, alt);
2587c478bd9Sstevel@tonic-gate print_ifarg(objname);
2597c478bd9Sstevel@tonic-gate } else {
26061961e0fSrobinson if (alt)
2617c478bd9Sstevel@tonic-gate print_ifopen(indent, alt);
26261961e0fSrobinson else
2637c478bd9Sstevel@tonic-gate print_ifopen(indent, "array");
2647c478bd9Sstevel@tonic-gate print_ifarg("(char **)");
26561961e0fSrobinson if (*objname == '&')
2667c478bd9Sstevel@tonic-gate f_print(fout, "%s.%s_val, (u_int *) %s.%s_len",
2677c478bd9Sstevel@tonic-gate objname, name, objname, name);
26861961e0fSrobinson else
2697c478bd9Sstevel@tonic-gate f_print(fout,
2707c478bd9Sstevel@tonic-gate "&%s->%s_val, (u_int *) &%s->%s_len",
2717c478bd9Sstevel@tonic-gate objname, name, objname, name);
2727c478bd9Sstevel@tonic-gate }
2737c478bd9Sstevel@tonic-gate print_ifarg(amax);
27461961e0fSrobinson if (!alt)
2757c478bd9Sstevel@tonic-gate print_ifsizeof(indent + 1, prefix, type);
2767c478bd9Sstevel@tonic-gate break;
2777c478bd9Sstevel@tonic-gate case REL_ALIAS:
2787c478bd9Sstevel@tonic-gate print_ifopen(indent, type);
2797c478bd9Sstevel@tonic-gate print_ifarg(objname);
2807c478bd9Sstevel@tonic-gate break;
2817c478bd9Sstevel@tonic-gate }
2827c478bd9Sstevel@tonic-gate print_ifclose(indent);
2837c478bd9Sstevel@tonic-gate }
2847c478bd9Sstevel@tonic-gate
2857c478bd9Sstevel@tonic-gate /* ARGSUSED */
28661961e0fSrobinson static void
emit_enum(definition * def)28761961e0fSrobinson emit_enum(definition *def)
2887c478bd9Sstevel@tonic-gate {
2897c478bd9Sstevel@tonic-gate print_ifopen(1, "enum");
2907c478bd9Sstevel@tonic-gate print_ifarg("(enum_t *)objp");
2917c478bd9Sstevel@tonic-gate print_ifclose(1);
2927c478bd9Sstevel@tonic-gate }
2937c478bd9Sstevel@tonic-gate
29461961e0fSrobinson static void
emit_program(definition * def)29561961e0fSrobinson emit_program(definition *def)
2967c478bd9Sstevel@tonic-gate {
2977c478bd9Sstevel@tonic-gate decl_list *dl;
2987c478bd9Sstevel@tonic-gate version_list *vlist;
2997c478bd9Sstevel@tonic-gate proc_list *plist;
3007c478bd9Sstevel@tonic-gate
3017c478bd9Sstevel@tonic-gate for (vlist = def->def.pr.versions; vlist != NULL; vlist = vlist->next)
3027c478bd9Sstevel@tonic-gate for (plist = vlist->procs; plist != NULL; plist = plist->next) {
3037c478bd9Sstevel@tonic-gate if (!newstyle || plist->arg_num < 2)
3047c478bd9Sstevel@tonic-gate continue; /* old style, or single argument */
3057c478bd9Sstevel@tonic-gate print_prog_header(plist);
3067c478bd9Sstevel@tonic-gate for (dl = plist->args.decls; dl != NULL;
3077c478bd9Sstevel@tonic-gate dl = dl->next)
3087c478bd9Sstevel@tonic-gate print_stat(1, &dl->decl);
3097c478bd9Sstevel@tonic-gate print_trailer();
3107c478bd9Sstevel@tonic-gate }
3117c478bd9Sstevel@tonic-gate }
3127c478bd9Sstevel@tonic-gate
3137c478bd9Sstevel@tonic-gate
31461961e0fSrobinson static void
emit_union(definition * def)31561961e0fSrobinson emit_union(definition *def)
3167c478bd9Sstevel@tonic-gate {
3177c478bd9Sstevel@tonic-gate declaration *dflt;
3187c478bd9Sstevel@tonic-gate case_list *cl;
3197c478bd9Sstevel@tonic-gate declaration *cs;
3207c478bd9Sstevel@tonic-gate char *object;
3217c478bd9Sstevel@tonic-gate
3227c478bd9Sstevel@tonic-gate print_stat(1, &def->def.un.enum_decl);
3237c478bd9Sstevel@tonic-gate f_print(fout, "\tswitch (objp->%s) {\n", def->def.un.enum_decl.name);
3247c478bd9Sstevel@tonic-gate for (cl = def->def.un.cases; cl != NULL; cl = cl->next) {
3257c478bd9Sstevel@tonic-gate
3267c478bd9Sstevel@tonic-gate f_print(fout, "\tcase %s:\n", cl->case_name);
3277c478bd9Sstevel@tonic-gate if (cl->contflag == 1) /* a continued case statement */
3287c478bd9Sstevel@tonic-gate continue;
3297c478bd9Sstevel@tonic-gate cs = &cl->case_decl;
3307c478bd9Sstevel@tonic-gate if (!streq(cs->type, "void")) {
33161961e0fSrobinson size_t len = strlen(def->def_name) +
33261961e0fSrobinson strlen("&objp->%s_u.%s") +
33361961e0fSrobinson strlen(cs->name) + 1;
33461961e0fSrobinson object = malloc(len);
33561961e0fSrobinson if (isvectordef(cs->type, cs->rel))
33661961e0fSrobinson (void) snprintf(object, len, "objp->%s_u.%s",
33761961e0fSrobinson def->def_name, cs->name);
33861961e0fSrobinson else
33961961e0fSrobinson (void) snprintf(object, len, "&objp->%s_u.%s",
34061961e0fSrobinson def->def_name, cs->name);
3417c478bd9Sstevel@tonic-gate print_ifstat(2, cs->prefix, cs->type, cs->rel,
3427c478bd9Sstevel@tonic-gate cs->array_max, object, cs->name);
3437c478bd9Sstevel@tonic-gate free(object);
3447c478bd9Sstevel@tonic-gate }
3457c478bd9Sstevel@tonic-gate f_print(fout, "\t\tbreak;\n");
3467c478bd9Sstevel@tonic-gate }
3477c478bd9Sstevel@tonic-gate dflt = def->def.un.default_decl;
3487c478bd9Sstevel@tonic-gate if (dflt != NULL) {
3497c478bd9Sstevel@tonic-gate if (!streq(dflt->type, "void")) {
35061961e0fSrobinson size_t len = strlen(def->def_name) +
35161961e0fSrobinson strlen("&objp->%s_u.%s") +
35261961e0fSrobinson strlen(dflt->name) + 1;
3537c478bd9Sstevel@tonic-gate f_print(fout, "\tdefault:\n");
35461961e0fSrobinson object = malloc(len);
35561961e0fSrobinson if (isvectordef(dflt->type, dflt->rel))
35661961e0fSrobinson (void) snprintf(object, len, "objp->%s_u.%s",
35761961e0fSrobinson def->def_name, dflt->name);
35861961e0fSrobinson else
35961961e0fSrobinson (void) snprintf(object, len, "&objp->%s_u.%s",
36061961e0fSrobinson def->def_name, dflt->name);
3617c478bd9Sstevel@tonic-gate
3627c478bd9Sstevel@tonic-gate print_ifstat(2, dflt->prefix, dflt->type, dflt->rel,
3637c478bd9Sstevel@tonic-gate dflt->array_max, object, dflt->name);
3647c478bd9Sstevel@tonic-gate free(object);
3657c478bd9Sstevel@tonic-gate f_print(fout, "\t\tbreak;\n");
3667c478bd9Sstevel@tonic-gate }
3677c478bd9Sstevel@tonic-gate } else {
3687c478bd9Sstevel@tonic-gate f_print(fout, "\tdefault:\n");
3697c478bd9Sstevel@tonic-gate f_print(fout, "\t\treturn (FALSE);\n");
3707c478bd9Sstevel@tonic-gate }
3717c478bd9Sstevel@tonic-gate
3727c478bd9Sstevel@tonic-gate f_print(fout, "\t}\n");
3737c478bd9Sstevel@tonic-gate }
3747c478bd9Sstevel@tonic-gate
3757c478bd9Sstevel@tonic-gate static void
expand_inline(int indent,const char * sizestr,int size,int flag,decl_list * dl,decl_list * cur)3767c478bd9Sstevel@tonic-gate expand_inline(int indent, const char *sizestr,
3777c478bd9Sstevel@tonic-gate int size, int flag, decl_list *dl, decl_list *cur)
3787c478bd9Sstevel@tonic-gate {
3797c478bd9Sstevel@tonic-gate decl_list *psav;
3807c478bd9Sstevel@tonic-gate
3817c478bd9Sstevel@tonic-gate /*
3827c478bd9Sstevel@tonic-gate * were already looking at a xdr_inlineable structure
3837c478bd9Sstevel@tonic-gate */
3847c478bd9Sstevel@tonic-gate tabify(fout, indent + 1);
3857c478bd9Sstevel@tonic-gate if (sizestr == NULL)
3867c478bd9Sstevel@tonic-gate f_print(fout,
3877c478bd9Sstevel@tonic-gate "buf = XDR_INLINE(xdrs, %d * BYTES_PER_XDR_UNIT);",
3887c478bd9Sstevel@tonic-gate size);
3897c478bd9Sstevel@tonic-gate else if (size == 0)
3907c478bd9Sstevel@tonic-gate f_print(fout,
3917c478bd9Sstevel@tonic-gate "buf = XDR_INLINE(xdrs, (%s) * BYTES_PER_XDR_UNIT);",
3927c478bd9Sstevel@tonic-gate sizestr);
3937c478bd9Sstevel@tonic-gate else
3947c478bd9Sstevel@tonic-gate f_print(fout,
3957c478bd9Sstevel@tonic-gate "buf = XDR_INLINE(xdrs, (%d + (%s)) "
3967c478bd9Sstevel@tonic-gate "* BYTES_PER_XDR_UNIT);", size, sizestr);
3977c478bd9Sstevel@tonic-gate
3987c478bd9Sstevel@tonic-gate f_print(fout, "\n");
3997c478bd9Sstevel@tonic-gate tabify(fout, indent + 1);
4007c478bd9Sstevel@tonic-gate f_print(fout, "if (buf == NULL) {\n");
4017c478bd9Sstevel@tonic-gate
4027c478bd9Sstevel@tonic-gate psav = cur;
4037c478bd9Sstevel@tonic-gate while (cur != dl) {
4047c478bd9Sstevel@tonic-gate print_stat(indent + 2,
4057c478bd9Sstevel@tonic-gate &cur->decl);
4067c478bd9Sstevel@tonic-gate cur = cur->next;
4077c478bd9Sstevel@tonic-gate }
4087c478bd9Sstevel@tonic-gate
4097c478bd9Sstevel@tonic-gate tabify(fout, indent+1);
4107c478bd9Sstevel@tonic-gate f_print(fout, "} else {\n");
4117c478bd9Sstevel@tonic-gate
4127c478bd9Sstevel@tonic-gate f_print(fout, "#if defined(_LP64) || defined(_KERNEL)\n");
4137c478bd9Sstevel@tonic-gate cur = psav;
4147c478bd9Sstevel@tonic-gate while (cur != dl) {
4157c478bd9Sstevel@tonic-gate emit_inline64(indent + 2, &cur->decl, flag);
4167c478bd9Sstevel@tonic-gate cur = cur->next;
4177c478bd9Sstevel@tonic-gate }
4187c478bd9Sstevel@tonic-gate f_print(fout, "#else\n");
4197c478bd9Sstevel@tonic-gate cur = psav;
4207c478bd9Sstevel@tonic-gate while (cur != dl) {
4217c478bd9Sstevel@tonic-gate emit_inline(indent + 2, &cur->decl, flag);
4227c478bd9Sstevel@tonic-gate cur = cur->next;
4237c478bd9Sstevel@tonic-gate }
4247c478bd9Sstevel@tonic-gate f_print(fout, "#endif\n");
4257c478bd9Sstevel@tonic-gate
4267c478bd9Sstevel@tonic-gate tabify(fout, indent + 1);
4277c478bd9Sstevel@tonic-gate f_print(fout, "}\n");
4287c478bd9Sstevel@tonic-gate }
4297c478bd9Sstevel@tonic-gate
4307c478bd9Sstevel@tonic-gate /*
4317c478bd9Sstevel@tonic-gate * An inline type is a base type (interger type) or a vector of base types.
4327c478bd9Sstevel@tonic-gate */
4337c478bd9Sstevel@tonic-gate static int
inline_type(declaration * dc,int * size)4347c478bd9Sstevel@tonic-gate inline_type(declaration *dc, int *size)
4357c478bd9Sstevel@tonic-gate {
4367c478bd9Sstevel@tonic-gate bas_type *ptr;
4377c478bd9Sstevel@tonic-gate
4387c478bd9Sstevel@tonic-gate *size = 0;
4397c478bd9Sstevel@tonic-gate
4407c478bd9Sstevel@tonic-gate if (dc->prefix == NULL &&
4417c478bd9Sstevel@tonic-gate (dc->rel == REL_ALIAS || dc->rel == REL_VECTOR)) {
4427c478bd9Sstevel@tonic-gate ptr = find_type(dc->type);
4437c478bd9Sstevel@tonic-gate if (ptr != NULL) {
4447c478bd9Sstevel@tonic-gate *size = ptr->length;
4457c478bd9Sstevel@tonic-gate return (1);
4467c478bd9Sstevel@tonic-gate }
4477c478bd9Sstevel@tonic-gate }
4487c478bd9Sstevel@tonic-gate
4497c478bd9Sstevel@tonic-gate return (0);
4507c478bd9Sstevel@tonic-gate }
4517c478bd9Sstevel@tonic-gate
4527c478bd9Sstevel@tonic-gate static char *
arraysize(char * sz,declaration * dc,int elsize)4537c478bd9Sstevel@tonic-gate arraysize(char *sz, declaration *dc, int elsize)
4547c478bd9Sstevel@tonic-gate {
4557c478bd9Sstevel@tonic-gate int len;
4567c478bd9Sstevel@tonic-gate int elsz = elsize;
4577c478bd9Sstevel@tonic-gate int digits;
4587c478bd9Sstevel@tonic-gate int slen = 0;
4597c478bd9Sstevel@tonic-gate char *plus = "";
4607c478bd9Sstevel@tonic-gate char *tmp;
46161961e0fSrobinson size_t tlen;
4627c478bd9Sstevel@tonic-gate
4637c478bd9Sstevel@tonic-gate /*
4647c478bd9Sstevel@tonic-gate * Calculate the size of a string to hold the size of all arrays
4657c478bd9Sstevel@tonic-gate * to be inlined.
4667c478bd9Sstevel@tonic-gate *
4677c478bd9Sstevel@tonic-gate * We have the string representation of the total size that has already
4687c478bd9Sstevel@tonic-gate * been seen. (Null if this is the first array).
4697c478bd9Sstevel@tonic-gate * We have the string representation of array max from the declaration,
4707c478bd9Sstevel@tonic-gate * optionally the plus string, " + ", if this is not the first array,
4717c478bd9Sstevel@tonic-gate * and the number of digits for the element size for this declaration.
4727c478bd9Sstevel@tonic-gate */
4737c478bd9Sstevel@tonic-gate if (sz != NULL) {
4747c478bd9Sstevel@tonic-gate plus = " + ";
4757c478bd9Sstevel@tonic-gate slen = strlen(sz);
4767c478bd9Sstevel@tonic-gate }
4777c478bd9Sstevel@tonic-gate
4787c478bd9Sstevel@tonic-gate /* Calculate the number of digits to hold the element size */
4797c478bd9Sstevel@tonic-gate for (digits = 1; elsz >= 10; digits++)
4807c478bd9Sstevel@tonic-gate elsz /= 10;
4817c478bd9Sstevel@tonic-gate
4827c478bd9Sstevel@tonic-gate /*
4837c478bd9Sstevel@tonic-gate * If elsize != 1 the allocate 3 extra bytes for the times
4847c478bd9Sstevel@tonic-gate * string, " * ", the "()" below, and the digits. One extra
4857c478bd9Sstevel@tonic-gate * for the trailing NULL
4867c478bd9Sstevel@tonic-gate */
4877c478bd9Sstevel@tonic-gate len = strlen(dc->array_max) + (elsize == 1 ? 0 : digits + 5) + 1;
48861961e0fSrobinson tlen = slen + len + strlen(plus);
48961961e0fSrobinson tmp = realloc(sz, tlen);
4907c478bd9Sstevel@tonic-gate if (tmp == NULL) {
4917c478bd9Sstevel@tonic-gate f_print(stderr, "Fatal error : no memory\n");
4927c478bd9Sstevel@tonic-gate crash();
4937c478bd9Sstevel@tonic-gate }
4947c478bd9Sstevel@tonic-gate
4957c478bd9Sstevel@tonic-gate if (elsize == 1)
49661961e0fSrobinson (void) snprintf(tmp + slen, tlen - slen, "%s%s",
49761961e0fSrobinson plus, dc->array_max);
4987c478bd9Sstevel@tonic-gate else
49961961e0fSrobinson (void) snprintf(tmp + slen, tlen - slen, "%s(%s) * %d",
50061961e0fSrobinson plus, dc->array_max, elsize);
5017c478bd9Sstevel@tonic-gate
5027c478bd9Sstevel@tonic-gate return (tmp);
5037c478bd9Sstevel@tonic-gate }
5047c478bd9Sstevel@tonic-gate
5057c478bd9Sstevel@tonic-gate static void
inline_struct(decl_list * dl,decl_list * last,int flag,int indent)5067c478bd9Sstevel@tonic-gate inline_struct(decl_list *dl, decl_list *last, int flag, int indent)
5077c478bd9Sstevel@tonic-gate {
5087c478bd9Sstevel@tonic-gate int size, tsize;
50961961e0fSrobinson decl_list *cur;
51061961e0fSrobinson char *sizestr;
5117c478bd9Sstevel@tonic-gate
5127c478bd9Sstevel@tonic-gate cur = NULL;
5137c478bd9Sstevel@tonic-gate tsize = 0;
5147c478bd9Sstevel@tonic-gate sizestr = NULL;
5157c478bd9Sstevel@tonic-gate for (; dl != last; dl = dl->next) {
5167c478bd9Sstevel@tonic-gate if (inline_type(&dl->decl, &size)) {
5177c478bd9Sstevel@tonic-gate if (cur == NULL)
5187c478bd9Sstevel@tonic-gate cur = dl;
5197c478bd9Sstevel@tonic-gate
5207c478bd9Sstevel@tonic-gate if (dl->decl.rel == REL_ALIAS)
5217c478bd9Sstevel@tonic-gate tsize += size;
5227c478bd9Sstevel@tonic-gate else {
5237c478bd9Sstevel@tonic-gate /* this code is required to handle arrays */
5247c478bd9Sstevel@tonic-gate sizestr = arraysize(sizestr, &dl->decl, size);
5257c478bd9Sstevel@tonic-gate }
5267c478bd9Sstevel@tonic-gate } else {
5277c478bd9Sstevel@tonic-gate if (cur != NULL)
5287c478bd9Sstevel@tonic-gate if (sizestr == NULL && tsize < inlinelen) {
5297c478bd9Sstevel@tonic-gate /*
5307c478bd9Sstevel@tonic-gate * don't expand into inline code
5317c478bd9Sstevel@tonic-gate * if tsize < inlinelen
5327c478bd9Sstevel@tonic-gate */
5337c478bd9Sstevel@tonic-gate while (cur != dl) {
5347c478bd9Sstevel@tonic-gate print_stat(indent + 1,
5357c478bd9Sstevel@tonic-gate &cur->decl);
5367c478bd9Sstevel@tonic-gate cur = cur->next;
5377c478bd9Sstevel@tonic-gate }
5387c478bd9Sstevel@tonic-gate } else {
5397c478bd9Sstevel@tonic-gate expand_inline(indent, sizestr,
5407c478bd9Sstevel@tonic-gate tsize, flag, dl, cur);
5417c478bd9Sstevel@tonic-gate }
5427c478bd9Sstevel@tonic-gate tsize = 0;
5437c478bd9Sstevel@tonic-gate cur = NULL;
5447c478bd9Sstevel@tonic-gate sizestr = NULL;
5457c478bd9Sstevel@tonic-gate print_stat(indent + 1, &dl->decl);
5467c478bd9Sstevel@tonic-gate }
5477c478bd9Sstevel@tonic-gate }
5487c478bd9Sstevel@tonic-gate
54961961e0fSrobinson if (cur == NULL)
55061961e0fSrobinson return;
5517c478bd9Sstevel@tonic-gate if (sizestr == NULL && tsize < inlinelen) {
5527c478bd9Sstevel@tonic-gate /* don't expand into inline code if tsize < inlinelen */
5537c478bd9Sstevel@tonic-gate while (cur != dl) {
5547c478bd9Sstevel@tonic-gate print_stat(indent + 1, &cur->decl);
5557c478bd9Sstevel@tonic-gate cur = cur->next;
5567c478bd9Sstevel@tonic-gate }
5577c478bd9Sstevel@tonic-gate } else {
5587c478bd9Sstevel@tonic-gate expand_inline(indent, sizestr, tsize, flag, dl, cur);
5597c478bd9Sstevel@tonic-gate }
5607c478bd9Sstevel@tonic-gate }
5617c478bd9Sstevel@tonic-gate
5627c478bd9Sstevel@tonic-gate /*
5637c478bd9Sstevel@tonic-gate * Check if we can inline this structure. While we are at it check if the
5647c478bd9Sstevel@tonic-gate * declaration list has any vectors defined of "basic" types.
5657c478bd9Sstevel@tonic-gate */
5667c478bd9Sstevel@tonic-gate static int
check_inline(decl_list * dl,int inlinelen,int * have_vector)5677c478bd9Sstevel@tonic-gate check_inline(decl_list *dl, int inlinelen, int *have_vector)
5687c478bd9Sstevel@tonic-gate {
5697c478bd9Sstevel@tonic-gate int tsize = 0;
5707c478bd9Sstevel@tonic-gate int size;
5717c478bd9Sstevel@tonic-gate int doinline = 0;
5727c478bd9Sstevel@tonic-gate
5737c478bd9Sstevel@tonic-gate *have_vector = 0;
5747c478bd9Sstevel@tonic-gate if (inlinelen == 0)
5757c478bd9Sstevel@tonic-gate return (0);
5767c478bd9Sstevel@tonic-gate
5777c478bd9Sstevel@tonic-gate for (; dl != NULL; dl = dl->next) {
57861961e0fSrobinson if (!inline_type(&dl->decl, &size)) {
57961961e0fSrobinson tsize = 0;
58061961e0fSrobinson continue;
58161961e0fSrobinson }
5827c478bd9Sstevel@tonic-gate if (dl->decl.rel == REL_VECTOR) {
5837c478bd9Sstevel@tonic-gate *have_vector = 1;
58461961e0fSrobinson return (1);
58561961e0fSrobinson }
5867c478bd9Sstevel@tonic-gate tsize += size;
5877c478bd9Sstevel@tonic-gate if (tsize >= inlinelen)
5887c478bd9Sstevel@tonic-gate doinline = 1;
5897c478bd9Sstevel@tonic-gate }
5907c478bd9Sstevel@tonic-gate
5917c478bd9Sstevel@tonic-gate return (doinline);
5927c478bd9Sstevel@tonic-gate }
5937c478bd9Sstevel@tonic-gate
5947c478bd9Sstevel@tonic-gate
5957c478bd9Sstevel@tonic-gate static void
emit_struct_tail_recursion(definition * defp,int can_inline)5967c478bd9Sstevel@tonic-gate emit_struct_tail_recursion(definition *defp, int can_inline)
5977c478bd9Sstevel@tonic-gate {
5987c478bd9Sstevel@tonic-gate int indent = 3;
5997c478bd9Sstevel@tonic-gate struct_def *sp = &defp->def.st;
6007c478bd9Sstevel@tonic-gate decl_list *dl;
6017c478bd9Sstevel@tonic-gate
6027c478bd9Sstevel@tonic-gate
6037c478bd9Sstevel@tonic-gate f_print(fout, "\t%s *tmp_%s;\n",
6047c478bd9Sstevel@tonic-gate defp->def_name, defp->def_name);
6057c478bd9Sstevel@tonic-gate
6067c478bd9Sstevel@tonic-gate f_print(fout, "\tbool_t more_data = TRUE;\n");
6077c478bd9Sstevel@tonic-gate f_print(fout, "\tbool_t first_objp = TRUE;\n\n");
6087c478bd9Sstevel@tonic-gate
6097c478bd9Sstevel@tonic-gate f_print(fout, "\n\tif (xdrs->x_op == XDR_DECODE) {\n");
6107c478bd9Sstevel@tonic-gate f_print(fout, "\n\t\twhile (more_data) {\n");
6117c478bd9Sstevel@tonic-gate f_print(fout, "\n\t\t\tvoid bzero();\n\n");
6127c478bd9Sstevel@tonic-gate
6137c478bd9Sstevel@tonic-gate if (can_inline)
6147c478bd9Sstevel@tonic-gate inline_struct(sp->decls, sp->tail, GET, indent);
6157c478bd9Sstevel@tonic-gate else
6167c478bd9Sstevel@tonic-gate for (dl = sp->decls; dl != NULL && dl != sp->tail;
6177c478bd9Sstevel@tonic-gate dl = dl->next)
6187c478bd9Sstevel@tonic-gate print_stat(indent, &dl->decl);
6197c478bd9Sstevel@tonic-gate
6207c478bd9Sstevel@tonic-gate f_print(fout, "\t\t\tif (!xdr_bool(xdrs, "
6217c478bd9Sstevel@tonic-gate "&more_data))\n\t\t\t\treturn (FALSE);\n");
6227c478bd9Sstevel@tonic-gate
6237c478bd9Sstevel@tonic-gate f_print(fout, "\n\t\t\tif (!more_data) {\n");
6247c478bd9Sstevel@tonic-gate f_print(fout, "\t\t\t\tobjp->%s = NULL;\n", sp->tail->decl.name);
6257c478bd9Sstevel@tonic-gate f_print(fout, "\t\t\t\tbreak;\n");
6267c478bd9Sstevel@tonic-gate f_print(fout, "\t\t\t}\n\n");
6277c478bd9Sstevel@tonic-gate f_print(fout, "\t\t\tif (objp->%s == NULL) {\n", sp->tail->decl.name);
6287c478bd9Sstevel@tonic-gate f_print(fout, "\t\t\t\tobjp->%s = "
6297c478bd9Sstevel@tonic-gate "(%s *)\n\t\t\t\t\tmem_alloc(sizeof (%s));\n",
6307c478bd9Sstevel@tonic-gate sp->tail->decl.name, defp->def_name, defp->def_name);
6317c478bd9Sstevel@tonic-gate
6327c478bd9Sstevel@tonic-gate f_print(fout, "\t\t\t\tif (objp->%s == NULL)\n"
6337c478bd9Sstevel@tonic-gate "\t\t\t\t\treturn (FALSE);\n", sp->tail->decl.name);
6347c478bd9Sstevel@tonic-gate f_print(fout, "\t\t\t\tbzero(objp->%s, sizeof (%s));\n",
6357c478bd9Sstevel@tonic-gate sp->tail->decl.name, defp->def_name);
6367c478bd9Sstevel@tonic-gate f_print(fout, "\t\t\t}\n");
6377c478bd9Sstevel@tonic-gate f_print(fout, "\t\t\tobjp = objp->%s;\n", sp->tail->decl.name);
6387c478bd9Sstevel@tonic-gate f_print(fout, "\t\t}\n");
6397c478bd9Sstevel@tonic-gate
6407c478bd9Sstevel@tonic-gate f_print(fout, "\n\t} else if (xdrs->x_op == XDR_ENCODE) {\n");
6417c478bd9Sstevel@tonic-gate f_print(fout, "\n\t\twhile (more_data) {\n");
6427c478bd9Sstevel@tonic-gate
6437c478bd9Sstevel@tonic-gate if (can_inline)
6447c478bd9Sstevel@tonic-gate inline_struct(sp->decls, sp->tail, PUT, indent);
6457c478bd9Sstevel@tonic-gate else
6467c478bd9Sstevel@tonic-gate for (dl = sp->decls; dl != NULL && dl != sp->tail;
6477c478bd9Sstevel@tonic-gate dl = dl->next)
6487c478bd9Sstevel@tonic-gate print_stat(indent, &dl->decl);
6497c478bd9Sstevel@tonic-gate
6507c478bd9Sstevel@tonic-gate f_print(fout, "\t\t\tobjp = objp->%s;\n", sp->tail->decl.name);
6517c478bd9Sstevel@tonic-gate f_print(fout, "\t\t\tif (objp == NULL)\n");
6527c478bd9Sstevel@tonic-gate f_print(fout, "\t\t\t\tmore_data = FALSE;\n");
6537c478bd9Sstevel@tonic-gate
6547c478bd9Sstevel@tonic-gate f_print(fout, "\t\t\tif (!xdr_bool(xdrs, &more_data))\n"
6557c478bd9Sstevel@tonic-gate "\t\t\t\treturn (FALSE);\n");
6567c478bd9Sstevel@tonic-gate
6577c478bd9Sstevel@tonic-gate f_print(fout, "\t\t}\n");
6587c478bd9Sstevel@tonic-gate
6597c478bd9Sstevel@tonic-gate f_print(fout, "\n\t} else {\n");
6607c478bd9Sstevel@tonic-gate f_print(fout, "\n\t\twhile (more_data) {\n");
6617c478bd9Sstevel@tonic-gate
6627c478bd9Sstevel@tonic-gate for (dl = sp->decls; dl != NULL && dl != sp->tail; dl = dl->next)
6637c478bd9Sstevel@tonic-gate print_stat(indent, &dl->decl);
6647c478bd9Sstevel@tonic-gate
6657c478bd9Sstevel@tonic-gate f_print(fout, "\t\t\ttmp_%s = objp;\n", defp->def_name);
6667c478bd9Sstevel@tonic-gate f_print(fout, "\t\t\tobjp = objp->%s;\n", sp->tail->decl.name);
6677c478bd9Sstevel@tonic-gate
6687c478bd9Sstevel@tonic-gate f_print(fout, "\t\t\tif (objp == NULL)\n");
6697c478bd9Sstevel@tonic-gate f_print(fout, "\t\t\t\tmore_data = FALSE;\n");
6707c478bd9Sstevel@tonic-gate
6717c478bd9Sstevel@tonic-gate f_print(fout, "\t\t\tif (!first_objp)\n");
6727c478bd9Sstevel@tonic-gate
6737c478bd9Sstevel@tonic-gate f_print(fout, "\t\t\t\tmem_free(tmp_%s, sizeof (%s));\n",
6747c478bd9Sstevel@tonic-gate defp->def_name, defp->def_name);
6757c478bd9Sstevel@tonic-gate
6767c478bd9Sstevel@tonic-gate f_print(fout, "\t\t\telse\n\t\t\t\tfirst_objp = FALSE;\n\t\t}\n");
6777c478bd9Sstevel@tonic-gate
6787c478bd9Sstevel@tonic-gate f_print(fout, "\n\t}\n");
6797c478bd9Sstevel@tonic-gate }
6807c478bd9Sstevel@tonic-gate
68161961e0fSrobinson static void
emit_struct(definition * def)68261961e0fSrobinson emit_struct(definition *def)
6837c478bd9Sstevel@tonic-gate {
6847c478bd9Sstevel@tonic-gate decl_list *dl = def->def.st.decls;
6857c478bd9Sstevel@tonic-gate int can_inline, have_vector;
6867c478bd9Sstevel@tonic-gate
6877c478bd9Sstevel@tonic-gate
6887c478bd9Sstevel@tonic-gate can_inline = check_inline(dl, inlinelen, &have_vector);
6897c478bd9Sstevel@tonic-gate if (have_vector)
6907c478bd9Sstevel@tonic-gate f_print(fout, "\tint i;\n");
6917c478bd9Sstevel@tonic-gate
6927c478bd9Sstevel@tonic-gate
6937c478bd9Sstevel@tonic-gate if (rflag && def->def.st.self_pointer) {
6947c478bd9Sstevel@tonic-gate /* Handle tail recursion elimination */
6957c478bd9Sstevel@tonic-gate emit_struct_tail_recursion(def, can_inline);
6967c478bd9Sstevel@tonic-gate return;
6977c478bd9Sstevel@tonic-gate }
6987c478bd9Sstevel@tonic-gate
6997c478bd9Sstevel@tonic-gate
7007c478bd9Sstevel@tonic-gate if (can_inline) {
7017c478bd9Sstevel@tonic-gate f_print(fout, "\n\tif (xdrs->x_op == XDR_ENCODE) {\n");
7027c478bd9Sstevel@tonic-gate inline_struct(dl, NULL, PUT, 1);
7037c478bd9Sstevel@tonic-gate
7047c478bd9Sstevel@tonic-gate f_print(fout, "\t\treturn (TRUE);\n\t}"
7057c478bd9Sstevel@tonic-gate " else if (xdrs->x_op == XDR_DECODE) {\n");
7067c478bd9Sstevel@tonic-gate
7077c478bd9Sstevel@tonic-gate inline_struct(dl, NULL, GET, 1);
7087c478bd9Sstevel@tonic-gate f_print(fout, "\t\treturn (TRUE);\n\t}\n\n");
7097c478bd9Sstevel@tonic-gate }
7107c478bd9Sstevel@tonic-gate
7117c478bd9Sstevel@tonic-gate /* now take care of XDR_FREE inline case or the non-inline cases */
7127c478bd9Sstevel@tonic-gate
7137c478bd9Sstevel@tonic-gate for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
7147c478bd9Sstevel@tonic-gate print_stat(1, &dl->decl);
7157c478bd9Sstevel@tonic-gate
7167c478bd9Sstevel@tonic-gate }
7177c478bd9Sstevel@tonic-gate
71861961e0fSrobinson static void
emit_typedef(definition * def)71961961e0fSrobinson emit_typedef(definition *def)
7207c478bd9Sstevel@tonic-gate {
7217c478bd9Sstevel@tonic-gate char *prefix = def->def.ty.old_prefix;
7227c478bd9Sstevel@tonic-gate char *type = def->def.ty.old_type;
7237c478bd9Sstevel@tonic-gate char *amax = def->def.ty.array_max;
7247c478bd9Sstevel@tonic-gate relation rel = def->def.ty.rel;
7257c478bd9Sstevel@tonic-gate
7267c478bd9Sstevel@tonic-gate print_ifstat(1, prefix, type, rel, amax, "objp", def->def_name);
7277c478bd9Sstevel@tonic-gate }
7287c478bd9Sstevel@tonic-gate
72961961e0fSrobinson static void
print_stat(int indent,declaration * dec)73061961e0fSrobinson print_stat(int indent, declaration *dec)
7317c478bd9Sstevel@tonic-gate {
7327c478bd9Sstevel@tonic-gate char *prefix = dec->prefix;
7337c478bd9Sstevel@tonic-gate char *type = dec->type;
7347c478bd9Sstevel@tonic-gate char *amax = dec->array_max;
7357c478bd9Sstevel@tonic-gate relation rel = dec->rel;
7367c478bd9Sstevel@tonic-gate char name[256];
7377c478bd9Sstevel@tonic-gate
73861961e0fSrobinson if (isvectordef(type, rel))
73961961e0fSrobinson (void) snprintf(name, sizeof (name), "objp->%s", dec->name);
74061961e0fSrobinson else
74161961e0fSrobinson (void) snprintf(name, sizeof (name), "&objp->%s", dec->name);
7427c478bd9Sstevel@tonic-gate print_ifstat(indent, prefix, type, rel, amax, name, dec->name);
7437c478bd9Sstevel@tonic-gate }
7447c478bd9Sstevel@tonic-gate
7457c478bd9Sstevel@tonic-gate
74661961e0fSrobinson static void
emit_inline(int indent,declaration * decl,int flag)74761961e0fSrobinson emit_inline(int indent, declaration *decl, int flag)
7487c478bd9Sstevel@tonic-gate {
7497c478bd9Sstevel@tonic-gate switch (decl->rel) {
7507c478bd9Sstevel@tonic-gate case REL_ALIAS :
7517c478bd9Sstevel@tonic-gate emit_single_in_line(indent, decl, flag, REL_ALIAS);
7527c478bd9Sstevel@tonic-gate break;
7537c478bd9Sstevel@tonic-gate case REL_VECTOR :
7547c478bd9Sstevel@tonic-gate tabify(fout, indent);
7557c478bd9Sstevel@tonic-gate f_print(fout, "{\n");
7567c478bd9Sstevel@tonic-gate tabify(fout, indent + 1);
75761961e0fSrobinson f_print(fout, "%s *genp;\n\n", decl->type);
7587c478bd9Sstevel@tonic-gate tabify(fout, indent + 1);
7597c478bd9Sstevel@tonic-gate f_print(fout,
7607c478bd9Sstevel@tonic-gate "for (i = 0, genp = objp->%s;\n", decl->name);
7617c478bd9Sstevel@tonic-gate tabify(fout, indent + 2);
7627c478bd9Sstevel@tonic-gate f_print(fout, "i < %s; i++) {\n", decl->array_max);
7637c478bd9Sstevel@tonic-gate emit_single_in_line(indent + 2, decl, flag, REL_VECTOR);
7647c478bd9Sstevel@tonic-gate tabify(fout, indent + 1);
7657c478bd9Sstevel@tonic-gate f_print(fout, "}\n");
7667c478bd9Sstevel@tonic-gate tabify(fout, indent);
7677c478bd9Sstevel@tonic-gate f_print(fout, "}\n");
7687c478bd9Sstevel@tonic-gate }
7697c478bd9Sstevel@tonic-gate }
7707c478bd9Sstevel@tonic-gate
77161961e0fSrobinson static void
emit_inline64(int indent,declaration * decl,int flag)77261961e0fSrobinson emit_inline64(int indent, declaration *decl, int flag)
7737c478bd9Sstevel@tonic-gate {
7747c478bd9Sstevel@tonic-gate switch (decl->rel) {
7757c478bd9Sstevel@tonic-gate case REL_ALIAS :
7767c478bd9Sstevel@tonic-gate emit_single_in_line64(indent, decl, flag, REL_ALIAS);
7777c478bd9Sstevel@tonic-gate break;
7787c478bd9Sstevel@tonic-gate case REL_VECTOR :
7797c478bd9Sstevel@tonic-gate tabify(fout, indent);
7807c478bd9Sstevel@tonic-gate f_print(fout, "{\n");
7817c478bd9Sstevel@tonic-gate tabify(fout, indent + 1);
78261961e0fSrobinson f_print(fout, "%s *genp;\n\n", decl->type);
7837c478bd9Sstevel@tonic-gate tabify(fout, indent + 1);
7847c478bd9Sstevel@tonic-gate f_print(fout,
7857c478bd9Sstevel@tonic-gate "for (i = 0, genp = objp->%s;\n", decl->name);
7867c478bd9Sstevel@tonic-gate tabify(fout, indent + 2);
7877c478bd9Sstevel@tonic-gate f_print(fout, "i < %s; i++) {\n", decl->array_max);
7887c478bd9Sstevel@tonic-gate emit_single_in_line64(indent + 2, decl, flag, REL_VECTOR);
7897c478bd9Sstevel@tonic-gate tabify(fout, indent + 1);
7907c478bd9Sstevel@tonic-gate f_print(fout, "}\n");
7917c478bd9Sstevel@tonic-gate tabify(fout, indent);
7927c478bd9Sstevel@tonic-gate f_print(fout, "}\n");
7937c478bd9Sstevel@tonic-gate }
7947c478bd9Sstevel@tonic-gate }
7957c478bd9Sstevel@tonic-gate
79661961e0fSrobinson static void
emit_single_in_line(int indent,declaration * decl,int flag,relation rel)79761961e0fSrobinson emit_single_in_line(int indent, declaration *decl, int flag, relation rel)
7987c478bd9Sstevel@tonic-gate {
7997c478bd9Sstevel@tonic-gate char *upp_case;
8007c478bd9Sstevel@tonic-gate int freed = 0;
8017c478bd9Sstevel@tonic-gate
8027c478bd9Sstevel@tonic-gate tabify(fout, indent);
8037c478bd9Sstevel@tonic-gate if (flag == PUT)
8047c478bd9Sstevel@tonic-gate f_print(fout, "IXDR_PUT_");
8057c478bd9Sstevel@tonic-gate else
8067c478bd9Sstevel@tonic-gate if (rel == REL_ALIAS)
8077c478bd9Sstevel@tonic-gate f_print(fout, "objp->%s = IXDR_GET_", decl->name);
8087c478bd9Sstevel@tonic-gate else
8097c478bd9Sstevel@tonic-gate f_print(fout, "*genp++ = IXDR_GET_");
8107c478bd9Sstevel@tonic-gate
8117c478bd9Sstevel@tonic-gate upp_case = upcase(decl->type);
8127c478bd9Sstevel@tonic-gate
8137c478bd9Sstevel@tonic-gate /* hack - XX */
81461961e0fSrobinson if (strcmp(upp_case, "INT") == 0) {
8157c478bd9Sstevel@tonic-gate free(upp_case);
8167c478bd9Sstevel@tonic-gate freed = 1;
8177c478bd9Sstevel@tonic-gate upp_case = "LONG";
8187c478bd9Sstevel@tonic-gate }
8197c478bd9Sstevel@tonic-gate if ((strcmp(upp_case, "U_INT") == 0) ||
8207c478bd9Sstevel@tonic-gate (strcmp(upp_case, "RPCPROG") == 0) ||
8217c478bd9Sstevel@tonic-gate (strcmp(upp_case, "RPCVERS") == 0) ||
8227c478bd9Sstevel@tonic-gate (strcmp(upp_case, "RPCPROC") == 0) ||
8237c478bd9Sstevel@tonic-gate (strcmp(upp_case, "RPCPROT") == 0) ||
82461961e0fSrobinson (strcmp(upp_case, "RPCPORT") == 0)) {
8257c478bd9Sstevel@tonic-gate free(upp_case);
8267c478bd9Sstevel@tonic-gate freed = 1;
8277c478bd9Sstevel@tonic-gate upp_case = "U_LONG";
8287c478bd9Sstevel@tonic-gate }
8297c478bd9Sstevel@tonic-gate
8307c478bd9Sstevel@tonic-gate if (flag == PUT)
8317c478bd9Sstevel@tonic-gate if (rel == REL_ALIAS)
8327c478bd9Sstevel@tonic-gate f_print(fout,
8337c478bd9Sstevel@tonic-gate "%s(buf, objp->%s);\n", upp_case, decl->name);
8347c478bd9Sstevel@tonic-gate else
8357c478bd9Sstevel@tonic-gate f_print(fout, "%s(buf, *genp++);\n", upp_case);
8367c478bd9Sstevel@tonic-gate
8377c478bd9Sstevel@tonic-gate else
8387c478bd9Sstevel@tonic-gate f_print(fout, "%s(buf);\n", upp_case);
8397c478bd9Sstevel@tonic-gate if (!freed)
8407c478bd9Sstevel@tonic-gate free(upp_case);
8417c478bd9Sstevel@tonic-gate }
8427c478bd9Sstevel@tonic-gate
84361961e0fSrobinson static void
emit_single_in_line64(int indent,declaration * decl,int flag,relation rel)84461961e0fSrobinson emit_single_in_line64(int indent, declaration *decl, int flag, relation rel)
8457c478bd9Sstevel@tonic-gate {
8467c478bd9Sstevel@tonic-gate char *upp_case;
8477c478bd9Sstevel@tonic-gate int freed = 0;
8487c478bd9Sstevel@tonic-gate
8497c478bd9Sstevel@tonic-gate tabify(fout, indent);
8507c478bd9Sstevel@tonic-gate if (flag == PUT)
8517c478bd9Sstevel@tonic-gate f_print(fout, "IXDR_PUT_");
8527c478bd9Sstevel@tonic-gate else
8537c478bd9Sstevel@tonic-gate if (rel == REL_ALIAS)
8547c478bd9Sstevel@tonic-gate f_print(fout, "objp->%s = IXDR_GET_", decl->name);
8557c478bd9Sstevel@tonic-gate else
8567c478bd9Sstevel@tonic-gate f_print(fout, "*genp++ = IXDR_GET_");
8577c478bd9Sstevel@tonic-gate
8587c478bd9Sstevel@tonic-gate upp_case = upcase(decl->type);
8597c478bd9Sstevel@tonic-gate
8607c478bd9Sstevel@tonic-gate /* hack - XX */
86161961e0fSrobinson if ((strcmp(upp_case, "INT") == 0)||(strcmp(upp_case, "LONG") == 0)) {
8627c478bd9Sstevel@tonic-gate free(upp_case);
8637c478bd9Sstevel@tonic-gate freed = 1;
8647c478bd9Sstevel@tonic-gate upp_case = "INT32";
8657c478bd9Sstevel@tonic-gate }
8667c478bd9Sstevel@tonic-gate if ((strcmp(upp_case, "U_INT") == 0) ||
8677c478bd9Sstevel@tonic-gate (strcmp(upp_case, "U_LONG") == 0) ||
8687c478bd9Sstevel@tonic-gate (strcmp(upp_case, "RPCPROG") == 0) ||
8697c478bd9Sstevel@tonic-gate (strcmp(upp_case, "RPCVERS") == 0) ||
8707c478bd9Sstevel@tonic-gate (strcmp(upp_case, "RPCPROC") == 0) ||
8717c478bd9Sstevel@tonic-gate (strcmp(upp_case, "RPCPROT") == 0) ||
87261961e0fSrobinson (strcmp(upp_case, "RPCPORT") == 0)) {
8737c478bd9Sstevel@tonic-gate free(upp_case);
8747c478bd9Sstevel@tonic-gate freed = 1;
8757c478bd9Sstevel@tonic-gate upp_case = "U_INT32";
8767c478bd9Sstevel@tonic-gate }
8777c478bd9Sstevel@tonic-gate
8787c478bd9Sstevel@tonic-gate if (flag == PUT)
8797c478bd9Sstevel@tonic-gate if (rel == REL_ALIAS)
8807c478bd9Sstevel@tonic-gate f_print(fout,
8817c478bd9Sstevel@tonic-gate "%s(buf, objp->%s);\n", upp_case, decl->name);
8827c478bd9Sstevel@tonic-gate else
8837c478bd9Sstevel@tonic-gate f_print(fout, "%s(buf, *genp++);\n", upp_case);
8847c478bd9Sstevel@tonic-gate
8857c478bd9Sstevel@tonic-gate else
8867c478bd9Sstevel@tonic-gate f_print(fout, "%s(buf);\n", upp_case);
8877c478bd9Sstevel@tonic-gate if (!freed)
8887c478bd9Sstevel@tonic-gate free(upp_case);
8897c478bd9Sstevel@tonic-gate }
8907c478bd9Sstevel@tonic-gate
89161961e0fSrobinson static char *
upcase(char * str)89261961e0fSrobinson upcase(char *str)
8937c478bd9Sstevel@tonic-gate {
8947c478bd9Sstevel@tonic-gate char *ptr, *hptr;
8957c478bd9Sstevel@tonic-gate
89661961e0fSrobinson ptr = malloc(strlen(str)+1);
89761961e0fSrobinson if (ptr == NULL) {
8987c478bd9Sstevel@tonic-gate f_print(stderr, "malloc failed\n");
8997c478bd9Sstevel@tonic-gate exit(1);
9007c478bd9Sstevel@tonic-gate };
9017c478bd9Sstevel@tonic-gate
9027c478bd9Sstevel@tonic-gate hptr = ptr;
9037c478bd9Sstevel@tonic-gate while (*str != '\0')
9047c478bd9Sstevel@tonic-gate *ptr++ = toupper(*str++);
9057c478bd9Sstevel@tonic-gate
9067c478bd9Sstevel@tonic-gate *ptr = '\0';
9077c478bd9Sstevel@tonic-gate return (hptr);
9087c478bd9Sstevel@tonic-gate }
909