summaryrefslogtreecommitdiffstats
path: root/fips/tools/api_fns.pm
blob: d668be12ba844fc1ece91ed8e69e3a8a703bb24c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
package api_data;
use strict;

use Data::Dumper;
use File::Slurp;

# The basic data store for a declaration is a hash holding the following
# information (let's simply call this structure "declaration"):
# sym       => string (the symbol of the declaration)
# symcomment=> string (if there's a comment about this symbol) or undef
# type      => string (type definition text, with a '?' where the symbol should be
# kind      => 0 (variable)
#              1 (function)
# params    => list reference (list of declarations, one for each parameter)
#              [only exists when kind = 1]
# direction => 0 (input)
#              1 (output)
#              2 (input and output)
#              3 (output or input and output)
#              +4 (guess)
#              [only exists when this symbol is a parameter to a function]

# Constructor
sub new {
    my $class = shift;
    my $self = {};
    $self->{DECLARATIONS} = {};
    bless($self, $class);
    return $self;
}

sub read_declaration_db {
    my $self = shift;
    my $declaration_file = shift;
    my $buf = read_file($declaration_file);
    $self->{DECLARATIONS} = eval $buf;
    die $@ if $@;
}

sub write_declaration_db {
    my $self = shift;
    my $declaration_file = shift;

    $Data::Dumper::Purity = 1;
    open FILE,">".$declaration_file ||
	die "Can't open '$declaration_file': $!\n";
    print FILE "my ",Data::Dumper->Dump([ $self->{DECLARATIONS} ], [qw(declaration_db)]);
    close FILE;
}

sub insert_declaration {
    my $self = shift;
    my %decl = @_;
    my $sym = $decl{sym};

    if ($self->{DECLARATIONS}->{$sym}) {
	foreach my $k (('sym', 'symcomment','oldsym','objfile','kind')) {
	    $self->{DECLARATIONS}->{$sym}->{$k} = $decl{$k};
	}
	if ($self->{DECLARATIONS}->{$sym}->{kind} == 1) {
	    # Replace parameters only if the kind or type has changed
	    my $oldp = $self->{DECLARATIONS}->{$sym}->{params};
	    my $newp = $decl{params};
	    my $l = scalar(@{$oldp});
	    for my $pn (0..($l - 1)) {
		if ($oldp->[$pn]->{kind} != $newp->[$pn]->{kind}
		    || $oldp->[$pn]->{type} ne $newp->[$pn]->{type}) {
		    $self->{DECLARATIONS}->{$sym}->{params} = $newp;
		}
	    }
	}
    } else {
	$self->{DECLARATIONS}->{$decl{sym}} = { %decl };
    }
}

# Input is a simple C declaration, output is a declaration structure
sub _parse_declaration {
    my $decl = shift;
    my $newname = shift;
    my $objfile = shift;
    my $namecomment = shift;
    my %parsed_decl = ();

    my $debug = 0;

    print "DEBUG: going to parse: $decl\n" if $debug;

    # Start with changing all parens to { and } except the outermost
    # Within these, convert all commas to semi-colons
    my $s = "";
    do {
	print "DEBUG: decl: $decl\n" if $debug;
	$s = $decl;
	if ($decl =~ m/
		       \(
		         ([^\(\)]*)
		         \(
		           ([^\(\)]*)
		         \)
		     /x) {
	    print "DEBUG: \`: $`\n" if $debug;
	    print "DEBUG: 1: $1\n" if $debug;
	    print "DEBUG: 2: $2\n" if $debug;
	    print "DEBUG: \': $'\n" if $debug;

	    my $a = "$`"."("."$1";
	    my $b = "{"."$2"."}";
	    my $c = "$'";
	    print "DEBUG: a: $a\n" if $debug;
	    print "DEBUG: b: $b\n" if $debug;
	    print "DEBUG: c: $c\n" if $debug;
	    $b =~ s/,/;/g;
	    print "DEBUG: b: $b\n" if $debug;

	    $decl = $a.$b.$c;
	}
    } while ($s ne $decl);

    # There are types that we look for.  The first is the function pointer
    # T (*X)(...)
    if ($decl =~ m/
		   ^\s*
		   ([^\(]+)	# Return type of the function pointed at
		   \(
		     \s*\*\s*
		     ([^\)]*)	# Function returning or variable holding fn ptr
		   \)
		   \s*
		   \(
		     ([^\)]*)	# Parameter for the function pointed at
		   \)
		   \s*$
		 /x) {
	print "DEBUG: function pointer variable or function\n" if $debug;
	print "DEBUG:  1: $1\n" if $debug;
	print "DEBUG:  2: $2\n" if $debug;
	print "DEBUG:  3: $3\n" if $debug;

	my $tmp1 = $1 . "(*?)" . "(" . $3 . ")";
	my $tmp2 = $2;

	$tmp1 =~ tr/\{\}\;/(),/; # Convert all braces and semi-colons
				# back to parens and commas

	$tmp2 =~ tr/\{\}\;/(),/; # Convert all braces and semi-colons
				# back to parens and commas

	# Parse the symbol part with a fake type.  This will determine if
	# it's a variable or a function.
	my $subdeclaration = _parse_declaration("int " . $tmp2, $newname);
	map { $parsed_decl{$_} = $subdeclaration->{$_} } ( "sym",
							   "kind",
							   "params" );
	$parsed_decl{symcomment} = $namecomment if $namecomment;
	$parsed_decl{type} = $tmp1;
    }
    # If that wasn't it, check for the simple function declaration
    # T X(...)
    elsif ($decl =~ m/^\s*(.*?\W)(\w+)\s*\(\s*(.*)\s*\)\s*$/) {
	print "DEBUG: function\n" if $debug;
	print "DEBUG:  1: $1\n" if $debug;
	print "DEBUG:  2: $2\n" if $debug;
	print "DEBUG:  3: $3\n" if $debug;

	$parsed_decl{kind} = 1;
	$parsed_decl{type} = $1."?";
	$parsed_decl{sym} = $newname ? $newname : $2;
	$parsed_decl{symcomment} = $namecomment if $namecomment;
	$parsed_decl{oldsym} = $newname ? $2 : undef;
	$parsed_decl{params} = [
	    map { tr/\{\}\;/(),/; _parse_declaration($_,undef,undef,undef) }
	    grep { !/^\s*void\s*$/ }
	    split(/\s*,\s*/, $3)
	    ];
    }
    # If that wasn't it either, try to get a variable
    # T X or T X[...]
    elsif ($decl =~ m/^\s*(.*\W)(\w+)(\s*\[.*\])?\s*$/) {
	print "DEBUG: variable\n" if $debug;
	print "DEBUG:  1: $1\n" if $debug;
	print "DEBUG:  2: $2\n" if $debug;

	$parsed_decl{kind} = 0;
	$parsed_decl{type} = $1."?";
	$parsed_decl{sym} = $newname ? $newname : $2;
	$parsed_decl{symcomment} = $namecomment if $namecomment;
	$parsed_decl{oldsym} = $newname ? $2 : undef;
    }
    # Special for the parameter "..."
    elsif ($decl =~ m/^\s*\.\.\.\s*$/) {
	%parsed_decl = ( kind => 0, type => "?", sym => "..." );
    }
    # Otherwise, we got something weird
    else {
	print "Warning: weird declaration: $decl\n";
	%parsed_decl = ( kind => -1, decl => $decl );
    }
    $parsed_decl{objfile} = $objfile;

    print Dumper({ %parsed_decl }) if $debug;
    return { %parsed_decl };
}

sub add_declaration {
    my $self = shift;
    my $parsed = _parse_declaration(@_);
    $self->insert_declaration( %{$parsed} );
}

sub complete_directions {
    my $self = shift;
    foreach my $sym (keys %{$self->{DECLARATIONS}}) {
	if ($self->{DECLARATIONS}->{$sym}->{kind} == 1) {
	    map {
		if (!$_->{direction} || $_->{direction} =~ m/\?/) {
		    if ($_->{type} =~ m/const/) {
			$_->{direction} = '->'; # Input
		    } elsif ($_->{sym} =~ m/ctx/ || $_->{type} =~ m/ctx/i) {
			$_->{direction} = '<-?'; # Guess output</