3 # sfdc - Compile SFD files into someting useful
4 # Copyright (C) 2003-2004 Martin Blom <martin@blom.org>
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 # The default AmigaOS GG installation of does not seem to include
26 # Pod::Usage, so we have to provide a fallback. Ugly, but it works and
36 # Minimal fall-back ...
44 my $output = \*STDERR;
48 /^-verbose$/ && do { $verbose = shift @params};
49 /^-exitval$/ && do { $exitval = shift @params};
50 /^-message$/ && do { $message = shift @params};
51 /^-output$/ && do { $output = shift @params};
55 print $output "$message\n" if $message;
57 print $output "Perl module Pod::Usage is missing.\n";
58 print $output "Please refer to the sfdc documentation for usage, ".
59 "or install Pod::Usage.\n";
66 sub parse_proto
( $$$ );
67 sub open_output
( $$ );
68 sub will_close_output
( $$ );
73 'struct Library* LibInit(struct Library* library,' .
75 ' struct ExecBase* SysBase)' .
77 'struct Library* LibOpen(ULONG version) (d0)',
79 'BPTR LibExpunge() ()',
85 'struct Library* DevInit(struct Library* library,' .
87 ' struct ExecBase* SysBase)' .
89 'ULONG DevOpen(struct IORequest* ioreq,' .
91 ' ULONG flags) (a1,d0,d1)',
92 'BPTR DevClose(struct IORequest* ioreq) (a1)',
93 'BPTR DevExpunge() ()',
95 'VOID DevBeginIO(struct IORequest* ioreq) (a1)',
96 'ULONG DevAbortIO(struct IORequest* ioreq) (a1)'
101 'struct ClassLibrary* ClassInit(struct ClassLibrary* library,' .
103 ' struct ExecBase* SysBase)' .
105 'struct ClassLibrary* ClassOpen(ULONG version) (d0)',
106 'BPTR ClassClose() ()',
107 'BPTR ClassExpunge() ()',
108 'ULONG ClassNull() ()',
109 'Class* ObtainEngine() ()',
114 { target
=> 'generic',
115 vectors
=> { 'library' => @lf, 'device' => @df, 'boopsi' => @bf },
121 '(\w)+(-.*)?-aros' =>
123 vectors
=> { 'library' => @lf, 'device' => @df, 'boopsi' => @bf },
124 macros
=> 'MacroAROS',
126 gatestubs
=> 'GateAROS',
127 sdistubs
=> 'SDIAROS'
130 'i.86be(-pc)?-amithlon' =>
131 { target
=> 'amithlon',
132 vectors
=> { 'library' => @lf, 'device' => @df, 'boopsi' => @bf },
134 stubs
=> 'StubAmithlon',
135 gatestubs
=> 'GateAmithlon'
138 'm68k(-unknown)?-amigaos' =>
139 { target
=> 'amigaos',
140 vectors
=> { 'library' => @lf, 'device' => @df, 'boopsi' => @bf },
141 macros
=> 'Macro68k',
143 gatestubs
=> 'Gate68k'
146 'p(ower)?pc(-unknown)?-amigaos' =>
147 { target
=> 'amigaos4',
148 vectors
=> { 'library' => @lf, 'device' => @df, 'boopsi' => @bf },
149 macros
=> 'MacroAOS4',
151 gatestubs
=> 'GateAOS4'
154 'p(ower)?pc(-unknown)?-morphos' =>
155 { target
=> 'morphos',
156 vectors
=> { 'library' => @lf, 'device' => @df, 'boopsi' => @bf },
157 macros
=> 'MacroMOS',
159 gatestubs
=> 'GateMOS'
165 ###############################################################################
166 ### Main program ##############################################################
167 ###############################################################################
169 Getopt
::Long
::Configure
("bundling");
175 my $addvectors = 'none';
180 my $target = 'm68k-unknown-amigaos';
183 GetOptions
('addvectors=s' => \
$addvectors,
184 'gateprefix=s' => \
$gateprefix,
186 'libarg=s' => \
$libarg,
187 'libprefix=s' => \
$libprefix,
190 'output|o=s' => \
$output,
191 'quiet|q' => \
$quiet,
192 'target=s' => \
$target,
193 'version|v' => \
$version) or exit 10;
196 print STDERR
"sfdc SFDC_VERSION (SFDC_DATE)\n";
197 print STDERR
"Copyright (C) 2003-2004 Martin Blom <martin\@blom.org>\n";
198 print STDERR
"This is free software; " .
199 "see the source for copying conditions.\n";
204 pod2usage
(-verbose
=> 1,
206 -output
=> \
*STDOUT
);
210 pod2usage
(-verbose
=> 3,
216 pod2usage
(-message
=> "No SFD file specified.",
223 if (!($mode =~ /^(clib|dump|fd|libproto|lvo|functable|macros|proto|pragmas|stubs|gateproto|gatestubs|sdiproto|sdistubs|verify)$/)) {
224 pod2usage
(-message
=> "Unknown mode specified. Use --help for a list.",
229 if ($libarg !~ /^(first|last|none)$/) {
230 pod2usage
(-message
=> "Unknown libarg specified. Use --help for a list.",
235 if ($addvectors !~ /^(none|library|device|boopsi)$/) {
236 pod2usage
(-message
=> "Unknown addvectors value. Use --help for a list.",
242 foreach my $target_regex (keys %targets) {
243 if ($target =~ /^$target_regex$/) {
244 $classes = $targets{$target_regex};
249 pod2usage
(-message
=> "Unknown target specified. Use --help for a list.",
256 open( OLDOUT
, ">&STDOUT" );
258 for my $i ( 0 .. $#ARGV ) {
259 my $sfd = parse_sfd
($ARGV[$i]);
260 my $num = $#{$$sfd{'prototypes'}};
266 $obj = CLib
->new( sfd
=> $sfd );
271 $obj = FD
->new( sfd
=> $sfd );
276 $obj = Dump
->new( sfd
=> $sfd );
281 $obj = Gate
->new( sfd
=> $sfd,
288 $obj = LVO
->new( sfd
=> $sfd );
292 /^functable$/ && do {
293 $obj = FuncTable
->new( sfd
=> $sfd );
298 $obj = $$classes{'macros'}->new( sfd
=> $sfd );
300 # By tradition, the functions in the macro files are sorted
301 # @{$$sfd{'prototypes'}} = sort {
302 # $$a{'funcname'} cmp $$b{'funcname'}
303 # } @{$$sfd{'prototypes'}};
308 $obj = Proto
->new( sfd
=> $sfd );
313 $obj = SASPragmas
->new( sfd
=> $sfd );
318 $obj = Verify
->new( sfd
=> $sfd );
323 $obj = $$classes{'stubs'}->new( sfd
=> $sfd );
325 # By tradition, the functions in the stub files are sorted
326 # @{$$sfd{'prototypes'}} = sort {
327 # $$a{'funcname'} cmp $$b{'funcname'}
328 # } @{$$sfd{'prototypes'}};
332 /^gateproto$/ && do {
333 $obj = $$classes{'gatestubs'}->new( sfd
=> $sfd,
339 /^gatestubs$/ && do {
340 $obj = $$classes{'gatestubs'}->new( sfd
=> $sfd,
348 $obj = $$classes{'sdistubs'}->new( sfd
=> $sfd,
355 $obj = $$classes{'sdistubs'}->new( sfd
=> $sfd,
361 die "Unknown mode specified: " . $mode;
365 for my $j ( 0 .. $num + 1) {
366 my $prototype = $$sfd{'prototypes'}[$j];
367 my $funcname = $$prototype{'funcname'};
369 if (!defined ($funcname) || will_close_output
($sfd, $funcname) != 0) {
377 if (open_output
($sfd, $funcname) != 0) {
381 $obj->function (prototype => $prototype);
388 print STDERR
"All done.\n";
391 open (STDOUT
, ">&OLDOUT");
401 ###############################################################################
402 ### Subroutines ###############################################################
403 ###############################################################################
406 ### parse_sfd: Parse an SFD file hand return a hash record #####################
408 sub parse_sfd
( $ ) {
412 my $type = 'function';
413 my $last_type = $type;
420 copyright
=> 'Copyright © 2001 Amiga, Inc.',
424 basetype
=> 'struct Library *',
433 # Why do I need this????
434 $$result{'prototypes'} = ();
435 $$result{'includes'} = ();
436 $$result{'typedefs'} = ();
438 if ($addvectors ne 'none') {
439 push @
{$$result{'includes'}}, '<dos/dos.h>';
440 push @
{$$result{'includes'}}, '<exec/execbase.h>';
442 if ($addvectors eq 'device') {
443 push @
{$$result{'includes'}}, '<exec/io.h>';
445 elsif ($addvectors eq 'boopsi') {
446 push @
{$$result{'includes'}}, '<intuition/classes.h>';
449 for my $i ( 0 .. $#{$classes->{vectors}->{$addvectors}} ) {
450 push @
{$$result{'prototypes'}}, {
452 subtype
=> $addvectors,
453 value
=> $classes->{vectors
}->{$addvectors}[$i],
468 ( my $fn = $file ) =~ s
,.*[/\\](.*),$1,;
469 print STDERR
"Processing SFD file '$fn'.\n";
473 unless (open (SFD
, "<" . $file)) {
474 print STDERR
"Unable to open file '$file'.\n";
481 while (my $line = <SFD
>) {
486 /^==copyright\s/ && do {
487 ( $$result{'copyright'} = $_ ) =~ s/==copyright\s+(.*)\s*/$1/;
492 ( $$result{'id'} = $_ ) =~ s/==id\s+(.*)\s*/$1/;
496 /^==libname\s+/ && do {
497 ( $$result{'libname'} = $_ ) =~ s/==libname\s+(.*)\s*/$1/;
502 ( $$result{'base'} = $_ ) =~ s/==base\s+_?(.*)\s*/$1/;
506 /^==basetype\s+/ && do {
507 ( $$result{'basetype'} = $_ ) =~ s/==basetype\s+(.*)\s*/$1/;
511 /^==include\s+/ && do {
512 ( my $inc = $_ ) =~ s/==include\s+(.*)\s*/$1/;
514 push @
{$$result{'includes'}}, $inc;
518 /^==typedef\s+/ && do {
519 ( my $td = $_ ) =~ s/==typedef\s+(.*)\s*$/$1/;
521 push @
{$$result{'typedefs'}}, $td;
526 ( $bias = $_ ) =~ s/==bias\s+(.*)\s*/$1/;
530 /^==reserve\s+/ && do {
531 ( my $reserve = $_ ) =~ s/==reserve\s+(.*)\s*/$1/;
533 $bias += 6 * $reserve;
537 /^==alias\s*$/ && do {
544 /^==varargs\s*$/ && do {
551 /^==private\s*$/ && do {
556 /^==public\s*$/ && do {
561 /^==version\s+/ && do {
562 ( $version = $_ ) =~ s/==version\s+(.*)\s*/$1/;
571 ( my $cmt = $_ ) =~ s/^\*(.*)\s*/$1/;
573 $comment .= ($comment eq '' ?
"" : "\n" ) . $cmt;
578 # Strip whitespaces and append
579 $line =~ s/\s*(.*)\s*/$1/;
580 $proto_line .= $line . " ";
589 # If we get here, we found a line we don't understand
590 print STDERR
"Unable to parse line $line_no in SFD file" .
591 " '$file'. The line looks like this:\n" . $line ;
596 /.*[A-Za-z0-9_]+\s*\(.*\).*\(((base|sysv|autoreg|[\saAdD][0-7]-?),?)*\)\s*$/
599 if ($proto_line =~ /.*\(.*[0-7]-.*\)\s*$/) {
600 if ($$classes{'target'} ne 'amigaos') {
601 print STDERR
"Warning: Multiregister functions are m68k only.\n";
603 $proto_line =~ s/([da][0-7])-[da][0-7]/$1/g;
606 push @
{$$result{'prototypes'}}, {
609 value
=> $proto_line,
627 if( $proto_line ne '' ) {
628 # If $proto_line isn't empty, we couldn't parse it
629 die "Unhanled proto '" . $proto_line . "'\n";
634 # Now parse the prototypes
635 my $real_funcname = '';
636 my $real_prototype = {};
637 my $varargs_type = '';
639 for my $i ( 0 .. $#{$$result{'prototypes'}} ) {
640 my $prototype = $$result{'prototypes'}[$i];
642 if ($$prototype{'type'} eq 'varargs') {
643 $$prototype{'real_funcname'} = $real_funcname;
644 $$prototype{'real_prototype'} = $real_prototype;
647 $$prototype{'real_funcname'} = '';
648 $$prototype{'real_prototype'} = '';
651 parse_proto
($result, $prototype, $varargs_type);
653 if ($$prototype{'type'} eq 'function') {
654 $varargs_type = $$prototype{'argtypes'}[$#{$$prototype{'argtypes'}}];
657 if ($$prototype{'type'} eq 'function') {
658 $real_funcname = $$prototype{'funcname'};
659 $real_prototype = $prototype;
663 # Create some other variables
665 ( $$result{'basename'} = $file ) =~ s
:.*/(\w
+?
)_lib\
.sfd
:$1:;
667 if ($$result{'basename'} eq '') {
668 ( $$result{'basename'} = $$result{'libname'} ) =~ s/(.*)\.\w+/$1/ or do {
669 print STDERR
"Unable to find or guess base name.\n";
670 print STDERR
"Please add \"==libname module_name\" to SFD file.\n";
674 # Fake the CIA libname
675 if ($$result{'basename'} eq "cia") {
676 $$result{'libname'} = "ciaX.resource";
679 $$result{'libname'} = $$result{'basename'} . ".library";
683 # Fake the Workbench basename
684 if ($$result{'basename'} eq "workbench") {
685 $$result{'basename'} = "wb";
688 $$result{'basename'} =~ s/-/_/g;
689 $$result{'basename'} =~ s/\//_
/g
;
690 $$result{'basename'} =~ s/\./_/g;
691 $$result{'basename'} = lc $$result{'basename'};
692 $$result{'BASENAME'} = uc $$result{'basename'};
693 $$result{'Basename'} = ucfirst $$result{'basename'};
694 ($result->{BaseName
} = $result->{base
}) =~ s/Base//;
700 ### parse_proto: Parse a single function prototype ###########################
702 sub parse_proto
( $$$ ) {
704 my $prototype = shift;
705 my $varargs_type = shift;
712 if (!(($return,undef,undef,$name,$arguments,$registers) =
713 ( $$prototype{'value'} =~
714 /^((struct\s+)?(\w+\s*?)+\**)\s*(\w+)\s*\((.*)\)\s*\((.*)\).*/ ))) {
715 print STDERR
"Unable to parse prototype on line $$prototype{'line'}.\n";
719 # Nuke whitespaces from the register specification
720 $registers =~ s/\s//;
722 $$prototype{'return'} = $return;
723 $$prototype{'funcname'} = $name;
725 $$prototype{'numargs'} = 0;
726 $$prototype{'numregs'} = 0;
728 @
{$$prototype{'regs'}} = ();
729 @
{$$prototype{'args'}} = ();
730 @
{$$prototype{'___args'}} = ();
731 @
{$$prototype{'argnames'}} = ();
732 @
{$$prototype{'___argnames'}} = ();
733 @
{$$prototype{'argtypes'}} = ();
735 if ($arguments =~ /^(void|VOID)$/) {
739 my @args = split(/,/,$arguments);
741 # Fix function pointer arguments and build $$prototype{'args'}
744 foreach my $arg (@args) {
746 $arg =~ s/\s*(.*?)\s*/$1/;
749 my $old_arg = pop @
{$$prototype{'args'}};
751 push @
{$$prototype{'args'}}, $old_arg . "," . $arg;
754 push @
{$$prototype{'args'}}, $arg;
757 # Count parentheses (a function pointer arguments is processed
758 # when $par_cnt is 0).
759 $par_cnt += ( $arg =~ tr/\(/\(/ );
760 $par_cnt -= ( $arg =~ tr/\)/\)/ );
763 $$prototype{'numargs'} = $#{$$prototype{'args'}} + 1;
765 if ($registers =~ /sysv/) {
766 $prototype->{type
} = 'cfunction';
767 $prototype->{nb
} = 1;
769 elsif ($registers =~ /autoreg/) {
772 foreach my $arg (@
{$$prototype{'args'}}) {
774 push @
{$$prototype{'regs'}}, "a$a_cnt";
778 push @
{$$prototype{'regs'}}, "d$d_cnt";
783 $prototype->{numregs
} = $#{$$prototype{'regs'}} + 1;
784 $prototype->{nb
} = $sfd->{base
} eq '';
787 # Split regs and make them lower case
788 @
{$$prototype{'regs'}} = split(/,/,lc $registers);
789 $prototype->{numregs
} = $#{$$prototype{'regs'}} + 1;
790 $prototype->{nb
} = $sfd->{base
} eq '' || $registers =~ /a6/;
793 $$prototype{'nr'} = $$prototype{'return'} =~ /^(VOID|void)$/;
796 # printfcall: LONG Printf( STRPTR format, ... );
797 # All varargs are optional
798 # tagcall: BOOL AslRequestTags( APTR requester, Tag Tag1, ... );
799 # First vararg is a Tag, then a TAG_DONE terminated tag list
800 # methodcall: ULONG DoGadgetMethod( ... ULONG message, ...);
801 # First vararg is required.
803 if ($prototype->{type
} eq 'varargs') {
805 /^\s*(const|CONST)?\s*struct\s+TagItem\s*\*\s*$/ ) {
806 $prototype->{subtype
} = 'tagcall';
808 if ($prototype->{numargs
} == $prototype->{numregs
}) {
810 print STDERR
"Warning: Adding missing Tag argument to " .
811 $prototype->{funcname
} . "()\n";
814 my $last = pop @
{$prototype->{args
}};
815 push @
{$prototype->{args
}}, "Tag _tag1" ;
816 push @
{$prototype->{args
}}, $last;
818 ++$prototype->{numargs
};
822 if ($prototype->{numargs
} == $prototype->{numregs
}) {
823 $prototype->{subtype
} = 'printfcall';
825 elsif ($prototype->{numargs
} == $prototype->{numregs
} + 1) {
826 $prototype->{subtype
} = 'methodcall';
830 elsif ($prototype->{type
} eq 'cfunction') {
831 foreach (split(/,/,lc $registers)) {
833 $prototype->{subtype
} = 'sysv';
838 if ($sfd->{base
} eq '') {
839 printf STDERR
"$prototype->{funcname}: " .
840 "Library has no base!\n";
844 $prototype->{nb
} = 0;
852 # Make sure we have the same number of arguments as registers, or,
853 # if this is a varargs function, possible one extra, á la "MethodID, ...".
854 # Tagcalls always have one extra, á la "Tag, ...".
856 if (($prototype->{type
} eq 'varargs' &&
857 $prototype->{subtype
} eq 'tagcall' &&
858 $prototype->{numargs
} != $prototype->{numregs
} + 1 ) ||
860 ($prototype->{type
} eq 'varargs' &&
861 $prototype->{subtype
} eq 'printfcall' &&
862 $prototype->{numargs
} != $prototype->{numregs
}) ||
864 ($prototype->{type
} eq 'varargs' &&
865 $prototype->{subtype
} eq 'methodcall' &&
866 $prototype->{numargs
} != $prototype->{numregs
} + 1) ||
868 ($prototype->{type
} eq 'function' &&
869 $prototype->{numargs
} != $prototype->{numregs
})) {
871 print STDERR
"Failed to parse arguments/registers on SFD " .
872 "line $$prototype{'line'}:\n$$prototype{'value'}\n";
873 print STDERR
"The number of arguments doesn't match " .
874 "the number of registers (+1 if tagcall).\n";
880 foreach my $arg (@
{$$prototype{'args'}}) {
885 # MorhOS includes use __CLIB_PROTOTYPE for some reason ...
886 if ($arg =~ /.*\(.*?\)\s*(__CLIB_PROTOTYPE)?\(.*\)/) {
890 ($type1, $name, $type2) =
891 ( $arg =~ /^\s*(.*)\(\s*\*\s*(\w+)\s*\)\s*(\w*\(.*\))\s*/ );
892 $type = "$type1(*)$type2";
893 $___name = "___$name";
894 $___arg = "$type1(*___$name) $type2";
896 elsif ($arg !~ /^\.\.\.$/) {
897 ($type, $name) = ( $arg =~ /^\s*(.*?[\s*]*?)\s*(\w+)\s*$/ );
898 $___name = "___$name";
899 $___arg = "$type ___$name";
902 if ($prototype->{type
} eq 'varargs') {
903 $type = $varargs_type;
915 if ($type eq '' || $name eq '' ) {
916 print STDERR
"Type or name missing from '$arg'.\n";
920 push @
{$$prototype{'___args'}}, $___arg;
921 push @
{$$prototype{'argnames'}}, $name;
922 push @
{$$prototype{'___argnames'}}, $___name;
924 push @
{$$prototype{'argtypes'}}, $type;
934 ### close_output: Close the output file if necessary #########################
936 sub close_output
() {
942 ### check_output: Check if the file will be reopended by open_output ##########
944 sub will_close_output
( $$ ) {
946 my $function = shift;
948 my $new_output = $output;
950 $new_output =~ s/%f/$function/;
951 $new_output =~ s/%b/$$sfd{'base'}/;
952 $new_output =~ s/%l/$$sfd{'libname'}/;
953 $new_output =~ s/%n/$$sfd{'basename'}/;
955 if( $old_output ne '' &&
956 $new_output ne $old_output ) {
964 ### open_output: (Re)open the output file if necessary #######################
966 sub open_output
( $$ ) {
968 my $function = shift;
970 my $new_output = $output;
972 $new_output =~ s/%f/$function/;
973 $new_output =~ s/%b/$$sfd{'base'}/;
974 $new_output =~ s/%l/$$sfd{'libname'}/;
975 $new_output =~ s/%n/$$sfd{'basename'}/;
977 if( $new_output ne $old_output ) {
981 if ($new_output eq '-') {
982 open (STDOUT
, ">&OLDOUT") or die;
985 open (STDOUT
, ">" . $new_output) or die;
988 print STDERR
"Writing to '$new_output'\n";
992 $old_output = $new_output;