Permalink
Browse files

Move remaining functions out of embed.c. move packfile-related functi…

…ons to src/packfile/api.c. Move disassembly-related functions into a new disassemble.c
  • Loading branch information...
1 parent 64a1204 commit 796e991ab3672631412006ee418aa056222bb5c9 @Whiteknight Whiteknight committed Aug 6, 2011
Showing with 366 additions and 73 deletions.
  1. +2 −2 MANIFEST
  2. +4 −9 config/gen/makefiles/root.in
  3. +47 −0 include/parrot/disassemble.h
  4. +6 −0 include/parrot/packfile.h
  5. +243 −0 src/disassemble.c
  6. +0 −62 src/embed.c
  7. +64 −0 src/packfile/api.c
View
4 MANIFEST
@@ -890,8 +890,8 @@ include/parrot/context.h [main]include
include/parrot/core_types.h [main]include
include/parrot/datatypes.h [main]include
include/parrot/debugger.h [main]include
+include/parrot/disassemble.h [main]include
include/parrot/dynext.h [main]include
-include/parrot/embed.h [main]include
include/parrot/encoding.h [main]include
include/parrot/enums.h [main]include
include/parrot/exceptions.h [main]include
@@ -1186,6 +1186,7 @@ src/call/ops.c []
src/call/pcc.c []
src/datatypes.c []
src/debug.c []
+src/disassemble.c []
src/dynext.c []
src/dynoplibs/Defines.in []
src/dynoplibs/README []doc
@@ -1212,7 +1213,6 @@ src/dynpmc/pccmethod_test.pmc []
src/dynpmc/rational.pmc []
src/dynpmc/rotest.pmc []
src/dynpmc/subproxy.pmc []
-src/embed.c []
src/embed/api.c []
src/embed/bytecode.c []
src/embed/embed_private.h []
View
13 config/gen/makefiles/root.in
@@ -442,8 +442,8 @@ INTERP_O_FILES = \
src/core_pmcs$(O) \
src/datatypes$(O) \
src/debug$(O) \
+ src/disassemble$(O) \
src/dynext$(O) \
- src/embed$(O) \
src/embed/api$(O) \
src/embed/bytecode$(O) \
src/embed/strings$(O) \
@@ -713,7 +713,6 @@ STR_FILES = \
src/string/api.str \
src/string/encoding.str \
src/sub.str \
- src/embed.str \
$(CLASS_STR_FILES)
src/string/private_cstring.h : $(STR_FILES) $(BUILD_TOOLS_DIR)/c2str.pl
@@ -1736,19 +1735,15 @@ src/runcore/cores$(O) : src/runcore/cores.str \
$(INC_DIR)/runcore_api.h $(INC_DIR)/runcore_trace.h \
$(PARROT_H_HEADERS)
-src/embed$(O) : \
+src/diassemble$(O) : \
$(PARROT_H_HEADERS) \
- src/embed.c \
- src/embed.str \
+ src/disassemble.c \
$(INC_DIR)/oplib/ops.h \
- $(INC_DIR)/extend.h \
- $(INC_DIR)/embed.h \
$(INC_DIR)/runcore_api.h \
$(INC_DIR)/extend_vtable.h \
$(INC_DIR)/oplib/core_ops.h \
include/pmc/pmc_sub.h \
- include/imcc/embed.h \
- include/imcc/yyscanner.h \
+ include/pmc/pmc_callcontext.h
src/embed/api$(O) : \
$(PARROT_H_HEADERS) \
View
47 include/parrot/disassemble.h
@@ -0,0 +1,47 @@
+/* embed.h
+ * Copyright (C) 2001-2010, Parrot Foundation.
+ * Overview:
+ * This is the Parrot embedding system--the only part of Parrot that
+ * the outside world should see.
+ * References:
+ * embed.c, docs/embed.pod.
+ */
+
+#ifndef PARROT_DISASSEMBLE_H_GUARD
+#define PARROT_DISASSEMBLE_H_GUARD
+
+#include "parrot/parrot.h"
+#include "parrot/core_types.h" /* types used */
+#include "parrot/compiler.h" /* compiler capabilities */
+#include "parrot/config.h" /* PARROT_VERSION... */
+#include "parrot/interpreter.h" /* give us the interpreter flags */
+#include "parrot/warnings.h" /* give us the warnings flags */
+
+typedef enum {
+ enum_DIS_BARE = 1,
+ enum_DIS_HEADER = 2
+} Parrot_disassemble_options;
+
+/* HEADERIZER BEGIN: src/disassemble.c */
+/* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will be lost. */
+
+PARROT_EXPORT
+void Parrot_disassemble(PARROT_INTERP,
+ ARGIN_NULLOK(const char *outfile),
+ Parrot_disassemble_options options)
+ __attribute__nonnull__(1);
+
+#define ASSERT_ARGS_Parrot_disassemble __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
+ PARROT_ASSERT_ARG(interp))
+/* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will be lost. */
+/* HEADERIZER END: src/disassemble.c */
+
+
+#endif /* PARROT_DISASSEMBLE_H_GUARD */
+
+/*
+ * Local variables:
+ * c-file-style: "parrot"
+ * End:
+ * vim: expandtab shiftwidth=4 cinoptions='\:2=2' :
+ */
View
6 include/parrot/packfile.h
@@ -772,6 +772,10 @@ void Parrot_pf_mark_packfile(PARROT_INTERP, ARGMOD_NULLOK(PackFile * pf))
__attribute__nonnull__(1)
FUNC_MODIFIES(* pf);
+PARROT_CANNOT_RETURN_NULL
+PMC* set_current_sub(PARROT_INTERP)
+ __attribute__nonnull__(1);
+
#define ASSERT_ARGS_do_sub_pragmas __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp) \
, PARROT_ASSERT_ARG(pfpmc))
@@ -893,6 +897,8 @@ void Parrot_pf_mark_packfile(PARROT_INTERP, ARGMOD_NULLOK(PackFile * pf))
, PARROT_ASSERT_ARG(pbc))
#define ASSERT_ARGS_Parrot_pf_mark_packfile __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
+#define ASSERT_ARGS_set_current_sub __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
+ PARROT_ASSERT_ARG(interp))
/* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will be lost. */
/* HEADERIZER END: src/packfile/api.c */
View
243 src/disassemble.c
@@ -0,0 +1,243 @@
+/*
+Copyright (C) 2001-2010, Parrot Foundation.
+
+=head1 NAME
+
+src/disassemble.c - The Parrot Disassembler
+
+=head1 DESCRIPTION
+
+This file implements some logic for the parrot disassembler, and related
+routines.
+
+=head2 Functions
+
+=over 4
+
+=cut
+
+*/
+
+#include "parrot/parrot.h"
+#include "parrot/disassemble.h"
+#include "parrot/oplib/ops.h"
+#include "pmc/pmc_sub.h"
+#include "parrot/oplib/core_ops.h"
+
+/* HEADERIZER HFILE: include/parrot/disassemble.h */
+
+/* HEADERIZER BEGIN: static */
+/* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will be lost. */
+
+static void print_constant_table(PARROT_INTERP, ARGIN(PMC *output))
+ __attribute__nonnull__(1)
+ __attribute__nonnull__(2);
+
+#define ASSERT_ARGS_print_constant_table __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
+ PARROT_ASSERT_ARG(interp) \
+ , PARROT_ASSERT_ARG(output))
+/* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will be lost. */
+/* HEADERIZER END: static */
+
+
+/*
+
+=item C<static void print_constant_table(PARROT_INTERP, PMC *output)>
+
+Prints the contents of the constants table.
+
+=cut
+
+*/
+static void
+print_constant_table(PARROT_INTERP, ARGIN(PMC *output))
+{
+ ASSERT_ARGS(print_constant_table)
+ const PackFile_ConstTable *ct = interp->code->const_table;
+ INTVAL i;
+
+ /* TODO: would be nice to print the name of the file as well */
+ Parrot_io_fprintf(interp, output, "=head1 Constant-table\n\n");
+
+ for (i = 0; i < ct->num.const_count; i++)
+ Parrot_io_fprintf(interp, output, "NUM_CONST(%d): %f\n", i, ct->num.constants[i]);
+
+ for (i = 0; i < ct->str.const_count; i++)
+ Parrot_io_fprintf(interp, output, "STR_CONST(%d): %S\n", i, ct->str.constants[i]);
+
+ for (i = 0; i < ct->pmc.const_count; i++) {
+ PMC * const c = ct->pmc.constants[i];
+ Parrot_io_fprintf(interp, output, "PMC_CONST(%d): ", i);
+
+ switch (c->vtable->base_type) {
+ /* each PBC file has a ParrotInterpreter, but it can't
+ * stringify by itself */
+ case enum_class_ParrotInterpreter:
+ Parrot_io_fprintf(interp, output, "'ParrotInterpreter'");
+ break;
+
+ /* FixedIntegerArrays used for signatures, handy to print */
+ case enum_class_FixedIntegerArray:
+ {
+ const INTVAL n = VTABLE_elements(interp, c);
+ INTVAL j;
+ Parrot_io_fprintf(interp, output, "[");
+
+ for (j = 0; j < n; ++j) {
+ const INTVAL val = VTABLE_get_integer_keyed_int(interp, c, j);
+ Parrot_io_fprintf(interp, output, "%d", val);
+ if (j < n - 1)
+ Parrot_io_fprintf(interp, output, ",");
+ }
+ Parrot_io_fprintf(interp, output, "]");
+ break;
+ }
+ case enum_class_NameSpace:
+ case enum_class_String:
+ case enum_class_Key:
+ case enum_class_ResizableStringArray:
+ {
+ STRING * const s = VTABLE_get_string(interp, c);
+ if (s)
+ Parrot_io_fprintf(interp, output, "%Ss", s);
+ break;
+ }
+ case enum_class_Sub:
+ Parrot_io_fprintf(interp, output, "%S", VTABLE_get_string(interp, c));
+ break;
+ default:
+ Parrot_io_fprintf(interp, output, "(PMC constant)");
+ break;
+ }
+
+ Parrot_io_fprintf(interp, output, "\n");
+ }
+
+ Parrot_io_fprintf(interp, output, "\n=cut\n\n");
+}
+
+
+
+/*
+
+=item C<void Parrot_disassemble(PARROT_INTERP, const char *outfile,
+Parrot_disassemble_options options)>
+
+Disassembles and prints out the interpreter's bytecode.
+
+This is used by the Parrot disassembler.
+
+TODO: Move this to a dedicated file, or some place more related to disassembly.
+
+=cut
+
+*/
+
+PARROT_EXPORT
+void
+Parrot_disassemble(PARROT_INTERP,
+ ARGIN_NULLOK(const char *outfile), Parrot_disassemble_options options)
+{
+ ASSERT_ARGS(Parrot_disassemble)
+ PDB_line_t *line;
+ PDB_t * const pdb = mem_gc_allocate_zeroed_typed(interp, PDB_t);
+ int num_mappings = 0;
+ int curr_mapping = 0;
+ int op_code_seq_num = 0;
+ int debugs;
+ PMC *output;
+
+ if (outfile != NULL) {
+ output = Parrot_io_open_handle(interp, PMCNULL,
+ Parrot_str_new(interp, outfile, 0),
+ Parrot_str_new_constant(interp, "tw"));
+ }
+ else
+ output = Parrot_io_stdhandle(interp, PIO_STDOUT_FILENO, PMCNULL);
+
+ interp->pdb = pdb;
+ pdb->cur_opcode = interp->code->base.data;
+
+ PDB_disassemble(interp, NULL);
+
+ line = pdb->file->line;
+ debugs = (interp->code->debugs != NULL);
+
+ print_constant_table(interp, output);
+ if (options & enum_DIS_HEADER)
+ return;
+
+ if (!(options & enum_DIS_BARE))
+ Parrot_io_fprintf(interp, output, "# %12s-%12s", "Seq_Op_Num", "Relative-PC");
+
+ if (debugs) {
+ if (!(options & enum_DIS_BARE))
+ Parrot_io_fprintf(interp, output, " %6s:\n", "SrcLn#");
+ num_mappings = interp->code->debugs->num_mappings;
+ }
+ else {
+ Parrot_io_fprintf(interp, output, "\n");
+ }
+
+ while (line->next) {
+ const char *c;
+
+ /* Parrot_io_fprintf(interp, output, "%i < %i %i == %i \n", curr_mapping,
+ * num_mappings, op_code_seq_num,
+ * interp->code->debugs->mappings[curr_mapping].offset); */
+
+ if (debugs && curr_mapping < num_mappings) {
+ if (op_code_seq_num == interp->code->debugs->mappings[curr_mapping].offset) {
+ const int filename_const_offset =
+ interp->code->debugs->mappings[curr_mapping].filename;
+ Parrot_io_fprintf(interp, output, "# Current Source Filename '%Ss'\n",
+ interp->code->const_table->str.constants[filename_const_offset]);
+ ++curr_mapping;
+ }
+ }
+
+ if (!(options & enum_DIS_BARE))
+ Parrot_io_fprintf(interp, output, "%012i-%012i",
+ op_code_seq_num, line->opcode - interp->code->base.data);
+
+ if (debugs && !(options & enum_DIS_BARE))
+ Parrot_io_fprintf(interp, output, " %06i: ",
+ interp->code->debugs->base.data[op_code_seq_num]);
+
+ /* If it has a label print it */
+ if (line->label)
+ Parrot_io_fprintf(interp, output, "L%li:\t", line->label->number);
+ else
+ Parrot_io_fprintf(interp, output, "\t");
+
+ c = pdb->file->source + line->source_offset;
+
+ while (c && *c != '\n')
+ Parrot_io_fprintf(interp, output, "%c", *(c++));
+
+ Parrot_io_fprintf(interp, output, "\n");
+ line = line->next;
+ ++op_code_seq_num;
+ }
+ if (outfile != NULL)
+ Parrot_io_close_handle(interp, output);
+
+ return;
+}
+
+
+/*
+
+=back
+
+
+=cut
+
+*/
+
+/*
+ * Local variables:
+ * c-file-style: "parrot"
+ * End:
+ * vim: expandtab shiftwidth=4 cinoptions='\:2=2' :
+ */
View
62 src/embed.c
@@ -45,68 +45,6 @@ static void print_constant_table(PARROT_INTERP, ARGIN(PMC *output))
/* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will be lost. */
/* HEADERIZER END: static */
-/*
-
-=item C<PMC* set_current_sub(PARROT_INTERP)>
-
-Search the fixup table for a PMC matching the argument. On a match,
-set up the appropriate context.
-
-If no match, set up a dummy PMC entry. In either case, return a
-pointer to the PMC.
-
-DEPRECATED: use Parrot_pf_get_packfile_main_sub instead
-
-=cut
-
-*/
-
-PARROT_CANNOT_RETURN_NULL
-PMC*
-set_current_sub(PARROT_INTERP)
-{
- ASSERT_ARGS(set_current_sub)
- PMC *new_sub_pmc;
-
- PackFile_ByteCode * const cur_cs = interp->code;
- PackFile_ConstTable * const ct = cur_cs->const_table;
- STRING * const SUB = CONST_STRING(interp, "Sub");
-
- opcode_t i;
-
- /*
- * Walk the fixup table. The first Sub-like entry should be our
- * entry point with the address at our resume_offset.
- */
-
- for (i = 0; i < ct->pmc.const_count; i++) {
- PMC * const sub_pmc = ct->pmc.constants[i];
- if (VTABLE_isa(interp, sub_pmc, SUB)) {
- Parrot_Sub_attributes *sub;
-
- PMC_get_sub(interp, sub_pmc, sub);
- if (sub->seg == cur_cs) {
- const size_t offs = sub->start_offs;
-
- if (offs == interp->resume_offset) {
- Parrot_pcc_set_sub(interp, CURRENT_CONTEXT(interp), sub_pmc);
- Parrot_pcc_set_HLL(interp, CURRENT_CONTEXT(interp), sub->HLL_id);
- return sub_pmc;
- }
-
- break;
- }
- }
- }
-
- /* If we didn't find anything, put a dummy PMC into current_sub.
- The default values set by SUb.init are appropriate for the
- dummy, don't need additional settings. */
- new_sub_pmc = Parrot_pmc_new(interp, enum_class_Sub);
- Parrot_pcc_set_sub(interp, CURRENT_CONTEXT(interp), new_sub_pmc);
-
- return new_sub_pmc;
-}
/*
View
64 src/packfile/api.c
@@ -2709,6 +2709,70 @@ read_pbc_file_packfile(PARROT_INTERP, ARGIN(STRING * const fullname),
/*
+=item C<PMC* set_current_sub(PARROT_INTERP)>
+
+Search the fixup table for a PMC matching the argument. On a match,
+set up the appropriate context.
+
+If no match, set up a dummy PMC entry. In either case, return a
+pointer to the PMC.
+
+DEPRECATED: use Parrot_pf_get_packfile_main_sub instead
+
+=cut
+
+*/
+
+PARROT_CANNOT_RETURN_NULL
+PMC*
+set_current_sub(PARROT_INTERP)
+{
+ ASSERT_ARGS(set_current_sub)
+ PMC *new_sub_pmc;
+
+ PackFile_ByteCode * const cur_cs = interp->code;
+ PackFile_ConstTable * const ct = cur_cs->const_table;
+ STRING * const SUB = CONST_STRING(interp, "Sub");
+
+ opcode_t i;
+
+ /*
+ * Walk the fixup table. The first Sub-like entry should be our
+ * entry point with the address at our resume_offset.
+ */
+
+ for (i = 0; i < ct->pmc.const_count; i++) {
+ PMC * const sub_pmc = ct->pmc.constants[i];
+ if (VTABLE_isa(interp, sub_pmc, SUB)) {
+ Parrot_Sub_attributes *sub;
+
+ PMC_get_sub(interp, sub_pmc, sub);
+ if (sub->seg == cur_cs) {
+ const size_t offs = sub->start_offs;
+
+ if (offs == interp->resume_offset) {
+ Parrot_pcc_set_sub(interp, CURRENT_CONTEXT(interp), sub_pmc);
+ Parrot_pcc_set_HLL(interp, CURRENT_CONTEXT(interp), sub->HLL_id);
+ return sub_pmc;
+ }
+
+ break;
+ }
+ }
+ }
+
+ /* If we didn't find anything, put a dummy PMC into current_sub.
+ The default values set by SUb.init are appropriate for the
+ dummy, don't need additional settings. */
+ new_sub_pmc = Parrot_pmc_new(interp, enum_class_Sub);
+ Parrot_pcc_set_sub(interp, CURRENT_CONTEXT(interp), new_sub_pmc);
+
+ return new_sub_pmc;
+}
+
+
+/*
+
=item C<void Parrot_pf_execute_bytecode_program(PARROT_INTERP, PMC *pbc, PMC
*args)>

0 comments on commit 796e991

Please sign in to comment.