cint_ast_debug.c (9084B)
1 /* 2 * 3 * This license is set out in https://raw.githubusercontent.com/Broadcom-Network-Switching-Software/OpenBCM/master/Legal/LICENSE file. 4 * 5 * Copyright 2007-2019 Broadcom Inc. All rights reserved. 6 * 7 * File: cint_ast_debug.c 8 * Purpose: CINT debug functions 9 */ 10 11 #include "cint_config.h" 12 #include "cint_porting.h" 13 #include "cint_internal.h" 14 #include "cint_ast.h" 15 16 static void 17 __indent(int count) 18 { 19 while(count--) CINT_PRINTF(" "); 20 } 21 22 static int 23 __print(int indent, const char* fmt, ...) 24 COMPILER_ATTRIBUTE((format (printf, 2, 3))); 25 26 static int 27 __print(int indent, const char* fmt, ...) 28 { 29 va_list args; 30 va_start(args, fmt); 31 __indent(indent); 32 CINT_VPRINTF(fmt, args); 33 va_end(args); 34 return 0; 35 } 36 37 static int 38 __start_member(int indent, const char* str) 39 { 40 return __print(indent, "{ %s\n", str); 41 } 42 43 static int 44 __end_member(int indent) 45 { 46 return __print(indent, "}\n"); 47 } 48 49 static int 50 __member(int indent, const char* fmt, ...) 51 COMPILER_ATTRIBUTE((format (printf, 2, 3))); 52 53 static int 54 __member(int indent, const char* fmt, ...) 55 { 56 va_list args; 57 va_start(args, fmt); 58 __indent(indent); 59 CINT_PRINTF("{ "); 60 CINT_VPRINTF(fmt, args); 61 CINT_PRINTF(" }\n"); 62 va_end(args); 63 return 0; 64 } 65 66 #define START(_str) __start_member(indent, _str) 67 #define END() __end_member(indent) 68 #define MEMBER(expr) __member expr 69 70 71 static void 72 __cint_ast_dump_Empty(cint_ast_t* ast, int indent) 73 { 74 } 75 76 static void 77 __cint_ast_dump_Integer(cint_ast_t* ast, int indent) 78 { 79 int i = ast->utype.integer.i; 80 __print(indent, "0x%x - %d - %u\n", i, i, i); 81 } 82 83 #if CINT_CONFIG_INCLUDE_LONGLONGS == 1 84 static void 85 __cint_ast_dump_LongLong(cint_ast_t* ast, int indent) 86 { 87 long long i = ast->utype._longlong.i; 88 char buf1[50], *s1; 89 char buf2[50], *s2; 90 char buf3[50], *s3; 91 92 s1 = cint_lltoa(buf1, sizeof(buf1), i, 0, 16, 16); 93 s2 = cint_lltoa(buf2, sizeof(buf2), i, 0, 10, 0); 94 s3 = cint_lltoa(buf3, sizeof(buf3), i, 0, 10, 0); 95 __print(indent, "0x%s - %s - %s\n", s1, s2, s3); 96 } 97 #endif 98 99 #if CINT_CONFIG_INCLUDE_DOUBLES == 1 100 static void 101 __cint_ast_dump_Double(cint_ast_t* ast, int indent) 102 { 103 double d = ast->utype._double.d; 104 __print(indent, "%f\n", d); 105 } 106 #endif 107 108 static void 109 __cint_ast_dump_String(cint_ast_t* ast, int indent) 110 { 111 __print(indent, "'%s'\n", 112 ast->utype.string.s); 113 } 114 115 116 static void 117 __cint_ast_dump_Type(cint_ast_t* ast, int indent) 118 { 119 __print(indent, "'%s'\n", 120 ast->utype.type.s); 121 } 122 123 124 static void 125 __cint_ast_dump_Identifier(cint_ast_t* ast, int indent) 126 { 127 __print(indent, "'%s'\n", 128 ast->utype.identifier.s); 129 } 130 131 static void 132 __cint_ast_dump_Initializer(cint_ast_t* ast, int indent) 133 { 134 cint_ast_dump(ast->utype.initializer.initializers, indent+4); 135 } 136 137 static void 138 __cint_ast_dump_Declaration(cint_ast_t* ast, int indent) 139 { 140 int dimension = 0; 141 142 START("TYPE"); 143 cint_ast_dump(ast->utype.declaration.type, indent+4); 144 END(); 145 146 MEMBER((indent, "PCOUNT %d", ast->utype.declaration.pcount)); 147 148 START("DIMENSIONS"); 149 for(dimension = 0; 150 dimension < CINT_CONFIG_ARRAY_DIMENSION_LIMIT; 151 ++dimension) { 152 cint_ast_dump( 153 ast->utype.declaration.dimension_initializers[dimension], 154 indent+4); 155 } 156 END(); 157 158 START("IDENT"); 159 cint_ast_dump(ast->utype.declaration.identifier, indent+4); 160 END(); 161 162 START("INIT"); 163 cint_ast_dump(ast->utype.declaration.init, indent+4); 164 END(); 165 166 MEMBER( 167 (indent, 168 "ARRAYDIMENSIONS %d", 169 ast->utype.declaration.num_dimension_initializers)); 170 } 171 172 173 static void 174 __cint_ast_dump_Operator(cint_ast_t* ast, int indent) 175 { 176 MEMBER((indent, "OP = '%s'", cint_operator_name(ast->utype.operator.op))); 177 178 START("LEFT"); 179 cint_ast_dump(ast->utype.operator.left, indent+4); 180 END(); 181 182 START("RIGHT"); 183 cint_ast_dump(ast->utype.operator.right, indent+4); 184 END(); 185 186 START("EXTRA"); 187 cint_ast_dump(ast->utype.operator.extra, indent+4); 188 END(); 189 } 190 191 static void 192 __cint_ast_dump_Function(cint_ast_t* ast, int indent) 193 { 194 MEMBER((indent, "NAME = '%s'", ast->utype.function.name)); 195 196 START("PARAMS"); 197 cint_ast_dump(ast->utype.function.parameters, indent+4); 198 END(); 199 } 200 201 static void 202 __cint_ast_dump_FunctionDef(cint_ast_t* ast, int indent) 203 { 204 START("DECLARATION"); 205 cint_ast_dump(ast->utype.functiondef.declaration, indent+4); 206 END(); 207 208 START("PARAMETERS"); 209 cint_ast_dump(ast->utype.functiondef.parameters, indent+4); 210 END(); 211 212 START("STATEMENTS"); 213 cint_ast_dump(ast->utype.functiondef.statements, indent+4); 214 END(); 215 } 216 217 static void 218 __cint_ast_dump_StructureDef(cint_ast_t* ast, int indent) 219 { 220 START("NAME"); 221 cint_ast_dump(ast->utype.structuredef.name, indent+4); 222 END(); 223 224 START("MEMBERS"); 225 cint_ast_dump(ast->utype.structuredef.members, indent+4); 226 END(); 227 } 228 229 static void 230 __cint_ast_dump_Elist(cint_ast_t* ast, int indent) 231 { 232 cint_ast_t* p; 233 for(p = ast->utype.elist.list; p; p = p->next) { 234 cint_ast_dump(p, indent); 235 } 236 } 237 238 static void 239 __cint_ast_dump_Enumerator(cint_ast_t* ast, int indent) 240 { 241 START("IDENTIFIER"); 242 cint_ast_dump(ast->utype.enumerator.identifier, indent+4); 243 END(); 244 245 START("VALUE"); 246 cint_ast_dump(ast->utype.enumerator.value, indent+4); 247 END(); 248 } 249 250 static void 251 __cint_ast_dump_EnumDef(cint_ast_t* ast, int indent) 252 { 253 START("IDENTIFIER"); 254 cint_ast_dump(ast->utype.enumdef.identifier, indent+4); 255 END(); 256 257 START("ENUMERATORS"); 258 cint_ast_dump(ast->utype.enumdef.enumerators, indent+4); 259 END(); 260 } 261 262 263 static void 264 __cint_ast_dump_While(cint_ast_t* ast, int indent) 265 { 266 MEMBER((indent, "ORDER = %d", ast->utype._while.order)); 267 268 START("CONDITION"); 269 cint_ast_dump(ast->utype._while.condition, indent+4); 270 END(); 271 272 START("STATEMENTS"); 273 cint_ast_dump(ast->utype._while.statements, indent+4); 274 END(); 275 } 276 277 278 static void 279 __cint_ast_dump_For(cint_ast_t* ast, int indent) 280 { 281 START("PRE"); 282 cint_ast_dump(ast->utype._for.pre, indent+4); 283 END(); 284 285 START("CONDITION"); 286 cint_ast_dump(ast->utype._for.condition, indent+4); 287 END(); 288 289 START("POST"); 290 cint_ast_dump(ast->utype._for.post, indent+4); 291 END(); 292 293 START("STATEMENTS"); 294 cint_ast_dump(ast->utype._for.statements, indent+4); 295 END(); 296 } 297 298 299 static void 300 __cint_ast_dump_If(cint_ast_t* ast, int indent) 301 { 302 START("CONDITION"); 303 cint_ast_dump(ast->utype._if.condition, indent+4); 304 END(); 305 306 START("STATEMENTS"); 307 cint_ast_dump(ast->utype._if.statements, indent+4); 308 END(); 309 310 START("ELSE"); 311 cint_ast_dump(ast->utype._if._else, indent+4); 312 END(); 313 } 314 315 static void 316 __cint_ast_dump_Return(cint_ast_t* ast, int indent) 317 { 318 START("EXPR"); 319 cint_ast_dump(ast->utype._return.expression, indent+4); 320 END(); 321 } 322 323 static void 324 __cint_ast_dump_Continue(cint_ast_t* ast, int indent) 325 { 326 /* Nothing */ 327 } 328 329 static void 330 __cint_ast_dump_Break(cint_ast_t* ast, int indent) 331 { 332 /* Nothing */ 333 } 334 335 static void 336 __cint_ast_dump_Print(cint_ast_t* ast, int indent) 337 { 338 START("EXPR"); 339 cint_ast_dump(ast->utype.print.expression, indent+4); 340 END(); 341 } 342 343 static void 344 __cint_ast_dump_Cint(cint_ast_t* ast, int indent) 345 { 346 START("EXPR"); 347 cint_ast_dump(ast->utype.cint.arguments, indent+4); 348 END(); 349 } 350 351 static void 352 __cint_ast_dump_Switch(cint_ast_t* ast, int indent) 353 { 354 START("EXPR"); 355 cint_ast_dump(ast->utype._switch.expression, indent+4); 356 END(); 357 START("STATEMENTS"); 358 cint_ast_dump(ast->utype._switch.statements, indent+4); 359 END(); 360 } 361 362 static void 363 __cint_ast_dump_Case(cint_ast_t* ast, int indent) 364 { 365 START("EXPR"); 366 cint_ast_dump(ast->utype._case.expression, indent+4); 367 END(); 368 START("STATEMENTS"); 369 cint_ast_dump(ast->utype._case.statements, indent+4); 370 END(); 371 } 372 373 374 375 struct { 376 const char* name; 377 void (*dump)(cint_ast_t* node, int indent); 378 } __ast_table[] = { 379 380 #define CINT_AST_LIST_ENTRY(_entry) { #_entry, __cint_ast_dump_##_entry }, 381 #include "cint_ast_entry.h" 382 { NULL } 383 }; 384 385 void 386 cint_ast_dump_single(cint_ast_t* ast, int indent) 387 { 388 cint_ast_type_t t; 389 390 if(ast == NULL) { 391 __print(indent, "NULL\n"); 392 } else if (ast == CINT_AST_PTR_VOID) { 393 __print(indent, "VOID *\n"); 394 } else if (ast == CINT_AST_PTR_AUTO) { 395 __print(indent, "AUTO\n"); 396 } else { 397 398 t = ast->ntype; 399 400 if(!CINT_AST_TYPE_VALID(t)) { 401 __print(indent, "INVALID AST TYPE %d", t); 402 } 403 else { 404 __print(indent, "{ %s\n", __ast_table[t].name); 405 __ast_table[t].dump(ast, indent+4); 406 __print(indent, "}\n"); 407 } 408 } 409 } 410 411 void 412 cint_ast_dump(cint_ast_t* ast, int indent) 413 { 414 cint_ast_dump_single(ast, indent); 415 if(ast && 416 ast != CINT_AST_PTR_VOID && 417 ast != CINT_AST_PTR_AUTO && ast->next) { 418 cint_ast_dump(ast->next, indent); 419 } 420 }