| 1 | package Language::Zcode::Translator::PIR; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | =head1 Language::Zcode::Translator::PIR | ||
| 7 | |||
| 8 | Translate Z-code into PIR, aka IMCC, aka PIL, aka a language that Parrot | ||
| 9 | preprocesses to Parrot assembly before compiling to Parrot bytecode. | ||
| 10 | |||
| 11 | =cut | ||
| 12 | |||
| 13 | @Language::Zcode::Translator::PIR::ISA = qw(Language::Zcode::Translator::Generic); | ||
| 14 | my $indent = ""; # indent subs for readability | ||
| 15 | |||
| 16 | sub new { | ||
| 17 | my ($class, @arg) = @_; | ||
| 18 | bless {}, $class; | ||
| 19 | } | ||
| 20 | |||
| 21 | sub program_start { | ||
| 22 | my $addr = $Language::Zcode::Util::Constants{first_instruction_address} - 1; | ||
| 23 | my $first_sub = "_rtn$addr"; | ||
| 24 | my $g = $Language::Zcode::Util::Constants{global_variable_address}; | ||
| 25 | my $top = <<"ENDTOP"; | ||
| 26 | |||
| 27 | .include "main.pir" | ||
| 28 | |||
| 29 | .sub _main2 | ||
| 30 | # this will at some point do the stuff we repeat for restart/restore | ||
| 31 | $first_sub() | ||
| 32 | .end | ||
| 33 | |||
| 34 | #.macro z_get_global (RESULT, GLOBAL_NUM) | ||
| 35 | # .loadw_indexed(.RESULT, $g, .GLOBAL_NUM) | ||
| 36 | #.endm | ||
| 37 | # | ||
| 38 | #.macro z_set_global (VALUE, GLOBAL_NUM) | ||
| 39 | # .storew_indexed(.VALUE, $g, .GLOBAL_NUM) | ||
| 40 | #.endm | ||
| 41 | |||
| 42 | ############################################# | ||
| 43 | ENDTOP | ||
| 44 | my $cstr = ".sub _set_machine_info\n"; | ||
| 45 | $cstr .= "\t\$P0 = new .PerlInt\n\t\$P1 = new PerlString\n"; | ||
| 46 | for (sort keys %Language::Zcode::Util::Constants) { | ||
| 47 | my $val = $Language::Zcode::Util::Constants{$_}; | ||
| 48 | my $n = $val =~ /^\d+$/ ? 0 : 1; | ||
| 49 | $cstr .= "\t\$P$n = $Language::Zcode::Util::Constants{$_}\n"; | ||
| 50 | $cstr .= "\tglobal \"$_\" = \$P$n\n" | ||
| 51 | } | ||
| 52 | $cstr .= ".end\n"; | ||
| 53 | return "$top\n$cstr\n"; | ||
| 54 | } | ||
| 55 | |||
| 56 | sub routine_start { | ||
| 57 | my ($self, $addr, @params) = @_; | ||
| 58 | my $name = "_rtn$addr"; | ||
| 59 | my $out_str = ".sub $name\n"; | ||
| 60 | $indent = "\t"; | ||
| 61 | # Create local variables with the same names as Inform variables | ||
| 62 | # call_v?2 passes in at most 7 args | ||
| 63 | for (0 .. ($#params > 6 ? 6 : $#params)) { | ||
| 64 | $out_str .= "${indent}.param int local$_\n"; | ||
| 65 | } | ||
| 66 | for (7 .. $#params) { | ||
| 67 | $out_str .= "${indent}.local int local$_\n"; | ||
| 68 | } | ||
| 69 | for (qw(result)) { | ||
| 70 | $out_str .= "${indent}.local int $_\n"; | ||
| 71 | } | ||
| 72 | $out_str .= "${indent}.local pmc memory\n\tmemory = global \"_Z_Memory\"\n"; | ||
| 73 | # Declare this sub's local variables | ||
| 74 | # XXX get last arg = num_args | ||
| 75 | # while i < num_args && i < num_locals | ||
| 76 | # get argi | ||
| 77 | return $out_str | ||
| 78 | } | ||
| 79 | |||
| 80 | sub routine_end { | ||
| 81 | $indent = ""; | ||
| 82 | return ".end\n\n"; | ||
| 83 | } | ||
| 84 | |||
| 85 | |||
| 86 | ##############################################3 | ||
| 87 | # Opcode translations... | ||
| 88 | # Z_* will later be replaced with values of %parsed | ||
| 89 | # _SW implements conversion to signed word. | ||
| 90 | # Branching "?(label)" and results "-> (result)" are not put into these | ||
| 91 | # translations because they're always handled the same way. | ||
| 92 | # | ||
| 93 | # SPEC 2.2: The operations of numerical comparison, multiplication, | ||
| 94 | # addition, subtraction, division, remainder-after-division and printing of | ||
| 95 | # numbers are signed; bitwise operations are unsigned. | ||
| 96 | # | ||
| 97 | # WARNING!!! If the same Z_* is found twice in the same translation, | ||
| 98 | # and that Z_* is translated to "pop@stack", bad things could happen! | ||
| 99 | # So use temporary variables. | ||
| 100 | # XXX Maybe I should fix this somehow, e.g. s/// add's translation to: | ||
| 101 | # $Z_A = $parsed{a}; $Z_B = $parsed{b}; _SW#\$Z_A# + _SW#$Z_B#; | ||
| 102 | # Then I can use $Z_FOO in the translation without fear. | ||
| 103 | # Only problem is things like make_var, especially var_to_lval | ||
| 104 | |||
| 105 | # XXX Perl translations - ignore these! | ||
| 106 | my %Perl_trans = ( | ||
| 107 | # Arithmetic ops | ||
| 108 | div => "int(_SW#Z_A# / _SW#Z_B#)", | ||
| 109 | # Perl: # (13 % -5) == -2; Zcode: 13 % -5 = (13 - (-5 * -2)) = 3 | ||
| 110 | # How many times does $y fit into $x; always round towards zero! | ||
| 111 | # Use commas so we can later set $result = (..., ... , a%b) | ||
| 112 | mod=>'($t1 = _SW#Z_A#, $t2 = _SW#Z_B#, $t1 - $t2*int($t1/$t2))', | ||
| 113 | |||
| 114 | # logical ops - make sure we get the right number of bits | ||
| 115 | 'not' => "0xffff & ~Z_VALUE", | ||
| 116 | log_shift => '($t1 = _SW#Z_PLACES#) > 0 ' . | ||
| 117 | '? Z_NUMBER << $t1 : (Z_NUMBER & 0xffff) >> -$t1', | ||
| 118 | # The |(...) fills in 1s from the left if bit fifteen (sign bit) is set | ||
| 119 | art_shift => 'do { $t2 = Z_NUMBER; | ||
| 120 | ($t1 = _SW#Z_PLACES#) > 0 ? | ||
| 121 | Z_NUMBER << $t1 : | ||
| 122 | ($t2 >> -$t1) | ($t2>>15 && ~(2**-$t1))}', | ||
| 123 | |||
| 124 | # Jumps (conditional & unconditional) | ||
| 125 | # Branch instructions just write their conditions: they'll be added to later | ||
| 126 | # jump if all given flags in the given bitmap are set | ||
| 127 | test => '(Z_BITMAP & ($t1 = Z_FLAGS)) == $t1', | ||
| 128 | # Zspec 1.1 'je 5' is illegal. | ||
| 129 | # I need to do _SW for the case of je -1 65535 | ||
| 130 | # XXX If I move this to sub_trans, then I can use grep for > 2 args, | ||
| 131 | # and just test == for 2 args. | ||
| 132 | je => '$t1 = Z_A, grep {_SW#$t1# == _SW#$_#} (_ARG_LIST)', | ||
| 133 | |||
| 134 | # Stack and Variables | ||
| 135 | # Note: this is where Z_VARIABLE lives - indirect variables. Beware! | ||
| 136 | # Spec15.html#inc: "This is signed, so -1 increments to 0." | ||
| 137 | # Spec15.html#dec: "This is signed, so 0 decrements to -1." | ||
| 138 | inc_chk => "_SW#++Z_VARIABLE# > _SW#Z_VALUE#", | ||
| 139 | dec_chk => "_SW#--Z_VARIABLE# < _SW#Z_VALUE#", | ||
| 140 | |||
| 141 | # Return | ||
| 142 | ret_popped => "return (pop \@stack)", | ||
| 143 | |||
| 144 | # Print_* | ||
| 145 | # print is equivalent to print_addr with address of the literal string! | ||
| 146 | "print" => '# print "Z_PRINT_STRING" | ||
| 147 | &write_text(&decode_text(Z_LITERAL_STRING))', | ||
| 148 | print_ret => '# print "Z_PRINT_STRING" | ||
| 149 | &write_text(&decode_text(Z_LITERAL_STRING)); | ||
| 150 | &newline(); | ||
| 151 | return(1)', | ||
| 152 | #print_num => "&write_text(_SW#Z_VALUE#)", | ||
| 153 | print_addr => "&write_text(&decode_text(Z_BYTE_ADDRESS_OF_STRING))", | ||
| 154 | # This is why we need to store entire program in memory | ||
| 155 | print_paddr => "&write_text(&decode_text(Z_PACKED_ADDRESS_OF_STRING))", | ||
| 156 | # XXX We're doing ASCII. Need to do ZSCII | ||
| 157 | #print_char => "&write_zchar(Z_OUTPUT_CHARACTER_CODE)", | ||
| 158 | #new_line => "&newline()", | ||
| 159 | |||
| 160 | # Other I/O | ||
| 161 | "read" => "&z_read(Z_TEXT, Z_PARSE, Z_TIME, Z_ROUTINE)", | ||
| 162 | show_status => "&show_status()", | ||
| 163 | tokenise => "&z_tokenise(Z_TEXT, Z_PARSE, Z_DICTIONARY, Z_FLAG)", | ||
| 164 | |||
| 165 | # Streams & windows & cursors | ||
| 166 | output_stream => "&output_stream(_SW#Z_NUMBER#)", # arg may be < 0 | ||
| 167 | input_stream => "&input_stream(Z_NUMBER)", | ||
| 168 | split_window => "&split_window(Z_LINES)", | ||
| 169 | set_window => "&set_window(Z_WINDOW)", | ||
| 170 | erase_window => "&erase_window(_SW#Z_WINDOW#)", # arg may be < 0 | ||
| 171 | get_cursor => "&get_cursor(Z_ARRAY)", | ||
| 172 | set_cursor => "&set_cursor(_SW#Z_LINE#, Z_COLUMN, Z_WINDOW)", # < 0 for v6 | ||
| 173 | set_text_style => "&set_text_style(Z_STYLE)", | ||
| 174 | |||
| 175 | # Objects | ||
| 176 | get_parent => "get_parent(Z_OBJECT)", | ||
| 177 | get_child => "get_child(Z_OBJECT)", | ||
| 178 | get_sibling => "get_sibling(Z_OBJECT)", | ||
| 179 | jin => "Z_OBJ2 == &get_object(&thing_location(Z_OBJ1, 'parent'))", | ||
| 180 | print_obj => "&write_text(&decode_text(&thing_location(Z_OBJECT, 'name')))", | ||
| 181 | insert_obj => "&insert_obj(Z_OBJECT, Z_DESTINATION)", | ||
| 182 | remove_obj => "&remove_obj(Z_OBJECT)", | ||
| 183 | |||
| 184 | # Properties | ||
| 185 | get_prop => "&get_prop(Z_OBJECT, Z_PROPERTY)", | ||
| 186 | put_prop => "&put_prop(Z_OBJECT, Z_PROPERTY, Z_VALUE)", | ||
| 187 | get_next_prop => "&get_next_prop(Z_OBJECT, Z_PROPERTY)", | ||
| 188 | get_prop_addr => "&get_prop_addr(Z_OBJECT, Z_PROPERTY)", | ||
| 189 | get_prop_len => "&get_prop_len(Z_PROPERTY_ADDRESS)", | ||
| 190 | |||
| 191 | # Attributes | ||
| 192 | set_attr => "&set_attr(Z_OBJECT, Z_ATTRIBUTE)", | ||
| 193 | clear_attr => "&clear_attr(Z_OBJECT, Z_ATTRIBUTE)", | ||
| 194 | test_attr => "&test_attr(Z_OBJECT, Z_ATTRIBUTE)", | ||
| 195 | |||
| 196 | # Save/restore | ||
| 197 | # XXX Different for v1-3 | ||
| 198 | save => '&save_state(Z_RESTORE_PC, \@locv, \@stack)', | ||
| 199 | restore => "&restore_state", | ||
| 200 | # Spec "save_undo": terp must return -1 if it doesn't implement save_undo | ||
| 201 | save_undo => "-1", | ||
| 202 | # Spec "restore_undo": illegal for a game to use this if save_undo | ||
| 203 | # returns -1. | ||
| 204 | restore_undo => "0", | ||
| 205 | restart => 'die "Restart\n"', | ||
| 206 | |||
| 207 | # Misc | ||
| 208 | check_arg_count => '@_ >= Z_ARGUMENT_NUMBER', | ||
| 209 | #quit => 'die "Quit\n"', | ||
| 210 | # Spec 15, 'piracy': "Interpreters are asked to be gullible" | ||
| 211 | piracy => "1", | ||
| 212 | random => "&z_random(_SW#Z_RANGE#)", | ||
| 213 | verify => "&z_verify()", | ||
| 214 | |||
| 215 | # Calls: Z subs are turned into Perl subs | ||
| 216 | # Use Perl's calling stack instead of building a separate one BUT do some | ||
| 217 | # bookkeeping (w/ extra args) to be able to save/restore machine state | ||
| 218 | #call_1s => 'z_call(Z_ROUTINE, \@locv, \@stack, Z_NEXT_PC, Z_RESULT_NUM, _ARG_LIST)', | ||
| 219 | |||
| 220 | ); | ||
| 221 | |||
| 222 | my %replace_trans = ( | ||
| 223 | # Arithmetic ops | ||
| 224 | add => "_SW#Z_A# + _SW#Z_B#", | ||
| 225 | 'sub' => "_SW#Z_A# - _SW#Z_B#", | ||
| 226 | mul => "_SW#Z_A# * _SW#Z_B#", | ||
| 227 | div => "_SW#Z_A# / _SW#Z_B#", | ||
| 228 | # Perl: # (13 % -5) == -2; Zcode: 13 % -5 = (13 - (-5 * -2)) = 3 | ||
| 229 | # How many times does $y fit into $x; always round towards zero! | ||
| 230 | # Use commas so we can later set $result = (..., ... , a%b) | ||
| 231 | # mod=>'($t1 = _SW#Z_A#, $t2 = _SW#Z_B#, $t1 - $t2*int($t1/$t2))', | ||
| 232 | |||
| 233 | # logical ops - make sure we get the right number of bits | ||
| 234 | 'or' => "Z_A | Z_B", | ||
| 235 | 'and'=> "Z_A & Z_B", | ||
| 236 | |||
| 237 | # Jumps (conditional & unconditional) | ||
| 238 | jump => "goto LZ_LABEL", | ||
| 239 | # Branch instructions just write their conditions: gotos will be added later | ||
| 240 | jz => "Z_A == 0", | ||
| 241 | jg => "_SW#Z_A# > _SW#Z_B#", | ||
| 242 | jl => "_SW#Z_A# < _SW#Z_B#", | ||
| 243 | |||
| 244 | # Stack and Variables | ||
| 245 | # Note: this is where Z_VARIABLE lives - indirect variables. Beware! | ||
| 246 | # TODO not really tested! | ||
| 247 | # TODO die on stack underflow | ||
| 248 | 'pop' => 'restore $I0', # (and do nothing with $I0) | ||
| 249 | 'push' => 'save Z_VALUE', | ||
| 250 | pull => 'restore Z_VARIABLE', | ||
| 251 | store => "Z_VARIABLE = Z_VALUE", | ||
| 252 | load => "Z_VARIABLE", | ||
| 253 | inc => "inc Z_VARIABLE", | ||
| 254 | dec => "dec Z_VARIABLE", | ||
| 255 | |||
| 256 | # Memory access | ||
| 257 | loadb => ".loadb_indexed(Z_RESULT, Z_ARRAY, Z_BYTE_INDEX)", | ||
| 258 | loadw => ".loadw_indexed(Z_RESULT, Z_ARRAY, Z_WORD_INDEX)", | ||
| 259 | storeb => ".storeb_indexed(Z_VALUE, Z_ARRAY, Z_BYTE_INDEX)", | ||
| 260 | storew => ".storew_indexed(Z_VALUE, Z_ARRAY, Z_WORD_INDEX)", | ||
| 261 | |||
| 262 | # Calls | ||
| 263 | # TODO calculated calls | ||
| 264 | call_1s => "_rtnZ_ROUTINE(_ARG_LIST)", | ||
| 265 | |||
| 266 | # Return | ||
| 267 | ret => [".pcc_begin_return", ".return Z_VALUE", ".pcc_end_return"], | ||
| 268 | rtrue => [".pcc_begin_return", ".return 0", ".pcc_end_return"], | ||
| 269 | rfalse => [".pcc_begin_return", ".return 1", ".pcc_end_return"], | ||
| 270 | # ret_popped => "return (pop \@stack)", | ||
| 271 | |||
| 272 | # Prints | ||
| 273 | # print_num => "&write_text(_SW#Z_VALUE#)", | ||
| 274 | print_num => "print _SW#Z_VALUE#", | ||
| 275 | # print_char => "&write_zchar(Z_OUTPUT_CHARACTER_CODE)", | ||
| 276 | print_char => ['$S0 = chr Z_OUTPUT_CHARACTER_CODE', "print \$S0"], | ||
| 277 | # new_line => "&newline()", | ||
| 278 | new_line => 'print "\n"', | ||
| 279 | # TODO KLUDGE! | ||
| 280 | "print" => 'print "Z_PRINT_STRING"', | ||
| 281 | |||
| 282 | # Misc | ||
| 283 | # TODO should really call IO cleanup, etc. | ||
| 284 | quit => "end", | ||
| 285 | ); | ||
| 286 | |||
| 287 | # All call subs work the same! (store var will be set to undef for call_*n, | ||
| 288 | # and "result = " will be added to call_s). | ||
| 289 | @replace_trans{qw(call_2s call_vs call_vs2 call_1n call_2n call_vn call_vn2)} = | ||
| 290 | ($replace_trans{call_1s}) x 7; | ||
| 291 | |||
| 292 | # Translate Z opcode and ops into Perl | ||
| 293 | my %unimplemented; # keep track of unimplemented opcodes | ||
| 294 | my $lcount = 0; | ||
| 295 | sub translate_command { | ||
| 296 | # Keys to %parsed are based on the arguments in the opcode syntax list | ||
| 297 | # in LZ::Parser::Opcode. There's a few others I put in: | ||
| 298 | # - opcode, opcode address are the name & address of the opcode | ||
| 299 | # - result is variable name (or stack top) where we're supposed | ||
| 300 | # to store the result, if any | ||
| 301 | # - negate_jump means negate the condition of jump opcodes | ||
| 302 | # - jump_return means return this value (0 or 1) instead of branching | ||
| 303 | # if the branch condition is met | ||
| 304 | # - op is an arrayref to remaining arguments (used for e.g., call_*) | ||
| 305 | my ($self, $href) = @_; | ||
| 306 | my %parsed = %$href; | ||
| 307 | my $opcode = $parsed{opcode} or return; # totally unknown opcode? | ||
| 308 | my $command = "OOPS. No Command Here\n"; # command to return | ||
| 309 | my $vcount = 100; | ||
| 310 | |||
| 311 | my %sub_trans = ( | ||
| 312 | # There's nothing to see here. Move along... | ||
| 313 | ); | ||
| 314 | |||
| 315 | # XXX Only print labels we actually jump to? Requires separate pass. | ||
| 316 | my $label = "L$parsed{opcode_address}:\t"; | ||
| 317 | # Quetzal stores the byte BEFORE the next command as its restore_pc, | ||
| 318 | # so we'll eventually call a sub and try to goto that address. | ||
| 319 | # $label .= "1; L$parsed{restore_pc}: " if exists $parsed{restore_pc}; | ||
| 320 | |||
| 321 | # Translate, e.g., "local1" to language-specific | ||
| 322 | # code representing second local variable | ||
| 323 | # Treat key "variable" specially - it's used in "indirect opcodes" | ||
| 324 | # (See make_var) | ||
| 325 | foreach my $key (keys %parsed) { | ||
| 326 | my %skip = map {$_=>1} | ||
| 327 | qw(args jump_return label literal_string negate_jump | ||
| 328 | next_pc opcode opcode_address print_string restore_pc); | ||
| 329 | if (!exists $skip{$key}) { | ||
| 330 | #warn "$key $parsed{$key}\n"; | ||
| 331 | warn "undefined $key\n" if !defined $parsed{$key}; | ||
| 332 | $parsed{$key} = $self->make_var($parsed{$key}, $key eq "variable"); | ||
| 333 | } | ||
| 334 | } | ||
| 335 | # Pack addresses | ||
| 336 | foreach my $key (qw(packed_address_of_string routine)) { | ||
| 337 | if (exists $parsed{$key}) { | ||
| 338 | $parsed{$key} = $self->packed_address_str($parsed{$key}, $key); | ||
| 339 | } | ||
| 340 | } | ||
| 341 | |||
| 342 | # Turn variable number of args (if any) into a Perl list | ||
| 343 | # Btw, call_1n takes no args, so arg_list will be "" for call_1n, too | ||
| 344 | my $arg_list = exists $parsed{args} | ||
| 345 | ? join(", ", map {$self->make_var($_)} @{$parsed{"args"}}) | ||
| 346 | : ""; | ||
| 347 | |||
| 348 | # Turn Z ops into Perl ops | ||
| 349 | my ($pre_command, $post_command) = ("", ""); | ||
| 350 | if (exists $replace_trans{$opcode}) { | ||
| 351 | $_ = $replace_trans{$opcode}; | ||
| 352 | # opcodes like @add need "result = ", but I've already added | ||
| 353 | # the lvalue for more complicated commands | ||
| 354 | s/^/Z_RESULT = / if exists $parsed{result} && !/Z_RESULT/; | ||
| 355 | # TODO spacing is screwed up for first line, .commands, etc. | ||
| 356 | if (ref $_ eq "ARRAY") { | ||
| 357 | #$_ = join "\n", map {/^\./ ? "$_" : "\t$_"} @$_; | ||
| 358 | $_ = join "\n\t", @$_; | ||
| 359 | } | ||
| 360 | |||
| 361 | # Replace Z_* with variables/constants FIRST | ||
| 362 | |||
| 363 | # TODO undef is bad! | ||
| 364 | s/Z_(\w+)/exists $parsed{lc $1} ? $parsed{lc $1} : "undef"/ge; | ||
| 365 | # Put in actual arguments | ||
| 366 | # (Note that sometimes there's a letter before the Z, | ||
| 367 | # but never after the whole key.) | ||
| 368 | # If there are optional args, then some of the args won't exist. | ||
| 369 | # First handle things where we set an lval to an rval | ||
| 370 | #s/Z_(\w+)\s+=\s+(.+)/$self->var_to_lval($parsed{lc $1}, $2)/e; | ||
| 371 | |||
| 372 | # g17 = local1 + local2 -> | ||
| 373 | # $Ix = local1 + local2 | ||
| 374 | # .set_global($Ix, 17) | ||
| 375 | # TODO what if it's an lval in a macro? Use "LVAL_" prefix? | ||
| 376 | # E.g, .loadb_indexed(g00, 0, 0); | ||
| 377 | while (/\b(g([\da-f]{2})|sp)\b(\s+=\s+)?/) { | ||
| 378 | my $name = $1; | ||
| 379 | my $temp_var = "\$I$vcount"; $vcount++; | ||
| 380 | my $set; | ||
| 381 | # TODO use ".const global_variable_address" & use that here? | ||
| 382 | my $g = $Language::Zcode::Util::Constants{global_variable_address}; | ||
| 383 | if ($3) { # lval | ||
| 384 | if ($2) { # global = $I17 | ||
| 385 | $set = ".storew_indexed($temp_var, $g, " . hex($2) . ")"; | ||
| 386 | } else { # sp = $I17 | ||
| 387 | $set = "save $temp_var"; | ||
| 388 | } | ||
| 389 | $post_command .= "\n\t$set"; | ||
| 390 | s/$name/$temp_var/; # there can be only one lval; no s///g nec. | ||
| 391 | } elsif ($2) { # rval: $I17 = global | ||
| 392 | # replace all g17's in call_vs 12a5 g17 g17 g17 | ||
| 393 | # with the same variable | ||
| 394 | $set = ".loadw_indexed($temp_var, $g, " . hex($2) . ")"; | ||
| 395 | $pre_command .= "$set\n\t"; | ||
| 396 | s/$name/$temp_var/g; | ||
| 397 | } else { # rval: $I17 = sp | ||
| 398 | # Need to replace each sp with a new variable | ||
| 399 | $set = "restore $temp_var"; | ||
| 400 | $pre_command .= "$set\n\t"; | ||
| 401 | s/$name/$temp_var/; | ||
| 402 | } | ||
| 403 | } | ||
| 404 | # Put in actual arguments | ||
| 405 | # (Note that sometimes there's a letter before the Z, | ||
| 406 | # but never after the whole key.) | ||
| 407 | # If there are optional args, then some of the args won't exist. | ||
| 408 | # First handle things where we set an lval to an rval | ||
| 409 | # s/Z_(\w+)\s+=\s+(.+)/$self->var_to_lval($parsed{lc $1}, $2)/e; | ||
| 410 | # s/([+-]{2})Z_(\w+)/$self->var_to_lval($parsed{lc $2}, $1)/e; | ||
| 411 | # May need to replace in "g00 + g01", so we replace differently | ||
| 412 | # OR may need to replace g00 more than one time in the expression | ||
| 413 | # for some reason | ||
| 414 | # while (/.z_get_global\((\$I\d+).*?\)/) { | ||
| 415 | # print "hi '$_' $& $1\n"; | ||
| 416 | # s//$1/g && print "did"; | ||
| 417 | # print "bye $_\n"; | ||
| 418 | # $pre_command .= "$&\n\t"; | ||
| 419 | # } | ||
| 420 | s/_ARG_LIST/$arg_list/; | ||
| 421 | s/, (undef(, )?)?\)(;|$)/)$3/; # clean up unneeded args | ||
| 422 | |||
| 423 | # Change numbers to signed words. | ||
| 424 | s/_SW#(.*?)#/my $res = $1; | ||
| 425 | if ($res =~ m#^-?\d+$#) { | ||
| 426 | $res -= 0x10000 if $res > 0x8000; | ||
| 427 | } else { | ||
| 428 | $pre_command .= ".signed_word($res)\n\t" | ||
| 429 | } | ||
| 430 | $res/ge; | ||
| 431 | |||
| 432 | $command = $_; | ||
| 433 | # print "$parsed{opcode_address} $command\n"; | ||
| 434 | |||
| 435 | } elsif (exists $sub_trans{$opcode}) { | ||
| 436 | $command = &{$sub_trans{$opcode}}(); | ||
| 437 | |||
| 438 | # Handle commands that have a "-> (result)" argument | ||
| 439 | # (result has already been translated from e.g. 3 to 'local2') | ||
| 440 | $command = $self->var_to_lval($parsed{result}, $command) | ||
| 441 | if exists $parsed{result} && $command !~ /result =/; | ||
| 442 | |||
| 443 | } else { | ||
| 444 | warn "Unimplemented opcode $opcode at $parsed{opcode_address}\n" | ||
| 445 | unless $unimplemented{$opcode}++; | ||
| 446 | $command = "&unimplemented_$opcode"; | ||
| 447 | } | ||
| 448 | |||
| 449 | # Handle branch instructions | ||
| 450 | # Do this AFTER store_result, so we get "goto L3 if $c = $a+$b" | ||
| 451 | # rather than "$c = goto L3 if $c=$a+$b" | ||
| 452 | # (Note: jump doesn't count as a branch instruction!) | ||
| 453 | # (This assumes Perl command is pretty simple) | ||
| 454 | if (exists $parsed{negate_jump}) { | ||
| 455 | my $action; | ||
| 456 | if (exists $parsed{jump_return}) { | ||
| 457 | $action = ".return $parsed{jump_return}"; | ||
| 458 | } else { | ||
| 459 | die "no label for command!" unless exists $parsed{label}; | ||
| 460 | $action = "goto L$parsed{label}"; | ||
| 461 | } | ||
| 462 | my $cond .= $parsed{negate_jump} ? "unless" : "if"; | ||
| 463 | if (exists $parsed{result}) { # needs to be two lines | ||
| 464 | $command .= "\n${indent}$cond $parsed{result} $action"; | ||
| 465 | } else { | ||
| 466 | $command = "$cond $command $action"; | ||
| 467 | } | ||
| 468 | } | ||
| 469 | |||
| 470 | $command = join("", | ||
| 471 | $indent, $label, $pre_command, $command, $post_command, "\n"); | ||
| 472 | |||
| 473 | # print "$command"; | ||
| 474 | return ($command); | ||
| 475 | } | ||
| 476 | |||
| 477 | # TODO this is copied from Perl. Needs to do PIR | ||
| 478 | my $vcount = 100; | ||
| 479 | sub make_var { | ||
| 480 | my ($self, $var, $is_indirect) = @_; | ||
| 481 | |||
| 482 | return $var; # for now, don't modify | ||
| 483 | |||
| 484 | my $is_bracket = ($var =~ s/\[(.*)\]/$1/); | ||
| 485 | # TODO Are is_bracket and is_indirect slightly overlapping? | ||
| 486 | # Isn't it impossible to be is_bracket w/out being is_indirect? | ||
| 487 | |||
| 488 | local $_ = $var; | ||
| 489 | if (/^g([\da-f]{2})$/) { # Global variable | ||
| 490 | my $var_num = hex($1); | ||
| 491 | $_ = ".z_get_global(\$I$vcount, $var_num)"; | ||
| 492 | $vcount++; | ||
| 493 | } elsif (/^local([\da-f]+)$/) { # Local variable | ||
| 494 | #my $h = hex $1; | ||
| 495 | #$_ = "local$h"; | ||
| 496 | # NOOP: local variables are named just like Inform locals | ||
| 497 | } elsif ($_ eq "sp") { # Stack | ||
| 498 | # Spec Version 1.1 (draft7): "an indirect reference to the stack | ||
| 499 | # pointer does not push or pull the top item of the stack - it is read | ||
| 500 | # or written in place." | ||
| 501 | # ADK: From testing (winfrotz2002) it apears this is true for | ||
| 502 | # "load sp". "load [sp]" does pop the stack in getting the number | ||
| 503 | # of the variable to use. But if sp == 0, "load [sp]" still pops | ||
| 504 | # only once. | ||
| 505 | $_ = $is_indirect && !$is_bracket ? '$stack[-1]' : 'pop(@stack)'; | ||
| 506 | } elsif (/^\d+$/) { | ||
| 507 | # Leave the numeric constant as it is | ||
| 508 | } else { # not a number? What is it? | ||
| 509 | warn "Unexpected arg to make_var: '$var'"; | ||
| 510 | # keep the garbage in the output file | ||
| 511 | } | ||
| 512 | |||
| 513 | # Get the value stored in the variable referenced by the current $_ | ||
| 514 | # Pass in local variables & stack so we have their values. | ||
| 515 | # No, pass in *refs* to local variables & stack, in case the indirect | ||
| 516 | # var is an lval which references e.g. a local variable which we | ||
| 517 | # then need to set within indirect_var! | ||
| 518 | $_ = "bracket_var($_, \\\@locv, \\\@stack)" | ||
| 519 | if $is_bracket; | ||
| 520 | |||
| 521 | return $_; | ||
| 522 | } | ||
| 523 | sub var_to_lval { | ||
| 524 | my ($self, $lval, $rval) = @_; | ||
| 525 | local $_ = $lval; # for convenience in //'s. | ||
| 526 | # XXX what's correct protocol for store [sp] sp? | ||
| 527 | # Pop stack before reading indirect variable? | ||
| 528 | my $is_bracket = /bracket_var\(/; | ||
| 529 | |||
| 530 | # $rval = "($rval) % 0x10000"; | ||
| 531 | |||
| 532 | if ($is_bracket) { # add $rval to args to bracket_var | ||
| 533 | die "Can't handle indirect vars yet\n"; | ||
| 534 | $rval =~ s/^[+-]{2}$/"$&"/; # Yuck! | ||
| 535 | s/\)$/, $rval)/; | ||
| 536 | } elsif (/.z_get_global\((\$I\d+)/) { # ... | ||
| 537 | $rval =~ s/^[+-]{2}$/"$&"/; # Yuck! | ||
| 538 | my $temp = $1; | ||
| 539 | s/^/$temp = $rval\n\t/; | ||
| 540 | s/z_get/z_set/; # TODO WRONG!!! | ||
| 541 | #s/\)$/, result)/; | ||
| 542 | #$_ = "result = $rval\n${indent}$_"; | ||
| 543 | } elsif (/local[\da-f]/) { # $I0 -> $I0 = rval | ||
| 544 | if ($rval eq "++" || $rval eq "--") { | ||
| 545 | die "Can't handle ++ or -- yet\n"; | ||
| 546 | my $op = substr($rval, 0, 1); | ||
| 547 | # XXX this wrong. Spec 15#dec/inc say this should be signed! | ||
| 548 | $_ = "($_ = ($_ $op 1) & 0xffff)"; | ||
| 549 | } else { | ||
| 550 | # $_ = "result = $rval\n\t$lval = result"; | ||
| 551 | # local0 = ... should just work | ||
| 552 | s/$/ = $rval/; | ||
| 553 | } | ||
| 554 | } elsif (/stack/) { # pop @stack -> $stack[@stack] = rval | ||
| 555 | die "Can't handle stack yet!\n"; | ||
| 556 | # push returns number of elements in array. I need the value I pushed. | ||
| 557 | # If indirect variable, this s/// won't happen. | ||
| 558 | s/pop\(\@stack\)/\$stack[\@stack]/; | ||
| 559 | if ($rval eq "++" || $rval eq "--") { | ||
| 560 | my $op = substr($rval, 0, 1); | ||
| 561 | $_ = "($_ = ($_ $op 1) & 0xffff)"; | ||
| 562 | } else { | ||
| 563 | $_ .= " = $rval"; | ||
| 564 | } | ||
| 565 | } else { | ||
| 566 | warn "Unexpected arg to var_to_lval $lval"; | ||
| 567 | } | ||
| 568 | |||
| 569 | return $_; | ||
| 570 | } | ||
| 571 | |||
| 572 | # TODO put something in here! | ||
| 573 | sub signed_word { | ||
| 574 | my ($self, $num) = @_; | ||
| 575 | return $num; | ||
| 576 | } | ||
| 577 | |||
| 578 | sub write_memory { | ||
| 579 | my $l = @Language::Zcode::Util::Memory; # i.e., file/memory length | ||
| 580 | my $str = <<"ENDUNPACK"; | ||
| 581 | .sub _read_memory | ||
| 582 | \$P0 = new .Array | ||
| 583 | \$P0 = $l | ||
| 584 | global \"_Z_Memory\" = \$P0 | ||
| 585 | # Address 0 1 2 3 4 5 6 7 8 9 a b c d e f | ||
| 586 | ENDUNPACK | ||
| 587 | # change each byte to two hex digits | ||
| 588 | for (my $c = 0; $c < $l; $c+=16) { | ||
| 589 | # Add hex "line number" & \n's. | ||
| 590 | my $len = $l - $c; | ||
| 591 | $len = 16 if $len > 16; | ||
| 592 | $str .= sprintf( | ||
| 593 | "\t_mem_add(0x%06x, \"" . " %02x" x $len . "\")\n", | ||
| 594 | $c, @Language::Zcode::Util::Memory[$c .. $c + $len -1]); | ||
| 595 | } | ||
| 596 | $str .= ".end\n"; | ||
| 597 | return $str; | ||
| 598 | |||
| 599 | } | ||
| 600 | |||
| 601 | sub library { | ||
| 602 | return ""; | ||
| 603 | } | ||
| 604 | 1; |