~ubuntu-branches/ubuntu/raring/parrot/raring-proposed

« back to all changes in this revision

Viewing changes to frontend/pbc_merge/main.c

  • Committer: Bazaar Package Importer
  • Author(s): Allison Randal
  • Date: 2011-07-30 18:45:03 UTC
  • mfrom: (1.1.13 upstream)
  • Revision ID: james.westby@ubuntu.com-20110730184503-34d4mprtfx6pt5h3
Tags: 3.6.0-1
* New upstream release
* debian/watch:
  - Modified regular expression to capture numbered directory name
    (patch from Dominique Dumont).
* debian/rules:
  - Split build-arch and build-indep, resolving lintian warning.
  - Update path to pbc_disassemble for manpage generation (patch
    from Dominique Dumont).
* debian/patches:
  - Added patch 02_fix_perl_interpreter_path.patch, resolving
    lintian warnings.
* debian/control:
  - Added DM-Upload-Allowed field.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
Copyright (C) 2005-2010, Parrot Foundation.
 
2
Copyright (C) 2005-2011, Parrot Foundation.
3
3
 
4
4
=head1 NAME
5
5
 
67
67
/* Don't modify between HEADERIZER BEGIN / HEADERIZER END.  Your changes will be lost. */
68
68
 
69
69
PARROT_DOES_NOT_RETURN
70
 
static void help(PARROT_INTERP)
71
 
        __attribute__nonnull__(1);
 
70
static void help(void);
72
71
 
73
72
static void pbc_fixup_bytecode(PARROT_INTERP,
74
73
    ARGMOD(pbc_merge_input **inputs),
82
81
 
83
82
static void pbc_fixup_constants(PARROT_INTERP,
84
83
    ARGMOD(pbc_merge_input **inputs),
85
 
    int num_inputs,
86
 
    SHIM(PackFile_ConstTable *ct))
 
84
    int num_inputs)
87
85
        __attribute__nonnull__(1)
88
86
        __attribute__nonnull__(2)
89
87
        FUNC_MODIFIES(*inputs);
131
129
        FUNC_MODIFIES(*inputs)
132
130
        FUNC_MODIFIES(*bc);
133
131
 
134
 
PARROT_WARN_UNUSED_RESULT
135
 
PARROT_CANNOT_RETURN_NULL
136
 
static PackFile* pbc_merge_loadpbc(PARROT_INTERP,
137
 
    ARGIN(const char *fullname))
138
 
        __attribute__nonnull__(1)
139
 
        __attribute__nonnull__(2);
140
 
 
141
132
static void pbc_merge_write(PARROT_INTERP,
142
133
    ARGMOD(PackFile *pf),
143
134
    ARGIN(const char *filename))
146
137
        __attribute__nonnull__(3)
147
138
        FUNC_MODIFIES(*pf);
148
139
 
149
 
#define ASSERT_ARGS_help __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
150
 
       PARROT_ASSERT_ARG(interp))
 
140
#define ASSERT_ARGS_help __attribute__unused__ int _ASSERT_ARGS_CHECK = (0)
151
141
#define ASSERT_ARGS_pbc_fixup_bytecode __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
152
142
       PARROT_ASSERT_ARG(interp) \
153
143
    , PARROT_ASSERT_ARG(inputs) \
170
160
       PARROT_ASSERT_ARG(interp) \
171
161
    , PARROT_ASSERT_ARG(inputs) \
172
162
    , PARROT_ASSERT_ARG(bc))
173
 
#define ASSERT_ARGS_pbc_merge_loadpbc __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
174
 
       PARROT_ASSERT_ARG(interp) \
175
 
    , PARROT_ASSERT_ARG(fullname))
176
163
#define ASSERT_ARGS_pbc_merge_write __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
177
164
       PARROT_ASSERT_ARG(interp) \
178
165
    , PARROT_ASSERT_ARG(pf) \
182
169
 
183
170
/*
184
171
 
185
 
=item C<static void help(PARROT_INTERP)>
 
172
=item C<static void help(void)>
186
173
 
187
174
Print out the user help info.
188
175
 
192
179
 
193
180
PARROT_DOES_NOT_RETURN
194
181
static void
195
 
help(PARROT_INTERP)
 
182
help(void)
196
183
{
197
184
    printf("pbc_merge - merge multiple parrot bytecode files into one\n");
198
185
    printf("Usage:\n");
199
186
    printf("   pbc_merge -o out.pbc file1.pbc file2.pbc ...\n\n");
200
 
    Parrot_x_exit(interp, 0);
201
 
}
202
 
 
203
 
/*
204
 
 
205
 
=item C<static PackFile* pbc_merge_loadpbc(PARROT_INTERP, const char *fullname)>
206
 
 
207
 
This function loads a PBC file and unpacks it. We can't
208
 
use Parrot_pbc_read because that is specified to also
209
 
fixup the segments, which we don't want.
210
 
 
211
 
=cut
212
 
 
213
 
*/
214
 
 
215
 
PARROT_WARN_UNUSED_RESULT
216
 
PARROT_CANNOT_RETURN_NULL
217
 
static PackFile*
218
 
pbc_merge_loadpbc(PARROT_INTERP, ARGIN(const char *fullname))
219
 
{
220
 
    ASSERT_ARGS(pbc_merge_loadpbc)
221
 
    INTVAL program_size, wanted;
222
 
    char *program_code;
223
 
    PackFile *pf;
224
 
    FILE * io = NULL;
225
 
    INTVAL is_mapped = 0;
226
 
    size_t chunk_size;
227
 
    char *cursor;
228
 
    INTVAL read_result;
229
 
 
230
 
    /* Check the file exists. */
231
 
    STRING * const fs = Parrot_str_new_init(interp, fullname,
232
 
            strlen(fullname), Parrot_default_encoding_ptr, 0);
233
 
    if (!Parrot_file_stat_intval(interp, fs, STAT_EXISTS)) {
234
 
        Parrot_io_eprintf(interp, "PBC Merge: Can't stat %s, code %i.\n",
235
 
                fullname, errno);
236
 
        Parrot_x_exit(interp, 1);
237
 
    }
238
 
 
239
 
    /* Get program size. */
240
 
    program_size = Parrot_file_stat_intval(interp, fs, STAT_FILESIZE);
241
 
 
242
 
    /* Attempt to open file and handle any errors. */
243
 
    io = fopen(fullname, "rb");
244
 
    if (!io) {
245
 
        Parrot_io_eprintf(interp, "PBC Merge: Can't open %s, code %i.\n",
246
 
                fullname, errno);
247
 
        Parrot_x_exit(interp, 1);
248
 
    }
249
 
 
250
 
    /* Read in program. Nabbed from Parrot_pbc_read. */
251
 
    chunk_size   = program_size > 0 ? program_size : 1024;
252
 
    program_code = mem_gc_allocate_n_typed(interp, chunk_size, char);
253
 
    wanted       = program_size;
254
 
    program_size = 0;
255
 
    cursor       = (char *)program_code;
256
 
 
257
 
    while ((read_result = fread(cursor, 1, chunk_size, io)) > 0) {
258
 
        program_size += read_result;
259
 
        if (program_size == wanted)
260
 
            break;
261
 
        chunk_size   = 1024;
262
 
        program_code = mem_gc_realloc_n_typed(interp, program_code,
263
 
                program_size + chunk_size, char);
264
 
 
265
 
        cursor = (char *)program_code + program_size;
266
 
    }
267
 
 
268
 
    if (read_result < 0) {
269
 
        Parrot_io_eprintf(interp,
270
 
                "PBC Merge: Problem reading packfile from PIO.\n");
271
 
        Parrot_x_exit(interp, 1);
272
 
    }
273
 
    fclose(io);
274
 
 
275
 
    /* Now that we have the bytecode, let's unpack it. */
276
 
    pf = PackFile_new(interp, is_mapped);
277
 
    if (!PackFile_unpack(interp,
278
 
                pf, (opcode_t *)program_code, program_size)) {
279
 
        Parrot_io_eprintf(interp, "PBC Merge: Can't unpack packfile %s.\n",
280
 
                fullname);
281
 
        Parrot_x_exit(interp, 1);
282
 
    }
283
 
 
284
 
    /* Return the packfile. */
285
 
    return pf;
286
 
}
287
 
 
 
187
    exit(0);
 
188
}
288
189
 
289
190
static void
290
191
ensure_libdep(PARROT_INTERP, PackFile_ByteCode *bc, STRING *lib) {
556
457
    debug_seg->num_mappings = num_mappings;
557
458
}
558
459
 
559
 
 
560
460
static opcode_t
561
461
bytecode_remap_op(PARROT_INTERP, PackFile *pf, opcode_t op) {
562
462
    int i;
729
629
/*
730
630
 
731
631
=item C<static void pbc_fixup_constants(PARROT_INTERP, pbc_merge_input **inputs,
732
 
int num_inputs, PackFile_ConstTable *ct)>
 
632
int num_inputs)>
733
633
 
734
634
Fixup constants. This includes correcting pointers into bytecode.
735
635
 
738
638
*/
739
639
 
740
640
static void
741
 
pbc_fixup_constants(PARROT_INTERP, ARGMOD(pbc_merge_input **inputs),
742
 
                                int num_inputs, SHIM(PackFile_ConstTable *ct))
 
641
pbc_fixup_constants(PARROT_INTERP, ARGMOD(pbc_merge_input **inputs), int num_inputs)
743
642
{
744
643
    ASSERT_ARGS(pbc_fixup_constants)
745
644
    int i, j;
828
727
    pbc_fixup_bytecode(interp, inputs, num_inputs, bc);
829
728
 
830
729
    /* Walk constants and fix references into bytecode. */
831
 
    pbc_fixup_constants(interp, inputs, num_inputs, ct);
 
730
    pbc_fixup_constants(interp, inputs, num_inputs);
832
731
 
833
732
    for (i = 0; i < num_inputs; ++i) {
834
733
        mem_gc_free(interp, inputs[i]->num.const_map);
906
805
    const char *output_file     = NULL;
907
806
    struct longopt_opt_info opt = LONGOPT_OPT_INFO_INIT;
908
807
    Interp * const interp = Parrot_new(NULL);
 
808
    STRING * pbcname = NULL;
 
809
    PMC * pbcpmc = NULL;
909
810
 
910
811
    {
911
812
        const int config_length = Parrot_get_config_hash_length();
918
819
 
919
820
    /* Get options, ensuring we have at least one input
920
821
       file and an output file. */
921
 
    if (argc < 4) {
922
 
        help(interp);
923
 
    }
 
822
    if (argc < 4)
 
823
        help();
 
824
 
924
825
    while ((status = longopt_get(argc, argv, options, &opt)) > 0) {
925
826
        switch (opt.opt_id) {
926
827
            case 'o':
927
828
                if (output_file == NULL)
928
829
                    output_file = opt.opt_arg;
929
830
                else
930
 
                    help(interp);
 
831
                    help();
931
832
                break;
932
833
            case '?':
933
 
                help(interp);
 
834
                help();
934
835
                break;
935
836
            default:
936
837
                break;
937
838
        }
938
839
    }
939
 
    if (status == -1 || !output_file) {
940
 
        help(interp);
941
 
    }
 
840
    if (status == -1 || !output_file)
 
841
        help();
 
842
 
942
843
    argc -= opt.opt_index;    /* Now the number of input files. */
943
844
    argv += opt.opt_index;    /* Now at first input filename. */
944
845
 
953
854
        /* Set filename */
954
855
        input_files[i]->filename = *argv;
955
856
 
 
857
        pbcname = Parrot_str_new(interp, input_files[i]->filename,
 
858
                strlen(input_files[i]->filename));
 
859
        {
 
860
            PackFile * const pf = Parrot_pf_read_pbc_file(interp, pbcname);
 
861
            pbcpmc = Parrot_pf_get_packfile_pmc(interp, pf);
 
862
        }
 
863
 
956
864
        /* Load the packfile and unpack it. */
957
 
        input_files[i]->pf = pbc_merge_loadpbc(interp,
958
 
            input_files[i]->filename);
 
865
        input_files[i]->pf = (PackFile *)VTABLE_get_pointer(interp, pbcpmc);
959
866
        if (input_files[i]->pf == NULL) {
960
867
            Parrot_io_eprintf(interp,
961
868
                "PBC Merge: Unknown error while reading and unpacking %s\n",