int main() { char good_template[] = "/tmp/dirXXXXXX"; /* 正确的方式,""引用的内容已全部分配进栈内存中,可以被mkstemp修改 */ char *bad_template = "/tmp/dirXXXXXX"; /* 错误的方式,""引用的内容仍在只读区中,不能被mkstemp修改 */ printf("trying to create first temp file...\n"); make_temp(good_template); printf("trying to create second temp file...\n"); make_temp(bad_template); exit(0); }
int main(void) { char good_template[] = "/tmp/dirXXXXXX"; char *bad_template = "/tmp/dirXXXXXX"; printf("trying to create first temp file...\n"); make_temp(good_template); printf("trying to create second temp file...\n"); make_temp(bad_template); exit(0); }
int main(int argc, char *argv[]) { /* right way */ char good_template[] = "/tmp/dirXXXXXX"; /* wrong way*/ char *bad_template = "/tmp/dirXXXXXXX"; printf("trying to create first temp file...\n"); make_temp(good_template); printf("trying to create second temp file...\n"); make_temp(bad_template); exit(0); }
static TAC *do_binoperation(int type, TAC *t1, TAC *t2) { HASH_NODE *newnode = NULL; TAC *list = NULL; TAC *final = NULL; newnode = make_temp(); list = tac_join(t1, t2); final = tac_create(type, newnode, t1->res, t2->res);
Expression *expression_call(Symbol *function, Expression *arguments) { debug("Creating expression call for %s Type: %d", symbol_to_string(function), function->type->code); if ((function->type->code != TYPE_FUNCTION) && (function->type->code != TYPE_PROCEDURE)) die("Symbol %s isn't a function or procedure", symbol_to_string(function)); Expression *alt; Symbol *result = NULL;//If this is a function, it returns a value Tac *code; Tac *temp; if ((function->type->code == TYPE_FUNCTION) && (function->external == NULL)) { //Create symbol for result, and declare variable result = make_temp(); code = make_tac(TAC_VARIABLE, result, NULL, NULL); } else { code = make_tac(TAC_NOP, NULL, NULL, NULL); } //Connect up rest of arguments alt = arguments; while (alt != NULL) { code = join_tac(code, alt->tac); alt = alt->next; } //Generate argument instructions while (arguments != NULL) { temp = make_tac(TAC_ARG, arguments->result, NULL, NULL); temp->prev = code; code = temp; alt = arguments->next; free(arguments); arguments = alt; } //temp = make_label_tac(TAC_CALL, function->label, result); temp = make_tac(TAC_CALL, result, function, NULL); temp->prev = code; code = temp; return make_expression(NULL, result, code); }
/** * Either return a precalculated constant value or emit code to * calculate these values dynamically in the case where material calls * are present between begin/end pairs. * * Probably want to shift this to the program compilation phase - if * we always emitted the calculation here, a smart compiler could * detect that it was constant (given a certain set of inputs), and * lift it out of the main loop. That way the programs created here * would be independent of the vertex_buffer details. */ static struct ureg get_scenecolor( struct tnl_program *p, GLuint side ) { if (p->materials & SCENE_COLOR_BITS(side)) { struct ureg lm_ambient = register_param1(p, STATE_LIGHTMODEL_AMBIENT); struct ureg material_emission = get_material(p, side, STATE_EMISSION); struct ureg material_ambient = get_material(p, side, STATE_AMBIENT); struct ureg material_diffuse = get_material(p, side, STATE_DIFFUSE); struct ureg tmp = make_temp(p, material_diffuse); emit_op3(p, OPCODE_MAD, tmp, WRITEMASK_XYZ, lm_ambient, material_ambient, material_emission); return tmp; } else return register_param2( p, STATE_LIGHTMODEL_SCENECOLOR, side ); }
int Expander::expand_reduce1d(bh_ir& bhir, int pc, int thread_limit) { static std::map<int,int> fold_map; int start_pc = pc; bh_instruction& instr = bhir.instr_list[pc]; // Grab the BH_POWER instruction bh_index elements = bh_nelements(instr.operand[1]); if (elements * 2 < thread_limit) return 0; int fold = 0; if (fold_map.find(elements) != fold_map.end()) { fold = fold_map.find(elements)->second; } else { fold = find_fold(elements,thread_limit); fold_map[elements] = fold; } if (fold < 2) return 0; bh_opcode opcode = instr.opcode; instr.opcode = BH_NONE; // Lazy choice... no re-use just NOP it. bh_view out = instr.operand[0]; // Grab operands bh_view in = instr.operand[1]; in.ndim = 2; in.shape[0] = fold; in.shape[1] = elements/fold; in.stride[1] = in.stride[0]; in.stride[0] = in.stride[0]*elements/fold; bh_view temp = make_temp(in.base->type, elements/fold); inject(bhir, ++pc, opcode, temp, in, 0, BH_INT64); inject(bhir, ++pc, opcode, out, temp, 0, BH_INT64); inject(bhir, ++pc, BH_FREE, temp); inject(bhir, ++pc, BH_DISCARD, temp); return pc-start_pc; }
/* Need to add some addtional parameters to allow lighting in object * space - STATE_SPOT_DIRECTION and STATE_HALF_VECTOR implicitly assume eye * space lighting. */ static void build_lighting( struct tnl_program *p ) { const GLboolean twoside = p->state->light_twoside; const GLboolean separate = p->state->separate_specular; GLuint nr_lights = 0, count = 0; struct ureg normal = get_transformed_normal(p); struct ureg lit = get_temp(p); struct ureg dots = get_temp(p); struct ureg _col0 = undef, _col1 = undef; struct ureg _bfc0 = undef, _bfc1 = undef; GLuint i; /* * NOTE: * dots.x = dot(normal, VPpli) * dots.y = dot(normal, halfAngle) * dots.z = back.shininess * dots.w = front.shininess */ for (i = 0; i < MAX_LIGHTS; i++) if (p->state->unit[i].light_enabled) nr_lights++; set_material_flags(p); { if (!p->state->material_shininess_is_zero) { struct ureg shininess = get_material(p, 0, STATE_SHININESS); emit_op1(p, OPCODE_MOV, dots, WRITEMASK_W, swizzle1(shininess,X)); release_temp(p, shininess); } _col0 = make_temp(p, get_scenecolor(p, 0)); if (separate) _col1 = make_temp(p, get_identity_param(p)); else _col1 = _col0; } if (twoside) { if (!p->state->material_shininess_is_zero) { /* Note that we negate the back-face specular exponent here. * The negation will be un-done later in the back-face code below. */ struct ureg shininess = get_material(p, 1, STATE_SHININESS); emit_op1(p, OPCODE_MOV, dots, WRITEMASK_Z, negate(swizzle1(shininess,X))); release_temp(p, shininess); } _bfc0 = make_temp(p, get_scenecolor(p, 1)); if (separate) _bfc1 = make_temp(p, get_identity_param(p)); else _bfc1 = _bfc0; } /* If no lights, still need to emit the scenecolor. */ { struct ureg res0 = register_output( p, VERT_RESULT_COL0 ); emit_op1(p, OPCODE_MOV, res0, 0, _col0); } if (separate) { struct ureg res1 = register_output( p, VERT_RESULT_COL1 ); emit_op1(p, OPCODE_MOV, res1, 0, _col1); } if (twoside) { struct ureg res0 = register_output( p, VERT_RESULT_BFC0 ); emit_op1(p, OPCODE_MOV, res0, 0, _bfc0); } if (twoside && separate) { struct ureg res1 = register_output( p, VERT_RESULT_BFC1 ); emit_op1(p, OPCODE_MOV, res1, 0, _bfc1); } if (nr_lights == 0) { release_temps(p); return; } for (i = 0; i < MAX_LIGHTS; i++) { if (p->state->unit[i].light_enabled) { struct ureg half = undef; struct ureg att = undef, VPpli = undef; count++; if (p->state->unit[i].light_eyepos3_is_zero) { /* Can used precomputed constants in this case. * Attenuation never applies to infinite lights. */ VPpli = register_param3(p, STATE_INTERNAL, STATE_LIGHT_POSITION_NORMALIZED, i); if (!p->state->material_shininess_is_zero) { if (p->state->light_local_viewer) { struct ureg eye_hat = get_eye_position_normalized(p); half = get_temp(p); emit_op2(p, OPCODE_SUB, half, 0, VPpli, eye_hat); emit_normalize_vec3(p, half, half); } else { half = register_param3(p, STATE_INTERNAL, STATE_LIGHT_HALF_VECTOR, i); } } } else { struct ureg Ppli = register_param3(p, STATE_INTERNAL, STATE_LIGHT_POSITION, i); struct ureg V = get_eye_position(p); struct ureg dist = get_temp(p); VPpli = get_temp(p); /* Calculate VPpli vector */ emit_op2(p, OPCODE_SUB, VPpli, 0, Ppli, V); /* Normalize VPpli. The dist value also used in * attenuation below. */ emit_op2(p, OPCODE_DP3, dist, 0, VPpli, VPpli); emit_op1(p, OPCODE_RSQ, dist, 0, dist); emit_op2(p, OPCODE_MUL, VPpli, 0, VPpli, dist); /* Calculate attenuation: */ if (!p->state->unit[i].light_spotcutoff_is_180 || p->state->unit[i].light_attenuated) { att = calculate_light_attenuation(p, i, VPpli, dist); } /* Calculate viewer direction, or use infinite viewer: */ if (!p->state->material_shininess_is_zero) { half = get_temp(p); if (p->state->light_local_viewer) { struct ureg eye_hat = get_eye_position_normalized(p); emit_op2(p, OPCODE_SUB, half, 0, VPpli, eye_hat); } else { struct ureg z_dir = swizzle(get_identity_param(p),X,Y,W,Z); emit_op2(p, OPCODE_ADD, half, 0, VPpli, z_dir); } emit_normalize_vec3(p, half, half); } release_temp(p, dist); } /* Calculate dot products: */ if (p->state->material_shininess_is_zero) { emit_op2(p, OPCODE_DP3, dots, 0, normal, VPpli); } else { emit_op2(p, OPCODE_DP3, dots, WRITEMASK_X, normal, VPpli); emit_op2(p, OPCODE_DP3, dots, WRITEMASK_Y, normal, half); } /* Front face lighting: */ { struct ureg ambient = get_lightprod(p, i, 0, STATE_AMBIENT); struct ureg diffuse = get_lightprod(p, i, 0, STATE_DIFFUSE); struct ureg specular = get_lightprod(p, i, 0, STATE_SPECULAR); struct ureg res0, res1; GLuint mask0, mask1; if (count == nr_lights) { if (separate) { mask0 = WRITEMASK_XYZ; mask1 = WRITEMASK_XYZ; res0 = register_output( p, VERT_RESULT_COL0 ); res1 = register_output( p, VERT_RESULT_COL1 ); } else { mask0 = 0; mask1 = WRITEMASK_XYZ; res0 = _col0; res1 = register_output( p, VERT_RESULT_COL0 ); } } else { mask0 = 0; mask1 = 0; res0 = _col0; res1 = _col1; } if (!is_undef(att)) { /* light is attenuated by distance */ emit_op1(p, OPCODE_LIT, lit, 0, dots); emit_op2(p, OPCODE_MUL, lit, 0, lit, att); emit_op3(p, OPCODE_MAD, _col0, 0, swizzle1(lit,X), ambient, _col0); } else if (!p->state->material_shininess_is_zero) { /* there's a non-zero specular term */ emit_op1(p, OPCODE_LIT, lit, 0, dots); emit_op2(p, OPCODE_ADD, _col0, 0, ambient, _col0); } else { /* no attenutation, no specular */ emit_degenerate_lit(p, lit, dots); emit_op2(p, OPCODE_ADD, _col0, 0, ambient, _col0); } emit_op3(p, OPCODE_MAD, res0, mask0, swizzle1(lit,Y), diffuse, _col0); emit_op3(p, OPCODE_MAD, res1, mask1, swizzle1(lit,Z), specular, _col1); release_temp(p, ambient); release_temp(p, diffuse); release_temp(p, specular); } /* Back face lighting: */ if (twoside) { struct ureg ambient = get_lightprod(p, i, 1, STATE_AMBIENT); struct ureg diffuse = get_lightprod(p, i, 1, STATE_DIFFUSE); struct ureg specular = get_lightprod(p, i, 1, STATE_SPECULAR); struct ureg res0, res1; GLuint mask0, mask1; if (count == nr_lights) { if (separate) { mask0 = WRITEMASK_XYZ; mask1 = WRITEMASK_XYZ; res0 = register_output( p, VERT_RESULT_BFC0 ); res1 = register_output( p, VERT_RESULT_BFC1 ); } else { mask0 = 0; mask1 = WRITEMASK_XYZ; res0 = _bfc0; res1 = register_output( p, VERT_RESULT_BFC0 ); } } else { res0 = _bfc0; res1 = _bfc1; mask0 = 0; mask1 = 0; } /* For the back face we need to negate the X and Y component * dot products. dots.Z has the negated back-face specular * exponent. We swizzle that into the W position. This * negation makes the back-face specular term positive again. */ dots = negate(swizzle(dots,X,Y,W,Z)); if (!is_undef(att)) { emit_op1(p, OPCODE_LIT, lit, 0, dots); emit_op2(p, OPCODE_MUL, lit, 0, lit, att); emit_op3(p, OPCODE_MAD, _bfc0, 0, swizzle1(lit,X), ambient, _bfc0); } else if (!p->state->material_shininess_is_zero) { emit_op1(p, OPCODE_LIT, lit, 0, dots); emit_op2(p, OPCODE_ADD, _bfc0, 0, ambient, _bfc0); /**/ } else { emit_degenerate_lit(p, lit, dots); emit_op2(p, OPCODE_ADD, _bfc0, 0, ambient, _bfc0); } emit_op3(p, OPCODE_MAD, res0, mask0, swizzle1(lit,Y), diffuse, _bfc0); emit_op3(p, OPCODE_MAD, res1, mask1, swizzle1(lit,Z), specular, _bfc1); /* restore dots to its original state for subsequent lights * by negating and swizzling again. */ dots = negate(swizzle(dots,X,Y,W,Z)); release_temp(p, ambient); release_temp(p, diffuse); release_temp(p, specular); } release_temp(p, half); release_temp(p, VPpli); release_temp(p, att); } } release_temps( p ); }
/* Need to add some addtional parameters to allow lighting in object * space - STATE_SPOT_DIRECTION and STATE_HALF_VECTOR implicitly assume eye * space lighting. */ static void build_lighting( struct tnl_program *p ) { const GLboolean twoside = p->state->light_twoside; const GLboolean separate = p->state->separate_specular; GLuint nr_lights = 0, count = 0; struct ureg normal = get_eye_normal(p); struct ureg lit = get_temp(p); struct ureg dots = get_temp(p); struct ureg _col0 = undef, _col1 = undef; struct ureg _bfc0 = undef, _bfc1 = undef; GLuint i; for (i = 0; i < MAX_LIGHTS; i++) if (p->state->unit[i].light_enabled) nr_lights++; set_material_flags(p); { struct ureg shininess = get_material(p, 0, STATE_SHININESS); emit_op1(p, OPCODE_MOV, dots, WRITEMASK_W, swizzle1(shininess,X)); release_temp(p, shininess); _col0 = make_temp(p, get_scenecolor(p, 0)); if (separate) _col1 = make_temp(p, get_identity_param(p)); else _col1 = _col0; } if (twoside) { struct ureg shininess = get_material(p, 1, STATE_SHININESS); emit_op1(p, OPCODE_MOV, dots, WRITEMASK_Z, ureg_negate(swizzle1(shininess,X))); release_temp(p, shininess); _bfc0 = make_temp(p, get_scenecolor(p, 1)); if (separate) _bfc1 = make_temp(p, get_identity_param(p)); else _bfc1 = _bfc0; } /* If no lights, still need to emit the scenecolor. */ /* KW: changed to do this always - v1.17 "Fix lighting alpha result"? */ if (p->state->fragprog_inputs_read & FRAG_BIT_COL0) { struct ureg res0 = register_output( p, VERT_RESULT_COL0 ); emit_op1(p, OPCODE_MOV, res0, 0, _col0); if (twoside) { struct ureg res0 = register_output( p, VERT_RESULT_BFC0 ); emit_op1(p, OPCODE_MOV, res0, 0, _bfc0); } } if (separate && (p->state->fragprog_inputs_read & FRAG_BIT_COL1)) { struct ureg res1 = register_output( p, VERT_RESULT_COL1 ); emit_op1(p, OPCODE_MOV, res1, 0, _col1); if (twoside) { struct ureg res1 = register_output( p, VERT_RESULT_BFC1 ); emit_op1(p, OPCODE_MOV, res1, 0, _bfc1); } } if (nr_lights == 0) { release_temps(p); return; } for (i = 0; i < MAX_LIGHTS; i++) { if (p->state->unit[i].light_enabled) { struct ureg half = undef; struct ureg att = undef, VPpli = undef; count++; if (p->state->unit[i].light_eyepos3_is_zero) { /* Can used precomputed constants in this case. * Attenuation never applies to infinite lights. */ VPpli = register_param3(p, STATE_LIGHT, i, STATE_POSITION_NORMALIZED); if (p->state->light_local_viewer) { struct ureg eye_hat = get_eye_position_normalized(p); half = get_temp(p); emit_op2(p, OPCODE_SUB, half, 0, VPpli, eye_hat); emit_normalize_vec3(p, half, half); } else { half = register_param3(p, STATE_LIGHT, i, STATE_HALF_VECTOR); } } else { struct ureg Ppli = register_param3(p, STATE_LIGHT, i, STATE_POSITION); struct ureg V = get_eye_position(p); struct ureg dist = get_temp(p); VPpli = get_temp(p); half = get_temp(p); /* Calulate VPpli vector */ emit_op2(p, OPCODE_SUB, VPpli, 0, Ppli, V); /* Normalize VPpli. The dist value also used in * attenuation below. */ emit_op2(p, OPCODE_DP3, dist, 0, VPpli, VPpli); emit_op1(p, OPCODE_RSQ, dist, 0, dist); emit_op2(p, OPCODE_MUL, VPpli, 0, VPpli, dist); /* Calculate attenuation: */ if (!p->state->unit[i].light_spotcutoff_is_180 || p->state->unit[i].light_attenuated) { att = calculate_light_attenuation(p, i, VPpli, dist); } /* Calculate viewer direction, or use infinite viewer: */ if (p->state->light_local_viewer) { struct ureg eye_hat = get_eye_position_normalized(p); emit_op2(p, OPCODE_SUB, half, 0, VPpli, eye_hat); } else { struct ureg z_dir = swizzle(get_identity_param(p),X,Y,W,Z); emit_op2(p, OPCODE_ADD, half, 0, VPpli, z_dir); } emit_normalize_vec3(p, half, half); release_temp(p, dist); } /* Calculate dot products: */ emit_op2(p, OPCODE_DP3, dots, WRITEMASK_X, normal, VPpli); emit_op2(p, OPCODE_DP3, dots, WRITEMASK_Y, normal, half); /* Front face lighting: */ { struct ureg ambient = get_lightprod(p, i, 0, STATE_AMBIENT); struct ureg diffuse = get_lightprod(p, i, 0, STATE_DIFFUSE); struct ureg specular = get_lightprod(p, i, 0, STATE_SPECULAR); struct ureg res0, res1; GLuint mask0, mask1; emit_op1(p, OPCODE_LIT, lit, 0, dots); if (!is_undef(att)) emit_op2(p, OPCODE_MUL, lit, 0, lit, att); mask0 = 0; mask1 = 0; res0 = _col0; res1 = _col1; if (count == nr_lights) { if (separate) { mask0 = WRITEMASK_XYZ; mask1 = WRITEMASK_XYZ; if (p->state->fragprog_inputs_read & FRAG_BIT_COL0) res0 = register_output( p, VERT_RESULT_COL0 ); if (p->state->fragprog_inputs_read & FRAG_BIT_COL1) res1 = register_output( p, VERT_RESULT_COL1 ); } else { mask1 = WRITEMASK_XYZ; if (p->state->fragprog_inputs_read & FRAG_BIT_COL0) res1 = register_output( p, VERT_RESULT_COL0 ); } } emit_op3(p, OPCODE_MAD, _col0, 0, swizzle1(lit,X), ambient, _col0); emit_op3(p, OPCODE_MAD, res0, mask0, swizzle1(lit,Y), diffuse, _col0); emit_op3(p, OPCODE_MAD, res1, mask1, swizzle1(lit,Z), specular, _col1); release_temp(p, ambient); release_temp(p, diffuse); release_temp(p, specular); } /* Back face lighting: */ if (twoside) { struct ureg ambient = get_lightprod(p, i, 1, STATE_AMBIENT); struct ureg diffuse = get_lightprod(p, i, 1, STATE_DIFFUSE); struct ureg specular = get_lightprod(p, i, 1, STATE_SPECULAR); struct ureg res0, res1; GLuint mask0, mask1; emit_op1(p, OPCODE_LIT, lit, 0, ureg_negate(swizzle(dots,X,Y,W,Z))); if (!is_undef(att)) emit_op2(p, OPCODE_MUL, lit, 0, lit, att); mask0 = 0; mask1 = 0; res0 = _bfc0; res1 = _bfc1; if (count == nr_lights) { if (separate) { mask0 = WRITEMASK_XYZ; mask1 = WRITEMASK_XYZ; if (p->state->fragprog_inputs_read & FRAG_BIT_COL0) res0 = register_output( p, VERT_RESULT_BFC0 ); if (p->state->fragprog_inputs_read & FRAG_BIT_COL1) res1 = register_output( p, VERT_RESULT_BFC1 ); } else { mask1 = WRITEMASK_XYZ; if (p->state->fragprog_inputs_read & FRAG_BIT_COL0) res1 = register_output( p, VERT_RESULT_BFC0 ); } } emit_op3(p, OPCODE_MAD, _bfc0, 0, swizzle1(lit,X), ambient, _bfc0); emit_op3(p, OPCODE_MAD, res0, mask0, swizzle1(lit,Y), diffuse, _bfc0); emit_op3(p, OPCODE_MAD, res1, mask1, swizzle1(lit,Z), specular, _bfc1); release_temp(p, ambient); release_temp(p, diffuse); release_temp(p, specular); } release_temp(p, half); release_temp(p, VPpli); release_temp(p, att); } } release_temps( p ); }
int Expander::expand_sign(bh_ir& bhir, int pc) { int start_pc = pc; bh_instruction& composite = bhir.instr_list[pc]; composite.opcode = BH_NONE; // Lazy choice... no re-use just NOP it. bh_view output = composite.operand[0]; // Grab operands bh_view input = composite.operand[1]; bh_type input_type = input.base->type; // Grab the input-type bh_view meta = composite.operand[0]; // Inherit ndim and shape meta.start = 0; bh_intp nelements = 1; // Count number of elements for(bh_intp dim=meta.ndim-1; dim >= 0; --dim) { // Contiguous stride meta.stride[dim] = nelements; nelements *= meta.shape[dim]; } if (!((input_type == BH_COMPLEX64) || \ (input_type == BH_COMPLEX128))) { // For non-complex: sign(x) = (x>0)-(x<0) bh_view lss = make_temp(meta, input_type, nelements);// Temps bh_view gtr = make_temp(meta, input_type, nelements); bh_view t_bool = make_temp(meta, BH_BOOL, nelements); inject(bhir, ++pc, BH_GREATER, t_bool, input, 0.0); // Sequence inject(bhir, ++pc, BH_IDENTITY, lss, t_bool); inject(bhir, ++pc, BH_FREE, t_bool); inject(bhir, ++pc, BH_DISCARD, t_bool); inject(bhir, ++pc, BH_LESS, t_bool, input, 0.0); inject(bhir, ++pc, BH_IDENTITY, gtr, t_bool); inject(bhir, ++pc, BH_FREE, t_bool); inject(bhir, ++pc, BH_DISCARD, t_bool); inject(bhir, ++pc, BH_SUBTRACT, output, lss, gtr); inject(bhir, ++pc, BH_FREE, lss); inject(bhir, ++pc, BH_DISCARD, lss); inject(bhir, ++pc, BH_FREE, gtr); inject(bhir, ++pc, BH_DISCARD, gtr); } else { // For complex: sign(0) = 0, sign(z) = z/|z| bh_type float_type = (input_type == BH_COMPLEX64) ? BH_FLOAT32 : BH_FLOAT64; // General form: sign(z) = z/(|z|+(z==0)) bh_view f_abs = make_temp(meta, float_type, nelements); // Temps bh_view b_zero = make_temp(meta, BH_BOOL, nelements); bh_view f_zero = make_temp(meta, float_type, nelements); inject(bhir, ++pc, BH_ABSOLUTE, f_abs, input); // Sequence inject(bhir, ++pc, BH_EQUAL, b_zero, f_abs, 0.0, float_type); inject(bhir, ++pc, BH_IDENTITY, f_zero, b_zero); inject(bhir, ++pc, BH_FREE, b_zero); inject(bhir, ++pc, BH_DISCARD, b_zero); inject(bhir, ++pc, BH_ADD, f_abs, f_abs, f_zero); inject(bhir, ++pc, BH_FREE, f_zero); inject(bhir, ++pc, BH_DISCARD, f_zero); inject(bhir, ++pc, BH_IDENTITY, output, f_abs); inject(bhir, ++pc, BH_FREE, f_abs); inject(bhir, ++pc, BH_DISCARD, f_abs); inject(bhir, ++pc, BH_DIVIDE, output, input, output); } return pc-start_pc; }
static void do_it(char *path) { /* If "path" is NULL, then "stdin" should be processed. */ char gs_cmd[2*MAXPATHLEN]; char input[MAXPATHLEN]; int status; FILE *fileout; #ifdef MSDOS char *gsargtemp; FILE *gsargfile; #endif fileout = stdout; if (strlen(outfile) != 0) { fileout = fopen(outfile, "w"); if (fileout == NULL) {perror(cmd); exit(1);} } signal(SIGINT, handler); signal(SIGTERM, handler); status = load_pstotext(); if (status!=0) { fprintf(stderr, "%s: internal error %d\n", cmd, status); exit(5); } if (cork) { status = dllfn_pstotextSetCork(pstotextInstance, TRUE); if (status!=0) { fprintf(stderr, "%s: internal error %d\n", cmd, status); exit(5); } } ocr_path = make_temp(OCR_PROLOG); switch (orientation) { case portrait: rotate_path = ""; break; case landscape: rotate_path = make_temp(ROT270_PROLOG); break; case landscapeOther: rotate_path = make_temp(ROT90_PROLOG); break; } if (path==NULL) strcpy(input, "-"); else {strcpy(input, "-- "); strcat(input, path);} #if defined(_Windows) || defined(MSDOS) /* don't support pipes, so write gs output to a temporary file */ if ( (gstemp = scratch_file()) == NULL) { cleanup(); exit(1); } #endif #ifdef MSDOS /* MSDOS has command line length problems */ if ( (gsargtemp = scratch_file()) == NULL) { cleanup(); exit(1); } if ( (gsargfile = fopen(gsargtemp, "w")) == (FILE *)NULL) { cleanup(); exit(1); } fprintf(gsargfile, "-r72 -dNODISPLAY -dFIXEDMEDIA -dDELAYBIND -dWRITESYSTEMDICT %s -dNOPAUSE\n", (debug ? "" : "-q")); fputs(rotate_path, gsargfile); fputs("\n", gsargfile); fputs(ocr_path, gsargfile); fputs("\n", gsargfile); fputs(input, gsargfile); fputs("\n", gsargfile); fclose(gsargfile); sprintf(gs_cmd, "%s @%s %s %s", gscommand, gsargtemp, #if defined(_Windows) || defined(MSDOS) "> ", gstemp #else "", "" #endif ); #else /* !MSDOS */ sprintf(gs_cmd, "%s -r72 -dNODISPLAY -dFIXEDMEDIA -dDELAYBIND -dWRITESYSTEMDICT %s -dNOPAUSE %s %s %s %s %s", gscommand, (debug ? "" : "-q"), ocr_path, rotate_path, input, #if defined(_Windows) || defined(MSDOS) "> ", gstemp #else "", "" #endif ); #endif if (debug) { fputs(gs_cmd, stdout); fputc('\n', stdout); } #if defined(_Windows) || defined(MSDOS) if (system(gs_cmd)) { fprintf(stderr,"\nCan't run (errno=%d):\n %s\n", errno, gs_cmd); cleanup(); exit(1); } gs = fopen(gstemp, "r"); #else gs = popen(gs_cmd, "r"); #endif #ifdef MSDOS if (!debug) unlink(gsargtemp); free(gsargtemp); #endif if( gs==NULL ) {perror(cmd); cleanup(); exit(1);} while (TRUE) { char line[LINELEN]; char *pre, *word, *post; int llx, lly, urx, ury; if (fgets(line, LINELEN, gs)==NULL) break; if (debug) fputs(line, stdout); status = dllfn_pstotextFilter( pstotextInstance, line, &pre, &word, &post, &llx, &lly, &urx, &ury); if (status!=0) { fprintf(stderr, "%s: internal error %d\n", cmd, status); cleanup(); exit(5); } if (word!=NULL) if (!bboxes) { fputs(pre, fileout); fputs(word, fileout); fputs(post, fileout); if (debug) fputc('\n', stdout); } else fprintf(fileout, "%6d\t%6d\t%6d\t%6d\t%s\n", llx, lly, urx, ury, word); } if (fileout != stdout) fclose(fileout); status = cleanup(); if (status!=0) exit(status); }
Expression *expression_unary(int op, Expression *only) { Tac *temp; Tac *result; Symbol *temp_variable; debug("Unary: %d Symbol: %s Type: %d", op, only->result->name, only->result->type->code); //Constant folding if (only->result->type->code == TYPE_NATURAL) { //Fix 5-19-2011 //Since each symbol is added to the constant table only once //if we delete a symbol that was used previously //We wil have problems //Instead - make a new symbol, and do the wiring //Remove from symbol table //symboltable_delete(constantTable, only->result); Symbol *fold_result = make_symbol(); fold_result->type = make_type(TYPE_NATURAL); fold_result->name = (char*)safe_malloc(sizeof(char) * 12); switch (op) { case TAC_NEGATIVE: //only->result->value.integer = - only->result->value.integer; fold_result->value.integer = - only->result->value.integer; //Make new name, overwriting old name //sprintf(only->result->name, "%d", only->result->value.integer); break; } //rewire expression to use new result only->result = fold_result; //Fix name sprintf(only->result->name, "%d", only->result->value.integer); //Reinsert symboltable_insert(constantTable, only->result); //debug("Folded constant to %d", only->result->value.integer); return only; } temp_variable = make_temp(); temp_variable->type = make_type(TYPE_VARIABLE);//Generic variable type //Since we dont have a constant, we make a new tac with a temporary symbol temp = make_tac(TAC_VARIABLE, temp_variable, NULL, NULL); temp->prev = only->tac; //This is result of "calling" operator //FIX 5-19-2011 //The backend looks for the only operand in operand1, not operand 2 //result = make_tac(op, temp->result, NULL, only->result); result = make_tac(op, temp->result, only->result, NULL); result->prev = temp; //Rewire the expression struct to point to the true result only->result = temp->result; only->tac = result; return only; }
Expression *expression_binary(int op, Expression *first, Expression *second) { Tac *temp; Tac *result; Symbol *temp_variable; debug("Binary Op: %d First: %s Type: %d Second: %s Type: %d", op, first->result->name, first->result->type->code, second->result->name, second->result->type->code); //Constant folding //if ((first->result->type->code == TYPE_NATURAL) && (second->result->type->code == TYPE_NATURAL) // && ((op == TAC_ADD) || (op == TAC_SUBTRACT) || (op == TAC_MULTIPLY) || (op == TAC_DIVIDE))) if ((first->result->type->code == TYPE_NATURAL) && (second->result->type->code == TYPE_NATURAL)) { //Fix 5-19-2011 //For same reasons in expression_unary //Remove both symbols from symbol table //symboltable_delete(constantTable, first->result); //symboltable_delete(constantTable, second->result); Symbol* fold_result = make_symbol(); fold_result->type = make_type(TYPE_NATURAL); fold_result->name = (char*)safe_malloc(sizeof(char) * 12); switch (op) { case TAC_ADD: fold_result->value.integer = first->result->value.integer + second->result->value.integer; break; case TAC_SUBTRACT: fold_result->value.integer = first->result->value.integer - second->result->value.integer; break; case TAC_MULTIPLY: fold_result->value.integer = first->result->value.integer * second->result->value.integer; break; case TAC_DIVIDE: fold_result->value.integer = first->result->value.integer / second->result->value.integer; break; case TAC_GT: fold_result->value.integer = first->result->value.integer > second->result->value.integer; break; case TAC_LT: fold_result->value.integer = first->result->value.integer < second->result->value.integer; break; case TAC_GTE: fold_result->value.integer = first->result->value.integer >= second->result->value.integer; break; case TAC_LTE: fold_result->value.integer = first->result->value.integer <= second->result->value.integer; break; case TAC_EQUAL: fold_result->value.integer = first->result->value.integer == second->result->value.integer; break; case TAC_NOTEQUAL: fold_result->value.integer = first->result->value.integer != second->result->value.integer; break; default: die("Unrecognized Binary Operation: %s", op); break; } //Rewire expression first->result = fold_result; //Now fix the symbol name sprintf(first->result->name, "%d", first->result->value.integer); //Insert the symbol back symboltable_insert(constantTable, first->result); //Give back some resources //free(second->result); free(second); debug("Folded constant %d", first->result->value.integer); return first; } //Create temp temp_variable = make_temp(); temp_variable->type = make_type(TYPE_VARIABLE);//Generic variable type temp = make_tac(TAC_VARIABLE, temp_variable, NULL, NULL); temp->prev = join_tac(first->tac, second->tac); //Call operator result = make_tac(op, temp->result, first->result, second->result); result->prev = temp; //Rewire the expression first->result = temp->result; first->tac = result; //Cleanup free(second); return first; }