[BACK]Return to gen_glue.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / asn1_compile

Annotation of src/usr.bin/asn1_compile/gen_glue.c, Revision 1.2

1.1       hin         1: /*
                      2:  * Copyright (c) 1997, 1999 Kungliga Tekniska Högskolan
                      3:  * (Royal Institute of Technology, Stockholm, Sweden).
                      4:  * All rights reserved.
                      5:  *
                      6:  * Redistribution and use in source and binary forms, with or without
                      7:  * modification, are permitted provided that the following conditions
                      8:  * are met:
                      9:  *
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
                     12:  *
                     13:  * 2. Redistributions in binary form must reproduce the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer in the
                     15:  *    documentation and/or other materials provided with the distribution.
                     16:  *
                     17:  * 3. Neither the name of the Institute nor the names of its contributors
                     18:  *    may be used to endorse or promote products derived from this software
                     19:  *    without specific prior written permission.
                     20:  *
                     21:  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
                     22:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     23:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     24:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
                     25:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     26:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     27:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     28:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     29:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     30:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     31:  * SUCH DAMAGE.
                     32:  */
                     33:
                     34: #include "gen_locl.h"
                     35:
                     36: /*
1.2     ! biorn      37: RCSID("$KTH: gen_glue.c,v 1.8 2005/04/25 18:07:07 lha Exp $");
1.1       hin        38: */
                     39:
                     40: static void
                     41: generate_2int (const Symbol *s)
                     42: {
                     43:     Type *t = s->type;
                     44:     Member *m;
                     45:     int tag = -1;
                     46:
                     47:     fprintf (headerfile,
                     48:             "unsigned %s2int(%s);\n",
                     49:             s->gen_name, s->gen_name);
                     50:
                     51:     fprintf (codefile,
                     52:             "unsigned %s2int(%s f)\n"
                     53:             "{\n"
                     54:             "unsigned r = 0;\n",
                     55:             s->gen_name, s->gen_name);
                     56:
                     57:     for (m = t->members; m && m->val != tag; m = m->next) {
                     58:        fprintf (codefile, "if(f.%s) r |= (1U << %d);\n",
                     59:                 m->gen_name, m->val);
                     60:
                     61:        if (tag == -1)
                     62:            tag = m->val;
                     63:     }
                     64:     fprintf (codefile, "return r;\n"
                     65:             "}\n\n");
                     66: }
                     67:
                     68: static void
                     69: generate_int2 (const Symbol *s)
                     70: {
                     71:     Type *t = s->type;
                     72:     Member *m;
                     73:     int tag = -1;
                     74:
                     75:     fprintf (headerfile,
                     76:             "%s int2%s(unsigned);\n",
                     77:             s->gen_name, s->gen_name);
                     78:
                     79:     fprintf (codefile,
                     80:             "%s int2%s(unsigned n)\n"
                     81:             "{\n"
                     82:             "\t%s flags;\n\n",
                     83:             s->gen_name, s->gen_name, s->gen_name);
                     84:
                     85:     for (m = t->members; m && m->val != tag; m = m->next) {
                     86:        fprintf (codefile, "\tflags.%s = (n >> %d) & 1;\n",
                     87:                 m->gen_name, m->val);
                     88:
                     89:        if (tag == -1)
                     90:            tag = m->val;
                     91:     }
                     92:     fprintf (codefile, "\treturn flags;\n"
                     93:             "}\n\n");
                     94: }
                     95:
                     96: /*
                     97:  * This depends on the bit string being declared in increasing order
                     98:  */
                     99:
                    100: static void
                    101: generate_units (const Symbol *s)
                    102: {
                    103:     Type *t = s->type;
                    104:     Member *m;
                    105:     int tag = -1;
                    106:
                    107:     fprintf (headerfile,
1.2     ! biorn     108:             "const struct units * asn1_%s_units(void);",
1.1       hin       109:             s->gen_name);
                    110:
                    111:     fprintf (codefile,
1.2     ! biorn     112:             "static struct units %s_units[] = {\n",
1.1       hin       113:             s->gen_name);
                    114:
                    115:     if(t->members)
                    116:        for (m = t->members->prev; m && m->val != tag; m = m->prev) {
                    117:            fprintf (codefile,
                    118:                     "\t{\"%s\",\t1U << %d},\n", m->gen_name, m->val);
                    119:
                    120:            if (tag == -1)
                    121:                tag = m->val;
                    122:        }
                    123:
                    124:     fprintf (codefile,
                    125:             "\t{NULL,\t0}\n"
                    126:             "};\n\n");
1.2     ! biorn     127:
        !           128:     fprintf (codefile,
        !           129:             "const struct units * asn1_%s_units(void){\n"
        !           130:             "return %s_units;\n"
        !           131:             "}\n\n",
        !           132:             s->gen_name, s->gen_name);
        !           133:
        !           134:
1.1       hin       135: }
                    136:
                    137: void
                    138: generate_glue (const Symbol *s)
                    139: {
                    140:     switch(s->type->type) {
                    141:     case TBitString :
                    142:        generate_2int (s);
                    143:        generate_int2 (s);
                    144:        generate_units (s);
                    145:        break;
                    146:     default :
                    147:        break;
                    148:     }
                    149: }