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
31 * rpc_cout.c, XDR routine outputter for the RPC protocol compiler
32 * Copyright (C) 1987, Sun Microsystems, Inc.
37 #include "rpc_parse.h"
41 static void print_header( definition * );
42 static void print_trailer( void );
43 static void print_stat( int , declaration * );
44 static void emit_enum( definition * );
45 static void emit_program( definition * );
46 static void emit_union( definition * );
47 static void emit_struct( definition * );
48 static void emit_typedef( definition * );
49 static void emit_inline( int, declaration *, int );
50 static void emit_single_in_line( int, declaration *, int, relation );
53 * Emit the C-routine for the given definition
58 if (def->def_kind == DEF_CONST) {
61 if (def->def_kind == DEF_PROGRAM) {
65 if (def->def_kind == DEF_TYPEDEF) {
67 * now we need to handle declarations like
68 * struct typedef foo foo;
69 * since we dont want this to be expanded into 2 calls to xdr_foo
72 if (strcmp(def->def.ty.old_type, def->def_name) == 0)
76 switch (def->def_kind) {
89 /* DEF_CONST and DEF_PROGRAM have already been handled */
97 findtype(definition *def, const char *type)
100 if (def->def_kind == DEF_PROGRAM || def->def_kind == DEF_CONST) {
103 return (streq(def->def_name, type));
108 undefined(const char *type)
112 def = (definition *) FINDVAL(defined, type, findtype);
113 return (def == NULL);
118 print_generic_header(const char *procname, int pointerp)
121 f_print(fout, "bool_t\n");
122 f_print(fout, "xdr_%s(", procname);
123 f_print(fout, "XDR *xdrs, ");
124 f_print(fout, "%s ", procname);
127 f_print(fout, "objp)\n{\n\n");
131 print_header(definition *def)
133 print_generic_header(def->def_name,
134 def->def_kind != DEF_TYPEDEF ||
135 !isvectordef(def->def.ty.old_type,
137 /* Now add Inline support */
139 if (inline_size == 0)
141 /* May cause lint to complain. but ... */
142 f_print(fout, "\tregister long *buf;\n\n");
146 print_prog_header(proc_list *plist)
148 print_generic_header(plist->args.argname, 1);
154 f_print(fout, "\treturn (TRUE);\n");
155 f_print(fout, "}\n");
160 print_ifopen(int indent, const char *name)
162 tabify(fout, indent);
163 f_print(fout, "if (!xdr_%s(xdrs", name);
167 print_ifarg(const char *arg)
169 f_print(fout, ", %s", arg);
173 print_ifsizeof(int indent, const char *prefix, const char *type)
176 f_print(fout, ",\n");
177 tabify(fout, indent);
181 if (streq(type, "bool")) {
182 f_print(fout, "sizeof (bool_t), (xdrproc_t) xdr_bool");
184 f_print(fout, "sizeof (");
185 if (undefined(type) && prefix) {
186 f_print(fout, "%s ", prefix);
188 f_print(fout, "%s), (xdrproc_t) xdr_%s", type, type);
193 print_ifclose(int indent, int brace)
195 f_print(fout, "))\n");
196 tabify(fout, indent);
197 f_print(fout, "\treturn (FALSE);\n");
199 f_print(fout, "\t}\n");
203 print_ifstat(int indent, const char *prefix, const char *type, relation rel,
204 const char *amax, const char *objname, const char *name)
206 const char *alt = NULL;
212 f_print(fout, "\t{\n");
213 f_print(fout, "\t%s **pp = %s;\n", type, objname);
214 print_ifopen(indent, "pointer");
215 print_ifarg("(char **)");
217 print_ifsizeof(0, prefix, type);
220 if (streq(type, "string")) {
222 } else if (streq(type, "opaque")) {
226 print_ifopen(indent, alt);
227 print_ifarg(objname);
229 print_ifopen(indent, "vector");
230 print_ifarg("(char *)");
231 f_print(fout, "%s", objname);
235 print_ifsizeof(indent + 1, prefix, type);
239 if (streq(type, "string")) {
241 } else if (streq(type, "opaque")) {
244 if (streq(type, "string")) {
245 print_ifopen(indent, alt);
246 print_ifarg(objname);
249 print_ifopen(indent, alt);
251 print_ifopen(indent, "array");
253 print_ifarg("(char **)");
254 if (*objname == '&') {
255 f_print(fout, "%s.%s_val, (u_int *) %s.%s_len",
256 objname, name, objname, name);
259 "&%s->%s_val, (u_int *) &%s->%s_len",
260 objname, name, objname, name);
265 print_ifsizeof(indent + 1, prefix, type);
269 print_ifopen(indent, type);
270 print_ifarg(objname);
273 print_ifclose(indent, brace);
278 emit_enum(definition *def __unused)
280 print_ifopen(1, "enum");
281 print_ifarg("(enum_t *)objp");
286 emit_program(definition *def)
292 for (vlist = def->def.pr.versions; vlist != NULL; vlist = vlist->next)
293 for (plist = vlist->procs; plist != NULL; plist = plist->next) {
294 if (!newstyle || plist->arg_num < 2)
295 continue; /* old style, or single argument */
296 print_prog_header(plist);
297 for (dl = plist->args.decls; dl != NULL;
299 print_stat(1, &dl->decl);
306 emit_union(definition *def)
312 const char *vecformat = "objp->%s_u.%s";
313 const char *format = "&objp->%s_u.%s";
315 print_stat(1, &def->def.un.enum_decl);
316 f_print(fout, "\tswitch (objp->%s) {\n", def->def.un.enum_decl.name);
317 for (cl = def->def.un.cases; cl != NULL; cl = cl->next) {
319 f_print(fout, "\tcase %s:\n", cl->case_name);
320 if (cl->contflag == 1) /* a continued case statement */
323 if (!streq(cs->type, "void")) {
324 object = xmalloc(strlen(def->def_name) +
325 strlen(format) + strlen(cs->name) + 1);
326 if (isvectordef (cs->type, cs->rel)) {
327 s_print(object, vecformat, def->def_name,
330 s_print(object, format, def->def_name,
333 print_ifstat(2, cs->prefix, cs->type, cs->rel,
334 cs->array_max, object, cs->name);
337 f_print(fout, "\t\tbreak;\n");
339 dflt = def->def.un.default_decl;
341 if (!streq(dflt->type, "void")) {
342 f_print(fout, "\tdefault:\n");
343 object = xmalloc(strlen(def->def_name) +
344 strlen(format) + strlen(dflt->name) + 1);
345 if (isvectordef (dflt->type, dflt->rel)) {
346 s_print(object, vecformat, def->def_name,
349 s_print(object, format, def->def_name,
353 print_ifstat(2, dflt->prefix, dflt->type, dflt->rel,
354 dflt->array_max, object, dflt->name);
356 f_print(fout, "\t\tbreak;\n");
358 f_print(fout, "\tdefault:\n");
359 f_print(fout, "\t\tbreak;\n");
362 f_print(fout, "\tdefault:\n");
363 f_print(fout, "\t\treturn (FALSE);\n");
366 f_print(fout, "\t}\n");
370 inline_struct(definition *def, int flag)
374 decl_list *cur, *psav;
383 f_print(fout, "\n\tif (xdrs->x_op == XDR_ENCODE) {\n");
385 f_print(fout, "\t\treturn (TRUE);\n\t} else if (xdrs->x_op == XDR_DECODE) {\n");
390 for (dl = def->def.st.decls; dl != NULL; dl = dl->next) { /* xxx */
391 /* now walk down the list and check for basic types */
392 if ((dl->decl.prefix == NULL) &&
393 ((ptr = find_type(dl->decl.type)) != NULL) &&
394 ((dl->decl.rel == REL_ALIAS) ||
395 (dl->decl.rel == REL_VECTOR))){
400 if (dl->decl.rel == REL_ALIAS)
403 /* this code is required to handle arrays */
409 if (ptr->length != 1)
410 s_print(ptemp, "%s%s * %d",
411 plus, dl->decl.array_max,
414 s_print(ptemp, "%s%s", plus,
417 /* now concatenate to sizestr !!!! */
418 if (sizestr == NULL) {
419 sizestr = xstrdup(ptemp);
422 sizestr = xrealloc(sizestr,
425 sizestr = strcat(sizestr, ptemp);
426 /* build up length of array */
431 if (sizestr == NULL && size < inline_size){
433 * don't expand into inline code
434 * if size < inline_size
437 print_stat(indent + 1, &cur->decl);
441 /* were already looking at a xdr_inlineable structure */
442 tabify(fout, indent + 1);
444 f_print(fout, "buf = XDR_INLINE(xdrs, %d * BYTES_PER_XDR_UNIT);",
449 "buf = XDR_INLINE(xdrs, (%s) * BYTES_PER_XDR_UNIT);",
453 "buf = XDR_INLINE(xdrs, (%d + (%s)) * BYTES_PER_XDR_UNIT);",
458 tabify(fout, indent + 1);
460 "if (buf == NULL) {\n");
464 print_stat(indent + 2, &cur->decl);
468 f_print(fout, "\n\t\t} else {\n");
472 emit_inline(indent + 2, &cur->decl, flag);
476 tabify(fout, indent + 1);
477 f_print(fout, "}\n");
484 print_stat(indent + 1, &dl->decl);
489 if (sizestr == NULL && size < inline_size){
490 /* don't expand into inline code if size < inline_size */
492 print_stat(indent + 1, &cur->decl);
496 /* were already looking at a xdr_inlineable structure */
498 f_print(fout, "\t\tbuf = XDR_INLINE(xdrs, %d * BYTES_PER_XDR_UNIT);",
503 "\t\tbuf = XDR_INLINE(xdrs, (%s) * BYTES_PER_XDR_UNIT);",
507 "\t\tbuf = XDR_INLINE(xdrs, (%d + (%s)) * BYTES_PER_XDR_UNIT);",
510 f_print(fout, "\n\t\tif (buf == NULL) {\n");
513 print_stat(indent + 2, &cur->decl);
516 f_print(fout, "\t\t} else {\n");
520 emit_inline(indent + 2, &cur->decl, flag);
523 f_print(fout, "\t\t}\n");
529 emit_struct(definition *def)
536 if (inline_size == 0) {
537 /* No xdr_inlining at all */
538 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
539 print_stat(1, &dl->decl);
543 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
544 if (dl->decl.rel == REL_VECTOR &&
545 strcmp(dl->decl.type, "opaque") != 0){
546 f_print(fout, "\tint i;\n");
553 * Make a first pass and see if inling is possible.
555 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
556 if ((dl->decl.prefix == NULL) &&
557 ((ptr = find_type(dl->decl.type)) != NULL) &&
558 ((dl->decl.rel == REL_ALIAS)||
559 (dl->decl.rel == REL_VECTOR))){
560 if (dl->decl.rel == REL_ALIAS)
564 break; /* can be inlined */
567 if (size >= inline_size){
569 break; /* can be inlined */
573 if (size >= inline_size)
576 if (can_inline == 0){ /* can not inline, drop back to old mode */
577 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
578 print_stat(1, &dl->decl);
583 for (j = 0; j < 2; j++){
584 inline_struct(def, flag);
589 f_print(fout, "\t\treturn (TRUE);\n\t}\n\n");
591 /* now take care of XDR_FREE case */
593 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
594 print_stat(1, &dl->decl);
599 emit_typedef(definition *def)
601 const char *prefix = def->def.ty.old_prefix;
602 const char *type = def->def.ty.old_type;
603 const char *amax = def->def.ty.array_max;
604 relation rel = def->def.ty.rel;
606 print_ifstat(1, prefix, type, rel, amax, "objp", def->def_name);
610 print_stat(int indent, declaration *dec)
612 const char *prefix = dec->prefix;
613 const char *type = dec->type;
614 const char *amax = dec->array_max;
615 relation rel = dec->rel;
618 if (isvectordef(type, rel)) {
619 s_print(name, "objp->%s", dec->name);
621 s_print(name, "&objp->%s", dec->name);
623 print_ifstat(indent, prefix, type, rel, amax, name, dec->name);
627 char *upcase(const char *);
630 emit_inline(int indent, declaration *decl, int flag)
634 emit_single_in_line(indent, decl, flag, REL_ALIAS);
637 tabify(fout, indent);
638 f_print(fout, "{\n");
639 tabify(fout, indent + 1);
640 f_print(fout, "%s *genp;\n\n", decl->type);
641 tabify(fout, indent + 1);
643 "for (i = 0, genp = objp->%s;\n", decl->name);
644 tabify(fout, indent + 2);
645 f_print(fout, "i < %s; i++) {\n", decl->array_max);
646 emit_single_in_line(indent + 2, decl, flag, REL_VECTOR);
647 tabify(fout, indent + 1);
648 f_print(fout, "}\n");
649 tabify(fout, indent);
650 f_print(fout, "}\n");
658 emit_single_in_line(int indent, declaration *decl, int flag, relation rel)
662 tabify(fout, indent);
664 f_print(fout, "IXDR_PUT_");
666 if (rel == REL_ALIAS)
667 f_print(fout, "objp->%s = IXDR_GET_", decl->name);
669 f_print(fout, "*genp++ = IXDR_GET_");
671 upp_case = upcase(decl->type);
674 if (strcmp(upp_case, "INT") == 0)
677 upp_case = strdup("LONG");
680 if (strcmp(upp_case, "U_INT") == 0)
683 upp_case = strdup("U_LONG");
686 if (rel == REL_ALIAS)
688 "%s(buf, objp->%s);\n", upp_case, decl->name);
690 f_print(fout, "%s(buf, *genp++);\n", upp_case);
693 f_print(fout, "%s(buf);\n", upp_case);
698 upcase(const char *str)
702 ptr = (char *)xmalloc(strlen(str)+1);
706 *ptr++ = toupper(*str++);