1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
|
/* assign.c - assignment and casting operations for bcc */
/* Copyright (C) 1992 Bruce Evans */
#include "bcc.h"
#include "byteord.h"
#include "gencode.h"
#include "reg.h"
#include "sc.h"
#include "sizes.h"
#include "type.h"
FORWARD void blockmove P((struct symstruct *source, struct symstruct *target));
FORWARD void call3 P((char *funcname, struct symstruct *target,
struct symstruct *source, uoffset_T size));
FORWARD void fconvert P((struct symstruct *source, struct typestruct *type));
PUBLIC void assign(source, target)
struct symstruct *source;
struct symstruct *target;
{
store_pt regpushed;
store_pt sourcereg;
scalar_t tscalar;
if (target->type->constructor & (ARRAY | FUNCTION) ||
target->flags == TEMP || target->flags == (LABELLED | STRING) ||
(target->indcount == 0 && target->flags != REGVAR))
{
bugerror("botched lvalue");
return;
}
if (source->storage != target->storage
|| source->indcount != target->indcount
|| source->level != target->level
|| source->offset.offi != target->offset.offi /* kludge union cmp */
|| source->type != target->type
|| ((source->storage & (LOCAL | GLOBAL)
|| source->level == OFFKLUDGELEVEL)
&& ((source->flags ^ target->flags) & LABELLED
|| (source->flags & LABELLED
&& source->name.label != target->name.label)
|| (!(source->flags & LABELLED)
&& strcmp(source->name.namep, target->name.namep) != 0))))
{
tscalar = target->type->scalar;
if (tscalar & CHAR && source->storage == CONSTANT)
{
source->offset.offv &= CHMASKTO;
source->type = target->type;
}
regpushed = preslval(source, target);
if (!(tscalar & CHAR) || source->flags != TEMP ||
source->offset.offi != sp || source->type->typesize > itypesize)
cast(target->type, source);
if (tscalar & RSCALAR)
{
if (source->storage == CONSTANT && (!reguse & doubleregs))
load(source, doubleregs & ~DREG);
if (source->storage != CONSTANT && source->indcount == 0)
{
/* XXX - 386 only */
storereg(DREG, target);
#ifdef I80386
if (i386_32)
{
if (tscalar & DOUBLE)
{
target->indcount = 1; /* XXX outnnadr clobbers this */
target->offset.offi += accregsize;
storereg(doubleregs & ~DREG, target);
}
}
else
#endif
if (tscalar & DOUBLE)
{
int i;
for(i=1; i; i<<=1) if( i!= DREG && (doubleregs & i) )
{
target->indcount = 1; /* XXX outnnadr clobbers this */
target->offset.offi += accregsize;
storereg(i, target);
}
}
#ifdef I8088
else if (tscalar & FLOAT)
{
target->indcount = 1; /* XXX outnnadr clobbers this */
target->offset.offi += accregsize;
storereg(DATREG2, target);
}
#endif
target->storage = source->storage;
target->offset.offi = 0;
}
else if (f_indirect(source) && tscalar & DOUBLE
&& (!(reguse & OPREG) || target->storage == OPREG))
{
struct symstruct temptarg;
temptarg = *target;
pointat(&temptarg);
call("Fpull");
outntypechar(temptarg.type);
sp += dtypesize;
}
else
blockmove(source, target);
}
else if (target->type->constructor & STRUCTU)
blockmove(source, target);
else
{
if (tscalar & CHAR)
load(source, DREG);
else if (target->indcount == 0 && target->storage & regregs)
load(source, target->storage);
else
loadany(source);
if (tscalar & DLONG)
{
storereg(DREG, target);
target->indcount = 1;
target->offset.offi += accregsize;
}
if ((store_t) (sourcereg = source->storage) == DREG &&
target->type->scalar & CHAR)
sourcereg = BREG;
storereg(sourcereg, target);
if ((store_t) regpushed != 0)
{
/* DLONG */
target->indcount = 1;
target->offset.offi -= accregsize;
recovlist(regpushed);
}
else
{
target->storage = sourcereg;
target->offset.offi = 0;
if (target->level == OFFKLUDGELEVEL)
target->level = EXPRLEVEL;
}
}
}
}
/* block move assumes itypesize == accregsize && BREG size == 1 */
PRIVATE void blockmove(source, target)
struct symstruct *source;
struct symstruct *target;
{
struct symstruct oldtarget;
uoffset_T typesize;
struct symstruct worksource;
oldtarget = *target;
if ((typesize = target->type->typesize) >= 8 * itypesize ||
source->indcount + target->indcount != 2)
{
address(source);
address(target);
call3("_memcpy", target, source, typesize);
}
else
{
if (source->level == OFFKLUDGELEVEL)
addoffset(source); /* else kludge is lost and offsets big */
if (target->level == OFFKLUDGELEVEL)
addoffset(target);
worksource = *source;
for (; typesize >= itypesize; typesize -= itypesize)
{
loadreg(source, DREG);
worksource.offset.offi += itypesize;
*source = worksource;
storereg(DREG, target);
target->indcount = 1;
target->offset.offi += accregsize;
}
while (typesize-- != 0)
{
outload();
outregname(BREG);
outopsep();
outadr(source);
worksource.offset.offi += 1;
*source = worksource;
storereg(BREG, target);
target->indcount = 1;
target->offset.offi += 1;
}
}
*target = oldtarget;
}
PRIVATE void call3(funcname, target, source, size)
char *funcname;
struct symstruct *target;
struct symstruct *source;
uoffset_T size;
{
store_pt regpushed;
offset_T spmark;
struct symstruct *length;
pushlist(regpushed = reguse & ~calleemask);
spmark = sp;
length = constsym((value_t) size);
length->type = uitype;
push(length);
push(source);
push(target);
call(funcname);
outnl();
if (regpushed)
{
modstk(spmark);
recovlist(regpushed);
}
}
PUBLIC void cast(type, target)
struct typestruct *type;
struct symstruct *target;
{
scalar_t newscalar;
uoffset_T newsize;
scalar_t oldscalar;
uoffset_T oldsize;
store_pt targreg;
if (type->constructor & (ARRAY | FUNCTION)
|| (type->constructor & STRUCTU && target->type != type))
{
bugerror("botched implicit cast");
return;
}
if (target->type == type)
return;
if (target->type->constructor == ARRAY)
oldsize = ptypesize;
else
oldsize = target->type->typesize;
newscalar = type->scalar;
oldscalar = target->type->scalar;
if ((newsize = type->typesize) == oldsize &&
!((newscalar | oldscalar) & RSCALAR))
;
else if (newsize == ctypesize) /* char only */
{
if (oldscalar & RSCALAR)
fconvert(target, type);
else if (target->indcount == 1)
{
#if DYNAMIC_LONG_ORDER
if (long_big_endian)
#endif
#if DYNAMIC_LONG_ORDER || LONG_BIG_ENDIAN
# if INT_BIG_ENDIAN
target->offset.offi += oldsize - ctypesize;
# else
{
if (oldscalar & DLONG)
target->offset.offi += itypesize; /* discard msword */
}
# endif
#endif
#if DYNAMIC_LONG_ORDER
else
#endif
#if DYNAMIC_LONG_ORDER || LONG_BIG_ENDIAN == 0
# if INT_BIG_ENDIAN
target->offset.offi += ctypesize;
# else
;
# endif
#endif
}
else if (target->storage != CONSTANT)
{
load(target, DREG);
target->storage = BREG;
}
}
else if ((newscalar & (SHORT | INT | LONG) && !(newscalar & DLONG))
|| type->constructor & POINTER)
{
if (oldscalar & RSCALAR)
fconvert(target, type);
else if (oldsize < newsize)
extend(target);
else if (target->indcount == 1)
{
#if DYNAMIC_LONG_ORDER
if (long_big_endian)
#endif
#if DYNAMIC_LONG_ORDER || LONG_BIG_ENDIAN
{
if (oldscalar & DLONG)
target->offset.offi += itypesize; /* discard msword */
}
#endif
}
else
load(target, DREG);
}
else if (newscalar & DLONG)
{
if (oldscalar & RSCALAR)
fconvert(target, type);
else if (target->storage != CONSTANT)
{
extend(target);
load(target, DREG);
target->storage = targreg = getindexreg();
if (oldscalar & UNSIGNED ||
target->type->constructor & (ARRAY | FUNCTION | POINTER))
uitol(targreg);
else
itol(targreg);
}
}
else if (newscalar & RSCALAR)
{
saveopreg(); /* XXX */
if (oldscalar & (ISCALAR | FLOAT))
fpush(target);
if (newscalar & FLOAT)
{
pointat(target);
call("dto");
outntypechar(type);
justpushed(target); /* XXX - sets dtype wrong (harmless) and
* wastes (dtypesize - ftypesize) stack */
}
restoreopreg();
}
target->type = type;
}
/* extend char or short to int (unsigned if from unsigned) */
PUBLIC void extend(target)
struct symstruct *target;
{
scalar_t tscalar;
if ((tscalar = target->type->scalar) & (CHAR | SHORT))
{
if (target->storage != CONSTANT &&
target->type->typesize < itypesize)
{
load(target, DREG);
if (target->type == sctype)
sctoi();
#if defined(I8088) && defined(I80386)
else if (tscalar & SHORT)
{
if (tscalar & UNSIGNED)
ustoi();
else
stoi();
}
#endif
else
ctoi();
target->storage = DREG;
}
if (tscalar & UNSIGNED)
target->type = uitype;
else
target->type = itype;
}
}
PRIVATE void fconvert(source, type)
struct symstruct *source;
struct typestruct *type;
{
offset_T spmark;
pushlist(reguse & OPREG);
spmark = sp;
pointat(source);
if (source->type->scalar & DOUBLE)
call("dto");
else
call("fto");
if (type->scalar & UNSIGNED)
outbyte('u');
outntypechar(type);
if (type->scalar & DLONG)
{
if (reguse & OPREG)
bugerror("loading long into used reg");
source->storage = OPREG;
}
else if (type->scalar & CHAR)
source->storage = BREG;
else
source->storage = DREG;
source->offset.offi =
source->flags =
source->indcount = 0;
modstk(spmark); /* could adjust later (load instead of pop) */
poplist(reguse & OPREG);
}
|