summaryrefslogtreecommitdiff
path: root/etc/tosail2.perl
blob: 893e2be85d305efc04760d611f627d6c4ab7ea5b (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
#!/usr/bin/env perl

use strict;
my $in_enum = 0;
my $in_bitfield = 0;

while (<>) {
	my $line = $_;

	# fix bitfield declaration, assumes zero based
	if (s/typedef\s+(\w+)\s+=\s+register\s+bits\s+\[\s*([\d]+)\s*:\s*0\s*\]/bitfield $1 : bits(@{[$2 + 1]}) =/) {
		$in_bitfield = 1;
	}
	if ($in_bitfield) {
		if (/\}/) { $in_bitfield = 0; }
		# fix fields of bitfields
		s!(\d+)\s*(\.\.\s*\d+)?\s*:\s*(\w+\s*);!$3 : $1$2,!;
	}
	# sail2 does not have != at all
	s!:=!=!;
	# fix comment style
	s!\(\*!\/\*!g;
	s!\*\)!\*\/!g;
	# fix register declarations
	s!register \(([^\)]+)\)\W+(\w+)!register $2 : $1!;

	# fix enumerations.
	if (s!typedef\s+(\w+)\s+=\s+enumerate!enum $1 =!) {
		$in_enum = 1;
	}
	if ($in_enum) {
		s/;/,/g;
		if (/\}/) {$in_enum = 0};
	}

	# fix type aliases
	s!typedef\s+(\w+)\s+=\s+(.*)!type $1 = $2!;

	# fix switch -> match
	s!switch\s*\((.*)\)!match $1!;
	# fix switch cases, need to add commas at end of lines
	s!case\s+(.*)->!$1 =>!;

	# fix lets with type
	s!let \(([^\)]+)\)\W+(\w+)!let $2 : $1!;

	# fix val declarations
	s!val (.*) (\w+)!val $2 : $1!;

	# attempt to decode pattern matches
	if (/clause decode/) {
		s/:/@/g;
		s!\((\w+)\) (\w+)!$2 : $1!g;
	}

	# fix any bits[n]
	s!bit\s*\[([^\]]+)\]!bits\($1\)!g;

	# fix option types
	s!option<([^>]+)>!option($1)!;

        # drop extern, Nat?
	print;
}