int main() { char filename[100]; printf("Enter filename:\n"); scanf("%s",filename); if((sourcefile=fopen(filename,"r"))==NULL) { cout << "file open error\n"; } else{ global_init(); symbtable_new_level("main"); parser_program(); /* if(!global_error_num) {*/ basicblock_func_to_block(); codes_to_codes(); optimazation_adr_alloc(); #ifdef MAIN_DEBUG symbtable_display(); global_quadruple_display(); cout << "#####################################"<<endl; bb_display_quad(); printf("Program compiles successfully!\n"); #else printf("Program compiles successfully!\n"); #endif generate_main(); // } } return 0; }
int generate_code_file(struct TestSet *set, FILE *file_out) { // set file-wide out file pointer fout = file_out; // XXX look for this file and emit it if not present fputs(module_code, fout); int set_i = 0; if (generate_set(set, &set_i)) return -1; set_i = 0; generate_main(set, &set_i); return 0; }
int generate_code_file(struct TestSuite *suites, FILE *file_out) { int suite_i; // set file-wide out file pointer fout = file_out; // XXX look for this file and emit it if not present fputs(module_code, fout); suite_i = 0; if (generate_suite(suites, &suite_i)) return -1; suite_i = 0; generate_main(suites, &suite_i); return 0; }
void generate_leftcorner_parser (string basename, string_list predefs) { compute_leftcorner_relations (); generate_goal_for_rules (); generate_goal_relation (); warning ("generating left corner parser..."); open_output_file (basename, "leftcorner", "c"); generate_std_includes (predefs); code_lattices (); code_lexicon (); generate_meta_rules (); generate_rule_declarations (); code_nodenrs (basename); if (editor) generate_enter_templates (); warning ("coding syntax rules..."); code_goal_for_rules (); code_goal_relation (); generate_parser_routines (); warning ("coding postamble and main..."); generate_start_rule (predefs); generate_module_interface (basename, predefs); generate_main (basename); close_output_file (); };
int main(int argc, char *argv[]) { int i, len; char *prefix = NULL; char *dfafile, *dictfile, *termfile; int gnum = 10; char *spname_default = SPNAME_DEF; char *spname = NULL; #define NEXTARG (++i >= argc) ? (char *)usage(argv[0]) : argv[i] /* argument */ for(i=1;i<argc;i++) { if (argv[i][0] == '-') { switch(argv[i][1]) { case 'v': /* verbose output */ verbose_flag = TRUE; gnum = 1; break; case 't': /* terminal mode */ term_mode = TRUE; break; case 'n': gnum = atoi(NEXTARG); break; case 's': if (++i >= argc) { usage(argv[0]); } spname = argv[i]; break; default: fprintf(stderr, "no such option: %s\n",argv[i]); usage(argv[0]); } } else { prefix = argv[i]; } } if (prefix == NULL) usage(argv[0]); if (spname == NULL) spname = spname_default; len = strlen(prefix) + 10; dfafile = (char *)mymalloc(len); dictfile = (char *)mymalloc(len); termfile = (char *)mymalloc(len); strcpy(dfafile, prefix); strcat(dfafile, ".dfa"); strcpy(dictfile, prefix); strcat(dictfile, ".dict"); strcpy(termfile, prefix); strcat(termfile, ".term"); /* start init */ winfo = word_info_new(); init_voca(winfo, dictfile, NULL, TRUE, FALSE); dfa = dfa_info_new(); init_dfa(dfa, dfafile); make_dfa_voca_ref(dfa, winfo); termname = (char **)mymalloc(sizeof(char *) * dfa->term_num); init_term(termfile, termname); if (termname[0] == NULL) { /* no .term file */ no_term_file = TRUE; } else { no_term_file = FALSE; } /* output info */ put_dfainfo(); /* set dfa->sp_id and dfa->is_sp[cid] from name "sp" */ { int t, i; WORD_ID w; dfa->sp_id = WORD_INVALID; dfa->is_sp = (boolean *)mymalloc(sizeof(boolean) * dfa->term_num); for(t=0;t<dfa->term_num;t++) { dfa->is_sp[t] = FALSE; for(i=0;i<dfa->term.wnum[t]; i++) { w = dfa->term.tw[t][i]; if (strcmp(winfo->woutput[w], spname) == 0) { if (dfa->sp_id == WORD_INVALID) dfa->sp_id = w; dfa->is_sp[t] = TRUE; break; } } } } if (verbose_flag) { if (dfa->sp_id != WORD_INVALID) { printf("skippable word for NOISE: %s\t%s\n", winfo->wname[dfa->sp_id], winfo->woutput[dfa->sp_id]); } } printf("----- \n"); /* random seed */ srand(getpid()); /* main loop */ generate_main(gnum); free(dfafile); free(dictfile); return 0; }
virtual bool generate(OutputIterator& sink, Context& context, no_delimiter) { return generate_main(sink, context, unused, Auto()); }
virtual bool generate(OutputIterator& sink, Context& context, delimiter_type const& delim) { return generate_main(sink, context, delim, Auto()); }
int main(int argc, char **argv) { options_t options = { 0, }; char *validity_str = NULL; libabac_init(); subjects = xmalloc(sizeof(subject_t) * 2); subjects_size = 2; roles = xmalloc(sizeof(char *) * 2); roles_size = 2; struct option getopts[] = { { "help", 0, &options.help, 1 }, { "generate", 0, &options.mode, MODE_GENERATE }, { "verify", 0, &options.mode, MODE_VERIFY }, { "keyid", 0, &options.mode, MODE_KEYID }, { "attribute", 0, &options.mode, MODE_ATTRIBUTE }, { "roles", 0, &options.mode, MODE_ROLES }, { "version", 0, &options.mode, MODE_VERSION }, { "display", 0, &options.mode, MODE_DISPLAY }, { "cert", 1, 0, OPT_CERT }, // generate options { "cn", 1, 0, OPT_CN }, { "validity", 1, 0, OPT_VALIDITY }, // attribute options { "issuer", 1, 0, OPT_ISSUER }, { "key", 1, 0, OPT_KEY }, { "role", 1, 0, OPT_ROLE }, { "subject-cert", 1, 0, OPT_SUBJECT_CERT }, { "subject-id", 1, 0, OPT_SUBJECT_ID }, { "subject-role", 1, 0, OPT_SUBJECT_ROLE }, { "out", 1, 0, OPT_OUT }, // attribute_rule option { "attrrule", 1, 0, OPT_ATTRRULE }, // verify option { "attrcert", 1, 0, OPT_ATTRCERT }, // display options { "show", 1, 0, OPT_SHOW }, { NULL }, }; for ( ; ; ) { int c = getopt_long(argc, argv, "", getopts, NULL); if (c < 0) break; switch (c) { // set the option from the value in the getopts struct case 0: continue; case OPT_CERT: options.cert = xstrdup(optarg); break; // generate options case OPT_CN: options.cn = xstrdup(optarg); break; case OPT_VALIDITY: // also an attribute option validity_str = xstrdup(optarg); break; // attribute options case OPT_ISSUER: options.issuer = xstrdup(optarg); break; case OPT_KEY: // also an generate option options.key = xstrdup(optarg); break; case OPT_ROLE: options.role = xstrdup(optarg); break; case OPT_SUBJECT_CERT: subject(xstrdup(optarg), 1); break; case OPT_SUBJECT_ID: subject(xstrdup(optarg), 0); break; case OPT_SUBJECT_ROLE: role(xstrdup(optarg)); break; case OPT_OUT: options.out = xstrdup(optarg); break; // attribute rule options case OPT_ATTRRULE: options.attrrule = xstrdup(optarg); break; // verify options case OPT_ATTRCERT: options.attrcert = xstrdup(optarg); break; // display options case OPT_SHOW: options.show = xstrdup(optarg); break; case '?': break; default: printf("wat\n"); return 45; } } if (options.help || optind < argc) { if (optind > 0 && optind < argc) printf("I don't understand %s\n", argv[optind]); usage(&options); } // parse the validity if (validity_str != NULL) { char suffix = 'd'; // default suffix is days int multiplier; int len = strlen(validity_str); assert(len > 0); // get the suffix char if it's alphabetical if (isalpha(validity_str[len - 1])) { suffix = validity_str[len - 1]; // truncate validity_str[len - 1] = '\0'; --len; // make sure it's not only a suffix if (len == 0) { printf("Invalid validity\n"); usage(&options); } } // convert the suffix to a multiplier switch(suffix) { case 's': multiplier = 1; break; case 'm': multiplier = 60; break; case 'h': multiplier = 3600; break; case 'd': multiplier = 86400; break; case 'y': multiplier = 31536000; break; default: printf("Invalid suffix, must be s m h d y\n"); usage(&options); } // ascii to int char *end; options.validity = strtol(validity_str, &end, 10); if (errno != 0 || end - validity_str < len) { printf("Invalid validity\n"); usage(&options); } if (options.validity <= 0) { printf("Invalid validity: must be > 0\n"); usage(&options); } // multiply! options.validity *= multiplier; free(validity_str); } if (options.mode == MODE_ATTRIBUTE && options.attrrule == NULL) { int i; // have to do error checking on subjects here if ( (num_subjects == 0) || (num_subjects != num_roles && num_subjects != 1 && num_roles != 0) ) { printf( "You have %d subject%s and %d role%s, which is invalid\n", num_subjects, num_subjects == 1 ? "" : "s", num_roles, num_roles == 1 ? "" : "s" ); usage(&options); } for (i = 0; i < num_roles; ++i) subjects[i].role = roles[i]; free(roles); options.subjects = subjects; options.num_subjects = num_subjects; } // launch the sub command switch (options.mode) { case MODE_GENERATE: if (options.validity == 0) options.validity = 1080 * 86400; generate_main(&options); break; case MODE_KEYID: keyid_main(&options); break; case MODE_ATTRIBUTE: if (options.validity == 0) options.validity = 365 * 86400; if(options.attrrule) attribute_rule_main(&options); else attribute_main(&options); break; case MODE_ROLES: roles_main(&options); break; case MODE_VERIFY: verify_main(&options); break; case MODE_DISPLAY: display_main(&options); break; case MODE_VERSION: printf("ABAC/creddy " ABAC_VERSION "\n"); break; default: usage(&options); } return 0; }
void main_cuc (char *filename) { int i, j; char tmp1[256]; char filename_cut[256]; #if 0 /* Select prefix, based on binary program name */ for (i = 0; i < sizeof (filename_cut); i++) { if (isalpha (filename[i])) filename_cut[i] = filename[i]; else { filename_cut[i] = '\0'; break; } } #else strcpy (filename_cut, "cu"); #endif PRINTF ("Entering OpenRISC Custom Unit Compiler command prompt\n"); PRINTF ("Using profile file \"%s\" and memory profile file \"%s\".\n", config.sim.prof_fn, config.sim.mprof_fn); sprintf (tmp1, "%s.log", filename_cut); PRINTF ("Analyzing. (log file \"%s\").\n", tmp1); assert (flog = fopen (tmp1, "wt+")); /* Loads in the specified timings table */ PRINTF ("Using timings from \"%s\" at %s\n", config.cuc.timings_fn, generate_time_pretty (tmp1, config.sim.clkcycle_ps)); load_timing_table (config.cuc.timings_fn); runtime.cuc.cycle_duration = 1000. * config.sim.clkcycle_ps; PRINTF ("Multicycle logic %s, bursts %s, %s memory order.\n", config.cuc.no_multicycle ? "OFF" : "ON", config.cuc.enable_bursts ? "ON" : "OFF", config.cuc.memory_order == MO_NONE ? "no" : config.cuc.memory_order == MO_WEAK ? "weak" : config.cuc.memory_order == MO_STRONG ? "strong" : "exact"); prof_set (1, 0); assert (prof_acquire (config.sim.prof_fn) == 0); if (config.cuc.calling_convention) PRINTF ("Assuming OpenRISC standard calling convention.\n"); /* Try all functions except "total" */ for (i = 0; i < prof_nfuncs - 1; i++) { long orig_time; unsigned long start_addr, end_addr; orig_time = prof_func[i].cum_cycles; start_addr = prof_func[i].addr; /* Extract the function from the binary */ sprintf (tmp1, "%s.bin", prof_func[i].name); end_addr = extract_function (tmp1, start_addr); log ("Testing function %s (%08lx - %08lx)\n", prof_func[i].name, start_addr, end_addr); PRINTF ("Testing function %s (%08lx - %08lx)\n", prof_func[i].name, start_addr, end_addr); func[i] = analyse_function (prof_func[i].name, orig_time, start_addr, end_addr, config.cuc.memory_order, prof_func[i].calls); func_v[i] = 0; } set_func_deps (); while (1) { char *s; wait_command: PRINTF ("(cuc) "); fflush (stdout); wait_command_empty: s = fgets (tmp1, sizeof tmp1, stdin); usleep (100); if (!s) goto wait_command_empty; for (s = tmp1; *s != '\0' && *s != '\n' && *s != '\r'; s++); *s = '\0'; /* quit command */ if (strcmp (tmp1, "q") == 0 || strcmp (tmp1, "quit") == 0) { /* Delete temporary files */ for (i = 0; i < prof_nfuncs - 1; i++) { sprintf (tmp1, "%s.bin", prof_func[i].name); log ("Deleting temporary file %s %s\n", tmp1, remove (tmp1) ? "FAILED" : "OK"); sprintf (tmp1, "%s.bin.bb", prof_func[i].name); log ("Deleting temporary file %s %s\n", tmp1, remove (tmp1) ? "FAILED" : "OK"); } break; /* profile command */ } else if (strcmp (tmp1, "p") == 0 || strcmp (tmp1, "profile") == 0) { int ntime = 0; int size = 0; PRINTF ("-----------------------------------------------------------------------------\n"); PRINTF ("|function name |calls|avg cycles |old%%| max. f. | impr. f.| options |\n"); PRINTF ("|--------------------+-----+------------+----+----------|---------+---------|\n"); for (j = 0; j < prof_nfuncs; j++) { int bestcyc = 0, besti = 0; char tmp[100]; for (i = 0; i < prof_nfuncs; i++) if (prof_func[i].cum_cycles > bestcyc) { bestcyc = prof_func[i].cum_cycles; besti = i; } i = besti; PRINTF ("|%-20s|%5li|%12.1f|%3.0f%%| ", strstrip (tmp, prof_func[i].name, 20), prof_func[i].calls, ((double) prof_func[i].cum_cycles / prof_func[i].calls), (100. * prof_func[i].cum_cycles / prof_cycles)); if (func[i]) { double f = 1.0; if (func_v[i]) { int nt = calc_cycles (func[i]); int s = calc_size (func[i]); f = 1. * func[i]->orig_time / nt; ntime += nt; size += s; } else ntime += prof_func[i].cum_cycles; PRINTF ("%8.1f |%8.1f | %-8s|\n", 1.f * prof_func[i].cum_cycles / func[i]->timings.new_time, f, format_func_options (tmp, func[i])); } else { PRINTF (" N/A | N/A | N/A |\n"); ntime += prof_func[i].cum_cycles; } prof_func[i].cum_cycles = -prof_func[i].cum_cycles; } for (i = 0; i < prof_nfuncs; i++) prof_func[i].cum_cycles = -prof_func[i].cum_cycles; PRINTF ("-----------------------------------------------------------------------------\n"); PRINTF ("Total %i cycles (was %i), total added gates = %i. Speed factor %.1f\n", ntime, prof_cycles, size, 1. * prof_cycles / ntime); /* debug command */ } else if (strncmp (tmp1, "d", 1) == 0 || strncmp (tmp1, "debug", 5) == 0) { sscanf (tmp1, "%*s %i", &cuc_debug); if (cuc_debug < 0) cuc_debug = 0; if (cuc_debug > 9) cuc_debug = 9; /* generate command */ } else if (strcmp (tmp1, "g") == 0 || strcmp (tmp1, "generate") == 0) { /* check for function dependencies */ for (i = 0; i < prof_nfuncs; i++) if (func[i]) func[i]->tmp = func_v[i]; for (i = 0; i < prof_nfuncs; i++) if (func[i]) for (j = 0; j < func[i]->nfdeps; j++) if (!func[i]->fdeps[j] || !func[i]->fdeps[j]->tmp) { PRINTF ("Function %s must be selected for translation (required by %s)\n", prof_func[j].name, prof_func[i].name); goto wait_command; } for (i = 0; i < prof_nfuncs; i++) if (func[i] && func_v[i]) generate_function (func[i], prof_func[i].name, filename_cut); generate_main (prof_nfuncs, func, filename_cut); /* list command */ } else if (strcmp (tmp1, "l") == 0 || strcmp (tmp1, "list") == 0) { /* check for function dependencies */ for (i = 0; i < prof_nfuncs; i++) if (func_v[i]) { PRINTF ("%s\n", prof_func[i].name); } /* selectall command */ } else if (strcmp (tmp1, "sa") == 0 || strcmp (tmp1, "selectall") == 0) { int f; for (f = 0; f < prof_nfuncs; f++) if (func[f]) { func_v[f] = 1; PRINTF ("Function %s selected for translation.\n", prof_func[f].name); } /* select command */ } else if (strncmp (tmp1, "s", 1) == 0 || strncmp (tmp1, "select", 6) == 0) { char tmp[50], ch; int p, o, b, f; p = sscanf (tmp1, "%*s %s %i%c", tmp, &b, &ch); if (p < 1) PRINTF ("Invalid parameters.\n"); else { /* Check if we have valid option */ for (f = 0; f < prof_nfuncs; f++) if (strcmp (prof_func[f].name, tmp) == 0 && func[f]) break; if (f < prof_nfuncs) { if (p == 1) { if (func[f]) { func_v[f] = 1; PRINTF ("Function %s selected for translation.\n", prof_func[f].name); } else PRINTF ("Function %s not suitable for translation.\n", prof_func[f].name); } else { if (!func_v[f]) PRINTF ("Function %s not yet selected for translation.\n", prof_func[f].name); if (p < 3) goto invalid_option; for (o = 0; option_char[o] != '\0' && option_char[o] != ch; o++); if (!option_char[o]) goto invalid_option; if (b < 0 || b >= func[f]->num_bb) goto invalid_option; if (o < 0 || o >= func[f]->bb[b].ntim) goto invalid_option; /* select an option */ func[f]->bb[b].selected_tim = o; if (func[f]->bb[b].tim[o].nshared) { PRINTF ("Option has shared instructions: "); print_shared (func[f], func[f]->bb[b].tim[o].shared, func[f]->bb[b].tim[o].nshared); PRINTF ("\n"); } goto wait_command; invalid_option: PRINTF ("Invalid option.\n"); } } else PRINTF ("Invalid function.\n"); } /* unselect command */ } else if (strncmp (tmp1, "u", 1) == 0 || strncmp (tmp1, "unselect", 8) == 0) { char tmp[50], ch; int p, o, b, f; p = sscanf (tmp1, "%*s %s %i%c", tmp, &b, &ch); if (p < 1) PRINTF ("Invalid parameters.\n"); else { /* Check if we have valid option */ for (f = 0; f < prof_nfuncs; f++) if (strcmp (prof_func[f].name, tmp) == 0 && func[f]) break; if (f < prof_nfuncs) { if (p == 1) { if (func[f]) { func_v[f] = 0; PRINTF ("Function %s unselected for translation.\n", prof_func[f].name); } else PRINTF ("Function %s not suitable for translation.\n", prof_func[f].name); } else { if (p < 3) goto invalid_option; for (o = 0; option_char[o] != '\0' && option_char[o] != ch; o++); if (!option_char[o]) goto invalid_option; if (b < 0 || b >= func[f]->num_bb) goto invalid_option; if (o < 0 || o >= func[f]->bb[b].ntim) goto invalid_option; /* select an option */ func[f]->bb[b].selected_tim = -1; } } else PRINTF ("Invalid function.\n"); } /* options command */ } else if (strcmp (tmp1, "o") == 0 || strcmp (tmp1, "options") == 0) { int any = 0; PRINTF ("Available options:\n"); for (i = 0; i < prof_nfuncs; i++) if (func[i]) { options_cmd (i, func[i]); any = 1; } if (any) PRINTF ("-----------------------------------------------------------------------------\n"); else PRINTF ("Sorry. No available options.\n"); /* Ignore empty string */ } else if (strcmp (tmp1, "") == 0) { /* help command */ } else { if (strcmp (tmp1, "h") != 0 && strcmp (tmp1, "help") != 0) PRINTF ("Unknown command.\n"); PRINTF ("OpenRISC Custom Unit Compiler command prompt\n"); PRINTF ("Available commands:\n"); PRINTF (" h | help displays this help\n"); PRINTF (" q | quit returns to or1ksim prompt\n"); PRINTF (" p | profile displays function profiling\n"); PRINTF (" d | debug # sets debug level (0-9)\n"); PRINTF (" o | options displays available options\n"); PRINTF (" s | select func [option] selects an option/function\n"); PRINTF (" u | unselect func [option] unselects an option/function\n"); PRINTF (" g | generate generates verilog file\n"); PRINTF (" l | list displays selected functions\n"); } } /* Dispose memory */ for (i = 0; i < prof_nfuncs - 1; i++) if (func[i]) free_func (func[i]); fclose (flog); }
void code_generator () { generate_header (); generate_declaration (stream_tab); generate_main (stream_tab, run_tab); }