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