Skip to content

Commit 5b03c11

Browse files
committed
refactored print based debug
1 parent e95dcf6 commit 5b03c11

File tree

6 files changed

+182
-161
lines changed

6 files changed

+182
-161
lines changed

ast.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -850,6 +850,8 @@ static const char *astnames[] = {
850850
"asm_elseif",
851851

852852
"asm_endif",
853+
"expect",
854+
"print_debug",
853855
};
854856

855857
//

ast.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,8 @@ enum astkind {
240240
AST_ASM_ELSEIF = 172,
241241

242242
AST_ASM_ENDIF = 173,
243-
244243
AST_EXPECT = 174,
244+
AST_PRINTDEBUG = 175,
245245
};
246246

247247
/* forward reference */

frontends/printdebug.c

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,168 @@
88
#include <ctype.h>
99
#include "spinc.h"
1010

11+
extern AST *genPrintf(AST *);
12+
13+
#define DBG_BITS_DELAY 0x1000
14+
15+
static struct s_dbgfmt {
16+
const char *name;
17+
const char *cfmt;
18+
int bits;
19+
} dbgfmt[] = {
20+
{ "uchar#", "%c", 0 },
21+
{ "zstr", "%s", 0 },
22+
{ "udec", "%u", 0 },
23+
{ "fdec", "%g", 0 },
24+
{ "udec_byte", "%03u", 8 },
25+
{ "udec_word", "%05u", 16 },
26+
{ "udec_long", "%09u", 0 },
27+
{ "sdec", "%d", 0 },
28+
{ "sdec_byte", "%3d", -8 },
29+
{ "sdec_word", "%5d", -16 },
30+
{ "sdec_long", "%9d", 0 },
31+
{ "uhex", "$%x", 0 },
32+
{ "uhex_byte", "$%02x", 8 },
33+
{ "uhex_word", "$%04x", 16 },
34+
{ "uhex_long", "$%08x", 0 },
35+
{ "dly", "%.0s", DBG_BITS_DELAY },
36+
{ 0, 0, 0 }
37+
};
38+
39+
static AST *GetFormatForDebug(struct flexbuf *fb, const char *itemname_orig, AST *args, int needcomma)
40+
{
41+
char itemname[128];
42+
struct s_dbgfmt *ptr = &dbgfmt[0];
43+
AST *arg;
44+
AST *outlist = NULL;
45+
int len;
46+
int output_name = 1;
47+
const char *idname;
48+
int bits = 0;
49+
50+
len = strlen(itemname_orig);
51+
if (len > sizeof(itemname)-1) {
52+
WARNING(args, "Unhandled debug format %s", itemname_orig);
53+
return NULL;
54+
}
55+
strcpy(itemname, itemname_orig);
56+
if (itemname[len-1] == '_') {
57+
output_name = 0;
58+
itemname[len-1] = 0;
59+
}
60+
while (ptr && ptr->name) {
61+
if (!strcasecmp(ptr->name, itemname)) {
62+
break;
63+
}
64+
ptr++;
65+
}
66+
if (!ptr->name) {
67+
WARNING(args, "Unhandled debug format %s", itemname_orig);
68+
return NULL;
69+
}
70+
bits = ptr->bits;
71+
if (bits == DBG_BITS_DELAY) {
72+
output_name = 0;
73+
}
74+
// now output all the arguments
75+
while (args) {
76+
arg = args->left;
77+
args = args->right;
78+
79+
if (needcomma) {
80+
flexbuf_addstr(fb, ", ");
81+
}
82+
if (output_name) {
83+
idname = GetExprString(arg);
84+
flexbuf_printf(fb, "%s = %s", idname, ptr->cfmt);
85+
} else {
86+
flexbuf_addstr(fb, ptr->cfmt);
87+
}
88+
needcomma = 1;
89+
if (bits == DBG_BITS_DELAY) {
90+
// cause a delay
91+
arg = NewAST(AST_FUNCCALL, AstIdentifier("_waitms"),
92+
NewAST(AST_EXPRLIST, arg, NULL));
93+
} else if (bits & 0x1f) {
94+
if (bits < 0) {
95+
arg = AstOperator(K_SIGNEXTEND, arg, AstInteger(-bits));
96+
} else {
97+
arg = AstOperator(K_ZEROEXTEND, arg, AstInteger(bits));
98+
}
99+
}
100+
arg = NewAST(AST_EXPRLIST, arg, NULL);
101+
outlist = AddToList(outlist, arg);
102+
}
103+
return outlist;
104+
}
105+
106+
AST *
107+
BuildDebugList(AST *exprlist)
108+
{
109+
if (!gl_debug) {
110+
return NULL;
111+
}
112+
if (gl_brkdebug) {
113+
AST *debug = NewAST(AST_BRKDEBUG, exprlist, NULL);
114+
return debug;
115+
}
116+
return NewAST(AST_PRINTDEBUG, exprlist, NULL);
117+
}
118+
119+
AST *
120+
CreatePrintfDebug(AST *exprlist)
121+
{
122+
AST *outlist = NULL;
123+
AST *item;
124+
AST *printf_call;
125+
AST *sub;
126+
struct flexbuf fb;
127+
const char *fmtstr;
128+
int needcomma = 0;
129+
130+
flexbuf_init(&fb, 1024);
131+
132+
if (exprlist && exprlist->left && exprlist->left->kind == AST_LABEL) {
133+
flexbuf_addstr(&fb, "Cog%d ");
134+
outlist = NewAST(AST_FUNCCALL,
135+
AstIdentifier("cogid"),
136+
NULL);
137+
outlist = NewAST(AST_EXPRLIST, outlist, NULL);
138+
exprlist = exprlist->right;
139+
} else {
140+
outlist = NULL;
141+
}
142+
while (exprlist && exprlist->kind == AST_EXPRLIST) {
143+
item = exprlist->left;
144+
exprlist = exprlist->right;
145+
if (!item) continue;
146+
if (item->kind == AST_STRING) {
147+
sub = NewAST(AST_STRINGPTR, item, NULL);
148+
sub = NewAST(AST_EXPRLIST, sub, NULL);
149+
outlist = AddToList(outlist, sub);
150+
flexbuf_addstr(&fb, "%s");
151+
needcomma=0;
152+
} else if (item->kind == AST_FUNCCALL) {
153+
const char *name = GetUserIdentifierName(item->left);
154+
AST *newarg;
155+
156+
item = item->right; /* the parameter list */
157+
newarg = GetFormatForDebug(&fb, name, item, needcomma);
158+
if (newarg) {
159+
needcomma = 1;
160+
outlist = AddToList(outlist, newarg);
161+
}
162+
}
163+
}
164+
flexbuf_addstr(&fb, "\r\n");
165+
flexbuf_addchar(&fb, 0);
166+
fmtstr = flexbuf_get(&fb);
167+
flexbuf_delete(&fb);
168+
169+
sub = AstStringPtr(fmtstr);
170+
sub = NewAST(AST_EXPRLIST, sub, NULL);
171+
outlist = AddToList(sub, outlist);
172+
printf_call = NewAST(AST_PRINT, NULL, NULL);
173+
printf_call = NewAST(AST_FUNCCALL, printf_call, outlist);
174+
return genPrintf(printf_call);
175+
}

frontends/spin/spin.y

Lines changed: 0 additions & 159 deletions
Original file line numberDiff line numberDiff line change
@@ -143,165 +143,6 @@ FixupList(AST *list)
143143
return origlist;
144144
}
145145

146-
#define DBG_BITS_DELAY 0x1000
147-
148-
static struct s_dbgfmt {
149-
const char *name;
150-
const char *cfmt;
151-
int bits;
152-
} dbgfmt[] = {
153-
{ "uchar#", "%c", 0 },
154-
{ "zstr", "%s", 0 },
155-
{ "udec", "%u", 0 },
156-
{ "fdec", "%g", 0 },
157-
{ "udec_byte", "%03u", 8 },
158-
{ "udec_word", "%05u", 16 },
159-
{ "udec_long", "%09u", 0 },
160-
{ "sdec", "%d", 0 },
161-
{ "sdec_byte", "%3d", -8 },
162-
{ "sdec_word", "%5d", -16 },
163-
{ "sdec_long", "%9d", 0 },
164-
{ "uhex", "$%x", 0 },
165-
{ "uhex_byte", "$%02x", 8 },
166-
{ "uhex_word", "$%04x", 16 },
167-
{ "uhex_long", "$%08x", 0 },
168-
{ "dly", "%.0s", DBG_BITS_DELAY },
169-
{ 0, 0, 0 }
170-
};
171-
172-
static AST *GetFormatForDebug(struct flexbuf *fb, const char *itemname_orig, AST *args, int needcomma)
173-
{
174-
char itemname[128];
175-
struct s_dbgfmt *ptr = &dbgfmt[0];
176-
AST *arg;
177-
AST *outlist = NULL;
178-
int len;
179-
int output_name = 1;
180-
const char *idname;
181-
int bits = 0;
182-
183-
len = strlen(itemname_orig);
184-
if (len > sizeof(itemname)-1) {
185-
WARNING(args, "Unhandled debug format %s", itemname_orig);
186-
return NULL;
187-
}
188-
strcpy(itemname, itemname_orig);
189-
if (itemname[len-1] == '_') {
190-
output_name = 0;
191-
itemname[len-1] = 0;
192-
}
193-
while (ptr && ptr->name) {
194-
if (!strcasecmp(ptr->name, itemname)) {
195-
break;
196-
}
197-
ptr++;
198-
}
199-
if (!ptr->name) {
200-
WARNING(args, "Unhandled debug format %s", itemname_orig);
201-
return NULL;
202-
}
203-
bits = ptr->bits;
204-
if (bits == DBG_BITS_DELAY) {
205-
output_name = 0;
206-
}
207-
// now output all the arguments
208-
while (args) {
209-
arg = args->left;
210-
args = args->right;
211-
212-
if (needcomma) {
213-
flexbuf_addstr(fb, ", ");
214-
}
215-
if (output_name) {
216-
idname = GetExprString(arg);
217-
flexbuf_printf(fb, "%s = %s", idname, ptr->cfmt);
218-
} else {
219-
flexbuf_addstr(fb, ptr->cfmt);
220-
}
221-
needcomma = 1;
222-
if (bits == DBG_BITS_DELAY) {
223-
// cause a delay
224-
arg = NewAST(AST_FUNCCALL, AstIdentifier("_waitms"),
225-
NewAST(AST_EXPRLIST, arg, NULL));
226-
} else if (bits & 0x1f) {
227-
if (bits < 0) {
228-
arg = AstOperator(K_SIGNEXTEND, arg, AstInteger(-bits));
229-
} else {
230-
arg = AstOperator(K_ZEROEXTEND, arg, AstInteger(bits));
231-
}
232-
}
233-
arg = NewAST(AST_EXPRLIST, arg, NULL);
234-
outlist = AddToList(outlist, arg);
235-
}
236-
return outlist;
237-
}
238-
239-
extern AST *genPrintf(AST *);
240-
241-
AST *
242-
BuildDebugList(AST *exprlist)
243-
{
244-
AST *outlist = NULL;
245-
AST *item;
246-
AST *printf_call;
247-
AST *sub;
248-
struct flexbuf fb;
249-
const char *fmtstr;
250-
int needcomma = 0;
251-
252-
if (!gl_debug) {
253-
return NULL;
254-
}
255-
if (gl_brkdebug) {
256-
AST *debug = NewAST(AST_BRKDEBUG, exprlist, NULL);
257-
return debug;
258-
}
259-
flexbuf_init(&fb, 1024);
260-
261-
if (exprlist && exprlist->left && exprlist->left->kind == AST_LABEL) {
262-
flexbuf_addstr(&fb, "Cog%d ");
263-
outlist = NewAST(AST_FUNCCALL,
264-
AstIdentifier("cogid"),
265-
NULL);
266-
outlist = NewAST(AST_EXPRLIST, outlist, NULL);
267-
exprlist = exprlist->right;
268-
} else {
269-
outlist = NULL;
270-
}
271-
while (exprlist && exprlist->kind == AST_EXPRLIST) {
272-
item = exprlist->left;
273-
exprlist = exprlist->right;
274-
if (!item) continue;
275-
if (item->kind == AST_STRING) {
276-
sub = NewAST(AST_STRINGPTR, item, NULL);
277-
sub = NewAST(AST_EXPRLIST, sub, NULL);
278-
outlist = AddToList(outlist, sub);
279-
flexbuf_addstr(&fb, "%s");
280-
needcomma=0;
281-
} else if (item->kind == AST_FUNCCALL) {
282-
const char *name = GetUserIdentifierName(item->left);
283-
AST *newarg;
284-
285-
item = item->right; /* the parameter list */
286-
newarg = GetFormatForDebug(&fb, name, item, needcomma);
287-
if (newarg) {
288-
needcomma = 1;
289-
outlist = AddToList(outlist, newarg);
290-
}
291-
}
292-
}
293-
flexbuf_addstr(&fb, "\r\n");
294-
flexbuf_addchar(&fb, 0);
295-
fmtstr = flexbuf_get(&fb);
296-
flexbuf_delete(&fb);
297-
298-
sub = AstStringPtr(fmtstr);
299-
sub = NewAST(AST_EXPRLIST, sub, NULL);
300-
outlist = AddToList(sub, outlist);
301-
printf_call = NewAST(AST_PRINT, NULL, NULL);
302-
printf_call = NewAST(AST_FUNCCALL, printf_call, outlist);
303-
return genPrintf(printf_call);
304-
}
305146

306147
#define YYERROR_VERBOSE 1
307148
%}

frontends/spin/spinlang.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -910,7 +910,13 @@ doSpinTransform(AST **astptr, int level, AST *parent)
910910
}
911911
}
912912
break;
913-
}
913+
}
914+
case AST_PRINTDEBUG:
915+
{
916+
AST *debugprint = CreatePrintfDebug(ast->left);
917+
*astptr = debugprint;
918+
break;
919+
}
914920
case AST_OPERATOR:
915921
if (level == 1) {
916922
AST *lhsast;

spinc.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,11 @@ int NumExprItemsOnStack(AST *param);
6262
// find the "main" function in a module, if any
6363
Function *GetMainFunction(Module *P);
6464

65+
// construct an AST corresponding to a DEBUG() statement
66+
// see (frontends/printdebug.c)
67+
AST *BuildDebugList(AST *exprlist);
68+
69+
// convert AST debug list into printf
70+
AST *CreatePrintfDebug(AST *exprlist);
71+
6572
#endif

0 commit comments

Comments
 (0)