@@ -88,47 +88,51 @@ static void p2_cmd_version(Potion *P) {
88
88
printf (p2_banner , POTION_JIT );
89
89
}
90
90
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
+
91
101
static PN p2_cmd_exec (Potion * P , PN buf , char * filename , exec_mode_t exec ) {
92
102
PN code = p2_source_load (P , PN_NIL , buf );
93
103
if (PN_IS_PROTO (code )) {
94
104
if (P -> flags & DEBUG_VERBOSE )
95
- printf ( "\n\n-- loaded --\n" );
105
+ fprintf ( stderr , "\n\n-- loaded --\n" );
96
106
} else {
97
107
code = p2_parse (P , buf , filename );
98
108
if (!code || PN_TYPE (code ) == PN_TERROR ) {
99
109
potion_p (P , code );
100
110
return code ;
101
111
}
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 );
106
114
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" );
109
116
}
110
- if (P -> flags & DEBUG_VERBOSE )
111
- potion_p (P , code );
117
+ dumpv_p (code );
112
118
if (exec == EXEC_VM ) {
113
119
code = potion_vm (P , code , P -> lobby , PN_NIL , 0 , NULL );
114
120
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 );
120
125
} else if (exec == EXEC_JIT ) {
121
126
#ifdef POTION_JIT_TARGET
122
127
PN val ;
123
128
PN cl = potion_closure_new (P , (PN_F )potion_jit_proto (P , code ), PN_NIL , 1 );
124
129
PN_CLOSURE (cl )-> data [0 ] = code ;
125
130
val = PN_PROTO (code )-> jit (P , cl , P -> lobby );
126
131
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 );
132
136
#else
133
137
fprintf (stderr , "** p2 built without JIT support\n" );
134
138
#endif
@@ -188,39 +192,46 @@ static void p2_cmd_compile(Potion *P, char *filename, exec_mode_t exec) {
188
192
potion_fatal ("--compile-c not yet implemented\n" );
189
193
}
190
194
else if (exec == EXEC_COMPILE_NATIVE ) {
191
- //code = potion_send(P, code, "dumpbin ");
195
+ //code = potion_send(P, code, "dumpexec ");
192
196
potion_fatal ("--compile-native not yet implemented\n" );
193
197
}
198
+
194
199
if (fwrite (PN_STR_PTR (code ), 1 , PN_STR_LEN (code ), plc ) == PN_STR_LEN (code )) {
195
200
printf ("** compiled code saved to %s\n" , plcpath );
196
201
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
+
197
210
} else {
198
211
fprintf (stderr , "** could not write all compiled code." );
199
212
}
200
- if (exec == EXEC_COMPILE )
201
- printf ("** run it with: p2 %s\n" , plcpath );
202
213
}
203
214
204
215
#if defined(DEBUG )
205
216
if (P -> flags & DEBUG_GC ) { // GC sanity check
206
- printf ( "\n-- gc check --\n" );
217
+ fprintf ( stderr , "\n-- gc check --\n" );
207
218
void * scanptr = (void * )((char * )P -> mem -> old_lo + (sizeof (PN ) * 2 ));
208
219
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 ));
212
223
if (((struct PNFwd * )scanptr )-> fwd != POTION_FWD
213
224
&& ((struct PNFwd * )scanptr )-> fwd != POTION_COPIED ) {
214
225
if ((signed long )(((struct PNObject * )scanptr )-> vt ) < 0
215
226
|| ((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 );
218
229
break ;
219
230
}
220
231
}
221
232
scanptr = (void * )((char * )scanptr + potion_type_size (P , scanptr ));
222
233
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" );
224
235
break ;
225
236
}
226
237
}
@@ -282,23 +293,13 @@ int main(int argc, char *argv[]) {
282
293
printf (" G GC\n" );
283
294
goto END ;
284
295
}
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 ;
302
303
continue ;
303
304
}
304
305
#endif
@@ -374,16 +375,16 @@ int main(int argc, char *argv[]) {
374
375
375
376
if (!interactive ) {
376
377
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 );
380
380
}
381
381
else {
382
382
potion_define_global (P , potion_str (P , "$0" ), potion_str (P , argv [argc - 1 ]));
383
383
p2_cmd_compile (P , argv [argc - 1 ], exec );
384
384
}
385
385
} else {
386
386
if (!exec || P -> flags & DEBUG_VERBOSE ) potion_fatal ("no filename given" );
387
+ potion_define_global (P , potion_str (P , "$0" ), potion_str (P , "" ));
387
388
// TODO: p5 not yet parsed
388
389
p2_eval (P , potion_byte_str (P ,
389
390
"p2::load 'readline';" \
0 commit comments