| 1 |
#include <mruby.h> |
| 2 |
|
| 3 |
#ifdef MRB_NO_STDIO |
| 4 |
# error mruby-bin-mrbc conflicts 'MRB_NO_STDIO' in your build configuration |
| 5 |
#endif |
| 6 |
|
| 7 |
#include <stdlib.h> |
| 8 |
#include <string.h> |
| 9 |
#include <mruby/compile.h> |
| 10 |
#include <mruby/dump.h> |
| 11 |
#include <mruby/proc.h> |
| 12 |
#include <mruby/internal.h> |
| 13 |
|
| 14 |
#define RITEBIN_EXT ".mrb" |
| 15 |
#define C_EXT ".c" |
| 16 |
|
| 17 |
struct mrbc_args { |
| 18 |
const char *prog; |
| 19 |
const char *outfile; |
| 20 |
const char *initname; |
| 21 |
char **argv; |
| 22 |
int argc; |
| 23 |
int idx; |
| 24 |
mrb_bool dump_struct : 1; |
| 25 |
mrb_bool check_syntax : 1; |
| 26 |
mrb_bool verbose : 1; |
| 27 |
mrb_bool remove_lv : 1; |
| 28 |
mrb_bool no_ext_ops : 1; |
| 29 |
mrb_bool no_optimize : 1; |
| 30 |
uint8_t flags : 2; |
| 31 |
}; |
| 32 |
|
| 33 |
static void |
| 34 |
usage(const char *name) |
| 35 |
{ |
| 36 |
static const char *const usage_msg[] = { |
| 37 |
"switches:", |
| 38 |
"-c check syntax only", |
| 39 |
"-o<outfile> place the output into <outfile>; required for multi-files", |
| 40 |
"-v print version number, then turn on verbose mode", |
| 41 |
"-g produce debugging information", |
| 42 |
"-B<symbol> binary <symbol> output in C language format", |
| 43 |
"-S dump C struct (requires -B)", |
| 44 |
"-s define <symbol> as static variable", |
| 45 |
"--remove-lv remove local variables", |
| 46 |
"--no-ext-ops prohibit using OP_EXTs", |
| 47 |
"--no-optimize disable peephole optimization", |
| 48 |
"--verbose run at verbose mode", |
| 49 |
"--version print the version", |
| 50 |
"--copyright print the copyright", |
| 51 |
NULL |
| 52 |
}; |
| 53 |
const char *const *p = usage_msg; |
| 54 |
|
| 55 |
printf("Usage: %s [switches] programfile...\n", name); |
| 56 |
while (*p) |
| 57 |
printf(" %s\n", *p++); |
| 58 |
} |
| 59 |
|
| 60 |
static char * |
| 61 |
get_outfilename(mrb_state *mrb, char *infile, const char *ext) |
| 62 |
{ |
| 63 |
size_t ilen, flen, elen; |
| 64 |
char *outfile; |
| 65 |
char *p = NULL; |
| 66 |
|
| 67 |
ilen = strlen(infile); |
| 68 |
flen = ilen; |
| 69 |
if (*ext) { |
| 70 |
elen = strlen(ext); |
| 71 |
if ((p = strrchr(infile, '.'))) { |
| 72 |
ilen = p - infile; |
| 73 |
} |
| 74 |
flen += elen; |
| 75 |
} |
| 76 |
else { |
| 77 |
flen = ilen; |
| 78 |
} |
| 79 |
outfile = (char*)mrb_malloc(mrb, flen+1); |
| 80 |
strncpy(outfile, infile, ilen+1); |
| 81 |
if (p) { |
| 82 |
strncpy(outfile+ilen, ext, elen+1); |
| 83 |
} |
| 84 |
|
| 85 |
return outfile; |
| 86 |
} |
| 87 |
|
| 88 |
static int |
| 89 |
parse_args(mrb_state *mrb, int argc, char **argv, struct mrbc_args *args) |
| 90 |
{ |
| 91 |
static const struct mrbc_args args_zero = { 0 }; |
| 92 |
int i; |
| 93 |
|
| 94 |
*args = args_zero; |
| 95 |
args->argc = argc; |
| 96 |
args->argv = argv; |
| 97 |
args->prog = argv[0]; |
| 98 |
|
| 99 |
for (i=1; i<argc; i++) { |
| 100 |
if (argv[i][0] == '-') { |
| 101 |
switch ((argv[i])[1]) { |
| 102 |
case 'o': |
| 103 |
if (args->outfile) { |
| 104 |
fprintf(stderr, "%s: an output file is already specified. (%s)\n", |
| 105 |
args->prog, args->outfile); |
| 106 |
return -1; |
| 107 |
} |
| 108 |
if (argv[i][2] == '\0' && argv[i+1]) { |
| 109 |
i++; |
| 110 |
args->outfile = get_outfilename(mrb, argv[i], ""); |
| 111 |
} |
| 112 |
else { |
| 113 |
args->outfile = get_outfilename(mrb, argv[i] + 2, ""); |
| 114 |
} |
| 115 |
break; |
| 116 |
case 'S': |
| 117 |
args->dump_struct = TRUE; |
| 118 |
break; |
| 119 |
case 'B': |
| 120 |
if (argv[i][2] == '\0' && argv[i+1]) { |
| 121 |
i++; |
| 122 |
args->initname = argv[i]; |
| 123 |
} |
| 124 |
else { |
| 125 |
args->initname = argv[i]+2; |
| 126 |
} |
| 127 |
if (*args->initname == '\0') { |
| 128 |
fprintf(stderr, "%s: function name is not specified.\n", args->prog); |
| 129 |
return -1; |
| 130 |
} |
| 131 |
break; |
| 132 |
case 'c': |
| 133 |
args->check_syntax = TRUE; |
| 134 |
break; |
| 135 |
case 'v': |
| 136 |
if (!args->verbose) mrb_show_version(mrb); |
| 137 |
args->verbose = TRUE; |
| 138 |
break; |
| 139 |
case 'g': |
| 140 |
args->flags |= MRB_DUMP_DEBUG_INFO; |
| 141 |
break; |
| 142 |
case 's': |
| 143 |
args->flags |= MRB_DUMP_STATIC; |
| 144 |
break; |
| 145 |
case 'E': |
| 146 |
case 'e': |
| 147 |
fprintf(stderr, "%s: -e/-E option no longer needed.\n", args->prog); |
| 148 |
break; |
| 149 |
case 'h': |
| 150 |
return -1; |
| 151 |
case '-': |
| 152 |
if (argv[i][1] == '\n') { |
| 153 |
return i; |
| 154 |
} |
| 155 |
if (strcmp(argv[i] + 2, "version") == 0) { |
| 156 |
mrb_show_version(mrb); |
| 157 |
exit(EXIT_SUCCESS); |
| 158 |
} |
| 159 |
else if (strcmp(argv[i] + 2, "verbose") == 0) { |
| 160 |
args->verbose = TRUE; |
| 161 |
break; |
| 162 |
} |
| 163 |
else if (strcmp(argv[i] + 2, "copyright") == 0) { |
| 164 |
mrb_show_copyright(mrb); |
| 165 |
exit(EXIT_SUCCESS); |
| 166 |
} |
| 167 |
else if (strcmp(argv[i] + 2, "remove-lv") == 0) { |
| 168 |
args->remove_lv = TRUE; |
| 169 |
break; |
| 170 |
} |
| 171 |
else if (strcmp(argv[i] + 2, "no-ext-ops") == 0) { |
| 172 |
args->no_ext_ops = TRUE; |
| 173 |
break; |
| 174 |
} |
| 175 |
else if (strcmp(argv[i] + 2, "no-optimize") == 0) { |
| 176 |
args->no_optimize = TRUE; |
| 177 |
break; |
| 178 |
} |
| 179 |
return -1; |
| 180 |
default: |
| 181 |
return i; |
| 182 |
} |
| 183 |
} |
| 184 |
else { |
| 185 |
break; |
| 186 |
} |
| 187 |
} |
| 188 |
return i; |
| 189 |
} |
| 190 |
|
| 191 |
static void |
| 192 |
cleanup(mrb_state *mrb, struct mrbc_args *args) |
| 193 |
{ |
| 194 |
mrb_free(mrb, (void*)args->outfile); |
| 195 |
mrb_close(mrb); |
| 196 |
} |
| 197 |
|
| 198 |
static int |
| 199 |
partial_hook(struct mrb_parser_state *p) |
| 200 |
{ |
| 201 |
mrbc_context *c = p->cxt; |
| 202 |
struct mrbc_args *args = (struct mrbc_args*)c->partial_data; |
| 203 |
const char *fn; |
| 204 |
|
| 205 |
if (p->f) fclose(p->f); |
| 206 |
if (args->idx >= args->argc) { |
| 207 |
p->f = NULL; |
| 208 |
return -1; |
| 209 |
} |
| 210 |
fn = args->argv[args->idx++]; |
| 211 |
p->f = fopen(fn, "rb"); |
| 212 |
if (p->f == NULL) { |
| 213 |
fprintf(stderr, "%s: cannot open program file. (%s)\n", args->prog, fn); |
| 214 |
return -1; |
| 215 |
} |
| 216 |
mrb_parser_set_filename(p, fn); |
| 217 |
return 0; |
| 218 |
} |
| 219 |
|
| 220 |
static mrb_value |
| 221 |
load_file(mrb_state *mrb, struct mrbc_args *args) |
| 222 |
{ |
| 223 |
mrbc_context *c; |
| 224 |
mrb_value result; |
| 225 |
char *input = args->argv[args->idx]; |
| 226 |
FILE *infile; |
| 227 |
mrb_bool need_close = FALSE; |
| 228 |
|
| 229 |
c = mrbc_context_new(mrb); |
| 230 |
if (args->verbose) |
| 231 |
c->dump_result = TRUE; |
| 232 |
c->no_exec = TRUE; |
| 233 |
c->no_ext_ops = args->no_ext_ops; |
| 234 |
c->no_optimize = args->no_optimize; |
| 235 |
if (input[0] == '-' && input[1] == '\0') { |
| 236 |
infile = stdin; |
| 237 |
} |
| 238 |
else { |
| 239 |
need_close = TRUE; |
| 240 |
if ((infile = fopen(input, "rb")) == NULL) { |
| 241 |
fprintf(stderr, "%s: cannot open program file. (%s)\n", args->prog, input); |
| 242 |
return mrb_nil_value(); |
| 243 |
} |
| 244 |
} |
| 245 |
mrbc_filename(mrb, c, input); |
| 246 |
args->idx++; |
| 247 |
if (args->idx < args->argc) { |
| 248 |
need_close = FALSE; |
| 249 |
mrbc_partial_hook(mrb, c, partial_hook, (void*)args); |
| 250 |
} |
| 251 |
|
| 252 |
result = mrb_load_file_cxt(mrb, infile, c); |
| 253 |
if (need_close) fclose(infile); |
| 254 |
mrbc_context_free(mrb, c); |
| 255 |
if (mrb_undef_p(result)) { |
| 256 |
return mrb_nil_value(); |
| 257 |
} |
| 258 |
return result; |
| 259 |
} |
| 260 |
|
| 261 |
static int |
| 262 |
dump_file(mrb_state *mrb, FILE *wfp, const char *outfile, struct RProc *proc, struct mrbc_args *args) |
| 263 |
{ |
| 264 |
int n = MRB_DUMP_OK; |
| 265 |
const mrb_irep *irep = proc->body.irep; |
| 266 |
|
| 267 |
if (args->remove_lv) { |
| 268 |
mrb_irep_remove_lv(mrb, (mrb_irep*)irep); |
| 269 |
} |
| 270 |
if (args->initname) { |
| 271 |
if (args->dump_struct) { |
| 272 |
n = mrb_dump_irep_cstruct(mrb, irep, args->flags, wfp, args->initname); |
| 273 |
} |
| 274 |
else { |
| 275 |
n = mrb_dump_irep_cfunc(mrb, irep, args->flags, wfp, args->initname); |
| 276 |
} |
| 277 |
if (n == MRB_DUMP_INVALID_ARGUMENT) { |
| 278 |
fprintf(stderr, "%s: invalid C language symbol name\n", args->initname); |
| 279 |
} |
| 280 |
} |
| 281 |
else { |
| 282 |
n = mrb_dump_irep_binary(mrb, irep, args->flags, wfp); |
| 283 |
} |
| 284 |
if (n != MRB_DUMP_OK) { |
| 285 |
fprintf(stderr, "%s: error in mrb dump (%s) %d\n", args->prog, outfile, n); |
| 286 |
} |
| 287 |
return n; |
| 288 |
} |
| 289 |
|
| 290 |
int |
| 291 |
main(int argc, char **argv) |
| 292 |
{ |
| 293 |
mrb_state *mrb = mrb_open_core(NULL, NULL); |
| 294 |
int n, result; |
| 295 |
struct mrbc_args args; |
| 296 |
FILE *wfp; |
| 297 |
mrb_value load; |
| 298 |
|
| 299 |
if (mrb == NULL) { |
| 300 |
fputs("Invalid mrb_state, exiting mrbc\n", stderr); |
| 301 |
return EXIT_FAILURE; |
| 302 |
} |
| 303 |
|
| 304 |
n = parse_args(mrb, argc, argv, &args); |
| 305 |
if (n < 0) { |
| 306 |
cleanup(mrb, &args); |
| 307 |
usage(argv[0]); |
| 308 |
return EXIT_FAILURE; |
| 309 |
} |
| 310 |
if (n == argc) { |
| 311 |
fprintf(stderr, "%s: no program file given\n", args.prog); |
| 312 |
return EXIT_FAILURE; |
| 313 |
} |
| 314 |
if (args.outfile == NULL && !args.check_syntax) { |
| 315 |
if (n + 1 == argc) { |
| 316 |
args.outfile = get_outfilename(mrb, argv[n], args.initname ? C_EXT : RITEBIN_EXT); |
| 317 |
} |
| 318 |
else { |
| 319 |
fprintf(stderr, "%s: output file should be specified to compile multiple files\n", args.prog); |
| 320 |
return EXIT_FAILURE; |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
args.idx = n; |
| 325 |
load = load_file(mrb, &args); |
| 326 |
if (mrb_nil_p(load)) { |
| 327 |
cleanup(mrb, &args); |
| 328 |
return EXIT_FAILURE; |
| 329 |
} |
| 330 |
if (args.check_syntax) { |
| 331 |
printf("%s:%s:Syntax OK\n", args.prog, argv[n]); |
| 332 |
} |
| 333 |
|
| 334 |
if (args.check_syntax) { |
| 335 |
cleanup(mrb, &args); |
| 336 |
return EXIT_SUCCESS; |
| 337 |
} |
| 338 |
|
| 339 |
if (args.outfile) { |
| 340 |
if (strcmp("-", args.outfile) == 0) { |
| 341 |
wfp = stdout; |
| 342 |
} |
| 343 |
else if ((wfp = fopen(args.outfile, "wb")) == NULL) { |
| 344 |
fprintf(stderr, "%s: cannot open output file:(%s)\n", args.prog, args.outfile); |
| 345 |
return EXIT_FAILURE; |
| 346 |
} |
| 347 |
} |
| 348 |
else { |
| 349 |
fputs("Output file is required\n", stderr); |
| 350 |
return EXIT_FAILURE; |
| 351 |
} |
| 352 |
result = dump_file(mrb, wfp, args.outfile, mrb_proc_ptr(load), &args); |
| 353 |
fclose(wfp); |
| 354 |
cleanup(mrb, &args); |
| 355 |
if (result != MRB_DUMP_OK) { |
| 356 |
return EXIT_FAILURE; |
| 357 |
} |
| 358 |
return EXIT_SUCCESS; |
| 359 |
} |
| 360 |
|
| 361 |
void |
| 362 |
mrb_init_mrblib(mrb_state *mrb) |
| 363 |
{ |
| 364 |
} |
| 365 |
|
| 366 |
#ifndef MRB_NO_GEMS |
| 367 |
void |
| 368 |
mrb_init_mrbgems(mrb_state *mrb) |
| 369 |
{ |
| 370 |
} |
| 371 |
#endif |