int edp_init(int thread_num){ int ret = -1; ret = logger_init(); if(ret != 0){ return ret; } // FIXME:pre-alloc memory for your application ret = mcache_init(NULL, 0); if(ret != 0){ log_warn("mcache init fail:%d\n", ret); goto exit_mcache; } ret = hset_init(); if(ret != 0){ log_warn("hset init fail:%d\n", ret); goto exit_hset; } ret = worker_init(thread_num); if(ret != 0){ log_warn("init worker fail:%d\n", ret); goto exit_worker; } ret = emit_init(); if(ret != 0){ log_warn("init emitter fail:%d\n", ret); goto exit_emit; } ret = edpnet_init(); if(ret != 0){ log_warn("init edpnet fail:%d\n", ret); goto exit_net; } log_info("edp have been initialized!\n"); return 0; exit_net: emit_fini(); exit_emit: worker_fini(); exit_worker: hset_fini(); exit_hset: mcache_fini(); exit_mcache: logger_fini(); return ret; }
void write_asm(Syntax* complete_syntax,Stack* string_stack){ tablevar = list_new(); openf("output.s"); //open file. emit_format(); emit_str(string_stack); emit_header(); //print .text function_declare("main"); //declare .globl main emit_init(); //alloc for main. if(complete_syntax->type == BLOCK){ emitblock(complete_syntax); }else printf("invalid syntax"); emit_footer(); //ralloc for main closef(fp); //close file. free(tablevar); }
int main(int argc, char **argv) { char *output = NULL; int format = 1, ch, no_symbol_default = 0; while ((ch = getopt(argc, argv, "o:f:nhvV")) != -1) { switch (ch) { case 'o': if (output) { fprintf(stderr, _("%s: cannot handle multiple output files\n"), argv[0]); exit(1); } output = xstrdup(optarg); break; case 'f': if (!strncasecmp(optarg, "bin", strlen(optarg))) format = 0; else if (!strncasecmp(optarg, "hex", strlen(optarg))) format = 1; else { fprintf(stderr, _("%s: unknown output format `%s'\n"), argv[0], optarg); exit(1); } break; case 'h': usage(argv[0], 0); exit(0); case 'n': no_symbol_default = 1; break; case 'v': verbosity++; break; case 'V': printf("%s %s\n%s\n%s\n", NAME, VERSION, COPYRIGHT, _(LICENSE)); break; default: break; } } if (optind >= argc) { fprintf(stderr, _("%s: insufficient parameters, type `%s -h' for help\n"), argv[0], argv[0]); exit(1); } if (optind < argc - 1) { fprintf(stderr, _("%s: too many parameters, type `%s -h' for help\n"), argv[0], argv[0]); exit(1); } filename = xstrdup(argv[optind]); if (!(yyin = fopen(filename, "r"))) { fprintf(stderr, "%s: %s\n", argv[1], strerror(errno)); exit(2); } if (!no_symbol_default) symbol_default(); if (verbosity) warning("first pass"); pass = 1; yyparse(); if (errors) exit(3); if (verbosity) warning("second pass"); pass = 2; lineno = 1; rewind(yyin); emit_init(); yyparse(); if (errors) exit(3); fclose(yyin); if (verbosity) { struct symbol *s; printf("declared symbols:\n"); for (s = symbols; s; s = s->next) { if (s->defined) printf(" %s = %.4xh\n", s->name, s->value); else printf(" %s = ?\n", s->name); } } if (!output) { if (!strcasecmp(filename + strlen(filename) - 4, ".asm")) { output = xstrdup(filename); strcpy(output + strlen(output) - 4, (format) ? ".hex" : ".bin"); } else { output = xmalloc(strlen(filename) + 5); sprintf(output, "%s.%s", filename, (format) ? "hex" : "bin"); } } if (format) emit_hex(output); else emit_bin(output); xfree(output); return 0; }