diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2018-04-12 19:57:17 -0700 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2018-04-12 19:57:17 -0700 |
commit | 69d9874359752a68a61055c1117a06aae7cb4f1d (patch) | |
tree | 8a1b2c8402ea0afee63505e453b6c72ef60572bf /parser.c | |
parent | 0bdcbc08658036dbf46142c1fb41e320e6019148 (diff) | |
download | txr-69d9874359752a68a61055c1117a06aae7cb4f1d.tar.gz txr-69d9874359752a68a61055c1117a06aae7cb4f1d.tar.bz2 txr-69d9874359752a68a61055c1117a06aae7cb4f1d.zip |
compile-file: need endian mark in .tlo files.
VM machine code is endian-specific: it consists of 32 bit
instruction words which are 32 bit in the local byte order.
Thus code assembled on a little-endian machine won't run
on a big endian-machine, or vice versa: unless we identify
the situation and byte-swap the code when we load it.
* buf.c (buf_swap32): New function.
* buf.h (buf_swap32): Declared.
* parser.c (read_file_common): Decode the third element from
the version: a Boolean indicating big endian, if true.
If the object file's endian is opposite from our endian, then
byte swap the code.
* itypes.c (itypes_init): Oops, calculation of
itypes_little_endian was broken due to classic C =/== typo.
Luckily, nothing has used this flag so far; it's been waiting
for this first use. I caught this due to testing on a PPC64
box.
* share/txr/stdlib/compiler.tl (%big-endian%, %tlo-ver%): New
variables.
(usr:compile-file): The file version comes from %tlo-ver% now,
which includes the big-endian flag.
Diffstat (limited to 'parser.c')
-rw-r--r-- | parser.c | 9 |
1 files changed, 8 insertions, 1 deletions
@@ -55,6 +55,8 @@ #include "cadr.h" #include "struct.h" #include "parser.h" +#include "itypes.h" +#include "buf.h" #include "vm.h" #include "txr.h" #if HAVE_TERMIOS @@ -611,6 +613,7 @@ static val read_file_common(val stream, val error_stream, val compiled) val error_val = gensym(nil); val name = stream_get_prop(stream, name_k); val first = t; + val big_endian = nil; for (;;) { val form = lisp_parse(stream, error_stream, error_val, name, colon_k); @@ -625,11 +628,12 @@ static val read_file_common(val stream, val error_stream, val compiled) } if (compiled && first) { - val major = pop(&form); + val major = car(form); if (gt(major, zero)) uw_throwf(error_s, lit("cannot load ~s; it was compiled by a newer implementation"), stream, nao); + big_endian = caddr(form); first = nil; } else if (compiled) { for (; form; form = cdr(form)) { @@ -640,6 +644,9 @@ static val read_file_common(val stream, val error_stream, val compiled) val datavec = pop(&item); val funvec = car(item); val desc = vm_make_desc(nlevels, nregs, bytecode, datavec, funvec); + if ((big_endian && itypes_little_endian) || + (!big_endian && !itypes_little_endian)) + buf_swap32(bytecode); (void) vm_execute_toplevel(desc); gc_hint(desc); } |