nelhage / bemu

A just-in-time compiler for MIT 6.004's "Beta" processor.

This URL has Read+Write access

bemu / insts.pl
100644 201 lines (168 sloc) 5.186 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
use strict;
use warnings;
use List::Util qw(max);
 
my %arith = ( # imm, r imm, r/m r, r/m r/m, r
    add => [ undef, [0x81, 0], 0x01, 0x03 ],
    and => [ undef, [0x81, 4], 0x21, 0x23 ],
    cmp => [ undef, [0x81, 7], 0x39, 0x3B ],
    lea => [ undef, undef, undef, 0x8D ],
    mov => [ 0xB8, [0xC7, 0], 0x89, 0x8B ],
    or => [ undef, [0x81, 1], 0x09, 0x0B ],
    sub => [ undef, [0x81, 5], 0x29, 0x2B ],
    test => [ undef, [0xF7, 0], 0x85, undef ],
    xor => [ undef, [0x81, 6], 0x31, 0x33 ]
   );
 
sub h($) {sprintf "0x%02x", shift}
 
print <<END_WARNING;
/***********************************************************************
 * This file is AUTOMATICALLY GENERATED. Do not edit
 * This file is produced by the script `insts.pl'
 ***********************************************************************/
END_WARNING
 
while (my ($mnm, $opc) = each %arith) {
    header(uc $mnm);
    
    my $imm_r = shift @$opc;
    if(defined $imm_r) {
        opcode($mnm, "imm32", "r32", ["dst"] => sub {
                   byte(h($imm_r) . "+dst");
               });
    }
    
    my $imm_rm = shift @$opc;
    if(defined $imm_rm) {
        my ($base, $ext) = @$imm_rm;
        opcode($mnm, "imm32", "rm32", [qw(mod reg)] => sub {
                   byte(h $base);
                   modrm('mod', h $ext, 'reg');
               });
    }
    
    my $r_rm = shift @$opc;
    if(defined $r_rm) {
        opcode($mnm, "r32", "rm32", [qw(src mod reg)] => sub {
                   byte(h $r_rm);
                   modrm('mod', 'src', 'reg');
               });
    }
    my $rm_r = shift @$opc;
    if(defined $rm_r) {
        opcode($mnm, "rm32", "r32", [qw(mod reg dst)] => sub {
                   byte(h $rm_r);
                   modrm('mod', 'dst', 'reg');
               });
    }
}
 
my %shifts = ( # CL imm8
    shl => [[0xD3, 4], [0xC1, 4]],
    shr => [[0xD3, 5], [0xC1, 5]],
    sar => [[0xD3, 7], [0xC1, 7]],
    );
 
while(my ($mnm, $spec) = each %shifts) {
    my $cl = shift @$spec;
    my ($opc, $reg) = @$cl;
    opcode($mnm, 'cl', 'rm32', [qw(mod reg)] => sub {
               byte(h $opc);
               modrm('mod', $reg, 'reg');
           });
    my $imm = shift @$spec;
    ($opc, $reg) = @$imm;
    opcode($mnm, 'imm8', 'rm32', [qw(mod reg)] => sub {
               byte(h $opc);
               modrm('mod', $reg, 'reg');
           });
}
 
header ('imul');
 
opcode('imul', 'rm32', 'r32', [qw(mod reg dst)] => sub {
           byte(h 0x0f);
           byte(h 0xaf);
           modrm('mod', 'dst', 'reg');
       });
 
opcode('imul', 'imm32', 'rm32', 'r32', [qw(mod reg dst)] => sub {
           byte(h 0x69);
           modrm('mod', 'dst', 'reg');
       });
 
header ('idiv');
 
opcode('idiv', 'rm32', [qw(mod reg)] => sub {
           byte(h 0xf7);
           modrm('mod', h 0x7, 'reg');
       });
 
header ('cdq');
 
opcode('cdq', [] => sub {
           byte(h 0x99);
       });
 
header ('call');
 
opcode('call', 'rel32', [], sub {
           byte(h 0xe8);
       });
opcode('call', 'rel32', [], sub {
           byte(h 0xe8);
       });
opcode('call', 'indir', 'rm32', [qw(mod reg)] => sub {
           byte(h 0xff);
           modrm('mod', h 0x2, 'reg');
       });
 
header ('jmp');
 
opcode('jmp', 'rel8', [], sub {
           byte(h 0xEB);
       });
opcode('jmp', 'rel32', [], sub {
           byte(h 0xE9);
       });
opcode('jmp', 'indir', 'rm32', [qw(mod reg)] => sub {
           byte(h 0xff);
           modrm('mod', h 0x4, 'reg');
       });
 
header ('jcc');
 
opcode('jcc', 'rel8', ['cc'] => sub {
           byte('0x70 | (cc)');
       });
opcode('jcc', 'rel32', ['cc'] => sub {
           byte(h 0x0F);
           byte('0x80 | (cc)');
       });
 
header ('cmov');
 
opcode ('cmov', 'rm32', 'r32', [qw(cc mod reg dst)] => sub {
            byte(h 0x0f);
            byte('0x40 | (cc)');
            modrm('mod', 'dst', 'reg');
        });
 
header ('setcc');
 
opcode ('setcc', 'rm8', [qw(cc mod reg)] => sub {
            byte(h 0x0f);
            byte('0x90 | (cc)');
            modrm('mod', 0, 'reg');
        });
 
# Start helper routines
 
our @LINES;
 
sub header {
    print "/* " . uc $_[0] . " */\n\n";
}
 
sub opcode {
    my $mnm = shift;
    my @asmargs = ("x86", "$mnm");
    my ($arg, $margs, $sub);
    while(!ref($arg = shift)) {
        push @asmargs, $arg;
    }
    $margs = $arg;
    $sub = shift;
  {
      local @LINES = ();
      $sub->();
      unshift @LINES, ("#define " .
                       join("_", map {uc} @asmargs) .
                       "(" . join (",", 'buf', @$margs) . ")") . " {";
      push @LINES, ' ' x 8 . '}';
      my $mlen = max map{length} @LINES;
      my $macro = join("\\\n",
                       map{$_ . (" " x ($mlen + 2 - length $_)) } @LINES);
      $macro =~ s/\s*$//;
      print "$macro\n\n";
  }
}
 
sub byte {
    my $byte = shift;
    push @LINES, " " x 8 . "X86_BYTE(buf, $byte);";
}
 
sub modrm {
    my ($mod, $regop, $rm) = @_;
    byte("MODRM($mod, $regop, $rm)");
}