static void print_constant_terse (FILE *out, JCF *jcf, int index, int expected) { if (! CPOOL_INDEX_IN_RANGE (&jcf->cpool, index)) fprintf (out, "<constant pool index %d not in range>", index); else if (JPOOL_TAG (jcf, index) != expected) { fprintf (out, "<Unexpected constant type "); print_constant (out, jcf, index, 1); fprintf (out, ">"); } else print_constant (out, jcf, index, 0); }
void print_classfile(classfile* c, FILE* out){ fprintf(out, "Magic: %x\n", c->magic); fprintf(out, "Minor: %x\n", c->minor_version); fprintf(out, "Major: %x\n", c->major_version); fprintf(out, "Constant Pool Count: %d\n", c->constant_pool_count); for(int i = 0; i < c->constant_pool_count-1; i++){ fprintf(out, "\t%d\t", i + 1); print_constant(c->constant_pool[i], out); fprintf(out, "\n"); } fprintf(out, "Access Flags: %x\n", c->access_flags); fprintf(out, "This class index: %x\n", c->this_class); fprintf(out, "Super class index: %x\n", c->super_class); fprintf(out, "Interface count: %x\n", c->interfaces_count); fprintf(out, "Fields count: %x\n", c->fields_count); for(int i = 0; i < c->fields_count; i++){ print_field(c, c->fields[i], out); } fprintf(out, "Methods count: %x\n", c->methods_count); for(int i = 0; i < c->methods_count; i++){ print_method(c, c->methods[i], out); } fprintf(out, "Attributes count: %x\n", c->attributes_count); for(int i = 0; i < c->attributes_count; i++){ print_attribute(c, c->attributes[i], out); } }
void print_cls_directives(Output& out, const PreClass* cls) { print_cls_enum_ty(out, cls); print_cls_used_traits(out, cls); for (auto& c : cls->allConstants()) print_constant(out, &c); for (auto& p : cls->allProperties()) print_property(out, &p); for (auto* m : cls->allMethods()) print_method(out, m); }
/* generate code from function call arguments*/ int print_arg(Expr expr, int reg, char* proc_id, int paramNum,char* callee) { int curr_reg = reg; int next_reg; int ID_type; int ID_type2; switch (expr->kind) { Type ID_type; int stackNo; case EXPR_ID: ID_type = getType(proc_id,expr->id); stackNo = getStackSlotNum(proc_id, expr->id); if(isParamRef(callee,paramNum)==0){ printf("load r%d, %d\n", curr_reg,stackNo); } if(isParamRef(callee,paramNum)==1){ if(isRef(proc_id, expr->id)==1) printf("load r%d, %d\n", curr_reg,stackNo); else printf("load_address r%d, %d\n", curr_reg,stackNo); } break; case EXPR_CONST: print_constant(expr->constant, curr_reg); break; case EXPR_BINOP: ID_type = getExprType(expr->e1, proc_id); ID_type2 = getExprType(expr->e2, proc_id); curr_reg = print_expr(expr->e1, curr_reg, proc_id); next_reg = print_expr(expr->e2, curr_reg + 1, proc_id); print_binop_string(expr->binop, curr_reg, next_reg, ID_type, ID_type2); break; case EXPR_RELOP: ID_type = getExprType(expr->e1, proc_id); ID_type2 = getExprType(expr->e2, proc_id); curr_reg = print_expr(expr->e1, curr_reg, proc_id); next_reg = print_expr(expr->e2, curr_reg + 1, proc_id); print_relop_string(expr->relop, curr_reg, next_reg, ID_type, ID_type2); break; case EXPR_UNOP: ID_type = getExprType(expr->e1, proc_id); print_unop_string(expr->unop, curr_reg, ID_type); break; } if(getExprType(expr,proc_id) != getParamType(callee,paramNum)){ //printf("%d,%d ",getExprType(expr), getParamType(callee,paramNum)); printf("int_to_real r%d, r%d\n", curr_reg,curr_reg); } return curr_reg; }
static void print_constant_pool (JCF *jcf) { int i; for (i = 1; i < JPOOL_SIZE(jcf); i++) { int kind = JPOOL_TAG (jcf, i); fprintf (out, "#%d: ", i); print_constant (out, jcf, i, 2); fprintf (out, "\n"); if (kind == CONSTANT_Double || kind == CONSTANT_Long) i++; /* These take up two slots in the constant table */ } }
static void print_constant_ref (FILE *stream, JCF *jcf, int index) { if (index <= 0 || index >= JPOOL_SIZE(jcf)) fprintf (stream, "<out of range>"); else { if (flag_print_constant_pool) fprintf (stream, "#%d=", index); fputc ('<', stream); print_constant (stream, jcf, index, 1); fputc ('>', stream); } }
/** print_identifier print information about an * identifier's definition and type. * */ void cx_symtab_node::print_identifier(void) const { switch (defn.how) { case dc_constant: print_constant(); break; case dc_type: print_type(); break; case dc_variable: case dc_member: print_var_or_field(); break; default: break; } }
std::ostream& node(const ast::abstract::Node& node) { switch(node) { case ast::ExternNode: print_extern(dynamic_cast<const ast::Extern&>(node)); break; case ast::FuncNode: print_func(dynamic_cast<const ast::Func&>(node)); break; case ast::VariableNode: print_variable(dynamic_cast<const ast::Variable&>(node)); break; case ast::ConstantNode: print_constant(dynamic_cast<const ast::Constant&>(node)); break; case ast::CastNode: print_cast(dynamic_cast<const ast::Cast&>(node)); break; case ast::UnOpNode: print_un_op(dynamic_cast<const ast::UnOp&>(node)); break; case ast::BinOpNode: print_bin_op(dynamic_cast<const ast::BinOp&>(node)); break; case ast::CallNode: print_call(dynamic_cast<const ast::Call&>(node)); break; case ast::ReturnNode: print_return(dynamic_cast<const ast::Return&>(node)); break; case ast::BranchNode: print_branch(dynamic_cast<const ast::Branch&>(node)); break; case ast::AssignmentNode: print_assignment(dynamic_cast<const ast::Assignment&>(node)); break; case ast::WhileNode: print_while(dynamic_cast<const ast::While&>(node)); break; case ast::VoidContextNode: print_void_context(dynamic_cast<const ast::VoidContext&>(node)); break; default: stream << "[Unknown Node] ??"; } return stream; }
/* Generating code for expression */ int print_expr(Expr expr, int reg, char* proc_id) { int curr_reg = reg; int next_reg; int ID_type; int ID_type2; switch (expr->kind) { Type ID_type; int stackNo; case EXPR_ID: ID_type = getType(proc_id,expr->id); stackNo = getStackSlotNum(proc_id, expr->id); printf("load r%d, %d\n", curr_reg,stackNo); if(isRef(proc_id, expr->id)) printf("load_indirect r%d, r%d\n", curr_reg ,curr_reg); break; case EXPR_CONST: print_constant(expr->constant, curr_reg); break; case EXPR_BINOP: ID_type = getExprType(expr->e1, proc_id); ID_type2 = getExprType(expr->e2, proc_id); curr_reg = print_expr(expr->e1, curr_reg, proc_id); next_reg = print_expr(expr->e2, curr_reg + 1, proc_id); print_binop_string(expr->binop, curr_reg, next_reg, ID_type, ID_type2); break; case EXPR_RELOP: ID_type = getExprType(expr->e1, proc_id); ID_type2 = getExprType(expr->e2, proc_id); curr_reg = print_expr(expr->e1, curr_reg, proc_id); next_reg = print_expr(expr->e2, curr_reg + 1, proc_id); print_relop_string(expr->relop, curr_reg, next_reg, ID_type, ID_type2); break; case EXPR_UNOP: ID_type = getExprType(expr->e1, proc_id); print_unop_string(expr->unop, curr_reg, ID_type); break; case EXPR_ARRAY: curr_reg = calculate_offset(expr->es, curr_reg, expr->id, proc_id); break; } return curr_reg; }
static void print_entity(const entity_t *entity) { print_indent(); switch (entity->kind) { case ENTITY_FUNCTION: print_function(&entity->function); break; case ENTITY_CONCEPT: print_concept(&entity->concept); break; case ENTITY_VARIABLE: print_variable(&entity->variable); break; case ENTITY_TYPEALIAS: print_typealias(&entity->typealias); break; case ENTITY_CONSTANT: print_constant(&entity->constant); break; case ENTITY_CONCEPT_FUNCTION: case ENTITY_FUNCTION_PARAMETER: case ENTITY_ERROR: // TODO fprintf(out, "some entity of type '%s'\n", get_entity_kind_name(entity->kind)); break; case ENTITY_TYPE_VARIABLE: case ENTITY_LABEL: break; case ENTITY_INVALID: fprintf(out, "invalid entity (%s)\n", get_entity_kind_name(entity->kind)); break; } }
void BytecodePrinter::print_attributes(int bci, outputStream* st) { // Show attributes of pre-rewritten codes Bytecodes::Code code = Bytecodes::java_code(raw_code()); // If the code doesn't have any fields there's nothing to print. // note this is ==1 because the tableswitch and lookupswitch are // zero size (for some reason) and we want to print stuff out for them. if (Bytecodes::length_for(code) == 1) { st->cr(); return; } switch(code) { // Java specific bytecodes only matter. case Bytecodes::_bipush: st->print_cr(" " INT32_FORMAT, get_byte()); break; case Bytecodes::_sipush: st->print_cr(" " INT32_FORMAT, get_short()); break; case Bytecodes::_ldc: if (Bytecodes::uses_cp_cache(raw_code())) { print_constant(get_index_u1_cpcache(), st); } else { print_constant(get_index_u1(), st); } break; case Bytecodes::_ldc_w: case Bytecodes::_ldc2_w: if (Bytecodes::uses_cp_cache(raw_code())) { print_constant(get_index_u2_cpcache(), st); } else { print_constant(get_index_u2(), st); } break; case Bytecodes::_iload: case Bytecodes::_lload: case Bytecodes::_fload: case Bytecodes::_dload: case Bytecodes::_aload: case Bytecodes::_istore: case Bytecodes::_lstore: case Bytecodes::_fstore: case Bytecodes::_dstore: case Bytecodes::_astore: st->print_cr(" #%d", get_index_special()); break; case Bytecodes::_iinc: { int index = get_index_special(); jint offset = is_wide() ? get_short(): get_byte(); st->print_cr(" #%d " INT32_FORMAT, index, offset); } break; case Bytecodes::_newarray: { BasicType atype = (BasicType)get_index_u1(); const char* str = type2name(atype); if (str == NULL || atype == T_OBJECT || atype == T_ARRAY) { assert(false, "Unidentified basic type"); } st->print_cr(" %s", str); } break; case Bytecodes::_anewarray: { int klass_index = get_index_u2(); ConstantPool* constants = method()->constants(); Symbol* name = constants->klass_name_at(klass_index); st->print_cr(" %s ", name->as_C_string()); } break; case Bytecodes::_multianewarray: { int klass_index = get_index_u2(); int nof_dims = get_index_u1(); ConstantPool* constants = method()->constants(); Symbol* name = constants->klass_name_at(klass_index); st->print_cr(" %s %d", name->as_C_string(), nof_dims); } break; case Bytecodes::_ifeq: case Bytecodes::_ifnull: case Bytecodes::_iflt: case Bytecodes::_ifle: case Bytecodes::_ifne: case Bytecodes::_ifnonnull: case Bytecodes::_ifgt: case Bytecodes::_ifge: case Bytecodes::_if_icmpeq: case Bytecodes::_if_icmpne: case Bytecodes::_if_icmplt: case Bytecodes::_if_icmpgt: case Bytecodes::_if_icmple: case Bytecodes::_if_icmpge: case Bytecodes::_if_acmpeq: case Bytecodes::_if_acmpne: case Bytecodes::_goto: case Bytecodes::_jsr: st->print_cr(" %d", bci + get_short()); break; case Bytecodes::_goto_w: case Bytecodes::_jsr_w: st->print_cr(" %d", bci + get_int()); break; case Bytecodes::_ret: st->print_cr(" %d", get_index_special()); break; case Bytecodes::_tableswitch: { align(); int default_dest = bci + get_int(); int lo = get_int(); int hi = get_int(); int len = hi - lo + 1; jint* dest = NEW_RESOURCE_ARRAY(jint, len); for (int i = 0; i < len; i++) { dest[i] = bci + get_int(); } st->print(" %d " INT32_FORMAT " " INT32_FORMAT " ", default_dest, lo, hi); int first = true; for (int ll = lo; ll <= hi; ll++, first = false) { int idx = ll - lo; const char *format = first ? " %d:" INT32_FORMAT " (delta: %d)" : ", %d:" INT32_FORMAT " (delta: %d)"; st->print(format, ll, dest[idx], dest[idx]-bci); } st->cr(); } break; case Bytecodes::_lookupswitch: { align(); int default_dest = bci + get_int(); int len = get_int(); jint* key = NEW_RESOURCE_ARRAY(jint, len); jint* dest = NEW_RESOURCE_ARRAY(jint, len); for (int i = 0; i < len; i++) { key [i] = get_int(); dest[i] = bci + get_int(); }; st->print(" %d %d ", default_dest, len); bool first = true; for (int ll = 0; ll < len; ll++, first = false) { const char *format = first ? " " INT32_FORMAT ":" INT32_FORMAT : ", " INT32_FORMAT ":" INT32_FORMAT ; st->print(format, key[ll], dest[ll]); } st->cr(); } break; case Bytecodes::_putstatic: case Bytecodes::_getstatic: case Bytecodes::_putfield: case Bytecodes::_getfield: print_field_or_method(get_index_u2_cpcache(), st); break; case Bytecodes::_invokevirtual: case Bytecodes::_invokespecial: case Bytecodes::_invokestatic: print_field_or_method(get_index_u2_cpcache(), st); break; case Bytecodes::_invokeinterface: { int i = get_index_u2_cpcache(); int n = get_index_u1(); get_byte(); // ignore zero byte print_field_or_method(i, st); } break; case Bytecodes::_invokedynamic: print_field_or_method(get_index_u4(), st); break; case Bytecodes::_new: case Bytecodes::_checkcast: case Bytecodes::_instanceof: { int i = get_index_u2(); ConstantPool* constants = method()->constants(); Symbol* name = constants->klass_name_at(i); st->print_cr(" %d <%s>", i, name->as_C_string()); } break; case Bytecodes::_wide: // length is zero not one, but printed with no more info. break; default: ShouldNotReachHere(); break; } }
int main(void) { print_constant("syscall-o-rdonly", O_RDONLY); print_constant("syscall-o-wronly", O_WRONLY); print_constant("syscall-o-creat", O_CREAT); return 0; }
static void print_constant (FILE *out, JCF *jcf, int index, int verbosity) { int j, n; jlong num; const char *str; int kind = JPOOL_TAG (jcf, index); switch (kind) { case CONSTANT_Class: n = JPOOL_USHORT1 (jcf, index); if (verbosity > 0) { if (verbosity > 1) fprintf (out, "Class name: %d=", n); else fprintf (out, "Class "); } if (! CPOOL_INDEX_IN_RANGE (&jcf->cpool, n)) fprintf (out, "<out of range>"); else if (verbosity < 2 && JPOOL_TAG (jcf, n) == CONSTANT_Utf8) { int len = JPOOL_UTF_LENGTH (jcf, n); jcf_print_utf8_replace (out, JPOOL_UTF_DATA(jcf,n), len, '/', '.'); } else print_constant_terse (out, jcf, n, CONSTANT_Utf8); break; case CONSTANT_Fieldref: str = "Field"; goto field_or_method; case CONSTANT_Methodref: str = "Method"; goto field_or_method; case CONSTANT_InterfaceMethodref: str = "InterfaceMethod"; goto field_or_method; field_or_method: { uint16 tclass = JPOOL_USHORT1 (jcf, index); uint16 name_and_type = JPOOL_USHORT2 (jcf, index); if (verbosity == 2) fprintf (out, "%sref class: %d=", str, tclass); else if (verbosity > 0) fprintf (out, "%s ", str); print_constant_terse (out, jcf, tclass, CONSTANT_Class); if (verbosity < 2) fprintf (out, "."); else fprintf (out, " name_and_type: %d=<", name_and_type); print_constant_terse (out, jcf, name_and_type, CONSTANT_NameAndType); if (verbosity == 2) fputc ('>', out); } break; case CONSTANT_String: j = JPOOL_USHORT1 (jcf, index); if (verbosity > 0) { if (verbosity > 1) fprintf (out, "String %d=", j); else fprintf (out, "String "); } print_constant_terse (out, jcf, j, CONSTANT_Utf8); break; case CONSTANT_Integer: if (verbosity > 0) fprintf (out, "Integer "); num = JPOOL_INT (jcf, index); goto integer; case CONSTANT_Long: if (verbosity > 0) fprintf (out, "Long "); num = JPOOL_LONG (jcf, index); goto integer; integer: { char buffer[25]; format_int (buffer, num, 10); fprintf (out, "%s", buffer); if (verbosity > 1) { format_uint (buffer, (uint64)num, 16); fprintf (out, "=0x%s", buffer); } } break; case CONSTANT_Float: { jfloat fnum = JPOOL_FLOAT (jcf, index); if (verbosity > 0) fputs ("Float ", out); if (fnum.negative) putc ('-', out); if (JFLOAT_FINITE (fnum)) { int dummy; int exponent = fnum.exponent - JFLOAT_EXP_BIAS; double f; uint32 mantissa = fnum.mantissa; if (fnum.exponent == 0) /* Denormal. */ exponent++; else /* Normal; add the implicit bit. */ mantissa |= ((uint32)1 << 23); f = frexp ((float) mantissa, &dummy); f = ldexp (f, exponent + 1); fprintf (out, "%.10g", f); } else { if (fnum.mantissa == 0) fputs ("Inf", out); else if (fnum.mantissa & JFLOAT_QNAN_MASK) fprintf (out, "QNaN(%u)", (fnum.mantissa & ~JFLOAT_QNAN_MASK)); else fprintf (out, "SNaN(%u)", (fnum.mantissa & ~JFLOAT_QNAN_MASK)); } if (verbosity > 1) fprintf (out, ", bits = 0x%08lx", (unsigned long) JPOOL_UINT (jcf, index)); break; } case CONSTANT_Double: { jdouble dnum = JPOOL_DOUBLE (jcf, index); if (verbosity > 0) fputs ("Double ", out); if (dnum.negative) putc ('-', out); if (JDOUBLE_FINITE (dnum)) { int dummy; int exponent = dnum.exponent - JDOUBLE_EXP_BIAS; double d; uint64 mantissa = ((((uint64) dnum.mantissa0) << 32) + dnum.mantissa1); if (dnum.exponent == 0) /* Denormal. */ exponent++; else /* Normal; add the implicit bit. */ mantissa |= ((uint64)1 << 52); d = frexp ((double) mantissa, &dummy); d = ldexp (d, exponent + 1); fprintf (out, "%.20g", d); } else { uint64 mantissa = dnum.mantissa0 & ~JDOUBLE_QNAN_MASK; mantissa = (mantissa << 32) + dnum.mantissa1; if (dnum.mantissa0 == 0 && dnum.mantissa1 == 0) fputs ("Inf", out); else if (dnum.mantissa0 & JDOUBLE_QNAN_MASK) fprintf (out, "QNaN(%" HOST_LONG_LONG_FORMAT "u)", (unsigned long long)mantissa); else fprintf (out, "SNaN(%" HOST_LONG_LONG_FORMAT "u)", (unsigned long long)mantissa); } if (verbosity > 1) { int32 hi, lo; hi = JPOOL_UINT (jcf, index); lo = JPOOL_UINT (jcf, index + 1); fprintf (out, ", bits = 0x%08lx%08lx", (unsigned long) hi, (unsigned long) lo); } break; } case CONSTANT_NameAndType: { uint16 name = JPOOL_USHORT1 (jcf, index); uint16 sig = JPOOL_USHORT2 (jcf, index); if (verbosity > 0) { if (verbosity > 1) fprintf (out, "NameAndType name: %d=", name); else fprintf (out, "NameAndType "); } print_name (out, jcf, name); if (verbosity <= 1) fputc (' ', out); else fprintf (out, ", signature: %d=", sig); print_signature (out, jcf, sig, 0); } break; case CONSTANT_Utf8: { const unsigned char *str = JPOOL_UTF_DATA (jcf, index); int length = JPOOL_UTF_LENGTH (jcf, index); if (verbosity > 0) { /* Print as 8-bit bytes. */ fputs ("Utf8: \"", out); while (--length >= 0) jcf_print_char (out, *str++); } else { /* Print as Unicode. */ fputc ('\"', out); jcf_print_utf8 (out, str, length); } fputc ('\"', out); } break; case CONSTANT_MethodHandle: { int kind = JPOOL_USHORT1 (jcf, index); if (verbosity > 0) fprintf (out, "MethodHandle kind: %d=", kind); switch(kind) { case 1: case 2: case 3: case 4: if (verbosity > 0) fprintf (out, "Fieldref: %ld=", (long) JPOOL_USHORT2 (jcf, index)); print_constant (out, jcf, JPOOL_USHORT2 (jcf, index), 0); break; case 5: case 6: case 7: case 8: if (verbosity > 0) fprintf (out, "Methodref: %ld=", (long) JPOOL_USHORT2 (jcf, index)); print_constant (out, jcf, JPOOL_USHORT2 (jcf, index), 0); break; case 9: if (verbosity > 0) fprintf (out, "InterfaceMethodref: %ld=", (long) JPOOL_USHORT2 (jcf, index)); print_constant (out, jcf, JPOOL_USHORT2 (jcf, index), 0); break; } break; } case CONSTANT_MethodType: if (verbosity > 0) fprintf (out, "MethodType %ld: ", (long) JPOOL_USHORT1 (jcf, index)); print_signature (out, jcf, JPOOL_USHORT1 (jcf, index), 0); break; case CONSTANT_InvokeDynamic: { uint16 name_and_type = JPOOL_USHORT2 (jcf, index); if (verbosity > 0) fprintf (out, "InvokeDynamic: "); fprintf (out, "bootstrap_method: %ld ", (long) JPOOL_USHORT1 (jcf, index)); if (verbosity == 2) fprintf (out, " name_and_type: %d=<", name_and_type); print_constant_terse (out, jcf, name_and_type, CONSTANT_NameAndType); if (verbosity == 2) fputc ('>', out); break; } default: fprintf (out, "(Unknown constant type %d)", kind); } }