Commit cf81a88b authored by Yuichiro MASUI's avatar Yuichiro MASUI

Removed mrbc -C option what's C function dump. and TAB to space

parent 0410bb76
/*
** mruby/cdump.h - mruby binary dumper (C source format)
**
** See Copyright Notice in mruby.h
*/
#ifndef MRUBY_CDUMP_H
#define MRUBY_CDUMP_H
#if defined(__cplusplus)
extern "C" {
#endif
#include "mruby.h"
#ifdef DISABLE_STDIO
# error "Configuration conflict. Can't use with DISABLE_STDIO option."
#else
# include <stdio.h>
#endif
int mrb_cdump_irep(mrb_state *mrb, int n, FILE *f,const char *initname);
/* error code */
#define MRB_CDUMP_OK 0
#define MRB_CDUMP_GENERAL_FAILURE -1
#define MRB_CDUMP_WRITE_FAULT -2
#define MRB_CDUMP_INVALID_IREP -6
#define MRB_CDUMP_INVALID_ARGUMENT -7
#if defined(__cplusplus)
} /* extern "C" { */
#endif
#endif /* MRUBY_CDUMP_H */
/*
** cdump.c - mruby binary dumper (C source format)
**
** See Copyright Notice in mruby.h
*/
#include "mruby/cdump.h"
#include <string.h>
#include "mruby/irep.h"
#include "mruby/string.h"
#define MRB_CDUMP_LINE_LEN 128
#define SOURCE_CODE(fmt, ...) fprintf(f, fmt"\n", __VA_ARGS__)
#define SOURCE_CODE0(str) do {fputs(str, f); putc('\n', f);} while (0)
static int
make_cdump_isec(mrb_state *mrb, int irep_no, FILE *f)
{
int i;
mrb_irep *irep = mrb->irep[irep_no];
if (irep == NULL)
return MRB_CDUMP_INVALID_IREP;
/* dump isec struct*/
if (irep->ilen > 0) {
SOURCE_CODE ("static mrb_code iseq_%d[] = {", irep_no);
for (i=0; i<irep->ilen; i++)
SOURCE_CODE(" 0x%08x," , irep->iseq[i]);
SOURCE_CODE0 ("};");
SOURCE_CODE0 ("");
}
return MRB_CDUMP_OK;
}
static size_t
str_format_len(mrb_value str)
{
size_t dump_len = 0;
char *src;
for (src = RSTRING_PTR(str); src < RSTRING_END(str); src++) {
switch (*src) {
case 0x07:/* BEL */ /* fall through */
case 0x08:/* BS */ /* fall through */
case 0x09:/* HT */ /* fall through */
case 0x0A:/* LF */ /* fall through */
case 0x0B:/* VT */ /* fall through */
case 0x0C:/* FF */ /* fall through */
case 0x0D:/* CR */ /* fall through */
case 0x22:/* " */ /* fall through */
case 0x27:/* ' */ /* fall through */
case 0x3F:/* ? */ /* fall through */
case 0x5C:/* \ */ /* fall through */
dump_len += 2;
break;
default:
dump_len++;
break;
}
}
return dump_len;
}
static char*
str_to_format(mrb_value str, char *buf)
{
char *src;
char *dst;
for (src = RSTRING_PTR(str), dst = buf; src < RSTRING_END(str); src++) {
switch (*src) {
case 0x07:/* BEL */ *dst++ = '\\'; *dst++ = 'a'; break;
case 0x08:/* BS */ *dst++ = '\\'; *dst++ = 'b'; break;
case 0x09:/* HT */ *dst++ = '\\'; *dst++ = 't'; break;
case 0x0A:/* LF */ *dst++ = '\\'; *dst++ = 'n'; break;
case 0x0B:/* VT */ *dst++ = '\\'; *dst++ = 'v'; break;
case 0x0C:/* FF */ *dst++ = '\\'; *dst++ = 'f'; break;
case 0x0D:/* CR */ *dst++ = '\\'; *dst++ = 'r'; break;
case 0x22:/* " */ *dst++ = '\\'; *dst++ = '\"'; break;
case 0x27:/* ' */ *dst++ = '\\'; *dst++ = '\''; break;
case 0x3F:/* ? */ *dst++ = '\\'; *dst++ = '\?'; break;
case 0x5C:/* \ */ *dst++ = '\\'; *dst++ = '\\'; break;
default: *dst++ = *src; break;
}
}
return buf;
}
int
make_cdump_irep(mrb_state *mrb, int irep_no, FILE *f)
{
mrb_irep *irep = mrb->irep[irep_no];
int n;
char *buf = 0;
size_t buf_len, str_len;
if (irep == NULL)
return MRB_CDUMP_INVALID_IREP;
buf_len = MRB_CDUMP_LINE_LEN;
if ((buf = (char *)mrb_malloc(mrb, buf_len)) == NULL) {
return MRB_CDUMP_GENERAL_FAILURE;
}
SOURCE_CODE0 (" ai = mrb->arena_idx;");
SOURCE_CODE0 (" irep = mrb_add_irep(mrb);");
SOURCE_CODE0 (" irep->flags = MRB_ISEQ_NO_FREE;");
SOURCE_CODE (" irep->nlocals = %d;", irep->nlocals);
SOURCE_CODE (" irep->nregs = %d;", irep->nregs);
SOURCE_CODE (" irep->ilen = %d;", irep->ilen);
SOURCE_CODE (" irep->iseq = iseq_%d;", irep_no);
SOURCE_CODE (" irep->slen = %d;", irep->slen);
if(irep->slen > 0) {
SOURCE_CODE (" irep->syms = mrb_malloc(mrb, sizeof(mrb_sym)*%d);", irep->slen);
for (n=0; n<irep->slen; n++)
if (irep->syms[n]) {
const char *name;
int len;
name = mrb_sym2name_len(mrb, irep->syms[n], &len);
SOURCE_CODE (" irep->syms[%d] = mrb_intern2(mrb, \"%s\", %d);", n, name, len);
}
}
else
SOURCE_CODE0 (" irep->syms = NULL;");
SOURCE_CODE0 (" irep->pool = NULL;");
SOURCE_CODE0 (" irep->lines = NULL;");
SOURCE_CODE0 (" mrb->irep_len = idx;");
SOURCE_CODE0 (" irep->plen = 0;");
if(irep->plen > 0) {
SOURCE_CODE (" irep->pool = mrb_malloc(mrb, sizeof(mrb_value)*%d);", irep->plen);
for (n=0; n<irep->plen; n++) {
switch (mrb_type(irep->pool[n])) {
case MRB_TT_FLOAT:
SOURCE_CODE(" irep->pool[%d] = mrb_float_value(%.16e);", n, mrb_float(irep->pool[n])); break;
case MRB_TT_FIXNUM:
SOURCE_CODE(" irep->pool[%d] = mrb_fixnum_value(%d);", n, mrb_fixnum(irep->pool[n])); break;
case MRB_TT_STRING:
str_len = str_format_len(irep->pool[n]) + 1;
if ( str_len > buf_len ) {
buf_len = str_len;
if ((buf = (char *)mrb_realloc(mrb, buf, buf_len)) == NULL) {
return MRB_CDUMP_GENERAL_FAILURE;
}
}
memset(buf, 0, buf_len);
SOURCE_CODE(" irep->pool[%d] = mrb_str_new(mrb, \"%s\", %d);", n, str_to_format(irep->pool[n], buf), RSTRING_LEN(irep->pool[n]));
SOURCE_CODE0 (" mrb->arena_idx = ai;");
break;
/* TODO MRB_TT_REGEX */
default: break;
}
SOURCE_CODE0(" irep->plen++;");
}
}
else
SOURCE_CODE0("");
mrb_free(mrb, buf);
return MRB_CDUMP_OK;
}
int
mrb_cdump_irep(mrb_state *mrb, int n, FILE *f,const char *initname)
{
int irep_no;
int error;
if (mrb == NULL || n < 0 || n >= mrb->irep_len || f == NULL || initname == NULL)
return MRB_CDUMP_INVALID_ARGUMENT;
SOURCE_CODE0("#include \"mruby.h\"");
SOURCE_CODE0("#include \"mruby/irep.h\"");
SOURCE_CODE0("#include \"mruby/string.h\"");
SOURCE_CODE0("#include \"mruby/proc.h\"");
SOURCE_CODE0("");
for (irep_no=n; irep_no<mrb->irep_len; irep_no++) {
error = make_cdump_isec(mrb, irep_no, f);
if (error != MRB_CDUMP_OK)
return error;
}
SOURCE_CODE0("void");
SOURCE_CODE ("%s(mrb_state *mrb)", initname);
SOURCE_CODE0("{");
SOURCE_CODE0(" int n = mrb->irep_len;");
SOURCE_CODE0(" int idx = n;");
SOURCE_CODE0(" int ai;");
SOURCE_CODE0(" mrb_irep *irep;");
SOURCE_CODE0("");
for (irep_no=n; irep_no<mrb->irep_len; irep_no++) {
error = make_cdump_irep(mrb, irep_no, f);
if (error != MRB_CDUMP_OK)
return error;
}
SOURCE_CODE0(" mrb_run(mrb, mrb_proc_new(mrb, mrb->irep[n]), mrb_top_self(mrb));");
SOURCE_CODE0("}");
return MRB_CDUMP_OK;
}
#include "mruby.h"
#include "mruby/proc.h"
#include "mruby/dump.h"
#include "mruby/cdump.h"
#include "mruby/compile.h"
#include <stdio.h>
#include <string.h>
......@@ -21,7 +20,6 @@ struct _args {
char *initname;
char *ext;
int check_syntax : 1;
int dump_type : 2;
int verbose : 1;
};
......@@ -34,7 +32,6 @@ usage(const char *name)
"-o<outfile> place the output into <outfile>",
"-v print version number, then trun on verbose mode",
"-B<symbol> binary <symbol> output in C language format",
"-C<func> function <func> output in C language format",
"--verbose run at verbose mode",
"--version print the version",
"--copyright print the copyright",
......@@ -89,7 +86,6 @@ parse_args(mrb_state *mrb, int argc, char **argv, struct _args *args)
outfile = get_outfilename((*argv) + 2, "");
break;
case 'B':
case 'C':
args->ext = C_EXT;
args->initname = (*argv) + 2;
if (*args->initname == '\0') {
......@@ -97,7 +93,6 @@ parse_args(mrb_state *mrb, int argc, char **argv, struct _args *args)
result = -2;
goto exit;
}
args->dump_type = ((*argv)[1] == 'B') ? DUMP_TYPE_BIN : DUMP_TYPE_CODE;
break;
case 'c':
args->check_syntax = 1;
......@@ -208,10 +203,7 @@ main(int argc, char **argv)
return EXIT_SUCCESS;
}
if (args.initname) {
if (args.dump_type == DUMP_TYPE_BIN)
n = mrb_bdump_irep(mrb, n, args.wfp, args.initname);
else
n = mrb_cdump_irep(mrb, n, args.wfp, args.initname);
}
else {
n = mrb_dump_irep(mrb, n, args.wfp);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment