gecko-dev/js2/tools/gencode.pl

279 строки
8.2 KiB
Perl
Исходник Обычный вид История

2000-10-11 06:42:39 +04:00
#!/usr/bin/perl
#
# The contents of this file are subject to the Netscape Public
# License Version 1.1 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy of
# the License at http://www.mozilla.org/NPL/
#
# Software distributed under the License is distributed on an "AS
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
2000-10-11 06:42:39 +04:00
# implied. See the License for the specific language governing
# rights and limitations under the License.
#
# The Original Code is JavaScript 2 Prototype.
2000-10-11 06:42:39 +04:00
#
# The Initial Developer of the Original Code is Netscape
# Communications Corporation. Portions created by Netscape are
# Copyright (C) 1997-1999 Netscape Communications Corporation. All
# Rights Reserved.
#
# Alternatively, the contents of this file may be used under the
# terms of the GNU Public License (the "GPL"), in which case the
# provisions of the GPL are applicable instead of those above.
# If you wish to allow use of your version of this file only
# under the terms of the GPL and not to allow others to use your
# version of this file under the NPL, indicate your decision by
# deleting the provisions above and replace them with the notice
# and other provisions required by the GPL. If you do not delete
# the provisions above, a recipient may use your version of this
# file under either the NPL or the GPL.
#
# Contributers:
#
2000-04-28 17:17:35 +04:00
use strict;
2000-10-11 06:42:39 +04:00
use jsicodes;
##############################################################################
# HELLO, ARE YOU READING THIS?
# The opcode definition is now in jsicodes.pm, please go there to make changes.
##############################################################################
my $tab = " ";
my $init_tab = $tab;
my $enum_decs = "";
my $class_decs = "";
2000-04-18 05:19:12 +04:00
my @name_array;
my $opcode_maxlen = 0;
my $k;
if (!$ARGV[0]) {
2000-04-25 01:43:49 +04:00
# no args, collect all opcodes
2000-10-11 06:42:39 +04:00
for $k (sort(keys(%jsicodes::ops))) {
&collect($k);
}
} else {
2000-04-25 01:43:49 +04:00
# collect defs for only the opcodes specified on the command line
while ($k = pop(@ARGV)) {
2000-04-25 01:43:49 +04:00
&collect (uc($k));
}
}
&spew;
sub collect {
2000-04-25 01:43:49 +04:00
# grab the info from the $k record in $ops, and append it to
# $enum_decs, @name_array, and $class_decs.
my ($k) = @_;
2000-04-18 05:19:12 +04:00
if (length($k) > $opcode_maxlen) {
$opcode_maxlen = length($k);
}
2000-10-11 06:42:39 +04:00
my $c = $jsicodes::ops{$k};
2000-04-25 01:43:49 +04:00
if (!$c) {
die ("Unknown opcode, $k\n");
}
my $opname = $k;
my $cname = jsicodes::get_classname ($k);
my $super = $c->{"super"};
my $constructor = $super;
2000-04-25 02:40:53 +04:00
my @params;
if ($c->{"params"}) {
@params = @{$c->{"params"}};
} else {
@params = ();
}
my $rem = $c->{"rem"};
2000-04-18 11:04:24 +04:00
my ($dec_list, $call_list, $template_list) =
2000-04-25 02:40:53 +04:00
&get_paramlists(@params);
2000-04-28 06:20:23 +04:00
my @types = split (", ", $template_list);
2000-04-25 02:40:53 +04:00
my $constr_params = $call_list ? $opname . ", " . $call_list : $opname;
2000-04-18 11:04:24 +04:00
if ($super =~ /Instruction_\d/) {
$super .= "<" . $template_list . ">";
2000-04-18 05:19:12 +04:00
}
2000-04-18 05:19:12 +04:00
push (@name_array, $opname);
$enum_decs .= "$init_tab$tab$opname, /* $rem */\n";
if ($super) {
$class_decs .= ($init_tab . "class $cname : public $super {\n" .
$init_tab . "public:\n" .
$init_tab . $tab . "/* $rem */\n" .
$init_tab . $tab . "$cname ($dec_list) :\n" .
$init_tab . $tab . $tab . "$super\n" .
"$init_tab$tab$tab($constr_params) " .
"{};\n");
2000-04-28 19:32:54 +04:00
if (!$c->{"super_has_print"}) {
$class_decs .= ($init_tab . $tab .
"virtual Formatter& print(Formatter& f) {\n" .
$init_tab . $tab . $tab . "f << opcodeNames[$opname]" .
&get_print_body(@types) . ";\n" .
$init_tab . $tab . $tab . "return f;\n" .
$init_tab . $tab . "}\n");
my $printops_body = &get_printops_body(@types);
my $printops_decl = "virtual Formatter& printOperands(Formatter& f, ";
2000-09-20 00:35:05 +04:00
# $printops_decl .= ($dec_list =~ /ArgumentList/) ?
# "const JSValues& registers" :
# "const JSValues& registers";
$printops_decl .= ($printops_body eq "") ?
"const JSValues& /*registers*/" :
2000-08-23 05:07:19 +04:00
"const JSValues& registers";
$printops_decl .= ") {\n";
2000-04-28 17:17:35 +04:00
$class_decs .= ($init_tab . $tab .
$printops_decl .
$printops_body .
$init_tab . $tab . $tab . "return f;\n" .
$init_tab . $tab . "}\n");
} else {
$class_decs .= $init_tab . $tab .
"/* print() and printOperands() inherited from $super */\n";
}
$class_decs .= $init_tab . "};\n\n";
}
}
sub spew {
2000-04-25 01:43:49 +04:00
# print the info in $enum_decs, @name_aray, and $class_decs to stdout.
2000-04-18 05:19:12 +04:00
my $opname;
print "// THIS FILE IS MACHINE GENERATED! DO NOT EDIT BY HAND!\n\n";
print "#if !defined(OPCODE_NAMES)\n\n";
print $tab . "enum {\n$enum_decs$tab};\n\n";
print $class_decs;
print "#else\n\n";
2000-04-18 05:19:12 +04:00
print $tab . "char *opcodeNames[] = {\n";
2000-04-18 05:19:12 +04:00
for $opname (@name_array) {
print "$tab$tab\"$opname";
for (0 .. $opcode_maxlen - length($opname) - 1) {
print " ";
}
print "\",\n"
}
print "$tab};\n\n";
print "#endif\n\n"
}
sub get_paramlists {
2000-04-25 01:43:49 +04:00
# parse the params entry (passed into @types) into various parameter lists
# used in the class declaration
my @types = @_;
my @dec;
my @call;
my @tostr;
my @template;
my $op = 1;
my $type;
for $type (@types) {
my $pfx;
my $deref;
my $member;
my $default;
($type, $default) = split (" = ", $type);
if ($default ne "") {
$default = " = " . $default;
}
$pfx = $deref = "";
$member = "mOp$op";
push (@dec, "$type aOp$op" . "$default");
push (@call, "aOp$op");
push (@template, $type);
2000-04-18 11:04:24 +04:00
$op++;
}
return (join (", ", @dec), join (", ", @call), join (", ", @template));
}
2000-04-28 06:20:23 +04:00
sub get_print_body {
2000-04-25 01:43:49 +04:00
# generate the body of the print() function
2000-04-18 11:04:24 +04:00
my (@types) = @_;
my $type;
my @oplist;
my $op = 1;
my $in = $init_tab . $tab . $tab;
2000-04-18 11:04:24 +04:00
for $type (@types) {
2000-06-21 02:44:46 +04:00
if ($type eq "TypedRegister") {
push (@oplist, "mOp$op" );
} elsif ($type eq "Label*") {
push (@oplist, "\"Offset \" << ((mOp$op) ? mOp$op->mOffset : NotAnOffset)")
2000-06-29 05:21:41 +04:00
} elsif ($type =~ /String/) {
push (@oplist, "\"'\" << *mOp$op << \"'\"");
} elsif ($type =~ /JSType *\*/) {
push (@oplist, "\"'\" << mOp$op->getName() << \"'\"");
} elsif ($type =~ /JSFunction *\*/) {
push (@oplist, "\"JSFunction\"");
2000-06-21 02:44:46 +04:00
} elsif ($type =~ /bool/) {
push (@oplist, "\"'\" << ((mOp$op) ? \"true\" : \"false\") << \"'\"");
2000-06-22 02:32:21 +04:00
} elsif ($type =~ /ICodeModule/) {
push (@oplist, "\"ICodeModule\"");
} elsif ($type =~ /FunctionDefinition/) {
push (@oplist, "\"FunctionDefinition\"");
} elsif ($type =~ /JSClass *\*/) {
2000-06-29 05:21:41 +04:00
push (@oplist, "mOp$op->getName()");
} else {
push (@oplist, "mOp$op");
}
2000-04-18 11:04:24 +04:00
$op++;
}
my $rv = join (" << \", \" << ", @oplist);
2000-04-18 11:04:24 +04:00
if ($rv ne "") {
$rv = " << \"\\t\" << " . $rv;
2000-04-18 11:04:24 +04:00
}
2000-04-28 09:39:27 +04:00
return $rv;
}
2000-04-28 06:20:23 +04:00
2000-04-28 09:39:27 +04:00
sub get_printops_body {
2000-04-28 19:32:54 +04:00
# generate the body of the printOperands() function
2000-04-28 09:39:27 +04:00
my (@types) = @_;
my $type;
my @oplist;
my $op = 1;
my $in = $init_tab . $tab . $tab;
2000-04-28 06:20:23 +04:00
2000-04-28 09:39:27 +04:00
for $type (@types) {
2000-06-21 02:44:46 +04:00
if ($type eq "TypedRegister") {
2000-08-23 05:07:19 +04:00
push (@oplist, "getRegisterValue(registers, mOp$op.first)");
# push (@oplist, "mOp$op.first");
# push (@oplist, "\"R\" << mOp$op.first << '=' << registers[mOp$op.first]");
} elsif ($type eq "ArgumentList") {
2000-04-28 17:17:35 +04:00
push (@oplist, "ArgList(mOp$op, registers)");
2000-04-28 09:39:27 +04:00
}
$op++;
}
my $rv = join (" << \", \" << ", @oplist);
if ($rv ne "") {
$rv = $init_tab . $tab . $tab . "f << " . $rv . ";\n";
}
return $rv;
2000-04-28 06:20:23 +04:00
}