bool Assembler::assemble(string path){ Assembler* ptr = this; setRootDirectory(parser.filePathToParentDirectory(path)); setProgramName(parser.filePathToFileName(path)); bool invalidateAssembly = false; try{ loadProgramFromFile(programName); splitLabels(); replaceEqv(); extractMacroDefinitions(); replaceMacros(); pseudoInstructionPad(); alignRawProgram(); pseudoInstructionReplace(); replaceLabels(); writeAlignedRawProgramToDisk(rootDirectory + programName + alignedProgramNamePostfix); mapAlignedProgramToVirtualMemory(); builtObjectFileName = rootDirectory + programName + objectNamePostfix; virtualMemory.serialize(builtObjectFileName); }catch(AssemblerException &e){ string message = e.toString(); cout << '\n' << message << '\n'; invalidateAssembly = true; }catch(InvalidTokenException &e){ cout << "ERROR [Assembler::assemble(string fileName)]: HANDLE INVALID TOKEN EXCEPTION IN ASSEMBLER!!!:\t" + e.toString(); getchar(); } if(recoverableExceptions.size() != 0 || invalidateAssembly){ for(int i=0; i<recoverableExceptions.size(); i++){ cout << recoverableExceptions[i].toString() << '\n'; } return false; } return true; }
int main(int argc, char *argv[]) { char **av, fname[STRLEN], *out_fname, *subject_name, *cp ; int ac, nargs, i, n, noint = 0, options ; int msec, minutes, seconds, nsubjects, input ; struct timeb start ; GCA *gca ; MRI *mri_seg, *mri_tmp, *mri_inputs ; TRANSFORM *transform ; LTA *lta; GCA_BOUNDARY *gcab ; Progname = argv[0] ; ErrorInit(NULL, NULL, NULL) ; DiagInit(NULL, NULL, NULL) ; TimerStart(&start) ; parms.use_gradient = 0 ; spacing = 8 ; /* rkt: check for and handle version tag */ nargs = handle_version_option (argc, argv, "$Id: mri_gcab_train.c,v 1.4 2011/03/16 20:23:33 fischl Exp $", "$Name: $"); if (nargs && argc - nargs == 1) exit (0); argc -= nargs; // parse command line args ac = argc ; av = argv ; for ( ; argc > 1 && ISOPTION(*argv[1]) ; argc--, argv++) { nargs = get_option(argc, argv) ; argc -= nargs ; argv += nargs ; } printf("reading gca from %s\n", argv[1]) ; gca = GCAread(argv[1]) ; if (!gca) exit(Gerror) ; if (!strlen(subjects_dir)) /* hasn't been set on command line */ { cp = getenv("SUBJECTS_DIR") ; if (!cp) ErrorExit(ERROR_BADPARM, "%s: SUBJECTS_DIR not defined in environment", Progname); strcpy(subjects_dir, cp) ; if (argc < 4) usage_exit(1) ; } // options parsed. subjects and gca name remaining out_fname = argv[argc-1] ; nsubjects = argc-3 ; for (options = i = 0 ; i < nsubjects ; i++) { if (argv[i+1][0] == '-') { nsubjects-- ; options++ ; } } printf("training on %d subject and writing results to %s\n", nsubjects, out_fname) ; n = 0 ; gcab = GCABalloc(gca, 8, 0, 30, 10, target_label); strcpy(gcab->gca_fname, argv[1]) ; // going through the subject one at a time for (nargs = i = 0 ; i < nsubjects+options ; i++) { subject_name = argv[i+2] ; ////////////////////////////////////////////////////////////// printf("***************************************" "************************************\n"); printf("processing subject %s, %d of %d...\n", subject_name,i+1-nargs, nsubjects); if (stricmp(subject_name, "-NOINT") == 0) { printf("not using intensity information for subsequent subjects...\n"); noint = 1 ; nargs++ ; continue ; } else if (stricmp(subject_name, "-INT") == 0) { printf("using intensity information for subsequent subjects...\n"); noint = 0 ; nargs++ ; continue ; } // reading this subject segmentation sprintf(fname, "%s/%s/mri/%s", subjects_dir, subject_name, seg_dir) ; if (Gdiag & DIAG_SHOW && DIAG_VERBOSE_ON) fprintf(stderr, "Reading segmentation from %s...\n", fname) ; mri_seg = MRIread(fname) ; if (!mri_seg) ErrorExit(ERROR_NOFILE, "%s: could not read segmentation file %s", Progname, fname) ; if ((mri_seg->type != MRI_UCHAR) && (mri_seg->type != MRI_FLOAT)) { ErrorExit (ERROR_NOFILE, "%s: segmentation file %s is not type UCHAR or FLOAT", Progname, fname) ; } if (binarize) { int j ; for (j = 0 ; j < 256 ; j++) { if (j == binarize_in) MRIreplaceValues(mri_seg, mri_seg, j, binarize_out) ; else MRIreplaceValues(mri_seg, mri_seg, j, 0) ; } } if (insert_fname) { MRI *mri_insert ; sprintf(fname, "%s/%s/mri/%s", subjects_dir, subject_name, insert_fname) ; mri_insert = MRIread(fname) ; if (mri_insert == NULL) ErrorExit(ERROR_NOFILE, "%s: could not read volume from %s for insertion", Progname, insert_fname) ; MRIbinarize(mri_insert, mri_insert, 1, 0, insert_label) ; MRIcopyLabel(mri_insert, mri_seg, insert_label) ; MRIfree(&mri_insert) ; } replaceLabels(mri_seg) ; MRIeraseBorderPlanes(mri_seg, 1) ; for (input = 0 ; input < gca->ninputs ; input++) { //////////// set the gca type ////////////////////////////// // is this T1/PD training? // how can we allow flash data training ??????? // currently checks the TE, TR, FA to be the same for all inputs // thus we cannot allow flash data training. //////////////////////////////////////////////////////////// sprintf(fname, "%s/%s/mri/%s", subjects_dir, subject_name,input_names[input]); if (DIAG_VERBOSE_ON) printf("reading co-registered input from %s...\n", fname) ; fprintf(stderr, " reading input %d: %s\n", input, fname); mri_tmp = MRIread(fname) ; if (!mri_tmp) ErrorExit (ERROR_NOFILE, "%s: could not read image from file %s", Progname, fname) ; // input check 1 if (getSliceDirection(mri_tmp) != MRI_CORONAL) { ErrorExit (ERROR_BADPARM, "%s: must be in coronal direction, but it is not\n", fname); } // input check 2 if (mri_tmp->xsize != 1 || mri_tmp->ysize != 1 || mri_tmp->zsize != 1) { ErrorExit (ERROR_BADPARM, "%s: must have 1mm voxel size, but have (%f, %f, %f)\n", fname, mri_tmp->xsize, mri_tmp->ysize, mri_tmp->ysize); } // input check 3 is removed. now we can handle c_(ras) != 0 case // input check 4 if (i == 0) { TRs[input] = mri_tmp->tr ; FAs[input] = mri_tmp->flip_angle ; TEs[input] = mri_tmp->te ; } else if (!FEQUAL(TRs[input],mri_tmp->tr) || !FEQUAL(FAs[input],mri_tmp->flip_angle) || !FEQUAL(TEs[input], mri_tmp->te)) ErrorExit (ERROR_BADPARM, "%s: subject %s input volume %s: sequence parameters " "(%2.1f, %2.1f, %2.1f)" "don't match other inputs (%2.1f, %2.1f, %2.1f)", Progname, subject_name, fname, mri_tmp->tr, DEGREES(mri_tmp->flip_angle), mri_tmp->te, TRs[input], DEGREES(FAs[input]), TEs[input]) ; // first time do the following if (input == 0) { int nframes = gca->ninputs ; /////////////////////////////////////////////////////////// mri_inputs = MRIallocSequence(mri_tmp->width, mri_tmp->height, mri_tmp->depth, mri_tmp->type, nframes) ; if (!mri_inputs) ErrorExit (ERROR_NOMEMORY, "%s: could not allocate input volume %dx%dx%dx%d", mri_tmp->width, mri_tmp->height, mri_tmp->depth,nframes) ; MRIcopyHeader(mri_tmp, mri_inputs) ; } // -mask option //////////////////////////////////////////// if (mask_fname) { MRI *mri_mask ; sprintf(fname, "%s/%s/mri/%s", subjects_dir, subject_name, mask_fname); printf("reading volume %s for masking...\n", fname) ; mri_mask = MRIread(fname) ; if (!mri_mask) ErrorExit(ERROR_NOFILE, "%s: could not open mask volume %s.\n", Progname, fname) ; MRImask(mri_tmp, mri_mask, mri_tmp, 0, 0) ; MRIfree(&mri_mask) ; } MRIcopyFrame(mri_tmp, mri_inputs, 0, input) ; MRIfree(&mri_tmp) ; }// end of inputs per subject ///////////////////////////////////////////////////////// // xform_name is given, then we can use the consistent c_(r,a,s) for gca ///////////////////////////////////////////////////////// if (xform_name) { // we read talairach.xfm which is a RAS-to-RAS sprintf(fname, "%s/%s/mri/transforms/%s", subjects_dir, subject_name, xform_name) ; if (Gdiag & DIAG_SHOW && DIAG_VERBOSE_ON) printf("INFO: reading transform file %s...\n", fname); if (!FileExists(fname)) { fprintf(stderr,"ERROR: cannot find transform file %s\n",fname); exit(1); } transform = TransformRead(fname); if (!transform) ErrorExit(ERROR_NOFILE, "%s: could not read transform from file %s", Progname, fname); modify_transform(transform, mri_inputs, gca); // Here we do 2 things // 1. modify gca direction cosines to // that of the transform destination (both linear and non-linear) // 2. if ras-to-ras transform, // then change it to vox-to-vox transform (linear case) // modify transform to store inverse also TransformInvert(transform, mri_inputs) ; // verify inverse lta = (LTA *) transform->xform; } else { GCAreinit(mri_inputs, gca); // just use the input value, since dst = src volume transform = TransformAlloc(LINEAR_VOXEL_TO_VOXEL, NULL) ; } //////////////////////////////////////////////////////////////////// // train gca //////////////////////////////////////////////////////////////////// // segmentation is seg volume // inputs is the volumes of all inputs // transform is for this subject // noint is whether to use intensity information or not GCABtrain(gcab, mri_inputs, mri_seg, transform, target_label) ; MRIfree(&mri_seg) ; MRIfree(&mri_inputs) ; TransformFree(&transform) ; } GCABcompleteTraining(gcab) ; if (smooth > 0) { printf("regularizing conditional densities with smooth=%2.2f\n", smooth) ; GCAregularizeConditionalDensities(gca, smooth) ; } if (navgs) { printf("applying mean filter %d times to conditional densities\n", navgs) ; GCAmeanFilterConditionalDensities(gca, navgs) ; } printf("writing trained GCAB to %s...\n", out_fname) ; if (GCABwrite(gcab, out_fname) != NO_ERROR) ErrorExit (ERROR_BADFILE, "%s: could not write gca to %s", Progname, out_fname) ; if (Gdiag & DIAG_WRITE && DIAG_VERBOSE_ON) { MRI *mri ; mri = GCAbuildMostLikelyVolume(gca, NULL) ; MRIwrite(mri, "m.mgz") ; MRIfree(&mri) ; } if (histo_fname) { FILE *fp ; int histo_counts[10000], xn, yn, zn, max_count ; GCA_NODE *gcan ; memset(histo_counts, 0, sizeof(histo_counts)) ; fp = fopen(histo_fname, "w") ; if (!fp) ErrorExit(ERROR_BADFILE, "%s: could not open histo file %s", Progname, histo_fname) ; max_count = 0 ; for (xn = 0 ; xn < gca->node_width; xn++) { for (yn = 0 ; yn < gca->node_height ; yn++) { for (zn = 0 ; zn < gca->node_depth ; zn++) { gcan = &gca->nodes[xn][yn][zn] ; if (gcan->nlabels < 1) continue ; if (gcan->nlabels == 1 && IS_UNKNOWN(gcan->labels[0])) continue ; histo_counts[gcan->nlabels]++ ; if (gcan->nlabels > max_count) max_count = gcan->nlabels ; } } } max_count = 20 ; for (xn = 1 ; xn < max_count ; xn++) fprintf(fp, "%d %d\n", xn, histo_counts[xn]) ; fclose(fp) ; } GCAfree(&gca) ; msec = TimerStop(&start) ; seconds = nint((float)msec/1000.0f) ; minutes = seconds / 60 ; seconds = seconds % 60 ; printf("classifier array training took %d minutes" " and %d seconds.\n", minutes, seconds) ; exit(0) ; return(0) ; }
int main(int argc, char *argv[]) { char **av, fname[STRLEN], *out_fname, *subject_name, *cp, *tp1_name, *tp2_name ; char s1_name[STRLEN], s2_name[STRLEN], *sname ; int ac, nargs, i, n, options, max_index ; int msec, minutes, seconds, nsubjects, input ; struct timeb start ; MRI *mri_seg, *mri_tmp, *mri_in ; TRANSFORM *transform ; // int counts ; int t; RANDOM_FOREST *rf = NULL ; GCA *gca = NULL ; Progname = argv[0] ; ErrorInit(NULL, NULL, NULL) ; DiagInit(NULL, NULL, NULL) ; TimerStart(&start) ; parms.width = parms.height = parms.depth = DEFAULT_VOLUME_SIZE ; parms.ntrees = 10 ; parms.max_depth = 10 ; parms.wsize = 1 ; parms.training_size = 100 ; parms.training_fraction = .5 ; parms.feature_fraction = 1 ; /* rkt: check for and handle version tag */ nargs = handle_version_option (argc, argv, "$Id: mri_rf_long_train.c,v 1.5 2012/06/15 12:22:28 fischl Exp $", "$Name: $"); if (nargs && argc - nargs == 1) exit (0); argc -= nargs; // parse command line args ac = argc ; av = argv ; for ( ; argc > 1 && ISOPTION(*argv[1]) ; argc--, argv++) { nargs = get_option(argc, argv) ; argc -= nargs ; argv += nargs ; } if (!strlen(subjects_dir)) /* hasn't been set on command line */ { cp = getenv("SUBJECTS_DIR") ; if (!cp) ErrorExit(ERROR_BADPARM, "%s: SUBJECTS_DIR not defined in environment", Progname); strcpy(subjects_dir, cp) ; } if (argc < 3) usage_exit(1) ; // options parsed. subjects, tp1 and tp2 and rf name remaining out_fname = argv[argc-1] ; nsubjects = (argc-2)/3 ; for (options = i = 0 ; i < nsubjects ; i++) { if (argv[i+1][0] == '-') { nsubjects-- ; options++ ; } } printf("training on %d subject and writing results to %s\n", nsubjects, out_fname) ; // rf_inputs can be T1, PD, ...per subject if (parms.nvols == 0) parms.nvols = ninputs ; /* gca reads same # of inputs as we read from command line - not the case if we are mapping to flash */ n = 0 ; ////////////////////////////////////////////////////////////////// // set up gca direction cosines, width, height, depth defaults gca = GCAread(gca_name) ; if (gca == NULL) ErrorExit(ERROR_NOFILE, "%s: could not read GCA from %s", Progname, gca_name) ; ///////////////////////////////////////////////////////////////////////// // weird way options and subject name are mixed here ///////////////////////////////////////////////////////// // first calculate mean //////////////////////////////////////////////////////// // going through the subject one at a time max_index = nsubjects+options ; nargs = 0 ; mri_in = NULL ; #ifdef HAVE_OPENMP subject_name = NULL ; sname = NULL ; t = 0 ; // counts = 0 ; would be private input = 0 ; transform = NULL ; tp1_name = tp2_name = NULL ; mri_tmp = mri_seg = NULL ; #pragma omp parallel for firstprivate(tp1_name, tp2_name, mri_in,mri_tmp, input, xform_name, transform, subjects_dir, force_inputs, conform, Progname, mri_seg, subject_name, s1_name, s2_name, sname, t, fname) shared(mri_inputs, transforms, mri_segs,argv) schedule(static,1) #endif for (i = 0 ; i < max_index ; i++) { subject_name = argv[3*i+1] ; tp1_name = argv[3*i+2] ; tp2_name = argv[3*i+3] ; sprintf(s1_name, "%s_%s.long.%s_base", subject_name, tp1_name, subject_name) ; sprintf(s2_name, "%s_%s.long.%s_base", subject_name, tp2_name, subject_name) ; ////////////////////////////////////////////////////////////// printf("***************************************" "************************************\n"); printf("processing subject %s, %d of %d (%s and %s)...\n", subject_name,i+1-nargs, nsubjects, s1_name,s2_name); for (t = 0 ; t < 2 ; t++) { sname = t == 0 ? s1_name : s2_name; // reading this subject segmentation sprintf(fname, "%s/%s/mri/%s", subjects_dir, sname, seg_dir) ; if (Gdiag & DIAG_SHOW && DIAG_VERBOSE_ON) fprintf(stderr, "Reading segmentation from %s...\n", fname) ; mri_seg = MRIread(fname) ; if (!mri_seg) ErrorExit(ERROR_NOFILE, "%s: could not read segmentation file %s", Progname, fname) ; if ((mri_seg->type != MRI_UCHAR) && (make_uchar != 0)) { MRI *mri_tmp ; mri_tmp = MRIchangeType(mri_seg, MRI_UCHAR, 0, 1,1); MRIfree(&mri_seg) ; mri_seg = mri_tmp ; } if (wmsa_fname) { MRI *mri_wmsa ; sprintf(fname, "%s/%s/mri/%s", subjects_dir, sname, wmsa_fname) ; printf("reading WMSA labels from %s...\n", fname) ; mri_wmsa = MRIread(fname) ; if (mri_wmsa == NULL) ErrorExit(ERROR_NOFILE, "%s: could not read WMSA file %s", fname) ; MRIbinarize(mri_wmsa, mri_wmsa, 1, 0, WM_hypointensities) ; MRIcopyLabel(mri_wmsa, mri_seg, WM_hypointensities) ; lateralize_hypointensities(mri_seg) ; if (Gdiag & DIAG_WRITE && DIAG_VERBOSE_ON ) { char s[STRLEN] ; sprintf(s, "%s/%s/mri/seg_%s", subjects_dir, subject_name, wmsa_fname) ; MRIwrite(mri_seg, s) ; } } if (binarize) { int j ; for (j = 0 ; j < 256 ; j++) { if (j == binarize_in) MRIreplaceValues(mri_seg, mri_seg, j, binarize_out) ; else MRIreplaceValues(mri_seg, mri_seg, j, 0) ; } } if (insert_fname) { MRI *mri_insert ; sprintf(fname, "%s/%s/mri/%s", subjects_dir, subject_name, insert_fname) ; mri_insert = MRIread(fname) ; if (mri_insert == NULL) ErrorExit(ERROR_NOFILE, "%s: could not read volume from %s for insertion", Progname, insert_fname) ; MRIbinarize(mri_insert, mri_insert, 1, 0, insert_label) ; MRIcopyLabel(mri_insert, mri_seg, insert_label) ; MRIfree(&mri_insert) ; } replaceLabels(mri_seg) ; MRIeraseBorderPlanes(mri_seg, 1) ; //////////////////////////////////////////////////////////// if (DIAG_VERBOSE_ON) fprintf(stderr, "Gather all input volumes for the subject %s.\n", subject_name); // inputs must be coregistered // note that inputs are T1, PD, ... per subject (same TE, TR, FA) for (input = 0 ; input < ninputs ; input++) { //////////// set the gca type ////////////////////////////// // is this T1/PD training? // how can we allow flash data training ??????? // currently checks the TE, TR, FA to be the same for all inputs // thus we cannot allow flash data training. //////////////////////////////////////////////////////////// sprintf(fname, "%s/%s/mri/%s", subjects_dir, sname,input_names[input]); if (DIAG_VERBOSE_ON) printf("reading co-registered input from %s...\n", fname) ; fprintf(stderr, " reading input %d: %s\n", input, fname); mri_tmp = MRIread(fname) ; if (!mri_tmp) ErrorExit (ERROR_NOFILE, "%s: could not read image from file %s", Progname, fname) ; // input check 1 if (getSliceDirection(mri_tmp) != MRI_CORONAL) { ErrorExit (ERROR_BADPARM, "%s: must be in coronal direction, but it is not\n", fname); } // input check 2 if (conform && (mri_tmp->xsize != 1 || mri_tmp->ysize != 1 || mri_tmp->zsize != 1)) { ErrorExit (ERROR_BADPARM, "%s: must have 1mm voxel size, but have (%f, %f, %f)\n", fname, mri_tmp->xsize, mri_tmp->ysize, mri_tmp->ysize); } // input check 3 is removed. now we can handle c_(ras) != 0 case // input check 4 if (i == 0) { TRs[input] = mri_tmp->tr ; FAs[input] = mri_tmp->flip_angle ; TEs[input] = mri_tmp->te ; } else if ((force_inputs == 0) && (!FEQUAL(TRs[input],mri_tmp->tr) || !FEQUAL(FAs[input],mri_tmp->flip_angle) || !FEQUAL(TEs[input], mri_tmp->te))) ErrorExit (ERROR_BADPARM, "%s: subject %s input volume %s: sequence parameters " "(%2.1f, %2.1f, %2.1f)" "don't match other inputs (%2.1f, %2.1f, %2.1f)", Progname, subject_name, fname, mri_tmp->tr, DEGREES(mri_tmp->flip_angle), mri_tmp->te, TRs[input], DEGREES(FAs[input]), TEs[input]) ; // first time do the following if (input == 0) { int nframes = ninputs ; /////////////////////////////////////////////////////////// mri_in = MRIallocSequence(mri_tmp->width, mri_tmp->height, mri_tmp->depth, mri_tmp->type, nframes) ; if (!mri_in) ErrorExit (ERROR_NOMEMORY, "%s: could not allocate input volume %dx%dx%dx%d", mri_tmp->width, mri_tmp->height, mri_tmp->depth,nframes) ; MRIcopyHeader(mri_tmp, mri_in) ; } // -mask option //////////////////////////////////////////// if (mask_fname) { MRI *mri_mask ; sprintf(fname, "%s/%s/mri/%s", subjects_dir, subject_name, mask_fname); printf("reading volume %s for masking...\n", fname) ; mri_mask = MRIread(fname) ; if (!mri_mask) ErrorExit(ERROR_NOFILE, "%s: could not open mask volume %s.\n", Progname, fname) ; MRImask(mri_tmp, mri_mask, mri_tmp, 0, 0) ; MRIfree(&mri_mask) ; } MRIcopyFrame(mri_tmp, mri_in, 0, input) ; MRIfree(&mri_tmp) ; }// end of inputs per subject ///////////////////////////////////////////////////////// // xform_name is given, then we can use the consistent c_(r,a,s) for gca ///////////////////////////////////////////////////////// if (xform_name) { // we read talairach.xfm which is a RAS-to-RAS sprintf(fname, "%s/%s/mri/transforms/%s", subjects_dir, sname, xform_name) ; if (Gdiag & DIAG_SHOW && DIAG_VERBOSE_ON) printf("INFO: reading transform file %s...\n", fname); if (!FileExists(fname)) { fprintf(stderr,"ERROR: cannot find transform file %s\n",fname); exit(1); } transform = TransformRead(fname); if (!transform) ErrorExit(ERROR_NOFILE, "%s: could not read transform from file %s", Progname, fname); // modify_transform(transform, mri_in, gca); // Here we do 2 things // 1. modify gca direction cosines to // that of the transform destination (both linear and non-linear) // 2. if ras-to-ras transform, // then change it to vox-to-vox transform (linear case) // modify transform to store inverse also TransformInvert(transform, mri_in) ; } else { // GCAreinit(mri_in, gca); // just use the input value, since dst = src volume transform = TransformAlloc(LINEAR_VOXEL_TO_VOXEL, NULL) ; } ///////////////////////////////////////////////////////// if (do_sanity_check) { // conduct a sanity check of particular labels, most importantly // hippocampus, that such labels do not exist in talairach coords // where they are known not to belong (indicating a bad manual edit) int errs = check(mri_seg, subjects_dir, subject_name); if (errs) { printf( "ERROR: mri_ca_train: possible bad training data! subject:\n" "\t%s/%s\n\n", subjects_dir, subject_name); fflush(stdout) ; sanity_check_badsubj_count++; } } mri_segs[i][t] = mri_seg ; mri_inputs[i][t] = mri_in ; transforms[i][t] = transform ; } } rf = train_rforest(mri_inputs, mri_segs, transforms, nsubjects, gca, &parms, wm_thresh,wmsa_whalf, 2) ; printf("writing random forest to %s\n", out_fname) ; if (RFwrite(rf, out_fname) != NO_ERROR) ErrorExit (ERROR_BADFILE, "%s: could not write rf to %s", Progname, out_fname) ; msec = TimerStop(&start) ; seconds = nint((float)msec/1000.0f) ; minutes = seconds / 60 ; seconds = seconds % 60 ; printf("classifier array training took %d minutes and %d seconds.\n", minutes, seconds) ; exit(0) ; return(0) ; }
void closeAssemblyOutput(char const *path) { char *pos; char labelBuf[6]; int unknownLength = strlen(UNKNOWN_ADDRESS); size_t length; callFunction(getFunction("main"), 0, NULL); char *buf2; asprintf(&buf2, "%s%s", assemblyBuffer, globalBuffer); free(assemblyBuffer); free(globalBuffer); assemblyBuffer = buf2; pos = assemblyBuffer; length = sprintf(labelBuf, "%d", instructionsCount()); while((pos = strstr(pos, "EOC"UNKNOWN_ADDRESS)) != NULL) { stringReplaceWithShorter(pos, 3 + unknownLength, labelBuf, length); } pos = assemblyBuffer; length = sprintf(labelBuf, "%d", instructionsCount() + globalCount); while((pos = strstr(pos, "EOF"UNKNOWN_ADDRESS)) != NULL) { stringReplaceWithShorter(pos, 3 + unknownLength, labelBuf, length); } pos = assemblyBuffer; while((pos = strstr(pos, "FUN"UNKNOWN_ADDRESS)) != NULL) { int functionIndex; int charatersConsumed; sscanf(pos + strlen("FUN"UNKNOWN_ADDRESS), "%d%n", &functionIndex, &charatersConsumed); function_t *function = getFunctionWithIndex(functionIndex); if(function->address == 0) { yyerror("La fonction %s n'a pas été définie !\n", function->name); } length = sprintf(labelBuf, "%d", function->address); stringReplaceWithShorter(pos, strlen("FUN"UNKNOWN_ADDRESS) + charatersConsumed, labelBuf, length); } replaceLabels(assemblyBuffer, "\n"UNKNOWN_IF_PREFIX, &ifLabels, labelBuf, unknownLength); replaceLabels(assemblyBuffer, "\n"UNKNOWN_LOOP_PREFIX, &loopLabels, labelBuf, unknownLength); { // On dépile la stack des goto dans la liste pour pouvoir utiliser le même algo que pour les if et while label_t newTab[MAX_SIZE]; for(int i = 0; i < gotoLabels.size; ++i) { free(gotoLabels.labels[i].name); gotoLabels.labels[i].name = NULL; newTab[i] = gotoLabels.labels[i]; } for(int i = 0; i < gotoLabels.stackSize; ++i) { gotoLabels.labels[i] = newTab[gotoLabels.stack[i] - gotoLabels.labels]; } gotoLabels.size = gotoLabels.stackSize; } replaceLabels(assemblyBuffer, "\n"UNKNOWN_GOTO_PREFIX, &gotoLabels, labelBuf, unknownLength); #ifdef STRIP_COMMENTS pos = assemblyBuffer; while((pos = strstr(pos, ";")) != NULL) { if(pos[-1] == ' ') --pos; char *pos2 = strstr(pos, "\n"); size_t length = pos2 - pos; stringReplaceWithShorter(pos, length, "", 0); } #endif if(errorsOccurred()) { fclose(output); remove(path); } else { fputs(assemblyBuffer, output); fclose(output); } free(assemblyBuffer); for(int i = 0; i < MAX_SIZE; ++i) { free(returnAddressStack.address[i]); } }