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 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
|
#!/usr/bin/env perl6
use lib $?FILE.IO.parent.child("lib");
use MAST::Ops;
role CLanguageBase {
regex ws {
:r
[ | \s+
| '//' \N* \n
| [ '/*' [<-[*]>+ || '*'<!before '/'>]* '*/' ]
]*
[ <!before \s> | $ ]
}
regex curly_block {
'{'
[
| \s+
| <curly_block>
| <-[ { ]>+
]*
'}'
}
}
# Easiest thing first: op_to_func
sub parse_op_to_func($source) {
grammar OpToFuncGrammar does CLanguageBase {
rule TOP {
op_to_func '(' MVMThreadContext '*' tc ',' MVMint16 <opcodevar=.ident> ')' '{' # introduction
'switch(' $<opcodevar>=[<[a..z A..Z 0..9 _]>+] ')' '{'
<entry>+
default ':'
[\N*\n]*?
'}'
.*
}
rule entry {
[
case MVM_OP_$<opname>=[<[a..z A..Z 0..9 _]>+ ] ':'
]+
return '&'? <funcname=.ident> ';'
{ note "parsed an entry for $<funcname>" }
}
}
my $cut_off_source = $source.substr($source.index("op_to_func\(MVMThreadContext"));
my $op_func_table = OpToFuncGrammar.parse($cut_off_source);
note "parsed";
my %result;
for $op_func_table<entry>.list -> $/ {
%result{$<opname>>>.Str} = $<funcname>.Str xx *;
}
return %result;
}
sub parse_consume_ins_reprops($source, %opcode_to_cfunc) {
# first, we'll cut the relevant sections of the source out:
# the part of jgb_consume_reprop after the type-specialized parts
# and then all of jgb_consume_ins
my @sourcelines = $source.lines;
@sourcelines .= grep({ / "couldn't be devirtualized" | " jgb_consume_ins" / ^ff^ / "default:" / });
@sourcelines .= grep({ $_ !~~ / ^ \s* '/*' .*? '*/' \s* $ / });
@sourcelines .= grep({ $_ !~~ / ^ \s* $ / });
# chunkify based on case: and break;
# we are a very simple parser so if we find a break that's not followed
# by a new case (or a "}") we just skip ahead until we see the next case.
my @chunks = gather loop {
# find the first non-case line.
my $until = @sourcelines.first({ $_ !~~ / "case MVM_".*?':' / }, :k);
my @case-lines = @sourcelines[^$until];
@sourcelines.shift for ^$until;
# we'll put all case statements into a single string for easier combing
my $casestring = [~] @case-lines;
my @ops = $casestring.comb(/ "case " \s* 'MVM_OP_'<( .*? )> \s* ':' /);
# find the next case-line.
$until = @sourcelines.first( / "case MVM_".*?':' /, :k );
$until = +@sourcelines unless $until; # may have to slurp until EOF.
my @implementationlines = @sourcelines[^$until];
@sourcelines.shift for ^$until;
take @ops => @implementationlines;
last unless @sourcelines;
}
# collect everything we've bailed on
my @skipped_opcodes;
# also collect everything we've had success with
my @success_opcodes;
chunkloop: for @chunks.kv -> $chunkidx, $_ {
my @ops = .key.list;
my @lines = .value.list;
# what C variable refers to what piece of the op in the code
my %var_sources;
# do we have something to read out of a register or a
# constant or something like that?
my %reg_types;
# what arguments do we push to the C stack for this?
my @c_arguments;
# keep lines in case we abort somewhere.
my @lines_so_far;
# put this outside of the while loop for the report error sub
my $line;
sub report_unhandled($reason?) {
note "";
note "=============";
note "handling @ops.join(', ')";
if $reason {
note "";
note $reason;
note "";
}
.note for @lines_so_far;
note $line;
note "";
@skipped_opcodes.push: @ops.join(", ");
next chunkloop;
}
# we expect the chunk to begin with some setup:
# initialise local variables with
# register numbers
# literal numbers, a string index, ...
while @lines {
last if @lines[0] !~~ / ^ \s+ [MVMint|MVMuint] /;
while ($line = @lines.shift) ~~ m:s/^ [MVMint|MVMuint][16|32|64] <varname=.ident> '='
'ins->operands[' $<operandnum>=[\d+] ']'
[
| $<register>=".reg.orig"
| $<lit_str_idx>=".lit_str_idx"
| $<literal>=[".lit_i16"|".lit_i64"]
]
/ {
@lines_so_far.push: "var_source: $line";
%var_sources{$<varname>.Str} = $<operandnum>.Int;
%reg_types{$<operandnum>.Int} = (
$<register> ?? 'register' !!
$<lit_str_idx> ?? 'str_idx' !!
$<literal> ?? 'literal' !!
die "kind of operand source not defined: $/.perl()");
}
unless $line ~~ m:s/ MVMJitCallArg / {
report_unhandled "this line surprised us (expected MVMJitCallArg):";
}
# since we consume the line in the condition for the coming
# loop, but we want to handle this current line there as well,
# we just unshift it into the lines array again ...
@lines.unshift($line);
while ($line = @lines.shift) ~~ m:s/
^
[MVMJitCallArg args"[]" "=" '{']?
[
| '{' <argkind=.ident> ',' [ '{' <argvalue=.ident> '}' | <argvalue=.ident> ]
| '{' $<argkind>="MVM_JIT_LITERAL" ',' [
| '{' $<argvalue>=[\d+] '}'
| '{' op '==' MVM_OP_<direct_comparison=.ident> '}'
]
]
[ '}' '}' ';' | '}' ',' ]
$ / {
#say $/;
given $<argkind>.Str {
when "MVM_JIT_INTERP_VAR" {
given $<argvalue> {
when "MVM_JIT_INTERP_TC" {
@c_arguments.push:
"(carg (tc) ptr)";
}
when "MVM_JIT_INTERP_CU" {
@c_arguments.push:
"(carg (cu) ptr)";
}
when "MVM_JIT_INTERP_FRAME" {
@c_arguments.push:
"(carg (frame) ptr)";
}
when "MVM_JIT_INTERP_PARAMS" {
@c_arguments.push:
"(carg (^params) ptr)";
}
when "MVM_JIT_INTERP_CALLER" {
@c_arguments.push:
"(carg (^caller) ptr)";
}
default {
report_unhandled "this kind of interp var ($_) isn't handled yet";
}
}
}
when "MVM_JIT_REG_VAL" {
# later on: figure out if it's a str/obj or an
# int register that the op(s) take here.
@c_arguments.push:
'(carg $' ~ %var_sources{$<argvalue>.Str} ~ " int)";
}
when "MVM_JIT_REG_VAL_F" {
@c_arguments.push:
'(carg $' ~ %var_sources{$<argvalue>.Str} ~ " num)";
}
when "MVM_JIT_REG_ADDR" {
my %result;
my $operand_idx = %var_sources{$<argvalue>.Str};
for @ops -> $op {
my $op_number = %codes{$op};
my $op_values_offset = @offsets[$op_number];
my $operand_flags = @values[$op_values_offset] + $operand_idx;
my $operand_rw_flags = $operand_flags +& %flags<MVM_operand_rw_mask>;
if $operand_rw_flags == %flags<MVM_operand_write_reg> {
%result{$op} = '(carg $' ~ $operand_idx ~ ' ptr)';
} else {
report_unhandled "there's a MVM_JIT_REG_ADDR here, but the operand isn't a MVM_operand_write_reg (it's $operand_rw_flags instead).";
}
}
if [eq] %result.values {
@c_arguments.push: %result.values[0];
} else {
@c_arguments.push: %result;
}
}
when "MVM_JIT_LITERAL" {
if defined try $<argvalue>.Int {
@c_arguments.push:
'(carg (const ' ~ $<argvalue>.Int ~ ' int_sz) int)';
} elsif $<direct_comparison> {
my %result;
for @ops -> $op {
%result{$op} = +($op eq $<direct_comparison>);
}
@c_arguments.push: %result;
} elsif $<argvalue>.Str ~~ %var_sources {
my $source_register = %var_sources{$<argvalue>.Str};
if %reg_types{$source_register} eq 'literal' {
@c_arguments.push:
'(carg (copy $' ~ $source_register ~ ') int)';
} else {
report_unhandled "expected $<argvalue>.Str() (from $source_register) to be declared as literal";
}
} else {
report_unhandled "didn't understand this kind of MVM_JIT_LITERAL.";
}
}
default {
report_unhandled "this line surprised us (expected jg_append_call_c):";
}
}
@lines_so_far.push: "c_args: $line";
}
$line = $line ~ @lines.shift unless $line ~~ m/ ';' $ /;
unless $line ~~ m:s/ jg_append_call_c '('
tc ',' jgb '->' graph ',' op_to_func '(' tc ',' op ')' ',' \d+ ',' args ','
$<return_type>=[ MVM_JIT_RV_VOID | MVM_JIT_RV_INT | MVM_JIT_RV_PTR | MVM_JIT_RV_NUM ] ','
$<return_dst>=[ '-1' | <.ident> ] ')' ';'
/ {
report_unhandled "this line surprised us (expected jg_append_call_c):";
}
my %rv_to_returnkind = (
MVM_JIT_RV_VOID => 'void',
MVM_JIT_RV_INT => 'int',
MVM_JIT_RV_PTR => 'ptr',
MVM_JIT_RV_NUM => 'num',
);
for @ops -> $opname {
note %opcode_to_cfunc{$opname} ~ " going to have a template built for it";
say "(template: $opname";
say " (call (^func {%opcode_to_cfunc{$opname}})";
say " (arglist {+@c_arguments}";
for @c_arguments -> $carg {
if $carg ~~ Associative {
say " $carg{$opname}";
} else {
say " $carg";
}
}
say " )";
say " " ~ %rv_to_returnkind{$<return_type>};
say " ) )";
say "";
@success_opcodes.push: $opname;
}
}
}
note "all successfully parsed opcodes:";
note " + $_" for @success_opcodes;
note "";
note "all skipped operations:";
note " - $_" for @skipped_opcodes;
}
sub MAIN($graph_c_file? is copy) {
$graph_c_file //= $?FILE.IO.parent.parent.child("src").child("jit").child("graph.c");
my $graph_c_source = slurp($graph_c_file);
note "got the source";
my %opcode_to_cfunc = parse_op_to_func($graph_c_source);
parse_consume_ins_reprops($graph_c_source, %opcode_to_cfunc);
}
|