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
36 * rpc_cout.c, XDR routine outputter for the RPC protocol compiler
41 #include "rpc_parse.h"
45 static void emit_enum (definition
* def
);
46 static void emit_program (const definition
* def
);
47 static void emit_union (const definition
* def
);
48 static void emit_struct (definition
* def
);
49 static void emit_typedef (const definition
* def
);
50 static void emit_inline (int indent
, declaration
* decl
, int flag
);
51 static void emit_single_in_line (int indent
, declaration
*decl
, int flag
,
53 static int findtype (const definition
* def
, const char *type
);
54 static int undefined (const char *type
);
55 static void print_generic_header (const char *procname
, int pointerp
);
56 static void print_ifopen (int indent
, const char *name
);
57 static void print_ifarg (const char *arg
);
58 static void print_ifsizeof (int indent
, const char *prefix
, const char *type
);
59 static void print_ifclose (int indent
);
60 static void print_ifstat (int indent
, const char *prefix
, const char *type
,
61 relation rel
, const char *amax
,
62 const char *objname
, const char *name
);
63 static void print_stat (int indent
, const declaration
* dec
);
64 static void print_header (const definition
* def
);
65 static void print_trailer (void);
66 static char *upcase (const char *str
);
69 * Emit the C-routine for the given definition
72 emit (definition
* def
)
74 if (def
->def_kind
== DEF_CONST
)
78 if (def
->def_kind
== DEF_PROGRAM
)
83 if (def
->def_kind
== DEF_TYPEDEF
)
85 /* now we need to handle declarations like
86 struct typedef foo foo;
87 since we don't want this to be expanded
88 into 2 calls to xdr_foo */
90 if (strcmp (def
->def
.ty
.old_type
, def
->def_name
) == 0)
95 switch (def
->def_kind
)
117 findtype (const definition
* def
, const char *type
)
119 if (def
->def_kind
== DEF_PROGRAM
|| def
->def_kind
== DEF_CONST
)
125 return (streq (def
->def_name
, type
));
130 undefined (const char *type
)
133 def
= (definition
*) FINDVAL (defined
, type
, findtype
);
134 return (def
== NULL
);
139 print_generic_header (const char *procname
, int pointerp
)
141 f_print (fout
, "\n");
142 f_print (fout
, "bool_t\n");
145 f_print (fout
, "xdr_%s (", procname
);
146 f_print (fout
, "XDR *xdrs, ");
147 f_print (fout
, "%s ", procname
);
150 f_print (fout
, "objp)\n{\n");
154 f_print (fout
, "xdr_%s (xdrs, objp)\n", procname
);
155 f_print (fout
, "\tXDR *xdrs;\n");
156 f_print (fout
, "\t%s ", procname
);
159 f_print (fout
, "objp;\n{\n");
164 print_header (const definition
* def
)
166 print_generic_header (def
->def_name
,
167 def
->def_kind
!= DEF_TYPEDEF
||
168 !isvectordef (def
->def
.ty
.old_type
,
171 /* Now add Inline support */
175 /*May cause lint to complain. but ... */
176 f_print (fout
, "\tregister int32_t *buf;\n\n");
180 print_prog_header (const proc_list
* plist
)
182 print_generic_header (plist
->args
.argname
, 1);
188 f_print (fout
, "\treturn TRUE;\n");
189 f_print (fout
, "}\n");
194 print_ifopen (int indent
, const char *name
)
196 tabify (fout
, indent
);
197 f_print (fout
, " if (!xdr_%s (xdrs", name
);
201 print_ifarg (const char *arg
)
203 f_print (fout
, ", %s", arg
);
207 print_ifsizeof (int indent
, const char *prefix
, const char *type
)
211 fprintf (fout
, ",\n");
212 tabify (fout
, indent
);
215 fprintf (fout
, ", ");
217 if (streq (type
, "bool"))
218 fprintf (fout
, "sizeof (bool_t), (xdrproc_t) xdr_bool");
221 fprintf (fout
, "sizeof (");
222 if (undefined (type
) && prefix
)
224 f_print (fout
, "%s ", prefix
);
226 fprintf (fout
, "%s), (xdrproc_t) xdr_%s", type
, type
);
231 print_ifclose (int indent
)
233 f_print (fout
, "))\n");
234 tabify (fout
, indent
);
235 f_print (fout
, "\t return FALSE;\n");
239 print_ifstat (int indent
, const char *prefix
, const char *type
, relation rel
,
240 const char *amax
, const char *objname
, const char *name
)
242 const char *alt
= NULL
;
247 print_ifopen (indent
, "pointer");
248 print_ifarg ("(char **)");
249 f_print (fout
, "%s", objname
);
250 print_ifsizeof (0, prefix
, type
);
253 if (streq (type
, "string"))
257 else if (streq (type
, "opaque"))
263 print_ifopen (indent
, alt
);
264 print_ifarg (objname
);
268 print_ifopen (indent
, "vector");
269 print_ifarg ("(char *)");
270 f_print (fout
, "%s", objname
);
275 print_ifsizeof (indent
+ 1, prefix
, type
);
279 if (streq (type
, "string"))
283 else if (streq (type
, "opaque"))
287 if (streq (type
, "string"))
289 print_ifopen (indent
, alt
);
290 print_ifarg (objname
);
296 print_ifopen (indent
, alt
);
300 print_ifopen (indent
, "array");
302 print_ifarg ("(char **)");
305 f_print (fout
, "%s.%s_val, (u_int *) %s.%s_len",
306 objname
, name
, objname
, name
);
310 f_print (fout
, "&%s->%s_val, (u_int *) &%s->%s_len",
311 objname
, name
, objname
, name
);
317 print_ifsizeof (indent
+ 1, prefix
, type
);
321 print_ifopen (indent
, type
);
322 print_ifarg (objname
);
325 print_ifclose (indent
);
329 emit_enum (definition
* def
)
333 print_ifopen (1, "enum");
334 print_ifarg ("(enum_t *) objp");
339 emit_program (const definition
* def
)
345 for (vlist
= def
->def
.pr
.versions
; vlist
!= NULL
; vlist
= vlist
->next
)
346 for (plist
= vlist
->procs
; plist
!= NULL
; plist
= plist
->next
)
348 if (!newstyle
|| plist
->arg_num
< 2)
349 continue; /* old style, or single argument */
350 print_prog_header (plist
);
351 for (dl
= plist
->args
.decls
; dl
!= NULL
;
353 print_stat (1, &dl
->decl
);
359 emit_union (const definition
* def
)
365 const char *vecformat
= "objp->%s_u.%s";
366 const char *format
= "&objp->%s_u.%s";
368 print_stat (1, &def
->def
.un
.enum_decl
);
369 f_print (fout
, "\tswitch (objp->%s) {\n", def
->def
.un
.enum_decl
.name
);
370 for (cl
= def
->def
.un
.cases
; cl
!= NULL
; cl
= cl
->next
)
373 f_print (fout
, "\tcase %s:\n", cl
->case_name
);
374 if (cl
->contflag
== 1) /* a continued case statement */
377 if (!streq (cs
->type
, "void"))
379 object
= alloc (strlen (def
->def_name
) + strlen (format
) +
380 strlen (cs
->name
) + 1);
381 if (isvectordef (cs
->type
, cs
->rel
))
383 s_print (object
, vecformat
, def
->def_name
,
388 s_print (object
, format
, def
->def_name
,
391 print_ifstat (2, cs
->prefix
, cs
->type
, cs
->rel
, cs
->array_max
,
395 f_print (fout
, "\t\tbreak;\n");
397 dflt
= def
->def
.un
.default_decl
;
400 if (!streq (dflt
->type
, "void"))
402 f_print (fout
, "\tdefault:\n");
403 object
= alloc (strlen (def
->def_name
) + strlen (format
) +
404 strlen (dflt
->name
) + 1);
405 if (isvectordef (dflt
->type
, dflt
->rel
))
407 s_print (object
, vecformat
, def
->def_name
,
412 s_print (object
, format
, def
->def_name
,
416 print_ifstat (2, dflt
->prefix
, dflt
->type
, dflt
->rel
,
417 dflt
->array_max
, object
, dflt
->name
);
419 f_print (fout
, "\t\tbreak;\n");
421 #ifdef __GNU_LIBRARY__
424 f_print (fout
, "\tdefault:\n");
425 f_print (fout
, "\t\tbreak;\n");
431 f_print (fout
, "\tdefault:\n");
432 f_print (fout
, "\t\treturn FALSE;\n");
435 f_print (fout
, "\t}\n");
439 inline_struct (definition
*def
, int flag
)
443 decl_list
*cur
= NULL
;
452 f_print (fout
, "\n\tif (xdrs->x_op == XDR_ENCODE) {\n");
455 "\t\treturn TRUE;\n\t} else if (xdrs->x_op == XDR_DECODE) {\n");
460 for (dl
= def
->def
.st
.decls
; dl
!= NULL
; dl
= dl
->next
)
462 /* now walk down the list and check for basic types */
463 if ((dl
->decl
.prefix
== NULL
) &&
464 ((ptr
= find_type (dl
->decl
.type
)) != NULL
) &&
465 ((dl
->decl
.rel
== REL_ALIAS
) || (dl
->decl
.rel
== REL_VECTOR
)))
471 if (dl
->decl
.rel
== REL_ALIAS
)
475 /* this is required to handle arrays */
481 if (ptr
->length
!= 1)
482 s_print (ptemp
, " %s %s * %d", plus
, dl
->decl
.array_max
,
485 s_print (ptemp
, " %s%s ", plus
, dl
->decl
.array_max
);
487 /*now concatenate to sizestr !!!! */
489 sizestr
= strdup (ptemp
);
492 sizestr
= realloc (sizestr
, strlen (sizestr
) +
496 f_print (stderr
, "Fatal error : no memory \n");
499 sizestr
= strcat (sizestr
, ptemp
);
500 /*build up length of array */
508 if (sizestr
== NULL
&& size
< inlineflag
)
510 /* don't expand into inline code if size < inlineflag */
513 print_stat (indent
+ 1, &cur
->decl
);
519 /* were already looking at a xdr_inlineable structure */
520 tabify (fout
, indent
+ 1);
522 f_print (fout
, "buf = XDR_INLINE (xdrs, %d * BYTES_PER_XDR_UNIT);", size
);
525 "buf = XDR_INLINE (xdrs, (%s) * BYTES_PER_XDR_UNIT);",
529 "buf = XDR_INLINE (xdrs, (%d + (%s)) * BYTES_PER_XDR_UNIT);",
531 f_print (fout
, "\n");
532 tabify (fout
, indent
+ 1);
533 fprintf (fout
, "if (buf == NULL) {\n");
537 print_stat (indent
+ 2, &cur
->decl
);
541 f_print (fout
, "\n\t\t} else {\n");
545 emit_inline (indent
+ 1, &cur
->decl
, flag
);
548 tabify (fout
, indent
+ 1);
549 f_print (fout
, "}\n");
555 print_stat (indent
+ 1, &dl
->decl
);
560 if (sizestr
== NULL
&& size
< inlineflag
)
562 /* don't expand into inline code if size < inlineflag */
565 print_stat (indent
+ 1, &cur
->decl
);
571 /* were already looking at a xdr_inlineable structure */
574 "\t\tbuf = XDR_INLINE (xdrs, %d * BYTES_PER_XDR_UNIT);",
578 "\t\tbuf = XDR_INLINE (xdrs, (%s) * BYTES_PER_XDR_UNIT);",
582 "\t\tbuf = XDR_INLINE (xdrs, (%d + %s)* BYTES_PER_XDR_UNIT);",
584 f_print (fout
, "\n\t\tif (buf == NULL) {\n");
588 print_stat (indent
+ 2, &cur
->decl
);
591 f_print (fout
, "\t\t} else {\n");
596 emit_inline (indent
+ 2, &cur
->decl
, flag
);
599 f_print (fout
, "\t\t}\n");
604 /* this may be const. i haven't traced this one through yet. */
607 emit_struct (definition
* def
)
617 /* No xdr_inlining at all */
618 for (dl
= def
->def
.st
.decls
; dl
!= NULL
; dl
= dl
->next
)
619 print_stat (1, &dl
->decl
);
623 for (dl
= def
->def
.st
.decls
; dl
!= NULL
; dl
= dl
->next
)
624 if (dl
->decl
.rel
== REL_VECTOR
)
626 f_print (fout
, "\tint i;\n");
633 * Make a first pass and see if inling is possible.
635 for (dl
= def
->def
.st
.decls
; dl
!= NULL
; dl
= dl
->next
)
636 if ((dl
->decl
.prefix
== NULL
) &&
637 ((ptr
= find_type (dl
->decl
.type
)) != NULL
) &&
638 ((dl
->decl
.rel
== REL_ALIAS
) || (dl
->decl
.rel
== REL_VECTOR
)))
640 if (dl
->decl
.rel
== REL_ALIAS
)
645 break; /* can be inlined */
650 if (size
>= inlineflag
)
653 break; /* can be inlined */
657 if (size
> inlineflag
)
661 { /* can not inline, drop back to old mode */
662 for (dl
= def
->def
.st
.decls
; dl
!= NULL
; dl
= dl
->next
)
663 print_stat (1, &dl
->decl
);
668 for (j
= 0; j
< 2; j
++)
670 inline_struct (def
, flag
);
675 f_print (fout
, "\t return TRUE;\n\t}\n\n");
677 /* now take care of XDR_FREE case */
679 for (dl
= def
->def
.st
.decls
; dl
!= NULL
; dl
= dl
->next
)
680 print_stat (1, &dl
->decl
);
684 emit_typedef (const definition
* def
)
686 const char *prefix
= def
->def
.ty
.old_prefix
;
687 const char *type
= def
->def
.ty
.old_type
;
688 const char *amax
= def
->def
.ty
.array_max
;
689 relation rel
= def
->def
.ty
.rel
;
691 print_ifstat (1, prefix
, type
, rel
, amax
, "objp", def
->def_name
);
695 print_stat (int indent
, const declaration
* dec
)
697 const char *prefix
= dec
->prefix
;
698 const char *type
= dec
->type
;
699 const char *amax
= dec
->array_max
;
700 relation rel
= dec
->rel
;
703 if (isvectordef (type
, rel
))
705 s_print (name
, "objp->%s", dec
->name
);
709 s_print (name
, "&objp->%s", dec
->name
);
711 print_ifstat (indent
, prefix
, type
, rel
, amax
, name
, dec
->name
);
716 emit_inline (int indent
, declaration
* decl
, int flag
)
721 emit_single_in_line (indent
, decl
, flag
, REL_ALIAS
);
724 tabify (fout
, indent
);
725 f_print (fout
, "{\n");
726 tabify (fout
, indent
+ 1);
727 f_print (fout
, "register %s *genp;\n\n", decl
->type
);
728 tabify (fout
, indent
+ 1);
730 "for (i = 0, genp = objp->%s;\n", decl
->name
);
731 tabify (fout
, indent
+ 2);
732 f_print (fout
, "i < %s; ++i) {\n", decl
->array_max
);
733 emit_single_in_line (indent
+ 2, decl
, flag
, REL_VECTOR
);
734 tabify (fout
, indent
+ 1);
735 f_print (fout
, "}\n");
736 tabify (fout
, indent
);
737 f_print (fout
, "}\n");
741 /* ?... do nothing I guess */
746 emit_single_in_line (int indent
, declaration
*decl
, int flag
, relation rel
)
751 tabify (fout
, indent
);
753 f_print (fout
, "IXDR_PUT_");
756 if (rel
== REL_ALIAS
)
757 f_print (fout
, "objp->%s = IXDR_GET_", decl
->name
);
759 f_print (fout
, "*genp++ = IXDR_GET_");
762 upp_case
= upcase (decl
->type
);
765 if (!strcmp (upp_case
, "INT"))
769 /* Casting is safe since the `freed' flag is set. */
770 upp_case
= (char *) "LONG";
773 if (!strcmp (upp_case
, "U_INT"))
777 /* Casting is safe since the `freed' flag is set. */
778 upp_case
= (char *) "U_LONG";
783 if (rel
== REL_ALIAS
)
784 f_print (fout
, "%s(buf, objp->%s);\n", upp_case
, decl
->name
);
786 f_print (fout
, "%s(buf, *genp++);\n", upp_case
);
790 f_print (fout
, "%s(buf);\n", upp_case
);
799 upcase (const char *str
)
802 ptr
= malloc (strlen (str
) + 1);
805 f_print (stderr
, "malloc failed\n");
810 *ptr
++ = toupper (*str
++);