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)
205 f_print(fout, "))\n");
206 tabify(fout, indent);
207 f_print(fout, "\treturn (FALSE);\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;
218 print_ifopen(indent, "pointer");
219 print_ifarg("(char **)");
220 f_print(fout, "%s", objname);
221 print_ifsizeof(0, prefix, type);
224 if (streq(type, "string")) {
226 } else if (streq(type, "opaque")) {
230 print_ifopen(indent, alt);
231 print_ifarg(objname);
233 print_ifopen(indent, "vector");
234 print_ifarg("(char *)");
235 f_print(fout, "%s", objname);
239 print_ifsizeof(indent + 1, prefix, type);
243 if (streq(type, "string")) {
245 } else if (streq(type, "opaque")) {
248 if (streq(type, "string")) {
249 print_ifopen(indent, alt);
250 print_ifarg(objname);
253 print_ifopen(indent, alt);
255 print_ifopen(indent, "array");
257 print_ifarg("(char **)");
258 if (*objname == '&') {
259 f_print(fout, "%s.%s_val, (u_int *) %s.%s_len",
260 objname, name, objname, name);
263 "&%s->%s_val, (u_int *) &%s->%s_len",
264 objname, name, objname, name);
269 print_ifsizeof(indent + 1, prefix, type);
273 print_ifopen(indent, type);
274 print_ifarg(objname);
277 print_ifclose(indent);
282 emit_enum(definition *def __unused)
284 print_ifopen(1, "enum");
285 print_ifarg("(enum_t *)objp");
290 emit_program(definition *def)
296 for (vlist = def->def.pr.versions; vlist != NULL; vlist = vlist->next)
297 for (plist = vlist->procs; plist != NULL; plist = plist->next) {
298 if (!newstyle || plist->arg_num < 2)
299 continue; /* old style, or single argument */
300 print_prog_header(plist);
301 for (dl = plist->args.decls; dl != NULL;
303 print_stat(1, &dl->decl);
310 emit_union(definition *def)
316 const char *vecformat = "objp->%s_u.%s";
317 const char *format = "&objp->%s_u.%s";
319 print_stat(1, &def->def.un.enum_decl);
320 f_print(fout, "\tswitch (objp->%s) {\n", def->def.un.enum_decl.name);
321 for (cl = def->def.un.cases; cl != NULL; cl = cl->next) {
323 f_print(fout, "\tcase %s:\n", cl->case_name);
324 if (cl->contflag == 1) /* a continued case statement */
327 if (!streq(cs->type, "void")) {
328 object = xmalloc(strlen(def->def_name) +
329 strlen(format) + strlen(cs->name) + 1);
330 if (isvectordef (cs->type, cs->rel)) {
331 s_print(object, vecformat, def->def_name,
334 s_print(object, format, def->def_name,
337 print_ifstat(2, cs->prefix, cs->type, cs->rel,
338 cs->array_max, object, cs->name);
341 f_print(fout, "\t\tbreak;\n");
343 dflt = def->def.un.default_decl;
345 if (!streq(dflt->type, "void")) {
346 f_print(fout, "\tdefault:\n");
347 object = xmalloc(strlen(def->def_name) +
348 strlen(format) + strlen(dflt->name) + 1);
349 if (isvectordef (dflt->type, dflt->rel)) {
350 s_print(object, vecformat, def->def_name,
353 s_print(object, format, def->def_name,
357 print_ifstat(2, dflt->prefix, dflt->type, dflt->rel,
358 dflt->array_max, object, dflt->name);
360 f_print(fout, "\t\tbreak;\n");
362 f_print(fout, "\tdefault:\n");
363 f_print(fout, "\t\tbreak;\n");
366 f_print(fout, "\tdefault:\n");
367 f_print(fout, "\t\treturn (FALSE);\n");
370 f_print(fout, "\t}\n");
374 inline_struct(definition *def, int flag)
378 decl_list *cur, *psav;
387 f_print(fout, "\n\tif (xdrs->x_op == XDR_ENCODE) {\n");
389 f_print(fout, "\t\treturn (TRUE);\n\t} else if (xdrs->x_op == XDR_DECODE) {\n");
394 for (dl = def->def.st.decls; dl != NULL; dl = dl->next) { /* xxx */
395 /* now walk down the list and check for basic types */
396 if ((dl->decl.prefix == NULL) &&
397 ((ptr = find_type(dl->decl.type)) != NULL) &&
398 ((dl->decl.rel == REL_ALIAS) ||
399 (dl->decl.rel == REL_VECTOR))){
404 if (dl->decl.rel == REL_ALIAS)
407 /* this code is required to handle arrays */
413 if (ptr->length != 1)
414 s_print(ptemp, "%s%s * %d",
415 plus, dl->decl.array_max,
418 s_print(ptemp, "%s%s", plus,
421 /* now concatenate to sizestr !!!! */
422 if (sizestr == NULL) {
423 sizestr = xstrdup(ptemp);
426 sizestr = xrealloc(sizestr,
429 sizestr = strcat(sizestr, ptemp);
430 /* build up length of array */
435 if (sizestr == NULL && size < inline_size){
437 * don't expand into inline code
438 * if size < inline_size
441 print_stat(indent + 1, &cur->decl);
445 /* were already looking at a xdr_inlineable structure */
446 tabify(fout, indent + 1);
448 f_print(fout, "buf = XDR_INLINE(xdrs, %d * BYTES_PER_XDR_UNIT);",
453 "buf = XDR_INLINE(xdrs, (%s) * BYTES_PER_XDR_UNIT);",
457 "buf = XDR_INLINE(xdrs, (%d + (%s)) * BYTES_PER_XDR_UNIT);",
462 tabify(fout, indent + 1);
464 "if (buf == NULL) {\n");
468 print_stat(indent + 2, &cur->decl);
472 f_print(fout, "\n\t\t} else {\n");
476 emit_inline(indent + 2, &cur->decl, flag);
480 tabify(fout, indent + 1);
481 f_print(fout, "}\n");
487 print_stat(indent + 1, &dl->decl);
492 if (sizestr == NULL && size < inline_size){
493 /* don't expand into inline code if size < inline_size */
495 print_stat(indent + 1, &cur->decl);
499 /* were already looking at a xdr_inlineable structure */
501 f_print(fout, "\t\tbuf = XDR_INLINE(xdrs, %d * BYTES_PER_XDR_UNIT);",
506 "\t\tbuf = XDR_INLINE(xdrs, (%s) * BYTES_PER_XDR_UNIT);",
510 "\t\tbuf = XDR_INLINE(xdrs, (%d + (%s)) * BYTES_PER_XDR_UNIT);",
513 f_print(fout, "\n\t\tif (buf == NULL) {\n");
516 print_stat(indent + 2, &cur->decl);
519 f_print(fout, "\t\t} else {\n");
523 emit_inline(indent + 2, &cur->decl, flag);
526 f_print(fout, "\t\t}\n");
532 emit_struct(definition *def)
539 if (inline_size == 0) {
540 /* No xdr_inlining at all */
541 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
542 print_stat(1, &dl->decl);
546 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
547 if (dl->decl.rel == REL_VECTOR){
548 f_print(fout, "\tint i;\n");
555 * Make a first pass and see if inling is possible.
557 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
558 if ((dl->decl.prefix == NULL) &&
559 ((ptr = find_type(dl->decl.type)) != NULL) &&
560 ((dl->decl.rel == REL_ALIAS)||
561 (dl->decl.rel == REL_VECTOR))){
562 if (dl->decl.rel == REL_ALIAS)
566 break; /* can be inlined */
569 if (size >= inline_size){
571 break; /* can be inlined */
575 if (size >= inline_size)
578 if (can_inline == 0){ /* can not inline, drop back to old mode */
579 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
580 print_stat(1, &dl->decl);
585 for (j = 0; j < 2; j++){
586 inline_struct(def, flag);
591 f_print(fout, "\t\treturn (TRUE);\n\t}\n\n");
593 /* now take care of XDR_FREE case */
595 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
596 print_stat(1, &dl->decl);
601 emit_typedef(definition *def)
603 const char *prefix = def->def.ty.old_prefix;
604 const char *type = def->def.ty.old_type;
605 const char *amax = def->def.ty.array_max;
606 relation rel = def->def.ty.rel;
608 print_ifstat(1, prefix, type, rel, amax, "objp", def->def_name);
612 print_stat(int indent, declaration *dec)
614 const char *prefix = dec->prefix;
615 const char *type = dec->type;
616 const char *amax = dec->array_max;
617 relation rel = dec->rel;
620 if (isvectordef(type, rel)) {
621 s_print(name, "objp->%s", dec->name);
623 s_print(name, "&objp->%s", dec->name);
625 print_ifstat(indent, prefix, type, rel, amax, name, dec->name);
629 char *upcase(const char *);
632 emit_inline(int indent, declaration *decl, int flag)
636 emit_single_in_line(indent, decl, flag, REL_ALIAS);
639 tabify(fout, indent);
640 f_print(fout, "{\n");
641 tabify(fout, indent + 1);
642 f_print(fout, "%s *genp;\n\n", decl->type);
643 tabify(fout, indent + 1);
645 "for (i = 0, genp = objp->%s;\n", decl->name);
646 tabify(fout, indent + 2);
647 f_print(fout, "i < %s; i++) {\n", decl->array_max);
648 emit_single_in_line(indent + 2, decl, flag, REL_VECTOR);
649 tabify(fout, indent + 1);
650 f_print(fout, "}\n");
651 tabify(fout, indent);
652 f_print(fout, "}\n");
660 emit_single_in_line(int indent, declaration *decl, int flag, relation rel)
664 tabify(fout, indent);
666 f_print(fout, "IXDR_PUT_");
668 if (rel == REL_ALIAS)
669 f_print(fout, "objp->%s = IXDR_GET_", decl->name);
671 f_print(fout, "*genp++ = IXDR_GET_");
673 upp_case = upcase(decl->type);
676 if (strcmp(upp_case, "INT") == 0)
679 upp_case = strdup("LONG");
682 if (strcmp(upp_case, "U_INT") == 0)
685 upp_case = strdup("U_LONG");
688 if (rel == REL_ALIAS)
690 "%s(buf, objp->%s);\n", upp_case, decl->name);
692 f_print(fout, "%s(buf, *genp++);\n", upp_case);
695 f_print(fout, "%s(buf);\n", upp_case);
700 upcase(const char *str)
704 ptr = (char *)xmalloc(strlen(str)+1);
708 *ptr++ = toupper(*str++);