1989-02-07 11:04:05 +00:00
|
|
|
/*
|
|
|
|
* (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
|
|
|
|
* See the copyright notice in the ACK home directory, in the file "Copyright".
|
|
|
|
*/
|
1994-06-27 08:03:14 +00:00
|
|
|
/* $Id$ */
|
1989-02-07 11:04:05 +00:00
|
|
|
/* C O N V E R S I O N - C O D E G E N E R A T O R */
|
|
|
|
|
2013-05-12 19:45:55 +00:00
|
|
|
#include "parameters.h"
|
1989-02-07 11:04:05 +00:00
|
|
|
#ifndef LINT
|
|
|
|
|
2019-02-18 16:42:15 +00:00
|
|
|
#include "conversion.h"
|
1989-02-07 11:04:05 +00:00
|
|
|
#include <em.h>
|
2019-02-18 16:42:15 +00:00
|
|
|
#include "interface.h"
|
1989-02-07 11:04:05 +00:00
|
|
|
#include "arith.h"
|
|
|
|
#include "type.h"
|
|
|
|
#include "sizes.h"
|
|
|
|
#include "Lpars.h"
|
2019-02-18 16:42:15 +00:00
|
|
|
#include "error.h"
|
|
|
|
|
1989-02-07 11:04:05 +00:00
|
|
|
|
|
|
|
#define T_SIGNED 1
|
|
|
|
#define T_UNSIGNED 2
|
|
|
|
#define T_FLOATING 3
|
|
|
|
|
|
|
|
/* conversion() generates the EM code for a conversion between
|
|
|
|
the types char, short, int, long, float, double and pointer.
|
|
|
|
There are three conversion types: signed, unsigned and floating.
|
|
|
|
The EM code to obtain this conversion looks like:
|
|
|
|
LOC sizeof(from_type)
|
|
|
|
LOC sizeof(to_type)
|
|
|
|
C??
|
|
|
|
*/
|
|
|
|
|
2019-02-18 16:42:15 +00:00
|
|
|
static int convtype(register struct type *);
|
1989-02-07 11:04:05 +00:00
|
|
|
|
2019-02-18 16:42:15 +00:00
|
|
|
void conversion(register struct type *from_type, register struct type *to_type)
|
1989-02-07 11:04:05 +00:00
|
|
|
{
|
|
|
|
register arith from_size = from_type->tp_size;
|
|
|
|
register arith to_size = to_type->tp_size;
|
|
|
|
int from_cnvtype = convtype(from_type);
|
|
|
|
int to_cnvtype = convtype(to_type);
|
|
|
|
|
|
|
|
if ((int)to_size < (int)word_size) to_size = word_size;
|
1991-05-13 10:08:54 +00:00
|
|
|
if ((int)from_size != (int)to_size || from_cnvtype != to_cnvtype) {
|
|
|
|
switch (from_cnvtype) {
|
|
|
|
case T_SIGNED:
|
1989-02-07 11:04:05 +00:00
|
|
|
switch (to_cnvtype) {
|
|
|
|
case T_SIGNED:
|
|
|
|
C_loc(from_size);
|
|
|
|
C_loc(to_size);
|
|
|
|
C_cii();
|
|
|
|
break;
|
|
|
|
case T_UNSIGNED:
|
|
|
|
case T_FLOATING:
|
|
|
|
if ((int)from_size < (int)word_size) {
|
|
|
|
C_loc(from_size);
|
|
|
|
C_loc(word_size);
|
|
|
|
C_cii();
|
|
|
|
from_size = word_size;
|
|
|
|
}
|
1990-07-13 10:18:27 +00:00
|
|
|
/* 3.2.1.2 */
|
|
|
|
if (to_cnvtype == T_UNSIGNED
|
|
|
|
&& (int)from_size < (int)to_size) {
|
|
|
|
C_loc(from_size);
|
|
|
|
C_loc(to_size);
|
|
|
|
C_cii();
|
|
|
|
from_size = to_size;
|
|
|
|
}
|
1989-02-07 11:04:05 +00:00
|
|
|
C_loc(from_size);
|
|
|
|
C_loc(to_size);
|
|
|
|
if (to_cnvtype == T_UNSIGNED) C_ciu();
|
|
|
|
else C_cif();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
1991-05-13 10:08:54 +00:00
|
|
|
case T_UNSIGNED:
|
1989-02-07 11:04:05 +00:00
|
|
|
if ((int)from_size < (int)word_size) from_size = word_size;
|
|
|
|
C_loc(from_size);
|
|
|
|
C_loc(to_size);
|
|
|
|
switch (to_cnvtype) {
|
|
|
|
case T_SIGNED:
|
|
|
|
C_cui();
|
|
|
|
break;
|
|
|
|
case T_UNSIGNED:
|
|
|
|
C_cuu();
|
|
|
|
break;
|
|
|
|
case T_FLOATING:
|
|
|
|
C_cuf();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
1991-05-13 10:08:54 +00:00
|
|
|
case T_FLOATING:
|
1989-02-07 11:04:05 +00:00
|
|
|
C_loc(from_size);
|
|
|
|
C_loc(to_size);
|
|
|
|
switch (to_cnvtype) {
|
|
|
|
case T_SIGNED:
|
|
|
|
C_cfi();
|
|
|
|
break;
|
|
|
|
case T_UNSIGNED:
|
|
|
|
C_cfu();
|
|
|
|
break;
|
|
|
|
case T_FLOATING:
|
|
|
|
C_cff();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
1991-05-13 10:08:54 +00:00
|
|
|
default:
|
1989-02-07 11:04:05 +00:00
|
|
|
crash("(conversion) illegal type conversion");
|
|
|
|
/*NOTREACHED*/
|
1991-05-13 10:08:54 +00:00
|
|
|
}
|
1989-02-07 11:04:05 +00:00
|
|
|
}
|
|
|
|
if ((int)(to_type->tp_size) < (int)word_size
|
|
|
|
&& to_cnvtype != T_FLOATING
|
|
|
|
) {
|
|
|
|
if (to_cnvtype == T_SIGNED) {
|
|
|
|
C_loc(to_type->tp_size);
|
|
|
|
C_loc(word_size);
|
|
|
|
C_cii();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
C_loc((arith) full_mask[(int)(to_type->tp_size)]);
|
|
|
|
C_and(word_size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* convtype() returns in which category a given type falls:
|
|
|
|
signed, unsigned or floating
|
|
|
|
*/
|
2019-02-18 16:42:15 +00:00
|
|
|
static int convtype(register struct type *tp)
|
1989-02-07 11:04:05 +00:00
|
|
|
{
|
|
|
|
switch (tp->tp_fund) {
|
|
|
|
case CHAR:
|
|
|
|
case SHORT:
|
|
|
|
case INT:
|
|
|
|
case ERRONEOUS:
|
|
|
|
case LONG:
|
Begin to add `long long` to C compiler for linux386.
Add long long type, but without literals; you can't say '123LL' yet.
You can try constant operations, like `(long long)123 + 1`, but the
compiler's `arith` type might not be wide enough. Conversions,
shifts, and some other operations don't work in i386 ncg; I am using a
union instead of conversions:
union q {
long long ll;
unsigned long long ull;
int i[2];
};
Hack plat/linux386/descr to enable long long (size 8, alignment 4)
only for this platform. The default for other platforms is to disable
long long (size -1).
In lang/cem/cemcom.ansi,
- BigPars, SmallPars: Add default size, alignment of long long.
- align.h: Add lnglng_align.
- arith.c: Convert arithmetic operands to long long or unsigned long
long when necessary; avoid conversion from long long to long.
Allow long long as an arithmetic, integral, or logical operand.
- ch3.c: Handle long long like int and long when erroneously applying
a selector, like `long long ll; ll.member` or `ll->member`. Add
long long to integral and arithmetic types.
- code.c: Add long long to type stabs for debugging.
- conversion.c: Add long long to integral conversions.
- cstoper.c: Write masks up to full_mask[8]. Add FIXME comment.
- declar.g: Parse `long long` in code.
- decspecs.c: Understand long long in type declarations.
- eval.c: Add long long to operations, to generate code like `adi 8`.
Don't use `ldc` with constant over 4 bytes.
- ival.g: Allow long long in initializations.
- main.c: Set lnglng_type and related values.
- options.c: Add option like `-Vq8.4` to set long long to size 8,
alignment 4. I chose 'q', because Perl's pack and Ruby's
Array#pack use 'q' for 64-bit or long long values; it might be a
reference to BSD's old quad_t alias for long long.
- sizes.h: Add lnglng_size.
- stab.c: Allow long long when writing the type stab for debugging.
Switch from calculating the ranges to hardcoding them in strings;
add 8-byte ranges as a special case. This also hardcodes the
unsigned 4-byte range as "0;-1". Before it was either "0;-1" or
"0;4294967295", depending on sizeof(long) in the compiler.
- struct.c: Try long long bitfield, but it will probably give the
error, "bit field type long long does not fit in a word".
- switch.c: Update comment.
- tokenname.c: Define LNGLNG (long long) like LNGDBL (long double).
- type.c, type.str: Add lnglng_type and ulnglng_type. Add function
no_long_long() to check if long long is disabled.
2019-09-02 15:24:44 +00:00
|
|
|
case LNGLNG:
|
1989-02-07 11:04:05 +00:00
|
|
|
case ENUM:
|
|
|
|
return tp->tp_unsigned ? T_UNSIGNED : T_SIGNED;
|
|
|
|
case FLOAT:
|
|
|
|
case DOUBLE:
|
|
|
|
case LNGDBL:
|
|
|
|
return T_FLOATING;
|
|
|
|
case POINTER:
|
|
|
|
return T_UNSIGNED;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1991-12-17 13:12:22 +00:00
|
|
|
#endif /* LINT */
|
1989-02-07 11:04:05 +00:00
|
|
|
|