goomsl.c
来自「linux下的MPEG1」· C语言 代码 · 共 1,510 行 · 第 1/3 页
C
1,510 行
DEST_VAR_INT = SRC_VAR_INT; ++ip; break; /* SET.F */ case INSTR_SETF_VAR_FLOAT: DEST_VAR_FLOAT = VALUE_FLOAT; ++ip; break; case INSTR_SETF_VAR_VAR: DEST_VAR_FLOAT = SRC_VAR_FLOAT; ++ip; break; /* SET.P */ case INSTR_SETP_VAR_VAR: DEST_VAR_PTR = SRC_VAR_PTR; ++ip; break; case INSTR_SETP_VAR_PTR: DEST_VAR_PTR = VALUE_PTR; ++ip; break; /* JUMP */ case INSTR_JUMP: ip += JUMP_OFFSET; break; /* JZERO */ case INSTR_JZERO: ip += (flag ? 1 : JUMP_OFFSET); break; case INSTR_NOP: ++ip; break; /* ISEQUAL.P */ case INSTR_ISEQUALP_VAR_VAR: flag = (DEST_VAR_PTR == SRC_VAR_PTR); ++ip; break; case INSTR_ISEQUALP_VAR_PTR: flag = (DEST_VAR_PTR == VALUE_PTR); ++ip; break; /* ISEQUAL.I */ case INSTR_ISEQUALI_VAR_VAR: flag = (DEST_VAR_INT == SRC_VAR_INT); ++ip; break; case INSTR_ISEQUALI_VAR_INTEGER: flag = (DEST_VAR_INT == VALUE_INT); ++ip; break; /* ISEQUAL.F */ case INSTR_ISEQUALF_VAR_VAR: flag = (DEST_VAR_FLOAT == SRC_VAR_FLOAT); ++ip; break; case INSTR_ISEQUALF_VAR_FLOAT: flag = (DEST_VAR_FLOAT == VALUE_FLOAT); ++ip; break; /* ISLOWER.I */ case INSTR_ISLOWERI_VAR_VAR: flag = (DEST_VAR_INT < SRC_VAR_INT); ++ip; break; case INSTR_ISLOWERI_VAR_INTEGER: flag = (DEST_VAR_INT < VALUE_INT); ++ip; break; /* ISLOWER.F */ case INSTR_ISLOWERF_VAR_VAR: flag = (DEST_VAR_FLOAT < SRC_VAR_FLOAT); ++ip; break; case INSTR_ISLOWERF_VAR_FLOAT: flag = (DEST_VAR_FLOAT < VALUE_FLOAT); ++ip; break; /* ADD.I */ case INSTR_ADDI_VAR_VAR: DEST_VAR_INT += SRC_VAR_INT; ++ip; break; case INSTR_ADDI_VAR_INTEGER: DEST_VAR_INT += VALUE_INT; ++ip; break; /* ADD.F */ case INSTR_ADDF_VAR_VAR: DEST_VAR_FLOAT += SRC_VAR_FLOAT; ++ip; break; case INSTR_ADDF_VAR_FLOAT: DEST_VAR_FLOAT += VALUE_FLOAT; ++ip; break; /* MUL.I */ case INSTR_MULI_VAR_VAR: DEST_VAR_INT *= SRC_VAR_INT; ++ip; break; case INSTR_MULI_VAR_INTEGER: DEST_VAR_INT *= VALUE_INT; ++ip; break; /* MUL.F */ case INSTR_MULF_VAR_FLOAT: DEST_VAR_FLOAT *= VALUE_FLOAT; ++ip; break; case INSTR_MULF_VAR_VAR: DEST_VAR_FLOAT *= SRC_VAR_FLOAT; ++ip; break; /* DIV.I */ case INSTR_DIVI_VAR_VAR: DEST_VAR_INT /= SRC_VAR_INT; ++ip; break; case INSTR_DIVI_VAR_INTEGER: DEST_VAR_INT /= VALUE_INT; ++ip; break; /* DIV.F */ case INSTR_DIVF_VAR_FLOAT: DEST_VAR_FLOAT /= VALUE_FLOAT; ++ip; break; case INSTR_DIVF_VAR_VAR: DEST_VAR_FLOAT /= SRC_VAR_FLOAT; ++ip; break; /* SUB.I */ case INSTR_SUBI_VAR_VAR: DEST_VAR_INT -= SRC_VAR_INT; ++ip; break; case INSTR_SUBI_VAR_INTEGER: DEST_VAR_INT -= VALUE_INT; ++ip; break; /* SUB.F */ case INSTR_SUBF_VAR_FLOAT: DEST_VAR_FLOAT -= VALUE_FLOAT; ++ip; break; case INSTR_SUBF_VAR_VAR: DEST_VAR_FLOAT -= SRC_VAR_FLOAT; ++ip; break; /* CALL */ case INSTR_CALL: stack[stack_pointer++] = ip + 1; ip += JUMP_OFFSET; break; /* RET */ case INSTR_RET: ip = stack[--stack_pointer]; if (ip<0) return; break; /* EXT_CALL */ case INSTR_EXT_CALL: instr[ip].data.udest.external_function->function(gsl, gsl->vars, instr[ip].data.udest.external_function->vars); ++ip; break; /* NOT */ case INSTR_NOT_VAR: flag = !flag; ++ip; break; /* JNZERO */ case INSTR_JNZERO: ip += (flag ? JUMP_OFFSET : 1); break; case INSTR_SETS_VAR_VAR: memcpy(pDEST_VAR, pSRC_VAR, DEST_STRUCT_SIZE); ++ip; break; case INSTR_ISEQUALS_VAR_VAR: break; case INSTR_ADDS_VAR_VAR: /* process integers */ i=0; while (DEST_STRUCT_IBLOCK(i).size > 0) { int j=DEST_STRUCT_IBLOCK(i).size; while (j--) { DEST_STRUCT_IBLOCK_VAR(i,j) += SRC_STRUCT_IBLOCK_VAR(i,j); } ++i; } /* process floats */ i=0; while (DEST_STRUCT_FBLOCK(i).size > 0) { int j=DEST_STRUCT_FBLOCK(i).size; while (j--) { DEST_STRUCT_FBLOCK_VAR(i,j) += SRC_STRUCT_FBLOCK_VAR(i,j); } ++i; } ++ip; break; case INSTR_SUBS_VAR_VAR: /* process integers */ i=0; while (DEST_STRUCT_IBLOCK(i).size > 0) { int j=DEST_STRUCT_IBLOCK(i).size; while (j--) { DEST_STRUCT_IBLOCK_VAR(i,j) -= SRC_STRUCT_IBLOCK_VAR(i,j); } ++i; } /* process floats */ i=0; while (DEST_STRUCT_FBLOCK(i).size > 0) { int j=DEST_STRUCT_FBLOCK(i).size; while (j--) { DEST_STRUCT_FBLOCK_VAR(i,j) -= SRC_STRUCT_FBLOCK_VAR(i,j); } ++i; } ++ip; break; case INSTR_MULS_VAR_VAR: /* process integers */ i=0; while (DEST_STRUCT_IBLOCK(i).size > 0) { int j=DEST_STRUCT_IBLOCK(i).size; while (j--) { DEST_STRUCT_IBLOCK_VAR(i,j) *= SRC_STRUCT_IBLOCK_VAR(i,j); } ++i; } /* process floats */ i=0; while (DEST_STRUCT_FBLOCK(i).size > 0) { int j=DEST_STRUCT_FBLOCK(i).size; while (j--) { DEST_STRUCT_FBLOCK_VAR(i,j) *= SRC_STRUCT_FBLOCK_VAR(i,j); } ++i; } ++ip; break; case INSTR_DIVS_VAR_VAR: /* process integers */ i=0; while (DEST_STRUCT_IBLOCK(i).size > 0) { int j=DEST_STRUCT_IBLOCK(i).size; while (j--) { DEST_STRUCT_IBLOCK_VAR(i,j) /= SRC_STRUCT_IBLOCK_VAR(i,j); } ++i; } /* process floats */ i=0; while (DEST_STRUCT_FBLOCK(i).size > 0) { int j=DEST_STRUCT_FBLOCK(i).size; while (j--) { DEST_STRUCT_FBLOCK_VAR(i,j) /= SRC_STRUCT_FBLOCK_VAR(i,j); } ++i; } ++ip; break; default: printf("NOT IMPLEMENTED : %d\n", instr[ip].id); ++ip; exit(1); } }} /* }}} */int gsl_malloc(GoomSL *_this, int size){ /* {{{ */ if (_this->nbPtr >= _this->ptrArraySize) { _this->ptrArraySize *= 2; _this->ptrArray = (void**)realloc(_this->ptrArray, sizeof(void*) * _this->ptrArraySize); } _this->ptrArray[_this->nbPtr] = malloc(size); return _this->nbPtr++;} /* }}} */void *gsl_get_ptr(GoomSL *_this, int id){ /* {{{ */ if ((id>=0)&&(id<_this->nbPtr)) return _this->ptrArray[id]; fprintf(stderr,"INVALID GET PTR 0x%08x\n", id); return NULL;} /* }}} */void gsl_free_ptr(GoomSL *_this, int id){ /* {{{ */ if ((id>=0)&&(id<_this->nbPtr)) { free(_this->ptrArray[id]); _this->ptrArray[id] = 0; }} /* }}} */void gsl_enternamespace(const char *name){ /* {{{ */ HashValue *val = goom_hash_get(currentGoomSL->functions, name); if (val) { ExternalFunctionStruct *function = (ExternalFunctionStruct*)val->ptr; currentGoomSL->currentNS++; currentGoomSL->namespaces[currentGoomSL->currentNS] = function->vars; } else { fprintf(stderr, "ERROR: Line %d, Could not find namespace: %s\n", currentGoomSL->num_lines, name); exit(1); }} /* }}} */void gsl_reenternamespace(GoomHash *nsinfo) { currentGoomSL->currentNS++; currentGoomSL->namespaces[currentGoomSL->currentNS] = nsinfo;}GoomHash *gsl_leavenamespace(void){ /* {{{ */ currentGoomSL->currentNS--; return currentGoomSL->namespaces[currentGoomSL->currentNS+1];} /* }}} */GoomHash *gsl_find_namespace(const char *name){ /* {{{ */ int i; for (i=currentGoomSL->currentNS;i>=0;--i) { if (goom_hash_get(currentGoomSL->namespaces[i], name)) return currentGoomSL->namespaces[i]; } return NULL;} /* }}} */void gsl_declare_task(const char *name){ /* {{{ */ if (goom_hash_get(currentGoomSL->functions, name)) { return; } else { ExternalFunctionStruct *gef = (ExternalFunctionStruct*)malloc(sizeof(ExternalFunctionStruct)); gef->function = 0; gef->vars = goom_hash_new(); gef->is_extern = 0; goom_hash_put_ptr(currentGoomSL->functions, name, (void*)gef); }} /* }}} */void gsl_declare_external_task(const char *name){ /* {{{ */ if (goom_hash_get(currentGoomSL->functions, name)) { fprintf(stderr, "ERROR: Line %d, Duplicate declaration of %s\n", currentGoomSL->num_lines, name); return; } else { ExternalFunctionStruct *gef = (ExternalFunctionStruct*)malloc(sizeof(ExternalFunctionStruct)); gef->function = 0; gef->vars = goom_hash_new(); gef->is_extern = 1; goom_hash_put_ptr(currentGoomSL->functions, name, (void*)gef); }} /* }}} */static void reset_scanner(GoomSL *gss){ /* {{{ */ gss->num_lines = 0; gss->instr = NULL; iflow_clean(gss->iflow); /* reset variables */ goom_hash_free(gss->vars); gss->vars = goom_hash_new(); gss->currentNS = 0; gss->namespaces[0] = gss->vars; goom_hash_free(gss->structIDS); gss->structIDS = goom_hash_new(); while (gss->nbStructID > 0) { int i; gss->nbStructID--; for(i=0;i<gss->gsl_struct[gss->nbStructID]->nbFields;++i) free(gss->gsl_struct[gss->nbStructID]->fields[i]); free(gss->gsl_struct[gss->nbStructID]); } gss->compilationOK = 1; goom_heap_delete(gss->data_heap); gss->data_heap = goom_heap_new();} /* }}} */static void calculate_labels(InstructionFlow *iflow){ /* {{{ */ int i = 0; while (i < iflow->number) { Instruction *instr = iflow->instr[i]; if (instr->jump_label) { HashValue *label = goom_hash_get(iflow->labels,instr->jump_label); if (label) { instr->data.udest.jump_offset = -instr->address + label->i; } else { fprintf(stderr, "ERROR: Line %d, Could not find label %s\n", instr->line_number, instr->jump_label); instr->id = INSTR_NOP; instr->nop_label = 0; exit(1); } } ++i; }} /* }}} */static int powerOfTwo(int i){ int b; for (b=0;b<31;b++) if (i == (1<<b)) return b; return 0;}/* Cree un flow d'instruction optimise */static void gsl_create_fast_iflow(void){ /* {{{ */ int number = currentGoomSL->iflow->number; int i;#ifdef USE_JITC_X86 /* pour compatibilite avec les MACROS servant a execution */ int ip = 0; GoomSL *gsl = currentGoomSL; JitcX86Env *jitc; if (currentGoomSL->jitc != NULL) jitc_x86_delete(currentGoomSL->jitc); jitc = currentGoomSL->jitc = jitc_x86_env_new(0xffff); currentGoomSL->jitc_func = jitc_prepare_func(jitc);#if 0 #define SRC_STRUCT_ID instr[ip].data.usrc.var_int[-1]#define DEST_STRUCT_ID instr[ip].data.udest.var_int[-1]#define SRC_STRUCT_IBLOCK(i) gsl->gsl_struct[SRC_STRUCT_ID]->iBlock[i]#define SRC_STRUCT_FBLOCK(i) gsl->gsl_struct[SRC_STRUCT_ID]->fBlock[i]#define DEST_STRUCT_IBLOCK(i) gsl->gsl_struct[DEST_STRUCT_ID]->iBlock[i]#define DEST_STRUCT_FBLOCK(i) gsl->gsl_struct[DEST_STRUCT_ID]->fBlock[i]#define DEST_STRUCT_IBLOCK_VAR(i,j) \ ((int*)((char*)pDEST_VAR + gsl->gsl_struct[DEST_STRUCT_ID]->iBlock[i].data))[j]#define DEST_STRUCT_FBLOCK_VAR(i,j) \ ((float*)((char*)pDEST_VAR + gsl->gsl_struct[DEST_STRUCT_ID]->fBlock[i].data))[j]#define SRC_STRUCT_IBLOCK_VAR(i,j) \ ((int*)((char*)pSRC_VAR + gsl->gsl_struct[SRC_STRUCT_ID]->iBlock[i].data))[j]#define SRC_STRUCT_FBLOCK_VAR(i,j) \ ((float*)((char*)pSRC_VAR + gsl->gsl_struct[SRC_STRUCT_ID]->fBlock[i].data))[j]#define DEST_STRUCT_SIZE gsl->gsl_struct[DEST_STRUCT_ID]->size#endif JITC_JUMP_LABEL(jitc, "__very_end__"); JITC_ADD_LABEL (jitc, "__very_start__"); for (i=0;i<number;++i) { Instruction *instr = currentGoomSL->iflow->instr[i]; switch (instr->id) { case INSTR_SETI_VAR_INTEGER : jitc_add(jitc, "mov [$d], $d", instr->data.udest.var_int, instr->data.usrc.value_int); break; case INSTR_SETI_VAR_VAR : jitc_add(jitc, "mov eax, [$d]", instr->data.usrc.var_int); jitc_add(jitc, "mov [$d], eax", instr->data.udest.var_int); break; /* SET.F */ case INSTR_SETF_VAR_FLOAT : jitc_add(jitc, "mov [$d], $d", instr->data.udest.var_float, *(int*)(&instr->data.usrc.value_float)); break; case INSTR_SETF_VAR_VAR : jitc_add(jitc, "mov eax, [$d]", instr->data.usrc.var_float); jitc_add(jitc, "mov [$d], eax", instr->data.udest.var_float); break; case INSTR_NOP : if (instr->nop_label != 0) JITC_ADD_LABEL(jitc, instr->nop_label); break; case INSTR_JUMP : JITC_JUMP_LABEL(jitc,instr->jump_label); break; case INSTR_SETP_VAR_PTR : jitc_add(jitc, "mov [$d], $d", instr->data.udest.var_ptr, instr->data.usrc.value_ptr); break; case INSTR_SETP_VAR_VAR : jitc_add(jitc, "mov eax, [$d]", instr->data.usrc.var_ptr); jitc_add(jitc, "mov [$d], eax", instr->data.udest.var_ptr); break; case INSTR_SUBI_VAR_INTEGER : jitc_add(jitc, "add [$d], $d", instr->data.udest.var_int, -instr->data.usrc.value_int); break; case INSTR_SUBI_VAR_VAR : jitc_add(jitc, "mov eax, [$d]", instr->data.udest.var_int); jitc_add(jitc, "sub eax, [$d]", instr->data.usrc.var_int); jitc_add(jitc, "mov [$d], eax", instr->data.udest.var_int); break; case INSTR_SUBF_VAR_FLOAT : printf("NOT IMPLEMENTED : %d\n", instr->id); break;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?