Skip to content

Commit 664cdb7

Browse files
committed
COMMON: moved decompiler to command line
1 parent 9039603 commit 664cdb7

File tree

8 files changed

+76
-72
lines changed

8 files changed

+76
-72
lines changed

ChangeLog

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
2018-02-03 (0.12.12)
2+
COMMON: restore 6d array handling
3+
COMMON: increase default stack size
4+
15
2018-01-13 (0.12.12)
26
Fix osx crash
37

src/common/Makefile.am

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ libsb_common_a_SOURCES = \
5353
blib_sound.c \
5454
brun.c \
5555
ceval.c \
56-
decomp.c \
5756
device.c device.h \
5857
screen.c \
5958
system.c \

src/common/brun.c

Lines changed: 8 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ int exec_close_task();
3131
void sys_before_comp();
3232

3333
static char fileName[OS_FILENAME_SIZE + 1];
34-
static int exec_tid;
3534
static stknode_t err_node;
3635

3736
#define EVT_CHECK_EVERY 50
@@ -1528,47 +1527,7 @@ int sbasic_recursive_exec(int tid) {
15281527
}
15291528

15301529
/**
1531-
* dump-taskinfo
1532-
*/
1533-
void sbasic_dump_taskinfo(FILE * output) {
1534-
int i;
1535-
int prev_tid = 0;
1536-
1537-
fprintf(output, "\n* task list:\n");
1538-
for (i = 0; i < count_tasks(); i++) {
1539-
prev_tid = activate_task(i);
1540-
fprintf(output, " id %d, child of %d, file %s, status %d\n", ctask->tid, ctask->parent, ctask->file,
1541-
ctask->status);
1542-
}
1543-
activate_task(prev_tid);
1544-
}
1545-
1546-
/**
1547-
* dump-bytecode
1548-
*/
1549-
void sbasic_dump_bytecode(int tid, FILE *output) {
1550-
int i;
1551-
int prev_tid;
1552-
1553-
prev_tid = activate_task(tid);
1554-
1555-
for (i = 0; i < count_tasks(); i++) {
1556-
activate_task(i);
1557-
if (ctask->parent == tid) {
1558-
// do the same for the childs
1559-
sbasic_dump_bytecode(ctask->tid, output);
1560-
}
1561-
}
1562-
1563-
activate_task(tid);
1564-
fprintf(output, "\n* task: %d/%d (%s)\n", ctask->tid, count_tasks(), prog_file);
1565-
// run the code
1566-
dump_bytecode(output);
1567-
activate_task(prev_tid);
1568-
}
1569-
1570-
/**
1571-
* TODO add comment
1530+
* compile the given file into bytecode
15721531
*/
15731532
int sbasic_compile(const char *file) {
15741533
int comp_rq = 0; // compilation required = 0
@@ -1626,18 +1585,21 @@ int sbasic_compile(const char *file) {
16261585
/**
16271586
* initialize executor and run a binary
16281587
*/
1629-
void sbasic_exec_prepare(const char *filename) {
1588+
int sbasic_exec_prepare(const char *filename) {
1589+
int taskId;
1590+
16301591
v_init_pool();
16311592

16321593
// load source
16331594
if (opt_nosave) {
1634-
exec_tid = brun_create_task(filename, ctask->bytecode, 0);
1595+
taskId = brun_create_task(filename, ctask->bytecode, 0);
16351596
} else {
1636-
exec_tid = brun_create_task(filename, 0, 0);
1597+
taskId = brun_create_task(filename, 0, 0);
16371598
}
16381599
// reset system
16391600
cmd_play_reset();
16401601
graph_reset();
1602+
return taskId;
16411603
}
16421604

16431605
/*
@@ -1694,9 +1656,6 @@ int sbasic_exec(const char *file) {
16941656
opt_pref_height = 0;
16951657
opt_show_page = 0;
16961658

1697-
if (opt_decomp) {
1698-
opt_nosave = 1;
1699-
}
17001659
// setup global values
17011660
gsb_last_line = gsb_last_error = 0;
17021661
strlcpy(gsb_last_file, file, sizeof(gsb_last_file));
@@ -1708,20 +1667,14 @@ int sbasic_exec(const char *file) {
17081667
// cannot run a unit
17091668
exec_rq = 0;
17101669
gsb_last_error = 1;
1711-
} else if (opt_decomp && success) {
1712-
sbasic_exec_prepare(file); // load everything
1713-
sbasic_dump_taskinfo(stdout);
1714-
sbasic_dump_bytecode(exec_tid, stdout);
1715-
exec_close(exec_tid); // clean up executor's garbages
1716-
exec_rq = 0;
17171670
} else if (!success) { // there was some errors; do not continue
17181671
exec_rq = 0;
17191672
gsb_last_error = 1;
17201673
}
17211674

17221675
if (exec_rq) { // we will run it
17231676
// load everything
1724-
sbasic_exec_prepare(file);
1677+
int exec_tid = sbasic_exec_prepare(file);
17251678

17261679
dev_init(opt_graphics, 0); // initialize output device for graphics
17271680
srand(clock()); // randomize

src/common/smbas.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ typedef struct {
101101

102102
EXTERN byte opt_graphics; /**< command-line option: start in graphics mode */
103103
EXTERN byte opt_quiet; /**< command-line option: quiet */
104-
EXTERN byte opt_decomp; /**< decompile */
105104
EXTERN char opt_command[OPT_CMD_SZ]; /**< command-line parameters (COMMAND$) */
106105
EXTERN int opt_base; /**< OPTION BASE x */
107106
EXTERN byte opt_loadmod; /**< load all modules */
@@ -242,14 +241,6 @@ void brun_stop(void);
242241
*/
243242
int brun_status(void);
244243

245-
/**
246-
* decompiler,
247-
* dumps the code in the current task
248-
*
249-
* @param output the output stream (FILE*)
250-
*/
251-
void dump_bytecode(FILE *output);
252-
253244
/**
254245
* returns the last-modified time of the file
255246
*

src/platform/android/jni/common/Android.mk

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ LOCAL_SRC_FILES := \
2929
$(COMMON)/blib_sound.c \
3030
$(COMMON)/brun.c \
3131
$(COMMON)/ceval.c \
32-
$(COMMON)/decomp.c \
3332
$(COMMON)/device.c \
3433
$(COMMON)/screen.c \
3534
$(COMMON)/system.c \

src/platform/console/Makefile.am

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ AM_CPPFLAGS = -I$(top_builddir)/src -I. @PACKAGE_CFLAGS@
99

1010
bin_PROGRAMS = sbasic
1111

12-
sbasic_SOURCES = \
12+
sbasic_SOURCES = \
1313
../console/main.c \
14+
../console/decomp.c \
1415
../console/dev_null.c
1516

1617
sbasic_LDADD = -L$(top_srcdir)/src/common -lsb_common @PACKAGE_LIBS@

src/common/decomp.c renamed to src/platform/console/decomp.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,9 @@
1010
#include "common/sbapp.h"
1111

1212
/**
13-
*
1413
* decompiler
15-
*
1614
*/
17-
void dump_bytecode(FILE * output) {
15+
void dump_bytecode(FILE *output) {
1816
int i, c, b, d, h, l, j;
1917
long lng;
2018
int len, new_ip;

src/platform/console/main.c

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@
1313
// global filename (its needed for CTRL+C signal - delete temporary)
1414
char g_file[OS_PATHNAME_SIZE + 1];
1515

16+
// decompile handling
17+
int decomp;
18+
void dump_bytecode(FILE *output);
19+
int sbasic_compile(const char *file);
20+
int sbasic_exec_prepare(const char *file);
21+
int exec_close(int tid);
22+
1623
/*
1724
* remove temporary files
1825
*
@@ -131,6 +138,7 @@ int setup_file(const char *program) {
131138
int process_options(int argc, char *argv[]) {
132139
int opt_ihavename = 0;
133140
int opt_nomore = 0;
141+
decomp = 0;
134142

135143
for (int i = 1; i < argc; i++) {
136144
if (argv[i][0] == '-') {
@@ -152,7 +160,8 @@ int process_options(int argc, char *argv[]) {
152160

153161
case 's':
154162
// decompile
155-
opt_decomp++;
163+
decomp = 1;
164+
opt_nosave = 1;
156165
break;
157166

158167
case 'c':
@@ -261,6 +270,52 @@ void *malloc(size_t size) {
261270
}
262271
#endif
263272

273+
void print_taskinfo(FILE *output) {
274+
int prev_tid = 0;
275+
276+
fprintf(output, "\n* task list:\n");
277+
for (int i = 0; i < count_tasks(); i++) {
278+
prev_tid = activate_task(i);
279+
fprintf(output, " id %d, child of %d, file %s, status %d\n",
280+
ctask->tid, ctask->parent, ctask->file, ctask->status);
281+
}
282+
activate_task(prev_tid);
283+
}
284+
285+
void print_bytecode(int tid, FILE *output) {
286+
int prev_tid = activate_task(tid);
287+
fprintf(stderr, "%d %d %d\n", prev_tid, tid, count_tasks());
288+
289+
for (int i = 0; i < count_tasks(); i++) {
290+
activate_task(i);
291+
if (ctask->parent == tid) {
292+
// do the same for the childs
293+
print_bytecode(ctask->tid, output);
294+
exit(1);
295+
}
296+
}
297+
298+
activate_task(tid);
299+
fprintf(output, "\n* task: %d/%d (%s)\n", ctask->tid, count_tasks(), prog_file);
300+
dump_bytecode(output);
301+
activate_task(prev_tid);
302+
}
303+
304+
void decompile() {
305+
init_tasks();
306+
unit_mgr_init();
307+
sblmgr_init(0, NULL);
308+
if (sbasic_compile(g_file)) {
309+
int exec_tid = sbasic_exec_prepare(g_file);
310+
print_taskinfo(stdout);
311+
print_bytecode(exec_tid, stdout);
312+
exec_close(exec_tid);
313+
}
314+
unit_mgr_close();
315+
sblmgr_close();
316+
destroy_tasks();
317+
}
318+
264319
/*
265320
* program entry point
266321
*/
@@ -282,7 +337,11 @@ int main(int argc, char *argv[]) {
282337
char prev_cwd[OS_PATHNAME_SIZE + 1];
283338
prev_cwd[0] = 0;
284339
getcwd(prev_cwd, sizeof(prev_cwd) - 1);
285-
sbasic_main(g_file);
340+
if (decomp) {
341+
decompile();
342+
} else {
343+
sbasic_main(g_file);
344+
}
286345
chdir(prev_cwd);
287346
} else {
288347
show_brief_help();

0 commit comments

Comments
 (0)