-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtinyopt_dot_cfg.c
More file actions
186 lines (174 loc) · 7.37 KB
/
tinyopt_dot_cfg.c
File metadata and controls
186 lines (174 loc) · 7.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*⠀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
*⠀⣿⢫⣶⢾⣰⣿⣑⣶⣕⡫⢙⣭⣖⣷⣶⣹⣿⣻⣟⠉⣿⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
*⠀⣿⣮⡟⣼⣶⣶⠶⠶⠶⢶⣶⢶⠶⠶⠶⢶⣶⣶⡶⢰⢿⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
*⠀⣿⡼⠱⣫⣶⣾⠟⠿⣿⠨⣆⣿⢰⣿⠿⣿⢶⣬⠃⣷⣾⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
*⠀⣿⢿⣾⢿⣷⣌⣀⣈⣿⠘⣧⢸⢸⡿⡟⢁⣋⡩⣷⣬⣾⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
*⠀⣿⠹⣑⡲⣶⣦⡻⠿⣿⢠⣍⢴⢸⣿⣰⣿⡿⢃⣜⢗⣸⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
*⠀⣿⢸⢿⡿⠋⠙⣿⠹⣿⢸⠏⠾⢸⣯⢣⣾⣟⣻⣭⡛⣿⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣠⣤⣤⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
*⠀⣿⣞⢾⣧⣻⣾⣋⣾⣾⠀⠸⢓⢸⡟⣆⢿⢼⣗⠉⡽⣸⡇⠀⠀⣠⡦⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⠂⠁⠀⠉⠙⢿⣷⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡀⠀⠀
*⠀⣿⢛⣥⣬⣛⡿⣦⢫⣿⢸⣇⣿⢸⡿⣳⣥⣜⣿⢿⡾⢿⡇⠀⠀⢈⡀⠀⠀⢀⡀⠀⢀⣀⠀⠀⠀⣀⡀⠀⠀⠀⢀⡀⠀⠀⣴⠃⠀⠀⠀⠀⠀⠀⢻⣿⠀⠀⣀⡀⠀⢀⣀⠀⠀⢀⣾⣇⣀⡀
*⠀⣿⣿⢋⣽⣶⣯⡈⢼⣿⠸⣦⢸⢸⡟⠿⢍⣿⣝⠸⣸⢿⡇⠀⠐⢿⡇⠀⠐⢿⡧⠐⠙⢻⣷⠀⠈⢻⣿⠀⠀⠀⢻⣷⠀⢸⣿⠀⠀⠀⠀⠀⠀⠀⠈⣿⠂⠘⣿⡧⠊⠙⢿⣷⠀⠉⣿⡏⠉⠀
*⠀⣿⡗⣼⣟⣉⢻⡹⢸⣿⢸⡌⣸⠘⡿⠿⣿⣟⣻⡇⣱⣿⡇⠀⠀⢸⡇⠀⠀⢸⡇⠀⠀⢸⣿⠀⠀⢸⣿⠀⠀⠀⢸⣿⠀⢸⣿⡀⠀⠀⠀⠀⠀⠀⢀⡿⠀⠀⣿⡇⠀⠀⠈⣿⠀⠀⣿⡇⠀⠀
*⠀⣿⣷⢩⣿⣵⡿⣣⡾⣣⣟⣁⣙⣧⡻⢇⡽⢟⣉⠪⢭⣿⡇⠀⠀⢸⡇⠀⠀⢸⡇⠀⠀⢸⣿⠀⠀⢸⣿⡀⠀⠀⣸⡇⠀⠈⢿⣷⣄⠀⠀⠀⠀⢀⡼⠁⠀⠀⣿⡇⠀⠀⢀⠟⠀⠀⣿⡇⠀⠀
*⠀⠿⠶⠾⠿⠶⠿⠿⠶⠶⠶⠶⠶⠶⠶⠾⠿⠿⠿⠿⠾⠿⠇⠀⠀⠸⠿⠂⠀⠸⠿⠂⠀⠘⠿⠓⠀⠈⠻⡿⠒⠁⢸⣷⠀⠀⠀⠙⠻⠿⠶⠶⠖⠉⠀⠀⠀⠀⣿⡿⠿⠖⠀⠀⠀⠀⠻⠿⠖⠀
*⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⡎⠀⠀⠀⢸⠏⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
*⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⠻⠿⠶⠂⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠛⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
* TinyOpt
* Copyright (c) 2025 leozamboni
*
* this program is free software: you can redistribute it and/or modify
* it under the terms of the gnu general public license as published by
* the free software foundation, either version 3 of the license, or
* (at your option) any later version.
*
* this program is distributed in the hope that it will be useful,
* but without any warranty; without even the implied warranty of
* merchantability or fitness for a particular purpose. see the
* gnu general public license for more details.
*
* you should have received a copy of the gnu general public license
* along with this program. if not, see <http://www.gnu.org/licenses/>.
*/
#include "tinyopt_dot_cfg.h"
#include "tinyopt_cfg.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static void
emit_node_label (int id, const char *label)
{
printf (" n%d [label=\"%s\"];\n", id, label);
}
static void
emit_edge (int from, int to, const char *label)
{
if (label && *label)
{
printf (" n%d -> n%d [label=\"%s\"];\n", from, to, label);
}
else
{
printf (" n%d -> n%d;\n", from, to);
}
}
static void
append_stmt_list_labels (TinyOptASTNode_t *node, char *buf, size_t bufsz)
{
for (TinyOptASTNode_t *cur = node; cur; cur = cur->next)
{
if (strlen (buf) > bufsz - 8)
break;
switch (cur->type)
{
case NODE_DECLARATION:
strncat (buf, "decl; ", bufsz - strlen (buf) - 1);
break;
case NODE_ASSIGNMENT:
strncat (buf, "assign; ", bufsz - strlen (buf) - 1);
break;
case NODE_RETURN:
strncat (buf, "return; ", bufsz - strlen (buf) - 1);
break;
case NODE_BREAK:
strncat (buf, "break; ", bufsz - strlen (buf) - 1);
break;
case NODE_CONTINUE:
strncat (buf, "continue; ", bufsz - strlen (buf) - 1);
break;
default:
strncat (buf, "stmt; ", bufsz - strlen (buf) - 1);
break;
}
}
}
static const char *
simple_stmt_label (TinyOptASTNode_t *node, char *tmp, size_t tmpsz)
{
if (!node)
return "";
switch (node->type)
{
case NODE_DECLARATION:
{
TinyOptDeclarationNode_t *d = (TinyOptDeclarationNode_t *)node;
snprintf (tmp, tmpsz, "decl %s", d->name ? d->name : "");
return tmp;
}
case NODE_ASSIGNMENT:
{
TinyOptAssignmentNode_t *a = (TinyOptAssignmentNode_t *)node;
snprintf (tmp, tmpsz, "assign %s", a->variable ? a->variable : "");
return tmp;
}
case NODE_FUNCTION_CALL:
{
TinyOptFunctionCallNode_t *c = (TinyOptFunctionCallNode_t *)node;
snprintf (tmp, tmpsz, "call %s", c->name ? c->name : "");
return tmp;
}
case NODE_RETURN:
return "return";
case NODE_BREAK:
return "break";
case NODE_CONTINUE:
return "continue";
default:
return "stmt";
}
}
void
print_cfg_dot (TinyOptASTNode_t *ast)
{
printf ("digraph CFG {\n");
printf (" node [shape=box];\n");
if (!ast)
{
printf ("}\n");
return;
}
TinyOptCFG_t *cfg = tinyopt_cfg_build (ast);
if (!cfg)
{
printf ("}\n");
return;
}
/* Nós */
for (TinyOptCFGNode_t *n = cfg->nodes; n; n = n->next_in_cfg)
{
char tmp[128];
const char *label;
if (n->ast)
{
/* Reaproveita a lógica de rotulagem simples já usada
* anteriormente, agora baseada no nó da AST associado
* ao nó do CFG. */
label = simple_stmt_label (n->ast, tmp, sizeof (tmp));
}
else
{
snprintf (tmp, sizeof (tmp), "n%d", n->id);
label = tmp;
}
emit_node_label (n->id, label);
}
/* Arestas */
for (TinyOptCFGNode_t *n = cfg->nodes; n; n = n->next_in_cfg)
{
if (n->succ_true)
{
const char *label = NULL;
if (n->succ_false)
label = "true";
emit_edge (n->id, n->succ_true->id, label);
}
if (n->succ_false)
{
const char *label = NULL;
if (n->succ_true)
label = "false";
emit_edge (n->id, n->succ_false->id, label);
}
}
printf ("}\n");
tinyopt_cfg_free (cfg);
}