sbsv1/abld/e32util/h2inc.pl
author jascui
Wed, 17 Nov 2010 13:45:48 +0800
changeset 689 b70d8ea6cf75
parent 606 30b30f9da0b7
permissions -rw-r--r--
Updating release text and product version
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
606
30b30f9da0b7 Add ..\dev\.. to path
jjkang
parents: 599
diff changeset
     1
#!/usr/bin/perl
599
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
     2
# Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies).
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
     3
# All rights reserved.
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
     4
# This component and the accompanying materials are made available
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
     5
# under the terms of "Eclipse Public License v1.0"
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
     6
# which accompanies this distribution, and is available
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
     7
# at the URL "http://www.eclipse.org/legal/epl-v10.html".
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
     8
#
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
     9
# Initial Contributors:
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    10
# Nokia Corporation - initial contribution.
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    11
#
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    12
# Contributors:
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    13
#
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    14
# Description:
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    15
# e32toolp\e32util\h2inc.pl
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    16
# Convert structures in C++ include files to assembler format
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    17
# Syntax:
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    18
# perl h2inc.pl <input.h> <output.inc> <format>
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    19
# where <format>=arm or x86
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    20
# 
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    21
#
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    22
606
30b30f9da0b7 Add ..\dev\.. to path
jjkang
parents: 599
diff changeset
    23
# Version
30b30f9da0b7 Add ..\dev\.. to path
jjkang
parents: 599
diff changeset
    24
my $MajorVersion = 1;
30b30f9da0b7 Add ..\dev\.. to path
jjkang
parents: 599
diff changeset
    25
my $MinorVersion = 1;
30b30f9da0b7 Add ..\dev\.. to path
jjkang
parents: 599
diff changeset
    26
my $PatchVersion = 0;
30b30f9da0b7 Add ..\dev\.. to path
jjkang
parents: 599
diff changeset
    27
599
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    28
%basictypes = (
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    29
	TInt8		=>	1,
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    30
	TUint8		=>	1,
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    31
	TInt16		=>	2,
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    32
	TUint16		=>	2,
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    33
	TInt32		=>	4,
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    34
	TUint32		=>	4,
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    35
	TInt		=>	4,
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    36
	TUint		=>	4,
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    37
	TInt64		=>	8,
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    38
	TUint64		=>	8,
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    39
	TLinAddr	=>	4,
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    40
	TVersion	=>	4,
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    41
	TPde		=>	4,
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    42
	TPte		=>	4,
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    43
	TProcessPriority => 4
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    44
);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    45
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    46
if (scalar(@ARGV)!=3) {
606
30b30f9da0b7 Add ..\dev\.. to path
jjkang
parents: 599
diff changeset
    47
	die "H2INC format management tools V$MajorVersion.$MinorVersion.$PatchVersion\nperl h2inc.pl <input.h> <output.inc> <format>\n";
599
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    48
}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    49
my ($infile, $outfile, $format) = @ARGV;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    50
open IN, $infile or die "Can't open $infile for input\n";
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    51
my $in;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    52
while (<IN>) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    53
	$in.=$_;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    54
}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    55
close IN;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    56
$format = uc($format);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    57
$format_sub = undef();
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    58
$comment_sub = undef();
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    59
$end_sub = undef();
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    60
if ($format eq "ARMASM") {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    61
	$format_sub = \&armasm_format;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    62
	$comment_sub = \&armasm_comment;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    63
	$end_sub = \&armasm_end;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    64
} elsif ($format eq "AS") {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    65
	$format_sub = \&as_format;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    66
	$comment_sub = \&as_comment;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    67
	$end_sub = \&as_end;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    68
} elsif ($format eq "TASM") {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    69
	$format_sub = \&tasm_format;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    70
	$comment_sub = \&tasm_comment;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    71
	$end_sub = \&tasm_end;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    72
} else {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    73
	die "Format $format unknown\nOnly ARMASM, AS or TASM supported\n";
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    74
}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    75
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    76
# First remove any backslash-newline combinations
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    77
$in =~ s/\\\n//gms;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    78
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    79
# Change escaped quotes to double quotes
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    80
$in =~ s/\\\"/\"\"/gms;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    81
$in =~ s/\\\'/\'\'/gms;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    82
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    83
# Remove any character constants
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    84
$in =~  s/\'(.?(${0})*?)\'//gms;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    85
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    86
# Remove any string literals
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    87
$in =~ s/\"(.*?)\"//gms;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    88
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    89
# Strip comments
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    90
$in =~ s/\/\*(.*?)\*\//\n/gms;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    91
$in =~ s/\/\/(.*?)\n/\n/gms;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    92
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    93
# Collapse whitespace into a single space or newline
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    94
$in =~ s/\t/\ /gms;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    95
$in =~ s/\r/\ /gms;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    96
$in =~ s/(\ )+/\ /gms;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    97
$in =~ s/\n(\ )*/\n/gms;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    98
$in =~ s/(\ )*\n/\n/gms;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    99
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   100
# Tokenize on non-identifier characters
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   101
my @tokens0 = split(/(\W)/,$in);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   102
my @tokens;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   103
foreach $t (@tokens0) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   104
	next if ($t eq " " or $t eq "");
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   105
	push @tokens, $t;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   106
}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   107
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   108
my %macros;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   109
my %filescope;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   110
$filescope{file}=1;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   111
$filescope{name}='*** FILE SCOPE ***';
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   112
my @ftypedefs;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   113
$filescope{typedefs}=\@ftypedefs;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   114
my $line=1;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   115
parse_scope(\%filescope, \@tokens, \$line);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   116
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   117
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   118
my @output;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   119
push @output, &$comment_sub('*' x 80);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   120
push @output, &$comment_sub($outfile);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   121
push @output, &$comment_sub('*' x 80);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   122
push @output, &$comment_sub("GENERATED FILE - DO NOT EDIT");
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   123
push @output, "";
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   124
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   125
output_scope(\%filescope, \@output);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   126
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   127
push @output, &$end_sub();
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   128
push @output, "";
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   129
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   130
open OUT, ">$outfile" or die "Can't open $outfile for write\n";
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   131
print OUT join("\n", @output);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   132
print OUT "\n\n";
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   133
close OUT;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   134
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   135
sub get_token($$) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   136
	my ($tokenlist,$line) = @_;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   137
	while (scalar(@$tokenlist)) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   138
		my $t = shift @$tokenlist;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   139
		return $t if (!defined($t));
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   140
		return $t if ($t !~ /^\s*$/);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   141
		++$$line;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   142
	}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   143
}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   144
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   145
sub skip_qualifiers($) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   146
	my ($tokens) = @_;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   147
	my $f=0;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   148
	my %quals = (
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   149
		EXPORT_C => 1,
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   150
		IMPORT_C => 1,
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   151
		inline => 1,
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   152
		const => 0,
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   153
		volatile => 0,
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   154
		static => 0,
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   155
		extern => 0,
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   156
		LOCAL_C => 0,
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   157
		LOCAL_D => 0,
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   158
		GLDEF_C => 0,
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   159
		GLREF_C => 0,
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   160
		GLDEF_D => 0,
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   161
		GLREF_D => 0
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   162
		);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   163
	for (;;) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   164
		my $t = $$tokens[0];
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   165
		my $q = $quals{$t};
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   166
		last unless (defined ($q));
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   167
		$f |= $q;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   168
		shift @$tokens;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   169
	}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   170
	return $f;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   171
}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   172
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   173
sub parse_indirection($) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   174
	my ($tokens) = @_;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   175
	my $level = 0;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   176
	for (;;) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   177
		my $t = $$tokens[0];
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   178
		if ($t eq '*') {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   179
			++$level;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   180
			shift @$tokens;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   181
			next;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   182
		}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   183
		last if ($t ne "const" and $t ne "volatile");
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   184
		shift @$tokens;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   185
	}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   186
	return $level;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   187
}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   188
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   189
sub parse_scope($$$) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   190
	my ($scope, $tokens, $line) = @_;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   191
	my $state = 0;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   192
	my %values;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   193
	my @classes;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   194
	my @enums;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   195
	my $curr_offset=0;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   196
	my $overall_align=0;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   197
	$scope->{values}=\%values;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   198
	$scope->{classes}=\@classes;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   199
	$scope->{enums}=\@enums;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   200
	while (scalar(@$tokens)) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   201
		my $t = shift @$tokens;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   202
		if ($state>=-1 and $t eq "\n") {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   203
			++$$line;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   204
			$state=1;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   205
			next;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   206
		} elsif ($state==-1 and $t ne "\n") {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   207
			next;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   208
		} elsif ($state==-2 and $t ne ';') {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   209
			next;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   210
		}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   211
		if ($state>0 and $t eq '#') {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   212
			if ($scope->{scope}) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   213
				warn "Preprocessor directive in class/struct at line $$line\n";
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   214
			}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   215
			$t = shift @$tokens;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   216
			if ($t eq 'define') {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   217
				my $ident = shift @$tokens;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   218
				my $defn = shift @$tokens;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   219
				if ($defn ne '(') {	# don't do macros with parameters
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   220
					$macros{$ident} = $defn;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   221
				}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   222
			}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   223
			$state=-1;	# skip to next line
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   224
			next;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   225
		}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   226
		if ($t eq "struct" or $t eq "class") {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   227
			next if ($state==0);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   228
			$state=0;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   229
			my %cl;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   230
			$cl{specifier}=$t;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   231
			$cl{scope}=$scope;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   232
			my @members;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   233
			my @typedefs;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   234
			$cl{members}=\@members;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   235
			$cl{typedefs}=\@typedefs;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   236
			my $new_class = \%cl;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   237
			my $n = get_token($tokens,$line);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   238
			if ($n !~ /\w+/) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   239
				die "Unnamed $t not supported at line $$line\n";
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   240
			}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   241
			$new_class->{name}=$n;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   242
			my @class_match = grep {$_->{name} eq $n} @classes;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   243
			my $exists = scalar(@class_match);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   244
			my $b = get_token($tokens,$line);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   245
			if ($b eq ':') {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   246
				die "Inheritance not supported at line $$line\n";
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   247
			} elsif ($b eq ';') {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   248
				# forward declaration
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   249
				push @classes, $new_class unless ($exists);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   250
				next;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   251
			} elsif ($b ne '{') {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   252
				die "Syntax error at line $$line\n";
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   253
			}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   254
			if ($exists) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   255
				$new_class = $class_match[0];
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   256
				if ($new_class->{complete}) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   257
					die "Duplicate definition of $cl{specifier} $n\n";
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   258
				}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   259
			}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   260
			push @classes, $new_class unless ($exists);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   261
			parse_scope($new_class, $tokens, $line);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   262
			next;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   263
		} elsif ($t eq "enum") {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   264
			$state=0;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   265
			my $n = get_token($tokens,$line);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   266
			my $name="";
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   267
			if ($n =~ /\w+/) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   268
				$name = $n;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   269
				$n = get_token($tokens,$line);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   270
			}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   271
			push @enums, $name;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   272
			if ($n ne '{') {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   273
				die "Syntax error at line $$line\n";
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   274
			}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   275
			parse_enum($scope, $tokens, $line, $name);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   276
			next;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   277
		} elsif ($t eq '}') {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   278
			$state=0;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   279
			if ($scope->{scope}) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   280
				$t = get_token($tokens,$line);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   281
				if ($t eq ';') {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   282
					$scope->{complete}=1;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   283
					last;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   284
				}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   285
			}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   286
			die "Syntax error at line $$line\n";
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   287
		}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   288
		$state=0;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   289
		if ($scope->{scope}) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   290
			if ($t eq "public" or $t eq "private" or $t eq "protected") {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   291
				if (shift (@$tokens) eq ':') {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   292
					next;	# ignore access specifiers
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   293
				}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   294
			die "Syntax error at line $$line\n";
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   295
			}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   296
		}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   297
		unshift @$tokens, $t;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   298
		my @currdecl = parse_decl_def($scope, $tokens, $line);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   299
		if ($t eq 'static') {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   300
			next;	# skip static members
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   301
		}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   302
		my $typedef;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   303
		if ($t eq 'typedef') {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   304
			$typedef = 1;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   305
			$t = shift @currdecl;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   306
			$t = $currdecl[0];
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   307
		} else {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   308
			$typedef = 0;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   309
		}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   310
		next if (scalar(@currdecl)==0);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   311
		if ($t eq "const") {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   312
			# check for constant declaration
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   313
			my $ctype = lookup_type($scope, $currdecl[1]);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   314
			if ($ctype->{basic} and $currdecl[2]=~/^\w+$/ and $currdecl[3] eq '=') {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   315
				if ($typedef!=0) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   316
					die "Syntax error at line $$line\n";
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   317
				}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   318
				shift @currdecl;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   319
				shift @currdecl;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   320
				my $type = $ctype->{name};
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   321
				my $name = shift @currdecl;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   322
				my $size = $ctype->{size};
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   323
				shift @currdecl;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   324
				my $value = get_constant_expr($scope,\@currdecl,$line);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   325
				$values{$name} = {type=>$type, size=>$size, value=>$value};
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   326
				next;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   327
			}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   328
		}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   329
		if (skip_qualifiers(\@currdecl)!=0 or ($scope->{file} and !$typedef)) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   330
			next;	# function declaration or stuff at file scope
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   331
		}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   332
		my $type1 = shift @currdecl;	# type, type pointed to or return type
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   333
		if ($type1 !~ /^\w+$/) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   334
			die "Syntax error at line $$line\n";
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   335
		}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   336
		my $ind1 = parse_indirection(\@currdecl);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   337
		my $ident;	# identifier being declared
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   338
		my $size = -1;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   339
		my $array = -1;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   340
		my $align = 0;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   341
		my $alias;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   342
		my $category;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   343
		if ($currdecl[0] eq '(' and $currdecl[1] eq '*' and $currdecl[2]=~/^\w+$/) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   344
			# function pointer
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   345
			$ident = $currdecl[2];
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   346
			$size = 4;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   347
			$category = 'fptr';
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   348
			shift @currdecl;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   349
			shift @currdecl;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   350
			shift @currdecl;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   351
		} elsif ($currdecl[0]=~/^\w+$/) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   352
			$ident = shift @currdecl;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   353
			if ($currdecl[0] ne '(') {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   354
				# not function declaration
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   355
				if ($ind1>0) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   356
					# pointer
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   357
					$category = 'ptr';
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   358
					$size = 4;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   359
				} else {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   360
					my $type2 = lookup_type($scope, $type1);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   361
					if (!defined($type2)) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   362
						die "Unrecognised type $type1 at line $$line\n";
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   363
					}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   364
					if ($type2->{basic}) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   365
						$alias = $type2->{name};
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   366
						$size = $type2->{size};
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   367
						$category = 'basic';
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   368
					} elsif ($type2->{enum}) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   369
						$alias = $type2->{name};
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   370
						$category = 'enum';
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   371
						$size = 4;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   372
					} elsif ($type2->{class}) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   373
						$alias = $type2->{name};
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   374
						$size = $type2->{class}->{size};
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   375
						$category = 'class';
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   376
						$align = $type2->{class}->{align};
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   377
					} elsif ($type->{ptr}) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   378
						$size = 4;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   379
						$category = 'ptr';
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   380
						$align = 4;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   381
					} elsif ($type->{fptr}) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   382
						$size = 4;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   383
						$category = 'ptr';
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   384
						$align = 4;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   385
					}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   386
				}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   387
			}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   388
		}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   389
		if ($size>0) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   390
			# data member declared
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   391
			# check for array
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   392
			if ($currdecl[0] eq '[') {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   393
				shift @currdecl;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   394
				$array = get_constant_expr($scope, \@currdecl, $line);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   395
				if ($array<=0) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   396
					die "Bad array size at line $$line\n";
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   397
				}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   398
				if ($currdecl[0] ne ']') {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   399
					die "Syntax error at line $$line\n";
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   400
				}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   401
			}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   402
			my $members = $scope->{members};
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   403
			my $typedefs = $scope->{typedefs};
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   404
			if ($align==0) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   405
				$align = $size;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   406
			}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   407
			my $am = $align-1;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   408
			unless ($typedef) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   409
				my $al = $curr_offset & $am;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   410
				if ($align==8 and $al!=0) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   411
					die "Bad alignment of 64-bit data $ident at line $$line\n";
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   412
				}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   413
				$curr_offset += ($align-$al) if ($al!=0);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   414
			}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   415
			if ($array>0) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   416
				$size = ($size + $am) &~ $am;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   417
				if ($typedef) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   418
					push @$typedefs, {name=>$ident, category=>$category, alias=>$alias, size=>$size*$array, spacing=>$size, array=>$array};
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   419
				} else {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   420
					push @$members, {name=>$ident, size=>$size*$array, offset=>$curr_offset, spacing=>$size};
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   421
				}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   422
				$size *= $array;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   423
			} else {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   424
				if ($typedef) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   425
					push @$typedefs, {name=>$ident, category=>$category, alias=>$alias, size=>$size};
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   426
				} else {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   427
					push @$members, {name=>$ident, size=>$size, offset=>$curr_offset};
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   428
				}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   429
			}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   430
			unless ($typedef) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   431
				$curr_offset += $size;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   432
				if ($align > $overall_align) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   433
					$overall_align = $align;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   434
				}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   435
			}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   436
		}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   437
	}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   438
	if ($scope->{scope}) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   439
		if ($state==-2) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   440
			die "Missing ; at end of file\n";
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   441
		}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   442
		if (!$scope->{complete}) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   443
			die "Unexpected end of file at line $$line\n";
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   444
		}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   445
		my $total_size = ($curr_offset + $overall_align - 1) &~ ($overall_align - 1);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   446
		$scope->{size} = $total_size;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   447
		$scope->{align} = $overall_align;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   448
	}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   449
}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   450
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   451
sub get_operand($$$) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   452
	my ($scope,$tokens,$line) = @_;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   453
	my $t = get_token($tokens,$line);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   454
	if ($t eq '-') {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   455
		my $x = get_operand($scope,$tokens,$line);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   456
		return -$x;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   457
	} elsif ($t eq '+') {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   458
		my $x = get_operand($scope,$tokens,$line);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   459
		return $x;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   460
	} elsif ($t eq '~') {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   461
		my $x = get_operand($scope,$tokens,$line);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   462
		return ~$x;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   463
	} elsif ($t eq '!') {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   464
		my $x = get_operand($scope,$tokens,$line);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   465
		return $x ? 0 : 1;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   466
	} elsif ($t eq '(') {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   467
		my $x = get_constant_expr($scope,$tokens,$line);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   468
		my $t = get_token($tokens,$line);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   469
		if ($t ne ')') {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   470
			die "Missing ) at line $$line\n";
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   471
		}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   472
		return $x;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   473
	} elsif ($t eq "sizeof") {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   474
		my $ident = get_token($tokens,$line);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   475
		if ($ident eq '(') {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   476
			$ident = get_token($tokens,$line);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   477
			my $cb = get_token($tokens,$line);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   478
			if ($cb ne ')') {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   479
				die "Bad sizeof() syntax at line $$line\n";
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   480
			}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   481
		}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   482
		$ident = look_through_macros($ident);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   483
		if ($ident !~ /^\w+$/) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   484
			die "Bad sizeof() syntax at line $$line\n";
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   485
		}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   486
		my $type = lookup_type($scope, $ident);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   487
		if (!defined $type) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   488
			die "Unrecognised type $ident at line $$line\n";
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   489
		}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   490
		if ($type->{basic}) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   491
			return $type->{size};
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   492
		} elsif ($type->{enum}) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   493
			return 4;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   494
		} elsif ($type->{ptr}) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   495
			return 4;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   496
		} elsif ($type->{fptr}) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   497
			return 4;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   498
		}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   499
		my $al = $type->{class}->{align};
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   500
		my $sz = $type->{class}->{size};
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   501
		return ($sz+$al-1)&~($al-1);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   502
	}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   503
	$t = look_through_macros($t);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   504
	if ($t =~ /^0x[0-9a-f]+/i) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   505
		return oct($t);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   506
	} elsif ($t =~ /^\d/) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   507
		return $t;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   508
	} elsif ($t =~ /^\w+$/) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   509
		my $x = lookup_value($scope,$t);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   510
		die "Unrecognised identifier '$t' at line $$line\n" unless defined($x);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   511
		return $x;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   512
	} else {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   513
		die "Syntax error at line $$line\n";
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   514
	}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   515
}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   516
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   517
sub look_through_macros($) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   518
	my ($ident) = @_;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   519
	while ($ident and $macros{$ident}) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   520
		$ident = $macros{$ident};
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   521
	}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   522
	return $ident;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   523
}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   524
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   525
sub lookup_value($$) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   526
	my ($scope,$ident) = @_;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   527
	while ($scope) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   528
		my $vl = $scope->{values};
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   529
		if (defined($vl->{$ident})) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   530
			return $vl->{$ident}->{value};
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   531
		}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   532
		$scope = $scope->{scope};
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   533
	}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   534
	return undef();
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   535
}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   536
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   537
sub lookup_type($$) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   538
	my ($scope,$ident) = @_;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   539
	if ($basictypes{$ident}) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   540
		return {scope=>$scope, basic=>1, name=>$ident, size=>$basictypes{$ident} };
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   541
	}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   542
	while ($scope) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   543
		if ($basictypes{$ident}) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   544
			return {scope=>$scope, basic=>1, name=>$ident, size=>$basictypes{$ident} };
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   545
		}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   546
		my $el = $scope->{enums};
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   547
		my $cl = $scope->{classes};
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   548
		my $td = $scope->{typedefs};
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   549
		if (grep {$_ eq $ident} @$el) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   550
			return {scope=>$scope, enum=>1, name=>$ident, size=>4 };
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   551
		}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   552
		my @match_class = (grep {$_->{name} eq $ident} @$cl);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   553
		if (scalar(@match_class)) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   554
			return {scope=>$scope, class=>$match_class[0]};
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   555
		}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   556
		my @match_td = (grep {$_->{name} eq $ident} @$td);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   557
		if (scalar(@match_td)) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   558
			my $tdr = $match_td[0];
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   559
			my $cat = $tdr->{category};
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   560
			if ($cat eq 'basic' or $cat eq 'enum' or $cat eq 'class') {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   561
				$ident = $tdr->{alias};
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   562
				next;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   563
			} else {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   564
				return { scope=>$scope, $cat=>1, $size=>$tdr->{size} };
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   565
			}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   566
		}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   567
		$scope = $scope->{scope};
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   568
	}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   569
	return undef();
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   570
}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   571
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   572
sub get_mult_expr($$$) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   573
	my ($scope,$tokens,$line) = @_;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   574
	my $x = get_operand($scope,$tokens,$line);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   575
	my $t;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   576
	for (;;) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   577
		$t = get_token($tokens,$line);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   578
		if ($t eq '*') {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   579
			my $y = get_operand($scope,$tokens,$line);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   580
			$x = $x * $y;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   581
		} elsif ($t eq '/') {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   582
			my $y = get_operand($scope,$tokens,$line);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   583
			$x = int($x / $y);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   584
		} elsif ($t eq '%') {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   585
			my $y = get_operand($scope,$tokens,$line);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   586
			$x = int($x % $y);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   587
		} else {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   588
			last;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   589
		}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   590
	}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   591
	unshift @$tokens, $t;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   592
	return $x;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   593
}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   594
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   595
sub get_add_expr($$$) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   596
	my ($scope,$tokens,$line) = @_;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   597
	my $x = get_mult_expr($scope,$tokens,$line);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   598
	my $t;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   599
	for (;;) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   600
		$t = get_token($tokens,$line);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   601
		if ($t eq '+') {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   602
			my $y = get_mult_expr($scope,$tokens,$line);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   603
			$x = $x + $y;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   604
		} elsif ($t eq '-') {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   605
			my $y = get_mult_expr($scope,$tokens,$line);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   606
			$x = $x - $y;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   607
		} else {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   608
			last;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   609
		}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   610
	}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   611
	unshift @$tokens, $t;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   612
	return $x;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   613
}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   614
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   615
sub get_shift_expr($$$) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   616
	my ($scope,$tokens,$line) = @_;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   617
	my $x = get_add_expr($scope,$tokens,$line);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   618
	my $t, $t2;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   619
	for (;;) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   620
		$t = get_token($tokens,$line);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   621
		if ($t eq '<' or $t eq '>') {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   622
			$t2 = get_token($tokens,$line);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   623
			if ($t2 ne $t) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   624
				unshift @$tokens, $t2;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   625
				last;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   626
			}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   627
		}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   628
		if ($t eq '<') {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   629
			my $y = get_add_expr($scope,$tokens,$line);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   630
			$x = $x << $y;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   631
		} elsif ($t eq '>') {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   632
			my $y = get_add_expr($scope,$tokens,$line);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   633
			$x = $x >> $y;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   634
		} else {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   635
			last;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   636
		}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   637
	}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   638
	unshift @$tokens, $t;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   639
	return $x;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   640
}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   641
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   642
sub get_and_expr($$$) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   643
	my ($scope,$tokens,$line) = @_;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   644
	my $x = get_shift_expr($scope,$tokens,$line);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   645
	my $t;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   646
	for (;;) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   647
		$t = get_token($tokens,$line);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   648
		if ($t eq '&') {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   649
			my $y = get_shift_expr($scope,$tokens,$line);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   650
			$x = $x & $y;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   651
		} else {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   652
			last;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   653
		}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   654
	}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   655
	unshift @$tokens, $t;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   656
	return $x;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   657
}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   658
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   659
sub get_xor_expr($$$) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   660
	my ($scope,$tokens,$line) = @_;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   661
	my $x = get_and_expr($scope,$tokens,$line);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   662
	my $t;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   663
	for (;;) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   664
		$t = get_token($tokens,$line);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   665
		if ($t eq '^') {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   666
			my $y = get_and_expr($scope,$tokens,$line);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   667
			$x = $x ^ $y;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   668
		} else {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   669
			last;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   670
		}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   671
	}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   672
	unshift @$tokens, $t;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   673
	return $x;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   674
}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   675
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   676
sub get_ior_expr($$$) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   677
	my ($scope,$tokens,$line) = @_;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   678
	my $x = get_xor_expr($scope,$tokens,$line);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   679
	my $t;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   680
	for (;;) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   681
		$t = get_token($tokens,$line);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   682
		if ($t eq '|') {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   683
			my $y = get_xor_expr($scope,$tokens,$line);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   684
			$x = $x | $y;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   685
		} else {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   686
			last;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   687
		}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   688
	}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   689
	unshift @$tokens, $t;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   690
	return $x;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   691
}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   692
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   693
sub get_constant_expr($$$) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   694
	my ($scope,$tokens,$line) = @_;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   695
	my $x = get_ior_expr($scope,$tokens,$line);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   696
	return $x;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   697
}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   698
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   699
sub parse_enum($$$$) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   700
	my ($scope,$tokens,$line,$enum_name) = @_;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   701
	my $vl = $scope->{values};
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   702
	my $x = 0;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   703
	for (;;) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   704
		my $t = get_token($tokens,$line);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   705
		last if ($t eq '}');
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   706
		if (!defined($t)) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   707
			die "Unexpected end of file at line $$line\n";
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   708
		}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   709
		if ($t !~ /^\w+$/) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   710
			die "Syntax error at line $$line\n";
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   711
		}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   712
		if (defined($vl->{$t})) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   713
			die "Duplicate identifier at line $$line\n";
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   714
		}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   715
		my $t2 = get_token($tokens,$line);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   716
		if ($t2 eq ',') {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   717
			$vl->{$t} = {type=>$enum_name, size=>4, value=>$x, enum=>1};
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   718
			++$x;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   719
		} elsif ($t2 eq '}') {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   720
			$vl->{$t} = {type=>$enum_name, size=>4, value=>$x, enum=>1};
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   721
			++$x;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   722
			last;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   723
		} elsif ($t2 eq '=') {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   724
			$x = get_constant_expr($scope, $tokens, $line);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   725
			$vl->{$t} = {type=>$enum_name, size=>4, value=>$x, enum=>1};
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   726
			++$x;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   727
			$t2 = get_token($tokens,$line);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   728
			last if ($t2 eq '}');
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   729
			next if ($t2 eq ',');
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   730
			die "Syntax error at line $$line\n";
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   731
		} else {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   732
			unshift @$tokens, $t2;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   733
		}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   734
	}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   735
	my $t = get_token($tokens,$line);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   736
	if ($t ne ';') {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   737
		die "Missing ; at line $$line\n";
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   738
	}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   739
}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   740
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   741
sub parse_decl_def($$$) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   742
	my ($scope,$tokens,$line) = @_;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   743
	my $level=0;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   744
	my @decl;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   745
	while ( scalar(@$tokens) ) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   746
		my $t = get_token($tokens, $line);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   747
		if ($t eq ';' and $level==0) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   748
			return @decl;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   749
		}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   750
		push @decl, $t;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   751
		if ($t eq '{') {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   752
			++$level;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   753
		}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   754
		if ($t eq '}') {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   755
			if ($level==0) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   756
				die "Syntax error at line $$line\n";
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   757
			}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   758
			if (--$level==0) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   759
				return ();	# end of function definition reached
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   760
			}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   761
		}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   762
	}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   763
	die "Unexpected end of file at line $$line\n";
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   764
}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   765
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   766
sub dump_scope($) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   767
	my ($scope) = @_;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   768
	my $el = $scope->{enums};
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   769
	my $cl = $scope->{classes};
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   770
	my $vl = $scope->{values};
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   771
	print "SCOPE: $scope->{name}\n";
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   772
	if (scalar(@$el)) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   773
		print "\tenums:\n";
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   774
		foreach (@$el) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   775
			print "\t\t$_\n";
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   776
		}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   777
	}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   778
	if (scalar(keys(%$vl))) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   779
		print "\tvalues:\n";
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   780
		foreach $vname (keys(%$vl)) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   781
			my $v = $vl->{$vname};
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   782
			my $x = $v->{value};
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   783
			my $t = $v->{type};
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   784
			my $sz = $v->{size};
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   785
			if ($v->{enum}) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   786
				print "\t\t$vname\=$x (enum $t) size=$sz\n";
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   787
			} else {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   788
				print "\t\t$vname\=$x (type $t) size=$sz\n";
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   789
			}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   790
		}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   791
	}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   792
	if ($scope->{scope}) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   793
		my $members = $scope->{members};
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   794
		foreach (@$members) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   795
			my $n = $_->{name};
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   796
			my $sz = $_->{size};
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   797
			my $off = $_->{offset};
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   798
			my $spc = $_->{spacing};
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   799
			if (defined $spc) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   800
				print "\t$n\[\]\: spacing $spc size $sz offset $off\n";
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   801
			} else {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   802
				print "\t$n\: size $sz offset $off\n";
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   803
			}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   804
		}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   805
		print "\tOverall size : $scope->{size}\n";
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   806
		print "\tOverall align: $scope->{align}\n";
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   807
	}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   808
	foreach $s (@$cl) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   809
		dump_scope($s);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   810
	}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   811
}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   812
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   813
sub output_scope($$) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   814
	my ($scope, $out) = @_;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   815
	my $el = $scope->{enums};
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   816
	my $cl = $scope->{classes};
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   817
	my $vl = $scope->{values};
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   818
	my $sn = scope_full_name($scope);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   819
	my $sp = ($scope->{file}) ? "" : $sn."_";
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   820
	if ($scope->{file}) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   821
		push @$out, "";
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   822
		push @$out, &$comment_sub("FILE SCOPE");
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   823
		push @$out, "";
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   824
	} else {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   825
		push @$out, "";
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   826
		push @$out, &$comment_sub($scope->{specifier}." ".$scope->{name});
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   827
		push @$out, "";
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   828
	}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   829
	if (scalar(keys(%$vl))) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   830
		foreach $vname (keys(%$vl)) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   831
			my $v = $vl->{$vname};
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   832
			my $x = $v->{value};
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   833
			my $t = $v->{type};
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   834
			my $sz = $v->{size};
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   835
			push @$out, &$format_sub($sp.$vname, $x);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   836
		}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   837
	}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   838
	if ($scope->{scope}) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   839
		my $members = $scope->{members};
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   840
		foreach (@$members) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   841
			my $n = $_->{name};
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   842
			my $sz = $_->{size};
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   843
			my $off = $_->{offset};
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   844
			my $spc = $_->{spacing};
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   845
			push @$out, &$format_sub($sp.$n, $off);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   846
			if (defined $spc) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   847
				push @$out, &$format_sub($sp.$n."_spc", $spc);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   848
			}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   849
		}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   850
		push @$out, &$format_sub($sp."sz", $scope->{size});
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   851
	}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   852
	foreach $s (@$cl) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   853
		if ($s->{complete})	{
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   854
			output_scope($s, $out);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   855
		}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   856
	}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   857
}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   858
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   859
sub scope_full_name($) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   860
	my ($scope) = @_;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   861
	if ($scope->{file}) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   862
		return "";
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   863
	}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   864
	my $parent = $scope->{scope};
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   865
	if ($parent->{file}) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   866
		return $scope->{name};
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   867
	}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   868
	return scope_full_name($parent)."_".$scope->{name};
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   869
}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   870
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   871
sub pad($$) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   872
	my ($lineref, $n) = @_;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   873
	my $l = length ($$lineref);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   874
	if ($l < $n) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   875
		$$lineref .= ' 'x($n-$l);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   876
	}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   877
}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   878
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   879
#
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   880
# Subroutines for ARMASM compatible output
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   881
#
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   882
sub armasm_format($$;$) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   883
	my ($name, $value, $comment) = @_;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   884
	my $r = "$name ";
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   885
	pad(\$r, 40);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   886
	$r .= sprintf("EQU 0x%08x", $value & 0xFFFFFFFF);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   887
	if ($comment and $comment!~/^\s*$/) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   888
		$r .= " ";
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   889
		pad(\$r, 60);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   890
		$r .= "; $comment";
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   891
	}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   892
	return $r;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   893
}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   894
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   895
sub armasm_comment($) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   896
	my ($comment) = @_;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   897
	return "; $comment";
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   898
}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   899
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   900
sub armasm_end() {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   901
	return "\n\tEND\n";
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   902
}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   903
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   904
#
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   905
# Subroutines for GNU AS compatible output
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   906
#
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   907
sub as_format($$;$) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   908
	my ($name, $value, $comment) = @_;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   909
	my $r = "    .equ $name, ";
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   910
	pad(\$r, 50);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   911
	$r .= sprintf("0x%08x", $value & 0xFFFFFFFF);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   912
	if ($comment and $comment!~/^\s*$/) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   913
		$r .= " ";
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   914
		pad(\$r, 65);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   915
		$r .= "/* $comment */";
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   916
	}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   917
	return $r;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   918
}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   919
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   920
sub as_comment($) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   921
	my ($comment) = @_;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   922
	if (length ($comment) > 0) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   923
		return "/* $comment */";
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   924
	} else {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   925
		return "";
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   926
	}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   927
}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   928
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   929
sub as_end() {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   930
	return "";
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   931
}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   932
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   933
#
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   934
# Subroutines for Turbo Assembler compatible output
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   935
#
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   936
sub tasm_format($$;$) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   937
	my ($name, $value, $comment) = @_;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   938
	my $r = "$name ";
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   939
	pad(\$r, 40);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   940
	$r .= sprintf("EQU 0%08xh", $value & 0xFFFFFFFF);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   941
	if ($comment and $comment!~/^\s*$/) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   942
		$r .= " ";
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   943
		pad(\$r, 60);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   944
		$r .= "; $comment";
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   945
	}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   946
	return $r;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   947
}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   948
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   949
sub tasm_comment($) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   950
	my ($comment) = @_;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   951
	return "; $comment";
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   952
}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   953
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   954
sub tasm_end() {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   955
	return "";
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   956
}