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