summaryrefslogtreecommitdiff
path: root/bcc/loadexp.c
blob: 30f0b50b2bf178f8ca4221891abccebc58ed326b (plain)
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
/* loadexp.c - load expressions for bcc */

/* Copyright (C) 1992 Bruce Evans */

#include "bcc.h"
#include "byteord.h"
#include "gencode.h"
#include "parse.h"
#include "reg.h"
#include "sc.h"
#include "scan.h"
#include "sizes.h"
#include "table.h"
#include "type.h"

PUBLIC value_t constexpression()
{
    struct nodestruct *etmark;
    struct nodestruct *exp;
    struct symstruct *exprmark;
    struct symstruct *symptr;

    etmark = etptr;
    exprmark = exprptr;
    exp = assignment_exp();
    etptr = etmark;
    exprptr = exprmark;
    if (exp->tag == LEAF && (symptr = exp->left.symptr)->storage == CONSTANT
	&& symptr->type->scalar & ISCALAR)
	return symptr->offset.offv;
    error("constant expression required");
    return 1;
}

PUBLIC void initexpression(type)
struct typestruct *type;
{
    struct nodestruct *etmark;
    struct nodestruct *exp;
    struct symstruct *exprmark;
    struct nodestruct *lhs;
    struct symstruct *symptr;
    uoffset_T value;

    if (gvarsymptr->storage != GLOBAL)
	reslocals();
    exprmark = exprptr;
    etmark = etptr;
    exp = assignment_exp();
    if (gvarsymptr->storage != GLOBAL)
    {
	lhs = leafnode(exprsym(gvarsymptr));
	if (!(lhs->nodetype->constructor & (ARRAY | FUNCTION | VOID)))
	    lhs->flags = LVALUE;
	makeleaf(node(ASSIGNOP, lhs, exp));
    }
    else if (exp->tag != LEAF ||
	     ((symptr = exp->left.symptr)->storage != CONSTANT &&
	     (symptr->storage != GLOBAL || symptr->indcount != 0 ||
	      type->scalar & DLONG)) ||
	     (type->constructor | (symptr->type->constructor & ~FUNCTION)) &
	     ~(ARRAY | POINTER))
	error("initializer too complicated");
    else
    {
	if ((symptr->type->scalar | type->scalar) & RSCALAR)
	{
	    /* Can only afford castnode if result known constant. */
	    exp = castnode(type, exp);
	    symptr = exp->left.symptr;
	}
	if (type->scalar & RSCALAR)
	{
	    if (type->scalar & FLOAT)
	    {
		float val;

		val = *symptr->offset.offd;
		deflong(((uoffset_T *) &val)[0]);
	    }
	    else
	    {
		deflong(((uoffset_T *) symptr->offset.offd)[0]);
		deflong(((uoffset_T *) symptr->offset.offd)[1]);
	    }
	    etptr = etmark;	/* XXX - stuff from end of function */
	    exprptr = exprmark;
	    return;
	}
	if (type->typesize == 1)
	    defbyte();
	else if (type->typesize == 2)
	    defword();
#ifdef I8088
	else if (!(type->scalar & DLONG))
	    defdword();
#endif
	switch (symptr->storage)
	{
	case CONSTANT:
	    value = (uoffset_T) symptr->offset.offv;
	    if (type->scalar & DLONG)
	    {
		deflong(value);
		break;
	    }
	    /* XXX - put sizes in type struct to avoid tests */
	    if (type->scalar & CHAR)
		value &= CHMASKTO;
	    else if (type->scalar & SHORT)
		value &= shortmaskto;
	    else if (type->scalar & INT)
		value &= intmaskto;
	    /* XXX - no longmaskto */
	    outnhex(value);
	    break;
	case GLOBAL:
	    if (symptr->flags & LABELLED)
	    {
		outlabel(symptr->name.label);
		outplus();
		outnhex((uoffset_T) symptr->offset.offi);
		break;
	    }
	    if (*symptr->name.namep == 0)	/* constant address */
	    {
		outnhex((uoffset_T) symptr->offset.offi);
		break;
	    }
	    outccname(symptr->name.namep);
	    if (symptr->offset.offi != 0)
	    {
		if (symptr->offset.offi > 0)
		    outplus();
		outshex(symptr->offset.offi);
	    }
	    outnl();
	    break;
	}
    }
    etptr = etmark;
    exprptr = exprmark;
}

PUBLIC struct typestruct *loadexpression(targreg, targtype)
store_pt targreg;
struct typestruct *targtype;
{
    struct nodestruct *etmark;
    struct nodestruct *exp;
    struct symstruct *exprmark;

    etmark = etptr;
    exprmark = exprptr;
    exp = expression();
    if (targtype != NULL)
	exp = castnode(targtype, exp);
    makeleaf(exp);
    if (targtype == NULL)	/* this is for a switch */
    {
	targtype = exp->left.symptr->type;
	if (!(targtype->scalar & (CHAR | INT))
	    && (targtype->scalar & (LONG | DLONG)) != LONG)
	{
	    if (targtype->scalar & SHORT)
		targtype = promote(targtype);
	    else
	    {
		error("non-integral selector in switch");
		targtype = itype;
	    }
	    makeleaf(exp = castnode(targtype, exp));
	}
    }
    load(exp->left.symptr, targreg);	/* resets stack if value was there */
    etptr = etmark;
    exprptr = exprmark;
    return exp->left.symptr->type;
}