Allow implicit cast from null to maybe types ("Type?")
[delight/core.git] / dmd2 / bit.c
blobb5c8b50ff4293354ef3c3beac2aa980ee696353a
2 // Copyright (c) 1999-2002 by Digital Mars
3 // All Rights Reserved
4 // written by Walter Bright
5 // http://www.digitalmars.com
6 // License for redistribution is by either the Artistic License
7 // in artistic.txt, or the GNU General Public License in gnu.txt.
8 // See the included readme.txt for details.
10 // Routines to convert expressions to elems.
12 #include <stdio.h>
13 #include <string.h>
14 #include <time.h>
16 #include "cc.h"
17 #include "el.h"
18 #include "oper.h"
19 #include "global.h"
20 #include "code.h"
21 #include "type.h"
22 #include "dt.h"
24 static char __file__[] = __FILE__; /* for tassert.h */
25 #include "tassert.h"
27 /**********************************************
28 * Generate code for:
29 * (*eb)[ei] = ev;
30 * ev should already be a bit type.
31 * result:
32 * 0 don't want result
33 * 1 want result in flags
34 * 2 want value of result
37 #if 1
38 #define BIT_SHIFT 3
39 #define BIT_MASK 7
40 #define TYbit TYuchar
41 #else
42 #define BIT_SHIFT 5
43 #define BIT_MASK 31
44 #define TYbit TYuint
45 #endif
47 elem *bit_assign(enum OPER op, elem *eb, elem *ei, elem *ev, int result)
49 #if 1
50 elem *e;
51 elem *es;
52 elem *er;
54 es = el_bin(OPbts, TYbit, eb, ei);
55 er = el_copytree(es);
56 er->Eoper = OPbtr;
57 es = el_bin(OPcomma, TYbit, es, el_long(TYbit, 1));
58 er = el_bin(OPcomma, TYbit, er, el_long(TYbit, 0));
60 e = el_bin(OPcolon, TYvoid, es, er);
61 e = el_bin(OPcond, ev->Ety, ev, e);
62 return e;
63 #else
65 The idea is:
67 *(eb + (ei >> 5)) &= ~(1 << (ei & 31));
68 *(eb + (ei >> 5)) |= ev << (ei & 31);
69 ev;
71 So we generate:
73 et = (eb + (ei >> 5));
74 em = (eit & 31);
75 *ett = (*et & ~(1 << em)) | (ev << em);
76 evt;
79 printf("bit_assign()\n");
81 elem *e;
82 elem *em;
83 elem *eit = el_same(&ei);
84 elem *et;
85 elem *ett;
86 elem *evt = el_same(&ev);
88 ei->Ety = TYuint;
89 et = el_bin(OPshr, TYuint, ei, el_long(TYuint, BIT_SHIFT));
90 et = el_bin(OPadd, TYnptr, eb, et);
91 ett = el_same(&et);
93 eit->Ety = TYbit;
94 em = el_bin(OPand, TYbit, eit, el_long(TYbit, BIT_MASK));
96 e = el_bin(OPshl, TYbit, el_long(TYbit, 1), em);
97 e = el_una(OPcom, TYbit, e);
98 et = el_una(OPind, TYbit, et);
99 e = el_bin(OPand, TYbit, et, e);
101 ev->Ety = TYbit;
102 e = el_bin(OPor, TYbit, e, el_bin(OPshl, TYbit, ev, el_copytree(em)));
103 ett = el_una(OPind, TYbit, ett);
104 e = el_bin(OPeq, TYbit, ett, e);
106 e = el_bin(OPcomma, evt->Ety, e, evt);
107 return e;
108 #endif
111 /**********************************************
112 * Generate code for:
113 * (*eb)[ei]
114 * ev should already be a bit type.
115 * result:
116 * 0 don't want result
117 * 1 want result in flags
118 * 2 want value of result
119 * 3 ?
122 elem *bit_read(elem *eb, elem *ei, int result)
124 #if 1
125 elem *e;
127 e = el_bin(OPbt, TYbit, eb, ei);
128 e = el_bin(OPand, TYbit, e, el_long(TYbit, 1));
129 return e;
130 #else
131 // eb[ei] => (eb[ei >>> 5] >> (ei & 31)) & 1
132 elem *e;
133 elem *eit = el_same(&ei);
135 // Now generate ((*(eb + (ei >>> 5)) >>> (eit & 31)) & 1
137 ei->Ety = TYuint;
138 e = el_bin(OPshr, TYuint, ei, el_long(TYuint, BIT_SHIFT));
139 e = el_bin(OPadd, TYnptr, eb, e);
140 e = el_una(OPind, TYbit, e);
141 eit->Ety = TYbit;
142 eit = el_bin(OPand, TYbit, eit, el_long(TYbit, BIT_MASK));
143 e = el_bin(OPshr, TYbit, e, eit);
144 e = el_bin(OPand, TYbit, e, el_long(TYbit, 1));
146 // BUG: what about return type of e?
147 return e;
148 #endif