Build problem with R9C-0

Per Gustafsson per.gustafsson@REDACTED
Mon Sep 15 12:48:23 CEST 2003


This problem seems to have to do with the shared heap. It seems that you
get the hipe_literals.hrl file that is produced when making the shared
emulator. I think you can solve this problem by changing your:

erts/emulator/hipe/hipe_mkliterals.c

and

lib/hipe/rtl/hipe_tagscheme.erl

to the ones I've attached to this mail.

On Mon, 15 Sep 2003, Zoltan Peter Toth wrote:

> Hi,
>
> Can anybody advise me what's wrong with my build:
> (it's Debian Linux, gcc 3.0 [but 2.95 does the same]. I attached my config.cache.)
>
> ----------
> === Entering application hipe
> make[3]: Entering directory `.../otp_src_R9C-0/lib/hipe/rtl'
> erlc -W  -bbeam  +debug_info  -o../ebin hipe_rtl.erl
> erlc -W  -bbeam  +debug_info  -o../ebin hipe_rtl_cfg.erl
> erlc -W  -bbeam  +debug_info  -o../ebin hipe_rtl_cse.erl
> erlc -W  -bbeam  +debug_info  -o../ebin hipe_rtl_ebb.erl
> erlc -W  -bbeam  +debug_info  -o../ebin hipe_rtl_liveness.erl
> erlc -W  -bbeam  +debug_info  -o../ebin hipe_rtl_prop.erl
> erlc -W  -bbeam  +debug_info  -o../ebin hipe_icode2rtl.erl
> erlc -W  -bbeam  +debug_info  -o../ebin hipe_tagscheme.erl
> ./otp_src_R9C-0/lib/hipe/rtl/hipe_tagscheme.erl:695: undefined macro ''P_OFF_HEAP_OVERHEAD''
> ./otp_src_R9C-0/lib/hipe/rtl/hipe_tagscheme.erl:37: function finalize_bin/4 undefined
> ---------
> (The latter error is a consequence of the former, as it wants to export the erroneous function.)
>
> And really, the string P_OFF_HEAP_OVERHEAD was not found in the whole source tree
> with a recursive egrep.
> Of course, hipe does not work.
> Thanks for any advice.
> 	Zoltan
>
-------------- next part --------------
%%% $Id: hipe_tagscheme.erl,v 1.23 2003/07/10 12:52:36 pergu Exp $
%%%
%%% hipe_tagscheme.erl
%%%
%%% XXX: This is specific for Erlang 5.0 / R9.
%%% 
%%%  020904: Happi - added support for external pids and ports.
%%%     

-module(hipe_tagscheme).

-export([mk_nil/0, mk_fixnum/1, mk_arityval/1, mk_atom/1, mk_non_value/0]).
-export([is_fixnum/1]).
-export([tag_bignum/2, tag_flonum/2, tag_tuple/2, tag_cons/2]).
-export([write_catch_frame/3]).
-export([save_CP/4, restore_CP/4]).
-export([test_is_boxed/4, get_header/2]).
-export([test_nil/4, test_cons/4, test_flonum/4, test_fixnum/4,
	 test_tuple/4, test_atom/4, test_bignum/4, 
	 test_any_pid/4,test_any_port/4,
	 test_internal_pid/4,
	 test_internal_port/4, test_ref/4, test_fun/4, test_binary/4, test_list/4,
	 test_integer/4, test_number/4, test_constant/4, test_tuple_N/5]).
-export([untag_fixnum/2]).
-export([test_two_fixnums/3,
	 fixnum_gt/5, fixnum_lt/5, fixnum_ge/5, fixnum_le/5,
	 fixnum_addsub/5, fixnum_mul/4,
	 fixnum_andorxor/4, fixnum_not/2]).
-export([unsafe_car/2, unsafe_cdr/2,
	 unsafe_constant_element/3, unsafe_update_element/3, element/6]).
-export([unsafe_closure_element/3]).
-export([mk_fun_header/0, tag_fun/2, untag_fun/2,
	 if_fun_get_arity_and_address/5]).
-export([unsafe_untag_float/2, unsafe_tag_float/3]).
-export([unsafe_mk_sub_binary/4, unsafe_mk_float/3, unsafe_mk_big/4, unsafe_load_float/3]).
-export([test_subbinary/3, test_heap_binary/3]).
-export([finalize_bin/4, mk_var_header/3, get_base/2]).
-include("hipe_icode2rtl.hrl").
-include("hipe_literals.hrl").
-undef(TAG_PRIMARY_BOXED).
-undef(TAG_IMMED2_MASK).
-undef(TAG_IMMED2_CATCH).
-undef(TAG_IMMED2_SIZE).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

-define(TAG_PRIMARY_SIZE,   2).
-define(TAG_PRIMARY_MASK,   16#3).
-define(TAG_PRIMARY_HEADER, 16#0).
-define(TAG_PRIMARY_LIST,   16#1).
-define(TAG_PRIMARY_BOXED,  16#2).
-define(TAG_PRIMARY_IMMED1, 16#3).

-define(TAG_IMMED1_SIZE,  4).
-define(TAG_IMMED1_MASK,  16#F).
-define(TAG_IMMED1_PID,   ((16#0 bsl ?TAG_PRIMARY_SIZE) bor ?TAG_PRIMARY_IMMED1)).
-define(TAG_IMMED1_PORT,  ((16#1 bsl ?TAG_PRIMARY_SIZE) bor ?TAG_PRIMARY_IMMED1)).
-define(TAG_IMMED1_IMMED2,((16#2 bsl ?TAG_PRIMARY_SIZE) bor ?TAG_PRIMARY_IMMED1)).
-define(TAG_IMMED1_SMALL, ((16#3 bsl ?TAG_PRIMARY_SIZE) bor ?TAG_PRIMARY_IMMED1)).

-define(TAG_IMMED2_SIZE,  6).
-define(TAG_IMMED2_MASK,  16#3F).
-define(TAG_IMMED2_ATOM,  ((16#0 bsl ?TAG_IMMED1_SIZE) bor ?TAG_IMMED1_IMMED2)).
-define(TAG_IMMED2_CATCH, ((16#1 bsl ?TAG_IMMED1_SIZE) bor ?TAG_IMMED1_IMMED2)).
-define(TAG_IMMED2_NIL,   ((16#3 bsl ?TAG_IMMED1_SIZE) bor ?TAG_IMMED1_IMMED2)).

-define(TAG_HEADER_ARITYVAL,((16#0 bsl ?TAG_PRIMARY_SIZE) bor ?TAG_PRIMARY_HEADER)).
-define(TAG_HEADER_VECTOR,  ((16#1 bsl ?TAG_PRIMARY_SIZE) bor ?TAG_PRIMARY_HEADER)).
-define(TAG_HEADER_POS_BIG, ((16#2 bsl ?TAG_PRIMARY_SIZE) bor ?TAG_PRIMARY_HEADER)).
-define(TAG_HEADER_NEG_BIG, ((16#3 bsl ?TAG_PRIMARY_SIZE) bor ?TAG_PRIMARY_HEADER)).
-define(BIG_SIGN_BIT, (16#1 bsl ?TAG_PRIMARY_SIZE)).
-define(TAG_HEADER_REF,     ((16#4 bsl ?TAG_PRIMARY_SIZE) bor ?TAG_PRIMARY_HEADER)).
-define(TAG_HEADER_FUN,     ((16#5 bsl ?TAG_PRIMARY_SIZE) bor ?TAG_PRIMARY_HEADER)).
-define(TAG_HEADER_FLOAT,   ((16#6 bsl ?TAG_PRIMARY_SIZE) bor ?TAG_PRIMARY_HEADER)).
-define(BINARY_XXX_MASK, (16#3 bsl ?TAG_PRIMARY_SIZE)).
-define(TAG_HEADER_REFC_BIN,((16#8 bsl ?TAG_PRIMARY_SIZE) bor ?TAG_PRIMARY_HEADER)).
-define(TAG_HEADER_HEAP_BIN,((16#9 bsl ?TAG_PRIMARY_SIZE) bor ?TAG_PRIMARY_HEADER)).
-define(TAG_HEADER_SUB_BIN, ((16#A bsl ?TAG_PRIMARY_SIZE) bor ?TAG_PRIMARY_HEADER)).
-define(TAG_HEADER_EXTERNAL_PID, ((16#C bsl ?TAG_PRIMARY_SIZE) bor ?TAG_PRIMARY_HEADER)).
-define(TAG_HEADER_EXTERNAL_PORT,((16#D bsl ?TAG_PRIMARY_SIZE) bor ?TAG_PRIMARY_HEADER)).
-define(TAG_HEADER_EXTERNAL_REF, ((16#E bsl ?TAG_PRIMARY_SIZE) bor ?TAG_PRIMARY_HEADER)).

-define(TAG_HEADER_MASK, 16#3F).
-define(HEADER_ARITY_OFFS, 6).


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

mk_header(SZ,TAG) -> (SZ bsl ?HEADER_ARITY_OFFS) + TAG.
mk_arityval(SZ)	-> mk_header(SZ, ?TAG_HEADER_ARITYVAL).

mk_fixnum(X)	-> (X bsl ?TAG_IMMED1_SIZE) + ?TAG_IMMED1_SMALL.

-define(NIL, ((-1 bsl ?TAG_IMMED2_SIZE) bor ?TAG_IMMED2_NIL)).
mk_nil()	-> ?NIL.
mk_atom(X)	-> (X bsl ?TAG_IMMED2_SIZE) + ?TAG_IMMED2_ATOM.
mk_non_value()	-> ?THE_NON_VALUE.

-define(SMALL_BITS, 28).
-define(MAX_SMALL, ((1 bsl (?SMALL_BITS - 1)) - 1)).
-define(MIN_SMALL, (-(1 bsl (?SMALL_BITS - 1)))).

is_fixnum(N) when N =< ?MAX_SMALL, N >= ?MIN_SMALL -> true;
is_fixnum(_) -> false.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

-define(HEADER_FUN, mk_header(?ERL_FUN_SIZE-2,?TAG_HEADER_FUN)).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

tag_boxed(Res, X) ->
    hipe_rtl:mk_alu(Res, X, 'add', hipe_rtl:mk_imm(?TAG_PRIMARY_BOXED)).

tag_bignum(Res, X) -> tag_boxed(Res, X).
tag_flonum(Res, X) -> tag_boxed(Res, X).
tag_tuple(Res, X) -> tag_boxed(Res, X).

tag_cons(Res, X) ->
    hipe_rtl:mk_alu(Res, X, 'add', hipe_rtl:mk_imm(?TAG_PRIMARY_LIST)).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

write_catch_frame(SP, Off,  CatchLab) ->
    %% SP[Off] = make_catch(CatchLab)
    %% loader should transform the label to a catch table index,
    %% tag it, and emit a 'load constant' insn
    CatchPC = hipe_rtl:mk_new_reg(),
    [hipe_rtl:mk_load_address(CatchPC, CatchLab, 'catch'),
     hipe_rtl:mk_store(SP, hipe_rtl:mk_imm(Off), CatchPC)].

%%% no longer needed
%tag_catch(Ix) -> (Ix bsl ?TAG_IMMED2_SIZE) bor ?TAG_IMMED2_CATCH.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%% This is safe for SPARC and other RISCs, which always create 32-bit
%%% aligned return addresses.
%%% For the x86, we assume that either CALL insns are aligned to ensure
%%% 32-bit aligned return addresses, or that stack/register maps are
%%% generated to inform the gc which words contain return addresses.
%%%
%%% XXX: this is trivial now -- inline at call sites?

save_CP(CP, SP, Off, Code) ->
    [hipe_rtl:mk_store(SP, Off, CP) |
     Code].

restore_CP(CP, SP, Off, Code) ->
    [hipe_rtl:mk_load(CP, SP, Off) |
     Code].

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%% Operations to test if an object has a known type T.

test_nil(X, TrueLab, FalseLab, Pred) ->
    hipe_rtl:mk_branch(X, eq, hipe_rtl:mk_imm(?NIL), TrueLab, FalseLab, Pred).

test_cons(X, TrueLab, FalseLab, Pred) ->
    Tmp = hipe_rtl:mk_new_reg(),
    Mask = hipe_rtl:mk_imm(?TAG_PRIMARY_MASK - ?TAG_PRIMARY_LIST),
    hipe_rtl:mk_alub(Tmp, X, 'and', Mask, 'eq', TrueLab, FalseLab, Pred).

test_is_boxed(X, TrueLab, FalseLab, Pred) ->
    Tmp = hipe_rtl:mk_new_reg(),
    Mask = hipe_rtl:mk_imm(?TAG_PRIMARY_MASK - ?TAG_PRIMARY_BOXED),
    hipe_rtl:mk_alub(Tmp, X, 'and', Mask, 'eq', TrueLab, FalseLab, Pred).

get_header(Res, X) ->
    hipe_rtl:mk_load(Res, X, hipe_rtl:mk_imm(-(?TAG_PRIMARY_BOXED))).

mask_and_compare(X, Mask, Value, TrueLab, FalseLab, Pred) ->
    Tmp = hipe_rtl:mk_new_reg(),
    [hipe_rtl:mk_alu(Tmp, X, 'and', hipe_rtl:mk_imm(Mask)),
     hipe_rtl:mk_branch(Tmp, 'eq', hipe_rtl:mk_imm(Value), TrueLab, FalseLab, Pred)].

test_immed1(X, Value, TrueLab, FalseLab, Pred) ->
    mask_and_compare(X, ?TAG_IMMED1_MASK, Value, TrueLab, FalseLab, Pred).

test_internal_pid(X, TrueLab, FalseLab, Pred) ->
    test_immed1(X, ?TAG_IMMED1_PID, TrueLab, FalseLab, Pred).

test_any_pid(X, TrueLab, FalseLab, Pred) ->
    NotInternalPidLab = hipe_rtl:mk_new_label(),
    [test_internal_pid(X, TrueLab, hipe_rtl:label_name(NotInternalPidLab), Pred),
     NotInternalPidLab,
     test_external_pid(X, TrueLab,FalseLab,Pred)].

test_external_pid(X, TrueLab, FalseLab, Pred) ->
  Tmp = hipe_rtl:mk_new_reg(),
  HalfTrueLab = hipe_rtl:mk_new_label(),
  ExternalPidMask = ?TAG_HEADER_MASK,
  [test_is_boxed(X, hipe_rtl:label_name(HalfTrueLab), FalseLab, Pred),
   HalfTrueLab,
   get_header(Tmp, X),
   mask_and_compare(Tmp, ExternalPidMask, ?TAG_HEADER_EXTERNAL_PID,
		    TrueLab, FalseLab, Pred)].

test_internal_port(X, TrueLab, FalseLab, Pred) ->
    test_immed1(X, ?TAG_IMMED1_PORT, TrueLab, FalseLab, Pred).

test_any_port(X, TrueLab, FalseLab, Pred) ->
    NotInternalPortLab = hipe_rtl:mk_new_label(),
    [test_internal_port(X, TrueLab, hipe_rtl:label_name(NotInternalPortLab), Pred),
     NotInternalPortLab,
     test_external_port(X, TrueLab,FalseLab,Pred)].

test_external_port(X, TrueLab, FalseLab, Pred) ->
  Tmp = hipe_rtl:mk_new_reg(),
  HalfTrueLab = hipe_rtl:mk_new_label(),
  ExternalPortMask = ?TAG_HEADER_MASK,
  [test_is_boxed(X, hipe_rtl:label_name(HalfTrueLab), FalseLab, Pred),
   HalfTrueLab,
   get_header(Tmp, X),
   mask_and_compare(Tmp, ExternalPortMask, ?TAG_HEADER_EXTERNAL_PORT,
		    TrueLab, FalseLab, Pred)].

test_fixnum(X, TrueLab, FalseLab, Pred) ->
    test_immed1(X, ?TAG_IMMED1_SMALL, TrueLab, FalseLab, Pred).

test_atom(X, TrueLab, FalseLab, Pred) ->
    mask_and_compare(X, ?TAG_IMMED2_MASK, ?TAG_IMMED2_ATOM,
		     TrueLab, FalseLab, Pred).

test_tuple(X, TrueLab, FalseLab, Pred) ->
    Tmp = hipe_rtl:mk_new_reg(),
    Tmp2 = hipe_rtl:mk_new_reg(),
    HalfTrueLab = hipe_rtl:mk_new_label(),
    [test_is_boxed(X, hipe_rtl:label_name(HalfTrueLab), FalseLab, Pred),
     HalfTrueLab,
     get_header(Tmp, X),
     hipe_rtl:mk_alub(Tmp2, Tmp, 'and', hipe_rtl:mk_imm(?TAG_HEADER_MASK), 'eq',
		 TrueLab, FalseLab, Pred)].

test_tuple_N(X, N, TrueLab, FalseLab, Pred) ->
    Tmp = hipe_rtl:mk_new_reg(),
    HalfTrueLab = hipe_rtl:mk_new_label(),
    [test_is_boxed(X, hipe_rtl:label_name(HalfTrueLab), FalseLab, Pred),
     HalfTrueLab,
     get_header(Tmp, X),
     hipe_rtl:mk_branch(Tmp, 'eq', hipe_rtl:mk_imm(mk_arityval(N)),
		   TrueLab, FalseLab, Pred)].

test_ref(X, TrueLab, FalseLab, Pred) ->
    Tmp = hipe_rtl:mk_new_reg(),
    HalfTrueLab = hipe_rtl:mk_new_label(),
    TwoThirdsTrueLab = hipe_rtl:mk_new_label(),
    [test_is_boxed(X, hipe_rtl:label_name(HalfTrueLab), FalseLab, Pred),
     HalfTrueLab,
     get_header(Tmp, X),
     mask_and_compare(Tmp, ?TAG_HEADER_MASK, ?TAG_HEADER_REF,
		      TrueLab, hipe_rtl:label_name(TwoThirdsTrueLab), Pred),
     TwoThirdsTrueLab,
     mask_and_compare(Tmp, ?TAG_HEADER_MASK, ?TAG_HEADER_EXTERNAL_REF,
		      TrueLab, FalseLab, Pred)
    ].

test_fun(X, TrueLab, FalseLab, Pred) ->
    Tmp = hipe_rtl:mk_new_reg(),
    HalfTrueLab = hipe_rtl:mk_new_label(),
    [test_is_boxed(X, hipe_rtl:label_name(HalfTrueLab), FalseLab, Pred),
     HalfTrueLab,
     get_header(Tmp, X),
     mask_and_compare(Tmp, ?TAG_HEADER_MASK, ?TAG_HEADER_FUN,
		      TrueLab, FalseLab, Pred)].

test_flonum(X, TrueLab, FalseLab, Pred) ->
    HeaderFlonum = mk_header(2, ?TAG_HEADER_FLOAT),
    Tmp = hipe_rtl:mk_new_reg(),
    HalfTrueLab = hipe_rtl:mk_new_label(),
    [test_is_boxed(X, hipe_rtl:label_name(HalfTrueLab), FalseLab, Pred),
     HalfTrueLab,
     get_header(Tmp, X),
     hipe_rtl:mk_branch(Tmp, 'eq', hipe_rtl:mk_imm(HeaderFlonum),
		   TrueLab, FalseLab, Pred)].

test_bignum(X, TrueLab, FalseLab, Pred) ->
    Tmp = hipe_rtl:mk_new_reg(),
    HalfTrueLab = hipe_rtl:mk_new_label(),
    BigMask = ?TAG_HEADER_MASK - ?BIG_SIGN_BIT,
    [test_is_boxed(X, hipe_rtl:label_name(HalfTrueLab), FalseLab, Pred),
     HalfTrueLab,
     get_header(Tmp, X),
     mask_and_compare(Tmp, BigMask, ?TAG_HEADER_POS_BIG,
		      TrueLab, FalseLab, Pred)].

test_binary(X, TrueLab, FalseLab, Pred) ->
    Tmp = hipe_rtl:mk_new_reg(),
    HalfTrueLab = hipe_rtl:mk_new_label(),
    Mask = ?TAG_HEADER_MASK - ?BINARY_XXX_MASK,
    [test_is_boxed(X, hipe_rtl:label_name(HalfTrueLab), FalseLab, Pred),
     HalfTrueLab,
     get_header(Tmp, X),
     mask_and_compare(Tmp, Mask, ?TAG_HEADER_REFC_BIN, TrueLab, FalseLab, Pred)].

test_list(X, TrueLab, FalseLab, Pred) ->
    Lab = hipe_rtl:mk_new_label(),
    [test_cons(X, TrueLab, hipe_rtl:label_name(Lab), 0.5),
     Lab,
     test_nil(X, TrueLab, FalseLab, Pred)].

test_integer(X, TrueLab, FalseLab, Pred) ->
    Lab = hipe_rtl:mk_new_label(),
    [test_fixnum(X, TrueLab, hipe_rtl:label_name(Lab), 0.5),
     Lab,
     test_bignum(X, TrueLab, FalseLab, Pred)].

test_number(X, TrueLab, FalseLab, Pred) ->
    Lab1 = hipe_rtl:mk_new_label(),
    Lab2 = hipe_rtl:mk_new_label(),
    Lab3 = hipe_rtl:mk_new_label(),
    Tmp = hipe_rtl:mk_new_reg(),
    BigMask = ?TAG_HEADER_MASK - ?BIG_SIGN_BIT,
    HeaderFlonum = mk_header(2, ?TAG_HEADER_FLOAT),
    [test_fixnum(X, TrueLab, hipe_rtl:label_name(Lab1), 0.5),
     Lab1,
     test_is_boxed(X, hipe_rtl:label_name(Lab2), FalseLab, 0.5),
     Lab2,
     get_header(Tmp, X),
     mask_and_compare(Tmp, BigMask, ?TAG_HEADER_POS_BIG,
		      TrueLab, hipe_rtl:label_name(Lab3), 0.5),
     Lab3,
     hipe_rtl:mk_branch(Tmp, 'eq', hipe_rtl:mk_imm(HeaderFlonum),
		   TrueLab, FalseLab, Pred)].

%%% CONS, NIL, and TUPLE are not constants, everything else is
test_constant(X, TrueLab, FalseLab, Pred) ->
    Lab1 = hipe_rtl:mk_new_label(),
    Lab2 = hipe_rtl:mk_new_label(),
    Pred1 = 1-Pred,
    [test_cons(X, FalseLab, hipe_rtl:label_name(Lab1), Pred1),
     Lab1,
     test_nil(X, FalseLab, hipe_rtl:label_name(Lab2), Pred1),
     Lab2,
     test_tuple(X, FalseLab, TrueLab, Pred1)].

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

untag_fixnum(DestReg, SrcVar) ->
  hipe_rtl:mk_alu(DestReg, SrcVar, 'sra', hipe_rtl:mk_imm(?TAG_IMMED1_SIZE)).

test_two_fixnums(Arg1, Arg2, FalseLab) ->
    Tmp = hipe_rtl:mk_new_reg(),
    TrueLab = hipe_rtl:mk_new_label(),
    [hipe_rtl:mk_alu(Tmp, Arg1, 'and', Arg2),
     test_fixnum(Tmp, hipe_rtl:label_name(TrueLab), FalseLab, 0.99),
     TrueLab].

fixnum_cmp(Arg1, Arg2, TrueLab, FalseLab, Pred, CmpOp) ->
    hipe_rtl:mk_branch(Arg1, CmpOp, Arg2, TrueLab, FalseLab, Pred).

fixnum_gt(Arg1, Arg2, TrueLab, FalseLab, Pred) ->
    fixnum_cmp(Arg1, Arg2, TrueLab, FalseLab, Pred, gt).

fixnum_lt(Arg1, Arg2, TrueLab, FalseLab, Pred) ->
    fixnum_cmp(Arg1, Arg2, TrueLab, FalseLab, Pred, lt).

fixnum_ge(Arg1, Arg2, TrueLab, FalseLab, Pred) ->
    fixnum_cmp(Arg1, Arg2, TrueLab, FalseLab, Pred, ge).

fixnum_le(Arg1, Arg2, TrueLab, FalseLab, Pred) ->
    fixnum_cmp(Arg1, Arg2, TrueLab, FalseLab, Pred, le).

%%% (16X+tag)+((16Y+tag)-tag) = 16X+tag+16Y = 16(X+Y)+tag
%%% (16X+tag)-((16Y+tag)-tag) = 16X+tag-16Y = 16(X-Y)+tag
fixnum_addsub(AluOp, Arg1, Arg2, Res, OtherLab) ->
    Tmp = hipe_rtl:mk_new_reg(),
    %% XXX: Consider moving this test to the users of fixnum_addsub.
    case Arg1 =/= Res andalso Arg2 =/= Res of 
        true -> 
	    %% Args differ from res.
	    NoOverflowLab = hipe_rtl:mk_new_label(),
	   [hipe_rtl:mk_alu(Tmp, Arg2, sub, 
			    hipe_rtl:mk_imm(?TAG_IMMED1_SMALL)),
	    hipe_rtl:mk_alub(Res, Arg1, AluOp, Tmp, overflow, 
			     hipe_rtl:label_name(OtherLab),
			     hipe_rtl:label_name(NoOverflowLab), 0.01),
	    NoOverflowLab];
        false ->
            %% At least one of the arguments is the same as Res.
            Tmp2 = hipe_rtl:mk_new_var(),
            NoOverflowLab = hipe_rtl:mk_new_label(),
            [hipe_rtl:mk_alu(Tmp, Arg2, sub, 
			     hipe_rtl:mk_imm(?TAG_IMMED1_SMALL)),
	     hipe_rtl:mk_alub(Tmp2, Arg1, AluOp, Tmp, overflow, 
			      hipe_rtl:label_name(OtherLab),
			      hipe_rtl:label_name(NoOverflowLab), 0.01),
	     NoOverflowLab,
	     hipe_rtl:mk_move(Res, Tmp2)]
    end.

%%% ((16X+tag) div 16) * ((16Y+tag)-tag) + tag = X*16Y+tag = 16(XY)+tag
fixnum_mul(Arg1, Arg2, Res, OtherLab) ->
    Tmp = hipe_rtl:mk_new_reg(),
    U1 = hipe_rtl:mk_new_reg(),
    U2 = hipe_rtl:mk_new_reg(),
    NoOverflowLab = hipe_rtl:mk_new_label(),
    [hipe_rtl:mk_alu(U1, Arg1, 'sra', hipe_rtl:mk_imm(?TAG_IMMED1_SIZE)),
     hipe_rtl:mk_alu(U2, Arg2, 'sub', hipe_rtl:mk_imm(?TAG_IMMED1_SMALL)),
     hipe_rtl:mk_alub(Tmp, U1, 'mul', U2, overflow, hipe_rtl:label_name(OtherLab),
		 hipe_rtl:label_name(NoOverflowLab), 0.01),
     NoOverflowLab,
     hipe_rtl:mk_alu(Res, Tmp, 'add', hipe_rtl:mk_imm(?TAG_IMMED1_SMALL))].

fixnum_andorxor(AluOp, Arg1, Arg2, Res) ->
    case AluOp of
	'xor' ->
	    Tmp = hipe_rtl:mk_new_reg(),
	    [hipe_rtl:mk_alu(Tmp, Arg1, 'xor', Arg2),	% clears tag :-(
	     hipe_rtl:mk_alu(Res, Tmp, 'or', hipe_rtl:mk_imm(?TAG_IMMED1_SMALL))];
	_ -> hipe_rtl:mk_alu(Res, Arg1, AluOp, Arg2)
    end.

fixnum_not(Arg, Res) ->
    Mask = (-1 bsl ?TAG_IMMED1_SIZE),
    hipe_rtl:mk_alu(Res, Arg, 'xor', hipe_rtl:mk_imm(Mask)).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

unsafe_car(Dst, Arg) ->
    hipe_rtl:mk_load(Dst, Arg, hipe_rtl:mk_imm(-(?TAG_PRIMARY_LIST))).

unsafe_cdr(Dst, Arg) ->
    hipe_rtl:mk_load(Dst, Arg, hipe_rtl:mk_imm(-(?TAG_PRIMARY_LIST)+4)).

unsafe_constant_element(Dst, Index, Tuple) ->	% Index is an immediate
    Offset = -(?TAG_PRIMARY_BOXED) + 4 * hipe_rtl:imm_value(Index),
    hipe_rtl:mk_load(Dst, Tuple, hipe_rtl:mk_imm(Offset)).

unsafe_update_element(Tuple, Index, Value) ->   % Index is an immediate
    Offset = -(?TAG_PRIMARY_BOXED) + 4 * hipe_rtl:imm_value(Index),
    hipe_rtl:mk_store(Tuple, hipe_rtl:mk_imm(Offset), Value).

%%% wrong semantics
% unsafe_variable_element(Dst, Index, Tuple) ->	% Index is an unknown fixnum
%     %% Load word at (Tuple - 2) + ((Index >> 4) << 2).
%     %% Offset = ((Index >> 4) << 2) - 2.
%     %% Index = x..x1111 (fixnum tag is 2#1111).
%     %% (Index >> 2) = 00x..x11 and ((Index >> 4) << 2) = 00x..x00.
%     %% Therefore, ((Index >> 4) << 2) = (Index >> 2) - 3.
%     %% So Offset = ((Index >> 4) << 2) - 2 = (Index >> 2) - (3 + 2).
%     Tmp1 = hipe_rtl:mk_new_reg(),
%     Tmp2 = hipe_rtl:mk_new_reg(),
%     Shift = ?TAG_IMMED1_SIZE - 2,
%     OffAdj = (?TAG_IMMED1_SMALL bsr Shift) + ?TAG_PRIMARY_BOXED,
%     [hipe_rtl:mk_alu(Tmp1, Index, 'srl', hipe_rtl:mk_imm(Shift)),
%      hipe_rtl:mk_alu(Tmp2, Tmp1, 'sub', hipe_rtl:mk_imm(OffAdj)),
%      hipe_rtl:mk_load(Dst, Tuple, Tmp2)].

element(Dst, Index, Tuple, FailLab, {tuple, A}, IndexInfo) ->
  FailLabName = hipe_rtl:label_name(FailLab),
  FixnumOkLab = hipe_rtl:mk_new_label(),
  IndexOkLab = hipe_rtl:mk_new_label(),
  Ptr = hipe_rtl:mk_new_reg(),
  UIndex = hipe_rtl:mk_new_reg(),
  Arity = hipe_rtl:mk_imm(A),
  InvIndex = hipe_rtl:mk_new_reg(),
  Offset = hipe_rtl:mk_new_reg(),

  case IndexInfo of
    valid ->
      %% This is no branch, 1 load and 3 alus = 4 instr
      [hipe_rtl:mk_alu(UIndex, Index, 'sra',hipe_rtl:mk_imm(?TAG_IMMED1_SIZE)),
       hipe_rtl:mk_alu(Ptr, Tuple, 'sub', hipe_rtl:mk_imm(?TAG_PRIMARY_BOXED)),
       hipe_rtl:mk_alu(Offset, UIndex, 'sll', hipe_rtl:mk_imm(2)),
       hipe_rtl:mk_load(Dst, Ptr, Offset)];
    fixnums ->
      %% This is 1 branch, 1 load and 4 alus = 6 instr
      [hipe_rtl:mk_alu(UIndex, Index,'sra',hipe_rtl:mk_imm(?TAG_IMMED1_SIZE)),
       hipe_rtl:mk_alu(Ptr, Tuple, 'sub',hipe_rtl:mk_imm(?TAG_PRIMARY_BOXED))|
       gen_element_tail(Dst, Ptr, InvIndex, Arity, Offset, UIndex, 
			FailLabName, IndexOkLab)];
    _ ->
      %% This is 3 branches, 1 load and 5 alus = 9 instr
      [test_fixnum(Index, hipe_rtl:label_name(FixnumOkLab),
		   FailLabName, 0.99),
       FixnumOkLab,
       hipe_rtl:mk_alu(UIndex, Index,'sra',hipe_rtl:mk_imm(?TAG_IMMED1_SIZE)),
       hipe_rtl:mk_alu(Ptr, Tuple, 'sub',hipe_rtl:mk_imm(?TAG_PRIMARY_BOXED))|
       gen_element_tail(Dst, Ptr, InvIndex, Arity, Offset, UIndex, 
			FailLabName, IndexOkLab)]
  end;
element(Dst, Index, Tuple, FailLab, tuple, IndexInfo) ->
  FailLabName = hipe_rtl:label_name(FailLab),
  FixnumOkLab = hipe_rtl:mk_new_label(),
  IndexOkLab = hipe_rtl:mk_new_label(),
  Ptr = hipe_rtl:mk_new_reg(),
  Header = hipe_rtl:mk_new_reg(),
  UIndex = hipe_rtl:mk_new_reg(),
  Arity = hipe_rtl:mk_new_reg(),
  InvIndex = hipe_rtl:mk_new_reg(),
  Offset = hipe_rtl:mk_new_reg(),

  case IndexInfo of
    fixnums ->
      %% This is 1 branch, 2 loads and 5 alus = 8 instr
      [hipe_rtl:mk_alu(Ptr, Tuple, 'sub', hipe_rtl:mk_imm(?TAG_PRIMARY_BOXED)),
       hipe_rtl:mk_load(Header, Ptr, hipe_rtl:mk_imm(0)),
       hipe_rtl:mk_alu(UIndex, Index, 'sra',hipe_rtl:mk_imm(?TAG_IMMED1_SIZE)),
       hipe_rtl:mk_alu(Arity,Header,'srl',hipe_rtl:mk_imm(?HEADER_ARITY_OFFS))|
       gen_element_tail(Dst, Ptr, InvIndex, Arity, Offset, UIndex, 
			FailLabName, IndexOkLab)];
    Num when is_integer(Num) ->
      %% This is 1 branch, 1 load and 3 alus = 5 instr
      [hipe_rtl:mk_alu(Ptr, Tuple, 'sub', hipe_rtl:mk_imm(?TAG_PRIMARY_BOXED))|
       gen_element_tail(Dst, Ptr, InvIndex, hipe_rtl:mk_imm(Num), 
			Offset, UIndex, FailLabName, IndexOkLab)];
    _ ->
      %% This is 2 branches, 2 loads and 6 alus = 10 instr
      [test_fixnum(Index, hipe_rtl:label_name(FixnumOkLab),
		   FailLabName, 0.99),
       FixnumOkLab,      
       hipe_rtl:mk_alu(Ptr, Tuple, 'sub', hipe_rtl:mk_imm(?TAG_PRIMARY_BOXED)),
       hipe_rtl:mk_load(Header, Ptr, hipe_rtl:mk_imm(0)),
       hipe_rtl:mk_alu(UIndex, Index, 'sra',hipe_rtl:mk_imm(?TAG_IMMED1_SIZE)),
       hipe_rtl:mk_alu(Arity,Header,'srl',hipe_rtl:mk_imm(?HEADER_ARITY_OFFS))|
       gen_element_tail(Dst, Ptr, InvIndex, Arity, Offset, UIndex, 
			FailLabName, IndexOkLab)]
  end;
element(Dst, Index, Tuple, FailLab, _TupleInfo, IndexInfo) ->
  FailLabName = hipe_rtl:label_name(FailLab),
  FixnumOkLab = hipe_rtl:mk_new_label(),
  BoxedOkLab = hipe_rtl:mk_new_label(),
  TupleOkLab = hipe_rtl:mk_new_label(),
  IndexOkLab = hipe_rtl:mk_new_label(),
  Ptr = hipe_rtl:mk_new_reg(),
  Header = hipe_rtl:mk_new_reg(),
  Tmp = hipe_rtl:mk_new_reg(),
  UIndex = hipe_rtl:mk_new_reg(),
  Arity = hipe_rtl:mk_new_reg(),
  InvIndex = hipe_rtl:mk_new_reg(),
  
  Offset = hipe_rtl:mk_new_reg(),

  case IndexInfo of
    fixnums ->
      %% This is 3 branches, 2 loads and 5 alus = 10 instr
      [test_is_boxed(Tuple, hipe_rtl:label_name(BoxedOkLab),
		     FailLabName, 0.99),
       BoxedOkLab,
       hipe_rtl:mk_alu(Ptr, Tuple, 'sub', hipe_rtl:mk_imm(?TAG_PRIMARY_BOXED)),
       hipe_rtl:mk_load(Header, Ptr, hipe_rtl:mk_imm(0)),
       hipe_rtl:mk_alub(Tmp, Header, 'and', 
			hipe_rtl:mk_imm(?TAG_HEADER_MASK), 'eq',
			hipe_rtl:label_name(TupleOkLab), FailLabName, 0.99),
       TupleOkLab,
       hipe_rtl:mk_alu(UIndex, Index, 'sra',hipe_rtl:mk_imm(?TAG_IMMED1_SIZE)),
       hipe_rtl:mk_alu(Arity, Header, 'srl',
		       hipe_rtl:mk_imm(?HEADER_ARITY_OFFS))|
       gen_element_tail(Dst, Ptr, InvIndex, Arity, Offset, 
			UIndex, FailLabName, IndexOkLab)];

    Num when is_integer(Num) ->
      %% This is 3 branches, 2 loads and 4 alus = 9 instr
      [test_is_boxed(Tuple, hipe_rtl:label_name(BoxedOkLab),
		     FailLabName, 0.99),
       BoxedOkLab,
       hipe_rtl:mk_alu(Ptr, Tuple, 'sub', hipe_rtl:mk_imm(?TAG_PRIMARY_BOXED)),
       hipe_rtl:mk_load(Header, Ptr, hipe_rtl:mk_imm(0)),
       hipe_rtl:mk_alub(Tmp, Header, 'and', 
			hipe_rtl:mk_imm(?TAG_HEADER_MASK), 'eq',
			hipe_rtl:label_name(TupleOkLab), FailLabName, 0.99),
       TupleOkLab,
       hipe_rtl:mk_alu(Arity, Header, 'srl', 
		       hipe_rtl:mk_imm(?HEADER_ARITY_OFFS))|
       gen_element_tail(Dst, Ptr, InvIndex, Arity, Offset, 
			hipe_rtl:mk_imm(Num), FailLabName, IndexOkLab)];
    _ ->
      %% This is 4 branches, 2 loads, and 6 alus = 12 instr :(
      [test_fixnum(Index, hipe_rtl:label_name(FixnumOkLab),
		   FailLabName, 0.99),
       FixnumOkLab,      
       test_is_boxed(Tuple, hipe_rtl:label_name(BoxedOkLab),
		     FailLabName, 0.99),
       BoxedOkLab,
       hipe_rtl:mk_alu(Ptr, Tuple, 'sub', hipe_rtl:mk_imm(?TAG_PRIMARY_BOXED)),
       hipe_rtl:mk_load(Header, Ptr, hipe_rtl:mk_imm(0)),
       hipe_rtl:mk_alub(Tmp, Header, 'and', 
			hipe_rtl:mk_imm(?TAG_HEADER_MASK), 'eq',
			hipe_rtl:label_name(TupleOkLab), FailLabName, 0.99),
       TupleOkLab,
       hipe_rtl:mk_alu(UIndex, Index, 'sra',hipe_rtl:mk_imm(?TAG_IMMED1_SIZE)),
       hipe_rtl:mk_alu(Arity, Header, 'srl',
		       hipe_rtl:mk_imm(?HEADER_ARITY_OFFS))|
       gen_element_tail(Dst, Ptr, InvIndex, Arity, Offset, UIndex, 
			FailLabName, IndexOkLab)]
  end.

gen_element_tail(Dst, Ptr, InvIndex, Arity, Offset, 
		 UIndex, FailLabName, IndexOkLab)->
  %% now check that 1 <= UIndex <= Arity
  %% if UIndex < 1, then (Arity - UIndex) >= Arity
  %% if UIndex > Arity, then (Arity - UIndex) < 0, which is >=u Arity
  %% otherwise, 0 <= (Arity - UIndex) < Arity
  [hipe_rtl:mk_alu(InvIndex, Arity, 'sub', UIndex),
   hipe_rtl:mk_branch(InvIndex, 'geu', Arity, FailLabName,
		      hipe_rtl:label_name(IndexOkLab), 0.01),
   IndexOkLab,
   hipe_rtl:mk_alu(Offset, UIndex, 'sll', hipe_rtl:mk_imm(2)),
   hipe_rtl:mk_load(Dst, Ptr, Offset)].

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
unsafe_closure_element(Dst, Index, Closure) ->	% Index is an immediate
    Offset = -(?TAG_PRIMARY_BOXED)    %% Untag
    + ?EFT_ENV                        %% Field offset
    + (4 * (hipe_rtl:imm_value(Index)-1)), %% Index from 1 to N hence -1)
    hipe_rtl:mk_load(Dst, Closure, hipe_rtl:mk_imm(Offset)).

mk_fun_header() ->
  hipe_rtl:mk_imm(?HEADER_FUN).

tag_fun(Res, X) ->
    tag_boxed(Res, X).

untag_fun(Res, X) ->
    hipe_rtl:mk_alu(Res, X, 'sub', hipe_rtl:mk_imm(?TAG_PRIMARY_BOXED)).

if_fun_get_arity_and_address(ArityReg, AddressReg, FunP, BadFunLab, Pred) ->
%    EmuAddressPtrReg = hipe_rtl:mk_new_reg(),
%    FEPtrReg = hipe_rtl:mk_new_reg(),
%    ArityReg = hipe_rtl:mk_new_reg(),
%    NumFreeReg = hipe_rtl:mk_new_reg(),
%    RealArityReg = hipe_rtl:mk_new_reg(),
    TrueLab0 = hipe_rtl:mk_new_label(),
%    TrueLab1 = hipe_rtl:mk_new_label(),

    IsFunCode = test_fun(FunP, hipe_rtl:label_name(TrueLab0), BadFunLab, Pred),
    GetArityCode =
    [TrueLab0,
     %% Funp->arity contains the arity
     hipe_rtl:mk_load(ArityReg, FunP,
		      hipe_rtl:mk_imm(-(?TAG_PRIMARY_BOXED)+
				      ?EFT_ARITY)),
     hipe_rtl:mk_load(AddressReg, FunP,
		      hipe_rtl:mk_imm(-(?TAG_PRIMARY_BOXED)+
				      ?EFT_NATIVE_ADDRESS))],
    IsFunCode ++ GetArityCode.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Floating point stuff.
%

unsafe_untag_float(Dst, Src) ->
    %% The tag is 2. Use 2 as offset and we don't have to untag.
    [hipe_rtl:mk_fload(Dst, Src, hipe_rtl:mk_imm(2))].

unsafe_tag_float(Dst, Src, Options) ->
  {GetHPInsn, HP, PutHPInsn} = hipe_rtl_arch:heap_pointer(),
  Head = hipe_rtl:mk_imm(mk_header(2, ?TAG_HEADER_FLOAT)),
  
  Code = [GetHPInsn,
	  hipe_rtl:mk_store(HP, hipe_rtl:mk_imm(0), Head),
	  hipe_rtl:mk_fstore(HP, hipe_rtl:mk_imm(4), Src),
	  tag_flonum(Dst, HP),
	  hipe_rtl:mk_alu(HP, HP, add, hipe_rtl:mk_imm(12)),
	  PutHPInsn],
  case ?AddGC(Options) of
    true -> [hipe_rtl:mk_gctest(3)|Code];
    false -> Code
  end.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Binary stuff
%
finalize_bin(Dst, Base, Offset, TrueLblName) ->
  {GetHPInsn, HP, PutHPInsn} = hipe_rtl_arch:heap_pointer(),
  TmpOffset = hipe_rtl:mk_new_reg(),
  Tmp1 = hipe_rtl:mk_new_reg(),
  Tmp2 = hipe_rtl:mk_new_reg(),
  HeapLbl = hipe_rtl:mk_new_label(),
  REFCLbl = hipe_rtl:mk_new_label(),
  ProcBinHeader = hipe_rtl:mk_imm(mk_header(?PROC_BIN_BYTESIZE-1, ?TAG_HEADER_REFC_BIN)),
  
  [GetHPInsn,
   tag_boxed(Dst, HP),
   hipe_rtl:mk_alu(TmpOffset, Offset, sra, hipe_rtl:mk_imm(3)),
   hipe_rtl:mk_branch(TmpOffset, le, hipe_rtl:mk_imm(?MAX_HEAP_BIN_SIZE), 
		      hipe_rtl:label_name(HeapLbl), hipe_rtl:label_name(REFCLbl)),
   HeapLbl,
   hipe_rtl:mk_alu(Tmp1, HP, add, TmpOffset),
   hipe_rtl:mk_alu(Tmp2, Tmp1, add, hipe_rtl:mk_imm(11)),
   hipe_rtl:mk_alu(HP, Tmp2, 'and', hipe_rtl:mk_imm(16#fffffffc)),
   PutHPInsn,
   hipe_rtl:mk_goto(TrueLblName),
   REFCLbl,
   hipe_rtl:mk_store(HP, hipe_rtl:mk_imm(?PROC_BIN_THING_WORD), ProcBinHeader),
   hipe_rtl:mk_store(HP, hipe_rtl:mk_imm(?PROC_BIN_BINSIZE), TmpOffset),
   heap_arch_spec(HP),
   hipe_rtl:mk_alu(Tmp2, Base, sub, hipe_rtl:mk_imm(?BINARY_ORIG_BYTES)),
   hipe_rtl:mk_store(HP, hipe_rtl:mk_imm(?PROC_BIN_VAL), Tmp2),
   hipe_rtl:mk_store(HP, hipe_rtl:mk_imm(?PROC_BIN_BYTES), Base),
   hipe_rtl:mk_alu(HP, HP, add, hipe_rtl:mk_imm(?PROC_BIN_BYTESIZE*4)),
   PutHPInsn,
   hipe_rtl:mk_goto(TrueLblName)].
-ifdef(HEAP_ARCH_PRIVATE).
heap_arch_spec(HP) ->
  Tmp1 = hipe_rtl:mk_new_reg(),
  [hipe_rtl_arch:pcb_load(Tmp1, ?P_OFF_HEAP_MSO),
   hipe_rtl:mk_store(HP, hipe_rtl:mk_imm(?PROC_BIN_NEXT), Tmp1),
   hipe_rtl_arch:pcb_store(?P_OFF_HEAP_MSO, HP)].
-else.
heap_arch_spec(HP) ->
  Tmp1 = hipe_rtl:mk_new_reg(),
  MSO = hipe_rtl:mk_new_reg(),
  [hipe_rtl:mk_load_address(MSO, erts_global_mso, c_const),
   hipe_rtl:mk_load(Tmp1, MSO, ?OFF_HEAP_MSO),
   hipe_rtl:mk_store(HP, hipe_rtl:mk_imm(?PROC_BIN_NEXT), Tmp1),
   hipe_rtl:mk_store(MSO, ?OFF_HEAP_MSO, HP)].
-endif.

get_base(Base, ByteSize) ->
  {GetHPInsn, HP, PutHPInsn} = hipe_rtl_arch:heap_pointer(), 
  Header = hipe_rtl:mk_new_reg(),
  Tmp1 = hipe_rtl:mk_new_reg(),
  Tmp2 = hipe_rtl:mk_new_reg(),
  EvenWordSize = hipe_rtl:mk_new_reg(),
  [GetHPInsn,
   hipe_rtl:mk_alu(Tmp1, ByteSize, add, hipe_rtl:mk_imm(3)),
   hipe_rtl:mk_alu(EvenWordSize, Tmp1, sra, hipe_rtl:mk_imm(2)),
   hipe_rtl:mk_alu(Tmp2, EvenWordSize, add, hipe_rtl:mk_imm(1)),
   hipe_rtl:mk_alu(Base, HP, add, hipe_rtl:mk_imm(8)),
   mk_var_header(Header, Tmp2, ?TAG_HEADER_HEAP_BIN),
   hipe_rtl:mk_store(HP, hipe_rtl:mk_imm(0), Header),
   hipe_rtl:mk_store(HP, hipe_rtl:mk_imm(4), ByteSize),
   PutHPInsn].

unsafe_mk_sub_binary(Dst, Size, Offs, Orig) -> 
    {GetHPInsn, HP, PutHPInsn} = hipe_rtl_arch:heap_pointer(),
    Head = hipe_rtl:mk_imm(mk_header(2, ?TAG_HEADER_SUB_BIN)),
    [GetHPInsn,
     hipe_rtl:mk_store(HP, hipe_rtl:mk_imm(0), Head),
     hipe_rtl:mk_store(HP, hipe_rtl:mk_imm(4), Size),
     hipe_rtl:mk_store(HP, hipe_rtl:mk_imm(8), Offs),
     hipe_rtl:mk_store(HP, hipe_rtl:mk_imm(12), Orig),
     tag_boxed(Dst, HP),
     hipe_rtl:mk_alu(HP, HP, add, hipe_rtl:mk_imm(16)),
     PutHPInsn].

unsafe_mk_float(Dst, FloatLo, FloatHi) ->
    {GetHPInsn, HP, PutHPInsn} = hipe_rtl_arch:heap_pointer(),
    Head = hipe_rtl:mk_imm(mk_header(2, ?TAG_HEADER_FLOAT)),
    [GetHPInsn,
     hipe_rtl:mk_store(HP, hipe_rtl:mk_imm(0), Head),
     hipe_rtl:mk_store(HP, hipe_rtl:mk_imm(4), FloatLo),
     hipe_rtl:mk_store(HP, hipe_rtl:mk_imm(8), FloatHi),
     tag_boxed(Dst, HP),
     hipe_rtl:mk_alu(HP, HP, add, hipe_rtl:mk_imm(12)),
     PutHPInsn].

unsafe_load_float(Dst1, Dst2, Src) ->
  case get(hipe_target_arch) of
    x86 ->
      [hipe_rtl:mk_load(Dst1, Src, hipe_rtl:mk_imm(2)),
       hipe_rtl:mk_load(Dst2, Src, hipe_rtl:mk_imm(6))];
    ultrasparc ->
      [hipe_rtl:mk_load(Dst2, Src, hipe_rtl:mk_imm(2)),
       hipe_rtl:mk_load(Dst1, Src, hipe_rtl:mk_imm(6))]
  end.



unsafe_mk_big(Dst, Src, Signedness, ultrasparc) ->
  {GetHPInsn, HP, PutHPInsn} = hipe_rtl_arch:heap_pointer(),
  PosHead = hipe_rtl:mk_imm(mk_header(1, ?TAG_HEADER_POS_BIG)),
  NegHead = hipe_rtl:mk_imm(mk_header(1, ?TAG_HEADER_NEG_BIG)),
  PosLabel = hipe_rtl:mk_new_label(),
  NegLabel = hipe_rtl:mk_new_label(),
  JoinLabel = hipe_rtl:mk_new_label(),
  Tmp1 = hipe_rtl:mk_new_reg(),
  [GetHPInsn | case Signedness of
    unsigned ->
      [hipe_rtl:mk_store(HP, hipe_rtl:mk_imm(0), PosHead),
       hipe_rtl:mk_alu(Tmp1, Src, sll, hipe_rtl:mk_imm(16)),
       hipe_rtl:mk_alu(Src, Src, srl, hipe_rtl:mk_imm(16)),
       hipe_rtl:mk_alu(Src, Src, 'or', Tmp1),
       hipe_rtl:mk_store(HP, hipe_rtl:mk_imm(4), Src),
       tag_boxed(Dst, HP),
       hipe_rtl:mk_alu(HP, HP, add, hipe_rtl:mk_imm(8)),
       PutHPInsn];
    signed ->
      [hipe_rtl:mk_alub(Tmp1, Src, 'and', hipe_rtl:mk_imm(1 bsl 31), eq, hipe_rtl:label_name(PosLabel), hipe_rtl:label_name(NegLabel)),
       PosLabel,
       hipe_rtl:mk_store(HP, hipe_rtl:mk_imm(0), PosHead),
       hipe_rtl:mk_goto(hipe_rtl:label_name(JoinLabel)),
       NegLabel,
       hipe_rtl:mk_store(HP, hipe_rtl:mk_imm(0), NegHead),
       JoinLabel,
       hipe_rtl:mk_alu(Tmp1, Src, sll, hipe_rtl:mk_imm(16)),
       hipe_rtl:mk_alu(Src, Src, srl, hipe_rtl:mk_imm(16)),
       hipe_rtl:mk_alu(Src, Src, 'or', Tmp1),
       hipe_rtl:mk_store(HP, hipe_rtl:mk_imm(4), Src),
       tag_boxed(Dst, HP),
       hipe_rtl:mk_alu(HP, HP, add, hipe_rtl:mk_imm(8)),
       PutHPInsn]
  end];
unsafe_mk_big(Dst, Src, Signedness, x86) ->
  {GetHPInsn, HP, PutHPInsn} = hipe_rtl_arch:heap_pointer(),
  PosHead = hipe_rtl:mk_imm(mk_header(1, ?TAG_HEADER_POS_BIG)),
  NegHead = hipe_rtl:mk_imm(mk_header(1, ?TAG_HEADER_NEG_BIG)),
  PosLabel = hipe_rtl:mk_new_label(),
  NegLabel = hipe_rtl:mk_new_label(),
  JoinLabel = hipe_rtl:mk_new_label(),
  Tmp1 = hipe_rtl:mk_new_reg(),
  [GetHPInsn | case Signedness of
    unsigned ->
      [hipe_rtl:mk_store(HP, hipe_rtl:mk_imm(0), PosHead),
       hipe_rtl:mk_store(HP, hipe_rtl:mk_imm(4), Src),
       tag_boxed(Dst, HP),
       hipe_rtl:mk_alu(HP, HP, add, hipe_rtl:mk_imm(8)),
       PutHPInsn];
    signed ->
      [hipe_rtl:mk_alub(Tmp1, Src, 'and', hipe_rtl:mk_imm(1 bsl 31), eq, hipe_rtl:label_name(PosLabel), hipe_rtl:label_name(NegLabel)),
       PosLabel,
       hipe_rtl:mk_store(HP, hipe_rtl:mk_imm(0), PosHead),
       hipe_rtl:mk_goto(hipe_rtl:label_name(JoinLabel)),
       NegLabel,
       hipe_rtl:mk_store(HP, hipe_rtl:mk_imm(0), NegHead),
       JoinLabel,
       hipe_rtl:mk_store(HP, hipe_rtl:mk_imm(4), Src),
       tag_boxed(Dst, HP),
       hipe_rtl:mk_alu(HP, HP, add, hipe_rtl:mk_imm(8)),
       PutHPInsn]
  end].

test_subbinary(Binary, TrueLblName, FalseLblName) ->
  Tmp1 = hipe_rtl:mk_new_reg(),
  Tmp2 = hipe_rtl:mk_new_reg(),
  [hipe_rtl:mk_load(Tmp1, Binary, hipe_rtl:mk_imm(-2)),
   hipe_rtl:mk_alu(Tmp2, Tmp1, 'and', hipe_rtl:mk_imm(?TAG_HEADER_MASK)),
   hipe_rtl:mk_branch(Tmp2, eq, hipe_rtl:mk_imm(?TAG_HEADER_SUB_BIN), TrueLblName, FalseLblName)].

test_heap_binary(Binary, TrueLblName, FalseLblName) ->
  Tmp1 = hipe_rtl:mk_new_reg(),
  Tmp2 = hipe_rtl:mk_new_reg(),
  [hipe_rtl:mk_load(Tmp1, Binary, hipe_rtl:mk_imm(-2)),
   hipe_rtl:mk_alu(Tmp2, Tmp1, 'and', hipe_rtl:mk_imm(?TAG_HEADER_MASK)),
   hipe_rtl:mk_branch(Tmp2, eq, hipe_rtl:mk_imm(?TAG_HEADER_HEAP_BIN), TrueLblName, FalseLblName)].

mk_var_header(Header, Size, Tag) ->
  Tmp = hipe_rtl:mk_new_reg(),
  [hipe_rtl:mk_alu(Tmp, Size, sll, hipe_rtl:mk_imm(?HEADER_ARITY_OFFS)),
   hipe_rtl:mk_alu(Header, Tmp, 'add', hipe_rtl:mk_imm(Tag))].


   
   
   
-------------- next part --------------
/*
 * $Id: hipe_mkliterals.c,v 1.28 2003/07/10 12:53:12 pergu Exp $
 */
#include <stdio.h>
#include <stddef.h>
#include <string.h>
#include <errno.h>
#include <math.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#if HIPE /* hipe_mkliterals is needed even if HIPE is not enabled */
#include "sys.h"
#include "erl_vm.h"
#include "global.h"
#include "erl_process.h"
#include "error.h"
#include "erl_bits.h"
#include "erl_message.h"
/* this sucks, but the loaders need data for all platforms */
#include "hipe_x86_asm.h"
#undef P
#undef HP
#undef NSP
#undef TEMP0
#undef TEMP1
#undef ARG0
#undef ARG1
#undef ARG2
#include "hipe_sparc_registers.h"
#undef P
#undef HP
#undef TEMP0
#undef TEMP1
#undef ARG0
#undef ARG1
#undef ARG2
#include "erl_binary.h"
#endif /* HIPE */

static const unsigned long CRCTABLE[256] = {
    0x00000000L, 0x77073096L, 0xEE0E612CL, 0x990951BAL,
    0x076DC419L, 0x706AF48FL, 0xE963A535L, 0x9E6495A3L,
    0x0EDB8832L, 0x79DCB8A4L, 0xE0D5E91EL, 0x97D2D988L,
    0x09B64C2BL, 0x7EB17CBDL, 0xE7B82D07L, 0x90BF1D91L,
    0x1DB71064L, 0x6AB020F2L, 0xF3B97148L, 0x84BE41DEL,
    0x1ADAD47DL, 0x6DDDE4EBL, 0xF4D4B551L, 0x83D385C7L,
    0x136C9856L, 0x646BA8C0L, 0xFD62F97AL, 0x8A65C9ECL,
    0x14015C4FL, 0x63066CD9L, 0xFA0F3D63L, 0x8D080DF5L,
    0x3B6E20C8L, 0x4C69105EL, 0xD56041E4L, 0xA2677172L,
    0x3C03E4D1L, 0x4B04D447L, 0xD20D85FDL, 0xA50AB56BL,
    0x35B5A8FAL, 0x42B2986CL, 0xDBBBC9D6L, 0xACBCF940L,
    0x32D86CE3L, 0x45DF5C75L, 0xDCD60DCFL, 0xABD13D59L,
    0x26D930ACL, 0x51DE003AL, 0xC8D75180L, 0xBFD06116L,
    0x21B4F4B5L, 0x56B3C423L, 0xCFBA9599L, 0xB8BDA50FL,
    0x2802B89EL, 0x5F058808L, 0xC60CD9B2L, 0xB10BE924L,
    0x2F6F7C87L, 0x58684C11L, 0xC1611DABL, 0xB6662D3DL,
    0x76DC4190L, 0x01DB7106L, 0x98D220BCL, 0xEFD5102AL,
    0x71B18589L, 0x06B6B51FL, 0x9FBFE4A5L, 0xE8B8D433L,
    0x7807C9A2L, 0x0F00F934L, 0x9609A88EL, 0xE10E9818L,
    0x7F6A0DBBL, 0x086D3D2DL, 0x91646C97L, 0xE6635C01L,
    0x6B6B51F4L, 0x1C6C6162L, 0x856530D8L, 0xF262004EL,
    0x6C0695EDL, 0x1B01A57BL, 0x8208F4C1L, 0xF50FC457L,
    0x65B0D9C6L, 0x12B7E950L, 0x8BBEB8EAL, 0xFCB9887CL,
    0x62DD1DDFL, 0x15DA2D49L, 0x8CD37CF3L, 0xFBD44C65L,
    0x4DB26158L, 0x3AB551CEL, 0xA3BC0074L, 0xD4BB30E2L,
    0x4ADFA541L, 0x3DD895D7L, 0xA4D1C46DL, 0xD3D6F4FBL,
    0x4369E96AL, 0x346ED9FCL, 0xAD678846L, 0xDA60B8D0L,
    0x44042D73L, 0x33031DE5L, 0xAA0A4C5FL, 0xDD0D7CC9L,
    0x5005713CL, 0x270241AAL, 0xBE0B1010L, 0xC90C2086L,
    0x5768B525L, 0x206F85B3L, 0xB966D409L, 0xCE61E49FL,
    0x5EDEF90EL, 0x29D9C998L, 0xB0D09822L, 0xC7D7A8B4L,
    0x59B33D17L, 0x2EB40D81L, 0xB7BD5C3BL, 0xC0BA6CADL,
    0xEDB88320L, 0x9ABFB3B6L, 0x03B6E20CL, 0x74B1D29AL,
    0xEAD54739L, 0x9DD277AFL, 0x04DB2615L, 0x73DC1683L,
    0xE3630B12L, 0x94643B84L, 0x0D6D6A3EL, 0x7A6A5AA8L,
    0xE40ECF0BL, 0x9309FF9DL, 0x0A00AE27L, 0x7D079EB1L,
    0xF00F9344L, 0x8708A3D2L, 0x1E01F268L, 0x6906C2FEL,
    0xF762575DL, 0x806567CBL, 0x196C3671L, 0x6E6B06E7L,
    0xFED41B76L, 0x89D32BE0L, 0x10DA7A5AL, 0x67DD4ACCL,
    0xF9B9DF6FL, 0x8EBEEFF9L, 0x17B7BE43L, 0x60B08ED5L,
    0xD6D6A3E8L, 0xA1D1937EL, 0x38D8C2C4L, 0x4FDFF252L,
    0xD1BB67F1L, 0xA6BC5767L, 0x3FB506DDL, 0x48B2364BL,
    0xD80D2BDAL, 0xAF0A1B4CL, 0x36034AF6L, 0x41047A60L,
    0xDF60EFC3L, 0xA867DF55L, 0x316E8EEFL, 0x4669BE79L,
    0xCB61B38CL, 0xBC66831AL, 0x256FD2A0L, 0x5268E236L,
    0xCC0C7795L, 0xBB0B4703L, 0x220216B9L, 0x5505262FL,
    0xC5BA3BBEL, 0xB2BD0B28L, 0x2BB45A92L, 0x5CB36A04L,
    0xC2D7FFA7L, 0xB5D0CF31L, 0x2CD99E8BL, 0x5BDEAE1DL,
    0x9B64C2B0L, 0xEC63F226L, 0x756AA39CL, 0x026D930AL,
    0x9C0906A9L, 0xEB0E363FL, 0x72076785L, 0x05005713L,
    0x95BF4A82L, 0xE2B87A14L, 0x7BB12BAEL, 0x0CB61B38L,
    0x92D28E9BL, 0xE5D5BE0DL, 0x7CDCEFB7L, 0x0BDBDF21L,
    0x86D3D2D4L, 0xF1D4E242L, 0x68DDB3F8L, 0x1FDA836EL,
    0x81BE16CDL, 0xF6B9265BL, 0x6FB077E1L, 0x18B74777L,
    0x88085AE6L, 0xFF0F6A70L, 0x66063BCAL, 0x11010B5CL,
    0x8F659EFFL, 0xF862AE69L, 0x616BFFD3L, 0x166CCF45L,
    0xA00AE278L, 0xD70DD2EEL, 0x4E048354L, 0x3903B3C2L,
    0xA7672661L, 0xD06016F7L, 0x4969474DL, 0x3E6E77DBL,
    0xAED16A4AL, 0xD9D65ADCL, 0x40DF0B66L, 0x37D83BF0L,
    0xA9BCAE53L, 0xDEBB9EC5L, 0x47B2CF7FL, 0x30B5FFE9L,
    0xBDBDF21CL, 0xCABAC28AL, 0x53B39330L, 0x24B4A3A6L,
    0xBAD03605L, 0xCDD70693L, 0x54DE5729L, 0x23D967BFL,
    0xB3667A2EL, 0xC4614AB8L, 0x5D681B02L, 0x2A6F2B94L,
    0xB40BBE37L, 0xC30C8EA1L, 0x5A05DF1BL, 0x2D02EF8DL,
};

static const struct literal {
    const char *name;
    unsigned long value;
} literals[] = {
#if HIPE
    /* Field offsets in a process struct */
    { "P_HP", offsetof(struct process, htop) },
#ifdef SHARED_HEAP
    { "P_HP_LIMIT", offsetof(struct process, hend) },
    { "OFF_HEAP_MSO", offsetof(struct erl_off_heap, mso) },
#else
    { "P_HP_LIMIT", offsetof(struct process, stop) },
    { "P_OFF_HEAP_FUNS", offsetof(struct process, off_heap.funs) },
    { "P_OFF_HEAP_MSO", offsetof(struct process, off_heap.mso) },
#endif
    { "P_ID", offsetof(struct process, id) },
    { "P_FLAGS", offsetof(struct process, flags) },
    { "P_FVALUE", offsetof(struct process, fvalue) },
    { "P_FREASON", offsetof(struct process, freason) },
    { "P_FCALLS", offsetof(struct process, fcalls) },
    { "P_BEAM_IP", offsetof(struct process, i) },
    { "P_ARITY", offsetof(struct process, arity) },
    { "P_ARG0", offsetof(struct process, def_arg_reg[0]) },
    { "P_ARG1", offsetof(struct process, def_arg_reg[1]) },
    { "P_ARG2", offsetof(struct process, def_arg_reg[2]) },
    { "P_ARG3", offsetof(struct process, def_arg_reg[3]) },
    { "P_ARG4", offsetof(struct process, def_arg_reg[4]) },
#if defined(__sparc__)
    { "P_ARG5", offsetof(struct process, def_arg_reg[5]) },
    { "P_ARG6", offsetof(struct process, def_arg_reg[6]) },
    { "P_ARG7", offsetof(struct process, def_arg_reg[7]) },
    { "P_ARG8", offsetof(struct process, def_arg_reg[8]) },
    { "P_ARG9", offsetof(struct process, def_arg_reg[9]) },
    { "P_ARG10", offsetof(struct process, def_arg_reg[10]) },
    { "P_ARG11", offsetof(struct process, def_arg_reg[11]) },
    { "P_ARG12", offsetof(struct process, def_arg_reg[12]) },
    { "P_ARG13", offsetof(struct process, def_arg_reg[13]) },
    { "P_ARG14", offsetof(struct process, def_arg_reg[14]) },
    { "P_ARG15", offsetof(struct process, def_arg_reg[15]) },
#endif
    { "P_NSP", offsetof(struct process, hipe.nsp) },
    { "P_NCALLEE", offsetof(struct process, hipe.ncallee) },
    { "P_CLOSURE", offsetof(struct process, hipe.closure) },
#if defined(__sparc__)
    { "P_NSP_LIMIT", offsetof(struct process, hipe.nstend) },
    { "P_NRA", offsetof(struct process, hipe.nra) },
    { "P_CRA", offsetof(struct process, hipe.ncra) },
#elif defined(__i386__)
    { "P_NSP_LIMIT", offsetof(struct process, hipe.nstack) },
    { "P_CSP", offsetof(struct process, hipe.ncsp) },
    { "P_NARITY", offsetof(struct process, hipe.narity) },
#endif

    /* process flags bits */
    {  "F_TIMO", F_TIMO },

    /* freason codes */
    { "FREASON_THROWN", THROWN },
    { "FREASON_USER_EXIT", USER_EXIT },
    { "FREASON_USER_ERROR", USER_ERROR },
    { "FREASON_USER_ERROR2", USER_ERROR2 },
    { "FREASON_TRAP", TRAP },
    { "FREASON_RESCHEDULE", RESCHEDULE },

    /* special Erlang constants */
    { "ERL_NIL", NIL },
    { "THE_NON_VALUE", THE_NON_VALUE },

    /* funs */
    { "EFE_BUCKET", offsetof(struct erl_fun_entry, bucket) },
    { "EFE_OLD_UNIQ", offsetof(struct erl_fun_entry, old_uniq) },
    { "EFE_OLD_INDEX", offsetof(struct erl_fun_entry, old_index) },
    { "EFE_ADDRESS", offsetof(struct erl_fun_entry, address) },
    { "EFE_NATIVE_ADDRESS", offsetof(struct erl_fun_entry, native_address) },
    { "EFE_MODULE", offsetof(struct erl_fun_entry, module) },
    { "EFE_REFC", offsetof(struct erl_fun_entry, refc) },
    { "EFT_THING", offsetof(struct erl_fun_thing, thing_word) },
#ifndef SHARED_HEAP
    { "EFT_NEXT", offsetof(struct erl_fun_thing, next) },
#endif
    { "EFT_CREATOR", offsetof(struct erl_fun_thing, creator) },
    { "EFT_FE", offsetof(struct erl_fun_thing, fe) },
    { "EFT_NATIVE_ADDRESS", offsetof(struct erl_fun_thing, native_address) },
    { "EFT_ARITY", offsetof(struct erl_fun_thing, arity) },
    { "EFT_NUM_FREE", offsetof(struct erl_fun_thing, num_free) },
    { "EFT_ENV", offsetof(struct erl_fun_thing, env[0]) },
    { "ERL_FUN_SIZE", ERL_FUN_SIZE },

    /* bit syntax */
    { "BSF_ALIGNED", BSF_ALIGNED},
    { "BSF_LITTLE", BSF_LITTLE},
    { "BSF_SIGNED", BSF_SIGNED},
    { "BSF_EXACT", BSF_EXACT},
    { "MB_ORIG", offsetof(struct erl_bin_match_buffer, orig) },
    { "MB_BASE", offsetof(struct erl_bin_match_buffer, base) },
    { "MB_OFFSET", offsetof(struct erl_bin_match_buffer, offset) },
    { "MB_SIZE", offsetof(struct erl_bin_match_buffer, size) },
    { "PROC_BIN_THING_WORD", offsetof(struct proc_bin, thing_word) },
    { "PROC_BIN_BINSIZE", offsetof(struct proc_bin, size) },
    { "PROC_BIN_NEXT", offsetof(struct proc_bin, next) },
    { "PROC_BIN_VAL", offsetof(struct proc_bin, val) },
    { "PROC_BIN_BYTES", offsetof(struct proc_bin, bytes) },
    { "PROC_BIN_BYTESIZE", PROC_BIN_SIZE},
    { "BINARY_ORIG_BYTES", offsetof(struct binary, orig_bytes) },
    { "MAX_HEAP_BIN_SIZE", ERL_ONHEAP_BIN_LIMIT},
    { "OVERHEAD_FACTOR", (BINARY_OVERHEAD_FACTOR*sizeof(Eterm))},

    /* x86 */
    { "X86_NR_ARG_REGS", X86_NR_ARG_REGS },
#if X86_HP_IN_ESI
    { "X86_HP_IN_ESI", 1 },
#endif
#if X86_SIMULATE_NSP
    { "X86_SIMULATE_NSP", 1 },
#endif

    /* SPARC */
    { "HIPE_SPARC_LEAF_WORDS", HIPE_SPARC_LEAF_WORDS },
    { "SPARC_ARGS_IN_REGS", HIPE_SPARC_ARGS_IN_REGS},
    { "SPARC_REG_P", P_NR},
    { "SPARC_REG_NSP", NSP_NR},
    { "SPARC_REG_NSP_LIMIT", NSP_LIMIT_NR},
    { "SPARC_REG_HP", HP_NR},
    { "SPARC_REG_HP_LIMIT", HP_LIMIT_NR},
    { "SPARC_REG_FCALLS", FCALLS_NR},
    { "SPARC_REG_RA", RA_NR},
    { "SPARC_REG_TEMP0", TEMP0_NR},
    { "SPARC_REG_TEMP1", TEMP1_NR},
    { "SPARC_REG_TEMP2", TEMP2_NR},
    { "SPARC_REG_TEMP3", TEMP3_NR},
    { "SPARC_REG_ARG0", ARG0_NR},
    { "SPARC_REG_ARG1", ARG1_NR},
    { "SPARC_REG_ARG2", ARG2_NR},
    { "SPARC_REG_ARG3", ARG3_NR},
    { "SPARC_REG_ARG4", ARG4_NR},
    { "SPARC_REG_ARG5", ARG5_NR},
    { "SPARC_REG_ARG6", ARG6_NR},
    { "SPARC_REG_ARG7", ARG7_NR},
    { "SPARC_REG_ARG8", ARG8_NR},
    { "SPARC_REG_ARG9", ARG9_NR},
    { "SPARC_REG_ARG10", ARG10_NR},
    { "SPARC_REG_ARG11", ARG11_NR},
    { "SPARC_REG_ARG12", ARG12_NR},
    { "SPARC_REG_ARG13", ARG13_NR},
    { "SPARC_REG_ARG14", ARG14_NR},
    { "SPARC_REG_ARG15", ARG15_NR},
#else /* !HIPE, fake minimum set to allow compiling the loaders */
    { "SPARC_ARGS_IN_REGS", 0 },
    { "P_BEAM_IP", 0 },
    { "P_ARITY", 0 },
#endif /* HIPE */

#ifdef SHARED_HEAP
    { "HEAP_ARCH_SHARED", 1 },
#else
    { "HEAP_ARCH_PRIVATE", 1 },
#endif
};
#define NLITERALS ((sizeof literals)/sizeof(literals[0]))

/*
 *  The algorithm for calculating the 32 bit CRC checksum is based upon
 *  documentation and algorithms provided by Dr. Ross N. Williams in the
 *  document "A Painless Guide to CRC Error Detection Algorithms."
 *  This document may be downloaded from
 *  ftp://ftp.rocksoft.com/cliens/rocksoft/papers/crc_v3.txt
 *  as of 12/15/1998. Dr. Williams has placed this document and algorithms
 *  in the public domain.
 */
static unsigned long crc_init(void)
{
    return 0xFFFFFFFF;
}

static unsigned long crc_update(unsigned long crc_value, const void *buf, unsigned int length)
{
    const unsigned char *tab;

    tab = (const unsigned char*)buf;
    for(; length > 0; --length) {
	unsigned char t = (crc_value >> 24) & 0xFF;
	crc_value = (crc_value << 8) | *tab++;
	crc_value ^= CRCTABLE[t];
    }
    return crc_value;
}

static unsigned long literals_crc(void)
{
    unsigned long crc_value;
    unsigned int i;

    crc_value = crc_init();
    for(i = 0; i < NLITERALS; ++i)
	crc_value = crc_update(crc_value, &literals[i].value, sizeof(literals[i].value));
    return crc_value & 0x07FFFFFF;
}

static void c_print1(FILE *fp, const struct literal *literal)
{
    fprintf(fp, "#define %s %lu\n", literal->name, literal->value);
}

static void e_print1(FILE *fp, const struct literal *literal)
{
    fprintf(fp, "-define(%s,%lu).\n", literal->name, literal->value);
}

static void printall(FILE *fp, void (*print1)(FILE*,const struct literal*))
{
    unsigned int i;

    for(i = 0; i < NLITERALS; ++i)
	(*print1)(fp, &literals[i]);
}

static int do_c(FILE *fp)
{
    fprintf(fp, "/* File: hipe_literals.h, generated by hipe_mkliterals */\n");
    fprintf(fp, "#ifndef __HIPE_LITERALS_H__\n");
    fprintf(fp, "#define __HIPE_LITERALS_H__\n\n");
    printall(fp, c_print1);
    fprintf(fp, "#define HIPE_SYSTEM_CRC %luL\n", literals_crc());
    fprintf(fp, "\n#endif\n");
    return 0;
}

static int do_e(FILE *fp)
{
    fprintf(fp, "%% File: hipe_literals.hrl, generated by hipe_mkliterals\n\n");
    printall(fp, e_print1);
    fprintf(fp, "-define(HIPE_SYSTEM_CRC,%lu).\n", literals_crc());
    return 0;
}

int main(int argc, const char **argv)
{
    if( argc > 0 ) {
	if( strcmp(argv[1], "-c") == 0 )
	    return do_c(stdout);
	if( strcmp(argv[1], "-e") == 0 )
	    return do_e(stdout);
    }
    fprintf(stderr, "usage: %s [-c | -e] > output-file\n", argv[0]);
    return 1;
}


More information about the erlang-questions mailing list