2 * From: @(#)rpc_cout.c 1.13 89/02/22
4 * Copyright (c) 2010, Oracle America, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following
13 * disclaimer in the documentation and/or other materials
14 * provided with the distribution.
15 * * Neither the name of the "Oracle America, Inc." nor the names of its
16 * contributors may be used to endorse or promote products derived
17 * from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
24 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
26 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 * rpc_cout.c, XDR routine outputter for the RPC protocol compiler
39 #include "rpc_parse.h"
43 static void emit_enum (definition
* def
);
44 static void emit_program (const definition
* def
);
45 static void emit_union (const definition
* def
);
46 static void emit_struct (definition
* def
);
47 static void emit_typedef (const definition
* def
);
48 static void emit_inline (int indent
, declaration
* decl
, int flag
);
49 static void emit_single_in_line (int indent
, declaration
*decl
, int flag
,
51 static int findtype (const definition
* def
, const char *type
);
52 static int undefined (const char *type
);
53 static void print_generic_header (const char *procname
, int pointerp
);
54 static void print_ifopen (int indent
, const char *name
);
55 static void print_ifarg (const char *arg
);
56 static void print_ifsizeof (int indent
, const char *prefix
, const char *type
);
57 static void print_ifclose (int indent
);
58 static void print_ifstat (int indent
, const char *prefix
, const char *type
,
59 relation rel
, const char *amax
,
60 const char *objname
, const char *name
);
61 static void print_stat (int indent
, const declaration
* dec
);
62 static void print_header (const definition
* def
);
63 static void print_trailer (void);
64 static char *upcase (const char *str
);
67 * Emit the C-routine for the given definition
70 emit (definition
* def
)
72 if (def
->def_kind
== DEF_CONST
)
76 if (def
->def_kind
== DEF_PROGRAM
)
81 if (def
->def_kind
== DEF_TYPEDEF
)
83 /* now we need to handle declarations like
84 struct typedef foo foo;
85 since we don't want this to be expanded
86 into 2 calls to xdr_foo */
88 if (strcmp (def
->def
.ty
.old_type
, def
->def_name
) == 0)
93 switch (def
->def_kind
)
115 findtype (const definition
* def
, const char *type
)
117 if (def
->def_kind
== DEF_PROGRAM
|| def
->def_kind
== DEF_CONST
)
123 return (streq (def
->def_name
, type
));
128 undefined (const char *type
)
131 def
= (definition
*) FINDVAL (defined
, type
, findtype
);
132 return (def
== NULL
);
137 print_generic_header (const char *procname
, int pointerp
)
139 f_print (fout
, "\n");
140 f_print (fout
, "bool_t\n");
143 f_print (fout
, "xdr_%s (", procname
);
144 f_print (fout
, "XDR *xdrs, ");
145 f_print (fout
, "%s ", procname
);
148 f_print (fout
, "objp)\n{\n");
152 f_print (fout
, "xdr_%s (xdrs, objp)\n", procname
);
153 f_print (fout
, "\tXDR *xdrs;\n");
154 f_print (fout
, "\t%s ", procname
);
157 f_print (fout
, "objp;\n{\n");
162 print_header (const definition
* def
)
164 print_generic_header (def
->def_name
,
165 def
->def_kind
!= DEF_TYPEDEF
||
166 !isvectordef (def
->def
.ty
.old_type
,
169 /* Now add Inline support */
173 /*May cause lint to complain. but ... */
174 f_print (fout
, "\tregister int32_t *buf;\n\n");
178 print_prog_header (const proc_list
* plist
)
180 print_generic_header (plist
->args
.argname
, 1);
186 f_print (fout
, "\treturn TRUE;\n");
187 f_print (fout
, "}\n");
192 print_ifopen (int indent
, const char *name
)
194 tabify (fout
, indent
);
195 f_print (fout
, " if (!xdr_%s (xdrs", name
);
199 print_ifarg (const char *arg
)
201 f_print (fout
, ", %s", arg
);
205 print_ifsizeof (int indent
, const char *prefix
, const char *type
)
209 fprintf (fout
, ",\n");
210 tabify (fout
, indent
);
213 fprintf (fout
, ", ");
215 if (streq (type
, "bool"))
216 fprintf (fout
, "sizeof (bool_t), (xdrproc_t) xdr_bool");
219 fprintf (fout
, "sizeof (");
220 if (undefined (type
) && prefix
)
222 f_print (fout
, "%s ", prefix
);
224 fprintf (fout
, "%s), (xdrproc_t) xdr_%s", type
, type
);
229 print_ifclose (int indent
)
231 f_print (fout
, "))\n");
232 tabify (fout
, indent
);
233 f_print (fout
, "\t return FALSE;\n");
237 print_ifstat (int indent
, const char *prefix
, const char *type
, relation rel
,
238 const char *amax
, const char *objname
, const char *name
)
240 const char *alt
= NULL
;
245 print_ifopen (indent
, "pointer");
246 print_ifarg ("(char **)");
247 f_print (fout
, "%s", objname
);
248 print_ifsizeof (0, prefix
, type
);
251 if (streq (type
, "string"))
255 else if (streq (type
, "opaque"))
261 print_ifopen (indent
, alt
);
262 print_ifarg (objname
);
266 print_ifopen (indent
, "vector");
267 print_ifarg ("(char *)");
268 f_print (fout
, "%s", objname
);
273 print_ifsizeof (indent
+ 1, prefix
, type
);
277 if (streq (type
, "string"))
281 else if (streq (type
, "opaque"))
285 if (streq (type
, "string"))
287 print_ifopen (indent
, alt
);
288 print_ifarg (objname
);
294 print_ifopen (indent
, alt
);
298 print_ifopen (indent
, "array");
300 print_ifarg ("(char **)");
303 f_print (fout
, "%s.%s_val, (u_int *) %s.%s_len",
304 objname
, name
, objname
, name
);
308 f_print (fout
, "&%s->%s_val, (u_int *) &%s->%s_len",
309 objname
, name
, objname
, name
);
315 print_ifsizeof (indent
+ 1, prefix
, type
);
319 print_ifopen (indent
, type
);
320 print_ifarg (objname
);
323 print_ifclose (indent
);
327 emit_enum (definition
* def
)
331 print_ifopen (1, "enum");
332 print_ifarg ("(enum_t *) objp");
337 emit_program (const definition
* def
)
343 for (vlist
= def
->def
.pr
.versions
; vlist
!= NULL
; vlist
= vlist
->next
)
344 for (plist
= vlist
->procs
; plist
!= NULL
; plist
= plist
->next
)
346 if (!newstyle
|| plist
->arg_num
< 2)
347 continue; /* old style, or single argument */
348 print_prog_header (plist
);
349 for (dl
= plist
->args
.decls
; dl
!= NULL
;
351 print_stat (1, &dl
->decl
);
357 emit_union (const definition
* def
)
363 const char *vecformat
= "objp->%s_u.%s";
364 const char *format
= "&objp->%s_u.%s";
366 print_stat (1, &def
->def
.un
.enum_decl
);
367 f_print (fout
, "\tswitch (objp->%s) {\n", def
->def
.un
.enum_decl
.name
);
368 for (cl
= def
->def
.un
.cases
; cl
!= NULL
; cl
= cl
->next
)
371 f_print (fout
, "\tcase %s:\n", cl
->case_name
);
372 if (cl
->contflag
== 1) /* a continued case statement */
375 if (!streq (cs
->type
, "void"))
377 object
= alloc (strlen (def
->def_name
) + strlen (format
) +
378 strlen (cs
->name
) + 1);
379 if (isvectordef (cs
->type
, cs
->rel
))
381 s_print (object
, vecformat
, def
->def_name
,
386 s_print (object
, format
, def
->def_name
,
389 print_ifstat (2, cs
->prefix
, cs
->type
, cs
->rel
, cs
->array_max
,
393 f_print (fout
, "\t\tbreak;\n");
395 dflt
= def
->def
.un
.default_decl
;
398 if (!streq (dflt
->type
, "void"))
400 f_print (fout
, "\tdefault:\n");
401 object
= alloc (strlen (def
->def_name
) + strlen (format
) +
402 strlen (dflt
->name
) + 1);
403 if (isvectordef (dflt
->type
, dflt
->rel
))
405 s_print (object
, vecformat
, def
->def_name
,
410 s_print (object
, format
, def
->def_name
,
414 print_ifstat (2, dflt
->prefix
, dflt
->type
, dflt
->rel
,
415 dflt
->array_max
, object
, dflt
->name
);
417 f_print (fout
, "\t\tbreak;\n");
419 #ifdef __GNU_LIBRARY__
422 f_print (fout
, "\tdefault:\n");
423 f_print (fout
, "\t\tbreak;\n");
429 f_print (fout
, "\tdefault:\n");
430 f_print (fout
, "\t\treturn FALSE;\n");
433 f_print (fout
, "\t}\n");
437 inline_struct (definition
*def
, int flag
)
441 decl_list
*cur
= NULL
;
450 f_print (fout
, "\n\tif (xdrs->x_op == XDR_ENCODE) {\n");
453 "\t\treturn TRUE;\n\t} else if (xdrs->x_op == XDR_DECODE) {\n");
458 for (dl
= def
->def
.st
.decls
; dl
!= NULL
; dl
= dl
->next
)
460 /* now walk down the list and check for basic types */
461 if ((dl
->decl
.prefix
== NULL
) &&
462 ((ptr
= find_type (dl
->decl
.type
)) != NULL
) &&
463 ((dl
->decl
.rel
== REL_ALIAS
) || (dl
->decl
.rel
== REL_VECTOR
)))
469 if (dl
->decl
.rel
== REL_ALIAS
)
473 /* this is required to handle arrays */
479 if (ptr
->length
!= 1)
480 s_print (ptemp
, " %s %s * %d", plus
, dl
->decl
.array_max
,
483 s_print (ptemp
, " %s%s ", plus
, dl
->decl
.array_max
);
485 /*now concatenate to sizestr !!!! */
487 sizestr
= strdup (ptemp
);
490 sizestr
= realloc (sizestr
, strlen (sizestr
) +
494 f_print (stderr
, "Fatal error : no memory \n");
497 sizestr
= strcat (sizestr
, ptemp
);
498 /*build up length of array */
506 if (sizestr
== NULL
&& size
< inlineflag
)
508 /* don't expand into inline code if size < inlineflag */
511 print_stat (indent
+ 1, &cur
->decl
);
517 /* were already looking at a xdr_inlineable structure */
518 tabify (fout
, indent
+ 1);
520 f_print (fout
, "buf = XDR_INLINE (xdrs, %d * BYTES_PER_XDR_UNIT);", size
);
523 "buf = XDR_INLINE (xdrs, (%s) * BYTES_PER_XDR_UNIT);",
527 "buf = XDR_INLINE (xdrs, (%d + (%s)) * BYTES_PER_XDR_UNIT);",
529 f_print (fout
, "\n");
530 tabify (fout
, indent
+ 1);
531 fprintf (fout
, "if (buf == NULL) {\n");
535 print_stat (indent
+ 2, &cur
->decl
);
539 f_print (fout
, "\n\t\t} else {\n");
543 emit_inline (indent
+ 1, &cur
->decl
, flag
);
546 tabify (fout
, indent
+ 1);
547 f_print (fout
, "}\n");
554 print_stat (indent
+ 1, &dl
->decl
);
559 if (sizestr
== NULL
&& size
< inlineflag
)
561 /* don't expand into inline code if size < inlineflag */
564 print_stat (indent
+ 1, &cur
->decl
);
570 /* were already looking at a xdr_inlineable structure */
573 "\t\tbuf = XDR_INLINE (xdrs, %d * BYTES_PER_XDR_UNIT);",
577 "\t\tbuf = XDR_INLINE (xdrs, (%s) * BYTES_PER_XDR_UNIT);",
581 "\t\tbuf = XDR_INLINE (xdrs, (%d + %s)* BYTES_PER_XDR_UNIT);",
583 f_print (fout
, "\n\t\tif (buf == NULL) {\n");
587 print_stat (indent
+ 2, &cur
->decl
);
590 f_print (fout
, "\t\t} else {\n");
595 emit_inline (indent
+ 2, &cur
->decl
, flag
);
598 f_print (fout
, "\t\t}\n");
603 /* this may be const. i haven't traced this one through yet. */
606 emit_struct (definition
* def
)
616 /* No xdr_inlining at all */
617 for (dl
= def
->def
.st
.decls
; dl
!= NULL
; dl
= dl
->next
)
618 print_stat (1, &dl
->decl
);
622 for (dl
= def
->def
.st
.decls
; dl
!= NULL
; dl
= dl
->next
)
623 if (dl
->decl
.rel
== REL_VECTOR
)
625 f_print (fout
, "\tint i;\n");
632 * Make a first pass and see if inling is possible.
634 for (dl
= def
->def
.st
.decls
; dl
!= NULL
; dl
= dl
->next
)
635 if ((dl
->decl
.prefix
== NULL
) &&
636 ((ptr
= find_type (dl
->decl
.type
)) != NULL
) &&
637 ((dl
->decl
.rel
== REL_ALIAS
) || (dl
->decl
.rel
== REL_VECTOR
)))
639 if (dl
->decl
.rel
== REL_ALIAS
)
644 break; /* can be inlined */
649 if (size
>= inlineflag
)
652 break; /* can be inlined */
656 if (size
> inlineflag
)
660 { /* can not inline, drop back to old mode */
661 for (dl
= def
->def
.st
.decls
; dl
!= NULL
; dl
= dl
->next
)
662 print_stat (1, &dl
->decl
);
667 for (j
= 0; j
< 2; j
++)
669 inline_struct (def
, flag
);
674 f_print (fout
, "\t return TRUE;\n\t}\n\n");
676 /* now take care of XDR_FREE case */
678 for (dl
= def
->def
.st
.decls
; dl
!= NULL
; dl
= dl
->next
)
679 print_stat (1, &dl
->decl
);
683 emit_typedef (const definition
* def
)
685 const char *prefix
= def
->def
.ty
.old_prefix
;
686 const char *type
= def
->def
.ty
.old_type
;
687 const char *amax
= def
->def
.ty
.array_max
;
688 relation rel
= def
->def
.ty
.rel
;
690 print_ifstat (1, prefix
, type
, rel
, amax
, "objp", def
->def_name
);
694 print_stat (int indent
, const declaration
* dec
)
696 const char *prefix
= dec
->prefix
;
697 const char *type
= dec
->type
;
698 const char *amax
= dec
->array_max
;
699 relation rel
= dec
->rel
;
702 if (isvectordef (type
, rel
))
704 s_print (name
, "objp->%s", dec
->name
);
708 s_print (name
, "&objp->%s", dec
->name
);
710 print_ifstat (indent
, prefix
, type
, rel
, amax
, name
, dec
->name
);
715 emit_inline (int indent
, declaration
* decl
, int flag
)
720 emit_single_in_line (indent
, decl
, flag
, REL_ALIAS
);
723 tabify (fout
, indent
);
724 f_print (fout
, "{\n");
725 tabify (fout
, indent
+ 1);
726 f_print (fout
, "register %s *genp;\n\n", decl
->type
);
727 tabify (fout
, indent
+ 1);
729 "for (i = 0, genp = objp->%s;\n", decl
->name
);
730 tabify (fout
, indent
+ 2);
731 f_print (fout
, "i < %s; ++i) {\n", decl
->array_max
);
732 emit_single_in_line (indent
+ 2, decl
, flag
, REL_VECTOR
);
733 tabify (fout
, indent
+ 1);
734 f_print (fout
, "}\n");
735 tabify (fout
, indent
);
736 f_print (fout
, "}\n");
740 /* ?... do nothing I guess */
745 emit_single_in_line (int indent
, declaration
*decl
, int flag
, relation rel
)
750 tabify (fout
, indent
);
752 f_print (fout
, "IXDR_PUT_");
755 if (rel
== REL_ALIAS
)
756 f_print (fout
, "objp->%s = IXDR_GET_", decl
->name
);
758 f_print (fout
, "*genp++ = IXDR_GET_");
761 upp_case
= upcase (decl
->type
);
764 if (!strcmp (upp_case
, "INT"))
768 /* Casting is safe since the `freed' flag is set. */
769 upp_case
= (char *) "LONG";
772 if (!strcmp (upp_case
, "U_INT"))
776 /* Casting is safe since the `freed' flag is set. */
777 upp_case
= (char *) "U_LONG";
782 if (rel
== REL_ALIAS
)
783 f_print (fout
, "%s(buf, objp->%s);\n", upp_case
, decl
->name
);
785 f_print (fout
, "%s(buf, *genp++);\n", upp_case
);
789 f_print (fout
, "%s(buf);\n", upp_case
);
798 upcase (const char *str
)
801 ptr
= malloc (strlen (str
) + 1);
804 f_print (stderr
, "malloc failed\n");
809 *ptr
++ = toupper (*str
++);