dfhack/plugins/ruby/codegen.pl

391 lines
10 KiB
Perl

2012-03-26 06:37:18 -06:00
#!/usr/bin/perl
use strict;
use warnings;
use XML::LibXML;
our @lines_rb;
2012-04-03 08:09:29 -06:00
my @lines_cpp;
my @include_cpp;
my %offsets;
2012-03-26 06:37:18 -06:00
sub indent_rb(&) {
my ($sub) = @_;
my @lines;
{
local @lines_rb;
$sub->();
@lines = map { " " . $_ } @lines_rb;
}
push @lines_rb, @lines
}
sub rb_ucase {
my ($name) = @_;
2012-03-31 17:33:30 -06:00
return $name if ($name eq uc($name));
2012-03-26 06:37:18 -06:00
return join("", map { ucfirst $_ } (split('_', $name)));
}
2012-04-02 11:00:38 -06:00
my %global_type_renderer = (
2012-03-31 17:33:30 -06:00
'enum-type' => \&render_global_enum,
'struct-type' => \&render_global_class,
'class-type' => \&render_global_class,
'bitfield-type' => \&render_global_bitfield,
);
2012-04-02 11:00:38 -06:00
my %item_renderer = (
2012-03-31 17:33:30 -06:00
'global' => \&render_item_global,
2012-04-02 11:00:38 -06:00
'number' => \&render_item_number,
2012-03-31 17:33:30 -06:00
'container' => \&render_item_container,
'compound' => \&render_item_compound,
2012-04-02 11:00:38 -06:00
'pointer' => \&render_item_pointer,
'static-array' => \&render_item_staticarray,
'primitive' => \&render_item_primitive,
'bytes' => \&render_item_bytes,
2012-03-26 06:37:18 -06:00
);
2012-04-02 11:00:38 -06:00
2012-03-26 06:37:18 -06:00
sub render_global_enum {
my ($name, $type) = @_;
my $rbname = rb_ucase($name);
push @lines_rb, "class $rbname";
indent_rb {
2012-03-31 17:33:30 -06:00
render_enum_fields($type);
2012-03-26 06:37:18 -06:00
};
push @lines_rb, "end";
}
2012-03-31 17:33:30 -06:00
sub render_enum_fields {
my ($type) = @_;
my $value = -1;
for my $item ($type->findnodes('child::enum-item')) {
$value = $item->getAttribute('value') || ($value+1);
my $elemname = $item->getAttribute('name'); # || "unk_$value";
if ($elemname) {
my $rbelemname = rb_ucase($elemname);
push @lines_rb, "$rbelemname = $value";
}
}
}
2012-04-02 11:00:38 -06:00
2012-03-26 06:37:18 -06:00
sub render_global_bitfield {
my ($name, $type) = @_;
my $rbname = rb_ucase($name);
push @lines_rb, "class $rbname < MemStruct";
indent_rb {
2012-03-31 17:33:30 -06:00
render_bitfield_fields($type);
2012-03-26 06:37:18 -06:00
};
push @lines_rb, "end";
}
2012-03-31 17:33:30 -06:00
sub render_bitfield_fields {
my ($type) = @_;
my $shift = 0;
for my $field ($type->findnodes('child::ld:field')) {
my $count = $field->getAttribute('count') || 1;
my $name = $field->getAttribute('name');
$name = $field->getAttribute('ld:anon-name') if (!$name);
print "bitfield $name !number\n" if (!($field->getAttribute('ld:meta') eq 'number'));
if ($count == 1) {
push @lines_rb, "bit :$name, $shift" if ($name);
} else {
push @lines_rb, "bits :$name, $shift, $count" if ($name);
}
$shift += $count;
}
}
2012-04-02 11:00:38 -06:00
my $cpp_var_counter = 0;
2012-03-26 06:37:18 -06:00
sub render_global_class {
my ($name, $type) = @_;
my $cppvar = "v_$cpp_var_counter";
$cpp_var_counter++;
push @lines_cpp, "}" if @include_cpp;
push @lines_cpp, "void cpp_$name(FILE *fout) {";
push @lines_cpp, " df::$name *$cppvar = (df::$name*)moo;";
2012-04-03 08:09:29 -06:00
push @include_cpp, $name;
2012-03-26 06:37:18 -06:00
my $rbname = rb_ucase($name);
my $parent = rb_ucase($type->getAttribute('inherits-from') || 'MemStruct');
push @lines_rb, "class $rbname < $parent";
indent_rb {
render_struct_fields($type, "(*$cppvar)");
2012-03-26 06:37:18 -06:00
};
push @lines_rb, "end";
}
2012-04-02 11:00:38 -06:00
sub render_struct_fields {
2012-04-03 08:09:29 -06:00
my ($type, $cppvar) = @_;
2012-03-26 06:37:18 -06:00
2012-04-02 11:00:38 -06:00
for my $field ($type->findnodes('child::ld:field')) {
my $name = $field->getAttribute('name');
$name = $field->getAttribute('ld:anon-name') if (!$name);
next if (!$name);
2012-04-03 08:09:29 -06:00
my $offset = get_offset($cppvar, $name);
2012-03-31 17:33:30 -06:00
2012-04-02 11:00:38 -06:00
push @lines_rb, "field(:$name, $offset) {";
2012-03-31 17:33:30 -06:00
indent_rb {
2012-04-03 08:09:29 -06:00
render_item($field, "$cppvar.$name");
2012-03-31 17:33:30 -06:00
};
2012-04-02 11:00:38 -06:00
push @lines_rb, "}";
2012-03-31 17:33:30 -06:00
}
2012-03-26 06:37:18 -06:00
}
2012-04-02 11:00:38 -06:00
sub render_item {
2012-04-03 08:09:29 -06:00
my ($item, $cppvar) = @_;
2012-04-02 11:00:38 -06:00
return if (!$item);
my $meta = $item->getAttribute('ld:meta');
my $renderer = $item_renderer{$meta};
if ($renderer) {
2012-04-03 08:09:29 -06:00
$renderer->($item, $cppvar);
2012-03-26 06:37:18 -06:00
} else {
2012-04-02 11:00:38 -06:00
print "no render item $meta\n";
2012-03-26 06:37:18 -06:00
}
}
2012-04-02 11:00:38 -06:00
sub render_item_global {
2012-04-03 08:09:29 -06:00
my ($item, $cppvar) = @_;
2012-03-31 17:33:30 -06:00
2012-04-02 11:00:38 -06:00
my $typename = $item->getAttribute('type-name');
my $rbname = rb_ucase($typename);
2012-03-31 17:33:30 -06:00
2012-04-02 11:00:38 -06:00
push @lines_rb, "global :$rbname";
2012-03-31 17:33:30 -06:00
}
2012-04-02 11:00:38 -06:00
sub render_item_number {
2012-04-03 08:09:29 -06:00
my ($item, $cppvar) = @_;
2012-03-31 17:33:30 -06:00
2012-04-02 11:00:38 -06:00
my $subtype = $item->getAttribute('ld:subtype');
$subtype = $item->getAttribute('base-type') if ($subtype eq 'enum');
$subtype = 'int32_t' if (!$subtype);
if ($subtype eq 'int64_t') {
push @lines_rb, 'number 64, true';
} elsif ($subtype eq 'uint32_t') {
push @lines_rb, 'number 32, false';
} elsif ($subtype eq 'int32_t') {
push @lines_rb, 'number 32, true';
} elsif ($subtype eq 'uint16_t') {
push @lines_rb, 'number 16, false';
} elsif ($subtype eq 'int16_t') {
push @lines_rb, 'number 16, true';
} elsif ($subtype eq 'uint8_t') {
push @lines_rb, 'number 8, false';
} elsif ($subtype eq 'int8_t') {
push @lines_rb, 'number 8, false';
} elsif ($subtype eq 'bool') {
push @lines_rb, 'number 8, true';
} elsif ($subtype eq 's-float') {
push @lines_rb, 'float';
2012-03-31 17:33:30 -06:00
} else {
2012-04-02 11:00:38 -06:00
print "no render number $subtype\n";
2012-03-31 17:33:30 -06:00
}
}
2012-04-02 11:00:38 -06:00
sub render_item_compound {
2012-04-03 08:09:29 -06:00
my ($item, $cppvar) = @_;
2012-03-31 17:33:30 -06:00
2012-04-02 11:00:38 -06:00
my $subtype = $item->getAttribute('ld:subtype');
if (!$subtype || $subtype eq 'bitfield') {
push @lines_rb, "compound {";
indent_rb {
if (!$subtype) {
2012-04-03 08:09:29 -06:00
render_struct_fields($item, $cppvar);
2012-04-02 11:00:38 -06:00
} else {
render_bitfield_fields($item);
}
};
push @lines_rb, "}"
} elsif ($subtype eq 'enum') {
# declare constants
render_enum_fields($item);
# actual field
2012-04-03 08:09:29 -06:00
render_item_number($item, $cppvar);
2012-03-31 17:33:30 -06:00
} else {
2012-04-02 11:00:38 -06:00
print "no render compound $subtype\n";
2012-03-31 17:33:30 -06:00
}
}
2012-04-02 11:00:38 -06:00
sub render_item_container {
2012-04-03 08:09:29 -06:00
my ($item, $cppvar) = @_;
2012-03-26 06:37:18 -06:00
2012-04-02 11:00:38 -06:00
my $subtype = $item->getAttribute('ld:subtype');
my $rbmethod = join('_', split('-', $subtype));
my $tg = $item->findnodes('child::ld:item')->[0];
# df-linked-list has no list[0]
if ($tg and $rbmethod ne 'df_linked_list') {
2012-04-03 08:09:29 -06:00
my $tglen = get_tglen($tg, $cppvar);
2012-04-02 11:00:38 -06:00
push @lines_rb, "$rbmethod($tglen) {";
indent_rb {
2012-04-03 08:09:29 -06:00
render_item($tg, "${cppvar}[0]");
2012-04-02 11:00:38 -06:00
};
push @lines_rb, "}";
} else {
push @lines_rb, "$rbmethod";
2012-03-26 06:37:18 -06:00
}
}
2012-04-02 11:00:38 -06:00
sub render_item_pointer {
2012-04-03 08:09:29 -06:00
my ($item, $cppvar) = @_;
2012-03-31 17:33:30 -06:00
2012-04-02 11:00:38 -06:00
my $tg = $item->findnodes('child::ld:item')->[0];
2012-04-03 08:09:29 -06:00
my $tglen = get_tglen($tg, $cppvar);
2012-04-02 11:00:38 -06:00
push @lines_rb, "pointer($tglen) {";
indent_rb {
2012-04-03 08:09:29 -06:00
render_item($tg, "${cppvar}[0]");
2012-04-02 11:00:38 -06:00
};
push @lines_rb, "}";
2012-03-31 17:33:30 -06:00
}
2012-04-02 11:00:38 -06:00
sub render_item_staticarray {
2012-04-03 08:09:29 -06:00
my ($item, $cppvar) = @_;
2012-03-31 17:33:30 -06:00
2012-04-02 11:00:38 -06:00
my $count = $item->getAttribute('count');
my $tg = $item->findnodes('child::ld:item')->[0];
2012-04-03 08:09:29 -06:00
my $tglen = get_tglen($tg, $cppvar);
2012-04-02 11:00:38 -06:00
push @lines_rb, "static_array($count, $tglen) {";
indent_rb {
2012-04-03 08:09:29 -06:00
render_item($tg, "${cppvar}[0]");
2012-04-02 11:00:38 -06:00
};
push @lines_rb, "}";
2012-03-31 17:33:30 -06:00
}
sub render_item_primitive {
2012-04-03 08:09:29 -06:00
my ($item, $cppvar) = @_;
2012-03-31 17:33:30 -06:00
my $subtype = $item->getAttribute('ld:subtype');
if ($subtype eq 'stl-string') {
2012-04-02 11:00:38 -06:00
push @lines_rb, "stl_string";
2012-03-31 17:33:30 -06:00
} else {
2012-04-02 11:00:38 -06:00
print "no render primitive $subtype\n";
2012-03-31 17:33:30 -06:00
}
}
2012-04-02 11:00:38 -06:00
sub render_item_bytes {
2012-04-03 08:09:29 -06:00
my ($item, $cppvar) = @_;
2012-03-26 06:37:18 -06:00
2012-04-02 11:00:38 -06:00
my $subtype = $item->getAttribute('ld:subtype');
if ($subtype eq 'padding') {
} elsif ($subtype eq 'static-string') {
my $size = $item->getAttribute('size');
push @lines_rb, "static_string($size)";
2012-03-31 17:33:30 -06:00
} else {
2012-04-02 11:00:38 -06:00
print "no render bytes $subtype\n";
2012-03-31 17:33:30 -06:00
}
}
2012-04-03 08:09:29 -06:00
sub get_offset {
my ($cppvar, $fname) = @_;
2012-04-05 08:46:07 -06:00
# GCC fails with this
#return query_cpp("offsetof(typeof($cppvar), $fname)");
return query_cpp("((char*)&$cppvar.$fname - (char*)&$cppvar)");
2012-04-03 08:09:29 -06:00
}
2012-04-02 11:00:38 -06:00
sub get_tglen {
2012-04-03 08:09:29 -06:00
my ($tg, $cppvar) = @_;
if (!$tg) {
return 'nil';
2012-04-03 08:09:29 -06:00
}
2012-03-31 17:33:30 -06:00
2012-04-02 11:00:38 -06:00
my $meta = $tg->getAttribute('ld:meta');
if ($meta eq 'number') {
return $tg->getAttribute('ld:bits')/8;
} elsif ($meta eq 'pointer') {
return 4;
2012-03-31 17:33:30 -06:00
} else {
2012-04-03 08:09:29 -06:00
return query_cpp("sizeof(${cppvar}[0])");
2012-03-31 17:33:30 -06:00
}
}
2012-03-26 06:37:18 -06:00
2012-04-03 08:09:29 -06:00
sub query_cpp {
my ($query) = @_;
my $ans = $offsets{$query};
return $ans if (defined($ans));
2012-04-03 08:09:29 -06:00
push @lines_cpp, " fprintf(fout, \"%s = %d\\n\", \"$query\", $query);";
2012-04-03 08:09:29 -06:00
return "'$query'";
}
2012-03-26 06:37:18 -06:00
my $input = $ARGV[0] || '../../library/include/df/codegen.out.xml';
2012-04-03 08:09:29 -06:00
# run once with output = 'ruby-autogen.cpp'
# compile
# execute, save output to 'ruby-autogen.offsets'
# re-run this script with output = 'ruby-autogen.rb' and offsetfile = 'ruby-autogen.offsets'
# delete binary
# delete offsets
my $output = $ARGV[1] or die "need output file";
my $offsetfile = $ARGV[2];
if ($offsetfile) {
2012-04-05 08:46:07 -06:00
open OF, "<$offsetfile";
while (my $line = <OF>) {
chomp($line);
my ($key, $val) = split(' = ', $line);
$offsets{$key} = $val;
2012-04-03 08:09:29 -06:00
}
2012-04-05 08:46:07 -06:00
close OF;
2012-04-03 08:09:29 -06:00
}
2012-03-26 06:37:18 -06:00
my $doc = XML::LibXML->new()->parse_file($input);
my %global_types;
$global_types{$_->getAttribute('type-name')} = $_ foreach $doc->findnodes('/ld:data-definition/ld:global-type');
for my $name (sort { $a cmp $b } keys %global_types) {
my $type = $global_types{$name};
my $meta = $type->getAttribute('ld:meta');
2012-04-02 11:00:38 -06:00
my $renderer = $global_type_renderer{$meta};
2012-03-26 06:37:18 -06:00
if ($renderer) {
$renderer->($name, $type);
} else {
print "no render global type $meta\n";
}
}
for my $obj ($doc->findnodes('/ld:data-definition/ld:global-object')) {
my $name = $obj->getAttribute('name');
2012-04-03 08:09:29 -06:00
# TODO
2012-03-26 06:37:18 -06:00
}
2012-04-03 08:09:29 -06:00
open FH, ">$output";
if ($output =~ /\.cpp$/) {
2012-04-05 08:46:07 -06:00
print FH "#include \"Core.h\"\n";
print FH "#include \"Console.h\"\n";
print FH "#include \"Export.h\"\n";
print FH "#include \"PluginManager.h\"\n";
print FH "#include \"DataDefs.h\"\n";
print FH "#include \"df/$_.h\"\n" for @include_cpp;
print FH "#include <stdio.h>\n";
print FH "static void *moo[1024];\n";
print FH "$_\n" for @lines_cpp;
print FH "}\n";
2012-04-05 08:46:07 -06:00
print FH "int main(int argc, char **argv) {\n";
print FH " FILE *fout;\n";
2012-04-05 08:46:07 -06:00
print FH " if (argc < 2) return 1;\n";
# sometimes gcc will generate accesses to the structures at 0 just for a sizeof/offsetof, this works around the segfaults...
print FH " for (int i=0 ; i<1024 ; i++) moo[i] = &moo;\n";
print FH " fout = fopen(argv[1], \"w\");\n";
print FH " cpp_$_(fout);\n" for @include_cpp;
print FH " fclose(fout);\n";
2012-04-05 08:46:07 -06:00
print FH " return 0;\n";
2012-04-03 08:09:29 -06:00
print FH "}\n";
} else {
print FH "$_\n" for @lines_rb;
}
2012-03-26 06:37:18 -06:00
close FH;