11 #include "adt/error.h"
13 struct obstack ast_obstack;
18 /** If set, implicit casts are printed. */
19 bool print_implicit_casts = false;
21 /** If set parenthesis are printed to indicate operator precedence. */
22 bool print_parenthesis = false;
24 static void print_statement(const statement_t *statement);
25 static void print_expression_prec(const expression_t *expression, unsigned prec);
27 void change_indent(int delta)
33 void print_indent(void)
35 for(int i = 0; i < indent; ++i)
41 PREC_COMMA = 2, /* , left to right */
42 PREC_ASSIGN = 4, /* = += -= *= /= %= <<= >>= &= ^= |= right to left */
43 PREC_COND = 6, /* ?: right to left */
44 PREC_LOG_OR = 8, /* || left to right */
45 PREC_LOG_AND = 10, /* && left to right */
46 PREC_BIT_OR = 12, /* | left to right */
47 PREC_BIT_XOR = 14, /* ^ left to right */
48 PREC_BIT_AND = 16, /* & left to right */
49 PREC_EQ = 18, /* == != left to right */
50 PREC_CMP = 20, /* < <= > >= left to right */
51 PREC_SHF = 22, /* << >> left to right */
52 PREC_PLUS = 24, /* + - left to right */
53 PREC_MUL = 26, /* * / % left to right */
54 PREC_UNARY = 28, /* ! ~ ++ -- + - (type) * & sizeof right to left */
55 PREC_ACCESS = 30, /* () [] -> . left to right */
56 PREC_PRIM = 32, /* primary */
61 * Returns 1 if a given precedence level has right-to-left
62 * associativity, else -1.
64 * @param precedence the operator precedence
66 static int right_to_left(unsigned precedence) {
67 return (precedence == PREC_ASSIGN || precedence == PREC_COND ||
68 precedence == PREC_UNARY) ? 1 : -1;
72 * Return the precedence of an expression given by its kind.
74 * @param kind the expression kind
76 static unsigned get_expression_precedence(expression_kind_t kind)
78 static const unsigned prec[] = {
79 [EXPR_UNKNOWN] = PREC_PRIM,
80 [EXPR_INVALID] = PREC_PRIM,
81 [EXPR_REFERENCE] = PREC_PRIM,
82 [EXPR_CHAR_CONST] = PREC_PRIM,
83 [EXPR_CONST] = PREC_PRIM,
84 [EXPR_STRING_LITERAL] = PREC_PRIM,
85 [EXPR_WIDE_STRING_LITERAL] = PREC_PRIM,
86 [EXPR_COMPOUND_LITERAL] = PREC_UNARY,
87 [EXPR_CALL] = PREC_PRIM,
88 [EXPR_CONDITIONAL] = PREC_COND,
89 [EXPR_SELECT] = PREC_ACCESS,
90 [EXPR_ARRAY_ACCESS] = PREC_ACCESS,
91 [EXPR_SIZEOF] = PREC_UNARY,
92 [EXPR_CLASSIFY_TYPE] = PREC_UNARY,
93 [EXPR_ALIGNOF] = PREC_UNARY,
95 [EXPR_FUNCTION] = PREC_PRIM,
96 [EXPR_PRETTY_FUNCTION] = PREC_PRIM,
97 [EXPR_BUILTIN_SYMBOL] = PREC_PRIM,
98 [EXPR_BUILTIN_CONSTANT_P] = PREC_PRIM,
99 [EXPR_BUILTIN_PREFETCH] = PREC_PRIM,
100 [EXPR_OFFSETOF] = PREC_PRIM,
101 [EXPR_VA_START] = PREC_PRIM,
102 [EXPR_VA_ARG] = PREC_PRIM,
103 [EXPR_STATEMENT] = PREC_ACCESS,
105 [EXPR_UNARY_NEGATE] = PREC_UNARY,
106 [EXPR_UNARY_PLUS] = PREC_UNARY,
107 [EXPR_UNARY_BITWISE_NEGATE] = PREC_UNARY,
108 [EXPR_UNARY_NOT] = PREC_UNARY,
109 [EXPR_UNARY_DEREFERENCE] = PREC_UNARY,
110 [EXPR_UNARY_TAKE_ADDRESS] = PREC_UNARY,
111 [EXPR_UNARY_POSTFIX_INCREMENT] = PREC_UNARY,
112 [EXPR_UNARY_POSTFIX_DECREMENT] = PREC_UNARY,
113 [EXPR_UNARY_PREFIX_INCREMENT] = PREC_UNARY,
114 [EXPR_UNARY_PREFIX_DECREMENT] = PREC_UNARY,
115 [EXPR_UNARY_CAST] = PREC_UNARY,
116 [EXPR_UNARY_CAST_IMPLICIT] = PREC_UNARY,
117 [EXPR_UNARY_ASSUME] = PREC_PRIM,
118 [EXPR_UNARY_BITFIELD_EXTRACT] = PREC_ACCESS,
120 [EXPR_BINARY_ADD] = PREC_PLUS,
121 [EXPR_BINARY_SUB] = PREC_PLUS,
122 [EXPR_BINARY_MUL] = PREC_MUL,
123 [EXPR_BINARY_DIV] = PREC_MUL,
124 [EXPR_BINARY_MOD] = PREC_MUL,
125 [EXPR_BINARY_EQUAL] = PREC_EQ,
126 [EXPR_BINARY_NOTEQUAL] = PREC_EQ,
127 [EXPR_BINARY_LESS] = PREC_CMP,
128 [EXPR_BINARY_LESSEQUAL] = PREC_CMP,
129 [EXPR_BINARY_GREATER] = PREC_CMP,
130 [EXPR_BINARY_GREATEREQUAL] = PREC_CMP,
131 [EXPR_BINARY_BITWISE_AND] = PREC_BIT_AND,
132 [EXPR_BINARY_BITWISE_OR] = PREC_BIT_OR,
133 [EXPR_BINARY_BITWISE_XOR] = PREC_BIT_XOR,
134 [EXPR_BINARY_LOGICAL_AND] = PREC_LOG_AND,
135 [EXPR_BINARY_LOGICAL_OR] = PREC_LOG_OR,
136 [EXPR_BINARY_SHIFTLEFT] = PREC_SHF,
137 [EXPR_BINARY_SHIFTRIGHT] = PREC_SHF,
138 [EXPR_BINARY_ASSIGN] = PREC_ASSIGN,
139 [EXPR_BINARY_MUL_ASSIGN] = PREC_ASSIGN,
140 [EXPR_BINARY_DIV_ASSIGN] = PREC_ASSIGN,
141 [EXPR_BINARY_MOD_ASSIGN] = PREC_ASSIGN,
142 [EXPR_BINARY_ADD_ASSIGN] = PREC_ASSIGN,
143 [EXPR_BINARY_SUB_ASSIGN] = PREC_ASSIGN,
144 [EXPR_BINARY_SHIFTLEFT_ASSIGN] = PREC_ASSIGN,
145 [EXPR_BINARY_SHIFTRIGHT_ASSIGN] = PREC_ASSIGN,
146 [EXPR_BINARY_BITWISE_AND_ASSIGN] = PREC_ASSIGN,
147 [EXPR_BINARY_BITWISE_XOR_ASSIGN] = PREC_ASSIGN,
148 [EXPR_BINARY_BITWISE_OR_ASSIGN] = PREC_ASSIGN,
149 [EXPR_BINARY_COMMA] = PREC_COMMA,
151 [EXPR_BINARY_BUILTIN_EXPECT] = PREC_PRIM,
152 [EXPR_BINARY_ISGREATER] = PREC_PRIM,
153 [EXPR_BINARY_ISGREATEREQUAL] = PREC_PRIM,
154 [EXPR_BINARY_ISLESS] = PREC_PRIM,
155 [EXPR_BINARY_ISLESSEQUAL] = PREC_PRIM,
156 [EXPR_BINARY_ISLESSGREATER] = PREC_PRIM,
157 [EXPR_BINARY_ISUNORDERED] = PREC_PRIM
159 assert((unsigned)kind < (sizeof(prec)/sizeof(prec[0])));
160 unsigned res = prec[kind];
162 assert(res != PREC_BOTTOM);
167 * Print a constant expression.
169 * @param cnst the constant expression
171 static void print_const(const const_expression_t *cnst)
173 if(cnst->base.type == NULL)
176 const type_t *const type = skip_typeref(cnst->base.type);
178 if (is_type_integer(type)) {
179 fprintf(out, "%lld", cnst->v.int_value);
180 } else if (is_type_float(type)) {
181 fprintf(out, "%Lf", cnst->v.float_value);
183 panic("unknown constant");
188 * Print a quoted string constant.
190 * @param string the string constant
191 * @param border the border char
193 static void print_quoted_string(const string_t *const string, char border)
196 const char *end = string->begin + string->size;
197 for (const char *c = string->begin; c != end; ++c) {
202 case '\\': fputs("\\\\", out); break;
203 case '\a': fputs("\\a", out); break;
204 case '\b': fputs("\\b", out); break;
205 case '\f': fputs("\\f", out); break;
206 case '\n': fputs("\\n", out); break;
207 case '\r': fputs("\\r", out); break;
208 case '\t': fputs("\\t", out); break;
209 case '\v': fputs("\\v", out); break;
210 case '\?': fputs("\\?", out); break;
213 fprintf(out, "\\%03o", *c);
224 * Print a constant character expression.
226 * @param cnst the constant character expression
228 static void print_char_const(const const_expression_t *cnst)
230 print_quoted_string(&cnst->v.chars, '\'');
234 * Prints a string literal expression.
236 * @param string_literal the string literal expression
238 static void print_string_literal(
239 const string_literal_expression_t *string_literal)
241 print_quoted_string(&string_literal->value, '"');
245 * Prints a wide string literal expression.
247 * @param wstr the wide string literal expression
249 static void print_quoted_wide_string(const wide_string_t *const wstr)
252 for (const wchar_rep_t *c = wstr->begin, *end = wstr->begin + wstr->size;
255 case L'\"': fputs("\\\"", out); break;
256 case L'\\': fputs("\\\\", out); break;
257 case L'\a': fputs("\\a", out); break;
258 case L'\b': fputs("\\b", out); break;
259 case L'\f': fputs("\\f", out); break;
260 case L'\n': fputs("\\n", out); break;
261 case L'\r': fputs("\\r", out); break;
262 case L'\t': fputs("\\t", out); break;
263 case L'\v': fputs("\\v", out); break;
264 case L'\?': fputs("\\?", out); break;
266 const unsigned tc = *c;
269 fprintf(out, "\\%03o", (char)*c);
273 } else if (tc < 0x800) {
274 fputc(0xC0 | (tc >> 6), out);
275 fputc(0x80 | (tc & 0x3F), out);
276 } else if (tc < 0x10000) {
277 fputc(0xE0 | ( tc >> 12), out);
278 fputc(0x80 | ((tc >> 6) & 0x3F), out);
279 fputc(0x80 | ( tc & 0x3F), out);
281 fputc(0xF0 | ( tc >> 18), out);
282 fputc(0x80 | ((tc >> 12) & 0x3F), out);
283 fputc(0x80 | ((tc >> 6) & 0x3F), out);
284 fputc(0x80 | ( tc & 0x3F), out);
292 static void print_wide_string_literal(
293 const wide_string_literal_expression_t *const wstr)
295 print_quoted_wide_string(&wstr->value);
298 static void print_compound_literal(
299 const compound_literal_expression_t *expression)
302 print_type(expression->type);
304 print_initializer(expression->initializer);
308 * Prints a call expression.
310 * @param call the call expression
312 static void print_call_expression(const call_expression_t *call)
314 unsigned prec = get_expression_precedence(call->base.kind);
315 print_expression_prec(call->function, prec);
317 call_argument_t *argument = call->arguments;
319 while(argument != NULL) {
325 print_expression_prec(argument->expression, PREC_COMMA + 1);
327 argument = argument->next;
333 * Prints a binary expression.
335 * @param binexpr the binary expression
337 static void print_binary_expression(const binary_expression_t *binexpr)
339 unsigned prec = get_expression_precedence(binexpr->base.kind);
340 int r2l = right_to_left(prec);
342 if(binexpr->base.kind == EXPR_BINARY_BUILTIN_EXPECT) {
343 fputs("__builtin_expect(", out);
344 print_expression_prec(binexpr->left, prec);
346 print_expression_prec(binexpr->right, prec);
351 print_expression_prec(binexpr->left, prec + r2l);
352 if (binexpr->base.kind != EXPR_BINARY_COMMA) {
355 switch (binexpr->base.kind) {
356 case EXPR_BINARY_COMMA: fputs(",", out); break;
357 case EXPR_BINARY_ASSIGN: fputs("=", out); break;
358 case EXPR_BINARY_ADD: fputs("+", out); break;
359 case EXPR_BINARY_SUB: fputs("-", out); break;
360 case EXPR_BINARY_MUL: fputs("*", out); break;
361 case EXPR_BINARY_MOD: fputs("%", out); break;
362 case EXPR_BINARY_DIV: fputs("/", out); break;
363 case EXPR_BINARY_BITWISE_OR: fputs("|", out); break;
364 case EXPR_BINARY_BITWISE_AND: fputs("&", out); break;
365 case EXPR_BINARY_BITWISE_XOR: fputs("^", out); break;
366 case EXPR_BINARY_LOGICAL_OR: fputs("||", out); break;
367 case EXPR_BINARY_LOGICAL_AND: fputs("&&", out); break;
368 case EXPR_BINARY_NOTEQUAL: fputs("!=", out); break;
369 case EXPR_BINARY_EQUAL: fputs("==", out); break;
370 case EXPR_BINARY_LESS: fputs("<", out); break;
371 case EXPR_BINARY_LESSEQUAL: fputs("<=", out); break;
372 case EXPR_BINARY_GREATER: fputs(">", out); break;
373 case EXPR_BINARY_GREATEREQUAL: fputs(">=", out); break;
374 case EXPR_BINARY_SHIFTLEFT: fputs("<<", out); break;
375 case EXPR_BINARY_SHIFTRIGHT: fputs(">>", out); break;
377 case EXPR_BINARY_ADD_ASSIGN: fputs("+=", out); break;
378 case EXPR_BINARY_SUB_ASSIGN: fputs("-=", out); break;
379 case EXPR_BINARY_MUL_ASSIGN: fputs("*=", out); break;
380 case EXPR_BINARY_MOD_ASSIGN: fputs("%=", out); break;
381 case EXPR_BINARY_DIV_ASSIGN: fputs("/=", out); break;
382 case EXPR_BINARY_BITWISE_OR_ASSIGN: fputs("|=", out); break;
383 case EXPR_BINARY_BITWISE_AND_ASSIGN: fputs("&=", out); break;
384 case EXPR_BINARY_BITWISE_XOR_ASSIGN: fputs("^=", out); break;
385 case EXPR_BINARY_SHIFTLEFT_ASSIGN: fputs("<<=", out); break;
386 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: fputs(">>=", out); break;
387 default: panic("invalid binexpression found");
390 print_expression_prec(binexpr->right, prec - r2l);
394 * Prints an unary expression.
396 * @param unexpr the unary expression
398 static void print_unary_expression(const unary_expression_t *unexpr)
400 unsigned prec = get_expression_precedence(unexpr->base.kind);
401 switch(unexpr->base.kind) {
402 case EXPR_UNARY_NEGATE: fputs("-", out); break;
403 case EXPR_UNARY_PLUS: fputs("+", out); break;
404 case EXPR_UNARY_NOT: fputs("!", out); break;
405 case EXPR_UNARY_BITWISE_NEGATE: fputs("~", out); break;
406 case EXPR_UNARY_PREFIX_INCREMENT: fputs("++", out); break;
407 case EXPR_UNARY_PREFIX_DECREMENT: fputs("--", out); break;
408 case EXPR_UNARY_DEREFERENCE: fputs("*", out); break;
409 case EXPR_UNARY_TAKE_ADDRESS: fputs("&", out); break;
411 case EXPR_UNARY_BITFIELD_EXTRACT:
412 print_expression_prec(unexpr->value, prec);
415 case EXPR_UNARY_POSTFIX_INCREMENT:
416 print_expression_prec(unexpr->value, prec);
419 case EXPR_UNARY_POSTFIX_DECREMENT:
420 print_expression_prec(unexpr->value, prec);
423 case EXPR_UNARY_CAST_IMPLICIT:
424 if(!print_implicit_casts) {
425 print_expression_prec(unexpr->value, prec);
429 case EXPR_UNARY_CAST:
431 print_type(unexpr->base.type);
434 case EXPR_UNARY_ASSUME:
435 fputs("__assume(", out);
436 print_expression_prec(unexpr->value, PREC_COMMA + 1);
440 panic("invalid unary expression found");
442 print_expression_prec(unexpr->value, prec);
446 * Prints a reference expression.
448 * @param ref the reference expression
450 static void print_reference_expression(const reference_expression_t *ref)
452 fprintf(out, "%s", ref->declaration->symbol->string);
456 * Prints an array expression.
458 * @param expression the array expression
460 static void print_array_expression(const array_access_expression_t *expression)
462 unsigned prec = get_expression_precedence(expression->base.kind);
463 if(!expression->flipped) {
464 print_expression_prec(expression->array_ref, prec);
466 print_expression_prec(expression->index, prec);
469 print_expression_prec(expression->index, prec);
471 print_expression_prec(expression->array_ref, prec);
477 * Prints a typeproperty expression (sizeof or __alignof__).
479 * @param expression the type property expression
481 static void print_typeprop_expression(const typeprop_expression_t *expression)
483 if (expression->base.kind == EXPR_SIZEOF) {
484 fputs("sizeof", out);
486 assert(expression->base.kind == EXPR_ALIGNOF);
487 fputs("__alignof__", out);
489 if(expression->tp_expression != NULL) {
490 /* always print the '()' here, sizeof x is right but unusual */
492 print_expression_prec(expression->tp_expression, PREC_ACCESS);
496 print_type(expression->type);
502 * Prints an builtin symbol.
504 * @param expression the builtin symbol expression
506 static void print_builtin_symbol(const builtin_symbol_expression_t *expression)
508 fputs(expression->symbol->string, out);
512 * Prints a builtin constant expression.
514 * @param expression the builtin constant expression
516 static void print_builtin_constant(const builtin_constant_expression_t *expression)
518 fputs("__builtin_constant_p(", out);
519 print_expression_prec(expression->value, PREC_COMMA + 1);
524 * Prints a builtin prefetch expression.
526 * @param expression the builtin prefetch expression
528 static void print_builtin_prefetch(const builtin_prefetch_expression_t *expression)
530 fputs("__builtin_prefetch(", out);
531 print_expression_prec(expression->adr, PREC_COMMA + 1);
532 if (expression->rw) {
534 print_expression_prec(expression->rw, PREC_COMMA + 1);
536 if (expression->locality) {
538 print_expression_prec(expression->locality, PREC_COMMA + 1);
544 * Prints a conditional expression.
546 * @param expression the conditional expression
548 static void print_conditional(const conditional_expression_t *expression)
550 unsigned prec = get_expression_precedence(expression->base.kind);
552 print_expression_prec(expression->condition, prec);
554 print_expression_prec(expression->true_expression, prec);
556 print_expression_prec(expression->false_expression, prec);
561 * Prints a va_start expression.
563 * @param expression the va_start expression
565 static void print_va_start(const va_start_expression_t *const expression)
567 fputs("__builtin_va_start(", out);
568 print_expression_prec(expression->ap, PREC_COMMA + 1);
570 fputs(expression->parameter->symbol->string, out);
575 * Prints a va_arg expression.
577 * @param expression the va_arg expression
579 static void print_va_arg(const va_arg_expression_t *expression)
581 fputs("__builtin_va_arg(", out);
582 print_expression_prec(expression->ap, PREC_COMMA + 1);
584 print_type(expression->base.type);
589 * Prints a select expression (. or ->).
591 * @param expression the select expression
593 static void print_select(const select_expression_t *expression)
595 unsigned prec = get_expression_precedence(expression->base.kind);
596 print_expression_prec(expression->compound, prec);
597 if(expression->compound->base.type == NULL ||
598 expression->compound->base.type->kind == TYPE_POINTER) {
603 fputs(expression->symbol->string, out);
607 * Prints a type classify expression.
609 * @param expr the type classify expression
611 static void print_classify_type_expression(
612 const classify_type_expression_t *const expr)
614 fputs("__builtin_classify_type(", out);
615 print_expression_prec(expr->type_expression, PREC_COMMA + 1);
620 * Prints a designator.
622 * @param designator the designator
624 static void print_designator(const designator_t *designator)
626 for ( ; designator != NULL; designator = designator->next) {
627 if (designator->symbol == NULL) {
629 print_expression_prec(designator->array_index, PREC_ACCESS);
633 fputs(designator->symbol->string, out);
639 * Prints an offsetof expression.
641 * @param expression the offset expression
643 static void print_offsetof_expression(const offsetof_expression_t *expression)
645 fputs("__builtin_offsetof", out);
647 print_type(expression->type);
649 print_designator(expression->designator);
654 * Prints a statement expression.
656 * @param expression the statement expression
658 static void print_statement_expression(const statement_expression_t *expression)
661 print_statement(expression->statement);
666 * Prints an expression with parenthesis if needed.
668 * @param expression the expression to print
669 * @param top_prec the precedence of the user of this expression.
671 static void print_expression_prec(const expression_t *expression, unsigned top_prec)
673 unsigned prec = get_expression_precedence(expression->base.kind);
674 if (print_parenthesis && top_prec != PREC_BOTTOM)
678 switch(expression->kind) {
681 fprintf(out, "*invalid expression*");
683 case EXPR_CHAR_CONST:
684 print_char_const(&expression->conste);
687 print_const(&expression->conste);
690 case EXPR_PRETTY_FUNCTION:
691 case EXPR_STRING_LITERAL:
692 print_string_literal(&expression->string);
694 case EXPR_WIDE_STRING_LITERAL:
695 print_wide_string_literal(&expression->wide_string);
697 case EXPR_COMPOUND_LITERAL:
698 print_compound_literal(&expression->compound_literal);
701 print_call_expression(&expression->call);
704 print_binary_expression(&expression->binary);
707 print_reference_expression(&expression->reference);
709 case EXPR_ARRAY_ACCESS:
710 print_array_expression(&expression->array_access);
713 print_unary_expression(&expression->unary);
717 print_typeprop_expression(&expression->typeprop);
719 case EXPR_BUILTIN_SYMBOL:
720 print_builtin_symbol(&expression->builtin_symbol);
722 case EXPR_BUILTIN_CONSTANT_P:
723 print_builtin_constant(&expression->builtin_constant);
725 case EXPR_BUILTIN_PREFETCH:
726 print_builtin_prefetch(&expression->builtin_prefetch);
728 case EXPR_CONDITIONAL:
729 print_conditional(&expression->conditional);
732 print_va_start(&expression->va_starte);
735 print_va_arg(&expression->va_arge);
738 print_select(&expression->select);
740 case EXPR_CLASSIFY_TYPE:
741 print_classify_type_expression(&expression->classify_type);
744 print_offsetof_expression(&expression->offsetofe);
747 print_statement_expression(&expression->statement);
752 fprintf(out, "some expression of type %d", (int) expression->kind);
760 * Print an compound statement.
762 * @param block the compound statement
764 static void print_compound_statement(const compound_statement_t *block)
769 statement_t *statement = block->statements;
770 while(statement != NULL) {
771 if (statement->base.kind == STATEMENT_CASE_LABEL)
774 print_statement(statement);
776 statement = statement->base.next;
784 * Print a return statement.
786 * @param statement the return statement
788 static void print_return_statement(const return_statement_t *statement)
790 fprintf(out, "return ");
791 if(statement->value != NULL)
792 print_expression(statement->value);
797 * Print an expression statement.
799 * @param statement the expression statement
801 static void print_expression_statement(const expression_statement_t *statement)
803 print_expression(statement->expression);
808 * Print a goto statement.
810 * @param statement the goto statement
812 static void print_goto_statement(const goto_statement_t *statement)
814 fprintf(out, "goto ");
815 fputs(statement->label->symbol->string, out);
816 fprintf(stderr, "(%p)", (void*) statement->label);
821 * Print a label statement.
823 * @param statement the label statement
825 static void print_label_statement(const label_statement_t *statement)
827 fprintf(stderr, "(%p)", (void*) statement->label);
828 fprintf(out, "%s:\n", statement->label->symbol->string);
829 if(statement->statement != NULL) {
830 print_statement(statement->statement);
835 * Print an if statement.
837 * @param statement the if statement
839 static void print_if_statement(const if_statement_t *statement)
842 print_expression(statement->condition);
844 if(statement->true_statement != NULL) {
845 print_statement(statement->true_statement);
848 if(statement->false_statement != NULL) {
851 print_statement(statement->false_statement);
856 * Print a switch statement.
858 * @param statement the switch statement
860 static void print_switch_statement(const switch_statement_t *statement)
862 fputs("switch(", out);
863 print_expression(statement->expression);
865 print_statement(statement->body);
869 * Print a case label (including the default label).
871 * @param statement the case label statement
873 static void print_case_label(const case_label_statement_t *statement)
875 if(statement->expression == NULL) {
876 fputs("default:\n", out);
879 print_expression(statement->expression);
880 if (statement->end_range != NULL) {
882 print_expression(statement->end_range);
887 if(statement->statement != NULL) {
888 if (statement->statement->base.kind == STATEMENT_CASE_LABEL) {
892 print_statement(statement->statement);
897 * Print a declaration statement.
899 * @param statement the statement
901 static void print_declaration_statement(
902 const declaration_statement_t *statement)
905 declaration_t *declaration = statement->declarations_begin;
906 for( ; declaration != statement->declarations_end->next;
907 declaration = declaration->next) {
913 print_declaration(declaration);
919 * Print a while statement.
921 * @param statement the statement
923 static void print_while_statement(const while_statement_t *statement)
925 fputs("while(", out);
926 print_expression(statement->condition);
928 print_statement(statement->body);
932 * Print a do-while statement.
934 * @param statement the statement
936 static void print_do_while_statement(const do_while_statement_t *statement)
939 print_statement(statement->body);
941 fputs("while(", out);
942 print_expression(statement->condition);
947 * Print a for statement.
949 * @param statement the statement
951 static void print_for_statement(const for_statement_t *statement)
954 if(statement->scope.declarations != NULL) {
955 assert(statement->initialisation == NULL);
956 print_declaration(statement->scope.declarations);
957 if(statement->scope.declarations->next != NULL) {
958 panic("multiple declarations in for statement not supported yet");
962 if(statement->initialisation) {
963 print_expression(statement->initialisation);
967 if(statement->condition != NULL) {
968 print_expression(statement->condition);
971 if(statement->step != NULL) {
972 print_expression(statement->step);
975 print_statement(statement->body);
979 * Print assembler constraints.
981 * @param constraints the constraints
983 static void print_asm_constraints(asm_constraint_t *constraints)
985 asm_constraint_t *constraint = constraints;
986 for( ; constraint != NULL; constraint = constraint->next) {
987 if(constraint != constraints)
990 if(constraint->symbol) {
991 fprintf(out, "[%s] ", constraint->symbol->string);
993 print_quoted_string(&constraint->constraints, '"');
995 print_expression(constraint->expression);
1001 * Print assembler clobbers.
1003 * @param clobbers the clobbers
1005 static void print_asm_clobbers(asm_clobber_t *clobbers)
1007 asm_clobber_t *clobber = clobbers;
1008 for( ; clobber != NULL; clobber = clobber->next) {
1009 if(clobber != clobbers)
1012 print_quoted_string(&clobber->clobber, '"');
1017 * Print an assembler statement.
1019 * @param statement the statement
1021 static void print_asm_statement(const asm_statement_t *statement)
1024 if(statement->is_volatile) {
1025 fputs("volatile ", out);
1028 print_quoted_string(&statement->asm_text, '"');
1029 if(statement->inputs == NULL && statement->outputs == NULL
1030 && statement->clobbers == NULL)
1031 goto end_of_print_asm_statement;
1034 print_asm_constraints(statement->inputs);
1035 if(statement->outputs == NULL && statement->clobbers == NULL)
1036 goto end_of_print_asm_statement;
1039 print_asm_constraints(statement->outputs);
1040 if(statement->clobbers == NULL)
1041 goto end_of_print_asm_statement;
1044 print_asm_clobbers(statement->clobbers);
1046 end_of_print_asm_statement:
1051 * Print a statement.
1053 * @param statement the statement
1055 void print_statement(const statement_t *statement)
1057 switch(statement->kind) {
1058 case STATEMENT_COMPOUND:
1059 print_compound_statement(&statement->compound);
1061 case STATEMENT_RETURN:
1062 print_return_statement(&statement->returns);
1064 case STATEMENT_EXPRESSION:
1065 print_expression_statement(&statement->expression);
1067 case STATEMENT_LABEL:
1068 print_label_statement(&statement->label);
1070 case STATEMENT_GOTO:
1071 print_goto_statement(&statement->gotos);
1073 case STATEMENT_CONTINUE:
1074 fputs("continue;\n", out);
1076 case STATEMENT_BREAK:
1077 fputs("break;\n", out);
1080 print_if_statement(&statement->ifs);
1082 case STATEMENT_SWITCH:
1083 print_switch_statement(&statement->switchs);
1085 case STATEMENT_CASE_LABEL:
1086 print_case_label(&statement->case_label);
1088 case STATEMENT_DECLARATION:
1089 print_declaration_statement(&statement->declaration);
1091 case STATEMENT_WHILE:
1092 print_while_statement(&statement->whiles);
1094 case STATEMENT_DO_WHILE:
1095 print_do_while_statement(&statement->do_while);
1098 print_for_statement(&statement->fors);
1101 print_asm_statement(&statement->asms);
1103 case STATEMENT_INVALID:
1104 fprintf(out, "*invalid statement*");
1110 * Print a storage class.
1112 * @param storage_class the storage class
1114 static void print_storage_class(unsigned storage_class)
1116 switch((storage_class_tag_t) storage_class) {
1117 case STORAGE_CLASS_ENUM_ENTRY:
1118 case STORAGE_CLASS_NONE:
1120 case STORAGE_CLASS_TYPEDEF: fputs("typedef ", out); break;
1121 case STORAGE_CLASS_EXTERN: fputs("extern ", out); break;
1122 case STORAGE_CLASS_STATIC: fputs("static ", out); break;
1123 case STORAGE_CLASS_AUTO: fputs("auto ", out); break;
1124 case STORAGE_CLASS_REGISTER: fputs("register ", out); break;
1125 case STORAGE_CLASS_THREAD: fputs("__thread", out); break;
1126 case STORAGE_CLASS_THREAD_EXTERN: fputs("extern __thread", out); break;
1127 case STORAGE_CLASS_THREAD_STATIC: fputs("static __thread", out); break;
1132 * Print an initializer.
1134 * @param initializer the initializer
1136 void print_initializer(const initializer_t *initializer)
1138 if(initializer == NULL) {
1139 fputs("{ NIL-INITIALIZER }", out);
1143 switch(initializer->kind) {
1144 case INITIALIZER_VALUE: {
1145 const initializer_value_t *value = &initializer->value;
1146 print_expression(value->value);
1149 case INITIALIZER_LIST: {
1150 assert(initializer->kind == INITIALIZER_LIST);
1152 const initializer_list_t *list = &initializer->list;
1154 for(size_t i = 0 ; i < list->len; ++i) {
1155 const initializer_t *sub_init = list->initializers[i];
1156 print_initializer(list->initializers[i]);
1157 if(i < list->len-1 && sub_init->kind != INITIALIZER_DESIGNATOR) {
1164 case INITIALIZER_STRING:
1165 print_quoted_string(&initializer->string.string, '"');
1167 case INITIALIZER_WIDE_STRING:
1168 print_quoted_wide_string(&initializer->wide_string.string);
1170 case INITIALIZER_DESIGNATOR:
1171 print_designator(initializer->designator.designator);
1176 panic("invalid initializer kind found");
1180 * Print a declaration in the NORMAL namespace.
1182 * @param declaration the declaration
1184 static void print_normal_declaration(const declaration_t *declaration)
1186 print_storage_class(declaration->storage_class);
1187 if(declaration->is_inline) {
1188 if (declaration->modifiers & DM_FORCEINLINE)
1189 fputs("__forceinline ", out);
1191 fputs("inline ", out);
1193 print_type_ext(declaration->type, declaration->symbol,
1194 &declaration->scope);
1196 if(declaration->type->kind == TYPE_FUNCTION) {
1197 if(declaration->init.statement != NULL) {
1199 print_statement(declaration->init.statement);
1202 } else if(declaration->init.initializer != NULL) {
1204 print_initializer(declaration->init.initializer);
1210 * Prints an expression.
1212 * @param expression the expression
1214 void print_expression(const expression_t *expression) {
1215 print_expression_prec(expression, PREC_BOTTOM);
1219 * Print a declaration.
1221 * @param declaration the declaration
1223 void print_declaration(const declaration_t *declaration)
1225 if(declaration->namespc != NAMESPACE_NORMAL &&
1226 declaration->symbol == NULL)
1229 switch(declaration->namespc) {
1230 case NAMESPACE_NORMAL:
1231 print_normal_declaration(declaration);
1233 case NAMESPACE_STRUCT:
1234 fputs("struct ", out);
1235 fputs(declaration->symbol->string, out);
1237 print_compound_definition(declaration);
1240 case NAMESPACE_UNION:
1241 fputs("union ", out);
1242 fputs(declaration->symbol->string, out);
1244 print_compound_definition(declaration);
1247 case NAMESPACE_ENUM:
1248 fputs("enum ", out);
1249 fputs(declaration->symbol->string, out);
1251 print_enum_definition(declaration);
1258 * Print the AST of a translation unit.
1260 * @param unit the translation unit
1262 void print_ast(const translation_unit_t *unit)
1266 declaration_t *declaration = unit->scope.declarations;
1267 for( ; declaration != NULL; declaration = declaration->next) {
1268 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY)
1270 if(declaration->namespc != NAMESPACE_NORMAL &&
1271 declaration->symbol == NULL)
1275 print_declaration(declaration);
1281 * Returns true if a given expression is a compile time
1284 * @param expression the expression to check
1286 bool is_constant_expression(const expression_t *expression)
1288 switch(expression->kind) {
1291 case EXPR_CHAR_CONST:
1292 case EXPR_STRING_LITERAL:
1293 case EXPR_WIDE_STRING_LITERAL:
1295 case EXPR_CLASSIFY_TYPE:
1297 case EXPR_PRETTY_FUNCTION:
1300 case EXPR_BUILTIN_CONSTANT_P:
1303 case EXPR_BUILTIN_SYMBOL:
1304 case EXPR_BUILTIN_PREFETCH:
1309 case EXPR_STATEMENT:
1310 case EXPR_UNARY_POSTFIX_INCREMENT:
1311 case EXPR_UNARY_POSTFIX_DECREMENT:
1312 case EXPR_UNARY_PREFIX_INCREMENT:
1313 case EXPR_UNARY_PREFIX_DECREMENT:
1314 case EXPR_UNARY_BITFIELD_EXTRACT:
1315 case EXPR_UNARY_ASSUME: /* has VOID type */
1316 case EXPR_BINARY_ASSIGN:
1317 case EXPR_BINARY_MUL_ASSIGN:
1318 case EXPR_BINARY_DIV_ASSIGN:
1319 case EXPR_BINARY_MOD_ASSIGN:
1320 case EXPR_BINARY_ADD_ASSIGN:
1321 case EXPR_BINARY_SUB_ASSIGN:
1322 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1323 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1324 case EXPR_BINARY_BITWISE_AND_ASSIGN:
1325 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
1326 case EXPR_BINARY_BITWISE_OR_ASSIGN:
1327 case EXPR_BINARY_COMMA:
1330 case EXPR_UNARY_NEGATE:
1331 case EXPR_UNARY_PLUS:
1332 case EXPR_UNARY_BITWISE_NEGATE:
1333 case EXPR_UNARY_NOT:
1334 case EXPR_UNARY_DEREFERENCE:
1335 case EXPR_UNARY_TAKE_ADDRESS:
1336 case EXPR_UNARY_CAST:
1337 case EXPR_UNARY_CAST_IMPLICIT:
1338 return is_constant_expression(expression->unary.value);
1340 case EXPR_BINARY_ADD:
1341 case EXPR_BINARY_SUB:
1342 case EXPR_BINARY_MUL:
1343 case EXPR_BINARY_DIV:
1344 case EXPR_BINARY_MOD:
1345 case EXPR_BINARY_EQUAL:
1346 case EXPR_BINARY_NOTEQUAL:
1347 case EXPR_BINARY_LESS:
1348 case EXPR_BINARY_LESSEQUAL:
1349 case EXPR_BINARY_GREATER:
1350 case EXPR_BINARY_GREATEREQUAL:
1351 case EXPR_BINARY_BITWISE_AND:
1352 case EXPR_BINARY_BITWISE_OR:
1353 case EXPR_BINARY_BITWISE_XOR:
1354 case EXPR_BINARY_LOGICAL_AND:
1355 case EXPR_BINARY_LOGICAL_OR:
1356 case EXPR_BINARY_SHIFTLEFT:
1357 case EXPR_BINARY_SHIFTRIGHT:
1358 case EXPR_BINARY_BUILTIN_EXPECT:
1359 case EXPR_BINARY_ISGREATER:
1360 case EXPR_BINARY_ISGREATEREQUAL:
1361 case EXPR_BINARY_ISLESS:
1362 case EXPR_BINARY_ISLESSEQUAL:
1363 case EXPR_BINARY_ISLESSGREATER:
1364 case EXPR_BINARY_ISUNORDERED:
1365 return is_constant_expression(expression->binary.left)
1366 && is_constant_expression(expression->binary.right);
1368 case EXPR_COMPOUND_LITERAL:
1369 /* TODO: check initializer if it is constant */
1372 case EXPR_CONDITIONAL:
1373 /* TODO: not correct, we only have to test expressions which are
1374 * evaluated, which means either the true or false part might be not
1376 return is_constant_expression(expression->conditional.condition)
1377 && is_constant_expression(expression->conditional.true_expression)
1378 && is_constant_expression(expression->conditional.false_expression);
1380 case EXPR_ARRAY_ACCESS:
1381 return is_constant_expression(expression->array_access.array_ref)
1382 && is_constant_expression(expression->array_access.index);
1384 case EXPR_REFERENCE: {
1385 declaration_t *declaration = expression->reference.declaration;
1386 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY)
1396 panic("invalid expression found (is constant expression)");
1400 * Initialize the AST construction.
1404 obstack_init(&ast_obstack);
1412 obstack_free(&ast_obstack, NULL);
1416 * Set the output stream for the AST printer.
1418 * @param stream the output stream
1420 void ast_set_output(FILE *stream)
1423 type_set_output(stream);
1427 * Allocate an AST object of the given size.
1429 * @param size the size of the object to allocate
1431 * @return A new allocated object in the AST memeory space.
1433 void *(allocate_ast)(size_t size)
1435 return _allocate_ast(size);