1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* 23*7c478bd9Sstevel@tonic-gate * Copyright 2003 Sun Microsystems, Inc. All rights reserved. 24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 25*7c478bd9Sstevel@tonic-gate */ 26*7c478bd9Sstevel@tonic-gate 27*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*7c478bd9Sstevel@tonic-gate 29*7c478bd9Sstevel@tonic-gate #include <unistd.h> 30*7c478bd9Sstevel@tonic-gate #include <math.h> 31*7c478bd9Sstevel@tonic-gate #include "stabs.h" 32*7c478bd9Sstevel@tonic-gate 33*7c478bd9Sstevel@tonic-gate void genassym_do_sou(struct tdesc *tdp, struct node *np); 34*7c478bd9Sstevel@tonic-gate void genassym_do_enum(struct tdesc *tdp, struct node *np); 35*7c478bd9Sstevel@tonic-gate void genassym_do_intrinsic(struct tdesc *tdp, struct node *np); 36*7c478bd9Sstevel@tonic-gate 37*7c478bd9Sstevel@tonic-gate static void switch_on_type(struct mlist *mlp, struct tdesc *tdp, 38*7c478bd9Sstevel@tonic-gate char *format, int level); 39*7c478bd9Sstevel@tonic-gate 40*7c478bd9Sstevel@tonic-gate static void print_intrinsic(struct mlist *mlp, struct tdesc *tdp, 41*7c478bd9Sstevel@tonic-gate char *format, int level); 42*7c478bd9Sstevel@tonic-gate static void print_forward(struct mlist *mlp, struct tdesc *tdp, 43*7c478bd9Sstevel@tonic-gate char *format, int level); 44*7c478bd9Sstevel@tonic-gate static void print_pointer(struct mlist *mlp, struct tdesc *tdp, 45*7c478bd9Sstevel@tonic-gate char *format, int level); 46*7c478bd9Sstevel@tonic-gate static void print_array(struct mlist *mlp, struct tdesc *tdp, 47*7c478bd9Sstevel@tonic-gate char *format, int level); 48*7c478bd9Sstevel@tonic-gate static void print_function(struct mlist *mlp, struct tdesc *tdp, 49*7c478bd9Sstevel@tonic-gate char *format, int level); 50*7c478bd9Sstevel@tonic-gate static void print_union(struct mlist *mlp, struct tdesc *tdp, 51*7c478bd9Sstevel@tonic-gate char *format, int level); 52*7c478bd9Sstevel@tonic-gate static void print_enum(struct mlist *mlp, struct tdesc *tdp, 53*7c478bd9Sstevel@tonic-gate char *format, int level); 54*7c478bd9Sstevel@tonic-gate static void print_forward(struct mlist *mlp, struct tdesc *tdp, 55*7c478bd9Sstevel@tonic-gate char *format, int level); 56*7c478bd9Sstevel@tonic-gate static void print_typeof(struct mlist *mlp, struct tdesc *tdp, 57*7c478bd9Sstevel@tonic-gate char *format, int level); 58*7c478bd9Sstevel@tonic-gate static void print_struct(struct mlist *mlp, struct tdesc *tdp, 59*7c478bd9Sstevel@tonic-gate char *format, int level); 60*7c478bd9Sstevel@tonic-gate static void print_volatile(struct mlist *mlp, struct tdesc *tdp, 61*7c478bd9Sstevel@tonic-gate char *format, int level); 62*7c478bd9Sstevel@tonic-gate static int stabs_log2(unsigned int value); 63*7c478bd9Sstevel@tonic-gate 64*7c478bd9Sstevel@tonic-gate void 65*7c478bd9Sstevel@tonic-gate genassym_do_intrinsic(struct tdesc *tdp, struct node *np) 66*7c478bd9Sstevel@tonic-gate { 67*7c478bd9Sstevel@tonic-gate if (np->format != NULL) { 68*7c478bd9Sstevel@tonic-gate char *upper = uc(np->format); 69*7c478bd9Sstevel@tonic-gate 70*7c478bd9Sstevel@tonic-gate printf("#define\t%s 0x%x\n", upper, tdp->size); 71*7c478bd9Sstevel@tonic-gate 72*7c478bd9Sstevel@tonic-gate free(upper); 73*7c478bd9Sstevel@tonic-gate } 74*7c478bd9Sstevel@tonic-gate } 75*7c478bd9Sstevel@tonic-gate 76*7c478bd9Sstevel@tonic-gate 77*7c478bd9Sstevel@tonic-gate void 78*7c478bd9Sstevel@tonic-gate genassym_do_sou(struct tdesc *tdp, struct node *np) 79*7c478bd9Sstevel@tonic-gate { 80*7c478bd9Sstevel@tonic-gate struct mlist *mlp; 81*7c478bd9Sstevel@tonic-gate struct child *chp; 82*7c478bd9Sstevel@tonic-gate char *format; 83*7c478bd9Sstevel@tonic-gate 84*7c478bd9Sstevel@tonic-gate if (np->format != NULL) { 85*7c478bd9Sstevel@tonic-gate char *upper = uc(np->format); 86*7c478bd9Sstevel@tonic-gate int l; 87*7c478bd9Sstevel@tonic-gate 88*7c478bd9Sstevel@tonic-gate printf("#define\t%s 0x%x\n", upper, tdp->size); 89*7c478bd9Sstevel@tonic-gate 90*7c478bd9Sstevel@tonic-gate if ((np->format2 != NULL) && 91*7c478bd9Sstevel@tonic-gate (l = stabs_log2(tdp->size)) != -1) { 92*7c478bd9Sstevel@tonic-gate printf("#define\t%s 0x%x\n", np->format2, l); 93*7c478bd9Sstevel@tonic-gate } 94*7c478bd9Sstevel@tonic-gate 95*7c478bd9Sstevel@tonic-gate free(upper); 96*7c478bd9Sstevel@tonic-gate } 97*7c478bd9Sstevel@tonic-gate 98*7c478bd9Sstevel@tonic-gate /* 99*7c478bd9Sstevel@tonic-gate * Run thru all the fields of a struct and print them out 100*7c478bd9Sstevel@tonic-gate */ 101*7c478bd9Sstevel@tonic-gate for (mlp = tdp->data.members.forw; mlp != NULL; mlp = mlp->next) { 102*7c478bd9Sstevel@tonic-gate /* 103*7c478bd9Sstevel@tonic-gate * If there's a child list, only print those members. 104*7c478bd9Sstevel@tonic-gate */ 105*7c478bd9Sstevel@tonic-gate if (np->child != NULL) { 106*7c478bd9Sstevel@tonic-gate if (mlp->name == NULL) 107*7c478bd9Sstevel@tonic-gate continue; 108*7c478bd9Sstevel@tonic-gate chp = find_child(np, mlp->name); 109*7c478bd9Sstevel@tonic-gate if (chp == NULL) 110*7c478bd9Sstevel@tonic-gate continue; 111*7c478bd9Sstevel@tonic-gate format = uc(chp->format); 112*7c478bd9Sstevel@tonic-gate } else { 113*7c478bd9Sstevel@tonic-gate format = NULL; 114*7c478bd9Sstevel@tonic-gate } 115*7c478bd9Sstevel@tonic-gate if (mlp->fdesc == NULL) 116*7c478bd9Sstevel@tonic-gate continue; 117*7c478bd9Sstevel@tonic-gate switch_on_type(mlp, mlp->fdesc, format, 0); 118*7c478bd9Sstevel@tonic-gate if (format != NULL) 119*7c478bd9Sstevel@tonic-gate free(format); 120*7c478bd9Sstevel@tonic-gate } 121*7c478bd9Sstevel@tonic-gate } 122*7c478bd9Sstevel@tonic-gate 123*7c478bd9Sstevel@tonic-gate void 124*7c478bd9Sstevel@tonic-gate genassym_do_enum(struct tdesc *tdp, struct node *np) 125*7c478bd9Sstevel@tonic-gate { 126*7c478bd9Sstevel@tonic-gate int nelem = 0; 127*7c478bd9Sstevel@tonic-gate struct elist *elp; 128*7c478bd9Sstevel@tonic-gate 129*7c478bd9Sstevel@tonic-gate printf("\n"); 130*7c478bd9Sstevel@tonic-gate for (elp = tdp->data.emem; elp != NULL; elp = elp->next) { 131*7c478bd9Sstevel@tonic-gate printf("#define\tENUM_%s 0x%x\n", elp->name, elp->number); 132*7c478bd9Sstevel@tonic-gate nelem++; 133*7c478bd9Sstevel@tonic-gate } 134*7c478bd9Sstevel@tonic-gate printf("%x c-enum .%s\n", nelem, np->name); 135*7c478bd9Sstevel@tonic-gate } 136*7c478bd9Sstevel@tonic-gate 137*7c478bd9Sstevel@tonic-gate static void 138*7c478bd9Sstevel@tonic-gate switch_on_type(struct mlist *mlp, struct tdesc *tdp, char *format, int level) 139*7c478bd9Sstevel@tonic-gate { 140*7c478bd9Sstevel@tonic-gate boolean_t allocated = B_FALSE; 141*7c478bd9Sstevel@tonic-gate 142*7c478bd9Sstevel@tonic-gate if (format == NULL) { 143*7c478bd9Sstevel@tonic-gate allocated = B_TRUE; 144*7c478bd9Sstevel@tonic-gate format = uc(mlp->name); 145*7c478bd9Sstevel@tonic-gate } 146*7c478bd9Sstevel@tonic-gate 147*7c478bd9Sstevel@tonic-gate switch (tdp->type) { 148*7c478bd9Sstevel@tonic-gate case INTRINSIC: 149*7c478bd9Sstevel@tonic-gate print_intrinsic(mlp, tdp, format, level); 150*7c478bd9Sstevel@tonic-gate break; 151*7c478bd9Sstevel@tonic-gate case POINTER: 152*7c478bd9Sstevel@tonic-gate print_pointer(mlp, tdp, format, level); 153*7c478bd9Sstevel@tonic-gate break; 154*7c478bd9Sstevel@tonic-gate case ARRAY: 155*7c478bd9Sstevel@tonic-gate print_array(mlp, tdp, format, level); 156*7c478bd9Sstevel@tonic-gate break; 157*7c478bd9Sstevel@tonic-gate case FUNCTION: 158*7c478bd9Sstevel@tonic-gate print_function(mlp, tdp, format, level); 159*7c478bd9Sstevel@tonic-gate break; 160*7c478bd9Sstevel@tonic-gate case UNION: 161*7c478bd9Sstevel@tonic-gate print_union(mlp, tdp, format, level); 162*7c478bd9Sstevel@tonic-gate break; 163*7c478bd9Sstevel@tonic-gate case ENUM: 164*7c478bd9Sstevel@tonic-gate print_enum(mlp, tdp, format, level); 165*7c478bd9Sstevel@tonic-gate break; 166*7c478bd9Sstevel@tonic-gate case FORWARD: 167*7c478bd9Sstevel@tonic-gate print_forward(mlp, tdp, format, level); 168*7c478bd9Sstevel@tonic-gate break; 169*7c478bd9Sstevel@tonic-gate case TYPEOF: 170*7c478bd9Sstevel@tonic-gate print_typeof(mlp, tdp, format, level); 171*7c478bd9Sstevel@tonic-gate break; 172*7c478bd9Sstevel@tonic-gate case STRUCT: 173*7c478bd9Sstevel@tonic-gate print_struct(mlp, tdp, format, level); 174*7c478bd9Sstevel@tonic-gate break; 175*7c478bd9Sstevel@tonic-gate case VOLATILE: 176*7c478bd9Sstevel@tonic-gate print_volatile(mlp, tdp, format, level); 177*7c478bd9Sstevel@tonic-gate break; 178*7c478bd9Sstevel@tonic-gate default: 179*7c478bd9Sstevel@tonic-gate fprintf(stderr, "Switch to Unknown type\n"); 180*7c478bd9Sstevel@tonic-gate error = B_TRUE; 181*7c478bd9Sstevel@tonic-gate break; 182*7c478bd9Sstevel@tonic-gate } 183*7c478bd9Sstevel@tonic-gate if (allocated) 184*7c478bd9Sstevel@tonic-gate free(format); 185*7c478bd9Sstevel@tonic-gate } 186*7c478bd9Sstevel@tonic-gate 187*7c478bd9Sstevel@tonic-gate 188*7c478bd9Sstevel@tonic-gate static void 189*7c478bd9Sstevel@tonic-gate print_forward(struct mlist *mlp, struct tdesc *tdp, char *format, int level) 190*7c478bd9Sstevel@tonic-gate { 191*7c478bd9Sstevel@tonic-gate fprintf(stderr, "%s never defined\n", mlp->name); 192*7c478bd9Sstevel@tonic-gate error = B_TRUE; 193*7c478bd9Sstevel@tonic-gate } 194*7c478bd9Sstevel@tonic-gate 195*7c478bd9Sstevel@tonic-gate static void 196*7c478bd9Sstevel@tonic-gate print_typeof(struct mlist *mlp, struct tdesc *tdp, char *format, int level) 197*7c478bd9Sstevel@tonic-gate { 198*7c478bd9Sstevel@tonic-gate switch_on_type(mlp, tdp->data.tdesc, format, level); 199*7c478bd9Sstevel@tonic-gate } 200*7c478bd9Sstevel@tonic-gate 201*7c478bd9Sstevel@tonic-gate static void 202*7c478bd9Sstevel@tonic-gate print_volatile(struct mlist *mlp, struct tdesc *tdp, char *format, int level) 203*7c478bd9Sstevel@tonic-gate { 204*7c478bd9Sstevel@tonic-gate switch_on_type(mlp, tdp->data.tdesc, format, level); 205*7c478bd9Sstevel@tonic-gate } 206*7c478bd9Sstevel@tonic-gate 207*7c478bd9Sstevel@tonic-gate static void 208*7c478bd9Sstevel@tonic-gate print_intrinsic(struct mlist *mlp, struct tdesc *tdp, 209*7c478bd9Sstevel@tonic-gate char *format, int level) 210*7c478bd9Sstevel@tonic-gate { 211*7c478bd9Sstevel@tonic-gate if (level != 0) { 212*7c478bd9Sstevel@tonic-gate switch (tdp->size) { 213*7c478bd9Sstevel@tonic-gate case 1: 214*7c478bd9Sstevel@tonic-gate printf("/* ' c@ ' %s */", format); 215*7c478bd9Sstevel@tonic-gate break; 216*7c478bd9Sstevel@tonic-gate case 2: 217*7c478bd9Sstevel@tonic-gate printf("/* ' w@ ' %s */", format); 218*7c478bd9Sstevel@tonic-gate break; 219*7c478bd9Sstevel@tonic-gate case 4: 220*7c478bd9Sstevel@tonic-gate printf("/* ' l@ ' %s */", format); 221*7c478bd9Sstevel@tonic-gate break; 222*7c478bd9Sstevel@tonic-gate case 8: 223*7c478bd9Sstevel@tonic-gate printf("/* ' x@ ' %s */", format); 224*7c478bd9Sstevel@tonic-gate break; 225*7c478bd9Sstevel@tonic-gate } 226*7c478bd9Sstevel@tonic-gate /* 227*7c478bd9Sstevel@tonic-gate * Check for bit field. 228*7c478bd9Sstevel@tonic-gate */ 229*7c478bd9Sstevel@tonic-gate } else if (mlp->size != 0 && 230*7c478bd9Sstevel@tonic-gate ((mlp->size % 8) != 0 || (mlp->offset % mlp->size) != 0)) { 231*7c478bd9Sstevel@tonic-gate int offset, shift, mask; 232*7c478bd9Sstevel@tonic-gate 233*7c478bd9Sstevel@tonic-gate offset = (mlp->offset / 32) * 4; 234*7c478bd9Sstevel@tonic-gate shift = 32 - ((mlp->offset % 32) + mlp->size); 235*7c478bd9Sstevel@tonic-gate mask = ((int)pow(2, mlp->size) - 1) << shift; 236*7c478bd9Sstevel@tonic-gate 237*7c478bd9Sstevel@tonic-gate printf("#define\t%s_SHIFT 0x%x\n", format, shift); 238*7c478bd9Sstevel@tonic-gate printf("#define\t%s_MASK 0x%x\n", format, mask); 239*7c478bd9Sstevel@tonic-gate printf("#define\t%s_OFFSET 0x%x\n", format, offset); 240*7c478bd9Sstevel@tonic-gate } else if (mlp->name != NULL) { 241*7c478bd9Sstevel@tonic-gate printf("#define\t%s 0x%x\n", format, mlp->offset / 8); 242*7c478bd9Sstevel@tonic-gate } 243*7c478bd9Sstevel@tonic-gate } 244*7c478bd9Sstevel@tonic-gate 245*7c478bd9Sstevel@tonic-gate static void 246*7c478bd9Sstevel@tonic-gate print_pointer(struct mlist *mlp, struct tdesc *tdp, char *format, int level) 247*7c478bd9Sstevel@tonic-gate { 248*7c478bd9Sstevel@tonic-gate if (level != 0) { 249*7c478bd9Sstevel@tonic-gate switch (tdp->size) { 250*7c478bd9Sstevel@tonic-gate case 1: 251*7c478bd9Sstevel@tonic-gate printf("/* ' c@ ' %s */", format); 252*7c478bd9Sstevel@tonic-gate break; 253*7c478bd9Sstevel@tonic-gate case 2: 254*7c478bd9Sstevel@tonic-gate printf("/* ' w@ ' %s */", format); 255*7c478bd9Sstevel@tonic-gate break; 256*7c478bd9Sstevel@tonic-gate case 4: 257*7c478bd9Sstevel@tonic-gate printf("/* ' l@ ' %s */", format); 258*7c478bd9Sstevel@tonic-gate break; 259*7c478bd9Sstevel@tonic-gate case 8: 260*7c478bd9Sstevel@tonic-gate printf("/* ' x@ ' %s */", format); 261*7c478bd9Sstevel@tonic-gate break; 262*7c478bd9Sstevel@tonic-gate } 263*7c478bd9Sstevel@tonic-gate } else { 264*7c478bd9Sstevel@tonic-gate printf("#define\t%s 0x%x\n", format, mlp->offset / 8); 265*7c478bd9Sstevel@tonic-gate } 266*7c478bd9Sstevel@tonic-gate } 267*7c478bd9Sstevel@tonic-gate 268*7c478bd9Sstevel@tonic-gate static void 269*7c478bd9Sstevel@tonic-gate print_array(struct mlist *mlp, struct tdesc *tdp, char *format, int level) 270*7c478bd9Sstevel@tonic-gate { 271*7c478bd9Sstevel@tonic-gate struct ardef *ap = tdp->data.ardef; 272*7c478bd9Sstevel@tonic-gate int items, inc; 273*7c478bd9Sstevel@tonic-gate 274*7c478bd9Sstevel@tonic-gate if (level == 0) { 275*7c478bd9Sstevel@tonic-gate items = ap->indices->range_end - ap->indices->range_start + 1; 276*7c478bd9Sstevel@tonic-gate inc = (mlp->size / items) / 8; 277*7c478bd9Sstevel@tonic-gate printf("#define\t%s 0x%x\n", format, mlp->offset / 8); 278*7c478bd9Sstevel@tonic-gate printf("#define\t%s_INCR 0x%x\n", format, inc); 279*7c478bd9Sstevel@tonic-gate } 280*7c478bd9Sstevel@tonic-gate } 281*7c478bd9Sstevel@tonic-gate 282*7c478bd9Sstevel@tonic-gate static void 283*7c478bd9Sstevel@tonic-gate print_function(struct mlist *mlp, struct tdesc *tdp, char *format, int level) 284*7c478bd9Sstevel@tonic-gate { 285*7c478bd9Sstevel@tonic-gate fprintf(stderr, "function in struct %s\n", tdp->name); 286*7c478bd9Sstevel@tonic-gate error = B_TRUE; 287*7c478bd9Sstevel@tonic-gate } 288*7c478bd9Sstevel@tonic-gate 289*7c478bd9Sstevel@tonic-gate static void 290*7c478bd9Sstevel@tonic-gate print_struct(struct mlist *mlp, struct tdesc *tdp, char *format, int level) 291*7c478bd9Sstevel@tonic-gate { 292*7c478bd9Sstevel@tonic-gate if (level != 0) 293*7c478bd9Sstevel@tonic-gate printf("/* ' noop ' %s */", format); 294*7c478bd9Sstevel@tonic-gate else 295*7c478bd9Sstevel@tonic-gate printf("#define\t%s 0x%x\n", format, mlp->offset / 8); 296*7c478bd9Sstevel@tonic-gate } 297*7c478bd9Sstevel@tonic-gate 298*7c478bd9Sstevel@tonic-gate static void 299*7c478bd9Sstevel@tonic-gate print_union(struct mlist *mlp, struct tdesc *tdp, char *format, int level) 300*7c478bd9Sstevel@tonic-gate { 301*7c478bd9Sstevel@tonic-gate if (level != 0) 302*7c478bd9Sstevel@tonic-gate printf("/* ' noop ' %s */", format); 303*7c478bd9Sstevel@tonic-gate else 304*7c478bd9Sstevel@tonic-gate printf("#define\t%s 0x%x\n", format, mlp->offset / 8); 305*7c478bd9Sstevel@tonic-gate } 306*7c478bd9Sstevel@tonic-gate 307*7c478bd9Sstevel@tonic-gate static void 308*7c478bd9Sstevel@tonic-gate print_enum(struct mlist *mlp, struct tdesc *tdp, char *format, int level) 309*7c478bd9Sstevel@tonic-gate { 310*7c478bd9Sstevel@tonic-gate if (level != 0) 311*7c478bd9Sstevel@tonic-gate printf("/* ' l@ ' %s */", format); 312*7c478bd9Sstevel@tonic-gate else 313*7c478bd9Sstevel@tonic-gate printf("#define\t%s 0x%x\n", format, mlp->offset / 8); 314*7c478bd9Sstevel@tonic-gate } 315*7c478bd9Sstevel@tonic-gate 316*7c478bd9Sstevel@tonic-gate static int 317*7c478bd9Sstevel@tonic-gate stabs_log2(unsigned int value) 318*7c478bd9Sstevel@tonic-gate { 319*7c478bd9Sstevel@tonic-gate int log = 1; 320*7c478bd9Sstevel@tonic-gate int i; 321*7c478bd9Sstevel@tonic-gate 322*7c478bd9Sstevel@tonic-gate for (i = 0; i < sizeof (value) * 8; i++) { 323*7c478bd9Sstevel@tonic-gate if ((log << i) == value) 324*7c478bd9Sstevel@tonic-gate return (i); 325*7c478bd9Sstevel@tonic-gate } 326*7c478bd9Sstevel@tonic-gate return (-1); 327*7c478bd9Sstevel@tonic-gate } 328