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>
38 __FBSDID("$FreeBSD$");
41 * rpc_cout.c, XDR routine outputter for the RPC protocol compiler
42 * Copyright (C) 1987, Sun Microsystems, Inc.
47 #include "rpc_parse.h"
51 static void print_header( definition * );
52 static void print_trailer( void );
53 static void print_stat( int , declaration * );
54 static void emit_enum( definition * );
55 static void emit_program( definition * );
56 static void emit_union( definition * );
57 static void emit_struct( definition * );
58 static void emit_typedef( definition * );
59 static void emit_inline( int, declaration *, int );
60 static void emit_single_in_line( int, declaration *, int, relation );
63 * Emit the C-routine for the given definition
68 if (def->def_kind == DEF_CONST) {
71 if (def->def_kind == DEF_PROGRAM) {
75 if (def->def_kind == DEF_TYPEDEF) {
77 * now we need to handle declarations like
78 * struct typedef foo foo;
79 * since we dont want this to be expanded into 2 calls to xdr_foo
82 if (strcmp(def->def.ty.old_type, def->def_name) == 0)
86 switch (def->def_kind) {
99 /* DEF_CONST and DEF_PROGRAM have already been handled */
107 findtype(definition *def, const char *type)
110 if (def->def_kind == DEF_PROGRAM || def->def_kind == DEF_CONST) {
113 return (streq(def->def_name, type));
118 undefined(const char *type)
122 def = (definition *) FINDVAL(defined, type, findtype);
123 return (def == NULL);
128 print_generic_header(const char *procname, int pointerp)
131 f_print(fout, "bool_t\n");
132 f_print(fout, "xdr_%s(", procname);
133 f_print(fout, "XDR *xdrs, ");
134 f_print(fout, "%s ", procname);
137 f_print(fout, "objp)\n{\n\n");
141 print_header(definition *def)
143 print_generic_header(def->def_name,
144 def->def_kind != DEF_TYPEDEF ||
145 !isvectordef(def->def.ty.old_type,
147 /* Now add Inline support */
149 if (inline_size == 0)
151 /* May cause lint to complain. but ... */
152 f_print(fout, "\tregister long *buf;\n\n");
156 print_prog_header(proc_list *plist)
158 print_generic_header(plist->args.argname, 1);
164 f_print(fout, "\treturn (TRUE);\n");
165 f_print(fout, "}\n");
170 print_ifopen(int indent, const char *name)
172 tabify(fout, indent);
173 f_print(fout, "if (!xdr_%s(xdrs", name);
177 print_ifarg(const char *arg)
179 f_print(fout, ", %s", arg);
183 print_ifsizeof(int indent, const char *prefix, const char *type)
186 f_print(fout, ",\n");
187 tabify(fout, indent);
191 if (streq(type, "bool")) {
192 f_print(fout, "sizeof (bool_t), (xdrproc_t) xdr_bool");
194 f_print(fout, "sizeof (");
195 if (undefined(type) && prefix) {
196 f_print(fout, "%s ", prefix);
198 f_print(fout, "%s), (xdrproc_t) xdr_%s", type, type);
203 print_ifclose(int indent, int brace)
205 f_print(fout, "))\n");
206 tabify(fout, indent);
207 f_print(fout, "\treturn (FALSE);\n");
209 f_print(fout, "\t}\n");
213 print_ifstat(int indent, const char *prefix, const char *type, relation rel,
214 const char *amax, const char *objname, const char *name)
216 const char *alt = NULL;
222 f_print(fout, "\t{\n");
223 f_print(fout, "\t%s **pp = %s;\n", type, objname);
224 print_ifopen(indent, "pointer");
225 print_ifarg("(char **)");
227 print_ifsizeof(0, prefix, type);
230 if (streq(type, "string")) {
232 } else if (streq(type, "opaque")) {
236 print_ifopen(indent, alt);
237 print_ifarg(objname);
239 print_ifopen(indent, "vector");
240 print_ifarg("(char *)");
241 f_print(fout, "%s", objname);
245 print_ifsizeof(indent + 1, prefix, type);
249 if (streq(type, "string")) {
251 } else if (streq(type, "opaque")) {
254 if (streq(type, "string")) {
255 print_ifopen(indent, alt);
256 print_ifarg(objname);
259 print_ifopen(indent, alt);
261 print_ifopen(indent, "array");
263 print_ifarg("(char **)");
264 if (*objname == '&') {
265 f_print(fout, "%s.%s_val, (u_int *) %s.%s_len",
266 objname, name, objname, name);
269 "&%s->%s_val, (u_int *) &%s->%s_len",
270 objname, name, objname, name);
275 print_ifsizeof(indent + 1, prefix, type);
279 print_ifopen(indent, type);
280 print_ifarg(objname);
283 print_ifclose(indent, brace);
288 emit_enum(definition *def __unused)
290 print_ifopen(1, "enum");
291 print_ifarg("(enum_t *)objp");
296 emit_program(definition *def)
302 for (vlist = def->def.pr.versions; vlist != NULL; vlist = vlist->next)
303 for (plist = vlist->procs; plist != NULL; plist = plist->next) {
304 if (!newstyle || plist->arg_num < 2)
305 continue; /* old style, or single argument */
306 print_prog_header(plist);
307 for (dl = plist->args.decls; dl != NULL;
309 print_stat(1, &dl->decl);
316 emit_union(definition *def)
322 const char *vecformat = "objp->%s_u.%s";
323 const char *format = "&objp->%s_u.%s";
325 print_stat(1, &def->def.un.enum_decl);
326 f_print(fout, "\tswitch (objp->%s) {\n", def->def.un.enum_decl.name);
327 for (cl = def->def.un.cases; cl != NULL; cl = cl->next) {
329 f_print(fout, "\tcase %s:\n", cl->case_name);
330 if (cl->contflag == 1) /* a continued case statement */
333 if (!streq(cs->type, "void")) {
334 object = xmalloc(strlen(def->def_name) +
335 strlen(format) + strlen(cs->name) + 1);
336 if (isvectordef (cs->type, cs->rel)) {
337 s_print(object, vecformat, def->def_name,
340 s_print(object, format, def->def_name,
343 print_ifstat(2, cs->prefix, cs->type, cs->rel,
344 cs->array_max, object, cs->name);
347 f_print(fout, "\t\tbreak;\n");
349 dflt = def->def.un.default_decl;
351 if (!streq(dflt->type, "void")) {
352 f_print(fout, "\tdefault:\n");
353 object = xmalloc(strlen(def->def_name) +
354 strlen(format) + strlen(dflt->name) + 1);
355 if (isvectordef (dflt->type, dflt->rel)) {
356 s_print(object, vecformat, def->def_name,
359 s_print(object, format, def->def_name,
363 print_ifstat(2, dflt->prefix, dflt->type, dflt->rel,
364 dflt->array_max, object, dflt->name);
366 f_print(fout, "\t\tbreak;\n");
368 f_print(fout, "\tdefault:\n");
369 f_print(fout, "\t\tbreak;\n");
372 f_print(fout, "\tdefault:\n");
373 f_print(fout, "\t\treturn (FALSE);\n");
376 f_print(fout, "\t}\n");
380 inline_struct(definition *def, int flag)
384 decl_list *cur, *psav;
393 f_print(fout, "\n\tif (xdrs->x_op == XDR_ENCODE) {\n");
395 f_print(fout, "\t\treturn (TRUE);\n\t} else if (xdrs->x_op == XDR_DECODE) {\n");
400 for (dl = def->def.st.decls; dl != NULL; dl = dl->next) { /* xxx */
401 /* now walk down the list and check for basic types */
402 if ((dl->decl.prefix == NULL) &&
403 ((ptr = find_type(dl->decl.type)) != NULL) &&
404 ((dl->decl.rel == REL_ALIAS) ||
405 (dl->decl.rel == REL_VECTOR))){
410 if (dl->decl.rel == REL_ALIAS)
413 /* this code is required to handle arrays */
419 if (ptr->length != 1)
420 s_print(ptemp, "%s%s * %d",
421 plus, dl->decl.array_max,
424 s_print(ptemp, "%s%s", plus,
427 /* now concatenate to sizestr !!!! */
428 if (sizestr == NULL) {
429 sizestr = xstrdup(ptemp);
432 sizestr = xrealloc(sizestr,
435 sizestr = strcat(sizestr, ptemp);
436 /* build up length of array */
441 if (sizestr == NULL && size < inline_size){
443 * don't expand into inline code
444 * if size < inline_size
447 print_stat(indent + 1, &cur->decl);
451 /* were already looking at a xdr_inlineable structure */
452 tabify(fout, indent + 1);
454 f_print(fout, "buf = XDR_INLINE(xdrs, %d * BYTES_PER_XDR_UNIT);",
459 "buf = XDR_INLINE(xdrs, (%s) * BYTES_PER_XDR_UNIT);",
463 "buf = XDR_INLINE(xdrs, (%d + (%s)) * BYTES_PER_XDR_UNIT);",
468 tabify(fout, indent + 1);
470 "if (buf == NULL) {\n");
474 print_stat(indent + 2, &cur->decl);
478 f_print(fout, "\n\t\t} else {\n");
482 emit_inline(indent + 2, &cur->decl, flag);
486 tabify(fout, indent + 1);
487 f_print(fout, "}\n");
494 print_stat(indent + 1, &dl->decl);
499 if (sizestr == NULL && size < inline_size){
500 /* don't expand into inline code if size < inline_size */
502 print_stat(indent + 1, &cur->decl);
506 /* were already looking at a xdr_inlineable structure */
508 f_print(fout, "\t\tbuf = XDR_INLINE(xdrs, %d * BYTES_PER_XDR_UNIT);",
513 "\t\tbuf = XDR_INLINE(xdrs, (%s) * BYTES_PER_XDR_UNIT);",
517 "\t\tbuf = XDR_INLINE(xdrs, (%d + (%s)) * BYTES_PER_XDR_UNIT);",
520 f_print(fout, "\n\t\tif (buf == NULL) {\n");
523 print_stat(indent + 2, &cur->decl);
526 f_print(fout, "\t\t} else {\n");
530 emit_inline(indent + 2, &cur->decl, flag);
533 f_print(fout, "\t\t}\n");
539 emit_struct(definition *def)
546 if (inline_size == 0) {
547 /* No xdr_inlining at all */
548 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
549 print_stat(1, &dl->decl);
553 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
554 if (dl->decl.rel == REL_VECTOR){
555 f_print(fout, "\tint i;\n");
562 * Make a first pass and see if inling is possible.
564 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
565 if ((dl->decl.prefix == NULL) &&
566 ((ptr = find_type(dl->decl.type)) != NULL) &&
567 ((dl->decl.rel == REL_ALIAS)||
568 (dl->decl.rel == REL_VECTOR))){
569 if (dl->decl.rel == REL_ALIAS)
573 break; /* can be inlined */
576 if (size >= inline_size){
578 break; /* can be inlined */
582 if (size >= inline_size)
585 if (can_inline == 0){ /* can not inline, drop back to old mode */
586 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
587 print_stat(1, &dl->decl);
592 for (j = 0; j < 2; j++){
593 inline_struct(def, flag);
598 f_print(fout, "\t\treturn (TRUE);\n\t}\n\n");
600 /* now take care of XDR_FREE case */
602 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
603 print_stat(1, &dl->decl);
608 emit_typedef(definition *def)
610 const char *prefix = def->def.ty.old_prefix;
611 const char *type = def->def.ty.old_type;
612 const char *amax = def->def.ty.array_max;
613 relation rel = def->def.ty.rel;
615 print_ifstat(1, prefix, type, rel, amax, "objp", def->def_name);
619 print_stat(int indent, declaration *dec)
621 const char *prefix = dec->prefix;
622 const char *type = dec->type;
623 const char *amax = dec->array_max;
624 relation rel = dec->rel;
627 if (isvectordef(type, rel)) {
628 s_print(name, "objp->%s", dec->name);
630 s_print(name, "&objp->%s", dec->name);
632 print_ifstat(indent, prefix, type, rel, amax, name, dec->name);
636 char *upcase(const char *);
639 emit_inline(int indent, declaration *decl, int flag)
643 emit_single_in_line(indent, decl, flag, REL_ALIAS);
646 tabify(fout, indent);
647 f_print(fout, "{\n");
648 tabify(fout, indent + 1);
649 f_print(fout, "%s *genp;\n\n", decl->type);
650 tabify(fout, indent + 1);
652 "for (i = 0, genp = objp->%s;\n", decl->name);
653 tabify(fout, indent + 2);
654 f_print(fout, "i < %s; i++) {\n", decl->array_max);
655 emit_single_in_line(indent + 2, decl, flag, REL_VECTOR);
656 tabify(fout, indent + 1);
657 f_print(fout, "}\n");
658 tabify(fout, indent);
659 f_print(fout, "}\n");
667 emit_single_in_line(int indent, declaration *decl, int flag, relation rel)
671 tabify(fout, indent);
673 f_print(fout, "IXDR_PUT_");
675 if (rel == REL_ALIAS)
676 f_print(fout, "objp->%s = IXDR_GET_", decl->name);
678 f_print(fout, "*genp++ = IXDR_GET_");
680 upp_case = upcase(decl->type);
683 if (strcmp(upp_case, "INT") == 0)
686 upp_case = strdup("LONG");
689 if (strcmp(upp_case, "U_INT") == 0)
692 upp_case = strdup("U_LONG");
695 if (rel == REL_ALIAS)
697 "%s(buf, objp->%s);\n", upp_case, decl->name);
699 f_print(fout, "%s(buf, *genp++);\n", upp_case);
702 f_print(fout, "%s(buf);\n", upp_case);
707 upcase(const char *str)
711 ptr = (char *)xmalloc(strlen(str)+1);
715 *ptr++ = toupper(*str++);