[cage] Add some karma aliases for myself
[parrot.git] / tools / util / ncidef2pasm.pl
blob1b821d66a8912df81fcde35d3ebb6910703be84b
1 #! perl
3 # Copyright (C) 2003-2007, Parrot Foundation.
4 # $Id$
6 =head1 ncidef2asm.pl
8 Take an NCI library definition file and turn it into a
10 =head1 Sections
12 An NCI library definition file provides the information needed to
13 generate a parrot wrapper for the named library (or libraries). Its
14 format is simple, and looks like:
16 [package]
17 ncurses
19 [lib]
20 libform.so
22 [defs]
23 p new_field i i i i i i
25 [lib]
26 libncurses.so
28 [defs]
29 i is_term_resized i i
31 Note that the assembly file is generated in the order you specify, so
32 if there are library dependencies, make sure you have them in the
33 correct order.
35 =head2 package
37 Declares the package that all subsequent sub PMCs will be put
38 into. The name is a simple concatenation of the package name, double
39 colon, and the routine name, with no preceding punctuation.
41 =head2 lib
43 The name of the library to be loaded. Should be as qualified as
44 necessary for your platform--generally the full filename is required,
45 though the directory generally isn't.
47 You may load multiple libraries here, but only the last one loaded
48 will be exposed to subsequent defs.
50 =head2 defs
52 This section holds the definitions of functions. Each function is
53 assumed to be in the immediate preceding library. The definition of
54 the function is:
56 return_type name [param [param [param ...]]]
58 The param and return_type parameters use the NCI standard, which for
59 reference is:
61 =over 4
63 =item p
65 Parameter is a void pointer, taken from the PMC's data pointer. PMC is
66 assumed to be an unmanagedstruct or child class.
68 Taken from a P register
70 =item c
72 Parameter is a character.
74 Taken from an I register
76 =item s
78 Parameter is a short
80 Taken from an I register
82 =item i
84 Parameter is an int
86 Taken from an I register
88 =item l
90 Parameter is a long
92 Taken from an I register
94 =item f
96 Paramter is a float
98 Taken from an N register.
100 =item d
102 Parameter is a double.
104 Taken from an N register.
106 =item t
108 Paramter is a char *, presumably a C string
110 Taken from an S register
112 =item v
114 Void. Only valid as a return type, noting that the function returns no data.
116 =item I
118 Interpreter pointer. The current interpreter pointer is passed in
120 =item P
122 PMC.
124 =item 2
126 Pointer to short.
128 Taken from an I register.
130 =item 3
132 Pointer to int.
134 Taken from an I register
136 =item 4
138 Pointer to long
140 Taken from an I register
142 =back
144 =cut
146 use strict;
147 use warnings;
149 my ( $from_file, $to_file ) = @ARGV;
151 # If there is no destination file, strip off the extension of the
152 # source file and add a .pasm to it
153 if ( !defined $to_file ) {
154 $to_file = $from_file;
155 $to_file =~ s/\..*$//;
156 $to_file .= ".pasm";
159 open my $INPUT, '<', "$from_file" or die "Can't open up $from_file, error $!";
160 open my $OUTPUT, '>', "$to_file" or die "Can't open up $to_file, error $!";
162 # To start, save all the registers, just in case
163 print $OUTPUT "saveall\n";
165 my @libs;
166 my ( $cur_package, $line, $cur_section );
168 # Our dispatch table
169 my (%dispatch) = (
170 package => \&package_line,
171 lib => \&lib_line,
172 defs => \&def_line,
175 while ( $line = <$INPUT> ) {
177 # Throw away trailing newlines, comments, and whitespace. If the
178 # line's empty, then off to the next line
179 chomp $line;
180 $line =~ s/#.*//;
181 $line =~ s/\s*$//;
182 next unless $line;
184 # Is it a section line? If so, extract the section and set it.
185 if ( $line =~ /\[(\w+)\]/ ) {
186 $cur_section = $1;
187 next;
190 # Everything else goes to the handler
191 $dispatch{$cur_section}->($line);
195 # Put the registers back and end
196 print $OUTPUT "restoreall\n";
197 print $OUTPUT "end\n";
198 close $OUTPUT;
200 sub package_line {
201 my $line = shift;
203 # Trim leading and trailing spaces
204 $line =~ s/^\s*//;
205 $line =~ s/\s*$//;
207 # Set the global current package
208 $cur_package = $line;
212 sub lib_line {
213 my $line = shift;
214 print $OUTPUT "loadlib P1, '$line'\n";
217 sub def_line {
218 my $line = shift;
219 my ( $return_type, $name, @params ) = split ' ', $line;
220 unshift @params, $return_type;
221 my $signature = join( "", @params );
222 print $OUTPUT "dlfunc P2, P1, '$name', '$signature'\n";
223 print $OUTPUT "store_global '${cur_package}::${name}', P2\n";
226 # Local Variables:
227 # mode: cperl
228 # cperl-indent-level: 4
229 # fill-column: 100
230 # End:
231 # vim: expandtab shiftwidth=4: