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 or with the express written consent of
8 * Sun Microsystems, Inc.
10 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
11 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
12 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14 * Sun RPC is provided with no support and without any obligation on the
15 * part of Sun Microsystems, Inc. to assist in its use, correction,
16 * modification or enhancement.
18 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
19 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
20 * OR ANY PART THEREOF.
22 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
23 * or profits or other special, indirect and consequential damages, even if
24 * Sun has been advised of the possibility of such damages.
26 * Sun Microsystems, Inc.
28 * Mountain View, California 94043
32 * From: @(#)rpc_cout.c 1.13 89/02/22 (C) 1987 SMI
38 * rpc_cout.c, XDR routine outputter for the RPC protocol compiler
43 #include "rpc_parse.h"
47 static void emit_enum (definition
* def
);
48 static void emit_program (const definition
* def
);
49 static void emit_union (const definition
* def
);
50 static void emit_struct (definition
* def
);
51 static void emit_typedef (const definition
* def
);
52 static void emit_inline (int indent
, declaration
* decl
, int flag
);
53 static void emit_single_in_line (int indent
, declaration
*decl
, int flag
,
55 static int findtype (const definition
* def
, const char *type
);
56 static int undefined (const char *type
);
57 static void print_generic_header (const char *procname
, int pointerp
);
58 static void print_ifopen (int indent
, const char *name
);
59 static void print_ifarg (const char *arg
);
60 static void print_ifsizeof (int indent
, const char *prefix
, const char *type
);
61 static void print_ifclose (int indent
);
62 static void print_ifstat (int indent
, const char *prefix
, const char *type
,
63 relation rel
, const char *amax
,
64 const char *objname
, const char *name
);
65 static void print_stat (int indent
, const declaration
* dec
);
66 static void print_header (const definition
* def
);
67 static void print_trailer (void);
68 static char *upcase (const char *str
);
71 * Emit the C-routine for the given definition
74 emit (definition
* def
)
76 if (def
->def_kind
== DEF_CONST
)
80 if (def
->def_kind
== DEF_PROGRAM
)
85 if (def
->def_kind
== DEF_TYPEDEF
)
87 /* now we need to handle declarations like
88 struct typedef foo foo;
89 since we don't want this to be expanded
90 into 2 calls to xdr_foo */
92 if (strcmp (def
->def
.ty
.old_type
, def
->def_name
) == 0)
97 switch (def
->def_kind
)
119 findtype (const definition
* def
, const char *type
)
121 if (def
->def_kind
== DEF_PROGRAM
|| def
->def_kind
== DEF_CONST
)
127 return (streq (def
->def_name
, type
));
132 undefined (const char *type
)
135 def
= (definition
*) FINDVAL (defined
, type
, findtype
);
136 return (def
== NULL
);
141 print_generic_header (const char *procname
, int pointerp
)
143 f_print (fout
, "\n");
144 f_print (fout
, "bool_t\n");
147 f_print (fout
, "xdr_%s (", procname
);
148 f_print (fout
, "XDR *xdrs, ");
149 f_print (fout
, "%s ", procname
);
152 f_print (fout
, "objp)\n{\n");
156 f_print (fout
, "xdr_%s (xdrs, objp)\n", procname
);
157 f_print (fout
, "\tXDR *xdrs;\n");
158 f_print (fout
, "\t%s ", procname
);
161 f_print (fout
, "objp;\n{\n");
166 print_header (const definition
* def
)
168 print_generic_header (def
->def_name
,
169 def
->def_kind
!= DEF_TYPEDEF
||
170 !isvectordef (def
->def
.ty
.old_type
,
173 /* Now add Inline support */
177 /*May cause lint to complain. but ... */
178 f_print (fout
, "\t register long *buf;\n\n");
182 print_prog_header (const proc_list
* plist
)
184 print_generic_header (plist
->args
.argname
, 1);
190 f_print (fout
, "\treturn TRUE;\n");
191 f_print (fout
, "}\n");
196 print_ifopen (int indent
, const char *name
)
198 tabify (fout
, indent
);
199 f_print (fout
, " if (!xdr_%s (xdrs", name
);
203 print_ifarg (const char *arg
)
205 f_print (fout
, ", %s", arg
);
209 print_ifsizeof (int indent
, const char *prefix
, const char *type
)
213 fprintf (fout
, ",\n");
214 tabify (fout
, indent
);
217 fprintf (fout
, ", ");
219 if (streq (type
, "bool"))
220 fprintf (fout
, "sizeof (bool_t), (xdrproc_t) xdr_bool");
223 fprintf (fout
, "sizeof (");
224 if (undefined (type
) && prefix
)
226 f_print (fout
, "%s ", prefix
);
228 fprintf (fout
, "%s), (xdrproc_t) xdr_%s", type
, type
);
233 print_ifclose (int indent
)
235 f_print (fout
, "))\n");
236 tabify (fout
, indent
);
237 f_print (fout
, "\t return FALSE;\n");
241 print_ifstat (int indent
, const char *prefix
, const char *type
, relation rel
,
242 const char *amax
, const char *objname
, const char *name
)
244 const char *alt
= NULL
;
249 print_ifopen (indent
, "pointer");
250 print_ifarg ("(char **)");
251 f_print (fout
, "%s", objname
);
252 print_ifsizeof (0, prefix
, type
);
255 if (streq (type
, "string"))
259 else if (streq (type
, "opaque"))
265 print_ifopen (indent
, alt
);
266 print_ifarg (objname
);
270 print_ifopen (indent
, "vector");
271 print_ifarg ("(char *)");
272 f_print (fout
, "%s", objname
);
277 print_ifsizeof (indent
+ 1, prefix
, type
);
281 if (streq (type
, "string"))
285 else if (streq (type
, "opaque"))
289 if (streq (type
, "string"))
291 print_ifopen (indent
, alt
);
292 print_ifarg (objname
);
298 print_ifopen (indent
, alt
);
302 print_ifopen (indent
, "array");
304 print_ifarg ("(char **)");
307 f_print (fout
, "%s.%s_val, (u_int *) %s.%s_len",
308 objname
, name
, objname
, name
);
312 f_print (fout
, "&%s->%s_val, (u_int *) &%s->%s_len",
313 objname
, name
, objname
, name
);
319 print_ifsizeof (indent
+ 1, prefix
, type
);
323 print_ifopen (indent
, type
);
324 print_ifarg (objname
);
327 print_ifclose (indent
);
331 emit_enum (definition
* def
)
335 print_ifopen (1, "enum");
336 print_ifarg ("(enum_t *) objp");
341 emit_program (const definition
* def
)
347 for (vlist
= def
->def
.pr
.versions
; vlist
!= NULL
; vlist
= vlist
->next
)
348 for (plist
= vlist
->procs
; plist
!= NULL
; plist
= plist
->next
)
350 if (!newstyle
|| plist
->arg_num
< 2)
351 continue; /* old style, or single argument */
352 print_prog_header (plist
);
353 for (dl
= plist
->args
.decls
; dl
!= NULL
;
355 print_stat (1, &dl
->decl
);
361 emit_union (const definition
* def
)
367 const char *vecformat
= "objp->%s_u.%s";
368 const char *format
= "&objp->%s_u.%s";
370 print_stat (1, &def
->def
.un
.enum_decl
);
371 f_print (fout
, "\tswitch (objp->%s) {\n", def
->def
.un
.enum_decl
.name
);
372 for (cl
= def
->def
.un
.cases
; cl
!= NULL
; cl
= cl
->next
)
375 f_print (fout
, "\tcase %s:\n", cl
->case_name
);
376 if (cl
->contflag
== 1) /* a continued case statement */
379 if (!streq (cs
->type
, "void"))
381 object
= alloc (strlen (def
->def_name
) + strlen (format
) +
382 strlen (cs
->name
) + 1);
383 if (isvectordef (cs
->type
, cs
->rel
))
385 s_print (object
, vecformat
, def
->def_name
,
390 s_print (object
, format
, def
->def_name
,
393 print_ifstat (2, cs
->prefix
, cs
->type
, cs
->rel
, cs
->array_max
,
397 f_print (fout
, "\t\tbreak;\n");
399 dflt
= def
->def
.un
.default_decl
;
402 if (!streq (dflt
->type
, "void"))
404 f_print (fout
, "\tdefault:\n");
405 object
= alloc (strlen (def
->def_name
) + strlen (format
) +
406 strlen (dflt
->name
) + 1);
407 if (isvectordef (dflt
->type
, dflt
->rel
))
409 s_print (object
, vecformat
, def
->def_name
,
414 s_print (object
, format
, def
->def_name
,
418 print_ifstat (2, dflt
->prefix
, dflt
->type
, dflt
->rel
,
419 dflt
->array_max
, object
, dflt
->name
);
421 f_print (fout
, "\t\tbreak;\n");
423 #ifdef __GNU_LIBRARY__
426 f_print (fout
, "\tdefault:\n");
427 f_print (fout
, "\t\tbreak;\n");
433 f_print (fout
, "\tdefault:\n");
434 f_print (fout
, "\t\treturn FALSE;\n");
437 f_print (fout
, "\t}\n");
441 inline_struct (definition
*def
, int flag
)
445 decl_list
*cur
= NULL
;
454 f_print (fout
, "\n\tif (xdrs->x_op == XDR_ENCODE) {\n");
457 "\t\treturn TRUE;\n\t} else if (xdrs->x_op == XDR_DECODE) {\n");
462 for (dl
= def
->def
.st
.decls
; dl
!= NULL
; dl
= dl
->next
)
464 /* now walk down the list and check for basic types */
465 if ((dl
->decl
.prefix
== NULL
) &&
466 ((ptr
= find_type (dl
->decl
.type
)) != NULL
) &&
467 ((dl
->decl
.rel
== REL_ALIAS
) || (dl
->decl
.rel
== REL_VECTOR
)))
473 if (dl
->decl
.rel
== REL_ALIAS
)
477 /* this is required to handle arrays */
483 if (ptr
->length
!= 1)
484 s_print (ptemp
, " %s %s * %d", plus
, dl
->decl
.array_max
,
487 s_print (ptemp
, " %s%s ", plus
, dl
->decl
.array_max
);
489 /*now concatenate to sizestr !!!! */
491 sizestr
= strdup (ptemp
);
494 sizestr
= realloc (sizestr
, strlen (sizestr
) +
498 f_print (stderr
, "Fatal error : no memory \n");
501 sizestr
= strcat (sizestr
, ptemp
);
502 /*build up length of array */
510 if (sizestr
== NULL
&& size
< inlineflag
)
512 /* don't expand into inline code if size < inlineflag */
515 print_stat (indent
+ 1, &cur
->decl
);
521 /* were already looking at a xdr_inlineable structure */
522 tabify (fout
, indent
+ 1);
524 f_print (fout
, "buf = XDR_INLINE (xdrs, %d * BYTES_PER_XDR_UNIT);", size
);
527 "buf = XDR_INLINE (xdrs, %s * BYTES_PER_XDR_UNIT);",
531 "buf = XDR_INLINE(xdrs, (%d + (%s)) * BYTES_PER_XDR_UNIT);",
533 f_print (fout
, "\n");
534 tabify (fout
, indent
+ 1);
535 fprintf (fout
, "if (buf == NULL) {\n");
539 print_stat (indent
+ 2, &cur
->decl
);
543 f_print (fout
, "\n\t\t} else {\n");
547 emit_inline (indent
+ 1, &cur
->decl
, flag
);
550 tabify (fout
, indent
+ 1);
551 f_print (fout
, "}\n");
557 print_stat (indent
+ 1, &dl
->decl
);
562 if (sizestr
== NULL
&& size
< inlineflag
)
564 /* don't expand into inline code if size < inlineflag */
567 print_stat (indent
+ 1, &cur
->decl
);
573 /* were already looking at a xdr_inlineable structure */
576 "\t\tbuf = XDR_INLINE(xdrs,%d * BYTES_PER_XDR_UNIT);",
580 "\t\tbuf = XDR_INLINE(xdrs,%s * BYTES_PER_XDR_UNIT);",
584 "\t\tbuf = XDR_INLINE(xdrs,(%d + %s)* BYTES_PER_XDR_UNIT);",
586 f_print (fout
, "\n\t\tif (buf == NULL) {\n");
590 print_stat (indent
+ 2, &cur
->decl
);
593 f_print (fout
, "\t\t} else {\n");
598 emit_inline (indent
+ 2, &cur
->decl
, flag
);
601 f_print (fout
, "\t\t}\n");
606 /* this may be const. i haven't traced this one through yet. */
609 emit_struct (definition
* def
)
619 /* No xdr_inlining at all */
620 for (dl
= def
->def
.st
.decls
; dl
!= NULL
; dl
= dl
->next
)
621 print_stat (1, &dl
->decl
);
625 for (dl
= def
->def
.st
.decls
; dl
!= NULL
; dl
= dl
->next
)
626 if (dl
->decl
.rel
== REL_VECTOR
)
628 f_print (fout
, "\tint i;\n");
635 * Make a first pass and see if inling is possible.
637 for (dl
= def
->def
.st
.decls
; dl
!= NULL
; dl
= dl
->next
)
638 if ((dl
->decl
.prefix
== NULL
) &&
639 ((ptr
= find_type (dl
->decl
.type
)) != NULL
) &&
640 ((dl
->decl
.rel
== REL_ALIAS
) || (dl
->decl
.rel
== REL_VECTOR
)))
642 if (dl
->decl
.rel
== REL_ALIAS
)
647 break; /* can be inlined */
652 if (size
>= inlineflag
)
655 break; /* can be inlined */
659 if (size
> inlineflag
)
663 { /* can not inline, drop back to old mode */
664 for (dl
= def
->def
.st
.decls
; dl
!= NULL
; dl
= dl
->next
)
665 print_stat (1, &dl
->decl
);
670 for (j
= 0; j
< 2; j
++)
672 inline_struct (def
, flag
);
677 f_print (fout
, "\t return TRUE;\n\t}\n\n");
679 /* now take care of XDR_FREE case */
681 for (dl
= def
->def
.st
.decls
; dl
!= NULL
; dl
= dl
->next
)
682 print_stat (1, &dl
->decl
);
686 emit_typedef (const definition
* def
)
688 const char *prefix
= def
->def
.ty
.old_prefix
;
689 const char *type
= def
->def
.ty
.old_type
;
690 const char *amax
= def
->def
.ty
.array_max
;
691 relation rel
= def
->def
.ty
.rel
;
693 print_ifstat (1, prefix
, type
, rel
, amax
, "objp", def
->def_name
);
697 print_stat (int indent
, const declaration
* dec
)
699 const char *prefix
= dec
->prefix
;
700 const char *type
= dec
->type
;
701 const char *amax
= dec
->array_max
;
702 relation rel
= dec
->rel
;
705 if (isvectordef (type
, rel
))
707 s_print (name
, "objp->%s", dec
->name
);
711 s_print (name
, "&objp->%s", dec
->name
);
713 print_ifstat (indent
, prefix
, type
, rel
, amax
, name
, dec
->name
);
718 emit_inline (int indent
, declaration
* decl
, int flag
)
723 emit_single_in_line (indent
, decl
, flag
, REL_ALIAS
);
726 tabify (fout
, indent
);
727 f_print (fout
, "{\n");
728 tabify (fout
, indent
+ 1);
729 f_print (fout
, "register %s *genp;\n\n", decl
->type
);
730 tabify (fout
, indent
+ 1);
732 "for (i = 0, genp = objp->%s;\n", decl
->name
);
733 tabify (fout
, indent
+ 2);
734 f_print (fout
, "i < %s; ++i) {\n", decl
->array_max
);
735 emit_single_in_line (indent
+ 2, decl
, flag
, REL_VECTOR
);
736 tabify (fout
, indent
+ 1);
737 f_print (fout
, "}\n");
738 tabify (fout
, indent
);
739 f_print (fout
, "}\n");
742 /* ?... do nothing I guess */
747 emit_single_in_line (int indent
, declaration
*decl
, int flag
, relation rel
)
752 tabify (fout
, indent
);
754 f_print (fout
, "IXDR_PUT_");
757 if (rel
== REL_ALIAS
)
758 f_print (fout
, "objp->%s = IXDR_GET_", decl
->name
);
760 f_print (fout
, "*genp++ = IXDR_GET_");
763 upp_case
= upcase (decl
->type
);
766 if (!strcmp (upp_case
, "INT"))
770 /* Casting is safe since the `freed' flag is set. */
771 upp_case
= (char *) "LONG";
774 if (!strcmp (upp_case
, "U_INT"))
778 /* Casting is safe since the `freed' flag is set. */
779 upp_case
= (char *) "U_LONG";
784 if (rel
== REL_ALIAS
)
785 f_print (fout
, "%s(buf, objp->%s);\n", upp_case
, decl
->name
);
787 f_print (fout
, "%s(buf, *genp++);\n", upp_case
);
791 f_print (fout
, "%s(buf);\n", upp_case
);
800 upcase (const char *str
)
803 ptr
= malloc (strlen (str
));
806 f_print (stderr
, "malloc failed\n");
811 *ptr
++ = toupper (*str
++);