2 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3 * unrestricted use provided that this legend is included on all tape
4 * media and as a part of the software program in whole or part. Users
5 * may copy or modify Sun RPC without charge, but are not authorized
6 * to license or distribute it to anyone else except as part of a product or
7 * program developed by the user.
9 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13 * Sun RPC is provided with no support and without any obligation on the
14 * part of Sun Microsystems, Inc. to assist in its use, correction,
15 * modification or enhancement.
17 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19 * OR ANY PART THEREOF.
21 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22 * or profits or other special, indirect and consequential damages, even if
23 * Sun has been advised of the possibility of such damages.
25 * Sun Microsystems, Inc.
27 * Mountain View, California 94043
32 #ident "@(#)rpc_cout.c 1.14 93/07/05 SMI"
33 static char sccsid[] = "@(#)rpc_cout.c 1.13 89/02/22 (C) 1987 SMI";
37 #include <sys/cdefs.h>
39 * rpc_cout.c, XDR routine outputter for the RPC protocol compiler
40 * Copyright (C) 1987, Sun Microsystems, Inc.
45 #include "rpc_parse.h"
49 static void print_header( definition * );
50 static void print_trailer( void );
51 static void print_stat( int , declaration * );
52 static void emit_enum( definition * );
53 static void emit_program( definition * );
54 static void emit_union( definition * );
55 static void emit_struct( definition * );
56 static void emit_typedef( definition * );
57 static void emit_inline( int, declaration *, int );
58 static void emit_single_in_line( int, declaration *, int, relation );
61 * Emit the C-routine for the given definition
66 if (def->def_kind == DEF_CONST) {
69 if (def->def_kind == DEF_PROGRAM) {
73 if (def->def_kind == DEF_TYPEDEF) {
75 * now we need to handle declarations like
76 * struct typedef foo foo;
77 * since we dont want this to be expanded into 2 calls to xdr_foo
80 if (strcmp(def->def.ty.old_type, def->def_name) == 0)
84 switch (def->def_kind) {
97 /* DEF_CONST and DEF_PROGRAM have already been handled */
105 findtype(definition *def, const char *type)
108 if (def->def_kind == DEF_PROGRAM || def->def_kind == DEF_CONST) {
111 return (streq(def->def_name, type));
116 undefined(const char *type)
120 def = (definition *) FINDVAL(defined, type, findtype);
121 return (def == NULL);
126 print_generic_header(const char *procname, int pointerp)
129 f_print(fout, "bool_t\n");
130 f_print(fout, "xdr_%s(", procname);
131 f_print(fout, "XDR *xdrs, ");
132 f_print(fout, "%s ", procname);
135 f_print(fout, "objp)\n{\n\n");
139 print_header(definition *def)
141 print_generic_header(def->def_name,
142 def->def_kind != DEF_TYPEDEF ||
143 !isvectordef(def->def.ty.old_type,
145 /* Now add Inline support */
147 if (inline_size == 0)
149 /* May cause lint to complain. but ... */
150 f_print(fout, "\tregister long *buf;\n\n");
154 print_prog_header(proc_list *plist)
156 print_generic_header(plist->args.argname, 1);
162 f_print(fout, "\treturn (TRUE);\n");
163 f_print(fout, "}\n");
168 print_ifopen(int indent, const char *name)
170 tabify(fout, indent);
171 f_print(fout, "if (!xdr_%s(xdrs", name);
175 print_ifarg(const char *arg)
177 f_print(fout, ", %s", arg);
181 print_ifsizeof(int indent, const char *prefix, const char *type)
184 f_print(fout, ",\n");
185 tabify(fout, indent);
189 if (streq(type, "bool")) {
190 f_print(fout, "sizeof (bool_t), (xdrproc_t) xdr_bool");
192 f_print(fout, "sizeof (");
193 if (undefined(type) && prefix) {
194 f_print(fout, "%s ", prefix);
196 f_print(fout, "%s), (xdrproc_t) xdr_%s", type, type);
201 print_ifclose(int indent, int brace)
203 f_print(fout, "))\n");
204 tabify(fout, indent);
205 f_print(fout, "\treturn (FALSE);\n");
207 f_print(fout, "\t}\n");
211 print_ifstat(int indent, const char *prefix, const char *type, relation rel,
212 const char *amax, const char *objname, const char *name)
214 const char *alt = NULL;
220 f_print(fout, "\t{\n");
221 f_print(fout, "\t%s **pp = %s;\n", type, objname);
222 print_ifopen(indent, "pointer");
223 print_ifarg("(char **)");
225 print_ifsizeof(0, prefix, type);
228 if (streq(type, "string")) {
230 } else if (streq(type, "opaque")) {
234 print_ifopen(indent, alt);
235 print_ifarg(objname);
237 print_ifopen(indent, "vector");
238 print_ifarg("(char *)");
239 f_print(fout, "%s", objname);
243 print_ifsizeof(indent + 1, prefix, type);
247 if (streq(type, "string")) {
249 } else if (streq(type, "opaque")) {
252 if (streq(type, "string")) {
253 print_ifopen(indent, alt);
254 print_ifarg(objname);
257 print_ifopen(indent, alt);
259 print_ifopen(indent, "array");
261 print_ifarg("(char **)");
262 if (*objname == '&') {
263 f_print(fout, "%s.%s_val, (u_int *) %s.%s_len",
264 objname, name, objname, name);
267 "&%s->%s_val, (u_int *) &%s->%s_len",
268 objname, name, objname, name);
273 print_ifsizeof(indent + 1, prefix, type);
277 print_ifopen(indent, type);
278 print_ifarg(objname);
281 print_ifclose(indent, brace);
286 emit_enum(definition *def __unused)
288 print_ifopen(1, "enum");
289 print_ifarg("(enum_t *)objp");
294 emit_program(definition *def)
300 for (vlist = def->def.pr.versions; vlist != NULL; vlist = vlist->next)
301 for (plist = vlist->procs; plist != NULL; plist = plist->next) {
302 if (!newstyle || plist->arg_num < 2)
303 continue; /* old style, or single argument */
304 print_prog_header(plist);
305 for (dl = plist->args.decls; dl != NULL;
307 print_stat(1, &dl->decl);
314 emit_union(definition *def)
320 const char *vecformat = "objp->%s_u.%s";
321 const char *format = "&objp->%s_u.%s";
323 print_stat(1, &def->def.un.enum_decl);
324 f_print(fout, "\tswitch (objp->%s) {\n", def->def.un.enum_decl.name);
325 for (cl = def->def.un.cases; cl != NULL; cl = cl->next) {
327 f_print(fout, "\tcase %s:\n", cl->case_name);
328 if (cl->contflag == 1) /* a continued case statement */
331 if (!streq(cs->type, "void")) {
332 object = xmalloc(strlen(def->def_name) +
333 strlen(format) + strlen(cs->name) + 1);
334 if (isvectordef (cs->type, cs->rel)) {
335 s_print(object, vecformat, def->def_name,
338 s_print(object, format, def->def_name,
341 print_ifstat(2, cs->prefix, cs->type, cs->rel,
342 cs->array_max, object, cs->name);
345 f_print(fout, "\t\tbreak;\n");
347 dflt = def->def.un.default_decl;
349 if (!streq(dflt->type, "void")) {
350 f_print(fout, "\tdefault:\n");
351 object = xmalloc(strlen(def->def_name) +
352 strlen(format) + strlen(dflt->name) + 1);
353 if (isvectordef (dflt->type, dflt->rel)) {
354 s_print(object, vecformat, def->def_name,
357 s_print(object, format, def->def_name,
361 print_ifstat(2, dflt->prefix, dflt->type, dflt->rel,
362 dflt->array_max, object, dflt->name);
364 f_print(fout, "\t\tbreak;\n");
366 f_print(fout, "\tdefault:\n");
367 f_print(fout, "\t\tbreak;\n");
370 f_print(fout, "\tdefault:\n");
371 f_print(fout, "\t\treturn (FALSE);\n");
374 f_print(fout, "\t}\n");
378 inline_struct(definition *def, int flag)
382 decl_list *cur, *psav;
391 f_print(fout, "\n\tif (xdrs->x_op == XDR_ENCODE) {\n");
393 f_print(fout, "\t\treturn (TRUE);\n\t} else if (xdrs->x_op == XDR_DECODE) {\n");
398 for (dl = def->def.st.decls; dl != NULL; dl = dl->next) { /* xxx */
399 /* now walk down the list and check for basic types */
400 if ((dl->decl.prefix == NULL) &&
401 ((ptr = find_type(dl->decl.type)) != NULL) &&
402 ((dl->decl.rel == REL_ALIAS) ||
403 (dl->decl.rel == REL_VECTOR))){
408 if (dl->decl.rel == REL_ALIAS)
411 /* this code is required to handle arrays */
417 if (ptr->length != 1)
418 s_print(ptemp, "%s%s * %d",
419 plus, dl->decl.array_max,
422 s_print(ptemp, "%s%s", plus,
425 /* now concatenate to sizestr !!!! */
426 if (sizestr == NULL) {
427 sizestr = xstrdup(ptemp);
430 sizestr = xrealloc(sizestr,
433 sizestr = strcat(sizestr, ptemp);
434 /* build up length of array */
439 if (sizestr == NULL && size < inline_size){
441 * don't expand into inline code
442 * if size < inline_size
445 print_stat(indent + 1, &cur->decl);
449 /* were already looking at a xdr_inlineable structure */
450 tabify(fout, indent + 1);
452 f_print(fout, "buf = XDR_INLINE(xdrs, %d * BYTES_PER_XDR_UNIT);",
457 "buf = XDR_INLINE(xdrs, (%s) * BYTES_PER_XDR_UNIT);",
461 "buf = XDR_INLINE(xdrs, (%d + (%s)) * BYTES_PER_XDR_UNIT);",
466 tabify(fout, indent + 1);
468 "if (buf == NULL) {\n");
472 print_stat(indent + 2, &cur->decl);
476 f_print(fout, "\n\t\t} else {\n");
480 emit_inline(indent + 2, &cur->decl, flag);
484 tabify(fout, indent + 1);
485 f_print(fout, "}\n");
492 print_stat(indent + 1, &dl->decl);
497 if (sizestr == NULL && size < inline_size){
498 /* don't expand into inline code if size < inline_size */
500 print_stat(indent + 1, &cur->decl);
504 /* were already looking at a xdr_inlineable structure */
506 f_print(fout, "\t\tbuf = XDR_INLINE(xdrs, %d * BYTES_PER_XDR_UNIT);",
511 "\t\tbuf = XDR_INLINE(xdrs, (%s) * BYTES_PER_XDR_UNIT);",
515 "\t\tbuf = XDR_INLINE(xdrs, (%d + (%s)) * BYTES_PER_XDR_UNIT);",
518 f_print(fout, "\n\t\tif (buf == NULL) {\n");
521 print_stat(indent + 2, &cur->decl);
524 f_print(fout, "\t\t} else {\n");
528 emit_inline(indent + 2, &cur->decl, flag);
531 f_print(fout, "\t\t}\n");
537 emit_struct(definition *def)
544 if (inline_size == 0) {
545 /* No xdr_inlining at all */
546 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
547 print_stat(1, &dl->decl);
551 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
552 if (dl->decl.rel == REL_VECTOR &&
553 strcmp(dl->decl.type, "opaque") != 0){
554 f_print(fout, "\tint i;\n");
561 * Make a first pass and see if inling is possible.
563 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
564 if ((dl->decl.prefix == NULL) &&
565 ((ptr = find_type(dl->decl.type)) != NULL) &&
566 ((dl->decl.rel == REL_ALIAS)||
567 (dl->decl.rel == REL_VECTOR))){
568 if (dl->decl.rel == REL_ALIAS)
572 break; /* can be inlined */
575 if (size >= inline_size){
577 break; /* can be inlined */
581 if (size >= inline_size)
584 if (can_inline == 0){ /* can not inline, drop back to old mode */
585 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
586 print_stat(1, &dl->decl);
591 for (j = 0; j < 2; j++){
592 inline_struct(def, flag);
597 f_print(fout, "\t\treturn (TRUE);\n\t}\n\n");
599 /* now take care of XDR_FREE case */
601 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
602 print_stat(1, &dl->decl);
607 emit_typedef(definition *def)
609 const char *prefix = def->def.ty.old_prefix;
610 const char *type = def->def.ty.old_type;
611 const char *amax = def->def.ty.array_max;
612 relation rel = def->def.ty.rel;
614 print_ifstat(1, prefix, type, rel, amax, "objp", def->def_name);
618 print_stat(int indent, declaration *dec)
620 const char *prefix = dec->prefix;
621 const char *type = dec->type;
622 const char *amax = dec->array_max;
623 relation rel = dec->rel;
626 if (isvectordef(type, rel)) {
627 s_print(name, "objp->%s", dec->name);
629 s_print(name, "&objp->%s", dec->name);
631 print_ifstat(indent, prefix, type, rel, amax, name, dec->name);
635 char *upcase(const char *);
638 emit_inline(int indent, declaration *decl, int flag)
642 emit_single_in_line(indent, decl, flag, REL_ALIAS);
645 tabify(fout, indent);
646 f_print(fout, "{\n");
647 tabify(fout, indent + 1);
648 f_print(fout, "%s *genp;\n\n", decl->type);
649 tabify(fout, indent + 1);
651 "for (i = 0, genp = objp->%s;\n", decl->name);
652 tabify(fout, indent + 2);
653 f_print(fout, "i < %s; i++) {\n", decl->array_max);
654 emit_single_in_line(indent + 2, decl, flag, REL_VECTOR);
655 tabify(fout, indent + 1);
656 f_print(fout, "}\n");
657 tabify(fout, indent);
658 f_print(fout, "}\n");
666 emit_single_in_line(int indent, declaration *decl, int flag, relation rel)
670 tabify(fout, indent);
672 f_print(fout, "IXDR_PUT_");
674 if (rel == REL_ALIAS)
675 f_print(fout, "objp->%s = IXDR_GET_", decl->name);
677 f_print(fout, "*genp++ = IXDR_GET_");
679 upp_case = upcase(decl->type);
682 if (strcmp(upp_case, "INT") == 0)
685 upp_case = strdup("LONG");
688 if (strcmp(upp_case, "U_INT") == 0)
691 upp_case = strdup("U_LONG");
694 if (rel == REL_ALIAS)
696 "%s(buf, objp->%s);\n", upp_case, decl->name);
698 f_print(fout, "%s(buf, *genp++);\n", upp_case);
701 f_print(fout, "%s(buf);\n", upp_case);
706 upcase(const char *str)
710 ptr = (char *)xmalloc(strlen(str)+1);
714 *ptr++ = toupper(*str++);