Debian Codegraph @

src/decoder/formatter.c

raw
1 #include "decoder/formatter.h"
2 #include "lexical/parser.h"
3 #include "lexical/token.h"
4 #include "main.h"
5 #include "utils/color.h"
6 #include <assert.h>
7 #include <stdarg.h>
8 #include <stdint.h>
9 #include <stdio.h>
10 #include <string.h>
11
12 FILE *fp;
13
14 static void
15 print_num (obj_t *tk)
16 {
17 if (tk->literal.start != NULL)
18 fwrite (tk->literal.start, 1, tk->literal.len, fp);
19 else
20 fprintf (fp, "0x%x", tk->data);
21 }
22
23 static void
24 print_str (obj_t *tk)
25 {
26 fwrite (token_pairs[tk->type].start, 1, token_pairs[tk->type].len, fp);
27 }
28
29 static void
30 print_identifier (obj_t *tk)
31 {
32 fwrite (tk->literal.start, 1, tk->literal.len, fp);
33 }
34
35 static void
36 print_dec_bracket (obj_t *tk)
37 {
38 fwrite (token_pairs[tk->type].start, 1, token_pairs[tk->type].len, fp);
39 fprintf (fp, "[%u]", tk->data);
40 }
41
42 static void
43 print_hex_bracket (obj_t *tk)
44 {
45 fwrite (token_pairs[tk->type].start, 1, token_pairs[tk->type].len, fp);
46 fprintf (fp, "[0x%x]", tk->data);
47 }
48
49 static void
50 print_paren (obj_t *tk)
51 {
52 fwrite (token_pairs[tk->type].start, 1, token_pairs[tk->type].len, fp);
53 fprintf (fp, "(%u)", tk->data);
54 }
55
56 static const obj_print_t obj_print[] = {
57 [A] = { print_str, BRIGHT_YELLOWCLR },
58 [X] = { print_str, BRIGHT_YELLOWCLR },
59
60 [MEM] = { print_hex_bracket, BRIGHT_YELLOWCLR },
61 [ATTR_LOWARG] = { print_dec_bracket, BRIGHT_BLUECLR },
62 [ATTR_HIGHARG] = { print_dec_bracket, BRIGHT_BLUECLR },
63
64 [ATTR_SYSCALL] = { print_str, BRIGHT_BLUECLR },
65 [ATTR_ARCH] = { print_str, BRIGHT_BLUECLR },
66 [ATTR_LOWPC] = { print_str, BRIGHT_BLUECLR },
67 [ATTR_HIGHPC] = { print_str, BRIGHT_BLUECLR },
68 [ATTR_LEN] = { print_str, BRIGHT_BLUECLR },
69 [IDENTIFIER] = { print_identifier, BRIGHT_CYANCLR },
70
71 [NUMBER] = { print_num, BRIGHT_CYANCLR },
72
73 [KILL_PROC] = { print_str, REDCLR },
74 [KILL] = { print_str, REDCLR },
75 [ALLOW] = { print_str, GREENCLR },
76 [NOTIFY] = { print_str, YELLOWCLR },
77 [LOG] = { print_str, YELLOWCLR },
78 [TRACE] = { print_paren, YELLOWCLR },
79 [TRAP] = { print_paren, YELLOWCLR },
80 [ERRNO] = { print_paren, REDCLR },
81 };
82
83 static void
84 obj_printer (obj_t *obj)
85 {
86 if (color_enable)
87 fprintf (fp, "%s", obj_print[obj->type].color);
88 obj_print[obj->type].handler (obj);
89 if (color_enable)
90 fprintf (fp, "%s", CLR);
91 }
92
93 void
94 extern_obj_printer (FILE *output_fp, obj_t *obj)
95 {
96 fp = output_fp;
97 obj_printer (obj);
98 }
99
100 static inline void
101 print_token_pair (token_type type)
102 {
103 fwrite (token_pairs[type].start, 1, token_pairs[type].len, fp);
104 }
105
106 static void
107 assign_line (statement_t *statement)
108 {
109 assign_line_t *assign_line = &statement->assign_line;
110 obj_t *left = &assign_line->left_var;
111 obj_t *right = &assign_line->right_var;
112
113 obj_printer (left);
114 fputc (' ', fp);
115 if (assign_line->operator == NEGATIVE)
116 fprintf (fp, "= -");
117 else
118 {
119 print_token_pair (assign_line->operator);
120 fputc (' ', fp);
121 }
122
123 obj_printer (right);
124 }
125
126 static inline void
127 print_label (label_t *label, uint16_t pc)
128 {
129 if (label->key.start == NULL)
130 fprintf (fp, DEFAULT_LABEL, pc + label->code_nr + 1);
131 else
132 fwrite (label->key.start, 1, label->key.len, fp);
133 }
134
135 static void
136 print_ja (statement_t *statement)
137 {
138 print_token_pair (GOTO);
139 fputc (' ', fp);
140 print_label (&statement->jump_line.jt, statement->code_nr);
141 }
142
143 static void
144 jump_line (statement_t *statement)
145 {
146 jump_line_t *jump_line = &statement->jump_line;
147 if (!jump_line->if_condition)
148 {
149 print_ja (statement);
150 return;
151 }
152
153 print_token_pair (IF);
154 fputc (' ', fp);
155 if (jump_line->if_bang)
156 print_token_pair (BANG);
157 print_token_pair (LEFT_PAREN);
158 obj_t obj_A = { .type = A, .data = 0 };
159 obj_printer (&obj_A);
160 fputc (' ', fp);
161 print_token_pair (jump_line->comparator);
162 fputc (' ', fp);
163 obj_printer (&jump_line->cmpobj);
164 print_token_pair (RIGHT_PAREN);
165 fputc (' ', fp);
166 print_token_pair (GOTO);
167 fputc (' ', fp);
168 print_label (&jump_line->jt, statement->code_nr);
169
170 if (jump_line->jf.code_nr == 0)
171 return;
172
173 print_token_pair (COMMA);
174 fputc (' ', fp);
175 print_token_pair (ELSE);
176 fputc (' ', fp);
177 print_token_pair (GOTO);
178 fputc (' ', fp);
179 print_label (&jump_line->jf, statement->code_nr);
180 }
181
182 static void
183 return_line (statement_t *statement)
184 {
185 print_token_pair (RETURN);
186 fputc (' ', fp);
187 obj_printer (&statement->return_line.ret_obj);
188 }
189
190 static void
191 print_comment (statement_t *statement)
192 {
193 if (statement->comment == -1)
194 return;
195
196 char *comment_start = statement->line_start + statement->comment;
197 uint16_t comment_len = statement->line_len - statement->comment;
198 if (comment_len == 0)
199 return;
200 if (statement->type != EMPTY_LINE)
201 fputc (' ', fp); // prepend a ' ' if a effective line (return 1 # aa)
202 if (color_enable)
203 fwrite (LIGHTCLR, 1, LITERAL_STRLEN (LIGHTCLR), fp);
204 fwrite (comment_start, 1, comment_len, fp);
205 if (color_enable)
206 fwrite (CLR, 1, LITERAL_STRLEN (CLR), fp);
207 }
208
209 void
210 print_as_comment (FILE *output_fp, const char *comment_fmt, ...)
211 {
212 fp = output_fp;
213
214 va_list args;
215 va_start (args, comment_fmt);
216
217 char buf[0x400];
218 buf[0] = '#';
219 statement_t statement
220 = { .type = EMPTY_LINE, .line_start = buf, .comment = 0 };
221 statement.line_len = vsnprintf (buf + 1, 0x3ff, comment_fmt, args) + 1;
222
223 print_comment (&statement);
224 fputc ('\n', fp);
225
226 va_end (args);
227 }
228
229 void
230 print_statement (FILE *output_fp, statement_t *statement)
231 {
232 fp = output_fp;
233
234 switch (statement->type)
235 {
236 case ASSIGN_LINE:
237 assign_line (statement);
238 break;
239 case JUMP_LINE:
240 jump_line (statement);
241 break;
242 case RETURN_LINE:
243 return_line (statement);
244 break;
245 case EMPTY_LINE:
246 break;
247 case ERROR_LINE:
248 case EOF_LINE:
249 assert (0);
250 }
251 print_comment (statement);
252 fputc ('\n', fp);
253 }