scc

Simple C99 Compiler
Log | Files | Refs | README | LICENSE

commit 4698e1692871826efbc031b0d2c9991ea8ee6be5
parent 6474d05ae74cc9a59f67bdc84ae4428983f09ffe
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Thu, 12 Feb 2015 12:20:58 +0100

Add prtree() to cc2

This function prints a tree, that is really useful for debugging.

Diffstat:
cc2/cc2.h | 2++
cc2/parser.c | 26++++++++++++++++++++++++++
2 files changed, 28 insertions(+), 0 deletions(-)

diff --git a/cc2/cc2.h b/cc2/cc2.h @@ -5,6 +5,7 @@ typedef struct node Node; typedef struct { short size; uint8_t align; + char letter; bool sign : 1; bool c_int : 1; } Type; @@ -116,3 +117,4 @@ extern void genstack(Symbol *fun); extern void apply(Node *list[], void (*fun)(Node *)); extern Symbol *parse(void); extern void code(char op, ...); +extern void prtree(Node *np); diff --git a/cc2/parser.c b/cc2/parser.c @@ -26,6 +26,7 @@ static Node nodepool[NR_NODEPOOL], *newp; Type l_int8 = { + .letter = L_INT8, .size = 1, .align = 2, .sign = 1, @@ -33,6 +34,7 @@ Type l_int8 = { }; Type l_int16 = { + .letter = L_INT16, .size = 2, .align = 2, .sign = 1, @@ -40,6 +42,7 @@ Type l_int16 = { }; Type l_int32 = { + .letter = L_INT32, .size = 4, .align = 4, .sign = 1, @@ -47,6 +50,7 @@ Type l_int32 = { }; Type l_int64 = { + .letter = L_INT64, .size = 8, .align = 8, .sign = 1, @@ -54,29 +58,51 @@ Type l_int64 = { }; Type l_uint8 = { + .letter = L_UINT8, .size = 1, .align = 2, .c_int = 1, }; Type l_uint16 = { + .letter = L_UINT16, .size = 2, .align = 2, .c_int = 1, }; Type l_uint32 = { + .letter = L_UINT32, .size = 4, .align = 4, .c_int = 1, }; Type l_uint64 = { + .letter = L_UINT64, .size = 8, .align = 8, .c_int = 1, }; + +static void +prnode(Node *np) +{ + if (np->left) + prnode(np->left); + if (np->right) + prnode(np->right); + fprintf(stderr, "\t%c%c", np->op, np->type->letter); +} + +void +prtree(Node *np) +{ + prnode(np); + putc('\n', stderr); +} + void apply(Node *list[], void (*fun)(Node *)) {