/* * Commit the just defined function for use. * This replaces any existing definition for the function. * This should only be called for normal user-defined functions. */ void endfunc(void) { register FUNC *fp; /* function just finished */ unsigned long size; /* size of just created function */ unsigned long index; if (oldop != OP_RETURN) { addop(OP_UNDEF); addop(OP_RETURN); } checklabels(); if (errorcount) { printf("\"%s\": %ld error%s\n", newname, errorcount, ((errorcount == 1) ? "" : "s")); return; } size = funcsize(curfunc->f_opcodecount); fp = (FUNC *) malloc(size); if (fp == NULL) { math_error("Cannot commit function"); /*NOTREACHED*/ } memcpy((char *) fp, (char *) curfunc, size); if (curfunc != functemplate) free(curfunc); if (newname[0] != '*' && (conf->traceflags & TRACE_FNCODES)) { dumpnames = TRUE; for (size = 0; size < fp->f_opcodecount; ) { printf("%ld: ", (long)size); size += dumpop(&fp->f_opcodes[size]); } } if ((inputisterminal() && conf->resource_debug & RSCDBG_STDIN_FUNC) || (!inputisterminal() && conf->resource_debug & RSCDBG_FILE_FUNC)) { printf("%s(", newname); for (index = 0; index < fp->f_paramcount; index++) { if (index) putchar(','); printf("%s", paramname(index)); } printf(") "); if (functions[newindex]) printf("re"); printf("defined\n"); } if (functions[newindex]) { freenumbers(functions[newindex]); free(functions[newindex]); } functions[newindex] = fp; }
/* * Add an opcode and and two integer arguments to the current function * being compiled. */ void addoptwo(long op, long arg1, long arg2) { addop(op); curfunc->f_opcodes[curfunc->f_opcodecount++] = arg1; curfunc->f_opcodes[curfunc->f_opcodecount++] = arg2; }
/** * 添加函数 * 主要添加三条指令,一条将该字节码从表中导入运行栈中,第二条提示栈顶是一个 * 字节码对象,第三条指令根据函数名存入该字节码对象 */ int32_t CodeBuilder::make_function( std::string fun_name,CodeObject* opcode ) { StrTable::iterator iter = names.find(fun_name); int32_t arg = names.size(); if (iter == names.end()) { names.insert(std::make_pair(fun_name,arg)); } else { arg = iter->second; } //上面得到了name的ID,现在插入函数的字节码 CodeObjectEntry::iterator iter_object = codes.find(arg); if (iter_object != codes.end()) { //预留,这里已经出错了,函数重定义 assert(0); //到这里弹出 return 0; } codes.insert(std::make_pair(arg,opcode)); addop_i(LOAD_OBJECT,arg); addop(MAKE_FUNCTION); addop_name(STORE_NAME,fun_name); return 1; }
int main (int argc, char **argv) { int index; mu_url_t url; MU_APP_INIT_NLS (); mh_argp_init (program_version); mh_argp_parse (&argc, &argv, 0, options, mh_option, args_doc, doc, opt_handler, NULL, &index); if (!oplist) addop ("date", comp_date); if (action == ACTION_LIST && mh_format_parse (format_str, &format)) { mu_error (_("Bad format string")); exit (1); } mbox = mh_open_folder (mh_current_folder (), 0); mu_mailbox_get_url (mbox, &url); mbox_path = mu_url_to_string (url); if (memcmp (mbox_path, "mh:", 3) == 0) mbox_path += 3; argc -= index; argv += index; mh_msgset_parse (mbox, &msgset, argc, argv, "all"); sort (mbox, msgset); return 0; }
/* * Add an opcode and a character pointer to the function being compiled. */ void addopptr(long op, char *ptr) { char **ptraddr; addop(op); ptraddr = (char **) &curfunc->f_opcodes[curfunc->f_opcodecount]; *ptraddr = ptr; curfunc->f_opcodecount += PTR_SIZE; }
/* * Add an opcode and an index and an argument count for a function call. */ void addopfunction(long op, long index, int count) { long newop; if ((op == OP_CALL) && ((newop = builtinopcode(index)) != OP_NOP)) { if ((newop == OP_SETCONFIG) && (count == 1)) newop = OP_GETCONFIG; if ((newop == OP_SETEPSILON) && (count == 0)) newop = OP_GETEPSILON; if ((newop == OP_ABS) && (count == 1)) addop(OP_GETEPSILON); addop(newop); return; } addop(op); curfunc->f_opcodes[curfunc->f_opcodecount++] = index; curfunc->f_opcodes[curfunc->f_opcodecount++] = count; }
void seprime() { int incoming = addop(); if(incoming == 1) { term(); seprime(); } else return; }
/* * Add an opcode and and one integer argument to the current function * being compiled. */ void addopone(long op, long arg) { if (op == OP_DEBUG) { if ((conf->traceflags & TRACE_NODEBUG) || (arg == debugline)) return; debugline = arg; if (oldop == OP_DEBUG) { curfunc->f_opcodes[curfunc->f_opcodecount - 1] = arg; return; } } addop(op); curfunc->f_opcodes[curfunc->f_opcodecount] = arg; curfunc->f_opcodecount++; }
void postfix(char *input){ int i,valid=TRUE; for(i=0;input[i]!='\0'&&valid;i++){ //printf("%c\n",input[i]); //if(input[i]=='0'||input[i]=='1'||input[i]=='2'||input[i]=='3'||input[i]=='4'||input[i]=='5'||input[i]=='6'||input[i]=='7'||input[i]=='8'||input[i]=='9'){ ////push(start,(int)input[i]); //printf("%d",input[i]); //debug //} //else if(input[i]=='+'||input[i]=='-'||input[i]=='*'||input[i]=='/'||input[i]=='$'){ //printf("%c",input[i]); //debug //} //else{ //printf("\nInvalid String !\n"); //} switch(input[i]){ case '0': push(&(start->top),0); break; case '1': push(&(start->top),1); break; case '2': push(&(start->top),2); break; case '3': push(&(start->top),3); break; case '4': push(&(start->top),4); break; case '5': push(&(start->top),5); break; case '6': push(&(start->top),6); break; case '7': push(&(start->top),7); break; case '8': push(&(start->top),8); break; case '9': push(&(start->top),9); break; /* case '0': push(start,0); break; case '1': push(start,1); break; case '2': push(start,2); break; case '3': push(start,3); break; case '4': push(start,4); break; case '5': push(start,5); break; case '6': push(start,6); break; case '7': push(start,7); break; case '8': push(start,8); break; case '9': push(start,9); break; */ case '+': addop(); break; case '-': subop(); break; case '*': mulop(); break; case '/': divop(); break; case '$': expop(); break; default: printf("\nInvalid String !\n"); valid=FALSE; break; }//end switch }//end for }
int seprime() { if(addop()) { if(term()) { if(seprime()) return 1; else return 0; } else return 0; } else return 1; }
//*********Main Function************** int main(){ int i,valid=TRUE; start=getNode(); start->info=0; start->next=NULL; printf("\nEnter Postfix String: "); gets(input); for(i=0;input[i]!='\0'&&valid;i++){ //printf("%c\n",input[i]); //if(input[i]=='0'||input[i]=='1'||input[i]=='2'||input[i]=='3'||input[i]=='4'||input[i]=='5'||input[i]=='6'||input[i]=='7'||input[i]=='8'||input[i]=='9'){ ////push(start,(int)input[i]); //printf("%d",input[i]); //debug //} //else if(input[i]=='+'||input[i]=='-'||input[i]=='*'||input[i]=='/'||input[i]=='$'){ //printf("%c",input[i]); //debug //} //else{ //printf("\nInvalid String !\n"); //} switch(input[i]){ case '0': push(start,0); break; case '1': push(start,1); break; case '2': push(start,2); break; case '3': push(start,3); break; case '4': push(start,4); break; case '5': push(start,5); break; case '6': push(start,6); break; case '7': push(start,7); break; case '8': push(start,8); break; case '9': push(start,9); break; case '+': addop(); break; case '-': subop(); break; case '*': mulop(); break; case '/': divop(); break; case '$': expop(); break; default: printf("\nInvalid String !\n"); valid=FALSE; break; }//end switch }//end for printf("\nValue of expression %s\t=",input); display(start->next); printf("\n"); }//end main
int expression() { int sentinel = 0; // fail by default if (primary()) { sentinel = 1; while (addop()) { if (primary()) { sentinel = 1; } else { printf("Expected Primary\n"); sentinel = 0; } } } else { printf("Expecting primary\n"); } return sentinel; }
/** * 回填CFG */ void CodeBuilder::back_pach() { if (curblock->b_return) { next_block(); //python在结束的时候添加了NONE,这里没有 addop(RETURN_VALUE); } int32_t nblocks = 0; Basicblock *b, *entryblock; entryblock = NULL; //遍历所有的block,并计数,最终entry是第一个被分配的块地址 //block现在是最末尾的块,所以向上遍历是对的 for (b = block; b != NULL; b = b->b_list) { nblocks++; entryblock = b; } Assembler a(nblocks); //从entryblock中进入完成向反序表中添加 a.dfs(entryblock); /* Can't modify the bytecode after computing jump offsets. */ //从最末尾开始 a.jump_offsets(block); //到这里将所有的跳转地址都填好了,现在生成指令 code_object = a.makecode(); //将剩余的全局信息全部拷贝过去 code_object->set_consts(consts); code_object->set_names(names); code_object->set_varname(varnames); code_object->set_interger(intergers); code_object->set_floats(floats); code_object->set_codes(codes); code_object->set_var(vars); return; }
static error_t opt_handler (int key, char *arg, struct argp_state *state) { switch (key) { case ARG_FOLDER: mh_set_current_folder (arg); break; case ARG_DATEFIELD: addop (arg, comp_date); break; case ARG_NUMFIELD: addop (arg, comp_number); break; case ARG_NODATEFIELD: remop (comp_date); break; case ARG_TEXTFIELD: addop (arg, comp_text); break; case ARG_NOTEXTFIELD: remop (comp_text); break; case ARG_LIMIT: limit = strtoul (arg, NULL, 0); break; case ARG_NOLIMIT: limit = -1; break; case ARG_VERBOSE: if (!arg || mu_isalpha (arg[0])) verbose = is_true (arg); else verbose = arg[0] - '0'; break; case ARG_NOVERBOSE: verbose = 0; break; case ARG_FORM: mh_read_formfile (arg, &format_str); break; case ARG_FORMAT: format_str = arg; break; case ARG_REORDER: action = ACTION_REORDER; break; case ARG_LIST: action = ACTION_LIST; break; case ARG_DRY_RUN: action = ACTION_DRY_RUN; if (!verbose) verbose = 1; break; case ARG_SHELL: case ARG_QUICKSORT: algorithm = key; break; case ARG_LICENSE: mh_license (argp_program_version); break; default: return ARGP_ERR_UNKNOWN; } return 0; }
void opTick() { repair(10); addop(L_SHIELDS, 10); }
/* * Add a jump-type opcode and a label to the function being compiled. * * given: * label label to be added */ void addoplabel(long op, LABEL *label) { addop(op); uselabel(label); }
int main(int argc, char **argv) { int curarg; osfildef *fpin; osfildef *fpout; char tmpfile[OSFNMAX + 1]; char inbuf[OSFNMAX + 1]; char *p; char *infile; char buf[128]; opdef *oplist = (opdef *)0; opctxdef opctx; int do_create = FALSE; /* print main banner */ rscptf("TADS Resource Manager version 2.2.4\n"); rscptf("Copyright (c) 1992, 1999 by Michael J. Roberts. "); rscptf("All Rights Reserved.\n"); if (argc < 2) usage(); /* set default parsing options */ opctx.restype = RESTYPE_DFLT; opctx.flag = OPFADD | OPFDEL; opctx.doing_type = FALSE; /* scan file options (these come before the filename) */ for (curarg = 1 ; curarg < argc ; ++curarg) { /* check if it's an option - if not, stop looking */ if (argv[curarg][0] != '-') break; /* check the option */ if (!stricmp(argv[curarg], "-create")) { /* note that we want to create the file */ do_create = TRUE; } else { rscptf("unrecognized file option \"%s\"", argv[curarg]); errexit("", 1); } } /* get the file name */ infile = argv[curarg++]; strcpy(inbuf, infile); os_defext(inbuf, "gam"); /* open the file for reading, unless we're creating a new file */ if (do_create) { /* creating - we have no input file */ fpin = 0; } else if ((fpin = osfoprb(inbuf, OSFTGAME)) == 0) { /* * not creating, so the file must already exist - it doesn't, so * issue an error and quit */ errexit("unable to open resource file", 1); } /* * if no operations are desired, and we're not creating a new file, * just list the existing file's contents and quit */ if (curarg == argc && fpin != 0) { rscproc(fpin, (osfildef *)0, 0); osfcls(fpin); os_term(OSEXSUCC); } /* * Create an output file. If we're creating a new file, create the * file named on the command line; otherwise, create a temporary * file that we'll write to while working and then rename to the * original input filename after we've finished with the original * input file. */ if (do_create) { /* create the new file */ if ((fpout = osfopwb(inbuf, OSFTGAME)) == 0) errexit("unable to create file", 1); /* report the creation */ rscptf("\nFile created.\n"); } else { /* generate a temporary filename */ strcpy(tmpfile, inbuf); for (p = tmpfile + strlen(tmpfile) ; p > tmpfile && *(p-1) != ':' && *(p-1) != '\\' && *(p-1) != '/' ; --p); strcpy(p, "$TADSRSC.TMP"); /* open the temporary file */ if ((fpout = osfopwb(tmpfile, OSFTGAME)) == 0) errexit("unable to create temporary file", 1); } /* see if we need to read a response file */ if (curarg < argc && argv[curarg][0] == '@') { osfildef *argfp; int l; char *p; if (!(argfp = osfoprt(argv[curarg]+1, OSFTTEXT))) errexit("unable to open response file", 1); for (;;) { if (!osfgets(buf, sizeof(buf), argfp)) break; l = strlen(buf); if (l && buf[l-1] == '\n') buf[--l] = '\0'; for (p = buf ; t_isspace(*p) ; ++p); if (!*p) continue; oplist = addop(oplist, p, &opctx); } osfcls(argfp); } else { for ( ; curarg < argc ; ++curarg) oplist = addop(oplist, argv[curarg], &opctx); } /* process the resources */ oplist = rscproc(fpin, fpout, oplist); /* make sure they all got processed */ for ( ; oplist != 0 ; oplist = oplist->opnxt) { if (!(oplist->opflag & OPFDONE)) rscptf("warning: resource \"%s\" not found\n", oplist->opres); } /* close files */ if (fpin != 0) osfcls(fpin); if (fpout != 0) osfcls(fpout); /* * if we didn't create a new file, remove the original input file * and rename the temp file to the original file name */ if (!do_create) { /* remove the original input file */ if (remove(inbuf)) errexit("error deleting input file", 1); /* rename the temp file to the output file */ if (rename(tmpfile, inbuf)) errexit("error renaming temporary file", 1); } /* success */ os_term(OSEXSUCC); return OSEXSUCC; }