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 57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 77c478bd9Sstevel@tonic-gate * with the License. 87c478bd9Sstevel@tonic-gate * 97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 127c478bd9Sstevel@tonic-gate * and limitations under the License. 137c478bd9Sstevel@tonic-gate * 147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 197c478bd9Sstevel@tonic-gate * 207c478bd9Sstevel@tonic-gate * CDDL HEADER END 217c478bd9Sstevel@tonic-gate */ 22*61961e0fSrobinson 237c478bd9Sstevel@tonic-gate /* 247c478bd9Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 257c478bd9Sstevel@tonic-gate * Use is subject to license terms. 267c478bd9Sstevel@tonic-gate */ 277c478bd9Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */ 287c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 297c478bd9Sstevel@tonic-gate /* 307c478bd9Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988 317c478bd9Sstevel@tonic-gate * The Regents of the University of California 327c478bd9Sstevel@tonic-gate * All Rights Reserved 337c478bd9Sstevel@tonic-gate * 347c478bd9Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from 357c478bd9Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its 367c478bd9Sstevel@tonic-gate * contributors. 377c478bd9Sstevel@tonic-gate */ 387c478bd9Sstevel@tonic-gate 397c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 407c478bd9Sstevel@tonic-gate 417c478bd9Sstevel@tonic-gate /* 427c478bd9Sstevel@tonic-gate * rpc_cout.c, XDR routine outputter for the RPC protocol compiler 437c478bd9Sstevel@tonic-gate */ 447c478bd9Sstevel@tonic-gate #include <stdio.h> 45*61961e0fSrobinson #include <stdlib.h> 467c478bd9Sstevel@tonic-gate #include <string.h> 47*61961e0fSrobinson #include <ctype.h> 487c478bd9Sstevel@tonic-gate #include "rpc_parse.h" 497c478bd9Sstevel@tonic-gate #include "rpc_util.h" 507c478bd9Sstevel@tonic-gate 51*61961e0fSrobinson extern void crash(void); 52*61961e0fSrobinson 53*61961e0fSrobinson static void print_header(definition *); 54*61961e0fSrobinson static void print_trailer(void); 55*61961e0fSrobinson static void emit_enum(definition *); 56*61961e0fSrobinson static void emit_program(definition *); 57*61961e0fSrobinson static void emit_union(definition *); 58*61961e0fSrobinson static void emit_struct(definition *); 59*61961e0fSrobinson static void emit_typedef(definition *); 60*61961e0fSrobinson static void print_stat(int, declaration *); 61*61961e0fSrobinson static void emit_inline(int, declaration *, int); 62*61961e0fSrobinson static void emit_inline64(int, declaration *, int); 63*61961e0fSrobinson static void emit_single_in_line(int, declaration *, int, relation); 64*61961e0fSrobinson static void emit_single_in_line64(int, declaration *, int, relation); 65*61961e0fSrobinson static char *upcase(char *); 66*61961e0fSrobinson 677c478bd9Sstevel@tonic-gate /* 687c478bd9Sstevel@tonic-gate * Emit the C-routine for the given definition 697c478bd9Sstevel@tonic-gate */ 707c478bd9Sstevel@tonic-gate void 71*61961e0fSrobinson emit(definition *def) 727c478bd9Sstevel@tonic-gate { 73*61961e0fSrobinson if (def->def_kind == DEF_CONST) 747c478bd9Sstevel@tonic-gate return; 757c478bd9Sstevel@tonic-gate if (def->def_kind == DEF_PROGRAM) { 767c478bd9Sstevel@tonic-gate emit_program(def); 777c478bd9Sstevel@tonic-gate return; 787c478bd9Sstevel@tonic-gate } 797c478bd9Sstevel@tonic-gate if (def->def_kind == DEF_TYPEDEF) { 807c478bd9Sstevel@tonic-gate /* 817c478bd9Sstevel@tonic-gate * now we need to handle declarations like 827c478bd9Sstevel@tonic-gate * struct typedef foo foo; 837c478bd9Sstevel@tonic-gate * since we dont want this to be expanded into 2 calls 847c478bd9Sstevel@tonic-gate * to xdr_foo 857c478bd9Sstevel@tonic-gate */ 867c478bd9Sstevel@tonic-gate 877c478bd9Sstevel@tonic-gate if (strcmp(def->def.ty.old_type, def->def_name) == 0) 887c478bd9Sstevel@tonic-gate return; 897c478bd9Sstevel@tonic-gate }; 907c478bd9Sstevel@tonic-gate print_header(def); 917c478bd9Sstevel@tonic-gate switch (def->def_kind) { 927c478bd9Sstevel@tonic-gate case DEF_UNION: 937c478bd9Sstevel@tonic-gate emit_union(def); 947c478bd9Sstevel@tonic-gate break; 957c478bd9Sstevel@tonic-gate case DEF_ENUM: 967c478bd9Sstevel@tonic-gate emit_enum(def); 977c478bd9Sstevel@tonic-gate break; 987c478bd9Sstevel@tonic-gate case DEF_STRUCT: 997c478bd9Sstevel@tonic-gate emit_struct(def); 1007c478bd9Sstevel@tonic-gate break; 1017c478bd9Sstevel@tonic-gate case DEF_TYPEDEF: 1027c478bd9Sstevel@tonic-gate emit_typedef(def); 1037c478bd9Sstevel@tonic-gate break; 1047c478bd9Sstevel@tonic-gate } 1057c478bd9Sstevel@tonic-gate print_trailer(); 1067c478bd9Sstevel@tonic-gate } 1077c478bd9Sstevel@tonic-gate 108*61961e0fSrobinson static int 109*61961e0fSrobinson findtype(definition *def, char *type) 1107c478bd9Sstevel@tonic-gate { 1117c478bd9Sstevel@tonic-gate 112*61961e0fSrobinson if (def->def_kind == DEF_PROGRAM || def->def_kind == DEF_CONST) 1137c478bd9Sstevel@tonic-gate return (0); 1147c478bd9Sstevel@tonic-gate return (streq(def->def_name, type)); 1157c478bd9Sstevel@tonic-gate } 1167c478bd9Sstevel@tonic-gate 117*61961e0fSrobinson static int 118*61961e0fSrobinson undefined(char *type) 1197c478bd9Sstevel@tonic-gate { 1207c478bd9Sstevel@tonic-gate definition *def; 1217c478bd9Sstevel@tonic-gate 1227c478bd9Sstevel@tonic-gate def = (definition *)FINDVAL(defined, type, findtype); 1237c478bd9Sstevel@tonic-gate return (def == NULL); 1247c478bd9Sstevel@tonic-gate } 1257c478bd9Sstevel@tonic-gate 1267c478bd9Sstevel@tonic-gate 127*61961e0fSrobinson static void 128*61961e0fSrobinson print_generic_header(char *procname, int pointerp) 1297c478bd9Sstevel@tonic-gate { 1307c478bd9Sstevel@tonic-gate f_print(fout, "\n"); 1317c478bd9Sstevel@tonic-gate f_print(fout, "bool_t\n"); 1327c478bd9Sstevel@tonic-gate if (Cflag) { 1337c478bd9Sstevel@tonic-gate f_print(fout, "xdr_%s(", procname); 134*61961e0fSrobinson f_print(fout, "XDR *xdrs, "); 1357c478bd9Sstevel@tonic-gate f_print(fout, "%s ", procname); 1367c478bd9Sstevel@tonic-gate if (pointerp) 1377c478bd9Sstevel@tonic-gate f_print(fout, "*"); 1387c478bd9Sstevel@tonic-gate f_print(fout, "objp)\n{\n\n"); 1397c478bd9Sstevel@tonic-gate } else { 1407c478bd9Sstevel@tonic-gate f_print(fout, "xdr_%s(xdrs, objp)\n", procname); 141*61961e0fSrobinson f_print(fout, "\tXDR *xdrs;\n"); 1427c478bd9Sstevel@tonic-gate f_print(fout, "\t%s ", procname); 1437c478bd9Sstevel@tonic-gate if (pointerp) 1447c478bd9Sstevel@tonic-gate f_print(fout, "*"); 1457c478bd9Sstevel@tonic-gate f_print(fout, "objp;\n{\n\n"); 1467c478bd9Sstevel@tonic-gate } 1477c478bd9Sstevel@tonic-gate } 1487c478bd9Sstevel@tonic-gate 149*61961e0fSrobinson static void 150*61961e0fSrobinson print_header(definition *def) 1517c478bd9Sstevel@tonic-gate { 1527c478bd9Sstevel@tonic-gate print_generic_header(def->def_name, 1537c478bd9Sstevel@tonic-gate def->def_kind != DEF_TYPEDEF || 1547c478bd9Sstevel@tonic-gate !isvectordef(def->def.ty.old_type, 1557c478bd9Sstevel@tonic-gate def->def.ty.rel)); 1567c478bd9Sstevel@tonic-gate /* Now add Inline support */ 1577c478bd9Sstevel@tonic-gate 1587c478bd9Sstevel@tonic-gate if (inlinelen == 0) 1597c478bd9Sstevel@tonic-gate return; 1607c478bd9Sstevel@tonic-gate /* May cause lint to complain. but ... */ 161*61961e0fSrobinson f_print(fout, "\trpc_inline_t *buf;\n\n"); 1627c478bd9Sstevel@tonic-gate } 1637c478bd9Sstevel@tonic-gate 164*61961e0fSrobinson static void 165*61961e0fSrobinson print_prog_header(proc_list *plist) 1667c478bd9Sstevel@tonic-gate { 1677c478bd9Sstevel@tonic-gate print_generic_header(plist->args.argname, 1); 1687c478bd9Sstevel@tonic-gate } 1697c478bd9Sstevel@tonic-gate 170*61961e0fSrobinson static void 171*61961e0fSrobinson print_trailer(void) 1727c478bd9Sstevel@tonic-gate { 1737c478bd9Sstevel@tonic-gate f_print(fout, "\treturn (TRUE);\n"); 1747c478bd9Sstevel@tonic-gate f_print(fout, "}\n"); 1757c478bd9Sstevel@tonic-gate } 1767c478bd9Sstevel@tonic-gate 1777c478bd9Sstevel@tonic-gate 178*61961e0fSrobinson static void 179*61961e0fSrobinson print_ifopen(int indent, char *name) 1807c478bd9Sstevel@tonic-gate { 1817c478bd9Sstevel@tonic-gate tabify(fout, indent); 1827c478bd9Sstevel@tonic-gate if (streq(name, "rpcprog_t") || 1837c478bd9Sstevel@tonic-gate streq(name, "rpcvers_t") || 1847c478bd9Sstevel@tonic-gate streq(name, "rpcproc_t") || 1857c478bd9Sstevel@tonic-gate streq(name, "rpcprot_t") || 1867c478bd9Sstevel@tonic-gate streq(name, "rpcport_t")) 187*61961e0fSrobinson (void) strtok(name, "_"); 1887c478bd9Sstevel@tonic-gate f_print(fout, "if (!xdr_%s(xdrs", name); 1897c478bd9Sstevel@tonic-gate } 1907c478bd9Sstevel@tonic-gate 191*61961e0fSrobinson static void 192*61961e0fSrobinson print_ifarg(char *arg) 1937c478bd9Sstevel@tonic-gate { 1947c478bd9Sstevel@tonic-gate f_print(fout, ", %s", arg); 1957c478bd9Sstevel@tonic-gate } 1967c478bd9Sstevel@tonic-gate 197*61961e0fSrobinson static void 198*61961e0fSrobinson print_ifsizeof(int indent, char *prefix, char *type) 1997c478bd9Sstevel@tonic-gate { 2007c478bd9Sstevel@tonic-gate if (indent) { 2017c478bd9Sstevel@tonic-gate f_print(fout, ",\n"); 2027c478bd9Sstevel@tonic-gate tabify(fout, indent); 2037c478bd9Sstevel@tonic-gate } else { 2047c478bd9Sstevel@tonic-gate f_print(fout, ", "); 2057c478bd9Sstevel@tonic-gate } 2067c478bd9Sstevel@tonic-gate if (streq(type, "bool")) { 2077c478bd9Sstevel@tonic-gate f_print(fout, "sizeof (bool_t), (xdrproc_t)xdr_bool"); 2087c478bd9Sstevel@tonic-gate } else { 2097c478bd9Sstevel@tonic-gate f_print(fout, "sizeof ("); 2107c478bd9Sstevel@tonic-gate if (undefined(type) && prefix) { 2117c478bd9Sstevel@tonic-gate f_print(fout, "%s ", prefix); 2127c478bd9Sstevel@tonic-gate } 2137c478bd9Sstevel@tonic-gate f_print(fout, "%s), (xdrproc_t)xdr_%s", type, type); 2147c478bd9Sstevel@tonic-gate } 2157c478bd9Sstevel@tonic-gate } 2167c478bd9Sstevel@tonic-gate 217*61961e0fSrobinson static void 218*61961e0fSrobinson print_ifclose(int indent) 2197c478bd9Sstevel@tonic-gate { 2207c478bd9Sstevel@tonic-gate f_print(fout, "))\n"); 2217c478bd9Sstevel@tonic-gate tabify(fout, indent); 2227c478bd9Sstevel@tonic-gate f_print(fout, "\treturn (FALSE);\n"); 2237c478bd9Sstevel@tonic-gate } 2247c478bd9Sstevel@tonic-gate 225*61961e0fSrobinson static void 226*61961e0fSrobinson print_ifstat(int indent, char *prefix, char *type, relation rel, 227*61961e0fSrobinson char *amax, char *objname, char *name) 2287c478bd9Sstevel@tonic-gate { 2297c478bd9Sstevel@tonic-gate char *alt = NULL; 2307c478bd9Sstevel@tonic-gate 2317c478bd9Sstevel@tonic-gate switch (rel) { 2327c478bd9Sstevel@tonic-gate case REL_POINTER: 2337c478bd9Sstevel@tonic-gate print_ifopen(indent, "pointer"); 2347c478bd9Sstevel@tonic-gate print_ifarg("(char **)"); 2357c478bd9Sstevel@tonic-gate f_print(fout, "%s", objname); 2367c478bd9Sstevel@tonic-gate print_ifsizeof(0, prefix, type); 2377c478bd9Sstevel@tonic-gate break; 2387c478bd9Sstevel@tonic-gate case REL_VECTOR: 239*61961e0fSrobinson if (streq(type, "string")) 2407c478bd9Sstevel@tonic-gate alt = "string"; 241*61961e0fSrobinson else if (streq(type, "opaque")) 2427c478bd9Sstevel@tonic-gate alt = "opaque"; 2437c478bd9Sstevel@tonic-gate if (alt) { 2447c478bd9Sstevel@tonic-gate print_ifopen(indent, alt); 2457c478bd9Sstevel@tonic-gate print_ifarg(objname); 2467c478bd9Sstevel@tonic-gate } else { 2477c478bd9Sstevel@tonic-gate print_ifopen(indent, "vector"); 2487c478bd9Sstevel@tonic-gate print_ifarg("(char *)"); 2497c478bd9Sstevel@tonic-gate f_print(fout, "%s", objname); 2507c478bd9Sstevel@tonic-gate } 2517c478bd9Sstevel@tonic-gate print_ifarg(amax); 252*61961e0fSrobinson if (!alt) 2537c478bd9Sstevel@tonic-gate print_ifsizeof(indent + 1, prefix, type); 2547c478bd9Sstevel@tonic-gate break; 2557c478bd9Sstevel@tonic-gate case REL_ARRAY: 256*61961e0fSrobinson if (streq(type, "string")) 2577c478bd9Sstevel@tonic-gate alt = "string"; 258*61961e0fSrobinson else if (streq(type, "opaque")) 2597c478bd9Sstevel@tonic-gate alt = "bytes"; 2607c478bd9Sstevel@tonic-gate if (streq(type, "string")) { 2617c478bd9Sstevel@tonic-gate print_ifopen(indent, alt); 2627c478bd9Sstevel@tonic-gate print_ifarg(objname); 2637c478bd9Sstevel@tonic-gate } else { 264*61961e0fSrobinson if (alt) 2657c478bd9Sstevel@tonic-gate print_ifopen(indent, alt); 266*61961e0fSrobinson else 2677c478bd9Sstevel@tonic-gate print_ifopen(indent, "array"); 2687c478bd9Sstevel@tonic-gate print_ifarg("(char **)"); 269*61961e0fSrobinson if (*objname == '&') 2707c478bd9Sstevel@tonic-gate f_print(fout, "%s.%s_val, (u_int *) %s.%s_len", 2717c478bd9Sstevel@tonic-gate objname, name, objname, name); 272*61961e0fSrobinson else 2737c478bd9Sstevel@tonic-gate f_print(fout, 2747c478bd9Sstevel@tonic-gate "&%s->%s_val, (u_int *) &%s->%s_len", 2757c478bd9Sstevel@tonic-gate objname, name, objname, name); 2767c478bd9Sstevel@tonic-gate } 2777c478bd9Sstevel@tonic-gate print_ifarg(amax); 278*61961e0fSrobinson if (!alt) 2797c478bd9Sstevel@tonic-gate print_ifsizeof(indent + 1, prefix, type); 2807c478bd9Sstevel@tonic-gate break; 2817c478bd9Sstevel@tonic-gate case REL_ALIAS: 2827c478bd9Sstevel@tonic-gate print_ifopen(indent, type); 2837c478bd9Sstevel@tonic-gate print_ifarg(objname); 2847c478bd9Sstevel@tonic-gate break; 2857c478bd9Sstevel@tonic-gate } 2867c478bd9Sstevel@tonic-gate print_ifclose(indent); 2877c478bd9Sstevel@tonic-gate } 2887c478bd9Sstevel@tonic-gate 2897c478bd9Sstevel@tonic-gate /* ARGSUSED */ 290*61961e0fSrobinson static void 291*61961e0fSrobinson emit_enum(definition *def) 2927c478bd9Sstevel@tonic-gate { 2937c478bd9Sstevel@tonic-gate print_ifopen(1, "enum"); 2947c478bd9Sstevel@tonic-gate print_ifarg("(enum_t *)objp"); 2957c478bd9Sstevel@tonic-gate print_ifclose(1); 2967c478bd9Sstevel@tonic-gate } 2977c478bd9Sstevel@tonic-gate 298*61961e0fSrobinson static void 299*61961e0fSrobinson emit_program(definition *def) 3007c478bd9Sstevel@tonic-gate { 3017c478bd9Sstevel@tonic-gate decl_list *dl; 3027c478bd9Sstevel@tonic-gate version_list *vlist; 3037c478bd9Sstevel@tonic-gate proc_list *plist; 3047c478bd9Sstevel@tonic-gate 3057c478bd9Sstevel@tonic-gate for (vlist = def->def.pr.versions; vlist != NULL; vlist = vlist->next) 3067c478bd9Sstevel@tonic-gate for (plist = vlist->procs; plist != NULL; plist = plist->next) { 3077c478bd9Sstevel@tonic-gate if (!newstyle || plist->arg_num < 2) 3087c478bd9Sstevel@tonic-gate continue; /* old style, or single argument */ 3097c478bd9Sstevel@tonic-gate print_prog_header(plist); 3107c478bd9Sstevel@tonic-gate for (dl = plist->args.decls; dl != NULL; 3117c478bd9Sstevel@tonic-gate dl = dl->next) 3127c478bd9Sstevel@tonic-gate print_stat(1, &dl->decl); 3137c478bd9Sstevel@tonic-gate print_trailer(); 3147c478bd9Sstevel@tonic-gate } 3157c478bd9Sstevel@tonic-gate } 3167c478bd9Sstevel@tonic-gate 3177c478bd9Sstevel@tonic-gate 318*61961e0fSrobinson static void 319*61961e0fSrobinson emit_union(definition *def) 3207c478bd9Sstevel@tonic-gate { 3217c478bd9Sstevel@tonic-gate declaration *dflt; 3227c478bd9Sstevel@tonic-gate case_list *cl; 3237c478bd9Sstevel@tonic-gate declaration *cs; 3247c478bd9Sstevel@tonic-gate char *object; 3257c478bd9Sstevel@tonic-gate 3267c478bd9Sstevel@tonic-gate print_stat(1, &def->def.un.enum_decl); 3277c478bd9Sstevel@tonic-gate f_print(fout, "\tswitch (objp->%s) {\n", def->def.un.enum_decl.name); 3287c478bd9Sstevel@tonic-gate for (cl = def->def.un.cases; cl != NULL; cl = cl->next) { 3297c478bd9Sstevel@tonic-gate 3307c478bd9Sstevel@tonic-gate f_print(fout, "\tcase %s:\n", cl->case_name); 3317c478bd9Sstevel@tonic-gate if (cl->contflag == 1) /* a continued case statement */ 3327c478bd9Sstevel@tonic-gate continue; 3337c478bd9Sstevel@tonic-gate cs = &cl->case_decl; 3347c478bd9Sstevel@tonic-gate if (!streq(cs->type, "void")) { 335*61961e0fSrobinson size_t len = strlen(def->def_name) + 336*61961e0fSrobinson strlen("&objp->%s_u.%s") + 337*61961e0fSrobinson strlen(cs->name) + 1; 338*61961e0fSrobinson object = malloc(len); 339*61961e0fSrobinson if (isvectordef(cs->type, cs->rel)) 340*61961e0fSrobinson (void) snprintf(object, len, "objp->%s_u.%s", 341*61961e0fSrobinson def->def_name, cs->name); 342*61961e0fSrobinson else 343*61961e0fSrobinson (void) snprintf(object, len, "&objp->%s_u.%s", 344*61961e0fSrobinson def->def_name, cs->name); 3457c478bd9Sstevel@tonic-gate print_ifstat(2, cs->prefix, cs->type, cs->rel, 3467c478bd9Sstevel@tonic-gate cs->array_max, object, cs->name); 3477c478bd9Sstevel@tonic-gate free(object); 3487c478bd9Sstevel@tonic-gate } 3497c478bd9Sstevel@tonic-gate f_print(fout, "\t\tbreak;\n"); 3507c478bd9Sstevel@tonic-gate } 3517c478bd9Sstevel@tonic-gate dflt = def->def.un.default_decl; 3527c478bd9Sstevel@tonic-gate if (dflt != NULL) { 3537c478bd9Sstevel@tonic-gate if (!streq(dflt->type, "void")) { 354*61961e0fSrobinson size_t len = strlen(def->def_name) + 355*61961e0fSrobinson strlen("&objp->%s_u.%s") + 356*61961e0fSrobinson strlen(dflt->name) + 1; 3577c478bd9Sstevel@tonic-gate f_print(fout, "\tdefault:\n"); 358*61961e0fSrobinson object = malloc(len); 359*61961e0fSrobinson if (isvectordef(dflt->type, dflt->rel)) 360*61961e0fSrobinson (void) snprintf(object, len, "objp->%s_u.%s", 361*61961e0fSrobinson def->def_name, dflt->name); 362*61961e0fSrobinson else 363*61961e0fSrobinson (void) snprintf(object, len, "&objp->%s_u.%s", 364*61961e0fSrobinson def->def_name, dflt->name); 3657c478bd9Sstevel@tonic-gate 3667c478bd9Sstevel@tonic-gate print_ifstat(2, dflt->prefix, dflt->type, dflt->rel, 3677c478bd9Sstevel@tonic-gate dflt->array_max, object, dflt->name); 3687c478bd9Sstevel@tonic-gate free(object); 3697c478bd9Sstevel@tonic-gate f_print(fout, "\t\tbreak;\n"); 3707c478bd9Sstevel@tonic-gate } 3717c478bd9Sstevel@tonic-gate } else { 3727c478bd9Sstevel@tonic-gate f_print(fout, "\tdefault:\n"); 3737c478bd9Sstevel@tonic-gate f_print(fout, "\t\treturn (FALSE);\n"); 3747c478bd9Sstevel@tonic-gate } 3757c478bd9Sstevel@tonic-gate 3767c478bd9Sstevel@tonic-gate f_print(fout, "\t}\n"); 3777c478bd9Sstevel@tonic-gate } 3787c478bd9Sstevel@tonic-gate 3797c478bd9Sstevel@tonic-gate static void 3807c478bd9Sstevel@tonic-gate expand_inline(int indent, const char *sizestr, 3817c478bd9Sstevel@tonic-gate int size, int flag, decl_list *dl, decl_list *cur) 3827c478bd9Sstevel@tonic-gate { 3837c478bd9Sstevel@tonic-gate decl_list *psav; 3847c478bd9Sstevel@tonic-gate 3857c478bd9Sstevel@tonic-gate /* 3867c478bd9Sstevel@tonic-gate * were already looking at a xdr_inlineable structure 3877c478bd9Sstevel@tonic-gate */ 3887c478bd9Sstevel@tonic-gate tabify(fout, indent + 1); 3897c478bd9Sstevel@tonic-gate if (sizestr == NULL) 3907c478bd9Sstevel@tonic-gate f_print(fout, 3917c478bd9Sstevel@tonic-gate "buf = XDR_INLINE(xdrs, %d * BYTES_PER_XDR_UNIT);", 3927c478bd9Sstevel@tonic-gate size); 3937c478bd9Sstevel@tonic-gate else if (size == 0) 3947c478bd9Sstevel@tonic-gate f_print(fout, 3957c478bd9Sstevel@tonic-gate "buf = XDR_INLINE(xdrs, (%s) * BYTES_PER_XDR_UNIT);", 3967c478bd9Sstevel@tonic-gate sizestr); 3977c478bd9Sstevel@tonic-gate else 3987c478bd9Sstevel@tonic-gate f_print(fout, 3997c478bd9Sstevel@tonic-gate "buf = XDR_INLINE(xdrs, (%d + (%s)) " 4007c478bd9Sstevel@tonic-gate "* BYTES_PER_XDR_UNIT);", size, sizestr); 4017c478bd9Sstevel@tonic-gate 4027c478bd9Sstevel@tonic-gate f_print(fout, "\n"); 4037c478bd9Sstevel@tonic-gate tabify(fout, indent + 1); 4047c478bd9Sstevel@tonic-gate f_print(fout, "if (buf == NULL) {\n"); 4057c478bd9Sstevel@tonic-gate 4067c478bd9Sstevel@tonic-gate psav = cur; 4077c478bd9Sstevel@tonic-gate while (cur != dl) { 4087c478bd9Sstevel@tonic-gate print_stat(indent + 2, 4097c478bd9Sstevel@tonic-gate &cur->decl); 4107c478bd9Sstevel@tonic-gate cur = cur->next; 4117c478bd9Sstevel@tonic-gate } 4127c478bd9Sstevel@tonic-gate 4137c478bd9Sstevel@tonic-gate tabify(fout, indent+1); 4147c478bd9Sstevel@tonic-gate f_print(fout, "} else {\n"); 4157c478bd9Sstevel@tonic-gate 4167c478bd9Sstevel@tonic-gate f_print(fout, "#if defined(_LP64) || defined(_KERNEL)\n"); 4177c478bd9Sstevel@tonic-gate cur = psav; 4187c478bd9Sstevel@tonic-gate while (cur != dl) { 4197c478bd9Sstevel@tonic-gate emit_inline64(indent + 2, &cur->decl, flag); 4207c478bd9Sstevel@tonic-gate cur = cur->next; 4217c478bd9Sstevel@tonic-gate } 4227c478bd9Sstevel@tonic-gate f_print(fout, "#else\n"); 4237c478bd9Sstevel@tonic-gate cur = psav; 4247c478bd9Sstevel@tonic-gate while (cur != dl) { 4257c478bd9Sstevel@tonic-gate emit_inline(indent + 2, &cur->decl, flag); 4267c478bd9Sstevel@tonic-gate cur = cur->next; 4277c478bd9Sstevel@tonic-gate } 4287c478bd9Sstevel@tonic-gate f_print(fout, "#endif\n"); 4297c478bd9Sstevel@tonic-gate 4307c478bd9Sstevel@tonic-gate tabify(fout, indent + 1); 4317c478bd9Sstevel@tonic-gate f_print(fout, "}\n"); 4327c478bd9Sstevel@tonic-gate } 4337c478bd9Sstevel@tonic-gate 4347c478bd9Sstevel@tonic-gate /* 4357c478bd9Sstevel@tonic-gate * An inline type is a base type (interger type) or a vector of base types. 4367c478bd9Sstevel@tonic-gate */ 4377c478bd9Sstevel@tonic-gate static int 4387c478bd9Sstevel@tonic-gate inline_type(declaration *dc, int *size) 4397c478bd9Sstevel@tonic-gate { 4407c478bd9Sstevel@tonic-gate bas_type *ptr; 4417c478bd9Sstevel@tonic-gate 4427c478bd9Sstevel@tonic-gate *size = 0; 4437c478bd9Sstevel@tonic-gate 4447c478bd9Sstevel@tonic-gate if (dc->prefix == NULL && 4457c478bd9Sstevel@tonic-gate (dc->rel == REL_ALIAS || dc->rel == REL_VECTOR)) { 4467c478bd9Sstevel@tonic-gate ptr = find_type(dc->type); 4477c478bd9Sstevel@tonic-gate if (ptr != NULL) { 4487c478bd9Sstevel@tonic-gate *size = ptr->length; 4497c478bd9Sstevel@tonic-gate return (1); 4507c478bd9Sstevel@tonic-gate } 4517c478bd9Sstevel@tonic-gate } 4527c478bd9Sstevel@tonic-gate 4537c478bd9Sstevel@tonic-gate return (0); 4547c478bd9Sstevel@tonic-gate } 4557c478bd9Sstevel@tonic-gate 4567c478bd9Sstevel@tonic-gate static char * 4577c478bd9Sstevel@tonic-gate arraysize(char *sz, declaration *dc, int elsize) 4587c478bd9Sstevel@tonic-gate { 4597c478bd9Sstevel@tonic-gate int len; 4607c478bd9Sstevel@tonic-gate int elsz = elsize; 4617c478bd9Sstevel@tonic-gate int digits; 4627c478bd9Sstevel@tonic-gate int slen = 0; 4637c478bd9Sstevel@tonic-gate char *plus = ""; 4647c478bd9Sstevel@tonic-gate char *tmp; 465*61961e0fSrobinson size_t tlen; 4667c478bd9Sstevel@tonic-gate 4677c478bd9Sstevel@tonic-gate /* 4687c478bd9Sstevel@tonic-gate * Calculate the size of a string to hold the size of all arrays 4697c478bd9Sstevel@tonic-gate * to be inlined. 4707c478bd9Sstevel@tonic-gate * 4717c478bd9Sstevel@tonic-gate * We have the string representation of the total size that has already 4727c478bd9Sstevel@tonic-gate * been seen. (Null if this is the first array). 4737c478bd9Sstevel@tonic-gate * We have the string representation of array max from the declaration, 4747c478bd9Sstevel@tonic-gate * optionally the plus string, " + ", if this is not the first array, 4757c478bd9Sstevel@tonic-gate * and the number of digits for the element size for this declaration. 4767c478bd9Sstevel@tonic-gate */ 4777c478bd9Sstevel@tonic-gate if (sz != NULL) { 4787c478bd9Sstevel@tonic-gate plus = " + "; 4797c478bd9Sstevel@tonic-gate slen = strlen(sz); 4807c478bd9Sstevel@tonic-gate } 4817c478bd9Sstevel@tonic-gate 4827c478bd9Sstevel@tonic-gate /* Calculate the number of digits to hold the element size */ 4837c478bd9Sstevel@tonic-gate for (digits = 1; elsz >= 10; digits++) 4847c478bd9Sstevel@tonic-gate elsz /= 10; 4857c478bd9Sstevel@tonic-gate 4867c478bd9Sstevel@tonic-gate /* 4877c478bd9Sstevel@tonic-gate * If elsize != 1 the allocate 3 extra bytes for the times 4887c478bd9Sstevel@tonic-gate * string, " * ", the "()" below, and the digits. One extra 4897c478bd9Sstevel@tonic-gate * for the trailing NULL 4907c478bd9Sstevel@tonic-gate */ 4917c478bd9Sstevel@tonic-gate len = strlen(dc->array_max) + (elsize == 1 ? 0 : digits + 5) + 1; 492*61961e0fSrobinson tlen = slen + len + strlen(plus); 493*61961e0fSrobinson tmp = realloc(sz, tlen); 4947c478bd9Sstevel@tonic-gate if (tmp == NULL) { 4957c478bd9Sstevel@tonic-gate f_print(stderr, "Fatal error : no memory\n"); 4967c478bd9Sstevel@tonic-gate crash(); 4977c478bd9Sstevel@tonic-gate } 4987c478bd9Sstevel@tonic-gate 4997c478bd9Sstevel@tonic-gate if (elsize == 1) 500*61961e0fSrobinson (void) snprintf(tmp + slen, tlen - slen, "%s%s", 501*61961e0fSrobinson plus, dc->array_max); 5027c478bd9Sstevel@tonic-gate else 503*61961e0fSrobinson (void) snprintf(tmp + slen, tlen - slen, "%s(%s) * %d", 504*61961e0fSrobinson plus, dc->array_max, elsize); 5057c478bd9Sstevel@tonic-gate 5067c478bd9Sstevel@tonic-gate return (tmp); 5077c478bd9Sstevel@tonic-gate } 5087c478bd9Sstevel@tonic-gate 5097c478bd9Sstevel@tonic-gate static void 5107c478bd9Sstevel@tonic-gate inline_struct(decl_list *dl, decl_list *last, int flag, int indent) 5117c478bd9Sstevel@tonic-gate { 5127c478bd9Sstevel@tonic-gate int size, tsize; 513*61961e0fSrobinson decl_list *cur; 514*61961e0fSrobinson char *sizestr; 5157c478bd9Sstevel@tonic-gate 5167c478bd9Sstevel@tonic-gate cur = NULL; 5177c478bd9Sstevel@tonic-gate tsize = 0; 5187c478bd9Sstevel@tonic-gate sizestr = NULL; 5197c478bd9Sstevel@tonic-gate for (; dl != last; dl = dl->next) { 5207c478bd9Sstevel@tonic-gate if (inline_type(&dl->decl, &size)) { 5217c478bd9Sstevel@tonic-gate if (cur == NULL) 5227c478bd9Sstevel@tonic-gate cur = dl; 5237c478bd9Sstevel@tonic-gate 5247c478bd9Sstevel@tonic-gate if (dl->decl.rel == REL_ALIAS) 5257c478bd9Sstevel@tonic-gate tsize += size; 5267c478bd9Sstevel@tonic-gate else { 5277c478bd9Sstevel@tonic-gate /* this code is required to handle arrays */ 5287c478bd9Sstevel@tonic-gate sizestr = arraysize(sizestr, &dl->decl, size); 5297c478bd9Sstevel@tonic-gate } 5307c478bd9Sstevel@tonic-gate } else { 5317c478bd9Sstevel@tonic-gate if (cur != NULL) 5327c478bd9Sstevel@tonic-gate if (sizestr == NULL && tsize < inlinelen) { 5337c478bd9Sstevel@tonic-gate /* 5347c478bd9Sstevel@tonic-gate * don't expand into inline code 5357c478bd9Sstevel@tonic-gate * if tsize < inlinelen 5367c478bd9Sstevel@tonic-gate */ 5377c478bd9Sstevel@tonic-gate while (cur != dl) { 5387c478bd9Sstevel@tonic-gate print_stat(indent + 1, 5397c478bd9Sstevel@tonic-gate &cur->decl); 5407c478bd9Sstevel@tonic-gate cur = cur->next; 5417c478bd9Sstevel@tonic-gate } 5427c478bd9Sstevel@tonic-gate } else { 5437c478bd9Sstevel@tonic-gate expand_inline(indent, sizestr, 5447c478bd9Sstevel@tonic-gate tsize, flag, dl, cur); 5457c478bd9Sstevel@tonic-gate } 5467c478bd9Sstevel@tonic-gate tsize = 0; 5477c478bd9Sstevel@tonic-gate cur = NULL; 5487c478bd9Sstevel@tonic-gate sizestr = NULL; 5497c478bd9Sstevel@tonic-gate print_stat(indent + 1, &dl->decl); 5507c478bd9Sstevel@tonic-gate } 5517c478bd9Sstevel@tonic-gate } 5527c478bd9Sstevel@tonic-gate 553*61961e0fSrobinson if (cur == NULL) 554*61961e0fSrobinson return; 5557c478bd9Sstevel@tonic-gate if (sizestr == NULL && tsize < inlinelen) { 5567c478bd9Sstevel@tonic-gate /* don't expand into inline code if tsize < inlinelen */ 5577c478bd9Sstevel@tonic-gate while (cur != dl) { 5587c478bd9Sstevel@tonic-gate print_stat(indent + 1, &cur->decl); 5597c478bd9Sstevel@tonic-gate cur = cur->next; 5607c478bd9Sstevel@tonic-gate } 5617c478bd9Sstevel@tonic-gate } else { 5627c478bd9Sstevel@tonic-gate expand_inline(indent, sizestr, tsize, flag, dl, cur); 5637c478bd9Sstevel@tonic-gate } 5647c478bd9Sstevel@tonic-gate } 5657c478bd9Sstevel@tonic-gate 5667c478bd9Sstevel@tonic-gate /* 5677c478bd9Sstevel@tonic-gate * Check if we can inline this structure. While we are at it check if the 5687c478bd9Sstevel@tonic-gate * declaration list has any vectors defined of "basic" types. 5697c478bd9Sstevel@tonic-gate */ 5707c478bd9Sstevel@tonic-gate static int 5717c478bd9Sstevel@tonic-gate check_inline(decl_list *dl, int inlinelen, int *have_vector) 5727c478bd9Sstevel@tonic-gate { 5737c478bd9Sstevel@tonic-gate int tsize = 0; 5747c478bd9Sstevel@tonic-gate int size; 5757c478bd9Sstevel@tonic-gate int doinline = 0; 5767c478bd9Sstevel@tonic-gate 5777c478bd9Sstevel@tonic-gate *have_vector = 0; 5787c478bd9Sstevel@tonic-gate if (inlinelen == 0) 5797c478bd9Sstevel@tonic-gate return (0); 5807c478bd9Sstevel@tonic-gate 5817c478bd9Sstevel@tonic-gate for (; dl != NULL; dl = dl->next) { 582*61961e0fSrobinson if (!inline_type(&dl->decl, &size)) { 583*61961e0fSrobinson tsize = 0; 584*61961e0fSrobinson continue; 585*61961e0fSrobinson } 5867c478bd9Sstevel@tonic-gate if (dl->decl.rel == REL_VECTOR) { 5877c478bd9Sstevel@tonic-gate *have_vector = 1; 588*61961e0fSrobinson return (1); 589*61961e0fSrobinson } 5907c478bd9Sstevel@tonic-gate tsize += size; 5917c478bd9Sstevel@tonic-gate if (tsize >= inlinelen) 5927c478bd9Sstevel@tonic-gate doinline = 1; 5937c478bd9Sstevel@tonic-gate } 5947c478bd9Sstevel@tonic-gate 5957c478bd9Sstevel@tonic-gate return (doinline); 5967c478bd9Sstevel@tonic-gate } 5977c478bd9Sstevel@tonic-gate 5987c478bd9Sstevel@tonic-gate 5997c478bd9Sstevel@tonic-gate static void 6007c478bd9Sstevel@tonic-gate emit_struct_tail_recursion(definition *defp, int can_inline) 6017c478bd9Sstevel@tonic-gate { 6027c478bd9Sstevel@tonic-gate int indent = 3; 6037c478bd9Sstevel@tonic-gate struct_def *sp = &defp->def.st; 6047c478bd9Sstevel@tonic-gate decl_list *dl; 6057c478bd9Sstevel@tonic-gate 6067c478bd9Sstevel@tonic-gate 6077c478bd9Sstevel@tonic-gate f_print(fout, "\t%s *tmp_%s;\n", 6087c478bd9Sstevel@tonic-gate defp->def_name, defp->def_name); 6097c478bd9Sstevel@tonic-gate 6107c478bd9Sstevel@tonic-gate f_print(fout, "\tbool_t more_data = TRUE;\n"); 6117c478bd9Sstevel@tonic-gate f_print(fout, "\tbool_t first_objp = TRUE;\n\n"); 6127c478bd9Sstevel@tonic-gate 6137c478bd9Sstevel@tonic-gate f_print(fout, "\n\tif (xdrs->x_op == XDR_DECODE) {\n"); 6147c478bd9Sstevel@tonic-gate f_print(fout, "\n\t\twhile (more_data) {\n"); 6157c478bd9Sstevel@tonic-gate f_print(fout, "\n\t\t\tvoid bzero();\n\n"); 6167c478bd9Sstevel@tonic-gate 6177c478bd9Sstevel@tonic-gate if (can_inline) 6187c478bd9Sstevel@tonic-gate inline_struct(sp->decls, sp->tail, GET, indent); 6197c478bd9Sstevel@tonic-gate else 6207c478bd9Sstevel@tonic-gate for (dl = sp->decls; dl != NULL && dl != sp->tail; 6217c478bd9Sstevel@tonic-gate dl = dl->next) 6227c478bd9Sstevel@tonic-gate print_stat(indent, &dl->decl); 6237c478bd9Sstevel@tonic-gate 6247c478bd9Sstevel@tonic-gate f_print(fout, "\t\t\tif (!xdr_bool(xdrs, " 6257c478bd9Sstevel@tonic-gate "&more_data))\n\t\t\t\treturn (FALSE);\n"); 6267c478bd9Sstevel@tonic-gate 6277c478bd9Sstevel@tonic-gate f_print(fout, "\n\t\t\tif (!more_data) {\n"); 6287c478bd9Sstevel@tonic-gate f_print(fout, "\t\t\t\tobjp->%s = NULL;\n", sp->tail->decl.name); 6297c478bd9Sstevel@tonic-gate f_print(fout, "\t\t\t\tbreak;\n"); 6307c478bd9Sstevel@tonic-gate f_print(fout, "\t\t\t}\n\n"); 6317c478bd9Sstevel@tonic-gate f_print(fout, "\t\t\tif (objp->%s == NULL) {\n", sp->tail->decl.name); 6327c478bd9Sstevel@tonic-gate f_print(fout, "\t\t\t\tobjp->%s = " 6337c478bd9Sstevel@tonic-gate "(%s *)\n\t\t\t\t\tmem_alloc(sizeof (%s));\n", 6347c478bd9Sstevel@tonic-gate sp->tail->decl.name, defp->def_name, defp->def_name); 6357c478bd9Sstevel@tonic-gate 6367c478bd9Sstevel@tonic-gate f_print(fout, "\t\t\t\tif (objp->%s == NULL)\n" 6377c478bd9Sstevel@tonic-gate "\t\t\t\t\treturn (FALSE);\n", sp->tail->decl.name); 6387c478bd9Sstevel@tonic-gate f_print(fout, "\t\t\t\tbzero(objp->%s, sizeof (%s));\n", 6397c478bd9Sstevel@tonic-gate sp->tail->decl.name, defp->def_name); 6407c478bd9Sstevel@tonic-gate f_print(fout, "\t\t\t}\n"); 6417c478bd9Sstevel@tonic-gate f_print(fout, "\t\t\tobjp = objp->%s;\n", sp->tail->decl.name); 6427c478bd9Sstevel@tonic-gate f_print(fout, "\t\t}\n"); 6437c478bd9Sstevel@tonic-gate 6447c478bd9Sstevel@tonic-gate f_print(fout, "\n\t} else if (xdrs->x_op == XDR_ENCODE) {\n"); 6457c478bd9Sstevel@tonic-gate f_print(fout, "\n\t\twhile (more_data) {\n"); 6467c478bd9Sstevel@tonic-gate 6477c478bd9Sstevel@tonic-gate if (can_inline) 6487c478bd9Sstevel@tonic-gate inline_struct(sp->decls, sp->tail, PUT, indent); 6497c478bd9Sstevel@tonic-gate else 6507c478bd9Sstevel@tonic-gate for (dl = sp->decls; dl != NULL && dl != sp->tail; 6517c478bd9Sstevel@tonic-gate dl = dl->next) 6527c478bd9Sstevel@tonic-gate print_stat(indent, &dl->decl); 6537c478bd9Sstevel@tonic-gate 6547c478bd9Sstevel@tonic-gate f_print(fout, "\t\t\tobjp = objp->%s;\n", sp->tail->decl.name); 6557c478bd9Sstevel@tonic-gate f_print(fout, "\t\t\tif (objp == NULL)\n"); 6567c478bd9Sstevel@tonic-gate f_print(fout, "\t\t\t\tmore_data = FALSE;\n"); 6577c478bd9Sstevel@tonic-gate 6587c478bd9Sstevel@tonic-gate f_print(fout, "\t\t\tif (!xdr_bool(xdrs, &more_data))\n" 6597c478bd9Sstevel@tonic-gate "\t\t\t\treturn (FALSE);\n"); 6607c478bd9Sstevel@tonic-gate 6617c478bd9Sstevel@tonic-gate f_print(fout, "\t\t}\n"); 6627c478bd9Sstevel@tonic-gate 6637c478bd9Sstevel@tonic-gate f_print(fout, "\n\t} else {\n"); 6647c478bd9Sstevel@tonic-gate f_print(fout, "\n\t\twhile (more_data) {\n"); 6657c478bd9Sstevel@tonic-gate 6667c478bd9Sstevel@tonic-gate for (dl = sp->decls; dl != NULL && dl != sp->tail; dl = dl->next) 6677c478bd9Sstevel@tonic-gate print_stat(indent, &dl->decl); 6687c478bd9Sstevel@tonic-gate 6697c478bd9Sstevel@tonic-gate f_print(fout, "\t\t\ttmp_%s = objp;\n", defp->def_name); 6707c478bd9Sstevel@tonic-gate f_print(fout, "\t\t\tobjp = objp->%s;\n", sp->tail->decl.name); 6717c478bd9Sstevel@tonic-gate 6727c478bd9Sstevel@tonic-gate f_print(fout, "\t\t\tif (objp == NULL)\n"); 6737c478bd9Sstevel@tonic-gate f_print(fout, "\t\t\t\tmore_data = FALSE;\n"); 6747c478bd9Sstevel@tonic-gate 6757c478bd9Sstevel@tonic-gate f_print(fout, "\t\t\tif (!first_objp)\n"); 6767c478bd9Sstevel@tonic-gate 6777c478bd9Sstevel@tonic-gate f_print(fout, "\t\t\t\tmem_free(tmp_%s, sizeof (%s));\n", 6787c478bd9Sstevel@tonic-gate defp->def_name, defp->def_name); 6797c478bd9Sstevel@tonic-gate 6807c478bd9Sstevel@tonic-gate f_print(fout, "\t\t\telse\n\t\t\t\tfirst_objp = FALSE;\n\t\t}\n"); 6817c478bd9Sstevel@tonic-gate 6827c478bd9Sstevel@tonic-gate f_print(fout, "\n\t}\n"); 6837c478bd9Sstevel@tonic-gate } 6847c478bd9Sstevel@tonic-gate 685*61961e0fSrobinson static void 686*61961e0fSrobinson emit_struct(definition *def) 6877c478bd9Sstevel@tonic-gate { 6887c478bd9Sstevel@tonic-gate decl_list *dl = def->def.st.decls; 6897c478bd9Sstevel@tonic-gate int can_inline, have_vector; 6907c478bd9Sstevel@tonic-gate 6917c478bd9Sstevel@tonic-gate 6927c478bd9Sstevel@tonic-gate can_inline = check_inline(dl, inlinelen, &have_vector); 6937c478bd9Sstevel@tonic-gate if (have_vector) 6947c478bd9Sstevel@tonic-gate f_print(fout, "\tint i;\n"); 6957c478bd9Sstevel@tonic-gate 6967c478bd9Sstevel@tonic-gate 6977c478bd9Sstevel@tonic-gate if (rflag && def->def.st.self_pointer) { 6987c478bd9Sstevel@tonic-gate /* Handle tail recursion elimination */ 6997c478bd9Sstevel@tonic-gate emit_struct_tail_recursion(def, can_inline); 7007c478bd9Sstevel@tonic-gate return; 7017c478bd9Sstevel@tonic-gate } 7027c478bd9Sstevel@tonic-gate 7037c478bd9Sstevel@tonic-gate 7047c478bd9Sstevel@tonic-gate if (can_inline) { 7057c478bd9Sstevel@tonic-gate f_print(fout, "\n\tif (xdrs->x_op == XDR_ENCODE) {\n"); 7067c478bd9Sstevel@tonic-gate inline_struct(dl, NULL, PUT, 1); 7077c478bd9Sstevel@tonic-gate 7087c478bd9Sstevel@tonic-gate f_print(fout, "\t\treturn (TRUE);\n\t}" 7097c478bd9Sstevel@tonic-gate " else if (xdrs->x_op == XDR_DECODE) {\n"); 7107c478bd9Sstevel@tonic-gate 7117c478bd9Sstevel@tonic-gate inline_struct(dl, NULL, GET, 1); 7127c478bd9Sstevel@tonic-gate f_print(fout, "\t\treturn (TRUE);\n\t}\n\n"); 7137c478bd9Sstevel@tonic-gate } 7147c478bd9Sstevel@tonic-gate 7157c478bd9Sstevel@tonic-gate /* now take care of XDR_FREE inline case or the non-inline cases */ 7167c478bd9Sstevel@tonic-gate 7177c478bd9Sstevel@tonic-gate for (dl = def->def.st.decls; dl != NULL; dl = dl->next) 7187c478bd9Sstevel@tonic-gate print_stat(1, &dl->decl); 7197c478bd9Sstevel@tonic-gate 7207c478bd9Sstevel@tonic-gate } 7217c478bd9Sstevel@tonic-gate 722*61961e0fSrobinson static void 723*61961e0fSrobinson emit_typedef(definition *def) 7247c478bd9Sstevel@tonic-gate { 7257c478bd9Sstevel@tonic-gate char *prefix = def->def.ty.old_prefix; 7267c478bd9Sstevel@tonic-gate char *type = def->def.ty.old_type; 7277c478bd9Sstevel@tonic-gate char *amax = def->def.ty.array_max; 7287c478bd9Sstevel@tonic-gate relation rel = def->def.ty.rel; 7297c478bd9Sstevel@tonic-gate 7307c478bd9Sstevel@tonic-gate print_ifstat(1, prefix, type, rel, amax, "objp", def->def_name); 7317c478bd9Sstevel@tonic-gate } 7327c478bd9Sstevel@tonic-gate 733*61961e0fSrobinson static void 734*61961e0fSrobinson print_stat(int indent, declaration *dec) 7357c478bd9Sstevel@tonic-gate { 7367c478bd9Sstevel@tonic-gate char *prefix = dec->prefix; 7377c478bd9Sstevel@tonic-gate char *type = dec->type; 7387c478bd9Sstevel@tonic-gate char *amax = dec->array_max; 7397c478bd9Sstevel@tonic-gate relation rel = dec->rel; 7407c478bd9Sstevel@tonic-gate char name[256]; 7417c478bd9Sstevel@tonic-gate 742*61961e0fSrobinson if (isvectordef(type, rel)) 743*61961e0fSrobinson (void) snprintf(name, sizeof (name), "objp->%s", dec->name); 744*61961e0fSrobinson else 745*61961e0fSrobinson (void) snprintf(name, sizeof (name), "&objp->%s", dec->name); 7467c478bd9Sstevel@tonic-gate print_ifstat(indent, prefix, type, rel, amax, name, dec->name); 7477c478bd9Sstevel@tonic-gate } 7487c478bd9Sstevel@tonic-gate 7497c478bd9Sstevel@tonic-gate 750*61961e0fSrobinson static void 751*61961e0fSrobinson emit_inline(int indent, declaration *decl, int flag) 7527c478bd9Sstevel@tonic-gate { 7537c478bd9Sstevel@tonic-gate switch (decl->rel) { 7547c478bd9Sstevel@tonic-gate case REL_ALIAS : 7557c478bd9Sstevel@tonic-gate emit_single_in_line(indent, decl, flag, REL_ALIAS); 7567c478bd9Sstevel@tonic-gate break; 7577c478bd9Sstevel@tonic-gate case REL_VECTOR : 7587c478bd9Sstevel@tonic-gate tabify(fout, indent); 7597c478bd9Sstevel@tonic-gate f_print(fout, "{\n"); 7607c478bd9Sstevel@tonic-gate tabify(fout, indent + 1); 761*61961e0fSrobinson f_print(fout, "%s *genp;\n\n", decl->type); 7627c478bd9Sstevel@tonic-gate tabify(fout, indent + 1); 7637c478bd9Sstevel@tonic-gate f_print(fout, 7647c478bd9Sstevel@tonic-gate "for (i = 0, genp = objp->%s;\n", decl->name); 7657c478bd9Sstevel@tonic-gate tabify(fout, indent + 2); 7667c478bd9Sstevel@tonic-gate f_print(fout, "i < %s; i++) {\n", decl->array_max); 7677c478bd9Sstevel@tonic-gate emit_single_in_line(indent + 2, decl, flag, REL_VECTOR); 7687c478bd9Sstevel@tonic-gate tabify(fout, indent + 1); 7697c478bd9Sstevel@tonic-gate f_print(fout, "}\n"); 7707c478bd9Sstevel@tonic-gate tabify(fout, indent); 7717c478bd9Sstevel@tonic-gate f_print(fout, "}\n"); 7727c478bd9Sstevel@tonic-gate } 7737c478bd9Sstevel@tonic-gate } 7747c478bd9Sstevel@tonic-gate 775*61961e0fSrobinson static void 776*61961e0fSrobinson emit_inline64(int indent, declaration *decl, int flag) 7777c478bd9Sstevel@tonic-gate { 7787c478bd9Sstevel@tonic-gate switch (decl->rel) { 7797c478bd9Sstevel@tonic-gate case REL_ALIAS : 7807c478bd9Sstevel@tonic-gate emit_single_in_line64(indent, decl, flag, REL_ALIAS); 7817c478bd9Sstevel@tonic-gate break; 7827c478bd9Sstevel@tonic-gate case REL_VECTOR : 7837c478bd9Sstevel@tonic-gate tabify(fout, indent); 7847c478bd9Sstevel@tonic-gate f_print(fout, "{\n"); 7857c478bd9Sstevel@tonic-gate tabify(fout, indent + 1); 786*61961e0fSrobinson f_print(fout, "%s *genp;\n\n", decl->type); 7877c478bd9Sstevel@tonic-gate tabify(fout, indent + 1); 7887c478bd9Sstevel@tonic-gate f_print(fout, 7897c478bd9Sstevel@tonic-gate "for (i = 0, genp = objp->%s;\n", decl->name); 7907c478bd9Sstevel@tonic-gate tabify(fout, indent + 2); 7917c478bd9Sstevel@tonic-gate f_print(fout, "i < %s; i++) {\n", decl->array_max); 7927c478bd9Sstevel@tonic-gate emit_single_in_line64(indent + 2, decl, flag, REL_VECTOR); 7937c478bd9Sstevel@tonic-gate tabify(fout, indent + 1); 7947c478bd9Sstevel@tonic-gate f_print(fout, "}\n"); 7957c478bd9Sstevel@tonic-gate tabify(fout, indent); 7967c478bd9Sstevel@tonic-gate f_print(fout, "}\n"); 7977c478bd9Sstevel@tonic-gate } 7987c478bd9Sstevel@tonic-gate } 7997c478bd9Sstevel@tonic-gate 800*61961e0fSrobinson static void 801*61961e0fSrobinson emit_single_in_line(int indent, declaration *decl, int flag, relation rel) 8027c478bd9Sstevel@tonic-gate { 8037c478bd9Sstevel@tonic-gate char *upp_case; 8047c478bd9Sstevel@tonic-gate int freed = 0; 8057c478bd9Sstevel@tonic-gate 8067c478bd9Sstevel@tonic-gate tabify(fout, indent); 8077c478bd9Sstevel@tonic-gate if (flag == PUT) 8087c478bd9Sstevel@tonic-gate f_print(fout, "IXDR_PUT_"); 8097c478bd9Sstevel@tonic-gate else 8107c478bd9Sstevel@tonic-gate if (rel == REL_ALIAS) 8117c478bd9Sstevel@tonic-gate f_print(fout, "objp->%s = IXDR_GET_", decl->name); 8127c478bd9Sstevel@tonic-gate else 8137c478bd9Sstevel@tonic-gate f_print(fout, "*genp++ = IXDR_GET_"); 8147c478bd9Sstevel@tonic-gate 8157c478bd9Sstevel@tonic-gate upp_case = upcase(decl->type); 8167c478bd9Sstevel@tonic-gate 8177c478bd9Sstevel@tonic-gate /* hack - XX */ 818*61961e0fSrobinson if (strcmp(upp_case, "INT") == 0) { 8197c478bd9Sstevel@tonic-gate free(upp_case); 8207c478bd9Sstevel@tonic-gate freed = 1; 8217c478bd9Sstevel@tonic-gate upp_case = "LONG"; 8227c478bd9Sstevel@tonic-gate } 8237c478bd9Sstevel@tonic-gate if ((strcmp(upp_case, "U_INT") == 0) || 8247c478bd9Sstevel@tonic-gate (strcmp(upp_case, "RPCPROG") == 0) || 8257c478bd9Sstevel@tonic-gate (strcmp(upp_case, "RPCVERS") == 0) || 8267c478bd9Sstevel@tonic-gate (strcmp(upp_case, "RPCPROC") == 0) || 8277c478bd9Sstevel@tonic-gate (strcmp(upp_case, "RPCPROT") == 0) || 828*61961e0fSrobinson (strcmp(upp_case, "RPCPORT") == 0)) { 8297c478bd9Sstevel@tonic-gate free(upp_case); 8307c478bd9Sstevel@tonic-gate freed = 1; 8317c478bd9Sstevel@tonic-gate upp_case = "U_LONG"; 8327c478bd9Sstevel@tonic-gate } 8337c478bd9Sstevel@tonic-gate 8347c478bd9Sstevel@tonic-gate if (flag == PUT) 8357c478bd9Sstevel@tonic-gate if (rel == REL_ALIAS) 8367c478bd9Sstevel@tonic-gate f_print(fout, 8377c478bd9Sstevel@tonic-gate "%s(buf, objp->%s);\n", upp_case, decl->name); 8387c478bd9Sstevel@tonic-gate else 8397c478bd9Sstevel@tonic-gate f_print(fout, "%s(buf, *genp++);\n", upp_case); 8407c478bd9Sstevel@tonic-gate 8417c478bd9Sstevel@tonic-gate else 8427c478bd9Sstevel@tonic-gate f_print(fout, "%s(buf);\n", upp_case); 8437c478bd9Sstevel@tonic-gate if (!freed) 8447c478bd9Sstevel@tonic-gate free(upp_case); 8457c478bd9Sstevel@tonic-gate } 8467c478bd9Sstevel@tonic-gate 847*61961e0fSrobinson static void 848*61961e0fSrobinson emit_single_in_line64(int indent, declaration *decl, int flag, relation rel) 8497c478bd9Sstevel@tonic-gate { 8507c478bd9Sstevel@tonic-gate char *upp_case; 8517c478bd9Sstevel@tonic-gate int freed = 0; 8527c478bd9Sstevel@tonic-gate 8537c478bd9Sstevel@tonic-gate tabify(fout, indent); 8547c478bd9Sstevel@tonic-gate if (flag == PUT) 8557c478bd9Sstevel@tonic-gate f_print(fout, "IXDR_PUT_"); 8567c478bd9Sstevel@tonic-gate else 8577c478bd9Sstevel@tonic-gate if (rel == REL_ALIAS) 8587c478bd9Sstevel@tonic-gate f_print(fout, "objp->%s = IXDR_GET_", decl->name); 8597c478bd9Sstevel@tonic-gate else 8607c478bd9Sstevel@tonic-gate f_print(fout, "*genp++ = IXDR_GET_"); 8617c478bd9Sstevel@tonic-gate 8627c478bd9Sstevel@tonic-gate upp_case = upcase(decl->type); 8637c478bd9Sstevel@tonic-gate 8647c478bd9Sstevel@tonic-gate /* hack - XX */ 865*61961e0fSrobinson if ((strcmp(upp_case, "INT") == 0)||(strcmp(upp_case, "LONG") == 0)) { 8667c478bd9Sstevel@tonic-gate free(upp_case); 8677c478bd9Sstevel@tonic-gate freed = 1; 8687c478bd9Sstevel@tonic-gate upp_case = "INT32"; 8697c478bd9Sstevel@tonic-gate } 8707c478bd9Sstevel@tonic-gate if ((strcmp(upp_case, "U_INT") == 0) || 8717c478bd9Sstevel@tonic-gate (strcmp(upp_case, "U_LONG") == 0) || 8727c478bd9Sstevel@tonic-gate (strcmp(upp_case, "RPCPROG") == 0) || 8737c478bd9Sstevel@tonic-gate (strcmp(upp_case, "RPCVERS") == 0) || 8747c478bd9Sstevel@tonic-gate (strcmp(upp_case, "RPCPROC") == 0) || 8757c478bd9Sstevel@tonic-gate (strcmp(upp_case, "RPCPROT") == 0) || 876*61961e0fSrobinson (strcmp(upp_case, "RPCPORT") == 0)) { 8777c478bd9Sstevel@tonic-gate free(upp_case); 8787c478bd9Sstevel@tonic-gate freed = 1; 8797c478bd9Sstevel@tonic-gate upp_case = "U_INT32"; 8807c478bd9Sstevel@tonic-gate } 8817c478bd9Sstevel@tonic-gate 8827c478bd9Sstevel@tonic-gate if (flag == PUT) 8837c478bd9Sstevel@tonic-gate if (rel == REL_ALIAS) 8847c478bd9Sstevel@tonic-gate f_print(fout, 8857c478bd9Sstevel@tonic-gate "%s(buf, objp->%s);\n", upp_case, decl->name); 8867c478bd9Sstevel@tonic-gate else 8877c478bd9Sstevel@tonic-gate f_print(fout, "%s(buf, *genp++);\n", upp_case); 8887c478bd9Sstevel@tonic-gate 8897c478bd9Sstevel@tonic-gate else 8907c478bd9Sstevel@tonic-gate f_print(fout, "%s(buf);\n", upp_case); 8917c478bd9Sstevel@tonic-gate if (!freed) 8927c478bd9Sstevel@tonic-gate free(upp_case); 8937c478bd9Sstevel@tonic-gate } 8947c478bd9Sstevel@tonic-gate 895*61961e0fSrobinson static char * 896*61961e0fSrobinson upcase(char *str) 8977c478bd9Sstevel@tonic-gate { 8987c478bd9Sstevel@tonic-gate char *ptr, *hptr; 8997c478bd9Sstevel@tonic-gate 900*61961e0fSrobinson ptr = malloc(strlen(str)+1); 901*61961e0fSrobinson if (ptr == NULL) { 9027c478bd9Sstevel@tonic-gate f_print(stderr, "malloc failed\n"); 9037c478bd9Sstevel@tonic-gate exit(1); 9047c478bd9Sstevel@tonic-gate }; 9057c478bd9Sstevel@tonic-gate 9067c478bd9Sstevel@tonic-gate hptr = ptr; 9077c478bd9Sstevel@tonic-gate while (*str != '\0') 9087c478bd9Sstevel@tonic-gate *ptr++ = toupper(*str++); 9097c478bd9Sstevel@tonic-gate 9107c478bd9Sstevel@tonic-gate *ptr = '\0'; 9117c478bd9Sstevel@tonic-gate return (hptr); 9127c478bd9Sstevel@tonic-gate } 913