for review: Aditi bytecode bug fixes
Simon Taylor
stayl at cs.mu.OZ.AU
Mon Mar 8 10:46:34 AEDT 1999
Estimated hours taken: 3
Fix bugs in the Aditi bytecode output routines.
compiler/bytecode.m:
compiler/bytecode_data.m:
Move the data conversion predicates from bytecode.m to a new module
so they can be used by for output of Aditi-RL bytecode in the
compiler and in the RL bytecode assembler in the Aditi distribution.
Add new predicates to convert integers and floats to lists of
bytes rather than writing them out immediately.
compiler/rl_code.m:
Use the same data conversion predicates for Aditi-RL
bytecodes as for Mercury bytecodes.
This fixes a bug with the output of integers on 32 bit machines
(integers were being shifted by more than the width of the type,
the result of which is undefined) and endianness problems with
floating point numbers.
Index: bytecode.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/bytecode.m,v
retrieving revision 1.35
diff -u -u -r1.35 bytecode.m
--- bytecode.m 1998/03/03 17:33:35 1.35
+++ bytecode.m 1999/02/22 04:12:22
@@ -1,5 +1,5 @@
%---------------------------------------------------------------------------%
-% Copyright (C) 1996-1998 The University of Melbourne.
+% Copyright (C) 1996-1999 The University of Melbourne.
% This file may only be copied under the terms of the GNU General
% Public License - see the file COPYING in the Mercury distribution.
%---------------------------------------------------------------------------%
@@ -124,7 +124,7 @@
:- implementation.
-:- import_module hlds_pred, prog_out, llds_out.
+:- import_module bytecode_data, hlds_pred, prog_out, llds_out.
:- import_module library, int, string, require.
:- pred bytecode__version(int::out) is det.
@@ -1079,14 +1079,6 @@
%---------------------------------------------------------------------------%
-:- pred output_string(string, io__state, io__state).
-:- mode output_string(in, di, uo) is det.
-
-output_string(Val) -->
- io__write_bytes(Val),
- io__write_byte(0).
-
-
/*
** debug_cstring prints a string quoted in the manner of C.
*/
@@ -1101,166 +1093,6 @@
% the string as a bytecode argument. This is not very elegant.
io__write_char('"'),
io__write_char(' ').
-
-
-:- pred output_byte(int, io__state, io__state).
-:- mode output_byte(in, di, uo) is det.
-
-output_byte(Val) -->
- ( { Val < 256 } ->
- io__write_byte(Val)
- ;
- { error("byte does not fit in eight bits") }
- ).
-
-/*
-** Spit out a `short' in a portable format.
-** This format is: big-endian, 16-bit, 2's-complement.
-**
-** NOTE: We -assume- the machine architecture uses 2's-complement.
-*/
-:- pred output_short(int, io__state, io__state).
-:- mode output_short(in, di, uo) is det.
-
-output_short(Val) -->
- { Val1 is Val >> 8 },
- { Val2 is Val mod 256 },
- ( { Val1 < 256 } ->
- io__write_byte(Val1),
- io__write_byte(Val2)
- ;
- { error("small integer does not fit in sixteen bits") }
- ).
-
-/*
-** Spit out an `int' in a portable `highest common denominator' format.
-** This format is: big-endian, 64-bit, 2's-complement int.
-**
-** NOTE: We -assume- the machine architecture uses 2's-complement.
-*/
-:- pred output_int(int, io__state, io__state).
-:- mode output_int(in, di, uo) is det.
-
-output_int(IntVal) -->
- { int__bits_per_int(IntBits) },
- ( { IntBits > bytecode_int_bits } ->
- { error("size of int is larger than size of bytecode integer.")}
- ;
- { ZeroPadBytes is (bytecode_int_bits - IntBits) //
- bits_per_byte },
- output_padding_zeros(ZeroPadBytes),
- { FirstByteToDump is bytecode_int_bytes - ZeroPadBytes - 1 },
- output_int_bytes(FirstByteToDump, IntVal)
- ).
-
-:- func bytecode_int_bits = int.
-:- mode bytecode_int_bits = out is det.
-
-bytecode_int_bits = bits_per_byte * bytecode_int_bytes.
-
-:- func bytecode_int_bytes = int.
-:- mode bytecode_int_bytes = out is det.
-
-bytecode_int_bytes = 8.
-
-:- func bits_per_byte = int.
-:- mode bits_per_byte = out is det.
-
-bits_per_byte = 8.
-
-:- pred output_padding_zeros(int, io__state, io__state).
-:- mode output_padding_zeros(in, di, uo) is det.
-
-output_padding_zeros(NumBytes) -->
- ( { NumBytes > 0 } ->
- io__write_byte(0),
- { NumBytes1 is NumBytes - 1 },
- output_padding_zeros(NumBytes1)
- ;
- { true }
- ).
-
-:- pred output_int_bytes(int, int, io__state, io__state).
-:- mode output_int_bytes(in, in, di, uo) is det.
-
-output_int_bytes(ByteNum, IntVal) -->
- ( { ByteNum >= 0 } ->
- { BitShifts is ByteNum * bits_per_byte },
- { Byte is (IntVal >> BitShifts) mod (1 << bits_per_byte) },
- { ByteNum1 is ByteNum - 1 },
- io__write_byte(Byte),
- output_int_bytes(ByteNum1, IntVal)
- ;
- { true }
- ).
-
-/*
-** Spit out a `float' in a portable `highest common denominator format.
-** This format is: big-endian, 64-bit, IEEE-754 floating point value.
-**
-** NOTE: We -assume- the machine architecture uses IEEE-754.
-*/
-:- pred output_float(float, io__state, io__state).
-:- mode output_float(in, di, uo) is det.
-
-output_float(Val) -->
- { float_to_float64_bytes(Val, B0, B1, B2, B3, B4, B5, B6, B7) },
- output_byte(B0),
- output_byte(B1),
- output_byte(B2),
- output_byte(B3),
- output_byte(B4),
- output_byte(B5),
- output_byte(B6),
- output_byte(B7).
-
-/*
-** Convert a `float' to the representation used in the bytecode.
-** That is, a sequence of eight bytes.
-*/
-:- pred float_to_float64_bytes(float::in,
- int::out, int::out, int::out, int::out,
- int::out, int::out, int::out, int::out) is det.
-:- pragma c_code(
- float_to_float64_bytes(FloatVal::in, B0::out, B1::out, B2::out, B3::out,
- B4::out, B5::out, B6::out, B7::out),
- will_not_call_mercury,
- "
-
- {
- Float64 float64;
- unsigned char *raw_mem_p;
-
- float64 = (Float64) FloatVal;
- raw_mem_p = (unsigned char*) &float64;
-
- #if defined(MR_BIG_ENDIAN)
- B0 = raw_mem_p[0];
- B1 = raw_mem_p[1];
- B2 = raw_mem_p[2];
- B3 = raw_mem_p[3];
- B4 = raw_mem_p[4];
- B5 = raw_mem_p[5];
- B6 = raw_mem_p[6];
- B7 = raw_mem_p[7];
- #elif defined(MR_LITTLE_ENDIAN)
- B7 = raw_mem_p[0];
- B6 = raw_mem_p[1];
- B5 = raw_mem_p[2];
- B4 = raw_mem_p[3];
- B3 = raw_mem_p[4];
- B2 = raw_mem_p[5];
- B1 = raw_mem_p[6];
- B0 = raw_mem_p[7];
- #else
- #error Weird-endian architecture
- #endif
- }
-
- "
-).
-
-%---------------------------------------------------------------------------%
:- pred debug_string(string, io__state, io__state).
:- mode debug_string(in, di, uo) is det.
Index: bytecode_data.m
===================================================================
RCS file: bytecode_data.m
diff -N bytecode_data.m
--- /dev/null Mon Mar 8 10:23:38 1999
+++ bytecode_data.m Mon Mar 8 09:44:11 1999
@@ -0,0 +1,268 @@
+%---------------------------------------------------------------------------%
+% Copyright (C) 1999 The University of Melbourne.
+% This file may only be copied under the terms of the GNU General
+% Public License - see the file COPYING in the Mercury distribution.
+%---------------------------------------------------------------------------%
+%
+% This module defines the representation of basic types used by
+% the bytecode interpreter and by the Aditi bytecodes.
+%
+% Note: This file is included in both the Mercury compiler
+% and the Aditi bytecode assembler.
+%
+% Author: zs, stayl.
+%
+%---------------------------------------------------------------------------%
+
+:- module bytecode_data.
+
+:- interface.
+
+:- import_module io, int, list, string.
+
+:- pred output_string(string, io__state, io__state).
+:- mode output_string(in, di, uo) is det.
+
+:- pred output_byte(int, io__state, io__state).
+:- mode output_byte(in, di, uo) is det.
+
+/*
+** Spit out an `int' in a portable `highest common denominator' format.
+** This format is: big-endian, 64-bit, 2's-complement int.
+**
+** NOTE: We -assume- the machine architecture uses 2's-complement.
+*/
+:- pred output_int(int, io__state, io__state).
+:- mode output_int(in, di, uo) is det.
+
+:- pred int_to_byte_list(int, list(int)).
+:- mode int_to_byte_list(in, out) is det.
+
+/*
+** Same as output_int and int_to_byte_list, except only use 32 bits.
+*/
+:- pred output_int32(int, io__state, io__state).
+:- mode output_int32(in, di, uo) is det.
+
+:- pred int32_to_byte_list(int, list(int)).
+:- mode int32_to_byte_list(in, out) is det.
+
+/*
+** Spit out a `short' in a portable format.
+** This format is: big-endian, 16-bit, 2's-complement.
+**
+** NOTE: We -assume- the machine architecture uses 2's-complement.
+*/
+:- pred output_short(int, io__state, io__state).
+:- mode output_short(in, di, uo) is det.
+
+:- pred short_to_byte_list(int, list(int)).
+:- mode short_to_byte_list(in, out) is det.
+
+/*
+** Spit out a `float' in a portable `highest common denominator format.
+** This format is: big-endian, 64-bit, IEEE-754 floating point value.
+**
+** NOTE: We -assume- the machine architecture uses IEEE-754.
+*/
+:- pred output_float(float, io__state, io__state).
+:- mode output_float(in, di, uo) is det.
+
+:- pred float_to_byte_list(float, list(int)).
+:- mode float_to_byte_list(in, out) is det.
+
+%-----------------------------------------------------------------------------%
+
+:- implementation.
+
+:- import_module require.
+
+output_string(Val) -->
+ io__write_bytes(Val),
+ io__write_byte(0).
+
+output_byte(Val) -->
+ ( { Val < 256 } ->
+ io__write_byte(Val)
+ ;
+ { error("byte does not fit in eight bits") }
+ ).
+
+output_short(Val) -->
+ output_int(16, Val).
+
+short_to_byte_list(Val, Bytes) :-
+ int_to_byte_list(16, Val, Bytes).
+
+output_int32(IntVal) -->
+ output_int(32, IntVal).
+
+int32_to_byte_list(IntVal, List) :-
+ int_to_byte_list(32, IntVal, List).
+
+output_int(IntVal) -->
+ { int__bits_per_int(IntBits) },
+ ( { IntBits > bytecode_int_bits } ->
+ { error("size of int is larger than size of bytecode integer.")}
+ ;
+ output_int(io__write_byte, bytecode_int_bits, IntVal)
+ ).
+
+int_to_byte_list(IntVal, Bytes) :-
+ int__bits_per_int(IntBits),
+ ( IntBits > bytecode_int_bits ->
+ error("size of int is larger than size of bytecode integer.")
+ ;
+ int_to_byte_list(IntVal, bytecode_int_bits, Bytes)
+ ).
+
+:- pred output_int(int, int, io__state, io__state).
+:- mode output_int(in, in, di, uo) is det.
+
+output_int(Bits, IntVal) -->
+ output_int(io__write_byte, Bits, IntVal).
+
+:- pred int_to_byte_list(int, int, list(int)).
+:- mode int_to_byte_list(in, in, out) is det.
+
+int_to_byte_list(Bits, IntVal, Bytes) :-
+ output_int(cons, Bits, IntVal, [], RevBytes),
+ list__reverse(RevBytes, Bytes).
+
+:- pred cons(T, list(T), list(T)).
+:- mode cons(in, in, out) is det.
+
+cons(T, List, [T | List]).
+
+:- pred output_int(pred(int, T, T), int, int, T, T).
+:- mode output_int(pred(in, in, out) is det, in, in, in, out) is det.
+:- mode output_int(pred(in, di, uo) is det, in, in, di, uo) is det.
+
+output_int(Writer, Bits, IntVal) -->
+ { int__bits_per_int(IntBits) },
+ {
+ Bits < IntBits,
+ (IntVal /\ (\0 << Bits)) \= 0
+ ->
+ string__format(
+ "error: bytecode_data__output_int: %d does not fit in %d bits",
+ [i(IntVal), i(Bits)], Msg),
+ error(Msg)
+ ;
+ true
+ },
+ { Bits > IntBits ->
+ ZeroPadBytes is (Bits - IntBits) // bits_per_byte
+ ;
+ ZeroPadBytes = 0
+ },
+ output_padding_zeros(Writer, ZeroPadBytes),
+ { BytesToDump = Bits // bits_per_byte },
+ { FirstByteToDump is BytesToDump - ZeroPadBytes - 1 },
+ output_int_bytes(Writer, FirstByteToDump, IntVal).
+
+:- func bytecode_int_bits = int.
+:- mode bytecode_int_bits = out is det.
+
+bytecode_int_bits = bits_per_byte * bytecode_int_bytes.
+
+:- func bytecode_int_bytes = int.
+:- mode bytecode_int_bytes = out is det.
+
+bytecode_int_bytes = 8.
+
+:- func bits_per_byte = int.
+:- mode bits_per_byte = out is det.
+
+bits_per_byte = 8.
+
+:- pred output_padding_zeros(pred(int, T, T), int, T, T).
+:- mode output_padding_zeros(pred(in, in, out) is det, in, in, out) is det.
+:- mode output_padding_zeros(pred(in, di, uo) is det, in, di, uo) is det.
+
+output_padding_zeros(Writer, NumBytes) -->
+ ( { NumBytes > 0 } ->
+ call(Writer, 0),
+ { NumBytes1 is NumBytes - 1 },
+ output_padding_zeros(Writer, NumBytes1)
+ ;
+ []
+ ).
+
+:- pred output_int_bytes(pred(int, T, T), int, int, T, T).
+:- mode output_int_bytes(pred(in, in, out) is det, in, in, in, out) is det.
+:- mode output_int_bytes(pred(in, di, uo) is det, in, in, di, uo) is det.
+
+output_int_bytes(Writer, ByteNum, IntVal) -->
+ ( { ByteNum >= 0 } ->
+ { BitShifts is ByteNum * bits_per_byte },
+ { Byte is (IntVal >> BitShifts) mod (1 << bits_per_byte) },
+ { ByteNum1 is ByteNum - 1 },
+ call(Writer, Byte),
+ output_int_bytes(Writer, ByteNum1, IntVal)
+ ;
+ []
+ ).
+
+output_float(Val) -->
+ { float_to_float64_bytes(Val, B0, B1, B2, B3, B4, B5, B6, B7) },
+ output_byte(B0),
+ output_byte(B1),
+ output_byte(B2),
+ output_byte(B3),
+ output_byte(B4),
+ output_byte(B5),
+ output_byte(B6),
+ output_byte(B7).
+
+float_to_byte_list(Val, [B0, B1, B2, B3, B4, B5, B6, B7]) :-
+ float_to_float64_bytes(Val, B0, B1, B2, B3, B4, B5, B6, B7).
+
+/*
+** Convert a `float' to the representation used in the bytecode.
+** That is, a sequence of eight bytes.
+*/
+:- pred float_to_float64_bytes(float::in,
+ int::out, int::out, int::out, int::out,
+ int::out, int::out, int::out, int::out) is det.
+:- pragma c_code(
+ float_to_float64_bytes(FloatVal::in, B0::out, B1::out, B2::out, B3::out,
+ B4::out, B5::out, B6::out, B7::out),
+ will_not_call_mercury,
+ "
+
+ {
+ Float64 float64;
+ unsigned char *raw_mem_p;
+
+ float64 = (Float64) FloatVal;
+ raw_mem_p = (unsigned char*) &float64;
+
+ #if defined(MR_BIG_ENDIAN)
+ B0 = raw_mem_p[0];
+ B1 = raw_mem_p[1];
+ B2 = raw_mem_p[2];
+ B3 = raw_mem_p[3];
+ B4 = raw_mem_p[4];
+ B5 = raw_mem_p[5];
+ B6 = raw_mem_p[6];
+ B7 = raw_mem_p[7];
+ #elif defined(MR_LITTLE_ENDIAN)
+ B7 = raw_mem_p[0];
+ B6 = raw_mem_p[1];
+ B5 = raw_mem_p[2];
+ B4 = raw_mem_p[3];
+ B3 = raw_mem_p[4];
+ B2 = raw_mem_p[5];
+ B1 = raw_mem_p[6];
+ B0 = raw_mem_p[7];
+ #else
+ #error Weird-endian architecture
+ #endif
+ }
+
+ "
+).
+
+%---------------------------------------------------------------------------%
+
Index: rl_code.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/rl_code.m,v
retrieving revision 1.1
diff -u -u -r1.1 rl_code.m
--- rl_code.m 1998/12/06 23:44:53 1.1
+++ rl_code.m 1999/03/07 23:19:24
@@ -1,11 +1,11 @@
%-----------------------------------------------------------------------------%
-% Copyright (C) 1998 University of Melbourne.
+% Copyright (C) 1998-1999 University of Melbourne.
% This file may only be copied under the terms of the GNU General
% Public License - see the file COPYING in the Mercury distribution.
%-----------------------------------------------------------------------------%
% Do not edit - this file was automatically generated by
% $ADITI_ROOT/src/rosi/create_rl_code_m.
-% Created Wed Oct 7 12:33:52 1998
+% Created Mon Mar 8 10:19:23 1999
%-----------------------------------------------------------------------------%
:- module rl_code.
@@ -421,7 +421,8 @@
%-----------------------------------------------------------------------------%
:- implementation.
-:- import_module int, string, char, bool, rl, std_util.
+:- import_module bytecode_data.
+:- import_module int, string, char, bool, std_util.
:- import_module term, varset.
%-----------------------------------------------------------------------------%
@@ -1648,9 +1649,11 @@
int16_to_bytecode(340, I340Codes),
list__condense([I340Codes], Splits).
-int32_to_bytecode(X, [0xFF/\(X>>24), 0xFF/\(X>>16), 0xFF/\(X>>8), 0xFF/\X]).
+int32_to_bytecode(X, List) :-
+ int32_to_byte_list(X, List).
-int16_to_bytecode(X, [0xFF/\(X>>8), 0xFF/\X]).
+int16_to_bytecode(X, List) :-
+ short_to_byte_list(X, List).
aString_to_bytecode(Str, Ints) :-
string__to_char_list(Str, Chars),
@@ -1658,42 +1661,13 @@
list__map(ToInt, Chars, Ints0),
list__append(Ints0, [0], Ints).
-aDouble_to_bytecode(Flt, [B0, B1, B2, B3, B4, B5, B6, B7]) :-
- construct_bytes(Flt, B0, B1, B2, B3, B4, B5, B6, B7).
+aDouble_to_bytecode(Flt, List) :-
+ float_to_byte_list(Flt, List).
aBlob_to_bytecode(_Blob, [0]). % NYI
-:- pred construct_bytes(float, int, int, int, int, int, int, int, int).
-:- mode construct_bytes(in, out, out, out, out, out, out, out, out) is det.
-
-:- pragma(c_code, construct_bytes(F::in, B0::out, B1::out, B2::out, B3::out,
- B4::out, B5::out, B6::out, B7::out), "{
- union {
- double flt;
- char cs[8];
- } bender;
-
- bender.flt = F;
- B0 = bender.cs[0];
- B1 = bender.cs[1];
- B2 = bender.cs[2];
- B3 = bender.cs[3];
- B4 = bender.cs[4];
- B5 = bender.cs[5];
- B6 = bender.cs[6];
- B7 = bender.cs[7];
-}").
-
-aInt_to_bytecode(X, [B0, B1, B2, B3, B4, B5, B6, B7]) :-
- FF = (0xFF),
- B0 = FF/\(X>>56),
- B1 = FF/\(X>>48),
- B2 = FF/\(X>>40),
- B3 = FF/\(X>>32),
- B4 = FF/\(X>>24),
- B5 = FF/\(X>>16),
- B6 = FF/\(X>>8),
- B7 = FF/\X.
+aInt_to_bytecode(X, List) :-
+ int_to_byte_list(X, List).
rl_code__version(1, 19).
More information about the developers
mailing list