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) \
193
180
PARROT_DOES_NOT_RETURN
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);
205
=item C<static PackFile* pbc_merge_loadpbc(PARROT_INTERP, const char *fullname)>
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.
215
PARROT_WARN_UNUSED_RESULT
216
PARROT_CANNOT_RETURN_NULL
218
pbc_merge_loadpbc(PARROT_INTERP, ARGIN(const char *fullname))
220
ASSERT_ARGS(pbc_merge_loadpbc)
221
INTVAL program_size, wanted;
225
INTVAL is_mapped = 0;
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",
236
Parrot_x_exit(interp, 1);
239
/* Get program size. */
240
program_size = Parrot_file_stat_intval(interp, fs, STAT_FILESIZE);
242
/* Attempt to open file and handle any errors. */
243
io = fopen(fullname, "rb");
245
Parrot_io_eprintf(interp, "PBC Merge: Can't open %s, code %i.\n",
247
Parrot_x_exit(interp, 1);
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;
255
cursor = (char *)program_code;
257
while ((read_result = fread(cursor, 1, chunk_size, io)) > 0) {
258
program_size += read_result;
259
if (program_size == wanted)
262
program_code = mem_gc_realloc_n_typed(interp, program_code,
263
program_size + chunk_size, char);
265
cursor = (char *)program_code + program_size;
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);
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",
281
Parrot_x_exit(interp, 1);
284
/* Return the packfile. */
290
191
ensure_libdep(PARROT_INTERP, PackFile_ByteCode *bc, STRING *lib) {
828
727
pbc_fixup_bytecode(interp, inputs, num_inputs, bc);
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);
833
732
for (i = 0; i < num_inputs; ++i) {
834
733
mem_gc_free(interp, inputs[i]->num.const_map);
919
820
/* Get options, ensuring we have at least one input
920
821
file and an output file. */
924
825
while ((status = longopt_get(argc, argv, options, &opt)) > 0) {
925
826
switch (opt.opt_id) {
927
828
if (output_file == NULL)
928
829
output_file = opt.opt_arg;
939
if (status == -1 || !output_file) {
840
if (status == -1 || !output_file)
942
843
argc -= opt.opt_index; /* Now the number of input files. */
943
844
argv += opt.opt_index; /* Now at first input filename. */
953
854
/* Set filename */
954
855
input_files[i]->filename = *argv;
857
pbcname = Parrot_str_new(interp, input_files[i]->filename,
858
strlen(input_files[i]->filename));
860
PackFile * const pf = Parrot_pf_read_pbc_file(interp, pbcname);
861
pbcpmc = Parrot_pf_get_packfile_pmc(interp, pf);
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",