github.com/goccy/go-jit@v0.0.0-20200514131505-ff78d45cf6af/internal/ccall/jit-value.c (about)

     1  /*
     2   * jit-value.c - Functions for manipulating temporary values.
     3   *
     4   * Copyright (C) 2004  Southern Storm Software, Pty Ltd.
     5   *
     6   * This file is part of the libjit library.
     7   *
     8   * The libjit library is free software: you can redistribute it and/or
     9   * modify it under the terms of the GNU Lesser General Public License
    10   * as published by the Free Software Foundation, either version 2.1 of
    11   * the License, or (at your option) any later version.
    12   *
    13   * The libjit library is distributed in the hope that it will be useful,
    14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    16   * Lesser General Public License for more details.
    17   *
    18   * You should have received a copy of the GNU Lesser General Public
    19   * License along with the libjit library.  If not, see
    20   * <http://www.gnu.org/licenses/>.
    21   */
    22  
    23  #include "jit-internal.h"
    24  #include "jit-rules.h"
    25  
    26  /*@
    27  
    28  @cindex jit-value.h
    29  
    30  Values form the backbone of the storage system in @code{libjit}.
    31  Every value in the system, be it a constant, a local variable,
    32  or a temporary result, is represented by an object of type
    33  @code{jit_value_t}.  The JIT then allocates registers or memory
    34  locations to the values as appropriate.
    35  
    36  We will demonstrate how to use values with a simple example of
    37  adding two local variables together and putting the result into a
    38  third local variable.  First, we allocate storage for the
    39  three local variables:
    40  
    41  @example
    42  value1 = jit_value_create(func, jit_type_int);
    43  value2 = jit_value_create(func, jit_type_int);
    44  value3 = jit_value_create(func, jit_type_int);
    45  @end example
    46  
    47  Here, @code{func} is the function that we are building.  To add
    48  @code{value1} and @code{value2} and put the result into @code{value3},
    49  we use the following code:
    50  
    51  @example
    52  temp = jit_insn_add(func, value1, value2);
    53  jit_insn_store(func, value3, temp);
    54  @end example
    55  
    56  The @code{jit_insn_add} function allocates a temporary value
    57  (@code{temp}) and places the result of the addition into it.
    58  The @code{jit_insn_store} function then stores the temporary
    59  result into @code{value3}.
    60  
    61  You might be tempted to think that the above code is inefficient.
    62  Why do we copy the result into a temporary variable first?
    63  Why not put the result directly to @code{value3}?
    64  
    65  Behind the scenes, the JIT will typically optimize @code{temp} away,
    66  resulting in the final code that you expect (i.e. @code{value3 = value1 +
    67  value2}).  It is simply easier to use @code{libjit} if all results
    68  end up in temporary variables first, so that's what we do.
    69  
    70  Using temporary values, it is very easy to convert stack machine
    71  bytecodes into JIT instructions.  Consider the following Java
    72  Virtual Machine bytecode (representing @code{value4 = value1 * value2 +
    73  value3}):
    74  
    75  @example
    76  iload 1
    77  iload 2
    78  imul
    79  iload 3
    80  iadd
    81  istore 4
    82  @end example
    83  
    84  Let us demonstrate how this code would be translated, instruction
    85  by instruction.  We assume that we have a @code{stack} available,
    86  which keeps track of the temporary values in the system.  We also
    87  assume that @code{jit_value_t} objects representing the local variables
    88  are already stored in an array called @code{locals}.
    89  
    90  @noindent
    91  First, we load local variable 1 onto the stack:
    92  
    93  @example
    94  stack[size++] = jit_insn_load(func, locals[1]);
    95  @end example
    96  
    97  @noindent
    98  We repeat this for local variable 2:
    99  
   100  @example
   101  stack[size++] = jit_insn_load(func, locals[2]);
   102  @end example
   103  
   104  @noindent
   105  Now we pop these two values and push their multiplication:
   106  
   107  @example
   108  stack[size - 2] = jit_insn_mul(func, stack[size - 2], stack[size - 1]);
   109  --size;
   110  @end example
   111  
   112  @noindent
   113  Next, we need to push the value of local variable 3 and add it
   114  to the product that we just computed:
   115  
   116  @example
   117  stack[size++] = jit_insn_load(func, locals[3]);
   118  stack[size - 2] = jit_insn_add(func, stack[size - 2], stack[size - 1]);
   119  --size;
   120  @end example
   121  
   122  @noindent
   123  Finally, we store the result into local variable 4:
   124  
   125  @example
   126  jit_insn_store(func, locals[4], stack[--size]);
   127  @end example
   128  
   129  @noindent
   130  Collecting up all of the above code, we get the following:
   131  
   132  @example
   133  stack[size++] = jit_insn_load(func, locals[1]);
   134  stack[size++] = jit_insn_load(func, locals[2]);
   135  stack[size - 2] = jit_insn_mul(func, stack[size - 2], stack[size - 1]);
   136  --size;
   137  stack[size++] = jit_insn_load(func, locals[3]);
   138  stack[size - 2] = jit_insn_add(func, stack[size - 2], stack[size - 1]);
   139  --size;
   140  jit_insn_store(func, locals[4], stack[--size]);
   141  @end example
   142  
   143  The JIT will optimize away most of these temporary results, leaving
   144  the final machine code that you expect.
   145  
   146  If the virtual machine was register-based, then a slightly different
   147  translation strategy would be used.  Consider the following code,
   148  which computes @code{reg4 = reg1 * reg2 + reg3}, with the intermediate
   149  result stored temporarily in @code{reg5}:
   150  
   151  @example
   152  mul reg5, reg1, reg2
   153  add reg4, reg5, reg3
   154  @end example
   155  
   156  You would start by allocating value objects for all of the registers
   157  in your system (with @code{jit_value_create}):
   158  
   159  @example
   160  reg[1] = jit_value_create(func, jit_type_int);
   161  reg[2] = jit_value_create(func, jit_type_int);
   162  reg[3] = jit_value_create(func, jit_type_int);
   163  reg[4] = jit_value_create(func, jit_type_int);
   164  reg[5] = jit_value_create(func, jit_type_int);
   165  @end example
   166  
   167  @noindent
   168  Then, the virtual register machine code is translated as follows:
   169  
   170  @example
   171  temp1 = jit_insn_mul(func, reg[1], reg[2]);
   172  jit_insn_store(reg[5], temp1);
   173  temp2 = jit_insn_add(func, reg[5], reg[3]);
   174  jit_insn_store(reg[4], temp2);
   175  @end example
   176  
   177  Each virtual register machine instruction turns into two @code{libjit}
   178  function calls.  The JIT will normally optimize away the temporary
   179  results.  If the value in @code{reg5} is not used further down the code,
   180  then the JIT may also be able to optimize @code{reg5} away.
   181  
   182  The rest of this section describes the functions that are available
   183  to create and manipulate values.
   184  
   185  @*/
   186  
   187  /*
   188   * Allocate a new value from a function's memory pool.
   189   */
   190  static jit_value_t
   191  alloc_value(jit_function_t func, jit_type_t type)
   192  {
   193  	/* Ensure that we have a builder for this function */
   194  	if(!_jit_function_ensure_builder(func))
   195  	{
   196  		return 0;
   197  	}
   198  
   199  	jit_value_t value = jit_memory_pool_alloc(&func->builder->value_pool, struct _jit_value);
   200  	if(!value)
   201  	{
   202  		return 0;
   203  	}
   204  	value->block = func->builder->current_block;
   205  	value->type = jit_type_copy(type);
   206  	value->reg = -1;
   207  	value->frame_offset = JIT_INVALID_FRAME_OFFSET;
   208  	value->index = -1;
   209  
   210  	return value;
   211  }
   212  
   213  /*@
   214   * @deftypefun jit_value_t jit_value_create (jit_function_t @var{func}, jit_type_t @var{type})
   215   * Create a new value in the context of a function's current block.
   216   * The value initially starts off as a block-specific temporary.
   217   * It will be converted into a function-wide local variable if
   218   * it is ever referenced from a different block.  Returns NULL
   219   * if out of memory.
   220   *
   221   * Note: It isn't possible to refer to global variables directly using
   222   * values.  If you need to access a global variable, then load its
   223   * address into a temporary and use @code{jit_insn_load_relative}
   224   * or @code{jit_insn_store_relative} to manipulate it.  It simplifies
   225   * the JIT if it can assume that all values are local.
   226   * @end deftypefun
   227  @*/
   228  jit_value_t
   229  jit_value_create(jit_function_t func, jit_type_t type)
   230  {
   231  	jit_value_t value = alloc_value(func, type);
   232  	if(!value)
   233  	{
   234  		return 0;
   235  	}
   236  	value->is_temporary = 1;
   237  	if(jit_type_has_tag(type, JIT_TYPETAG_VOLATILE))
   238  	{
   239  		value->is_volatile = 1;
   240  	}
   241  	return value;
   242  }
   243  
   244  /*@
   245   * @deftypefun jit_value_t jit_value_create_nint_constant (jit_function_t @var{func}, jit_type_t @var{type}, jit_nint @var{const_value})
   246   * Create a new native integer constant in the specified function.
   247   * Returns NULL if out of memory.
   248   *
   249   * The @var{type} parameter indicates the actual type of the constant,
   250   * if it happens to be something other than @code{jit_type_nint}.
   251   * For example, the following will create an unsigned byte constant:
   252   *
   253   * @example
   254   * value = jit_value_create_nint_constant(func, jit_type_ubyte, 128);
   255   * @end example
   256   *
   257   * This function can be used to create constants of type @code{jit_type_sbyte},
   258   * @code{jit_type_ubyte}, @code{jit_type_short}, @code{jit_type_ushort},
   259   * @code{jit_type_int}, @code{jit_type_uint}, @code{jit_type_nint},
   260   * @code{jit_type_nuint}, and all pointer types.
   261   * @end deftypefun
   262  @*/
   263  jit_value_t
   264  jit_value_create_nint_constant(jit_function_t func, jit_type_t type, jit_nint const_value)
   265  {
   266  	jit_type_t stripped = 0;
   267  	if(!const_value)
   268  	{
   269  		/* Special cases: see if this is the NULL or zero constant */
   270  		stripped = jit_type_remove_tags(type);
   271  		if(stripped->kind == JIT_TYPE_SIGNATURE
   272  		   || stripped->kind == JIT_TYPE_PTR
   273  		   || stripped->kind == JIT_TYPE_NINT)
   274  		{
   275  			if(func && func->builder && func->builder->null_constant)
   276  			{
   277  				return func->builder->null_constant;
   278  			}
   279  		}
   280  		else if(stripped->kind == JIT_TYPE_INT)
   281  		{
   282  			if(func && func->builder && func->builder->zero_constant)
   283  			{
   284  				return func->builder->zero_constant;
   285  			}
   286  		}
   287  	}
   288  
   289  	jit_value_t value = alloc_value(func, type);
   290  	if(!value)
   291  	{
   292  		return 0;
   293  	}
   294  	value->is_constant = 1;
   295  	value->is_nint_constant = 1;
   296  	value->address = const_value;
   297  
   298  	if(stripped)
   299  	{
   300  		/* Special cases: see if we need to cache this constant for later */
   301  		if(stripped->kind == JIT_TYPE_SIGNATURE
   302  		   || stripped->kind == JIT_TYPE_PTR
   303  		   || stripped->kind == JIT_TYPE_NINT)
   304  		{
   305  			func->builder->null_constant = value;
   306  		}
   307  		else if(stripped->kind == JIT_TYPE_INT)
   308  		{
   309  			func->builder->zero_constant = value;
   310  		}
   311  	}
   312  
   313  	return value;
   314  }
   315  
   316  /*@
   317   * @deftypefun jit_value_t jit_value_create_long_constant (jit_function_t @var{func}, jit_type_t @var{type}, jit_long @var{const_value})
   318   * Create a new 64-bit integer constant in the specified
   319   * function.  This can also be used to create constants of
   320   * type @code{jit_type_ulong}.  Returns NULL if out of memory.
   321   * @end deftypefun
   322  @*/
   323  jit_value_t
   324  jit_value_create_long_constant(jit_function_t func, jit_type_t type, jit_long const_value)
   325  {
   326  	jit_value_t value = alloc_value(func, type);
   327  	if(!value)
   328  	{
   329  		return 0;
   330  	}
   331  	value->is_constant = 1;
   332  #ifdef JIT_NATIVE_INT64
   333  	value->is_nint_constant = 1;
   334  	value->address = (jit_nint) const_value;
   335  #else
   336  	value->address = (jit_nint) jit_malloc(sizeof(jit_long));
   337  	if(!value->address)
   338  	{
   339  		return 0;
   340  	}
   341  	*((jit_long *) value->address) = const_value;
   342  	value->free_address = 1;
   343  #endif
   344  	return value;
   345  }
   346  
   347  /*@
   348   * @deftypefun jit_value_t jit_value_create_float32_constant (jit_function_t @var{func}, jit_type_t @var{type}, jit_float32 @var{const_value})
   349   * Create a new 32-bit floating-point constant in the specified
   350   * function.  Returns NULL if out of memory.
   351   * @end deftypefun
   352  @*/
   353  jit_value_t
   354  jit_value_create_float32_constant(jit_function_t func, jit_type_t type, jit_float32 const_value)
   355  {
   356  	jit_value_t value = alloc_value(func, type);
   357  	if(!value)
   358  	{
   359  		return 0;
   360  	}
   361  	value->is_constant = 1;
   362  	value->address = (jit_nint) jit_malloc(sizeof(jit_float32));
   363  	if(!value->address)
   364  	{
   365  		return 0;
   366  	}
   367  	*((jit_float32 *) value->address) = const_value;
   368  	value->free_address = 1;
   369  	return value;
   370  }
   371  
   372  /*@
   373   * @deftypefun jit_value_t jit_value_create_float64_constant (jit_function_t @var{func}, jit_type_t @var{type}, jit_float64 @var{const_value})
   374   * Create a new 64-bit floating-point constant in the specified
   375   * function.  Returns NULL if out of memory.
   376   * @end deftypefun
   377  @*/
   378  jit_value_t
   379  jit_value_create_float64_constant(jit_function_t func, jit_type_t type, jit_float64 const_value)
   380  {
   381  	jit_value_t value = alloc_value(func, type);
   382  	if(!value)
   383  	{
   384  		return 0;
   385  	}
   386  	value->is_constant = 1;
   387  	value->address = (jit_nint) jit_malloc(sizeof(jit_float64));
   388  	if(!value->address)
   389  	{
   390  		return 0;
   391  	}
   392  	*((jit_float64 *) value->address) = const_value;
   393  	value->free_address = 1;
   394  	return value;
   395  }
   396  
   397  /*@
   398   * @deftypefun jit_value_t jit_value_create_nfloat_constant (jit_function_t @var{func}, jit_type_t @var{type}, jit_nfloat @var{const_value})
   399   * Create a new native floating-point constant in the specified
   400   * function.  Returns NULL if out of memory.
   401   * @end deftypefun
   402  @*/
   403  jit_value_t
   404  jit_value_create_nfloat_constant(jit_function_t func, jit_type_t type, jit_nfloat const_value)
   405  {
   406  	jit_value_t value = alloc_value(func, type);
   407  	if(!value)
   408  	{
   409  		return 0;
   410  	}
   411  	value->is_constant = 1;
   412  	value->address = (jit_nint) jit_malloc(sizeof(jit_nfloat));
   413  	if(!value->address)
   414  	{
   415  		return 0;
   416  	}
   417  	*((jit_nfloat *) value->address) = const_value;
   418  	value->free_address = 1;
   419  	return value;
   420  }
   421  
   422  /*@
   423   * @deftypefun jit_value_t jit_value_create_constant (jit_function_t @var{func}, const jit_constant_t *@var{const_value})
   424   * Create a new constant from a generic constant structure in the specified
   425   * function.  Returns NULL if out of memory or if the type in
   426   * @var{const_value} is not suitable for a constant.
   427   * @end deftypefun
   428  @*/
   429  jit_value_t
   430  jit_value_create_constant(jit_function_t func, const jit_constant_t *const_value)
   431  {
   432  	jit_type_t stripped = jit_type_remove_tags(const_value->type);
   433  	if(!stripped)
   434  	{
   435  		return 0;
   436  	}
   437  	switch(stripped->kind)
   438  	{
   439  	case JIT_TYPE_SBYTE:
   440  	case JIT_TYPE_UBYTE:
   441  	case JIT_TYPE_SHORT:
   442  	case JIT_TYPE_USHORT:
   443  	case JIT_TYPE_INT:
   444  	case JIT_TYPE_UINT:
   445  		return jit_value_create_nint_constant(func, const_value->type,
   446  						      const_value->un.int_value);
   447  
   448  	case JIT_TYPE_NINT:
   449  	case JIT_TYPE_NUINT:
   450  	case JIT_TYPE_PTR:
   451  	case JIT_TYPE_SIGNATURE:
   452  		return jit_value_create_nint_constant(func, const_value->type,
   453  						      const_value->un.nint_value);
   454  
   455  	case JIT_TYPE_LONG:
   456  	case JIT_TYPE_ULONG:
   457  		return jit_value_create_long_constant(func, const_value->type,
   458  						      const_value->un.long_value);
   459  
   460  	case JIT_TYPE_FLOAT32:
   461  		return jit_value_create_float32_constant(func, const_value->type,
   462  							 const_value->un.float32_value);
   463  
   464  	case JIT_TYPE_FLOAT64:
   465  		return jit_value_create_float64_constant(func, const_value->type,
   466  							 const_value->un.float64_value);
   467  
   468  	case JIT_TYPE_NFLOAT:
   469  		return jit_value_create_nfloat_constant(func, const_value->type,
   470  							const_value->un.nfloat_value);
   471  	}
   472  	return 0;
   473  }
   474  
   475  /*@
   476   * @deftypefun jit_value_t jit_value_get_param (jit_function_t @var{func}, unsigned int @var{param})
   477   * Get the value that corresponds to a specified function parameter.
   478   * Returns NULL if out of memory or @var{param} is invalid.
   479   * @end deftypefun
   480  @*/
   481  jit_value_t
   482  jit_value_get_param(jit_function_t func, unsigned int param)
   483  {
   484  	unsigned int num_params, current;
   485  
   486  	/* Ensure that we have a builder for this function */
   487  	if(!_jit_function_ensure_builder(func))
   488  	{
   489  		return 0;
   490  	}
   491  
   492  	/* Ensure valid param number. */
   493  	jit_type_t signature = func->signature;
   494  	num_params = jit_type_num_params(signature);
   495  	if(param >= num_params)
   496  	{
   497  		return 0;
   498  	}
   499  
   500  	/* If we have already created the values, then exit immediately */
   501  	jit_value_t *values = func->builder->param_values;
   502  	if(values)
   503  	{
   504  		return values[param];
   505  	}
   506  
   507  	/* Create the values for the first time */
   508  	values = (jit_value_t *) jit_calloc(num_params, sizeof(jit_value_t));
   509  	if(!values)
   510  	{
   511  		return 0;
   512  	}
   513  	func->builder->param_values = values;
   514  	for(current = 0; current < num_params; ++current)
   515  	{
   516  		jit_type_t type = jit_type_get_param(signature, current);
   517  		values[current] = jit_value_create(func, type);
   518  		if(values[current])
   519  		{
   520  			/* The value belongs to the entry block, no matter
   521  			   where it happens to be created */
   522  			values[current]->block = func->builder->entry_block;
   523  			values[current]->is_parameter = 1;
   524  		}
   525  	}
   526  
   527  	/* Return the value block for the desired parameter */
   528  	return values[param];
   529  }
   530  
   531  /*@
   532   * @deftypefun jit_value_t jit_value_get_struct_pointer (jit_function_t @var{func})
   533   * Get the value that contains the structure return pointer for
   534   * a function.  If the function does not have a structure return pointer
   535   * (i.e. structures are returned in registers), then this returns NULL.
   536   * @end deftypefun
   537  @*/
   538  jit_value_t
   539  jit_value_get_struct_pointer(jit_function_t func)
   540  {
   541  	jit_type_t type;
   542  	jit_value_t value;
   543  
   544  	/* Ensure that we have a builder for this function */
   545  	if(!_jit_function_ensure_builder(func))
   546  	{
   547  		return 0;
   548  	}
   549  
   550  	type = jit_type_remove_tags(jit_type_get_return(func->signature));
   551  	if(jit_type_is_struct(type) || jit_type_is_union(type))
   552  	{
   553  		if(jit_type_return_via_pointer(type))
   554  		{
   555  			if(!func->builder->struct_return)
   556  			{
   557  				type = jit_type_create_pointer(type, 1);
   558  				if(!type)
   559  				{
   560  					return 0;
   561  				}
   562  				value = jit_value_create(func, type);
   563  				func->builder->struct_return = value;
   564  				if(value)
   565  				{
   566  					/* The value belongs to the entry block, no matter
   567  					   where it happens to be created */
   568  					value->block = func->builder->entry_block;
   569  					value->is_parameter = 1;
   570  				}
   571  				jit_type_free(type);
   572  			}
   573  			return func->builder->struct_return;
   574  		}
   575  	}
   576  	return 0;
   577  }
   578  
   579  /*@
   580   * @deftypefun int jit_value_is_temporary (jit_value_t @var{value})
   581   * Determine if a value is temporary.  i.e. its scope extends
   582   * over a single block within its function.
   583   * @end deftypefun
   584  @*/
   585  int
   586  jit_value_is_temporary(jit_value_t value)
   587  {
   588  	return value->is_temporary;
   589  }
   590  
   591  /*@
   592   * @deftypefun int jit_value_is_local (jit_value_t @var{value})
   593   * Determine if a value is local.  i.e. its scope extends
   594   * over multiple blocks within its function.
   595   * @end deftypefun
   596  @*/
   597  int
   598  jit_value_is_local(jit_value_t value)
   599  {
   600  	return value->is_local;
   601  }
   602  
   603  /*@
   604   * @deftypefun int jit_value_is_constant (jit_value_t @var{value})
   605   * Determine if a value is a constant.
   606   * @end deftypefun
   607  @*/
   608  int
   609  jit_value_is_constant(jit_value_t value)
   610  {
   611  	return value->is_constant;
   612  }
   613  
   614  /*@
   615   * @deftypefun int jit_value_is_parameter (jit_value_t @var{value})
   616   * Determine if a value is a function parameter.
   617   * @end deftypefun
   618  @*/
   619  int
   620  jit_value_is_parameter(jit_value_t value)
   621  {
   622  	return value->is_parameter;
   623  }
   624  
   625  /*@
   626   * @deftypefun void jit_value_ref (jit_function_t @var{func}, jit_value_t @var{value})
   627   * Create a reference to the specified @var{value} from the current
   628   * block in @var{func}.  This will convert a temporary value into
   629   * a local value if @var{value} is being referenced from a different
   630   * block than its original.
   631   *
   632   * It is not necessary that @var{func} be the same function as the
   633   * one where the value was originally created.  It may be a nested
   634   * function, referring to a local variable in its parent function.
   635   * @end deftypefun
   636  @*/
   637  void
   638  jit_value_ref(jit_function_t func, jit_value_t value)
   639  {
   640  	/* Ensure that we have a builder for this function */
   641  	if(!_jit_function_ensure_builder(func))
   642  	{
   643  		return;
   644  	}
   645  
   646  	value->usage_count++;
   647  	if(value->is_temporary)
   648  	{
   649  		if(value->block->func != func)
   650  		{
   651  			/* Reference from a different function: local and addressable */
   652  			value->is_temporary = 0;
   653  			value->is_local = 1;
   654  			value->is_addressable = 1;
   655  
   656  			/* Mark the two functions as not leaves because we will need
   657  			   them to set up proper frame pointers to allow us to access
   658  			   the local variable across the nested function boundary */
   659  			value->block->func->builder->non_leaf = 1;
   660  			func->builder->non_leaf = 1;
   661  		}
   662  		else if(value->block != func->builder->current_block)
   663  		{
   664  			/* Reference from another block in same function: local */
   665  			value->is_temporary = 0;
   666  			value->is_local = 1;
   667  			if(_jit_gen_is_global_candidate(value->type))
   668  			{
   669  				value->global_candidate = 1;
   670  			}
   671  		}
   672  	}
   673  	else if(value->is_local && value->block->func != func)
   674  	{
   675  		/* Convert a previously local value into an addressable one */
   676  		value->is_addressable = 1;
   677  		value->block->func->builder->non_leaf = 1;
   678  		func->builder->non_leaf = 1;
   679  	}
   680  }
   681  
   682  /* TODO: seems to be unused */
   683  void _jit_value_ref_params(jit_function_t func)
   684  {
   685  	unsigned int num_params;
   686  	unsigned int param;
   687  	if(func->builder->param_values)
   688  	{
   689  		num_params = jit_type_num_params(func->signature);
   690  		for(param = 0; param < num_params; ++param)
   691  		{
   692  			jit_value_ref(func, func->builder->param_values[param]);
   693  		}
   694  	}
   695  	jit_value_ref(func, func->builder->struct_return);
   696  	jit_value_ref(func, func->builder->parent_frame);
   697  }
   698  
   699  /*@
   700   * @deftypefun void jit_value_set_volatile (jit_value_t @var{value})
   701   * Set a flag on a value to indicate that it is volatile.  The contents
   702   * of the value must always be reloaded from memory, never from a
   703   * cached register copy.
   704   * @end deftypefun
   705  @*/
   706  void
   707  jit_value_set_volatile(jit_value_t value)
   708  {
   709  	value->is_volatile = 1;
   710  }
   711  
   712  /*@
   713   * @deftypefun int jit_value_is_volatile (jit_value_t @var{value})
   714   * Determine if a value is volatile.
   715   * @end deftypefun
   716  @*/
   717  int
   718  jit_value_is_volatile(jit_value_t value)
   719  {
   720  	return value->is_volatile;
   721  }
   722  
   723  /*@
   724   * @deftypefun void jit_value_set_addressable (jit_value_t @var{value})
   725   * Set a flag on a value to indicate that it is addressable.
   726   * This should be used when you want to take the address of a
   727   * value (e.g. @code{&variable} in C).  The value is guaranteed
   728   * to not be stored in a register across a function call.
   729   * If you refer to a value from a nested function (@code{jit_value_ref}),
   730   * then the value will be automatically marked as addressable.
   731   * @end deftypefun
   732  @*/
   733  void
   734  jit_value_set_addressable(jit_value_t value)
   735  {
   736  	value->is_addressable = 1;
   737  }
   738  
   739  /*@
   740   * @deftypefun int jit_value_is_addressable (jit_value_t @var{value})
   741   * Determine if a value is addressable.
   742   * @end deftypefun
   743  @*/
   744  int
   745  jit_value_is_addressable(jit_value_t value)
   746  {
   747  	return value->is_addressable;
   748  }
   749  
   750  /*@
   751   * @deftypefun jit_type_t jit_value_get_type (jit_value_t @var{value})
   752   * Get the type that is associated with a value.
   753   * @end deftypefun
   754  @*/
   755  jit_type_t
   756  jit_value_get_type(jit_value_t value)
   757  {
   758  	return value->type;
   759  }
   760  
   761  /*@
   762   * @deftypefun jit_function_t jit_value_get_function (jit_value_t @var{value})
   763   * Get the function which owns a particular @var{value}.
   764   * @end deftypefun
   765  @*/
   766  jit_function_t
   767  jit_value_get_function(jit_value_t value)
   768  {
   769  	return value->block->func;
   770  }
   771  
   772  /*@
   773   * @deftypefun jit_block_t jit_value_get_block (jit_value_t @var{value})
   774   * Get the block which owns a particular @var{value}.
   775   * @end deftypefun
   776  @*/
   777  jit_block_t
   778  jit_value_get_block(jit_value_t value)
   779  {
   780  	return value->block;
   781  }
   782  
   783  /*@
   784   * @deftypefun jit_context_t jit_value_get_context (jit_value_t @var{value})
   785   * Get the context which owns a particular @var{value}.
   786   * @end deftypefun
   787  @*/
   788  jit_context_t
   789  jit_value_get_context(jit_value_t value)
   790  {
   791  	return value->block->func->context;
   792  }
   793  
   794  /*@
   795   * @deftypefun jit_constant_t jit_value_get_constant (jit_value_t @var{value})
   796   * Get the constant value within a particular @var{value}.  The returned
   797   * structure's @code{type} field will be @code{jit_type_void} if
   798   * @code{value} is not a constant.
   799   * @end deftypefun
   800  @*/
   801  jit_constant_t
   802  jit_value_get_constant(jit_value_t value)
   803  {
   804  	jit_constant_t result;
   805  	if(!value->is_constant)
   806  	{
   807  		result.type = jit_type_void;
   808  		return result;
   809  	}
   810  	result.type = value->type;
   811  	switch(jit_type_remove_tags(value->type)->kind)
   812  	{
   813  	case JIT_TYPE_SBYTE:
   814  	case JIT_TYPE_UBYTE:
   815  	case JIT_TYPE_SHORT:
   816  	case JIT_TYPE_USHORT:
   817  	case JIT_TYPE_INT:
   818  	case JIT_TYPE_UINT:
   819  		result.un.int_value = (jit_int) value->address;
   820  		break;
   821  
   822  	case JIT_TYPE_NINT:
   823  	case JIT_TYPE_NUINT:
   824  	case JIT_TYPE_PTR:
   825  	case JIT_TYPE_SIGNATURE:
   826  		result.un.nint_value = value->address;
   827  		break;
   828  
   829  	case JIT_TYPE_LONG:
   830  	case JIT_TYPE_ULONG:
   831  #ifdef JIT_NATIVE_INT64
   832  		result.un.long_value = (jit_long) value->address;
   833  #else
   834  		result.un.long_value = *((jit_long *) value->address);
   835  #endif
   836  		break;
   837  
   838  	case JIT_TYPE_FLOAT32:
   839  		result.un.float32_value = *((jit_float32 *) value->address);
   840  		break;
   841  
   842  	case JIT_TYPE_FLOAT64:
   843  		result.un.float64_value = *((jit_float64 *) value->address);
   844  		break;
   845  
   846  	case JIT_TYPE_NFLOAT:
   847  		result.un.nfloat_value = *((jit_nfloat *) value->address);
   848  		break;
   849  
   850  	default:
   851  		result.type = jit_type_void;
   852  		break;
   853  	}
   854  	return result;
   855  }
   856  
   857  /*@
   858   * @deftypefun jit_nint jit_value_get_nint_constant (jit_value_t @var{value})
   859   * Get the constant value within a particular @var{value}, assuming
   860   * that its type is compatible with @code{jit_type_nint}.
   861   * @end deftypefun
   862  @*/
   863  jit_nint
   864  jit_value_get_nint_constant(jit_value_t value)
   865  {
   866  	if(!value->is_nint_constant)
   867  	{
   868  		return 0;
   869  	}
   870  	return (jit_nint) value->address;
   871  }
   872  
   873  /*@
   874   * @deftypefun jit_nint jit_value_get_long_constant (jit_value_t @var{value})
   875   * Get the constant value within a particular @var{value}, assuming
   876   * that its type is compatible with @code{jit_type_long}.
   877   * @end deftypefun
   878  @*/
   879  jit_long
   880  jit_value_get_long_constant(jit_value_t value)
   881  {
   882  	if(!value->is_constant)
   883  	{
   884  		return 0;
   885  	}
   886  	switch(jit_type_normalize(value->type)->kind)
   887  	{
   888  	case JIT_TYPE_LONG:
   889  	case JIT_TYPE_ULONG:
   890  #ifdef JIT_NATIVE_INT64
   891  		return (jit_long) value->address;
   892  #else
   893  		return *((jit_long *) value->address);
   894  #endif
   895  	}
   896  	return 0;
   897  }
   898  
   899  /*@
   900   * @deftypefun jit_float32 jit_value_get_float32_constant (jit_value_t @var{value})
   901   * Get the constant value within a particular @var{value}, assuming
   902   * that its type is compatible with @code{jit_type_float32}.
   903   * @end deftypefun
   904  @*/
   905  jit_float32
   906  jit_value_get_float32_constant(jit_value_t value)
   907  {
   908  	if(!value->is_constant || jit_type_normalize(value->type)->kind != JIT_TYPE_FLOAT32)
   909  	{
   910  		return (jit_float32) 0.0;
   911  	}
   912  	return *((jit_float32 *) value->address);
   913  }
   914  
   915  /*@
   916   * @deftypefun jit_float64 jit_value_get_float64_constant (jit_value_t @var{value})
   917   * Get the constant value within a particular @var{value}, assuming
   918   * that its type is compatible with @code{jit_type_float64}.
   919   * @end deftypefun
   920  @*/
   921  jit_float64
   922  jit_value_get_float64_constant(jit_value_t value)
   923  {
   924  	if(!value->is_constant || jit_type_normalize(value->type)->kind != JIT_TYPE_FLOAT64)
   925  	{
   926  		return (jit_float64) 0.0;
   927  	}
   928  	return *((jit_float64 *) value->address);
   929  }
   930  
   931  /*@
   932   * @deftypefun jit_nfloat jit_value_get_nfloat_constant (jit_value_t @var{value})
   933   * Get the constant value within a particular @var{value}, assuming
   934   * that its type is compatible with @code{jit_type_nfloat}.
   935   * @end deftypefun
   936  @*/
   937  jit_nfloat
   938  jit_value_get_nfloat_constant(jit_value_t value)
   939  {
   940  	if(!value->is_constant || jit_type_normalize(value->type)->kind != JIT_TYPE_NFLOAT)
   941  	{
   942  		return (jit_nfloat) 0.0;
   943  	}
   944  	return *((jit_nfloat *) value->address);
   945  }
   946  
   947  /*@
   948   * @deftypefun int jit_value_is_true (jit_value_t @var{value})
   949   * Determine if @var{value} is constant and non-zero.
   950   * @end deftypefun
   951  @*/
   952  int
   953  jit_value_is_true(jit_value_t value)
   954  {
   955  	if(!value || !value->is_constant)
   956  	{
   957  		return 0;
   958  	}
   959  	if(value->is_nint_constant)
   960  	{
   961  		return (value->address != 0);
   962  	}
   963  	switch(jit_type_remove_tags(value->type)->kind)
   964  	{
   965  	case JIT_TYPE_LONG:
   966  	case JIT_TYPE_ULONG:
   967  		return (jit_value_get_long_constant(value) != 0);
   968  
   969  	case JIT_TYPE_FLOAT32:
   970  		return (jit_value_get_float32_constant(value) != (jit_float32) 0.0);
   971  
   972  	case JIT_TYPE_FLOAT64:
   973  		return (jit_value_get_float64_constant(value) != (jit_float64) 0.0);
   974  
   975  	case JIT_TYPE_NFLOAT:
   976  		return (jit_value_get_nfloat_constant(value) != (jit_nfloat) 0.0);
   977  	}
   978  	return 0;
   979  }
   980  
   981  /*@
   982   * @deftypefun int jit_constant_convert (jit_constant_t *@var{result}, const jit_constant_t *@var{value}, jit_type_t @var{type}, int @var{overflow_check})
   983   * Convert a the constant @var{value} into a new @var{type}, and
   984   * return its value in @var{result}.  Returns zero if the conversion
   985   * is not possible, usually due to overflow.
   986   * @end deftypefun
   987  @*/
   988  int
   989  jit_constant_convert(jit_constant_t *result, const jit_constant_t *value, jit_type_t type,
   990  		     int overflow_check)
   991  {
   992  	jit_type_t srctype;
   993  	jit_type_t desttype;
   994  
   995  	/* Normalize the source and destination types.  The source type
   996  	   is also promoted, to reduce the number of cases in the
   997  	   inner switch statements below */
   998  	srctype = jit_type_promote_int(jit_type_normalize(value->type));
   999  	if(!srctype)
  1000  	{
  1001  		return 0;
  1002  	}
  1003  	desttype = jit_type_normalize(type);
  1004  	if(!desttype)
  1005  	{
  1006  		return 0;
  1007  	}
  1008  
  1009  	/* Determine what kind of conversion to perform */
  1010  	result->type = type;
  1011  	switch(desttype->kind)
  1012  	{
  1013  	case JIT_TYPE_SBYTE:
  1014  		/* Convert to a signed 8-bit integer */
  1015  		switch(srctype->kind)
  1016  		{
  1017  		case JIT_TYPE_INT:
  1018  			if(overflow_check)
  1019  			{
  1020  				if(!jit_int_to_sbyte_ovf(&result->un.int_value,
  1021  							 value->un.int_value))
  1022  				{
  1023  					return 0;
  1024  				}
  1025  			}
  1026  			else
  1027  			{
  1028  				result->un.int_value = jit_int_to_sbyte(value->un.int_value);
  1029  			}
  1030  			break;
  1031  
  1032  		case JIT_TYPE_UINT:
  1033  			if(overflow_check)
  1034  			{
  1035  				if(!jit_uint_to_int_ovf(&result->un.int_value,
  1036  							value->un.uint_value))
  1037  				{
  1038  					return 0;
  1039  				}
  1040  				if(!jit_int_to_sbyte_ovf(&result->un.int_value,
  1041  							 result->un.int_value))
  1042  				{
  1043  					return 0;
  1044  				}
  1045  			}
  1046  			else
  1047  			{
  1048  				result->un.int_value = jit_int_to_sbyte(value->un.int_value);
  1049  			}
  1050  			break;
  1051  
  1052  		case JIT_TYPE_LONG:
  1053  			if(overflow_check)
  1054  			{
  1055  				if(!jit_long_to_int_ovf(&result->un.int_value,
  1056  							value->un.long_value))
  1057  				{
  1058  					return 0;
  1059  				}
  1060  				if(!jit_int_to_sbyte_ovf(&result->un.int_value,
  1061  							 result->un.int_value))
  1062  				{
  1063  					return 0;
  1064  				}
  1065  			}
  1066  			else
  1067  			{
  1068  				result->un.int_value =
  1069  					jit_int_to_sbyte(jit_long_to_int(value->un.long_value));
  1070  			}
  1071  			break;
  1072  
  1073  		case JIT_TYPE_ULONG:
  1074  			if(overflow_check)
  1075  			{
  1076  				if(!jit_ulong_to_int_ovf(&result->un.int_value,
  1077  							 value->un.ulong_value))
  1078  				{
  1079  					return 0;
  1080  				}
  1081  				if(!jit_int_to_sbyte_ovf(&result->un.int_value,
  1082  							 result->un.int_value))
  1083  				{
  1084  					return 0;
  1085  				}
  1086  			}
  1087  			else
  1088  			{
  1089  				result->un.int_value =
  1090  					jit_int_to_sbyte(jit_ulong_to_int(value->un.ulong_value));
  1091  			}
  1092  			break;
  1093  
  1094  		case JIT_TYPE_FLOAT32:
  1095  			if(overflow_check)
  1096  			{
  1097  				if(!jit_float32_to_int_ovf(&result->un.int_value,
  1098  							   value->un.float32_value))
  1099  				{
  1100  					return 0;
  1101  				}
  1102  				if(!jit_int_to_sbyte_ovf(&result->un.int_value,
  1103  							 result->un.int_value))
  1104  				{
  1105  					return 0;
  1106  				}
  1107  			}
  1108  			else
  1109  			{
  1110  				result->un.int_value =
  1111  					jit_int_to_sbyte(jit_float32_to_int(value->un.float32_value));
  1112  			}
  1113  			break;
  1114  
  1115  		case JIT_TYPE_FLOAT64:
  1116  			if(overflow_check)
  1117  			{
  1118  				if(!jit_float64_to_int_ovf(&result->un.int_value,
  1119  							   value->un.float64_value))
  1120  				{
  1121  					return 0;
  1122  				}
  1123  				if(!jit_int_to_sbyte_ovf(&result->un.int_value,
  1124  							 result->un.int_value))
  1125  				{
  1126  					return 0;
  1127  				}
  1128  			}
  1129  			else
  1130  			{
  1131  				result->un.int_value =
  1132  					jit_int_to_sbyte(jit_float64_to_int(value->un.float64_value));
  1133  			}
  1134  			break;
  1135  
  1136  		case JIT_TYPE_NFLOAT:
  1137  			if(overflow_check)
  1138  			{
  1139  				if(!jit_nfloat_to_int_ovf(&result->un.int_value,
  1140  							  value->un.nfloat_value))
  1141  				{
  1142  					return 0;
  1143  				}
  1144  				if(!jit_int_to_sbyte_ovf(&result->un.int_value,
  1145  							 result->un.int_value))
  1146  				{
  1147  					return 0;
  1148  				}
  1149  			}
  1150  			else
  1151  			{
  1152  				result->un.int_value =
  1153  					jit_int_to_sbyte(jit_nfloat_to_int(value->un.nfloat_value));
  1154  			}
  1155  			break;
  1156  
  1157  		default:
  1158  			return 0;
  1159  		}
  1160  		break;
  1161  
  1162  	case JIT_TYPE_UBYTE:
  1163  		/* Convert to an unsigned 8-bit integer */
  1164  		switch(srctype->kind)
  1165  		{
  1166  		case JIT_TYPE_INT:
  1167  			if(overflow_check)
  1168  			{
  1169  				if(!jit_int_to_ubyte_ovf(&result->un.int_value,
  1170  							 value->un.int_value))
  1171  				{
  1172  					return 0;
  1173  				}
  1174  			}
  1175  			else
  1176  			{
  1177  				result->un.int_value = jit_int_to_ubyte(value->un.int_value);
  1178  			}
  1179  			break;
  1180  
  1181  		case JIT_TYPE_UINT:
  1182  			if(overflow_check)
  1183  			{
  1184  				if(!jit_uint_to_int_ovf(&result->un.int_value,
  1185  							value->un.uint_value))
  1186  				{
  1187  					return 0;
  1188  				}
  1189  				if(!jit_int_to_ubyte_ovf(&result->un.int_value,
  1190  							 result->un.int_value))
  1191  				{
  1192  					return 0;
  1193  				}
  1194  			}
  1195  			else
  1196  			{
  1197  				result->un.int_value = jit_int_to_ubyte(value->un.int_value);
  1198  			}
  1199  			break;
  1200  
  1201  		case JIT_TYPE_LONG:
  1202  			if(overflow_check)
  1203  			{
  1204  				if(!jit_long_to_int_ovf(&result->un.int_value,
  1205  							value->un.long_value))
  1206  				{
  1207  					return 0;
  1208  				}
  1209  				if(!jit_int_to_ubyte_ovf(&result->un.int_value,
  1210  							 result->un.int_value))
  1211  				{
  1212  					return 0;
  1213  				}
  1214  			}
  1215  			else
  1216  			{
  1217  				result->un.int_value =
  1218  					jit_int_to_ubyte(jit_long_to_int(value->un.long_value));
  1219  			}
  1220  			break;
  1221  
  1222  		case JIT_TYPE_ULONG:
  1223  			if(overflow_check)
  1224  			{
  1225  				if(!jit_ulong_to_int_ovf(&result->un.int_value,
  1226  							 value->un.ulong_value))
  1227  				{
  1228  					return 0;
  1229  				}
  1230  				if(!jit_int_to_ubyte_ovf(&result->un.int_value,
  1231  							 result->un.int_value))
  1232  				{
  1233  					return 0;
  1234  				}
  1235  			}
  1236  			else
  1237  			{
  1238  				result->un.int_value =
  1239  					jit_int_to_ubyte(jit_ulong_to_int(value->un.ulong_value));
  1240  			}
  1241  			break;
  1242  
  1243  		case JIT_TYPE_FLOAT32:
  1244  			if(overflow_check)
  1245  			{
  1246  				if(!jit_float32_to_int_ovf(&result->un.int_value,
  1247  							   value->un.float32_value))
  1248  				{
  1249  					return 0;
  1250  				}
  1251  				if(!jit_int_to_ubyte_ovf(&result->un.int_value,
  1252  							 result->un.int_value))
  1253  				{
  1254  					return 0;
  1255  				}
  1256  			}
  1257  			else
  1258  			{
  1259  				result->un.int_value =
  1260  					jit_int_to_ubyte(jit_float32_to_int(value->un.float32_value));
  1261  			}
  1262  			break;
  1263  
  1264  		case JIT_TYPE_FLOAT64:
  1265  			if(overflow_check)
  1266  			{
  1267  				if(!jit_float64_to_int_ovf(&result->un.int_value,
  1268  							   value->un.float64_value))
  1269  				{
  1270  					return 0;
  1271  				}
  1272  				if(!jit_int_to_ubyte_ovf(&result->un.int_value,
  1273  							 result->un.int_value))
  1274  				{
  1275  					return 0;
  1276  				}
  1277  			}
  1278  			else
  1279  			{
  1280  				result->un.int_value =
  1281  					jit_int_to_ubyte(jit_float64_to_int(value->un.float64_value));
  1282  			}
  1283  			break;
  1284  
  1285  		case JIT_TYPE_NFLOAT:
  1286  			if(overflow_check)
  1287  			{
  1288  				if(!jit_nfloat_to_int_ovf(&result->un.int_value,
  1289  							  value->un.nfloat_value))
  1290  				{
  1291  					return 0;
  1292  				}
  1293  				if(!jit_int_to_ubyte_ovf(&result->un.int_value,
  1294  							 result->un.int_value))
  1295  				{
  1296  					return 0;
  1297  				}
  1298  			}
  1299  			else
  1300  			{
  1301  				result->un.int_value =
  1302  					jit_int_to_ubyte(jit_nfloat_to_int(value->un.nfloat_value));
  1303  			}
  1304  			break;
  1305  
  1306  		default:
  1307  			return 0;
  1308  		}
  1309  		break;
  1310  
  1311  	case JIT_TYPE_SHORT:
  1312  		/* Convert to a signed 16-bit integer */
  1313  		switch(srctype->kind)
  1314  		{
  1315  		case JIT_TYPE_INT:
  1316  			if(overflow_check)
  1317  			{
  1318  				if(!jit_int_to_short_ovf(&result->un.int_value,
  1319  							 value->un.int_value))
  1320  				{
  1321  					return 0;
  1322  				}
  1323  			}
  1324  			else
  1325  			{
  1326  				result->un.int_value = jit_int_to_short(value->un.int_value);
  1327  			}
  1328  			break;
  1329  
  1330  		case JIT_TYPE_UINT:
  1331  			if(overflow_check)
  1332  			{
  1333  				if(!jit_uint_to_int_ovf(&result->un.int_value,
  1334  							value->un.uint_value))
  1335  				{
  1336  					return 0;
  1337  				}
  1338  				if(!jit_int_to_short_ovf(&result->un.int_value,
  1339  							 result->un.int_value))
  1340  				{
  1341  					return 0;
  1342  				}
  1343  			}
  1344  			else
  1345  			{
  1346  				result->un.int_value = jit_int_to_short(value->un.int_value);
  1347  			}
  1348  			break;
  1349  
  1350  		case JIT_TYPE_LONG:
  1351  			if(overflow_check)
  1352  			{
  1353  				if(!jit_long_to_int_ovf(&result->un.int_value,
  1354  							value->un.long_value))
  1355  				{
  1356  					return 0;
  1357  				}
  1358  				if(!jit_int_to_short_ovf(&result->un.int_value,
  1359  							 result->un.int_value))
  1360  				{
  1361  					return 0;
  1362  				}
  1363  			}
  1364  			else
  1365  			{
  1366  				result->un.int_value =
  1367  					jit_int_to_short(
  1368  						jit_long_to_int(value->un.long_value));
  1369  			}
  1370  			break;
  1371  
  1372  		case JIT_TYPE_ULONG:
  1373  			if(overflow_check)
  1374  			{
  1375  				if(!jit_ulong_to_int_ovf(&result->un.int_value,
  1376  							 value->un.ulong_value))
  1377  				{
  1378  					return 0;
  1379  				}
  1380  				if(!jit_int_to_short_ovf(&result->un.int_value,
  1381  							 result->un.int_value))
  1382  				{
  1383  					return 0;
  1384  				}
  1385  			}
  1386  			else
  1387  			{
  1388  				result->un.int_value =
  1389  					jit_int_to_short(
  1390  						jit_ulong_to_int(value->un.ulong_value));
  1391  			}
  1392  			break;
  1393  
  1394  		case JIT_TYPE_FLOAT32:
  1395  			if(overflow_check)
  1396  			{
  1397  				if(!jit_float32_to_int_ovf(&result->un.int_value,
  1398  							   value->un.float32_value))
  1399  				{
  1400  					return 0;
  1401  				}
  1402  				if(!jit_int_to_short_ovf(&result->un.int_value,
  1403  							 result->un.int_value))
  1404  				{
  1405  					return 0;
  1406  				}
  1407  			}
  1408  			else
  1409  			{
  1410  				result->un.int_value =
  1411  					jit_int_to_short(
  1412  						jit_float32_to_int(value->un.float32_value));
  1413  			}
  1414  			break;
  1415  
  1416  		case JIT_TYPE_FLOAT64:
  1417  			if(overflow_check)
  1418  			{
  1419  				if(!jit_float64_to_int_ovf(&result->un.int_value,
  1420  							   value->un.float64_value))
  1421  				{
  1422  					return 0;
  1423  				}
  1424  				if(!jit_int_to_short_ovf(&result->un.int_value,
  1425  							 result->un.int_value))
  1426  				{
  1427  					return 0;
  1428  				}
  1429  			}
  1430  			else
  1431  			{
  1432  				result->un.int_value =
  1433  					jit_int_to_short(
  1434  						jit_float64_to_int(value->un.float64_value));
  1435  			}
  1436  			break;
  1437  
  1438  		case JIT_TYPE_NFLOAT:
  1439  			if(overflow_check)
  1440  			{
  1441  				if(!jit_nfloat_to_int_ovf(&result->un.int_value,
  1442  							  value->un.nfloat_value))
  1443  				{
  1444  					return 0;
  1445  				}
  1446  				if(!jit_int_to_short_ovf(&result->un.int_value,
  1447  							 result->un.int_value))
  1448  				{
  1449  					return 0;
  1450  				}
  1451  			}
  1452  			else
  1453  			{
  1454  				result->un.int_value =
  1455  					jit_int_to_short(
  1456  						jit_nfloat_to_int(value->un.nfloat_value));
  1457  			}
  1458  			break;
  1459  
  1460  		default:
  1461  			return 0;
  1462  		}
  1463  		break;
  1464  
  1465  	case JIT_TYPE_USHORT:
  1466  		/* Convert to an unsigned 16-bit integer */
  1467  		switch(srctype->kind)
  1468  		{
  1469  		case JIT_TYPE_INT:
  1470  			if(overflow_check)
  1471  			{
  1472  				if(!jit_int_to_ushort_ovf(&result->un.int_value,
  1473  							  value->un.int_value))
  1474  				{
  1475  					return 0;
  1476  				}
  1477  			}
  1478  			else
  1479  			{
  1480  				result->un.int_value = jit_int_to_ushort(value->un.int_value);
  1481  			}
  1482  			break;
  1483  
  1484  		case JIT_TYPE_UINT:
  1485  			if(overflow_check)
  1486  			{
  1487  				if(!jit_uint_to_int_ovf(&result->un.int_value,
  1488  							value->un.uint_value))
  1489  				{
  1490  					return 0;
  1491  				}
  1492  				if(!jit_int_to_ushort_ovf(&result->un.int_value,
  1493  							  result->un.int_value))
  1494  				{
  1495  					return 0;
  1496  				}
  1497  			}
  1498  			else
  1499  			{
  1500  				result->un.int_value = jit_int_to_ushort(value->un.int_value);
  1501  			}
  1502  			break;
  1503  
  1504  		case JIT_TYPE_LONG:
  1505  			if(overflow_check)
  1506  			{
  1507  				if(!jit_long_to_int_ovf(&result->un.int_value,
  1508  							value->un.long_value))
  1509  				{
  1510  					return 0;
  1511  				}
  1512  				if(!jit_int_to_ushort_ovf(&result->un.int_value,
  1513  							  result->un.int_value))
  1514  				{
  1515  					return 0;
  1516  				}
  1517  			}
  1518  			else
  1519  			{
  1520  				result->un.int_value =
  1521  					jit_int_to_ushort(
  1522  						jit_long_to_int(value->un.long_value));
  1523  			}
  1524  			break;
  1525  
  1526  		case JIT_TYPE_ULONG:
  1527  			if(overflow_check)
  1528  			{
  1529  				if(!jit_ulong_to_int_ovf(&result->un.int_value,
  1530  							 value->un.ulong_value))
  1531  				{
  1532  					return 0;
  1533  				}
  1534  				if(!jit_int_to_ushort_ovf(&result->un.int_value,
  1535  							  result->un.int_value))
  1536  				{
  1537  					return 0;
  1538  				}
  1539  			}
  1540  			else
  1541  			{
  1542  				result->un.int_value =
  1543  					jit_int_to_ushort(
  1544  						jit_ulong_to_int(value->un.ulong_value));
  1545  			}
  1546  			break;
  1547  
  1548  		case JIT_TYPE_FLOAT32:
  1549  			if(overflow_check)
  1550  			{
  1551  				if(!jit_float32_to_int_ovf(&result->un.int_value,
  1552  							   value->un.float32_value))
  1553  				{
  1554  					return 0;
  1555  				}
  1556  				if(!jit_int_to_ushort_ovf(&result->un.int_value,
  1557  							  result->un.int_value))
  1558  				{
  1559  					return 0;
  1560  				}
  1561  			}
  1562  			else
  1563  			{
  1564  				result->un.int_value =
  1565  					jit_int_to_ushort(
  1566  						jit_float32_to_int(value->un.float32_value));
  1567  			}
  1568  			break;
  1569  
  1570  		case JIT_TYPE_FLOAT64:
  1571  			if(overflow_check)
  1572  			{
  1573  				if(!jit_float64_to_int_ovf(&result->un.int_value,
  1574  							   value->un.float64_value))
  1575  				{
  1576  					return 0;
  1577  				}
  1578  				if(!jit_int_to_ushort_ovf(&result->un.int_value,
  1579  							  result->un.int_value))
  1580  				{
  1581  					return 0;
  1582  				}
  1583  			}
  1584  			else
  1585  			{
  1586  				result->un.int_value =
  1587  					jit_int_to_ushort(
  1588  						jit_float64_to_int(value->un.float64_value));
  1589  			}
  1590  			break;
  1591  
  1592  		case JIT_TYPE_NFLOAT:
  1593  			if(overflow_check)
  1594  			{
  1595  				if(!jit_nfloat_to_int_ovf(&result->un.int_value,
  1596  							  value->un.nfloat_value))
  1597  				{
  1598  					return 0;
  1599  				}
  1600  				if(!jit_int_to_ushort_ovf(&result->un.int_value,
  1601  							  result->un.int_value))
  1602  				{
  1603  					return 0;
  1604  				}
  1605  			}
  1606  			else
  1607  			{
  1608  				result->un.int_value =
  1609  					jit_int_to_ushort(
  1610  						jit_nfloat_to_int(value->un.nfloat_value));
  1611  			}
  1612  			break;
  1613  
  1614  		default:
  1615  			return 0;
  1616  		}
  1617  		break;
  1618  
  1619  	case JIT_TYPE_INT:
  1620  		/* Convert to a signed 32-bit integer */
  1621  		switch(srctype->kind)
  1622  		{
  1623  		case JIT_TYPE_INT:
  1624  			result->un.int_value = value->un.int_value;
  1625  			break;
  1626  
  1627  		case JIT_TYPE_UINT:
  1628  			if(overflow_check)
  1629  			{
  1630  				if(!jit_uint_to_int_ovf(&result->un.int_value,
  1631  							value->un.uint_value))
  1632  				{
  1633  					return 0;
  1634  				}
  1635  			}
  1636  			else
  1637  			{
  1638  				result->un.int_value = jit_uint_to_int(value->un.uint_value);
  1639  			}
  1640  			break;
  1641  
  1642  		case JIT_TYPE_LONG:
  1643  			if(overflow_check)
  1644  			{
  1645  				if(!jit_long_to_int_ovf(&result->un.int_value,
  1646  							value->un.long_value))
  1647  				{
  1648  					return 0;
  1649  				}
  1650  			}
  1651  			else
  1652  			{
  1653  				result->un.int_value = jit_long_to_int(value->un.long_value);
  1654  			}
  1655  			break;
  1656  
  1657  		case JIT_TYPE_ULONG:
  1658  			if(overflow_check)
  1659  			{
  1660  				if(!jit_ulong_to_int_ovf(&result->un.int_value,
  1661  							 value->un.ulong_value))
  1662  				{
  1663  					return 0;
  1664  				}
  1665  			}
  1666  			else
  1667  			{
  1668  				result->un.int_value = jit_ulong_to_int(value->un.ulong_value);
  1669  			}
  1670  			break;
  1671  
  1672  		case JIT_TYPE_FLOAT32:
  1673  			if(overflow_check)
  1674  			{
  1675  				if(!jit_float32_to_int_ovf(&result->un.int_value,
  1676  							   value->un.float32_value))
  1677  				{
  1678  					return 0;
  1679  				}
  1680  			}
  1681  			else
  1682  			{
  1683  				result->un.int_value = jit_float32_to_int(value->un.float32_value);
  1684  			}
  1685  			break;
  1686  
  1687  		case JIT_TYPE_FLOAT64:
  1688  			if(overflow_check)
  1689  			{
  1690  				if(!jit_float64_to_int_ovf(&result->un.int_value,
  1691  							   value->un.float64_value))
  1692  				{
  1693  					return 0;
  1694  				}
  1695  			}
  1696  			else
  1697  			{
  1698  				result->un.int_value = jit_float64_to_int(value->un.float64_value);
  1699  			}
  1700  			break;
  1701  
  1702  		case JIT_TYPE_NFLOAT:
  1703  			if(overflow_check)
  1704  			{
  1705  				if(!jit_nfloat_to_int_ovf(&result->un.int_value,
  1706  							  value->un.nfloat_value))
  1707  				{
  1708  					return 0;
  1709  				}
  1710  			}
  1711  			else
  1712  			{
  1713  				result->un.int_value = jit_nfloat_to_int(value->un.nfloat_value);
  1714  			}
  1715  			break;
  1716  
  1717  		default:
  1718  			return 0;
  1719  		}
  1720  		break;
  1721  
  1722  	case JIT_TYPE_UINT:
  1723  		/* Convert to an unsigned 32-bit integer */
  1724  		switch(srctype->kind)
  1725  		{
  1726  		case JIT_TYPE_INT:
  1727  			if(overflow_check)
  1728  			{
  1729  				if(!jit_int_to_uint_ovf(&result->un.uint_value,
  1730  							value->un.uint_value))
  1731  				{
  1732  					return 0;
  1733  				}
  1734  			}
  1735  			else
  1736  			{
  1737  				result->un.uint_value = jit_int_to_uint(value->un.int_value);
  1738  			}
  1739  			break;
  1740  
  1741  		case JIT_TYPE_UINT:
  1742  			result->un.uint_value = value->un.uint_value;
  1743  			break;
  1744  
  1745  		case JIT_TYPE_LONG:
  1746  			if(overflow_check)
  1747  			{
  1748  				if(!jit_long_to_uint_ovf(&result->un.uint_value,
  1749  							 value->un.long_value))
  1750  				{
  1751  					return 0;
  1752  				}
  1753  			}
  1754  			else
  1755  			{
  1756  				result->un.uint_value = jit_long_to_uint(value->un.long_value);
  1757  			}
  1758  			break;
  1759  
  1760  		case JIT_TYPE_ULONG:
  1761  			if(overflow_check)
  1762  			{
  1763  				if(!jit_ulong_to_uint_ovf(&result->un.uint_value,
  1764  							  value->un.ulong_value))
  1765  				{
  1766  					return 0;
  1767  				}
  1768  			}
  1769  			else
  1770  			{
  1771  				result->un.uint_value = jit_ulong_to_uint(value->un.ulong_value);
  1772  			}
  1773  			break;
  1774  
  1775  		case JIT_TYPE_FLOAT32:
  1776  			if(overflow_check)
  1777  			{
  1778  				if(!jit_float32_to_uint_ovf(&result->un.uint_value,
  1779  							    value->un.float32_value))
  1780  				{
  1781  					return 0;
  1782  				}
  1783  			}
  1784  			else
  1785  			{
  1786  				result->un.uint_value = jit_float32_to_uint(value->un.float32_value);
  1787  			}
  1788  			break;
  1789  
  1790  		case JIT_TYPE_FLOAT64:
  1791  			if(overflow_check)
  1792  			{
  1793  				if(!jit_float64_to_uint_ovf(&result->un.uint_value,
  1794  							    value->un.float64_value))
  1795  				{
  1796  					return 0;
  1797  				}
  1798  			}
  1799  			else
  1800  			{
  1801  				result->un.uint_value = jit_float64_to_uint(value->un.float64_value);
  1802  			}
  1803  			break;
  1804  
  1805  		case JIT_TYPE_NFLOAT:
  1806  			if(overflow_check)
  1807  			{
  1808  				if(!jit_nfloat_to_uint_ovf(&result->un.uint_value,
  1809  							   value->un.nfloat_value))
  1810  				{
  1811  					return 0;
  1812  				}
  1813  			}
  1814  			else
  1815  			{
  1816  				result->un.uint_value = jit_nfloat_to_uint(value->un.nfloat_value);
  1817  			}
  1818  			break;
  1819  
  1820  		default:
  1821  			return 0;
  1822  		}
  1823  		break;
  1824  
  1825  	case JIT_TYPE_LONG:
  1826  		/* Convert to a signed 64-bit integer */
  1827  		switch(srctype->kind)
  1828  		{
  1829  		case JIT_TYPE_INT:
  1830  			result->un.long_value =
  1831  				jit_int_to_long(value->un.int_value);
  1832  			break;
  1833  
  1834  		case JIT_TYPE_UINT:
  1835  			result->un.long_value =
  1836  				jit_uint_to_long(value->un.int_value);
  1837  			break;
  1838  
  1839  		case JIT_TYPE_LONG:
  1840  			result->un.long_value = value->un.long_value;
  1841  			break;
  1842  
  1843  		case JIT_TYPE_ULONG:
  1844  			if(overflow_check)
  1845  			{
  1846  				if(!jit_ulong_to_long_ovf(&result->un.long_value,
  1847  							  value->un.ulong_value))
  1848  				{
  1849  					return 0;
  1850  				}
  1851  			}
  1852  			else
  1853  			{
  1854  				result->un.long_value = jit_ulong_to_long(value->un.ulong_value);
  1855  			}
  1856  			break;
  1857  
  1858  		case JIT_TYPE_FLOAT32:
  1859  			if(overflow_check)
  1860  			{
  1861  				if(!jit_float32_to_long_ovf(&result->un.long_value,
  1862  							    value->un.float32_value))
  1863  				{
  1864  					return 0;
  1865  				}
  1866  			}
  1867  			else
  1868  			{
  1869  				result->un.long_value = jit_float32_to_long(value->un.float32_value);
  1870  			}
  1871  			break;
  1872  
  1873  		case JIT_TYPE_FLOAT64:
  1874  			if(overflow_check)
  1875  			{
  1876  				if(!jit_float64_to_long_ovf(&result->un.long_value,
  1877  							    value->un.float64_value))
  1878  				{
  1879  					return 0;
  1880  				}
  1881  			}
  1882  			else
  1883  			{
  1884  				result->un.long_value = jit_float64_to_long(value->un.float64_value);
  1885  			}
  1886  			break;
  1887  
  1888  		case JIT_TYPE_NFLOAT:
  1889  			if(overflow_check)
  1890  			{
  1891  				if(!jit_nfloat_to_long_ovf(&result->un.long_value,
  1892  							   value->un.nfloat_value))
  1893  				{
  1894  					return 0;
  1895  				}
  1896  			}
  1897  			else
  1898  			{
  1899  				result->un.long_value = jit_nfloat_to_long(value->un.nfloat_value);
  1900  			}
  1901  			break;
  1902  
  1903  		default:
  1904  			return 0;
  1905  		}
  1906  		break;
  1907  
  1908  	case JIT_TYPE_ULONG:
  1909  		/* Convert to an unsigned 64-bit integer */
  1910  		switch(srctype->kind)
  1911  		{
  1912  		case JIT_TYPE_INT:
  1913  			if(overflow_check)
  1914  			{
  1915  				if(!jit_int_to_ulong_ovf(&result->un.ulong_value,
  1916  							 value->un.int_value))
  1917  				{
  1918  					return 0;
  1919  				}
  1920  			}
  1921  			else
  1922  			{
  1923  				result->un.ulong_value = jit_int_to_ulong(value->un.int_value);
  1924  			}
  1925  			break;
  1926  
  1927  		case JIT_TYPE_UINT:
  1928  			result->un.ulong_value =
  1929  				jit_uint_to_ulong(value->un.uint_value);
  1930  			break;
  1931  
  1932  		case JIT_TYPE_LONG:
  1933  			if(overflow_check)
  1934  			{
  1935  				if(!jit_long_to_ulong_ovf(&result->un.ulong_value,
  1936  							  value->un.long_value))
  1937  				{
  1938  					return 0;
  1939  				}
  1940  			}
  1941  			else
  1942  			{
  1943  				result->un.ulong_value = jit_long_to_ulong(value->un.long_value);
  1944  			}
  1945  			break;
  1946  
  1947  		case JIT_TYPE_ULONG:
  1948  			result->un.ulong_value = value->un.ulong_value;
  1949  			break;
  1950  
  1951  		case JIT_TYPE_FLOAT32:
  1952  			if(overflow_check)
  1953  			{
  1954  				if(!jit_float32_to_ulong_ovf(&result->un.ulong_value,
  1955  							     value->un.float32_value))
  1956  				{
  1957  					return 0;
  1958  				}
  1959  			}
  1960  			else
  1961  			{
  1962  				result->un.ulong_value = jit_float32_to_ulong(value->un.float32_value);
  1963  			}
  1964  			break;
  1965  
  1966  		case JIT_TYPE_FLOAT64:
  1967  			if(overflow_check)
  1968  			{
  1969  				if(!jit_float64_to_ulong_ovf(&result->un.ulong_value,
  1970  							     value->un.float64_value))
  1971  				{
  1972  					return 0;
  1973  				}
  1974  			}
  1975  			else
  1976  			{
  1977  				result->un.ulong_value = jit_float64_to_ulong(value->un.float64_value);
  1978  			}
  1979  			break;
  1980  
  1981  		case JIT_TYPE_NFLOAT:
  1982  			if(overflow_check)
  1983  			{
  1984  				if(!jit_nfloat_to_ulong_ovf(&result->un.ulong_value,
  1985  							    value->un.nfloat_value))
  1986  				{
  1987  					return 0;
  1988  				}
  1989  			}
  1990  			else
  1991  			{
  1992  				result->un.ulong_value = jit_nfloat_to_ulong(value->un.nfloat_value);
  1993  			}
  1994  			break;
  1995  
  1996  		default:
  1997  			return 0;
  1998  		}
  1999  		break;
  2000  
  2001  	case JIT_TYPE_FLOAT32:
  2002  		/* Convert to a 32-bit float */
  2003  		switch(srctype->kind)
  2004  		{
  2005  		case JIT_TYPE_INT:
  2006  			result->un.float32_value = jit_int_to_float32(value->un.int_value);
  2007  			break;
  2008  
  2009  		case JIT_TYPE_UINT:
  2010  			result->un.float32_value = jit_uint_to_float32(value->un.uint_value);
  2011  			break;
  2012  
  2013  		case JIT_TYPE_LONG:
  2014  			result->un.float32_value = jit_long_to_float32(value->un.long_value);
  2015  			break;
  2016  
  2017  		case JIT_TYPE_ULONG:
  2018  			result->un.float32_value = jit_ulong_to_float32(value->un.ulong_value);
  2019  			break;
  2020  
  2021  		case JIT_TYPE_FLOAT32:
  2022  			result->un.float32_value = value->un.float32_value;
  2023  			break;
  2024  
  2025  		case JIT_TYPE_FLOAT64:
  2026  			result->un.float32_value = jit_float64_to_float32(value->un.float64_value);
  2027  			break;
  2028  
  2029  		case JIT_TYPE_NFLOAT:
  2030  			result->un.float32_value = jit_nfloat_to_float32(value->un.nfloat_value);
  2031  			break;
  2032  
  2033  		default:
  2034  			return 0;
  2035  		}
  2036  		break;
  2037  
  2038  	case JIT_TYPE_FLOAT64:
  2039  		/* Convert to a 64-bit float */
  2040  		switch(srctype->kind)
  2041  		{
  2042  		case JIT_TYPE_INT:
  2043  			result->un.float64_value = jit_int_to_float64(value->un.int_value);
  2044  			break;
  2045  
  2046  		case JIT_TYPE_UINT:
  2047  			result->un.float64_value = jit_uint_to_float64(value->un.uint_value);
  2048  			break;
  2049  
  2050  		case JIT_TYPE_LONG:
  2051  			result->un.float64_value = jit_long_to_float64(value->un.long_value);
  2052  			break;
  2053  
  2054  		case JIT_TYPE_ULONG:
  2055  			result->un.float64_value = jit_ulong_to_float64(value->un.ulong_value);
  2056  			break;
  2057  
  2058  		case JIT_TYPE_FLOAT32:
  2059  			result->un.float64_value = jit_float32_to_float64(value->un.float32_value);
  2060  			break;
  2061  
  2062  		case JIT_TYPE_FLOAT64:
  2063  			result->un.float64_value = value->un.float64_value;
  2064  			break;
  2065  
  2066  		case JIT_TYPE_NFLOAT:
  2067  			result->un.float64_value = jit_nfloat_to_float64(value->un.nfloat_value);
  2068  			break;
  2069  
  2070  		default:
  2071  			return 0;
  2072  		}
  2073  		break;
  2074  
  2075  	case JIT_TYPE_NFLOAT:
  2076  		/* Convert to a native float */
  2077  		switch(srctype->kind)
  2078  		{
  2079  		case JIT_TYPE_INT:
  2080  			result->un.nfloat_value = jit_int_to_nfloat(value->un.int_value);
  2081  			break;
  2082  
  2083  		case JIT_TYPE_UINT:
  2084  			result->un.nfloat_value = jit_uint_to_nfloat(value->un.uint_value);
  2085  			break;
  2086  
  2087  		case JIT_TYPE_LONG:
  2088  			result->un.nfloat_value = jit_long_to_nfloat(value->un.long_value);
  2089  			break;
  2090  
  2091  		case JIT_TYPE_ULONG:
  2092  			result->un.nfloat_value = jit_ulong_to_nfloat(value->un.ulong_value);
  2093  			break;
  2094  
  2095  		case JIT_TYPE_FLOAT32:
  2096  			result->un.nfloat_value = jit_float32_to_nfloat(value->un.float32_value);
  2097  			break;
  2098  
  2099  		case JIT_TYPE_FLOAT64:
  2100  			result->un.nfloat_value	= jit_float64_to_nfloat(value->un.float64_value);
  2101  			break;
  2102  
  2103  		case JIT_TYPE_NFLOAT:
  2104  			result->un.nfloat_value = value->un.nfloat_value;
  2105  			break;
  2106  
  2107  		default:
  2108  			return 0;
  2109  		}
  2110  		break;
  2111  
  2112  	default:
  2113  		return 0;
  2114  	}
  2115  	return 1;
  2116  }
  2117  
  2118  void
  2119  _jit_value_free(void *_value)
  2120  {
  2121  	jit_value_t value = (jit_value_t) _value;
  2122  	jit_type_free(value->type);
  2123  	if(value->free_address && value->address)
  2124  	{
  2125  		/* We need to free the memory for a large constant */
  2126  		jit_free((void *) value->address);
  2127  	}
  2128  }