Merge pull request #4621 from alexanderkyte/strdup_env
[mono.git] / mono / cil / make-opcodes-def.pl
1 #!/usr/bin/perl
2 #
3 # make-opcodes-def.pl: Loads the opcodes from the CIL-opcodes.xml and
4 # generates a spec compliant opcodes.def file
5 #
6 # Author: 
7 #   Miguel de Icaza (miguel@ximian.com)
8 #
9 # (C) 2001 Ximian, Inc.
10 #
11 # We should really be doing this with XSLT, but I know nothing about XSLT
12 # ;-)
13 # or maybe just an XML::Parser... - lupus
14
15 use strict;
16 use XML::Parser;
17
18 my %valid_flow;
19 # the XML file also includes "throw"
20 @valid_flow{qw(next call return branch meta cond-branch)} = ();
21
22 open OUTPUT, ">$ARGV[1]" || die "Can not create $ARGV[1] file: $!";
23
24 my $parser = new XML::Parser (Handlers => {Start => \&handle_opcode});
25 print_header();
26 $parser->parsefile($ARGV[0]);
27 print_trailer();
28 close(OUTPUT) || die "Can not close file: $!";
29
30 sub handle_opcode {
31     my ($parser, $elem, %attrs) = @_;
32     my ($name, $input, $output, $args, $o1, $o2, $flow, $uname, $count, $ff);
33         
34     return if ($elem ne 'opcode');
35
36     ($name, $input, $output, $args, $o1, $o2, $flow) = 
37                 @attrs{qw(name input output args o1 o2 flow)};
38
39     $uname = uc $name;
40     $uname =~ tr/./_/;
41     if (hex($o1) == 0xff){
42         $count = 1;
43     } else {
44         $count = 2;
45     }
46
47     $ff = "ERROR";
48     if (exists $valid_flow{$flow}) {
49         $ff = uc $flow;
50         $ff =~ tr/-/_/;
51     }
52
53     print OUTPUT "OPDEF(CEE_$uname, \"$name\", $input, $output, $args, X, $count, $o1, $o2, $ff)\n";
54     
55 }
56
57 sub print_header {
58 print OUTPUT<<EOF;
59 /* GENERATED FILE, DO NOT EDIT */
60 EOF
61 }
62
63 sub print_trailer {
64 print OUTPUT<<EOF;
65 #ifndef OPALIAS
66 #define _MONO_CIL_OPALIAS_DEFINED_
67 #define OPALIAS(a,s,r)
68 #endif
69
70 OPALIAS(CEE_BRNULL,     "brnull",    CEE_BRFALSE)
71 OPALIAS(CEE_BRNULL_S,   "brnull.s",  CEE_BRFALSE_S)
72 OPALIAS(CEE_BRZERO,     "brzero",    CEE_BRFALSE)
73 OPALIAS(CEE_BRZERO_S,   "brzero.s",  CEE_BRFALSE_S)
74 OPALIAS(CEE_BRINST,     "brinst",    CEE_BRTRUE)
75 OPALIAS(CEE_BRINST_S,   "brinst.s",  CEE_BRTRUE_S)
76 OPALIAS(CEE_LDIND_U8,   "ldind.u8",  CEE_LDIND_I8)
77 OPALIAS(CEE_LDELEM_U8,  "ldelem.u8", CEE_LDELEM_I8)
78 OPALIAS(CEE_LDX_I4_MIX, "ldc.i4.M1", CEE_LDC_I4_M1)
79 OPALIAS(CEE_ENDFAULT,   "endfault",  CEE_ENDFINALLY)
80
81 #ifdef _MONO_CIL_OPALIAS_DEFINED_
82 #undef OPALIAS
83 #undef _MONO_CIL_OPALIAS_DEFINED_
84 #endif
85 EOF
86 }
87