assemble 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #! perl
  2. use B::Assembler qw(assemble_fh);
  3. use FileHandle;
  4. =pod
  5. =head1 NAME
  6. assemble - Assemble Perl bytecode
  7. =head1 SYNOPSIS
  8. assemble [-d] [bytecode.asm | -] [bytecode.plc]
  9. =head1 DESCRIPTION
  10. Compiles an ascii bytecode as produced by L<disassemble> to binary bytecode assembler.
  11. C<bytecode> is a binary file wih either the magic 4 bytes 'PLBC'
  12. at the start, or something like "#! /usr/bin/perl\n
  13. use ByteLoader '0.07'", typically with the F<.plc> or F<.pmc> extension.
  14. If filename is -, the input is read from STDIN and
  15. you can still provide an output filename.
  16. =head1 OPTION -d
  17. Prints some debugging information.
  18. =cut
  19. my ($filename, $fh, $out);
  20. if ($ARGV[0] eq "-d") {
  21. B::Assembler::debug(1);
  22. shift;
  23. }
  24. $out = \*STDOUT;
  25. if (@ARGV == 0) {
  26. $fh = \*STDIN;
  27. $filename = "-";
  28. } elsif (@ARGV == 1) {
  29. $filename = $ARGV[0];
  30. $fh = new FileHandle "<$filename";
  31. } elsif (@ARGV == 2) {
  32. $filename = $ARGV[0];
  33. $fh = new FileHandle "<$filename";
  34. $out = new FileHandle ">$ARGV[1]";
  35. } else {
  36. die "Usage: assemble [-d] [filename | -] [outfilename]\n";
  37. }
  38. binmode $out;
  39. $SIG{__WARN__} = sub { warn "$filename:@_" };
  40. $SIG{__DIE__} = sub { die "$filename: @_" };
  41. assemble_fh($fh, sub { print $out @_ });