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
34 #if defined(LIBC_SCCS) && !defined(lint)
35 static const char cout_rcsid
[] =
40 * rpc_cout.c, XDR routine outputter for the RPC protocol compiler
45 #include "rpc_parse.h"
49 static void emit_enum (definition
* def
);
50 static void emit_program (const definition
* def
);
51 static void emit_union (const definition
* def
);
52 static void emit_struct (definition
* def
);
53 static void emit_typedef (const definition
* def
);
54 static void emit_inline (int indent
, declaration
* decl
, int flag
);
55 static void emit_single_in_line (int indent
, declaration
*decl
, int flag
,
57 static int findtype (const definition
* def
, const char *type
);
58 static int undefined (const char *type
);
59 static void print_generic_header (const char *procname
, int pointerp
);
60 static void print_ifopen (int indent
, const char *name
);
61 static void print_ifarg (const char *arg
);
62 static void print_ifsizeof (int indent
, const char *prefix
, const char *type
);
63 static void print_ifclose (int indent
);
64 static void print_ifstat (int indent
, const char *prefix
, const char *type
,
65 relation rel
, const char *amax
,
66 const char *objname
, const char *name
);
67 static void print_stat (int indent
, const declaration
* dec
);
68 static void print_header (const definition
* def
);
69 static void print_trailer (void);
70 static char *upcase (const char *str
);
73 * Emit the C-routine for the given definition
76 emit (definition
* def
)
78 if (def
->def_kind
== DEF_CONST
)
82 if (def
->def_kind
== DEF_PROGRAM
)
87 if (def
->def_kind
== DEF_TYPEDEF
)
89 /* now we need to handle declarations like
90 struct typedef foo foo;
91 since we don't want this to be expanded
92 into 2 calls to xdr_foo */
94 if (strcmp (def
->def
.ty
.old_type
, def
->def_name
) == 0)
99 switch (def
->def_kind
)
121 findtype (const definition
* def
, const char *type
)
123 if (def
->def_kind
== DEF_PROGRAM
|| def
->def_kind
== DEF_CONST
)
129 return (streq (def
->def_name
, type
));
134 undefined (const char *type
)
137 def
= (definition
*) FINDVAL (defined
, type
, findtype
);
138 return (def
== NULL
);
143 print_generic_header (const char *procname
, int pointerp
)
145 f_print (fout
, "\n");
146 f_print (fout
, "bool_t\n");
149 f_print (fout
, "xdr_%s (", procname
);
150 f_print (fout
, "XDR *xdrs, ");
151 f_print (fout
, "%s ", procname
);
154 f_print (fout
, "objp)\n{\n");
158 f_print (fout
, "xdr_%s (xdrs, objp)\n", procname
);
159 f_print (fout
, "\tXDR *xdrs;\n");
160 f_print (fout
, "\t%s ", procname
);
163 f_print (fout
, "objp;\n{\n");
168 print_header (const definition
* def
)
170 print_generic_header (def
->def_name
,
171 def
->def_kind
!= DEF_TYPEDEF
||
172 !isvectordef (def
->def
.ty
.old_type
,
175 /* Now add Inline support */
179 /*May cause lint to complain. but ... */
180 f_print (fout
, "\tregister int32_t *buf;\n\n");
184 print_prog_header (const proc_list
* plist
)
186 print_generic_header (plist
->args
.argname
, 1);
192 f_print (fout
, "\treturn TRUE;\n");
193 f_print (fout
, "}\n");
198 print_ifopen (int indent
, const char *name
)
200 tabify (fout
, indent
);
201 f_print (fout
, " if (!xdr_%s (xdrs", name
);
205 print_ifarg (const char *arg
)
207 f_print (fout
, ", %s", arg
);
211 print_ifsizeof (int indent
, const char *prefix
, const char *type
)
215 fprintf (fout
, ",\n");
216 tabify (fout
, indent
);
219 fprintf (fout
, ", ");
221 if (streq (type
, "bool"))
222 fprintf (fout
, "sizeof (bool_t), (xdrproc_t) xdr_bool");
225 fprintf (fout
, "sizeof (");
226 if (undefined (type
) && prefix
)
228 f_print (fout
, "%s ", prefix
);
230 fprintf (fout
, "%s), (xdrproc_t) xdr_%s", type
, type
);
235 print_ifclose (int indent
)
237 f_print (fout
, "))\n");
238 tabify (fout
, indent
);
239 f_print (fout
, "\t return FALSE;\n");
243 print_ifstat (int indent
, const char *prefix
, const char *type
, relation rel
,
244 const char *amax
, const char *objname
, const char *name
)
246 const char *alt
= NULL
;
251 print_ifopen (indent
, "pointer");
252 print_ifarg ("(char **)");
253 f_print (fout
, "%s", objname
);
254 print_ifsizeof (0, prefix
, type
);
257 if (streq (type
, "string"))
261 else if (streq (type
, "opaque"))
267 print_ifopen (indent
, alt
);
268 print_ifarg (objname
);
272 print_ifopen (indent
, "vector");
273 print_ifarg ("(char *)");
274 f_print (fout
, "%s", objname
);
279 print_ifsizeof (indent
+ 1, prefix
, type
);
283 if (streq (type
, "string"))
287 else if (streq (type
, "opaque"))
291 if (streq (type
, "string"))
293 print_ifopen (indent
, alt
);
294 print_ifarg (objname
);
300 print_ifopen (indent
, alt
);
304 print_ifopen (indent
, "array");
306 print_ifarg ("(char **)");
309 f_print (fout
, "%s.%s_val, (u_int *) %s.%s_len",
310 objname
, name
, objname
, name
);
314 f_print (fout
, "&%s->%s_val, (u_int *) &%s->%s_len",
315 objname
, name
, objname
, name
);
321 print_ifsizeof (indent
+ 1, prefix
, type
);
325 print_ifopen (indent
, type
);
326 print_ifarg (objname
);
329 print_ifclose (indent
);
333 emit_enum (definition
* def
)
337 print_ifopen (1, "enum");
338 print_ifarg ("(enum_t *) objp");
343 emit_program (const definition
* def
)
349 for (vlist
= def
->def
.pr
.versions
; vlist
!= NULL
; vlist
= vlist
->next
)
350 for (plist
= vlist
->procs
; plist
!= NULL
; plist
= plist
->next
)
352 if (!newstyle
|| plist
->arg_num
< 2)
353 continue; /* old style, or single argument */
354 print_prog_header (plist
);
355 for (dl
= plist
->args
.decls
; dl
!= NULL
;
357 print_stat (1, &dl
->decl
);
363 emit_union (const definition
* def
)
369 const char *vecformat
= "objp->%s_u.%s";
370 const char *format
= "&objp->%s_u.%s";
372 print_stat (1, &def
->def
.un
.enum_decl
);
373 f_print (fout
, "\tswitch (objp->%s) {\n", def
->def
.un
.enum_decl
.name
);
374 for (cl
= def
->def
.un
.cases
; cl
!= NULL
; cl
= cl
->next
)
377 f_print (fout
, "\tcase %s:\n", cl
->case_name
);
378 if (cl
->contflag
== 1) /* a continued case statement */
381 if (!streq (cs
->type
, "void"))
383 object
= alloc (strlen (def
->def_name
) + strlen (format
) +
384 strlen (cs
->name
) + 1);
385 if (isvectordef (cs
->type
, cs
->rel
))
387 s_print (object
, vecformat
, def
->def_name
,
392 s_print (object
, format
, def
->def_name
,
395 print_ifstat (2, cs
->prefix
, cs
->type
, cs
->rel
, cs
->array_max
,
399 f_print (fout
, "\t\tbreak;\n");
401 dflt
= def
->def
.un
.default_decl
;
404 if (!streq (dflt
->type
, "void"))
406 f_print (fout
, "\tdefault:\n");
407 object
= alloc (strlen (def
->def_name
) + strlen (format
) +
408 strlen (dflt
->name
) + 1);
409 if (isvectordef (dflt
->type
, dflt
->rel
))
411 s_print (object
, vecformat
, def
->def_name
,
416 s_print (object
, format
, def
->def_name
,
420 print_ifstat (2, dflt
->prefix
, dflt
->type
, dflt
->rel
,
421 dflt
->array_max
, object
, dflt
->name
);
423 f_print (fout
, "\t\tbreak;\n");
425 #ifdef __GNU_LIBRARY__
428 f_print (fout
, "\tdefault:\n");
429 f_print (fout
, "\t\tbreak;\n");
435 f_print (fout
, "\tdefault:\n");
436 f_print (fout
, "\t\treturn FALSE;\n");
439 f_print (fout
, "\t}\n");
443 inline_struct (definition
*def
, int flag
)
447 decl_list
*cur
= NULL
;
456 f_print (fout
, "\n\tif (xdrs->x_op == XDR_ENCODE) {\n");
459 "\t\treturn TRUE;\n\t} else if (xdrs->x_op == XDR_DECODE) {\n");
464 for (dl
= def
->def
.st
.decls
; dl
!= NULL
; dl
= dl
->next
)
466 /* now walk down the list and check for basic types */
467 if ((dl
->decl
.prefix
== NULL
) &&
468 ((ptr
= find_type (dl
->decl
.type
)) != NULL
) &&
469 ((dl
->decl
.rel
== REL_ALIAS
) || (dl
->decl
.rel
== REL_VECTOR
)))
475 if (dl
->decl
.rel
== REL_ALIAS
)
479 /* this is required to handle arrays */
485 if (ptr
->length
!= 1)
486 s_print (ptemp
, " %s %s * %d", plus
, dl
->decl
.array_max
,
489 s_print (ptemp
, " %s%s ", plus
, dl
->decl
.array_max
);
491 /*now concatenate to sizestr !!!! */
493 sizestr
= strdup (ptemp
);
496 sizestr
= realloc (sizestr
, strlen (sizestr
) +
500 f_print (stderr
, "Fatal error : no memory \n");
503 sizestr
= strcat (sizestr
, ptemp
);
504 /*build up length of array */
512 if (sizestr
== NULL
&& size
< inlineflag
)
514 /* don't expand into inline code if size < inlineflag */
517 print_stat (indent
+ 1, &cur
->decl
);
523 /* were already looking at a xdr_inlineable structure */
524 tabify (fout
, indent
+ 1);
526 f_print (fout
, "buf = XDR_INLINE (xdrs, %d * BYTES_PER_XDR_UNIT);", size
);
529 "buf = XDR_INLINE (xdrs, (%s) * BYTES_PER_XDR_UNIT);",
533 "buf = XDR_INLINE (xdrs, (%d + (%s)) * BYTES_PER_XDR_UNIT);",
535 f_print (fout
, "\n");
536 tabify (fout
, indent
+ 1);
537 fprintf (fout
, "if (buf == NULL) {\n");
541 print_stat (indent
+ 2, &cur
->decl
);
545 f_print (fout
, "\n\t\t} else {\n");
549 emit_inline (indent
+ 1, &cur
->decl
, flag
);
552 tabify (fout
, indent
+ 1);
553 f_print (fout
, "}\n");
559 print_stat (indent
+ 1, &dl
->decl
);
564 if (sizestr
== NULL
&& size
< inlineflag
)
566 /* don't expand into inline code if size < inlineflag */
569 print_stat (indent
+ 1, &cur
->decl
);
575 /* were already looking at a xdr_inlineable structure */
578 "\t\tbuf = XDR_INLINE (xdrs, %d * BYTES_PER_XDR_UNIT);",
582 "\t\tbuf = XDR_INLINE (xdrs, (%s) * BYTES_PER_XDR_UNIT);",
586 "\t\tbuf = XDR_INLINE (xdrs, (%d + %s)* BYTES_PER_XDR_UNIT);",
588 f_print (fout
, "\n\t\tif (buf == NULL) {\n");
592 print_stat (indent
+ 2, &cur
->decl
);
595 f_print (fout
, "\t\t} else {\n");
600 emit_inline (indent
+ 2, &cur
->decl
, flag
);
603 f_print (fout
, "\t\t}\n");
608 /* this may be const. i haven't traced this one through yet. */
611 emit_struct (definition
* def
)
621 /* No xdr_inlining at all */
622 for (dl
= def
->def
.st
.decls
; dl
!= NULL
; dl
= dl
->next
)
623 print_stat (1, &dl
->decl
);
627 for (dl
= def
->def
.st
.decls
; dl
!= NULL
; dl
= dl
->next
)
628 if (dl
->decl
.rel
== REL_VECTOR
)
630 f_print (fout
, "\tint i;\n");
637 * Make a first pass and see if inling is possible.
639 for (dl
= def
->def
.st
.decls
; dl
!= NULL
; dl
= dl
->next
)
640 if ((dl
->decl
.prefix
== NULL
) &&
641 ((ptr
= find_type (dl
->decl
.type
)) != NULL
) &&
642 ((dl
->decl
.rel
== REL_ALIAS
) || (dl
->decl
.rel
== REL_VECTOR
)))
644 if (dl
->decl
.rel
== REL_ALIAS
)
649 break; /* can be inlined */
654 if (size
>= inlineflag
)
657 break; /* can be inlined */
661 if (size
> inlineflag
)
665 { /* can not inline, drop back to old mode */
666 for (dl
= def
->def
.st
.decls
; dl
!= NULL
; dl
= dl
->next
)
667 print_stat (1, &dl
->decl
);
672 for (j
= 0; j
< 2; j
++)
674 inline_struct (def
, flag
);
679 f_print (fout
, "\t return TRUE;\n\t}\n\n");
681 /* now take care of XDR_FREE case */
683 for (dl
= def
->def
.st
.decls
; dl
!= NULL
; dl
= dl
->next
)
684 print_stat (1, &dl
->decl
);
688 emit_typedef (const definition
* def
)
690 const char *prefix
= def
->def
.ty
.old_prefix
;
691 const char *type
= def
->def
.ty
.old_type
;
692 const char *amax
= def
->def
.ty
.array_max
;
693 relation rel
= def
->def
.ty
.rel
;
695 print_ifstat (1, prefix
, type
, rel
, amax
, "objp", def
->def_name
);
699 print_stat (int indent
, const declaration
* dec
)
701 const char *prefix
= dec
->prefix
;
702 const char *type
= dec
->type
;
703 const char *amax
= dec
->array_max
;
704 relation rel
= dec
->rel
;
707 if (isvectordef (type
, rel
))
709 s_print (name
, "objp->%s", dec
->name
);
713 s_print (name
, "&objp->%s", dec
->name
);
715 print_ifstat (indent
, prefix
, type
, rel
, amax
, name
, dec
->name
);
720 emit_inline (int indent
, declaration
* decl
, int flag
)
725 emit_single_in_line (indent
, decl
, flag
, REL_ALIAS
);
728 tabify (fout
, indent
);
729 f_print (fout
, "{\n");
730 tabify (fout
, indent
+ 1);
731 f_print (fout
, "register %s *genp;\n\n", decl
->type
);
732 tabify (fout
, indent
+ 1);
734 "for (i = 0, genp = objp->%s;\n", decl
->name
);
735 tabify (fout
, indent
+ 2);
736 f_print (fout
, "i < %s; ++i) {\n", decl
->array_max
);
737 emit_single_in_line (indent
+ 2, decl
, flag
, REL_VECTOR
);
738 tabify (fout
, indent
+ 1);
739 f_print (fout
, "}\n");
740 tabify (fout
, indent
);
741 f_print (fout
, "}\n");
745 /* ?... do nothing I guess */
750 emit_single_in_line (int indent
, declaration
*decl
, int flag
, relation rel
)
755 tabify (fout
, indent
);
757 f_print (fout
, "IXDR_PUT_");
760 if (rel
== REL_ALIAS
)
761 f_print (fout
, "objp->%s = IXDR_GET_", decl
->name
);
763 f_print (fout
, "*genp++ = IXDR_GET_");
766 upp_case
= upcase (decl
->type
);
769 if (!strcmp (upp_case
, "INT"))
773 /* Casting is safe since the `freed' flag is set. */
774 upp_case
= (char *) "LONG";
777 if (!strcmp (upp_case
, "U_INT"))
781 /* Casting is safe since the `freed' flag is set. */
782 upp_case
= (char *) "U_LONG";
787 if (rel
== REL_ALIAS
)
788 f_print (fout
, "%s(buf, objp->%s);\n", upp_case
, decl
->name
);
790 f_print (fout
, "%s(buf, *genp++);\n", upp_case
);
794 f_print (fout
, "%s(buf);\n", upp_case
);
803 upcase (const char *str
)
806 ptr
= malloc (strlen (str
) + 1);
809 f_print (stderr
, "malloc failed\n");
814 *ptr
++ = toupper (*str
++);