Skip to content

Commit 334bdbe

Browse files
author
Reini Urban
committed
p2: refactor P->flags & DEBUG_VERBOSE... to macros
1 parent 0444ba6 commit 334bdbe

File tree

1 file changed

+50
-49
lines changed

1 file changed

+50
-49
lines changed

front/p2.c

Lines changed: 50 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -88,47 +88,51 @@ static void p2_cmd_version(Potion *P) {
8888
printf(p2_banner, POTION_JIT);
8989
}
9090

91+
#define dumpv_str(str) \
92+
if (P->flags & DEBUG_VERBOSE) \
93+
fprintf(stderr, str)
94+
#define dumpv_p(p) \
95+
if (P->flags & DEBUG_VERBOSE) \
96+
potion_p(P, p)
97+
#define dumpvi_p(p) \
98+
if (P->flags & (DEBUG_INSPECT|DEBUG_VERBOSE)) \
99+
potion_p(P, p)
100+
91101
static PN p2_cmd_exec(Potion *P, PN buf, char *filename, exec_mode_t exec) {
92102
PN code = p2_source_load(P, PN_NIL, buf);
93103
if (PN_IS_PROTO(code)) {
94104
if (P->flags & DEBUG_VERBOSE)
95-
printf("\n\n-- loaded --\n");
105+
fprintf(stderr, "\n\n-- loaded --\n");
96106
} else {
97107
code = p2_parse(P, buf, filename);
98108
if (!code || PN_TYPE(code) == PN_TERROR) {
99109
potion_p(P, code);
100110
return code;
101111
}
102-
if (P->flags & DEBUG_VERBOSE) {
103-
printf("\n-- parsed --\n");
104-
potion_p(P, code);
105-
}
112+
dumpv_str("\n-- parsed --\n");
113+
dumpv_p(code);
106114
code = potion_send(code, PN_compile, potion_str(P, filename), PN_NIL);
107-
if (P->flags & DEBUG_VERBOSE)
108-
printf("\n-- compiled --\n");
115+
dumpv_str("\n-- compiled --\n");
109116
}
110-
if (P->flags & DEBUG_VERBOSE)
111-
potion_p(P, code);
117+
dumpv_p(code);
112118
if (exec == EXEC_VM) {
113119
code = potion_vm(P, code, P->lobby, PN_NIL, 0, NULL);
114120
if (P->flags & DEBUG_VERBOSE)
115-
printf("\n-- vm returned %p (fixed=%ld, actual=%ld, reserved=%ld) --\n", (void *)code,
116-
PN_INT(potion_gc_fixed(P, 0, 0)), PN_INT(potion_gc_actual(P, 0, 0)),
117-
PN_INT(potion_gc_reserved(P, 0, 0)));
118-
if (P->flags & (DEBUG_INSPECT|DEBUG_VERBOSE))
119-
potion_p(P, code);
121+
fprintf(stderr, "\n-- vm returned %p (fixed=%ld, actual=%ld, reserved=%ld) --\n", (void *)code,
122+
PN_INT(potion_gc_fixed(P, 0, 0)), PN_INT(potion_gc_actual(P, 0, 0)),
123+
PN_INT(potion_gc_reserved(P, 0, 0)));
124+
dumpvi_p(code);
120125
} else if (exec == EXEC_JIT) {
121126
#ifdef POTION_JIT_TARGET
122127
PN val;
123128
PN cl = potion_closure_new(P, (PN_F)potion_jit_proto(P, code), PN_NIL, 1);
124129
PN_CLOSURE(cl)->data[0] = code;
125130
val = PN_PROTO(code)->jit(P, cl, P->lobby);
126131
if (P->flags & DEBUG_VERBOSE)
127-
printf("\n-- jit returned %p (fixed=%ld, actual=%ld, reserved=%ld) --\n", PN_PROTO(code)->jit,
128-
PN_INT(potion_gc_fixed(P, 0, 0)), PN_INT(potion_gc_actual(P, 0, 0)),
129-
PN_INT(potion_gc_reserved(P, 0, 0)));
130-
if (P->flags & (DEBUG_INSPECT|DEBUG_VERBOSE))
131-
potion_p(P, val);
132+
fprintf(stderr, "\n-- jit returned %p (fixed=%ld, actual=%ld, reserved=%ld) --\n", PN_PROTO(code)->jit,
133+
PN_INT(potion_gc_fixed(P, 0, 0)), PN_INT(potion_gc_actual(P, 0, 0)),
134+
PN_INT(potion_gc_reserved(P, 0, 0)));
135+
dumpvi_p(val);
132136
#else
133137
fprintf(stderr, "** p2 built without JIT support\n");
134138
#endif
@@ -188,39 +192,46 @@ static void p2_cmd_compile(Potion *P, char *filename, exec_mode_t exec) {
188192
potion_fatal("--compile-c not yet implemented\n");
189193
}
190194
else if (exec == EXEC_COMPILE_NATIVE) {
191-
//code = potion_send(P, code, "dumpbin");
195+
//code = potion_send(P, code, "dumpexec");
192196
potion_fatal("--compile-native not yet implemented\n");
193197
}
198+
194199
if (fwrite(PN_STR_PTR(code), 1, PN_STR_LEN(code), plc) == PN_STR_LEN(code)) {
195200
printf("** compiled code saved to %s\n", plcpath);
196201
fclose(plc);
202+
203+
if (exec == EXEC_COMPILE)
204+
printf("** run it with: potion %s\n", plcpath);
205+
else if (exec == EXEC_COMPILE_C)
206+
printf("** compile it with: %s %s %s\n", POTION_CC, POTION_CFLAGS, plcpath);
207+
else if (exec == EXEC_COMPILE_NATIVE)
208+
printf("** run it with: ./%s\n", plcpath);
209+
197210
} else {
198211
fprintf(stderr, "** could not write all compiled code.");
199212
}
200-
if (exec == EXEC_COMPILE)
201-
printf("** run it with: p2 %s\n", plcpath);
202213
}
203214

204215
#if defined(DEBUG)
205216
if (P->flags & DEBUG_GC) { // GC sanity check
206-
printf("\n-- gc check --\n");
217+
fprintf(stderr, "\n-- gc check --\n");
207218
void *scanptr = (void *)((char *)P->mem->old_lo + (sizeof(PN) * 2));
208219
while ((PN)scanptr < (PN)P->mem->old_cur) {
209-
printf("%p.vt = %x (%u)\n",
210-
scanptr, ((struct PNObject *)scanptr)->vt,
211-
potion_type_size(P, scanptr));
220+
fprintf(stderr, "%p.vt = %x (%u)\n",
221+
scanptr, ((struct PNObject *)scanptr)->vt,
222+
potion_type_size(P, scanptr));
212223
if (((struct PNFwd *)scanptr)->fwd != POTION_FWD
213224
&& ((struct PNFwd *)scanptr)->fwd != POTION_COPIED) {
214225
if ((signed long)(((struct PNObject *)scanptr)->vt) < 0
215226
|| ((struct PNObject *)scanptr)->vt > PN_TUSER) {
216-
printf("wrong type for allocated object: %p.vt = %x\n",
217-
scanptr, ((struct PNObject *)scanptr)->vt);
227+
fprintf(stderr, "** wrong type for allocated object: %p.vt = %x\n",
228+
scanptr, ((struct PNObject *)scanptr)->vt);
218229
break;
219230
}
220231
}
221232
scanptr = (void *)((char *)scanptr + potion_type_size(P, scanptr));
222233
if ((PN)scanptr > (PN)P->mem->old_cur) {
223-
printf("allocated object goes beyond GC pointer\n");
234+
fprintf(stderr, "** allocated object goes beyond GC pointer\n");
224235
break;
225236
}
226237
}
@@ -282,23 +293,13 @@ int main(int argc, char *argv[]) {
282293
printf(" G GC\n");
283294
goto END;
284295
}
285-
if (strchr(&argv[i][2], 'i')) {
286-
P->flags |= DEBUG_INSPECT;
287-
}
288-
if (strchr(&argv[i][2], 'v')) {
289-
P->flags |= DEBUG_VERBOSE;
290-
}
291-
if (strchr(&argv[i][2], 't')) {
292-
P->flags |= DEBUG_TRACE;
293-
}
294-
if (strchr(&argv[i][2], 'p'))
295-
P->flags |= DEBUG_PARSE;
296-
if (strchr(&argv[i][2], 'P'))
297-
P->flags |= (DEBUG_PARSE | DEBUG_PARSE_VERBOSE);
298-
if (strchr(&argv[i][2], 'J'))
299-
P->flags |= DEBUG_JIT;
300-
if (strchr(&argv[i][2], 'G'))
301-
P->flags |= DEBUG_GC;
296+
if (strchr(&argv[i][2], 'i')) P->flags |= DEBUG_INSPECT;
297+
if (strchr(&argv[i][2], 'v')) P->flags |= DEBUG_VERBOSE;
298+
if (strchr(&argv[i][2], 't')) P->flags |= DEBUG_TRACE;
299+
if (strchr(&argv[i][2], 'p')) P->flags |= DEBUG_PARSE;
300+
if (strchr(&argv[i][2], 'P')) P->flags |= (DEBUG_PARSE | DEBUG_PARSE_VERBOSE);
301+
if (strchr(&argv[i][2], 'J')) P->flags |= DEBUG_JIT;
302+
if (strchr(&argv[i][2], 'G')) P->flags |= DEBUG_GC;
302303
continue;
303304
}
304305
#endif
@@ -374,16 +375,16 @@ int main(int argc, char *argv[]) {
374375

375376
if (!interactive) {
376377
if (buf != PN_NIL) {
377-
PN code = p2_cmd_exec(P, buf, "-e", exec);
378-
if (!code || PN_TYPE(code) == PN_TERROR)
379-
goto END;
378+
potion_define_global(P, potion_str(P, "$0"), potion_str(P, "-e"));
379+
p2_cmd_exec(P, buf, "-e", exec);
380380
}
381381
else {
382382
potion_define_global(P, potion_str(P, "$0"), potion_str(P, argv[argc-1]));
383383
p2_cmd_compile(P, argv[argc-1], exec);
384384
}
385385
} else {
386386
if (!exec || P->flags & DEBUG_VERBOSE) potion_fatal("no filename given");
387+
potion_define_global(P, potion_str(P, "$0"), potion_str(P, ""));
387388
// TODO: p5 not yet parsed
388389
p2_eval(P, potion_byte_str(P,
389390
"p2::load 'readline';" \

0 commit comments

Comments
 (0)